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
13 if [ -z "$OPENSSL_SIGALG" ]; then
19 err=$("$@" >&3 2>&1) || {
20 printf "%s\n" "$err" >&2
30 if [ -n "$OPENSSL_KEYALG" ]; then
35 if [ -n "$OPENSSL_KEYBITS" ]; then
39 if [ ! -f "${key}.pem" ]; then
40 args=(-algorithm "$alg")
42 rsa) args=("${args[@]}" -pkeyopt rsa_keygen_bits:$bits );;
43 ec) args=("${args[@]}" -pkeyopt "ec_paramgen_curve:$bits")
44 args=("${args[@]}" -pkeyopt ec_param_enc:named_curve);;
45 *) printf "Unsupported key algorithm: %s\n" "$alg" >&2; return 1;;
48 openssl genpkey "${args[@]}" -out "${key}.pem"
60 openssl req -new -"${OPENSSL_SIGALG}" -key "${key}.pem" \
61 -config <(printf "[req]\n%s\n%s\n[dn]\nCN=%s\n" \
62 "prompt = no" "distinguished_name = dn" "${cn}")
70 openssl req -new -"${OPENSSL_SIGALG}" -subj / -key "${key}.pem" \
71 -config <(printf "[req]\n%s\n[dn]\nCN_default =\n" \
72 "distinguished_name = dn")
80 openssl x509 -req -"${OPENSSL_SIGALG}" -out "${cert}.pem" \
81 -extfile <(printf "%s\n" "$exts") "$@"
88 local skid="subjectKeyIdentifier = hash"
89 local akid="authorityKeyIdentifier = keyid"
91 exts=$(printf "%s\n%s\n%s\n" "$skid" "$akid" "basicConstraints = CA:true")
94 exts=$(printf "%s\nextendedKeyUsage = %s\n" "$exts" "$eku")
96 csr=$(req "$key" "$cn") || return 1
98 cert "$cert" "$exts" -signkey "${key}.pem" -set_serial 1 -days "${DAYS}"
105 local cakey=$1; shift
106 local cacert=$1; shift
107 local skid="subjectKeyIdentifier = hash"
108 local akid="authorityKeyIdentifier = keyid"
110 exts=$(printf "%s\n%s\n%s\n" "$skid" "$akid" "basicConstraints = CA:true")
113 exts=$(printf "%s\nextendedKeyUsage = %s\n" "$exts" "$eku")
115 csr=$(req "$key" "$cn") || return 1
117 cert "$cert" "$exts" -CA "${cacert}.pem" -CAkey "${cakey}.pem" \
118 -set_serial 2 -days "${DAYS}"
125 local cakey=$1; shift
126 local cacert=$1; shift
127 local skid="subjectKeyIdentifier = hash"
128 local akid="authorityKeyIdentifier = keyid"
130 exts=$(printf "%s\n%s\n%s\n" "$skid" "$akid")
131 exts=$(printf "%s\nkeyUsage = %s\n" "$exts" "keyCertSign, cRLSign")
134 exts=$(printf "%s\nextendedKeyUsage = %s\n" "$exts" "$eku")
136 csr=$(req "$key" "$cn") || return 1
138 cert "$cert" "$exts" -CA "${cacert}.pem" -CAkey "${cakey}.pem" \
139 -set_serial 2 -days "${DAYS}"
144 local purpose=serverAuth
149 p) purpose="$OPTARG";;
150 *) echo "Usage: $0 genee [-p EKU] cn keyname certname cakeyname cacertname" >&2
155 shift $((OPTIND - 1))
159 local cakey=$1; shift
162 exts=$(printf "%s\n%s\n%s\n%s\n%s\n[alts]\n%s\n" \
163 "subjectKeyIdentifier = hash" \
164 "authorityKeyIdentifier = keyid, issuer" \
165 "basicConstraints = CA:false" \
166 "extendedKeyUsage = $purpose" \
167 "subjectAltName = @alts" "DNS=${cn}")
168 csr=$(req "$key" "$cn") || return 1
170 cert "$cert" "$exts" -CA "${ca}.pem" -CAkey "${cakey}.pem" \
171 -set_serial 2 -days "${DAYS}" "$@"
179 exts=$(printf "%s\n%s\n%s\n%s\n%s\n[alts]\n%s\n" \
180 "subjectKeyIdentifier = hash" \
181 "authorityKeyIdentifier = keyid, issuer" \
182 "basicConstraints = CA:false" \
183 "extendedKeyUsage = serverAuth" \
184 "subjectAltName = @alts" "DNS=${cn}")
185 csr=$(req "$key" "$cn") || return 1
187 cert "$cert" "$exts" -signkey "${key}.pem" \
188 -set_serial 1 -days "${DAYS}" "$@"
195 csr=$(req_nocn "$key") || return 1
197 cert "$cert" "" -signkey "${key}.pem" -set_serial 1 -days -1 "$@"