class Bosh::Ssl::Certificate

Attributes

certificate_path[R]
key_path[R]

Public Class Methods

new(key_path, certificate_path, common_name, chain_path = nil) click to toggle source
# File lib/common/ssl.rb, line 13
def initialize(key_path, certificate_path, common_name, chain_path = nil)
  @key_path = key_path
  @certificate_path = certificate_path
  @chain_path = chain_path
  @subject_string = subject_string(common_name)
end

Public Instance Methods

certificate() click to toggle source
# File lib/common/ssl.rb, line 24
def certificate
  @csr_cert.to_pem
end
chain() click to toggle source
# File lib/common/ssl.rb, line 28
def chain
  @chain.to_pem if @chain
end
key() click to toggle source
# File lib/common/ssl.rb, line 20
def key
  @key.to_pem
end
load_or_create() click to toggle source
# File lib/common/ssl.rb, line 32
def load_or_create
  @key, @csr_cert = load_or_create_key_and_csr_cert
  @chain = OpenSSL::X509::Certificate.new(File.read(@chain_path)) if @chain_path

  self
end

Private Instance Methods

create_key_and_csr_cert() click to toggle source
# File lib/common/ssl.rb, line 64
def create_key_and_csr_cert
  subject = OpenSSL::X509::Name.parse(@subject_string)
  key = OpenSSL::PKey::RSA.new(2048)
  csr = new_csr(key, subject)
  csr_cert = new_csr_certificate(key, csr)

  File.write(@key_path, key.to_pem)
  File.write(@certificate_path, csr_cert.to_pem)

  [key, csr_cert]
end
load_key_and_csr_cert() click to toggle source
# File lib/common/ssl.rb, line 57
def load_key_and_csr_cert
  key = OpenSSL::PKey::RSA.new(File.read(@key_path))
  csr_cert = OpenSSL::X509::Certificate.new(File.read(@certificate_path))

  [key, csr_cert]
end
load_or_create_key_and_csr_cert() click to toggle source
# File lib/common/ssl.rb, line 41
def load_or_create_key_and_csr_cert
  if File.exists?(@key_path) && !File.exists?(@certificate_path)
    raise MatchingFileNotFound, 'The key that matches the given certificate could not be found.'
  end

  if File.exists?(@certificate_path) && !File.exists?(@key_path)
    raise MatchingFileNotFound, 'The certificate that matches the given key could not be found.'
  end

  if File.exists?(@key_path) && File.exists?(@certificate_path)
    load_key_and_csr_cert
  else
    create_key_and_csr_cert
  end
end
new_csr(key, subject) click to toggle source
# File lib/common/ssl.rb, line 76
def new_csr(key, subject)
  csr = OpenSSL::X509::Request.new
  csr.version = 0
  csr.subject = subject
  csr.public_key = key.public_key
  csr.sign key, OpenSSL::Digest::SHA1.new

  csr
end
new_csr_certificate(key, csr) click to toggle source
# File lib/common/ssl.rb, line 86
def new_csr_certificate(key, csr)
  csr_cert = OpenSSL::X509::Certificate.new
  csr_cert.serial = 0
  csr_cert.version = 2
  csr_cert.not_before = Time.now - 60 * 60 * 24
  csr_cert.not_after = Time.now + 94608000

  csr_cert.subject = csr.subject
  csr_cert.public_key = csr.public_key
  csr_cert.issuer = csr.subject

  csr_cert.sign key, OpenSSL::Digest::SHA1.new

  csr_cert
end
subject_string(common_name) click to toggle source
# File lib/common/ssl.rb, line 102
def subject_string(common_name)
  "/C=US/O=Pivotal/CN=#{common_name}"
end