File: gen_csr.rb

package info (click to toggle)
ruby1.9.1 1.9.3.194-8.1%2Bdeb7u5
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 67,684 kB
  • sloc: ruby: 528,270; ansic: 518,972; xml: 25,427; yacc: 17,944; sh: 3,208; lisp: 1,998; tcl: 949; pascal: 723; makefile: 635; perl: 62; python: 47; awk: 36; asm: 35; sed: 27
file content (51 lines) | stat: -rw-r--r-- 1,057 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env ruby

require 'optparse'
require 'openssl'

include OpenSSL

def usage
  myname = File::basename($0)
  $stderr.puts <<EOS
Usage: #{myname} [--key keypair_file] name
  name ... ex. /C=JP/O=RRR/OU=CA/CN=NaHi/emailAddress=nahi@example.org
EOS
  exit
end

options = ARGV.getopts(nil, "key:", "csrout:", "keyout:")
keypair_file = options["key"]
csrout = options["csrout"] || "csr.pem"
keyout = options["keyout"] || "keypair.pem"

$stdout.sync = true
name_str = ARGV.shift or usage()
name = X509::Name.parse(name_str)

keypair = nil
if keypair_file
  keypair = PKey::RSA.new(File.open(keypair_file).read)
else
  keypair = PKey::RSA.new(1024) { putc "." }
  puts
  puts "Writing #{keyout}..."
  File.open(keyout, "w", 0400) do |f|
    f << keypair.to_pem
  end
end

puts "Generating CSR for #{name_str}"

req = X509::Request.new
req.version = 0
req.subject = name
req.public_key = keypair.public_key
req.sign(keypair, Digest::MD5.new)

puts "Writing #{csrout}..."
File.open(csrout, "w") do |f|
  f << req.to_pem
end
puts req.to_text
puts req.to_pem