Repository navigation
feat(headers): COSE hash envelope — payload-hash-alg, preimage-content-type, payload-location (RFC 9995) (#215) - #220
Merged
Conversation
…t-type, payload-location (RFC 9995) CoseHeaders gains the three labels of RFC 9995 (LABEL_PAYLOAD_HASH_ALG 258, LABEL_PREIMAGE_CONTENT_TYPE 259, LABEL_PAYLOAD_LOCATION 260), the "content type" label the envelope excludes (LABEL_CONTENT_TYPE 3), and the typed accessors getPayloadHashAlg(), getPreimageContentType() and getPayloadLocation(). Each reads the protected bucket only and applies the placement rules of §4: the label in the unprotected bucket is rejected, and so is "content type" in either bucket of a message carrying "payload-hash-alg". 259 takes the value syntax of "content type" (RFC 9052 §3.1, CoAP Content-Format or media type name with parameters); 260 is a text string, not required to be a URI, and never dereferenced. Cose\Structure\HashEnvelope is the envelope itself: protectedHeaderFor() returns the header entries, payloadFor() the digest that becomes the payload, payloadHashAlgorithm() resolves 258 through the Manager and refuses anything but a Hash — SHA-1 and SHA-256/64 are Filter Only (RFC 9054 §2) and a payload standing for the content is not a filter — and matches() recomputes the digest and compares with hash_equals().
The §4.1 example (258: -16, 259: application/spdx+json, 260: the URL) round-trips through the accessors on the four message types the RFC covers; 258 in the unprotected bucket, or in both, and 259/260 in the unprotected bucket are rejected; "content type" (3) alongside 258, in either bucket, is rejected by all three accessors; malformed values of each parameter are rejected with the RFC section named. HashEnvelopeTest covers the entries protectedHeaderFor() produces, the optional hints, an invalid content type refused on the way in, matches() against the content and against another, SHA-1 and SHA-256/64 refused as payload-hash-alg, an unregistered and a non-hash identifier refused, a message without 258, a misplaced 258, and a COSE_Sign1 signed with ES256 over the SHA-256 of an SBOM, decoded, verified, then confirmed.
README and doc/Usage.md gain a Hash Envelope section: the three parameters with their labels, types, references and accessors, the placement rules of RFC 9995 §4, the two ends of HashEnvelope, and the three points of §5 — the signature is verified first and matches() confirms the content, the signature should be at least as strong as the hash, COSE_Encrypt is out of scope. payload-location is never fetched, like x5u. The header parameter table lists 258–260; RFC 9995 joins the implemented list, the references and the composer keywords; RELEASES.md describes the feature. examples/14-hash-envelope.php signs the SHA-256 of a file with preimage-content-type and payload-location set, verifies the signature, confirms the file against the digest, and shows what the envelope refuses.
15 tasks done
This was referenced Sep 12, 2026
Spomky
added a commit
that referenced
this pull request
Sep 13, 2026
doc/Usage.md (2600 lines) becomes fourteen chapters under doc/, with doc/README.md as the index: Installation, Messages, Signing, Countersignatures, Mac, Encryption, KeyManagement, Cwt, X509, HashEnvelope, Receipts, Algorithms, Keys and Upgrading. README.md keeps the introduction, the list of RFCs, the chapter table and the points to know before relying on the library; the algorithm and key tables live in doc/Algorithms.md and doc/Keys.md only, and RfcReferencesTest now checks them there — and checks that no other document carries a copy. The chapters cover everything merged since the split was started: RFC 9995 (hash envelope, #220), RFC 9338 (countersignatures, #222), RFC 9964 (ML-DSA and AKP keys, #223) and RFC 9942 (receipts, #221), each on its own page or in the Algorithms and Keys chapters, with the header table, the structures table, the doc index and the platform-gate table updated accordingly. RELEASES.md links point at the new chapters and its support matrix lists the 4.9.x line only.
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.
Target branch: 4.9.x
Resolves issue #215 (Closes #215)
Part of #202
What
The COSE hash envelope of RFC 9995: a
COSE_Sign/COSE_Sign1/COSE_Mac/COSE_Mac0whose payload is the digest of the content rather than the content, with the three header parameters of §3 that say so. Builds on the hash registry of #195 (#210). Everything is additive: new constants, new accessors, one new class, no existing public signature changes, no new dependency.CoseHeaderspayload-hash-algLABEL_PAYLOAD_HASH_ALG)intgetPayloadHashAlg(): ?intpreimage-content-typeLABEL_PREIMAGE_CONTENT_TYPE)uint / tstrgetPreimageContentType(): int|string|nullpayload-locationLABEL_PAYLOAD_LOCATION)tstrgetPayloadLocation(): ?stringPlus
LABEL_CONTENT_TYPE(3), the label the envelope excludes.payload-hash-algalso carriescontent type(3) in either bucket: "Label 3 (content_type) MUST NOT be present in the protected or unprotected headers" (§4). A message without 258 is not an envelope, so acontent typethere is fine and the three accessors answernull. The rawgetProtectedHeaderParameter(258)stays the lenient form, as fortyp.int, range-checked against the platform; a text string or byte string is rejected.content typethrough the existingHeaderMapHelper::assertContentTypeValue(): a CoAP Content-Format number 0–65535 or a<type-name>/<subtype-name>media type name, parameters allowed — what §3 says ("a content-format number (Section 12.3 of [RFC7252]) or as a media-type name optionally with parameters (Section 8.3 of [RFC9110])"). IANA's name is used for the accessor; the docblock mentions the CDDL aliaspayload_preimage_content_type.tstr— "The string or URI hint" of the IANA table — so it is not run throughassertUriValue(): a baremanifest.spdx.jsonis valid, a tagged URI or a byte string is not. Never dereferenced.Cose\Structure\HashEnvelopeprotectedHeaderFor(Hash $hash, int|string|null $preimageContentType = null, ?string $payloadLocation = null): list<MapItem>— the entries, typed as the CDDL writes them, to spread next toalg; the content type is checked on the way in with the rule the accessor applies on the way out.$hashis aHash, not aFilterOnlyHash.payloadFor(Hash $hash, string $preimage): string— the digest.create(Manager $manager)thenpayloadHashAlgorithm(CoseHeaders): Hashandmatches(CoseHeaders $headers, string $payload, string $preimage): bool— 258 resolves through theManagerof the application, like every identifier from the wire, and has to resolve to aHash: SHA-1 (-14) and SHA-256/64 (-15) are refused with a message naming RFC 9054 §2 and RFC 9995 §5.1, even when the sameManagerregisters them forx5t. Unregistered identifier, non-hash identifier (ES256 under -7 as 258) and missing 258 are refused too.matches()compares withhash_equals(); a payload of the wrong length isfalse. The Manager is held by the instance (same shape asCertificateSignatureVerifier), the sender-side helpers are static since nothing resolves.Acceptance criteria of #215
application/example+cose, 258: -16, 259:application/spdx+json, 260: the URL) round-trips through the accessors —CoseHeadersTest::theRfc9995ExampleRoundTripsThroughTheProtectedHeader, on the four message types §5.2 puts in scope.aPayloadHashAlgInTheUnprotectedBucketIsRejected,aPayloadHashAlgInBothBucketsIsRejected,aContentTypeAlongsidePayloadHashAlgIsRejected(3 protected, 3 unprotected, 3 as an integer × the three accessors).matches()with SHA-1 aspayload-hash-algis refused —HashEnvelopeTest::aFilterOnlyHashIsRefused(SHA-1 and SHA-256/64).Policy, as the issue and #202 set it
payload-location;matches()is the confirmation step once the application holds the bytes, same policy asx5u.COSE_Encrypt/COSE_Encrypt0are out of scope of the RFC and out of scope here. The accessors read any message, since a header is a header; the tests and docs stay on the four in-scope types.matches()verifies nothing on the envelope's behalf: verify the signature or the MAC first, then confirm the content. The docblocks, the usage guide and the example all say so.Tests
tests/Structure/CoseHeadersTest.php(+33): labels, the §4.1 round trip, absent parameters on a non-envelope with acontent type, a CoAP Content-Format and a plain location, every misplacement,content typealongside 258 (9 cases), malformed 258 / 259 / 260 (text, bytes, out of range, bare subtype, negative, 65536, tagged URI).tests/Structure/HashEnvelopeTest.php(new, 14 tests): the entries and their types, the optional hints, an invalid content type refused on the way in, the digest,matches()true / another content / another function / empty payload, SHA-1 and SHA-256/64 refused, unregistered and non-hash identifiers refused, no 258, misplaced 258, and aCOSE_Sign1signed with ES256 over the SHA-256 of an SBOM, decoded, verified withSignature1, then confirmed against the content and not against another.RfcReferencesTestknows RFC 9995;ExamplesTestrunsexamples/14-hash-envelope.php.Docs and examples
README (This library implements, Features, Documentation, new Hash Envelope section),
doc/Usage.md(intro, TOC, new Hash Envelope section under Reading Headers with the placement rules and the three points of §5, Common Header Parameters rows 258–260, examples and tests tables, References),RELEASES.md,composer.jsonkeywordRFC9995,examples/14-hash-envelope.php+ index: the SHA-256 of a file signed withpreimage-content-typeandpayload-locationset, verified, confirmed against the file, then what the envelope refuses (SHA-1, a misplaced 258, acontent type).Note
The base branch still has
doc/Usage.md; the main checkout has an uncommitted split of it intodoc/*.md. This PR documents indoc/Usage.mdas #202 asks, so the Hash Envelope section will need to move todoc/Messages.md(or wherever the header sections land) when that split is committed.Checks
Based on
4.9.xat #213.phpstan(level max, no baseline change),ecs,rector,deptrac,parallel-lint,composer validate --strict: clean. PHPUnit: 2756 tests, 53 skipped on the 8.4 image (was 2708 / 53).Not in this PR
Countersignatures (#216), timestamp tokens (#217) and receipts (#218) — the envelope binds to all three, none is needed by it. No fetching of
payload-location, no strength ranking of hash against signature, noCOSE_Encrypt— by design.