summaryrefslogtreecommitdiff
path: root/src/regress/lib/libcrypto/certs/make-certs.sh
blob: 3854ff13e047b9e1d5ee0e2b9c4e3b2310de088a (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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
#!/bin/ksh

#
# Copyright (c) 2020 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
}

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

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

create_expired_root() {
	local name=$1 file=$2

	openssl req -new -days 3650 -nodes -newkey rsa:2048 -sha256 \
	    -subj "${SUBJECT} ${name}" -keyout "${TMPDIR}/${file}.key" \
	    -config ${TMPDIR}/openssl.cnf -extensions v3_ca_root \
	    -out "${TMPDIR}/${file}.csr"
	openssl ca -batch -notext -selfsign \
	    -keyfile "${TMPDIR}/${file}.key" \
	    -startdate 20100101000000Z -enddate 20200101000000Z \
	    -config ${TMPDIR}/openssl.cnf -extensions v3_ca_root \
	    -in "${TMPDIR}/${file}.csr" -out "${TMPDIR}/${file}.crt"
}

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

	openssl req -new -days 3650 -nodes -newkey rsa:2048 -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_expired_intermediate() {
	local name=$1 file=$2 issuer_file=$3

	openssl req -new -days 3650 -nodes -newkey rsa:2048 -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" \
	    -startdate 20100101000000Z -enddate 20200101000000Z \
	    -config ${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

	openssl req -new -days 3650 -nodes -newkey rsa:2048 -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"
}

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

	openssl req -new -days 3650 -nodes -newkey rsa:2048 -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_cross_signed() {
	local file=$1 csr_file=$2 issuer_file=$3

	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}/${csr_file}.csr" -out "${TMPDIR}/${file}.crt"
}

create_expired_cross_signed() {
	local file=$1 csr_file=$2 issuer_file=$3

	openssl ca -batch -notext -cert "${TMPDIR}/${issuer_file}.crt" \
	    -keyfile "${TMPDIR}/${issuer_file}.key" \
	    -startdate 20100101000000Z -enddate 20200101000000Z \
	    -config ${TMPDIR}/openssl.cnf -extensions v3_ca_int \
	    -in "${TMPDIR}/${csr_file}.csr" -out "${TMPDIR}/${file}.crt"
}

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 -noout \
		    -in "${TMPDIR}/${_cert_file}.crt" >> ${bundle_file}
	done
	for _cert_file in $@; do
		cat "${TMPDIR}/${_cert_file}.crt" >> ${bundle_file}
	done
}

create_root_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
}

setup

# Scenario 1a.
reset
create_root "Root CA 1" "ca-root"
create_leaf "Server 1" "server-1" "ca-root"
create_root_bundle "./1a/roots.pem" "ca-root"
create_bundle "./1a/bundle.pem" "server-1"

# Scenarios 2a and 2b.
reset
create_root "Root CA 1" "ca-root"
create_intermediate "Intermediate CA 1" "ca-int-1" "ca-root"
create_leaf "Server 1" "server-1" "ca-int-1"
create_root_bundle "./2a/roots.pem" "ca-root"
create_bundle "./2a/bundle.pem" "server-1" "ca-int-1"
create_root_bundle "./2b/roots.pem" "ca-root"
create_bundle "./2b/bundle.pem" "server-1"
create_root_bundle "./2c/roots.pem" "ca-root"
create_bundle "./2c/bundle.pem" "server-1" "ca-root" "ca-int-1"

# Scenarios 3a, 3b, 3c, 3d and 3e.
reset
create_root "Root CA 1" "ca-root"
create_intermediate "Intermediate CA 1" "ca-int-1" "ca-root"
create_intermediate "Intermediate CA 2" "ca-int-2" "ca-int-1"
create_intermediate "Intermediate CA 3" "ca-int-3" "ca-int-2"
create_leaf "Server 1" "server-1" "ca-int-3"
create_root_bundle "./3a/roots.pem" "ca-root"
create_bundle "./3a/bundle.pem" "server-1" "ca-int-3" "ca-int-2" "ca-int-1"
create_root_bundle "./3b/roots.pem" "ca-root"
create_bundle "./3b/bundle.pem" "server-1" "ca-int-3" "ca-int-2"
create_root_bundle "./3c/roots.pem" "ca-root"
create_bundle "./3c/bundle.pem" "server-1" "ca-int-3" "ca-int-1"
create_root_bundle "./3d/roots.pem" "ca-root"
create_bundle "./3d/bundle.pem" "server-1" "ca-int-2" "ca-int-1"
create_root_bundle "./3e/roots.pem" "ca-root"
create_bundle "./3e/bundle.pem" "server-1" "ca-int-1" "ca-int-2" "ca-int-3"

# Scenarios 4a, 4b, 4c, 4d, 4e, 4f, 4g and 4h.
reset
create_root "Root CA 1" "ca-root-1"
create_root "Root CA 2" "ca-root-2"
create_intermediate "Intermediate CA 1" "ca-int-1" "ca-root-1"
create_cross_signed "ca-int-1-xs" "ca-int-1" "ca-root-2"
create_leaf "Server 1" "server-1" "ca-int-1"
create_root_bundle "./4a/roots.pem" "ca-root-1" "ca-root-2"
create_bundle "./4a/bundle.pem" "server-1" "ca-int-1" "ca-int-1-xs"
create_root_bundle "./4b/roots.pem" "ca-root-1"
create_bundle "./4b/bundle.pem" "server-1" "ca-int-1" "ca-int-1-xs"
create_root_bundle "./4c/roots.pem" "ca-root-2"
create_bundle "./4c/bundle.pem" "server-1" "ca-int-1" "ca-int-1-xs"
create_root_bundle "./4d/roots.pem" "ca-root-1" "ca-root-2"
create_bundle "./4d/bundle.pem" "server-1" "ca-int-1"
create_root_bundle "./4e/roots.pem" "ca-root-1" "ca-root-2"
create_bundle "./4e/bundle.pem" "server-1" "ca-int-1-xs"
create_root_bundle "./4f/roots.pem" "ca-root-1" "ca-root-2"
create_bundle "./4f/bundle.pem" "server-1" "ca-int-1-xs" "ca-int-1"
create_root_bundle "./4g/roots.pem" "ca-root-1"
create_bundle "./4g/bundle.pem" "server-1" "ca-int-1-xs" "ca-int-1"
create_root_bundle "./4h/roots.pem" "ca-root-2"
create_bundle "./4h/bundle.pem" "server-1" "ca-int-1-xs" "ca-int-1"

# Scenario 5a, 5b, 5c, 5d, 5e, 5f, 5g, 5h and 5i.
reset
create_root "Root CA 1" "ca-root-1"
create_intermediate "Intermediate CA 1" "ca-int-1" "ca-root-1"
create_intermediate "Intermediate CA 2" "ca-int-2" "ca-int-1"
create_root "Root CA 2" "ca-root-2"
create_cross_signed "ca-int-2-xs" "ca-int-2" "ca-root-2"
create_leaf "Server 1" "server-1" "ca-int-2"
create_root_bundle "./5a/roots.pem" "ca-root-1" "ca-root-2"
create_bundle "./5a/bundle.pem" "server-1" "ca-int-2" "ca-int-2-xs" "ca-int-1"
create_root_bundle "./5b/roots.pem" "ca-root-2"
create_bundle "./5b/bundle.pem" "server-1" "ca-int-2" "ca-int-2-xs" "ca-int-1"
create_root_bundle "./5c/roots.pem" "ca-root-1"
create_bundle "./5c/bundle.pem" "server-1" "ca-int-2" "ca-int-2-xs" "ca-int-1"
create_root_bundle "./5d/roots.pem" "ca-root-1" "ca-root-2"
create_bundle "./5d/bundle.pem" "server-1" "ca-int-2-xs" "ca-int-1"
create_root_bundle "./5e/roots.pem" "ca-root-1" "ca-root-2"
create_bundle "./5e/bundle.pem" "server-1" "ca-int-2" "ca-int-2-xs"
create_root_bundle "./5f/roots.pem" "ca-root-1" "ca-root-2"
create_bundle "./5f/bundle.pem" "server-1" "ca-int-2" "ca-int-1"
create_root_bundle "./5g/roots.pem" "ca-root-1" "ca-root-2"
create_bundle "./5g/bundle.pem" "server-1" "ca-int-2-xs" "ca-int-2" "ca-int-1"
create_root_bundle "./5h/roots.pem" "ca-root-2"
create_bundle "./5h/bundle.pem" "server-1" "ca-int-2-xs" "ca-int-2" "ca-int-1"
create_root_bundle "./5i/roots.pem" "ca-root-1"
create_bundle "./5i/bundle.pem" "server-1" "ca-int-2-xs" "ca-int-2" "ca-int-1"

# Scenarios 6a and 6b.
reset
create_root "Root CA 1" "ca-root-1"
create_intermediate "Intermediate CA 1" "ca-int-1" "ca-root-1"
create_intermediate "Intermediate CA 2" "ca-int-2" "ca-int-1"
create_expired_root "Root CA 2" "ca-root-2"
create_cross_signed "ca-int-2-xs" "ca-int-2" "ca-root-2"
create_leaf "Server 1" "server-1" "ca-int-2"
create_root_bundle "./6a/roots.pem" "ca-root-1" "ca-root-2"
create_bundle "./6a/bundle.pem" "server-1" "ca-int-2" "ca-int-2-xs" "ca-int-1"
create_root_bundle "./6b/roots.pem" "ca-root-1" "ca-root-2"
create_bundle "./6b/bundle.pem" "server-1" "ca-int-2-xs" "ca-int-2" "ca-int-1"

# Scenarios 7a and 7b.
reset
create_expired_root "Root CA 1" "ca-root-1"
create_intermediate "Intermediate CA 1" "ca-int-1" "ca-root-1"
create_intermediate "Intermediate CA 2" "ca-int-2" "ca-int-1"
create_root "Root CA 2" "ca-root-2"
create_cross_signed "ca-int-2-xs" "ca-int-2" "ca-root-2"
create_leaf "Server 1" "server-1" "ca-int-2"
create_root_bundle "./7a/roots.pem" "ca-root-1" "ca-root-2"
create_bundle "./7a/bundle.pem" "server-1" "ca-int-2" "ca-int-2-xs" "ca-int-1"
create_root_bundle "./7b/roots.pem" "ca-root-1" "ca-root-2"
create_bundle "./7b/bundle.pem" "server-1" "ca-int-2-xs" "ca-int-2" "ca-int-1"

# Scenario 8a.
reset
create_root "Root CA 1" "ca-root"
create_intermediate "Intermediate CA 1" "ca-int-1" "ca-root"
create_expired_leaf "Server 1" "server-1" "ca-int-1"
create_root_bundle "./8a/roots.pem" "ca-root"
create_bundle "./8a/bundle.pem" "server-1" "ca-int-1"

# Scenario 9a.
reset
create_root "Root CA 1" "ca-root"
create_expired_intermediate "Intermediate CA 1" "ca-int-1" "ca-root"
create_leaf "Server 1" "server-1" "ca-int-1"
create_root_bundle "./9a/roots.pem" "ca-root"
create_bundle "./9a/bundle.pem" "server-1" "ca-int-1"

# Scenarios 10a and 10b.
reset
create_root "Root CA 1" "ca-root-1"
create_root "Root CA 2" "ca-root-2"
create_expired_intermediate "Intermediate CA 1" "ca-int-1" "ca-root-1"
create_cross_signed "ca-int-1-xs" "ca-int-1" "ca-root-2"
create_leaf "Server 1" "server-1" "ca-int-1"
create_root_bundle "./10a/roots.pem" "ca-root-1" "ca-root-2"
create_bundle "./10a/bundle.pem" "server-1" "ca-int-1" "ca-int-1-xs"
create_root_bundle "./10b/roots.pem" "ca-root-1" "ca-root-2"
create_bundle "./10b/bundle.pem" "server-1" "ca-int-1-xs" "ca-int-1"

# Scenarios 11a and 11b.
reset
create_root "Root CA 1" "ca-root-1"
create_expired_intermediate "Intermediate CA 1" "ca-int-1" "ca-root-1"
create_intermediate "Intermediate CA 2" "ca-int-2" "ca-int-1"
create_root "Root CA 2" "ca-root-2"
create_cross_signed "ca-int-2-xs" "ca-int-2" "ca-root-2"
create_leaf "Server 1" "server-1" "ca-int-2"
create_root_bundle "./11a/roots.pem" "ca-root-1" "ca-root-2"
create_bundle "./11a/bundle.pem" "server-1" "ca-int-2" "ca-int-2-xs" "ca-int-1"
create_root_bundle "./11b/roots.pem" "ca-root-1" "ca-root-2"
create_bundle "./11b/bundle.pem" "server-1" "ca-int-2-xs" "ca-int-2" "ca-int-1"

# Scenario 12a.
reset
create_root "Root CA 1" "ca-root-1"
create_expired_intermediate "Intermediate CA 1" "ca-int-1" "ca-root-1"
create_cross_signed "ca-root-2" "ca-int-1" "ca-root-1"
create_intermediate "Intermediate CA 2" "ca-int-2" "ca-int-1"
create_leaf "Server 1" "server-1" "ca-int-2"
create_root_bundle "./12a/roots.pem" "ca-root-1" "ca-root-2"
create_bundle "./12a/bundle.pem" "server-1" "ca-int-2" "ca-int-1"

# Scenario 13a.
reset
create_root "Root CA 1" "ca-root-1"
create_intermediate "Intermediate CA 1" "ca-int-1" "ca-root-1"
create_expired_cross_signed "ca-root-2" "ca-int-1" "ca-root-1"
create_intermediate "Intermediate CA 2" "ca-int-2" "ca-int-1"
create_leaf "Server 1" "server-1" "ca-int-2"
create_root_bundle "./13a/roots.pem" "ca-root-1" "ca-root-2"
create_bundle "./13a/bundle.pem" "server-1" "ca-int-2" "ca-int-1"