{"record":{"id":"2f00f3261ca8ebb1","repo":"peass-ng/PEASS-ng","slug":"extra-data-found-after-object","errorCode":null,"errorMessage":"extra data found after object","messagePattern":"extra data found after object","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"winPEAS/winPEASexe/winPEAS/3rdParty/BouncyCastle/asn1/Asn1Object.cs","lineNumber":24,"sourceCode":"\tpublic abstract class Asn1Object\n\t\t: Asn1Encodable\n\t{\n\t\t/// <summary>Create a base ASN.1 object from a byte array.</summary>\n\t\t/// <param name=\"data\">The byte array to parse.</param>\n\t\t/// <returns>The base ASN.1 object represented by the byte array.</returns>\n\t\t/// <exception cref=\"IOException\">\n\t\t/// If there is a problem parsing the data, or parsing an object did not exhaust the available data.\n\t\t/// </exception>\n\t\tpublic static Asn1Object FromByteArray(\n\t\t\tbyte[] data)\n\t\t{\n\t\t\ttry\n\t\t\t{\n\t\t\t\tMemoryStream input = new MemoryStream(data, false);\n\t\t\t\tAsn1InputStream asn1 = new Asn1InputStream(input, data.Length);\n\t\t\t\tAsn1Object result = asn1.ReadObject();\n\t\t\t\tif (input.Position != input.Length)\n\t\t\t\t\tthrow new IOException(\"extra data found after object\");\n\t\t\t\treturn result;\n\t\t\t}\n\t\t\tcatch (InvalidCastException)\n\t\t\t{\n\t\t\t\tthrow new IOException(\"cannot recognise object in byte array\");\n\t\t\t}\n\t\t}\n\n\t\t/// <summary>Read a base ASN.1 object from a stream.</summary>\n\t\t/// <param name=\"inStr\">The stream to parse.</param>\n\t\t/// <returns>The base ASN.1 object represented by the byte array.</returns>\n\t\t/// <exception cref=\"IOException\">If there is a problem parsing the data.</exception>\n\t\tpublic static Asn1Object FromStream(\n\t\t\tStream inStr)\n\t\t{\n\t\t\ttry\n\t\t\t{\n\t\t\t\treturn new Asn1InputStream(inStr).ReadObject();","sourceCodeStart":6,"sourceCodeEnd":42,"githubUrl":"https://github.com/peass-ng/PEASS-ng/blob/53fb989abc2219826385683a6fee826bd6cd38d6/winPEAS/winPEASexe/winPEAS/3rdParty/BouncyCastle/asn1/Asn1Object.cs#L6-L42","documentation":"Asn1Object.FromByteArray parses exactly one ASN.1 object and then verifies the underlying MemoryStream was fully consumed. Leftover bytes after the first object mean the array holds extra trailing data, so IOException is thrown.","triggerScenarios":"Passing a byte[] that contains an ASN.1 object followed by additional bytes — e.g. a DER object concatenated with padding, signatures, or a second object; a blob extracted with a too-large length.","commonSituations":"Concatenated certificate chains parsed as a single object; PKCS#7 blobs sliced incorrectly; arrays copied from a larger buffer with wrong length arithmetic.","solutions":["Trim the array to the exact length of the first ASN.1 object before calling FromByteArray","If multiple objects are expected, read them sequentially with Asn1InputStream.ReadObject in a loop instead of FromByteArray","Fix the offset/length used when extracting the sub-array","Catch IOException and inspect input.Position vs Length to locate the extra data"],"exampleFix":"// before\nbyte[] blob = new byte[fileBytes.Length - 16]; // extra trailing bytes remain\nAsn1Object o = Asn1Object.FromByteArray(blob);\n// after\n// parse all objects sequentially\nAsn1InputStream s = new Asn1InputStream(new MemoryStream(fileBytes));\nAsn1Object o;\nwhile ((o = s.ReadObject()) != null) { Handle(o); }","handlingStrategy":"validation","validationCode":"static byte[] ExactObjectSlice(byte[] blob, int offset, int contentLen)\n{\n    // contentLen = declared DER content length + header size\n    int headerSize = 2;\n    byte lb = blob[offset + 1];\n    if (lb > 0x7f) headerSize = 2 + (lb & 0x7f);\n    int total = headerSize + contentLen;\n    byte[] slice = new byte[total];\n    Array.Copy(blob, offset, slice, 0, total);\n    return slice;\n}","typeGuard":null,"tryCatchPattern":"try { Asn1Object o = Asn1Object.FromByteArray(data); }\ncatch (IOException ex) when (ex.Message.Contains(\"extra data\"))\n{\n    // trailing bytes after first object: slice or loop-parse instead\n}","preventionTips":["Use Asn1InputStream.ReadObject in a loop for multi-object data","Trim arrays to the exact object length before FromByteArray","Fix offset/length arithmetic when extracting sub-blobs"],"tags":["asn1","der","parsing","extra-data"],"backgroundTag":"trailing-bytes-after-asn1-object","analyzedSha":"53fb989abc2219826385683a6fee826bd6cd38d6","analyzedAt":"2026-09-02T04:25:09.259Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T11:17:12.671Z"}