peass-ng/PEASS-ng · error · IOException

indefinite-length primitive encoding encountered

Error message

indefinite-length primitive encoding encountered

What it means

Asn1InputStream.ReadObject throws this IOException when it reads an indefinite-length (BER) encoding for a PRIMITIVE (non-constructed) object. Indefinite-length encoding is only legal for constructed types in BER, so a primitive tag with length 0x80 indicates non-conformant or corrupt data.

Source

Thrown at winPEAS/winPEASexe/winPEAS/3rdParty/BouncyCastle/asn1/Asn1InputStream.cs:189

                return null;
            }

            //
            // calculate tag number
            //
            int tagNo = ReadTagNumber(this.s, tag);

            bool isConstructed = (tag & Asn1Tags.Constructed) != 0;

            //
            // calculate length
            //
            int length = ReadLength(this.s, limit, false);

            if (length < 0) // indefinite-length method
            {
                if (!isConstructed)
                    throw new IOException("indefinite-length primitive encoding encountered");

                IndefiniteLengthInputStream indIn = new IndefiniteLengthInputStream(this.s, limit);
                Asn1StreamParser sp = new Asn1StreamParser(indIn, limit);

                if ((tag & Asn1Tags.Application) != 0)
                {
                    return new BerApplicationSpecificParser(tagNo, sp).ToAsn1Object();
                }

                if ((tag & Asn1Tags.Tagged) != 0)
                {
                    return new BerTaggedObjectParser(true, tagNo, sp).ToAsn1Object();
                }

                // TODO There are other tags that may be constructed (e.g. BitString)
                switch (tagNo)
                {
                    case Asn1Tags.OctetString:

View on GitHub (pinned to 53fb989abc)

Solutions

  1. Re-encode the source data with definite lengths (proper DER) before parsing.
  2. Verify the byte stream alignment — misalignment can make the parser read 0x80 as a length for a primitive tag.
  3. Use Asn1StreamParser with indefinite-length support if the source legitimately uses indefinite-length BER, and ensure primitive tags are never indefinite.
  4. Catch IOException around ReadObject and surface a clear 'non-conformant BER encoding' error to the caller.

Example fix

// before
Asn1Object obj = new Asn1InputStream(berBytes).ReadObject();
// after
try { Asn1Object obj = new Asn1InputStream(berBytes).ReadObject(); }
catch (IOException ex) { throw new InvalidDataException("Indefinite-length primitive BER encoding is illegal", ex); }
Defensive patterns

Strategy: validation

Validate before calling

static bool HasDefiniteLengthPrimitive(byte[] data)
{
    if (data == null || data.Length < 3) return false;
    bool isConstructed = (data[0] & 0x20) != 0;
    if (isConstructed) return true; // indefinite allowed for constructed
    return data[1] != 0x80; // primitive must not use indefinite length 0x80
}

Try / catch

try {
    Asn1Object obj = stream.ReadObject();
} catch (IOException ex) when (ex.Message == "indefinite-length primitive encoding encountered") {
    throw new InvalidDataException("Source data violates BER: primitive element uses indefinite length", ex);
}

Prevention

When it happens

Trigger: Parsing BER data where a primitive tag (e.g. INTEGER, OCTET STRING, OID) is encoded with indefinite length 0x80 instead of a definite length — produced by a non-compliant encoder or by corrupted/misaligned bytes.

Common situations: Data generated by a non-standards-compliant BER writer (some embedded/proprietary systems); parsing DER (which forbids indefinite length entirely) that was actually BER with indefinite primitives; byte-offset misalignment making a length byte be read as a tag or vice versa.

Related errors


AI-assisted analysis of peass-ng/PEASS-ng@53fb989abc (2026-09-02). Data as JSON: /api/errors/6ac1c7dd07868039. Report an issue: GitHub.