Class: CertificateGenerator::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/certificate_generator/base.rb

Instance Method Summary collapse

Instance Method Details

#generate_core_cert(cname, serial) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/certificate_generator/base.rb', line 7

def generate_core_cert (cname, serial)

  key = OpenSSL::PKey::RSA.new(2048)

  cert = OpenSSL::X509::Certificate.new
  subject = "/C=GB/ST=London/L=London/O=Acme Inc/OU=Tech/CN=#{cname}/[email protected]"
  parsed_subject = OpenSSL::X509::Name.parse(subject)
  cert.subject = parsed_subject

  cert.not_before = Time.now
  cert.not_after = Time.now + (3600*24*365) # add a year
  cert.public_key = key.public_key
  cert.serial = serial
  cert.version = 2

  return cert, key

end

#save_cert_and_key(cert, key, output_dir, prefix = '') ⇒ Object



26
27
28
29
30
31
32
# File 'lib/certificate_generator/base.rb', line 26

def save_cert_and_key (cert, key, output_dir, prefix = '')

  FileUtils.mkdir_p("#{output_dir}")
  File.open("#{output_dir}/cert.pem", "w") { |f| f.write(cert.to_pem) }
  File.open("#{output_dir}/key.pem", "w") { |f| f.write(key.to_pem) }

end