Scripts to generate verify test certs
[openssl.git] / test / certs / mkcert.sh
1 #! /bin/bash
2 #
3 # Copyright (c) 2016 Viktor Dukhovni <openssl-users@dukhovni.org>.
4 # All rights reserved.
5 #
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.
8
9 # 100 years should be enough for now
10 #
11 DAYS=36525
12
13 stderr_onerror() {
14     (
15         err=$("$@" >&3 2>&1) || {
16             printf "%s\n" "$err" >&2
17             exit 1
18         }
19     ) 3>&1
20 }
21
22 key() {
23     local key=$1; shift
24
25     local alg=rsa
26     if [ -n "$OPENSSL_KEYALG" ]; then
27         alg=$OPENSSL_KEYALG
28     fi
29
30     local bits=2048
31     if [ -n "$OPENSSL_KEYBITS" ]; then
32         bits=$OPENSSL_KEYBITS
33     fi
34
35     if [ ! -f "${key}.pem" ]; then
36         args=(-algorithm "$alg")
37         case $alg in
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;;
42         esac
43         stderr_onerror \
44             openssl genpkey "${args[@]}" -out "${key}.pem"
45     fi
46 }
47
48 req() {
49     local key=$1; shift
50     local cn=$1; shift
51
52     key "$key"
53     local errs
54
55     stderr_onerror \
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}")
59 }
60
61 req_nocn() {
62     local key=$1; shift
63
64     key "$key"
65     stderr_onerror \
66         openssl req -new -sha256 -subj / -key "${key}.pem" \
67             -config <(printf "[req]\n%s\n[dn]\nCN_default =\n" \
68                       "distinguished_name = dn")
69 }
70
71 cert() {
72     local cert=$1; shift
73     local exts=$1; shift
74
75     stderr_onerror \
76         openssl x509 -req -sha256 -out "${cert}.pem" \
77             -extfile <(printf "%s\n" "$exts") "$@"
78 }
79
80 genroot() {
81     local cn=$1; shift
82     local key=$1; shift
83     local cert=$1; shift
84     local skid="subjectKeyIdentifier = hash"
85     local akid="authorityKeyIdentifier = keyid"
86
87     exts=$(printf "%s\n%s\n%s\n" "$skid" "$akid" "basicConstraints = CA:true")
88     csr=$(req "$key" "$cn") || return 1
89     echo "$csr" |
90        cert "$cert" "$exts" -signkey "${key}.pem" -set_serial 1 -days "${DAYS}"
91 }
92
93 genca() {
94     local cn=$1; shift
95     local key=$1; shift
96     local cert=$1; shift
97     local cakey=$1; shift
98     local cacert=$1; shift
99     local skid="subjectKeyIdentifier = hash"
100     local akid="authorityKeyIdentifier = keyid"
101
102     exts=$(printf "%s\n%s\n%s\n" "$skid" "$akid" "basicConstraints = CA:true")
103     csr=$(req "$key" "$cn") || return 1
104     echo "$csr" |
105         cert "$cert" "$exts" -CA "${cacert}.pem" -CAkey "${cakey}.pem" \
106             -set_serial 2 -days "${DAYS}" "$@"
107 }
108
109 genee() {
110     local OPTIND=1
111     local purpose=serverAuth
112
113     while getopts p: o
114     do
115         case $o in
116         p) purpose="$OPTARG";;
117         *) echo "Usage: $0 genee [-p EKU] cn keyname certname cakeyname cacertname" >&2
118            return 1;;
119         esac
120     done
121
122     shift $((OPTIND - 1))
123     local cn=$1; shift
124     local key=$1; shift
125     local cert=$1; shift
126     local cakey=$1; shift
127     local ca=$1; shift
128
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
136     echo "$csr" |
137         cert "$cert" "$exts" -CA "${ca}.pem" -CAkey "${cakey}.pem" \
138             -set_serial 2 -days "${DAYS}" "$@"
139 }
140
141 genss() {
142     local cn=$1; shift
143     local key=$1; shift
144     local cert=$1; shift
145
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
153     echo "$csr" |
154         cert "$cert" "$exts" -signkey "${key}.pem" \
155             -set_serial 1 -days "${DAYS}" "$@"
156 }
157
158 gennocn() {
159     local key=$1; shift
160     local cert=$1; shift
161
162     csr=$(req_nocn "$key") || return 1
163     echo "$csr" |
164         cert "$cert" "" -signkey "${key}.pem" -set_serial 1 -days -1 "$@"
165 }
166
167 "$@"