Testsuite: testcase for Bug 2198
[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 1024 -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 1024 -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 example.$tld/CA/Signer.pem >$SPFX.chain.pem
193     done
194
195 done
196
197 ###############################################################################
198
199 echo Please to reset date to now.
200 echo 'service ntpdate start (not on a systemd though...)'
201 echo 
202 echo Then hit return
203 read junk
204
205
206
207 # Create CRL files in .der and .pem
208 # empty versions, and ones with the revoked servers
209 for tld in com org net
210 do
211     CADIR=example.$tld/CA
212     CRLIN=$CADIR/crl.empty.in.txt
213     DATENOW=`date -u +%Y%m%d%H%M%SZ`
214     echo "update=$DATENOW " >$CRLIN
215     crlutil -G -d $CADIR -f $CADIR/pwdfile \
216         -n 'Signing Cert rsa' -c $CRLIN -o $CADIR/crl.empty
217     openssl crl -in $CADIR/crl.empty -inform der -out $CADIR/crl.empty.pem
218 done
219 sleep 2
220 for tld in com org net
221 do
222     CADIR=example.$tld/CA
223     CRLIN=$CADIR/crl.v2.in.txt
224     DATENOW=`date -u +%Y%m%d%H%M%SZ`
225     echo "update=$DATENOW " >$CRLIN
226     echo "addcert 102 $DATENOW" >>$CRLIN
227     echo "addcert 202 $DATENOW" >>$CRLIN
228     crlutil -G -d $CADIR -f $CADIR/pwdfile \
229         -n 'Signing Cert rsa' -c $CRLIN -o $CADIR/crl.v2
230     openssl crl -in $CADIR/crl.v2 -inform der -out $CADIR/crl.v2.pem
231 done
232
233 # Finally, a single certificate-directory
234 cd example.com/server1.example.com
235 mkdir -p certdir
236 cd certdir
237 f=../../CA/CA.pem
238 h=`openssl x509 -hash -noout -in $f`
239 rm -f $h.0
240 ln -s $f $h.0
241 f=../../CA/Signer.pem
242 h=`openssl x509 -hash -noout -in $f`
243 rm -f $h.0
244 ln -s $f $h.0
245 cd ../../..
246
247 pwd
248 ls -l
249
250 find example* -type d -print0 | xargs -0 chmod 755
251 find example* -type f -print0 | xargs -0 chmod 644
252
253 echo "CA, Certificate, CRL and OSCP Response generation complete"