peass-ng/PEASS-ng · error · ArgumentException

illegal object in GetInstance:

Error message

illegal object in GetInstance: 

What it means

This GetInstance overload performs a strict cast: it only accepts null or an existing DerGeneralString and returns it unchanged; it does not convert other object types. Anything else (e.g. DerUtf8String, DerPrintableString, byte[]) triggers ArgumentException naming the actual runtime type.

Source

Thrown at winPEAS/winPEASexe/winPEAS/3rdParty/BouncyCastle/asn1/DerGeneralString.cs:20

using winPEAS._3rdParty.BouncyCastle.crypto.util;
using winPEAS._3rdParty.BouncyCastle.util;

namespace winPEAS._3rdParty.BouncyCastle.asn1
{
    public class DerGeneralString
       : DerStringBase
    {
        private readonly string str;

        public static DerGeneralString GetInstance(
            object obj)
        {
            if (obj == null || obj is DerGeneralString)
            {
                return (DerGeneralString)obj;
            }

            throw new ArgumentException("illegal object in GetInstance: "
                    + Platform.GetTypeName(obj));
        }

        public static DerGeneralString GetInstance(
            Asn1TaggedObject obj,
            bool isExplicit)
        {
            Asn1Object o = obj.GetObject();

            if (isExplicit || o is DerGeneralString)
            {
                return GetInstance(o);
            }

            return new DerGeneralString(((Asn1OctetString)o).GetOctets());
        }

        public DerGeneralString(

View on GitHub (pinned to 53fb989abc)

Solutions

  1. Use the correct GetInstance for the actual type (e.g. DerUtf8String.GetInstance) or convert via obj.ToString()/GetString()
  2. If any string type is acceptable, check 'obj is DerGeneralString' first and otherwise decode via Asn1Object.GetEncoded re-parse or accept a DerString base
  3. Verify the ASN.1 module definition to confirm the field really is GeneralString

Example fix

// before
var gs = (DerGeneralString)DerGeneralString.GetInstance(obj); // throws if obj is DerUtf8String
// after
var gs = obj is DerGeneralString g ? g
       : new DerGeneralString(obj is Asn1String s ? s.GetString() : obj.ToString());
Defensive patterns

Strategy: type-guard

Validate before calling

if (obj is DerGeneralString gs) { /* use gs */ }

Type guard

static bool IsDerGeneralString(Asn1Object o) => o is DerGeneralString;

Try / catch

try { var gs = DerGeneralString.GetInstance(obj); }
catch (ArgumentException ex) when (ex.Message.StartsWith("illegal object in GetInstance")) { /* convert */ }

Prevention

When it happens

Trigger: Calling DerGeneralString.GetInstance(obj) with an Asn1Object that is a different string type (DerUtf8String, DerPrintableString, DerBmpString, etc.) or a non-ASN.1 object.

Common situations: Parsing certificates/protocols where a field encoded as another ASN.1 string type is fetched as GeneralString; code that assumed GetInstance converts between string types; changes in encoding by the data producer.

Related errors


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