<PackageReference Include="BouncyCastle.Cryptography" Version="2.6.2" />

Asn1Dump

public static class Asn1Dump
using Org.BouncyCastle.Utilities.Encoders; using System; using System.IO; using System.Text; namespace Org.BouncyCastle.Asn1.Utilities { public static class Asn1Dump { private const string Tab = " "; private const int SampleSize = 32; private static void AsString(string indent, bool verbose, Asn1Object obj, StringBuilder buf) { buf.Append(indent); if (obj is Asn1Null) buf.AppendLine("NULL"); else { Asn1Sequence asn1Sequence = obj as Asn1Sequence; if (asn1Sequence != null) { if (asn1Sequence is BerSequence) buf.AppendLine("BER Sequence"); else if (!(asn1Sequence is DLSequence)) { buf.AppendLine("DER Sequence"); } else { buf.AppendLine("Sequence"); } string indent2 = indent + " "; int i = 0; for (int count = asn1Sequence.Count; i < count; i++) { AsString(indent2, verbose, asn1Sequence[i].ToAsn1Object(), buf); } } else { Asn1Set asn1Set = obj as Asn1Set; if (asn1Set != null) { if (asn1Set is BerSet) buf.AppendLine("BER Set"); else if (!(asn1Set is DLSet)) { buf.AppendLine("DER Set"); } else { buf.AppendLine("Set"); } string indent3 = indent + " "; int j = 0; for (int count2 = asn1Set.Count; j < count2; j++) { AsString(indent3, verbose, asn1Set[j].ToAsn1Object(), buf); } } else { Asn1TaggedObject asn1TaggedObject = obj as Asn1TaggedObject; if (asn1TaggedObject != null) { if (asn1TaggedObject is BerTaggedObject) buf.Append("BER Tagged "); else if (!(asn1TaggedObject is DLTaggedObject)) { buf.Append("DER Tagged "); } else { buf.Append("Tagged "); } buf.Append(Asn1Utilities.GetTagText(asn1TaggedObject)); if (!asn1TaggedObject.IsExplicit()) buf.Append(" IMPLICIT"); buf.AppendLine(); AsString(indent + " ", verbose, asn1TaggedObject.GetBaseObject().ToAsn1Object(), buf); } else { DerObjectIdentifier derObjectIdentifier = obj as DerObjectIdentifier; if (derObjectIdentifier != null) buf.AppendLine("ObjectIdentifier(" + derObjectIdentifier.GetID() + ")"); else { Asn1RelativeOid asn1RelativeOid = obj as Asn1RelativeOid; if (asn1RelativeOid != null) buf.AppendLine("RelativeOID(" + asn1RelativeOid.GetID() + ")"); else { DerBoolean derBoolean = obj as DerBoolean; if (derBoolean != null) buf.AppendLine("Boolean(" + derBoolean.IsTrue.ToString() + ")"); else { DerInteger derInteger = obj as DerInteger; if (derInteger != null) buf.AppendLine("Integer(" + derInteger.Value?.ToString() + ")"); else { Asn1OctetString asn1OctetString = obj as Asn1OctetString; int num; if (asn1OctetString != null) { if (obj is BerOctetString) buf.Append("BER Octet String["); else buf.Append("DER Octet String["); num = asn1OctetString.GetOctetsLength(); buf.AppendLine(num.ToString() + "]"); if (verbose) DumpBinaryDataAsString(buf, indent, asn1OctetString.GetOctets()); } else { DerBitString derBitString = obj as DerBitString; if (derBitString != null) { if (derBitString is BerBitString) buf.Append("BER Bit String["); else if (derBitString is DLBitString) { buf.Append("DL Bit String["); } else { buf.Append("DER Bit String["); } num = derBitString.GetBytesLength(); string str = num.ToString(); num = derBitString.PadBits; buf.AppendLine(str + ", " + num.ToString() + "]"); if (verbose) DumpBinaryDataAsString(buf, indent, derBitString.GetBytes()); } else { DerIA5String derIA5String = obj as DerIA5String; if (derIA5String != null) buf.AppendLine("IA5String(" + derIA5String.GetString() + ")"); else { DerUtf8String derUtf8String = obj as DerUtf8String; if (derUtf8String != null) buf.AppendLine("UTF8String(" + derUtf8String.GetString() + ")"); else { DerPrintableString derPrintableString = obj as DerPrintableString; if (derPrintableString != null) buf.AppendLine("PrintableString(" + derPrintableString.GetString() + ")"); else { DerVisibleString derVisibleString = obj as DerVisibleString; if (derVisibleString != null) buf.AppendLine("VisibleString(" + derVisibleString.GetString() + ")"); else { DerBmpString derBmpString = obj as DerBmpString; if (derBmpString != null) buf.AppendLine("BMPString(" + derBmpString.GetString() + ")"); else { DerT61String derT61String = obj as DerT61String; if (derT61String != null) buf.AppendLine("T61String(" + derT61String.GetString() + ")"); else { DerGraphicString derGraphicString = obj as DerGraphicString; if (derGraphicString != null) buf.AppendLine("GraphicString(" + derGraphicString.GetString() + ")"); else { DerVideotexString derVideotexString = obj as DerVideotexString; if (derVideotexString != null) buf.AppendLine("VideotexString(" + derVideotexString.GetString() + ")"); else { Asn1UtcTime asn1UtcTime = obj as Asn1UtcTime; if (asn1UtcTime != null) buf.AppendLine("UTCTime(" + asn1UtcTime.TimeString + ")"); else { Asn1GeneralizedTime asn1GeneralizedTime = obj as Asn1GeneralizedTime; if (asn1GeneralizedTime != null) buf.AppendLine("GeneralizedTime(" + asn1GeneralizedTime.TimeString + ")"); else { DerEnumerated derEnumerated = obj as DerEnumerated; if (derEnumerated != null) buf.AppendLine("DER Enumerated(" + derEnumerated.Value?.ToString() + ")"); else { DerExternal derExternal = obj as DerExternal; if (derExternal != null) { buf.AppendLine("External "); string text = indent + " "; if (derExternal.DirectReference != null) { buf.Append(text); buf.AppendLine("Direct Reference: " + derExternal.DirectReference.GetID()); } if (derExternal.IndirectReference != null) { buf.Append(text); buf.AppendLine("Indirect Reference: " + derExternal.IndirectReference.ToString()); } if (derExternal.DataValueDescriptor != null) AsString(text, verbose, derExternal.DataValueDescriptor, buf); buf.Append(text); num = derExternal.Encoding; buf.AppendLine("Encoding: " + num.ToString()); AsString(text, verbose, derExternal.ExternalContent, buf); } else { buf.Append(obj); buf.AppendLine(); } } } } } } } } } } } } } } } } } } } } } } } public static void Dump(Stream input, TextWriter output) { using (Asn1InputStream asn1InputStream = new Asn1InputStream(input, 2147483647, true)) { Asn1Object obj; while ((obj = asn1InputStream.ReadObject()) != null) { output.Write(DumpAsString(obj)); } } } public static string DumpAsString(Asn1Encodable obj) { return DumpAsString(obj, false); } public static string DumpAsString(Asn1Encodable obj, bool verbose) { StringBuilder stringBuilder = new StringBuilder(); AsString("", verbose, obj.ToAsn1Object(), stringBuilder); return stringBuilder.ToString(); } private static void DumpBinaryDataAsString(StringBuilder buf, string indent, byte[] bytes) { if (bytes.Length >= 1) { indent += " "; for (int i = 0; i < bytes.Length; i += 32) { int num = System.Math.Min(bytes.Length - i, 32); buf.Append(indent); buf.Append(Hex.ToHexString(bytes, i, num)); for (int j = num; j < 32; j++) { buf.Append(" "); } buf.Append(" "); AppendAscString(buf, bytes, i, num); buf.AppendLine(); } } } private static void AppendAscString(StringBuilder buf, byte[] bytes, int off, int len) { for (int i = off; i != off + len; i++) { char c = (char)bytes[i]; if (c >= ' ' && c <= '~') buf.Append(c); } } } }