Initial Commit
This commit is contained in:
250
iTechSharp/srcbc/asn1/util/Asn1Dump.cs
Normal file
250
iTechSharp/srcbc/asn1/util/Asn1Dump.cs
Normal file
@@ -0,0 +1,250 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Text;
|
||||
|
||||
using Org.BouncyCastle.Utilities;
|
||||
using Org.BouncyCastle.Utilities.Encoders;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1.Utilities
|
||||
{
|
||||
public sealed class Asn1Dump
|
||||
{
|
||||
private static readonly string NewLine = Platform.NewLine;
|
||||
|
||||
private Asn1Dump()
|
||||
{
|
||||
}
|
||||
|
||||
private const string Tab = " ";
|
||||
|
||||
/**
|
||||
* dump a Der object as a formatted string with indentation
|
||||
*
|
||||
* @param obj the Asn1Object to be dumped out.
|
||||
*/
|
||||
private static string AsString(
|
||||
string indent,
|
||||
Asn1Object obj)
|
||||
{
|
||||
if (obj is Asn1Sequence)
|
||||
{
|
||||
StringBuilder buf = new StringBuilder(indent);
|
||||
|
||||
string tab = indent + Tab;
|
||||
|
||||
if (obj is BerSequence)
|
||||
{
|
||||
buf.Append("BER Sequence");
|
||||
}
|
||||
else if (obj is DerSequence)
|
||||
{
|
||||
buf.Append("DER Sequence");
|
||||
}
|
||||
else
|
||||
{
|
||||
buf.Append("Sequence");
|
||||
}
|
||||
|
||||
buf.Append(NewLine);
|
||||
|
||||
foreach (Asn1Encodable o in ((Asn1Sequence)obj))
|
||||
{
|
||||
if (o == null || o is Asn1Null)
|
||||
{
|
||||
buf.Append(tab);
|
||||
buf.Append("NULL");
|
||||
buf.Append(NewLine);
|
||||
}
|
||||
else
|
||||
{
|
||||
buf.Append(AsString(tab, o.ToAsn1Object()));
|
||||
}
|
||||
}
|
||||
return buf.ToString();
|
||||
}
|
||||
else if (obj is DerTaggedObject)
|
||||
{
|
||||
StringBuilder buf = new StringBuilder();
|
||||
string tab = indent + Tab;
|
||||
|
||||
buf.Append(indent);
|
||||
if (obj is BerTaggedObject)
|
||||
{
|
||||
buf.Append("BER Tagged [");
|
||||
}
|
||||
else
|
||||
{
|
||||
buf.Append("Tagged [");
|
||||
}
|
||||
|
||||
DerTaggedObject o = (DerTaggedObject)obj;
|
||||
|
||||
buf.Append(((int)o.TagNo).ToString());
|
||||
buf.Append(']');
|
||||
|
||||
if (!o.IsExplicit())
|
||||
{
|
||||
buf.Append(" IMPLICIT ");
|
||||
}
|
||||
|
||||
buf.Append(NewLine);
|
||||
|
||||
if (o.IsEmpty())
|
||||
{
|
||||
buf.Append(tab);
|
||||
buf.Append("EMPTY");
|
||||
buf.Append(NewLine);
|
||||
}
|
||||
else
|
||||
{
|
||||
buf.Append(AsString(tab, o.GetObject()));
|
||||
}
|
||||
|
||||
return buf.ToString();
|
||||
}
|
||||
else if (obj is BerSet)
|
||||
{
|
||||
StringBuilder buf = new StringBuilder();
|
||||
string tab = indent + Tab;
|
||||
|
||||
buf.Append(indent);
|
||||
buf.Append("BER Set");
|
||||
buf.Append(NewLine);
|
||||
|
||||
foreach (Asn1Encodable o in ((Asn1Set)obj))
|
||||
{
|
||||
if (o == null)
|
||||
{
|
||||
buf.Append(tab);
|
||||
buf.Append("NULL");
|
||||
buf.Append(NewLine);
|
||||
}
|
||||
else
|
||||
{
|
||||
buf.Append(AsString(tab, o.ToAsn1Object()));
|
||||
}
|
||||
}
|
||||
|
||||
return buf.ToString();
|
||||
}
|
||||
else if (obj is DerSet)
|
||||
{
|
||||
StringBuilder buf = new StringBuilder();
|
||||
string tab = indent + Tab;
|
||||
|
||||
buf.Append(indent);
|
||||
buf.Append("DER Set");
|
||||
buf.Append(NewLine);
|
||||
|
||||
foreach (Asn1Encodable o in ((Asn1Set)obj))
|
||||
{
|
||||
if (o == null)
|
||||
{
|
||||
buf.Append(tab);
|
||||
buf.Append("NULL");
|
||||
buf.Append(NewLine);
|
||||
}
|
||||
else
|
||||
{
|
||||
buf.Append(AsString(tab, o.ToAsn1Object()));
|
||||
}
|
||||
}
|
||||
|
||||
return buf.ToString();
|
||||
}
|
||||
else if (obj is DerObjectIdentifier)
|
||||
{
|
||||
return indent + "ObjectIdentifier(" + ((DerObjectIdentifier)obj).Id + ")" + NewLine;
|
||||
}
|
||||
else if (obj is DerBoolean)
|
||||
{
|
||||
return indent + "Boolean(" + ((DerBoolean)obj).IsTrue + ")" + NewLine;
|
||||
}
|
||||
else if (obj is DerInteger)
|
||||
{
|
||||
return indent + "Integer(" + ((DerInteger)obj).Value + ")" + NewLine;
|
||||
}
|
||||
else if (obj is BerOctetString)
|
||||
{
|
||||
return indent + "BER Octet String" + "[" + ((Asn1OctetString)obj).GetOctets().Length + "] " + NewLine;
|
||||
}
|
||||
else if (obj is DerOctetString)
|
||||
{
|
||||
return indent + "DER Octet String" + "[" + ((Asn1OctetString)obj).GetOctets().Length + "] " + NewLine;
|
||||
}
|
||||
else if (obj is DerBitString)
|
||||
{
|
||||
return indent + "DER Bit String" + "[" + ((DerBitString)obj).GetBytes().Length + ", " + ((DerBitString)obj).PadBits + "] " + NewLine;
|
||||
}
|
||||
else if (obj is DerIA5String)
|
||||
{
|
||||
return indent + "IA5String(" + ((DerIA5String)obj).GetString() + ") " + NewLine;
|
||||
}
|
||||
else if (obj is DerUtf8String)
|
||||
{
|
||||
return indent + "UTF8String(" + ((DerUtf8String)obj).GetString() + ") " + NewLine;
|
||||
}
|
||||
else if (obj is DerPrintableString)
|
||||
{
|
||||
return indent + "PrintableString(" + ((DerPrintableString)obj).GetString() + ") " + NewLine;
|
||||
}
|
||||
else if (obj is DerVisibleString)
|
||||
{
|
||||
return indent + "VisibleString(" + ((DerVisibleString)obj).GetString() + ") " + NewLine;
|
||||
}
|
||||
else if (obj is DerBmpString)
|
||||
{
|
||||
return indent + "BMPString(" + ((DerBmpString)obj).GetString() + ") " + NewLine;
|
||||
}
|
||||
else if (obj is DerT61String)
|
||||
{
|
||||
return indent + "T61String(" + ((DerT61String)obj).GetString() + ") " + NewLine;
|
||||
}
|
||||
else if (obj is DerUtcTime)
|
||||
{
|
||||
return indent + "UTCTime(" + ((DerUtcTime)obj).TimeString + ") " + NewLine;
|
||||
}
|
||||
else if (obj is DerGeneralizedTime)
|
||||
{
|
||||
return indent + "GeneralizedTime(" + ((DerGeneralizedTime)obj).GetTime() + ") " + NewLine;
|
||||
}
|
||||
else if (obj is DerUnknownTag)
|
||||
{
|
||||
byte[] hex = Hex.Encode(((DerUnknownTag)obj).GetData());
|
||||
return indent + "Unknown " + ((int)((DerUnknownTag)obj).Tag).ToString("X") + " "
|
||||
+ Encoding.ASCII.GetString(hex, 0, hex.Length) + NewLine;
|
||||
}
|
||||
else
|
||||
{
|
||||
return indent + obj.ToString() + NewLine;
|
||||
}
|
||||
}
|
||||
|
||||
[Obsolete("Use version accepting Asn1Encodable")]
|
||||
public static string DumpAsString(
|
||||
object obj)
|
||||
{
|
||||
if (obj is Asn1Object)
|
||||
{
|
||||
return AsString("", (Asn1Object)obj);
|
||||
}
|
||||
else if (obj is Asn1Encodable)
|
||||
{
|
||||
return AsString("", ((Asn1Encodable)obj).ToAsn1Object());
|
||||
}
|
||||
|
||||
return "unknown object type " + obj.ToString();
|
||||
}
|
||||
|
||||
/**
|
||||
* dump out a DER object as a formatted string
|
||||
*
|
||||
* @param obj the Asn1Encodable to be dumped out.
|
||||
*/
|
||||
public static string DumpAsString(
|
||||
Asn1Encodable obj)
|
||||
{
|
||||
return AsString("", obj.ToAsn1Object());
|
||||
}
|
||||
}
|
||||
}
|
28
iTechSharp/srcbc/asn1/util/Dump.cs
Normal file
28
iTechSharp/srcbc/asn1/util/Dump.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
using Org.BouncyCastle.Asn1;
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1.Utilities
|
||||
{
|
||||
public sealed class Dump
|
||||
{
|
||||
private Dump()
|
||||
{
|
||||
}
|
||||
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
FileStream fIn = File.OpenRead(args[0]);
|
||||
Asn1InputStream bIn = new Asn1InputStream(fIn);
|
||||
|
||||
Asn1Object obj;
|
||||
while ((obj = bIn.ReadObject()) != null)
|
||||
{
|
||||
Console.WriteLine(Asn1Dump.DumpAsString(obj));
|
||||
}
|
||||
|
||||
bIn.Close();
|
||||
}
|
||||
}
|
||||
}
|
67
iTechSharp/srcbc/asn1/util/FilterStream.cs
Normal file
67
iTechSharp/srcbc/asn1/util/FilterStream.cs
Normal file
@@ -0,0 +1,67 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1.Utilities
|
||||
{
|
||||
public class FilterStream : Stream
|
||||
{
|
||||
public FilterStream(Stream s)
|
||||
{
|
||||
this.s = s;
|
||||
}
|
||||
public override bool CanRead
|
||||
{
|
||||
get { return s.CanRead; }
|
||||
}
|
||||
public override bool CanSeek
|
||||
{
|
||||
get { return s.CanSeek; }
|
||||
}
|
||||
public override bool CanWrite
|
||||
{
|
||||
get { return s.CanWrite; }
|
||||
}
|
||||
public override long Length
|
||||
{
|
||||
get { return s.Length; }
|
||||
}
|
||||
public override long Position
|
||||
{
|
||||
get { return s.Position; }
|
||||
set { s.Position = value; }
|
||||
}
|
||||
public override void Close()
|
||||
{
|
||||
s.Close();
|
||||
}
|
||||
public override void Flush()
|
||||
{
|
||||
s.Flush();
|
||||
}
|
||||
public override long Seek(long offset, SeekOrigin origin)
|
||||
{
|
||||
return s.Seek(offset, origin);
|
||||
}
|
||||
public override void SetLength(long value)
|
||||
{
|
||||
s.SetLength(value);
|
||||
}
|
||||
public override int Read(byte[] buffer, int offset, int count)
|
||||
{
|
||||
return s.Read(buffer, offset, count);
|
||||
}
|
||||
public override int ReadByte()
|
||||
{
|
||||
return s.ReadByte();
|
||||
}
|
||||
public override void Write(byte[] buffer, int offset, int count)
|
||||
{
|
||||
s.Write(buffer, offset, count);
|
||||
}
|
||||
public override void WriteByte(byte value)
|
||||
{
|
||||
s.WriteByte(value);
|
||||
}
|
||||
private readonly Stream s;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user