Ejemplo n.º 1
0
        public static void ReadWithInsufficientData(string inputHex)
        {
            byte[]    inputData = inputHex.HexToByteArray();
            AsnReader reader    = new AsnReader(inputData, AsnEncodingRules.DER);

            Assert.Throws <CryptographicException>(() => reader.ReadTagAndLength(out _, out _));
        }
Ejemplo n.º 2
0
        public static bool CanDecode(ReadOnlyMemory <byte> encoded)
        {
            var reader = new AsnReader(encoded, AsnEncodingRules.DER);

            var tag = reader.ReadTagAndLength(out _, out _);

            return(tag.HasSameClassAndValue(KrbErrorTag));
        }
Ejemplo n.º 3
0
        private protected static bool CanDecode(ReadOnlyMemory <byte> encoded, Asn1Tag expectedTag)
        {
            var reader = new AsnReader(encoded, AsnEncodingRules.DER);

            var tag = reader.ReadTagAndLength(out _, out _);

            return(tag.HasSameClassAndValue(expectedTag));
        }
Ejemplo n.º 4
0
        public static void InvalidLengths(
            string description,
            PublicEncodingRules rules,
            string inputHex)
        {
            byte[]    inputData = inputHex.HexToByteArray();
            AsnReader reader    = new AsnReader(inputData, (AsnEncodingRules)rules);

            Assert.Throws <CryptographicException>(() => reader.ReadTagAndLength(out _, out _));
        }
Ejemplo n.º 5
0
        public PrincipalName(ref AsnReader reader)
        {
            var tag = reader.ReadTagAndLength(out var contentLength, out var bytesRead);

            reader = reader.AdvanceReader(bytesRead);

            if (tag.TagClass != TagClass.Universal || tag.TagValue != (int)UniversalTagNumber.Sequence)
            {
                throw new InvalidOperationException("Not a sequence but " + tag);
            }

            while (reader.HasData)
            {
                tag    = reader.ReadTagAndLength(out contentLength, out bytesRead);
                reader = reader.AdvanceReader(bytesRead);

                if (tag.TagClass == TagClass.ContextSpecific)
                {
                    switch (tag.TagValue)
                    {
                    case 0:
                        Type = (NameType)(int)reader.GetInteger();
                        break;

                    case 1:
                        reader = reader.ReadSequence();
                        var names = new List <string>();
                        while (reader.HasData)
                        {
                            names.Add(reader.GetCharacterString(new Asn1Tag(UniversalTagNumber.GeneralString), KerberosTags.KerberosStringTag));
                        }
                        Name = names.ToArray();
                        break;
                    }
                }
            }
        }
Ejemplo n.º 6
0
        public KerberosError(ref AsnReader reader)
        {
            var tag = reader.ReadTagAndLength(out var contentLength, out var bytesRead);

            reader = reader.AdvanceReader(bytesRead);

            if (tag.TagClass != TagClass.Universal || tag.TagValue != (int)UniversalTagNumber.Sequence)
            {
                throw new InvalidOperationException("Not a sequence but " + tag);
            }

            while (reader.HasData)
            {
                tag    = reader.ReadTagAndLength(out contentLength, out bytesRead);
                reader = reader.AdvanceReader(bytesRead);

                if (tag.TagClass == TagClass.ContextSpecific)
                {
                    switch (tag.TagValue)
                    {
                    case 0:
                        //         pvno            [0] INTEGER (5),
                        ProtocolVersionNumber = (int)reader.GetInteger();
                        break;

                    case 1:
                        //         msg-type        [1] INTEGER (30),
                        MessageType = (MessageType)(int)reader.GetInteger();
                        break;

                    case 2:
                        //         ctime           [2] KerberosTime OPTIONAL,
                        CTime = reader.GetGeneralizedTime(disallowFractions: true);
                        break;

                    case 3:
                        //         cusec           [3] Microseconds OPTIONAL,
                        var cusec = reader.GetInteger();
                        CUsec = new Microseconds((int)cusec);
                        break;

                    case 4:
                        //         stime           [4] KerberosTime,
                        STime = reader.GetGeneralizedTime(disallowFractions: true);
                        break;

                    case 5:
                        //         susec           [5] Microseconds,
                        var susec = reader.GetInteger();
                        SUsec = new Microseconds((int)susec);
                        break;

                    case 6:
                        //         error-code      [6] Int32,
                        ErrorCode = (KrbErrorCode)(int)reader.GetInteger();
                        break;

                    case 7:
                        //         crealm          [7] Realm OPTIONAL,
                        CRealm = reader.GetCharacterString(new Asn1Tag(UniversalTagNumber.GeneralString), KerberosTags.RealmTag);
                        break;

                    case 8:
                        //         cname           [8] PrincipalName OPTIONAL,
                        CName = new PrincipalName(ref reader);
                        break;

                    case 9:
                        //         realm           [9] Realm -- service realm --,
                        ServiceRealm = reader.GetCharacterString(new Asn1Tag(UniversalTagNumber.GeneralString), KerberosTags.RealmTag);
                        break;

                    case 10:
                        //         sname           [10] PrincipalName -- service name --,
                        SName = new PrincipalName(ref reader);
                        break;

                    case 11:
                        //         e-text          [11] KerberosString OPTIONAL,
                        EText = reader.GetCharacterString(new Asn1Tag(UniversalTagNumber.GeneralString), KerberosTags.KerberosStringTag);
                        break;

                    case 12:
                        //         e-data          [12] OCTET STRING OPTIONAL
                        EData = new byte[contentLength.Value];
                        reader.TryCopyOctetStringBytes(EData, out _);
                        break;
                    }
                }
            }
        }