Skip to content

feat(signature): version 2 countersignatures, labels 11 and 12 (RFC 9338) (#216) - #222

Merged
Spomky merged 1 commit into
4.9.xfrom
feat/issue-216-countersignatures
Sep 12, 2026
Merged

Spomky merged 1 commit into
4.9.xfrom
feat/issue-216-countersignatures

Conversation

@Spomky

@Spomky Spomky commented Sep 12, 2026 •

Copy link
Copy Markdown
Contributor

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

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

What

The version 2 countersignatures of RFC 9338: the Countersign_structure of §3.3, the per-target derivation of its fields for the eight structures the section names, the sign / verify process for both forms, and the two header parameters of §2. Pure structure work, as the issue says — the signature algorithms are the existing ones. Everything is additive: three new classes in Cose\Signature, two constants and two accessors on CoseHeaders, two public helpers on HeaderMapHelper; no existing public signature changes.

Class What
§3.3 Cose\Signature\Countersign The Countersign_structure, on the CoseStructure base of Signature1: create() from fields, full($target, $signProtected) and abbreviated($target) from a target. The context string is a function of both optional fields — "CounterSignature" / "CounterSignatureV2" with sign_protected, "CounterSignature0" / "CounterSignature0V2" without, the V2 suffix when other_fields is present — encoded once, in context(). h'a0' → h'' through emptyOrSerializedMap(), deterministic encoding (§4) through the base.
§3.3 Cose\Signature\CountersignTarget What is countersigned: of($message) dispatches on the six cbor-php messages, CoseSignature and CoseRecipient (plus one named constructor per target and create() from fields). The rule is written once: the second byte string of the target takes the payload slot, every later one goes into other_fields. A detached payload / ciphertext is the application's to supply, one or the other, never both. It carries the unprotected bucket, so getCountersignatures() / getCountersignature0() read what the target holds.
§3.3 Cose\Signature\Countersigner sign(target, Signature, Key, CoseHeaders, aad = '') → CoseSignature (the COSE_Countersignature) and verify(...): the full form, label 11. sign0() → the bare value and verify0(): the abbreviated form, label 12, no sign_protected at all. attach() writes into the unprotected bucket of the target in place and normalises label 11 to an array from the second entry on, keeping what is there as it is (tagged or not); attach0() writes label 12; tagged() wraps under tag 19 as a GenericTag. An alg the countersigner's headers announce has to be the algorithm in hand, on both sides.
§2 CoseHeaders::getCountersignatures(): list<CoseSignature> and getCountersignature0(): ?string LABEL_COUNTERSIGNATURE_V2 (11), LABEL_COUNTERSIGNATURE0_V2 (12). One or many, tagged 19 or bare, normalised to a list. Unprotected bucket only: either label in the protected bucket is rejected, wherever else it appears. Labels 7 and 9 have no constant and are not read.
§3.1 HeaderMapHelper::countersignatureItems(), tagNumberOf(), TAG_COUNTERSIGNATURE The shape check ([bstr, map, bstr], an array told apart from a single entry by its first item, an empty array refused, a tag other than 19 refused, tag 19 around anything else refused) and the tag number read from the head — so the GenericTag of cbor-php 3.4 and the CoseCountersignatureTag of Spomky-Labs/cbor-php#170 pass the same check.

The per-target table

Target RFC 9052 shape payload other_fields context (full)
COSE_Sign1 [ protected, unprotected, payload, signature ] payload [signature] CounterSignatureV2
COSE_Sign [ protected, unprotected, payload, signatures ] payload — CounterSignature
COSE_Signature [ protected, unprotected, signature ] signature — CounterSignature
COSE_Encrypt [ protected, unprotected, ciphertext, recipients ] ciphertext — CounterSignature
COSE_Encrypt0 [ protected, unprotected, ciphertext ] ciphertext — CounterSignature
COSE_recipient [ protected, unprotected, ciphertext, ? recipients ] ciphertext — CounterSignature
COSE_Mac [ protected, unprotected, payload, tag, recipients ] payload [tag] CounterSignatureV2
COSE_Mac0 [ protected, unprotected, payload, tag ] payload [tag] CounterSignatureV2

One point where this departs from the issue text: the COSE_Signature target

The issue says "COSE_Signature (payload = the enclosing message's payload, other_fields = [signature])". This PR takes payload = the signature value, no other_fields, for three reasons:

  1. §3.3 defines other_fields as "an array of all bstr fields after the second" of the target structure and omits it "if there are only two bstr fields in the target structure". A COSE_Signature is [protected, unprotected, signature]: two byte strings. The enclosing message's payload is not a field of the target.
  2. It is what RFC 8152 did (its countersign/signed-01 fixture records ToBeSign_hex = ["CounterSignature", signer_protected, cs_protected, h'', signature]), and §1 says the new structure "is designed to produce the same countersignature value in those cases where the computed cryptographic value was already included".
  3. It is what go-cose (countersign.go), python-cwt (cose_message.py) and Lumoin/Verifiable do.

tests/Signature/CountersignTest.php::theRfc8152ValueOfATwoFieldTargetIsAVersion2Value is the check: the RFC 8152 countersignature of countersign/signed-01 (on a signer) verifies under the version 2 rules, byte for byte the recorded structure — and so do those on a COSE_Sign (signed-03, RFC8152/Appendix_C_1_3 with ES256), a COSE_Encrypt (Enveloped-01, RFC8152/Appendix_C_3_3 with ES512), a COSE_Encrypt0 and a COSE_recipient; the three-field targets (COSE_Sign1, COSE_Mac, COSE_Mac0) do not, which is the point of the new version.

A second reading worth naming: the abbreviated structure omits sign_protected entirely (§3.3, "This field is omitted for the Countersignature0V2 attribute"; the CDDL marks it ?). go-cose keeps an h'' there, python-cwt and Lumoin omit it, and RFC 8152's own abbreviated structure had the h'' — theRfc8152AbbreviatedValueIsNotAVersion2Value shows that countersign1/Encrypt-01 therefore does not verify under version 2, as expected. The issue reads it the same way ("no protected attributes at all").

Acceptance criteria of #216

  • The RFC 9338 Appendix A examples verify — A.1.1 (COSE_Sign), A.2.1 (COSE_Sign1), A.3.1 (COSE_Encrypt, ECDH-ES recipient with a compressed y), A.4.1 (COSE_Encrypt0), A.5.1 (COSE_Mac), A.6.1 (COSE_Mac0), committed as fixtures under tests/fixtures/rfc9338/appendix-a/ with their section in the title, in the cose-wg schema, and run by Rfc9338FixtureTest through the extended harness: the primary signature / MAC / decryption, then each countersignature verified with the RFC's key over the structure this library builds, compared with a ToBeSign_hex encoded independently with cbor2, produced again (EdDSA byte for byte), and refused as the other form. The RFC gives diagnostic notation only; generate.py transcribes it and checks the size the RFC states for each file. Five match; A.6.1 states 159 bytes, which is the A.5.1 size — the printed notation encodes to 139, documented in the fixture README. The appendix has no Countersignature0 V2 example; the abbreviated form is covered by the unit tests and by the vendored countersign1/ fixtures (negative, see below).
  • The cose-wg countersign/ and countersign1/ directories — vendored unchanged (22 files, upstream 53c9d634). Every output.cbor carries label 7 or 9; CoseWgFixture::deprecatedCountersignatureLabels() scans every unprotected bucket of the decoded output (message, signers, recipients at any depth) and the harness reports such a fixture as skipped: "Deprecated, RFC 8152: the message carries the countersignature label(s) 7 of RFC 8152 section 4.5, deprecated by RFC 9338; this library reads the version 2 labels 11 and 12 only". CoseWgHarnessTest::anRfc8152CountersignatureFixtureIsSkippedAsDeprecatedAndNotRead asserts the skip and that getCountersignatures() / getCountersignature0() yield nothing for them. Two pre-existing fixtures fall under the same rule: RFC8152/Appendix_C_1_3 and Appendix_C_3_3, the two countersigned examples of RFC 8152 Appendix C (their primary messages are C_1_1 / C_3_1, which run; and they are RFC 9338 A.1.1 / A.3.1 with the label changed, which run).
  • A countersignature over a countersignature round-trips — CountersignerTest::aCountersignatureOfACountersignatureRoundTripsThroughTheWire: the notary countersigns a COSE_Sign1, the archive countersigns the notary's COSE_Countersignature, both attached, encoded, decoded, read back and verified; the inner one does not verify against the message.
  • Label 11 in a protected bucket is rejected — CountersignatureHeadersTest::label11InTheProtectedBucketIsRejected, label11InBothBucketsIsRejected, label12InTheProtectedBucketIsRejected; the raw getProtectedHeaderParameter(11) stays lenient, as for typ.

Also from the issue: the context rule encoded once (Countersign::context()); per-target derivation for the seven targets of §2 plus COSE_Sign of §3.3; deterministic encoding reused from Signature1 (CoseStructure); "only algorithms with appendix" in the Countersigner docblock; the §6 tag-length warning where the encrypt/MAC targets are documented (README, doc/Usage.md, Countersigner docblock, example 15).

Tests

  • tests/CoseWg/ — the harness verifies countersignatures now: CoseWgParty::countersigners() / countersigners0() read the countersign / countersign0 blocks of a party, CoseWgFixture::requiredAlgorithms() includes them, CoseWgFixtureTestCase::assertCountersignatures() walks the message, its signers and its recipients (nested included). Rfc9338FixtureTest (18 tests). CoseWgHarnessTest gains the deprecation check over the 22 vendored files and aBrokenCountersignatureFailsTheSuite (a flipped byte, and a countersigner declared but absent).
  • tests/Signature/CountersignTest.php — the four contexts, sign_protected omitted, other_fields as an array, h'a0', the deterministic encoding decoded back, and the RFC 8152 cross-checks above (7 two-field, 3 three-field, 1 abbreviated).
  • tests/Signature/CountersignTargetTest.php — the eight derivations, named constructors ≡ of(), a countersignature as a target, detached payload / ciphertext supplied, missing, or supplied twice, a COSE_Signature refusing detached content, an unknown AbstractCoseTag refused, create() from fields.
  • tests/Signature/CountersignerTest.php — both forms on every target (wrong key, wrong aad, the other form: all false), the wire round trips (message, countersignature of a countersignature, abbreviated, on a signer of a COSE_Sign), attach() one → array → append with a tagged entry kept tagged and read back as a GenericTag, tagged(), the alg mismatch on sign and on verify, text alg, headers without alg.
  • tests/Structure/CountersignatureHeadersTest.php — one, many, tagged, mixed, indefinite-length, ten invalid shapes, the protected-bucket rejections, label 12, both forms together, labels 7 / 9 ignored.
  • RfcReferencesTest knows RFC 9338; ExamplesTest runs the new example 15.

Docs

README (This library implements, Features — structures bullet and a new Countersignatures block — Documentation, a new Countersignatures section with the header table, the usage and the §6 warning), doc/Usage.md (intro, TOC, the two Countersign_structure rows of the structures table, a new Countersignatures section under Signature Operations — countersigning, verifying, the per-target table, what the reader refuses, the §6 warning — the common header parameters table, examples and tests tables, references), RELEASES.md, examples/README.md, examples/15-countersignatures.php, tests/fixtures/rfc9338/README.md (provenance, the RFC 8152 twins, the A.6.1 note), tests/fixtures/cose-wg/README.md (the two directories, the policy), composer.json keywords.

Note

The working tree of 4.9.x on my machine has an uncommitted split of doc/Usage.md into doc/*.md. This PR documents in doc/Usage.md as committed on 4.9.x; the Countersignatures section will need to move into doc/Signing.md (or wherever the split puts it) when that lands.

Checks

Rebased on 4.9.x after #220 (hash envelope, RFC 9995): the example is 15-countersignatures.php, #220 having taken 14; README, doc/Usage.md, CoseHeaders and the RFC lists merged with both features. phpstan (level max, no baseline change), ecs, rector, deptrac, parallel-lint, composer validate --strict, the CI pre-checks (permissions, control characters): clean. PHPUnit: 2937 tests, 77 skipped on the 8.4 image (2760 / 53 after #220 — the 24 new skips are the 22 countersign*/ files and RFC8152/Appendix_C_1_3 / C_3_3, all under the deprecation reason); 2883 / 98 on 8.2 with --prefer-lowest (cbor-php 3.4.0; the pre-existing Ed448 / Brainpool skips of that image); 2889 / 77 on 8.5.

Not in this PR

Labels 7 and 9 (Deprecated); any timestamp semantics (#217); a Tag subclass for tag 19 (Spomky-Labs/cbor-php#170 — GenericTag carries the same bytes and countersignatureItems() reads the number from the head, so the dedicated class needs no change here); a Manager-resolving verify() (the application resolves alg from the countersignature's headers, as examples/15 shows).

@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
…338) (#216)

Countersign (the Countersign_structure of RFC 9338 section 3.3), CountersignTarget
(the payload and other_fields of each of the eight targets), Countersigner (sign /
verify for the full form, sign0 / verify0 for the abbreviated one, attach / attach0,
tagged), CoseHeaders::getCountersignatures() and getCountersignature0() with the
labels 11 and 12 read from the unprotected bucket only, HeaderMapHelper::
countersignatureItems() and tagNumberOf().

The six examples of RFC 9338 Appendix A are fixtures under tests/fixtures/rfc9338
and verify; the countersign/ and countersign1/ directories of cose-wg/Examples are
vendored and reported as skipped (Deprecated, RFC 8152), their two-field targets
confirming the per-target derivation all the same.
@Spomky
Spomky force-pushed the feat/issue-216-countersignatures branch from 1b62dad to e74dc8a Compare September 12, 2026 16:27
@Spomky
Spomky merged commit 765e6e9 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(signature): version 2 countersignatures, labels 11 and 12 (RFC 9338)

1 participant