feat(libSceLibreSsl): implement d2i_X509 and X509_free #13266
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/13266"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by JaceTheBlu on 2026-10-30T10:28:57Z.
What
Implements
d2i_X509andX509_freein libSceLibreSsl. This is stacked on #11359, and through it #11129. Merge those first; this PR's own diff is then only the decoder, its tests and its debt entry.src/X509.cppports the LibreSSL 2.5.4 template decoder (tasn_dec.c,tasn_new.c,tasn_fre.c), limited to the items anX509holds:X509_CINF, which keeps the encoding of the signed part;ASN1_TYPEparameter;x_name.cextern functions: the DER cache, entries with their RDN index, and the canonical encoding;It follows LibreSSL closely:
X509_freehonours the reference count.x509_cbsetsex_pathlento -1 and caches the one-line subject with theX509_NAME_onelinefrom #11359.The error queue gets the same codes, in the same order, as LibreSSL.
Api.hppnow has the full 176-byteX509and theX509_CINF,X509_ALGOR,X509_VAL,X509_PUBKEY,X509_EXTENSION,ASN1_TYPEandASN1_ENCODINGlayouts. They were checked withoffsetofagainst the 2.5.4 headers. The existingreferencesoffset (28), whichX509_STORE_CTX_get1_chainrelies on, does not change.There is a new entry in
docs/dev/debt/libSceLibreSsl.md, and the pending count goes from 288 to 286:Field=/Type=error data is not kept, because the queue holds codes only.EVP_PKEYthrows. No export creates them yet.X509_get_pubkeyis not part of this PR: it needsEVP_PKEY/RSA decoding.Tested
Debian container, GCC 14.2, full Release build of head
da60c57806:ctest -R 'libressl|crypto_memcmp'passes 38 of 38.tools/check_test_registration.pyandpython3 tools/check_conventions.py --base upstream/mainpass.guest_libressl_x509decodes a hand-built certificate and checks:guest_libressl_x509_allocatorfails each guest allocation of a decode in turn and checks that nothing leaks and that the decode either fails with an error or succeeds.guest_libressl_oracle(ANYPS5_LIBRESSL_ORACLE) now also builds random certificates and decodes each one with both implementations. The certificates:ASN1_TYPEparameters of every class, optional fields, extensions and odd bit counts;For each case it compares every decoded field, the consumed length, the error queue and, for decodes into an existing certificate, what happened to the pointer. Results:
AES_set_*_keyschedule cases already described in the debt file. They come from the reference building aes-x86_64.Windows: see the run in the comments. Not tested on a title or on a console.
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
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-30T10:40:02Z.
Review: CHANGES_REQUESTED
Reviewed — changes requested
The required pull request template sections are missing, so the repository’s Conventions check fails on this head.
What I ran
Reviewed head:
da60c5780644cd413f491df03d3f96dd15ec6c83. The PR targetsmainfrom basee198fd3cdc2290c579522d032e24a26e549cc6c4; a local merge preview against currentmain3d19f3fed31be38d7196ea1bd74e29ab6cc3c9a4merged cleanly. Environment: Linux x86_64, Python 3.11.2, Git 2.39.5, and Debian GCC/G++ 12.2.0. I inspected the decoder, certificate/name ownership paths, API declarations, tests, CMake registration, contribution rules, and the hosted check snapshot; I did not compile or execute the C++ tests.Results
### What,### Tested, and### Checklistheadings are absent.git diff --checkemitted no findings.b3104fa1712c12e9a7a0b5b75564f7c82d08a45a.core/libs/prx/libSceLibreSsl/CMakeLists.txt.Unedited check output excerpts
Findings
Blocking
### What,### Tested, and### Checklist, retaining the applicable checklist items. Contribution rules at this PR headNon-blocking
Not verified
To get this to a good state
Restore the three required template headings and keep the existing dependency and tested-work details in the corresponding sections. I will rerun the same checks on the updated head; build-dependent behavior will remain unverified unless an applicable hosted run supplies that evidence.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-31T10:35:37Z.
Review: APPROVED
Re-checked — new head, looks good
At head
d865e35d15db1eb3ba5399b47bc80456bb1f581e, the template issue from my earlier review is fixed, and I found no source blocker.What I ran
Reviewed head
d865e35d15db1eb3ba5399b47bc80456bb1f581eagainst basec3c45d73abd34dca81da6bad87cb6d36ea437a66; its merge preview was clean against currentmainatfd31320e83fe417fa9c7fef792613653d9eb4f02. Environment: Linux x86_64, Python 3.11.2, GCC 12.2.0. No local build or C++ tests were run.Results
mainConvention-check stdout (verbatim)
Findings
Blocking
None found in the reviewed decoder and ownership paths.
Non-blocking
The stack scan reports that the latest head of dependency #11129 is not included, although #13266 is clean against current
mainand its declared dependency #11359 is included. The PR already says to land its dependencies first; refresh the stack after those dependencies land and review the resulting head.Sources
Not verified
I did not build the current head or run CTest/oracle locally. Console behavior is unmeasured, and the hosted Linux and specialized jobs were skipped.
Imported from boykopovar/AnyPS5. Originally posted by XairRrR on 2026-11-03T23:14:30Z.
Review: APPROVED
Reviewed — approved
The
d2i_X509/X509_freeport merges and builds cleanly, and all the LibreSSL tests pass. The new tests catch a stubbed decoder and targeted mutations, and the parts of the port I compared with LibreSSL 2.5.4 match it. I found nothing blocking.What I ran
Head
f51c1c80e3074d9c0b14a3adc24ba4da5c01fef0merged with--no-ffintomain1c8c0efc8c41314402e6966da5bef6ea3d639d10in a clean worktree. Environment: Debian 13 VM with 32 vCPU (Xeon Gold 6136), GCC 14.2.0, Release, Ninja,BUILD_TESTING=ON.Results
git diff --checkcheck_conventions.pyAll conventions checks passed.libssucceededguest_interfaces,guest_socket_options_multicast,unwrap_self_…,vector_constant_factor_tininess) also fail onmain. Theguest_net_ctlskip also happens onmain.ctest -R libresslguest_libressl_x509,_x509_allocator,_x509_name,_x509_name_oneline,_x509_name_oneline_allocator) takes about 0.01 s.NotImplemented_nid_no_patch(__func__)stubs left inExport.cppNegative control
Restoring
main's production files is not a usable control: the tests use the newApi.hpplayouts and signatures, as JJenkx already observed. I used these instead:src/X509.cppwithd2i_X509/X509_freethat callNotImplemented_nid_no_patch, which ismain's behaviour. Bothguest_libressl_x509andguest_libressl_x509_allocatorabort withd2i_X509_nid_postfix not implemented.BitStringFromContent(src/X509.cpp:585).guest_libressl_x509fails withthe signature keeps its unused bit count and clears the unused bits.ItemFree(&internal, NameInternalItem)from theNameDecodefailure path (src/X509.cpp:1714).guest_libressl_x509_allocatorfails withnothing leaks.ItemFree(&element, …)after a failedStackPush(src/X509.cpp:1309).guest_libressl_x509_allocatorstill passes; see the second non-blocking finding.Afterwards I restored the merged
src/X509.cppand rebuilt. Both tests pass andgit status --porcelainis empty.Findings
Blocking
None.
Non-blocking
CMakeLists.txt:394,guest_libressl_x509andguest_libressl_x509_allocatorgetTIMEOUT 60. CONTRIBUTING says each test finishes in 30 s and that a test is shrunk rather than given a longer timeout. Both run in about 0.01 s here, so 30 (or the default) is enough. Suggested fix: useTIMEOUT 30, like the other tests in this file.StackPushonly reallocates when a stack reaches 4 entries. The certificate in the allocator test has at most 3 entries in any stack (issuer sets, extensions). The cleanup atX509.cpp:1307-1311is therefore never exercised: mutation 4 above leaks and the test still passes. The code is correct as written. Suggested fix: add a case with four or more extensions or name entries to the allocator run.Export.cppactually go from 231 to 224: five X509_NAME exports come from the stacked #11129/#11359, plus these two.On the existing discussion
3rdparty/is still waiting for a maintainer's answer, and I don't settle it here. One new data point:mainalready has the3rdparty/libressl-openbsdsubmodule (pinned at078b3d1df8ca, which contains the 2.5.4 commit2e2c612066aa).ErrorQueue.cmakereads it at build time. I used it to compare the port with the 2.5.4 sources. If maintainers want the3rdparty/route, the source is already there.2e2c612066aa:CertificateCallbackmatchesx509_cb. In 2.5.4 there is noASN1_OP_D2I_PREcase, so a reused certificate keepsvalid,ex_flagsandex_pathlen, as the port does.FREE_POSTreleases the fields the port refuses to free (it throws instead).asn1_ex_i2cdoes the same, because it handles onlyV_ASN1_INTEGER/V_ASN1_ENUMERATED.Not verified
guest_libressl_oracle(ANYPS5_LIBRESSL_ORACLE) is off in this build. It fetches LibreSSL 2.5.4 at configure time and the sandbox has no network, so I did not reproduce the 60,000-case and 600,000-case runs.X509_NAME_onelineonly whered2i_X509uses it, for the cachedname.da60c57806.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-14T06:16:36Z.
Review: CHANGES_REQUESTED
Reviewed — changes requested
Changes are required before merge: reject negative input lengths before using them as buffer bounds.
What I ran
Reviewed head
06d66e4b08ae5468a2bada43bd2d6927a06ec42b. I inspected the prepared diff and all eight changed files; no commands or tests were run in this read-only review.Results
Findings
Blocking
core/libs/prx/libSceLibreSsl/src/X509.cpp:1864-1869:d2i_X509validates null pointers but not a negativelength. The decoder passes it toGetObject, which only checks!maxbefore dereferencing the input and later uses the negative value in pointer bounds arithmetic (core/libs/prx/libSceLibreSsl/src/X509.cpp:885-887, 916). A caller can therefore trigger an out-of-bounds read or invalid pointer arithmetic by supplying a negative length. Return failure for negative lengths before entering the decoder.Non-blocking
None.
Not verified
Build, tests, Windows behavior, and console behavior were not run. The author reports Linux test and oracle runs in the description; those results were not independently verified. Read:
core/libs/prx/libSceLibreSsl/CMakeLists.txt,core/libs/prx/libSceLibreSsl/Export.cpp,core/libs/prx/libSceLibreSsl/include/Api.hpp,core/libs/prx/libSceLibreSsl/src/X509.cpp,core/libs/prx/libSceLibreSsl/tests/Oracle.cpp,core/libs/prx/libSceLibreSsl/tests/OracleReference.cpp,core/libs/prx/libSceLibreSsl/tests/X509.cpp, anddocs/dev/debt/libSceLibreSsl.md. No changed files were excluded.To get this to a good state
Reject negative input lengths before parsing, then rerun the decoder tests and the reported build checks on the updated head.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-16T11:17:15Z.
Review: COMMENTED
Reviewed — comments
This review inspected all eight changed files at head
4c4fe904879c5615aa342815ecac2868f82c22b5; no blocking defect was verified, but the changed API is not connected to the library's export registry.What I ran
Read the prepared description, commit list, diff, manifest, and full text for every file listed as reviewed. Inspected related library export configuration. No build or tests were run in this review; test results in the description are author-reported.
Results
Export.cppFindings
Blocking
None.
Non-blocking
d2i_X509_nid_postfixandX509_free_nid_postfix, but neither is present inExport.cpp. Please confirm these functions are exposed through the generated library export table and add the required registrations if they are not; otherwise callers resolving the PRX exports will not reach the implementations. Changed declaration lines:core/libs/prx/libSceLibreSsl/include/Api.hpp:995andcore/libs/prx/libSceLibreSsl/include/Api.hpp:1009.Not verified
No tests were run. The full text of all eight files marked included in the manifest was read:
core/libs/prx/libSceLibreSsl/CMakeLists.txt,core/libs/prx/libSceLibreSsl/Export.cpp,core/libs/prx/libSceLibreSsl/include/Api.hpp,core/libs/prx/libSceLibreSsl/src/X509.cpp,core/libs/prx/libSceLibreSsl/tests/Oracle.cpp,core/libs/prx/libSceLibreSsl/tests/OracleReference.cpp,core/libs/prx/libSceLibreSsl/tests/X509.cpp, anddocs/dev/debt/libSceLibreSsl.md. No files were excluded by the manifest. Export generation/registration outside these files was not confirmed.To get this to a good state
Nothing is required. Optional: verify the generated export table includes both functions and add registration if needed.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-17T13:32:56Z.
Review: COMMENTED
Reviewed d2i_X509 and X509_free — no blocking findings
Nothing blocks this; the points below are optional.
What I ran
89b23f5a5f, base main.diff.patch) andmanifest.json,files.json,description.mdandcommits.mdfrom the review folder.repo_read.py search:X509_NAME_oneline_nid_postfix, and the callers ofX509_free_nid_postfix.Results
X509_NAME_onelineused by the decoderCommands
Findings
Blocking
None.
Non-blocking
core/libs/prx/libSceLibreSsl/CMakeLists.txt:806registersguest_libressl_x509_derandguest_libressl_x509_der_allocator(line 807). The description namesguest_libressl_x509andguest_libressl_x509_allocator. Either the description or the registration should change so that a reader can find the tests by the names given.Not verified
ctest -R 'libressl|crypto_memcmp'(38 of 38) and the 600,000-case oracle run are the author's report. I did not run them.X509NameOneline.cpp, which the decoder calls, so those PRs may already be in main. I did not check their merge state.docs/dev/debt/libSceLibreSsl.md(288 to 286) is not checked; the diff only adds one entry line.X509.cppandApi.hpp, and the definitions ofLibreSslX509Name,LibreSslX509NameEntry,LibreSslExData,LibreSslStackand theError.hppandObjectTable.hppincludes. I read these only through the diff.files.jsonunderreviewedwere read through their diff hunks. None were excluded.To get this to a good state
Nothing is required. The naming point above is optional.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-21T13:54:59Z.
Review: DISMISSED
Reviewed — changes requested
Changes are required before merge: reject lengths that overflow the constructed-string accumulator before converting or allocating.
What I ran
Reviewed head
4d13d0448d3a2a1ad46ee6668f04e00a50647b8fagainst the prepared diff. No build or tests were run. The author reports a Debian/GCC 14.2 Release build and 38 passing tests, plus a 600,000-case oracle run; these were not run during this review.Results
Findings
Blocking
core/libs/prx/libSceLibreSsl/src/X509.cpp:1021-1026convertsbuffer->lengthtointand adds the untrusted decodedlengthusing signed arithmetic, then casts the result tosize_tforGrow. A constructed ASN.1 string can contain enough segments to exceedINT_MAX; the conversion or addition can wrap, causing an undersized buffer allocation followed bymemcpyof the full segment length. Keep the accumulator in a checked unsigned/wide type and reject totals that cannot be represented or allocated before growing or copying.Non-blocking
None.
Not verified
core/libs/prx/libSceLibreSsl/CMakeLists.txt,Export.cpp,include/Api.hpp,src/X509.cpp,tests/Oracle.cpp,tests/OracleReference.cpp,tests/X509.cpp, anddocs/dev/debt/libSceLibreSsl.md. All changed files listed in the manifest were included and read; no changed files were excluded.To get this to a good state
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-22T13:28:05Z.
Review: COMMENTED
Reviewed — no blocking findings
The inspected implementation of
d2i_X509andX509_freeappears consistent with the stated goal; a complete review is not justified because the review interface truncated long reads before all changed lines could be inspected.What I ran
Reviewed head
f024b6b00cf0e41d72a0cdf942ccd6cc6dcce160usingrepo_read.pyvia the review reader. No build or tests were run.Results
Findings
Blocking
None in the code inspected.
Non-blocking
None.
Not verified
The review artifacts list eight changed files, all marked included. I read the description, commit list, README, parts of the diff and manifest, and selected ranges of
X509.cpp,Api.hpp,CMakeLists.txt,Export.cpp,tests/X509.cpp,tests/Oracle.cpp,tests/OracleReference.cpp, and the debt file. The reader capped long reads at 400 lines, so I did not inspect every added line in the changed files or all relevant callers. No build, tests, console behavior, or performance checks were verified.To get this to a good state
Nothing is required. Optional: complete inspection of the remaining changed lines and run the reported checks on this exact head before approval.
Imported from boykopovar/AnyPS5. Originally posted by ligmaSec on 2026-12-03T13:25:02Z.
Review: COMMENTED
Built the focused checks against the existing Linux runtime. The
X509fixture passed. The two reference-oracle executables did not build in this standalone setup.AI-assisted: yes.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.