feat(libSceLibreSsl3): implement the SSL_CTX, SSL, SSL_SESSION and X509 retains, the SSL_CTX version bounds, SSL_get_client_random and X509_get_version #12754
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/12754"
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 Lindamea1 on 2026-10-29T07:51:15Z.
What
On
main, libSceLibreSsl3 exports these eight LibreSSL calls as stubs that throw (Export.cppL279-L307, L364-L367, L379-L382). This PR implements them with LibreSSL 4.0.0 behaviour, the version of the firmware module (OpenSSL_version(0)measured in #8981). Names and NIDs are unchanged; the stubs are replaced bysrc/Key.cpp,src/Ssl.cppandsrc/X509.cpp.SSL_CTX_up_ref, itsguest_libressl3_key_referencecase and theLibreSsl3SslCtxstruct ininclude/Key.hppcome from #12578. PrivacyOnion closed that pull request unmerged on 31 October, together with 28 other pull requests of theirs and without a comment. No other open PR implements the call and the version bounds need the struct, so this PR keeps that commit under their authorship and no longer depends on #12578. Its code is unchanged by the rebase; only its debt lines were merged withmain. This PR's own change is the last commit. It appends theSSL_CTXmembers the version bounds need to that struct, with theirstatic_assertlines.int SSL_up_ref(SSL*)lHGGE+Ubycsint SSL_SESSION_up_ref(SSL_SESSION*)MOISxAYBtc0int X509_up_ref(X509*)HrCznvA8AQkint SSL_CTX_up_ref(SSL_CTX*)AmNeqCMs2oUint SSL_CTX_set_min_proto_version(SSL_CTX*, uint16_t)NG+D00RsX1Yint SSL_CTX_set_max_proto_version(SSL_CTX*, uint16_t)mF9+FT4UKSEsize_t SSL_get_client_random(const SSL*, unsigned char*, size_t)1sWluYvAfBMlong X509_get_version(const X509*)gg2cJQtaEnsRetain_nid_no_patchof the mergedEVP_PKEY_up_ref(#11165), moved fromsrc/Key.cppintoinclude/Reference.hpp(inline; the overflow message no longer says "key").SSL_CTX_up_refstays insrc/Key.cpp, next to the mergedEVP_PKEY_up_ref,EC_KEY_up_refandRSA_up_ref, which use the moved helper too. It adds atomically. In 4.0.0,CRYPTO_addisCRYPTO_add_lock(crypto.hL207-L208), which adds under the library's own pthread mutexes and calls no locking callbacks (crypto_lock.cL24-L96). A null object and a count ofINT32_MAXthrow without changing memory, as in the merged retains: upstream dereferences the null object, and its signed add atINT32_MAXis undefined behaviour. BunnyFl's native run of the firmware module for #11165 showed that the module wrapsEVP_PKEYandEC_KEYcounts atINT32_MAX(comment); the SSL and X509 counts were not measured.ssl_version_set_minandssl_version_set_maxonctx->method. Version 0 stores the method bound inmin_tls_version(offset 128) ormax_tls_version(130) and 0 inmin_proto_version(132) ormax_proto_version(134), and returns 1. For a DTLS method (any nonzerodtls), 0xFEFF and 0xFEFD map to TLS 1.1 and 1.2 and every other nonzero value returns 0. A TLS method takes the value as a plain number, so there a DTLS value, numerically above every TLS version, fails as a minimum and is lowered to the method maximum as a maximum. The call returns 0 when the method range is inverted, when the value and the stored opposite TLS bound are inverted, or when the two ranges do not meet. Otherwise a minimum below the method minimum is raised to it, or a maximum above the method maximum is lowered to it. For a DTLS method the result is converted back to the DTLS form, and the call returns 0 if it has none. Then both fields are stored and the call returns 1. A call that returns 0 writes nothing and queues no error. A null context or method throws; upstream dereferences both.SSL_get_client_random: a nulloutreturns 32 without reading theSSL. Otherwise it copiesmin(32, max_out)bytes froms->s3->client_random(s3at offset 56 ofSSL,client_randomat 40 ofSSL3_STATE) and returns the count. A nullSSLthrows. A nulls3throws only when there is something to copy, since a 0-byte copy reads nothing.X509_get_versionreturnsASN1_INTEGER_get(x->cert_info->version)with the 4.0.0 rules, as a 64-bit value on every host, including Windows, wherelongis 32 bits. A null version gives 0. A negative length gives -1 with no error. A type other than 2 or 0x102 gives -1 and queuesERR_PACK(13, 0xfff, 225)(0x0DFFF0E1). Every byte is read, so leading zeros are accepted; a byte that would overflow 64 bits gives -1 with 0x0DFFF0DF (ASN1_R_TOO_LARGE), as does a positive value aboveINT64_MAX. A negative magnitude above 2^63 gives -1 with 0x0DFFF0E0 (ASN1_R_TOO_SMALL). A null certificate orcert_info, and null data with a positive length, throw.X509_get_versionis the first libSceLibreSsl3 export that queues errors (the mergedd2i_ASN1_INTEGERdoes not, debt line 9). It queues throughLibreSslPutError_nid_no_patchinto the libSceLibreSsl queue. On Linux and macOS, theERR_get_errorof a libSceLibreSsl3 importer resolves to libSceLibreSsl by dependency lookup and reads that queue. On Windows, a scoped import searches only the named module and libc (WindowsEntryStubBuilder.cppL171-L200), and the merged #12369 forwards libSceLibreSsl3'sERR_get_errorto libSceLibreSsl (ParentExports.cppL234-L235), so the importer reads that queue there too. #12369's debt entry lists that forwarding as unverified at runtime on Windows, and I have not run it.include/Ssl.hppandinclude/X509.hppdeclare the 4.0.0 prefixes ofSSL_METHOD,SSL,SSL3_STATE,SSL_SESSION,X509andX509_CINF. TheSSL_CTXprefix isLibreSsl3SslCtxininclude/Key.hpp, extended to the version bounds at 128-135; the setters read itsconst void*methodasSSL_METHOD. The fields are fixed-width, so the offsets also hold with the 32-bitlongof MinGW, and every offset used has astatic_assert. No constructor of these objects is implemented in either LibreSSL library (SSL_CTX_new,SSL_newandd2i_X509throw in libSceLibreSsl), so all of them are caller-owned here, as for the mergedEVP_PKEY_up_ref.References, LibreSSL 4.0.0 (
libressl-v4.0.0, ce488b78e0bd):ssl_lib.c:SSL_CTX_up_refL2239-L2243,SSL_up_refL603-L608,SSL_get_client_randomL3092-L3107,SSL_CTX_set_min_proto_versionL3522-L3529,SSL_CTX_set_max_proto_versionL3538-L3545;ssl_sess.c:SSL_SESSION_up_refL883-L888.ssl_versions.cL20-L125: the DTLS and TLS version maps,ssl_clamp_tls_version_range,ssl_version_set_minandssl_version_set_max.ssl_local.hprefixes:ssl_method_stL358-L364,ssl_session_stL410-L437,ssl_ctx_stL665-L709,ssl_stL862-L910,ssl3_state_stL1091-L1095. None of these prefixes has a conditional member.DTLS_client_method_dataL83-L88 andTLS_client_method_dataL286-L291 ofssl_methods.c,SSL3_RANDOM_SIZE(ssl3.hL235),tls1.hL167-L178,dtls1.hL76-L77.X509_up_ref(x_all.cL531-L536),X509_get_version(x509_set.cL102-L107),x509_local.hx509_cinf_stL167-L168 andx509_stL181-L186.a_int.casn1_aint_get_uint64L134-L155,asn1_aint_get_int64L202-L225,ASN1_INTEGER_get_int64L267-L287,ASN1_INTEGER_getL308-L323;asn1.hL92 (V_ASN1_NEG0x100), L97-L98, L1095, L1097, L1116;err.hL160, L244, L271-L273.libressl-v2.5.4, 2e2c61206), the version of libSceLibreSsl: itsX509andX509_CINFprefixes are the same (x509.hL238-L240, L268-L274), and so is itsSSL3_STATEprefix, withclient_randomat 40 (ssl3.hL364-L368), but its other SSL objects are laid out differently. TheSSLandSSL_SESSIONcounts are at 168 and 160, andSSLkeepss3at 48 (ssl.hL435-L461, L806-L853).SSL_METHODstarts with function pointers (ssl.hL399-L406), and the method andSSL_CTXversion bounds are in internal structs (ssl_locl.hL377-L381, L434-L436).The code is written from the behaviour above; no upstream source is copied.
Gaps, as three new entries in
docs/dev/debt/libSceLibreSsl3.md:SSL_CTX_up_ref, #12578's entry: the console layout and retain/release interoperability are unverified. The count at offset 40 follows 4.0.0, and null contexts and signed-counter overflow, undefined upstream, throw without changing memory.SSL_METHOD,SSL_CTX,SSLandSSL_SESSIONobjects these calls read differently, so every SSL call here would misread a 2.5.4 object once those constructors are implemented. Signed-counter overflow atINT32_MAXand null objects, undefined upstream, throw without changing memory.X509_get_versionqueues its errors in the libSceLibreSsl queue without file or line, while the firmware module keeps its own queue.The pending-export count in the same file goes from 80 to 72.
python3 tools/progress.py --module libSceLibreSsl3goes fromlibraries 12/270 (4.44%)tolibraries 20/270 (7.41%).Firmware evidence: the SSL offsets (
SSL_CTX40 and 128-135,SSL56 and 176,SSL3_STATE40,SSL_SESSION184) would be settled by a native run of the firmwarelibSceLibreSsl3.sprxon module-created objects, like BunnyFl's for #11165 (comment), for exampleSSL_CTX_new(TLS_client_method())followed by these calls. If review wants that first, the X509 pair can go in its own pull request:src/X509.cpp,include/X509.hpp,include/Reference.hppwith thesrc/Key.cppchange, the X509 cases of the test, the third debt entry, and its own lines inCMakeLists.txt,Api.hpp,Export.cppand on line 5 of the debt file.No title is known to import these functions; this is coverage, and it unblocks nothing while the constructors throw. They are TLS client calls of code built against LibreSSL 2.9 or later. LibreSSL 2.7 and 2.8 already export the other seven, but define
X509_get_versionas a macro overASN1_INTEGER_get(2.8.0x509.hL592); 2.9.0 makes it a function (x509_set.cL90-L94).Overlap, checked at 15:36Z on 31 October with this branch against
main474030e584and the 1,670 open PR heads fetched then:gh pr list --searchfinds no other open PR with the eight names or their NIDs, apart from #12945, which carries this branch's commits, and a mention in a comment on #10960.BIO_up_ref) is stacked on this PR and names it onDepends on. It still carries this branch's older commits; replayed on this head, its own commit conflicts only in the debt file.tools/pr_overlap.py --branchlists #10395, #10452, #10490, #10491, #10493, #11146, #12315 and #13897, none with a shared export. It could not compare #9970, #10461, #10557, #10631, #10960, #12928 and #13662, which conflict withmain;git diffof each against its merge base has no changed line with the eight functions. It also lists this PR's previous head and #12945, which carry the same changes.Lines this branch shares with those it could compare:
src/Key.cpp(#10395, #11146);Export.cppL354-L383 andApi.hppL81-L86 (#10491, #10493);CMakeLists.txtL67 and L69 (#12315).#9571 merges with this branch without a conflict. It gives
LibreSslPutError_nid_no_patchan optional file and line, which the three-argument call here still compiles against. It also implements libSceLibreSsl3'sERR_peek_last_erroron the libSceLibreSsl queue, so after it that call also seesX509_get_versionerrors, asERR_peek_erroralready does.Proposed order: this PR before #12945; the others in any order. This PR needs nothing from those, and I rebase it onto each one that lands before it.
Tested
Linux (Gentoo, kernel 7.2.8, AMD Ryzen 7 9800X3D), GCC 16.2.1, Ninja, Release build with
BUILD_TESTING=ON.Current head,
34728c573e, onmain474030e584, with #12578's commit as3e2103d0f3:mainininclude/Key.hpp,src/Key.cpp,GuestLibreSsl3KeyReference.cppand the debt file after #13267 (RSA_up_ref), and #12578 was closed, so I rebased both commits ontomain. Against the previous head0ee29b8d1b, the added and removed lines of both commits are unchanged outsidedocs/. The resolutions keepRSA_up_refandSSL_CTX_up_refside by side in those files and recount line 5 of the debt file. The rebase also drops the blank line that #12578's rebase left at the end of the debt file, andSSL_CTX_up_ref's entry now citesssl_local.hat the 4.0.0 commit its label names. #13659 (EVP_CIPHER_key_length,EVP_CIPHER_iv_length) landed in this library afterwards and rebased without a conflict. Its two exports came from the additional export inventory, so line 5 stays at 72.public_headers,guest_crypto_memcmpand the sevenguest_libressl3_*tests.python3 tools/check_test_registration.py build: passed (1632 test executables, none built and not run).python3 tools/check_conventions.py --base 474030e584, also with this title and description: passed.build/core/libs/libs/libSceLibreSsl3.prx: the eight NIDs are exported at the addresses of their_nid_postfixdefinitions. The inline helper is not among the library's 397 defined dynamic symbols.Export.cpp,Key.cpp,Ssl.cpp,X509.cpp,GuestLibreSsl3Ssl.cppandGuestLibreSsl3KeyReference.cpp: no diagnostics. This is not a Windows build or run.main474030e584: 2524 tests, 0 failed, 894 skipped.guest_libressl3_ssl,guest_libressl3_key_reference,guest_crypto_memcmpand the other fiveguest_libressl3_*tests passed.Previous head,
0ee29b8d1b, on #12578'sced92ec4ed(mainf7f03daf5), with the same code and tests:mainf91137142eand builds everything, but it runs none of the LibreSSL tests.guest_crypto_memcmpamong them). This ran before the last amend, which changed only the debt file.python3 tools/check_test_registration.py build: passed (1579 test executables, none built and not run).python3 tools/check_conventions.py --base upstream/main, also with that head's title and description: passed.build/core/libs/libs/libSceLibreSsl3.prx: the seven NIDs, and #12578'sSSL_CTX_up_ref, are exported at the addresses of their_nid_postfixdefinitions. The inline helper is not among the library's 397 defined dynamic symbols.Export.cpp,Key.cpp,Ssl.cpp,X509.cpp,GuestLibreSsl3Ssl.cppandGuestLibreSsl3KeyReference.cpp: no warnings. This is not a Windows build or run.Earlier head,
48bc460a1d(#12578's00b24c75b4and this commit, onmain319621ba2), with the same code and tests:ctest --test-dir build -R 'guest_libressl3_ssl|guest_libressl3_key_reference|public_headers' --output-on-failure -j8 --timeout 300: 67/67 passed (public_headersand its 64 compile batches,guest_libressl3_key_referencewith #12578'sSSL_CTX_up_refcase on the extended struct and the moved helper, andguest_libressl3_ssl).python3 tools/check_test_registration.py build: passed (1516 test executables, none built and not run).tools/select_tests.py dump,selectwith the changed files,ninja -f select.ninja selected,select_tests.py ctest): 491 of 2746 tests selected, 491/491 passed.python3 tools/check_conventions.py --base 319621ba2, also with that head's title and description: passed.main3a2b91981without conflicts and rebuilt:ctest -R 'guest_libressl3|public_headers', 69/69 passed; CI test selection on the merge, 580 of 2858 tests selected, all five libSceLibreSsl3 tests (withguest_crypto_memcmp) among them, 580/580 passed.guest_libressl3_ssltest (core/libs/tests/GuestLibreSsl3Ssl.cpp):INT32_MAX - 1: the return value, and only the four count bytes at the stated offset change. Then 4 threads × 1000 retains give exactly 4001.dtlsflag of -1, and the opposite bound read from the TLS field. Each case checks the return value and every byte of the guarded context, so a failing call writes nothing.SSL_get_client_randomwith a null output (also with a nullSSL) and with limits 0, 1, 10, 31, 32, 33, 64 andSIZE_MAX: the count, the bytes taken from offset 40 of the state, and an untouched tail. A 0-byte copy with a nulls3returns 0.X509_get_versionon 22 integers (null, empty, negative length, leading zeros, big-endian order,INT64_MAX,INT64_MIN, negative zero, wrong types, each overflow): the value, the exact error code and that nothing else is queued.Ssl.cpp,X509.cppandReference.hppagainst the built libSceLibreSsl, libc and libkernel, and every one aborted. The mutants dropped each clamp condition, read the opposite bound from the proto field, mapped or rejected DTLS values on a TLS method, used a wrong count field, copied the server random, ignoredmax_out, added the 2.5.4 eight-byte limit, changed an error reason or the library, and so on. The unmodified sources passed.build/core/libs/libs/libSceLibreSsl3.prx: the seven NIDs, and #12578'sSSL_CTX_up_ref, are exported at the addresses of their_nid_postfixdefinitions. The library has 88 defined dynamic symbols, the 87 exports andDummyFunction_nid_no_patch, so the inline helper is not exported.x86_64-w64-mingw32-g++16.2.0,-std=gnu++20 -fsyntax-only -Wall -Wextra -D_WIN32_WINNT=0x0602 -I core/libs -I core/libs/nid/include -I core/libs/prx/libkernel) onExport.cpp,Key.cpp,Ssl.cpp,X509.cpp,GuestLibreSsl3Ssl.cppandGuestLibreSsl3KeyReference.cpp, and onKey.hpp,Ssl.hpp,X509.hpp,Reference.hppandApi.hppeach included alone with-Werror: no warnings. This is not a Windows build or run.mainedf6804cf5, Windows 11 Enterprise, WinLibs GCC 15.2.0 UCRT, Ninja, Release,BUILD_TESTING=ON.guest_libressl3_key_referenceandguest_libressl3_sslpassed 3 of 3 withctest --repeat until-fail:3; no negative control, since the build fails with this PR's sources put back tomain's (comment). The pull request's Windows CI job also passed.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