{"record":{"id":"6ac1c7dd07868039","repo":"peass-ng/PEASS-ng","slug":"indefinite-length-primitive-encoding-encountered","errorCode":null,"errorMessage":"indefinite-length primitive encoding encountered","messagePattern":"indefinite-length primitive encoding encountered","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"winPEAS/winPEASexe/winPEAS/3rdParty/BouncyCastle/asn1/Asn1InputStream.cs","lineNumber":189,"sourceCode":"                return null;\n            }\n\n            //\n            // calculate tag number\n            //\n            int tagNo = ReadTagNumber(this.s, tag);\n\n            bool isConstructed = (tag & Asn1Tags.Constructed) != 0;\n\n            //\n            // calculate length\n            //\n            int length = ReadLength(this.s, limit, false);\n\n            if (length < 0) // indefinite-length method\n            {\n                if (!isConstructed)\n                    throw new IOException(\"indefinite-length primitive encoding encountered\");\n\n                IndefiniteLengthInputStream indIn = new IndefiniteLengthInputStream(this.s, limit);\n                Asn1StreamParser sp = new Asn1StreamParser(indIn, limit);\n\n                if ((tag & Asn1Tags.Application) != 0)\n                {\n                    return new BerApplicationSpecificParser(tagNo, sp).ToAsn1Object();\n                }\n\n                if ((tag & Asn1Tags.Tagged) != 0)\n                {\n                    return new BerTaggedObjectParser(true, tagNo, sp).ToAsn1Object();\n                }\n\n                // TODO There are other tags that may be constructed (e.g. BitString)\n                switch (tagNo)\n                {\n                    case Asn1Tags.OctetString:","sourceCodeStart":171,"sourceCodeEnd":207,"githubUrl":"https://github.com/peass-ng/PEASS-ng/blob/53fb989abc2219826385683a6fee826bd6cd38d6/winPEAS/winPEASexe/winPEAS/3rdParty/BouncyCastle/asn1/Asn1InputStream.cs#L171-L207","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Re-encode the source data with definite lengths (proper DER) before parsing.","Verify the byte stream alignment — misalignment can make the parser read 0x80 as a length for a primitive tag.","Use Asn1StreamParser with indefinite-length support if the source legitimately uses indefinite-length BER, and ensure primitive tags are never indefinite.","Catch IOException around ReadObject and surface a clear 'non-conformant BER encoding' error to the caller."],"exampleFix":"// before\nAsn1Object obj = new Asn1InputStream(berBytes).ReadObject();\n// after\ntry { Asn1Object obj = new Asn1InputStream(berBytes).ReadObject(); }\ncatch (IOException ex) { throw new InvalidDataException(\"Indefinite-length primitive BER encoding is illegal\", ex); }","handlingStrategy":"validation","validationCode":"static bool HasDefiniteLengthPrimitive(byte[] data)\n{\n    if (data == null || data.Length < 3) return false;\n    bool isConstructed = (data[0] & 0x20) != 0;\n    if (isConstructed) return true; // indefinite allowed for constructed\n    return data[1] != 0x80; // primitive must not use indefinite length 0x80\n}","typeGuard":null,"tryCatchPattern":"try {\n    Asn1Object obj = stream.ReadObject();\n} catch (IOException ex) when (ex.Message == \"indefinite-length primitive encoding encountered\") {\n    throw new InvalidDataException(\"Source data violates BER: primitive element uses indefinite length\", ex);\n}","preventionTips":["Require DER (definite lengths only) from data producers; DER forbids indefinite length entirely","Inspect the raw bytes (openssl asn1parse) to confirm the sender's encoder is compliant","Check stream alignment — 0x80 read as a length often means an offset bug upstream","Use Asn1StreamParser with constructed/indefinite handling when parsing legacy BER rather than raw Asn1InputStream"],"tags":["asn1","bouncycastle","parsing","ber","encoding"],"backgroundTag":"malformed-asn1-data","analyzedSha":"53fb989abc2219826385683a6fee826bd6cd38d6","analyzedAt":"2026-09-02T04:25:09.259Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T11:17:12.671Z"}