Make DH opaque
[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 if [ -z "$OPENSSL_SIGALG" ]; then
14     OPENSSL_SIGALG=sha256
15 fi
16
17 stderr_onerror() {
18     (
19         err=$("$@" >&3 2>&1) || {
20             printf "%s\n" "$err" >&2
21             exit 1
22         }
23     ) 3>&1
24 }
25
26 key() {
27     local key=$1; shift
28
29     local alg=rsa
30     if [ -n "$OPENSSL_KEYALG" ]; then
31         alg=$OPENSSL_KEYALG
32     fi
33
34     local bits=2048
35     if [ -n "$OPENSSL_KEYBITS" ]; then
36         bits=$OPENSSL_KEYBITS
37     fi
38
39     if [ ! -f "${key}.pem" ]; then
40         args=(-algorithm "$alg")
41         case $alg in
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;;
46         esac
47         stderr_onerror \
48             openssl genpkey "${args[@]}" -out "${key}.pem"
49     fi
50 }
51
52 req() {
53     local key=$1; shift
54     local cn=$1; shift
55
56     key "$key"
57     local errs
58
59     stderr_onerror \
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}")
63 }
64
65 req_nocn() {
66     local key=$1; shift
67
68     key "$key"
69     stderr_onerror \
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")
73 }
74
75 cert() {
76     local cert=$1; shift
77     local exts=$1; shift
78
79     stderr_onerror \
80         openssl x509 -req -"${OPENSSL_SIGALG}" -out "${cert}.pem" \
81             -extfile <(printf "%s\n" "$exts") "$@"
82 }
83
84 genroot() {
85     local cn=$1; shift
86     local key=$1; shift
87     local cert=$1; shift
88     local skid="subjectKeyIdentifier = hash"
89     local akid="authorityKeyIdentifier = keyid"
90
91     exts=$(printf "%s\n%s\n%s\n" "$skid" "$akid" "basicConstraints = CA:true")
92     for eku in "$@"
93     do
94         exts=$(printf "%s\nextendedKeyUsage = %s\n" "$exts" "$eku")
95     done
96     csr=$(req "$key" "$cn") || return 1
97     echo "$csr" |
98        cert "$cert" "$exts" -signkey "${key}.pem" -set_serial 1 -days "${DAYS}"
99 }
100
101 genca() {
102     local cn=$1; shift
103     local key=$1; shift
104     local cert=$1; shift
105     local cakey=$1; shift
106     local cacert=$1; shift
107     local skid="subjectKeyIdentifier = hash"
108     local akid="authorityKeyIdentifier = keyid"
109
110     exts=$(printf "%s\n%s\n%s\n" "$skid" "$akid" "basicConstraints = CA:true")
111     for eku in "$@"
112     do
113         exts=$(printf "%s\nextendedKeyUsage = %s\n" "$exts" "$eku")
114     done
115     csr=$(req "$key" "$cn") || return 1
116     echo "$csr" |
117         cert "$cert" "$exts" -CA "${cacert}.pem" -CAkey "${cakey}.pem" \
118             -set_serial 2 -days "${DAYS}"
119 }
120
121 gen_nonbc_ca() {
122     local cn=$1; shift
123     local key=$1; shift
124     local cert=$1; shift
125     local cakey=$1; shift
126     local cacert=$1; shift
127     local skid="subjectKeyIdentifier = hash"
128     local akid="authorityKeyIdentifier = keyid"
129
130     exts=$(printf "%s\n%s\n%s\n" "$skid" "$akid")
131     exts=$(printf "%s\nkeyUsage = %s\n" "$exts" "keyCertSign, cRLSign")
132     for eku in "$@"
133     do
134         exts=$(printf "%s\nextendedKeyUsage = %s\n" "$exts" "$eku")
135     done
136     csr=$(req "$key" "$cn") || return 1
137     echo "$csr" |
138         cert "$cert" "$exts" -CA "${cacert}.pem" -CAkey "${cakey}.pem" \
139             -set_serial 2 -days "${DAYS}"
140 }
141
142 genee() {
143     local OPTIND=1
144     local purpose=serverAuth
145
146     while getopts p: o
147     do
148         case $o in
149         p) purpose="$OPTARG";;
150         *) echo "Usage: $0 genee [-p EKU] cn keyname certname cakeyname cacertname" >&2
151            return 1;;
152         esac
153     done
154
155     shift $((OPTIND - 1))
156     local cn=$1; shift
157     local key=$1; shift
158     local cert=$1; shift
159     local cakey=$1; shift
160     local ca=$1; shift
161
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
169     echo "$csr" |
170         cert "$cert" "$exts" -CA "${ca}.pem" -CAkey "${cakey}.pem" \
171             -set_serial 2 -days "${DAYS}" "$@"
172 }
173
174 genss() {
175     local cn=$1; shift
176     local key=$1; shift
177     local cert=$1; shift
178
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
186     echo "$csr" |
187         cert "$cert" "$exts" -signkey "${key}.pem" \
188             -set_serial 1 -days "${DAYS}" "$@"
189 }
190
191 gennocn() {
192     local key=$1; shift
193     local cert=$1; shift
194
195     csr=$(req_nocn "$key") || return 1
196     echo "$csr" |
197         cert "$cert" "" -signkey "${key}.pem" -set_serial 1 -days -1 "$@"
198 }
199
200 "$@"