/// <summary> /// Retrieve the node description. /// </summary> /// <param name="pureHexMode">true:Return hex string only; /// false:Convert to more readable string depending on the node tag.</param> /// <returns>string</returns> public string GetDataStr(bool pureHexMode) { const int lineLen = 32; string dataStr = ""; if (pureHexMode) { dataStr = Asn1Util.FormatString(Asn1Util.ToHexString(data), lineLen, 2); } else { switch (tag) { case Asn1Tag.BIT_STRING: dataStr = Asn1Util.FormatString(Asn1Util.ToHexString(data), lineLen, 2); break; case Asn1Tag.OBJECT_IDENTIFIER: Oid xoid = new Oid(); dataStr = xoid.Decode(new MemoryStream(data)); break; case Asn1Tag.RELATIVE_OID: RelativeOid roid = new RelativeOid(); dataStr = roid.Decode(new MemoryStream(data)); 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.BMPSTRING: case Asn1Tag.GENERAL_STRING: case Asn1Tag.GENERALIZED_TIME: dataStr = Asn1Util.BytesToString(data); break; case Asn1Tag.UTF8_STRING: UTF8Encoding utf8 = new UTF8Encoding(); dataStr = utf8.GetString(data); break; case Asn1Tag.INTEGER: dataStr = Asn1Util.FormatString(Asn1Util.ToHexString(data), lineLen, 2); break; default: if ((tag & Asn1Tag.TAG_MASK) == 6) // Visible string for certificate { dataStr = Asn1Util.BytesToString(data); } else { dataStr = Asn1Util.FormatString(Asn1Util.ToHexString(data), lineLen, 2); } break; }; } return dataStr; }
/// <summary> /// Get node label string. /// </summary> /// <param name="mask"> /// <code> /// SHOW_OFFSET /// SHOW_DATA /// USE_HEX_OFFSET /// SHOW_TAG_NUMBER /// SHOW_PATH</code> /// </param> /// <returns>string</returns> public string GetLabel(uint mask) { string nodeStr = ""; string dataStr = ""; string offsetStr = ""; if ((mask & TagTextMask.USE_HEX_OFFSET) != 0) { if ((mask & TagTextMask.SHOW_TAG_NUMBER) != 0) offsetStr = String.Format("(0x{0:X2},0x{1:X6},0x{2:X4})", tag, dataOffset, dataLength); else offsetStr = String.Format("(0x{0:X6},0x{1:X4})", dataOffset, dataLength); } else { if ((mask & TagTextMask.SHOW_TAG_NUMBER) != 0) offsetStr = String.Format("({0},{1},{2})", tag, dataOffset, dataLength); else offsetStr = String.Format("({0},{1})", dataOffset, dataLength); } string oid, oidName; switch (tag) { case Asn1Tag.BIT_STRING: if ((mask & TagTextMask.SHOW_OFFSET) != 0) { nodeStr += offsetStr; } nodeStr += " " + TagName + " UnusedBits: " + unusedBits.ToString(); if ((mask & TagTextMask.SHOW_DATA) != 0) { dataStr = Asn1Util.ToHexString(data); nodeStr += ((dataStr.Length>0) ? " : '" + dataStr + "'" : ""); } break; case Asn1Tag.OBJECT_IDENTIFIER: Oid xoid = new Oid(); oid = xoid.Decode(data); oidName = xoid.GetOidName(oid); if ((mask & TagTextMask.SHOW_OFFSET) != 0) { nodeStr += offsetStr; } nodeStr += " " + TagName; nodeStr += " : " + oidName; if ((mask & TagTextMask.SHOW_DATA) != 0) { nodeStr += ((oid.Length>0) ? " : '" + oid + "'" : ""); } break; case Asn1Tag.RELATIVE_OID: RelativeOid roid = new RelativeOid(); oid = roid.Decode(data); oidName = ""; if ((mask & TagTextMask.SHOW_OFFSET) != 0) { nodeStr += offsetStr; } nodeStr += " " + TagName; nodeStr += " : " + oidName; if ((mask & TagTextMask.SHOW_DATA) != 0) { nodeStr += ((oid.Length>0) ? " : '" + 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: if ((mask & TagTextMask.SHOW_OFFSET) != 0) { nodeStr += offsetStr; } nodeStr += " " + TagName; if ((mask & TagTextMask.SHOW_DATA) != 0) { if ( tag == Asn1Tag.UTF8_STRING ) { UTF8Encoding unicode = new UTF8Encoding(); dataStr = unicode.GetString(data); } else { dataStr = Asn1Util.BytesToString(data); } nodeStr += ((dataStr.Length>0) ? " : '" + dataStr + "'" : ""); } break; case Asn1Tag.INTEGER: if ((mask & TagTextMask.SHOW_OFFSET) != 0) { nodeStr += offsetStr; } nodeStr += " " + TagName; if ((mask & TagTextMask.SHOW_DATA) != 0) { if (data != null && dataLength < 8) { dataStr = Asn1Util.BytesToLong(data).ToString(); } else { dataStr = Asn1Util.ToHexString(data); } nodeStr += ((dataStr.Length>0) ? " : '" + dataStr + "'" : ""); } break; default: if ((mask & TagTextMask.SHOW_OFFSET) != 0) { nodeStr += offsetStr; } nodeStr += " " + TagName; if ((mask & TagTextMask.SHOW_DATA) != 0) { if ((tag & Asn1Tag.TAG_MASK) == 6) // Visible string for certificate { dataStr = Asn1Util.BytesToString(data); } else { dataStr = Asn1Util.ToHexString(data); } nodeStr += ((dataStr.Length>0) ? " : '" + dataStr + "'" : ""); } break; }; if ((mask & TagTextMask.SHOW_PATH) != 0) { nodeStr = "(" + path + ") " + nodeStr; } return nodeStr; }
/// <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; }