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)
{
if (obj is Asn1Null) {
buf.Append(indent);
buf.AppendLine("NULL");
} else {
Asn1Sequence asn1Sequence = obj as Asn1Sequence;
if (asn1Sequence != null) {
buf.Append(indent);
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) {
buf.Append(indent);
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) {
buf.Append(indent);
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.Append(indent);
buf.AppendLine("ObjectIdentifier(" + derObjectIdentifier.GetID() + ")");
} else {
Asn1RelativeOid asn1RelativeOid = obj as Asn1RelativeOid;
if (asn1RelativeOid != null) {
buf.Append(indent);
buf.AppendLine("RelativeOID(" + asn1RelativeOid.GetID() + ")");
} else {
DerBoolean derBoolean = obj as DerBoolean;
if (derBoolean != null) {
buf.Append(indent);
buf.AppendLine("Boolean(" + derBoolean.IsTrue.ToString() + ")");
} else {
DerInteger derInteger = obj as DerInteger;
if (derInteger != null) {
buf.Append(indent);
buf.AppendLine("Integer(" + derInteger.Value?.ToString() + ")");
} else {
Asn1OctetString asn1OctetString = obj as Asn1OctetString;
int num;
if (asn1OctetString != null) {
byte[] octets = asn1OctetString.GetOctets();
buf.Append(indent);
if (obj is BerOctetString) {
num = octets.Length;
buf.AppendLine("BER Octet String[" + num.ToString() + "]");
} else {
num = octets.Length;
buf.AppendLine("DER Octet String[" + num.ToString() + "]");
}
if (verbose)
DumpBinaryDataAsString(buf, indent, octets);
} else {
DerBitString derBitString = obj as DerBitString;
if (derBitString != null) {
byte[] bytes = derBitString.GetBytes();
int padBits = derBitString.PadBits;
buf.Append(indent);
if (derBitString is BerBitString) {
string[] obj2 = new string[5] {
"BER Bit String[",
null,
null,
null,
null
};
num = bytes.Length;
obj2[1] = num.ToString();
obj2[2] = ", ";
obj2[3] = padBits.ToString();
obj2[4] = "]";
buf.AppendLine(string.Concat(obj2));
} else if (derBitString is DLBitString) {
string[] obj3 = new string[5] {
"DL Bit String[",
null,
null,
null,
null
};
num = bytes.Length;
obj3[1] = num.ToString();
obj3[2] = ", ";
obj3[3] = padBits.ToString();
obj3[4] = "]";
buf.AppendLine(string.Concat(obj3));
} else {
string[] obj4 = new string[5] {
"DER Bit String[",
null,
null,
null,
null
};
num = bytes.Length;
obj4[1] = num.ToString();
obj4[2] = ", ";
obj4[3] = padBits.ToString();
obj4[4] = "]";
buf.AppendLine(string.Concat(obj4));
}
if (verbose)
DumpBinaryDataAsString(buf, indent, bytes);
} else {
DerIA5String derIA5String = obj as DerIA5String;
if (derIA5String != null) {
buf.Append(indent);
buf.AppendLine("IA5String(" + derIA5String.GetString() + ")");
} else {
DerUtf8String derUtf8String = obj as DerUtf8String;
if (derUtf8String != null) {
buf.Append(indent);
buf.AppendLine("UTF8String(" + derUtf8String.GetString() + ")");
} else {
DerPrintableString derPrintableString = obj as DerPrintableString;
if (derPrintableString != null) {
buf.Append(indent);
buf.AppendLine("PrintableString(" + derPrintableString.GetString() + ")");
} else {
DerVisibleString derVisibleString = obj as DerVisibleString;
if (derVisibleString != null) {
buf.Append(indent);
buf.AppendLine("VisibleString(" + derVisibleString.GetString() + ")");
} else {
DerBmpString derBmpString = obj as DerBmpString;
if (derBmpString != null) {
buf.Append(indent);
buf.AppendLine("BMPString(" + derBmpString.GetString() + ")");
} else {
DerT61String derT61String = obj as DerT61String;
if (derT61String != null) {
buf.Append(indent);
buf.AppendLine("T61String(" + derT61String.GetString() + ")");
} else {
DerGraphicString derGraphicString = obj as DerGraphicString;
if (derGraphicString != null) {
buf.Append(indent);
buf.AppendLine("GraphicString(" + derGraphicString.GetString() + ")");
} else {
DerVideotexString derVideotexString = obj as DerVideotexString;
if (derVideotexString != null) {
buf.Append(indent);
buf.AppendLine("VideotexString(" + derVideotexString.GetString() + ")");
} else {
Asn1UtcTime asn1UtcTime = obj as Asn1UtcTime;
if (asn1UtcTime != null) {
buf.Append(indent);
buf.AppendLine("UTCTime(" + asn1UtcTime.TimeString + ")");
} else {
Asn1GeneralizedTime asn1GeneralizedTime = obj as Asn1GeneralizedTime;
if (asn1GeneralizedTime != null) {
buf.Append(indent);
buf.AppendLine("GeneralizedTime(" + asn1GeneralizedTime.TimeString + ")");
} else {
DerEnumerated derEnumerated = obj as DerEnumerated;
if (derEnumerated != null) {
buf.Append(indent);
buf.AppendLine("DER Enumerated(" + derEnumerated.Value?.ToString() + ")");
} else {
DerExternal derExternal = obj as DerExternal;
if (derExternal != null) {
buf.Append(indent);
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(indent);
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);
}
}
}
}