Click here to Skip to main content
15,886,519 members
Articles / Programming Languages / C#

ASN1 Visualizer for Visual Studio 2008

Rate me:
Please Sign up or sign in to vote.
4.63/5 (6 votes)
17 Sep 2009CPOL8 min read 39.4K   1.1K   21  
A Visualizer for Visual Studio 2008 which translates ASN1 der encoded hex strings into a visible tree of data.
/*
    Copyright 2009 Juan Estrada
    
    This file is part of ASN1 Visualizer.

    ASN1 Visualizer is free software: you can redistribute it and/or modify
    it under the terms of the The Code Project Open License (CPOL) as published by
    The Code Project, either version 1.02 of the License, or
    (at your option) any later version.

    ASN1 Visualizer is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    The Code Project Open License for more details.

    You should have received a copy of the The Code Project Open License
    along with ASN1 Visualizer.  If not, see <http://www.codeproject.com/info/cpol10.aspx>.
*/

using System;
using System.Linq;
using System.Windows.Forms;
using System.Xml.Linq;
using ASN1Visualizer.ASN1;

namespace ASN1Visualizer
{
    public partial class ShowBox : Form
    {
        public ShowBox()
        {
            InitializeComponent();
            rootElement = XElement.Parse(Properties.Resources.OID);
            
        }

        public void fill(ASN1Node pRoot)
        {
            treeView1.Nodes.Add(Transform(pRoot,-1));
        }

        XElement rootElement = null;

        private string obtainNameFromOID(string pNodeString)
        {
            //(ObjectIdentifier,8) UnB: 0 --> 1.2.840.113549.2.5
            string lOID = pNodeString.Remove(0, pNodeString.IndexOf('>') + 2);

            foreach (XElement childElement in rootElement.Elements())
            {
                if (childElement.Attribute("ID").Value == lOID)
                {
                    return pNodeString + " (" + childElement.Attribute("Name").Value + ")";
                }                
            }

            return pNodeString;
        }

        public TreeNode Transform(ASN1Node pRoot, int index)
        {
            TreeNode lNode;
            if ((ASN1DataType)pRoot.Tag == ASN1DataType.ObjectIdentifier)
            {
                lNode = new TreeNode(obtainNameFromOID(pRoot.ToString(true)), ConvertTagToIndex(pRoot.Tag), ConvertTagToIndex(pRoot.Tag));                
            }
            else
            {
                lNode = new TreeNode(pRoot.ToString(true), ConvertTagToIndex(pRoot.Tag), ConvertTagToIndex(pRoot.Tag));
            }
            if (index >= 0)
                lNode.Name = "[" + index + "] " + pRoot.ToShortName();
            else
                lNode.Name = pRoot.ToShortName();
            int ind = -1;
            if (((ASN1DataType)pRoot.Tag == ASN1DataType.Sequence) || ((ASN1DataType)pRoot.Tag == ASN1DataType.Set))
                ind = 0;
            foreach (ASN1Node item in pRoot.ChildNodes)
            {
                lNode.Nodes.Add(Transform(item,ind));
                ind++;
            }
            return lNode;
        }

        private int ConvertTagToIndex(byte pTag)
        {
            switch (pTag)
            {
                case (byte)ASN1DataType.Bitstring:
                    return 0;                    
                case (byte)ASN1DataType.Enumerated:
                    return 1;
                case (byte)ASN1DataType.ObjectIdentifier:
                    return 3; 
                case (byte)ASN1DataType.Integer:
                    return 4;
                case (byte)ASN1DataType.Null:
                    return 5;
                case (byte)ASN1DataType.OctetString:
                    return 6;
                case (byte)ASN1DataType.Sequence:
                    return 7;
                case (byte)ASN1DataType.Set:
                    return 8;
                case (byte)ASN1DataType.GeneralizedTime:
                case (byte)ASN1DataType.UTCTime:
                    return 9;
                case (byte)ASN1DataType.IA5String:
                case (byte)ASN1DataType.PrintableString:
                case (byte)ASN1DataType.T61String:
                    return 10;
                default:
                    return 2;
            }
        }

        private void copyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Clipboard.Clear();
            Clipboard.SetText(treeView1.SelectedNode.Text);
            
        }

        private void oIDLookUpToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
            try
            {
                if (treeView1.SelectedNode.Text.StartsWith("(ObjectIdentifier"))
                {
                    //(ObjectIdentifier,9) UnB: 0 --> 1.2.840.113549.1.7.2 (signedData)
                    int start = treeView1.SelectedNode.Text.IndexOf('>') + 2;
                    int count = 0;
                    if(treeView1.SelectedNode.Text.Contains('('))
                        count = (treeView1.SelectedNode.Text.LastIndexOf('(')) - start;
                    else
                        count = treeView1.SelectedNode.Text.Length - start;
                    System.Diagnostics.Process.Start(new Uri("http://www.oid-info.com/get/" + treeView1.SelectedNode.Text.Substring(start, count).Trim()).AbsoluteUri);
                }
                else
                {
                    System.Diagnostics.Process.Start(new Uri("http://www.google.com/#q=" + treeView1.SelectedNode.Text.Trim()).AbsoluteUri);
                }
            }
            catch
            { }
        }

        private void btnExpand_Click(object sender, EventArgs e)
        {            
            treeView1.ExpandAll();
        }

        private void btnCollapse_Click(object sender, EventArgs e)
        {
            treeView1.CollapseAll();
        }

        private void collapseExpandToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (treeView1.SelectedNode.IsExpanded)
                treeView1.SelectedNode.Collapse(false);
            else
                treeView1.SelectedNode.ExpandAll();
        }

        private void treeView1_BeforeCollapse(object sender, TreeViewCancelEventArgs e)
        {
            this.Cursor = Cursors.AppStarting;
        }

        private void treeView1_BeforeExpand(object sender, TreeViewCancelEventArgs e)
        {
            this.Cursor = Cursors.AppStarting;
        }

        private void treeView1_AfterCollapse(object sender, TreeViewEventArgs e)
        {
            this.Cursor = Cursors.Default;
        }

        private void treeView1_AfterExpand(object sender, TreeViewEventArgs e)
        {
            this.Cursor = Cursors.Default;
        }

        private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
                treeView1.SelectedNode = e.Node;
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            txtPath.Text = GetPath(treeView1.SelectedNode);
        }

        private string GetPath(TreeNode treeNode)
        {
            if (treeNode.Parent != null)
                return GetPath(treeNode.Parent) + @"\" + treeNode.Name;
            else
                return treeNode.Name;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Uruguay Uruguay
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions