A Type System Built from Length Fields

Site Admin · 3 Dec 2026 · Core Internet Infrastructure

Tag, length, value: how ASN.1 becomes C structures, how the mbstring conversion sizes its output, and where big-number and TLS hazards sit.

ASN.1 encodes everything as tag, length, value. OpenSSL's job is to turn
those bytes into C structures safely — and the conversions between string
types are where the arithmetic lives.

ASN.1 in one paragraph

ASN.1 describes data; DER is the encoding that puts it on the wire. Every DER
value is a triple:

tag        length        value
0x30       0x82 0x01 0x2A  ...
SEQUENCE   290 bytes     contents

The length is the dangerous part. It can be short form (one byte, up to 127)
or long form (a count byte followed by up to four length bytes), and it can
describe a value nested several layers deep. A parser that trusts a length
without checking it against the remaining buffer is a parser with a bug.

OpenSSL's decoder does check bounds: it tracks the end of each constructed
value and refuses lengths that exceed the input. That is why most ASN.1 bugs
here are not trusted-length errors but arithmetic errors in the code that
sizes a buffer before conversion.

The string types

X.509 names are not byte strings. They carry a type:

Internally OpenSSL stores an ASN1_STRING with a type, length and data
pointer. ASN1_mbstring_ncopy() converts an input in one of these encodings
into the smallest output type permitted by a mask.

The conversion, step by step

The function takes the input, its length len, the input form inform, and a
bitmask of permitted output types. It does five things:

  1. Counts characters. For BMP it halves len; for Universal it quarters
    it; for UTF-8 it counts code points by walking the string.
  2. Checks size limits. Certificate name fields have per-NID limits;
    callers can pass their own.
  3. Picks the output type by narrowing the mask to types that can represent
    every character.
  4. Sizes the destination. For BMP, outlen = nchar << 1; for Universal,
    outlen = nchar << 2; for UTF-8, it sums the bytes each code point will
    need.
  5. Allocates and copies: OPENSSL_malloc(outlen + 1), then a second
    traversal writes through a function pointer.

Step four is where CVE-2026-7383 lives: the size is an int, the shift is
signed, and nothing checks that the result is representable.

/* the shape of the pre-fix code */
nchar  = len >> 2;          /* UniversalString: 4 bytes per char */
outlen = nchar << 2;        /* wraps when nchar >= 2^30 */
dest   = OPENSSL_malloc(outlen + 1);

At nchar == 0x40000000, nchar << 2 is zero in 32-bit signed arithmetic.
The allocation is one byte. The copy writes four gigabytes.

Big numbers and the TLS stack

BN is the arbitrary-precision integer layer behind RSA, DSA,
Diffie-Hellman and elliptic curves. Its hazards are timing and correctness,
not buffer sizes: operations that touch secrets must be constant-time.

The TLS stack is a state machine over the record layer — handshakes,
cipher negotiation, key derivation, certificate verification. Since 3.0 the
algorithms sit behind providers (default, FIPS, legacy), and that boundary
is a security boundary: the FIPS module is separately validated. CVE-2026-7383
is outside it. The string conversion is not part of the validated boundary.

Why the conversion is subtle

The function has to answer a question that sounds simple and is not: how many
bytes will this string occupy after conversion? The answer depends on the
input type, the output type, the characters themselves, and the mask. The code
computes a worst case first and converts second, allocating once.

That design is correct only if the worst-case arithmetic cannot overflow. The
fix adds exactly that check: nchar > INT_MAX / 2 before the BMP shift,
nchar > INT_MAX / 4 before the Universal shift, and *outlen > INT_MAX - len
inside the UTF-8 accumulator.

Next: Part 6 — the same bug, four different doors: the smaller ASN.1
failures that shipped in the same release cycle.