Skip to content

feat(libSceLibreSsl): implement i2d_X509_PUBKEY - #13546

Open
JaceTheBlu wants to merge 46 commits into
boykopovar:mainfrom
JaceTheBlu:feat/libressl-i2d-pubkey
Open

JaceTheBlu wants to merge 46 commits into
boykopovar:mainfrom
JaceTheBlu:feat/libressl-i2d-pubkey

Conversation

@JaceTheBlu

Copy link
Copy Markdown
Contributor

What

Implements i2d_X509_PUBKEY in libSceLibreSsl, on top of the X509 decoder from #13266. This is stacked on #13266, and through it #11359 and #11129. Merge those first; this PR's own diff is then only the encoder, its tests and its debt entry.

src/X509.cpp ports the LibreSSL 2.5.4 template encoder (tasn_enc.c) for the items an X509_PUBKEY holds. It reuses the item tables of the decoder.

  • Sequences, primitives and ASN1_TYPE parameters of every type are encoded, including high tag numbers, and SEQUENCE, SET and OTHER values written as they are.
  • i2c_ASN1_INTEGER handles padding and negative values, as LibreSSL does.
  • i2c_ASN1_BIT_STRING counts unused bits when ASN1_STRING_FLAG_BITS_LEFT is not set, and clears them otherwise.
  • A BOOLEAN parameter of -1 is omitted, and null fields are left out.
  • The three output forms work as in LibreSSL:
    • a null out returns the length;
    • a null *out gets a buffer from the guest heap;
    • otherwise the encoding is written at *out and the pointer advances.

PutHeader, which the canonical name encoding already used, now also takes a tag class.

There is a new entry in docs/dev/debt/libSceLibreSsl.md, and the pending count goes from 286 to 285.

These cases throw instead of being encoded, because nothing reachable from an X509_PUBKEY needs them:

  • names;
  • SET OF and SEQUENCE OF fields;
  • cached encodings.

These cases throw where LibreSSL reads before the data or faults:

  • an all-zero BIT STRING without BITS_LEFT, and a negative zero INTEGER;
  • a null object or string inside an ASN1_TYPE;
  • a negative length, or null data behind a positive length.

Tested

Debian container, GCC 14.2, full Release build of head 16388cabad: ctest -R 'libressl|crypto_memcmp' passes 38 of 38. tools/check_test_registration.py and python3 tools/check_conventions.py --base upstream/main pass.

  • guest_libressl_x509 checks that a decoded key encodes back to its DER in all three output forms, and that nothing is written outside the encoding. It also checks:

    • null key, algorithm, object, parameter and public key;
    • unused-bit handling with and without BITS_LEFT;
    • BOOLEAN, positive and negative INTEGER and ENUMERATED, SEQUENCE, OTHER, high-tag and OBJECT parameters;
    • the throwing cases.
  • guest_libressl_oracle (ANYPS5_LIBRESSL_ORACLE) now also decodes random certificates with both implementations and encodes their public keys. In 60% of runs it first applies the same change to both keys:

    • a different parameter type, or a BOOLEAN or NULL parameter;
    • an integer sign flip;
    • a removed field;
    • other bit-string flags.

    It compares the return value, the bytes, the pointer advance and the error queue for each output form. The 11,646 cases show no difference. The only differences the oracle reports are the 6 AES_set_*_key cases already described in the debt file.

Not tested on Windows, on a title or on a console.

Checklist

🤖 Generated with Claude Code

JaceTheBlu and others added 13 commits October 25, 2026 22:56
…_ASN1_OBJECT

i2t_ASN1_OBJECT threw and was declared without parameters. It now
prints an object's long name, or its short name, or its dotted OID, as
OBJ_obj2txt does in LibreSSL 2.5.4, the version string the module
holds: the NID is the object's own or found by the same binary search
over the same order, an unassigned NID reports OBJ_R_UNKNOWN_NID for
each name lookup, a truncated subidentifier returns 0 after the parts
already written, and arcs past 64 bits are printed exactly.

The 956 objects (short and long names, NID, OID) and their search order
are generated from 2.5.4's objects.txt and obj_mac.num with its own
objects.pl and obj_dat.pl, so d2i code can later hand out the same
static objects LibreSSL does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…blocks

On Linux the first exception the allocation test throws goes through
libc's unwinder, which caches two frame descriptions with operator new
for the rest of the process. The test counted them as blocks leaked by
i2t_ASN1_OBJECT and failed every fault case. A first failing conversion
now runs before the counted cases, and the blocks it leaves are not
tracked. Against the head before the allocation fix the test still
reports the escaped exceptions.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The export threw and was declared without parameters. It follows
LibreSSL 2.5.4's x509_obj.c: each entry is written as /SN=value with
the short name from the object table, or the dotted OID when the NID
is unknown, bytes outside ' ' to '~' escaped as \xHH and the zero
filler of a four-byte GeneralString dropped. A caller buffer keeps the
entries that fit; without one the text grows in a guest-heap BUF_MEM
as buffer.c and recallocarray do, and a failed allocation returns null
after the BUF and X509 malloc errors. d2i_X509 needs it for the cached
subject line.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
# Conflicts:
#	core/libs/prx/libSceLibreSsl/Export.cpp
#	core/libs/prx/libSceLibreSsl/include/Api.hpp
#	core/libs/prx/libSceLibreSsl/src/X509Name.cpp
#	docs/dev/debt/libSceLibreSsl.md
# Conflicts:
#	core/libs/prx/libSceLibreSsl/CMakeLists.txt
#	core/libs/prx/libSceLibreSsl/include/Api.hpp
#	docs/dev/debt/libSceLibreSsl.md
# Conflicts:
#	core/libs/prx/libSceLibreSsl/CMakeLists.txt
#	docs/dev/debt/libSceLibreSsl.md
# Conflicts:
#	docs/dev/debt/libSceLibreSsl.md
# Conflicts:
#	core/libs/prx/libSceLibreSsl/CMakeLists.txt
#	docs/dev/debt/libSceLibreSsl.md
# Conflicts:
#	core/libs/prx/libSceLibreSsl/CMakeLists.txt
#	core/libs/prx/libSceLibreSsl/Export.cpp
#	core/libs/prx/libSceLibreSsl/include/Api.hpp
#	docs/dev/debt/libSceLibreSsl.md
Port the LibreSSL 2.5.4 template decoder for the items an X509 holds
(certificate, X509_CINF, algorithms, names with their canonical
encoding, validity, public key, extensions and ASN1_TYPE), with the
X509 layouts of x509.h, the x509 reference count and cached subject,
and the error codes LibreSSL queues. guest_libressl_oracle compares
generated and mutated certificates with LibreSSL 2.5.4.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Port the LibreSSL 2.5.4 template encoder for the items an X509_PUBKEY
holds, with i2c_ASN1_INTEGER and i2c_ASN1_BIT_STRING, and compare it
with LibreSSL in the oracle on decoded and altered keys.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

github-actions Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
PR 27526 i2d_X509_PUBKEY

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 11146 core/libs/prx/libSceLibreSsl/CMakeLists.txt
core/libs/prx/libSceLibreSsl/Export.cpp
core/libs/prx/libSceLibreSsl/include/Api.hpp
PR 11176 core/libs/prx/libSceLibreSsl/CMakeLists.txt
core/libs/prx/libSceLibreSsl/Export.cpp
core/libs/prx/libSceLibreSsl/include/Api.hpp
PR 15278 core/libs/prx/libSceLibreSsl/Export.cpp
core/libs/prx/libSceLibreSsl/include/Api.hpp
PR 15280 core/libs/prx/libSceLibreSsl/Export.cpp
core/libs/prx/libSceLibreSsl/include/Api.hpp
PR 16529 core/libs/prx/libSceLibreSsl/Export.cpp
core/libs/prx/libSceLibreSsl/include/Api.hpp
PR 17844 core/libs/prx/libSceLibreSsl/include/Api.hpp
PR 18460 core/libs/prx/libSceLibreSsl/CMakeLists.txt
docs/dev/debt/libSceLibreSsl.md
PR 21701 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
PR 23336 core/libs/prx/libSceLibreSsl/Export.cpp
core/libs/prx/libSceLibreSsl/include/Api.hpp
PR 23573 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
PR 27430 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
PR 27502 core/libs/prx/libSceLibreSsl/CMakeLists.txt
core/libs/prx/libSceLibreSsl/Export.cpp
core/libs/prx/libSceLibreSsl/include/Api.hpp
PR 27504 core/libs/prx/libSceLibreSsl/CMakeLists.txt
core/libs/prx/libSceLibreSsl/Export.cpp
core/libs/prx/libSceLibreSsl/include/Api.hpp
PR 27517 core/libs/prx/libSceLibreSsl/CMakeLists.txt
core/libs/prx/libSceLibreSsl/Export.cpp
core/libs/prx/libSceLibreSsl/include/Api.hpp
PR 27518 core/libs/prx/libSceLibreSsl/CMakeLists.txt
core/libs/prx/libSceLibreSsl/Export.cpp
core/libs/prx/libSceLibreSsl/include/Api.hpp
PR 27526 core/libs/prx/libSceLibreSsl/CMakeLists.txt
core/libs/prx/libSceLibreSsl/Export.cpp
core/libs/prx/libSceLibreSsl/include/Api.hpp

@github-actions

github-actions Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

Progress report (8763aef → 08100a4)

📈 System libraries: 45.23% (+0.0051%, +2 implemented functions)

✅ 2 implemented
Library Name
libSceLibreSsl X509_free_nid_postfix
libSceLibreSsl d2i_X509_nid_postfix

Changed libraries (implemented exports)

Library Before After Change
libSceLibreSsl 275/402 (68.41%) 277/402 (68.91%) +0.50%

JaceTheBlu and others added 4 commits October 31, 2026 09:43
…name-oneline

# Conflicts:
#	docs/dev/debt/libSceLibreSsl.md
…-x509

# Conflicts:
#	core/libs/prx/libSceLibreSsl/tests/Oracle.cpp
#	docs/dev/debt/libSceLibreSsl.md
…ated

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
# Conflicts:
#	core/libs/prx/libSceLibreSsl/tests/Oracle.cpp
@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 31, 2026
The key encoding comparison reads reference certificates through the LP64 layout too, so it runs only when the host long is 8 bytes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Nov 16, 2026
JaceTheBlu and others added 2 commits November 17, 2026 08:44
main reverted the wcstombs change that made guest_multibyte_locale fail on Windows.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
main reverted the wcstombs change that made guest_multibyte_locale fail on Windows.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed — looks good

Nothing blocks this change; the points below are optional.

What I ran

Reviewed head 8057ade57bd671539090517c8d17fb765098b812 using the prepared diff and the pinned repository reader. No commands or tests were run; the build, test, and oracle results in the description are author-reported.

Read all changed lines in CMakeLists.txt, Api.hpp, src/X509.cpp, tests/Oracle.cpp, tests/OracleReference.cpp, tests/X509.cpp, and docs/dev/debt/libSceLibreSsl.md, plus the relevant Export.cpp diff. All changed files were included; no files were excluded.

Results

Check Result Note
Diff review passed Prepared patch inspected
Tests not run Results reported by author
Windows and console behavior not run Author reports not tested

Findings

Blocking

None.

Non-blocking

None.

Not verified

The full build, registered tests, oracle comparisons, Windows behavior, title behavior, and console behavior were not independently verified. The description reports a Debian/GCC 14.2 build and passing tests at a different head (16388cabad); those are author-reported results, not runs on this reviewed head.

To get this to a good state

Nothing is required.

JaceTheBlu and others added 3 commits November 21, 2026 13:13
main reads certificateInfo->enc in X509Store.cpp, so the decoder, its tests and the oracle use that name too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Nov 21, 2026

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed — looks good

Nothing blocks this change based on the source inspected; the reported tests were not run in this review.

What I ran

Reviewed head b97fded6b7be929b146fe207b402938ae96db3d7 using repo_read.py to inspect the prepared diff, manifest, and changed source and test files. No build or tests were run.

Results

Check Result Note
Source review passed Encoder and related API changes inspected
Build and tests not run Description reports host tests; not independently verified

Findings

Blocking

None found in the inspected code.

Non-blocking

None.

Not verified

The full text of core/libs/prx/libSceLibreSsl/tests/Oracle.cpp and core/libs/prx/libSceLibreSsl/tests/OracleReference.cpp was not read; those files were included in the review bundle but excluded from detailed inspection. Build, automated tests, Windows behavior, title behavior, and console behavior were not verified. The description reports a Debian Release build and 38 passing tests, plus 11,646 oracle cases; these are author-reported results.

To get this to a good state

Nothing is required.

JaceTheBlu and others added 3 commits November 21, 2026 18:55
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…_X509

collect_data adds each segment to the buffer length in size_t, so the buffer growth limit rejects a too-long constructed string before any int conversion.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Nov 21, 2026

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed — changes requested

Changes are required before merge: fix d2i_X509 so decode failures do not leave a freed certificate pointer in the caller’s output.

What I ran

Reviewed head 08100a4b9d7ad08f135b037f1e393a38b01f70c5 using the prepared diff and pinned reader. No build or tests were run. The author reports a Debian/GCC 14.2 build, 38 passing tests, and 11,646 oracle cases; those results were not independently verified.

Results

Check Result Note
Source review passed All eight changed files read
Build and tests not run Author-reported only
Console behavior not run Not verified on console

Findings

Blocking

  • core/libs/prx/libSceLibreSsl/src/X509.cpp:2051: ItemDecode frees the reused certificate on failure (SequenceDecode’s failure path calls ItemFree), but this wrapper returns null without clearing *certificate. The existing reuse test expects the output pointer to be null after a failed decode, but its call passes the now-freed pointer back in and does not check the pointer’s value. Clear the caller’s output pointer when decoding fails, and ensure the failed decode path does not leave a dangling pointer.

Non-blocking

None.

Not verified

No commands, build, tests, or console execution were performed. All changed files were included and 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, and docs/dev/debt/libSceLibreSsl.md.

To get this to a good state

Fix the dangling output pointer on decode failure, then rerun the X.509 tests and allocator test on the new head.

@JaceTheBlu

Copy link
Copy Markdown
Contributor Author

@MarcZX10 Thanks for the review. I checked the blocking point: the caller's pointer is already cleared, so I'm leaving the code as it is.

d2i_X509 passes the caller's X509 ** to ItemDecode as the value pointer. On failure, the SEQUENCE decoder calls ItemFree(value, item), and ItemFree sets *value = nullptr after freeing (src/X509.cpp, in ItemFree). LibreSSL 2.5.4 does the same: ASN1_item_ex_d2i calls ASN1_item_ex_free(pval, it) on its err: path, and asn1_item_combine_free ends with *pval = NULL. So after a failed decode into a reused certificate, *certificate is null on both sides.

The reuse test checks this. In tests/X509.cpp, Reuse() decodes a broken certificate into &certificate and requires !d2i_X509_nid_postfix(&certificate, ...) && !certificate && in == broken.data(). The !certificate term is the check on the caller's pointer after the failure. The allocator test (guest_libressl_x509_der_allocator) also runs these paths and requires that nothing is left allocated or freed twice.

Linux: all 203 libressl|crypto_memcmp tests pass on the current head. If you have an input where the pointer stays set, please send it and I'll add it.

@ligmaSec ligmaSec left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On Linux, CTest passed guest_libressl_x509_der and guest_libressl_x509_der_allocator with this revision integrated. The default cases cover DER algorithm identifiers, omitted fields, and bit-string padding. The allocator variant passed separately.

AI-assisted: yes.

@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Dec 5, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

build docs needs rebase Conflicts with main, rebase needed prx

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants