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
15 err=$("$@" >&3 2>&1) || {
16 printf "%s\n" "$err" >&2
26 if [ -n "$OPENSSL_KEYALG" ]; then
31 if [ -n "$OPENSSL_KEYBITS" ]; then
35 if [ ! -f "${key}.pem" ]; then
36 args=(-algorithm "$alg")
38 rsa) args=("${args[@]}" -pkeyopt rsa_keygen_bits:$bits );;
39 ec) args=("${args[@]}" -pkeyopt "ec_paramgen_curve:$bits")
40 args=("${args[@]}" -pkeyopt ec_param_enc:named_curve);;
41 *) printf "Unsupported key algorithm: %s\n" "$alg" >&2; return 1;;
44 openssl genpkey "${args[@]}" -out "${key}.pem"
56 openssl req -new -sha256 -key "${key}.pem" \
57 -config <(printf "[req]\n%s\n%s\n[dn]\nCN=%s\n" \
58 "prompt = no" "distinguished_name = dn" "${cn}")
66 openssl req -new -sha256 -subj / -key "${key}.pem" \
67 -config <(printf "[req]\n%s\n[dn]\nCN_default =\n" \
68 "distinguished_name = dn")
76 openssl x509 -req -sha256 -out "${cert}.pem" \
77 -extfile <(printf "%s\n" "$exts") "$@"
84 local skid="subjectKeyIdentifier = hash"
85 local akid="authorityKeyIdentifier = keyid"
87 exts=$(printf "%s\n%s\n%s\n" "$skid" "$akid" "basicConstraints = CA:true")
90 exts=$(printf "%s\nextendedKeyUsage = %s\n" "$exts" "$eku")
92 csr=$(req "$key" "$cn") || return 1
94 cert "$cert" "$exts" -signkey "${key}.pem" -set_serial 1 -days "${DAYS}"
101 local cakey=$1; shift
102 local cacert=$1; shift
103 local skid="subjectKeyIdentifier = hash"
104 local akid="authorityKeyIdentifier = keyid"
106 exts=$(printf "%s\n%s\n%s\n" "$skid" "$akid" "basicConstraints = CA:true")
109 exts=$(printf "%s\nextendedKeyUsage = %s\n" "$exts" "$eku")
111 csr=$(req "$key" "$cn") || return 1
113 cert "$cert" "$exts" -CA "${cacert}.pem" -CAkey "${cakey}.pem" \
114 -set_serial 2 -days "${DAYS}"
119 local purpose=serverAuth
124 p) purpose="$OPTARG";;
125 *) echo "Usage: $0 genee [-p EKU] cn keyname certname cakeyname cacertname" >&2
130 shift $((OPTIND - 1))
134 local cakey=$1; shift
137 exts=$(printf "%s\n%s\n%s\n%s\n%s\n[alts]\n%s\n" \
138 "subjectKeyIdentifier = hash" \
139 "authorityKeyIdentifier = keyid, issuer" \
140 "basicConstraints = CA:false" \
141 "extendedKeyUsage = $purpose" \
142 "subjectAltName = @alts" "DNS=${cn}")
143 csr=$(req "$key" "$cn") || return 1
145 cert "$cert" "$exts" -CA "${ca}.pem" -CAkey "${cakey}.pem" \
146 -set_serial 2 -days "${DAYS}" "$@"
154 exts=$(printf "%s\n%s\n%s\n%s\n%s\n[alts]\n%s\n" \
155 "subjectKeyIdentifier = hash" \
156 "authorityKeyIdentifier = keyid, issuer" \
157 "basicConstraints = CA:false" \
158 "extendedKeyUsage = serverAuth" \
159 "subjectAltName = @alts" "DNS=${cn}")
160 csr=$(req "$key" "$cn") || return 1
162 cert "$cert" "$exts" -signkey "${key}.pem" \
163 -set_serial 1 -days "${DAYS}" "$@"
170 csr=$(req_nocn "$key") || return 1
172 cert "$cert" "" -signkey "${key}.pem" -set_serial 1 -days -1 "$@"