2003-07-23 12:12:24 -04:00
|
|
|
#!/usr/bin/env ruby
|
|
|
|
|
2007-12-24 02:31:23 -05:00
|
|
|
require 'optparse'
|
2003-07-23 12:12:24 -04:00
|
|
|
require 'openssl'
|
|
|
|
|
|
|
|
def usage
|
|
|
|
myname = File::basename($0)
|
|
|
|
$stderr.puts <<EOS
|
2004-03-26 11:47:16 -05:00
|
|
|
Usage: #{myname} [--key keypair_file] name
|
2003-07-23 12:12:24 -04:00
|
|
|
name ... ex. /C=JP/O=RRR/OU=CA/CN=NaHi/emailAddress=nahi@example.org
|
|
|
|
EOS
|
|
|
|
exit
|
|
|
|
end
|
|
|
|
|
2007-12-24 02:31:23 -05:00
|
|
|
options = ARGV.getopts(nil, "key:", "csrout:", "keyout:")
|
|
|
|
keypair_file = options["key"]
|
|
|
|
csrout = options["csrout"] || "csr.pem"
|
|
|
|
keyout = options["keyout"] || "keypair.pem"
|
2003-07-23 12:12:24 -04:00
|
|
|
|
|
|
|
$stdout.sync = true
|
2004-01-29 08:30:05 -05:00
|
|
|
name_str = ARGV.shift or usage()
|
2021-02-25 02:49:11 -05:00
|
|
|
name = OpenSSL::X509::Name.parse(name_str)
|
2003-07-23 12:12:24 -04:00
|
|
|
|
|
|
|
keypair = nil
|
|
|
|
if keypair_file
|
2021-02-25 02:49:11 -05:00
|
|
|
keypair = OpenSSL::PKey.read(File.read(keypair_file))
|
2003-07-23 12:12:24 -04:00
|
|
|
else
|
2021-02-25 02:55:40 -05:00
|
|
|
keypair = OpenSSL::PKey::RSA.new(2048) { putc "." }
|
2003-07-23 12:12:24 -04:00
|
|
|
puts
|
|
|
|
puts "Writing #{keyout}..."
|
|
|
|
File.open(keyout, "w", 0400) do |f|
|
|
|
|
f << keypair.to_pem
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2004-01-29 08:30:05 -05:00
|
|
|
puts "Generating CSR for #{name_str}"
|
2003-07-23 12:12:24 -04:00
|
|
|
|
2021-02-25 02:49:11 -05:00
|
|
|
req = OpenSSL::X509::Request.new
|
2003-11-05 09:55:52 -05:00
|
|
|
req.version = 0
|
2003-07-23 12:12:24 -04:00
|
|
|
req.subject = name
|
2021-02-25 02:55:40 -05:00
|
|
|
req.public_key = keypair
|
|
|
|
req.sign(keypair, "MD5")
|
2003-07-23 12:12:24 -04:00
|
|
|
|
|
|
|
puts "Writing #{csrout}..."
|
|
|
|
File.open(csrout, "w") do |f|
|
|
|
|
f << req.to_pem
|
|
|
|
end
|
2007-12-24 02:31:23 -05:00
|
|
|
puts req.to_text
|
|
|
|
puts req.to_pem
|