-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathgenerate.sh
executable file
·190 lines (146 loc) · 4.39 KB
/
generate.sh
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#!/bin/bash -e
if [[ $1 = "cleanup" ]]; then
rm -rf 1_root
rm -rf 2_intermediate
rm -rf 3_application
rm -rf 4_client
exit 0
fi
if [[ $1 = "" ]]; then
echo "please specify a domain ./generate.sh www.example.com"
exit 1
fi
if [[ $2 = "" ]]; then
echo "please specify a password for the private key"
exit 1
fi
echo
echo Generate the root key
echo ---
mkdir -p 1_root/private
openssl genrsa -aes256 -passout pass:$2 -out 1_root/private/ca.key.pem 4096
chmod 444 1_root/private/ca.key.pem
echo
echo Generate the root certificate
echo ---
mkdir -p 1_root/certs
mkdir -p 1_root/newcerts
touch 1_root/index.txt
echo "100212" > 1_root/serial
openssl req -config openssl.cnf \
-key 1_root/private/ca.key.pem \
-passin pass:$2 \
-new -x509 -days 7300 -sha256 -extensions v3_ca \
-subj "/C=US/ST=Denial/L=Springfield/O=Dis/CN=$1" \
-out 1_root/certs/ca.cert.pem
echo
echo Verify root key
echo ---
openssl x509 -noout -text -in 1_root/certs/ca.cert.pem
echo
echo Generate the key for the intermediary certificate
echo ---
mkdir -p 2_intermediate/private
openssl genrsa -aes256 \
-passout pass:$2 \
-out 2_intermediate/private/intermediate.key.pem 4096
chmod 444 2_intermediate/private/intermediate.key.pem
echo
echo Generate the signing request for the intermediary certificate
echo ---
mkdir -p 2_intermediate/csr
openssl req -config openssl.cnf -new -sha256 \
-passin pass:$2 \
-subj "/C=US/ST=Denial/L=Springfield/O=Dis/CN=$1" \
-key 2_intermediate/private/intermediate.key.pem \
-out 2_intermediate/csr/intermediate.csr.pem
echo
echo Sign the intermediary
echo ---
mkdir -p 2_intermediate/certs
mkdir -p 2_intermediate/newcerts
touch 2_intermediate/index.txt
echo "100212" > 2_intermediate/serial
openssl ca -config openssl.cnf -extensions v3_intermediate_ca \
-passin pass:$2 \
-days 3650 -notext -md sha256 \
-in 2_intermediate/csr/intermediate.csr.pem \
-out 2_intermediate/certs/intermediate.cert.pem
chmod 444 2_intermediate/certs/intermediate.cert.pem
echo
echo Verify intermediary
echo ---
openssl x509 -noout -text \
-in 2_intermediate/certs/intermediate.cert.pem
openssl verify -CAfile 1_root/certs/ca.cert.pem \
2_intermediate/certs/intermediate.cert.pem
echo
echo Create the chain file
echo ---
cat 2_intermediate/certs/intermediate.cert.pem \
1_root/certs/ca.cert.pem > 2_intermediate/certs/ca-chain.cert.pem
chmod 444 2_intermediate/certs/ca-chain.cert.pem
echo
echo Create the application key
echo ---
mkdir -p 3_application/private
openssl genrsa \
-passout pass:$2 \
-out 3_application/private/$1.key.pem 2048
chmod 444 3_application/private/$1.key.pem
echo
echo Create the application signing request
echo ---
mkdir -p 3_application/csr
openssl req -config intermediate_openssl.cnf \
-subj "/C=US/ST=Denial/L=Springfield/O=Dis/CN=$1" \
-passin pass:$2 \
-key 3_application/private/$1.key.pem \
-new -sha256 -out 3_application/csr/$1.csr.pem
echo
echo Create the application certificate
echo ---
mkdir -p 3_application/certs
openssl ca -config intermediate_openssl.cnf \
-passin pass:$2 \
-extensions server_cert -days 375 -notext -md sha256 \
-in 3_application/csr/$1.csr.pem \
-out 3_application/certs/$1.cert.pem
chmod 444 3_application/certs/$1.cert.pem
echo
echo Validate the certificate
echo ---
openssl x509 -noout -text \
-in 3_application/certs/$1.cert.pem
echo
echo Validate the certificate has the correct chain of trust
echo ---
openssl verify -CAfile 2_intermediate/certs/ca-chain.cert.pem \
3_application/certs/$1.cert.pem
echo
echo Generate the client key
echo ---
mkdir -p 4_client/private
openssl genrsa \
-passout pass:$2 \
-out 4_client/private/$1.key.pem 2048
chmod 444 4_client/private/$1.key.pem
echo
echo Generate the client signing request
echo ---
mkdir -p 4_client/csr
openssl req -config intermediate_openssl.cnf \
-subj "/C=US/ST=Denial/L=Springfield/O=Dis/CN=$1" \
-passin pass:$2 \
-key 4_client/private/$1.key.pem \
-new -sha256 -out 4_client/csr/$1.csr.pem
echo
echo Create the client certificate
echo ---
mkdir -p 4_client/certs
openssl ca -config intermediate_openssl.cnf \
-passin pass:$2 \
-extensions usr_cert -days 375 -notext -md sha256 \
-in 4_client/csr/$1.csr.pem \
-out 4_client/certs/$1.cert.pem
chmod 444 4_client/certs/$1.cert.pem