Skip to content

Instantly share code, notes, and snippets.

@xorrizon
Forked from ricardochimal/cert.rb
Last active August 29, 2015 14:15
Show Gist options
  • Select an option

  • Save xorrizon/08e49318696c68f74990 to your computer and use it in GitHub Desktop.

Select an option

Save xorrizon/08e49318696c68f74990 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
domain = "localhost"
organisation = "example"
country = "AT"
subjectAltDomains = [
"DNS:#{domain}",
"DNS:localhost.localdomain",
"IP:127.0.0.1",
"IP:10.0.2.2"
]
require 'openssl'
puts "Generating public and private keys..."
key = OpenSSL::PKey::RSA.new(4096)
subject = "/C=#{country}/O=#{organisation}/CN=#{domain}"
cert = OpenSSL::X509::Certificate.new
cert.subject = cert.issuer = OpenSSL::X509::Name.parse(subject)
cert.not_before = Time.now
cert.not_after = Time.now + 365*24*60*60
cert.public_key = key.public_key
cert.serial = 0x0
cert.version = 2
puts "Signing certificate..."
ef = OpenSSL::X509::ExtensionFactory.new
ef.subject_certificate = ef.issuer_certificate = cert
cert.extensions = [
ef.create_extension("basicConstraints","CA:FALSE", true),
ef.create_extension("subjectKeyIdentifier", "hash")
]
cert.add_extension ef.create_extension("authorityKeyIdentifier",
"keyid:always,issuer:always")
cert.add_extension ef.create_extension("subjectAltName", subjectAltDomains.join(','))
cert.sign key, OpenSSL::Digest::SHA256.new
File.open("cert.pem", "w") { |f| f.write(cert.to_pem) }
File.open("key.pem", "w") { |f| f.write(key.to_s) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment