From 3d3a15ecad1f0e27e3654b6c58a6136af16697e2 Mon Sep 17 00:00:00 2001 From: AI 13rac1 Date: Sun, 15 Nov 2026 17:47:33 +0000 Subject: [PATCH 1/3] Test the CT check with a certificate whose SCTs come from a tiled log only The certificate that rekor.sigstore.dev serves since 2026-11-14 carries only SCTs from Static CT API (tiled) logs, and the CT check refused it until tiled logs were looked up. This adds a regression test for that case: a log list with one tiled log only and an SCT that the log signed, with the leaf_index extension that a tiled log adds. The SCT passes. An SCT with the tiled log's ID but another key's signature fails on the signature, not on the lookup, and an SCT of a log that is not listed fails. To verify: go test ./internal/tlsct -run TestCheckTLSState_TiledLogSCT. Co-Authored-By: Claude Opus 5.5 --- internal/tlsct/checker_test.go | 88 ++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) diff --git a/internal/tlsct/checker_test.go b/internal/tlsct/checker_test.go index c4fe3971..fe4915d5 100644 --- a/internal/tlsct/checker_test.go +++ b/internal/tlsct/checker_test.go @@ -375,6 +375,94 @@ func TestCheckTLSState_SCTLogNotFound(t *testing.T) { t.Logf("SCT not found error: %v", err) } +// An SCT from a Static CT API (tiled) log verifies with the tiled log's key: +// a certificate whose SCTs all come from tiled logs passes. An SCT that the +// tiled log did not sign, or one from a log that is not listed, fails. +func TestCheckTLSState_TiledLogSCT(t *testing.T) { + logKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) + if err != nil { + t.Fatalf("generate log key: %v", err) + } + pubDER, err := x509.MarshalPKIXPublicKey(&logKey.PublicKey) + if err != nil { + t.Fatalf("marshal log key: %v", err) + } + logID := sha256.Sum256(pubDER) + tiledOnly := &loglist3.LogList{Operators: []*loglist3.Operator{{ + Name: "test operator", + TiledLogs: []*loglist3.TiledLog{{Description: "test tiled log", LogID: logID[:], Key: pubDER}}, + }}} + cert := selfSignedCert(t) + + check := func(ll *loglist3.LogList, sct []byte) error { + c := tlsct.NewChecker() + c.InjectLogList(ll) + return c.CheckTLSState(context.Background(), "example.com", &tls.ConnectionState{ + PeerCertificates: []*x509.Certificate{cert}, + SignedCertificateTimestamps: [][]byte{sct}, + }) + } + if err := check(tiledOnly, signedTiledHandshakeSCT(t, logKey, logID, cert)); err != nil { + t.Errorf("SCT of a tiled log: %v", err) + } + + otherKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) + if err != nil { + t.Fatalf("generate other key: %v", err) + } + // The log is found, so the failure must come from the signature check. + err = check(tiledOnly, signedTiledHandshakeSCT(t, otherKey, logID, cert)) + if err == nil { + t.Error("SCT with the tiled log's ID but another key passed") + } else if !strings.Contains(err.Error(), "no valid SCTs") || strings.Contains(err.Error(), "not found in trusted log list") { + t.Errorf("SCT with another key: %v, want a signature failure of a found log", err) + } + if err := check(emptyLogList(t), signedTiledHandshakeSCT(t, logKey, logID, cert)); err == nil || + !strings.Contains(err.Error(), "SCT log ID not found in trusted log list") { + t.Errorf("SCT of an unlisted log: %v", err) + } +} + +// signedTiledHandshakeSCT returns a TLS-marshalled SCT for cert, signed by key +// under logID, with the leaf_index extension that a Static CT API log adds. +func signedTiledHandshakeSCT(t *testing.T, key *ecdsa.PrivateKey, logID [32]byte, cert *x509.Certificate) []byte { + t.Helper() + sct := ct.SignedCertificateTimestamp{ + SCTVersion: ct.V1, + LogID: ct.LogID{KeyID: logID}, + Timestamp: uint64(time.Now().UnixMilli()), + Extensions: ct.CTExtensions{0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0x2a}, + } + leaf := ct.MerkleTreeLeaf{ + Version: ct.V1, + LeafType: ct.TimestampedEntryLeafType, + TimestampedEntry: &ct.TimestampedEntry{ + Timestamp: sct.Timestamp, + EntryType: ct.X509LogEntryType, + X509Entry: &ct.ASN1Cert{Data: cert.Raw}, + Extensions: sct.Extensions, + }, + } + input, err := ct.SerializeSCTSignatureInput(sct, ct.LogEntry{Leaf: leaf}) + if err != nil { + t.Fatalf("serialize SCT signature input: %v", err) + } + digest := sha256.Sum256(input) + sig, err := ecdsa.SignASN1(rand.Reader, key, digest[:]) + if err != nil { + t.Fatalf("sign SCT: %v", err) + } + sct.Signature = ct.DigitallySigned{ + Algorithm: cttls.SignatureAndHashAlgorithm{Hash: cttls.SHA256, Signature: cttls.ECDSA}, + Signature: sig, + } + raw, err := cttls.Marshal(sct) + if err != nil { + t.Fatalf("marshal SCT: %v", err) + } + return raw +} + // fabricateHandshakeSCT creates a TLS-marshalled SCT with a random LogID. func fabricateHandshakeSCT(t *testing.T) []byte { t.Helper() From 146913dc625b2377ecf92f91896ea6b09d6019a9 Mon Sep 17 00:00:00 2001 From: AI 13rac1 Date: Sun, 15 Nov 2026 17:58:57 +0000 Subject: [PATCH 2/3] Accept SCTs only from CT logs in a trusted state The CT check loads Google's list of all known CT logs and accepted an SCT from any listed log, whatever its state: test logs without a state, pending logs and rejected logs counted the same as usable logs, for RFC 6962 logs and tiled logs alike. With a mis-issued certificate and one SCT from a log that nobody monitors for real certificates, a connection passed the CT check. An SCT now counts only when its log is usable or qualified, or read-only or retired with the SCT issued before that state began, and, when the log has a temporal interval, the certificate expires inside it. The error for a log ID outside the list now names the CT log list, not a trusted log list. The test log lists now give their logs a usable state. The transport reference states the rule. The two tiled logs whose SCTs rekor.sigstore.dev carries since 2026-11-14 are usable in Google's list of that day, and the certificate expires inside both of their intervals. To verify: go test ./internal/tlsct -run 'TestCheckTLSState_LogState|TestCheckTLSState_TiledLogSCT|TestCheckTLSState_LogCollections'. Co-Authored-By: Claude Opus 5.5 --- docs/transport/README.md | 6 +- docs/transport/testing.md | 1 + internal/tlsct/checker.go | 57 ++++++++++++-- internal/tlsct/checker_test.go | 124 +++++++++++++++++++++++------- internal/tlsct/tiled_logs_test.go | 5 +- 5 files changed, 154 insertions(+), 39 deletions(-) diff --git a/docs/transport/README.md b/docs/transport/README.md index f9313000..a3951685 100644 --- a/docs/transport/README.md +++ b/docs/transport/README.md @@ -45,7 +45,11 @@ CT verification obtains log public keys from Google's [all-logs list](https://www.gstatic.com/ct/log_list/v3/all_logs_list.json), cached in memory for 24 hours. SCT log IDs can identify either RFC 6962 logs or Static CT API logs (`tiled_logs`). Both use SCT signature verification; -an unknown log ID or invalid signature does not authenticate an SCT. +an unknown log ID or invalid signature does not authenticate an SCT. The list +holds every known log, so an SCT counts only when its log is `usable` or +`qualified`, or `readonly` or `retired` with the SCT issued before that state +began. A pending, rejected or stateless log does not count. When the log has +a `temporal_interval`, the certificate must expire inside it. For E2EE routes, every logical request must acquire a valid authorization that binds the backend encryption key through attestation, including requests that diff --git a/docs/transport/testing.md b/docs/transport/testing.md index d6f4e36a..79c31c97 100644 --- a/docs/transport/testing.md +++ b/docs/transport/testing.md @@ -12,6 +12,7 @@ tests. | Behavior to preserve | Representative coverage | | --- | --- | | RFC 6962 and tiled CT logs verify signed SCTs during concurrent checks; unknown log IDs, invalid keys, and invalid signatures fail closed | `TestCheckTLSState_LogCollections` in [CT log tests](../../internal/tlsct/tiled_logs_test.go) | +| An SCT counts only from a log in a trusted state, within its temporal interval, for RFC 6962 and tiled logs alike | `TestCheckTLSState_LogState` and `TestCheckTLSState_TiledLogSCT` in [CT checker tests](../../internal/tlsct/checker_test.go) | | Concurrent KDS retrieval uses TLS 1.3/HTTP2 and verifies signed evidence; retrieval failures are terminal | `TestTinfoilKDS`, `TestTinfoilKDSEmbeddedChains` in [Tinfoil KDS tests](../../internal/provider/tinfoil/kds_test.go) | | Canceled discovery and capture collection do not race | `TestCanceledDiscoveryCapture` in [capture cancellation tests](../../internal/verify/capture_cancellation_test.go) | | Standalone EHBP rejects encrypted provider error events and malformed data without exposing response content | `TestStandaloneEHBPStreamCompletion` in [EHBP completion tests](../../internal/verify/ehbp_completion_test.go) | diff --git a/internal/tlsct/checker.go b/internal/tlsct/checker.go index 03983f06..839e5860 100644 --- a/internal/tlsct/checker.go +++ b/internal/tlsct/checker.go @@ -259,12 +259,16 @@ func (c *Checker) checkTLSState( var verifyErrs []string for _, candidate := range scts { - key, found := ctLogKey(logList, candidate.sct.LogID.KeyID) + log, found := findLog(logList, candidate.sct.LogID.KeyID) if !found { - verifyErrs = append(verifyErrs, "SCT log ID not found in trusted log list") + verifyErrs = append(verifyErrs, "SCT log ID not found in the CT log list") continue } - pub, pkErr := ctx509.ParsePKIXPublicKey(key) + if err := log.acceptsSCT(time.UnixMilli(int64(candidate.sct.Timestamp)), leaf.NotAfter); err != nil { //nolint:gosec // SCT timestamps are milliseconds since 1970 and fit in int64 + verifyErrs = append(verifyErrs, err.Error()) + continue + } + pub, pkErr := ctx509.ParsePKIXPublicKey(log.key) if pkErr != nil { verifyErrs = append(verifyErrs, "parse log public key: "+pkErr.Error()) continue @@ -284,15 +288,52 @@ func (c *Checker) checkTLSState( return fmt.Errorf("no valid SCTs: %s", strings.Join(verifyErrs, "; ")) } -// ctLogKey finds the SCT verification key in either CT log collection. -func ctLogKey(list *loglist3.LogList, id [sha256.Size]byte) ([]byte, bool) { +// listedLog holds what the SCT check needs of a log in the CT log list. +type listedLog struct { + key []byte + state *loglist3.LogStates + interval *loglist3.TemporalInterval +} + +// findLog returns the log in either CT log collection whose ID is id. +func findLog(list *loglist3.LogList, id [sha256.Size]byte) (listedLog, bool) { if log := list.FindLogByKeyHash(id); log != nil { - return log.Key, true + return listedLog{key: log.Key, state: log.State, interval: log.TemporalInterval}, true } if log := list.FindTiledLogByKeyHash(id); log != nil { - return log.Key, true + return listedLog{key: log.Key, state: log.State, interval: log.TemporalInterval}, true + } + return listedLog{}, false +} + +// acceptsSCT reports, as an error, why an SCT that the log issued at sctTime +// for a certificate that expires at notAfter cannot count. The list holds +// every known log, including test, pending and rejected logs, so only a log +// in a trusted state counts: usable or qualified, or read-only or retired for +// an SCT issued before that state began. When the log has a temporal +// interval, notAfter must fall inside it. +func (l listedLog) acceptsSCT(sctTime, notAfter time.Time) error { + switch l.state.LogStatus() { + case loglist3.UsableLogStatus, loglist3.QualifiedLogStatus: + case loglist3.ReadOnlyLogStatus: + if !sctTime.Before(l.state.ReadOnly.Timestamp) { + return errors.New("SCT issued after its log became read-only") + } + case loglist3.RetiredLogStatus: + if !sctTime.Before(l.state.Retired.Timestamp) { + return errors.New("SCT issued after its log was retired") + } + case loglist3.PendingLogStatus: + return errors.New("SCT log is pending, not trusted yet") + case loglist3.RejectedLogStatus: + return errors.New("SCT log is rejected") + default: + return errors.New("SCT log has no state in the CT log list") + } + if l.interval != nil && (notAfter.Before(l.interval.StartInclusive) || !notAfter.Before(l.interval.EndExclusive)) { + return errors.New("certificate expiry is outside the temporal interval of the SCT log") } - return nil, false + return nil } func (c *Checker) addCacheEntry(key string) { diff --git a/internal/tlsct/checker_test.go b/internal/tlsct/checker_test.go index fe4915d5..b588c560 100644 --- a/internal/tlsct/checker_test.go +++ b/internal/tlsct/checker_test.go @@ -375,62 +375,130 @@ func TestCheckTLSState_SCTLogNotFound(t *testing.T) { t.Logf("SCT not found error: %v", err) } -// An SCT from a Static CT API (tiled) log verifies with the tiled log's key: -// a certificate whose SCTs all come from tiled logs passes. An SCT that the -// tiled log did not sign, or one from a log that is not listed, fails. -func TestCheckTLSState_TiledLogSCT(t *testing.T) { - logKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) +// testLog is a CT log key for the SCT tests, with its ID and public key. +type testLog struct { + key *ecdsa.PrivateKey + id [32]byte + pubDER []byte +} + +func newTestLog(t *testing.T) testLog { + t.Helper() + key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) if err != nil { t.Fatalf("generate log key: %v", err) } - pubDER, err := x509.MarshalPKIXPublicKey(&logKey.PublicKey) + pubDER, err := x509.MarshalPKIXPublicKey(&key.PublicKey) if err != nil { t.Fatalf("marshal log key: %v", err) } - logID := sha256.Sum256(pubDER) + return testLog{key: key, id: sha256.Sum256(pubDER), pubDER: pubDER} +} + +// checkSCT runs the CT check of cert with one handshake SCT against ll. +func checkSCT(t *testing.T, ll *loglist3.LogList, cert *x509.Certificate, sct []byte) error { + t.Helper() + c := tlsct.NewChecker() + c.InjectLogList(ll) + return c.CheckTLSState(context.Background(), "example.com", &tls.ConnectionState{ + PeerCertificates: []*x509.Certificate{cert}, + SignedCertificateTimestamps: [][]byte{sct}, + }) +} + +// An SCT from a Static CT API (tiled) log verifies with the tiled log's key: +// a certificate whose SCTs all come from tiled logs passes. An SCT that the +// tiled log did not sign, or one from a log that is not listed, fails. +func TestCheckTLSState_TiledLogSCT(t *testing.T) { + log := newTestLog(t) + usable := &loglist3.LogStates{Usable: &loglist3.LogState{Timestamp: time.Now().Add(-24 * time.Hour)}} tiledOnly := &loglist3.LogList{Operators: []*loglist3.Operator{{ Name: "test operator", - TiledLogs: []*loglist3.TiledLog{{Description: "test tiled log", LogID: logID[:], Key: pubDER}}, + TiledLogs: []*loglist3.TiledLog{{Description: "test tiled log", LogID: log.id[:], Key: log.pubDER, State: usable}}, }}} cert := selfSignedCert(t) + now := time.Now() - check := func(ll *loglist3.LogList, sct []byte) error { - c := tlsct.NewChecker() - c.InjectLogList(ll) - return c.CheckTLSState(context.Background(), "example.com", &tls.ConnectionState{ - PeerCertificates: []*x509.Certificate{cert}, - SignedCertificateTimestamps: [][]byte{sct}, - }) - } - if err := check(tiledOnly, signedTiledHandshakeSCT(t, logKey, logID, cert)); err != nil { + if err := checkSCT(t, tiledOnly, cert, signedTiledHandshakeSCT(t, log.key, log.id, cert, now)); err != nil { t.Errorf("SCT of a tiled log: %v", err) } - otherKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) - if err != nil { - t.Fatalf("generate other key: %v", err) - } // The log is found, so the failure must come from the signature check. - err = check(tiledOnly, signedTiledHandshakeSCT(t, otherKey, logID, cert)) + other := newTestLog(t) + err := checkSCT(t, tiledOnly, cert, signedTiledHandshakeSCT(t, other.key, log.id, cert, now)) if err == nil { t.Error("SCT with the tiled log's ID but another key passed") - } else if !strings.Contains(err.Error(), "no valid SCTs") || strings.Contains(err.Error(), "not found in trusted log list") { + } else if !strings.Contains(err.Error(), "no valid SCTs") || strings.Contains(err.Error(), "not found") { t.Errorf("SCT with another key: %v, want a signature failure of a found log", err) } - if err := check(emptyLogList(t), signedTiledHandshakeSCT(t, logKey, logID, cert)); err == nil || - !strings.Contains(err.Error(), "SCT log ID not found in trusted log list") { + if err := checkSCT(t, emptyLogList(t), cert, signedTiledHandshakeSCT(t, log.key, log.id, cert, now)); err == nil || + !strings.Contains(err.Error(), "SCT log ID not found in the CT log list") { t.Errorf("SCT of an unlisted log: %v", err) } } +// Only an SCT of a log in a trusted state counts, for RFC 6962 logs and +// tiled logs alike: usable or qualified, read-only or retired only for an +// SCT issued before that state began, and a certificate expiry inside the +// log's temporal interval. Pending, rejected and stateless logs fail. +func TestCheckTLSState_LogState(t *testing.T) { + now := time.Now() + past, future := now.Add(-time.Hour), now.Add(time.Hour) + at := func(ts time.Time) *loglist3.LogState { return &loglist3.LogState{Timestamp: ts} } + cert := selfSignedCert(t) // expires in one hour + tests := []struct { + name string + state *loglist3.LogStates + interval *loglist3.TemporalInterval + wantErr string // empty: the SCT counts + }{ + {"usable", &loglist3.LogStates{Usable: at(past)}, nil, ""}, + {"qualified", &loglist3.LogStates{Qualified: at(past)}, nil, ""}, + {"read-only after the SCT", &loglist3.LogStates{ReadOnly: &loglist3.ReadOnlyLogState{LogState: *at(future)}}, nil, ""}, + {"read-only before the SCT", &loglist3.LogStates{ReadOnly: &loglist3.ReadOnlyLogState{LogState: *at(past)}}, nil, "read-only"}, + {"retired after the SCT", &loglist3.LogStates{Retired: at(future)}, nil, ""}, + {"retired before the SCT", &loglist3.LogStates{Retired: at(past)}, nil, "retired"}, + {"pending", &loglist3.LogStates{Pending: at(past)}, nil, "pending"}, + {"rejected", &loglist3.LogStates{Rejected: at(past)}, nil, "rejected"}, + {"no state", nil, nil, "no state"}, + {"expiry inside the interval", &loglist3.LogStates{Usable: at(past)}, + &loglist3.TemporalInterval{StartInclusive: past, EndExclusive: now.Add(2 * time.Hour)}, ""}, + {"expiry after the interval", &loglist3.LogStates{Usable: at(past)}, + &loglist3.TemporalInterval{StartInclusive: past.Add(-time.Hour), EndExclusive: past}, "temporal interval"}, + {"expiry before the interval", &loglist3.LogStates{Usable: at(past)}, + &loglist3.TemporalInterval{StartInclusive: now.Add(2 * time.Hour), EndExclusive: now.Add(3 * time.Hour)}, "temporal interval"}, + } + for _, kind := range []string{"rfc6962", "tiled"} { + for _, tc := range tests { + t.Run(kind+"/"+tc.name, func(t *testing.T) { + log := newTestLog(t) + op := &loglist3.Operator{Name: "test operator"} + if kind == "tiled" { + op.TiledLogs = []*loglist3.TiledLog{{LogID: log.id[:], Key: log.pubDER, State: tc.state, TemporalInterval: tc.interval}} + } else { + op.Logs = []*loglist3.Log{{LogID: log.id[:], Key: log.pubDER, State: tc.state, TemporalInterval: tc.interval}} + } + err := checkSCT(t, &loglist3.LogList{Operators: []*loglist3.Operator{op}}, cert, signedTiledHandshakeSCT(t, log.key, log.id, cert, now)) + switch { + case tc.wantErr == "" && err != nil: + t.Errorf("SCT refused: %v", err) + case tc.wantErr != "" && (err == nil || !strings.Contains(err.Error(), tc.wantErr)): + t.Errorf("err = %v, want one that names %q", err, tc.wantErr) + } + }) + } + } +} + // signedTiledHandshakeSCT returns a TLS-marshalled SCT for cert, signed by key -// under logID, with the leaf_index extension that a Static CT API log adds. -func signedTiledHandshakeSCT(t *testing.T, key *ecdsa.PrivateKey, logID [32]byte, cert *x509.Certificate) []byte { +// under logID at ts, with the leaf_index extension that a Static CT API log +// adds. +func signedTiledHandshakeSCT(t *testing.T, key *ecdsa.PrivateKey, logID [32]byte, cert *x509.Certificate, ts time.Time) []byte { t.Helper() sct := ct.SignedCertificateTimestamp{ SCTVersion: ct.V1, LogID: ct.LogID{KeyID: logID}, - Timestamp: uint64(time.Now().UnixMilli()), + Timestamp: uint64(ts.UnixMilli()), Extensions: ct.CTExtensions{0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0x2a}, } leaf := ct.MerkleTreeLeaf{ diff --git a/internal/tlsct/tiled_logs_test.go b/internal/tlsct/tiled_logs_test.go index e656517e..0c522e93 100644 --- a/internal/tlsct/tiled_logs_test.go +++ b/internal/tlsct/tiled_logs_test.go @@ -45,11 +45,12 @@ func TestCheckTLSState_LogCollections(t *testing.T) { case "invalid key": logKey = []byte("invalid public key") } + usable := &loglist3.LogStates{Usable: &loglist3.LogState{Timestamp: time.Now().Add(-time.Hour)}} op := &loglist3.Operator{} if tiled { - op.TiledLogs = []*loglist3.TiledLog{{LogID: logID[:], Key: logKey}} + op.TiledLogs = []*loglist3.TiledLog{{LogID: logID[:], Key: logKey, State: usable}} } else { - op.Logs = []*loglist3.Log{{LogID: logID[:], Key: logKey}} + op.Logs = []*loglist3.Log{{LogID: logID[:], Key: logKey, State: usable}} } checker := tlsct.NewChecker() checker.InjectLogList(&loglist3.LogList{Operators: []*loglist3.Operator{op}}) From bcecb1acca05157c6cf4c6ac2b4fdba6aa46d2c0 Mon Sep 17 00:00:00 2001 From: AI 13rac1 Date: Sun, 15 Nov 2026 18:14:06 +0000 Subject: [PATCH 3/3] Bound SCT timestamps and refuse ambiguous CT log entries A retired or read-only log counts for an SCT issued before its state changed, and the SCT timestamp comes from the log. The holder of such a log's key could therefore date an SCT before the state change, and one SCT is enough. A log ID that the list held twice let the state of the first entry hide the state of the second. An entry with more than one state was read by its first state, and a log ID was never checked against its key, which the SCT signature does not cover. An SCT dated more than a day before the certificate's NotBefore, or more than an hour in the future, now does not count. A log ID listed more than once, a log ID that is not the hash of its key, and an entry with more than one state do not count. The tests cover these rules, and the state and interval boundaries; the test helper has a neutral name. On 2026-11-14 Google's list held no entry that these rules refuse. To verify: go test ./internal/tlsct -run 'TestCheckTLSState_SCTTime|TestCheckTLSState_DuplicateLogID|TestCheckTLSState_MalformedLogEntry|TestCheckTLSState_LogState'. Co-Authored-By: Claude Opus 5.5 --- docs/transport/README.md | 6 ++- docs/transport/testing.md | 2 +- internal/tlsct/checker.go | 92 +++++++++++++++++++++++++++----- internal/tlsct/checker_test.go | 96 +++++++++++++++++++++++++++++++--- 4 files changed, 176 insertions(+), 20 deletions(-) diff --git a/docs/transport/README.md b/docs/transport/README.md index a3951685..8057dfab 100644 --- a/docs/transport/README.md +++ b/docs/transport/README.md @@ -49,7 +49,11 @@ an unknown log ID or invalid signature does not authenticate an SCT. The list holds every known log, so an SCT counts only when its log is `usable` or `qualified`, or `readonly` or `retired` with the SCT issued before that state began. A pending, rejected or stateless log does not count. When the log has -a `temporal_interval`, the certificate must expire inside it. +a `temporal_interval`, the certificate must expire inside it. An SCT dated more +than a day before the certificate's `NotBefore`, or more than an hour in the +future, does not count, and a log ID that the list holds more than once, that is not +the hash of its key, or whose entry sets more than one state does not count. The list holds each log's current state only, so an SCT of a log +that is rejected now does not count, whenever it was issued. For E2EE routes, every logical request must acquire a valid authorization that binds the backend encryption key through attestation, including requests that diff --git a/docs/transport/testing.md b/docs/transport/testing.md index 79c31c97..00537ca8 100644 --- a/docs/transport/testing.md +++ b/docs/transport/testing.md @@ -12,7 +12,7 @@ tests. | Behavior to preserve | Representative coverage | | --- | --- | | RFC 6962 and tiled CT logs verify signed SCTs during concurrent checks; unknown log IDs, invalid keys, and invalid signatures fail closed | `TestCheckTLSState_LogCollections` in [CT log tests](../../internal/tlsct/tiled_logs_test.go) | -| An SCT counts only from a log in a trusted state, within its temporal interval, for RFC 6962 and tiled logs alike | `TestCheckTLSState_LogState` and `TestCheckTLSState_TiledLogSCT` in [CT checker tests](../../internal/tlsct/checker_test.go) | +| An SCT counts only from a log in a trusted state, within its temporal interval and with a plausible timestamp, for RFC 6962 and tiled logs alike; a duplicated, mislabeled or multi-state log entry does not count | `TestCheckTLSState_LogState`, `TestCheckTLSState_SCTTime`, `TestCheckTLSState_DuplicateLogID`, `TestCheckTLSState_MalformedLogEntry` and `TestCheckTLSState_TiledLogSCT` in [CT checker tests](../../internal/tlsct/checker_test.go) | | Concurrent KDS retrieval uses TLS 1.3/HTTP2 and verifies signed evidence; retrieval failures are terminal | `TestTinfoilKDS`, `TestTinfoilKDSEmbeddedChains` in [Tinfoil KDS tests](../../internal/provider/tinfoil/kds_test.go) | | Canceled discovery and capture collection do not race | `TestCanceledDiscoveryCapture` in [capture cancellation tests](../../internal/verify/capture_cancellation_test.go) | | Standalone EHBP rejects encrypted provider error events and malformed data without exposing response content | `TestStandaloneEHBPStreamCompletion` in [EHBP completion tests](../../internal/verify/ehbp_completion_test.go) | diff --git a/internal/tlsct/checker.go b/internal/tlsct/checker.go index 839e5860..ad4a1852 100644 --- a/internal/tlsct/checker.go +++ b/internal/tlsct/checker.go @@ -3,6 +3,7 @@ package tlsct import ( "context" "crypto/sha256" + "crypto/subtle" "crypto/tls" "crypto/x509" "encoding/hex" @@ -259,16 +260,21 @@ func (c *Checker) checkTLSState( var verifyErrs []string for _, candidate := range scts { - log, found := findLog(logList, candidate.sct.LogID.KeyID) - if !found { - verifyErrs = append(verifyErrs, "SCT log ID not found in the CT log list") + listed, err := findLog(logList, candidate.sct.LogID.KeyID) + if err != nil { + verifyErrs = append(verifyErrs, err.Error()) continue } - if err := log.acceptsSCT(time.UnixMilli(int64(candidate.sct.Timestamp)), leaf.NotAfter); err != nil { //nolint:gosec // SCT timestamps are milliseconds since 1970 and fit in int64 + sctTime := time.UnixMilli(int64(candidate.sct.Timestamp)) //nolint:gosec // SCT timestamps are milliseconds since 1970 and fit in int64 + if err := checkSCTTime(sctTime, leaf.NotBefore, time.Now()); err != nil { verifyErrs = append(verifyErrs, err.Error()) continue } - pub, pkErr := ctx509.ParsePKIXPublicKey(log.key) + if err := listed.acceptsSCT(sctTime, leaf.NotAfter); err != nil { + verifyErrs = append(verifyErrs, err.Error()) + continue + } + pub, pkErr := ctx509.ParsePKIXPublicKey(listed.key) if pkErr != nil { verifyErrs = append(verifyErrs, "parse log public key: "+pkErr.Error()) continue @@ -295,15 +301,74 @@ type listedLog struct { interval *loglist3.TemporalInterval } -// findLog returns the log in either CT log collection whose ID is id. -func findLog(list *loglist3.LogList, id [sha256.Size]byte) (listedLog, bool) { - if log := list.FindLogByKeyHash(id); log != nil { - return listedLog{key: log.Key, state: log.State, interval: log.TemporalInterval}, true +// findLog returns the log in either CT log collection whose ID is id. A log +// ID that is not listed, or that is listed more than once, is an error: with +// two entries, the state and interval of one could hide those of the other. +func findLog(list *loglist3.LogList, id [sha256.Size]byte) (listedLog, error) { + var found []listedLog + for _, op := range list.Operators { + for _, log := range op.Logs { + if subtle.ConstantTimeCompare(log.LogID, id[:]) == 1 { + found = append(found, listedLog{key: log.Key, state: log.State, interval: log.TemporalInterval}) + } + } + for _, log := range op.TiledLogs { + if subtle.ConstantTimeCompare(log.LogID, id[:]) == 1 { + found = append(found, listedLog{key: log.Key, state: log.State, interval: log.TemporalInterval}) + } + } + } + // The SCT signature does not cover the log ID, so a listed key must hash + // to its ID; otherwise one key could appear under a second, trusted ID. + for _, l := range found { + keyHash := sha256.Sum256(l.key) + if subtle.ConstantTimeCompare(keyHash[:], id[:]) != 1 { + return listedLog{}, errors.New("SCT log ID does not match the hash of its key in the CT log list") + } } - if log := list.FindTiledLogByKeyHash(id); log != nil { - return listedLog{key: log.Key, state: log.State, interval: log.TemporalInterval}, true + switch len(found) { + case 0: + return listedLog{}, errors.New("SCT log ID not found in the CT log list") + case 1: + return found[0], nil + default: + return listedLog{}, errors.New("SCT log ID listed more than once in the CT log list") } - return listedLog{}, false +} + +// stateCount returns the number of states set in ls. The list should set one; +// LogStatus picks the first one set, which could hide a rejected state. +func stateCount(ls *loglist3.LogStates) int { + n := 0 + for _, set := range []bool{ls.Pending != nil, ls.Qualified != nil, ls.Usable != nil, ls.ReadOnly != nil, ls.Retired != nil, ls.Rejected != nil} { + if set { + n++ + } + } + return n +} + +// sctBeforeNotBefore and sctFutureSkew bound the SCT timestamp. A log issues +// the SCT for a certificate shortly before the CA signs it, and CAs date +// NotBefore at or before signing, so an honest SCT is not more than a day +// older than NotBefore. An SCT dated in the future is refused, with an +// allowance for clock skew. +const ( + sctBeforeNotBefore = 24 * time.Hour + sctFutureSkew = time.Hour +) + +// checkSCTTime refuses an SCT dated long before the certificate's NotBefore +// or in the future. Without it, the holder of a retired or read-only log's +// key could date an SCT before the state change. +func checkSCTTime(sctTime, notBefore, now time.Time) error { + if sctTime.Before(notBefore.Add(-sctBeforeNotBefore)) { + return errors.New("SCT timestamp is more than a day before the certificate's NotBefore") + } + if sctTime.After(now.Add(sctFutureSkew)) { + return errors.New("SCT timestamp is in the future") + } + return nil } // acceptsSCT reports, as an error, why an SCT that the log issued at sctTime @@ -313,6 +378,9 @@ func findLog(list *loglist3.LogList, id [sha256.Size]byte) (listedLog, bool) { // an SCT issued before that state began. When the log has a temporal // interval, notAfter must fall inside it. func (l listedLog) acceptsSCT(sctTime, notAfter time.Time) error { + if l.state != nil && stateCount(l.state) > 1 { + return errors.New("SCT log has more than one state in the CT log list") + } switch l.state.LogStatus() { case loglist3.UsableLogStatus, loglist3.QualifiedLogStatus: case loglist3.ReadOnlyLogStatus: diff --git a/internal/tlsct/checker_test.go b/internal/tlsct/checker_test.go index b588c560..3547edd9 100644 --- a/internal/tlsct/checker_test.go +++ b/internal/tlsct/checker_test.go @@ -419,19 +419,19 @@ func TestCheckTLSState_TiledLogSCT(t *testing.T) { cert := selfSignedCert(t) now := time.Now() - if err := checkSCT(t, tiledOnly, cert, signedTiledHandshakeSCT(t, log.key, log.id, cert, now)); err != nil { + if err := checkSCT(t, tiledOnly, cert, signedTestSCT(t, log.key, log.id, cert, now)); err != nil { t.Errorf("SCT of a tiled log: %v", err) } // The log is found, so the failure must come from the signature check. other := newTestLog(t) - err := checkSCT(t, tiledOnly, cert, signedTiledHandshakeSCT(t, other.key, log.id, cert, now)) + err := checkSCT(t, tiledOnly, cert, signedTestSCT(t, other.key, log.id, cert, now)) if err == nil { t.Error("SCT with the tiled log's ID but another key passed") } else if !strings.Contains(err.Error(), "no valid SCTs") || strings.Contains(err.Error(), "not found") { t.Errorf("SCT with another key: %v, want a signature failure of a found log", err) } - if err := checkSCT(t, emptyLogList(t), cert, signedTiledHandshakeSCT(t, log.key, log.id, cert, now)); err == nil || + if err := checkSCT(t, emptyLogList(t), cert, signedTestSCT(t, log.key, log.id, cert, now)); err == nil || !strings.Contains(err.Error(), "SCT log ID not found in the CT log list") { t.Errorf("SCT of an unlisted log: %v", err) } @@ -467,6 +467,13 @@ func TestCheckTLSState_LogState(t *testing.T) { &loglist3.TemporalInterval{StartInclusive: past.Add(-time.Hour), EndExclusive: past}, "temporal interval"}, {"expiry before the interval", &loglist3.LogStates{Usable: at(past)}, &loglist3.TemporalInterval{StartInclusive: now.Add(2 * time.Hour), EndExclusive: now.Add(3 * time.Hour)}, "temporal interval"}, + // Boundaries: the SCT carries now in milliseconds. + {"read-only at the SCT time", &loglist3.LogStates{ReadOnly: &loglist3.ReadOnlyLogState{LogState: *at(time.UnixMilli(now.UnixMilli()))}}, nil, "read-only"}, + {"retired at the SCT time", &loglist3.LogStates{Retired: at(time.UnixMilli(now.UnixMilli()))}, nil, "retired"}, + {"expiry at the interval start", &loglist3.LogStates{Usable: at(past)}, + &loglist3.TemporalInterval{StartInclusive: cert.NotAfter, EndExclusive: cert.NotAfter.Add(time.Hour)}, ""}, + {"expiry at the interval end", &loglist3.LogStates{Usable: at(past)}, + &loglist3.TemporalInterval{StartInclusive: past, EndExclusive: cert.NotAfter}, "temporal interval"}, } for _, kind := range []string{"rfc6962", "tiled"} { for _, tc := range tests { @@ -478,7 +485,7 @@ func TestCheckTLSState_LogState(t *testing.T) { } else { op.Logs = []*loglist3.Log{{LogID: log.id[:], Key: log.pubDER, State: tc.state, TemporalInterval: tc.interval}} } - err := checkSCT(t, &loglist3.LogList{Operators: []*loglist3.Operator{op}}, cert, signedTiledHandshakeSCT(t, log.key, log.id, cert, now)) + err := checkSCT(t, &loglist3.LogList{Operators: []*loglist3.Operator{op}}, cert, signedTestSCT(t, log.key, log.id, cert, now)) switch { case tc.wantErr == "" && err != nil: t.Errorf("SCT refused: %v", err) @@ -490,10 +497,10 @@ func TestCheckTLSState_LogState(t *testing.T) { } } -// signedTiledHandshakeSCT returns a TLS-marshalled SCT for cert, signed by key +// signedTestSCT returns a TLS-marshalled SCT for cert, signed by key // under logID at ts, with the leaf_index extension that a Static CT API log // adds. -func signedTiledHandshakeSCT(t *testing.T, key *ecdsa.PrivateKey, logID [32]byte, cert *x509.Certificate, ts time.Time) []byte { +func signedTestSCT(t *testing.T, key *ecdsa.PrivateKey, logID [32]byte, cert *x509.Certificate, ts time.Time) []byte { t.Helper() sct := ct.SignedCertificateTimestamp{ SCTVersion: ct.V1, @@ -843,3 +850,80 @@ func TestLiveCheckTLSState(t *testing.T) { } t.Log("second call: cache hit") } + +// An SCT dated more than a day before the certificate's NotBefore, or in the +// future, does not count, whatever the state of its log. +func TestCheckTLSState_SCTTime(t *testing.T) { + log := newTestLog(t) + ll := &loglist3.LogList{Operators: []*loglist3.Operator{{ + Name: "test operator", + Logs: []*loglist3.Log{{LogID: log.id[:], Key: log.pubDER, State: &loglist3.LogStates{ + Retired: &loglist3.LogState{Timestamp: time.Now().Add(24 * time.Hour)}, + }}}, + }}} + cert := selfSignedCert(t) // NotBefore one hour ago + tests := []struct { + name string + ts time.Time + wantErr string + }{ + {"shortly before NotBefore", cert.NotBefore.Add(-time.Hour), ""}, + {"backdated before a retirement", cert.NotBefore.Add(-48 * time.Hour), "before the certificate's NotBefore"}, + {"in the future", time.Now().Add(2 * time.Hour), "in the future"}, + } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + err := checkSCT(t, ll, cert, signedTestSCT(t, log.key, log.id, cert, tc.ts)) + switch { + case tc.wantErr == "" && err != nil: + t.Errorf("SCT refused: %v", err) + case tc.wantErr != "" && (err == nil || !strings.Contains(err.Error(), tc.wantErr)): + t.Errorf("err = %v, want one that names %q", err, tc.wantErr) + } + }) + } +} + +// A log ID that the list holds twice does not count, so the state of one +// entry cannot hide the state of the other. +func TestCheckTLSState_DuplicateLogID(t *testing.T) { + log := newTestLog(t) + usable := &loglist3.LogStates{Usable: &loglist3.LogState{Timestamp: time.Now().Add(-time.Hour)}} + rejected := &loglist3.LogStates{Rejected: &loglist3.LogState{Timestamp: time.Now().Add(-time.Hour)}} + ll := &loglist3.LogList{Operators: []*loglist3.Operator{{ + Name: "test operator", + Logs: []*loglist3.Log{{LogID: log.id[:], Key: log.pubDER, State: usable}}, + TiledLogs: []*loglist3.TiledLog{{LogID: log.id[:], Key: log.pubDER, State: rejected}}, + }}} + cert := selfSignedCert(t) + err := checkSCT(t, ll, cert, signedTestSCT(t, log.key, log.id, cert, time.Now())) + if err == nil || !strings.Contains(err.Error(), "listed more than once") { + t.Errorf("err = %v, want a refusal of the duplicate log ID", err) + } +} + +// A log entry whose ID is not the hash of its key, or that sets more than one +// state, does not count. +func TestCheckTLSState_MalformedLogEntry(t *testing.T) { + log := newTestLog(t) + other := newTestLog(t) + cert := selfSignedCert(t) + usable := &loglist3.LogState{Timestamp: time.Now().Add(-time.Hour)} + tests := []struct { + name string + entry *loglist3.Log + wantErr string + }{ + {"ID of another key", &loglist3.Log{LogID: log.id[:], Key: other.pubDER, State: &loglist3.LogStates{Usable: usable}}, "does not match the hash of its key"}, + {"usable and rejected", &loglist3.Log{LogID: log.id[:], Key: log.pubDER, State: &loglist3.LogStates{Usable: usable, Rejected: usable}}, "more than one state"}, + } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + ll := &loglist3.LogList{Operators: []*loglist3.Operator{{Name: "test operator", Logs: []*loglist3.Log{tc.entry}}}} + err := checkSCT(t, ll, cert, signedTestSCT(t, log.key, log.id, cert, time.Now())) + if err == nil || !strings.Contains(err.Error(), tc.wantErr) { + t.Errorf("err = %v, want one that names %q", err, tc.wantErr) + } + }) + } +}