]> WPIA git - nre.git/blob - verify
upd: adjust duration of server certificates
[nre.git] / verify
1 #!/bin/bash
2 set -e
3 [ "$1" == "" ] && echo "Usage: $0 <year>" && exit 1
4 year=$1
5
6 . structure.bash
7 cd generated
8
9 verify(){ # crt, [untrusted], additional
10     untrusted="$2"
11     [[ "$untrusted" != "" ]] && untrusted="-untrusted $untrusted"
12     openssl verify $3 -CAfile root.ca/key.crt $untrusted "$1" || error "$1 did not verify"
13     echo openssl verify $3 -CAfile root.ca/key.crt $untrusted "$1" || error "$1 did not verify"
14 }
15
16 error() { # message
17     echo $1
18     exit -1
19 }
20
21 verifyExtlist() { # ext
22     EXTLIST=`echo "$1" | grep "X509v3\|Authority Information" | sed "s/^[ \t]*//"`
23     ADD="
24 X509v3 Certificate Policies: "
25     if [[ $2 == "root" ]]; then
26         ADD=""
27     fi
28     VAR="X509v3 extensions:
29 X509v3 Basic Constraints: critical
30 X509v3 Key Usage: critical
31 X509v3 Subject Key Identifier: 
32 X509v3 Authority Key Identifier: 
33 X509v3 CRL Distribution Points: 
34 Authority Information Access: $ADD"
35
36     diff <(echo "$EXTLIST") <(echo "$VAR") || error "Extensions order is wrong for $2"
37
38 }
39
40 # Verify root
41 verify root.ca/key.crt
42 verifyExtlist "$(openssl x509 -in "root.ca/key.crt" -noout -text)" root
43
44 # Verify level-1 structure
45 for ca in "${STRUCT_CAS[@]}"; do
46     verify $ca.ca/key.crt
47     verifyExtlist "$(openssl x509 -in "$ca.ca/key.crt" -noout -text)" "$ca"
48 done
49
50 # Verify level-2 (time) structure
51 for ca in "${STRUCT_CAS[@]}"; do
52     for i in "${TIME_IDX[@]}"; do
53         . ../CAs/$ca
54         CA_FILE=$year/ca/${ca}_${year}_${i}.crt
55         time=${points[${i}]}
56         timestamp=$(date --date="${time:0:2}/${time:2:2}/${year} 03:00:00 UTC" +"%s")
57         verify "$CA_FILE" "$ca.ca/key.crt" "-attime ${timestamp}"
58         EXT=`openssl x509 -in "$CA_FILE" -noout -text`
59
60         verifyExtlist "$EXT" "$ca-$i"
61
62         echo "$EXT" | grep "Subject: " | grep "CN=$name" > /dev/null || error "Subject field did not verify"
63
64         echo "$EXT" | grep -A 2 "Basic Constraints" | grep "CA:TRUE" > /dev/null || error "Basic Constraints field is wrong for $ca"
65         echo "$EXT" | grep -A 2 "Key Usage" | grep "^ *Certificate Sign, CRL Sign$" > /dev/null || error "KeyUsage field is wrong for $ca"
66
67         echo "$EXT" | grep -A 4 "CRL Distribution" | grep "g2.crl.${DOMAIN}/g2/$ca.crl" > /dev/null || error "CRL field is wrong for $ca"
68         echo "$EXT" | grep "CA Issuers" | grep "/$ca.crt" | grep "g2.crt.${DOMAIN}/g2/" > /dev/null || error "CA Issuers field is wrong for $ca"
69         echo "$EXT" | grep "OCSP" | grep "http://g2.ocsp.${DOMAIN}" > /dev/null || error "OCSP field is wrong for $ca"
70     done
71 done