Exemple #1
0
        /// <summary>
        /// Decode ASN.1 encoded node Stream data.
        /// </summary>
        /// <param name="xdata">Stream data.</param>
        /// <returns>true:Succeed, false:Failed.</returns>
        protected bool GeneralDecode(Stream xdata)
        {
            bool retval = false;
            long nodeMaxLen;

            nodeMaxLen = xdata.Length - xdata.Position;
            tag        = (byte)xdata.ReadByte();
            long start, end;

            start      = xdata.Position;
            dataLength = Asn1Util.DerLengthDecode(xdata, ref isIndefiniteLength);
            if (dataLength < 0)
            {
                return(retval);                // Node data length can not be negative.
            }
            end = xdata.Position;
            lengthFieldBytes = end - start;
            if (nodeMaxLen < (dataLength + TagLength + lengthFieldBytes))
            {
                return(retval);
            }
            if (ParentNode == null || ((ParentNode.tag & Asn1TagClasses.CONSTRUCTED) == 0))
            {
                if ((tag & Asn1Tag.TAG_MASK) <= 0 || (tag & Asn1Tag.TAG_MASK) > 0x1E)
                {
                    return(retval);
                }
            }
            if (tag == Asn1Tag.BIT_STRING)
            {
                // First byte of BIT_STRING is unused bits.
                // BIT_STRING data does not include this byte.

                // Fixed by Gustaf Bj�rklund.
                if (dataLength < 1)
                {
                    return(retval);                // We cannot read less than 1 - 1 bytes.
                }
                unusedBits = (byte)xdata.ReadByte();
                data       = new byte[dataLength - 1];
                xdata.Read(data, 0, (int)(dataLength - 1));
            }
            else
            {
                data = new byte[dataLength];
                xdata.Read(data, 0, (int)(dataLength));
            }
            retval = true;
            return(retval);
        }
Exemple #2
0
        /// <summary>
        /// Decode ASN.1 encoded complex data type Stream data.
        /// </summary>
        /// <param name="xdata">Stream data.</param>
        /// <returns>true:Succeed, false:Failed.</returns>
        protected bool ListDecode(Stream xdata)
        {
            bool retval           = false;
            long originalPosition = xdata.Position;
            long childNodeMaxLen;

            try
            {
                childNodeMaxLen = xdata.Length - xdata.Position;
                tag             = (byte)xdata.ReadByte();
                long start, end, offset;
                start      = xdata.Position;
                dataLength = Asn1Util.DerLengthDecode(xdata, ref isIndefiniteLength);
                if (dataLength < 0 || childNodeMaxLen < dataLength)
                {
                    return(retval);
                }
                end = xdata.Position;
                lengthFieldBytes = end - start;
                offset           = dataOffset + TagLength + lengthFieldBytes;
                Stream secData;
                byte[] secByte;
                if (tag == Asn1Tag.BIT_STRING)
                {
                    // First byte of BIT_STRING is unused bits.
                    // BIT_STRING data does not include this byte.
                    unusedBits = (byte)xdata.ReadByte();
                    dataLength--;
                    offset++;
                }
                if (dataLength <= 0)
                {
                    return(retval);                 // List data length cann't be zero.
                }
                secData = new MemoryStream((int)dataLength);
                secByte = new byte[dataLength];
                xdata.Read(secByte, 0, (int)(dataLength));
                if (tag == Asn1Tag.BIT_STRING)
                {
                    dataLength++;
                }
                secData.Write(secByte, 0, secByte.Length);
                secData.Position = 0;
                while (secData.Position < secData.Length)
                {
                    Asn1Node node = new Asn1Node(this, offset);
                    node.parseEncapsulatedData = this.parseEncapsulatedData;
                    start = secData.Position;
                    if (!node.InternalLoadData(secData))
                    {
                        return(retval);
                    }
                    AddChild(node);
                    end     = secData.Position;
                    offset += end - start;
                }
                retval = true;
            }
            finally
            {
                if (!retval)
                {
                    xdata.Position = originalPosition;
                    ClearAll();
                }
            }
            return(retval);
        }