Exemplo n.º 1
0
        public override void Decode(Asn1BerDecodeBuffer buffer, bool explicitTagging, int implicitLength)
        {
            var elemLength = explicitTagging ? MatchTag(buffer, Tag) : implicitLength;
            var lastTag    = buffer.LastTag;

            if ((lastTag == null) || !lastTag.Constructed)
            {
                if (elemLength < 0)
                {
                    throw ExceptionUtility.CryptographicException(Resources.Asn1InvalidLengthException);
                }

                Value = new byte[elemLength];

                if (elemLength != 0)
                {
                    buffer.Read(Value);
                }
            }
            else
            {
                var nbytes  = 0;
                var offset  = 0;
                var context = new Asn1BerDecodeContext(buffer, elemLength);

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

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

                    nbytes += num2;

                    if (offset == 0)
                    {
                        Value = new byte[nbytes];
                    }
                    else
                    {
                        ReAllocByteArray(nbytes);
                    }

                    buffer.Read(Value, offset, num2);
                    offset = nbytes;
                }

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

            buffer.TypeCode = OctetStringTypeCode;
        }
Exemplo n.º 2
0
        public override void Decode(Asn1BerDecodeBuffer buffer, bool explicitTagging, int implicitLength)
        {
            if (explicitTagging)
            {
                MatchTag(buffer, Tag);
            }

            var num = buffer.Read();

            if (num < 0)
            {
                throw ExceptionUtility.CryptographicException(Resources.Asn1EndOfBufferException, buffer.ByteCount);
            }

            buffer.TypeCode = BooleanTypeCode;
            Value           = num != 0;
        }
Exemplo n.º 3
0
        public override void Decode(Asn1BerDecodeBuffer buffer, bool explicitTagging, int implicitLength)
        {
            if (explicitTagging)
            {
                MatchTag(buffer, Tag);
            }

            var num = buffer.Read();

            if (num < 0)
            {
                throw ExceptionUtility.CryptographicException(Resources.Asn1EndOfBufferException, buffer.ByteCount);
            }

            buffer.TypeCode = BooleanTypeCode;
            Value = num != 0;
        }
Exemplo n.º 4
0
        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;
        }
Exemplo n.º 5
0
        protected virtual void Decode(Asn1BerDecodeBuffer buffer, bool explicitTagging, int implicitLength, Asn1Tag tag)
        {
            int num2;
            var elemLength = explicitTagging ? MatchTag(buffer, tag) : implicitLength;
            var num3       = elemLength;
            var num4       = 0;

            if (StringBuffer == null)
            {
                StringBuffer = new StringBuilder();
            }

            var lastTag = buffer.LastTag;

            if ((lastTag == null) || !lastTag.Constructed)
            {
                if (num3 < 0)
                {
                    throw ExceptionUtility.CryptographicException(Resources.Asn1InvalidLengthException);
                }

                StringBuffer.Length = num3;

                while (num3 > 0)
                {
                    num2 = buffer.Read();

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

                    StringBuffer[num4++] = (char)num2;
                    num3--;
                }
            }
            else
            {
                var capacity = 0;
                var context  = new Asn1BerDecodeContext(buffer, elemLength);

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

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

                    capacity += num5;
                    StringBuffer.EnsureCapacity(capacity);

                    while (num5 > 0)
                    {
                        num2 = buffer.Read();

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

                        StringBuffer.Append((char)num2);
                        num5--;
                    }
                }

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

            Value           = StringBuffer.ToString();
            buffer.TypeCode = (short)tag.IdCode;
        }
Exemplo n.º 6
0
        public override void Decode(Asn1BerDecodeBuffer buffer, bool explicitTagging, int implicitLength)
        {
            var elemLength = explicitTagging ? MatchTag(buffer, Tag) : implicitLength;
            var lastTag    = buffer.LastTag;

            if ((lastTag == null) || !lastTag.Constructed)
            {
                if (elemLength < 0)
                {
                    throw ExceptionUtility.CryptographicException(Resources.Asn1InvalidLengthException);
                }

                if (elemLength != 0)
                {
                    var num8 = elemLength - 1;
                    var num7 = buffer.Read();

                    if (num7 < 0)
                    {
                        throw ExceptionUtility.CryptographicException(Resources.Asn1EndOfBufferException, buffer.ByteCount);
                    }

                    if ((num7 < 0) || (num7 > 7))
                    {
                        throw ExceptionUtility.CryptographicException(Resources.Asn1InvalidFormatOfBitString, num7);
                    }

                    if ((num8 == 0) && (num7 != 0))
                    {
                        throw ExceptionUtility.CryptographicException(Resources.Asn1InvalidLengthException);
                    }

                    NumBits = (num8 * 8) - num7;
                    Value   = new byte[num8];
                    buffer.Read(Value);
                }
                else
                {
                    NumBits = 0;
                    Value   = null;
                }
            }
            else
            {
                var num3   = 0;
                var offset = 0;
                var index  = -1;
                var num6   = 0;

                var context = new Asn1BerDecodeContext(buffer, elemLength);

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

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

                    num3 += nbytes;

                    if (offset == 0)
                    {
                        AllocBitArray(num3 * 8);
                    }
                    else
                    {
                        ReallocBitArray(num3 * 8);
                    }

                    index = offset;
                    buffer.Read(Value, offset, nbytes);
                    offset = num3;
                }

                if (index >= 0)
                {
                    num6 = Value[index];

                    if (((offset - index) - 1) > 0)
                    {
                        Array.Copy(Value, index + 1, Value, index, (offset - index) - 1);
                    }

                    num3--;
                }

                if ((num6 < 0) || (num6 > 7))
                {
                    throw ExceptionUtility.CryptographicException(Resources.Asn1InvalidFormatOfBitString, num6);
                }

                ReallocBitArray((num3 * 8) - num6);

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

            buffer.TypeCode = 3;
        }
Exemplo n.º 7
0
        public override void Decode(Asn1BerDecodeBuffer buffer, bool explicitTagging, int implicitLength)
        {
            var length = explicitTagging ? MatchTag(buffer, Tag) : implicitLength;

            if (length == 0)
            {
                Value = 0.0;
            }
            else
            {
                var num2 = buffer.ReadByte();

                if (length == 1)
                {
                    switch (num2)
                    {
                        case PlusInfinity:
                            Value = double.PositiveInfinity;
                            return;

                        case MinusInfinity:
                            Value = double.NegativeInfinity;
                            return;
                    }

                    throw ExceptionUtility.CryptographicException(Resources.Asn1InvalidFormatOfConstructedValue, buffer.ByteCount);
                }

                length--;

                if ((num2 & RealBinary) == 0)
                {
                    var num8 = length;
                    var num9 = 0;

                    var builder = new StringBuilder { Length = num8 };

                    while (num8 > 0)
                    {
                        var num7 = buffer.Read();

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

                        builder[num9++] = (char)num7;
                        num8--;
                    }

                    var num10 = num2 & RealIso6093Mask;
                    var num11 = 0;

                    for (var i = 0; i < builder.Length; i++)
                    {
                        var ch = builder[i];

                        if ((num10 >= 2) && (ch == ','))
                        {
                            builder[i] = '.';
                            num11++;
                        }
                        else if (((num10 >= 1) && (((ch >= '0') && (ch <= '9')) || ((ch == '+') || (ch == '-')))) || (((num10 >= 2) && (ch == '.')) || ((num10 == 3) && ((ch == 'E') || (ch == 'e')))))
                        {
                            num11++;
                        }
                        else if ((num11 != 0) || (ch != ' '))
                        {
                            throw ExceptionUtility.CryptographicException(Resources.Asn1InvalidFormatOfConstructedValue, buffer.ByteCount);
                        }
                    }
                    try
                    {
                        Value = double.Parse(builder.ToString());
                    }
                    catch (FormatException)
                    {
                        throw ExceptionUtility.CryptographicException(Resources.Asn1InvalidFormatOfConstructedValue, buffer.ByteCount);
                    }
                }
                else
                {
                    int num6;
                    int num3;

                    switch ((num2 & RealExplenMask))
                    {
                        case RealExplen1:
                            num3 = 1;
                            break;

                        case RealExplen2:
                            num3 = 2;
                            break;

                        case RealExplen3:
                            num3 = 3;
                            break;

                        default:
                            num3 = buffer.ReadByte();
                            length--;
                            break;
                    }

                    var num4 = (int)Asn1RunTime.DecodeIntValue(buffer, num3, true);
                    length -= num3;

                    var num5 = Asn1RunTime.DecodeIntValue(buffer, length, false) * (1L << ((num2 & RealFactorMask) >> 2));

                    switch ((num2 & RealBaseMask))
                    {
                        case RealBase2:
                            num6 = 2;
                            break;

                        case RealBase8:
                            num6 = 8;
                            break;

                        case RealBase16:
                            num6 = 16;
                            break;

                        default:
                            throw ExceptionUtility.CryptographicException(Resources.Asn1InvalidFormatOfConstructedValue, buffer.ByteCount);
                    }

                    Value = num5 * Math.Pow(num6, num4);

                    if ((num2 & PlusInfinity) != 0)
                    {
                        Value = -Value;
                    }
                }

                buffer.TypeCode = RealTypeCode;
            }
        }
        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;
        }
Exemplo n.º 9
0
        protected virtual void Decode(Asn1BerDecodeBuffer buffer, bool explicitTagging, int implicitLength, Asn1Tag tag)
        {
            int num2;
            var elemLength = explicitTagging ? MatchTag(buffer, tag) : implicitLength;
            var num3 = elemLength;
            var num4 = 0;

            if (StringBuffer == null)
            {
                StringBuffer = new StringBuilder();
            }

            var lastTag = buffer.LastTag;

            if ((lastTag == null) || !lastTag.Constructed)
            {
                if (num3 < 0)
                {
                    throw ExceptionUtility.CryptographicException(Resources.Asn1InvalidLengthException);
                }

                StringBuffer.Length = num3;

                while (num3 > 0)
                {
                    num2 = buffer.Read();

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

                    StringBuffer[num4++] = (char)num2;
                    num3--;
                }
            }
            else
            {
                var capacity = 0;
                var context = new Asn1BerDecodeContext(buffer, elemLength);

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

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

                    capacity += num5;
                    StringBuffer.EnsureCapacity(capacity);

                    while (num5 > 0)
                    {
                        num2 = buffer.Read();

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

                        StringBuffer.Append((char)num2);
                        num5--;
                    }
                }

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

            Value = StringBuffer.ToString();
            buffer.TypeCode = (short)tag.IdCode;
        }
Exemplo n.º 10
0
        public override void Decode(Asn1BerDecodeBuffer buffer, bool explicitTagging, int implicitLength)
        {
            var length = explicitTagging ? MatchTag(buffer, Tag) : implicitLength;

            if (length == 0)
            {
                Value = 0.0;
            }
            else
            {
                var num2 = buffer.ReadByte();

                if (length == 1)
                {
                    switch (num2)
                    {
                    case PlusInfinity:
                        Value = double.PositiveInfinity;
                        return;

                    case MinusInfinity:
                        Value = double.NegativeInfinity;
                        return;
                    }

                    throw ExceptionUtility.CryptographicException(Resources.Asn1InvalidFormatOfConstructedValue, buffer.ByteCount);
                }

                length--;

                if ((num2 & RealBinary) == 0)
                {
                    var num8 = length;
                    var num9 = 0;

                    var builder = new StringBuilder {
                        Length = num8
                    };

                    while (num8 > 0)
                    {
                        var num7 = buffer.Read();

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

                        builder[num9++] = (char)num7;
                        num8--;
                    }

                    var num10 = num2 & RealIso6093Mask;
                    var num11 = 0;

                    for (var i = 0; i < builder.Length; i++)
                    {
                        var ch = builder[i];

                        if ((num10 >= 2) && (ch == ','))
                        {
                            builder[i] = '.';
                            num11++;
                        }
                        else if (((num10 >= 1) && (((ch >= '0') && (ch <= '9')) || ((ch == '+') || (ch == '-')))) || (((num10 >= 2) && (ch == '.')) || ((num10 == 3) && ((ch == 'E') || (ch == 'e')))))
                        {
                            num11++;
                        }
                        else if ((num11 != 0) || (ch != ' '))
                        {
                            throw ExceptionUtility.CryptographicException(Resources.Asn1InvalidFormatOfConstructedValue, buffer.ByteCount);
                        }
                    }
                    try
                    {
                        Value = double.Parse(builder.ToString());
                    }
                    catch (FormatException)
                    {
                        throw ExceptionUtility.CryptographicException(Resources.Asn1InvalidFormatOfConstructedValue, buffer.ByteCount);
                    }
                }
                else
                {
                    int num6;
                    int num3;

                    switch ((num2 & RealExplenMask))
                    {
                    case RealExplen1:
                        num3 = 1;
                        break;

                    case RealExplen2:
                        num3 = 2;
                        break;

                    case RealExplen3:
                        num3 = 3;
                        break;

                    default:
                        num3 = buffer.ReadByte();
                        length--;
                        break;
                    }

                    var num4 = (int)Asn1RunTime.DecodeIntValue(buffer, num3, true);
                    length -= num3;

                    var num5 = Asn1RunTime.DecodeIntValue(buffer, length, false) * (1L << ((num2 & RealFactorMask) >> 2));

                    switch ((num2 & RealBaseMask))
                    {
                    case RealBase2:
                        num6 = 2;
                        break;

                    case RealBase8:
                        num6 = 8;
                        break;

                    case RealBase16:
                        num6 = 16;
                        break;

                    default:
                        throw ExceptionUtility.CryptographicException(Resources.Asn1InvalidFormatOfConstructedValue, buffer.ByteCount);
                    }

                    Value = num5 * Math.Pow(num6, num4);

                    if ((num2 & PlusInfinity) != 0)
                    {
                        Value = -Value;
                    }
                }

                buffer.TypeCode = RealTypeCode;
            }
        }
Exemplo n.º 11
0
        public override void Decode(Asn1BerDecodeBuffer buffer, bool explicitTagging, int implicitLength)
        {
            var elemLength = explicitTagging ? MatchTag(buffer, Tag) : implicitLength;
            var lastTag = buffer.LastTag;

            if ((lastTag == null) || !lastTag.Constructed)
            {
                if (elemLength < 0)
                {
                    throw ExceptionUtility.CryptographicException(Resources.Asn1InvalidLengthException);
                }

                Value = new byte[elemLength];

                if (elemLength != 0)
                {
                    buffer.Read(Value);
                }
            }
            else
            {
                var nbytes = 0;
                var offset = 0;
                var context = new Asn1BerDecodeContext(buffer, elemLength);

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

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

                    nbytes += num2;

                    if (offset == 0)
                    {
                        Value = new byte[nbytes];
                    }
                    else
                    {
                        ReAllocByteArray(nbytes);
                    }

                    buffer.Read(Value, offset, num2);
                    offset = nbytes;
                }

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

            buffer.TypeCode = OctetStringTypeCode;
        }