Ejemplo n.º 1
0
 public virtual bool MatchTag(Asn1Tag tag, Asn1Tag parsedTag, IntHolder parsedLen)
 {
     return(MatchTag(tag.Class, tag.Form, tag.IdCode, parsedTag, parsedLen));
 }
Ejemplo n.º 2
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);
        }
Ejemplo n.º 3
0
 public Asn1BerDecodeBuffer(Stream inputStream)
     : base(inputStream)
 {
     _tagHolder = new Asn1Tag();
     _lenHolder = new IntHolder();
 }
Ejemplo n.º 4
0
 public Asn1BerDecodeBuffer(byte[] msgdata)
     : base(msgdata)
 {
     _tagHolder = new Asn1Tag();
     _lenHolder = new IntHolder();
 }
Ejemplo n.º 5
0
        public override void ParseString(string data)
        {
            if (data == null)
            {
                throw new Exception("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 new FormatException("Asn1InvalidYearValue");
                }

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

                if ((MonthValue < 1) || (MonthValue > 12))
                {
                    throw new FormatException("Asn1InvalidMonthValue");
                }

                var num = DaysInMonth[MonthValue];

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

                if ((DayValue < 1) || (DayValue > num))
                {
                    throw new FormatException("Asn1InvalidDayValue");
                }

                var num2 = 0;

                if (!char.IsDigit(CharAt(data, off.Value)))
                {
                    throw new FormatException("Asn1HoursExpected");
                }

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

                if (!char.IsDigit(CharAt(data, off.Value)))
                {
                    throw new FormatException("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 new FormatException("Asn1InvalidHourValue");
                }

                if ((num2 >= 2) && ((MinuteValue < 0) || (MinuteValue > 0x3b)))
                {
                    throw new FormatException("Asn1InvalidMinuteValue");
                }

                if ((num2 == 3) && ((SecondValue < 0) || (SecondValue > 0x3b)))
                {
                    throw new FormatException("Asn1InvalidSecondValue");
                }

                CharAt(data, off.Value);

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

                    if (off.Value != data.Length)
                    {
                        throw new FormatException("Asn1UnexpectedValuesAtEndOfString");
                    }
                }
                else
                {
                    if (DerRules)
                    {
                        throw new FormatException("Asn1UnexpectedZoneOffset");
                    }

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

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

                        if (!char.IsDigit(CharAt(data, off.Value)))
                        {
                            throw new FormatException("Asn1InvalidDiffHour");
                        }

                        DiffHourValue = ParseInt(data, off, 2);

                        if (!char.IsDigit(CharAt(data, off.Value)))
                        {
                            throw new FormatException("Asn1InvalidDiffMinute");
                        }

                        DiffMinValue = ParseInt(data, off, 2);

                        if ((DiffHourValue < 0) || (DiffHourValue > 12))
                        {
                            throw new FormatException("Resources.Asn1InvalidDiffHourValue");
                        }

                        if ((DiffMinValue < 0) || (DiffMinValue > 0x3b))
                        {
                            throw new FormatException("Asn1InvalidDiffMinuteValue");
                        }

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

                Parsed = true;

                if (data != Value)
                {
                    CompileString();
                }
            }
            catch (IndexOutOfRangeException)
            {
                throw new FormatException("Asn1InvalidDateFormat");
            }
            catch (FormatException)
            {
                throw new FormatException("Asn1InvalidNumberFormat");
            }
            catch (ArgumentException)
            {
                throw new FormatException("Asn1InvalidDateFormat");
            }
        }
Ejemplo n.º 6
0
        public override void ParseString(string data)
        {
            if (data == null)
            {
                throw new Exception("Argument data is Null");
            }

            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 new Exception("Asn1InvalidYearValue");
                }

                if ((MonthValue < 1) || (MonthValue > 12))
                {
                    throw new Exception("Asn1InvalidMonthValue");
                }

                int num = DaysInMonth[MonthValue];

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

                if ((DayValue < 1) || (DayValue > num))
                {
                    throw new Exception("Asn1InvalidDayValue");
                }

                var num2 = 0;

                if (!char.IsDigit(CharAt(data, off.Value)))
                {
                    throw new Exception("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 new Exception("Asn1InvalidHourValue");
                }

                if ((num2 >= 2) && ((MinuteValue < 0) || (MinuteValue > 0x3b)))
                {
                    throw new Exception("Asn1InvalidMinuteValue");
                }

                if ((num2 == 3) && ((SecondValue < 0) || (SecondValue > 0x3b)))
                {
                    throw new Exception("Asn1InvalidSecondValue");
                }

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

                if (DerRules && (ch == ','))
                {
                    throw new Exception("Asn1InvalidDecimalMark");
                }

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

                    if (num2 != 3)
                    {
                        throw new Exception("Asn1UnexpectedSymbol");
                    }

                    var length = 0;

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

                    if (length == 0)
                    {
                        throw new Exception("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 new Exception("Asn1UnexpectedValuesAtEndOfString");
                    }
                }
                else
                {
                    if (DerRules)
                    {
                        throw new Exception("Asn1UnexpectedZoneOffset");
                    }

                    UtcFlag = false;

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

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

                        if (!char.IsDigit(CharAt(data, off.Value)))
                        {
                            throw new Exception("Asn1InvalidDiffHour");
                        }

                        DiffHourValue = ParseInt(data, off, 2);

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

                        if ((DiffHourValue < 0) || (DiffHourValue > 12))
                        {
                            throw new Exception("Asn1InvalidDiffHourValue");
                        }

                        if ((DiffMinValue < 0) || (DiffMinValue > 0x3b))
                        {
                            throw new Exception("Asn1InvalidDiffMinuteValue");
                        }

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

                Parsed = true;

                if (data != Value)
                {
                    CompileString();
                }
            }
            catch (IndexOutOfRangeException)
            {
                throw new Exception("Asn1InvalidDateFormat");
            }
            catch (FormatException)
            {
                throw new Exception("Asn1InvalidNumberFormat");
            }
            catch (ArgumentException)
            {
                throw new Exception("Asn1InvalidDateFormat");
            }
        }
Ejemplo n.º 7
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 new Exception("Asn1ValueParseException");
                        }

                        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')
                        {
                            throw new Exception("Asn1ValueParseException");
                        }
                    }

                    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));
        }