3 # Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved.
4 # Copyright (c) 2016 Viktor Dukhovni <openssl-users@dukhovni.org>.
7 # Licensed under the OpenSSL license (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}" "$@"
241 exts=$(printf "%s\n%s\n%s\n%s\n%s\n[alts]\n%s\n" \
242 "subjectKeyIdentifier = hash" \
243 "authorityKeyIdentifier = keyid, issuer" \
244 "basicConstraints = CA:false" \
245 "extendedKeyUsage = serverAuth" \
246 "subjectAltName = @alts" "DNS=${cn}")
247 csr=$(req "$key" "CN = $cn") || return 1
249 cert "$cert" "$exts" -signkey "${key}.pem" \
250 -set_serial 1 -days "${DAYS}" "$@"
257 csr=$(req_nocn "$key") || return 1
259 cert "$cert" "" -signkey "${key}.pem" -set_serial 1 -days -1 "$@"