{"record":{"id":"4a4ede2e74d83319","repo":"peass-ng/PEASS-ng","slug":"def-length-object-truncated-by","errorCode":null,"errorMessage":"DEF length  object truncated by ","messagePattern":"DEF length  object truncated by ","errorType":"validation","errorClass":"EndOfStreamException","httpStatus":null,"severity":"error","filePath":"winPEAS/winPEASexe/winPEAS/3rdParty/BouncyCastle/asn1/DefiniteLengthInputStream.cs","lineNumber":105,"sourceCode":"\n\t\t\tif ((_remaining -= Streams.ReadFully(_in, buf)) != 0)\n\t\t\t\tthrow new EndOfStreamException(\"DEF length \" + _originalLength + \" object truncated by \" + _remaining);\n\t\t\tSetParentEofDetect(true);\n\t\t}\n\n\t\tinternal byte[] ToArray()\n\t\t{\n\t\t\tif (_remaining == 0)\n\t\t\t\treturn EmptyBytes;\n\n\t\t\t// make sure it's safe to do this!\n\t\t\tint limit = Limit;\n\t\t\tif (_remaining >= limit)\n\t\t\t\tthrow new IOException(\"corrupted stream - out of bounds length found: \" + _remaining + \" >= \" + limit);\n\n\t\t\tbyte[] bytes = new byte[_remaining];\n\t\t\tif ((_remaining -= Streams.ReadFully(_in, bytes)) != 0)\n\t\t\t\tthrow new EndOfStreamException(\"DEF length \" + _originalLength + \" object truncated by \" + _remaining);\n\t\t\tSetParentEofDetect(true);\n\t\t\treturn bytes;\n\t\t}\n\t}\n}\n","sourceCodeStart":87,"sourceCodeEnd":111,"githubUrl":"https://github.com/peass-ng/PEASS-ng/blob/53fb989abc2219826385683a6fee826bd6cd38d6/winPEAS/winPEASexe/winPEAS/3rdParty/BouncyCastle/asn1/DefiniteLengthInputStream.cs#L87-L111","documentation":"DefiniteLengthInputStream.ToArray reads the declared number of bytes via Streams.ReadFully; if the underlying stream ends early, the remaining count is nonzero and EndOfStreamException 'DEF length N object truncated by M' is thrown. The declared definite-length object could not be fully read.","triggerScenarios":"An ASN.1 object header declares a definite length N, but ReadFully obtains fewer than N bytes because the stream is truncated mid-object (network drop, partial file, read past a buffer boundary).","commonSituations":"Parsing truncated downloads of certificates/PKCS#12/signed data; reading ASN.1 from a network stream that closed early; slicing a byte[] too short before wrapping in MemoryStream.","solutions":["Ensure the complete encoded object is available before parsing (buffer the stream fully first)","Check file size / transfer completeness; re-download the blob","When reading from sockets, loop reading until the expected length is received or use a higher-level protocol framing","Catch EndOfStreamException and report the data as truncated/corrupt"],"exampleFix":"// before\nAsn1Object o = Asn1Object.FromStream(networkStream); // may end early\n// after\nusing (var ms = new MemoryStream())\n{\n    networkStream.CopyTo(ms); // or read exactly N bytes from framed protocol\n    ms.Position = 0;\n    Asn1Object o = Asn1Object.FromStream(ms); // complete data buffered\n}","handlingStrategy":"validation","validationCode":"// ensure all declared bytes are present before parsing\nbool IsComplete(byte[] data) { int i = 1; int n = data[1] & 0x7f; if ((data[1] & 0x80) != 0) { int len = data[1] & 0x7f; long v = 0; for (int k = 0; k < len; k++) v = (v << 8) | data[2 + k]; n = checked((int)v); i = 2 + len; } return data.Length >= i + n; }","typeGuard":null,"tryCatchPattern":"try { return Asn1Object.FromStream(ms); }\ncatch (EndOfStreamException ex) { throw new InvalidDataException(\"ASN.1 object truncated: \" + ex.Message, ex); }","preventionTips":["Fully buffer network/file streams before parsing","Frame network protocols by exact byte counts before handing to ASN.1 parsing","Detect truncation with checksums or length prefixes at the transport layer","Never parse a partially received message"],"tags":["asn1","truncated","eof","bouncycastle"],"backgroundTag":"der-object-truncated","analyzedSha":"53fb989abc2219826385683a6fee826bd6cd38d6","analyzedAt":"2026-09-02T04:25:09.259Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T11:17:12.671Z"}