One Signed Token, Two Verdicts: CBOR Type Bugs in Both SwornMail Verifiers

The Go and Rust SwornMail verifiers disagreed on validly signed tokens because their CBOR libraries coerce types. The fix, and the splits that are still open.

By · · 4 min read

SwornMail has two verifiers, one in Go and one in Rust. They were written separately on purpose, so one can catch the mistakes of the other. In November a code review found tokens that one of them accepts and the other rejects. Both are fixed on main now, and the spec got 12 new test vectors. Here is what was wrong and what I think about it.

A bit of context

A SwornMail token is a COSE_Sign1 message over a small CBOR map. The keys are integers: 1 is the operator domain, 2 is the IPv6 prefix, 3 is the unit, 4 and 5 are issued-at and expiry, 6 is the role. The draft gives each of them a type: text, bytes, unsigned integer.

Nobody uses SwornMail in production yet, so nothing was exploited. That is exactly the good time to find this kind of things.

What Go accepted

The Go verifier decodes CBOR with fxamacker/cbor, and the library is a bit too helpful with types.

  • 3: null (or undefined) was read as “no unit given”, so the token got the default unit 64.
  • 3: simple(64) was read as the integer 64.
  • 4: null was read as issued-at 0.
  • An array of 17 small integers was read as the 17-byte prefix.
  • A self-described tag (55799) around the payload, around a key or around a value was dropped silently. So 55799(1) worked as the operator key.

Go was also too strict in one place. An unknown key of 2^63 or bigger was rejected as malformed, because the code narrowed keys to a machine int (2^31 on a 32-bit build). The draft says unknown integer keys are ignored, and in CDDL int means every CBOR integer.

What Rust accepted

The Rust verifier uses ciborium, and its Value type folds a bignum into a plain integer. So this payload passed:

{2(h'01'): "evil.example", 2: ..., 4: ..., 5: ..., 6: "mta"}

There is no integer key 1 in it. The operator was taken from a bignum that encodes 1. The same trick worked for COSE header labels (2(h'04') as the kid label) and for alg, where 3(h'07') is the bignum spelling of -8.

Why this matters

To be honest, none of this is a forgery. The payload and the protected header are signed, so changing them needs the operator’s own key. The unprotected header isn’t signed: whoever holds a valid token can change it and the signature still verifies. But that can’t change what the token claims.

The problem is the split verdict. A receiver running the Go verifier says pass, a receiver running the Rust one says permerror, for the same bytes. SwornMail is about reputation. A sender who can choose which receivers see it as attested has something to play with, and two conforming verifiers should not disagree on a signed token.

The fix

Both verifiers now read the type from the encoded bytes, instead of trusting what the decoder returns. In CBOR the top 3 bits of an item’s first byte are its major type, so the check is small:

if len(rm) == 0 || rm[0]>>5 != major {
	return ErrMalformed
}

Payload keys must be integers (major type 0 or 1). Defined values must be the type the draft names. Header labels must be an integer or text, and alg an integer. Values under unknown keys are still not type-checked, as before. Duplicate keys are compared by value, so two encodings of one key still count as a duplicate.

You can’t simply ask the decoder for the raw item and check its first byte: fxamacker/cbor drops a leading 55799 tag even when you decode into cbor.RawMessage, and go-cose drops it inside header buckets. The check has to slice the item from the input bytes itself.

What the differential test missed

There is a harness that feeds the same tokens to Go and Rust and compares the verdicts. Before this change its corpus was 3079 cases, with 0 divergences.

It was true for that corpus, but the corpus didn’t cover nulls, simple values or bignum keys in these places. With 76 new shapes added, the old code disagreed on 17 of them, 10 because of Rust and the rest because of Go. Now it is 3155 cases and 0 again.

The Go PR also lists splits that are older than this change and still outside the corpus: an unassigned simple value under an unknown key, header labels beyond int64, indefinite-length items outside the payload, non-minimal envelope heads, nesting deeper than 32, and go-cose’s own validation of registered headers. They are still open, and they are on my list. I’ll write more about the corpus in the next post.

Was the second implementation worth it? Yes. These splits only showed up because there were two verifiers to compare. If I started again, I’d still write two, but I’d build the test corpus from the type rules in the draft, not only from valid tokens.

What changed

  • Spec: 12 new token cases, 62 to 74, every earlier case byte-identical (spec#11).
  • Go: swornmail-go#16. The new tests fail against the old decoder (36 failures) and pass now.
  • Rust: swornmail#15, released as crate 0.2.5. If you use 0.2.4, please update.

Three of the new vectors freeze “a 55799 tag is rejected”. The draft does not mention this tag at all yet, and RFC 8949 calls it semantically transparent, so the draft needs a sentence there too.

My decision card

  • What I’d do today: if you parse anything signed, check the encoded type yourself, before the library maps it into its own types.
  • What would change my mind: a CBOR library with a strict mode that rejects tags, nulls and bignums where the schema names a plain type.
  • Who should choose differently: if you control both ends and never verify someone else’s bytes, the library’s mapping is probably fine.

Check what your CBOR library does with null and with tags. Let me know if you hit something different.

Disclosure: I designed SwornMail. The protocol and its reference code are free and open source (Apache-2.0).