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

            AssertExtensions.Throws <ArgumentException>(
                "expectedTag",
                () => reader.GetEnumeratedValue <ShortBacked>(Asn1Tag.Null));

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

            Assert.Throws <CryptographicException>(() => reader.GetEnumeratedValue <ShortBacked>());

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

            Assert.Throws <CryptographicException>(
                () => reader.GetEnumeratedValue <ShortBacked>(new Asn1Tag(TagClass.Application, 0)));

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

            Assert.Throws <CryptographicException>(
                () => reader.GetEnumeratedValue <ShortBacked>(new Asn1Tag(TagClass.ContextSpecific, 1)));

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

            ShortBacked value = reader.GetEnumeratedValue <ShortBacked>(new Asn1Tag(TagClass.ContextSpecific, 7));

            Assert.Equal((ShortBacked)0x80, value);
            Assert.False(reader.HasData, "HasData after reading value");
        }
Exemplo n.º 2
0
        public static void GetEnumeratedValue_NonEnumType(PublicEncodingRules ruleSet)
        {
            byte[]    data   = { 0x0A, 0x01, 0x00 };
            AsnReader reader = new AsnReader(data, (AsnEncodingRules)ruleSet);

            Assert.Throws <ArgumentException>(() => reader.GetEnumeratedValue <Guid>());
        }
Exemplo n.º 3
0
        public static void GetEnumeratedValue_Invalid_ULong(PublicEncodingRules ruleSet, string inputHex)
        {
            byte[]    inputData = inputHex.HexToByteArray();
            AsnReader reader    = new AsnReader(inputData, (AsnEncodingRules)ruleSet);

            Assert.Throws <CryptographicException>(() => reader.GetEnumeratedValue <ULongBacked>());
        }
Exemplo n.º 4
0
        public static void GetEnumeratedValue_FlagsEnum(PublicEncodingRules ruleSet)
        {
            byte[]    data   = { 0x0A, 0x01, 0x00 };
            AsnReader reader = new AsnReader(data, (AsnEncodingRules)ruleSet);

            AssertExtensions.Throws <ArgumentException>(
                "tEnum",
                () => reader.GetEnumeratedValue <AssemblyFlags>());
        }
Exemplo n.º 5
0
        public static void TagMustBeCorrect_Universal(PublicEncodingRules ruleSet)
        {
            byte[]    inputData = { 0x0A, 1, 0x7E };
            AsnReader reader    = new AsnReader(inputData, (AsnEncodingRules)ruleSet);

            AssertExtensions.Throws <ArgumentException>(
                "expectedTag",
                () => reader.GetEnumeratedValue <ShortBacked>(Asn1Tag.Null));

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

            Assert.Throws <CryptographicException>(
                () => reader.GetEnumeratedValue <ShortBacked>(new Asn1Tag(TagClass.ContextSpecific, 0)));

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

            ShortBacked value = reader.GetEnumeratedValue <ShortBacked>();

            Assert.Equal((ShortBacked)0x7E, value);
            Assert.False(reader.HasData, "HasData after read");
        }
Exemplo n.º 6
0
        private static void GetExpectedValue <TEnum>(
            PublicEncodingRules ruleSet,
            TEnum expectedValue,
            string inputHex)
            where TEnum : struct
        {
            byte[]    inputData = inputHex.HexToByteArray();
            AsnReader reader    = new AsnReader(inputData, (AsnEncodingRules)ruleSet);
            TEnum     value     = reader.GetEnumeratedValue <TEnum>();

            Assert.Equal(expectedValue, value);
        }
Exemplo n.º 7
0
        internal static void Decode(AsnReader reader, Asn1Tag expectedTag, out Asn1Change decoded)
        {
            if (reader == null)
            {
                throw new ArgumentNullException(nameof(reader));
            }

            decoded = new Asn1Change();
            AsnReader sequenceReader = reader.ReadSequence(expectedTag);

            decoded.Operation = sequenceReader.GetEnumeratedValue <ChangeOperation>();
            Asn1PartialAttribute.Decode(sequenceReader, out decoded.Modification);

            sequenceReader.ThrowIfNotEmpty();
        }
Exemplo n.º 8
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);
            ShortBacked val1      = reader.GetEnumeratedValue <ShortBacked>(new Asn1Tag((TagClass)tagClass, tagValue, true));

            Assert.False(reader.HasData);
            reader = new AsnReader(inputData, (AsnEncodingRules)ruleSet);
            ShortBacked val2 = reader.GetEnumeratedValue <ShortBacked>(new Asn1Tag((TagClass)tagClass, tagValue, false));

            Assert.False(reader.HasData);

            Assert.Equal(val1, val2);
        }
Exemplo n.º 9
0
        internal static void Decode(AsnReader reader, Asn1Tag expectedTag, out Asn1LDAPResult decoded)
        {
            if (reader == null)
            {
                throw new ArgumentNullException(nameof(reader));
            }

            decoded = new Asn1LDAPResult();
            AsnReader sequenceReader = reader.ReadSequence(expectedTag);
            AsnReader collectionReader;

            decoded.ResultCode = sequenceReader.GetEnumeratedValue <ResultCode>();

            if (sequenceReader.TryGetPrimitiveOctetStringBytes(out ReadOnlyMemory <byte> tmpMatchedDN))
            {
                decoded.MatchedDN = tmpMatchedDN;
            }
            else
            {
                decoded.MatchedDN = sequenceReader.ReadOctetString();
            }


            if (sequenceReader.TryGetPrimitiveOctetStringBytes(out ReadOnlyMemory <byte> tmpDiagnosticMessage))
            {
                decoded.DiagnosticMessage = tmpDiagnosticMessage;
            }
            else
            {
                decoded.DiagnosticMessage = sequenceReader.ReadOctetString();
            }


            if (sequenceReader.HasData && sequenceReader.PeekTag().HasSameClassAndValue(new Asn1Tag(TagClass.ContextSpecific, 3)))
            {
                // Decode SEQUENCE OF for Referral
                {
                    collectionReader = sequenceReader.ReadSequence(new Asn1Tag(TagClass.ContextSpecific, 3));
                    var tmpList = new List <ReadOnlyMemory <byte> >();
                    ReadOnlyMemory <byte> tmpItem;

                    while (collectionReader.HasData)
                    {
                        if (collectionReader.TryGetPrimitiveOctetStringBytes(out ReadOnlyMemory <byte> tmp))
                        {
                            tmpItem = tmp;
                        }
                        else
                        {
                            tmpItem = collectionReader.ReadOctetString();
                        }

                        tmpList.Add(tmpItem);
                    }

                    decoded.Referral = tmpList.ToArray();
                }
            }


            sequenceReader.ThrowIfNotEmpty();
        }
Exemplo n.º 10
0
        internal static void Decode(AsnReader reader, Asn1Tag expectedTag, out Asn1SearchRequest decoded)
        {
            if (reader == null)
            {
                throw new ArgumentNullException(nameof(reader));
            }

            decoded = new Asn1SearchRequest();
            AsnReader sequenceReader = reader.ReadSequence(expectedTag);
            AsnReader collectionReader;


            if (sequenceReader.TryGetPrimitiveOctetStringBytes(out ReadOnlyMemory <byte> tmpBaseObject))
            {
                decoded.BaseObject = tmpBaseObject;
            }
            else
            {
                decoded.BaseObject = sequenceReader.ReadOctetString();
            }

            decoded.Scope        = sequenceReader.GetEnumeratedValue <SearchScope>();
            decoded.DerefAliases = sequenceReader.GetEnumeratedValue <DerefAliases>();

            if (!sequenceReader.TryReadInt32(out decoded.SizeLimit))
            {
                sequenceReader.ThrowIfNotEmpty();
            }


            if (!sequenceReader.TryReadInt32(out decoded.TimeLimit))
            {
                sequenceReader.ThrowIfNotEmpty();
            }

            decoded.TypesOnly = sequenceReader.ReadBoolean();
            Asn1Filter.Decode(sequenceReader, out decoded.Filter);

            // Decode SEQUENCE OF for Attributes
            {
                collectionReader = sequenceReader.ReadSequence();
                var tmpList = new List <ReadOnlyMemory <byte> >();
                ReadOnlyMemory <byte> tmpItem;

                while (collectionReader.HasData)
                {
                    if (collectionReader.TryGetPrimitiveOctetStringBytes(out ReadOnlyMemory <byte> tmp))
                    {
                        tmpItem = tmp;
                    }
                    else
                    {
                        tmpItem = collectionReader.ReadOctetString();
                    }

                    tmpList.Add(tmpItem);
                }

                decoded.Attributes = tmpList.ToArray();
            }


            sequenceReader.ThrowIfNotEmpty();
        }