Ejemplo n.º 1
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.TryGetPrimitiveCharacterStringBytes(
                    new Asn1Tag((TagClass)tagClass, tagValue, true),
                    UniversalTagNumber.UTF8String,
                    out ReadOnlyMemory <byte> val1));

            Assert.False(reader.HasData);

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

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

            Assert.False(reader.HasData);

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

            bool got = reader.TryGetPrimitiveCharacterStringBytes(
                UniversalTagNumber.UTF8String,
                out ReadOnlyMemory <byte> contents);

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

                Assert.True(
                    Unsafe.AreSame(
                        ref MemoryMarshal.GetReference(contents.Span),
                        ref inputData[2]));
            }
            else
            {
                Assert.False(got, "reader.TryGetUTF8StringBytes");
                Assert.True(contents.IsEmpty, "contents.IsEmpty");
            }
        }
Ejemplo n.º 3
0
 public static bool TryGetIA5StringBytes(
     this AsnReader reader,
     out ReadOnlyMemory <byte> contents)
 {
     return(reader.TryGetPrimitiveCharacterStringBytes(
                UniversalTagNumber.IA5String,
                out contents));
 }
Ejemplo n.º 4
0
 public static bool TryGetBMPStringBytes(
     this AsnReader reader,
     Asn1Tag expectedTag,
     out ReadOnlyMemory <byte> contents)
 {
     return(reader.TryGetPrimitiveCharacterStringBytes(
                expectedTag,
                UniversalTagNumber.BMPString,
                out contents));
 }
Ejemplo n.º 5
0
        public static void TryGetUTF8StringBytes_Throws(
            string description,
            PublicEncodingRules ruleSet,
            string inputHex)
        {
            byte[]    inputData = inputHex.HexToByteArray();
            AsnReader reader    = new AsnReader(inputData, (AsnEncodingRules)ruleSet);

            Assert.Throws <CryptographicException>(
                () => reader.TryGetPrimitiveCharacterStringBytes(UniversalTagNumber.UTF8String, out _));
        }
Ejemplo n.º 6
0
        public static void TagMustBeCorrect_Universal(PublicEncodingRules ruleSet)
        {
            byte[]    inputData = { 0x0C, 2, (byte)'e', (byte)'l' };
            AsnReader reader    = new AsnReader(inputData, (AsnEncodingRules)ruleSet);
            const UniversalTagNumber EncodingType = UniversalTagNumber.UTF8String;

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

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

            Assert.Throws <CryptographicException>(
                () => reader.TryGetPrimitiveCharacterStringBytes(new Asn1Tag(TagClass.ContextSpecific, 0), EncodingType, out _));

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

            Assert.True(reader.TryGetPrimitiveCharacterStringBytes(EncodingType, out ReadOnlyMemory <byte> value));
            Assert.Equal("656C", value.ByteArrayToHex());
            Assert.False(reader.HasData, "HasData after read");
        }