3 # Copyright (c) 2016 Viktor Dukhovni <openssl-users@dukhovni.org>.
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.
9 # 100 years should be enough for now
11 if [ -z "$DAYS" ]; then
15 if [ -z "$OPENSSL_SIGALG" ]; then
21 err=$("$@" >&3 2>&1) || {
22 printf "%s\n" "$err" >&2
32 if [ -n "$OPENSSL_KEYALG" ]; then
37 if [ -n "$OPENSSL_KEYBITS" ]; then
41 if [ ! -f "${key}.pem" ]; then
42 args=(-algorithm "$alg")
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;;
50 openssl genpkey "${args[@]}" -out "${key}.pem"
54 # Usage: $0 req keyname dn1 dn2 ...
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" "${dn}"
65 for dn in "$@"; do echo "$dn"; done)
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")
83 openssl x509 -req -"${OPENSSL_SIGALG}" -out "${cert}.pem" \
84 -extfile <(printf "%s\n" "$exts") "$@"
91 local skid="subjectKeyIdentifier = hash"
92 local akid="authorityKeyIdentifier = keyid"
94 exts=$(printf "%s\n%s\n%s\n" "$skid" "$akid" "basicConstraints = critical,CA:true")
97 exts=$(printf "%s\nextendedKeyUsage = %s\n" "$exts" "$eku")
99 csr=$(req "$key" "CN = $cn") || return 1
101 cert "$cert" "$exts" -signkey "${key}.pem" -set_serial 1 -days "${DAYS}"
108 local cakey=$1; shift
109 local cacert=$1; shift
110 local skid="subjectKeyIdentifier = hash"
111 local akid="authorityKeyIdentifier = keyid"
113 exts=$(printf "%s\n%s\n%s\n" "$skid" "$akid" "basicConstraints = critical,CA:true")
116 exts=$(printf "%s\nextendedKeyUsage = %s\n" "$exts" "$eku")
118 csr=$(req "$key" "CN = $cn") || return 1
120 cert "$cert" "$exts" -CA "${cacert}.pem" -CAkey "${cakey}.pem" \
121 -set_serial 2 -days "${DAYS}"
128 local cakey=$1; shift
129 local cacert=$1; shift
130 local skid="subjectKeyIdentifier = hash"
131 local akid="authorityKeyIdentifier = keyid"
133 exts=$(printf "%s\n%s\n%s\n" "$skid" "$akid")
134 exts=$(printf "%s\nkeyUsage = %s\n" "$exts" "keyCertSign, cRLSign")
137 exts=$(printf "%s\nextendedKeyUsage = %s\n" "$exts" "$eku")
139 csr=$(req "$key" "CN = $cn") || return 1
141 cert "$cert" "$exts" -CA "${cacert}.pem" -CAkey "${cakey}.pem" \
142 -set_serial 2 -days "${DAYS}"
145 # Usage: $0 genpc keyname certname eekeyname eecertname pcext1 pcext2 ...
147 # Note: takes csr on stdin, so must be used with $0 req like this:
149 # $0 req keyname dn | $0 genpc keyname certname eekeyname eecertname pcext ...
153 local cakey=$1; shift
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";
162 for x in "$@"; do echo $x; done)
163 cert "$cert" "$exts" -CA "${ca}.pem" -CAkey "${cakey}.pem" \
164 -set_serial 2 -days "${DAYS}"
169 local purpose=serverAuth
174 p) purpose="$OPTARG";;
175 *) echo "Usage: $0 genee [-p EKU] cn keyname certname cakeyname cacertname" >&2
180 shift $((OPTIND - 1))
184 local cakey=$1; shift
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
195 cert "$cert" "$exts" -CA "${ca}.pem" -CAkey "${cakey}.pem" \
196 -set_serial 2 -days "${DAYS}" "$@"
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
212 cert "$cert" "$exts" -signkey "${key}.pem" \
213 -set_serial 1 -days "${DAYS}" "$@"
220 csr=$(req_nocn "$key") || return 1
222 cert "$cert" "" -signkey "${key}.pem" -set_serial 1 -days -1 "$@"