class CSR

Constants

VERSION

Attributes

bits[R]
cipher[R]
city[R]
common_name[R]
country[R]
department[R]
digest[R]
email[R]
organization[R]
passphrase[R]
state[R]

Public Class Methods

new(country:, state:, city:, department:, organization:, common_name:, email:, bits: 4096, private_key: nil, passphrase: nil, cipher: nil, digest: nil) click to toggle source
# File lib/csr.rb, line 16
def initialize(country:, state:, city:, department:, organization:,
                common_name:, email:, bits: 4096, private_key: nil,
                passphrase: nil, cipher: nil, digest: nil)

  cipher        ||= OpenSSL::Cipher.new("des-ede3-cbc")
  digest        ||= OpenSSL::Digest::SHA256.new
  @country      = country
  @state        = state
  @city         = city
  @department   = department
  @organization = organization
  @common_name  = common_name
  @email        = email
  @bits         = bits
  @passphrase   = passphrase
  @private_key  = OpenSSL::PKey::RSA.new(private_key) if private_key
  @cipher       = cipher
  @digest       = digest
end
verify?(request_key, private_key, passphrase = nil) click to toggle source
# File lib/csr.rb, line 9
def self.verify?(request_key, private_key, passphrase = nil)
  private_key = OpenSSL::PKey::RSA.new(private_key, passphrase)
  csr = OpenSSL::X509::Request.new(request_key)
  csr.public_key = private_key.public_key
  csr.verify(csr.public_key)
end

Public Instance Methods

pem() click to toggle source
# File lib/csr.rb, line 77
def pem
  request.to_pem
end
private_key() click to toggle source
# File lib/csr.rb, line 36
def private_key
  @private_key ||= OpenSSL::PKey::RSA.new(bits)
end
private_key_pem() click to toggle source
# File lib/csr.rb, line 66
def private_key_pem
  args = []

  if passphrase
    args << cipher
    args << passphrase
  end

  private_key.to_pem(*args)
end
request() click to toggle source
# File lib/csr.rb, line 40
def request
  @request ||= OpenSSL::X509::Request.new.tap do |request|
    request.version = 0
    request.subject = OpenSSL::X509::Name.new([
      ["C",             country,      OpenSSL::ASN1::PRINTABLESTRING],
      ["ST",            state,        OpenSSL::ASN1::PRINTABLESTRING],
      ["L",             city,         OpenSSL::ASN1::PRINTABLESTRING],
      ["O",             organization, OpenSSL::ASN1::UTF8STRING],
      ["OU",            department,   OpenSSL::ASN1::UTF8STRING],
      ["CN",            common_name,  OpenSSL::ASN1::UTF8STRING],
      ["emailAddress",  email,        OpenSSL::ASN1::UTF8STRING]
    ])

    request.public_key = private_key.public_key
    request.sign(private_key, digest)
  end
end
save_to(directory, name) click to toggle source
# File lib/csr.rb, line 58
def save_to(directory, name)
  FileUtils.mkdir_p(directory)
  base_path = File.join(directory, name)
  save_private_key_to("#{base_path}.key")
  save_csr_to("#{base_path}.csr")
  true
end

Private Instance Methods

save_csr_to(path) click to toggle source
# File lib/csr.rb, line 89
def save_csr_to(path)
  File.open(path, "w") do |file|
    file << pem
  end
end
save_private_key_to(path) click to toggle source
# File lib/csr.rb, line 83
def save_private_key_to(path)
  File.open(path, "w") do |file|
    file << private_key_pem
  end
end