module DevCert::Util

Public Class Methods

export(path, entity) click to toggle source
# File lib/devcert/util.rb, line 42
def self.export(path, entity)
  open path, 'w' do |io|
    io.write(entity.to_pem)
  end
end
generate_ec_key(size) click to toggle source
# File lib/devcert/util.rb, line 74
def self.generate_ec_key(size)
  curve_name = nil
  if size == 256
    curve_name = 'prime256v1'
  elsif curve_name == 384
    curve_name = 'secp384r1'
  end

  raise 'Unsupported curve!' if curve_name.nil?

  private_key = ::OpenSSL::PKey::EC.new(curve_name)
  public_key = ::OpenSSL::PKey::EC.new(curve_name)

  private_key.generate_key
  public_key.public_key = private_key.public_key
  return private_key, public_key
end
generate_rsa_key(size) click to toggle source
# File lib/devcert/util.rb, line 69
def self.generate_rsa_key(size)
  key = ::OpenSSL::PKey::RSA.new(size)
  return key, key.public_key
end
generate_serial() click to toggle source
# File lib/devcert/util.rb, line 62
def self.generate_serial
  machine_bytes = ['foo'].pack('p').size
  machine_bits = machine_bytes * 8
  machine_max_signed = 2**(machine_bits - 1) - 1
  ::SecureRandom.random_number(machine_max_signed)
end
get_defaults() click to toggle source
# File lib/devcert/util.rb, line 9
def self.get_defaults
  path = ::File.absolute_path('defaults.yaml', ::Dir.pwd)
  data = \
    if ::File.exist?(path)
      ::YAML.load(::File.open(path)).fetch('devcert', {})
    else
      {}
    end

  {
    organization: data.fetch('organization', 'Acme Ltd.'),
    country: data.fetch('country', 'US'),
    state_name: data.fetch('state_name', 'California'),
    locality: data.fetch('locality', 'San Francisco')
  }
end
load_bundle(path) click to toggle source
# File lib/devcert/util.rb, line 48
def self.load_bundle(path)
  full_path = ::File.absolute_path(path, __dir__)
  if ::File.exist?(full_path)
    data = ::YAML.load(::File.open(full_path))
    {
      common_name: data[:common_name],
      private_key: ::OpenSSL::PKey.read(data[:private_key]),
      certificate: ::OpenSSL::X509::Certificate.new(data[:certificate])
    }
  else
    raise "No bundle at #{full_path} exists!"
  end
end
normalize_name(name) click to toggle source
# File lib/devcert/util.rb, line 26
def self.normalize_name(name)
  name.gsub(/[ .-]/, '_')
end
save_bundle(path, common_name, key, cert) click to toggle source
# File lib/devcert/util.rb, line 30
def self.save_bundle(path, common_name, key, cert)
  bundle = {
    common_name: common_name,
    private_key: key.to_der,
    certificate: cert.to_der
  }

  open path, 'w' do |io|
    io.write(bundle.to_yaml)
  end
end