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
|
package x509util
import (
"bytes"
"fmt"
"testing"
"crypto/x509"
"github.com/system-transparency/stfe/x509util/testdata"
)
func TestNewEd25519PrivateKey(t *testing.T) {
for _, table := range []struct {
description string
pem []byte
wantErr bool
}{
{
description: "bad block: unwanted white space",
pem: testdata.Ed25519PrivateKeyBadWhiteSpace,
wantErr: true,
},
{
description: "invalid block type",
pem: testdata.EndEntityCertificate,
wantErr: true,
},
{
description: "bad block: trailing data",
pem: testdata.DoubleEd25519PrivateKey,
wantErr: true,
},
{
description: "bad block bytes: truncated key",
pem: testdata.TruncatedEd25519PrivateKey,
wantErr: true,
},
{
description: "bad block bytes: not an ed25519 private key",
pem: testdata.NotEd25519PrivateKey,
wantErr: true,
},
{
description: "ok ed25519 private key",
pem: testdata.EndEntityPrivateKey,
},
} {
_, err := NewEd25519PrivateKey(table.pem)
if got, want := err != nil, table.wantErr; got != want {
t.Errorf("got error=%v but wanted %v in test %q: %v", got, want, table.description, err)
}
}
}
func TestNewCertificateList(t *testing.T) {
for _, table := range []struct {
description string
pem []byte
wantErr bool
wantSerial []string
}{
{
description: "invalid block type",
pem: testdata.EndEntityPrivateKey,
wantErr: true,
},
{
description: "bad block bytes: not a certificate",
pem: testdata.NotACertificate,
wantErr: true,
},
{
description: "bad block bytes: truncated certificate",
pem: testdata.TruncatedCertificate,
wantErr: true,
},
{
description: "bad block bytes: truncated certificate in list",
pem: append(testdata.TruncatedCertificate, testdata.IntermediateCertificate...),
wantErr: true,
},
{
description: "bad block: unwanted white spaces",
pem: testdata.CertificateBadWhiteSpace,
wantErr: true,
},
{
description: "ok certificate list: empty",
pem: []byte{},
wantSerial: nil,
},
{
description: "ok certificate list: size 1",
pem: testdata.EndEntityCertificate,
wantSerial: []string{testdata.EndEntityCertificateSerial},
},
{
description: "ok certificate list: size 2",
pem: testdata.IntermediateChain,
wantSerial: []string{testdata.EndEntityCertificateSerial, testdata.IntermediateCertificateSerial},
},
{
description: "ok certificate list: size 3",
pem: testdata.RootChain,
wantSerial: []string{
testdata.EndEntityCertificateSerial,
testdata.IntermediateCertificateSerial,
testdata.RootCertificateSerial,
},
},
} {
list, err := NewCertificateList(table.pem)
if got, want := err != nil, table.wantErr; got != want {
t.Errorf("got error=%v but wanted %v in test %q: %v", got, want, table.description, err)
}
if err != nil {
continue
}
if got, want := len(list), len(table.wantSerial); got != want {
t.Errorf("got list of length %d but wanted %d in test %q", got, want, table.description)
continue
}
for i, certificate := range list {
if got, want := fmt.Sprintf("%v", certificate.SerialNumber), table.wantSerial[i]; got != want {
t.Errorf("Got serial number %s but wanted %s on index %d and test %q", got, want, i, table.description)
}
}
}
}
func TestNewCertPool(t *testing.T) {
for i, pem := range [][]byte{
testdata.EndEntityCertificate,
testdata.IntermediateChain,
testdata.RootChain,
} {
list, err := NewCertificateList(pem)
if err != nil {
t.Fatalf("must parse chain: %v", err)
}
pool := NewCertPool(list)
if got, want := len(pool.Subjects()), len(list); got != want {
t.Errorf("got pool of size %d but wanted %d in test %d", got, want, i)
continue
}
for j, got := range pool.Subjects() {
if want := list[j].RawSubject; !bytes.Equal(got, want) {
t.Errorf("got subject[%d]=%X but wanted %X in test %d", j, got, want, i)
}
}
}
}
func TestParseDerChain(t *testing.T) {
for _, table := range []struct {
description string
chain [][]byte
wantErr bool
}{
{
description: "invalid chain: empty",
wantErr: true,
},
{
description: "invalid chain: first certificate: byte is missing",
chain: [][]byte{
mustMakeDerList(t, testdata.IntermediateChain)[0][1:],
mustMakeDerList(t, testdata.IntermediateChain)[1],
},
wantErr: true,
},
{
description: "valid chain: size 1",
chain: mustMakeDerList(t, testdata.EndEntityCertificate),
},
{
description: "valid chain: size 2",
chain: mustMakeDerList(t, testdata.IntermediateChain),
},
{
description: "valid chain: size 3",
chain: mustMakeDerList(t, testdata.RootChain),
},
} {
cert, pool, err := ParseDerChain(table.chain)
if got, want := err != nil, table.wantErr; got != want {
t.Errorf("got error=%v but wanted %v in test %q: %v", got, want, table.description, err)
}
if err != nil {
continue
}
if got, want := cert.Raw, table.chain[0]; !bytes.Equal(got, want) {
t.Errorf("got end-entity certificate %X but wanted %X in test %q", got, want, table.description)
}
if got, want := len(pool.Subjects()), len(table.chain)-1; got != want {
t.Errorf("got %d intermediates but wanted %d in test %q", got, want, table.description)
continue
}
for _, der := range table.chain[1:] {
want := mustMakeCertificate(t, der).RawSubject
ok := false
for _, got := range pool.Subjects() {
if bytes.Equal(got, want) {
ok = true
break
}
}
if !ok {
t.Errorf("want subject %X but found no match in test %q", want, table.description)
}
}
}
}
func TestParseDerList(t *testing.T) {
for _, table := range []struct {
description string
list [][]byte
wantErr bool
}{
{
description: "invalid certificate: first certificate: byte is missing",
list: [][]byte{
mustMakeDerList(t, testdata.IntermediateChain)[0][1:],
mustMakeDerList(t, testdata.IntermediateChain)[1],
},
wantErr: true,
},
{
description: "invalid certificate: second certificate: byte is missing",
list: [][]byte{
mustMakeDerList(t, testdata.IntermediateChain)[0],
mustMakeDerList(t, testdata.IntermediateChain)[1][1:],
},
wantErr: true,
},
{
description: "valid certificate list: empty",
},
{
description: "valid certificate list: size 1",
list: mustMakeDerList(t, testdata.EndEntityCertificate),
},
{
description: "valid certificate list: size 2",
list: mustMakeDerList(t, testdata.IntermediateChain),
},
{
description: "valid certificate list: size 3",
list: mustMakeDerList(t, testdata.RootChain),
},
} {
list, err := ParseDerList(table.list)
if got, want := err != nil, table.wantErr; got != want {
t.Errorf("got error=%v but wanted %v in test %q: %v", got, want, table.description, err)
}
if err != nil {
continue
}
if got, want := len(list), len(table.list); got != want {
t.Errorf("got %d certifictes but wanted %d in test %q", got, want, table.description)
continue
}
for i, cert := range list {
if got, want := cert.Raw, table.list[i]; !bytes.Equal(got, want) {
t.Errorf("got certificate bytes %X but wanted %X in test %q", got, want, table.description)
}
}
}
}
func TestVerifyChain(t *testing.T) {
for _, table := range []struct {
description string
pem []byte
wantErr bool
}{
{
description: "invalid chain: intermediate did not sign end-entity",
pem: testdata.ChainBadIntermediate,
wantErr: true,
},
{
description: "invalid chain: root did not sign intermediate",
pem: testdata.ChainBadRoot,
wantErr: true,
},
{
description: "valid chain",
pem: testdata.RootChain,
},
{
description: "valid chain 2",
pem: testdata.RootChain2,
},
} {
chain, err := NewCertificateList(table.pem)
if err != nil {
t.Fatalf("must parse chain: %v", err)
}
err = VerifyChain(chain)
if got, want := err != nil, table.wantErr; got != want {
t.Errorf("got error %v but wanted %v in test %q: %v", got, want, table.description, err)
}
}
}
// mustMakeDerList must parse a PEM-encoded list of certificates to DER
func mustMakeDerList(t *testing.T, pem []byte) [][]byte {
certs, err := NewCertificateList(pem)
if err != nil {
t.Fatalf("must parse pem-encoded certificates: %v", err)
}
list := make([][]byte, 0, len(certs))
for _, cert := range certs {
list = append(list, cert.Raw)
}
return list
}
// mustMakeCertificate must parse a DER-encoded certificate
func mustMakeCertificate(t *testing.T, der []byte) *x509.Certificate {
cert, err := x509.ParseCertificate(der)
if err != nil {
t.Fatalf("must parsse der-encoded certificate: %v", err)
}
return cert
}
|