peass-ng/PEASS-ng · error · EndOfStreamException
DEF length object truncated by
Error message
DEF length object truncated by
What it means
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.
Source
Thrown at winPEAS/winPEASexe/winPEAS/3rdParty/BouncyCastle/asn1/DefiniteLengthInputStream.cs:105
if ((_remaining -= Streams.ReadFully(_in, buf)) != 0)
throw new EndOfStreamException("DEF length " + _originalLength + " object truncated by " + _remaining);
SetParentEofDetect(true);
}
internal byte[] ToArray()
{
if (_remaining == 0)
return EmptyBytes;
// make sure it's safe to do this!
int limit = Limit;
if (_remaining >= limit)
throw new IOException("corrupted stream - out of bounds length found: " + _remaining + " >= " + limit);
byte[] bytes = new byte[_remaining];
if ((_remaining -= Streams.ReadFully(_in, bytes)) != 0)
throw new EndOfStreamException("DEF length " + _originalLength + " object truncated by " + _remaining);
SetParentEofDetect(true);
return bytes;
}
}
}
View on GitHub (pinned to 53fb989abc)
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
Example fix
// before
Asn1Object o = Asn1Object.FromStream(networkStream); // may end early
// after
using (var ms = new MemoryStream())
{
networkStream.CopyTo(ms); // or read exactly N bytes from framed protocol
ms.Position = 0;
Asn1Object o = Asn1Object.FromStream(ms); // complete data buffered
} Defensive patterns
Strategy: validation
Validate before calling
// ensure all declared bytes are present before parsing
bool 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; } Try / catch
try { return Asn1Object.FromStream(ms); }
catch (EndOfStreamException ex) { throw new InvalidDataException("ASN.1 object truncated: " + ex.Message, ex); } Prevention
- 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
When it happens
Trigger: 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).
Common situations: 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.
Related errors
- corrupted stream - out of bounds length found:
- unknown object encountered in constructed OCTET STRING:
- unknown tag {tagNo} encountered
- unexpected end-of-contents marker
- indefinite-length primitive encoding encountered
AI-assisted analysis of peass-ng/PEASS-ng@53fb989abc (2026-09-02).
Data as JSON: /api/errors/4a4ede2e74d83319.
Report an issue: GitHub.