-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathmake-ssl.sh
executable file
·335 lines (273 loc) · 7.27 KB
/
make-ssl.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#!/bin/bash
set -e -x
ROOT="out"
PASS="secret"
#CA_ALIAS="infinispan-ca"
#CA_DIR=${ROOT}/ssl/ca
#CA_KEYSTORE="ca.jks"
# Remove any previous certificate
rm -drf ${ROOT}/ssl
# Create keystore
create_keystore() {
local alias=$1
local dname=$2
local dir=$3
local keystore=$4
keytool -genkeypair \
-alias $alias \
-dname $dname \
-keystore $dir/$keystore \
-storepass $PASS \
-keypass $PASS \
-keyalg RSA \
-keysize 2048 \
-storetype JKS
}
# Create certificate sign request
create_sign_request() {
local alias=$1
local dname=$2
local dir=$3
local keystore=$4
local csr=$5
keytool -certreq \
-alias $alias \
-dname $dname \
-keystore $dir/$keystore \
-storepass $PASS \
-keypass $PASS \
-file $dir/$csr
}
# Sign certificate with CA
sign_certificate() {
local ca=$1
local dir=$2
local csr=$3
local cer=$4
local ca_alias="infinispan-$ca"
local ca_dir=${ROOT}/ssl/$ca
local ca_keystore="$ca.jks"
keytool -gencert \
-alias $ca_alias \
-keystore $ca_dir/$ca_keystore \
-storepass $PASS \
-keypass $PASS \
-infile $dir/$csr \
-outfile $dir/$cer
}
# Import CA keystore into keystore
import_ca() {
local ca=$1
local dir=$2
local keystore=$3
local ca_alias="infinispan-$ca"
local ca_dir=${ROOT}/ssl/$ca
local ca_keystore="$ca.jks"
keytool -importkeystore \
-srcalias $ca_alias \
-srcstorepass $PASS \
-srckeystore $ca_dir/$ca_keystore \
-deststorepass $PASS \
-destkeystore $dir/$keystore
}
# Import certificate into keystore
import_certificate() {
local alias=$1
local dir=$2
local keystore=$3
local cer=$4
keytool -importcert \
-alias $alias \
-keystore $dir/$keystore \
-storepass $PASS \
-keypass $PASS \
-file $dir/$cer
}
# Convert into a p12 keystore
keystore_to_p12() {
local dir=$1
local keystore=$2
local p12store=$3
keytool -importkeystore \
-srckeystore $dir/$keystore \
-destkeystore $dir/$p12store \
-srcstoretype jks \
-deststoretype pkcs12 \
-srcstorepass $PASS \
-deststorepass $PASS
}
# Extract client certificate as pem
extract_pem_certificate() {
local dir=$1
local p12store=$2
local pem=$3
openssl pkcs12 \
-in $dir/$p12store \
-passin pass:$PASS \
-passout pass:$PASS \
-nokeys \
-clcerts \
| awk '/-BEGIN CERTIFICATE-/{a=1};a;/-END CERTIFICATE-/{exit}' \
> $dir/$pem
}
make_ca() {
local name=$1
local dir=${ROOT}/ssl/$name
local alias="infinispan-$name"
local dname="CN=CA,OU=Infinispan,O=JBoss,L=RedHat"
local keystore="$name.jks"
local p12store="$name.p12"
local pem="$name.pem"
local pass="secret"
# Make directories to work from
mkdir -p $dir
# Create your very own Root Certificate Authority
keytool -genkeypair \
-alias $alias \
-dname $dname \
-keystore $dir/$keystore \
-storepass $pass \
-keypass $PASS \
-keyalg RSA \
-keysize 2048 \
-storetype JKS \
-ext bc:c
# Convert into a p12 keystore
keytool -importkeystore \
-srckeystore $dir/$keystore \
-destkeystore $dir/$p12store \
-srcstoretype jks \
-deststoretype pkcs12 \
-srcstorepass $pass \
-deststorepass $pass
# Extract pem
openssl pkcs12 \
-in $dir/$p12store \
-out $dir/$pem \
-passin pass:$pass \
-passout pass:$pass \
-nokeys
}
make_server_keystore() {
local ca=$1
local dir=${ROOT}/ssl/server
local alias="server"
# CN has to match Hostname/IP, otherwise you get:
# Hostname/IP doesn't match certificate's altnames: "Host: localhost. is not cert's CN: Server"
local dname="CN=localhost,OU=Infinispan,O=JBoss,L=RedHat"
local keystore="server.jks"
local p12store="server.p12"
local csr="server.csr"
local pass="secret"
local cer="server.cer"
local ca_dir=${ROOT}/ssl/$ca # TODO global
local ca_keystore="$ca.jks" # TODO global
local ca_alias="infinispan-$ca" # TODO global
# Make directories to work from
mkdir -p $dir
# Create server keystore
create_keystore $alias $dname $dir $keystore
# Create certificate sign request
keytool -certreq \
-alias $alias \
-dname $dname \
-keystore $dir/$keystore \
-storepass $pass \
-keypass $PASS \
-file $dir/$csr \
# Sign certicate with CA
keytool -gencert \
-alias $ca_alias \
-keystore $ca_dir/$ca_keystore \
-storepass $pass \
-keypass $PASS \
-infile $dir/$csr \
-outfile $dir/$cer
# Import CA keystore into server keystore
keytool -importkeystore \
-srcalias $ca_alias \
-srcstorepass $pass \
-srckeystore $ca_dir/$ca_keystore \
-deststorepass $pass \
-destkeystore $dir/$keystore
# Import server certificate into server keystore
keytool -importcert \
-alias $alias \
-keystore $dir/$keystore \
-storepass $pass \
-keypass $PASS \
-file $dir/$cer
# Convert into a p12 keystore
keytool -importkeystore \
-srckeystore $dir/$keystore \
-destkeystore $dir/$p12store \
-srcstoretype jks \
-deststoretype pkcs12 \
-srcstorepass $pass \
-deststorepass $pass
}
make_client_keystore() {
local ca=$1
local dir=${ROOT}/ssl/client
local alias="client"
# CN has to match Hostname/IP, otherwise you get:
# Hostname/IP doesn't match certificate's altnames: "Host: localhost. is not cert's CN: Server"
local dname="CN=localhost,OU=Infinispan,O=JBoss,L=RedHat"
local keystore="client.jks"
local p12store="client.p12"
local pass="secret"
local csr="client.csr"
local cer="client.cer"
local pem="client.pem"
local privkey="client.pk"
# Make directories to work from
mkdir -p $dir
create_keystore $alias $dname $dir $keystore
create_sign_request $alias $dname $dir $keystore $csr
sign_certificate $ca $dir $csr $cer
import_ca $ca $dir $keystore
import_certificate $alias $dir $keystore $cer
keystore_to_p12 $dir $keystore $p12store
extract_pem_certificate $dir $p12store $pem
# Extract client private key as pem
openssl pkcs12 \
-in $dir/$p12store \
-passin pass:$pass \
-passout pass:$pass \
-nocerts \
-nodes \
| awk '/-BEGIN PRIVATE KEY-/{a=1};a;/-END PRIVATE KEY-/{exit}' \
> $dir/$privkey
}
make_sni() {
local ca=$1
local host=$2
local dir=${ROOT}/ssl/sni-$host
local alias=$host
# CN has to match Hostname/IP
local dname="CN=$host,OU=Infinispan,O=JBoss,L=RedHat"
local keystore="$host.jks"
local p12store="$host.p12"
local csr="$host.csr"
local cer="$host.cer"
local pem="$host.pem"
# Make directories to work from
mkdir -p $dir
create_keystore $alias $dname $dir $keystore
create_sign_request $alias $dname $dir $keystore $csr
sign_certificate $ca $dir $csr $cer
import_ca $ca $dir $keystore
import_certificate $alias $dir $keystore $cer
keystore_to_p12 $dir $keystore $p12store
extract_pem_certificate $dir $p12store $pem
}
main() {
make_ca ca
make_server_keystore ca
make_client_keystore ca
make_sni ca trust1
make_sni ca trust2
make_ca untrust-ca
make_sni untrust-ca untrust
}
main