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 ecdsa) 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")
88 csr=$(req "$key" "$cn") || return 1
90 cert "$cert" "$exts" -signkey "${key}.pem" -set_serial 1 -days "${DAYS}"
98 local cacert=$1; shift
99 local skid="subjectKeyIdentifier = hash"
100 local akid="authorityKeyIdentifier = keyid"
102 exts=$(printf "%s\n%s\n%s\n" "$skid" "$akid" "basicConstraints = CA:true")
103 csr=$(req "$key" "$cn") || return 1
105 cert "$cert" "$exts" -CA "${cacert}.pem" -CAkey "${cakey}.pem" \
106 -set_serial 2 -days "${DAYS}" "$@"
111 local purpose=serverAuth
116 p) purpose="$OPTARG";;
117 *) echo "Usage: $0 genee [-p EKU] cn keyname certname cakeyname cacertname" >&2
122 shift $((OPTIND - 1))
126 local cakey=$1; shift
129 exts=$(printf "%s\n%s\n%s\n%s\n%s\n[alts]\n%s\n" \
130 "subjectKeyIdentifier = hash" \
131 "authorityKeyIdentifier = keyid, issuer" \
132 "basicConstraints = CA:false" \
133 "extendedKeyUsage = $purpose" \
134 "subjectAltName = @alts" "DNS=${cn}")
135 csr=$(req "$key" "$cn") || return 1
137 cert "$cert" "$exts" -CA "${ca}.pem" -CAkey "${cakey}.pem" \
138 -set_serial 2 -days "${DAYS}" "$@"
146 exts=$(printf "%s\n%s\n%s\n%s\n%s\n[alts]\n%s\n" \
147 "subjectKeyIdentifier = hash" \
148 "authorityKeyIdentifier = keyid, issuer" \
149 "basicConstraints = CA:false" \
150 "extendedKeyUsage = serverAuth" \
151 "subjectAltName = @alts" "DNS=${cn}")
152 csr=$(req "$key" "$cn") || return 1
154 cert "$cert" "$exts" -signkey "${key}.pem" \
155 -set_serial 1 -days "${DAYS}" "$@"
162 csr=$(req_nocn "$key") || return 1
164 cert "$cert" "" -signkey "${key}.pem" -set_serial 1 -days -1 "$@"