{"record":{"id":"62f89977112148a4","repo":"peass-ng/PEASS-ng","slug":"encoding-error-in-getinstance","errorCode":null,"errorMessage":"encoding error in GetInstance: ","messagePattern":"encoding error in GetInstance: ","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"winPEAS/winPEASexe/winPEAS/3rdParty/BouncyCastle/asn1/DerBitString.cs","lineNumber":38,"sourceCode":"\t\t *\n\t\t * @exception ArgumentException if the object cannot be converted.\n\t\t */\n        public static DerBitString GetInstance(\n            object obj)\n        {\n            if (obj == null || obj is DerBitString)\n            {\n                return (DerBitString)obj;\n            }\n            if (obj is byte[])\n            {\n                try\n                {\n                    return (DerBitString)FromByteArray((byte[])obj);\n                }\n                catch (Exception e)\n                {\n                    throw new ArgumentException(\"encoding error in GetInstance: \" + e.ToString());\n                }\n            }\n\n            throw new ArgumentException(\"illegal object in GetInstance: \" + Platform.GetTypeName(obj));\n        }\n\n        /**\n\t\t * return a Bit string from a tagged object.\n\t\t *\n\t\t * @param obj the tagged object holding the object we want\n\t\t * @param explicitly true if the object is meant to be explicitly\n\t\t *              tagged false otherwise.\n\t\t * @exception ArgumentException if the tagged object cannot\n\t\t *               be converted.\n\t\t */\n        public static DerBitString GetInstance(\n            Asn1TaggedObject obj,\n            bool isExplicit)","sourceCodeStart":20,"sourceCodeEnd":56,"githubUrl":"https://github.com/peass-ng/PEASS-ng/blob/53fb989abc2219826385683a6fee826bd6cd38d6/winPEAS/winPEASexe/winPEAS/3rdParty/BouncyCastle/asn1/DerBitString.cs#L20-L56","documentation":"DerBitString.GetInstance, given a byte[], parses it as an ASN.1 object and casts to DerBitString; any exception in that parse/cast is rethrown as ArgumentException 'encoding error in GetInstance: ' + exception details. The byte[] did not decode to a BIT STRING.","triggerScenarios":"Calling DerBitString.GetInstance(byte[]) where the bytes encode something other than a BIT STRING (e.g. a SEQUENCE or OCTET STRING), or are not valid ASN.1 at all (the cast to DerBitString also throws InvalidCastException).","commonSituations":"Extracting signature/keys bits from a structure and passing the wrong embedded object; passing raw bits that were never DER-encoded; parsing a structure whose field order was misread.","solutions":["Confirm the byte[] is a DER BIT STRING (first byte 0x03, then length, then unused-bits count)","If the bytes are raw bit data, wrap directly: new DerBitString(bytes)","Parse generically first (Asn1Object.FromByteArray) and check the concrete type before casting","Read the appended e.ToString() to see the exact underlying failure"],"exampleFix":"// before\nDerBitString bits = DerBitString.GetInstance(rawBits); // raw, not DER\n// after\nDerBitString bits = new DerBitString(rawBits); // wrap raw data directly\n// or, when unsure of the encoded type:\nAsn1Object o = Asn1Object.FromByteArray(bytes);\nif (o is DerBitString bs) { /* use bs */ }","handlingStrategy":"type-guard","validationCode":"bool IsBitStringEncoding(byte[] data) { return data.Length >= 2 && data[0] == 0x03; }","typeGuard":"bool IsDerBitString(byte[] data) { try { return Asn1Object.FromByteArray(data) is DerBitString; } catch { return false; } }","tryCatchPattern":"try { return DerBitString.GetInstance(bytes); }\ncatch (ArgumentException ex) { log.Warn(\"not a BIT STRING encoding: \" + ex.Message); return null; }","preventionTips":["Use new DerBitString(rawBits) for raw bit data; GetInstance only for DER-encoded data","Confirm the 0x03 tag byte before GetInstance on opaque bytes","Verify you selected the correct field (signature value vs algorithm identifier) in parent structures"],"tags":["asn1","bit-string","argument","bouncycastle"],"backgroundTag":"invalid-asn1-data","analyzedSha":"53fb989abc2219826385683a6fee826bd6cd38d6","analyzedAt":"2026-09-02T04:25:09.259Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T11:17:12.671Z"}