summaryrefslogtreecommitdiff
path: root/regress/lib/libssl/certs/make-certs.sh
blob: c90b7c8ff3280f20e5a5b99dfcd6f82b34790412 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#!/bin/ksh

#
# Copyright (c) 2020, 2021 Joel Sing <jsing@openbsd.org>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
# 

set -e
set -u
set -x

readonly SUBJECT="/CN=LibreSSL Test"

readonly TMPDIR=$(mktemp -d)

cleanup() {
        rm -rf "${TMPDIR}"
}

trap cleanup EXIT INT

reset() {
	echo '100001' > ${TMPDIR}/certserial
	cat /dev/null > ${TMPDIR}/certindex
}

setup() {
	reset

	cat > ${TMPDIR}/openssl.cnf <<EOF
[ca]
default_ca = test_ca

[test_ca]
new_certs_dir = ${TMPDIR}/
database = ${TMPDIR}/certindex
default_days = 365
default_md = sha256
policy = test_policy
serial = ${TMPDIR}/certserial

[test_policy]
countryName             = optional
stateOrProvinceName     = optional
localityName            = optional
organizationName        = optional
organizationalUnitName  = optional
commonName              = supplied
emailAddress            = optional

[v3_ca_root]
subjectKeyIdentifier = hash
authorityKeyIdentifier = keyid:always,issuer
basicConstraints = critical, CA:true
keyUsage = critical, cRLSign, keyCertSign

[v3_ca_int]
subjectKeyIdentifier = hash
authorityKeyIdentifier = keyid:always,issuer
basicConstraints = critical, CA:true
keyUsage = critical, cRLSign, keyCertSign

[v3_other]
subjectKeyIdentifier = hash
authorityKeyIdentifier = keyid:always,issuer
basicConstraints = critical, CA:false
keyUsage = critical, digitalSignature

[req]
distinguished_name      = req_distinguished_name

[ req_distinguished_name ]
EOF
}

key_type_to_args() {
	local key_type=$1

	alg=${key_type%:*}
	param=${key_type#*:}

	if [[ "${alg}" == "rsa" ]]; then
		echo "-newkey ${key_type}";
	elif [[ "${alg}" == "ec" ]]; then
		echo "-newkey $alg -pkeyopt ec_paramgen_curve:${param}"
	else
		echo "Unknown key type ${key_type}" >&2
		exit 1
	fi
}

create_root() {
	local name=$1 file=$2 key_type=$3

	key_args=$(key_type_to_args "${key_type}")

	openssl req -new -days 3650 -nodes ${key_args} -sha256 -x509 \
	    -subj "${SUBJECT} ${name}" -keyout "${TMPDIR}/${file}.key" \
	    -config ${TMPDIR}/openssl.cnf -extensions v3_ca_root \
	    -out "${TMPDIR}/${file}.crt"
}

create_intermediate() {
	local name=$1 file=$2 issuer_file=$3 key_type=$4

	key_args=$(key_type_to_args "${key_type}")

	openssl req -new -days 3650 -nodes ${key_args} -sha256 \
	    -subj "${SUBJECT} ${name}" -keyout "${TMPDIR}/${file}.key" \
	    -out "${TMPDIR}/${file}.csr"
	openssl x509 -req -days 3650 -CA "${TMPDIR}/${issuer_file}.crt" \
	    -CAkey "${TMPDIR}/${issuer_file}.key" -CAcreateserial \
	    -extfile ${TMPDIR}/openssl.cnf -extensions v3_ca_int \
	    -in "${TMPDIR}/${file}.csr" -out "${TMPDIR}/${file}.crt"
}

create_leaf() {
	local name=$1 file=$2 issuer_file=$3 key_type=$4

	key_args=$(key_type_to_args "${key_type}")

	openssl req -new -days 3650 -nodes ${key_args} -sha256 \
	    -subj "${SUBJECT} ${name}" -keyout "${TMPDIR}/${file}.key" \
	    -out "${TMPDIR}/${file}.csr"
	openssl x509 -req -days 3650 -CA "${TMPDIR}/${issuer_file}.crt" \
	    -CAkey "${TMPDIR}/${issuer_file}.key" -CAcreateserial -sha256 \
	    -extfile ${TMPDIR}/openssl.cnf -extensions v3_other \
	    -in "${TMPDIR}/${file}.csr" -out "${TMPDIR}/${file}.crt"
}

create_expired_leaf() {
	local name=$1 file=$2 issuer_file=$3 key_type=$4

	key_args=$(key_type_to_args "${key_type}")

	openssl req -new -days 3650 -nodes ${key_args} -sha256 \
	    -subj "${SUBJECT} ${name}" -keyout "${TMPDIR}/${file}.key" \
	    -out "${TMPDIR}/${file}.csr"
	openssl ca -batch -notext -cert "${TMPDIR}/${issuer_file}.crt" \
	    -keyfile "${TMPDIR}/${issuer_file}.key" \
	    -config ${TMPDIR}/openssl.cnf -extensions v3_other \
	    -startdate 20100101000000Z -enddate 20200101000000Z \
	    -in "${TMPDIR}/${file}.csr" -out "${TMPDIR}/${file}.crt"
}

create_revoked_leaf() {
	local name=$1 file=$2 issuer_file=$3 key_type=$4

	key_args=$(key_type_to_args "${key_type}")

	openssl req -new -days 3650 -nodes ${key_args} -sha256 \
	    -subj "${SUBJECT} ${name}" -keyout "${TMPDIR}/${file}.key" \
	    -out "${TMPDIR}/${file}.csr"
	openssl x509 -req -days 3650 -CA "${TMPDIR}/${issuer_file}.crt" \
	    -CAkey "${TMPDIR}/${issuer_file}.key" -CAcreateserial \
	    -extfile ${TMPDIR}/openssl.cnf -extensions v3_other \
	    -in "${TMPDIR}/${file}.csr" -out "${TMPDIR}/${file}.crt"
	openssl ca -cert "${TMPDIR}/${issuer_file}.crt" \
	    -keyfile "${TMPDIR}/${issuer_file}.key" \
	    -config "${TMPDIR}/openssl.cnf" -extensions v3_other \
	    -revoke "${TMPDIR}/${file}.crt"
	openssl ca -gencrl -cert "${TMPDIR}/${issuer_file}.crt" \
	    -keyfile "${TMPDIR}/${issuer_file}.key" \
	    -config "${TMPDIR}/openssl.cnf" -extensions v3_other \
	    -crldays 30 -out "${TMPDIR}/${issuer_file}.crl"
}

create_bundle() {
	local bundle_file=$1
	shift

	mkdir -p $(dirname ${bundle_file})
	cat /dev/null > ${bundle_file}

	for _cert_file in $@; do
		openssl x509 -nameopt oneline -subject -issuer \
		    -in "${TMPDIR}/${_cert_file}.crt" >> ${bundle_file}
	done
}

create_bundle_with_key() {
	local bundle_file=$1
	shift

	mkdir -p $(dirname ${bundle_file})
	cat /dev/null > ${bundle_file}

	for _cert_file in $@; do
		openssl x509 -nameopt oneline -subject -issuer -noout \
		    -in "${TMPDIR}/${_cert_file}.crt" >> ${bundle_file}
	done
	for _cert_file in $@; do
		cat "${TMPDIR}/${_cert_file}.crt" >> ${bundle_file}
	done
	for _key_file in $@; do
		cat "${TMPDIR}/${_key_file}.key" >> ${bundle_file}
	done
}

setup

reset
create_root "Root CA RSA" "ca-root-rsa" "rsa:2048"
create_intermediate "Intermediate CA RSA" "ca-int-rsa" "ca-root-rsa" "rsa:2048"
create_leaf "Server 1 RSA" "server-1-rsa" "ca-int-rsa" "rsa:2048"
create_expired_leaf "Server 2 RSA" "server-2-rsa" "ca-int-rsa" "rsa:2048"
create_revoked_leaf "Server 3 RSA" "server-3-rsa" "ca-int-rsa" "rsa:2048"
create_leaf "Client 1 RSA" "client-1-rsa" "ca-int-rsa" "rsa:2048"
create_expired_leaf "Client 2 RSA" "client-2-rsa" "ca-int-rsa" "rsa:2048"
create_revoked_leaf "Client 3 RSA" "client-3-rsa" "ca-int-rsa" "rsa:2048"

create_bundle "./ca-root-rsa.pem" "ca-root-rsa"
create_bundle "./ca-int-rsa.pem" "ca-int-rsa"
cp "${TMPDIR}/ca-int-rsa.crl" "./ca-int-rsa.crl"
create_bundle_with_key "./server1-rsa.pem" "server-1-rsa"
create_bundle "./server1-rsa-chain.pem" "server-1-rsa" "ca-int-rsa"
create_bundle_with_key "./server2-rsa.pem" "server-2-rsa"
create_bundle "./server2-rsa-chain.pem" "server-2-rsa" "ca-int-rsa"
create_bundle_with_key "./server3-rsa.pem" "server-3-rsa"
create_bundle "./server3-rsa-chain.pem" "server-3-rsa" "ca-int-rsa"
create_bundle_with_key "./client1-rsa.pem" "client-1-rsa"
create_bundle "./client1-rsa-chain.pem" "client-1-rsa" "ca-int-rsa"
create_bundle_with_key "./client2-rsa.pem" "client-2-rsa"
create_bundle "./client2-rsa-chain.pem" "client-2-rsa" "ca-int-rsa"
create_bundle_with_key "./client3-rsa.pem" "client-3-rsa"
create_bundle "./client3-rsa-chain.pem" "client-3-rsa" "ca-int-rsa"

reset
create_root "Root CA ECDSA" "ca-root-ecdsa" "ec:prime256v1"
create_intermediate "Intermediate CA ECDSA" "ca-int-ecdsa" "ca-root-ecdsa" "ec:prime256v1"
create_leaf "Server 1 ECDSA" "server-1-ecdsa" "ca-int-ecdsa" "ec:prime256v1"
create_expired_leaf "Server 2 ECDSA" "server-2-ecdsa" "ca-int-ecdsa" "ec:prime256v1"
create_revoked_leaf "Server 3 ECDSA" "server-3-ecdsa" "ca-int-ecdsa" "ec:prime256v1"
create_leaf "Client 1 ECDSA" "client-1-ecdsa" "ca-int-ecdsa" "ec:prime256v1"
create_expired_leaf "Client 2 ECDSA" "client-2-ecdsa" "ca-int-ecdsa" "ec:prime256v1"
create_revoked_leaf "Client 3 ECDSA" "client-3-ecdsa" "ca-int-ecdsa" "ec:prime256v1"

create_bundle "./ca-root-ecdsa.pem" "ca-root-ecdsa"
create_bundle "./ca-int-ecdsa.pem" "ca-int-ecdsa"
cp "${TMPDIR}/ca-int-ecdsa.crl" "./ca-int-ecdsa.crl"
create_bundle_with_key "./server1-ecdsa.pem" "server-1-ecdsa"
create_bundle "./server1-ecdsa-chain.pem" "server-1-ecdsa" "ca-int-ecdsa"
create_bundle_with_key "./server2-ecdsa.pem" "server-2-ecdsa"
create_bundle "./server2-ecdsa-chain.pem" "server-2-ecdsa" "ca-int-ecdsa"
create_bundle_with_key "./server3-ecdsa.pem" "server-3-ecdsa"
create_bundle "./server3-ecdsa-chain.pem" "server-3-ecdsa" "ca-int-ecdsa"
create_bundle_with_key "./client1-ecdsa.pem" "client-1-ecdsa"
create_bundle "./client1-ecdsa-chain.pem" "client-1-ecdsa" "ca-int-ecdsa"
create_bundle_with_key "./client2-ecdsa.pem" "client-2-ecdsa"
create_bundle "./client2-ecdsa-chain.pem" "client-2-ecdsa" "ca-int-ecdsa"
create_bundle_with_key "./client3-ecdsa.pem" "client-3-ecdsa"
create_bundle "./client3-ecdsa-chain.pem" "client-3-ecdsa" "ca-int-ecdsa"