-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathct.go
More file actions
466 lines (434 loc) · 13.2 KB
/
Copy pathct.go
File metadata and controls
466 lines (434 loc) · 13.2 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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
package certkit
import (
"crypto/sha256"
"crypto/x509"
_ "embed"
"encoding/base64"
"errors"
"fmt"
"log/slog"
"strings"
"sync"
"time"
ct "github.com/google/certificate-transparency-go"
"github.com/google/certificate-transparency-go/ctutil"
"github.com/google/certificate-transparency-go/loglist3"
cttls "github.com/google/certificate-transparency-go/tls"
ctx509 "github.com/google/certificate-transparency-go/x509"
)
//go:embed internal/ct/log_list.json
var ctLogListJSON []byte
type ctLogInfo struct {
Description string
KeyHash [sha256.Size]byte
Verifier *ct.SignatureVerifier
}
var (
ctLogsOnce sync.Once
ctLogs map[[sha256.Size]byte]ctLogInfo
errCTLogs error
errSCTEmptyData = errors.New("parsing SCT: empty data")
errSCTTrailingData = errors.New("parsing SCT: trailing data")
errCTChainEmpty = errors.New("CT chain is empty")
errCTChainNilCert = errors.New("CT chain contains nil certificate")
errCTChainParsedNilCert = errors.New("parsing CT chain certificate: nil certificate")
errCTNoUsableLogs = errors.New("CT log list has no usable logs")
)
// CheckCTInput contains parameters for Certificate Transparency verification.
type CheckCTInput struct {
// Chain is the certificate chain to verify SCTs against (leaf first).
Chain []*x509.Certificate
// TLSSCTs are serialized SCTs from the TLS handshake extension.
TLSSCTs [][]byte
// LogList overrides the embedded CT log list when provided.
LogList []byte
}
// SCTInfo describes a single SCT verification result.
type SCTInfo struct {
// Source indicates whether the SCT came from the TLS extension or the cert.
Source string `json:"source"`
// LogID is the base64-encoded SHA-256 hash of the SCT log key.
LogID string `json:"log_id,omitempty"`
// LogName is the human-readable name from the CT log list.
LogName string `json:"log_name,omitempty"`
// Timestamp is the SCT timestamp in RFC 3339 format.
Timestamp string `json:"timestamp,omitempty"`
// Status indicates this SCT outcome: valid, invalid, unknown-log, or unavailable.
Status string `json:"status"`
// Detail contains optional context about the SCT outcome.
Detail string `json:"detail,omitempty"`
}
// CTResult summarizes Certificate Transparency verification outcomes.
type CTResult struct {
// Status is the aggregate result: ok, missing, unavailable, invalid, unknown-log, or mixed.
Status string `json:"status"`
// Total is the number of SCT entries processed.
Total int `json:"total"`
// Valid is the number of SCTs successfully verified.
Valid int `json:"valid"`
// Invalid is the number of malformed or unverifiable SCTs.
Invalid int `json:"invalid"`
// UnknownLog is the number of SCTs for logs not present in the CT log list.
UnknownLog int `json:"unknown_log"`
// Unavailable is the number of SCTs that could not be verified due to missing CT data.
Unavailable int `json:"unavailable"`
// SCTs are the per-SCT verification outcomes.
SCTs []SCTInfo `json:"scts,omitempty"`
}
// CheckCT verifies SCTs from TLS and embedded certificate data against known CT logs.
func CheckCT(input CheckCTInput) (*CTResult, []ChainDiagnostic) {
if len(input.Chain) == 0 && len(input.TLSSCTs) == 0 {
return nil, nil
}
var (
results []SCTInfo
candidates []sctCandidate
embeddedParseError bool
ctDiagnostics []ChainDiagnostic
)
for _, raw := range input.TLSSCTs {
sct, err := parseSCT(raw)
if err != nil {
results = append(results, SCTInfo{
Source: "tls",
Status: "invalid",
Detail: err.Error(),
})
continue
}
candidates = append(candidates, sctCandidate{source: "tls", embedded: false, sct: sct})
}
if len(input.Chain) > 0 {
embeddedRaw, err := embeddedSCTBytes(input.Chain[0])
if err != nil {
embeddedParseError = true
slog.Debug("embedded SCT parsing failed", "error", err)
} else {
for _, raw := range embeddedRaw {
sct, parseErr := parseSCT(raw)
if parseErr != nil {
results = append(results, SCTInfo{
Source: "embedded",
Status: "invalid",
Detail: parseErr.Error(),
})
continue
}
candidates = append(candidates, sctCandidate{source: "embedded", embedded: true, sct: sct})
}
}
}
if len(results) == 0 && len(candidates) == 0 {
if embeddedParseError {
ctDiagnostics = append(ctDiagnostics, ChainDiagnostic{
Check: "ct-unavailable",
Status: "warn",
Detail: "embedded SCT parsing failed",
})
return &CTResult{Status: "unavailable"}, ctDiagnostics
}
ctDiagnostics = append(ctDiagnostics, ChainDiagnostic{
Check: "ct-missing",
Status: "warn",
Detail: "no signed certificate timestamps (SCTs) found",
})
return &CTResult{Status: "missing"}, ctDiagnostics
}
logs, err := ctLogsFromInput(input.LogList)
if err != nil {
slog.Debug("ct log list load failed", "error", err)
ctDiagnostics = append(ctDiagnostics, ChainDiagnostic{
Check: "ct-unavailable",
Status: "warn",
Detail: "ct log list unavailable",
})
for _, candidate := range candidates {
info := newSCTInfo(candidate)
info.Status = "unavailable"
info.Detail = "CT log list unavailable"
results = append(results, info)
}
result := summarizeCTResults(results)
if embeddedParseError {
ctDiagnostics = append(ctDiagnostics, ChainDiagnostic{
Check: "ct-unavailable",
Status: "warn",
Detail: "embedded SCT parsing failed",
})
}
if result.Invalid > 0 {
ctDiagnostics = append(ctDiagnostics, ChainDiagnostic{
Check: "ct-invalid",
Status: "warn",
Detail: fmt.Sprintf("invalid SCT(s) detected (%d)", result.Invalid),
})
}
if result.UnknownLog > 0 {
ctDiagnostics = append(ctDiagnostics, ChainDiagnostic{
Check: "ct-unknown-log",
Status: "warn",
Detail: fmt.Sprintf("SCT(s) signed by unknown log(s) (%d)", result.UnknownLog),
})
}
return result, ctDiagnostics
}
ctChain, chainErr := ctChainFromCertificates(input.Chain)
if chainErr != nil {
slog.Debug("ct chain conversion failed", "error", chainErr)
ctDiagnostics = append(ctDiagnostics, ChainDiagnostic{
Check: "ct-unavailable",
Status: "warn",
Detail: "certificate chain unavailable for ct verification",
})
}
for _, candidate := range candidates {
info := newSCTInfo(candidate)
log, ok := logs[candidate.sct.LogID.KeyID]
if ok {
info.LogName = log.Description
}
switch {
case chainErr != nil:
info.Status = "unavailable"
info.Detail = "CT verification unavailable: certificate chain conversion failed"
case !ok:
info.Status = "unknown-log"
info.Detail = "log not in CT log list"
default:
verifyErr := ctutil.VerifySCTWithVerifier(log.Verifier, ctChain, &candidate.sct, candidate.embedded)
if verifyErr != nil {
info.Status = "invalid"
info.Detail = fmt.Sprintf("SCT verification failed: %s", verifyErr)
} else {
info.Status = "valid"
}
}
results = append(results, info)
}
result := summarizeCTResults(results)
if embeddedParseError {
ctDiagnostics = append(ctDiagnostics, ChainDiagnostic{
Check: "ct-unavailable",
Status: "warn",
Detail: "embedded SCT parsing failed",
})
}
if result.Invalid > 0 {
ctDiagnostics = append(ctDiagnostics, ChainDiagnostic{
Check: "ct-invalid",
Status: "warn",
Detail: fmt.Sprintf("invalid SCT(s) detected (%d)", result.Invalid),
})
}
if result.UnknownLog > 0 {
ctDiagnostics = append(ctDiagnostics, ChainDiagnostic{
Check: "ct-unknown-log",
Status: "warn",
Detail: fmt.Sprintf("SCT(s) signed by unknown log(s) (%d)", result.UnknownLog),
})
}
return result, ctDiagnostics
}
// FormatCTLine formats a one-line Certificate Transparency summary.
func FormatCTLine(r *CTResult) string {
if r == nil {
return ""
}
countSummary := ctCountSummary(r)
label := "CT: "
switch r.Status {
case "missing":
return label + "missing (no SCTs)\n"
case "unavailable":
return label + "unavailable\n"
case "ok":
return fmt.Sprintf("%sok (%s)\n", label, countSummary)
case "invalid":
return fmt.Sprintf("%sinvalid (%s)\n", label, countSummary)
case "unknown-log":
return fmt.Sprintf("%sunknown log (%s)\n", label, countSummary)
case "mixed":
return fmt.Sprintf("%sissues (%s)\n", label, countSummary)
default:
return fmt.Sprintf("%s%s (%s)\n", label, r.Status, countSummary)
}
}
type sctCandidate struct {
sct ct.SignedCertificateTimestamp
source string
embedded bool
}
func parseSCT(raw []byte) (ct.SignedCertificateTimestamp, error) {
if len(raw) == 0 {
return ct.SignedCertificateTimestamp{}, errSCTEmptyData
}
var sct ct.SignedCertificateTimestamp
rest, err := cttls.Unmarshal(raw, &sct)
if err != nil {
return ct.SignedCertificateTimestamp{}, fmt.Errorf("parsing SCT: %w", err)
}
if len(rest) > 0 {
return ct.SignedCertificateTimestamp{}, errSCTTrailingData
}
return sct, nil
}
func newSCTInfo(candidate sctCandidate) SCTInfo {
return SCTInfo{
Source: candidate.source,
LogID: base64.StdEncoding.EncodeToString(candidate.sct.LogID.KeyID[:]),
Timestamp: ct.TimestampToTime(candidate.sct.Timestamp).UTC().Format(time.RFC3339),
}
}
func embeddedSCTBytes(leaf *x509.Certificate) ([][]byte, error) {
if leaf == nil {
return nil, nil
}
ctLeaf, err := ctx509.ParseCertificate(leaf.Raw)
if err != nil && ctx509.IsFatal(err) {
return nil, fmt.Errorf("parsing certificate for embedded SCTs: %w", err)
}
if err != nil {
slog.Debug("ct leaf parsing warning", "error", err)
}
if ctLeaf.SCTList.SCTList == nil {
return nil, nil
}
entries := make([][]byte, 0, len(ctLeaf.SCTList.SCTList))
for _, entry := range ctLeaf.SCTList.SCTList {
entries = append(entries, entry.Val)
}
return entries, nil
}
func ctChainFromCertificates(chain []*x509.Certificate) ([]*ctx509.Certificate, error) {
if len(chain) == 0 {
return nil, errCTChainEmpty
}
out := make([]*ctx509.Certificate, 0, len(chain))
for i, cert := range chain {
if cert == nil {
return nil, fmt.Errorf("%w at index %d", errCTChainNilCert, i)
}
ctCert, err := ctx509.ParseCertificate(cert.Raw)
if err != nil && ctx509.IsFatal(err) {
return nil, fmt.Errorf("parsing CT chain certificate %d: %w", i, err)
}
if err != nil {
slog.Debug("ct chain parsing warning", "index", i, "error", err)
}
if ctCert == nil {
return nil, fmt.Errorf("%w %d", errCTChainParsedNilCert, i)
}
out = append(out, ctCert)
}
return out, nil
}
func ctLogsFromInput(override []byte) (map[[sha256.Size]byte]ctLogInfo, error) {
if len(override) > 0 {
return ctLogMapFromList(override)
}
ctLogsOnce.Do(func() {
ctLogs, errCTLogs = ctLogMapFromList(ctLogListJSON)
})
return ctLogs, errCTLogs
}
func ctLogMapFromList(data []byte) (map[[sha256.Size]byte]ctLogInfo, error) {
logList, err := loglist3.NewFromJSON(data)
if err != nil {
return nil, fmt.Errorf("parsing CT log list: %w", err)
}
logs := make(map[[sha256.Size]byte]ctLogInfo)
for _, operator := range logList.Operators {
for _, log := range operator.Logs {
if log.Type == "test" {
continue
}
info, infoErr := ctLogInfoFromKey(log.Description, log.Key)
if infoErr != nil {
return nil, fmt.Errorf("loading CT log %q: %w", log.Description, infoErr)
}
logs[info.KeyHash] = info
}
for _, log := range operator.TiledLogs {
if log.Type == "test" {
continue
}
info, infoErr := ctLogInfoFromKey(log.Description, log.Key)
if infoErr != nil {
return nil, fmt.Errorf("loading CT log %q: %w", log.Description, infoErr)
}
logs[info.KeyHash] = info
}
}
if len(logs) == 0 {
return nil, errCTNoUsableLogs
}
return logs, nil
}
func ctLogInfoFromKey(description string, keyDER []byte) (ctLogInfo, error) {
pubKey, err := ctx509.ParsePKIXPublicKey(keyDER)
if err != nil {
return ctLogInfo{}, fmt.Errorf("parsing CT log key: %w", err)
}
verifier, err := ct.NewSignatureVerifier(pubKey)
if err != nil {
return ctLogInfo{}, fmt.Errorf("building CT log verifier: %w", err)
}
return ctLogInfo{
Description: description,
KeyHash: sha256.Sum256(keyDER),
Verifier: verifier,
}, nil
}
func summarizeCTResults(results []SCTInfo) *CTResult {
result := &CTResult{SCTs: results}
for _, sct := range results {
switch sct.Status {
case "valid":
result.Valid++
case "invalid":
result.Invalid++
case "unknown-log":
result.UnknownLog++
case "unavailable":
result.Unavailable++
}
}
result.Total = len(results)
result.Status = ctStatus(result)
return result
}
func ctStatus(result *CTResult) string {
switch {
case result.Total == 0:
return "missing"
case result.Invalid > 0 && result.UnknownLog > 0:
return "mixed"
case result.Invalid > 0:
return "invalid"
case result.UnknownLog > 0:
return "unknown-log"
case result.Unavailable > 0:
return "unavailable"
default:
return "ok"
}
}
func ctCountSummary(result *CTResult) string {
var parts []string
if result.Valid > 0 {
parts = append(parts, fmt.Sprintf("%d valid", result.Valid))
}
if result.Invalid > 0 {
parts = append(parts, fmt.Sprintf("%d invalid", result.Invalid))
}
if result.UnknownLog > 0 {
parts = append(parts, fmt.Sprintf("%d unknown log", result.UnknownLog))
}
if result.Unavailable > 0 {
parts = append(parts, fmt.Sprintf("%d unavailable", result.Unavailable))
}
if len(parts) == 0 {
return fmt.Sprintf("%d total", result.Total)
}
return strings.Join(parts, ", ")
}