Created
September 2, 2016 03:54
-
-
Save technion/0e63d42299cc3d91f387d41e9e5c2c74 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'openssl' | |
def subject_alt_name(domains) | |
domains = domains.split(/,/) | |
ef = OpenSSL::X509::ExtensionFactory.new | |
ef.create_extension("subjectAltName", | |
domains.map { |d| "DNS: #{d}" }.join(',')) | |
end | |
domainlist = 'test.example.com,test2.example.com,example.com,www.example.com' | |
rsa_key = OpenSSL::PKey::RSA.new(2048) | |
csr = OpenSSL::X509::Request.new | |
csr.subject = OpenSSL::X509::Name.new([ | |
["C", 'AU'], | |
["ST", "NSW"], | |
["O", 'org'], | |
["CN", 'www.example.com'] | |
]) | |
csr.public_key = rsa_key.public_key | |
extensions = OpenSSL::ASN1::Set([OpenSSL::ASN1::Sequence( | |
[subject_alt_name(domainlist)])]) | |
csr.add_attribute(OpenSSL::X509::Attribute.new('extReq', extensions)) | |
csr.add_attribute(OpenSSL::X509::Attribute.new('msExtReq', extensions)) | |
csr.sign rsa_key, OpenSSL::Digest::SHA256.new | |
puts csr.to_pem |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment