示例#1
0
        /// <summary>
        /// Convert XML to ASN.1 PDU bytes.
        /// </summary>
        /// <param name="xml">XML. </param>
        /// <returns>ASN.1 PDU. </returns>
        public static byte[] XmlToPdu(string xml)
        {
            XmlDocument doc = new XmlDocument();

            doc.LoadXml(xml);
            IList <object> list = new List <object>();
            GXAsn1Settings s    = new GXAsn1Settings();

            ReadNode(doc.DocumentElement, s, list);
            return(ToByteArray(list[0]));
        }
示例#2
0
        /// <summary>
        /// Convert ASN.1 PDU bytes to XML.
        /// </summary>
        /// <param name="value">Bytes to convert. </param>
        /// <param name="comments">Are comments added to generated XML. </param>
        /// <returns>Converted XML. </returns>
        public static string PduToXml(GXByteBuffer value, bool comments)
        {
            GXAsn1Settings s = new GXAsn1Settings();

            s.Comments = comments;
            IList <object> objects = new List <object>();

            while (value.Position != value.Size)
            {
                GetValue(value, objects, s);
            }
            return(s.ToString());
        }
示例#3
0
        private static int ReadNode(XmlElement node, GXAsn1Settings s, IList <object> list)
        {
            IList <object> tmp;
            string         str = node.Name.ToLower();
            int            tag = s.GetTag(str);

            switch (tag)
            {
            case (byte)BerType.Application:
                tmp = new List <object>();
                foreach (XmlElement node2 in node.ChildNodes)
                {
                    if (node2.NodeType == XmlNodeType.Element)
                    {
                        ReadNode(node2, s, tmp);
                    }
                }
                list.Add(tmp);
                break;

            case (byte)(BerType.Constructed | BerType.Context):
                tmp = new GXAsn1Context();
                foreach (XmlElement node2 in node.ChildNodes)
                {
                    if (node2.NodeType == XmlNodeType.Element)
                    {
                        ReadNode(node2, s, tmp);
                    }
                }
                list.Add(tmp);
                break;

            case (byte)(BerType.Constructed | BerType.Sequence):
                tmp = new GXAsn1Sequence();
                foreach (XmlElement node2 in node.ChildNodes)
                {
                    if (node2.NodeType == XmlNodeType.Element)
                    {
                        ReadNode(node2, s, tmp);
                    }
                }
                list.Add(tmp);
                break;

            case (byte)(BerType.Constructed | BerType.Set):
                tmp = new List <object>();
                foreach (XmlElement node2 in node.ChildNodes)
                {
                    if (node2.NodeType == XmlNodeType.Element)
                    {
                        ReadNode(node2, s, tmp);
                    }
                }
                foreach (object val in tmp)
                {
                    KeyValuePair <object, object> e;
                    if (val is IList)
                    {
                        IList t = (IList)val;
                        e = new KeyValuePair <object, object>(t[0], t[1]);
                    }
                    else
                    {
                        e = new KeyValuePair <object, object>(tmp, null);
                    }
                    list.Add(e);
                }
                break;

            case (byte)BerType.ObjectIdentifier:
                list.Add(new GXAsn1ObjectIdentifier(node.ChildNodes[0].Value));
                break;

            case (byte)BerType.PrintableString:
                list.Add(node.ChildNodes[0].Value);
                break;

            case (byte)BerType.Utf8StringTag:
                list.Add(new GXAsn1Utf8String(node.ChildNodes[0].Value));
                break;

            case (byte)BerType.Ia5String:
                list.Add(new GXAsn1Ia5String(node.ChildNodes[0].Value));
                break;

            case (byte)BerType.Integer:
                list.Add(new GXAsn1Integer(node.ChildNodes[0].Value));
                break;

            case (byte)BerType.Null:
                list.Add(null);
                break;

            case (byte)BerType.BitString:
                list.Add(new GXAsn1BitString(node.ChildNodes[0].Value));
                break;

            case (byte)BerType.UtcTime:
                DateTime d = DateTime.ParseExact(node.ChildNodes[0].Value, "yyyy-MM-dd HH:mm", CultureInfo.InvariantCulture);
                list.Add(d);
                break;

            case (byte)BerType.GeneralizedTime:
                break;

            case (byte)BerType.OctetString:
                break;

            case -1:
                list.Add(Convert.ToSByte(node.ChildNodes[0].Value));
                break;

            case -2:
                list.Add(Convert.ToInt16(node.ChildNodes[0].Value));
                break;

            case -4:
                list.Add(Convert.ToInt32(node.ChildNodes[0].Value));
                break;

            case -8:
                list.Add(Convert.ToInt64(node.ChildNodes[0].Value));
                break;

            default:
                throw new System.ArgumentException("Invalid node: " + node.Name);
            }
            return(0);
        }
示例#4
0
        private static void GetValue(GXByteBuffer bb, IList <object> objects, GXAsn1Settings s)
        {
            int            len;
            short          type;
            IList <object> tmp;

            byte[] tmp2;
            type = bb.GetUInt8();
            len  = GXCommon.GetObjectCount(bb);
            if (len > bb.Available)
            {
                throw new OutOfMemoryException("GXAsn1Converter.GetValue");
            }
            int connectPos = 0;

            if (s != null)
            {
                connectPos = s.XmlLength;
            }
            int    start     = bb.Position;
            string tagString = null;

            if (s != null)
            {
                s.AppendSpaces();
                if (type == (byte)BerType.Integer)
                {
                    if (len == 1 || len == 2 || len == 4 || len == 8)
                    {
                        tagString = s.GetTag((short)-len);
                    }
                    else
                    {
                        tagString = s.GetTag((byte)BerType.Integer);
                    }
                }
                else
                {
                    tagString = s.GetTag(type);
                }
                s.Append("<" + tagString + ">");
            }

            switch (type)
            {
            case (byte)(BerType.Constructed | BerType.Context):
            case ((byte)(BerType.Constructed | BerType.Context) | 1):
            case ((byte)(BerType.Constructed | BerType.Context) | 2):
            case ((byte)(BerType.Constructed | BerType.Context) | 3):
            case ((byte)(BerType.Constructed | BerType.Context) | 4):
                if (s != null)
                {
                    s.Increase();
                }
                tmp = new GXAsn1Context()
                {
                    Index = type & 0xF
                };
                objects.Add(tmp);
                while (bb.Position < start + len)
                {
                    GetValue(bb, tmp, s);
                }
                if (s != null)
                {
                    s.Decrease();
                }
                break;

            case (byte)(BerType.Constructed | BerType.Sequence):
                if (s != null)
                {
                    s.Increase();
                }
                tmp = new GXAsn1Sequence();
                objects.Add(tmp);
                int cnt = 0;
                while (bb.Position < start + len)
                {
                    ++cnt;
                    GetValue(bb, tmp, s);
                }
                if (s != null)
                {
                    // Append comment.
                    s.AppendComment(connectPos, Convert.ToString(cnt) + " elements.");
                    s.Decrease();
                }
                break;

            case (byte)(BerType.Constructed | BerType.Set):
                if (s != null)
                {
                    s.Increase();
                }
                tmp = new List <object>();
                GetValue(bb, tmp, s);
                if (tmp[0] is GXAsn1Sequence)
                {
                    tmp = (GXAsn1Sequence)tmp[0];
                    objects.Add(new KeyValuePair <object, object>(tmp[0], tmp[1]));
                }
                else
                {
                    KeyValuePair <object, object> e = new KeyValuePair <object, object>(tmp, null);
                    objects.Add(e);
                }
                if (s != null)
                {
                    s.Decrease();
                }
                break;

            case (byte)BerType.ObjectIdentifier:
                GXAsn1ObjectIdentifier oi = new GXAsn1ObjectIdentifier(bb, len);
                objects.Add(oi);
                if (s != null)
                {
                    string str = oi.Description;
                    if (str != null)
                    {
                        s.AppendComment(connectPos, str);
                    }
                    s.Append(oi.ToString());
                }

                break;

            case (byte)BerType.PrintableString:
                objects.Add(bb.GetString(len));
                if (s != null)
                {
                    s.Append(Convert.ToString(objects[objects.Count - 1]));
                }

                break;

            case (byte)BerType.Utf8StringTag:
                objects.Add(new GXAsn1Utf8String(bb.GetString(bb.Position, len)));
                bb.Position = bb.Position + len;
                if (s != null)
                {
                    s.Append(Convert.ToString(objects[objects.Count - 1]));
                }

                break;

            case (byte)BerType.Ia5String:
                objects.Add(new GXAsn1Ia5String(bb.GetString(len)));
                if (s != null)
                {
                    s.Append(Convert.ToString(objects[objects.Count - 1]));
                }
                break;

            case (byte)BerType.Integer:
                if (len == 1)
                {
                    objects.Add(bb.GetInt8());
                }
                else if (len == 2)
                {
                    objects.Add(bb.GetInt16());
                }
                else if (len == 4)
                {
                    objects.Add(bb.GetInt32());
                }
                else
                {
                    tmp2 = new byte[len];
                    bb.Get(tmp2);
                    objects.Add(new GXAsn1Integer(tmp2));
                }
                if (s != null)
                {
                    s.Append(Convert.ToString(objects[objects.Count - 1]));
                }
                break;

            case (byte)BerType.Null:
                objects.Add(null);
                break;

            case (byte)BerType.BitString:
                GXAsn1BitString tmp3 = new GXAsn1BitString(bb.SubArray(bb.Position, len));
                objects.Add(tmp3);
                bb.Position = bb.Position + len;
                if (s != null)
                {
                    // Append comment.
                    s.AppendComment(connectPos, Convert.ToString(tmp3.Length) + " bit.");
                    s.Append(tmp3.asString());
                }
                break;

            case (byte)BerType.UtcTime:
                tmp2 = new byte[len];
                bb.Get(tmp2);
                objects.Add(GetUtcTime(ASCIIEncoding.ASCII.GetString(tmp2)));
                if (s != null)
                {
                    s.Append(((DateTimeOffset)objects[objects.Count - 1]).UtcDateTime.ToString("yyyy-MM-dd HH:mm", CultureInfo.InvariantCulture));
                }
                break;

            case (byte)BerType.GeneralizedTime:
                tmp2 = new byte[len];
                bb.Get(tmp2);
                objects.Add(GXCommon.GetGeneralizedTime(ASCIIEncoding.ASCII.GetString(tmp2)));
                if (s != null)
                {
                    s.Append(Convert.ToString(objects[objects.Count - 1]));
                }
                break;

            case (byte)BerType.Context:
            case (byte)BerType.Context | 1:
            case (byte)BerType.Context | 2:
            case (byte)BerType.Context | 3:
                tmp = new GXAsn1Context()
                {
                    Constructed = false, Index = type & 0xF
                };
                tmp2 = new byte[len];
                bb.Get(tmp2);
                tmp.Add(tmp2);
                objects.Add(tmp);
                if (s != null)
                {
                    s.Append(GXCommon.ToHex(tmp2, false));
                }
                break;

            case (byte)BerType.OctetString:
                int t = bb.GetUInt8(bb.Position);
                switch (t)
                {
                case (byte)(BerType.Constructed | BerType.Sequence):
                case (byte)BerType.BitString:
                    if (s != null)
                    {
                        s.Increase();
                    }
                    GetValue(bb, objects, s);
                    if (s != null)
                    {
                        s.Decrease();
                    }
                    break;

                default:
                    tmp2 = new byte[len];
                    bb.Get(tmp2);
                    objects.Add(tmp2);
                    if (s != null)
                    {
                        s.Append(GXCommon.ToHex(tmp2, false));
                    }
                    break;
                }
                break;

            case (byte)BerType.Boolean:
                bool b = bb.GetUInt8() != 0;
                objects.Add(b);
                if (s != null)
                {
                    s.Append(Convert.ToString(b));
                }
                break;

            default:
                throw new System.ArgumentException("Invalid type: " + type);
            }
            if (s != null)
            {
                s.Append("</" + tagString + ">\r\n");
            }
        }