Add a test that checks the store utility
[openssl.git] / test / certs / mkcert.sh
1 #! /bin/bash
2 #
3 # Copyright (c) 2016 Viktor Dukhovni <openssl-users@dukhovni.org>.
4 # All rights reserved.
5 #
6 # Contributed to the OpenSSL project under the terms of the OpenSSL license
7 # included with the version of the OpenSSL software that includes this module.
8
9 # 100 years should be enough for now
10 #
11 if [ -z "$DAYS" ]; then
12     DAYS=36525
13 fi
14
15 if [ -z "$OPENSSL_SIGALG" ]; then
16     OPENSSL_SIGALG=sha256
17 fi
18
19 if [ -z "$REQMASK" ]; then
20     REQMASK=utf8only
21 fi
22
23 stderr_onerror() {
24     (
25         err=$("$@" >&3 2>&1) || {
26             printf "%s\n" "$err" >&2
27             exit 1
28         }
29     ) 3>&1
30 }
31
32 key() {
33     local key=$1; shift
34
35     local alg=rsa
36     if [ -n "$OPENSSL_KEYALG" ]; then
37         alg=$OPENSSL_KEYALG
38     fi
39
40     local bits=2048
41     if [ -n "$OPENSSL_KEYBITS" ]; then
42         bits=$OPENSSL_KEYBITS
43     fi
44
45     if [ ! -f "${key}.pem" ]; then
46         args=(-algorithm "$alg")
47         case $alg in
48         rsa) args=("${args[@]}" -pkeyopt rsa_keygen_bits:$bits );;
49         ec)  args=("${args[@]}" -pkeyopt "ec_paramgen_curve:$bits")
50                args=("${args[@]}" -pkeyopt ec_param_enc:named_curve);;
51         dsa)  args=(-paramfile "$bits");;
52         ed25519)  ;;
53         *) printf "Unsupported key algorithm: %s\n" "$alg" >&2; return 1;;
54         esac
55         stderr_onerror \
56             openssl genpkey "${args[@]}" -out "${key}.pem"
57     fi
58 }
59
60 # Usage: $0 req keyname dn1 dn2 ...
61 req() {
62     local key=$1; shift
63
64     key "$key"
65     local errs
66
67     stderr_onerror \
68         openssl req -new -"${OPENSSL_SIGALG}" -key "${key}.pem" \
69             -config <(printf "string_mask=%s\n[req]\n%s\n%s\n[dn]\n" \
70               "$REQMASK" "prompt = no" "distinguished_name = dn"
71                       for dn in "$@"; do echo "$dn"; done)
72 }
73
74 req_nocn() {
75     local key=$1; shift
76
77     key "$key"
78     stderr_onerror \
79         openssl req -new -"${OPENSSL_SIGALG}" -subj / -key "${key}.pem" \
80             -config <(printf "[req]\n%s\n[dn]\nCN_default =\n" \
81                       "distinguished_name = dn")
82 }
83
84 cert() {
85     local cert=$1; shift
86     local exts=$1; shift
87
88     stderr_onerror \
89         openssl x509 -req -"${OPENSSL_SIGALG}" -out "${cert}.pem" \
90             -extfile <(printf "%s\n" "$exts") "$@"
91 }
92
93 genroot() {
94     local cn=$1; shift
95     local key=$1; shift
96     local cert=$1; shift
97     local skid="subjectKeyIdentifier = hash"
98     local akid="authorityKeyIdentifier = keyid"
99
100     exts=$(printf "%s\n%s\n%s\n" "$skid" "$akid" "basicConstraints = critical,CA:true")
101     for eku in "$@"
102     do
103         exts=$(printf "%s\nextendedKeyUsage = %s\n" "$exts" "$eku")
104     done
105     csr=$(req "$key" "CN = $cn") || return 1
106     echo "$csr" |
107        cert "$cert" "$exts" -signkey "${key}.pem" -set_serial 1 -days "${DAYS}"
108 }
109
110 genca() {
111     local cn=$1; shift
112     local key=$1; shift
113     local cert=$1; shift
114     local cakey=$1; shift
115     local cacert=$1; shift
116     local skid="subjectKeyIdentifier = hash"
117     local akid="authorityKeyIdentifier = keyid"
118
119     exts=$(printf "%s\n%s\n%s\n" "$skid" "$akid" "basicConstraints = critical,CA:true")
120     for eku in "$@"
121     do
122         exts=$(printf "%s\nextendedKeyUsage = %s\n" "$exts" "$eku")
123     done
124     if [ -n "$NC" ]; then
125         exts=$(printf "%s\nnameConstraints = %s\n" "$exts" "$NC")
126     fi
127     csr=$(req "$key" "CN = $cn") || return 1
128     echo "$csr" |
129         cert "$cert" "$exts" -CA "${cacert}.pem" -CAkey "${cakey}.pem" \
130             -set_serial 2 -days "${DAYS}"
131 }
132
133 gen_nonbc_ca() {
134     local cn=$1; shift
135     local key=$1; shift
136     local cert=$1; shift
137     local cakey=$1; shift
138     local cacert=$1; shift
139     local skid="subjectKeyIdentifier = hash"
140     local akid="authorityKeyIdentifier = keyid"
141
142     exts=$(printf "%s\n%s\n%s\n" "$skid" "$akid")
143     exts=$(printf "%s\nkeyUsage = %s\n" "$exts" "keyCertSign, cRLSign")
144     for eku in "$@"
145     do
146         exts=$(printf "%s\nextendedKeyUsage = %s\n" "$exts" "$eku")
147     done
148     csr=$(req "$key" "CN = $cn") || return 1
149     echo "$csr" |
150         cert "$cert" "$exts" -CA "${cacert}.pem" -CAkey "${cakey}.pem" \
151             -set_serial 2 -days "${DAYS}"
152 }
153
154 # Usage: $0 genpc keyname certname eekeyname eecertname pcext1 pcext2 ...
155 #
156 # Note: takes csr on stdin, so must be used with $0 req like this:
157 #
158 # $0 req keyname dn | $0 genpc keyname certname eekeyname eecertname pcext ...
159 genpc() {
160     local key=$1; shift
161     local cert=$1; shift
162     local cakey=$1; shift
163     local ca=$1; shift
164
165     exts=$(printf "%s\n%s\n%s\n%s\n" \
166             "subjectKeyIdentifier = hash" \
167             "authorityKeyIdentifier = keyid, issuer:always" \
168             "basicConstraints = CA:false" \
169             "proxyCertInfo = critical, @pcexts";
170            echo "[pcexts]";
171            for x in "$@"; do echo $x; done)
172     cert "$cert" "$exts" -CA "${ca}.pem" -CAkey "${cakey}.pem" \
173          -set_serial 2 -days "${DAYS}"
174 }
175
176 # Usage: $0 genalt keyname certname eekeyname eecertname alt1 alt2 ...
177 #
178 # Note: takes csr on stdin, so must be used with $0 req like this:
179 #
180 # $0 req keyname dn | $0 genalt keyname certname eekeyname eecertname alt ...
181 geneealt() {
182     local key=$1; shift
183     local cert=$1; shift
184     local cakey=$1; shift
185     local ca=$1; shift
186
187     exts=$(printf "%s\n%s\n%s\n%s\n" \
188             "subjectKeyIdentifier = hash" \
189             "authorityKeyIdentifier = keyid" \
190             "basicConstraints = CA:false" \
191             "subjectAltName = @alts";
192            echo "[alts]";
193            for x in "$@"; do echo $x; done)
194     cert "$cert" "$exts" -CA "${ca}.pem" -CAkey "${cakey}.pem" \
195          -set_serial 2 -days "${DAYS}"
196 }
197
198 genee() {
199     local OPTIND=1
200     local purpose=serverAuth
201
202     while getopts p: o
203     do
204         case $o in
205         p) purpose="$OPTARG";;
206         *) echo "Usage: $0 genee [-p EKU] cn keyname certname cakeyname cacertname" >&2
207            return 1;;
208         esac
209     done
210
211     shift $((OPTIND - 1))
212     local cn=$1; shift
213     local key=$1; shift
214     local cert=$1; shift
215     local cakey=$1; shift
216     local ca=$1; shift
217
218     exts=$(printf "%s\n%s\n%s\n%s\n%s\n[alts]\n%s\n" \
219             "subjectKeyIdentifier = hash" \
220             "authorityKeyIdentifier = keyid, issuer" \
221             "basicConstraints = CA:false" \
222             "extendedKeyUsage = $purpose" \
223             "subjectAltName = @alts" "DNS=${cn}")
224     csr=$(req "$key" "CN = $cn") || return 1
225     echo "$csr" |
226         cert "$cert" "$exts" -CA "${ca}.pem" -CAkey "${cakey}.pem" \
227             -set_serial 2 -days "${DAYS}" "$@"
228 }
229
230 genss() {
231     local cn=$1; shift
232     local key=$1; shift
233     local cert=$1; shift
234
235     exts=$(printf "%s\n%s\n%s\n%s\n%s\n[alts]\n%s\n" \
236             "subjectKeyIdentifier   = hash" \
237             "authorityKeyIdentifier = keyid, issuer" \
238             "basicConstraints = CA:false" \
239             "extendedKeyUsage = serverAuth" \
240             "subjectAltName = @alts" "DNS=${cn}")
241     csr=$(req "$key" "CN = $cn") || return 1
242     echo "$csr" |
243         cert "$cert" "$exts" -signkey "${key}.pem" \
244             -set_serial 1 -days "${DAYS}" "$@"
245 }
246
247 gennocn() {
248     local key=$1; shift
249     local cert=$1; shift
250
251     csr=$(req_nocn "$key") || return 1
252     echo "$csr" |
253         cert "$cert" "" -signkey "${key}.pem" -set_serial 1 -days -1 "$@"
254 }
255
256 "$@"