peass-ng/PEASS-ng · error · IOException

cannot recognise object in byte array

Error message

cannot recognise object in byte array

What it means

FromByteArray wraps parsing in a catch for InvalidCastException (from GetInstance-style casts inside decoding) and rethrows it as IOException 'cannot recognise object in byte array'. The bytes did not decode into a recognizable ASN.1 object.

Source

Thrown at winPEAS/winPEASexe/winPEAS/3rdParty/BouncyCastle/asn1/Asn1Object.cs:29

		/// <returns>The base ASN.1 object represented by the byte array.</returns>
		/// <exception cref="IOException">
		/// If there is a problem parsing the data, or parsing an object did not exhaust the available data.
		/// </exception>
		public static Asn1Object FromByteArray(
			byte[] data)
		{
			try
			{
				MemoryStream input = new MemoryStream(data, false);
				Asn1InputStream asn1 = new Asn1InputStream(input, data.Length);
				Asn1Object result = asn1.ReadObject();
				if (input.Position != input.Length)
					throw new IOException("extra data found after object");
				return result;
			}
			catch (InvalidCastException)
			{
				throw new IOException("cannot recognise object in byte array");
			}
		}

		/// <summary>Read a base ASN.1 object from a stream.</summary>
		/// <param name="inStr">The stream to parse.</param>
		/// <returns>The base ASN.1 object represented by the byte array.</returns>
		/// <exception cref="IOException">If there is a problem parsing the data.</exception>
		public static Asn1Object FromStream(
			Stream inStr)
		{
			try
			{
				return new Asn1InputStream(inStr).ReadObject();
			}
			catch (InvalidCastException)
			{
				throw new IOException("cannot recognise object in stream");
			}

View on GitHub (pinned to 53fb989abc)

Solutions

  1. Confirm the input is binary DER; if it is PEM, strip headers and Convert.FromBase64String first
  2. Check the format your key/cert actually is (PKCS#1 vs PKCS#8 vs X.509) and use the right parser
  3. Hex-dump the first bytes: a DER object starts with a valid tag byte (e.g. 0x30 for SEQUENCE)
  4. Catch IOException and fall back to alternative parsers or report a format error

Example fix

// before
string pem = File.ReadAllText("cert.pem");
Asn1Object o = Asn1Object.FromByteArray(Encoding.UTF8.GetBytes(pem));
// after
string b64 = pem.Replace("-----BEGIN CERTIFICATE-----", "")
                .Replace("-----END CERTIFICATE-----", "");
Asn1Object o = Asn1Object.FromByteArray(Convert.FromBase64String(b64));
Defensive patterns

Strategy: validation

Validate before calling

static bool LooksLikeBinaryDer(byte[] data)
{
    if (data == null || data.Length < 2) return false;
    if (data[0] == '-' || (data[0] >= 32 && data[0] < 127 && data[1] >= 32 && data[1] < 127))
        return false; // ASCII/PEM text, not DER
    return true; // plausible binary
}

Try / catch

try { Asn1Object o = Asn1Object.FromByteArray(data); }
catch (IOException ex) when (ex.Message.Contains("cannot recognise"))
{
    // not valid ASN.1: check PEM vs DER vs raw key format
}

Prevention

When it happens

Trigger: Input that is not valid ASN.1/DER at all — e.g. PEM text, a random key blob, or bytes whose leading tag/length bytes cause an invalid cast during object construction.

Common situations: Forgetting to base64-decode PEM before parsing; passing a raw key (e.g. RSA Parameters or PKCS#8 without proper handling) to an ASN.1 API; wrong file format (DER vs PEM vs raw).

Related errors


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