Skip to content

feat(headers): receipts, vds, vdp and RFC9162_SHA256 inclusion proofs (RFC 9942) (#218) - #221

Merged
Spomky merged 2 commits into
4.9.xfrom
feat/issue-218-receipts-vds-vdp
Sep 12, 2026
Merged

Spomky merged 2 commits into
4.9.xfrom
feat/issue-218-receipts-vds-vdp

Conversation

@Spomky

@Spomky Spomky commented Sep 12, 2026 •

Copy link
Copy Markdown
Contributor

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

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

What

The COSE receipts of RFC 9942: the receipts (394), vds (395) and vdp (396) header parameters, and the one registered verifiable data structure, RFC9162_SHA256 (vds = 1), with its inclusion and consistency proofs — the consistency verification fitted in the same class without contortion, so it is in (the issue left it optional). Everything is additive: new classes, new constants, new methods, two formerly private helpers made public; no existing public signature changes, no new dependency.

The library establishes no trust in a receipt issuer. That sentence opens the README section, the doc/Usage.md section and the docblock of ReceiptVerifier. A true result means exactly: the entry is a leaf of a tree whose head the holder of the given key signed. Who the key belongs to, whether the log is honest, whether the receipt is still valid (§7.2), what to do with crit: the application's.

CoseHeaders

  • getReceipts(): list<CBOR\Tag\CoseSign1Tag> — protected bucket first (§4.3 registers the parameter "in the protected and unprotected headers"), an empty list when absent so the caller can iterate without a check. Each entry is bstr .cbor Receipt: an empty byte string or trailing bytes after the item make it malformed, and "Receipts MUST be tagged as COSE_Sign1" — an untagged array, a COSE_Mac0 (17) or a CWT (61) around the message are rejected by name. A GenericTag 18 (decoder without the class) is rebuilt as CoseSign1Tag.
  • getVds(): ?int — protected bucket only, as §5.2.1/§5.3.1 require ("The VDS in the protected header is necessary to understand the inclusion proof structure"); an unprotected copy is ignored, not refused. Handed back as carried; the registry check happens where the proofs are read.
  • getVdp(): ?MapObject — the proof map as it travels, keys checked to be labels. The CDDL puts it in the unprotected header; a protected one is read too, first (a proof gains nothing from the signature — the root it leads to is what is signed).
  • LABEL_RECEIPTS, LABEL_VDS, LABEL_VDP.

Cose\Structure\VerifiableDataStructure

Class Role
Rfc9162Sha256 The structure (IDENTIFIER 1, NAME, LABEL_INCLUSION_PROOF -1, LABEL_CONSISTENCY_PROOF -2, HASH_SIZE). leafHash(), nodeHash(), emptyTreeHash(), treeHash(...$entries) (RFC 9162 §2.1.1–2.1.2, stack algorithm). inclusionProofs(CoseHeaders) / consistencyProofs(CoseHeaders) read the proofs of a receipt after checking vds === 1 and every vdp key against the proof registry — §4.3: "the verifier MUST confirm that the associated VDS and VDPs match entries present in the registries". An unregistered vds (0 reserved, 2, …) or proof label (-3, 0, "inclusion") is an InvalidArgumentException naming the value, never skipped.
Rfc9162Sha256InclusionProof [ tree-size: uint, leaf-index: uint, inclusion-path: [ + bstr ] ] wrapped in a byte string (§5.2). root($entry): ?string applies RFC 9162 §2.1.3.2 and returns the tree head or null (leaf-index ≥ tree-size — the RFC's own guard —, path of the wrong length); verify($entry, $root) with hash_equals(); rootFromLeafHash() / verifyLeafHash() for callers holding the leaf hash. Nodes are checked to be 32 bytes; uint beyond PHP_INT_MAX refused.
Rfc9162Sha256ConsistencyProof [ tree-size-1, tree-size-2, consistency-path: [ + bstr ] ] (§5.3). newerRoot($olderRoot): ?string applies RFC 9162 §2.1.4.2 and returns the newer tree head the proof binds the older one to (the payload of a receipt of consistency, §5.3.1) or null; verify($older, $newer). Defined for 0 < first < second only; an empty path is refused on decode and on construction (CDDL +, and step 1 of the algorithm).
ReceiptVerifier create(Manager, ?Decoder), verifyInclusion(CoseSign1Tag, string $entry, Key, ?ByteStringObject $externalAad): bool, verifyConsistency(CoseSign1Tag, string $olderRoot, Key, ...). The two steps of §5.2 as one boolean (§5.3.1: "It is recommended that implementations return a single boolean result"): proof → tree head → Signature1 over it → Signature::verify(), the algorithm resolved from the receipt's alg through the operator's Manager (same reasoning as CertificateSignatureVerifier: alg comes from the wire and cannot select an unregistered verifier; HS256 registered under 5 is refused as "not a signature algorithm"). Several proofs in one receipt: verifies as soon as one leads to a signed root. An attached payload (§4.4 says SHOULD be detached) has to be the tree head the proof leads to, else the receipt fails.

HeaderMapHelper gains decodeEmbedded() (a bstr .cbor value under the rules decodeProtected() already applied: one item, no trailing bytes, no empty string; decodeProtected() now delegates to the same private routine) and tagNumber() goes public (was private) to recognize a GenericTag 18.

Three decisions the issue did not settle

  1. An empty inclusion-path is accepted. RFC 9942 §5.2 writes [ + bstr ], but points to RFC 9162 §2.1.3.1 for "a complete description of this VDS Proof Type", and that section defines PATH(0, {d[0]}) = {} — the only leaf of a one-entry tree has an empty proof. The reference implementation (transmute-industries/cose) encodes whatever the log gives it and would emit [] for the first entry of any log. Rejecting it would refuse the first receipt every log issues, for no security gain: the RFC 9162 walk then succeeds for tree size 1 and nothing else (theEmptyProofOfASingleLeafTreeVerifies). Documented in the class, in doc/Usage.md and in RELEASES.md; a one-line change if you want the CDDL applied literally. The consistency path, by contrast, is never empty (RFC 9162 §2.1.4.2 step 1 agrees with the CDDL) and is refused.
  2. tree-size is not bound by the signature. RFC 9942 signs the tree head alone. A proof for leaf 5 of 8 also "verifies" under tree size 7 (6 and 7 shift to the same bits): the size steers the walk and is otherwise the issuer's word. Documented (theTreeSizeSteersTheWalkAndIsNotOtherwiseBound), since a reader could take it for a bug.
  3. Failure shapes. root() / newerRoot() return null when the proof leads nowhere and verify*() return false, like Signature::verify(); an argument that makes the walk meaningless (a leaf hash or an older tree head that is not 32 bytes) throws, like the rest of the library. ReceiptVerifier throws when the receipt is not a receipt of the requested kind for a registered structure, and returns false for every cryptographic or proof failure.

Acceptance criteria of #218

  • The RFC 9942 EDN examples (§3/§4.3 Figure 2, §5.2) decode into the typed structures. The RFC elides the middle bytes of every hash and signature (h'fc9f050f...221c92cb'), so the examples cannot be pasted; the tests rebuild them with the exact shapes the RFC prints — Figure 2: a message with two receipts, size 9 / leaf 8 / one node and size 6 / leaf 5 / two nodes, detached payloads, ES256, vds 1 in the protected header, vdp in the unprotected one (CoseHeadersTest::theReceiptsOfTheRfc9942ExampleAreReadFromEveryMessageType, on all six message types, through the bytes); §5.2: size 20 / leaf 17 / three nodes; §5.3.1: old 20 / new 104 / six nodes — and check that PATH/PROOF of RFC 9162 give exactly that many nodes for those parameters (they do).
  • Inclusion proofs verify against RFC 9162 test data. The 186 inclusion and consistency probes of transparency-dev/merkle (testdata/, commit a09734c, Apache-2.0, LICENSE + README vendored under tests/fixtures/rfc9162/) run in Rfc9162FixtureTest: every wantErr probe is refused or fails, every happy path verifies. Two probes rely on the upstream extension first == second with an empty path (an equality check, outside RFC 9162's 0 < first < second and the CDDL) and are asserted as refused, with the reason in the fixtures README. The eight CT tree heads (6e340b9c…, …, 5dc9da79…) are reproduced by treeHash(), and the seven-leaf example tree of RFC 9162 §2.1.5 gives the proofs the RFC spells out by node name (d0 → [b, h, l], PROOF(3, D[7]) = [c, d, g, l], …).
  • A flipped path node, a wrong leaf-index, leaf-index >= tree-size fail — Rfc9162Sha256InclusionProofTest, plus every leaf of every tree size up to 40 against proofs produced by the recursive definitions (test helper MerkleTree, which implements MTH/PATH/PROOF of RFC 9162 independently of the stack algorithm of the library), and every pair 0 < older < newer ≤ 40 for consistency.
  • A receipt with an unregistered vds is rejected — Rfc9162Sha256Test (0, 2, 1000, -1), ReceiptVerifierTest, and the example.
  • Documentation of what is proven and what is not — README, doc/Usage.md ([!IMPORTANT] block plus "four points that follow from the RFC"), ReceiptVerifier and Rfc9162Sha256 docblocks.

Tests

  • tests/Structure/VerifiableDataStructure/: Rfc9162Sha256Test, Rfc9162Sha256InclusionProofTest, Rfc9162Sha256ConsistencyProofTest (wire forms incl. indefinite-length, every malformed CDDL shape with its message, 2⁶⁴−1 refused, the failures above), Rfc9162FixtureTest (186 probes), ReceiptVerifierTest (detached/attached/mismatching payload, tampered proof, other entry, other key, external_aad, several proofs, consistency, a receipt read out of a message, unregistered vds/label, missing/text/unregistered/non-signature alg).
  • CoseHeadersTest (+11: Figure 2 on six message types, protected bucket, GenericTag 18, nine invalid receipts shapes, vds bucket rules and range, vdp bucket rules and labels), HeaderMapHelperTest (+6), RfcReferencesTest knows RFC 9942, ExamplesTest runs examples/17-receipts.php.
  • 3037 tests, 62130 assertions, green on the CI image; PHPStan (max), ECS, Rector, Deptrac, parallel-lint clean.

Docs and examples

README (This library implements, Features, Documentation, new COSE Receipts section), doc/Usage.md (new COSE Receipts section under Reading Headers, TOC, Common Header Parameters rows 394–396, examples table, References incl. RFC 9162), RELEASES.md, composer.json keyword RFC9942, examples/17-receipts.php (issues and verifies a receipt of inclusion and a receipt of consistency over the eight-leaf CT tree, shows a tampered proof, an unregistered structure and where the library stops) + index.

@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 d886361 into 4.9.x Sep 12, 2026
18 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): receipts, vds, vdp and RFC9162_SHA256 inclusion proofs (RFC 9942)

1 participant