{"record":{"id":"6dcaf5142b3a1324","repo":"peass-ng/PEASS-ng","slug":"malformed-bmpstring-encoding-encountered-6dcaf5","errorCode":null,"errorMessage":"malformed BMPString encoding encountered","messagePattern":"malformed BMPString encoding encountered","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"winPEAS/winPEASexe/winPEAS/3rdParty/BouncyCastle/asn1/DerBmpString.cs","lineNumber":65,"sourceCode":"            {\n                return GetInstance(o);\n            }\n\n            return new DerBmpString(Asn1OctetString.GetInstance(o).GetOctets());\n        }\n\n        /**\n         * basic constructor - byte encoded string.\n         */\n        [Obsolete(\"Will become internal\")]\n        public DerBmpString(byte[] str)\n        {\n            if (str == null)\n                throw new ArgumentNullException(\"str\");\n\n            int byteLen = str.Length;\n            if (0 != (byteLen & 1))\n                throw new ArgumentException(\"malformed BMPString encoding encountered\", \"str\");\n\n            int charLen = byteLen / 2;\n            char[] cs = new char[charLen];\n\n            for (int i = 0; i != charLen; i++)\n            {\n                cs[i] = (char)((str[2 * i] << 8) | (str[2 * i + 1] & 0xff));\n            }\n\n            this.str = new string(cs);\n        }\n\n        internal DerBmpString(char[] str)\n        {\n            if (str == null)\n                throw new ArgumentNullException(\"str\");\n\n            this.str = new string(str);","sourceCodeStart":47,"sourceCodeEnd":83,"githubUrl":"https://github.com/peass-ng/PEASS-ng/blob/53fb989abc2219826385683a6fee826bd6cd38d6/winPEAS/winPEASexe/winPEAS/3rdParty/BouncyCastle/asn1/DerBmpString.cs#L47-L83","documentation":"BMPString is UCS-2 encoded, so its byte payload must contain an even number of bytes (2 per character). The byte[] constructor throws ArgumentException when the length is odd, because the trailing byte cannot form a character.","triggerScenarios":"Calling new DerBmpString(byteArray) where byteArray.Length is odd — e.g. a buffer sliced mid-character, concatenated with a stray byte, or produced by a non-UCS-2 encoder (UTF-8/ASCII bytes).","commonSituations":"Feeding UTF-8 or ASCII bytes into DerBmpString instead of Encoding.BigEndianUnicode bytes; truncating a buffer by one byte; manual DER parsing with a wrong length.","solutions":["Encode with Encoding.BigEndianUnicode.GetBytes(text) so the array is always even-length.","Fix the slicing/concatenation that produced an odd-length buffer.","Construct from a string (new DerBmpString(string)) and let the library encode correctly."],"exampleFix":"// before\nvar bmp = new DerBmpString(Encoding.UTF8.GetBytes(name)); // odd length possible\n// after\nvar bmp = new DerBmpString(name); // or Encoding.BigEndianUnicode.GetBytes(name)","handlingStrategy":"validation","validationCode":"if (str == null || (str.Length & 1) != 0) throw new InvalidDataException(\"BMPString byte length must be even (UCS-2)\");","typeGuard":"static bool IsWellFormedBmpBytes(byte[] b) => b != null && (b.Length & 1) == 0;","tryCatchPattern":null,"preventionTips":["Encode BMPStrings with Encoding.BigEndianUnicode, never UTF-8/ASCII","Verify buffer slicing doesn't cut a 2-byte character","Construct from string and let the library encode"],"tags":["csharp","asn1","encoding","bouncycastle","bmpstring"],"backgroundTag":"invalid-string-encoding","analyzedSha":"53fb989abc2219826385683a6fee826bd6cd38d6","analyzedAt":"2026-09-02T04:25:09.259Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T11:17:12.671Z"}