diff --git a/docs/transport/README.md b/docs/transport/README.md index f9313000..8057dfab 100644 --- a/docs/transport/README.md +++ b/docs/transport/README.md @@ -45,7 +45,15 @@ 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. 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 d6f4e36a..00537ca8 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 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 03983f06..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,12 +260,21 @@ func (c *Checker) checkTLSState( var verifyErrs []string for _, candidate := range scts { - key, found := ctLogKey(logList, candidate.sct.LogID.KeyID) - if !found { - verifyErrs = append(verifyErrs, "SCT log ID not found in trusted log list") + listed, err := findLog(logList, candidate.sct.LogID.KeyID) + if err != nil { + verifyErrs = append(verifyErrs, err.Error()) + continue + } + 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(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 @@ -284,15 +294,114 @@ 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) { - if log := list.FindLogByKeyHash(id); log != nil { - return log.Key, true +// 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. 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") + } + } + 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") + } +} + +// 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 +// 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 { + 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: + 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 log := list.FindTiledLogByKeyHash(id); log != nil { - return log.Key, true + 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 c4fe3971..3547edd9 100644 --- a/internal/tlsct/checker_test.go +++ b/internal/tlsct/checker_test.go @@ -375,6 +375,169 @@ func TestCheckTLSState_SCTLogNotFound(t *testing.T) { t.Logf("SCT not found error: %v", err) } +// 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(&key.PublicKey) + if err != nil { + t.Fatalf("marshal log key: %v", err) + } + 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: log.id[:], Key: log.pubDER, State: usable}}, + }}} + cert := selfSignedCert(t) + now := time.Now() + + 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, 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, 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) + } +} + +// 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"}, + // 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 { + 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, signedTestSCT(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) + } + }) + } + } +} + +// 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 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, + LogID: ct.LogID{KeyID: logID}, + Timestamp: uint64(ts.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() @@ -687,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) + } + }) + } +} 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}})