Ejemplo n.º 1
0
        public static void TryCopyBMPStringBytes_Success_CER_MaxPrimitiveLength()
        {
            // CER says that the maximum encoding length for a BMPString primitive
            // is 1000.
            //
            // So we need 1E [1000] { 1000 anythings }
            // 1000 => 0x3E8, so the length encoding is 82 03 E8.
            // 1000 + 3 + 1 == 1004
            byte[] input = new byte[1004];
            input[0] = 0x1E;
            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.TryCopyBMPStringBytes(output, out int bytesWritten);

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

            Assert.Equal(
                input.AsSpan(4).ByteArrayToHex(),
                output.ByteArrayToHex());
        }
Ejemplo n.º 2
0
        public static void TryCopyBMPStringBytes(
            PublicEncodingRules ruleSet,
            string inputHex,
            string expectedString)
        {
            byte[] inputData   = inputHex.HexToByteArray();
            string expectedHex = Text.Encoding.BigEndianUnicode.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.TryCopyBMPStringBytes(output.AsSpan(0, output.Length - 1),
                                                      out bytesWritten);

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

            copied = reader.TryCopyBMPStringBytes(output,
                                                  out bytesWritten);

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

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

            Assert.Equal(output.Length, bytesWritten);
        }
Ejemplo n.º 3
0
        public static void TryCopyBMPStringBytes_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.TryCopyBMPStringBytes(outputData, out bytesWritten);
            });

            Assert.Equal(-1, bytesWritten);
            Assert.Equal(252, outputData[0]);
        }
Ejemplo n.º 4
0
        public static void TryCopyBMPStringBytes_Success_CER_MinConstructedLength()
        {
            // CER says that the maximum encoding length for a BMPString 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]
            //
            // 3E 80 (indefinite constructed BMPString)
            //    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 BMPSTRING (Indefinite)
            input[offset++] = 0x3E;
            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.TryCopyBMPStringBytes(output, out int bytesWritten);

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

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