Example #1
0
 /// <summary>
 /// Check if the string is ASN.1 encoded hex string.
 /// </summary>
 /// <param name="dataStr">The string.</param>
 /// <returns>true:Yes, false:No.</returns>
 public static bool IsAsn1EncodedHexStr(string dataStr)
 {
     bool retval = false;
     try
     {
         byte[] data = HexStrToBytes(dataStr);
         if (data.Length > 0)
         {
             Asn1Node node = new Asn1Node();
             retval = node.LoadData(data);
         }
     }
     catch
     {
         retval = false;
     }
     return retval;
 }
 /// <summary>
 /// Search a Asn1Node in the sub tree of treeNode.
 /// </summary>
 /// <param name="treeNode">The sub tree node.</param>
 /// <param name="node">The Asn1Node node.</param>
 /// <returns></returns>
 public static TreeNode SearchTreeNode(TreeNode treeNode, Asn1Node node)
 {
     TreeNode retval = null;
     if (node == null) return retval;
     if (((Asn1TreeNode)treeNode).ANode == node) return treeNode;
     Asn1Node tmpTreeNode;
     for (int i =0; i<treeNode.Nodes.Count; i++)
     {
         tmpTreeNode = ((Asn1TreeNode) treeNode.Nodes[i]).ANode;
         if (tmpTreeNode == node)
         {
             retval = treeNode.Nodes[i];
             break;
         }
         retval = SearchTreeNode(treeNode.Nodes[i], node);
         if (retval != null)
             break;
     }
     return retval;
 }
Example #3
0
        /// <summary>
        /// Insert a node in the children list after the pointed node.
        /// </summary>
        /// <param name="xdata">Asn1Node</param>
        /// <param name="indexNode">Index node.</param>
		/// <returns>New node index.</returns>
		public int InsertChildAfter(Asn1Node xdata, Asn1Node indexNode)
        {
			int index = childNodeList.IndexOf(indexNode)+1;
            childNodeList.Insert(index, xdata);
            RecalculateTreePar();
			return index;
        }
Example #4
0
        /// <summary>
        /// Insert a node in the children list before the pointed index.
        /// </summary>
        /// <param name="xdata">Asn1Node</param>
        /// <param name="index">0 based index.</param>
		/// <returns>New node index.</returns>
		public int InsertChild(Asn1Node xdata, int index)
        {
            childNodeList.Insert(index, xdata);
            RecalculateTreePar();
			return index;
        }
Example #5
0
 /// <summary>
 /// Add child node at the end of children list.
 /// </summary>
 /// <param name="xdata">the node that will be add in.</param>
 public void AddChild(Asn1Node xdata)
 {
     childNodeList.Add(xdata);
     RecalculateTreePar();
 }
Example #6
0
		/// <summary>
		/// Retrieve all the node count in the node subtree.
		/// </summary>
		/// <param name="node">starting node.</param>
		/// <returns>long integer node count in the node subtree.</returns>
		public static long GetDescendantNodeCount(Asn1Node node)
		{
			long count =0;
			count += node.ChildNodeCount;
			for (int i=0; i<node.ChildNodeCount; i++)
			{
				count += GetDescendantNodeCount(node.GetChildNode(i));
			}
			return count;
		}
Example #7
0
		/// <summary>
		/// Get node by OID.
		/// </summary>
		/// <param name="oid">OID.</param>
		/// <param name="startNode">Starting node.</param>
		/// <returns>Null or Asn1Node.</returns>
		static public Asn1Node GetDecendantNodeByOid(string oid, Asn1Node startNode)
		{
			Asn1Node retval = null;
            Oid xoid = new Oid();
			for (int i = 0; i<startNode.ChildNodeCount; i++)
			{
				Asn1Node childNode = startNode.GetChildNode(i);
				int tmpTag = childNode.tag & Asn1Tag.TAG_MASK;
				if (tmpTag == Asn1Tag.OBJECT_IDENTIFIER)
				{
					if (oid == xoid.Decode(childNode.Data))
					{
						retval = childNode;
						break;
					}
				}
				retval = GetDecendantNodeByOid(oid, childNode);
				if (retval != null) break;
			}
			return retval;
		}
Example #8
0
        /// <summary>
        /// Get the node and all the descendents text description.
        /// </summary>
        /// <param name="startNode">starting node.</param>
        /// <param name="lineLen">line length.</param>
        /// <returns></returns>
        public string GetText(Asn1Node startNode, int lineLen)
        {
            string nodeStr = "";
            string baseLine = "";
            string dataStr = "";
            const string lStr = "      |      |       | ";
            string oid, oidName;
            switch (tag)
            {
                case Asn1Tag.BIT_STRING:
                    baseLine = 
                        String.Format("{0,6}|{1,6}|{2,7}|{3} {4} UnusedBits:{5} : ", 
                        dataOffset, 
                        dataLength, 
                        lengthFieldBytes, 
                        GetIndentStr(startNode), 
                        TagName, 
                        unusedBits
                        ); 
                    dataStr = Asn1Util.ToHexString(data);
                    if (baseLine.Length + dataStr.Length < lineLen)
                    {
                        if (dataStr.Length<1)
                        {
                            nodeStr += baseLine + "\r\n";
                        }
                        else
                        {
                            nodeStr += baseLine + "'" + dataStr + "'\r\n";
                        }
                    }
                    else
                    {
                        nodeStr += baseLine + FormatLineHexString(
                            lStr, 
                            GetIndentStr(startNode).Length, 
                            lineLen, 
                            dataStr + "\r\n"
                            );
                    }
                    break;
                case Asn1Tag.OBJECT_IDENTIFIER:
                    Oid xoid  = new Oid();
                    oid = xoid.Decode(new MemoryStream(data));
                    oidName = xoid.GetOidName(oid);
                    nodeStr += String.Format("{0,6}|{1,6}|{2,7}|{3} {4} : {5} [{6}]\r\n", 
                        dataOffset,
                        dataLength, 
                        lengthFieldBytes, 
                        GetIndentStr(startNode), 
                        TagName, 
                        oidName,
                        oid
                        ); 
                    break;
                case Asn1Tag.RELATIVE_OID:
                    RelativeOid xiod = new RelativeOid();
                    oid = xiod.Decode(new MemoryStream(data));
                    oidName = "";
                    nodeStr += String.Format("{0,6}|{1,6}|{2,7}|{3} {4} : {5} [{6}]\r\n", 
                        dataOffset,
                        dataLength, 
                        lengthFieldBytes, 
                        GetIndentStr(startNode), 
                        TagName, 
                        oidName,
                        oid
                        ); 
                    break;
                case Asn1Tag.PRINTABLE_STRING:
                case Asn1Tag.IA5_STRING:
                case Asn1Tag.UNIVERSAL_STRING:
                case Asn1Tag.VISIBLE_STRING:
                case Asn1Tag.NUMERIC_STRING:
                case Asn1Tag.UTC_TIME:
                case Asn1Tag.UTF8_STRING:
                case Asn1Tag.BMPSTRING:
				case Asn1Tag.GENERAL_STRING:
                case Asn1Tag.GENERALIZED_TIME:
                    baseLine = 
                        String.Format("{0,6}|{1,6}|{2,7}|{3} {4} : ", 
                        dataOffset,
                        dataLength, 
                        lengthFieldBytes, 
                        GetIndentStr(startNode), 
                        TagName
                        );
					if ( tag == Asn1Tag.UTF8_STRING )
					{
						UTF8Encoding unicode = new UTF8Encoding();
						dataStr = unicode.GetString(data);
					} 
					else 
					{
						dataStr = Asn1Util.BytesToString(data);
					}
                    if (baseLine.Length + dataStr.Length < lineLen)
                    {
                        nodeStr += baseLine + "'" + dataStr + "'\r\n";
                    }
                    else
                    {
                        nodeStr += baseLine + FormatLineString(
                            lStr, 
                            GetIndentStr(startNode).Length, 
                            lineLen, 
                            dataStr) + "\r\n";
                    }
                    break;
                case Asn1Tag.INTEGER:
                    if (data != null && dataLength < 8)
                    {
                        nodeStr += String.Format("{0,6}|{1,6}|{2,7}|{3} {4} : {5}\r\n", 
                            dataOffset,
                            dataLength, 
                            lengthFieldBytes, 
                            GetIndentStr(startNode), 
                            TagName,
                            Asn1Util.BytesToLong(data).ToString()
                            );
                    }
                    else
                    {
                        baseLine = 
                            String.Format("{0,6}|{1,6}|{2,7}|{3} {4} : ", 
                            dataOffset,
                            dataLength, 
                            lengthFieldBytes, 
                            GetIndentStr(startNode), 
                            TagName
                            );
                        nodeStr += GetHexPrintingStr(startNode, baseLine, lStr, lineLen);
                    }
                    break;
                default:
                    if ((tag & Asn1Tag.TAG_MASK) == 6) // Visible string for certificate
                    {
                        baseLine = 
                            String.Format("{0,6}|{1,6}|{2,7}|{3} {4} : ", 
                            dataOffset,
                            dataLength, 
                            lengthFieldBytes, 
                            GetIndentStr(startNode), 
                            TagName
                            );
                        dataStr = Asn1Util.BytesToString(data);
                        if (baseLine.Length + dataStr.Length < lineLen)
                        {
                            nodeStr += baseLine + "'" + dataStr + "'\r\n";
                        }
                        else
                        {
                            nodeStr += baseLine + FormatLineString(
                                lStr, 
                                GetIndentStr(startNode).Length, 
                                lineLen, 
                                dataStr) + "\r\n";
                        }
                    }
                    else
                    {
                        baseLine = 
                            String.Format("{0,6}|{1,6}|{2,7}|{3} {4} : ", 
                            dataOffset,
                            dataLength, 
                            lengthFieldBytes, 
                            GetIndentStr(startNode), 
                            TagName
                            );
                        nodeStr += GetHexPrintingStr(startNode, baseLine, lStr, lineLen);
                    }
                    break;
            };
            if (childNodeList.Count >= 0)
            {
                nodeStr += GetListStr(startNode, lineLen);
            }
            return nodeStr;
        }
Example #9
0
		private Asn1Node(Asn1Node parentNode, long dataOffset)
		{
			Init();
			deepness = parentNode.Deepness + 1;
			this.parentNode = parentNode;
			this.dataOffset = dataOffset;
		}
Example #10
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;
        }
Example #11
0
 /// <summary>
 /// Generate the node indent string.
 /// </summary>
 /// <param name="startNode">The node.</param>
 /// <returns>Text string.</returns>
 protected string GetIndentStr(Asn1Node startNode)
 {
     string retval = "";
     long startLen = 0;
     if (startNode!=null)
     {
         startLen = startNode.Deepness;
     }
     for (long i = 0; i<deepness - startLen; i++)
     {
         retval += "   ";
     }
     return retval;
 }
Example #12
0
 /// <summary>
 /// Generate all the child text from childNodeList.
 /// </summary>
 /// <param name="startNode">Starting node.</param>
 /// <param name="lineLen">Line length.</param>
 /// <returns>Text string.</returns>
 protected string GetListStr(Asn1Node startNode, int lineLen)
 {
     string nodeStr = "";
     int i;
     Asn1Node tempNode;
     for (i=0; i<childNodeList.Count; i++)
     {
         tempNode = (Asn1Node) childNodeList[i];
         nodeStr += tempNode.GetText(startNode, lineLen);
     }
     return nodeStr;
 }
Example #13
0
 /// <summary>
 /// Recursively set all the child parameters, except dataLength.
 /// dataLength is set by ResetBranchDataLength.
 /// </summary>
 /// <param name="xNode">Starting node.</param>
 /// <param name="subOffset">Starting node offset.</param>
 protected void ResetChildNodePar(Asn1Node xNode, long subOffset)
 {
     int i;
     if (xNode.tag == Asn1Tag.BIT_STRING)
     {
         subOffset++;
     }
     Asn1Node tempNode;
     for (i=0; i<xNode.ChildNodeCount; i++)
     {
         tempNode = xNode.GetChildNode(i);
         tempNode.parentNode = xNode;
         tempNode.dataOffset = subOffset;
         tempNode.deepness = xNode.deepness + 1;
         tempNode.path = xNode.path + '/' + i.ToString();
         subOffset += TagLength + tempNode.lengthFieldBytes;
         ResetChildNodePar(tempNode, subOffset);
         subOffset += tempNode.dataLength;
     }
 }
Example #14
0
 /// <summary>
 /// Encode the node data length field and set lengthFieldBytes and dataLength.
 /// </summary>
 /// <param name="node">The node needs to be reset.</param>
 protected static void ResetDataLengthFieldWidth(Asn1Node node)
 {
     MemoryStream tempStream = new MemoryStream();
     Asn1Util.DERLengthEncode(tempStream, (ulong) node.dataLength);
     node.lengthFieldBytes = tempStream.Length;
     tempStream.Close();
 }
Example #15
0
 /// <summary>
 /// Recursively set all the node data length.
 /// </summary>
 /// <param name="node"></param>
 /// <returns>node data length.</returns>
 protected static long ResetBranchDataLength(Asn1Node node)
 {
     long retval = 0;
     long childDataLength = 0;
     if (node.ChildNodeCount < 1)
     {
         if (node.data != null)
             childDataLength += node.data.Length;
     }
     else
     {
         for (int i=0; i<node.ChildNodeCount; i++)
         {
             childDataLength += ResetBranchDataLength(node.GetChildNode(i));
         }
     }
     node.dataLength = childDataLength;
     if (node.tag == Asn1Tag.BIT_STRING)
         node.dataLength += BitStringUnusedFiledLength;
     ResetDataLengthFieldWidth(node);
     retval = node.dataLength + TagLength + node.lengthFieldBytes;
     return retval;
 }
Example #16
0
        /// <summary>
        /// Insert a node in the children list after the pointed node.
        /// </summary>
        /// <param name="xdata">Asn1Node that will be instered in the children list.</param>
        /// <param name="index">0 based index.</param>
		/// <returns>New node index.</returns>
		public int InsertChildAfter(Asn1Node xdata, int index)
        {
			int xindex = index+1;
            childNodeList.Insert(xindex, xdata);
            RecalculateTreePar();
			return xindex;
        }
Example #17
0
 /// <summary>
 /// Remove the child from children node list.
 /// </summary>
 /// <param name="node">The node needs to be removed.</param>
 /// <returns></returns>
 public Asn1Node RemoveChild(Asn1Node node)
 {
     Asn1Node retval = null;
     int i = childNodeList.IndexOf(node);
     retval = RemoveChild(i);
     return retval;
 }
Example #18
0
		private void Init()
		{
			childNodeList = new ArrayList();
			data = null;
			dataLength = 0;
			lengthFieldBytes = 0;
			unusedBits = 0;
			tag = Asn1Tag.SEQUENCE | Asn1TagClasses.CONSTRUCTED;
			childNodeList.Clear();
			deepness = 0;
			parentNode = null;
		}
Example #19
0
 /// <summary>
 /// Use an Asn1Node and display label mask to construct an Asn1TreeNode.
 /// Mask is defined in <see cref="Asn1Node.TagTextMask"/>.
 /// </summary>
 /// <param name="node">node.</param>
 /// <param name="mask">mask <see cref="Asn1Node.TagTextMask"/>.</param>
 public Asn1TreeNode(Asn1Node node, uint mask)
 {
     asn1Node = node;
     this.Text = node.GetLabel(mask);
 }
Example #20
0
		private string GetHexPrintingStr(Asn1Node startNode, string baseLine, 
			string lStr, int lineLen)
		{
			string nodeStr = "";
			string iStr = GetIndentStr(startNode);
			string dataStr = Asn1Util.ToHexString(data);
			if (dataStr.Length > 0)
			{
				if (baseLine.Length + dataStr.Length < lineLen)
				{
					nodeStr += baseLine + "'" + dataStr + "'";
				}
				else
				{
					nodeStr += baseLine + FormatLineHexString(
						lStr, 
						iStr.Length, 
						lineLen, 
						dataStr
						);
				}
			}
			else
			{
				nodeStr += baseLine;
			}
			return nodeStr + "\r\n";
		}
Example #21
0
 /// <summary>
 /// Generate node text description. It uses GetNodeTextHeader to generate
 /// the heading and Asn1Node.GetText to generate the node text.
 /// </summary>
 /// <param name="node">Target node.</param>
 /// <param name="lineLen">Line length.</param>
 /// <returns>Text string.</returns>
 public static string GetNodeText(Asn1Node node, int lineLen)
 {
     string nodeStr = GetNodeTextHeader(lineLen);
     nodeStr +=node.GetText(node, lineLen);
     return nodeStr;
 }
Example #22
0
 /// <summary>
 /// Clone a new Asn1Node by current node.
 /// </summary>
 /// <returns>new node.</returns>
 public Asn1Node Clone()
 {
     MemoryStream ms = new MemoryStream();
     this.SaveData(ms);
     //ms.Position = 0;
     Asn1Node node = new Asn1Node();
     node.LoadData(ms);
     return node;
 }