public override void Decode(Asn1BerDecodeBuffer buffer, bool explicitTagging, int implicitLength)
        {
            var elemLength = explicitTagging ? MatchTag(buffer, Asn1Tag.Sequence) : implicitLength;

            Init();

            var context = new Asn1BerDecodeContext(buffer, elemLength);
            var parsedLen = new IntHolder();

            if (!context.MatchElemTag(0, 0, 6, parsedLen, false))
            {
                throw ExceptionUtility.CryptographicException(Resources.Asn1MissingRequiredException, buffer.ByteCount);
            }

            _publicKeyParamSet = new Asn1ObjectIdentifier();
            _publicKeyParamSet.Decode(buffer, true, parsedLen.Value);

            if (!context.MatchElemTag(0, 0, 6, parsedLen, false))
            {
                throw ExceptionUtility.CryptographicException(Resources.Asn1MissingRequiredException, buffer.ByteCount);
            }

            _digestParamSet = new Asn1ObjectIdentifier();
            _digestParamSet.Decode(buffer, true, parsedLen.Value);

            if (context.MatchElemTag(0, 0, 6, parsedLen, false))
            {
                _encryptionParamSet = new Gost2814789ParamSet();
                _encryptionParamSet.Decode(buffer, true, parsedLen.Value);
            }
        }
        public override void Decode(Asn1BerDecodeBuffer buffer, bool explicitTagging, int implicitLength)
        {
            var elemLength = explicitTagging ? MatchTag(buffer, Asn1Tag.Sequence) : implicitLength;

            Init();

            var context = new Asn1BerDecodeContext(buffer, elemLength);
            var parsedLen = new IntHolder();

            if (!context.MatchElemTag(0, 0x20, SequenceTypeCode, parsedLen, false))
            {
                throw ExceptionUtility.CryptographicException(Resources.Asn1MissingRequiredException, buffer.ByteCount);
            }

            EncryptedKey = new Gost2814789EncryptedKey();
            EncryptedKey.Decode(buffer, true, parsedLen.Value);

            if (!context.MatchElemTag(0, 0x20, SequenceTypeCode, parsedLen, false))
            {
                throw ExceptionUtility.CryptographicException(Resources.Asn1MissingRequiredException, buffer.ByteCount);
            }

            EncryptedParameters = new Gost2814789KeyWrapParameters();
            EncryptedParameters.Decode(buffer, true, parsedLen.Value);
        }
        public override void Decode(Asn1BerDecodeBuffer buffer, bool explicitTagging, int implicitLength)
        {
            var elemLength = explicitTagging ? MatchTag(buffer, Asn1Tag.Sequence) : implicitLength;

            Init();

            var context = new Asn1BerDecodeContext(buffer, elemLength);
            var parsedLen = new IntHolder();

            if (!context.MatchElemTag(0, 0x20, 0x10, parsedLen, false))
            {
                throw ExceptionUtility.CryptographicException(Resources.Asn1MissingRequiredException, buffer.ByteCount);
            }

            Algorithm = new AlgorithmIdentifier();
            Algorithm.Decode(buffer, true, parsedLen.Value);

            if (!context.MatchElemTag(0, 0, 3, parsedLen, false))
            {
                throw ExceptionUtility.CryptographicException(Resources.Asn1MissingRequiredException, buffer.ByteCount);
            }

            SubjectPublicKey = new Asn1BitString();
            SubjectPublicKey.Decode(buffer, true, parsedLen.Value);
        }
        public override void Decode(Asn1BerDecodeBuffer buffer, bool explicitTagging, int implicitLength)
        {
            var elemLength = explicitTagging ? MatchTag(buffer, Asn1Tag.Sequence) : implicitLength;

            Init();

            var context = new Asn1BerDecodeContext(buffer, elemLength);
            var parsedLen = new IntHolder();

            if (!context.MatchElemTag(0, 0, 6, parsedLen, false))
            {
                throw ExceptionUtility.CryptographicException(Resources.Asn1MissingRequiredException, buffer.ByteCount);
            }

            Algorithm = new Asn1ObjectIdentifier();
            Algorithm.Decode(buffer, true, parsedLen.Value);

            if (!context.Expired())
            {
                Parameters = new Asn1OpenType();
                Parameters.Decode(buffer, true, 0);
            }

            CheckTc(true);
        }
        public override void Decode(Asn1BerDecodeBuffer buffer, bool explicitTagging, int implicitLength)
        {
            var llen = explicitTagging ? MatchTag(buffer, Tag) : implicitLength;
            var idx = new IntHolder(0);
            var lastTag = buffer.LastTag;

            if ((lastTag == null) || !lastTag.Constructed)
            {
                ReadSegment(buffer, llen, idx);
            }
            else
            {
                var context = new Asn1BerDecodeContext(buffer, llen);

                while (!context.Expired())
                {
                    var num2 = MatchTag(buffer, Asn1OctetString.Tag);

                    if (num2 <= 0)
                    {
                        throw ExceptionUtility.CryptographicException(Resources.Asn1InvalidFormatOfConstructedValue, buffer.ByteCount);
                    }

                    ReadSegment(buffer, num2, idx);
                }

                if (llen == Asn1Status.IndefiniteLength)
                {
                    MatchTag(buffer, Asn1Tag.Eoc);
                }
            }

            buffer.TypeCode = UniversalStringTypeCode;
        }
        public override void Decode(Asn1BerDecodeBuffer buffer, bool explicitTagging, int implicitLength)
        {
            var elemLength = explicitTagging ? MatchTag(buffer, Asn1Tag.Sequence) : implicitLength;

            Init();

            var context = new Asn1BerDecodeContext(buffer, elemLength);
            var parsedLen = new IntHolder();

            if (!context.MatchElemTag(0, 0, ObjectIdentifierTypeCode, parsedLen, false))
            {
                throw ExceptionUtility.CryptographicException(Resources.Asn1MissingRequiredException, buffer.ByteCount);
            }

            EncryptionParamSet = new Gost2814789ParamSet();
            EncryptionParamSet.Decode(buffer, true, parsedLen.Value);

            if (context.MatchElemTag(0, 0, OctetStringTypeCode, parsedLen, false))
            {
                Ukm = new Asn1OctetString();
                Ukm.Decode(buffer, true, parsedLen.Value);

                if (Ukm.Length != 8)
                {
                    throw ExceptionUtility.CryptographicException(Resources.Asn1ConsVioException, "Ukm.Length", Ukm.Length);
                }
            }
        }
        public virtual bool MatchElemTag(short tagClass, short tagForm, int tagIdCode, IntHolder parsedLen, bool advance)
        {
            if (Expired())
            {
                return false;
            }

            var flag = _decodeBuffer.MatchTag(tagClass, tagForm, tagIdCode, _tagHolder, parsedLen);

            if ((_elemLength != Asn1Status.IndefiniteLength) && (parsedLen.Value != Asn1Status.IndefiniteLength))
            {
                var num = _decodeBuffer.ByteCount - _decBufByteCount;

                if ((parsedLen.Value < 0) || (parsedLen.Value > (_elemLength - num)))
                {
                    throw ExceptionUtility.CryptographicException(Resources.Asn1InvalidLengthException);
                }
            }

            if (flag && !advance)
            {
                _decodeBuffer.Reset();
            }

            return flag;
        }
        public virtual bool Expired()
        {
            if (_elemLength == Asn1Status.IndefiniteLength)
            {
                var parsedLen = new IntHolder();
                var flag = _decodeBuffer.MatchTag(0, 0, 0, null, parsedLen);

                if (flag)
                {
                    _decodeBuffer.Reset();
                }

                return flag;
            }

            var num = _decodeBuffer.ByteCount - _decBufByteCount;

            return (num >= _elemLength);
        }
        public override void Decode(Asn1BerDecodeBuffer buffer, bool explicitTagging, int implicitLength)
        {
            var elemLength = explicitTagging ? MatchTag(buffer, Asn1Tag.Sequence) : implicitLength;

            Init();

            var context = new Asn1BerDecodeContext(buffer, elemLength);
            var parsedLen = new IntHolder();

            if (!context.MatchElemTag(0, 0, ObjectIdentifierTypeCode, parsedLen, false))
            {
                throw ExceptionUtility.CryptographicException(Resources.Asn1MissingRequiredException, buffer.ByteCount);
            }

            EncryptionParamSet = new Gost2814789ParamSet();
            EncryptionParamSet.Decode(buffer, true, parsedLen.Value);

            if (!context.Expired())
            {
                if (buffer.PeekTag().Equals(0, 0, ObjectIdentifierTypeCode))
                {
                    throw ExceptionUtility.CryptographicException(Resources.Asn1SeqOrderException);
                }

                _extElem1 = new Asn1OpenExt();

                while (!context.Expired())
                {
                    _extElem1.DecodeComponent(buffer);
                }
            }
            else
            {
                _extElem1 = null;
            }
        }
 public Asn1BerDecodeBuffer(Stream inputStream)
     : base(inputStream)
 {
     _tagHolder = new Asn1Tag();
     _lenHolder = new IntHolder();
 }
        public virtual bool MatchTag(short tagClass, short tagForm, int tagIdCode, Asn1Tag parsedTag, IntHolder parsedLen)
        {
            Mark();

            var tag = parsedTag ?? _tagHolder;
            var holder = parsedLen ?? _lenHolder;

            holder.Value = DecodeTagAndLength(tag);

            if (!tag.Equals(tagClass, tagForm, tagIdCode))
            {
                Reset();
                return false;
            }

            return true;
        }
 public virtual bool MatchElemTag(Asn1Tag tag, IntHolder parsedLen, bool advance)
 {
     return MatchElemTag(tag.Class, tag.Form, tag.IdCode, parsedLen, advance);
 }
Esempio n. 13
0
        public static byte[] ParseString(string data, IntHolder numbits)
        {
            char ch;
            int  num;
            int  num2;
            int  num3;
            int  num4;
            int  num5;

            char ch2 = data[0];

            byte[] buffer;

            switch (ch2)
            {
            case '\'':
            case '"':
                if (!data.EndsWith("B"))
                {
                    if (data.EndsWith("H"))
                    {
                        var builder = new StringBuilder();
                        num3           = (data.Length - 3) * 4;
                        buffer         = AllocBitArray(num3);
                        builder.Length = 2;
                        num            = 1;
                        num2           = 0;
                        ch             = '\0';

                        while ((num < data.Length) && (ch != ch2))
                        {
                            ch = data[num++];

                            if (ch != ch2)
                            {
                                builder[0]     = ch;
                                ch             = (num >= data.Length) ? '0' : data[num];
                                builder[1]     = (ch == ch2) ? '0' : ch;
                                buffer[num2++] = (byte)Convert.ToInt32(builder.ToString(), 0x10);
                            }

                            num++;
                        }
                    }
                    else
                    {
                        if (data[data.Length - 1] != ch2)
                        {
                            throw ExceptionUtility.CryptographicException(Resources.Asn1ValueParseException, data, data.Length - 1);
                        }

                        num3   = (data.Length - 2) * 8;
                        buffer = AllocBitArray(num3);
                        num    = 1;
                        ch     = '\0';

                        while ((num < data.Length) && (ch != ch2))
                        {
                            ch = data[num];

                            if (ch != ch2)
                            {
                                buffer[num - 1] = (byte)ch;
                            }

                            num++;
                        }
                    }

                    return(SetNumBits(numbits, num3, buffer));
                }

                num3   = data.Length - 3;
                buffer = AllocBitArray(num3);
                num5   = 0x80;
                num    = 1;
                num4   = 0;
                num2   = 0;

                while (num < data.Length)
                {
                    ch = data[num];

                    if (ch == '1')
                    {
                        num4 |= num5;
                    }
                    else
                    {
                        if (ch == ch2)
                        {
                            break;
                        }
                        if (ch != '0')
                        {
                            ExceptionUtility.CryptographicException(Resources.Asn1ValueParseException, data, num);
                        }
                    }

                    num5 = num5 >> 1;

                    if (num5 == 0)
                    {
                        buffer[num2++] = (byte)num4;
                        num5           = 0x80;
                        num4           = 0;
                    }

                    num++;
                }
                break;

            default:
                num3   = data.Length * 8;
                buffer = AllocBitArray(num3);
                num    = 0;

                while (num < data.Length)
                {
                    ch          = data[num];
                    buffer[num] = (byte)ch;
                    num++;
                }

                return(SetNumBits(numbits, num3, buffer));
            }

            if (num5 != 0x80)
            {
                buffer[num2] = (byte)num4;
            }

            return(SetNumBits(numbits, num3, buffer));
        }
        private void ParseElement(IAsn1TaggedEventHandler handler, Asn1Tag tag, IntHolder len)
        {
            _parserCaptureBuffer.Seek(0L, SeekOrigin.Begin);
            _parserCaptureBuffer.SetLength(0L);

            len.Value = DecodeTagAndLength(tag);

            if (!tag.IsEoc() || (len.Value != 0))
            {
                handler.StartElement(tag, len.Value, _parserCaptureBuffer.ToArray());

                _parserCaptureBuffer.Seek(0L, SeekOrigin.Begin);
                _parserCaptureBuffer.SetLength(0L);

                if ((len.Value > 0) || (len.Value == Asn1Status.IndefiniteLength))
                {
                    if (tag.Constructed)
                    {
                        ParseCons(handler, len.Value);
                    }
                    else
                    {
                        ParsePrim(handler, len.Value);
                    }
                }

                handler.EndElement(tag);
            }
        }
Esempio n. 15
0
        protected static int ParseInt(string str, IntHolder off, int len)
        {
            if ((off.Value + len) > str.Length)
            {
                throw ExceptionUtility.ArgumentOutOfRange("off");
            }

            var mValue = off.Value;
            off.Value += len;

            return int.Parse(str.Substring(mValue, len));
        }
Esempio n. 16
0
        private static byte[] SetNumBits(IntHolder numbits, int num3, byte[] buffer)
        {
            if (numbits != null)
            {
                numbits.Value = num3;
            }

            return buffer;
        }
Esempio n. 17
0
        public static byte[] ParseString(string data, IntHolder numbits)
        {
            char ch;
            int num;
            int num2;
            int num3;
            int num4;
            int num5;

            char ch2 = data[0];
            byte[] buffer;

            switch (ch2)
            {
                case '\'':
                case '"':
                    if (!data.EndsWith("B"))
                    {
                        if (data.EndsWith("H"))
                        {
                            var builder = new StringBuilder();
                            num3 = (data.Length - 3) * 4;
                            buffer = AllocBitArray(num3);
                            builder.Length = 2;
                            num = 1;
                            num2 = 0;
                            ch = '\0';

                            while ((num < data.Length) && (ch != ch2))
                            {
                                ch = data[num++];

                                if (ch != ch2)
                                {
                                    builder[0] = ch;
                                    ch = (num >= data.Length) ? '0' : data[num];
                                    builder[1] = (ch == ch2) ? '0' : ch;
                                    buffer[num2++] = (byte)Convert.ToInt32(builder.ToString(), 0x10);
                                }

                                num++;
                            }
                        }
                        else
                        {
                            if (data[data.Length - 1] != ch2)
                            {
                                throw ExceptionUtility.CryptographicException(Resources.Asn1ValueParseException, data, data.Length - 1);
                            }

                            num3 = (data.Length - 2) * 8;
                            buffer = AllocBitArray(num3);
                            num = 1;
                            ch = '\0';

                            while ((num < data.Length) && (ch != ch2))
                            {
                                ch = data[num];

                                if (ch != ch2)
                                {
                                    buffer[num - 1] = (byte)ch;
                                }

                                num++;
                            }
                        }

                        return SetNumBits(numbits, num3, buffer);
                    }

                    num3 = data.Length - 3;
                    buffer = AllocBitArray(num3);
                    num5 = 0x80;
                    num = 1;
                    num4 = 0;
                    num2 = 0;

                    while (num < data.Length)
                    {
                        ch = data[num];

                        if (ch == '1')
                        {
                            num4 |= num5;
                        }
                        else
                        {
                            if (ch == ch2)
                            {
                                break;
                            }
                            if (ch != '0')
                            {
                                ExceptionUtility.CryptographicException(Resources.Asn1ValueParseException, data, num);
                            }
                        }

                        num5 = num5 >> 1;

                        if (num5 == 0)
                        {
                            buffer[num2++] = (byte)num4;
                            num5 = 0x80;
                            num4 = 0;
                        }

                        num++;
                    }
                    break;
                default:
                    num3 = data.Length * 8;
                    buffer = AllocBitArray(num3);
                    num = 0;

                    while (num < data.Length)
                    {
                        ch = data[num];
                        buffer[num] = (byte)ch;
                        num++;
                    }

                    return SetNumBits(numbits, num3, buffer);
            }

            if (num5 != 0x80)
            {
                buffer[num2] = (byte)num4;
            }

            return SetNumBits(numbits, num3, buffer);
        }
        private void DecodeOpenTypeElement(Asn1Tag tag, IntHolder len, bool saveData)
        {
            var nbytes = DecodeTagAndLength(tag);
            var byteCount = base.ByteCount;

            if (nbytes > 0)
            {
                if (saveData)
                {
                    Capture(nbytes);
                }
                else
                {
                    Skip(nbytes);
                }
            }
            else if (nbytes == Asn1Status.IndefiniteLength)
            {
                MovePastEoc(saveData);
            }

            len.Value = base.ByteCount - byteCount;
        }
        private void ParseCons(IAsn1TaggedEventHandler handler, int len)
        {
            var tag2 = new Asn1Tag();
            var holder = new IntHolder();
            var byteCount = base.ByteCount;

            while (true)
            {
                ParseElement(handler, tag2, holder);

                if (len == Asn1Status.IndefiniteLength)
                {
                    if (tag2.IsEoc() && (holder.Value == 0))
                    {
                        return;
                    }

                    continue;
                }

                if ((base.ByteCount - byteCount) >= len)
                {
                    return;
                }
            }
        }
Esempio n. 20
0
 public virtual bool MatchElemTag(Asn1Tag tag, IntHolder parsedLen, bool advance)
 {
     return(MatchElemTag(tag.Class, tag.Form, tag.IdCode, parsedLen, advance));
 }
        private void ReadSegment(Asn1BerDecodeBuffer buffer, int llen, IntHolder idx)
        {
            if ((llen < 0) || ((llen % 4) != 0))
            {
                throw ExceptionUtility.CryptographicException(Resources.Asn1InvalidLengthException);
            }

            var num4 = llen / 4;

            if (_value.Length == 0)
            {
                _value = new int[num4];
            }
            else if ((idx.Value + num4) >= _value.Length)
            {
                ReallocIntArray(idx.Value + num4);
            }

            var value = idx.Value;

            while (value < (idx.Value + num4))
            {
                _value[value] = 0;

                for (var i = 0; i < 4; i++)
                {
                    var num = buffer.Read();

                    if (num == -1)
                    {
                        throw ExceptionUtility.CryptographicException(Resources.Asn1EndOfBufferException, buffer.ByteCount);
                    }

                    _value[value] = (_value[value] * 0x100) + num;
                }

                value++;
            }

            idx.Value = value;
        }
Esempio n. 22
0
        public virtual bool MatchElemTag(short tagClass, short tagForm, int tagIdCode, IntHolder parsedLen, bool advance)
        {
            if (Expired())
            {
                return(false);
            }

            var flag = _decodeBuffer.MatchTag(tagClass, tagForm, tagIdCode, _tagHolder, parsedLen);

            if ((_elemLength != Asn1Status.IndefiniteLength) && (parsedLen.Value != Asn1Status.IndefiniteLength))
            {
                var num = _decodeBuffer.ByteCount - _decBufByteCount;

                if ((parsedLen.Value < 0) || (parsedLen.Value > (_elemLength - num)))
                {
                    throw ExceptionUtility.CryptographicException(Resources.Asn1InvalidLengthException);
                }
            }

            if (flag && !advance)
            {
                _decodeBuffer.Reset();
            }

            return(flag);
        }
        public override void ParseString(string data)
        {
            if (data == null)
            {
                throw ExceptionUtility.ArgumentNull("data");
            }

            Clear();

            var off = new IntHolder(0);

            try
            {
                YearValue  = ParseInt(data, off, 4);
                MonthValue = ParseInt(data, off, 2);
                DayValue   = ParseInt(data, off, 2);

                if (YearValue < 0)
                {
                    throw ExceptionUtility.CryptographicException(Resources.Asn1InvalidYearValue, YearValue);
                }

                if ((MonthValue < 1) || (MonthValue > 12))
                {
                    throw ExceptionUtility.CryptographicException(Resources.Asn1InvalidMonthValue, MonthValue);
                }

                int num = DaysInMonth[MonthValue];

                if (((MonthValue == 2) && ((YearValue % 4) == 0)) && (((YearValue % 100) != 0) || ((YearValue % 400) == 0)))
                {
                    num++;
                }

                if ((DayValue < 1) || (DayValue > num))
                {
                    throw ExceptionUtility.CryptographicException(Resources.Asn1InvalidDayValue, DayValue);
                }

                var num2 = 0;

                if (!char.IsDigit(CharAt(data, off.Value)))
                {
                    throw ExceptionUtility.CryptographicException(Resources.Asn1HoursExpected);
                }

                HourValue = ParseInt(data, off, 2);
                num2++;

                if (char.IsDigit(CharAt(data, off.Value)))
                {
                    MinuteValue = ParseInt(data, off, 2);
                    num2++;

                    if (char.IsDigit(CharAt(data, off.Value)))
                    {
                        SecondValue = ParseInt(data, off, 2);
                        num2++;
                    }
                }

                if ((num2 >= 1) && ((HourValue < 0) || (HourValue > 0x17)))
                {
                    throw ExceptionUtility.CryptographicException(Resources.Asn1InvalidHourValue, HourValue);
                }

                if ((num2 >= 2) && ((MinuteValue < 0) || (MinuteValue > 0x3b)))
                {
                    throw ExceptionUtility.CryptographicException(Resources.Asn1InvalidMinuteValue, MinuteValue);
                }

                if ((num2 == 3) && ((SecondValue < 0) || (SecondValue > 0x3b)))
                {
                    throw ExceptionUtility.CryptographicException(Resources.Asn1InvalidSecondValue, SecondValue);
                }

                var ch = CharAt(data, off.Value);

                if (DerRules && (ch == ','))
                {
                    throw ExceptionUtility.CryptographicException(Resources.Asn1InvalidDecimalMark);
                }

                if ((ch == '.') || (ch == ','))
                {
                    off.Value++;

                    if (num2 != 3)
                    {
                        throw ExceptionUtility.CryptographicException(Resources.Asn1UnexpectedSymbol);
                    }

                    var length = 0;

                    while (char.IsDigit(CharAt(data, off.Value + length)))
                    {
                        length++;
                    }

                    if (length == 0)
                    {
                        throw ExceptionUtility.CryptographicException(Resources.Asn1UnexpectedSymbol);
                    }

                    SecFraction = data.Substring(off.Value, length);
                    off.Value  += length;
                }

                if (CharAt(data, off.Value) == 'Z')
                {
                    off.Value++;
                    UtcFlag = true;

                    if (off.Value != data.Length)
                    {
                        throw ExceptionUtility.CryptographicException(Resources.Asn1UnexpectedValuesAtEndOfString);
                    }
                }
                else
                {
                    if (DerRules)
                    {
                        throw ExceptionUtility.CryptographicException(Resources.Asn1UnexpectedZoneOffset);
                    }

                    UtcFlag = false;

                    var ch2 = CharAt(data, off.Value);

                    switch (ch2)
                    {
                    case '-':
                    case '+':
                        off.Value++;

                        if (!char.IsDigit(CharAt(data, off.Value)))
                        {
                            throw ExceptionUtility.CryptographicException(Resources.Asn1InvalidDiffHour);
                        }

                        DiffHourValue = ParseInt(data, off, 2);

                        if (char.IsDigit(CharAt(data, off.Value)))
                        {
                            DiffMinValue = ParseInt(data, off, 2);
                        }

                        if ((DiffHourValue < 0) || (DiffHourValue > 12))
                        {
                            throw ExceptionUtility.CryptographicException(Resources.Asn1InvalidDiffHourValue, DiffHourValue);
                        }

                        if ((DiffMinValue < 0) || (DiffMinValue > 0x3b))
                        {
                            throw ExceptionUtility.CryptographicException(Resources.Asn1InvalidDiffMinuteValue, DiffMinValue);
                        }

                        if (ch2 == '-')
                        {
                            DiffHourValue = -DiffHourValue;
                            DiffMinValue  = -DiffMinValue;
                        }
                        break;
                    }
                }

                Parsed = true;

                if (data != Value)
                {
                    CompileString();
                }
            }
            catch (IndexOutOfRangeException)
            {
                throw ExceptionUtility.CryptographicException(Resources.Asn1InvalidDateFormat);
            }
            catch (FormatException)
            {
                throw ExceptionUtility.CryptographicException(Resources.Asn1InvalidNumberFormat);
            }
            catch (ArgumentException)
            {
                throw ExceptionUtility.CryptographicException(Resources.Asn1InvalidDateFormat);
            }
        }
Esempio n. 24
0
 public Asn1BerDecodeBuffer(Stream inputStream)
     : base(inputStream)
 {
     _tagHolder = new Asn1Tag();
     _lenHolder = new IntHolder();
 }
Esempio n. 25
0
 public Asn1BerDecodeBuffer(byte[] msgdata)
     : base(msgdata)
 {
     _tagHolder = new Asn1Tag();
     _lenHolder = new IntHolder();
 }
Esempio n. 26
0
        public virtual bool MatchTag(short tagClass, short tagForm, int tagIdCode, Asn1Tag parsedTag, IntHolder parsedLen)
        {
            Mark();

            var tag    = parsedTag ?? _tagHolder;
            var holder = parsedLen ?? _lenHolder;

            holder.Value = DecodeTagAndLength(tag);

            if (!tag.Equals(tagClass, tagForm, tagIdCode))
            {
                Reset();
                return(false);
            }

            return(true);
        }
Esempio n. 27
0
 public virtual bool MatchTag(Asn1Tag tag, Asn1Tag parsedTag, IntHolder parsedLen)
 {
     return(MatchTag(tag.Class, tag.Form, tag.IdCode, parsedTag, parsedLen));
 }
 public Asn1BerDecodeBuffer(byte[] msgdata)
     : base(msgdata)
 {
     _tagHolder = new Asn1Tag();
     _lenHolder = new IntHolder();
 }
Esempio n. 29
0
        public override void ParseString(string data)
        {
            if (data == null)
            {
                throw ExceptionUtility.ArgumentNull("data");
            }

            Clear();

            var off = new IntHolder(0);

            try
            {
                YearValue = ParseInt(data, off, 2);
                MonthValue = ParseInt(data, off, 2);
                DayValue = ParseInt(data, off, 2);

                if (YearValue < 0)
                {
                    throw ExceptionUtility.CryptographicException(Resources.Asn1InvalidYearValue, YearValue);
                }

                if (YearValue < 100)
                {
                    if (YearValue > 70)
                    {
                        YearValue += 0x76c;
                    }
                    else
                    {
                        YearValue += 0x7d0;
                    }
                }

                if ((MonthValue < 1) || (MonthValue > 12))
                {
                    throw ExceptionUtility.CryptographicException(Resources.Asn1InvalidMonthValue, MonthValue);
                }

                var num = DaysInMonth[MonthValue];

                if (((MonthValue == 2) && ((YearValue % 4) == 0)) && (((YearValue % 100) != 0) || ((YearValue % 400) == 0)))
                {
                    num++;
                }

                if ((DayValue < 1) || (DayValue > num))
                {
                    throw ExceptionUtility.CryptographicException(Resources.Asn1InvalidDayValue, DayValue);
                }

                var num2 = 0;

                if (!char.IsDigit(CharAt(data, off.Value)))
                {
                    throw ExceptionUtility.CryptographicException(Resources.Asn1HoursExpected);
                }

                HourValue = ParseInt(data, off, 2);
                num2++;

                if (!char.IsDigit(CharAt(data, off.Value)))
                {
                    throw ExceptionUtility.CryptographicException(Resources.Asn1MinutesExpected);
                }

                MinuteValue = ParseInt(data, off, 2);
                num2++;

                if (char.IsDigit(CharAt(data, off.Value)))
                {
                    SecondValue = ParseInt(data, off, 2);
                    num2++;
                }

                if ((num2 >= 2) && ((HourValue < 0) || (HourValue > UtcTimeTypeCode)))
                {
                    throw ExceptionUtility.CryptographicException(Resources.Asn1InvalidHourValue, HourValue);
                }

                if ((num2 >= 2) && ((MinuteValue < 0) || (MinuteValue > 0x3b)))
                {
                    throw ExceptionUtility.CryptographicException(Resources.Asn1InvalidMinuteValue, MinuteValue);
                }

                if ((num2 == 3) && ((SecondValue < 0) || (SecondValue > 0x3b)))
                {
                    throw ExceptionUtility.CryptographicException(Resources.Asn1InvalidSecondValue, SecondValue);
                }

                CharAt(data, off.Value);

                if (CharAt(data, off.Value) == 'Z')
                {
                    off.Value++;
                    UtcFlag = true;

                    if (off.Value != data.Length)
                    {
                        throw ExceptionUtility.CryptographicException(Resources.Asn1UnexpectedValuesAtEndOfString);
                    }
                }
                else
                {
                    if (DerRules)
                    {
                        throw ExceptionUtility.CryptographicException(Resources.Asn1UnexpectedZoneOffset);
                    }

                    UtcFlag = false;
                    var ch = CharAt(data, off.Value);

                    switch (ch)
                    {
                        case '-':
                        case '+':
                            off.Value++;

                            if (!char.IsDigit(CharAt(data, off.Value)))
                            {
                                throw ExceptionUtility.CryptographicException(Resources.Asn1InvalidDiffHour);
                            }

                            DiffHourValue = ParseInt(data, off, 2);

                            if (!char.IsDigit(CharAt(data, off.Value)))
                            {
                                throw ExceptionUtility.CryptographicException(Resources.Asn1InvalidDiffMinute);
                            }

                            DiffMinValue = ParseInt(data, off, 2);

                            if ((DiffHourValue < 0) || (DiffHourValue > 12))
                            {
                                throw ExceptionUtility.CryptographicException(Resources.Asn1InvalidDiffHourValue, DiffHourValue);
                            }

                            if ((DiffMinValue < 0) || (DiffMinValue > 0x3b))
                            {
                                throw ExceptionUtility.CryptographicException(Resources.Asn1InvalidDiffMinuteValue, DiffMinValue);
                            }

                            if (ch == '-')
                            {
                                DiffHourValue = -DiffHourValue;
                                DiffMinValue = -DiffMinValue;
                            }
                            break;
                    }
                }

                Parsed = true;

                if (data != Value)
                {
                    CompileString();
                }
            }
            catch (IndexOutOfRangeException)
            {
                throw ExceptionUtility.CryptographicException(Resources.Asn1InvalidDateFormat);
            }
            catch (FormatException)
            {
                throw ExceptionUtility.CryptographicException(Resources.Asn1InvalidNumberFormat);
            }
            catch (ArgumentException)
            {
                throw ExceptionUtility.CryptographicException(Resources.Asn1InvalidDateFormat);
            }
        }
 public virtual bool MatchTag(Asn1Tag tag, Asn1Tag parsedTag, IntHolder parsedLen)
 {
     return MatchTag(tag.Class, tag.Form, tag.IdCode, parsedTag, parsedLen);
 }