peass-ng/PEASS-ng · error · ArgumentException

illegal object in GetInstance:

Error message

illegal object in GetInstance: 

What it means

DerBmpString.GetInstance(object) only accepts null or an actual DerBmpString instance; any other object type causes ArgumentException with 'illegal object in GetInstance'. This is BouncyCastle's standard type-assertion pattern for ASN.1 string conversions.

Source

Thrown at winPEAS/winPEASexe/winPEAS/3rdParty/BouncyCastle/asn1/DerBmpString.cs:28

        : DerStringBase
    {
        private readonly string str;

        /**
         * return a BMP string from the given object.
         *
         * @param obj the object we want converted.
         * @exception ArgumentException if the object cannot be converted.
         */
        public static DerBmpString GetInstance(
            object obj)
        {
            if (obj == null || obj is DerBmpString)
            {
                return (DerBmpString)obj;
            }

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

        /**
         * return a BMP string from a tagged object.
         *
         * @param obj the tagged object holding the object we want
         * @param explicitly true if the object is meant to be explicitly
         *              tagged false otherwise.
         * @exception ArgumentException if the tagged object cannot
         *              be converted.
         */
        public static DerBmpString GetInstance(
            Asn1TaggedObject obj,
            bool isExplicit)
        {
            Asn1Object o = obj.GetObject();

            if (isExplicit || o is DerBmpString)

View on GitHub (pinned to 53fb989abc)

Solutions

  1. Call DerBmpString.GetInstance(obj, false) / GetInstance(asn1TaggedObject, false) only when the encoded type is truly BMPString ( UniversalString tag 30).
  2. Use the explicit-type overload GetInstance(Asn1TaggedObject, bool explicitly, Type) or check obj is DerBmpString first.
  3. If the type can vary, convert via Asn1Object.GetInstance and branch on the concrete type, or use the string's Getstring().

Example fix

// before
var s = DerBmpString.GetInstance(taggedObj.GetObject()); // may be another string type
// after
if (obj is DerBmpString bmp) { /* use bmp */ }
else if (obj is DerUtf8String u) { /* handle utf8 */ }
else throw new InvalidDataException("unexpected ASN.1 string type: " + obj.GetType());
Defensive patterns

Strategy: type-guard

Validate before calling

if (!(obj is DerBmpString)) { /* it is another ASN.1 type — branch accordingly */ }

Type guard

static bool IsBmpString(object o) => o is DerBmpString;

Try / catch

try { var s = DerBmpString.GetInstance(obj); }
catch (ArgumentException) { /* obj was a different ASN.1 type: inspect obj.GetType() */ }

Prevention

When it happens

Trigger: Calling DerBmpString.GetInstance(obj) where obj is a DerUtf8String, DerPrintableString, DerOctetString, Asn1Sequence, byte[], or any other non-DerBmpString ASN.1 object — commonly after reading an untyped Asn1Object from a tag.

Common situations: Parsing certificates with a generic GetInstance on an explicitly tagged value without specifying the expected type; assuming a string-typed ASN.1 field is BMPString when it is UTF8String or PrintableString.

Related errors


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