summaryrefslogtreecommitdiffstats
path: root/net/data/ssl/scripts/generate-multi-root-test-chains.sh
blob: c4d1792e1b7d0aa2f1d308800007158ddab2f0bb (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
#!/bin/sh

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

# The following documentation uses the annotation approach from RFC 4158.
# CAs (entities that share the same name and public key) are denoted in boxes,
# while the indication that a CA Foo signed a certificate for CA Bar is denoted
# by directed arrows.
#
#   +---+    +-----+
#   | D |    |  E  |
#   +---+    +-----+
#     |       |   |
#     +--v v--+   |
#       +---+   +---+
#       | C |   | F |
#       +---+   +---+
#         |       |
#         v   v---+
#        +-----+
#        |  B  |
#        +-----+
#           |
#           v
#         +---+
#         | A |
#         +---+
#
# To validate A, there are several possible paths, using A(B) to indicate
# the certificate A signed by B:
#
# 1. A(B) -> B(C) -> C(D) -> D(D)
# 3. A(B) -> B(C) -> C(E) -> E(E)
# 4. A(B) -> B(F) -> F(E) -> E(E)
#
# That is, there are two different versions of C (signed by D and E) and
# two versions of B (signed by C and F). Possible trust anchors are D and E,
# which are both self-signed.
#
# The goal is to ensure that, as long as at least one of C or F is still valid,
# clients are able to successfully build a valid path.

# Exit script as soon a something fails.
set -e

rm -rf out
mkdir out

echo Create the serial and index number files.
serial=1000
for i in B C D E F
do
  /bin/sh -c "echo ${serial} > out/${i}-serial"
  touch "out/${i}-index.txt"
done

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

echo "Generating the self-signed roots"
for i in D E
do
  echo "Generating CSR ${i}"
  CA_COMMON_NAME="${i} Root CA - Multi-root" \
  CERTIFICATE="${i}" \
  openssl req \
    -config redundant-ca.cnf \
    -new \
    -key "out/${i}.key" \
    -out "out/${i}.csr"

  echo "Generating self-signed ${i}"
  CA_COMMON_NAME="${i} Root CA - Multi-root" \
  CERTIFICATE="${i}" \
  openssl ca \
    -config redundant-ca.cnf \
    -batch \
    -startdate 160102000000Z \
    -enddate 260102000000Z \
    -extensions ca_cert \
    -extfile redundant-ca.cnf \
    -selfsign \
    -in "out/${i}.csr" \
    -out "out/${i}.pem"
done

echo "Generating intermediate CSRs"
for i in B C F
do
  echo "Generating CSR ${i}"
  CA_COMMON_NAME="${i} CA - Multi-root" \
  CERTIFICATE="${i}" \
  openssl req \
    -config redundant-ca.cnf \
    -new \
    -key "out/${i}.key" \
    -out "out/${i}.csr"
done

echo D signs C
CA_COMMON_NAME="D CA - Multi-root" \
CERTIFICATE=D \
openssl ca \
  -config redundant-ca.cnf \
  -batch \
  -startdate 160103000000Z \
  -enddate 260102000000Z \
  -extensions ca_cert \
  -extfile redundant-ca.cnf \
  -in out/C.csr \
  -out out/C.pem

echo C signs B
CA_COMMON_NAME="C CA - Multi-root" \
CERTIFICATE=C \
openssl ca \
  -config redundant-ca.cnf \
  -batch \
  -startdate 160104000000Z \
  -enddate 260102000000Z \
  -extensions ca_cert \
  -extfile redundant-ca.cnf \
  -in out/B.csr \
  -out out/B.pem

echo E signs C2
CA_COMMON_NAME="E CA - Multi-root" \
CERTIFICATE=E \
openssl ca \
  -config redundant-ca.cnf \
  -batch \
  -startdate 160105000000Z \
  -enddate 260102000000Z \
  -extensions ca_cert \
  -extfile redundant-ca.cnf \
  -in out/C.csr \
  -out out/C2.pem

echo E signs F
CA_COMMON_NAME="E CA - Multi-root" \
CERTIFICATE=E \
openssl ca \
  -config redundant-ca.cnf \
  -batch \
  -startdate 160102000000Z \
  -enddate 260102000000Z \
  -extensions ca_cert \
  -extfile redundant-ca.cnf \
  -in out/F.csr \
  -out out/F.pem

# Note: The startdate for B-by-F MUST be different than that of B-by-C; to make
# B-by-F more preferable, the startdate is chosen to be GREATER (later) than
# B-by-C.
echo F signs B2
CA_COMMON_NAME="F CA - Multi-root" \
CERTIFICATE=F \
openssl ca \
  -config redundant-ca.cnf \
  -batch \
  -startdate 160105000000Z \
  -enddate 260102000000Z \
  -extensions ca_cert \
  -extfile redundant-ca.cnf \
  -in out/B.csr \
  -out out/B2.pem

echo "Generating leaf CSRs"
for i in A
do
  echo "Generating leaf ${i}"
  openssl req \
    -config ee.cnf \
    -new \
    -key "out/${i}.key" \
    -out "out/${i}.csr"
done

echo "Signing leaves"
CA_COMMON_NAME="B CA - Multi-root" \
CERTIFICATE=B \
openssl ca \
  -config redundant-ca.cnf \
  -batch \
  -days 3650 \
  -extensions user_cert \
  -extfile redundant-ca.cnf \
  -in out/A.csr \
  -out out/A.pem

echo "Copying outputs"
/bin/sh -c "cat out/A.key out/A.pem > ../certificates/multi-root-A-by-B.pem"
/bin/sh -c "cat out/A.pem out/B.pem out/C.pem out/D.pem \
    > ../certificates/multi-root-chain1.pem"
/bin/sh -c "cat out/A.pem out/B.pem out/C2.pem out/E.pem \
    > ../certificates/multi-root-chain2.pem"
cp out/B.pem ../certificates/multi-root-B-by-C.pem
cp out/B2.pem ../certificates/multi-root-B-by-F.pem
cp out/C.pem ../certificates/multi-root-C-by-D.pem
cp out/C2.pem ../certificates/multi-root-C-by-E.pem
cp out/F.pem ../certificates/multi-root-F-by-E.pem
cp out/D.pem ../certificates/multi-root-D-by-D.pem
cp out/E.pem ../certificates/multi-root-E-by-E.pem

echo "Generating CRLSets"
# Block D and E by SPKI; invalidates all paths.
python crlsetutil.py -o ../certificates/multi-root-crlset-D-and-E.raw \
<<CRLSETDOCBLOCK
{
  "BlockedBySPKI": [
    "out/D.pem",
    "out/E.pem"
  ]
}
CRLSETDOCBLOCK

# Block E by SPKI.
python crlsetutil.py -o ../certificates/multi-root-crlset-E.raw \
<<CRLSETDOCBLOCK
{
  "BlockedBySPKI": [
    "out/E.pem"
  ]
}
CRLSETDOCBLOCK

# Block C-by-D (serial number 0x1001) and F-by-E (serial number 0x1002) by
# way of serial number.
python crlsetutil.py -o ../certificates/multi-root-crlset-CD-and-FE.raw \
<<CRLSETDOCBLOCK
{
  "BlockedByHash": {
    "out/D.pem": [4097],
    "out/E.pem": [4098]
  }
}
CRLSETDOCBLOCK

# Block C (all versions) by way of SPKI
python crlsetutil.py -o ../certificates/multi-root-crlset-C.raw \
<<CRLSETDOCBLOCK
{
  "BlockedBySPKI": [ "out/C.pem" ]
}
CRLSETDOCBLOCK

# Block an unrelated/unissued serial (0x0FFF) to enable all paths.
python crlsetutil.py -o ../certificates/multi-root-crlset-unrelated.raw \
<<CRLSETDOCBLOCK
{
  "BlockedByHash": {
    "out/E.pem": [4095]
  }
}
CRLSETDOCBLOCK