Skip to content

Commit

Permalink
Add time extraction
Browse files Browse the repository at this point in the history
  • Loading branch information
AlCutter committed Apr 11, 2024
1 parent 0f11a0c commit dc192e9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
15 changes: 15 additions & 0 deletions note/note_rfc6962.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"fmt"
"strconv"
"strings"
"time"

ct "github.com/google/certificate-transparency-go"
"golang.org/x/mod/sumdb/note"
Expand Down Expand Up @@ -112,6 +113,20 @@ func RFC6962STHToCheckpoint(j []byte, v note.Verifier) ([]byte, error) {
return n, nil
}

// RFC6962STHTimestamp extracts the embedded timestamp from a translated RFC6962 STH signature.
func RFC6962STHTimestamp(s note.Signature) (time.Time, error) {
r, err := base64.StdEncoding.DecodeString(s.Base64)
if err != nil {
return time.UnixMilli(0), errMalformedSig
}
if len(r) <= keyHashSize+timestampSize {
return time.UnixMilli(0), errVerifierAlg
}
r = r[keyHashSize:] // Skip the hash
// Next 8 bytes are the timestamp as Unix millis-since-epoch:
return time.Unix(0, int64(binary.BigEndian.Uint64(r)*1000)), nil
}

func rfc6962Keyhash(name string, logID [32]byte) uint32 {
h := sha256.New()
h.Write([]byte(name))
Expand Down
11 changes: 10 additions & 1 deletion note/note_rfc6962_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"strconv"
"strings"
"testing"
"time"

"golang.org/x/mod/sumdb/note"
)
Expand Down Expand Up @@ -110,7 +111,6 @@ func TestVerify(t *testing.T) {
if gotErr := err != nil; gotErr != test.wantErr {
t.Fatalf("Got err %q, want err %t", err, test.wantErr)
}

t.Logf("%v", n)
})
}
Expand Down Expand Up @@ -172,6 +172,15 @@ func TestRFC6962STHToCheckpoint(t *testing.T) {
if got, want := lines[2], base64.StdEncoding.EncodeToString(sth.SHA256RootHash); got != want {
t.Errorf("Got roothash %q, want %q", got, want)
}

ts, err := RFC6962STHTimestamp(n.Sigs[0])
if err != nil {
t.Fatalf("RFC6962STHTimestamp: %v", err)
}
if got, want := ts, time.Unix(0, int64(sth.Timestamp*1000)); got != want {
t.Fatalf("Got %v, want %v", got, want)
}

})
}
}
Expand Down

0 comments on commit dc192e9

Please sign in to comment.