Esempio n. 1
0
 public static bool TryCopyIA5StringBytes(
     this AsnReader reader,
     Span <byte> destination,
     out int bytesWritten)
 {
     return(reader.TryCopyCharacterStringBytes(
                UniversalTagNumber.IA5String,
                destination,
                out bytesWritten));
 }
Esempio n. 2
0
        public static void TryCopyUTF8StringBytes(
            PublicEncodingRules 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, (AsnEncodingRules)ruleSet);
            bool      copied;
            int       bytesWritten;

            if (output.Length > 0)
            {
                output[0] = 32;

                copied = reader.TryCopyCharacterStringBytes(
                    UniversalTagNumber.UTF8String,
                    output.AsSpan(0, output.Length - 1),
                    out bytesWritten);

                Assert.False(copied, "reader.TryCopyUTF8StringBytes - too short");
                Assert.Equal(0, bytesWritten);
                Assert.Equal(32, output[0]);
            }

            copied = reader.TryCopyCharacterStringBytes(
                UniversalTagNumber.UTF8String,
                output,
                out bytesWritten);

            Assert.True(copied, "reader.TryCopyUTF8StringBytes");

            Assert.Equal(
                expectedHex,
                new ReadOnlySpan <byte>(output, 0, bytesWritten).ByteArrayToHex());

            Assert.Equal(output.Length, bytesWritten);
        }
Esempio n. 3
0
        public static void TryCopyUTF8StringBytes_Throws(
            string description,
            PublicEncodingRules ruleSet,
            string inputHex)
        {
            byte[] inputData  = inputHex.HexToByteArray();
            byte[] outputData = new byte[inputData.Length + 1];
            outputData[0] = 252;

            int       bytesWritten = -1;
            AsnReader reader       = new AsnReader(inputData, (AsnEncodingRules)ruleSet);

            Assert.Throws <CryptographicException>(
                () => reader.TryCopyCharacterStringBytes(UniversalTagNumber.UTF8String, outputData, out bytesWritten));

            Assert.Equal(-1, bytesWritten);
            Assert.Equal(252, outputData[0]);
        }
Esempio n. 4
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.TryCopyCharacterStringBytes(
                UniversalTagNumber.UTF8String,
                output,
                out int bytesWritten);

            Assert.True(success, "reader.TryCopyUTF8StringBytes");
            Assert.Equal(1000, bytesWritten);

            Assert.Equal(
                input.AsReadOnlySpan().Slice(4).ByteArrayToHex(),
                output.ByteArrayToHex());
        }
Esempio n. 5
0
        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.TryCopyCharacterStringBytes(
                UniversalTagNumber.UTF8String,
                output,
                out int bytesWritten);

            Assert.True(success, "reader.TryCopyUTF8StringBytes");
            Assert.Equal(1001, bytesWritten);

            Assert.Equal(
                expected.ByteArrayToHex(),
                output.ByteArrayToHex());
        }