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.



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

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
27
# 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 = 'dongjia'
  cert.subject.country = 'CN'
  cert.not_before = Time.now - 7 * 24 * 60 * 60           # 7 days before
  cert.not_after = cert.not_before + 397 * 24 * 60 * 60   # 397 days
  cert.serial_number.number = serial_number || common_name.hash.abs
  cert.key_material.generate_key(2048)
  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



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

def pem
  @cert.to_pem
end

#private_keyObject



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

def private_key
  @cert.key_material.private_key
end

#public_keyObject



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

def public_key
  @cert.key_material.public_key
end

#x509Object



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

def x509
  @cert.openssl_body
end