Repository navigation
fix(libSceLibreSsl3): decode the algorithm and key bits in X509_get0_pubkey - #25654
Conversation
…pubkey X509_get0_pubkey passed the BIT STRING contents of the X509_PUBKEY to d2i_PUBKEY as if they were a whole SubjectPublicKeyInfo, so a certificate whose key was not cached yet, such as one loaded by X509_STORE_load_locations, gave a null key. LibreSSL X509_PUBKEY_get0 decodes the key from the algorithm identifier and the key bits, ignoring the unused-bits count. Re-encode both as a SubjectPublicKeyInfo and decode it with d2i_PUBKEY. The key is now cached on certificates decoded by the project, so free it with the X509_PUBKEY, as LibreSSL does, instead of leaking it when the certificate is freed. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
PrivacyOnion
left a comment
There was a problem hiding this comment.
Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against 4d5bc3a521:
- Conventions:
tools/check_conventions.pypassed. - Build:
guest_libressl3_pkcs7_tests,guest_libressl3_x509_public_key_accessors_testsbuilt cleanly. - CTest (
guest_libressl3_x509_public_key_accessors,guest_libressl3_pkcs7):1/2 Test #4771: guest_libressl3_x509_public_key_accessors ... Passed 0.02 sec 2/2 Test #4772: guest_libressl3_pkcs7 ....................... Passed 0.02 sec 100% tests passed, 0 tests failed out of 2 - Negative control: with
core/libs/prx/libSceLibreSsl3/src/X509PublicKeyAccessors.cpp,core/libs/prx/libSceLibreSsl3/src/X509StoreLoadLocations.cppreverted tomainand the new tests kept, the suite fails:Restoring the change makes it pass again.1/2 Test #4771: guest_libressl3_x509_public_key_accessors ...Subprocess aborted***Exception: 0.99 sec 2/2 Test #4772: guest_libressl3_pkcs7 .......................Subprocess aborted***Exception: 1.30 sec 0% tests passed, 2 tests failed out of 2 - Code reading:
- Verified SubjectPublicKeyInfo DER encoding in EncodeSubjectPublicKeyInfo (tag 0x30 SEQUENCE wrapper, AlgorithmIdentifier SEQUENCE with OID and parameters, and tag 0x03 BIT STRING with zero unused-bits prefix) matches RFC 5280 and d2i_PUBKEY requirements.
- Verified error handling and memory management in DecodeEncodedKey: cursor parsing errors, race conditions where another thread cached the key first, and read-only guest memory pages properly release decoded EVP_PKEY and preserve errno.
- Verified parameter parsing in AppendParameter across ASN.1 BOOLEAN (tag 0x01), NULL (tag 0x05), OBJECT IDENTIFIER (tag 0x06), constructed types (SEQUENCE/SET), and unsupported tags throwing NotImplemented_nid_no_patch.
- Verified resource cleanup in FreePubkey in X509StoreLoadLocations.cpp correctly calls EVP_PKEY_free_nid_disambig1_nid_postfix on pubkey->pkey without leaking memory or crashing on null pointers.
- Verified LP64/LLP64 portability, null checks, and bounds safety across AppendGuestBytes, AppendElement, and CopyInPreservingErrno.
- Verified strict AGENTS.md compliance: no comments introduced in source code, technical debt fragment present in docs/dev/debt/changes/, and conventions pass mechanically.
Looks Good To Me
MarcZX10
left a comment
There was a problem hiding this comment.
Reviewed — looks good
The change fixes public key decoding by rebuilding SubjectPublicKeyInfo from the algorithm and key bits; no blocking defect was verified in the code read.
What I ran
Read the prepared description, commits, manifest, diff, and changed source files at head 4d5bc3a52155eaa19d12d8c46d30223874c41a8d. No tests or other commands were run.
Results
| Check | Result | Note |
|---|---|---|
| Changed source review | passed | Core encoding and cleanup paths read |
| Tests | not run | Results in the description are author-reported |
| Remaining changed files | not run | Test and debt files not read in full |
Findings
Blocking
None.
Non-blocking
None.
Not verified
The changed test files and debt fragment were not read. Console, Windows, and macOS behavior was not verified. The author-reported test results were not independently run.
To get this to a good state
Nothing is required.
throny
left a comment
There was a problem hiding this comment.
Review: ✅ correct
Basis: code read; compared with LibreSSL ce488b78 (the revision the debt file links), x509/x509_cmp.c and asn1/x_pubkey.c
Checked
- On
main,DecodeEncodedKeyfed the BIT STRING contents tod2i_PUBKEYas if they were a whole SubjectPublicKeyInfo, whileDecodePubkeystores only the key bits inpublicKeyand the algorithm inalgor, soX509_get0_pubkeyreturned null for certificates loaded throughX509_STORE_load_locationsor PKCS7. - Now it re-encodes algorithm + key bits as an SPKI and caches the result in the
X509_PUBKEY, returning an already-cached key, asX509_PUBKEY_get0does (x_pubkey.c:185-229). The key is borrowed and freed inFreePubkey; the PKCS7 test's empty-allocation check covers leaks. - Tests (EC P-256, RSA 512/1024/2048, unused-bits flags, null algorithm, read-only memory) fail on
main.
Findings
- For a malformed key, the error queue holds what
d2i_PUBKEYqueues (ASN1/EVP codes); LibreSSL pushesX509_R_UNSUPPORTED_ALGORITHM/X509_R_PUBLIC_KEY_DECODE_ERROR. Covered by the debt entry's "compared with the console" condition; not blocking.
Suggested action: merge.
AI-assisted review (Claude Code) · head 4d5bc3a521 · base main 337a948bf1
What
X509_get0_pubkeypassed the BIT STRING contents of the certificate'sX509_PUBKEYtod2i_PUBKEYas if they were a whole SubjectPublicKeyInfo and ignored the algorithm. The BIT STRING of a SubjectPublicKeyInfo holds only the key itself (anRSAPublicKeyor an EC point), so every certificate whose key was not cached yet returned null. This library's ownX509_STORE_load_locationsfillsalgorandpublic_keythat way and leavespkeynull, so no certificate it loads had a public key.LibreSSL
X509_PUBKEY_get0picks the key method from the algorithm OID and calls itspub_decodeonpublic_key->data, without looking at the unused-bits count, then caches the key (3.4.1,x_pubkey.cl.178-222, the pinned submodule; 4.0.0 l.185-228 is the same). The function now re-encodes the algorithm identifier (OID and parameter) and the key bits, with no unused bits, as a SubjectPublicKeyInfo and decodes it with this library'sd2i_PUBKEY, so both entry points share one decoder. Caching, the other-thread check and errno handling are unchanged. Because the key is now cached on certificates that this library decodes (X509_STORE_load_locations,d2i_PKCS7),FreePubkeyinX509StoreLoadLocations.cppfrees it with theX509_PUBKEY, as LibreSSL'sASN1_OP_FREE_POSTcallback does; without that, every certificate read byX509_get0_pubkeyleaked its key when freed.BOOLEAN, NULL, OBJECT, SEQUENCE, SET and "other" parameters and the ASN.1 string types up to 30 are encoded; any other parameter type, a null algorithm, null parameter data and unreadable guest memory throw. A null algorithm object is encoded as an empty OID, which gives the unsupported-algorithm errors that
OBJ_obj2nid(NULL)gives in LibreSSL. The pending debt fragment ofX509_get0_pubkeysaid the field was assumed to be a complete SubjectPublicKeyInfo; it is rewritten in place, becausedebt_fold.pyrefuses a second fragment for the same entry and asks to merge them, and now lists thed2i_PUBKEYrefusals that this path inherits.Tested
Ubuntu 26.04 x86-64 container under Rosetta on macOS 27.2, GCC 15.2,
main0eb40875597. A harness gives the same SubjectPublicKeyInfo to LibreSSL 3.4.1 built from the3rdparty/libressl-openbsdsubmodule (d2i_X509_PUBKEY, thenX509_PUBKEY_get0) and to the module (the sameX509_PUBKEYbuilt by hand, with the last byte masked asc2i_ASN1_BIT_STRINGdoes, in a certificate, thenX509_get0_pubkey), and compares null or key, base type,EVP_PKEY_bits, the cached pointer and the error queue. Both sides also rund2i_PUBKEYon the original DER. Inputs: 20 keys from OpenSSL 3.5 (RSA 512 to 3072 with e = 3 and 65537; P-256, P-384, P-521, P-224 and secp256k1, named and explicit; X25519; Ed25519), each with absent, NULL and P-256 parameters and with 3 unused bits, then random mutations of the key bits, OID and parameter (seed 1101: 3 000 inputs; seed 1102: 4 000; seed 1103: 4 000, which also draws BOOLEAN, INTEGER, OCTET STRING, UTF8String and PrintableString parameters).maind2i_PUBKEYalready differs from 3.4.1mainand no longer matchThe inputs left are
d2i_PUBKEYdecisions, counted in this order for seeds 1101 / 1102 / 1103: an RSA key with a parameter other than NULL (105 / 125 / 165), bytes after theRSAPublicKey, whichrsa_pub_decodeignores (111 / 197 / 162), explicit EC parameters (52 / 73 / 64), P-224, P-521 and secp256k1 (29 / 34 / 44), EC keys for which this library'sEVP_PKEY_bitsreturns 0 (30 / 36 / 43), RSA-PSS (1 / 4 / 1) and other RSA (1 / 1 / 2). Every error queue that still differs from 3.4.1 (2 130 / 2 875 / 2 951 inputs) also differs ford2i_PUBKEY, mostly by the nested ASN.1 entries this library does not queue.End to end, four self-signed certificates (RSA 2048, RSA 1024 with e = 3, P-256, P-384) and a bundle of two, loaded with
X509_STORE_load_locationsand read back from the store:mainreturns null for all six, this PR returns the key with the same type as 3.4.1 and the same RSA bit counts.guest_libressl3_x509_public_key_accessorsnow builds theX509_PUBKEYas LibreSSL lays it out: P-256 with its curve OID, RSA 512 with a NULL parameter and 3 unused bits, RSA 1024 and 2048 (long-form lengths), EC with a BOOLEAN parameter, RSA 512 with an undefined (-1) BOOLEAN parameter, a whole SubjectPublicKeyInfo in the key bits, Ed25519 and a null algorithm object (null), a null algorithm (throws), and the earlier invalid, negative-length, null-data and read-only cases. It fails with themainsource, when the unused-bits count is copied into the re-encoded BIT STRING, when long-form lengths are written as0x81or as short form below 256, when a BOOLEAN parameter throws, and when an undefined BOOLEAN is encoded.guest_libressl3_pkcs7now reads the key of the decoded signed message's certificate twice (same pointer) beforePKCS7_freeand still requires every allocation to be freed; it fails when the newEVP_PKEY_freeline inFreePubkeyis removed. The 98libSceLibreSsl3tests and the 92public_headers, registration and progress tests pass, apart from the failuresmainalready has under Rosetta.Not tested on a console, on Windows or on macOS.
#25081, open, changes how
d2i_PUBKEYdecodes RSA keys (ParseAlgorithmandDecodeSpkiinKeyDer.cpp); this path goes through the same decoder and touches a different file.python3 tools/check_conventions.py --base upstream/mainpasses.python3 tools/pr_overlap.py --names X509_get0_pubkeyfinds no open pull request on the function.python3 tools/pr_overlap.py --branchcannot compare #25082, which conflicts with the base branch and sharesX509StoreLoadLocations.cpp; its diff changes the certificate file loader and its error queue and does not touchFreePubkeyor the public key accessors.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)🤖 Generated with Claude Code