Debian Codegraph @

crt/s2n/tests/pems/mixed_chains/generate-certs.sh

raw symbols SCIP
1 #!/usr/bin/envscip-shell filesystem . . `/usr/bin/env`. bash
2
3 # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
4 # SPDX-License-Identifier: Apache-2.0
5
6 # Usage: ./generate_certs.sh [clean]
7 # Generates mixed-chain certs for testing
8 # Use argument "clean" to remove all generated certs
9
10 # immediately bail if any command fails
11 set -e
12
13 # Generates certs with given algorithms and bits in $1$2/, ex. ec384/
14 # $1: rsa or ec
15 # $2: size of the key used by the leaf and intermediate
16 # $3: size of the key used by the issuing CA
17 # $4: digest using in the certificate signatures
18 # $5: name of the output directory
19 cert-genscip-shell shell shell-project 0.0.0 cert-gen(). () {
20 key_family=$1
21 key_size=$2
22 ca_key_size=$3
23 digest=$4
24 dir_name=$5
25
26 echoscip-shell system . . echo. -e "\n----- generating certs for ec $key_size with $digest $signature -----\n"
27
28 # make directory for certs
29 mkdirscip-shell system . . mkdir. -p $dir_name
30 cd $dir_name
31
32 echoscip-shell system . . echo. "generating CA private key and certificate"
33 opensslscip-shell system . . openssl. req -new -noenc -x509 \
34 -newkey ec \
35 -pkeyopt ec_paramgen_curve:P-$ca_key_size \
36 -keyout ca-key.pem \
37 -out ca-cert.pem \
38 -days 65536 \
39 -$digest \
40 -subj "/C=US/CN=rootscip-shell filesystem . . `/C=US/CN=root`." \
41 -addext "basicConstraints = critical,CA:true" \
42 -addext "keyUsage = critical,keyCertSign"
43
44 echoscip-shell system . . echo. "generating intermediate private key and CSR"
45 opensslscip-shell system . . openssl. req -new -noenc \
46 -newkey ec \
47 -pkeyopt ec_paramgen_curve:P-$key_size \
48 -keyout intermediate-key.pem \
49 -out intermediate.csr \
50 -subj "/C=US/CN=branchscip-shell filesystem . . `/C=US/CN=branch`." \
51 -addext "basicConstraints = critical,CA:true" \
52 -addext "keyUsage = critical,keyCertSign"
53
54 echoscip-shell system . . echo. "generating server private key and CSR"
55 opensslscip-shell system . . openssl. req -new -noenc \
56 -newkey ec \
57 -pkeyopt ec_paramgen_curve:P-$key_size \
58 -keyout server-key.pem \
59 -out server.csr \
60 -subj "/C=US/CN=leafscip-shell filesystem . . `/C=US/CN=leaf`." \
61 -addext "subjectAltName = DNS:localhost"
62
63 echoscip-shell system . . echo. "generating intermediate certificate and signing it"
64 opensslscip-shell system . . openssl. x509 -days 65536 \
65 -req -in intermediate.csr \
66 -$digest \
67 -CA ca-cert.pem \
68 -CAkey ca-key.pem \
69 -CAcreateserial \
70 -out intermediate-cert.pem \
71 -copy_extensions=copyall
72
73 echoscip-shell system . . echo. "generating server certificate and signing it"
74 opensslscip-shell system . . openssl. x509 -days 65536 \
75 -req -in server.csr \
76 -$digest \
77 -CA intermediate-cert.pem \
78 -CAkey intermediate-key.pem \
79 -CAcreateserial -out server-cert.pem \
80 -copy_extensions=copyall
81
82 touchscip-shell system . . touch. server-chain.pem
83 catscip-shell system . . cat. server-cert.pem >> server-chain.pem
84 catscip-shell system . . cat. intermediate-cert.pem >> server-chain.pem
85 catscip-shell system . . cat. ca-cert.pem >> server-chain.pem
86
87 echoscip-shell system . . echo. "verifying server certificates"
88 opensslscip-shell system . . openssl. verify -CAfile ca-cert.pem intermediate-cert.pem
89 opensslscip-shell system . . openssl. verify -CAfile ca-cert.pem -untrusted intermediate-cert.pem server-cert.pem
90
91 # certificate signing requests are never used after the certs are generated
92 rmscip-shell system . . rm. server.csr
93 rmscip-shell system . . rm. intermediate.csr
94
95 # serial files are generated during the signing process, but are not used
96 rmscip-shell system . . rm. ca-cert.srl
97 rmscip-shell system . . rm. intermediate-cert.srl
98
99 # the private keys of the CA and the intermediate CA are never needed after
100 # signing
101 rmscip-shell system . . rm. ca-key.pem
102 rmscip-shell system . . rm. intermediate-key.pem
103
104 # the intermediate and server certs are included in server-chain.pem, so
105 # the individual files can be deleted
106 rmscip-shell system . . rm. intermediate-cert.pem
107 rmscip-shell system . . rm. server-cert.pem
108
109 cd ..
110 }
111
112 if [[ $1 != "clean" ]]
113 then
114 # key key_size ca_key_size digest directory
115 cert-genscip-shell shell shell-project 0.0.0 cert-gen(). ec 384 256 SHA384 ecdsa
116 else
117 echoscip-shell system . . echo. "cleaning certs"
118 rmscip-shell system . . rm. -rf ecdsa*
119 fi