peass-ng/PEASS-ng · error · InvalidOperationException

malformed object

Error message

malformed object

What it means

The DerApplicationSpecific constructor that concatenates DER encodings of a vector of Asn1Encodable elements catches IOException from GetDerEncoded and rethrows InvalidOperationException 'malformed object'. An element in the vector could not produce its DER encoding.

Source

Thrown at winPEAS/winPEASexe/winPEAS/3rdParty/BouncyCastle/asn1/DerApplicationSpecific.cs:83

		public DerApplicationSpecific(
			int tagNo,
			Asn1EncodableVector vec)
		{
			this.tag = tagNo;
			this.isConstructed = true;
			MemoryStream bOut = new MemoryStream();

			for (int i = 0; i != vec.Count; i++)
			{
				try
				{
					byte[] bs = vec[i].GetDerEncoded();
					bOut.Write(bs, 0, bs.Length);
				}
				catch (IOException e)
				{
					throw new InvalidOperationException("malformed object", e);
				}
			}
			this.octets = bOut.ToArray();
		}

		private int GetLengthOfHeader(
			byte[] data)
		{
			int length = data[1]; // TODO: assumes 1 byte tag

			if (length == 0x80)
			{
				return 2;      // indefinite-length encoding
			}

			if (length > 127)
			{
				int size = length & 0x7f;

View on GitHub (pinned to 53fb989abc)

Solutions

  1. Validate each element encodes cleanly: call element.GetDerEncoded() in try/catch before adding to the vector
  2. Inspect e.InnerException for the real encoding failure
  3. Rebuild the offending element from verified input data
  4. Ensure nested objects were successfully parsed (catch earlier IOExceptions rather than letting malformed objects propagate)

Example fix

// before
var vec = new Asn1EncodableVector(maybeBrokenObj);
var app = new DerApplicationSpecific(1, vec); // throws
// after
byte[] enc;
try { enc = maybeBrokenObj.GetDerEncoded(); }
catch (IOException ex) { throw new InvalidDataException("element not encodable", ex); }
var vec = new Asn1EncodableVector(Asn1Object.FromByteArray(enc));
var app = new DerApplicationSpecific(1, vec);
Defensive patterns

Strategy: try-catch

Validate before calling

// pre-verify each element is encodable
foreach (Asn1Encodable e in elements) { try { e.GetDerEncoded(); } catch (IOException) { return false; } }

Try / catch

try { var app = new DerApplicationSpecific(tagNo, vec); }
catch (InvalidOperationException ex) { log.Error("element not DER-encodable", ex.InnerException); return null; }

Prevention

When it happens

Trigger: Building new DerApplicationSpecific(tagNo, Asn1EncodableVector) where one of the vector's elements throws during GetDerEncoded — e.g. an improperly initialized or recursively malformed nested object.

Common situations: Assembling application-specific structures for protocols (e.g. custom PKCS objects) with a nested object built from bad data; reusing Asn1Encodable objects after failed parsing.

Understand the failure class

Related errors


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