Class: Ritm::Certificate

Inherits:
Object
  • Object
show all
Defined in:
lib/ritm/certs/certificate.rb

Overview

Wraps a SSL Certificate via on-the-fly creation or loading from files

Direct Known Subclasses

CA

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cert) ⇒ Certificate

Returns a new instance of Certificate.



28
29
30
# File 'lib/ritm/certs/certificate.rb', line 28

def initialize(cert)
  @cert = cert
end

Instance Attribute Details

#certObject

Returns the value of attribute cert.



6
7
8
# File 'lib/ritm/certs/certificate.rb', line 6

def cert
  @cert
end

Class Method Details

.create(common_name, serial_number: nil) {|cert| ... } ⇒ Object

Yields:



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/ritm/certs/certificate.rb', line 16

def self.create(common_name, serial_number: nil)
  cert = CertificateAuthority::Certificate.new
  cert.subject.common_name = common_name
  cert.subject.organization = cert.subject.organizational_unit = 'RubyInTheMiddle'
  cert.subject.country = 'AR'
  cert.not_before = cert.not_before - 3600 * 24 * 30 # Substract 30 days
  cert.serial_number.number = serial_number || common_name.hash.abs
  cert.key_material.generate_key(1024)
  yield cert if block_given?
  new cert
end

.load(crt, private_key) {|cert| ... } ⇒ Object

Yields:



8
9
10
11
12
13
14
# File 'lib/ritm/certs/certificate.rb', line 8

def self.load(crt, private_key)
  x509 = OpenSSL::X509::Certificate.new(crt)
  cert = CertificateAuthority::Certificate.from_openssl(x509)
  cert.key_material.private_key = OpenSSL::PKey::RSA.new(private_key)
  yield cert if block_given?
  new cert
end

Instance Method Details

#pemObject



40
41
42
# File 'lib/ritm/certs/certificate.rb', line 40

def pem
  @cert.to_pem
end

#private_keyObject



32
33
34
# File 'lib/ritm/certs/certificate.rb', line 32

def private_key
  @cert.key_material.private_key
end

#public_keyObject



36
37
38
# File 'lib/ritm/certs/certificate.rb', line 36

def public_key
  @cert.key_material.public_key
end

#x509Object



44
45
46
# File 'lib/ritm/certs/certificate.rb', line 44

def x509
  @cert.openssl_body
end