File: generate-client-certificates.sh

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (315 lines) | stat: -rwxr-xr-x 7,942 bytes parent folder | download | duplicates (6)
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
#!/bin/bash

# Copyright 2012 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

# This script generates certificates that can be used to test SSL client
# authentication. Outputs for automated tests are stored in
# net/data/ssl/certificates, but may be re-generated for manual testing.
#
# This script generates several chains of test client certificates:
#
#   1. A (end-entity) -> B -> C (self-signed root)
#   2. D (end-entity) -> E -> C (self-signed root)
#   3. F (end-entity) -> E -> C (self-signed root)
#   4. G (end-entity, P-256) -> E -> C (self-signed root)
#   5. H (end-entity, P-384) -> E -> C (self-signed root)
#   6. I (end-entity, P-521) -> E -> C (self-signed root)
#   7. J (end-entity, RSA-1024) -> E -> C (self-signed root)
#
# In which the certificates all have distinct keypairs. The client
# certificates share the same root, but are issued by different
# intermediates. The names of these intermediates are hardcoded within
# unit tests, and thus should not be changed.

try () {
  echo "$@"
  "$@" || exit 1
}

try rm -rf out
try mkdir out

echo Create the serial number files and indices.
serial=1000
for i in B C E
do
  try /bin/sh -c "echo $serial > out/$i-serial"
  serial=$(expr $serial + 1)
  touch out/$i-index.txt
  touch out/$i-index.txt.attr
done

echo Generate the keys.
for i in A B C D E F
do
  try openssl genrsa -out out/$i.key 2048
done

try openssl ecparam -name prime256v1 -genkey -noout -out out/G.key
try openssl ecparam -name secp384r1 -genkey -noout -out out/H.key
try openssl ecparam -name secp521r1 -genkey -noout -out out/I.key
try openssl genrsa -out out/J.key 1024

echo Generate the C CSR
COMMON_NAME="C Root CA" \
  CA_DIR=out \
  ID=C \
  try openssl req \
    -new \
    -key out/C.key \
    -out out/C.csr \
    -config client-certs.cnf

echo C signs itself.
COMMON_NAME="C Root CA" \
  CA_DIR=out \
  ID=C \
  try openssl x509 \
    -req -days 3650 \
    -in out/C.csr \
    -extensions ca_cert \
    -extfile client-certs.cnf \
    -signkey out/C.key \
    -out out/C.pem

echo Generate the intermediates
COMMON_NAME="B CA" \
  CA_DIR=out \
  ID=B \
  try openssl req \
    -new \
    -key out/B.key \
    -out out/B.csr \
    -config client-certs.cnf

COMMON_NAME="C CA" \
  CA_DIR=out \
  ID=C \
  try openssl ca \
    -batch \
    -extensions ca_cert \
    -in out/B.csr \
    -out out/B.pem \
    -config client-certs.cnf

COMMON_NAME="E CA" \
  CA_DIR=out \
  ID=E \
  try openssl req \
    -new \
    -key out/E.key \
    -out out/E.csr \
    -config client-certs.cnf

COMMON_NAME="C CA" \
  CA_DIR=out \
  ID=C \
  try openssl ca \
    -batch \
    -extensions ca_cert \
    -in out/E.csr \
    -out out/E.pem \
    -config client-certs.cnf

echo Generate the leaf certs
for id in A D F G H I J
do
  COMMON_NAME="Client Cert $id" \
  ID=$id \
  try openssl req \
    -new \
    -key out/$id.key \
    -out out/$id.csr \
    -config client-certs.cnf
  # Store the private key also in PKCS#8 format.
  try openssl pkcs8 \
    -topk8 -nocrypt \
    -in out/$id.key \
    -outform DER \
    -out out/$id.pk8
done

echo B signs A
COMMON_NAME="B CA" \
  CA_DIR=out \
  ID=B \
  try openssl ca \
    -batch \
    -extensions user_cert \
    -in out/A.csr \
    -out out/A.pem \
    -config client-certs.cnf

echo E signs D
COMMON_NAME="E CA" \
  CA_DIR=out \
  ID=E \
  try openssl ca \
    -batch \
    -extensions user_cert \
    -in out/D.csr \
    -out out/D.pem \
    -config client-certs.cnf

echo E signs F
COMMON_NAME="E CA" \
  CA_DIR=out \
  ID=E \
  try openssl ca \
    -batch \
    -extensions san_user_cert \
    -in out/F.csr \
    -out out/F.pem \
    -config client-certs.cnf

echo E signs G
COMMON_NAME="E CA" \
  CA_DIR=out \
  ID=E \
  try openssl ca \
    -batch \
    -extensions user_cert \
    -in out/G.csr \
    -out out/G.pem \
    -config client-certs.cnf

echo E signs H
COMMON_NAME="E CA" \
  CA_DIR=out \
  ID=E \
  try openssl ca \
    -batch \
    -extensions user_cert \
    -in out/H.csr \
    -out out/H.pem \
    -config client-certs.cnf

echo E signs I
COMMON_NAME="E CA" \
  CA_DIR=out \
  ID=E \
  try openssl ca \
    -batch \
    -extensions user_cert \
    -in out/I.csr \
    -out out/I.pem \
    -config client-certs.cnf

echo E signs J
COMMON_NAME="E CA" \
  CA_DIR=out \
  ID=E \
  try openssl ca \
    -batch \
    -extensions user_cert \
    -in out/J.csr \
    -out out/J.pem \
    -config client-certs.cnf

echo Package the client certs and private keys into PKCS12 files
# This is done for easily importing all of the certs needed for clients.
try /bin/sh -c "cat out/A.pem out/A.key out/B.pem out/C.pem > out/A-chain.pem"
try /bin/sh -c "cat out/D.pem out/D.key out/E.pem out/C.pem > out/D-chain.pem"
try /bin/sh -c "cat out/F.pem out/F.key out/E.pem out/C.pem > out/F-chain.pem"
try /bin/sh -c "cat out/G.pem out/G.key out/E.pem out/C.pem > out/G-chain.pem"
try /bin/sh -c "cat out/H.pem out/H.key out/E.pem out/C.pem > out/H-chain.pem"
try /bin/sh -c "cat out/I.pem out/I.key out/E.pem out/C.pem > out/I-chain.pem"
try /bin/sh -c "cat out/J.pem out/J.key out/E.pem out/C.pem > out/J-chain.pem"

try openssl pkcs12 \
  -in out/A-chain.pem \
  -out out/client_1.p12 \
  -export \
  -passout pass:chrome

try openssl pkcs12 \
  -in out/D-chain.pem \
  -out out/client_2.p12 \
  -export \
  -passout pass:chrome

try openssl pkcs12 \
  -in out/F-chain.pem \
  -out out/client_3.p12 \
  -export \
  -passout pass:chrome

try openssl pkcs12 \
  -in out/G-chain.pem \
  -out out/client_4.p12 \
  -export \
  -passout pass:chrome

try openssl pkcs12 \
  -in out/H-chain.pem \
  -out out/client_5.p12 \
  -export \
  -passout pass:chrome

try openssl pkcs12 \
  -in out/I-chain.pem \
  -out out/client_6.p12 \
  -export \
  -passout pass:chrome

try openssl pkcs12 \
  -in out/J-chain.pem \
  -out out/client_7.p12 \
  -export \
  -passout pass:chrome

try openssl pkcs12 \
  -inkey out/A.key \
  -in out/A.pem \
  -out out/client_1_u16_password.p12 \
  -export \
  -passout pass:"Hello, 世界"

echo Package the client certs for unit tests
try cp out/A.pem ../certificates/client_1.pem
try cp out/A.key ../certificates/client_1.key
try cp out/A.pk8 ../certificates/client_1.pk8
try cp out/B.pem ../certificates/client_1_ca.pem

try cp out/D.pem ../certificates/client_2.pem
try cp out/D.key ../certificates/client_2.key
try cp out/D.pk8 ../certificates/client_2.pk8
try cp out/E.pem ../certificates/client_2_ca.pem

try cp out/F.pem ../certificates/client_3.pem
try cp out/F.key ../certificates/client_3.key
try cp out/F.pk8 ../certificates/client_3.pk8
try cp out/E.pem ../certificates/client_3_ca.pem

try cp out/G.pem ../certificates/client_4.pem
try cp out/G.key ../certificates/client_4.key
try cp out/G.pk8 ../certificates/client_4.pk8
try cp out/E.pem ../certificates/client_4_ca.pem

try cp out/H.pem ../certificates/client_5.pem
try cp out/H.key ../certificates/client_5.key
try cp out/H.pk8 ../certificates/client_5.pk8
try cp out/E.pem ../certificates/client_5_ca.pem

try cp out/I.pem ../certificates/client_6.pem
try cp out/I.key ../certificates/client_6.key
try cp out/I.pk8 ../certificates/client_6.pk8
try cp out/E.pem ../certificates/client_6_ca.pem

try cp out/J.pem ../certificates/client_7.pem
try cp out/J.key ../certificates/client_7.key
try cp out/J.pk8 ../certificates/client_7.pk8
try cp out/E.pem ../certificates/client_7_ca.pem

try cp out/client_1.p12 ../certificates/client_1.p12
try cp out/client_2.p12 ../certificates/client_2.p12
try cp out/client_3.p12 ../certificates/client_3.p12
try cp out/client_4.p12 ../certificates/client_4.p12
try cp out/client_5.p12 ../certificates/client_5.p12
try cp out/client_6.p12 ../certificates/client_6.p12
try cp out/client_7.p12 ../certificates/client_7.p12
try cp out/client_1_u16_password.p12 ../certificates/client_1_u16_password.p12

try cp out/C.pem ../certificates/client_root_ca.pem