public static void TryReadCharacterStringBytes_WrongKind() { byte[] inputData = "16026869".HexToByteArray(); AsnReader reader = new AsnReader(inputData, AsnEncodingRules.BER); AssertExtensions.Throws <ArgumentException>( "expectedTag", () => reader.TryReadCharacterStringBytes(Span <byte> .Empty, Asn1Tag.Boolean, out _)); }
public static bool TryCopyBMPStringBytes( this AsnReader reader, Span <byte> destination, out int bytesWritten) { return(reader.TryReadCharacterStringBytes( destination, new Asn1Tag(UniversalTagNumber.BMPString), out bytesWritten)); }
public static void TryCopyUTF8StringBytes( AsnEncodingRules ruleSet, string inputHex, string expectedString) { byte[] inputData = inputHex.HexToByteArray(); string expectedHex = Text.Encoding.UTF8.GetBytes(expectedString).ByteArrayToHex(); byte[] output = new byte[expectedHex.Length / 2]; AsnReader reader = new AsnReader(inputData, ruleSet); bool copied; int bytesWritten; if (output.Length > 0) { output[0] = 32; copied = reader.TryReadCharacterStringBytes( output.AsSpan(0, output.Length - 1), new Asn1Tag(UniversalTagNumber.UTF8String), out bytesWritten); Assert.False(copied, "reader.TryCopyUTF8StringBytes - too short"); Assert.Equal(0, bytesWritten); Assert.Equal(32, output[0]); } copied = reader.TryReadCharacterStringBytes( output, new Asn1Tag(UniversalTagNumber.UTF8String), out bytesWritten); Assert.True(copied, "reader.TryCopyUTF8StringBytes"); Assert.Equal( expectedHex, new ReadOnlySpan <byte>(output, 0, bytesWritten).ByteArrayToHex()); Assert.Equal(output.Length, bytesWritten); }
public static void TryCopyUTF8StringBytes_Throws( string description, AsnEncodingRules ruleSet, string inputHex) { _ = description; byte[] inputData = inputHex.HexToByteArray(); byte[] outputData = new byte[inputData.Length + 1]; outputData[0] = 252; int bytesWritten = -1; AsnReader reader = new AsnReader(inputData, ruleSet); Assert.Throws <AsnContentException>( () => reader.TryReadCharacterStringBytes( outputData, new Asn1Tag(UniversalTagNumber.UTF8String), out bytesWritten)); Assert.Equal(-1, bytesWritten); Assert.Equal(252, outputData[0]); }
public static void TryCopyUTF8StringBytes_Success_CER_MaxPrimitiveLength() { // CER says that the maximum encoding length for a UTF8String primitive // is 1000. // // So we need 0C [1000] { 1000 anythings } // 1000 => 0x3E8, so the length encoding is 82 03 E8. // 1000 + 3 + 1 == 1004 byte[] input = new byte[1004]; input[0] = 0x0C; input[1] = 0x82; input[2] = 0x03; input[3] = 0xE8; // Content input[4] = 0x65; input[5] = 0x65; input[1002] = 0x61; input[1003] = 0x61; byte[] output = new byte[1000]; AsnReader reader = new AsnReader(input, AsnEncodingRules.CER); bool success = reader.TryReadCharacterStringBytes( output, new Asn1Tag(UniversalTagNumber.UTF8String), out int bytesWritten); Assert.True(success, "reader.TryCopyUTF8StringBytes"); Assert.Equal(1000, bytesWritten); Assert.Equal( input.AsSpan(4).ByteArrayToHex(), output.ByteArrayToHex()); }
public static void TryCopyUTF8StringBytes_Success_CER_MinConstructedLength() { // CER says that the maximum encoding length for a UTF8String primitive // is 1000, and that a constructed form must be used for values greater // than 1000 bytes, with segments dividing up for each thousand // [1000, 1000, ..., len%1000]. // // So our smallest constructed form is 1001 bytes, [1000, 1] // // 2C 80 (indefinite constructed utf8 string) // 04 82 03 E9 (primitive octet string, 1000 bytes) // [1000 content bytes] // 04 01 (primitive octet string, 1 byte) // pp // 00 00 (end of contents, 0 bytes) // 1011 total. byte[] input = new byte[1011]; int offset = 0; // CONSTRUCTED UTF8 STRING (Indefinite) input[offset++] = 0x2C; input[offset++] = 0x80; // OCTET STRING (1000) input[offset++] = 0x04; input[offset++] = 0x82; input[offset++] = 0x03; input[offset++] = 0xE8; // Primitive 1: (65 65 :: 61 61) (1000) input[offset++] = 0x65; input[offset] = 0x65; offset += 997; input[offset++] = 0x61; input[offset++] = 0x61; // OCTET STRING (1) input[offset++] = 0x04; input[offset++] = 0x01; // Primitive 2: One more byte input[offset] = 0x2E; byte[] expected = new byte[1001]; offset = 0; expected[offset++] = 0x65; expected[offset] = 0x65; offset += 997; expected[offset++] = 0x61; expected[offset++] = 0x61; expected[offset] = 0x2E; byte[] output = new byte[1001]; AsnReader reader = new AsnReader(input, AsnEncodingRules.CER); bool success = reader.TryReadCharacterStringBytes( output, new Asn1Tag(UniversalTagNumber.UTF8String), out int bytesWritten); Assert.True(success, "reader.TryCopyUTF8StringBytes"); Assert.Equal(1001, bytesWritten); Assert.Equal( expected.ByteArrayToHex(), output.ByteArrayToHex()); }