elastic/elasticsearch · error · IOException

Invalid DER: size of ASN.1 object to be parsed appears to be

Error message

Invalid DER: size of ASN.1 object to be parsed appears to be larger than the size of the key file itself.

What it means

Thrown by DerParser.readAsn1Object() when an ASN.1 TLV's declared length exceeds maxAsnObjectLength (the byte-array size the parser was constructed with). This guards against corrupted or maliciously crafted DER that would otherwise force a huge allocation. It indicates the encoded length is inconsistent with the physical size of the source bytes.

Source

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

        final Asn1Object obj = readAsn1Object();
        if (obj.type != requiredType) {
            throw new IllegalStateException(
                "Expected ASN.1 object of type 0x" + Integer.toHexString(requiredType) + " but was 0x" + Integer.toHexString(obj.type)
            );
        }
        return obj;
    }

    public Asn1Object readAsn1Object() throws IOException {
        int tag = derInputStream.read();
        if (tag == -1) {
            throw new IOException("Invalid DER: stream too short, missing tag");
        }
        int length = getLength();
        // getLength() can return any 32 bit integer, so ensure that a corrupted encoding won't
        // force us into allocating a very large array
        if (length > maxAsnObjectLength) {
            throw new IOException(
                "Invalid DER: size of ASN.1 object to be parsed appears to be larger than the size of the key file " + "itself."
            );
        }
        byte[] value = new byte[length];
        int n = derInputStream.read(value);
        if (n < length) {
            throw new IOException(
                "Invalid DER: stream too short, missing value. " + "Could only read " + n + " out of " + length + " bytes"
            );
        }
        return new Asn1Object(tag, length, value);

    }

    /**
     * Decode the length of the field. Can only support length
     * encoding up to 4 octets.
     * <p>

View on GitHub (pinned to db6a809a66)

Solutions

  1. Re-export the private key from its source (openssl genrsa/openssl pkcs8) to get a clean, complete file.
  2. Verify the file is binary DER after base64 decoding: compare its size against the expected key length and inspect the first bytes for the expected SEQUENCE tag (0x30).
  3. If the file is PEM, ensure the base64 body is intact (run `openssl pkey -in <file> -check -noout` or `openssl rsa -check`).
  4. Confirm you are not feeding a PEM blob to a code path that already base64-decoded it, or vice versa.

Example fix

// before: feeding a truncated or wrong-format byte[] to DerParser
byte[] der = Files.readAllBytes(keyPath); // oops, this is still PEM text
new DerParser(der).readAsn1Object();

// after: decode PEM first, then parse DER
String pem = Files.readString(keyPath);
String body = pem.replaceAll("-----BEGIN.*?-----", "").replaceAll("-----END.*?-----", "").replaceAll("\\s", "");
byte[] der = Base64.getDecoder().decode(body);
new DerParser(der).readAsn1Object();
Defensive patterns

Strategy: validation

Validate before calling

// Validate that the byte[] is plausible DER before parsing.
private static void requirePlausibleDer(byte[] der) {
    if (der == null || der.length < 2) {
        throw new IllegalArgumentException("DER input is null or too short");
    }
    // First byte of a key/alg-identifier DER is almost always a SEQUENCE (0x30)
    if (der[0] != 0x30) {
        throw new IllegalArgumentException("Expected DER SEQUENCE (0x30) but found 0x" + Integer.toHexString(der[0] & 0xFF));
    }
}

Prevention

When it happens

Trigger: A DerParser is constructed over a byte array (e.g. from PEM-decoded PKCS#1/PKCS#8 DER, or the algorithm-identifier sequence inside an EncryptedPrivateKeyInfo) and readAsn1Object() reads a length octet whose decoded value is greater than the original byte[] length. Common during parseEcDer/parseRsaDer/parseDsaDer/getKeyAlgorithmIdentifier/getEncryptedPrivateKeyInfo when the input is truncated or non-DER.

Common situations: Truncated key file (incomplete download or copy/paste), base64 corruption that produces valid but wrong bytes, a DER blob that is actually PEM text or an HTML error page, mismatched key format (feeding PKCS#8 DER where PKCS#1 expected), or a key generated by a tool that emits non-standard DER.

Related errors


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