Debian Codegraph @

make-certs

raw
1 #!/usr/bin/env bash
2 #
3 # Generate a root CA cert for signing, and then a subject cert.
4 # Usage: make-certs.sh hostname [user[@domain]] [more ...]
5 # For testing only, probably still has some bugs in it.
6 #
7
8 set -e
9
10 if [[ ! -f `which openssl` ]]; then
11 echo "OpenSSL not found. Install it first, then run this script again."
12 exit 1
13 fi
14
15 DOMAIN=xn--u4h.net
16 DAYS=365
17 KEYTYPE=RSA
18 KEYSIZE=2048
19 DIGEST=SHA256
20 CRLHOURS=24
21 CRLDAYS=
22
23 # Cleanup temporary files at exit.
24 touch openssl.cnf
25 newcertdir=`mktemp -d`
26 cleanup() {
27 test -f openssl.cnf && rm -f openssl.cnf
28 test -f ca.txt && rm -f ca.txt
29 test -f ocsp.txt && rm -f ocsp.txt
30 test -n "$newcertdir" && rm -fr "$newcertdir"
31 }
32 trap cleanup EXIT
33
34 # The first argument is either a common name value or a flag indicating that
35 # we're doing something other than issuing a cert.
36 commonname="$1"
37 refresh_crl=false
38 revoke_cert=false
39 ocsp_serve=false
40 if test "x$commonname" = "x-refresh-crl" ; then
41 refresh_crl=true
42 commonname="$1"
43 fi
44 if test "x$commonname" = "x-refresh_crl" ; then
45 refresh_crl=true
46 commonname="$1"
47 fi
48 if test "x$commonname" = "x-revoke" ; then
49 revoke_cert=true
50 shift
51 commonname="$1"
52 fi
53 if test "x$commonname" = "x-ocsp" ; then
54 ocsp_serve=true
55 commonname="$1"
56 fi
57 if test "x$commonname" = x ; then
58 echo Usage: `basename $0` 'commonname' user'[@domain]' '[more [...]]'
59 echo Usage: `basename $0` -revoke 'commonname'
60 echo Usage: `basename $0` -ocsp
61 echo Usage: `basename $0` -refresh-crl
62 echo More:
63 echo -e \\tKey usage: "[sign|signing|encrypt|encryption|all]"
64 echo -e \\tAuthority Access Info OCSP responder: "ocsp:URI"
65 echo -e \\tCRL distribution point: "crl:URI"
66 echo -e \\tSubject Alternative Name:
67 echo -e \\t\\tHostname: "*"
68 echo -e \\t\\tIP address: w.x.y.z
69 echo -e \\t\\tEmail address: "*@*.com/edu/net/org/local"
70 echo -e \\t\\tKerberos principal name: "*@*.COM/EDU/NET/ORG/LOCAL"
71 echo -e \\tExtended key usage:
72 echo -e \\t\\t1....
73 echo -e \\t\\t2....
74 echo -e \\t\\tid-kp-server-auth \| tls-server
75 echo -e \\t\\tid-kp-client-auth \| tls-client
76 echo -e \\t\\tid-kp-email-protection \| email
77 echo -e \\t\\tid-ms-kp-sc-logon \| id-ms-sc-logon
78 echo -e \\t\\tid-pkinit-kp-client-auth \| id-pkinit-client
79 echo -e \\t\\tid-pkinit-kp-kdc \| id-pkinit-kdc
80 echo -e \\t\\tca \| CA
81 exit 1
82 fi
83
84 # Choose a user name part for email attributes.
85 GIVENUSER=$2
86 test x"$GIVENUSER" = x && GIVENUSER=$USER
87 echo "$GIVENUSER" | grep -q @ || GIVENUSER="$GIVENUSER"@$DOMAIN
88 DOMAIN=`echo "$GIVENUSER" | cut -f2- -d@`
89
90 shift || true
91 shift || true
92
93 # Done already?
94 done=:
95
96 keygen() {
97 case "$KEYTYPE" in
98 DSA)
99 openssl dsaparam -out "$1".param $KEYSIZE
100 openssl gendsa "$1".param
101 ;;
102 RSA|*)
103 #openssl genrsa $KEYSIZE -passout pass:qweqwe
104 openssl genrsa $KEYSIZE
105 #openssl genrsa $KEYSIZE -nodes
106 ;;
107 esac
108 }
109
110 # Set some defaults.
111 CA=FALSE
112 if test -s ca.crldp.uri.txt ; then
113 crlval="`cat ca.crldp.uri.txt`"
114 crl="URI:$crlval"
115 fi
116 if test -s ca.ocsp.uri.txt ; then
117 aiaval="`cat ca.ocsp.uri.txt`"
118 aia="OCSP;URI:$aiaval"
119 fi
120 if test -s ca.domain.txt ; then
121 domval="`cat ca.domain.txt`"
122 if test -n "$domval" ; then
123 DOMAIN="$domval"
124 fi
125 fi
126
127 # Parse the arguments which indicate what sort of information we want.
128 while test $# -gt 0 ; do
129 type=
130 value="$1"
131 case "$value" in
132 RSA|rsa)
133 KEYTYPE=RSA
134 ;;
135 DSA|dsa)
136 KEYTYPE=DSA
137 ;;
138 OCSP:*|ocsp:*)
139 aiaval=`echo "$value" | cut -f2- -d:`
140 aia="OCSP;URI:$aiaval"
141 ;;
142 CRL:*|crl:*)
143 crlval=`echo "$value" | cut -f2- -d:`
144 crl="URI:$crlval"
145 ;;
146 signing|sign)
147 keyusage="${keyusage:+${keyusage},}nonRepudiation,digitalSignature"
148 ;;
149 encryption|encrypt)
150 keyusage="${keyusage:+${keyusage},}keyEncipherment,dataEncipherment"
151 ;;
152 all)
153 keyusage="digitalSignature,nonRepudiation,keyEncipherment,dataEncipherment,keyAgreement,keyCertSign,cRLSign,encipherOnly,decipherOnly"
154 ;;
155 ca|CA)
156 CA=TRUE
157 keyusage="${keyusage:+${keyusage},}nonRepudiation,digitalSignature,keyEncipherment,dataEncipherment,keyAgreement,keyCertSign,cRLSign"
158 ;;
159 1.*|2.*|id-*|tls-*|email|mail|codesign)
160 ekuval=`echo "$value" | tr '[A-Z]' '[a-z]' | sed 's,\-,,g'`
161 case "$ekuval" in
162 idkpserverauth|tlsserver) ekuval=1.3.6.1.5.5.7.3.1;;
163 idkpclientauth|tlsclient) ekuval=1.3.6.1.5.5.7.3.2;;
164 idkpemailprotection|email|mail) ekuval=1.3.6.1.5.5.7.3.4;;
165 idkpcodesign|codesign) ekuval=1.3.6.1.5.5.7.3.3;;
166 idmskpsclogon|idmssclogon) ekuval=1.3.6.1.4.1.311.20.2.2;;
167 idpkinitkpclientauth|idpkinitclient) ekuval=1.3.6.1.5.2.3.4;;
168 idpkinitkpkdc|idpkinitkdc) ekuval=1.3.6.1.5.2.3.5;;
169 esac
170 if test -z "$eku" ; then
171 eku="$ekuval"
172 else
173 eku="$eku,$ekuval"
174 fi
175 ;;
176 *@*.COM|*@*.EDU|*@*.NET|*@*.ORG|*@*.LOCAL)
177 luser=`echo "$value" | tr '[A-Z]' '[a-z]'`
178 if test "$luser" = "$value" ; then
179 luser=
180 fi
181 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"
182 unset luser
183 principals="$principals $value"
184 ;;
185 *@*.com|*@*.edu|*@*.net|*@*.org|*@*.local) type=email;;
186 [0-9]*.[0-9]*.[0-9]*.[0-9]*) type=IP;;
187 *) type=DNS;;
188 esac
189 if test -n "$type" ; then
190 newvalue="${type}:$value"
191 if test -z "$altnames" ; then
192 altnames="${newvalue}"
193 else
194 altnames="${altnames},${newvalue}"
195 fi
196 fi
197 shift
198 done
199
200 # Build the configuration file, including bits on how to construct the CA
201 # certificate, an OCSP responder certificate, and the issued certificate.
202 cat > openssl.cnf <<- EOF
203 [ca]
204 default_ca = issuer
205
206 [issuer]
207 private_key = `pwd`/ca.key
208 certificate = `pwd`/ca.crt
209 database = `pwd`/ca.db
210 serial = `pwd`/ca.srl
211 default_md = $DIGEST
212 new_certs_dir = $newcertdir
213 policy = no_policy
214
215 [no_policy]
216
217 [req_oids]
218 domainComponent = 0.9.2342.19200300.100.1.25
219
220 [req_ca]
221 prompt = no
222 oid_section = req_oids
223 distinguished_name = req_ca_name
224 default_md = $DIGEST
225 subjectKeyIdentifier=hash
226
227 [req_ca_name]
228 C=US
229 #stateOrProvinceName=SomeState
230 localityName=SomeCity
231 O=SomeOrg
232 EOF
233 #echo $DOMAIN | awk 'BEGIN {FS="."}{for(i=NF;i>0;i--){print NF-i ".domainComponent="$i;}}' >> openssl.cnf
234 cat >> openssl.cnf <<- EOF
235 #commonName = Test Certifying CA
236
237 [v3_ca]
238 subjectKeyIdentifier=hash
239 authorityKeyIdentifier=keyid:always
240 #authorityKeyIdentifier=keyid:always,issuer:always
241 keyUsage=nonRepudiation,digitalSignature,keyEncipherment,dataEncipherment,keyAgreement,keyCertSign,cRLSign
242 basicConstraints=critical,CA:TRUE
243 nsComment="Testing CA Certificate"
244 EOF
245 if test -n "$aia" ; then
246 echo "authorityInfoAccess = ${aia}" >> openssl.cnf
247 echo -n "$aiaval" > ca.ocsp.uri.txt
248 fi
249 if test -n "$crl" ; then
250 echo "crlDistributionPoints = ${crl}" >> openssl.cnf
251 echo -n "$crlval" > ca.crldp.uri.txt
252 fi
253 echo "$DOMAIN" > ca.domain.txt
254 cat >> openssl.cnf <<- EOF
255
256 [req_ocsp]
257 prompt = no
258 oid_section = req_oids
259 distinguished_name = req_ocsp_name
260 default_md = $DIGEST
261
262 [req_ocsp_name]
263 C=US
264 #stateOrProvinceName=SomeState
265 localityName=SomeOrg
266 O=SomeOrg
267 EOF
268 #echo $DOMAIN | awk 'BEGIN {FS="."}{for(i=NF;i>0;i--){print NF-i ".domainComponent="$i;}}' >> openssl.cnf
269 cat >> openssl.cnf <<- EOF
270 #commonName = OCSP Signer for Test Certifying CA
271
272 [v3_ocsp]
273 subjectKeyIdentifier=hash
274 #authorityKeyIdentifier=keyid:always,issuer:always
275 authorityKeyIdentifier=keyid:always
276 keyUsage=digitalSignature,nonRepudiation,keyEncipherment,dataEncipherment,keyAgreement,keyCertSign,cRLSign
277 extendedKeyUsage=1.3.6.1.5.5.7.3.9
278 #basicConstraints=CA:FALSE
279 basicConstraints=CA:TRUE
280 nsComment="Testing OCSP Certificate"
281 1.3.6.1.5.5.7.48.1.5=ASN1:NULL
282 EOF
283 if test -n "$aia" ; then
284 echo "authorityInfoAccess = ${aia}" >> openssl.cnf
285 fi
286 if test -n "$crl" ; then
287 echo "crlDistributionPoints = ${crl}" >> openssl.cnf
288 fi
289 cat >> openssl.cnf <<- EOF
290
291 [req_issued]
292 prompt = no
293 oid_section = req_oids
294 distinguished_name = req_issued_name
295 default_md = $DIGEST
296
297 [req_issued_name]
298 C=US
299 #stateOrProvinceName=SomeState
300 localityName=SomeCity
301 O=SomeOrg
302 EOF
303 #echo $DOMAIN | awk 'BEGIN {FS="."}{for(i=NF;i>0;i--){print NF-i ".domainComponent="$i;}}' >> openssl.cnf
304 #mail = $GIVENUSER
305 cat >> openssl.cnf <<- EOF
306 commonName = $commonname
307
308 [v3_issued]
309 #certificatePolicies=2.5.29.32.0${eku:+,${eku}}
310 subjectKeyIdentifier=hash
311 authorityKeyIdentifier=keyid:always
312 #authorityKeyIdentifier=keyid:always,issuer:always
313 EOF
314 if test -n "$aia" ; then
315 echo "authorityInfoAccess = ${aia}" >> openssl.cnf
316 fi
317 if test -n "$crl" ; then
318 echo "crlDistributionPoints = ${crl}" >> openssl.cnf
319 fi
320 if test -n "$keyusage" ; then
321 echo "keyUsage = critical,${keyusage}" >> openssl.cnf
322 fi
323 if test -n "$altnames" ; then
324 echo "subjectAltName = ${altnames}" >> openssl.cnf
325 fi
326 if test -n "$eku" ; then
327 echo "extendedKeyUsage = ${eku}" >> openssl.cnf
328 :
329 fi
330 if test "x$CA" = xTRUE ; then
331 echo "basicConstraints=critical,CA:TRUE" >> openssl.cnf
332 echo 'nsComment="Testing CA Certificate for '"$commonname"'"' >> openssl.cnf
333 else
334 echo "basicConstraints=CA:FALSE" >> openssl.cnf
335 echo 'nsComment="Testing Certificate for '"$commonname"'"' >> openssl.cnf
336 fi
337 for value in $principals; do
338 user=`echo "$value" | cut -f1 -d@`
339 realm=`echo "$value" | cut -f2- -d@`
340 echo "" >> openssl.cnf
341 echo "[$value]" >> openssl.cnf
342 echo "realm=EXPLICIT:0,GeneralString:$realm" >> openssl.cnf
343 echo "kerberosname=EXPLICIT:1,SEQUENCE:krb5$user" >> openssl.cnf
344
345 echo "" >> openssl.cnf
346 echo "[krb5$user]" >> openssl.cnf
347 echo "nametype=EXPLICIT:0,INTEGER:1" >> openssl.cnf
348 echo "namelist=EXPLICIT:1,SEQUENCE:krb5basic$user" >> openssl.cnf
349
350 echo "[krb5basic$user]" >> openssl.cnf
351 count=0
352 for part in `echo "$user" | sed 's,/, ,g'` ; do
353 echo "$count.part=GeneralString:$part" >> openssl.cnf
354 count=`expr "$count" + 1`
355 done
356 done
357
358 # Create the data files for a new CA.
359 if ! test -s ca.srl ; then
360 (dd if=/dev/urandom bs=8 count=1 2> /dev/null) | od -t x1c | head -n 1 | awk '{$1="00";OFS="";print}' > ca.srl
361 else
362 echo "You already have a ca.srl file; not replacing."
363 fi
364 if ! test -s ca.db ; then
365 touch ca.db
366 else
367 echo "You already have a ca.db file; not replacing."
368 fi
369 if ! test -s ca.db.attr ; then
370 touch ca.db.attr
371 else
372 echo "You already have a ca.db.attr file; not replacing."
373 fi
374
375 # If we need a CA key, generate one.
376 if ! test -s ca.key ; then
377 umask=`umask -p`
378 umask 077
379 keygen ca > ca.key 2> /dev/null
380 $umask
381 else
382 echo "You already have a ca.key file; not replacing."
383 done=echo
384 fi
385
386 # If we need a CA certificate, generate one.
387 if ! test -s ca.crt ; then
388 sed -i -e 's,^\[req_ca\]$,\[req\],g' `pwd`/openssl.cnf
389 openssl req -config `pwd`/openssl.cnf -new -key ca.key > ca.csr 2> /dev/null -passin pass:shim
390 sed -i -e 's,^\[req\]$,\[req_ca\],g' `pwd`/openssl.cnf
391 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
392 openssl x509 -noout -text -in ca.crt > ca.txt
393 cat ca.crt >> ca.txt
394 cat ca.txt > ca.crt
395 rm ca.txt
396 cat ca.crt > ca.chain.crt
397 else
398 echo "You already have a ca.crt file; not replacing."
399 done=echo
400 fi
401
402 # If we need an OCSP key, generate one.
403 if ! test -s ocsp.key ; then
404 umask=`umask -p`
405 umask 077
406 keygen ocsp > ocsp.key 2> /dev/null
407 $umask
408 else
409 echo "You already have an ocsp.key file; not replacing."
410 done=echo
411 fi
412
413 # Generate the OCSP signing cert. Set the X.509v3 basic constraints and EKU.
414 if ! test -s ocsp.crt ; then
415 sed -i -e 's,^\[req_ocsp\]$,\[req\],g' `pwd`/openssl.cnf
416 openssl req -config `pwd`/openssl.cnf -new -key ocsp.key > ocsp.csr 2> /dev/null
417 sed -i -e 's,^\[req\]$,\[req_ocsp\],g' `pwd`/openssl.cnf
418 openssl ca -batch -config `pwd`/openssl.cnf -extensions v3_ocsp -preserveDN -in ocsp.csr -days $DAYS -out ocsp.crt 2> /dev/null
419 openssl x509 -noout -text -in ocsp.crt > ocsp.txt
420 cat ocsp.crt >> ocsp.txt
421 cat ocsp.txt > ocsp.crt
422 rm ocsp.txt
423 else
424 echo "You already have an ocsp.crt file; not replacing."
425 done=echo
426 fi
427
428 # If we were told to revoke the certificate with the specified common name,
429 # do so.
430 if $revoke_cert ; then
431 openssl ca -config `pwd`/openssl.cnf -revoke "$commonname".crt
432 fi
433
434 # Always refresh the CRL.
435 openssl ca -config `pwd`/openssl.cnf -gencrl ${CRLHOURS:+-crlhours ${CRLHOURS}} ${CRLDAYS:+-crldays ${CRLDAYS}} -out ca.crl.pem
436 openssl crl -in ca.crl.pem -outform der -out ca.crl
437 openssl crl -in ca.crl -inform der -noout -text > ca.crl.pem
438 openssl crl -in ca.crl -inform der >> ca.crl.pem
439
440 # If we were told to start up the mini OCSP server, do so.
441 if $ocsp_serve ; then
442 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'`"
443 exit 0
444 fi
445
446 # If we're just here to do a revocation or refresh the CRL, we're done.
447 if $revoke_cert || $refresh_crl ; then
448 exit 0
449 fi
450
451 # Create a new serial number and whatnot if this is a new sub-CA.
452 if test "x$CA" = xTRUE ; then
453 if ! test -d "$commonname" ; then
454 mkdir "$commonname"
455 fi
456 if ! test -s "$commonname/ca.srl" ; then
457 (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"
458 else
459 echo "You already have a $commonname/ca.srl file; not replacing."
460 fi
461 if test -n "$aia" ; then
462 echo -n "$aiaval" > "$commonname/ca.ocsp.uri.txt"
463 fi
464 if test -n "$crl" ; then
465 echo -n "$crlval" > "$commonname/ca.crldp.uri.txt"
466 fi
467 echo "$DOMAIN" > "$commonname/ca.domain.txt"
468 touch "$commonname/ca.db" "$commonname/ca.db.attr"
469 cert="$commonname/ca.crt"
470 csr="$commonname/ca.csr"
471 key="$commonname/ca.key"
472 pem="$commonname/ca.pem"
473 pfx="$commonname/ca.p12"
474 ln -s ../`basename $0` "$commonname"/
475 else
476 cert="$commonname.crt"
477 csr="$commonname.csr"
478 key="$commonname.key"
479 pem="$commonname.pem"
480 pfx="$commonname.p12"
481 fi
482
483 # Generate the subject's certificate. Set the X.509v3 basic constraints.
484 if ! test -s "$cert" ; then
485 # Generate another key, unless we have a key or CSR.
486 if ! test -s "$key" && ! test -s "$csr" ; then
487 umask=`umask -p`
488 umask 077
489 keygen "$commonname" > "$key" 2> /dev/null
490 $umask
491 else
492 echo "You already have a $key or $csr file; not replacing."
493 done=echo
494 fi
495
496 if ! test -s "$csr" ; then
497 sed -i -e 's,^\[req_issued\]$,\[req\],g' `pwd`/openssl.cnf
498 openssl req -config `pwd`/openssl.cnf -new -key "$key" > "$csr" 2> /dev/null
499 sed -i -e 's,^\[req\]$,\[req_issued\],g' `pwd`/openssl.cnf
500 fi
501 openssl ca -batch -config `pwd`/openssl.cnf -extensions v3_issued -preserveDN -in "$csr" -days $DAYS -out "$cert" 2> /dev/null
502 openssl x509 -noout -text -in "$cert" > "$cert.txt"
503 cat "$cert" >> "$cert.txt"
504 cat "$cert.txt" > "$cert"
505 rm -f "$cert.txt"
506 else
507 echo "You already have a $cert file; not replacing."
508 done=echo
509 fi
510
511 if test -s ca.chain.crt ; then
512 chain=ca.chain.crt
513 else
514 chain=ca.crt
515 fi
516 if test "x$CA" = xTRUE ; then
517 cat "$chain" "$cert" > "$commonname/ca.chain.crt"
518 fi
519
520 # Create ca.pem and the subject's name.pem for the benefit of applications
521 # which expect both the private key and the certificate in one file.
522 umask=`umask -p`
523 umask 077
524 if ! test -s ca.pem ; then
525 cat ca.key ca.crt > ca.pem
526 else
527 echo "You already have a ca.pem file; not replacing."
528 done=echo
529 fi
530 if ! test -s "$pem" ; then
531 cat "$key" "$cert" > "$pem"
532 else
533 echo "You already have a $pem file; not replacing."
534 done=echo
535 fi
536 if ! test -s "$pfx" ; then
537 #openssl pkcs12 -export -inkey "$key" -in "$cert" -name "$commonname" -out "$pfx" -nodes -passout pass:qweqwe
538 openssl pkcs12 -export -inkey "$key" -in "$cert" -name "$commonname" -out "$pfx" -nodes -passout pass:
539 else
540 echo "You already have a $pfx file; not replacing."
541 done=echo
542 fi
543 $umask
544 $done
545
546 echo CA certificate:
547 openssl x509 -noout -issuer -in ca.crt | sed s,=\ ,\ ,g
548 openssl x509 -noout -subject -in ca.crt | sed s,=\ ,\ ,g
549 echo
550 echo End entity certificate:
551 openssl x509 -noout -issuer -in "$cert" | sed s,=\ ,\ ,g
552 openssl x509 -noout -subject -in "$cert" | sed s,=\ ,\ ,g
553 openssl x509 -noout -serial -in "$cert" | sed s,=,\ ,g
554 echo
555 echo PKCS12 bag:
556 openssl pkcs12 -in "$pfx" -nodes -nokeys -nocerts -info -passin pass:
557 #openssl pkcs12 -in "$pfx" -nodes -nokeys -nocerts -info -passin pass:qweqwe
558 echo
559 echo Verifying:
560 echo + openssl verify -CAfile "$chain" "$cert"
561 openssl verify -CAfile "$chain" "$cert"