Skip to content

feat(libSceLibreSsl3): decode basicConstraints, extendedKeyUsage and privateKeyUsagePeriod in X509V3_EXT_d2i - #26421

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
ToRaNek:feat/libressl3-x509v3-ext-d2i-basic-decoders
Nov 25, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
ToRaNek:feat/libressl3-x509v3-ext-d2i-basic-decoders

Conversation

@ToRaNek

@ToRaNek ToRaNek commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

What

Resubmission of #25363, which was closed when my fork left the fork network (I switched it to private by mistake) and cannot be reopened. Same head commit d23fd217f5f; the reviews and the discussion are on #25363.

X509V3_EXT_d2i in libSceLibreSsl3 threw NotImplemented for three common extensions that LibreSSL 4.0.0 decodes: basicConstraints (NID 87), extendedKeyUsage (126) and privateKeyUsagePeriod (84). X509V3_get_d2i and X509_CRL_get_ext_d2i go through the same table and get them too.

Each follows its template (4.0.0, ce488b78) and the generic sequence decoder of tasn_dec.c:

Extension Template Object returned
basicConstraints (x509_bcons.c) SEQUENCE { ca BOOLEAN OPTIONAL, pathlen INTEGER OPTIONAL } the 16-byte BASIC_CONSTRAINTS { int ca; ASN1_INTEGER *pathlen; } of x509v3.h
extendedKeyUsage (x509_extku.c) SEQUENCE OF OBJECT a STACK_OF(ASN1_OBJECT)
privateKeyUsagePeriod (x509_pku.c) SEQUENCE { notBefore [0] GeneralizedTime OPTIONAL, notAfter [1] GeneralizedTime OPTIONAL } the 16-byte PKEY_USAGE_PERIOD

Points taken from the source rather than from the RFC:

  • ca receives the content byte of the BOOLEAN as it is: ff gives 255 and 01 gives 1. An absent ca is 0.
  • The last field of a sequence is not treated as optional while content remains: a wrong tag where pathlen or notAfter should be is an error, not an absence.
  • An empty SEQUENCE OF is an empty stack, and the "constructed" bit of a SEQUENCE OF is not checked (10 00 is accepted), as in asn1_template_stack_of_d2i.
  • Bytes after the value inside the extension are ignored (X509V3_EXT_d2i does not check the cursor).
  • 00 00 where a field should start in a definite-length sequence is ASN1_R_UNEXPECTED_EOC.
  • Each refusal queues the errors of the source, ERR_R_NESTED_ASN1_ERROR entries included; the Field= / Type= data is not kept.

The parts are allocated with the helpers the file already uses (d2i_ASN1_INTEGER, the object and GeneralizedTime decoders, sk_new / sk_push), so ASN1_INTEGER_free, ASN1_OBJECT_free and sk_pop_free release them.

Left as it was (in the debt fragment):

  • an indefinite length on the three sequences and a constructed [0] / [1] time still throw; the source decodes them;
  • a length written on more than 8 bytes is refused by the header reader of this file, where the source reads up to 126;
  • this library has no BASIC_CONSTRAINTS_free, EXTENDED_KEY_USAGE_free or PKEY_USAGE_PERIOD_free: a caller reads the public fields and releases the parts itself;
  • the other unsupported extensions (subjectAltName, certificatePolicies, ...) are unchanged.

Tested

Windows 11, MinGW GCC 15.2. Not tested on a console.

Vectors. A throwaway P-256 certificate made with OpenSSL 3.2.4:

openssl req -x509 -newkey ec -pkeyopt ec_paramgen_curve:prime256v1 -nodes -keyout key.pem -out cert.pem -days 30 -config ext.cnf

with basicConstraints = critical,CA:TRUE,pathlen:3 and extendedKeyUsage = serverAuth,clientAuth,codeSigning,1.2.3.4.5. Its extension values (30060101FF020103 and the four-usage list) decode to what openssl x509 -text prints; a second certificate with CA:FALSE gives 3000.

The test (guest_libressl3_x509v3_ext_d2i), four new functions:

  • every combination of optional fields; ca with each of the 256 content bytes; pathlen zero, positive, negative and large; usage lists that are empty, with one and several OIDs, with an OID outside the table; both times, one, none;
  • every field of the returned object read back, then everything released (the allocation tracker of the file ends empty);
  • each refusal with its whole error queue: bad sequence header, primitive sequence, constructed or wrong-length BOOLEAN, empty or padded INTEGER, bad OID, bad time, wrong tag on the last field, content left over, 00 00;
  • an allocation failure at each step, with nothing leaked;
  • round trips: the extension built by X509V3_EXT_conf_nid decodes back.

With X509V3ExtD2i.cpp from main the test stops on the NotImplemented exception of its first call; with the change it passes, and guest_libressl3_x509v3_ext_conf_nid and guest_libressl3_x509_crl still pass.

Negative controls. 61 mutations of the three decoders, each rebuilt and run on its own, all make the test fail: optional fields made mandatory and the reverse, the value stored for ca, each tag and class, each check removed, each error code changed, a release forgotten on each failure path, and the three table lines.

python tools/check_conventions.py --base origin/main and python core/libs/tests/test_public_headers.py --audit-only pass. python tools/debt_fold.py --check fails on this branch for the reason it fails on main today (two libkernel_sys fragments, which #25313 and #25318 address); on a copy without those two fragments it passes.

Not verified. The expected queues come from reading the source, not from running a LibreSSL 4.0.0 binary.

Overlap (python tools/pr_overlap.py --branch feat/libressl3-x509v3-ext-d2i-basic-decoders):

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt and the licence notice of code adapted from upstream (see AGENTS.md)
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

…privateKeyUsagePeriod in X509V3_EXT_d2i

X509V3_EXT_d2i threw NotImplemented for three common extensions that
LibreSSL 4.0.0 decodes. They now follow their templates:

- basicConstraints: SEQUENCE { ca BOOLEAN OPTIONAL, pathlen INTEGER
  OPTIONAL } into the 16-byte BASIC_CONSTRAINTS of x509v3.h; ca takes
  the content byte as it is;
- extendedKeyUsage: SEQUENCE OF OBJECT into a stack of ASN1_OBJECT;
- privateKeyUsagePeriod: SEQUENCE { [0] GeneralizedTime OPTIONAL,
  [1] GeneralizedTime OPTIONAL } into PKEY_USAGE_PERIOD.

Refusals queue the errors of tasn_dec.c. X509V3_get_d2i and
X509_CRL_get_ext_d2i use the same table.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

@catminou4 catminou4 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.

Built libSceLibreSsl3 and ran guest_libressl3_x509v3_ext_d2i_tests on Linux (x86-64, GCC 11): passes. New decoder surface, so no meaningful main control.

The debt fragment is appropriately precise about the LibreSSL 4.0.0 tasn_dec.c provenance and the known divergences (indefinite length / constructed time throw; >8-byte length refused where LibreSSL reads up to 126; Field=/Type= error data not queued). Good that it also flags the missing *_free exports so callers know they must release the parts themselves. Verified the three decoders are wired as throwing methods 87/126/84 in the export table.

@PrivacyOnion PrivacyOnion 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.

Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against d23fd217f5:

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_libressl3_x509v3_ext_d2i_tests built cleanly.
  • CTest (guest_libressl3_x509v3_ext_d2i):
    1/1 Test #4828: guest_libressl3_x509v3_ext_d2i ...   Passed    0.02 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/libs/prx/libSceLibreSsl3/src/X509V3ExtD2i.cpp reverted to main and the new tests kept, the suite fails:
    1/1 Test #4828: guest_libressl3_x509v3_ext_d2i ...Subprocess aborted***Exception:   0.49 sec
    0% tests passed, 1 tests failed out of 1
    
    Restoring the change makes it pass again.
  • Code reading:
    • Verified memory safety and RAII cleanup across NewBasicConstraints, NewExtendedKeyUsage, and NewUsagePeriod, ensuring all allocations and partial stack elements are freed on error paths without leaks.
    • Verified exact LibreSSL ASN.1 error reason codes (58, 65, 102, 106, 132, 148, 149, 155, 159, 168, 216, 231) for malformed tags, lengths, primitive/constructed mismatches, and EOC in definite forms.
    • Verified LP64 and LLP64 portability and ABI struct layouts for BasicConstraintsValue and UsagePeriodValue, both static_asserted to 16 bytes with 8-byte member offsets.
    • Verified correct handling of all optional field permutations: absent/present ca (tested across all 256 boolean byte values), absent/present pathLength, and independent absence/presence of notBefore and notAfter.
    • Verified strict AGENTS.md compliance: zero disallowed comments in implementation code, valid technical debt fragment added in docs/dev/debt/changes/, and unsupported BER indefinite-length/constructed forms explicitly throwing via NotImplemented_nid_no_patch.

Looks Good To Me

@oneandonlydean
oneandonlydean merged commit 9d4f39d into boykopovar:main Nov 25, 2026
28 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants