3 # Copyright 2016-2020 The OpenSSL Project Authors. All Rights Reserved.
4 # Copyright (c) 2016 Viktor Dukhovni <openssl-users@dukhovni.org>.
7 # Licensed under the Apache License 2.0 (the "License"). You may not use
8 # this file except in compliance with the License. You can obtain a copy
9 # in the file LICENSE in the source distribution or at
10 # https://www.openssl.org/source/license.html
12 # This file is dual-licensed and is also available under other terms.
13 # Please contact the author.
15 # 100 years should be enough for now
16 if [ -z "$DAYS" ]; then
20 if [ -z "$OPENSSL_SIGALG" ]; then
24 if [ -z "$REQMASK" ]; then
30 err=$("$@" >&3 2>&1) || {
31 printf "%s\n" "$err" >&2
41 if [ -n "$OPENSSL_KEYALG" ]; then
46 if [ -n "$OPENSSL_KEYBITS" ]; then
50 if [ ! -f "${key}.pem" ]; then
51 args=(-algorithm "$alg")
53 rsa) args=("${args[@]}" -pkeyopt rsa_keygen_bits:$bits );;
54 ec) args=("${args[@]}" -pkeyopt "ec_paramgen_curve:$bits")
55 args=("${args[@]}" -pkeyopt ec_param_enc:named_curve);;
56 dsa) args=(-paramfile "$bits");;
59 *) printf "Unsupported key algorithm: %s\n" "$alg" >&2; return 1;;
62 openssl genpkey "${args[@]}" -out "${key}.pem"
66 # Usage: $0 req keyname dn1 dn2 ...
74 openssl req -new -"${OPENSSL_SIGALG}" -key "${key}.pem" \
75 -config <(printf "string_mask=%s\n[req]\n%s\n%s\n[dn]\n" \
76 "$REQMASK" "prompt = no" "distinguished_name = dn"
77 for dn in "$@"; do echo "$dn"; done)
85 openssl req -new -"${OPENSSL_SIGALG}" -subj / -key "${key}.pem" \
86 -config <(printf "[req]\n%s\n[dn]\nCN_default =\n" \
87 "distinguished_name = dn")
95 openssl x509 -req -"${OPENSSL_SIGALG}" -out "${cert}.pem" \
96 -extfile <(printf "%s\n" "$exts") "$@"
103 local skid="subjectKeyIdentifier = hash"
104 local akid="authorityKeyIdentifier = keyid"
106 exts=$(printf "%s\n%s\n%s\n" "$skid" "$akid" "basicConstraints = critical,CA:true")
109 exts=$(printf "%s\nextendedKeyUsage = %s\n" "$exts" "$eku")
111 csr=$(req "$key" "CN = $cn") || return 1
113 cert "$cert" "$exts" -signkey "${key}.pem" -set_serial 1 -days "${DAYS}"
120 local cakey=$1; shift
121 local cacert=$1; shift
122 local skid="subjectKeyIdentifier = hash"
123 local akid="authorityKeyIdentifier = keyid"
125 exts=$(printf "%s\n%s\n%s\n" "$skid" "$akid" "basicConstraints = critical,CA:true")
128 exts=$(printf "%s\nextendedKeyUsage = %s\n" "$exts" "$eku")
130 if [ -n "$NC" ]; then
131 exts=$(printf "%s\nnameConstraints = %s\n" "$exts" "$NC")
133 csr=$(req "$key" "CN = $cn") || return 1
135 cert "$cert" "$exts" -CA "${cacert}.pem" -CAkey "${cakey}.pem" \
136 -set_serial 2 -days "${DAYS}"
143 local cakey=$1; shift
144 local cacert=$1; shift
145 local skid="subjectKeyIdentifier = hash"
146 local akid="authorityKeyIdentifier = keyid"
148 exts=$(printf "%s\n%s\n%s\n" "$skid" "$akid")
149 exts=$(printf "%s\nkeyUsage = %s\n" "$exts" "keyCertSign, cRLSign")
152 exts=$(printf "%s\nextendedKeyUsage = %s\n" "$exts" "$eku")
154 csr=$(req "$key" "CN = $cn") || return 1
156 cert "$cert" "$exts" -CA "${cacert}.pem" -CAkey "${cakey}.pem" \
157 -set_serial 2 -days "${DAYS}"
160 # Usage: $0 genpc keyname certname eekeyname eecertname pcext1 pcext2 ...
162 # Note: takes csr on stdin, so must be used with $0 req like this:
164 # $0 req keyname dn | $0 genpc keyname certname eekeyname eecertname pcext ...
168 local cakey=$1; shift
171 exts=$(printf "%s\n%s\n%s\n%s\n" \
172 "subjectKeyIdentifier = hash" \
173 "authorityKeyIdentifier = keyid, issuer:always" \
174 "basicConstraints = CA:false" \
175 "proxyCertInfo = critical, @pcexts";
177 for x in "$@"; do echo $x; done)
178 cert "$cert" "$exts" -CA "${ca}.pem" -CAkey "${cakey}.pem" \
179 -set_serial 2 -days "${DAYS}"
182 # Usage: $0 geneealt keyname certname eekeyname eecertname alt1 alt2 ...
184 # Note: takes csr on stdin, so must be used with $0 req like this:
186 # $0 req keyname dn | $0 geneealt keyname certname eekeyname eecertname alt ...
190 local cakey=$1; shift
193 exts=$(printf "%s\n%s\n%s\n%s\n" \
194 "subjectKeyIdentifier = hash" \
195 "authorityKeyIdentifier = keyid" \
196 "basicConstraints = CA:false" \
197 "subjectAltName = @alts";
199 for x in "$@"; do echo $x; done)
200 cert "$cert" "$exts" -CA "${ca}.pem" -CAkey "${cakey}.pem" \
201 -set_serial 2 -days "${DAYS}"
206 local purpose=serverAuth
211 p) purpose="$OPTARG";;
212 *) echo "Usage: $0 genee [-p EKU] cn keyname certname cakeyname cacertname" >&2
217 shift $((OPTIND - 1))
221 local cakey=$1; shift
224 exts=$(printf "%s\n%s\n%s\n%s\n%s\n[alts]\n%s\n" \
225 "subjectKeyIdentifier = hash" \
226 "authorityKeyIdentifier = keyid, issuer" \
227 "basicConstraints = CA:false" \
228 "extendedKeyUsage = $purpose" \
229 "subjectAltName = @alts" "DNS=${cn}")
230 csr=$(req "$key" "CN = $cn") || return 1
232 cert "$cert" "$exts" -CA "${ca}.pem" -CAkey "${cakey}.pem" \
233 -set_serial 2 -days "${DAYS}" "$@"
238 local purpose=serverAuth
243 p) purpose="$OPTARG";;
244 *) echo "Usage: $0 genee [-p EKU] cn certname cakeyname cacertname" >&2
249 shift $((OPTIND - 1))
252 local cakey=$1; shift
255 exts=$(printf "%s\n%s\n%s\n%s\n%s\n[alts]\n%s\n" \
256 "subjectKeyIdentifier = hash" \
257 "authorityKeyIdentifier = keyid, issuer" \
258 "basicConstraints = CA:false" \
259 "extendedKeyUsage = $purpose" \
260 "subjectAltName = @alts" "DNS=${cn}")
261 cert "$cert" "$exts" -CA "${ca}.pem" -CAkey "${cakey}.pem" \
262 -set_serial 2 -days "${DAYS}" "$@"
270 exts=$(printf "%s\n%s\n%s\n%s\n%s\n[alts]\n%s\n" \
271 "subjectKeyIdentifier = hash" \
272 "authorityKeyIdentifier = keyid, issuer" \
273 "basicConstraints = CA:false" \
274 "extendedKeyUsage = serverAuth" \
275 "subjectAltName = @alts" "DNS=${cn}")
276 csr=$(req "$key" "CN = $cn") || return 1
278 cert "$cert" "$exts" -signkey "${key}.pem" \
279 -set_serial 1 -days "${DAYS}" "$@"
286 csr=$(req_nocn "$key") || return 1
288 cert "$cert" "" -signkey "${key}.pem" -set_serial 1 -days -1 "$@"
293 local purpose=serverAuth
298 p) purpose="$OPTARG";;
299 *) echo "Usage: $0 genct [-p EKU] cn keyname certname cakeyname cacertname ctlogkey" >&2
304 shift $((OPTIND - 1))
308 local cakey=$1; shift
310 local logkey=$1; shift
312 exts=$(printf "%s\n%s\n%s\n%s\n%s\n%s\n[alts]\n%s\n" \
313 "subjectKeyIdentifier = hash" \
314 "authorityKeyIdentifier = keyid, issuer" \
315 "basicConstraints = CA:false" \
316 "extendedKeyUsage = $purpose" \
317 "1.3.6.1.4.1.11129.2.4.3 = critical,ASN1:NULL"\
318 "subjectAltName = @alts" "DNS=${cn}")
319 csr=$(req "$key" "CN = $cn") || return 1
321 cert "$cert" "$exts" -CA "${ca}.pem" -CAkey "${cakey}.pem" \
322 -set_serial 2 -days "${DAYS}" "$@"
323 cat ${cert}.pem ${ca}.pem > ${cert}-chain.pem
324 go run github.com/google/certificate-transparency-go/ctutil/sctgen \
325 --log_private_key ${logkey}.pem \
326 --timestamp="2020-01-01T00:00:00Z" \
327 --cert_chain ${cert}-chain.pem \
328 --tls_out ${cert}.tlssct
330 filesize=$(wc -c <${cert}.tlssct)
331 exts=$(printf "%s\n%s\n%s\n%s\n%s%04X%04X%s\n%s\n[alts]\n%s\n" \
332 "subjectKeyIdentifier = hash" \
333 "authorityKeyIdentifier = keyid, issuer" \
334 "basicConstraints = CA:false" \
335 "extendedKeyUsage = $purpose" \
336 "1.3.6.1.4.1.11129.2.4.2 = ASN1:FORMAT:HEX,OCT:" $((filesize+2)) $filesize `xxd -p ${cert}.tlssct | tr -d '\n'` \
337 "subjectAltName = @alts" "DNS=${cn}")
339 cert "$cert" "$exts" -CA "${ca}.pem" -CAkey "${cakey}.pem" \
340 -set_serial 2 -days "${DAYS}" "$@"