-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmkcert.rb
executable file
·85 lines (67 loc) · 1.55 KB
/
mkcert.rb
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/env ruby
#
# Creates a certificate signed by a CA made with mkca.rb
# Valid for two years.
require 'yaml'
CAKEY = "ca.key"
CACERT = "ca.cert"
def err(*args)
STDERR.puts(*args)
end
def usage
err "usage: #$0 <keysize> <hostname>..."
end
def checkfile(filename)
if File.exist?(filename)
err "#{filename} already exists. Pick another filename."
exit 1
end
end
if ARGV.size <= 1
usage
exit 1
end
keysize = ARGV.shift
filename = ARGV.first
checkfile "#{filename}.key"
checkfile "#{filename}.crt"
subject_hash = YAML.load(File.read("config.yml"))
subject_hash["CN"] = ARGV.first
subject = "/" + subject_hash.map { |k, v| "#{k}=#{v}"}.join("/")
subject_alt_names = ARGV.map { |hostname| "DNS:#{hostname}" }.join(',')
extensions = <<-END
[cert_extensions]
basicConstraints=critical,CA:false
subjectAltName=#{subject_alt_names}
END
system "openssl genrsa -out #{filename}.key #{keysize}"
system "bash", "-c", <<-SH
openssl req \\
-new \\
-sha256 \\
-key #{filename}.key \\
-subj "#{subject}" \\
-reqexts cert_extensions \\
-config <(cat /etc/ssl/openssl.cnf <(printf "#{extensions}")) \\
-days 730 \\
-out #{filename}.csr
SH
if File.exist?("ca.srl")
serial = "-CAserial ca.srl"
else
serial = "-CAcreateserial"
end
system "bash", "-c", <<-SH
openssl x509 \\
-req \\
-days 730 \\
-sha256 \\
-in #{filename}.csr \\
-CA ca.crt \\
-CAkey ca.key \\
#{serial} \\
-extensions cert_extensions \\
-extfile <(cat /etc/ssl/openssl.cnf <(printf "#{extensions}")) \\
-out #{filename}.crt
SH
system "rm #{filename}.csr"