Repository navigation
Conversation
to_bytes writes COSE_Mac0 (17) for MAC algorithms and COSE_Sign1 (18) for signature algorithms, but from_bytes discarded the tag, so a token tagged COSE_Mac0 carrying ES256 (or COSE_Sign1 carrying HMAC) decoded and verified. from_bytes now records any 17/18 tag among the leading tags and rejects it with InvalidFormat if it doesn't match alg.class(). Untagged and CWT-only (61) tokens are unchanged. Resolves the FOLLOWUPS item. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
zshenker
force-pushed
the
cose-tag-alg-check
branch
from
October 19, 2026 21:43
91cf0f4 to
385f270
Compare
Replace InvalidFormat(String) with Error::StructureTagMismatch { tag,
algorithm, expected }, so callers can distinguish a structure that
contradicts its declared algorithm from other malformed input without
matching on message text.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
to_bytespicks the COSE structure tag from the algorithm's class: COSE_Mac0 (17) for MAC algorithms and COSE_Sign1 (18) for signature algorithms.from_bytesthrows the tag away, so the encoder's class → tag rule is never checked on the way back in. A token tagged COSE_Mac0 that carriesalg = ES256, or one tagged COSE_Sign1 that carriesalg = HMAC, decodes and verifies without complaint. This was an openFOLLOWUPS.mditem. It matters more now that asymmetric algorithms are supported, because consumers shouldn't have to parse raw bytes to check that the structure and the declared algorithm agree.Change
from_bytesrecords any COSE_Mac0 or COSE_Sign1 tag among the (up to two) leading tags.alg.class(), decoding fails withError::StructureTagMismatch { tag, algorithm, expected }, so callers can match on the mismatch without parsing message text.alg(verifyalready rejects those)This complements #6. #6 binds the algorithm to the caller's key; this PR makes the wire structure agree with the declared algorithm. It is based on
mainand independent of #6. A dry merge with #6, #8 and #9 shows no conflicts. The tests are in a newsrc/tests_cose.rsfor that reason.Tests
The new
src/tests_cose.rssigns real HS256 and ES256 tokens and flips the tag byte. It covers:cargo fmt --check,cargo clippy --all-targets -- -D warningsandcargo testall pass: 60 unit tests and 73 doctests. The existing interop vector still verifies.🤖 Generated with Claude Code