Skip to content

Instantly share code, notes, and snippets.

@gdisneyleugers
Last active August 29, 2015 13:56
Show Gist options
  • Save gdisneyleugers/9297031 to your computer and use it in GitHub Desktop.
Save gdisneyleugers/9297031 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'openssl'
key = OpenSSL::PKey::RSA.new(4096)
cipher = OpenSSL::Cipher::AES.new(128, :CBC)
ctx = OpenSSL::SSL::SSLContext.new
public_key = key.public_key
printf "Common Name: "
cn = gets
commonname = cn.chomp
printf "Orgnization: "
org = gets
printf "Orgnizational Unit: "
orgu = gets
printf "Country: "
country = gets
printf "Issuer: "
ca = gets
subject = "CN=#{commonname}/O=#{org}/OU=#{orgu}/C=#{country}/"
issuer = ca.chomp
open "#{issuer}"".pem", 'w', 0400 do |io|
puts "Password for key: "
pass_phrase = gets
pass = pass_phrase.chomp
io.write key.export(cipher, pass_phrase)
end
keyd = ctx.key = "#{issuer}"".pem"
cert = OpenSSL::X509::Certificate.new
ctx.cert = "#{issuer}"".pem"
cert.subject = OpenSSL::X509::Name.parse(subject)
cert.issuer = OpenSSL::X509::Name.new [['CN', issuer], ['OU', '<Not Part Of Certificate>'],['O', org],['C', country]]
cert.not_before = Time.now
cert.not_after = Time.now + 365+365+365+365 * 24 * 60 * 60
cert.public_key = public_key
cert.serial = rand(999999)+rand(99999)*rand(9999)
cert.version = 3
ef = OpenSSL::X509::ExtensionFactory.new
ef.subject_certificate = cert
ef.issuer_certificate = cert
cert.extensions = [
ef.create_extension("basicConstraints","CA:TRUE", true),
ef.create_extension("subjectKeyIdentifier", "keyid:always,issuer:always"),
ef.create_extension("keyUsage", "cRLSign,keyCertSign,keyEncipherment,dataEncipherment,digitalSignature", true),
]
cert.add_extension ef.create_extension("authorityKeyIdentifier",
"keyid:always,issuer:always")
puts key, OpenSSL::Digest::SHA1.new
cer = puts cert.to_pem
file = File.open("#{commonname}"".pem", "w")
file.syswrite("#{cert.to_pem}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment