Testsuite: regenerate CA trees with 2048-bit keys
[exim.git] / test / aux-fixed / exim-ca / genall
1 #!/bin/bash
2 #
3
4 set -e
5
6 # Debugging.  Set V for clica verbosity.
7 set -x
8 #V=
9 V='-v'
10
11 clica --help >/dev/null 2>&1
12
13 echo Ensure time is set to 2012/11/01 12:34
14 echo use -  date -u 110112342012
15 echo hit return when ready
16 read junk
17
18 # Main suite: RSA certs
19 for tld in com org net
20 do
21     iname="example.$tld"
22     idir=$iname
23
24 ####
25     # create CAs & server certs
26     rm -fr "$idir"
27
28     # create CA cert + templates
29     clica $V -D "$idir" -p password -B 2048 -I -N $iname -F -C http://crl.$iname/latest.crl -O http://oscp.$iname/
30
31     # create server certs
32     # -m <months>
33     clica $V -D $idir -p password -s 101 -S server1.$iname -m 301 \
34         -8 alternatename.server1.example.$tld,alternatename2.server1.example.$tld,*.test.ex
35     clica $V -D $idir -p password -s 102 -S revoked1.$iname -m 301
36     clica $V -D $idir -p password -s 103 -S expired1.$iname -m 1
37
38     clica $V -D $idir -p password -s 201 -S  server2.$iname -m 301 \
39         -3 'CN=clica CA rsa,O=example.com' -8 '*.test.ex'
40     clica $V -D $idir -p password -s 202 -S revoked2.$iname -m 301
41     clica $V -D $idir -p password -s 203 -S expired2.$iname -m 1
42
43 ####
44
45     # openssl seems to generate a file (ca_chain.pam) in an order it
46     # cannot then use (the key applies to the first cert in the file?).
47     # Generate a shuffled one.
48     for n in 1 2
49     do
50       cd $idir/server$n.$iname
51         openssl pkcs12 -in server$n.$iname.p12 -passin file:pwdfile -cacerts -out cacerts.pem -nokeys
52         cat server$n.$iname.pem cacerts.pem > fullchain.pem
53         rm cacerts.pem
54       cd ../..
55     done
56
57 ####
58
59     # generate unlocked keys and client cert bundles
60     for server in server1 revoked1 expired1 server2 revoked2 expired2
61     do
62         SDIR=$idir/$server.$iname
63         SPFX=$SDIR/$server.$iname
64         openssl rsa -in $SPFX.key -passin file:$SDIR/pwdfile -out $SPFX.unlocked.key
65         cat $SPFX.pem $iname/CA/Signer.pem >$SPFX.chain.pem
66     done
67
68 ####
69
70     # create OCSP reqs & resps
71     CADIR=$idir/CA
72     #give ourselves an OSCP key to work with
73     pk12util -o $CADIR/OCSP.p12 -n 'OCSP Signer rsa' -d $CADIR -K password -W password
74     openssl pkcs12 -in $CADIR/OCSP.p12 -passin pass:password -passout pass:password -nodes -nocerts -out $CADIR/OCSP.key
75
76     # also need variation from Signer
77     pk12util -o $CADIR/Signer.p12 -n 'Signing Cert rsa' -d $CADIR -K password -W password
78     openssl pkcs12 -in $CADIR/Signer.p12 -passin pass:password -passout pass:password -nodes -nocerts -out $CADIR/Signer.key
79
80     # create some index files for the ocsp responder to work with
81 # tab-sep
82 # 0: Revoked/Expired/Valid letter
83 # 1: Expiry date (ASN1_UTCTIME)
84 # 2: Revocation date
85 # 3: Serial no. (unique)
86 # 4: file
87 # 5: DN, index
88
89     cat >$CADIR/index.valid.txt <<EOF
90 V       130110200751Z           65      unknown CN=server1.$iname
91 V       130110200751Z           66      unknown CN=revoked1.$iname
92 V       130110200751Z           67      unknown CN=expired1.$iname
93 V       130110200751Z           c9      unknown CN=server2.$iname
94 V       130110200751Z           ca      unknown CN=revoked2.$iname
95 V       130110200751Z           cb      unknown CN=expired2.$iname
96 EOF
97     cat >$CADIR/index.revoked.txt <<EOF
98 R       130110200751Z   100201142709Z,superseded        65      unknown CN=server1.$iname
99 R       130110200751Z   100201142709Z,superseded        66      unknown CN=revoked1.$iname
100 R       130110200751Z   100201142709Z,superseded        67      unknown CN=expired1.$iname
101 R       130110200751Z   100201142709Z,superseded        c9      unknown CN=server2.$iname
102 R       130110200751Z   100201142709Z,superseded        ca      unknown CN=revoked2.$iname
103 R       130110200751Z   100201142709Z,superseded        cb      unknown CN=expired2.$iname
104 EOF
105
106     # Now create all the ocsp requests and responses
107     IVALID="-index $CADIR/index.valid.txt"
108     IREVOKED="-index $CADIR/index.revoked.txt"
109
110     echo "unique_subject = yes" > $CADIR/index.valid.txt.attr
111     echo "unique_subject = yes" > $CADIR/index.revoked.txt.attr
112
113     for server in server1 revoked1 expired1 server2 revoked2 expired2
114     do
115         SPFX=$idir/$server.$iname/$server.$iname
116         openssl ocsp -issuer $CADIR/Signer.pem -sha256 -cert $SPFX.pem -no_nonce -reqout $SPFX.ocsp.req
117         REQIN="-reqin $SPFX.ocsp.req"
118
119         OGENCOMMON="-rsigner $CADIR/OCSP.pem -rkey $CADIR/OCSP.key -CA $CADIR/Signer.pem -noverify"
120         openssl ocsp $IVALID   $OGENCOMMON -ndays 3652 $REQIN -respout $SPFX.ocsp.good.resp
121         openssl ocsp $IVALID   $OGENCOMMON -ndays 30   $REQIN -respout $SPFX.ocsp.dated.resp
122         openssl ocsp $IREVOKED $OGENCOMMON -ndays 3652 $REQIN -respout $SPFX.ocsp.revoked.resp
123
124         OGENCOMMON="-rsigner $CADIR/Signer.pem -rkey $CADIR/Signer.key -CA $CADIR/Signer.pem -noverify"
125         openssl ocsp $IVALID   $OGENCOMMON -ndays 3652 $REQIN -respout $SPFX.ocsp.signer.good.resp
126         openssl ocsp $IVALID   $OGENCOMMON -ndays 30   $REQIN -respout $SPFX.ocsp.signer.dated.resp
127         openssl ocsp $IREVOKED $OGENCOMMON -ndays 3652 $REQIN -respout $SPFX.ocsp.signer.revoked.resp
128
129         OGENCOMMON="-rsigner $CADIR/Signer.pem -rkey $CADIR/Signer.key -CA $CADIR/Signer.pem -resp_no_certs -noverify"
130         openssl ocsp $IVALID   $OGENCOMMON -ndays 3652 $REQIN -respout $SPFX.ocsp.signernocert.good.resp
131         openssl ocsp $IVALID   $OGENCOMMON -ndays 30   $REQIN -respout $SPFX.ocsp.signernocert.dated.resp
132         openssl ocsp $IREVOKED $OGENCOMMON -ndays 3652 $REQIN -respout $SPFX.ocsp.signernocert.revoked.resp
133     done
134 ####
135 done
136
137 # Create one EC leaf cert in the RSA cert tree.  It will have an EC pubkey but be signed using its parent
138 # therefore its parent's algo, RSA.
139 clica $V -D example.com -p password -k ec -q nistp521 -s 1101 -S server1_ec.example.com -m 301 -8 'server1.example.com,*.test.ex'
140 SDIR=example.com/server1_ec.example.com
141 SPFX=$SDIR/server1_ec.example.com
142 openssl ec -in $SPFX.key -passin file:$SDIR/pwdfile -out $SPFX.unlocked.key
143 cat $SPFX.pem example.com/CA/Signer.pem >$SPFX.chain.pem
144
145
146
147 ###############################################################################
148 # Limited suite: EC certs
149 # separate trust root & chain
150 # .com only, server1 good only, no ocsp
151 # with server1 in SAN of leaf
152
153 for tld in com
154 do
155     iname="example_ec.$tld"
156     idir=$iname
157
158 ####
159     # create CAs & server certs
160     rm -fr "$idir"
161
162     # create CA cert + templates
163     clica $V -D "$idir" -p password -B 2048 -I -N $iname -F \
164         -k ec -q nistp521 \
165         -C http://crl.example.$tld/latest.crl -O http://oscp.example.$tld/
166
167     # create server certs
168     # -m <months>
169     clica $V -D $idir -p password -s 2101 -S server1.$iname -m 301 \
170         -k ec -q nistp521 \
171         -8 server1.example.$tld,alternatename.server1.example.$tld,alternatename2.server1.example.$tld,*.test.ex
172
173 ####
174
175     # openssl seems to generate a file (ca_chain.pam) in an order it
176     # cannot then use (the key applies to the first cert in the file?).
177     # Generate a shuffled one.
178     cd $idir/server1.$iname
179         openssl pkcs12 -in server1.$iname.p12 -passin file:pwdfile -cacerts -out cacerts.pem -nokeys
180         cat server1.$iname.pem cacerts.pem > fullchain.pem
181         rm cacerts.pem
182     cd ../..
183
184 ####
185
186     # generate unlocked keys and client cert bundles
187     for server in server1
188     do
189         SDIR=$idir/$server.$iname
190         SPFX=$SDIR/$server.$iname
191         openssl ec -in $SPFX.key -passin file:$SDIR/pwdfile -out $SPFX.unlocked.key
192         cat $SPFX.pem $idir/CA/Signer.pem >$SPFX.chain.pem
193     done
194
195 ####
196     # create OCSP reqs & resps
197     CADIR=$idir/CA
198     #give ourselves an OSCP key to work with
199     pk12util -o $CADIR/OCSP.p12 -n 'OCSP Signer ec' -d $CADIR -K password -W password
200     openssl pkcs12 -in $CADIR/OCSP.p12 -passin pass:password -passout pass:password -nodes -nocerts -out $CADIR/OCSP.key
201
202     # create some index files for the ocsp responder to work with
203 # tab-sep
204 # 0: Revoked/Expired/Valid letter
205 # 1: Expiry date (ASN1_UTCTIME)
206 # 2: Revocation date
207 # 3: Serial no. (unique)
208 # 4: file
209 # 5: DN, index
210
211     cat >$CADIR/index.valid.txt <<EOF
212 V       130110200751Z           65      unknown CN=server1.$iname
213 EOF
214
215     # Now create all the ocsp requests and responses
216     IVALID="-index $CADIR/index.valid.txt"
217     for server in server1
218     do
219         SPFX=$idir/$server.$iname/$server.$iname
220         openssl ocsp -issuer $CADIR/Signer.pem -sha256 -cert $SPFX.pem -no_nonce -reqout $SPFX.ocsp.req
221         REQIN="-reqin $SPFX.ocsp.req"
222
223         OGENCOMMON="-rsigner $CADIR/OCSP.pem -rkey $CADIR/OCSP.key -CA $CADIR/Signer.pem -noverify"
224         openssl ocsp $IVALID   $OGENCOMMON -ndays 3652 $REQIN -respout $SPFX.ocsp.good.resp
225     done
226 ####
227 done
228
229 ###############################################################################
230
231 echo Please to reset date to now.
232 echo 'service ntpdate start (not on a systemd though...)'
233 echo 
234 echo Then hit return
235 read junk
236
237
238
239 # Create CRL files in .der and .pem
240 # empty versions, and ones with the revoked servers
241 DATENOW=`date -u +%Y%m%d%H%M%SZ`
242 for tld in com org net
243 do
244     CADIR=example.$tld/CA
245     CRLIN=$CADIR/crl.empty.in.txt
246     echo "update=$DATENOW " >$CRLIN
247     crlutil -G -d $CADIR -f $CADIR/pwdfile \
248         -n 'Signing Cert rsa' -c $CRLIN -o $CADIR/crl.empty
249     openssl crl -in $CADIR/crl.empty -inform der -out $CADIR/crl.empty.pem
250 done
251 sleep 2
252 DATENOW=`date -u +%Y%m%d%H%M%SZ`
253 for tld in com org net
254 do
255     CADIR=example.$tld/CA
256     CRLIN=$CADIR/crl.v2.in.txt
257     echo "update=$DATENOW " >$CRLIN
258     echo "addcert 102 $DATENOW" >>$CRLIN
259     echo "addcert 202 $DATENOW" >>$CRLIN
260     crlutil -G -d $CADIR -f $CADIR/pwdfile \
261         -n 'Signing Cert rsa' -c $CRLIN -o $CADIR/crl.v2
262     openssl crl -in $CADIR/crl.v2 -inform der -out $CADIR/crl.v2.pem
263
264     CRLIN=$CADIR/crl.Signer.in.txt
265     echo "update=$DATENOW " >$CRLIN
266     crlutil -G -d $CADIR -f $CADIR/pwdfile \
267         -n 'Certificate Authority rsa' -c $CRLIN -o $CADIR/crl.Signer
268     openssl crl -in $CADIR/crl.Signer -inform der -out $CADIR/crl.Signer.pem
269
270     cat $CADIR/crl.Signer.pem $CADIR/crl.v2.pem > $CADIR/crl.chain.pem
271 done
272
273 # Finally, a single certificate-directory
274 cd example.com/server1.example.com
275 mkdir -p certdir
276 cd certdir
277 f=../../CA/CA.pem
278 h=`openssl x509 -hash -noout -in $f`
279 rm -f $h.0
280 ln -s $f $h.0
281 f=../../CA/Signer.pem
282 h=`openssl x509 -hash -noout -in $f`
283 rm -f $h.0
284 ln -s $f $h.0
285 cd ../../..
286
287 pwd
288 ls -l
289
290 find example* -type d -print0 | xargs -0 chmod 755
291 find example* -type f -print0 | xargs -0 chmod 644
292
293 echo "CA, Certificate, CRL and OSCP Response generation complete"