elastic/elasticsearch · error · IOException

Invalid DER: length field too big ({})

Error message

Invalid DER: length field too big ({})

What it means

Thrown by DerParser.getLength() in long-form length decoding when the first length octet is 0xFF (reserved/indefinite in DER) or when the number of subsequent length octets exceeds 4. DER length fields use long form only for sizes that don't fit in a single byte, and this parser caps support at 4 length bytes (enough for ~4 GiB).

Source

Thrown at libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/DerParser.java:135

     * number of additional length octets. Second and following
     * octets give the length, base 256, most significant digit first.
     * </li>
     * </ul>
     *
     * @return The length as integer
     */
    private int getLength() throws IOException {

        int i = derInputStream.read();
        if (i == -1) throw new IOException("Invalid DER: length missing");

        // A single byte short length
        if ((i & ~0x7F) == 0) return i;

        int num = i & 0x7F;

        // We can't handle length longer than 4 bytes
        if (i >= 0xFF || num > 4) throw new IOException("Invalid DER: length field too big (" + i + ")"); //$NON-NLS-2$

        byte[] bytes = new byte[num];
        int n = derInputStream.read(bytes);
        if (n < num) throw new IOException("Invalid DER: length too short");

        int len = new BigInteger(1, bytes).intValue();
        if (len < 0) {
            throw new IOException("Invalid DER: length larger than max-int");
        }

        return len;
    }

    /**
     * An ASN.1 TLV. The object is not parsed. It can
     * only handle integers.
     *
     * @author zhang

View on GitHub (pinned to db6a809a66)

Solutions

  1. Ensure the source emits DER (definite length), not BER with indefinite length. Re-encode with `openssl asn1parse -genconf` or `openssl pkcs8 -topk8`.
  2. Re-export the key from a trusted tool (openssl, keytool) to guarantee DER compliance.
  3. Inspect the bytes around the failing offset with a hex dump (`xxd`) to confirm the length octets are sensible.
  4. If you control the producer, validate length encoding before writing DER output.

Example fix

// before: feeding BER with indefinite length
new DerParser(berBytes).readAsn1Object();

// after: convert to DER first
// openssl pkcs8 -topk8 -inform BER -inkey ber.key -outform DER -out der.key
byte[] der = Files.readAllBytes(Path.of("der.key"));
new DerParser(der).readAsn1Object();
Defensive patterns

Strategy: validation

Validate before calling

// Reject indefinite-length (BER) or implausibly large length headers early.
private static void requireDerLength(byte[] der, int idx) {
    int i = der[idx] & 0xFF;
    if (i == 0x80) throw new IllegalArgumentException("indefinite length (BER) not supported");
    if (i == 0xFF || (i & 0x7F) > 4) {
        throw new IllegalArgumentException("length field too big or reserved: 0x" + Integer.toHexString(i));
    }
}

Prevention

When it happens

Trigger: getLength() reads i where (i & ~0x7F) != 0 (long form), then checks i >= 0xFF || (i & 0x7F) > 4. Fires on DER where the length-of-length byte is 0x85+ or exactly 0xFF — typical of malformed or non-DER (BER indefinite-length) input.

Common situations: BER (not DER) input that uses indefinite-length encoding (0x80), corrupt bytes that coincidentally look like a huge length, or a malformed key produced by a buggy/non-conformant encoder.

Related errors


AI-assisted analysis of elastic/elasticsearch@db6a809a66 (2026-08-12). Data as JSON: /api/errors/e6d8a68265cc616d. Report an issue: GitHub.