Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 35 additions & 27 deletions proof/tlog_proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package proof

import (
"bufio"
"bytes"
"crypto/sha256"
"encoding/base64"
Expand Down Expand Up @@ -43,7 +42,7 @@ type TLogProof struct {
func (p TLogProof) Marshal() []byte {
var proof bytes.Buffer
fmt.Fprintf(&proof, "%s\n", tlogProofHeaderV1)
if p.ExtraData != nil {
if len(p.ExtraData) > 0 {
proof.WriteString("extra ")
fmt.Fprintf(&proof, "%s\n", base64.StdEncoding.EncodeToString(p.ExtraData))
}
Expand All @@ -60,43 +59,58 @@ func (p *TLogProof) Unmarshal(data []byte) error {
if bytes.IndexByte(data, '\r') >= 0 {
return fmt.Errorf("tlog proof contains carriage return")
}
var err error
b := bufio.NewScanner(bytes.NewReader(data))

if b.Scan(); b.Text() != tlogProofHeaderV1 {
line, rem, ok := strings.Cut(string(data), "\n")
if !ok || line != tlogProofHeaderV1 {
return fmt.Errorf("tlog proof missing expected header")
}

line, rem, ok = strings.Cut(rem, "\n")
if !ok {
return fmt.Errorf("tlog proof missing required index")
}

// Handle optional extra line
var extra []byte
if b.Scan(); strings.HasPrefix(b.Text(), "extra ") {
e, _ := strings.CutPrefix(b.Text(), "extra ")
extra, err = base64.StdEncoding.Strict().DecodeString(e)
var (
extra []byte
err error
)
if extraData, hasExtra := strings.CutPrefix(line, "extra "); hasExtra {
if len(extraData) == 0 {
return fmt.Errorf("tlog proof empty extra data")
}
extra, err = base64.StdEncoding.Strict().DecodeString(extraData)
if err != nil {
return fmt.Errorf("tlog proof extra data not base64 encoded: %w", err)
}
b.Scan()
line, rem, ok = strings.Cut(rem, "\n")
if !ok {
return fmt.Errorf("tlog proof missing required index")
}
}

var idx uint64
idxStr, ok := strings.CutPrefix(b.Text(), "index ")
idxData, ok := strings.CutPrefix(line, "index ")
if !ok {
return fmt.Errorf("tlog proof missing required index")
}
if len(idxStr) > 1 && idxStr[0] == '0' {
return fmt.Errorf("tlog proof index has leading zeros: %q", idxStr)
if len(idxData) > 1 && idxData[0] == '0' {
return fmt.Errorf("tlog proof index has leading zeros: %q", idxData)
}
idx, err = strconv.ParseUint(idxStr, 10, 63)
idx, err := strconv.ParseUint(idxData, 10, 63)
if err != nil {
return fmt.Errorf("tlog proof index not a valid int64: %w", err)
}

var hashes [][sha256.Size]byte
for b.Scan() {
if b.Text() == "" {
for {
line, rem, ok = strings.Cut(rem, "\n")
if !ok {
return fmt.Errorf("tlog proof missing blank line before checkpoint")
}
if len(line) == 0 {
break
}
hash, err := base64.StdEncoding.Strict().DecodeString(b.Text())
hash, err := base64.StdEncoding.Strict().DecodeString(line)
if err != nil {
return fmt.Errorf("tlog proof hash not base64 encoded: %w", err)
}
Expand All @@ -106,19 +120,13 @@ func (p *TLogProof) Unmarshal(data []byte) error {
hashes = append(hashes, [sha256.Size]byte(hash))
}

var checkpoint bytes.Buffer
for b.Scan() {
checkpoint.Write(b.Bytes())
checkpoint.WriteByte('\n')
}

if err := b.Err(); err != nil {
return fmt.Errorf("scanning tlog proof: %w", err)
if len(rem) == 0 {
return fmt.Errorf("tlog proof missing checkpoint")
}

p.Index = idx
p.Hashes = hashes
p.Checkpoint = checkpoint.Bytes()
p.Checkpoint = []byte(rem)
p.ExtraData = extra

return nil
Expand Down
198 changes: 194 additions & 4 deletions proof/tlog_proof_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,16 @@ func TestMarshal(t *testing.T) {
},
want: "c2sp.org/tlog-proof@v1\nindex 0\n\ncheckpoint\n",
},
{
name: "proof with empty non-nil extra data omits extra line",
proof: TLogProof{
Index: 0,
Hashes: [][sha256.Size]byte{h1},
Checkpoint: []byte("checkpoint\n"),
ExtraData: []byte{},
},
want: fmt.Sprintf("c2sp.org/tlog-proof@v1\nindex 0\n%s\n\ncheckpoint\n", h1b64),
},
}

for _, tt := range tests {
Expand All @@ -82,11 +92,41 @@ func TestUnmarshalErrors(t *testing.T) {
proof []byte
wantErrSubstr string
}{
{
name: "empty input",
proof: []byte(""),
wantErrSubstr: "missing expected header",
},
{
name: "header without newline",
proof: []byte("c2sp.org/tlog-proof@v1"),
wantErrSubstr: "missing expected header",
},
{
name: "truncated after header",
proof: []byte("c2sp.org/tlog-proof@v1\n"),
wantErrSubstr: "missing required index",
},
{
name: "missing header",
proof: []byte("wrong-header\nindex 0\n\ncheckpoint\n"),
wantErrSubstr: "missing expected header",
},
{
name: "empty extra data",
proof: []byte("c2sp.org/tlog-proof@v1\nextra \nindex 0\n\ncheckpoint\n"),
wantErrSubstr: "extra data",
},
{
name: "extra line without newline",
proof: []byte("c2sp.org/tlog-proof@v1\nextra aGVsbG8="),
wantErrSubstr: "missing required index",
},
{
name: "truncated after extra line",
proof: []byte("c2sp.org/tlog-proof@v1\nextra aGVsbG8=\n"),
wantErrSubstr: "missing required index",
},
{
name: "invalid extra data encoding",
proof: []byte("c2sp.org/tlog-proof@v1\nextra !!notbase64!!\nindex 0\n\ncheckpoint\n"),
Expand All @@ -97,6 +137,11 @@ func TestUnmarshalErrors(t *testing.T) {
proof: []byte("c2sp.org/tlog-proof@v1\n\n\ncheckpoint\n"),
wantErrSubstr: "missing required index",
},
{
name: "index line without newline",
proof: []byte("c2sp.org/tlog-proof@v1\nindex 0"),
wantErrSubstr: "missing required index",
},
{
name: "invalid index - not a number",
proof: []byte("c2sp.org/tlog-proof@v1\nindex notanumber\n\ncheckpoint\n"),
Expand Down Expand Up @@ -156,9 +201,39 @@ func TestUnmarshalErrors(t *testing.T) {
wantErrSubstr: "hash not base64 encoded",
},
{
name: "scanner error - buffer too large",
proof: []byte("c2sp.org/tlog-proof@v1\nindex 0\n" + strings.Repeat("a", 65*1024) + "\n"),
wantErrSubstr: "scanning tlog proof",
name: "oversized hash line (>64 KiB)",
proof: []byte("c2sp.org/tlog-proof@v1\nindex 0\n" + strings.Repeat("a", 65*1024) + "\n\ncheckpoint\n"),
wantErrSubstr: "hash length",
},
{
name: "oversized invalid base64 hash line (>64 KiB)",
proof: []byte("c2sp.org/tlog-proof@v1\nindex 0\n" + strings.Repeat("!", 65*1024) + "\n\ncheckpoint\n"),
wantErrSubstr: "hash not base64 encoded",
},
{
name: "missing blank line separator after index",
proof: []byte("c2sp.org/tlog-proof@v1\nindex 0\n"),
wantErrSubstr: "missing blank line",
},
{
name: "missing blank line separator after hash",
proof: []byte("c2sp.org/tlog-proof@v1\nindex 0\n" + base64.StdEncoding.EncodeToString(make([]byte, 32)) + "\n"),
wantErrSubstr: "missing blank line",
},
{
name: "hash line without newline",
proof: []byte("c2sp.org/tlog-proof@v1\nindex 0\n" + base64.StdEncoding.EncodeToString(make([]byte, 32))),
wantErrSubstr: "missing blank line",
},
{
name: "empty checkpoint after blank line separator",
proof: []byte("c2sp.org/tlog-proof@v1\nindex 0\n\n"),
wantErrSubstr: "missing checkpoint",
},
{
name: "empty checkpoint after hashes and blank line separator",
proof: []byte("c2sp.org/tlog-proof@v1\nindex 0\n" + base64.StdEncoding.EncodeToString(make([]byte, 32)) + "\n\n"),
wantErrSubstr: "missing checkpoint",
},
{
name: "CRLF on header line",
Expand Down Expand Up @@ -195,7 +270,18 @@ func TestUnmarshalErrors(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var p TLogProof
sentinel := TLogProof{
Index: 42,
Hashes: [][sha256.Size]byte{sha256.Sum256([]byte("sentinel-hash"))},
Checkpoint: []byte("sentinel-checkpoint\n"),
ExtraData: []byte("sentinel-extra"),
}
p := TLogProof{
Index: sentinel.Index,
Hashes: append([][sha256.Size]byte(nil), sentinel.Hashes...),
Checkpoint: bytes.Clone(sentinel.Checkpoint),
ExtraData: bytes.Clone(sentinel.ExtraData),
}
err := p.Unmarshal(tt.proof)

if err == nil {
Expand All @@ -205,6 +291,13 @@ func TestUnmarshalErrors(t *testing.T) {
if !strings.Contains(err.Error(), tt.wantErrSubstr) {
t.Errorf("error message doesn't contain %q, got: %v", tt.wantErrSubstr, err)
}

if p.Index != sentinel.Index ||
len(p.Hashes) != len(sentinel.Hashes) || p.Hashes[0] != sentinel.Hashes[0] ||
!bytes.Equal(p.Checkpoint, sentinel.Checkpoint) ||
!bytes.Equal(p.ExtraData, sentinel.ExtraData) {
t.Errorf("Unmarshal mutated receiver on error: got %+v, want %+v", p, sentinel)
}
})
}
}
Expand All @@ -231,6 +324,15 @@ func TestRoundTrip(t *testing.T) {
ExtraData: []byte("some extra data"),
},
},
{
name: "large extra data (>64 KiB)",
proof: TLogProof{
Index: 456,
Hashes: [][sha256.Size]byte{sha256.Sum256([]byte("c"))},
Checkpoint: []byte("another checkpoint\n"),
ExtraData: bytes.Repeat([]byte("x"), 65*1024),
},
},
{
name: "empty hashes",
proof: TLogProof{
Expand All @@ -247,6 +349,34 @@ func TestRoundTrip(t *testing.T) {
Checkpoint: []byte("checkpoint\n"),
},
},
{
name: "multi-signature checkpoint with blank lines",
proof: TLogProof{
Index: 73894,
Hashes: [][sha256.Size]byte{sha256.Sum256([]byte("leaf-sibling"))},
Checkpoint: []byte("example.com/fancylog\n109482\nsFodV/vSp5O8n9a8QpW6PRY97tfOSW5bsc2Xl/EQi08=\n\n" +
"— example.com/fancylog hI2DJw1roloI=\n" +
"— witness1.example mJirIkljqY9v2B/5bg==\n"),
},
},
{
name: "checkpoint with trailing blank lines",
proof: TLogProof{
Index: 1,
Hashes: nil,
Checkpoint: []byte("example.com/fancylog\n109482\nsFodV/vSp5O8n9a8QpW6PRY97tfOSW5bsc2Xl/EQi08=\n\n" +
"— example.com/fancylog hI2DJw1roloI=\n\n\n"),
},
},
{
name: "checkpoint without trailing newline",
proof: TLogProof{
Index: 2,
Hashes: nil,
Checkpoint: []byte("example.com/fancylog\n109482\nsFodV/vSp5O8n9a8QpW6PRY97tfOSW5bsc2Xl/EQi08=\n\n" +
"— example.com/fancylog hI2DJw1roloI="),
},
},
}

for _, tt := range tests {
Expand Down Expand Up @@ -278,3 +408,63 @@ func TestRoundTrip(t *testing.T) {
})
}
}

func TestUnmarshalCheckpointDoesNotAliasInput(t *testing.T) {
raw := []byte("c2sp.org/tlog-proof@v1\nindex 0\n\nexample.com/log\n1\nhash==\n\n— example.com/log sig==\n")
p := TLogProof{
ExtraData: []byte("stale-extra"),
}
if err := p.Unmarshal(raw); err != nil {
t.Fatalf("Unmarshal failed: %v", err)
}
if p.ExtraData != nil {
t.Errorf("ExtraData = %q, want nil when extra line is omitted", p.ExtraData)
}

wantCP := bytes.Clone(p.Checkpoint)
// Mutate backing array of raw input.
for i := range raw {
raw[i] = 'X'
}
if !bytes.Equal(p.Checkpoint, wantCP) {
t.Errorf("Checkpoint aliased input buffer: got %q, want %q", p.Checkpoint, wantCP)
}
}

func BenchmarkMarshal(b *testing.B) {
p := TLogProof{
Index: 73894,
Hashes: [][sha256.Size]byte{
sha256.Sum256([]byte("h1")),
sha256.Sum256([]byte("h2")),
sha256.Sum256([]byte("h3")),
sha256.Sum256([]byte("h4")),
},
Checkpoint: []byte("example.com/fancylog\n109482\nsFodV/vSp5O8n9a8QpW6PRY97tfOSW5bsc2Xl/EQi08=\n\n— example.com/fancylog hI2DJw1roloI=\n"),
ExtraData: []byte("opaque application context"),
}
for b.Loop() {
_ = p.Marshal()
}
}

func BenchmarkUnmarshal(b *testing.B) {
raw := TLogProof{
Index: 73894,
Hashes: [][sha256.Size]byte{
sha256.Sum256([]byte("h1")),
sha256.Sum256([]byte("h2")),
sha256.Sum256([]byte("h3")),
sha256.Sum256([]byte("h4")),
},
Checkpoint: []byte("example.com/fancylog\n109482\nsFodV/vSp5O8n9a8QpW6PRY97tfOSW5bsc2Xl/EQi08=\n\n— example.com/fancylog hI2DJw1roloI=\n"),
ExtraData: []byte("opaque application context"),
}.Marshal()

var p TLogProof
for b.Loop() {
if err := p.Unmarshal(raw); err != nil {
b.Fatal(err)
}
}
}
Loading