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
7 changes: 5 additions & 2 deletions note/note_cosigv1.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,8 +375,7 @@ func CosignatureTimestamp(s note.Signature) (uint64, error) {
if err != nil {
return 0, errMalformedSig
}
const minSigSize = 64 // min(ed25519.SignatureSize, mldsa.MLDSA44SignatureSize)
if len(r) < keyHashSize+timestampSize+minSigSize {
if l := len(r); l != keyHashSize+timestampSize+ed25519.SignatureSize && l != keyHashSize+timestampSize+mldsa.MLDSA44SignatureSize {
return 0, errVerifierAlg
}
r = r[keyHashSize:] // Skip the hash
Expand Down Expand Up @@ -466,6 +465,10 @@ func formatEd25519CosignatureV1(t uint64, msg []byte) ([]byte, error) {
//
// See https://c2sp.org/tlog-cosignature for more details.

// SPEC: MUST NOT exceed 2^63-1
if t > math.MaxInt64 {
return nil, errInvalidTimestamp
}
if bytes.IndexByte(msg, '\r') >= 0 {
return nil, errors.New("cosigned note contains carriage return")
}
Expand Down
60 changes: 56 additions & 4 deletions note/note_cosigv1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"crypto/rand"
"crypto/sha256"
"encoding/base64"
"encoding/binary"
"math"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -145,14 +147,56 @@ func TestVerifierInvalidSig(t *testing.T) {
if err != nil {
t.Fatal(err)
}
v := s.Verifier()

msg := "test\n123\nf+7CoKgXKE/tNys9TTXcr/ad6U/K3xvznmzew9y6SP0=\n"
if _, err := note.Sign(&note.Note{Text: msg}, s); err != nil {
msg := []byte("test\n123\nf+7CoKgXKE/tNys9TTXcr/ad6U/K3xvznmzew9y6SP0=\n")
validSig, err := s.Sign(msg)
if err != nil {
t.Fatal(err)
}

if _, err := note.Open([]byte("nobbled"), note.VerifierList(s.Verifier())); err == nil {
t.Fatal("Verifier validated incorrect signature")
overMaxTsSig := append([]byte(nil), validSig...)
binary.BigEndian.PutUint64(overMaxTsSig, math.MaxInt64+1)

corruptSig := append([]byte(nil), validSig...)
corruptSig[len(corruptSig)-1] ^= 1

for _, test := range []struct {
name string
msg []byte
sig []byte
}{
{
name: "short signature",
msg: msg,
sig: validSig[:len(validSig)-1],
},
{
name: "long signature",
msg: msg,
sig: append(append([]byte(nil), validSig...), 0),
},
{
name: "corrupted signature bytes",
msg: msg,
sig: corruptSig,
},
{
name: "timestamp exceeds 2^63-1",
msg: msg,
sig: overMaxTsSig,
},
{
name: "malformed note body (too few lines)",
msg: []byte("nobbled"),
sig: validSig,
},
} {
t.Run(test.name, func(t *testing.T) {
if v.Verify(test.msg, test.sig) {
t.Errorf("Verify() = true, want false")
}
})
}
}

Expand Down Expand Up @@ -244,6 +288,10 @@ func TestCoSigV1Timestamp(t *testing.T) {
name: "gibberish",
sig: note.Signature{Base64: "5%/$!\n 2"},
wantErr: true,
}, {
name: "non-cosignature length (80 bytes)",
sig: note.Signature{Base64: base64.StdEncoding.EncodeToString(make([]byte, 80))},
wantErr: true,
},
} {
t.Run(test.name, func(t *testing.T) {
Expand Down Expand Up @@ -279,6 +327,10 @@ func TestCosignatureTimestamp(t *testing.T) {
name: "gibberish",
sig: note.Signature{Base64: "5%/$!\n 2"},
wantErr: true,
}, {
name: "non-cosignature length (80 bytes)",
sig: note.Signature{Base64: base64.StdEncoding.EncodeToString(make([]byte, 80))},
wantErr: true,
},
} {
t.Run(test.name, func(t *testing.T) {
Expand Down
50 changes: 25 additions & 25 deletions note/note_rfc6962.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,28 @@ import (
"encoding/json"
"errors"
"fmt"
"math"
"strconv"
"strings"
"time"

"golang.org/x/crypto/cryptobyte"
"golang.org/x/mod/sumdb/note"
)

// RFC6962VerifierString creates a note style verifier string for use with NewRFC6962Verifier below.
// logURL is the root URL of the log.
// pubK is the public key of the log.
func RFC6962VerifierString(logURL string, pubK crypto.PublicKey) (string, error) {
if !isValidName(logURL) {
name := rfc6962LogName(logURL)
if !isValidName(name) {
return "", errors.New("invalid name")
}
pubSer, err := x509.MarshalPKIXPublicKey(pubK)
if err != nil {
return "", err
}
logID := sha256.Sum256(pubSer)
name := rfc6962LogName(logURL)
hash := rfc6962Keyhash(name, logID)
return fmt.Sprintf("%s+%08x+%s", name, hash, base64.StdEncoding.EncodeToString(append([]byte{algRFC6962STH}, pubSer...))), nil
}
Expand Down Expand Up @@ -131,12 +133,19 @@ func RFC6962STHTimestamp(s note.Signature) (time.Time, error) {
if err != nil {
return time.UnixMilli(0), errMalformedSig
}
if len(r) <= keyHashSize+timestampSize {
var t uint64
b := cryptobyte.String(r)
// Skip the 4-byte key hash, read the 8-byte millisecond timestamp, and ensure
// the 4-byte TLS DigitallySigned header (hashAlg, sigAlg, uint16 sigLen) plus
// at least 1 signature byte follow.
if !b.Skip(keyHashSize) || !b.ReadUint64(&t) ||
!b.Skip(1+1+2) || b.Empty() {
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
if t > math.MaxInt64 {
return time.UnixMilli(0), errInvalidTimestamp
}
return time.UnixMilli(int64(t)), nil
}

func rfc6962Keyhash(name string, logID [32]byte) uint32 {
Expand Down Expand Up @@ -184,24 +193,15 @@ func (v *rfc6962Verifer) Verify(msg, sig []byte) bool {

func verifyRFC6962(key crypto.PublicKey) func([]byte, string, []byte) bool {
return func(msg []byte, origin string, sig []byte) bool {
if len(sig) < timestampSize {
return false
}
t := binary.BigEndian.Uint64(sig)
// slice off timestamp bytes
sig = sig[timestampSize:]

hAlg := sig[0]
sAlg := sig[1]
// slice off the hAlg and sAlg bytes read above
sig = sig[2:]

// Figure out sig bytes length
sigLen := binary.BigEndian.Uint16(sig)
sig = sig[2:] // Slice off length bytes

// All that remains should be the signature bytes themselves, and nothing more.
if len(sig) != int(sigLen) {
// Per https://c2sp.org/static-ct-api, sig is an RFC6962NoteSignature:
// uint64 timestamp followed by a TLS 1.2 DigitallySigned struct (RFC 5246 §4.7).
var t uint64
var hAlg, sAlg uint8
var rawSig cryptobyte.String
s := cryptobyte.String(sig)
if !s.ReadUint64(&t) || t > math.MaxInt64 ||
!s.ReadUint8(&hAlg) || !s.ReadUint8(&sAlg) ||
!s.ReadUint16LengthPrefixed(&rawSig) || !s.Empty() {
return false
}

Expand All @@ -224,7 +224,7 @@ func verifyRFC6962(key crypto.PublicKey) func([]byte, string, []byte) bool {
if sAlg != 0x03 {
return false
}
return ecdsa.VerifyASN1(k, dgst[:], sig)
return ecdsa.VerifyASN1(k, dgst[:], rawSig)
default:
return false
}
Expand Down
Loading
Loading