Generating an X.509 Cert in Ruby
Message from 2022
This post is pretty old! Opinions and technical information in it are almost certainly oudated. Commands and configurations will probably not work. Consider the age of the content before putting any of it into practice.
Ruby’s openssl module has basically no documentation because all its methods are added on dynamically or something.
Mega-thanks to the JRuby team. This would have been impossible to write if I didn’t scrape from https://svn.codehaus.org/jruby/branches/openssl/test/openssl/utils.rb and https://svn.codehaus.org/jruby/branches/openssl/test/openssl/test_x509crl.rb
require 'openssl'
key = OpenSSL::PKey::RSA.generate(4096)
pub = key.public_key
ca = OpenSSL::X509::Name.parse("/C=US/ST=Florida/L=Miami/O=Waitingf/OU=Poopstat/CN=waitingf.org/emailAddress=bkerley@brycekerley.net")
cert = OpenSSL::X509::Certificate.new
cert.version = 2
cert.serial = 1
cert.subject = ca
cert.issuer = ca
cert.public_key = pub
cert.not_before = Time.now
cert.not_after = Time.now + 3600
File.open("private.pem", "w") { |f| f.write key.to_pem }
File.open("cert.pem", "w") { |f| f.write cert.to_pem }