Skip to content

Store ASN1_INTEGER as value bytes in every build - #12172

Open
julek-wolfssl wants to merge 1 commit into
wolfSSL:masterfrom
julek-wolfssl:asn1-integer-value-storage
Open

julek-wolfssl wants to merge 1 commit into
wolfSSL:masterfrom
julek-wolfssl:asn1-integer-value-storage

Conversation

@julek-wolfssl

Copy link
Copy Markdown
Member

ASN1_INTEGER now uses the same layout as OpenSSL in all builds, replacing the separate WOLFSSL_QT/WOLFSSL_HAPROXY serial code. data holds the big-endian magnitude, length its size, and type the sign. Encoders add the tag, length, pad and two's complement.

This also fixes two bugs: the missing pad for serials with the high bit set, and an overflow in a2i on allocation failure.

Behavior changes in builds without WOLFSSL_QT or WOLFSSL_HAPROXY:

  • data no longer holds the DER tag, length and pad. Use i2d_ASN1_INTEGER() to get DER.
  • The sign is read only from type.
  • d2i_ASN1_INTEGER() rejects non-minimal DER.
  • i2a_ASN1_INTEGER() prints the sign.
  • The basic constraints pathlen holds its value in data.

Tests that built DER in data, or set the sign in negative or the pathlen in length, now use value bytes and type.

As in OpenSSL, data holds the big-endian magnitude, length its size and
type the sign. Encoders add the tag, length, pad and two's complement.
This replaces the WOLFSSL_QT/WOLFSSL_HAPROXY serial code. It also fixes
the missing pad for high-bit serials and an overflow in a2i on
allocation failure.

ChangeLog: in builds without WOLFSSL_QT or WOLFSSL_HAPROXY, ASN1_INTEGER
data no longer holds the DER tag, length and pad. Use i2d_ASN1_INTEGER()
for DER. The sign is only read from type. d2i_ASN1_INTEGER() rejects
non-minimal DER and i2a_ASN1_INTEGER() prints the sign. The basic
constraints pathlen holds its value in data.

Tests that built DER in data, or set the sign in negative or pathlen in
length, now use value bytes and type.
Copilot AI balanced review requested due to automatic review settings November 18, 2026 12:11
@julek-wolfssl julek-wolfssl self-assigned this Nov 18, 2026
@github-actions

github-actions Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

gcc-arm-cortex-m4-openssl-compat

  • FLASH: .text -256 B (-0.0%, 806,148 B / 1,048,576 B, total: 77% used)

zephyr-frdm_rw612

  • FLASH: datas +16 B, log_const_area -8 B, rodata +104 B, text +13,192 B (+4.7%, 295,768 B / 67,108,864 B, total: 0% used)
  • RAM: bss +49 B, datas +16 B (+0.1%, 58,423 B / 983,040 B, total: 6% used)

No memory changes detected for:


Catch memory regressions in CI with MemBrowse. Free for open source.

Copilot AI 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.

🟡 Changes recommended

The path-length check discards valid positive constraints above LONG_MAX, causing BASIC_CONSTRAINTS round-trips to lose the constraint.

1 open issue
What changed in this PR

This PR unifies wolfSSL’s OpenSSL-compatible ASN1_INTEGER representation across builds: data stores the magnitude, length counts value bytes, and type carries the sign.

Changes:

  • Centralizes integer conversion and encoding, including sign handling, DER padding, and minimal-encoding checks.
  • Migrates X.509, OCSP, CRL, and timestamp consumers to value-byte storage and fixes allocation-failure handling.
  • Updates tests and adds coverage for value layouts, high-bit serials, and leading-zero normalization.
File Description
wolfssl/​wolfcrypt/​asn_public.h Documents the unified integer representation.
wolfssl/​internal.h Declares the shared magnitude constructor.
wolfcrypt/​src/​asn.c Removes the obsolete negative-bignum build requirement.
tests/​api/​test_x509.c Updates serial and path-length test setup.
tests/​api/​test_ossl_x509.h Registers the serial-layout test.
tests/​api/​test_ossl_x509.c Tests serial value bytes and DER padding.
tests/​api/​test_ossl_x509_ext.c Updates basic-constraints value assertions.
tests/​api/​test_ossl_tsp.c Updates timestamp serial and nonce expectations.
tests/​api/​test_ossl_asn1.h Registers the integer-layout test.
tests/​api/​test_ossl_asn1.c Tests conversions, signs, layouts, and DER minimality.
tests/​api.c Adds OCSP high-bit and CRL leading-zero tests.
src/​x509.c Migrates serials and basic-constraints path lengths.
src/​ssl_tsp.c Reads timestamp integers as unsigned value bytes.
src/​ssl_asn1.c Implements unified storage, conversion, and encoding.
src/​ocsp.c Uses the shared constructor for serial integers.
src/​crl.c Normalizes revoked serials and shares integer construction.

Review effort: Balanced

Comment thread src/x509.c
Comment on lines +2219 to +2220
if ((object->pathlen != NULL) &&
(wolfSSL_ASN1_INTEGER_get(object->pathlen) >= 0)) {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants