| package checkpoint | |
| import ( | |
| "crypto/rand" | |
| "encoding/base64" | |
| "encoding/binary" | |
| "errors" | |
| "slices" | |
| "strings" | |
| "testing" | |
| "golang.org/x/mod/sumdb/note" | |
| "golang.org/x/mod/sumdb/tlog" | |
| ) | |
| // exampleCheckpoint is a canonical tlog-checkpoint note body the cosignature | |
| // tests sign and verify over. | |
| const exampleHashB64 = "CsUYapGGPo4dkMgIAUqom/Xajj7h2fB2MPA3j2jxq2I=" | |
| // emptyTreeHashB64 is the RFC 6962 empty-tree hash, SHA-256 of the empty | |
| // string, which is the only valid root hash for tree size 0. | |
| const emptyTreeHashB64 = "47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=" | |
| // exampleCheckpoint is a tlog-checkpoint note body, including the trailing | |
| // newline and no signature lines. | |
| const exampleCheckpoint = "example.com/behind-the-sofa\n20852163\n" + exampleHashB64 + "\n" | |
| func TestCheckpointUnmarshalRoundTrip(t *testing.T) { | |
| cases := []struct { | |
| name string | |
| text string | |
| origin string | |
| size int64 | |
| extensions []string | |
| }{ | |
| { | |
| name: "No extensions", | |
| text: "example.com/log\n20852163\n" + exampleHashB64 + "\n", | |
| origin: "example.com/log", | |
| size: 20852163, | |
| }, | |
| { | |
| name: "With extensions", | |
| text: "example.com/log\n20852163\n" + exampleHashB64 + "\nfoo extension\nbar extension\n", | |
| origin: "example.com/log", | |
| size: 20852163, | |
| extensions: []string{"foo extension", "bar extension"}, | |
| }, | |
| { | |
| name: "Zero size", | |
| text: "example.com/log\n0\n" + emptyTreeHashB64 + "\n", | |
| origin: "example.com/log", | |
| size: 0, | |
| }, | |
| } | |
| for _, tc := range cases { | |
| t.Run(tc.name, func(t *testing.T) { | |
| c, err := Unmarshal([]byte(tc.text)) | |
| if err != nil { | |
| t.Fatalf("Unmarshal: %s", err) | |
| } | |
| if c.Origin != tc.origin { | |
| t.Errorf("Origin = %q, want %q", c.Origin, tc.origin) | |
| } | |
| if c.Tree.N != tc.size { | |
| t.Errorf("Tree.N = %d, want %d", c.Tree.N, tc.size) | |
| } | |
| if !slices.Equal(c.Extensions, tc.extensions) { | |
| t.Errorf("Extensions = %v, want %v", c.Extensions, tc.extensions) | |
| } | |
| got, err := c.Marshal() | |
| if err != nil { | |
| t.Fatalf("Marshal: %s", err) | |
| } | |
| if string(got) != tc.text { | |
| t.Errorf("Marshal = %q, want %q", got, tc.text) | |
| } | |
| }) | |
| } | |
| } | |
| func TestCheckpointUnmarshalRejects(t *testing.T) { | |
| cases := []struct { | |
| name string | |
| text string | |
| }{ | |
| {"No trailing newline", "example.com/log\n1\n" + exampleHashB64}, | |
| {"Too few lines", "example.com/log\n1\n"}, | |
| {"Empty origin", "\n1\n" + exampleHashB64 + "\n"}, | |
| {"Leading zero size", "example.com/log\n01\n" + exampleHashB64 + "\n"}, | |
| {"Negative size", "example.com/log\n-1\n" + exampleHashB64 + "\n"}, | |
| {"Plus-signed size", "example.com/log\n+1\n" + exampleHashB64 + "\n"}, | |
| {"Minus-signed zero size", "example.com/log\n-0\n" + exampleHashB64 + "\n"}, | |
| {"Non-numeric size", "example.com/log\nx\n" + exampleHashB64 + "\n"}, | |
| {"Size above int64", "example.com/log\n9223372036854775808\n" + exampleHashB64 + "\n"}, | |
| {"Signed note instead of a bare body", "example.com/log\n1\n" + exampleHashB64 + "\n\nā key AAAA\n"}, | |
| {"Bad base64 hash", "example.com/log\n1\n!!!notbase64!!!\n"}, | |
| {"Non-canonical base64 hash", "example.com/log\n1\nCsUYapGGPo4dkMgIAUqom/Xajj7h2fB2MPA3j2jxq2J=\n"}, | |
| {"Carriage return in hash", "example.com/log\n1\nCsUY\rapGGPo4dkMgIAUqom/Xajj7h2fB2MPA3j2jxq2I=\n"}, | |
| {"Unpadded hash", "example.com/log\n1\nCsUYapGGPo4dkMgIAUqom/Xajj7h2fB2MPA3j2jxq2I\n"}, | |
| {"Short hash", "example.com/log\n1\nAAAA\n"}, | |
| {"Empty extension line", "example.com/log\n1\n" + exampleHashB64 + "\n\n"}, | |
| {"Carriage return in origin", "example.com/log\r\n1\n" + exampleHashB64 + "\n"}, | |
| {"Invalid UTF-8 in origin", "example.com/\xff\n1\n" + exampleHashB64 + "\n"}, | |
| {"Origin over 255 bytes", strings.Repeat("a", 256) + "\n1\n" + exampleHashB64 + "\n"}, | |
| {"Space in origin", "example.com/log 1\n1\n" + exampleHashB64 + "\n"}, | |
| {"Plus in origin", "example.com/log+1\n1\n" + exampleHashB64 + "\n"}, | |
| {"Control character in extension", "example.com/log\n1\n" + exampleHashB64 + "\next\x01ension\n"}, | |
| } | |
| for _, tc := range cases { | |
| t.Run(tc.name, func(t *testing.T) { | |
| _, err := Unmarshal([]byte(tc.text)) | |
| if err == nil { | |
| t.Error("Unmarshal = nil error, want error") | |
| } | |
| }) | |
| } | |
| } | |
| // TestCheckpointMarshal covers Marshal's validation of hand-constructed | |
| // Checkpoints, which bypass Unmarshal's checks. | |
| func TestCheckpointMarshal(t *testing.T) { | |
| valid, err := Unmarshal([]byte(exampleCheckpoint)) | |
| if err != nil { | |
| t.Fatalf("Unmarshal: %s", err) | |
| } | |
| t.Run("Valid", func(t *testing.T) { | |
| got, err := valid.Marshal() | |
| if err != nil { | |
| t.Fatalf("Marshal: %s", err) | |
| } | |
| if string(got) != exampleCheckpoint { | |
| t.Errorf("Marshal = %q, want %q", got, exampleCheckpoint) | |
| } | |
| }) | |
| cases := []struct { | |
| name string | |
| mutate func(Checkpoint) Checkpoint | |
| }{ | |
| {"Empty origin", func(c Checkpoint) Checkpoint { c.Origin = ""; return c }}, | |
| {"Newline in origin", func(c Checkpoint) Checkpoint { c.Origin = "two\nlines"; return c }}, | |
| {"Invalid UTF-8 in origin", func(c Checkpoint) Checkpoint { c.Origin = "bad\xff"; return c }}, | |
| {"Negative tree size", func(c Checkpoint) Checkpoint { c.Tree.N = -1; return c }}, | |
| {"Empty extension", func(c Checkpoint) Checkpoint { c.Extensions = []string{""}; return c }}, | |
| {"Newline in extension", func(c Checkpoint) Checkpoint { c.Extensions = []string{"two\nlines"}; return c }}, | |
| } | |
| for _, tc := range cases { | |
| t.Run(tc.name, func(t *testing.T) { | |
| mutated := tc.mutate(*valid) | |
| _, err := mutated.Marshal() | |
| if err == nil { | |
| t.Error("Marshal = nil error, want error") | |
| } | |
| }) | |
| } | |
| } | |
| // TestSignedNote checks that the signature lines follow the note text after a | |
| // blank line. | |
| func TestSignedNote(t *testing.T) { | |
| cp := &Checkpoint{Origin: "example.com/log", Tree: tlog.Tree{N: 5}} | |
| text, err := cp.Marshal() | |
| if err != nil { | |
| t.Fatalf("Marshal: %s", err) | |
| } | |
| lines := []byte("ā ca sig\nā mirror sig\n") | |
| signed, err := cp.SignedNote(lines) | |
| if err != nil { | |
| t.Fatalf("SignedNote: %s", err) | |
| } | |
| if string(signed) != string(text)+"\n"+string(lines) { | |
| t.Errorf("SignedNote = %q", signed) | |
| } | |
| } | |
| // TestOpenCheckpoint covers Open's happy path and its rejection of a note no | |
| // trusted key signed, using a generic note signer. | |
| func TestOpenCheckpoint(t *testing.T) { | |
| skey, vkey, err := note.GenerateKey(rand.Reader, "example.com/behind-the-sofa") | |
| if err != nil { | |
| t.Fatalf("GenerateKey: %s", err) | |
| } | |
| signer, err := note.NewSigner(skey) | |
| if err != nil { | |
| t.Fatalf("NewSigner: %s", err) | |
| } | |
| signed, err := note.Sign(¬e.Note{Text: exampleCheckpoint}, signer) | |
| if err != nil { | |
| t.Fatalf("note.Sign: %s", err) | |
| } | |
| verifier, err := note.NewVerifier(vkey) | |
| if err != nil { | |
| t.Fatalf("NewVerifier: %s", err) | |
| } | |
| t.Run("Valid", func(t *testing.T) { | |
| cp, n, err := Open(signed, verifier) | |
| if err != nil { | |
| t.Fatalf("Open: %s", err) | |
| } | |
| if cp.Origin != "example.com/behind-the-sofa" { | |
| t.Errorf("Origin = %q, want %q", cp.Origin, "example.com/behind-the-sofa") | |
| } | |
| if cp.Tree.N != 20852163 { | |
| t.Errorf("Tree.N = %d, want %d", cp.Tree.N, 20852163) | |
| } | |
| if len(n.Sigs) != 1 { | |
| t.Errorf("len(Sigs) = %d, want 1", len(n.Sigs)) | |
| } | |
| }) | |
| t.Run("Missing a verifier", func(t *testing.T) { | |
| _, otherVkey, err := note.GenerateKey(rand.Reader, "example.com/other") | |
| if err != nil { | |
| t.Fatalf("GenerateKey: %s", err) | |
| } | |
| otherV, err := note.NewVerifier(otherVkey) | |
| if err != nil { | |
| t.Fatalf("NewVerifier: %s", err) | |
| } | |
| _, _, err = Open(signed, verifier, otherV) | |
| if err == nil { | |
| t.Error("Open with a verifier that did not sign = nil error, want error") | |
| } | |
| }) | |
| t.Run("Extra signature from an unknown key", func(t *testing.T) { | |
| otherSkey, _, err := note.GenerateKey(rand.Reader, "example.com/other") | |
| if err != nil { | |
| t.Fatalf("GenerateKey: %s", err) | |
| } | |
| otherSigner, err := note.NewSigner(otherSkey) | |
| if err != nil { | |
| t.Fatalf("NewSigner: %s", err) | |
| } | |
| twiceSigned, err := note.Sign(¬e.Note{Text: exampleCheckpoint}, signer, otherSigner) | |
| if err != nil { | |
| t.Fatalf("note.Sign: %s", err) | |
| } | |
| _, n, err := Open(twiceSigned, verifier) | |
| if err != nil { | |
| t.Fatalf("Open with an extra signature from an unknown key: %s", err) | |
| } | |
| if len(n.Sigs) != 1 || len(n.UnverifiedSigs) != 1 { | |
| t.Errorf("Open with an extra signature from an unknown key = %d verified and %d unverified signatures, want 1 and 1", len(n.Sigs), len(n.UnverifiedSigs)) | |
| } | |
| }) | |
| t.Run("Repeated signature", func(t *testing.T) { | |
| repeated := append(slices.Clone(signed), signed[len(exampleCheckpoint)+1:]...) | |
| _, n, err := Open(repeated, verifier) | |
| if err != nil { | |
| t.Fatalf("Open with a repeated signature: %s", err) | |
| } | |
| if len(n.Sigs) != 1 { | |
| t.Errorf("Open with a repeated signature = %d verified signatures, want 1", len(n.Sigs)) | |
| } | |
| }) | |
| t.Run("Forged signature", func(t *testing.T) { | |
| // A line naming the verifier's key that another key signed is | |
| // rejected, not ignored as unknown. | |
| otherSkey, _, err := note.GenerateKey(rand.Reader, "example.com/behind-the-sofa") | |
| if err != nil { | |
| t.Fatalf("GenerateKey: %s", err) | |
| } | |
| otherSigner, err := note.NewSigner(otherSkey) | |
| if err != nil { | |
| t.Fatalf("NewSigner: %s", err) | |
| } | |
| otherSigned, err := note.Sign(¬e.Note{Text: exampleCheckpoint}, otherSigner) | |
| if err != nil { | |
| t.Fatalf("note.Sign: %s", err) | |
| } | |
| prefix := exampleCheckpoint + "\nā example.com/behind-the-sofa " | |
| body, err := base64.StdEncoding.DecodeString(strings.TrimSuffix(string(otherSigned[len(prefix):]), "\n")) | |
| if err != nil { | |
| t.Fatalf("DecodeString: %s", err) | |
| } | |
| binary.BigEndian.PutUint32(body[:4], verifier.KeyHash()) | |
| forged := []byte(prefix + base64.StdEncoding.EncodeToString(body) + "\n") | |
| _, _, err = Open(forged, verifier) | |
| _, ok := errors.AsType[*note.InvalidSignatureError](err) | |
| if !ok { | |
| t.Errorf("Open with a forged signature = %s, want note.InvalidSignatureError", err) | |
| } | |
| }) | |
| t.Run("Wrong key", func(t *testing.T) { | |
| _, otherVkey, err := note.GenerateKey(rand.Reader, "example.com/behind-the-sofa") | |
| if err != nil { | |
| t.Fatalf("GenerateKey: %s", err) | |
| } | |
| otherV, err := note.NewVerifier(otherVkey) | |
| if err != nil { | |
| t.Fatalf("NewVerifier: %s", err) | |
| } | |
| _, _, err = Open(signed, otherV) | |
| if err == nil { | |
| t.Error("Open with wrong key = nil error, want error") | |
| } | |
| }) | |
| } | |
| // TestOpenRejectsNonCheckpointBody covers the branch where the note signature | |
| // verifies but its body does not parse as a checkpoint. | |
| func TestOpenRejectsNonCheckpointBody(t *testing.T) { | |
| skey, vkey, err := note.GenerateKey(rand.Reader, "log.example") | |
| if err != nil { | |
| t.Fatalf("GenerateKey: %s", err) | |
| } | |
| signer, err := note.NewSigner(skey) | |
| if err != nil { | |
| t.Fatalf("NewSigner: %s", err) | |
| } | |
| signed, err := note.Sign(¬e.Note{Text: "not a checkpoint\n"}, signer) | |
| if err != nil { | |
| t.Fatalf("note.Sign: %s", err) | |
| } | |
| verifier, err := note.NewVerifier(vkey) | |
| if err != nil { | |
| t.Fatalf("NewVerifier: %s", err) | |
| } | |
| _, _, err = Open(signed, verifier) | |
| if err == nil { | |
| t.Error("Open of a verified non-checkpoint note = nil error, want error") | |
| } | |
| } |