Пример #1
0
        public static Asn1Node Parse(XElement xNode)
        {
            switch (xNode.Name.LocalName)
            {
            case Asn1Sequence.NODE_NAME:
                return(Asn1Sequence.Parse(xNode));

            case Asn1Set.NODE_NAME:
                return(Asn1Set.Parse(xNode));

            case Asn1Integer.NODE_NAME:
                return(Asn1Integer.Parse(xNode));

            case Asn1ObjectIdentifier.NODE_NAME: return(Asn1ObjectIdentifier.Parse(xNode));

            case Asn1PrintableString.NODE_NAME:
                return(Asn1PrintableString.Parse(xNode));

            case Asn1Utf8String.NODE_NAME:
                return(Asn1Utf8String.Parse(xNode));

            case Asn1Null.NODE_NAME:
                return(Asn1Null.Parse(xNode));

            case Asn1BitString.NODE_NAME:
                return(Asn1BitString.Parse(xNode));

            case Asn1CustomNode.NODE_NAME:
                return(Asn1CustomNode.Parse(xNode));

            default:
                throw new Exception("Invalid Node " + xNode.Name.LocalName);
            }
        }
Пример #2
0
        public static Asn1Node ReadNode(Stream stream)
        {
            var identifier = stream.ReadByte();
            var type       = (Asn1UniversalNodeType)(identifier & 0x1f);
            var tagClass   = (Asn1TagClass)(identifier >> 6);
            var tagForm    = (Asn1TagForm)((identifier >> 5) & 1);
            var length     = ReadTagLength(stream);

            if (length > stream.Length)
            {
                throw new Asn1ParsingException($"Try to read more bytes from stream than exists {length} > {stream.Length}");
            }
            var data = new byte[length];

            stream.Read(data, 0, length);
            stream = new MemoryStream(data);
            if (tagClass == Asn1TagClass.Universal)
            {
                var tag = ReadUniversalNode(type, tagForm, stream);
                tag.TagClass = tagClass;
                return(tag);
            }
            else
            {
                var tag = Asn1CustomNode.ReadFrom(type, tagForm, stream);
                tag.TagClass = tagClass;
                return(tag);
            }
        }
Пример #3
0
        public new static Asn1CustomNode Parse(XElement xNode)
        {
            var type     = int.Parse(xNode.Attribute("type").Value);
            var form     = (Asn1TagForm)Enum.Parse(typeof(Asn1TagForm), xNode.Attribute("form").Value);
            var tagClass = (Asn1TagClass)Enum.Parse(typeof(Asn1TagClass), xNode.Attribute("class").Value);
            var res      = new Asn1CustomNode(type, form)
            {
                TagClass = tagClass
            };

            res.Data = ReadDataFromHexString(xNode.Value);
            return(res);
        }
Пример #4
0
        public static Asn1CustomNode ReadFrom(Asn1UniversalNodeType type, Asn1TagForm tagForm, Stream stream)
        {
            if (tagForm == Asn1TagForm.Primitive)
            {
                var data = new byte[stream.Length];
                stream.Read(data, 0, data.Length);

                return(new Asn1CustomNode((int)type, tagForm)
                {
                    Data = data
                });
            }
            else
            {
                var res = new Asn1CustomNode((int)type, tagForm);
                res.ReadChildren(stream);
                return(res);
            }
        }
Пример #5
0
        public static Asn1Node ReadNode(Stream stream)
        {
            var identifier                 = stream.ReadByte();
            Asn1UniversalNodeType type     = (Asn1UniversalNodeType)(identifier & 0x1f);
            Asn1TagClass          tagClass = (Asn1TagClass)(identifier >> 6);
            Asn1TagForm           tagForm  = (Asn1TagForm)((identifier >> 5) & 1);
            int?length = ReadTagLength(stream);

            if (identifier == 0 && length == 0)
            {
                return(null);                                   // EOC detected
            }
            if (length > stream.Length)
            {
                throw new Asn1ParsingException($"Try to read more bytes from stream than exists {length} > {stream.Length}");
            }
            if (length != null)
            {
                var data = new byte[(int)length];
                stream.Read(data, 0, (int)length);
                stream = new MemoryStream(data);
                if (tagClass == Asn1TagClass.Universal)
                {
                    var tag = ReadUniversalNode(type, tagForm, stream);
                    tag.TagClass = tagClass;
                    return(tag);
                }
                else
                {
                    var tag = Asn1CustomNode.ReadFrom(type, tagForm, stream);
                    tag.TagClass = tagClass;
                    return(tag);
                }
            }
            else
            {
                // We must just keep on reading the stream rather than keeping a separate memory buffer
                Asn1DynamicArray tag = Asn1DynamicArray.ReadFrom(type, stream);
                tag.TagClass = tagClass;
                return(tag);
            }
        }
Пример #6
0
        public static Asn1Node ReadNode(Stream stream)
        {
            var identifier = stream.ReadByte();
            var type       = (Asn1UniversalNodeType)(identifier & 0x1f);
            var tagClass   = (Asn1TagClass)(identifier >> 6);
            var tagForm    = (Asn1TagForm)((identifier >> 5) & 1);
            var length     = ReadTagLength(stream);
            var data       = new byte[length];

            stream.Read(data, 0, length);
            stream = new MemoryStream(data);
            if (tagClass == Asn1TagClass.Universal)
            {
                var tag = ReadUniversalNode(type, tagForm, stream);
                tag.TagClass = tagClass;
                return(tag);
            }
            else
            {
                var tag = Asn1CustomNode.ReadFrom(type, tagForm, stream);
                tag.TagClass = tagClass;
                return(tag);
            }
        }