Skip to content

feat(headers): COSE hash envelope — payload-hash-alg, preimage-content-type, payload-location (RFC 9995) (#215) - #220

Merged
Spomky merged 3 commits into
4.9.xfrom
feat/issue-215-hash-envelope
Sep 12, 2026
Merged

Spomky merged 3 commits into
4.9.xfrom
feat/issue-215-hash-envelope

Conversation

@Spomky

@Spomky Spomky commented Sep 12, 2026

Copy link
Copy Markdown
Contributor

Target branch: 4.9.x
Resolves issue #215 (Closes #215)
Part of #202

  • It is a Bug fix
  • It is a New feature
  • Breaks BC
  • Includes Deprecations

What

The COSE hash envelope of RFC 9995: a COSE_Sign / COSE_Sign1 / COSE_Mac / COSE_Mac0 whose 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.

CoseHeaders

Name Label Type Placement (§4) Accessor
payload-hash-alg 258 (LABEL_PAYLOAD_HASH_ALG) int MUST be protected, MUST NOT be unprotected getPayloadHashAlg(): ?int
preimage-content-type 259 (LABEL_PREIMAGE_CONTENT_TYPE) uint / tstr MAY be protected, MUST NOT be unprotected getPreimageContentType(): int|string|null
payload-location 260 (LABEL_PAYLOAD_LOCATION) tstr MAY be protected, MUST NOT be unprotected getPayloadLocation(): ?string

Plus LABEL_CONTENT_TYPE (3), the label the envelope excludes.

  • Each accessor reads the protected bucket only and throws when its label sits in the unprotected one — whether or not the protected bucket carries it too — and throws when a message carrying payload-hash-alg also carries content 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 a content type there is fine and the three accessors answer null. The raw getProtectedHeaderParameter(258) stays the lenient form, as for typ.
  • 258 is int, range-checked against the platform; a text string or byte string is rejected.
  • 259 takes the value syntax of content type through the existing HeaderMapHelper::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 alias payload_preimage_content_type.
  • 260 is a tstr — "The string or URI hint" of the IANA table — so it is not run through assertUriValue(): a bare manifest.spdx.json is valid, a tagged URI or a byte string is not. Never dereferenced.

Cose\Structure\HashEnvelope

  • protectedHeaderFor(Hash $hash, int|string|null $preimageContentType = null, ?string $payloadLocation = null): list<MapItem> — the entries, typed as the CDDL writes them, to spread next to alg; the content type is checked on the way in with the rule the accessor applies on the way out. $hash is a Hash, not a FilterOnlyHash.
  • payloadFor(Hash $hash, string $preimage): string — the digest.
  • create(Manager $manager) then payloadHashAlgorithm(CoseHeaders): Hash and matches(CoseHeaders $headers, string $payload, string $preimage): bool — 258 resolves through the Manager of the application, like every identifier from the wire, and has to resolve to a Hash: 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 same Manager registers them for x5t. Unregistered identifier, non-hash identifier (ES256 under -7 as 258) and missing 258 are refused too. matches() compares with hash_equals(); a payload of the wrong length is false. The Manager is held by the instance (same shape as CertificateSignatureVerifier), the sender-side helpers are static since nothing resolves.

Acceptance criteria of #215

  • The EDN example of §4.1 (1: -35, 4: kid, 16: 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.
  • 258 in the unprotected header, or 3 present alongside 258, is rejected by the strict reader — aPayloadHashAlgInTheUnprotectedBucketIsRejected, aPayloadHashAlgInBothBucketsIsRejected, aContentTypeAlongsidePayloadHashAlgIsRejected (3 protected, 3 unprotected, 3 as an integer × the three accessors).
  • matches() with SHA-1 as payload-hash-alg is refused — HashEnvelopeTest::aFilterOnlyHashIsRefused (SHA-1 and SHA-256/64).

Policy, as the issue and #202 set it

  • §5.1: the signature should be at least as strong as the payload hash — documented, not enforced; which combinations an application accepts is its policy.
  • §5.3: the library never dereferences payload-location; matches() is the confirmation step once the application holds the bytes, same policy as x5u.
  • §5.2: COSE_Encrypt / COSE_Encrypt0 are 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 a content type, a CoAP Content-Format and a plain location, every misplacement, content type alongside 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 a COSE_Sign1 signed with ES256 over the SHA-256 of an SBOM, decoded, verified with Signature1, then confirmed against the content and not against another.
  • RfcReferencesTest knows RFC 9995; ExamplesTest runs examples/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.json keyword RFC9995, examples/14-hash-envelope.php + index: the SHA-256 of a file signed with preimage-content-type and payload-location set, verified, confirmed against the file, then what the envelope refuses (SHA-1, a misplaced 258, a content type).

Note

The base branch still has doc/Usage.md; the main checkout has an uncommitted split of it into doc/*.md. This PR documents in doc/Usage.md as #202 asks, so the Hash Envelope section will need to move to doc/Messages.md (or wherever the header sections land) when that split is committed.

Checks

Based on 4.9.x at #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, no COSE_Encrypt — by design.

…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.
@Spomky Spomky added this to the 4.9.0 milestone Sep 12, 2026
@Spomky Spomky added enhancement New feature or request compliance Issue related to the compliance of the project with the specs headers Header parameters and message structures documentation README, doc/ and examples/ tests Test suite, fixtures and interoperability vectors labels Sep 12, 2026
@Spomky Spomky self-assigned this Sep 12, 2026
@Spomky
Spomky merged commit 9f2cee8 into 4.9.x Sep 12, 2026
17 checks passed
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

compliance Issue related to the compliance of the project with the specs documentation README, doc/ and examples/ enhancement New feature or request headers Header parameters and message structures tests Test suite, fixtures and interoperability vectors

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(headers): COSE hash envelope — payload-hash-alg, preimage-content-type, payload-location (RFC 9995)

1 participant