forked from rhboot/shim
-
Notifications
You must be signed in to change notification settings - Fork 8
/
make-certs
executable file
·554 lines (515 loc) · 15.6 KB
/
make-certs
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
#!/bin/bash -e
#
# Generate a root CA cert for signing, and then a subject cert.
# Usage: make-certs.sh hostname [user[@domain]] [more ...]
# For testing only, probably still has some bugs in it.
#
DOMAIN=xn--u4h.net
DAYS=365
KEYTYPE=RSA
KEYSIZE=2048
DIGEST=SHA256
CRLHOURS=24
CRLDAYS=
# Cleanup temporary files at exit.
touch openssl.cnf
newcertdir=`mktemp -d`
cleanup() {
test -f openssl.cnf && rm -f openssl.cnf
test -f ca.txt && rm -f ca.txt
test -f ocsp.txt && rm -f ocsp.txt
test -n "$newcertdir" && rm -fr "$newcertdir"
}
trap cleanup EXIT
# The first argument is either a common name value or a flag indicating that
# we're doing something other than issuing a cert.
commonname="$1"
refresh_crl=false
revoke_cert=false
ocsp_serve=false
if test "x$commonname" = "x-refresh-crl" ; then
refresh_crl=true
commonname="$1"
fi
if test "x$commonname" = "x-refresh_crl" ; then
refresh_crl=true
commonname="$1"
fi
if test "x$commonname" = "x-revoke" ; then
revoke_cert=true
shift
commonname="$1"
fi
if test "x$commonname" = "x-ocsp" ; then
ocsp_serve=true
commonname="$1"
fi
if test "x$commonname" = x ; then
echo Usage: `basename $0` 'commonname' user'[@domain]' '[more [...]]'
echo Usage: `basename $0` -revoke 'commonname'
echo Usage: `basename $0` -ocsp
echo Usage: `basename $0` -refresh-crl
echo More:
echo -e \\tKey usage: "[sign|signing|encrypt|encryption|all]"
echo -e \\tAuthority Access Info OCSP responder: "ocsp:URI"
echo -e \\tCRL distribution point: "crl:URI"
echo -e \\tSubject Alternative Name:
echo -e \\t\\tHostname: "*"
echo -e \\t\\tIP address: w.x.y.z
echo -e \\t\\tEmail address: "*@*.com/edu/net/org/local"
echo -e \\t\\tKerberos principal name: "*@*.COM/EDU/NET/ORG/LOCAL"
echo -e \\tExtended key usage:
echo -e \\t\\t1....
echo -e \\t\\t2....
echo -e \\t\\tid-kp-server-auth \| tls-server
echo -e \\t\\tid-kp-client-auth \| tls-client
echo -e \\t\\tid-kp-email-protection \| email
echo -e \\t\\tid-ms-kp-sc-logon \| id-ms-sc-logon
echo -e \\t\\tid-pkinit-kp-client-auth \| id-pkinit-client
echo -e \\t\\tid-pkinit-kp-kdc \| id-pkinit-kdc
echo -e \\t\\tca \| CA
exit 1
fi
# Choose a user name part for email attributes.
GIVENUSER=$2
test x"$GIVENUSER" = x && GIVENUSER=$USER
echo "$GIVENUSER" | grep -q @ || GIVENUSER="$GIVENUSER"@$DOMAIN
DOMAIN=`echo "$GIVENUSER" | cut -f2- -d@`
shift || true
shift || true
# Done already?
done=:
keygen() {
case "$KEYTYPE" in
DSA)
openssl dsaparam -out "$1".param $KEYSIZE
openssl gendsa "$1".param
;;
RSA|*)
#openssl genrsa $KEYSIZE -passout pass:qweqwe
openssl genrsa $KEYSIZE
#openssl genrsa $KEYSIZE -nodes
;;
esac
}
# Set some defaults.
CA=FALSE
if test -s ca.crldp.uri.txt ; then
crlval="`cat ca.crldp.uri.txt`"
crl="URI:$crlval"
fi
if test -s ca.ocsp.uri.txt ; then
aiaval="`cat ca.ocsp.uri.txt`"
aia="OCSP;URI:$aiaval"
fi
if test -s ca.domain.txt ; then
domval="`cat ca.domain.txt`"
if test -n "$domval" ; then
DOMAIN="$domval"
fi
fi
# Parse the arguments which indicate what sort of information we want.
while test $# -gt 0 ; do
type=
value="$1"
case "$value" in
RSA|rsa)
KEYTYPE=RSA
;;
DSA|dsa)
KEYTYPE=DSA
;;
OCSP:*|ocsp:*)
aiaval=`echo "$value" | cut -f2- -d:`
aia="OCSP;URI:$aiaval"
;;
CRL:*|crl:*)
crlval=`echo "$value" | cut -f2- -d:`
crl="URI:$crlval"
;;
signing|sign)
keyusage="${keyusage:+${keyusage},}nonRepudiation,digitalSignature"
;;
encryption|encrypt)
keyusage="${keyusage:+${keyusage},}keyEncipherment,dataEncipherment"
;;
all)
keyusage="digitalSignature,nonRepudiation,keyEncipherment,dataEncipherment,keyAgreement,keyCertSign,cRLSign,encipherOnly,decipherOnly"
;;
ca|CA)
CA=TRUE
keyusage="${keyusage:+${keyusage},}nonRepudiation,digitalSignature,keyEncipherment,dataEncipherment,keyAgreement,keyCertSign,cRLSign"
;;
1.*|2.*|id-*|tls-*|email|mail|codesign)
ekuval=`echo "$value" | tr '[A-Z]' '[a-z]' | sed 's,\-,,g'`
case "$ekuval" in
idkpserverauth|tlsserver) ekuval=1.3.6.1.5.5.7.3.1;;
idkpclientauth|tlsclient) ekuval=1.3.6.1.5.5.7.3.2;;
idkpemailprotection|email|mail) ekuval=1.3.6.1.5.5.7.3.4;;
idkpcodesign|codesign) ekuval=1.3.6.1.5.5.7.3.3;;
idmskpsclogon|idmssclogon) ekuval=1.3.6.1.4.1.311.20.2.2;;
idpkinitkpclientauth|idpkinitclient) ekuval=1.3.6.1.5.2.3.4;;
idpkinitkpkdc|idpkinitkdc) ekuval=1.3.6.1.5.2.3.5;;
esac
if test -z "$eku" ; then
eku="$ekuval"
else
eku="$eku,$ekuval"
fi
;;
*@*.COM|*@*.EDU|*@*.NET|*@*.ORG|*@*.LOCAL)
luser=`echo "$value" | tr '[A-Z]' '[a-z]'`
if test "$luser" = "$value" ; then
luser=
fi
type="otherName:1.3.6.1.5.2.2;SEQUENCE:$value,${luser:+otherName:1.3.6.1.4.1.311.20.2.3;UTF8:${luser},}otherName:1.3.6.1.4.1.311.20.2.3;UTF8"
unset luser
principals="$principals $value"
;;
*@*.com|*@*.edu|*@*.net|*@*.org|*@*.local) type=email;;
[0-9]*.[0-9]*.[0-9]*.[0-9]*) type=IP;;
*) type=DNS;;
esac
if test -n "$type" ; then
newvalue="${type}:$value"
if test -z "$altnames" ; then
altnames="${newvalue}"
else
altnames="${altnames},${newvalue}"
fi
fi
shift
done
# Build the configuration file, including bits on how to construct the CA
# certificate, an OCSP responder certificate, and the issued certificate.
cat > openssl.cnf <<- EOF
[ca]
default_ca = issuer
[issuer]
private_key = `pwd`/ca.key
certificate = `pwd`/ca.crt
database = `pwd`/ca.db
serial = `pwd`/ca.srl
default_md = $DIGEST
new_certs_dir = $newcertdir
policy = no_policy
[no_policy]
[req_oids]
domainComponent = 0.9.2342.19200300.100.1.25
[req_ca]
prompt = no
oid_section = req_oids
distinguished_name = req_ca_name
default_md = $DIGEST
subjectKeyIdentifier=hash
[req_ca_name]
C=US
#stateOrProvinceName=SomeState
localityName=SomeCity
O=SomeOrg
EOF
#echo $DOMAIN | awk 'BEGIN {FS="."}{for(i=NF;i>0;i--){print NF-i ".domainComponent="$i;}}' >> openssl.cnf
cat >> openssl.cnf <<- EOF
#commonName = Test Certifying CA
[v3_ca]
subjectKeyIdentifier=hash
authorityKeyIdentifier=keyid:always
#authorityKeyIdentifier=keyid:always,issuer:always
keyUsage=nonRepudiation,digitalSignature,keyEncipherment,dataEncipherment,keyAgreement,keyCertSign,cRLSign
basicConstraints=critical,CA:TRUE
nsComment="Testing CA Certificate"
EOF
if test -n "$aia" ; then
echo "authorityInfoAccess = ${aia}" >> openssl.cnf
echo -n "$aiaval" > ca.ocsp.uri.txt
fi
if test -n "$crl" ; then
echo "crlDistributionPoints = ${crl}" >> openssl.cnf
echo -n "$crlval" > ca.crldp.uri.txt
fi
echo "$DOMAIN" > ca.domain.txt
cat >> openssl.cnf <<- EOF
[req_ocsp]
prompt = no
oid_section = req_oids
distinguished_name = req_ocsp_name
default_md = $DIGEST
[req_ocsp_name]
C=US
#stateOrProvinceName=SomeState
localityName=SomeOrg
O=SomeOrg
EOF
#echo $DOMAIN | awk 'BEGIN {FS="."}{for(i=NF;i>0;i--){print NF-i ".domainComponent="$i;}}' >> openssl.cnf
cat >> openssl.cnf <<- EOF
#commonName = OCSP Signer for Test Certifying CA
[v3_ocsp]
subjectKeyIdentifier=hash
#authorityKeyIdentifier=keyid:always,issuer:always
authorityKeyIdentifier=keyid:always
keyUsage=digitalSignature,nonRepudiation,keyEncipherment,dataEncipherment,keyAgreement,keyCertSign,cRLSign
extendedKeyUsage=1.3.6.1.5.5.7.3.9
#basicConstraints=CA:FALSE
basicConstraints=CA:TRUE
nsComment="Testing OCSP Certificate"
1.3.6.1.5.5.7.48.1.5=ASN1:NULL
EOF
if test -n "$aia" ; then
echo "authorityInfoAccess = ${aia}" >> openssl.cnf
fi
if test -n "$crl" ; then
echo "crlDistributionPoints = ${crl}" >> openssl.cnf
fi
cat >> openssl.cnf <<- EOF
[req_issued]
prompt = no
oid_section = req_oids
distinguished_name = req_issued_name
default_md = $DIGEST
[req_issued_name]
C=US
#stateOrProvinceName=SomeState
localityName=SomeCity
O=SomeOrg
EOF
#echo $DOMAIN | awk 'BEGIN {FS="."}{for(i=NF;i>0;i--){print NF-i ".domainComponent="$i;}}' >> openssl.cnf
#mail = $GIVENUSER
cat >> openssl.cnf <<- EOF
commonName = $commonname
[v3_issued]
#certificatePolicies=2.5.29.32.0${eku:+,${eku}}
subjectKeyIdentifier=hash
authorityKeyIdentifier=keyid:always
#authorityKeyIdentifier=keyid:always,issuer:always
EOF
if test -n "$aia" ; then
echo "authorityInfoAccess = ${aia}" >> openssl.cnf
fi
if test -n "$crl" ; then
echo "crlDistributionPoints = ${crl}" >> openssl.cnf
fi
if test -n "$keyusage" ; then
echo "keyUsage = critical,${keyusage}" >> openssl.cnf
fi
if test -n "$altnames" ; then
echo "subjectAltName = ${altnames}" >> openssl.cnf
fi
if test -n "$eku" ; then
echo "extendedKeyUsage = ${eku}" >> openssl.cnf
:
fi
if test "x$CA" = xTRUE ; then
echo "basicConstraints=critical,CA:TRUE" >> openssl.cnf
echo 'nsComment="Testing CA Certificate for '"$commonname"'"' >> openssl.cnf
else
echo "basicConstraints=CA:FALSE" >> openssl.cnf
echo 'nsComment="Testing Certificate for '"$commonname"'"' >> openssl.cnf
fi
for value in $principals; do
user=`echo "$value" | cut -f1 -d@`
realm=`echo "$value" | cut -f2- -d@`
echo "" >> openssl.cnf
echo "[$value]" >> openssl.cnf
echo "realm=EXPLICIT:0,GeneralString:$realm" >> openssl.cnf
echo "kerberosname=EXPLICIT:1,SEQUENCE:krb5$user" >> openssl.cnf
echo "" >> openssl.cnf
echo "[krb5$user]" >> openssl.cnf
echo "nametype=EXPLICIT:0,INTEGER:1" >> openssl.cnf
echo "namelist=EXPLICIT:1,SEQUENCE:krb5basic$user" >> openssl.cnf
echo "[krb5basic$user]" >> openssl.cnf
count=0
for part in `echo "$user" | sed 's,/, ,g'` ; do
echo "$count.part=GeneralString:$part" >> openssl.cnf
count=`expr "$count" + 1`
done
done
# Create the data files for a new CA.
if ! test -s ca.srl ; then
(dd if=/dev/urandom bs=8 count=1 2> /dev/null) | od -t x1c | head -n 1 | awk '{$1="00";OFS="";print}' > ca.srl
else
echo "You already have a ca.srl file; not replacing."
fi
if ! test -s ca.db ; then
touch ca.db
else
echo "You already have a ca.db file; not replacing."
fi
if ! test -s ca.db.attr ; then
touch ca.db.attr
else
echo "You already have a ca.db.attr file; not replacing."
fi
# If we need a CA key, generate one.
if ! test -s ca.key ; then
umask=`umask -p`
umask 077
keygen ca > ca.key 2> /dev/null
$umask
else
echo "You already have a ca.key file; not replacing."
done=echo
fi
# If we need a CA certificate, generate one.
if ! test -s ca.crt ; then
sed -i -e 's,^\[req_ca\]$,\[req\],g' `pwd`/openssl.cnf
openssl req -config `pwd`/openssl.cnf -new -key ca.key > ca.csr 2> /dev/null -passin pass:shim
sed -i -e 's,^\[req\]$,\[req_ca\],g' `pwd`/openssl.cnf
openssl x509 -extfile `pwd`/openssl.cnf -CAserial ca.srl -signkey ca.key -extensions v3_ca -req -in ca.csr -days $DAYS -out ca.crt ; : 2> /dev/null
openssl x509 -noout -text -in ca.crt > ca.txt
cat ca.crt >> ca.txt
cat ca.txt > ca.crt
rm ca.txt
cat ca.crt > ca.chain.crt
else
echo "You already have a ca.crt file; not replacing."
done=echo
fi
# If we need an OCSP key, generate one.
if ! test -s ocsp.key ; then
umask=`umask -p`
umask 077
keygen ocsp > ocsp.key 2> /dev/null
$umask
else
echo "You already have an ocsp.key file; not replacing."
done=echo
fi
# Generate the OCSP signing cert. Set the X.509v3 basic constraints and EKU.
if ! test -s ocsp.crt ; then
sed -i -e 's,^\[req_ocsp\]$,\[req\],g' `pwd`/openssl.cnf
openssl req -config `pwd`/openssl.cnf -new -key ocsp.key > ocsp.csr 2> /dev/null
sed -i -e 's,^\[req\]$,\[req_ocsp\],g' `pwd`/openssl.cnf
openssl ca -batch -config `pwd`/openssl.cnf -extensions v3_ocsp -preserveDN -in ocsp.csr -days $DAYS -out ocsp.crt 2> /dev/null
openssl x509 -noout -text -in ocsp.crt > ocsp.txt
cat ocsp.crt >> ocsp.txt
cat ocsp.txt > ocsp.crt
rm ocsp.txt
else
echo "You already have an ocsp.crt file; not replacing."
done=echo
fi
# If we were told to revoke the certificate with the specified common name,
# do so.
if $revoke_cert ; then
openssl ca -config `pwd`/openssl.cnf -revoke "$commonname".crt
fi
# Always refresh the CRL.
openssl ca -config `pwd`/openssl.cnf -gencrl ${CRLHOURS:+-crlhours ${CRLHOURS}} ${CRLDAYS:+-crldays ${CRLDAYS}} -out ca.crl.pem
openssl crl -in ca.crl.pem -outform der -out ca.crl
openssl crl -in ca.crl -inform der -noout -text > ca.crl.pem
openssl crl -in ca.crl -inform der >> ca.crl.pem
# If we were told to start up the mini OCSP server, do so.
if $ocsp_serve ; then
openssl ocsp -text -index `pwd`/ca.db -CA `pwd`/ca.crt -rsigner `pwd`/ocsp.crt -rkey `pwd`/ocsp.key -rother `pwd`/ocsp.crt -port "`cut -f3 -d/ ca.ocsp.uri.txt | sed -r 's,(^[^:]*),0.0.0.0,g'`"
exit 0
fi
# If we're just here to do a revocation or refresh the CRL, we're done.
if $revoke_cert || $refresh_crl ; then
exit 0
fi
# Create a new serial number and whatnot if this is a new sub-CA.
if test "x$CA" = xTRUE ; then
if ! test -d "$commonname" ; then
mkdir "$commonname"
fi
if ! test -s "$commonname/ca.srl" ; then
(dd if=/dev/urandom bs=8 count=1 2> /dev/null) | od -t x1c | head -n 1 | awk '{$1="00";OFS="";print}' > "$commonname/ca.srl"
else
echo "You already have a $commonname/ca.srl file; not replacing."
fi
if test -n "$aia" ; then
echo -n "$aiaval" > "$commonname/ca.ocsp.uri.txt"
fi
if test -n "$crl" ; then
echo -n "$crlval" > "$commonname/ca.crldp.uri.txt"
fi
echo "$DOMAIN" > "$commonname/ca.domain.txt"
touch "$commonname/ca.db" "$commonname/ca.db.attr"
cert="$commonname/ca.crt"
csr="$commonname/ca.csr"
key="$commonname/ca.key"
pem="$commonname/ca.pem"
pfx="$commonname/ca.p12"
ln -s ../`basename $0` "$commonname"/
else
cert="$commonname.crt"
csr="$commonname.csr"
key="$commonname.key"
pem="$commonname.pem"
pfx="$commonname.p12"
fi
# Generate the subject's certificate. Set the X.509v3 basic constraints.
if ! test -s "$cert" ; then
# Generate another key, unless we have a key or CSR.
if ! test -s "$key" && ! test -s "$csr" ; then
umask=`umask -p`
umask 077
keygen "$commonname" > "$key" 2> /dev/null
$umask
else
echo "You already have a $key or $csr file; not replacing."
done=echo
fi
if ! test -s "$csr" ; then
sed -i -e 's,^\[req_issued\]$,\[req\],g' `pwd`/openssl.cnf
openssl req -config `pwd`/openssl.cnf -new -key "$key" > "$csr" 2> /dev/null
sed -i -e 's,^\[req\]$,\[req_issued\],g' `pwd`/openssl.cnf
fi
openssl ca -batch -config `pwd`/openssl.cnf -extensions v3_issued -preserveDN -in "$csr" -days $DAYS -out "$cert" 2> /dev/null
openssl x509 -noout -text -in "$cert" > "$cert.txt"
cat "$cert" >> "$cert.txt"
cat "$cert.txt" > "$cert"
rm -f "$cert.txt"
else
echo "You already have a $cert file; not replacing."
done=echo
fi
if test -s ca.chain.crt ; then
chain=ca.chain.crt
else
chain=ca.crt
fi
if test "x$CA" = xTRUE ; then
cat "$chain" "$cert" > "$commonname/ca.chain.crt"
fi
# Create ca.pem and the subject's name.pem for the benefit of applications
# which expect both the private key and the certificate in one file.
umask=`umask -p`
umask 077
if ! test -s ca.pem ; then
cat ca.key ca.crt > ca.pem
else
echo "You already have a ca.pem file; not replacing."
done=echo
fi
if ! test -s "$pem" ; then
cat "$key" "$cert" > "$pem"
else
echo "You already have a $pem file; not replacing."
done=echo
fi
if ! test -s "$pfx" ; then
#openssl pkcs12 -export -inkey "$key" -in "$cert" -name "$commonname" -out "$pfx" -nodes -passout pass:qweqwe
openssl pkcs12 -export -inkey "$key" -in "$cert" -name "$commonname" -out "$pfx" -nodes -passout pass:
else
echo "You already have a $pfx file; not replacing."
done=echo
fi
$umask
$done
echo CA certificate:
openssl x509 -noout -issuer -in ca.crt | sed s,=\ ,\ ,g
openssl x509 -noout -subject -in ca.crt | sed s,=\ ,\ ,g
echo
echo End entity certificate:
openssl x509 -noout -issuer -in "$cert" | sed s,=\ ,\ ,g
openssl x509 -noout -subject -in "$cert" | sed s,=\ ,\ ,g
openssl x509 -noout -serial -in "$cert" | sed s,=,\ ,g
echo
echo PKCS12 bag:
openssl pkcs12 -in "$pfx" -nodes -nokeys -nocerts -info -passin pass:
#openssl pkcs12 -in "$pfx" -nodes -nokeys -nocerts -info -passin pass:qweqwe
echo
echo Verifying:
echo + openssl verify -CAfile "$chain" "$cert"
openssl verify -CAfile "$chain" "$cert"