Repository navigation
feat(signature): version 2 countersignatures, labels 11 and 12 (RFC 9338) (#216) - #222
Merged
Merged
Conversation
15 tasks done
…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
force-pushed
the
feat/issue-216-countersignatures
branch
from
September 12, 2026 16:27
1b62dad to
e74dc8a
Compare
Merged
8 of 11 tasks
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 #216 (Closes #216)
Part of #202
What
The version 2 countersignatures of RFC 9338: the
Countersign_structureof §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 inCose\Signature, two constants and two accessors onCoseHeaders, two public helpers onHeaderMapHelper; no existing public signature changes.Cose\Signature\CountersignCountersign_structure, on theCoseStructurebase ofSignature1:create()from fields,full($target, $signProtected)andabbreviated($target)from a target. The context string is a function of both optional fields —"CounterSignature"/"CounterSignatureV2"withsign_protected,"CounterSignature0"/"CounterSignature0V2"without, theV2suffix whenother_fieldsis present — encoded once, incontext().h'a0'→h''throughemptyOrSerializedMap(), deterministic encoding (§4) through the base.Cose\Signature\CountersignTargetof($message)dispatches on the six cbor-php messages,CoseSignatureandCoseRecipient(plus one named constructor per target andcreate()from fields). The rule is written once: the second byte string of the target takes the payload slot, every later one goes intoother_fields. A detached payload / ciphertext is the application's to supply, one or the other, never both. It carries the unprotected bucket, sogetCountersignatures()/getCountersignature0()read what the target holds.Cose\Signature\Countersignersign(target, Signature, Key, CoseHeaders, aad = '')→CoseSignature(theCOSE_Countersignature) andverify(...): the full form, label 11.sign0()→ the bare value andverify0(): the abbreviated form, label 12, nosign_protectedat 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 aGenericTag. Analgthe countersigner's headers announce has to be the algorithm in hand, on both sides.CoseHeaders::getCountersignatures(): list<CoseSignature>andgetCountersignature0(): ?stringLABEL_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.HeaderMapHelper::countersignatureItems(),tagNumberOf(),TAG_COUNTERSIGNATURE[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 theGenericTagof cbor-php 3.4 and theCoseCountersignatureTagof Spomky-Labs/cbor-php#170 pass the same check.The per-target table
COSE_Sign1[ protected, unprotected, payload, signature ][signature]CounterSignatureV2COSE_Sign[ protected, unprotected, payload, signatures ]CounterSignatureCOSE_Signature[ protected, unprotected, signature ]CounterSignatureCOSE_Encrypt[ protected, unprotected, ciphertext, recipients ]CounterSignatureCOSE_Encrypt0[ protected, unprotected, ciphertext ]CounterSignatureCOSE_recipient[ protected, unprotected, ciphertext, ? recipients ]CounterSignatureCOSE_Mac[ protected, unprotected, payload, tag, recipients ][tag]CounterSignatureV2COSE_Mac0[ protected, unprotected, payload, tag ][tag]CounterSignatureV2One point where this departs from the issue text: the
COSE_SignaturetargetThe issue says "
COSE_Signature(payload = the enclosing message's payload,other_fields = [signature])". This PR takes payload = the signature value, noother_fields, for three reasons:other_fieldsas "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". ACOSE_Signatureis[protected, unprotected, signature]: two byte strings. The enclosing message's payload is not a field of the target.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".countersign.go), python-cwt (cose_message.py) and Lumoin/Verifiable do.tests/Signature/CountersignTest.php::theRfc8152ValueOfATwoFieldTargetIsAVersion2Valueis the check: the RFC 8152 countersignature ofcountersign/signed-01(on a signer) verifies under the version 2 rules, byte for byte the recorded structure — and so do those on aCOSE_Sign(signed-03,RFC8152/Appendix_C_1_3with ES256), aCOSE_Encrypt(Enveloped-01,RFC8152/Appendix_C_3_3with ES512), aCOSE_Encrypt0and aCOSE_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_protectedentirely (§3.3, "This field is omitted for the Countersignature0V2 attribute"; the CDDL marks it?). go-cose keeps anh''there, python-cwt and Lumoin omit it, and RFC 8152's own abbreviated structure had theh''—theRfc8152AbbreviatedValueIsNotAVersion2Valueshows thatcountersign1/Encrypt-01therefore does not verify under version 2, as expected. The issue reads it the same way ("no protected attributes at all").Acceptance criteria of #216
COSE_Sign), A.2.1 (COSE_Sign1), A.3.1 (COSE_Encrypt, ECDH-ES recipient with a compressedy), A.4.1 (COSE_Encrypt0), A.5.1 (COSE_Mac), A.6.1 (COSE_Mac0), committed as fixtures undertests/fixtures/rfc9338/appendix-a/with their section in the title, in the cose-wg schema, and run byRfc9338FixtureTestthrough 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 aToBeSign_hexencoded independently with cbor2, produced again (EdDSA byte for byte), and refused as the other form. The RFC gives diagnostic notation only;generate.pytranscribes 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 noCountersignature0 V2example; the abbreviated form is covered by the unit tests and by the vendoredcountersign1/fixtures (negative, see below).countersign/andcountersign1/directories — vendored unchanged (22 files, upstream53c9d634). Everyoutput.cborcarries 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::anRfc8152CountersignatureFixtureIsSkippedAsDeprecatedAndNotReadasserts the skip and thatgetCountersignatures()/getCountersignature0()yield nothing for them. Two pre-existing fixtures fall under the same rule:RFC8152/Appendix_C_1_3andAppendix_C_3_3, the two countersigned examples of RFC 8152 Appendix C (their primary messages areC_1_1/C_3_1, which run; and they are RFC 9338 A.1.1 / A.3.1 with the label changed, which run).CountersignerTest::aCountersignatureOfACountersignatureRoundTripsThroughTheWire: the notary countersigns aCOSE_Sign1, the archive countersigns the notary'sCOSE_Countersignature, both attached, encoded, decoded, read back and verified; the inner one does not verify against the message.CountersignatureHeadersTest::label11InTheProtectedBucketIsRejected,label11InBothBucketsIsRejected,label12InTheProtectedBucketIsRejected; the rawgetProtectedHeaderParameter(11)stays lenient, as fortyp.Also from the issue: the context rule encoded once (
Countersign::context()); per-target derivation for the seven targets of §2 plusCOSE_Signof §3.3; deterministic encoding reused fromSignature1(CoseStructure); "only algorithms with appendix" in theCountersignerdocblock; the §6 tag-length warning where the encrypt/MAC targets are documented (README,doc/Usage.md,Countersignerdocblock, example 15).Tests
tests/CoseWg/— the harness verifies countersignatures now:CoseWgParty::countersigners()/countersigners0()read thecountersign/countersign0blocks of a party,CoseWgFixture::requiredAlgorithms()includes them,CoseWgFixtureTestCase::assertCountersignatures()walks the message, its signers and its recipients (nested included).Rfc9338FixtureTest(18 tests).CoseWgHarnessTestgains the deprecation check over the 22 vendored files andaBrokenCountersignatureFailsTheSuite(a flipped byte, and a countersigner declared but absent).tests/Signature/CountersignTest.php— the four contexts,sign_protectedomitted,other_fieldsas 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, aCOSE_Signaturerefusing detached content, an unknownAbstractCoseTagrefused,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 aCOSE_Sign),attach()one → array → append with a tagged entry kept tagged and read back as aGenericTag,tagged(), thealgmismatch on sign and on verify, textalg, headers withoutalg.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.RfcReferencesTestknows RFC 9338;ExamplesTestruns 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 twoCountersign_structurerows 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.jsonkeywords.Note
The working tree of
4.9.xon my machine has an uncommitted split ofdoc/Usage.mdintodoc/*.md. This PR documents indoc/Usage.mdas committed on4.9.x; the Countersignatures section will need to move intodoc/Signing.md(or wherever the split puts it) when that lands.Checks
Rebased on
4.9.xafter #220 (hash envelope, RFC 9995): the example is15-countersignatures.php, #220 having taken 14; README,doc/Usage.md,CoseHeadersand 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 22countersign*/files andRFC8152/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
Tagsubclass for tag 19 (Spomky-Labs/cbor-php#170 —GenericTagcarries the same bytes andcountersignatureItems()reads the number from the head, so the dedicated class needs no change here); aManager-resolvingverify()(the application resolvesalgfrom the countersignature's headers, asexamples/15shows).