Skip to content

feat(headers): X.509 header parameters x5bag, x5chain, x5t, x5u (RFC 9360) (#196) - #212

Merged
Spomky merged 2 commits into
4.9.xfrom
feat/issue-196-x509-header-parameters
Sep 11, 2026
Merged

Spomky merged 2 commits into
4.9.xfrom
feat/issue-196-x509-header-parameters

Conversation

@Spomky

@Spomky Spomky commented Sep 11, 2026 •

Copy link
Copy Markdown
Contributor

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

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

What

The X.509 header parameters of RFC 9360: x5bag (32), x5chain (33), x5t (34) and x5u (35), with the *-sender labels of §3 declared. Everything is additive: new classes, new constants, new methods, no existing public signature changes.

The library validates no certificate chain and fetches no URI. That sentence opens the README section, the doc/Usage.md section and the docblock of every new class. It reads the parameters, applies their CDDL rules, verifies a signature with the certificate a chain names, and hands the certificates to spomky-labs/pki-framework; path validation, revocation, trust anchors and x5u dereferencing are the application's (RFC 9360 §5).

Cose\Structure\X509

Class CDDL Role
CoseX509 bstr / [ 2*certs: bstr ] The wire structure. fromCBOR() / toCBOR(), certificates(): non-empty-list<string> (DER, as carried), toCertificates(): Certificate[] (parsed on demand), find(CoseCertHash, FilterOnlyHash): ?string. An array of length one is rejected on decode with a message naming the rule, and never produced on encode.
X5Bag COSE_X509 Unordered; toCertificateBundle() for pki-framework's path building.
X5Chain COSE_X509 Ordered, end-entity first; endEntityCertificate(), toCertificateChain() (→ CertificationPath::fromCertificateChain()), fromCertificateChain().
CoseCertHash [ hashAlg: (int / tstr), hashValue: bstr ] hashAlgorithm(Manager): FilterOnlyHash resolves through the RFC 9054 registry of #195 — SHA-1 and SHA-256/64 accepted, this being the filtering use; a text-string identifier, an unregistered one or one registered with a non-hash is refused. matches(string $der, FilterOnlyHash) compares with hash_equals() and throws if the hash given is not the one the thumbprint names. compute() makes one.

CoseHeaders

  • getX5Bag(): ?X5Bag, getX5Chain(): ?X5Chain, getX5T(): ?CoseCertHash, getX5U(): ?string — protected bucket first, null when absent, as the issue asks. Each of the four "can be in either the protected or unprotected header bucket" (§2), so unlike typ neither bucket is refused; the docs say why the bucket still matters ("The end-entity certificate MUST be integrity protected by COSE").
  • LABEL_X5BAG, LABEL_X5CHAIN, LABEL_X5T, LABEL_X5U, and LABEL_X5T_SENDER (-27), LABEL_X5U_SENDER (-28), LABEL_X5CHAIN_SENDER (-29) — the last three without accessors until ECDH-SS (feat(key management): direct, AES-KW, HKDF, ECDH-ES/SS recipient algorithms (RFC 9053 §5–6) #201), per the issue.
  • HeaderMapHelper::assertUriValue(): RFC 9360 says the value "contains a CBOR text string" while the CDDL type uri is #6.32(tstr), so both a bare text string and one under tag 32 are read (the default decoder yields UriTag; a decoder without it yields a GenericTag, both handled). The text must start with a scheme (RFC 3986 §3); nothing else is checked, nothing is fetched.

CertificateSignatureVerifier::verifyWithX5Chain()

verifyWithX5Chain(int $alg, X5Chain $chain, string $data, string $signature): bool — the "verify with the leaf certificate of the chain" path in one call. Only the end-entity certificate is looked at; the rest of the chain is the application's to validate.

One design point the issue did not anticipate: thumbprints are computed over the bytes as carried

CoseCertHash::compute() and matches() take the DER string, not a parsed Certificate. The certificates of cose-wg/Examples are not strict DER (their keyUsage BIT STRING carries a spare byte), so Certificate::fromDER($alice)->toDER() !== $alice, and the SHA-256 of the re-encoding is not the x5t that signed-05 carries. A receiver hashing a re-encoded certificate would never match a sender's thumbprint. CoseCertHashTest::theThumbprintIsComputedOverTheBytesAsCarriedNotOverAReEncoding pins this; CoseX509::fromCertificates() (sender side) keeps accepting parsed certificates, documented.

Dependency: spomky-labs/pki-framework floor raised to ^1.6.2 (was ^1.0)

The first CI run failed on the lowest-deps job only: pki-framework 1.0.0 rejects the cose-wg chain in the path-validation demonstration of X5ChainTest and of the example ("Certificate signature doesn't match"). Bisected: every release ≤ 1.6.1 verifies a certificate signature over a re-encoded tbsCertificate, so a certificate that is not strict DER (the cose-wg ones, over-long keyUsage BIT STRING) never validates; 1.6.2 verifies the bytes as carried. 1.6.2 (2026-09-08) is also the only supported line — its release notes close 27 advisories affecting ≤ 1.6.1, among them GHSA-gjx8-fvx6-qg7q (that very BIT STRING), and declare 1.0.x–1.5.x end of life. The API this library uses is unchanged across the range; the lowest-deps job now resolves pki-framework 1.6.2 + cbor-php 3.4.0 and passes (2428 tests on 8.2, reproduced locally). Documented in RELEASES.md.

Acceptance criteria of #196

  • Round-trip of the four header parameters through CoseHeaders, protected and unprotected — CoseHeadersTest, on all six message types, encoded to bytes and decoded again
  • A COSE_X509 array of length one is rejected on decode with an explicit message — CoseX509Test, X5ChainTest, X5BagTest, CoseHeadersTest::anX5ChainArrayOfOneIsRejected; never produced on encode
  • x5t with SHA-256 and with SHA-1 matches the right certificate of a chain and no other — CoseCertHashTest, CoseX509Test, X5ChainTest, X5BagTest
  • Documentation states in the first paragraph that no chain validation and no network access is performed — README ## X.509 Header Parameters and doc/Usage.md ### X.509 Header Parameters both open with a [!IMPORTANT] block saying exactly that

Tests

  • tests/CoseWg/X509FixtureTest.php — the five x509-examples fixtures, read through the accessors: the certificates on the wire are the input's, byte for byte, and encode back to the wire form; signed-03/04 verify with verifyWithX5Chain() (and not with the CA that follows); in signed-01/02 exactly one certificate of the bag verifies; the x5t of signed-05 resolves through the Manager, selects Alice out of a two-certificate store, and that certificate verifies the signature.
  • CoseX509Test, CoseCertHashTest, X5ChainTest, X5BagTest (tests/Structure/X509/): wire forms incl. indefinite-length, every malformed shape with its message, identifier forms (negative, unsigned, text, 2⁶⁴−1 refused), registry resolution and its three refusals, wrong-algorithm comparison refused, path validation of the chain with pki-framework against ca.crt as trust anchor (in the test, where it belongs).
  • CoseHeadersTest (+9), HeaderMapHelperTest (URI values: bare, indefinite, UriTag, GenericTag 32, tag 33 refused, byte string, relative reference, empty), CertificateSignatureVerifierTest (+2 over every algorithm of the provider).
  • RfcReferencesTest knows RFC 9360; ExamplesTest runs examples/13-x509-header-parameters.php.

Docs and examples

README (This library implements, Features, Documentation, new X.509 Header Parameters section), doc/Usage.md (new section under Reading Headers, TOC, Common Header Parameters rows 15/16/32–35, examples table, References; Verifying a Signature Made by a Certificate points at verifyWithX5Chain()), RELEASES.md, composer.json keyword RFC9360, examples/13-x509-header-parameters.php + index (13, since #197 took 12).

Checks

Rebased on 4.9.x after #211 (COSE Key Thumbprint) landed. CI: every job green after the pki-framework floor bump (the first run failed on lowest-deps only, see above). phpstan (level max, no baseline change), ecs, rector, deptrac, parallel-lint, composer validate --strict: clean. PHPUnit: 2434 tests on the 8.4 image (194 skipped, the key-management fixtures of #201, unchanged); 2346 on 8.2.

Not in this PR

Accessors for x5t-sender / x5u-sender / x5chain-sender (#201), C509 (draft), any form of chain validation or URI retrieval — by design.

@Spomky Spomky added this to the 4.9.0 milestone Sep 11, 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 11, 2026
@Spomky Spomky self-assigned this Sep 11, 2026
Every earlier release verifies a certificate signature over a re-encoded
tbsCertificate, so a certificate that is not strict DER -- the cose-wg
x509-examples, whose keyUsage BIT STRING carries a spare byte -- fails
path validation with "Certificate signature doesn't match". 1.6.2
verifies the bytes as carried, and is the only supported line: its
release notes close 27 advisories affecting <= 1.6.1 and declare 1.0.x
to 1.5.x end of life. Fixes the lowest-deps job of the CI.
@Spomky Spomky added the dependencies Pull requests that update a dependency file label Sep 11, 2026
@Spomky
Spomky merged commit 6a8b61d into 4.9.x Sep 11, 2026
17 checks passed
@Spomky
Spomky deleted the feat/issue-196-x509-header-parameters branch September 11, 2026 21:25
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 dependencies Pull requests that update a dependency file 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): X.509 header parameters x5bag, x5chain, x5t, x5u (RFC 9360)

1 participant