Drop extraneous printf argument in mkcert.sh
[oweals/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 stderr_onerror() {
20     (
21         err=$("$@" >&3 2>&1) || {
22             printf "%s\n" "$err" >&2
23             exit 1
24         }
25     ) 3>&1
26 }
27
28 key() {
29     local key=$1; shift
30
31     local alg=rsa
32     if [ -n "$OPENSSL_KEYALG" ]; then
33         alg=$OPENSSL_KEYALG
34     fi
35
36     local bits=2048
37     if [ -n "$OPENSSL_KEYBITS" ]; then
38         bits=$OPENSSL_KEYBITS
39     fi
40
41     if [ ! -f "${key}.pem" ]; then
42         args=(-algorithm "$alg")
43         case $alg in
44         rsa) args=("${args[@]}" -pkeyopt rsa_keygen_bits:$bits );;
45         ec)  args=("${args[@]}" -pkeyopt "ec_paramgen_curve:$bits")
46                args=("${args[@]}" -pkeyopt ec_param_enc:named_curve);;
47         *) printf "Unsupported key algorithm: %s\n" "$alg" >&2; return 1;;
48         esac
49         stderr_onerror \
50             openssl genpkey "${args[@]}" -out "${key}.pem"
51     fi
52 }
53
54 # Usage: $0 req keyname dn1 dn2 ...
55 req() {
56     local key=$1; shift
57
58     key "$key"
59     local errs
60
61     stderr_onerror \
62         openssl req -new -"${OPENSSL_SIGALG}" -key "${key}.pem" \
63             -config <(printf "[req]\n%s\n%s\n[dn]\n" \
64                       "prompt = no" "distinguished_name = dn"
65                       for dn in "$@"; do echo "$dn"; done)
66 }
67
68 req_nocn() {
69     local key=$1; shift
70
71     key "$key"
72     stderr_onerror \
73         openssl req -new -"${OPENSSL_SIGALG}" -subj / -key "${key}.pem" \
74             -config <(printf "[req]\n%s\n[dn]\nCN_default =\n" \
75                       "distinguished_name = dn")
76 }
77
78 cert() {
79     local cert=$1; shift
80     local exts=$1; shift
81
82     stderr_onerror \
83         openssl x509 -req -"${OPENSSL_SIGALG}" -out "${cert}.pem" \
84             -extfile <(printf "%s\n" "$exts") "$@"
85 }
86
87 genroot() {
88     local cn=$1; shift
89     local key=$1; shift
90     local cert=$1; shift
91     local skid="subjectKeyIdentifier = hash"
92     local akid="authorityKeyIdentifier = keyid"
93
94     exts=$(printf "%s\n%s\n%s\n" "$skid" "$akid" "basicConstraints = critical,CA:true")
95     for eku in "$@"
96     do
97         exts=$(printf "%s\nextendedKeyUsage = %s\n" "$exts" "$eku")
98     done
99     csr=$(req "$key" "CN = $cn") || return 1
100     echo "$csr" |
101        cert "$cert" "$exts" -signkey "${key}.pem" -set_serial 1 -days "${DAYS}"
102 }
103
104 genca() {
105     local cn=$1; shift
106     local key=$1; shift
107     local cert=$1; shift
108     local cakey=$1; shift
109     local cacert=$1; shift
110     local skid="subjectKeyIdentifier = hash"
111     local akid="authorityKeyIdentifier = keyid"
112
113     exts=$(printf "%s\n%s\n%s\n" "$skid" "$akid" "basicConstraints = critical,CA:true")
114     for eku in "$@"
115     do
116         exts=$(printf "%s\nextendedKeyUsage = %s\n" "$exts" "$eku")
117     done
118     csr=$(req "$key" "CN = $cn") || return 1
119     echo "$csr" |
120         cert "$cert" "$exts" -CA "${cacert}.pem" -CAkey "${cakey}.pem" \
121             -set_serial 2 -days "${DAYS}"
122 }
123
124 gen_nonbc_ca() {
125     local cn=$1; shift
126     local key=$1; shift
127     local cert=$1; shift
128     local cakey=$1; shift
129     local cacert=$1; shift
130     local skid="subjectKeyIdentifier = hash"
131     local akid="authorityKeyIdentifier = keyid"
132
133     exts=$(printf "%s\n%s\n%s\n" "$skid" "$akid")
134     exts=$(printf "%s\nkeyUsage = %s\n" "$exts" "keyCertSign, cRLSign")
135     for eku in "$@"
136     do
137         exts=$(printf "%s\nextendedKeyUsage = %s\n" "$exts" "$eku")
138     done
139     csr=$(req "$key" "CN = $cn") || return 1
140     echo "$csr" |
141         cert "$cert" "$exts" -CA "${cacert}.pem" -CAkey "${cakey}.pem" \
142             -set_serial 2 -days "${DAYS}"
143 }
144
145 # Usage: $0 genpc keyname certname eekeyname eecertname pcext1 pcext2 ...
146 #
147 # Note: takes csr on stdin, so must be used with $0 req like this:
148 #
149 # $0 req keyname dn | $0 genpc keyname certname eekeyname eecertname pcext ...
150 genpc() {
151     local key=$1; shift
152     local cert=$1; shift
153     local cakey=$1; shift
154     local ca=$1; shift
155
156     exts=$(printf "%s\n%s\n%s\n%s\n" \
157             "subjectKeyIdentifier = hash" \
158             "authorityKeyIdentifier = keyid, issuer:always" \
159             "basicConstraints = CA:false" \
160             "proxyCertInfo = critical, @pcexts";
161            echo "[pcexts]";
162            for x in "$@"; do echo $x; done)
163     cert "$cert" "$exts" -CA "${ca}.pem" -CAkey "${cakey}.pem" \
164          -set_serial 2 -days "${DAYS}"
165 }
166
167 genee() {
168     local OPTIND=1
169     local purpose=serverAuth
170
171     while getopts p: o
172     do
173         case $o in
174         p) purpose="$OPTARG";;
175         *) echo "Usage: $0 genee [-p EKU] cn keyname certname cakeyname cacertname" >&2
176            return 1;;
177         esac
178     done
179
180     shift $((OPTIND - 1))
181     local cn=$1; shift
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%s\n[alts]\n%s\n" \
188             "subjectKeyIdentifier = hash" \
189             "authorityKeyIdentifier = keyid, issuer" \
190             "basicConstraints = CA:false" \
191             "extendedKeyUsage = $purpose" \
192             "subjectAltName = @alts" "DNS=${cn}")
193     csr=$(req "$key" "CN = $cn") || return 1
194     echo "$csr" |
195         cert "$cert" "$exts" -CA "${ca}.pem" -CAkey "${cakey}.pem" \
196             -set_serial 2 -days "${DAYS}" "$@"
197 }
198
199 genss() {
200     local cn=$1; shift
201     local key=$1; shift
202     local cert=$1; shift
203
204     exts=$(printf "%s\n%s\n%s\n%s\n%s\n[alts]\n%s\n" \
205             "subjectKeyIdentifier   = hash" \
206             "authorityKeyIdentifier = keyid, issuer" \
207             "basicConstraints = CA:false" \
208             "extendedKeyUsage = serverAuth" \
209             "subjectAltName = @alts" "DNS=${cn}")
210     csr=$(req "$key" "CN = $cn") || return 1
211     echo "$csr" |
212         cert "$cert" "$exts" -signkey "${key}.pem" \
213             -set_serial 1 -days "${DAYS}" "$@"
214 }
215
216 gennocn() {
217     local key=$1; shift
218     local cert=$1; shift
219
220     csr=$(req_nocn "$key") || return 1
221     echo "$csr" |
222         cert "$cert" "" -signkey "${key}.pem" -set_serial 1 -days -1 "$@"
223 }
224
225 "$@"