-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathct_test.go
More file actions
440 lines (410 loc) · 11.7 KB
/
Copy pathct_test.go
File metadata and controls
440 lines (410 loc) · 11.7 KB
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
package certkit
import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/sha256"
"crypto/x509"
"crypto/x509/pkix"
"encoding/asn1"
"encoding/base64"
"encoding/binary"
"encoding/json"
"testing"
"time"
ct "github.com/google/certificate-transparency-go"
cttls "github.com/google/certificate-transparency-go/tls"
ctx509 "github.com/google/certificate-transparency-go/x509"
)
func TestCheckCT_TLSSCTStatus(t *testing.T) {
// WHY: CheckCT should classify TLS-delivered SCTs based on log list state.
t.Parallel()
ca := generateTestCA(t, "CT TLS CA")
leaf := generateTestLeafCert(t, ca)
leafCert, err := x509.ParseCertificate(leaf.DER)
if err != nil {
t.Fatalf("parse leaf cert: %v", err)
}
logKey, logKeyDER := buildTestLogKey(t)
stamp := uint64(time.Date(2026, 3, 1, 0, 0, 0, 0, time.UTC).UnixMilli())
sctBytes := buildTestSCT(t, buildTestSCTInput{
LogKey: logKey,
Chain: []*x509.Certificate{leafCert, ca.Cert},
Timestamp: stamp,
})
validLogList := buildTestLogList(t, "Test Log", logKeyDER)
_, otherKeyDER := buildTestLogKey(t)
unknownLogList := buildTestLogList(t, "Other Log", otherKeyDER)
chain := []*x509.Certificate{leafCert, ca.Cert}
type countExpect struct {
valid int
invalid int
unknown int
unavailable int
total int
}
tests := []struct {
name string
input CheckCTInput
wantStatus string
wantCounts *countExpect
wantSCTStatus string
wantDiag string
}{
{
name: "missing",
input: CheckCTInput{
Chain: chain,
},
wantStatus: "missing",
wantCounts: &countExpect{total: 0},
wantDiag: "ct-missing",
},
{
name: "valid log",
input: CheckCTInput{
Chain: chain,
TLSSCTs: [][]byte{sctBytes},
LogList: validLogList,
},
wantStatus: "ok",
wantCounts: &countExpect{valid: 1, total: 1},
wantSCTStatus: "valid",
},
{
name: "unknown log",
input: CheckCTInput{
Chain: chain,
TLSSCTs: [][]byte{sctBytes},
LogList: unknownLogList,
},
wantStatus: "unknown-log",
wantCounts: &countExpect{unknown: 1, total: 1},
wantSCTStatus: "unknown-log",
wantDiag: "ct-unknown-log",
},
{
name: "log list unavailable",
input: CheckCTInput{
Chain: chain,
TLSSCTs: [][]byte{sctBytes},
LogList: []byte("not-json"),
},
wantStatus: "unavailable",
wantCounts: &countExpect{unavailable: 1, total: 1},
wantSCTStatus: "unavailable",
wantDiag: "ct-unavailable",
},
{
name: "chain conversion unavailable",
input: CheckCTInput{
Chain: []*x509.Certificate{nil, ca.Cert},
TLSSCTs: [][]byte{sctBytes},
LogList: validLogList,
},
wantStatus: "unavailable",
wantCounts: &countExpect{unavailable: 1, total: 1},
wantSCTStatus: "unavailable",
wantDiag: "ct-unavailable",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// WHY: Each case verifies the expected CT status and diagnostics.
t.Parallel()
result, diags := CheckCT(tt.input)
if result == nil {
t.Fatal("expected CT result")
}
if result.Status != tt.wantStatus {
t.Fatalf("status=%q, want %q", result.Status, tt.wantStatus)
}
if tt.wantCounts != nil {
if result.Valid != tt.wantCounts.valid {
t.Fatalf("valid=%d, want %d", result.Valid, tt.wantCounts.valid)
}
if result.Invalid != tt.wantCounts.invalid {
t.Fatalf("invalid=%d, want %d", result.Invalid, tt.wantCounts.invalid)
}
if result.UnknownLog != tt.wantCounts.unknown {
t.Fatalf("unknown=%d, want %d", result.UnknownLog, tt.wantCounts.unknown)
}
if result.Unavailable != tt.wantCounts.unavailable {
t.Fatalf("unavailable=%d, want %d", result.Unavailable, tt.wantCounts.unavailable)
}
if result.Total != tt.wantCounts.total {
t.Fatalf("total=%d, want %d", result.Total, tt.wantCounts.total)
}
}
if tt.wantSCTStatus != "" {
if len(result.SCTs) != 1 {
t.Fatalf("SCTs=%d, want 1", len(result.SCTs))
}
if result.SCTs[0].Source != "tls" {
t.Fatalf("SCT source=%q, want tls", result.SCTs[0].Source)
}
if result.SCTs[0].Status != tt.wantSCTStatus {
t.Fatalf("SCT status=%q, want %q", result.SCTs[0].Status, tt.wantSCTStatus)
}
}
if tt.wantDiag != "" {
if !hasDiagnostic(diags, tt.wantDiag) {
t.Fatalf("expected %s diagnostic", tt.wantDiag)
}
} else if len(diags) != 0 {
t.Fatalf("unexpected diagnostics: %v", diags)
}
})
}
}
func TestCheckCT_EmbeddedSCTInvalid(t *testing.T) {
// WHY: CheckCT should parse embedded SCTs and surface invalid signatures.
t.Parallel()
ca := generateTestCA(t, "CT Embedded CA")
leaf := generateTestLeafCert(t, ca)
leafCert, err := x509.ParseCertificate(leaf.DER)
if err != nil {
t.Fatalf("parse leaf cert: %v", err)
}
logKey, logKeyDER := buildTestLogKey(t)
logList := buildTestLogList(t, "Embedded Log", logKeyDER)
stamp := uint64(time.Date(2026, 3, 1, 0, 0, 0, 0, time.UTC).UnixMilli())
sctBytes := buildTestSCT(t, buildTestSCTInput{
LogKey: logKey,
Chain: []*x509.Certificate{leafCert, ca.Cert},
Timestamp: stamp,
})
leafWithSCT := generateTestLeafCert(t, ca, withExtraExtensions(sctListExtension(t, sctBytes)))
leafWithSCTCert, err := x509.ParseCertificate(leafWithSCT.DER)
if err != nil {
t.Fatalf("parse leaf cert: %v", err)
}
result, diags := CheckCT(CheckCTInput{
Chain: []*x509.Certificate{leafWithSCTCert, ca.Cert},
LogList: logList,
})
if result == nil {
t.Fatal("expected CT result")
}
if result.Invalid != 1 || result.Total != 1 {
t.Fatalf("invalid=%d total=%d, want 1", result.Invalid, result.Total)
}
if len(result.SCTs) != 1 {
t.Fatalf("SCTs=%d, want 1", len(result.SCTs))
}
if result.SCTs[0].Source != "embedded" {
t.Fatalf("SCT source=%q, want embedded", result.SCTs[0].Source)
}
if !hasDiagnostic(diags, "ct-invalid") {
t.Fatal("expected ct-invalid diagnostic")
}
}
func TestFormatCTLine(t *testing.T) {
// WHY: FormatCTLine should render CT status summaries consistently.
t.Parallel()
tests := []struct {
name string
input *CTResult
want string
}{
{
name: "nil",
input: nil,
want: "",
},
{
name: "missing",
input: &CTResult{Status: "missing"},
want: "CT: missing (no SCTs)\n",
},
{
name: "unavailable",
input: &CTResult{Status: "unavailable"},
want: "CT: unavailable\n",
},
{
name: "ok",
input: &CTResult{Status: "ok", Valid: 1, Total: 1},
want: "CT: ok (1 valid)\n",
},
{
name: "invalid",
input: &CTResult{Status: "invalid", Invalid: 2, Total: 2},
want: "CT: invalid (2 invalid)\n",
},
{
name: "unknown log",
input: &CTResult{Status: "unknown-log", UnknownLog: 1, Total: 1},
want: "CT: unknown log (1 unknown log)\n",
},
{
name: "mixed",
input: &CTResult{Status: "mixed", Valid: 1, Invalid: 1, UnknownLog: 1, Total: 3},
want: "CT: issues (1 valid, 1 invalid, 1 unknown log)\n",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// WHY: Ensures FormatCTLine output matches expected text.
t.Parallel()
got := FormatCTLine(tt.input)
if got != tt.want {
t.Fatalf("FormatCTLine()=%q, want %q", got, tt.want)
}
})
}
}
func buildTestLogKey(t *testing.T) (*ecdsa.PrivateKey, []byte) {
t.Helper()
key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
t.Fatalf("generate log key: %v", err)
}
keyDER, err := x509.MarshalPKIXPublicKey(&key.PublicKey)
if err != nil {
t.Fatalf("marshal log key: %v", err)
}
return key, keyDER
}
func buildTestLogList(t *testing.T, description string, keyDER []byte) []byte {
t.Helper()
logID := sha256.Sum256(keyDER)
list := testLogList{
Version: "test",
LogListTimestamp: "2026-03-01T00:00:00Z",
Operators: []testLogOperator{
{
Name: "Test Operator",
Email: []string{"test@example.com"},
Logs: []testLog{
{
Description: description,
LogID: base64.StdEncoding.EncodeToString(logID[:]),
Key: base64.StdEncoding.EncodeToString(keyDER),
URL: "https://ct.example.test/log/",
MMD: 86400,
State: map[string]map[string]string{
"usable": {"timestamp": "2026-01-01T00:00:00Z"},
},
},
},
},
},
}
data, err := json.Marshal(list)
if err != nil {
t.Fatalf("marshal log list: %v", err)
}
return data
}
type buildTestSCTInput struct {
LogKey *ecdsa.PrivateKey
Chain []*x509.Certificate
Timestamp uint64
}
func buildTestSCT(t *testing.T, input buildTestSCTInput) []byte {
t.Helper()
ctChain := make([]*ctx509.Certificate, 0, len(input.Chain))
for i, cert := range input.Chain {
ctCert, err := ctx509.ParseCertificate(cert.Raw)
if err != nil && ctx509.IsFatal(err) {
t.Fatalf("parse ct cert %d: %v", i, err)
}
if ctCert == nil {
t.Fatalf("parse ct cert %d: nil cert", i)
}
ctChain = append(ctChain, ctCert)
}
keyDER, err := x509.MarshalPKIXPublicKey(&input.LogKey.PublicKey)
if err != nil {
t.Fatalf("marshal log key: %v", err)
}
logID := sha256.Sum256(keyDER)
sct := ct.SignedCertificateTimestamp{
SCTVersion: ct.V1,
LogID: ct.LogID{KeyID: logID},
Timestamp: input.Timestamp,
}
leaf, err := ct.MerkleTreeLeafFromChain(ctChain, ct.X509LogEntryType, input.Timestamp)
if err != nil {
t.Fatalf("build merkle leaf: %v", err)
}
signatureInput, err := ct.SerializeSCTSignatureInput(sct, ct.LogEntry{Leaf: *leaf})
if err != nil {
t.Fatalf("serialize sct input: %v", err)
}
sig, err := cttls.CreateSignature(*input.LogKey, cttls.SHA256, signatureInput)
if err != nil {
t.Fatalf("sign sct input: %v", err)
}
sct.Signature = ct.DigitallySigned(sig)
serialized, err := cttls.Marshal(sct)
if err != nil {
t.Fatalf("marshal sct: %v", err)
}
return serialized
}
func sctListExtension(t *testing.T, scts ...[]byte) pkix.Extension {
t.Helper()
var list []byte
for _, sct := range scts {
if len(sct) > 0xffff {
t.Fatalf("sct too large: %d", len(sct))
}
sctLen, err := checkedUint16Len(len(sct), "SCT length")
if err != nil {
t.Fatal(err)
}
var sctLenBytes [2]byte
binary.BigEndian.PutUint16(sctLenBytes[:], sctLen)
list = append(list, sctLenBytes[:]...)
list = append(list, sct...)
}
if len(list) > 0xffff {
t.Fatalf("sct list too large: %d", len(list))
}
listLen, err := checkedUint16Len(len(list), "SCT list length")
if err != nil {
t.Fatal(err)
}
var listLenBytes [2]byte
binary.BigEndian.PutUint16(listLenBytes[:], listLen)
sctList := append(listLenBytes[:], list...)
value, err := asn1.Marshal(sctList)
if err != nil {
t.Fatalf("marshal sct list: %v", err)
}
return pkix.Extension{Id: oidEmbeddedSCT, Value: value}
}
func withExtraExtensions(exts ...pkix.Extension) testLeafOption {
return func(tmpl *x509.Certificate) {
tmpl.ExtraExtensions = append(tmpl.ExtraExtensions, exts...)
}
}
func hasDiagnostic(diags []ChainDiagnostic, check string) bool {
for _, d := range diags {
if d.Check == check {
return true
}
}
return false
}
var oidEmbeddedSCT = asn1.ObjectIdentifier{1, 3, 6, 1, 4, 1, 11129, 2, 4, 2}
type testLogList struct {
Version string `json:"version"`
LogListTimestamp string `json:"log_list_timestamp"`
Operators []testLogOperator `json:"operators"`
}
type testLogOperator struct {
Name string `json:"name"`
Email []string `json:"email"`
Logs []testLog `json:"logs"`
}
type testLog struct {
Description string `json:"description"`
LogID string `json:"log_id"`
Key string `json:"key"`
URL string `json:"url"`
MMD int `json:"mmd"`
State map[string]map[string]string `json:"state"`
}