Skip to content

Commit

Permalink
With BC1.8
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasDorier committed Nov 6, 2014
1 parent ec11b89 commit ca5242a
Show file tree
Hide file tree
Showing 1,275 changed files with 192,527 additions and 19,445 deletions.
1,313 changes: 1,313 additions & 0 deletions NBitcoin.BouncyCastle/NBitcoin.BouncyCastle.csproj

Large diffs are not rendered by default.

36 changes: 36 additions & 0 deletions NBitcoin.BouncyCastle/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("NBitcoin.BouncyCastle")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Microsoft")]
[assembly: AssemblyProduct("NBitcoin.BouncyCastle")]
[assembly: AssemblyCopyright("Copyright © Microsoft 2014")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("46b9a006-4b65-45d4-be01-7400aa07e85b")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
27 changes: 27 additions & 0 deletions NBitcoin.BouncyCastle/asn1/ASN1Generator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System.Collections;
using System.IO;

namespace Org.BouncyCastle.Asn1
{
public abstract class Asn1Generator
{
private Stream _out;

protected Asn1Generator(
Stream outStream)
{
_out = outStream;
}

protected Stream Out
{
get { return _out; }
}

public abstract void AddObject(Asn1Encodable obj);

public abstract Stream GetRawOutputStream();

public abstract void Close();
}
}
10 changes: 10 additions & 0 deletions NBitcoin.BouncyCastle/asn1/ASN1OctetStringParser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.IO;

namespace Org.BouncyCastle.Asn1
{
public interface Asn1OctetStringParser
: IAsn1Convertible
{
Stream GetOctetStream();
}
}
8 changes: 8 additions & 0 deletions NBitcoin.BouncyCastle/asn1/ASN1SequenceParser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Org.BouncyCastle.Asn1
{
public interface Asn1SequenceParser
: IAsn1Convertible
{
IAsn1Convertible ReadObject();
}
}
8 changes: 8 additions & 0 deletions NBitcoin.BouncyCastle/asn1/ASN1SetParser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Org.BouncyCastle.Asn1
{
public interface Asn1SetParser
: IAsn1Convertible
{
IAsn1Convertible ReadObject();
}
}
234 changes: 234 additions & 0 deletions NBitcoin.BouncyCastle/asn1/ASN1StreamParser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,234 @@
using System;
using System.IO;

namespace Org.BouncyCastle.Asn1
{
public class Asn1StreamParser
{
private readonly Stream _in;
private readonly int _limit;

private readonly byte[][] tmpBuffers;

public Asn1StreamParser(
Stream inStream)
: this(inStream, Asn1InputStream.FindLimit(inStream))
{
}

public Asn1StreamParser(
Stream inStream,
int limit)
{
if (!inStream.CanRead)
throw new ArgumentException("Expected stream to be readable", "inStream");

this._in = inStream;
this._limit = limit;
this.tmpBuffers = new byte[16][];
}

public Asn1StreamParser(
byte[] encoding)
: this(new MemoryStream(encoding, false), encoding.Length)
{
}

internal IAsn1Convertible ReadIndef(int tagValue)
{
// Note: INDEF => CONSTRUCTED

// TODO There are other tags that may be constructed (e.g. BIT_STRING)
switch (tagValue)
{
case Asn1Tags.External:
return new DerExternalParser(this);
case Asn1Tags.OctetString:
return new BerOctetStringParser(this);
case Asn1Tags.Sequence:
return new BerSequenceParser(this);
case Asn1Tags.Set:
return new BerSetParser(this);
default:
throw new Asn1Exception("unknown BER object encountered: 0x" + tagValue.ToString("X"));
}
}

internal IAsn1Convertible ReadImplicit(bool constructed, int tag)
{
if (_in is IndefiniteLengthInputStream)
{
if (!constructed)
throw new IOException("indefinite length primitive encoding encountered");

return ReadIndef(tag);
}

if (constructed)
{
switch (tag)
{
case Asn1Tags.Set:
return new DerSetParser(this);
case Asn1Tags.Sequence:
return new DerSequenceParser(this);
case Asn1Tags.OctetString:
return new BerOctetStringParser(this);
}
}
else
{
switch (tag)
{
case Asn1Tags.Set:
throw new Asn1Exception("sequences must use constructed encoding (see X.690 8.9.1/8.10.1)");
case Asn1Tags.Sequence:
throw new Asn1Exception("sets must use constructed encoding (see X.690 8.11.1/8.12.1)");
case Asn1Tags.OctetString:
return new DerOctetStringParser((DefiniteLengthInputStream)_in);
}
}

throw new Asn1Exception("implicit tagging not implemented");
}

internal Asn1Object ReadTaggedObject(bool constructed, int tag)
{
if (!constructed)
{
// Note: !CONSTRUCTED => IMPLICIT
DefiniteLengthInputStream defIn = (DefiniteLengthInputStream)_in;
return new DerTaggedObject(false, tag, new DerOctetString(defIn.ToArray()));
}

Asn1EncodableVector v = ReadVector();

if (_in is IndefiniteLengthInputStream)
{
return v.Count == 1
? new BerTaggedObject(true, tag, v[0])
: new BerTaggedObject(false, tag, BerSequence.FromVector(v));
}

return v.Count == 1
? new DerTaggedObject(true, tag, v[0])
: new DerTaggedObject(false, tag, DerSequence.FromVector(v));
}

public virtual IAsn1Convertible ReadObject()
{
int tag = _in.ReadByte();
if (tag == -1)
return null;

// turn of looking for "00" while we resolve the tag
Set00Check(false);

//
// calculate tag number
//
int tagNo = Asn1InputStream.ReadTagNumber(_in, tag);

bool isConstructed = (tag & Asn1Tags.Constructed) != 0;

//
// calculate length
//
int length = Asn1InputStream.ReadLength(_in, _limit);

if (length < 0) // indefinite length method
{
if (!isConstructed)
throw new IOException("indefinite length primitive encoding encountered");

IndefiniteLengthInputStream indIn = new IndefiniteLengthInputStream(_in, _limit);
Asn1StreamParser sp = new Asn1StreamParser(indIn, _limit);

if ((tag & Asn1Tags.Application) != 0)
{
return new BerApplicationSpecificParser(tagNo, sp);
}

if ((tag & Asn1Tags.Tagged) != 0)
{
return new BerTaggedObjectParser(true, tagNo, sp);
}

return sp.ReadIndef(tagNo);
}
else
{
DefiniteLengthInputStream defIn = new DefiniteLengthInputStream(_in, length);

if ((tag & Asn1Tags.Application) != 0)
{
return new DerApplicationSpecific(isConstructed, tagNo, defIn.ToArray());
}

if ((tag & Asn1Tags.Tagged) != 0)
{
return new BerTaggedObjectParser(isConstructed, tagNo, new Asn1StreamParser(defIn));
}

if (isConstructed)
{
// TODO There are other tags that may be constructed (e.g. BitString)
switch (tagNo)
{
case Asn1Tags.OctetString:
//
// yes, people actually do this...
//
return new BerOctetStringParser(new Asn1StreamParser(defIn));
case Asn1Tags.Sequence:
return new DerSequenceParser(new Asn1StreamParser(defIn));
case Asn1Tags.Set:
return new DerSetParser(new Asn1StreamParser(defIn));
case Asn1Tags.External:
return new DerExternalParser(new Asn1StreamParser(defIn));
default:
throw new IOException("unknown tag " + tagNo + " encountered");
}
}

// Some primitive encodings can be handled by parsers too...
switch (tagNo)
{
case Asn1Tags.OctetString:
return new DerOctetStringParser(defIn);
}

try
{
return Asn1InputStream.CreatePrimitiveDerObject(tagNo, defIn, tmpBuffers);
}
catch (ArgumentException e)
{
throw new Asn1Exception("corrupted stream detected", e);
}
}
}

private void Set00Check(
bool enabled)
{
if (_in is IndefiniteLengthInputStream)
{
((IndefiniteLengthInputStream) _in).SetEofOn00(enabled);
}
}

internal Asn1EncodableVector ReadVector()
{
Asn1EncodableVector v = new Asn1EncodableVector();

IAsn1Convertible obj;
while ((obj = ReadObject()) != null)
{
v.Add(obj.ToAsn1Object());
}

return v;
}
}
}
10 changes: 10 additions & 0 deletions NBitcoin.BouncyCastle/asn1/ASN1TaggedObjectParser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Org.BouncyCastle.Asn1
{
public interface Asn1TaggedObjectParser
: IAsn1Convertible
{
int TagNo { get; }

IAsn1Convertible GetObjectParser(int tag, bool isExplicit);
}
}

0 comments on commit ca5242a

Please sign in to comment.