Repository navigation
feat(keys): COSE Key Thumbprint (RFC 9679) and compressed EC2 points (#197) - #211
Merged
Merged
Conversation
…197) Cose\Key\Thumbprint computes the RFC 9679 thumbprint over a COSE_Key rebuilt from the required parameters of the key type in the deterministic encoding of RFC 8949 section 4.2.1, with the section 5.7 URI for the hashes the IANA Named Information registry names. Ec2Key accepts y as the sign bit of a compressed point and decompresses it on load for all eight curves; PublicKeyLoader reads a compressed subjectPublicKey the same way.
15 tasks done
5 of 8 tasks
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 #197 (Closes #197)
Part of #202
What
The COSE Key Thumbprint of RFC 9679, on top of the hash registry of #195 (PR #210), and the compressed EC2 point form it needs. Everything is additive; no existing public signature changes and a key carrying
yas a byte string is handled exactly as before.Cose\Key\ThumbprintThe issue offered a class or a
Key::thumbprint()method; this is the class, as a small value object, with nothing added toKey.Thumbprint::of(Key $key, Hash $hash = new SHA256())Thumbprint::canonicalForm(Key $key): stringvalue(): stringkid, or the value of ackt(§5.6)hash(): Hashequals(string $thumbprint): boolhash_equals()toUri(): stringurn:ietf:params:oauth:ckt:<hash>:<base64url, unpadded>(§5.7)Thumbprint::URI_PREFIXurn:ietf:params:oauth:cktkty/crv/x, EC2kty/crv/x/y, RSAkty/n/e, Symmetrickty/k— written by a private 40-line encoder (integers and lengths in shortest form, keys sorted bytewise on their encodings, RFC 8949 §4.2.1). No runtime dependency on cbor-php, which staysrequire-dev; the test suite cross-checks the encoder againstCBOR\MapObjectfor every key type, including a Brainpoolcrv(two-byte argument) and an RSA modulus (two-byte length).ktyandcrvare always the registry integers, whatever the key names them under ('EC','EC2','2','P-256', …). Theyof an EC2 key is always the uncompressed coordinate (§4.2). Thexof an OKP private key that carries none is the recomputed one, so ad-only Ed25519 key has the thumbprint of its public key.Hash, notFilterOnlyHash: SHA-1 and SHA-256/64 are refused at the type level, as the issue asks. Default SHA-256 (§3: "MUST be supported").sha-256,sha-384,sha-512. SHA-512/256, SHAKE128 and SHAKE256 have no entry there, sotoUri()throws for them;value()works for all six.Keyof another type (HSS-LMS 5, AKP 7) is refused with the type in the message; §4.6 defers those to their own specifications, and RFC 8778 is out of scope of the milestone.doc/Usage.mdand in the example.Compressed EC2 points
RFC 9053 §7.1.1 lets a public EC2 key carry
yas the sign bit of the point, a CBOR boolean;Ec2Keyused to reject it as an "invalid type", and RFC 9679 §4.2 requires the thumbprint to be computed over the uncompressed point anyway. So:Ec2Keyacceptsyasbooland decompresses on load: y = (x³ + ax + b)^((p+1)/4) mod p, a singlemodPowthroughCose\BigInteger/ brick/math, valid because all eight supported curves have p ≡ 3 (mod 4). The candidate is squared and compared with the right-hand side, so anxthat lies on no point is rejected (Invalid EC2 key. The compressed point is not on the curve); anx≥ p is rejected too (SEC 1 §2.3.4 step 2.2); the β = 0 corner case (whose only other root is 0) is handled rather than returning p. The sign bit is the parity of y (SEC 1 §2.3.3: true = odd).y(),getUncompressedCoordinates()andasPEM()return the coordinate whatever form was supplied — so signing and verifying with a compressed key just works;getData()keeps the boolean, so the map round-trips unchanged.p,a,b) of the eight curves are constants onEc2Key, from SEC 2, FIPS 186-4 and RFC 5639; I checked every one of them againstopenssl ecparam -param_enc explicit, and the test suite checks them the practical way (below).PublicKeyLoaderreads a compressedsubjectPublicKey(0x02/0x03, RFC 5480 §2.2), which it used to refuse by name, and hands back a key carrying the uncompressed point. The hybrid form (0x06/0x07) is still refused.Cose\BigInteger(internal) gainsshiftRight(),isOdd()andisZero().Cost: ~0.1–4 ms per compressed key on the QA image (GMP); brick/math's pure-PHP calculator is slower but this is a one-time cost at load.
Acceptance criteria of #197
ThumbprintTest::theExampleOfRfc9679IsReproduced,examples/12-key-thumbprint.php); the §5.7 URIurn:ietf:params:oauth:ckt:sha-256:SWvYr63zB-WwjGSwQhv53AFSijRKQ72oj63RZp2iU-wtookid,alg,key_ops,d), inkty/crvas text vs int vs numeric string, or inyasboolvsbstrproduce the same thumbprint (everyRepresentationOfAKeyHasTheSameThumbprint, 8 representations)ythat does not yield a point on the curve is rejected on load (anXThatIsOnNoPointOfTheCurveIsRejected: the smallest off-curve x of each of the 8 curves, both sign bits)aPrivateKeyHasTheThumbprintOfItsPublicKey: P-256, secp256k1, brainpoolP256r1, Ed25519, Ed448, RSA; plus thed-only OKP case)doc/Usage.md,examples/updatedTests
tests/Key/ThumbprintTest.php(11 methods, 30 cases): the RFC vector; the representations; the canonical form vs cbor-php for OKP / EC2 / EC2-Brainpool / RSA / Symmetric; private = public;d-only OKP; SHA-384 / SHA-512 with their URI names; SHA-512/256 / SHAKE refused a URI; SHA-1 refused as aTypeError;equals(); unknownkty; two keys sharingxdiffer.tests/Key/Ec2KeyCompressedPointTest.php(11 methods, 51 cases): for each of the 8 curves, a fresh OpenSSL key loaded fromx+ parity equals the OpenSSL key (y(), DER, PEM,getData()keeps the bool) — which is what validates the curve constants; the opposite sign bit gives the other root; a named curve; a compressed private key signs with ES256 and its public half verifies; off-curvexper curve;x≥ p; wrongxlength;yas int/float/array still refused; compressed SPKI loaded for each curve and equal to the uncompressed load; off-curve SPKI refused; hybrid SPKI refused. Curves this OpenSSL build lacks skip.RfcReferencesTestknows RFC 9679;ExamplesTestrunsexamples/12-key-thumbprint.php.Docs
README (This library implements, Features, Documentation, Key Parameter Forms for the sign bit, new Key Thumbprints section with the §4 table),
doc/Usage.md(same, plus TOC and References; the examples table also gains the missing rows for examples 10 and 11),RELEASES.md(4.8.x → 4.9.x),composer.jsonkeywordRFC9679,examples/12-key-thumbprint.php+ index. One stale PHPStan baseline entry (Ec2Key::y()returning mixed) removed sincey()now returns a typed property.Checks
phpstan(level max),ecs,rector,deptrac,parallel-lint,composer validate --strict,dump-autoload --strict-psr, workflow audit: clean. PHPUnit: 2318 tests on the 8.4 image (194 skipped, unchanged: the key-management fixtures of #201), 2313 on 8.2 (215 skipped, unchanged); none of the 81 new tests skips on either.Not in this PR
HSS-LMS (
kty5, §4.5) and AKP (kty7) thumbprints, which need their key classes (RFC 8778 and RFC 9964, out of scope of #202); parsing a thumbprint URI back into its parts;x5t(#196), which #201 needs next to this.