Ejemplo n.º 1
0
        public static void TagMustBeCorrect_Custom(PublicEncodingRules ruleSet)
        {
            byte[]    inputData = { 0x87, 2, (byte)'h', (byte)'i' };
            AsnReader reader    = new AsnReader(inputData, (AsnEncodingRules)ruleSet);

            AssertExtensions.Throws <ArgumentException>(
                "expectedTag",
                () => reader.TryGetIA5StringBytes(Asn1Tag.Null, out _));

            Assert.True(reader.HasData, "HasData after bad universal tag");

            Assert.Throws <CryptographicException>(() => reader.TryGetIA5StringBytes(out _));

            Assert.True(reader.HasData, "HasData after default tag");

            Assert.Throws <CryptographicException>(
                () => reader.TryGetIA5StringBytes(new Asn1Tag(TagClass.Application, 0), out _));

            Assert.True(reader.HasData, "HasData after wrong custom class");

            Assert.Throws <CryptographicException>(
                () => reader.TryGetIA5StringBytes(new Asn1Tag(TagClass.ContextSpecific, 1), out _));

            Assert.True(reader.HasData, "HasData after wrong custom tag value");

            Assert.True(
                reader.TryGetIA5StringBytes(
                    new Asn1Tag(TagClass.ContextSpecific, 7),
                    out ReadOnlyMemory <byte> value));

            Assert.Equal("6869", value.ByteArrayToHex());
            Assert.False(reader.HasData, "HasData after reading value");
        }
Ejemplo n.º 2
0
        public static void ExpectedTag_IgnoresConstructed(
            PublicEncodingRules ruleSet,
            string inputHex,
            PublicTagClass tagClass,
            int tagValue)
        {
            byte[]    inputData = inputHex.HexToByteArray();
            AsnReader reader    = new AsnReader(inputData, (AsnEncodingRules)ruleSet);

            Assert.True(
                reader.TryGetIA5StringBytes(
                    new Asn1Tag((TagClass)tagClass, tagValue, true),
                    out ReadOnlyMemory <byte> val1));

            Assert.False(reader.HasData);

            reader = new AsnReader(inputData, (AsnEncodingRules)ruleSet);

            Assert.True(
                reader.TryGetIA5StringBytes(
                    new Asn1Tag((TagClass)tagClass, tagValue, false),
                    out ReadOnlyMemory <byte> val2));

            Assert.False(reader.HasData);

            Assert.Equal(val1.ByteArrayToHex(), val2.ByteArrayToHex());
        }
Ejemplo n.º 3
0
        public static void TryGetIA5StringBytes(
            PublicEncodingRules ruleSet,
            string inputHex,
            bool expectSuccess)
        {
            byte[]    inputData = inputHex.HexToByteArray();
            AsnReader reader    = new AsnReader(inputData, (AsnEncodingRules)ruleSet);

            bool got = reader.TryGetIA5StringBytes(out ReadOnlyMemory <byte> contents);

            if (expectSuccess)
            {
                Assert.True(got, "reader.TryGetIA5StringBytes");

                Assert.True(
                    Unsafe.AreSame(
                        ref MemoryMarshal.GetReference(contents.Span),
                        ref inputData[2]));
            }
            else
            {
                Assert.False(got, "reader.TryGetIA5StringBytes");
                Assert.True(contents.IsEmpty, "contents.IsEmpty");
            }
        }
Ejemplo n.º 4
0
        public static void TryGetIA5StringBytes_Throws(
            string description,
            PublicEncodingRules ruleSet,
            string inputHex)
        {
            byte[]    inputData = inputHex.HexToByteArray();
            AsnReader reader    = new AsnReader(inputData, (AsnEncodingRules)ruleSet);

            Assert.Throws <CryptographicException>(
                () => reader.TryGetIA5StringBytes(out ReadOnlyMemory <byte> contents));
        }
Ejemplo n.º 5
0
        public static void TagMustBeCorrect_Universal(AsnEncodingRules ruleSet)
        {
            byte[]    inputData = { 0x16, 2, (byte)'e', (byte)'l' };
            AsnReader reader    = new AsnReader(inputData, ruleSet);

            AssertExtensions.Throws <ArgumentException>(
                "expectedTag",
                () => reader.TryGetIA5StringBytes(Asn1Tag.Null, out _));

            Assert.True(reader.HasData, "HasData after bad universal tag");

            Assert.Throws <AsnContentException>(
                () => reader.TryGetIA5StringBytes(new Asn1Tag(TagClass.ContextSpecific, 0), out _));

            Assert.True(reader.HasData, "HasData after wrong tag");

            Assert.True(reader.TryGetIA5StringBytes(out ReadOnlyMemory <byte> value));
            Assert.Equal("656C", value.ByteArrayToHex());
            Assert.False(reader.HasData, "HasData after read");
        }
Ejemplo n.º 6
0
        public static void ExpectedTag_IgnoresConstructed(
            AsnEncodingRules ruleSet,
            string inputHex,
            TagClass tagClass,
            int tagValue)
        {
            byte[]    inputData   = inputHex.HexToByteArray();
            Asn1Tag   correctCons = new Asn1Tag(tagClass, tagValue, true);
            Asn1Tag   correctPrim = new Asn1Tag(tagClass, tagValue, false);
            AsnReader reader      = new AsnReader(inputData, ruleSet);

            Assert.True(
                reader.TryGetIA5StringBytes(
                    correctCons,
                    out ReadOnlyMemory <byte> val1));

            Assert.False(reader.HasData);

            reader = new AsnReader(inputData, ruleSet);

            Assert.True(
                reader.TryGetIA5StringBytes(
                    correctPrim,
                    out ReadOnlyMemory <byte> val2));

            Assert.False(reader.HasData);

            Assert.Equal(val1.ByteArrayToHex(), val2.ByteArrayToHex());

#if NETCOREAPP
            string expected = Encoding.ASCII.GetString(val1.Span);
#else
            string expected = Encoding.ASCII.GetString(val1.ToArray());
#endif

            reader = new AsnReader(inputData, ruleSet);
            Assert.Equal(expected, reader.ReadCharacterString(UniversalTagNumber.IA5String, correctPrim));

            reader = new AsnReader(inputData, ruleSet);
            Assert.Equal(expected, reader.ReadCharacterString(UniversalTagNumber.IA5String, correctCons));

            char[] output = new char[28];

            reader = new AsnReader(inputData, ruleSet);

            Assert.True(
                reader.TryReadCharacterString(
                    output.AsSpan(1),
                    UniversalTagNumber.IA5String,
                    out int charsWritten,
                    correctPrim));

            Assert.Equal(expected, output.AsSpan(1, charsWritten).ToString());

            reader = new AsnReader(inputData, ruleSet);

            Assert.True(
                reader.TryReadCharacterString(
                    output.AsSpan(2),
                    UniversalTagNumber.IA5String,
                    out charsWritten,
                    correctCons));

            Assert.Equal(expected, output.AsSpan(2, charsWritten).ToString());
        }