init commit
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
using System.Collections;
|
||||
using Org.BouncyCastle.Asn1.X9;
|
||||
using Org.BouncyCastle.Math;
|
||||
using Org.BouncyCastle.Math.EC;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
using Org.BouncyCastle.Utilities.Collections;
|
||||
using Org.BouncyCastle.Utilities.Encoders;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1.Anssi;
|
||||
|
||||
public class AnssiNamedCurves
|
||||
{
|
||||
internal class Frp256v1Holder : X9ECParametersHolder
|
||||
{
|
||||
internal static readonly X9ECParametersHolder Instance = new Frp256v1Holder();
|
||||
|
||||
private Frp256v1Holder()
|
||||
{
|
||||
}
|
||||
|
||||
protected override X9ECParameters CreateParameters()
|
||||
{
|
||||
BigInteger q = FromHex("F1FD178C0B3AD58F10126DE8CE42435B3961ADBCABC8CA6DE8FCF353D86E9C03");
|
||||
BigInteger a = FromHex("F1FD178C0B3AD58F10126DE8CE42435B3961ADBCABC8CA6DE8FCF353D86E9C00");
|
||||
BigInteger b = FromHex("EE353FCA5428A9300D4ABA754A44C00FDFEC0C9AE4B1A1803075ED967B7BB73F");
|
||||
byte[] seed = null;
|
||||
BigInteger bigInteger = FromHex("F1FD178C0B3AD58F10126DE8CE42435B53DC67E140D2BF941FFDD459C6D655E1");
|
||||
BigInteger one = BigInteger.One;
|
||||
ECCurve eCCurve = ConfigureCurve(new FpCurve(q, a, b, bigInteger, one));
|
||||
X9ECPoint g = new X9ECPoint(eCCurve, Hex.Decode("04B6B3D4C356C139EB31183D4749D423958C27D2DCAF98B70164C97A2DD98F5CFF6142E0F7C8B204911F9271F0F3ECEF8C2701C307E8E4C9E183115A1554062CFB"));
|
||||
return new X9ECParameters(eCCurve, g, bigInteger, one, seed);
|
||||
}
|
||||
}
|
||||
|
||||
private static readonly IDictionary objIds;
|
||||
|
||||
private static readonly IDictionary curves;
|
||||
|
||||
private static readonly IDictionary names;
|
||||
|
||||
public static IEnumerable Names => new EnumerableProxy(names.Values);
|
||||
|
||||
private static ECCurve ConfigureCurve(ECCurve curve)
|
||||
{
|
||||
return curve;
|
||||
}
|
||||
|
||||
private static BigInteger FromHex(string hex)
|
||||
{
|
||||
return new BigInteger(1, Hex.Decode(hex));
|
||||
}
|
||||
|
||||
private static void DefineCurve(string name, DerObjectIdentifier oid, X9ECParametersHolder holder)
|
||||
{
|
||||
objIds.Add(Platform.ToUpperInvariant(name), oid);
|
||||
names.Add(oid, name);
|
||||
curves.Add(oid, holder);
|
||||
}
|
||||
|
||||
static AnssiNamedCurves()
|
||||
{
|
||||
objIds = Platform.CreateHashtable();
|
||||
curves = Platform.CreateHashtable();
|
||||
names = Platform.CreateHashtable();
|
||||
DefineCurve("FRP256v1", AnssiObjectIdentifiers.FRP256v1, Frp256v1Holder.Instance);
|
||||
}
|
||||
|
||||
public static X9ECParameters GetByName(string name)
|
||||
{
|
||||
DerObjectIdentifier oid = GetOid(name);
|
||||
if (oid != null)
|
||||
{
|
||||
return GetByOid(oid);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static X9ECParameters GetByOid(DerObjectIdentifier oid)
|
||||
{
|
||||
return ((X9ECParametersHolder)curves[oid])?.Parameters;
|
||||
}
|
||||
|
||||
public static DerObjectIdentifier GetOid(string name)
|
||||
{
|
||||
return (DerObjectIdentifier)objIds[Platform.ToUpperInvariant(name)];
|
||||
}
|
||||
|
||||
public static string GetName(DerObjectIdentifier oid)
|
||||
{
|
||||
return (string)names[oid];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
namespace Org.BouncyCastle.Asn1.Anssi;
|
||||
|
||||
public sealed class AnssiObjectIdentifiers
|
||||
{
|
||||
public static readonly DerObjectIdentifier FRP256v1 = new DerObjectIdentifier("1.2.250.1.223.101.256.1");
|
||||
|
||||
private AnssiObjectIdentifiers()
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
using System.IO;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1;
|
||||
|
||||
public abstract class Asn1Encodable : IAsn1Convertible
|
||||
{
|
||||
public const string Der = "DER";
|
||||
|
||||
public const string Ber = "BER";
|
||||
|
||||
public byte[] GetEncoded()
|
||||
{
|
||||
MemoryStream memoryStream = new MemoryStream();
|
||||
Asn1OutputStream asn1OutputStream = new Asn1OutputStream(memoryStream);
|
||||
asn1OutputStream.WriteObject(this);
|
||||
return memoryStream.ToArray();
|
||||
}
|
||||
|
||||
public byte[] GetEncoded(string encoding)
|
||||
{
|
||||
if (encoding.Equals("DER"))
|
||||
{
|
||||
MemoryStream memoryStream = new MemoryStream();
|
||||
DerOutputStream derOutputStream = new DerOutputStream(memoryStream);
|
||||
derOutputStream.WriteObject(this);
|
||||
return memoryStream.ToArray();
|
||||
}
|
||||
return GetEncoded();
|
||||
}
|
||||
|
||||
public byte[] GetDerEncoded()
|
||||
{
|
||||
try
|
||||
{
|
||||
return GetEncoded("DER");
|
||||
}
|
||||
catch (IOException)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public sealed override int GetHashCode()
|
||||
{
|
||||
return ToAsn1Object().CallAsn1GetHashCode();
|
||||
}
|
||||
|
||||
public sealed override bool Equals(object obj)
|
||||
{
|
||||
if (obj == this)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
if (!(obj is IAsn1Convertible asn1Convertible))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
Asn1Object asn1Object = ToAsn1Object();
|
||||
Asn1Object asn1Object2 = asn1Convertible.ToAsn1Object();
|
||||
if (asn1Object != asn1Object2)
|
||||
{
|
||||
return asn1Object.CallAsn1Equals(asn1Object2);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public abstract Asn1Object ToAsn1Object();
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1;
|
||||
|
||||
public class Asn1EncodableVector : IEnumerable
|
||||
{
|
||||
private IList v = Platform.CreateArrayList();
|
||||
|
||||
public Asn1Encodable this[int index] => (Asn1Encodable)v[index];
|
||||
|
||||
[Obsolete("Use 'Count' property instead")]
|
||||
public int Size => v.Count;
|
||||
|
||||
public int Count => v.Count;
|
||||
|
||||
public static Asn1EncodableVector FromEnumerable(IEnumerable e)
|
||||
{
|
||||
Asn1EncodableVector asn1EncodableVector = new Asn1EncodableVector();
|
||||
foreach (Asn1Encodable item in e)
|
||||
{
|
||||
asn1EncodableVector.Add(item);
|
||||
}
|
||||
return asn1EncodableVector;
|
||||
}
|
||||
|
||||
public Asn1EncodableVector(params Asn1Encodable[] v)
|
||||
{
|
||||
Add(v);
|
||||
}
|
||||
|
||||
public void Add(params Asn1Encodable[] objs)
|
||||
{
|
||||
foreach (Asn1Encodable value in objs)
|
||||
{
|
||||
v.Add(value);
|
||||
}
|
||||
}
|
||||
|
||||
public void AddOptional(params Asn1Encodable[] objs)
|
||||
{
|
||||
if (objs == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
foreach (Asn1Encodable asn1Encodable in objs)
|
||||
{
|
||||
if (asn1Encodable != null)
|
||||
{
|
||||
v.Add(asn1Encodable);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Obsolete("Use 'object[index]' syntax instead")]
|
||||
public Asn1Encodable Get(int index)
|
||||
{
|
||||
return this[index];
|
||||
}
|
||||
|
||||
public IEnumerator GetEnumerator()
|
||||
{
|
||||
return v.GetEnumerator();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1;
|
||||
|
||||
[Serializable]
|
||||
public class Asn1Exception : IOException
|
||||
{
|
||||
public Asn1Exception()
|
||||
{
|
||||
}
|
||||
|
||||
public Asn1Exception(string message)
|
||||
: base(message)
|
||||
{
|
||||
}
|
||||
|
||||
public Asn1Exception(string message, Exception exception)
|
||||
: base(message, exception)
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
using System.IO;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1;
|
||||
|
||||
public abstract class Asn1Generator
|
||||
{
|
||||
private Stream _out;
|
||||
|
||||
protected Stream Out => _out;
|
||||
|
||||
protected Asn1Generator(Stream outStream)
|
||||
{
|
||||
_out = outStream;
|
||||
}
|
||||
|
||||
public abstract void AddObject(Asn1Encodable obj);
|
||||
|
||||
public abstract Stream GetRawOutputStream();
|
||||
|
||||
public abstract void Close();
|
||||
}
|
||||
@@ -0,0 +1,264 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using Org.BouncyCastle.Utilities.IO;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1;
|
||||
|
||||
public class Asn1InputStream : FilterStream
|
||||
{
|
||||
private readonly int limit;
|
||||
|
||||
private readonly byte[][] tmpBuffers;
|
||||
|
||||
internal static int FindLimit(Stream input)
|
||||
{
|
||||
if (input is LimitedInputStream)
|
||||
{
|
||||
return ((LimitedInputStream)input).GetRemaining();
|
||||
}
|
||||
if (input is MemoryStream)
|
||||
{
|
||||
MemoryStream memoryStream = (MemoryStream)input;
|
||||
return (int)(memoryStream.Length - memoryStream.Position);
|
||||
}
|
||||
return int.MaxValue;
|
||||
}
|
||||
|
||||
public Asn1InputStream(Stream inputStream)
|
||||
: this(inputStream, FindLimit(inputStream))
|
||||
{
|
||||
}
|
||||
|
||||
public Asn1InputStream(Stream inputStream, int limit)
|
||||
: base(inputStream)
|
||||
{
|
||||
this.limit = limit;
|
||||
tmpBuffers = new byte[16][];
|
||||
}
|
||||
|
||||
public Asn1InputStream(byte[] input)
|
||||
: this(new MemoryStream(input, writable: false), input.Length)
|
||||
{
|
||||
}
|
||||
|
||||
private Asn1Object BuildObject(int tag, int tagNo, int length)
|
||||
{
|
||||
bool flag = (tag & 0x20) != 0;
|
||||
DefiniteLengthInputStream definiteLengthInputStream = new DefiniteLengthInputStream(s, length);
|
||||
if ((tag & 0x40) != 0)
|
||||
{
|
||||
return new DerApplicationSpecific(flag, tagNo, definiteLengthInputStream.ToArray());
|
||||
}
|
||||
if ((tag & 0x80) != 0)
|
||||
{
|
||||
return new Asn1StreamParser(definiteLengthInputStream).ReadTaggedObject(flag, tagNo);
|
||||
}
|
||||
if (flag)
|
||||
{
|
||||
return tagNo switch
|
||||
{
|
||||
4 => new BerOctetString(BuildDerEncodableVector(definiteLengthInputStream)),
|
||||
16 => CreateDerSequence(definiteLengthInputStream),
|
||||
17 => CreateDerSet(definiteLengthInputStream),
|
||||
8 => new DerExternal(BuildDerEncodableVector(definiteLengthInputStream)),
|
||||
_ => throw new IOException("unknown tag " + tagNo + " encountered"),
|
||||
};
|
||||
}
|
||||
return CreatePrimitiveDerObject(tagNo, definiteLengthInputStream, tmpBuffers);
|
||||
}
|
||||
|
||||
internal Asn1EncodableVector BuildEncodableVector()
|
||||
{
|
||||
Asn1EncodableVector asn1EncodableVector = new Asn1EncodableVector();
|
||||
Asn1Object asn1Object;
|
||||
while ((asn1Object = ReadObject()) != null)
|
||||
{
|
||||
asn1EncodableVector.Add(asn1Object);
|
||||
}
|
||||
return asn1EncodableVector;
|
||||
}
|
||||
|
||||
internal virtual Asn1EncodableVector BuildDerEncodableVector(DefiniteLengthInputStream dIn)
|
||||
{
|
||||
return new Asn1InputStream(dIn).BuildEncodableVector();
|
||||
}
|
||||
|
||||
internal virtual DerSequence CreateDerSequence(DefiniteLengthInputStream dIn)
|
||||
{
|
||||
return DerSequence.FromVector(BuildDerEncodableVector(dIn));
|
||||
}
|
||||
|
||||
internal virtual DerSet CreateDerSet(DefiniteLengthInputStream dIn)
|
||||
{
|
||||
return DerSet.FromVector(BuildDerEncodableVector(dIn), needsSorting: false);
|
||||
}
|
||||
|
||||
public Asn1Object ReadObject()
|
||||
{
|
||||
int num = ReadByte();
|
||||
if (num <= 0)
|
||||
{
|
||||
if (num == 0)
|
||||
{
|
||||
throw new IOException("unexpected end-of-contents marker");
|
||||
}
|
||||
return null;
|
||||
}
|
||||
int num2 = ReadTagNumber(s, num);
|
||||
bool flag = (num & 0x20) != 0;
|
||||
int num3 = ReadLength(s, limit);
|
||||
if (num3 < 0)
|
||||
{
|
||||
if (!flag)
|
||||
{
|
||||
throw new IOException("indefinite length primitive encoding encountered");
|
||||
}
|
||||
IndefiniteLengthInputStream inStream = new IndefiniteLengthInputStream(s, limit);
|
||||
Asn1StreamParser parser = new Asn1StreamParser(inStream, limit);
|
||||
if ((num & 0x40) != 0)
|
||||
{
|
||||
return new BerApplicationSpecificParser(num2, parser).ToAsn1Object();
|
||||
}
|
||||
if ((num & 0x80) != 0)
|
||||
{
|
||||
return new BerTaggedObjectParser(constructed: true, num2, parser).ToAsn1Object();
|
||||
}
|
||||
return num2 switch
|
||||
{
|
||||
4 => new BerOctetStringParser(parser).ToAsn1Object(),
|
||||
16 => new BerSequenceParser(parser).ToAsn1Object(),
|
||||
17 => new BerSetParser(parser).ToAsn1Object(),
|
||||
8 => new DerExternalParser(parser).ToAsn1Object(),
|
||||
_ => throw new IOException("unknown BER object encountered"),
|
||||
};
|
||||
}
|
||||
try
|
||||
{
|
||||
return BuildObject(num, num2, num3);
|
||||
}
|
||||
catch (ArgumentException exception)
|
||||
{
|
||||
throw new Asn1Exception("corrupted stream detected", exception);
|
||||
}
|
||||
}
|
||||
|
||||
internal static int ReadTagNumber(Stream s, int tag)
|
||||
{
|
||||
int num = tag & 0x1F;
|
||||
if (num == 31)
|
||||
{
|
||||
num = 0;
|
||||
int num2 = s.ReadByte();
|
||||
if ((num2 & 0x7F) == 0)
|
||||
{
|
||||
throw new IOException("Corrupted stream - invalid high tag number found");
|
||||
}
|
||||
while (num2 >= 0 && (num2 & 0x80) != 0)
|
||||
{
|
||||
num |= num2 & 0x7F;
|
||||
num <<= 7;
|
||||
num2 = s.ReadByte();
|
||||
}
|
||||
if (num2 < 0)
|
||||
{
|
||||
throw new EndOfStreamException("EOF found inside tag value.");
|
||||
}
|
||||
num |= num2 & 0x7F;
|
||||
}
|
||||
return num;
|
||||
}
|
||||
|
||||
internal static int ReadLength(Stream s, int limit)
|
||||
{
|
||||
int num = s.ReadByte();
|
||||
if (num < 0)
|
||||
{
|
||||
throw new EndOfStreamException("EOF found when length expected");
|
||||
}
|
||||
if (num == 128)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
if (num > 127)
|
||||
{
|
||||
int num2 = num & 0x7F;
|
||||
if (num2 > 4)
|
||||
{
|
||||
throw new IOException("DER length more than 4 bytes: " + num2);
|
||||
}
|
||||
num = 0;
|
||||
for (int i = 0; i < num2; i++)
|
||||
{
|
||||
int num3 = s.ReadByte();
|
||||
if (num3 < 0)
|
||||
{
|
||||
throw new EndOfStreamException("EOF found reading length");
|
||||
}
|
||||
num = (num << 8) + num3;
|
||||
}
|
||||
if (num < 0)
|
||||
{
|
||||
throw new IOException("Corrupted stream - negative length found");
|
||||
}
|
||||
if (num >= limit)
|
||||
{
|
||||
throw new IOException("Corrupted stream - out of bounds length found");
|
||||
}
|
||||
}
|
||||
return num;
|
||||
}
|
||||
|
||||
internal static byte[] GetBuffer(DefiniteLengthInputStream defIn, byte[][] tmpBuffers)
|
||||
{
|
||||
int remaining = defIn.GetRemaining();
|
||||
if (remaining >= tmpBuffers.Length)
|
||||
{
|
||||
return defIn.ToArray();
|
||||
}
|
||||
byte[] array = tmpBuffers[remaining];
|
||||
if (array == null)
|
||||
{
|
||||
array = (tmpBuffers[remaining] = new byte[remaining]);
|
||||
}
|
||||
defIn.ReadAllIntoByteArray(array);
|
||||
return array;
|
||||
}
|
||||
|
||||
internal static Asn1Object CreatePrimitiveDerObject(int tagNo, DefiniteLengthInputStream defIn, byte[][] tmpBuffers)
|
||||
{
|
||||
switch (tagNo)
|
||||
{
|
||||
case 1:
|
||||
return DerBoolean.FromOctetString(GetBuffer(defIn, tmpBuffers));
|
||||
case 10:
|
||||
return DerEnumerated.FromOctetString(GetBuffer(defIn, tmpBuffers));
|
||||
case 6:
|
||||
return DerObjectIdentifier.FromOctetString(GetBuffer(defIn, tmpBuffers));
|
||||
default:
|
||||
{
|
||||
byte[] array = defIn.ToArray();
|
||||
return tagNo switch
|
||||
{
|
||||
3 => DerBitString.FromAsn1Octets(array),
|
||||
30 => new DerBmpString(array),
|
||||
24 => new DerGeneralizedTime(array),
|
||||
27 => new DerGeneralString(array),
|
||||
25 => new DerGraphicString(array),
|
||||
22 => new DerIA5String(array),
|
||||
2 => new DerInteger(array),
|
||||
5 => DerNull.Instance,
|
||||
18 => new DerNumericString(array),
|
||||
4 => new DerOctetString(array),
|
||||
19 => new DerPrintableString(array),
|
||||
20 => new DerT61String(array),
|
||||
28 => new DerUniversalString(array),
|
||||
23 => new DerUtcTime(array),
|
||||
12 => new DerUtf8String(array),
|
||||
21 => new DerVideotexString(array),
|
||||
26 => new DerVisibleString(array),
|
||||
_ => throw new IOException("unknown tag " + tagNo + " encountered"),
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
namespace Org.BouncyCastle.Asn1;
|
||||
|
||||
public abstract class Asn1Null : Asn1Object
|
||||
{
|
||||
internal Asn1Null()
|
||||
{
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return "NULL";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1;
|
||||
|
||||
public abstract class Asn1Object : Asn1Encodable
|
||||
{
|
||||
public static Asn1Object FromByteArray(byte[] data)
|
||||
{
|
||||
try
|
||||
{
|
||||
MemoryStream memoryStream = new MemoryStream(data, writable: false);
|
||||
Asn1InputStream asn1InputStream = new Asn1InputStream(memoryStream, data.Length);
|
||||
Asn1Object result = asn1InputStream.ReadObject();
|
||||
if (memoryStream.Position != memoryStream.Length)
|
||||
{
|
||||
throw new IOException("extra data found after object");
|
||||
}
|
||||
return result;
|
||||
}
|
||||
catch (InvalidCastException)
|
||||
{
|
||||
throw new IOException("cannot recognise object in byte array");
|
||||
}
|
||||
}
|
||||
|
||||
public static Asn1Object FromStream(Stream inStr)
|
||||
{
|
||||
try
|
||||
{
|
||||
return new Asn1InputStream(inStr).ReadObject();
|
||||
}
|
||||
catch (InvalidCastException)
|
||||
{
|
||||
throw new IOException("cannot recognise object in stream");
|
||||
}
|
||||
}
|
||||
|
||||
public sealed override Asn1Object ToAsn1Object()
|
||||
{
|
||||
return this;
|
||||
}
|
||||
|
||||
internal abstract void Encode(DerOutputStream derOut);
|
||||
|
||||
protected abstract bool Asn1Equals(Asn1Object asn1Object);
|
||||
|
||||
protected abstract int Asn1GetHashCode();
|
||||
|
||||
internal bool CallAsn1Equals(Asn1Object obj)
|
||||
{
|
||||
return Asn1Equals(obj);
|
||||
}
|
||||
|
||||
internal int CallAsn1GetHashCode()
|
||||
{
|
||||
return Asn1GetHashCode();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
using Org.BouncyCastle.Utilities.Encoders;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1;
|
||||
|
||||
public abstract class Asn1OctetString : Asn1Object, Asn1OctetStringParser, IAsn1Convertible
|
||||
{
|
||||
internal byte[] str;
|
||||
|
||||
public Asn1OctetStringParser Parser => this;
|
||||
|
||||
public static Asn1OctetString GetInstance(Asn1TaggedObject obj, bool isExplicit)
|
||||
{
|
||||
Asn1Object asn1Object = obj.GetObject();
|
||||
if (isExplicit || asn1Object is Asn1OctetString)
|
||||
{
|
||||
return GetInstance(asn1Object);
|
||||
}
|
||||
return BerOctetString.FromSequence(Asn1Sequence.GetInstance(asn1Object));
|
||||
}
|
||||
|
||||
public static Asn1OctetString GetInstance(object obj)
|
||||
{
|
||||
if (obj == null || obj is Asn1OctetString)
|
||||
{
|
||||
return (Asn1OctetString)obj;
|
||||
}
|
||||
if (obj is Asn1TaggedObject)
|
||||
{
|
||||
return GetInstance(((Asn1TaggedObject)obj).GetObject());
|
||||
}
|
||||
throw new ArgumentException("illegal object in GetInstance: " + Platform.GetTypeName(obj));
|
||||
}
|
||||
|
||||
internal Asn1OctetString(byte[] str)
|
||||
{
|
||||
if (str == null)
|
||||
{
|
||||
throw new ArgumentNullException("str");
|
||||
}
|
||||
this.str = str;
|
||||
}
|
||||
|
||||
internal Asn1OctetString(Asn1Encodable obj)
|
||||
{
|
||||
try
|
||||
{
|
||||
str = obj.GetEncoded("DER");
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
throw new ArgumentException("Error processing object : " + ex.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
public Stream GetOctetStream()
|
||||
{
|
||||
return new MemoryStream(str, writable: false);
|
||||
}
|
||||
|
||||
public virtual byte[] GetOctets()
|
||||
{
|
||||
return str;
|
||||
}
|
||||
|
||||
protected override int Asn1GetHashCode()
|
||||
{
|
||||
return Arrays.GetHashCode(GetOctets());
|
||||
}
|
||||
|
||||
protected override bool Asn1Equals(Asn1Object asn1Object)
|
||||
{
|
||||
if (!(asn1Object is DerOctetString derOctetString))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return Arrays.AreEqual(GetOctets(), derOctetString.GetOctets());
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return "#" + Hex.ToHexString(str);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
using System.IO;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1;
|
||||
|
||||
public interface Asn1OctetStringParser : IAsn1Convertible
|
||||
{
|
||||
Stream GetOctetStream();
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1;
|
||||
|
||||
public class Asn1OutputStream : DerOutputStream
|
||||
{
|
||||
public Asn1OutputStream(Stream os)
|
||||
: base(os)
|
||||
{
|
||||
}
|
||||
|
||||
[Obsolete("Use version taking an Asn1Encodable arg instead")]
|
||||
public override void WriteObject(object obj)
|
||||
{
|
||||
if (obj == null)
|
||||
{
|
||||
WriteNull();
|
||||
return;
|
||||
}
|
||||
if (obj is Asn1Object)
|
||||
{
|
||||
((Asn1Object)obj).Encode(this);
|
||||
return;
|
||||
}
|
||||
if (obj is Asn1Encodable)
|
||||
{
|
||||
((Asn1Encodable)obj).ToAsn1Object().Encode(this);
|
||||
return;
|
||||
}
|
||||
throw new IOException("object not Asn1Encodable");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
using System;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1;
|
||||
|
||||
[Serializable]
|
||||
public class Asn1ParsingException : InvalidOperationException
|
||||
{
|
||||
public Asn1ParsingException()
|
||||
{
|
||||
}
|
||||
|
||||
public Asn1ParsingException(string message)
|
||||
: base(message)
|
||||
{
|
||||
}
|
||||
|
||||
public Asn1ParsingException(string message, Exception exception)
|
||||
: base(message, exception)
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,207 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.IO;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
using Org.BouncyCastle.Utilities.Collections;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1;
|
||||
|
||||
public abstract class Asn1Sequence : Asn1Object, IEnumerable
|
||||
{
|
||||
private class Asn1SequenceParserImpl : Asn1SequenceParser, IAsn1Convertible
|
||||
{
|
||||
private readonly Asn1Sequence outer;
|
||||
|
||||
private readonly int max;
|
||||
|
||||
private int index;
|
||||
|
||||
public Asn1SequenceParserImpl(Asn1Sequence outer)
|
||||
{
|
||||
this.outer = outer;
|
||||
max = outer.Count;
|
||||
}
|
||||
|
||||
public IAsn1Convertible ReadObject()
|
||||
{
|
||||
if (index == max)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
Asn1Encodable asn1Encodable = outer[index++];
|
||||
if (asn1Encodable is Asn1Sequence)
|
||||
{
|
||||
return ((Asn1Sequence)asn1Encodable).Parser;
|
||||
}
|
||||
if (asn1Encodable is Asn1Set)
|
||||
{
|
||||
return ((Asn1Set)asn1Encodable).Parser;
|
||||
}
|
||||
return asn1Encodable;
|
||||
}
|
||||
|
||||
public Asn1Object ToAsn1Object()
|
||||
{
|
||||
return outer;
|
||||
}
|
||||
}
|
||||
|
||||
private readonly IList seq;
|
||||
|
||||
public virtual Asn1SequenceParser Parser => new Asn1SequenceParserImpl(this);
|
||||
|
||||
public virtual Asn1Encodable this[int index] => (Asn1Encodable)seq[index];
|
||||
|
||||
[Obsolete("Use 'Count' property instead")]
|
||||
public int Size => Count;
|
||||
|
||||
public virtual int Count => seq.Count;
|
||||
|
||||
public static Asn1Sequence GetInstance(object obj)
|
||||
{
|
||||
if (obj == null || obj is Asn1Sequence)
|
||||
{
|
||||
return (Asn1Sequence)obj;
|
||||
}
|
||||
if (obj is Asn1SequenceParser)
|
||||
{
|
||||
return GetInstance(((Asn1SequenceParser)obj).ToAsn1Object());
|
||||
}
|
||||
if (obj is byte[])
|
||||
{
|
||||
try
|
||||
{
|
||||
return GetInstance(Asn1Object.FromByteArray((byte[])obj));
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
throw new ArgumentException("failed to construct sequence from byte[]: " + ex.Message);
|
||||
}
|
||||
}
|
||||
if (obj is Asn1Encodable)
|
||||
{
|
||||
Asn1Object asn1Object = ((Asn1Encodable)obj).ToAsn1Object();
|
||||
if (asn1Object is Asn1Sequence)
|
||||
{
|
||||
return (Asn1Sequence)asn1Object;
|
||||
}
|
||||
}
|
||||
throw new ArgumentException("Unknown object in GetInstance: " + Platform.GetTypeName(obj), "obj");
|
||||
}
|
||||
|
||||
public static Asn1Sequence GetInstance(Asn1TaggedObject obj, bool explicitly)
|
||||
{
|
||||
Asn1Object asn1Object = obj.GetObject();
|
||||
if (explicitly)
|
||||
{
|
||||
if (!obj.IsExplicit())
|
||||
{
|
||||
throw new ArgumentException("object implicit - explicit expected.");
|
||||
}
|
||||
return (Asn1Sequence)asn1Object;
|
||||
}
|
||||
if (obj.IsExplicit())
|
||||
{
|
||||
if (obj is BerTaggedObject)
|
||||
{
|
||||
return new BerSequence(asn1Object);
|
||||
}
|
||||
return new DerSequence(asn1Object);
|
||||
}
|
||||
if (asn1Object is Asn1Sequence)
|
||||
{
|
||||
return (Asn1Sequence)asn1Object;
|
||||
}
|
||||
throw new ArgumentException("Unknown object in GetInstance: " + Platform.GetTypeName(obj), "obj");
|
||||
}
|
||||
|
||||
protected internal Asn1Sequence(int capacity)
|
||||
{
|
||||
seq = Platform.CreateArrayList(capacity);
|
||||
}
|
||||
|
||||
public virtual IEnumerator GetEnumerator()
|
||||
{
|
||||
return seq.GetEnumerator();
|
||||
}
|
||||
|
||||
[Obsolete("Use GetEnumerator() instead")]
|
||||
public IEnumerator GetObjects()
|
||||
{
|
||||
return GetEnumerator();
|
||||
}
|
||||
|
||||
[Obsolete("Use 'object[index]' syntax instead")]
|
||||
public Asn1Encodable GetObjectAt(int index)
|
||||
{
|
||||
return this[index];
|
||||
}
|
||||
|
||||
protected override int Asn1GetHashCode()
|
||||
{
|
||||
int num = Count;
|
||||
IEnumerator enumerator = GetEnumerator();
|
||||
try
|
||||
{
|
||||
while (enumerator.MoveNext())
|
||||
{
|
||||
object current = enumerator.Current;
|
||||
num *= 17;
|
||||
num = ((current != null) ? (num ^ current.GetHashCode()) : (num ^ DerNull.Instance.GetHashCode()));
|
||||
}
|
||||
return num;
|
||||
}
|
||||
finally
|
||||
{
|
||||
IDisposable disposable = enumerator as IDisposable;
|
||||
if (disposable != null)
|
||||
{
|
||||
disposable.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected override bool Asn1Equals(Asn1Object asn1Object)
|
||||
{
|
||||
if (!(asn1Object is Asn1Sequence asn1Sequence))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (Count != asn1Sequence.Count)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
IEnumerator enumerator = GetEnumerator();
|
||||
IEnumerator enumerator2 = asn1Sequence.GetEnumerator();
|
||||
while (enumerator.MoveNext() && enumerator2.MoveNext())
|
||||
{
|
||||
Asn1Object asn1Object2 = GetCurrent(enumerator).ToAsn1Object();
|
||||
Asn1Object obj = GetCurrent(enumerator2).ToAsn1Object();
|
||||
if (!asn1Object2.Equals(obj))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private Asn1Encodable GetCurrent(IEnumerator e)
|
||||
{
|
||||
Asn1Encodable asn1Encodable = (Asn1Encodable)e.Current;
|
||||
if (asn1Encodable == null)
|
||||
{
|
||||
return DerNull.Instance;
|
||||
}
|
||||
return asn1Encodable;
|
||||
}
|
||||
|
||||
protected internal void AddObject(Asn1Encodable obj)
|
||||
{
|
||||
seq.Add(obj);
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return CollectionUtilities.ToString(seq);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace Org.BouncyCastle.Asn1;
|
||||
|
||||
public interface Asn1SequenceParser : IAsn1Convertible
|
||||
{
|
||||
IAsn1Convertible ReadObject();
|
||||
}
|
||||
@@ -0,0 +1,293 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.IO;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
using Org.BouncyCastle.Utilities.Collections;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1;
|
||||
|
||||
public abstract class Asn1Set : Asn1Object, IEnumerable
|
||||
{
|
||||
private class Asn1SetParserImpl : Asn1SetParser, IAsn1Convertible
|
||||
{
|
||||
private readonly Asn1Set outer;
|
||||
|
||||
private readonly int max;
|
||||
|
||||
private int index;
|
||||
|
||||
public Asn1SetParserImpl(Asn1Set outer)
|
||||
{
|
||||
this.outer = outer;
|
||||
max = outer.Count;
|
||||
}
|
||||
|
||||
public IAsn1Convertible ReadObject()
|
||||
{
|
||||
if (index == max)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
Asn1Encodable asn1Encodable = outer[index++];
|
||||
if (asn1Encodable is Asn1Sequence)
|
||||
{
|
||||
return ((Asn1Sequence)asn1Encodable).Parser;
|
||||
}
|
||||
if (asn1Encodable is Asn1Set)
|
||||
{
|
||||
return ((Asn1Set)asn1Encodable).Parser;
|
||||
}
|
||||
return asn1Encodable;
|
||||
}
|
||||
|
||||
public virtual Asn1Object ToAsn1Object()
|
||||
{
|
||||
return outer;
|
||||
}
|
||||
}
|
||||
|
||||
private class DerComparer : IComparer
|
||||
{
|
||||
public int Compare(object x, object y)
|
||||
{
|
||||
byte[] array = (byte[])x;
|
||||
byte[] array2 = (byte[])y;
|
||||
int num = System.Math.Min(array.Length, array2.Length);
|
||||
for (int i = 0; i != num; i++)
|
||||
{
|
||||
byte b = array[i];
|
||||
byte b2 = array2[i];
|
||||
if (b != b2)
|
||||
{
|
||||
if (b >= b2)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
if (array.Length > array2.Length)
|
||||
{
|
||||
if (!AllZeroesFrom(array, num))
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
if (array.Length < array2.Length)
|
||||
{
|
||||
if (!AllZeroesFrom(array2, num))
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
private bool AllZeroesFrom(byte[] bs, int pos)
|
||||
{
|
||||
while (pos < bs.Length)
|
||||
{
|
||||
if (bs[pos++] != 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
private readonly IList _set;
|
||||
|
||||
public virtual Asn1Encodable this[int index] => (Asn1Encodable)_set[index];
|
||||
|
||||
[Obsolete("Use 'Count' property instead")]
|
||||
public int Size => Count;
|
||||
|
||||
public virtual int Count => _set.Count;
|
||||
|
||||
public Asn1SetParser Parser => new Asn1SetParserImpl(this);
|
||||
|
||||
public static Asn1Set GetInstance(object obj)
|
||||
{
|
||||
if (obj == null || obj is Asn1Set)
|
||||
{
|
||||
return (Asn1Set)obj;
|
||||
}
|
||||
if (obj is Asn1SetParser)
|
||||
{
|
||||
return GetInstance(((Asn1SetParser)obj).ToAsn1Object());
|
||||
}
|
||||
if (obj is byte[])
|
||||
{
|
||||
try
|
||||
{
|
||||
return GetInstance(Asn1Object.FromByteArray((byte[])obj));
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
throw new ArgumentException("failed to construct set from byte[]: " + ex.Message);
|
||||
}
|
||||
}
|
||||
if (obj is Asn1Encodable)
|
||||
{
|
||||
Asn1Object asn1Object = ((Asn1Encodable)obj).ToAsn1Object();
|
||||
if (asn1Object is Asn1Set)
|
||||
{
|
||||
return (Asn1Set)asn1Object;
|
||||
}
|
||||
}
|
||||
throw new ArgumentException("Unknown object in GetInstance: " + Platform.GetTypeName(obj), "obj");
|
||||
}
|
||||
|
||||
public static Asn1Set GetInstance(Asn1TaggedObject obj, bool explicitly)
|
||||
{
|
||||
Asn1Object asn1Object = obj.GetObject();
|
||||
if (explicitly)
|
||||
{
|
||||
if (!obj.IsExplicit())
|
||||
{
|
||||
throw new ArgumentException("object implicit - explicit expected.");
|
||||
}
|
||||
return (Asn1Set)asn1Object;
|
||||
}
|
||||
if (obj.IsExplicit())
|
||||
{
|
||||
return new DerSet(asn1Object);
|
||||
}
|
||||
if (asn1Object is Asn1Set)
|
||||
{
|
||||
return (Asn1Set)asn1Object;
|
||||
}
|
||||
if (asn1Object is Asn1Sequence)
|
||||
{
|
||||
Asn1EncodableVector asn1EncodableVector = new Asn1EncodableVector();
|
||||
Asn1Sequence asn1Sequence = (Asn1Sequence)asn1Object;
|
||||
foreach (Asn1Encodable item in asn1Sequence)
|
||||
{
|
||||
asn1EncodableVector.Add(item);
|
||||
}
|
||||
return new DerSet(asn1EncodableVector, needsSorting: false);
|
||||
}
|
||||
throw new ArgumentException("Unknown object in GetInstance: " + Platform.GetTypeName(obj), "obj");
|
||||
}
|
||||
|
||||
protected internal Asn1Set(int capacity)
|
||||
{
|
||||
_set = Platform.CreateArrayList(capacity);
|
||||
}
|
||||
|
||||
public virtual IEnumerator GetEnumerator()
|
||||
{
|
||||
return _set.GetEnumerator();
|
||||
}
|
||||
|
||||
[Obsolete("Use GetEnumerator() instead")]
|
||||
public IEnumerator GetObjects()
|
||||
{
|
||||
return GetEnumerator();
|
||||
}
|
||||
|
||||
[Obsolete("Use 'object[index]' syntax instead")]
|
||||
public Asn1Encodable GetObjectAt(int index)
|
||||
{
|
||||
return this[index];
|
||||
}
|
||||
|
||||
public virtual Asn1Encodable[] ToArray()
|
||||
{
|
||||
Asn1Encodable[] array = new Asn1Encodable[Count];
|
||||
for (int i = 0; i < Count; i++)
|
||||
{
|
||||
array[i] = this[i];
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
protected override int Asn1GetHashCode()
|
||||
{
|
||||
int num = Count;
|
||||
IEnumerator enumerator = GetEnumerator();
|
||||
try
|
||||
{
|
||||
while (enumerator.MoveNext())
|
||||
{
|
||||
object current = enumerator.Current;
|
||||
num *= 17;
|
||||
num = ((current != null) ? (num ^ current.GetHashCode()) : (num ^ DerNull.Instance.GetHashCode()));
|
||||
}
|
||||
return num;
|
||||
}
|
||||
finally
|
||||
{
|
||||
IDisposable disposable = enumerator as IDisposable;
|
||||
if (disposable != null)
|
||||
{
|
||||
disposable.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected override bool Asn1Equals(Asn1Object asn1Object)
|
||||
{
|
||||
if (!(asn1Object is Asn1Set asn1Set))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (Count != asn1Set.Count)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
IEnumerator enumerator = GetEnumerator();
|
||||
IEnumerator enumerator2 = asn1Set.GetEnumerator();
|
||||
while (enumerator.MoveNext() && enumerator2.MoveNext())
|
||||
{
|
||||
Asn1Object asn1Object2 = GetCurrent(enumerator).ToAsn1Object();
|
||||
Asn1Object obj = GetCurrent(enumerator2).ToAsn1Object();
|
||||
if (!asn1Object2.Equals(obj))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private Asn1Encodable GetCurrent(IEnumerator e)
|
||||
{
|
||||
Asn1Encodable asn1Encodable = (Asn1Encodable)e.Current;
|
||||
if (asn1Encodable == null)
|
||||
{
|
||||
return DerNull.Instance;
|
||||
}
|
||||
return asn1Encodable;
|
||||
}
|
||||
|
||||
protected internal void Sort()
|
||||
{
|
||||
if (_set.Count >= 2)
|
||||
{
|
||||
Asn1Encodable[] array = new Asn1Encodable[_set.Count];
|
||||
byte[][] array2 = new byte[_set.Count][];
|
||||
for (int i = 0; i < _set.Count; i++)
|
||||
{
|
||||
array2[i] = (array[i] = (Asn1Encodable)_set[i]).GetEncoded("DER");
|
||||
}
|
||||
Array.Sort(array2, array, new DerComparer());
|
||||
for (int j = 0; j < _set.Count; j++)
|
||||
{
|
||||
_set[j] = array[j];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected internal void AddObject(Asn1Encodable obj)
|
||||
{
|
||||
_set.Add(obj);
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return CollectionUtilities.ToString(_set);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace Org.BouncyCastle.Asn1;
|
||||
|
||||
public interface Asn1SetParser : IAsn1Convertible
|
||||
{
|
||||
IAsn1Convertible ReadObject();
|
||||
}
|
||||
@@ -0,0 +1,189 @@
|
||||
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");
|
||||
}
|
||||
_in = inStream;
|
||||
_limit = limit;
|
||||
tmpBuffers = new byte[16][];
|
||||
}
|
||||
|
||||
public Asn1StreamParser(byte[] encoding)
|
||||
: this(new MemoryStream(encoding, writable: false), encoding.Length)
|
||||
{
|
||||
}
|
||||
|
||||
internal IAsn1Convertible ReadIndef(int tagValue)
|
||||
{
|
||||
return tagValue switch
|
||||
{
|
||||
8 => new DerExternalParser(this),
|
||||
4 => new BerOctetStringParser(this),
|
||||
16 => new BerSequenceParser(this),
|
||||
17 => new BerSetParser(this),
|
||||
_ => 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 17:
|
||||
return new DerSetParser(this);
|
||||
case 16:
|
||||
return new DerSequenceParser(this);
|
||||
case 4:
|
||||
return new BerOctetStringParser(this);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
switch (tag)
|
||||
{
|
||||
case 17:
|
||||
throw new Asn1Exception("sequences must use constructed encoding (see X.690 8.9.1/8.10.1)");
|
||||
case 16:
|
||||
throw new Asn1Exception("sets must use constructed encoding (see X.690 8.11.1/8.12.1)");
|
||||
case 4:
|
||||
return new DerOctetStringParser((DefiniteLengthInputStream)_in);
|
||||
}
|
||||
}
|
||||
throw new Asn1Exception("implicit tagging not implemented");
|
||||
}
|
||||
|
||||
internal Asn1Object ReadTaggedObject(bool constructed, int tag)
|
||||
{
|
||||
if (!constructed)
|
||||
{
|
||||
DefiniteLengthInputStream definiteLengthInputStream = (DefiniteLengthInputStream)_in;
|
||||
return new DerTaggedObject(explicitly: false, tag, new DerOctetString(definiteLengthInputStream.ToArray()));
|
||||
}
|
||||
Asn1EncodableVector asn1EncodableVector = ReadVector();
|
||||
if (_in is IndefiniteLengthInputStream)
|
||||
{
|
||||
if (asn1EncodableVector.Count != 1)
|
||||
{
|
||||
return new BerTaggedObject(explicitly: false, tag, BerSequence.FromVector(asn1EncodableVector));
|
||||
}
|
||||
return new BerTaggedObject(explicitly: true, tag, asn1EncodableVector[0]);
|
||||
}
|
||||
if (asn1EncodableVector.Count != 1)
|
||||
{
|
||||
return new DerTaggedObject(explicitly: false, tag, DerSequence.FromVector(asn1EncodableVector));
|
||||
}
|
||||
return new DerTaggedObject(explicitly: true, tag, asn1EncodableVector[0]);
|
||||
}
|
||||
|
||||
public virtual IAsn1Convertible ReadObject()
|
||||
{
|
||||
int num = _in.ReadByte();
|
||||
if (num == -1)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
Set00Check(enabled: false);
|
||||
int num2 = Asn1InputStream.ReadTagNumber(_in, num);
|
||||
bool flag = (num & 0x20) != 0;
|
||||
int num3 = Asn1InputStream.ReadLength(_in, _limit);
|
||||
if (num3 < 0)
|
||||
{
|
||||
if (!flag)
|
||||
{
|
||||
throw new IOException("indefinite length primitive encoding encountered");
|
||||
}
|
||||
IndefiniteLengthInputStream inStream = new IndefiniteLengthInputStream(_in, _limit);
|
||||
Asn1StreamParser asn1StreamParser = new Asn1StreamParser(inStream, _limit);
|
||||
if ((num & 0x40) != 0)
|
||||
{
|
||||
return new BerApplicationSpecificParser(num2, asn1StreamParser);
|
||||
}
|
||||
if ((num & 0x80) != 0)
|
||||
{
|
||||
return new BerTaggedObjectParser(constructed: true, num2, asn1StreamParser);
|
||||
}
|
||||
return asn1StreamParser.ReadIndef(num2);
|
||||
}
|
||||
DefiniteLengthInputStream definiteLengthInputStream = new DefiniteLengthInputStream(_in, num3);
|
||||
if ((num & 0x40) != 0)
|
||||
{
|
||||
return new DerApplicationSpecific(flag, num2, definiteLengthInputStream.ToArray());
|
||||
}
|
||||
if ((num & 0x80) != 0)
|
||||
{
|
||||
return new BerTaggedObjectParser(flag, num2, new Asn1StreamParser(definiteLengthInputStream));
|
||||
}
|
||||
if (flag)
|
||||
{
|
||||
return num2 switch
|
||||
{
|
||||
4 => new BerOctetStringParser(new Asn1StreamParser(definiteLengthInputStream)),
|
||||
16 => new DerSequenceParser(new Asn1StreamParser(definiteLengthInputStream)),
|
||||
17 => new DerSetParser(new Asn1StreamParser(definiteLengthInputStream)),
|
||||
8 => new DerExternalParser(new Asn1StreamParser(definiteLengthInputStream)),
|
||||
_ => throw new IOException("unknown tag " + num2 + " encountered"),
|
||||
};
|
||||
}
|
||||
int num4 = num2;
|
||||
if (num4 == 4)
|
||||
{
|
||||
return new DerOctetStringParser(definiteLengthInputStream);
|
||||
}
|
||||
try
|
||||
{
|
||||
return Asn1InputStream.CreatePrimitiveDerObject(num2, definiteLengthInputStream, tmpBuffers);
|
||||
}
|
||||
catch (ArgumentException exception)
|
||||
{
|
||||
throw new Asn1Exception("corrupted stream detected", exception);
|
||||
}
|
||||
}
|
||||
|
||||
private void Set00Check(bool enabled)
|
||||
{
|
||||
if (_in is IndefiniteLengthInputStream)
|
||||
{
|
||||
((IndefiniteLengthInputStream)_in).SetEofOn00(enabled);
|
||||
}
|
||||
}
|
||||
|
||||
internal Asn1EncodableVector ReadVector()
|
||||
{
|
||||
Asn1EncodableVector asn1EncodableVector = new Asn1EncodableVector();
|
||||
IAsn1Convertible asn1Convertible;
|
||||
while ((asn1Convertible = ReadObject()) != null)
|
||||
{
|
||||
asn1EncodableVector.Add(asn1Convertible.ToAsn1Object());
|
||||
}
|
||||
return asn1EncodableVector;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1;
|
||||
|
||||
public abstract class Asn1TaggedObject : Asn1Object, Asn1TaggedObjectParser, IAsn1Convertible
|
||||
{
|
||||
internal int tagNo;
|
||||
|
||||
internal bool explicitly = true;
|
||||
|
||||
internal Asn1Encodable obj;
|
||||
|
||||
public int TagNo => tagNo;
|
||||
|
||||
internal static bool IsConstructed(bool isExplicit, Asn1Object obj)
|
||||
{
|
||||
if (isExplicit || obj is Asn1Sequence || obj is Asn1Set)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
if (!(obj is Asn1TaggedObject asn1TaggedObject))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return IsConstructed(asn1TaggedObject.IsExplicit(), asn1TaggedObject.GetObject());
|
||||
}
|
||||
|
||||
public static Asn1TaggedObject GetInstance(Asn1TaggedObject obj, bool explicitly)
|
||||
{
|
||||
if (explicitly)
|
||||
{
|
||||
return (Asn1TaggedObject)obj.GetObject();
|
||||
}
|
||||
throw new ArgumentException("implicitly tagged tagged object");
|
||||
}
|
||||
|
||||
public static Asn1TaggedObject GetInstance(object obj)
|
||||
{
|
||||
if (obj == null || obj is Asn1TaggedObject)
|
||||
{
|
||||
return (Asn1TaggedObject)obj;
|
||||
}
|
||||
throw new ArgumentException("Unknown object in GetInstance: " + Platform.GetTypeName(obj), "obj");
|
||||
}
|
||||
|
||||
protected Asn1TaggedObject(int tagNo, Asn1Encodable obj)
|
||||
{
|
||||
explicitly = true;
|
||||
this.tagNo = tagNo;
|
||||
this.obj = obj;
|
||||
}
|
||||
|
||||
protected Asn1TaggedObject(bool explicitly, int tagNo, Asn1Encodable obj)
|
||||
{
|
||||
this.explicitly = explicitly || obj is IAsn1Choice;
|
||||
this.tagNo = tagNo;
|
||||
this.obj = obj;
|
||||
}
|
||||
|
||||
protected override bool Asn1Equals(Asn1Object asn1Object)
|
||||
{
|
||||
if (!(asn1Object is Asn1TaggedObject asn1TaggedObject))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (tagNo == asn1TaggedObject.tagNo && explicitly == asn1TaggedObject.explicitly)
|
||||
{
|
||||
return object.Equals(GetObject(), asn1TaggedObject.GetObject());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
protected override int Asn1GetHashCode()
|
||||
{
|
||||
int num = tagNo.GetHashCode();
|
||||
if (obj != null)
|
||||
{
|
||||
num ^= obj.GetHashCode();
|
||||
}
|
||||
return num;
|
||||
}
|
||||
|
||||
public bool IsExplicit()
|
||||
{
|
||||
return explicitly;
|
||||
}
|
||||
|
||||
public bool IsEmpty()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public Asn1Object GetObject()
|
||||
{
|
||||
if (obj != null)
|
||||
{
|
||||
return obj.ToAsn1Object();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public IAsn1Convertible GetObjectParser(int tag, bool isExplicit)
|
||||
{
|
||||
switch (tag)
|
||||
{
|
||||
case 17:
|
||||
return Asn1Set.GetInstance(this, isExplicit).Parser;
|
||||
case 16:
|
||||
return Asn1Sequence.GetInstance(this, isExplicit).Parser;
|
||||
case 4:
|
||||
return Asn1OctetString.GetInstance(this, isExplicit).Parser;
|
||||
default:
|
||||
if (isExplicit)
|
||||
{
|
||||
return GetObject();
|
||||
}
|
||||
throw Platform.CreateNotImplementedException("implicit tagging for tag: " + tag);
|
||||
}
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return "[" + tagNo + "]" + obj;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace Org.BouncyCastle.Asn1;
|
||||
|
||||
public interface Asn1TaggedObjectParser : IAsn1Convertible
|
||||
{
|
||||
int TagNo { get; }
|
||||
|
||||
IAsn1Convertible GetObjectParser(int tag, bool isExplicit);
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
namespace Org.BouncyCastle.Asn1;
|
||||
|
||||
public class Asn1Tags
|
||||
{
|
||||
public const int Boolean = 1;
|
||||
|
||||
public const int Integer = 2;
|
||||
|
||||
public const int BitString = 3;
|
||||
|
||||
public const int OctetString = 4;
|
||||
|
||||
public const int Null = 5;
|
||||
|
||||
public const int ObjectIdentifier = 6;
|
||||
|
||||
public const int External = 8;
|
||||
|
||||
public const int Enumerated = 10;
|
||||
|
||||
public const int Sequence = 16;
|
||||
|
||||
public const int SequenceOf = 16;
|
||||
|
||||
public const int Set = 17;
|
||||
|
||||
public const int SetOf = 17;
|
||||
|
||||
public const int NumericString = 18;
|
||||
|
||||
public const int PrintableString = 19;
|
||||
|
||||
public const int T61String = 20;
|
||||
|
||||
public const int VideotexString = 21;
|
||||
|
||||
public const int IA5String = 22;
|
||||
|
||||
public const int UtcTime = 23;
|
||||
|
||||
public const int GeneralizedTime = 24;
|
||||
|
||||
public const int GraphicString = 25;
|
||||
|
||||
public const int VisibleString = 26;
|
||||
|
||||
public const int GeneralString = 27;
|
||||
|
||||
public const int UniversalString = 28;
|
||||
|
||||
public const int BmpString = 30;
|
||||
|
||||
public const int Utf8String = 12;
|
||||
|
||||
public const int Constructed = 32;
|
||||
|
||||
public const int Application = 64;
|
||||
|
||||
public const int Tagged = 128;
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
namespace Org.BouncyCastle.Asn1.BC;
|
||||
|
||||
public abstract class BCObjectIdentifiers
|
||||
{
|
||||
public static readonly DerObjectIdentifier bc = new DerObjectIdentifier("1.3.6.1.4.1.22554");
|
||||
|
||||
public static readonly DerObjectIdentifier bc_pbe = bc.Branch("1");
|
||||
|
||||
public static readonly DerObjectIdentifier bc_pbe_sha1 = bc_pbe.Branch("1");
|
||||
|
||||
public static readonly DerObjectIdentifier bc_pbe_sha256 = bc_pbe.Branch("2.1");
|
||||
|
||||
public static readonly DerObjectIdentifier bc_pbe_sha384 = bc_pbe.Branch("2.2");
|
||||
|
||||
public static readonly DerObjectIdentifier bc_pbe_sha512 = bc_pbe.Branch("2.3");
|
||||
|
||||
public static readonly DerObjectIdentifier bc_pbe_sha224 = bc_pbe.Branch("2.4");
|
||||
|
||||
public static readonly DerObjectIdentifier bc_pbe_sha1_pkcs5 = bc_pbe_sha1.Branch("1");
|
||||
|
||||
public static readonly DerObjectIdentifier bc_pbe_sha1_pkcs12 = bc_pbe_sha1.Branch("2");
|
||||
|
||||
public static readonly DerObjectIdentifier bc_pbe_sha256_pkcs5 = bc_pbe_sha256.Branch("1");
|
||||
|
||||
public static readonly DerObjectIdentifier bc_pbe_sha256_pkcs12 = bc_pbe_sha256.Branch("2");
|
||||
|
||||
public static readonly DerObjectIdentifier bc_pbe_sha1_pkcs12_aes128_cbc = bc_pbe_sha1_pkcs12.Branch("1.2");
|
||||
|
||||
public static readonly DerObjectIdentifier bc_pbe_sha1_pkcs12_aes192_cbc = bc_pbe_sha1_pkcs12.Branch("1.22");
|
||||
|
||||
public static readonly DerObjectIdentifier bc_pbe_sha1_pkcs12_aes256_cbc = bc_pbe_sha1_pkcs12.Branch("1.42");
|
||||
|
||||
public static readonly DerObjectIdentifier bc_pbe_sha256_pkcs12_aes128_cbc = bc_pbe_sha256_pkcs12.Branch("1.2");
|
||||
|
||||
public static readonly DerObjectIdentifier bc_pbe_sha256_pkcs12_aes192_cbc = bc_pbe_sha256_pkcs12.Branch("1.22");
|
||||
|
||||
public static readonly DerObjectIdentifier bc_pbe_sha256_pkcs12_aes256_cbc = bc_pbe_sha256_pkcs12.Branch("1.42");
|
||||
|
||||
public static readonly DerObjectIdentifier bc_sig = bc.Branch("2");
|
||||
|
||||
public static readonly DerObjectIdentifier sphincs256 = bc_sig.Branch("1");
|
||||
|
||||
public static readonly DerObjectIdentifier sphincs256_with_BLAKE512 = sphincs256.Branch("1");
|
||||
|
||||
public static readonly DerObjectIdentifier sphincs256_with_SHA512 = sphincs256.Branch("2");
|
||||
|
||||
public static readonly DerObjectIdentifier sphincs256_with_SHA3_512 = sphincs256.Branch("3");
|
||||
|
||||
public static readonly DerObjectIdentifier xmss = bc_sig.Branch("2");
|
||||
|
||||
public static readonly DerObjectIdentifier xmss_with_SHA256 = xmss.Branch("1");
|
||||
|
||||
public static readonly DerObjectIdentifier xmss_with_SHA512 = xmss.Branch("2");
|
||||
|
||||
public static readonly DerObjectIdentifier xmss_with_SHAKE128 = xmss.Branch("3");
|
||||
|
||||
public static readonly DerObjectIdentifier xmss_with_SHAKE256 = xmss.Branch("4");
|
||||
|
||||
public static readonly DerObjectIdentifier xmss_mt = bc_sig.Branch("3");
|
||||
|
||||
public static readonly DerObjectIdentifier xmss_mt_with_SHA256 = xmss_mt.Branch("1");
|
||||
|
||||
public static readonly DerObjectIdentifier xmss_mt_with_SHA512 = xmss_mt.Branch("2");
|
||||
|
||||
public static readonly DerObjectIdentifier xmss_mt_with_SHAKE128 = xmss_mt.Branch("3");
|
||||
|
||||
public static readonly DerObjectIdentifier xmss_mt_with_SHAKE256 = xmss_mt.Branch("4");
|
||||
|
||||
public static readonly DerObjectIdentifier bc_exch = bc.Branch("3");
|
||||
|
||||
public static readonly DerObjectIdentifier newHope = bc_exch.Branch("1");
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace Org.BouncyCastle.Asn1;
|
||||
|
||||
public class BerApplicationSpecific : DerApplicationSpecific
|
||||
{
|
||||
public BerApplicationSpecific(int tagNo, Asn1EncodableVector vec)
|
||||
: base(tagNo, vec)
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
namespace Org.BouncyCastle.Asn1;
|
||||
|
||||
public class BerApplicationSpecificParser : IAsn1ApplicationSpecificParser, IAsn1Convertible
|
||||
{
|
||||
private readonly int tag;
|
||||
|
||||
private readonly Asn1StreamParser parser;
|
||||
|
||||
internal BerApplicationSpecificParser(int tag, Asn1StreamParser parser)
|
||||
{
|
||||
this.tag = tag;
|
||||
this.parser = parser;
|
||||
}
|
||||
|
||||
public IAsn1Convertible ReadObject()
|
||||
{
|
||||
return parser.ReadObject();
|
||||
}
|
||||
|
||||
public Asn1Object ToAsn1Object()
|
||||
{
|
||||
return new BerApplicationSpecific(tag, parser.ReadVector());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
namespace Org.BouncyCastle.Asn1;
|
||||
|
||||
public class BerBitString : DerBitString
|
||||
{
|
||||
public BerBitString(byte[] data, int padBits)
|
||||
: base(data, padBits)
|
||||
{
|
||||
}
|
||||
|
||||
public BerBitString(byte[] data)
|
||||
: base(data)
|
||||
{
|
||||
}
|
||||
|
||||
public BerBitString(int namedBits)
|
||||
: base(namedBits)
|
||||
{
|
||||
}
|
||||
|
||||
public BerBitString(Asn1Encodable obj)
|
||||
: base(obj)
|
||||
{
|
||||
}
|
||||
|
||||
internal override void Encode(DerOutputStream derOut)
|
||||
{
|
||||
if (derOut is Asn1OutputStream || derOut is BerOutputStream)
|
||||
{
|
||||
derOut.WriteEncoded(3, (byte)mPadBits, mData);
|
||||
}
|
||||
else
|
||||
{
|
||||
base.Encode(derOut);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
using System.IO;
|
||||
using Org.BouncyCastle.Utilities.IO;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1;
|
||||
|
||||
public class BerGenerator : Asn1Generator
|
||||
{
|
||||
private bool _tagged = false;
|
||||
|
||||
private bool _isExplicit;
|
||||
|
||||
private int _tagNo;
|
||||
|
||||
protected BerGenerator(Stream outStream)
|
||||
: base(outStream)
|
||||
{
|
||||
}
|
||||
|
||||
public BerGenerator(Stream outStream, int tagNo, bool isExplicit)
|
||||
: base(outStream)
|
||||
{
|
||||
_tagged = true;
|
||||
_isExplicit = isExplicit;
|
||||
_tagNo = tagNo;
|
||||
}
|
||||
|
||||
public override void AddObject(Asn1Encodable obj)
|
||||
{
|
||||
new BerOutputStream(base.Out).WriteObject(obj);
|
||||
}
|
||||
|
||||
public override Stream GetRawOutputStream()
|
||||
{
|
||||
return base.Out;
|
||||
}
|
||||
|
||||
public override void Close()
|
||||
{
|
||||
WriteBerEnd();
|
||||
}
|
||||
|
||||
private void WriteHdr(int tag)
|
||||
{
|
||||
base.Out.WriteByte((byte)tag);
|
||||
base.Out.WriteByte(128);
|
||||
}
|
||||
|
||||
protected void WriteBerHeader(int tag)
|
||||
{
|
||||
if (_tagged)
|
||||
{
|
||||
int num = _tagNo | 0x80;
|
||||
if (_isExplicit)
|
||||
{
|
||||
WriteHdr(num | 0x20);
|
||||
WriteHdr(tag);
|
||||
}
|
||||
else if ((tag & 0x20) != 0)
|
||||
{
|
||||
WriteHdr(num | 0x20);
|
||||
}
|
||||
else
|
||||
{
|
||||
WriteHdr(num);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
WriteHdr(tag);
|
||||
}
|
||||
}
|
||||
|
||||
protected void WriteBerBody(Stream contentStream)
|
||||
{
|
||||
Streams.PipeAll(contentStream, base.Out);
|
||||
}
|
||||
|
||||
protected void WriteBerEnd()
|
||||
{
|
||||
base.Out.WriteByte(0);
|
||||
base.Out.WriteByte(0);
|
||||
if (_tagged && _isExplicit)
|
||||
{
|
||||
base.Out.WriteByte(0);
|
||||
base.Out.WriteByte(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
using System;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1;
|
||||
|
||||
public class BerNull : DerNull
|
||||
{
|
||||
public new static readonly BerNull Instance = new BerNull(0);
|
||||
|
||||
[Obsolete("Use static Instance object")]
|
||||
public BerNull()
|
||||
{
|
||||
}
|
||||
|
||||
private BerNull(int dummy)
|
||||
: base(dummy)
|
||||
{
|
||||
}
|
||||
|
||||
internal override void Encode(DerOutputStream derOut)
|
||||
{
|
||||
if (derOut is Asn1OutputStream || derOut is BerOutputStream)
|
||||
{
|
||||
derOut.WriteByte(5);
|
||||
}
|
||||
else
|
||||
{
|
||||
base.Encode(derOut);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.IO;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1;
|
||||
|
||||
public class BerOctetString : DerOctetString, IEnumerable
|
||||
{
|
||||
private const int MaxLength = 1000;
|
||||
|
||||
private readonly IEnumerable octs;
|
||||
|
||||
public static BerOctetString FromSequence(Asn1Sequence seq)
|
||||
{
|
||||
IList list = Platform.CreateArrayList();
|
||||
foreach (Asn1Encodable item in seq)
|
||||
{
|
||||
list.Add(item);
|
||||
}
|
||||
return new BerOctetString(list);
|
||||
}
|
||||
|
||||
private static byte[] ToBytes(IEnumerable octs)
|
||||
{
|
||||
MemoryStream memoryStream = new MemoryStream();
|
||||
foreach (DerOctetString oct in octs)
|
||||
{
|
||||
byte[] octets = oct.GetOctets();
|
||||
memoryStream.Write(octets, 0, octets.Length);
|
||||
}
|
||||
return memoryStream.ToArray();
|
||||
}
|
||||
|
||||
public BerOctetString(byte[] str)
|
||||
: base(str)
|
||||
{
|
||||
}
|
||||
|
||||
public BerOctetString(IEnumerable octets)
|
||||
: base(ToBytes(octets))
|
||||
{
|
||||
octs = octets;
|
||||
}
|
||||
|
||||
public BerOctetString(Asn1Object obj)
|
||||
: base(obj)
|
||||
{
|
||||
}
|
||||
|
||||
public BerOctetString(Asn1Encodable obj)
|
||||
: base(obj.ToAsn1Object())
|
||||
{
|
||||
}
|
||||
|
||||
public override byte[] GetOctets()
|
||||
{
|
||||
return str;
|
||||
}
|
||||
|
||||
public IEnumerator GetEnumerator()
|
||||
{
|
||||
if (octs == null)
|
||||
{
|
||||
return GenerateOcts().GetEnumerator();
|
||||
}
|
||||
return octs.GetEnumerator();
|
||||
}
|
||||
|
||||
[Obsolete("Use GetEnumerator() instead")]
|
||||
public IEnumerator GetObjects()
|
||||
{
|
||||
return GetEnumerator();
|
||||
}
|
||||
|
||||
private IList GenerateOcts()
|
||||
{
|
||||
IList list = Platform.CreateArrayList();
|
||||
for (int i = 0; i < str.Length; i += 1000)
|
||||
{
|
||||
int num = System.Math.Min(str.Length, i + 1000);
|
||||
byte[] array = new byte[num - i];
|
||||
Array.Copy(str, i, array, 0, array.Length);
|
||||
list.Add(new DerOctetString(array));
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
internal override void Encode(DerOutputStream derOut)
|
||||
{
|
||||
if (derOut is Asn1OutputStream || derOut is BerOutputStream)
|
||||
{
|
||||
derOut.WriteByte(36);
|
||||
derOut.WriteByte(128);
|
||||
{
|
||||
IEnumerator enumerator = GetEnumerator();
|
||||
try
|
||||
{
|
||||
while (enumerator.MoveNext())
|
||||
{
|
||||
DerOctetString obj = (DerOctetString)enumerator.Current;
|
||||
derOut.WriteObject(obj);
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
IDisposable disposable = enumerator as IDisposable;
|
||||
if (disposable != null)
|
||||
{
|
||||
disposable.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
derOut.WriteByte(0);
|
||||
derOut.WriteByte(0);
|
||||
}
|
||||
else
|
||||
{
|
||||
base.Encode(derOut);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using Org.BouncyCastle.Utilities.IO;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1;
|
||||
|
||||
public class BerOctetStringGenerator : BerGenerator
|
||||
{
|
||||
private class BufferedBerOctetStream : BaseOutputStream
|
||||
{
|
||||
private byte[] _buf;
|
||||
|
||||
private int _off;
|
||||
|
||||
private readonly BerOctetStringGenerator _gen;
|
||||
|
||||
private readonly DerOutputStream _derOut;
|
||||
|
||||
internal BufferedBerOctetStream(BerOctetStringGenerator gen, byte[] buf)
|
||||
{
|
||||
_gen = gen;
|
||||
_buf = buf;
|
||||
_off = 0;
|
||||
_derOut = new DerOutputStream(_gen.Out);
|
||||
}
|
||||
|
||||
public override void WriteByte(byte b)
|
||||
{
|
||||
_buf[_off++] = b;
|
||||
if (_off == _buf.Length)
|
||||
{
|
||||
DerOctetString.Encode(_derOut, _buf, 0, _off);
|
||||
_off = 0;
|
||||
}
|
||||
}
|
||||
|
||||
public override void Write(byte[] buf, int offset, int len)
|
||||
{
|
||||
while (len > 0)
|
||||
{
|
||||
int num = System.Math.Min(len, _buf.Length - _off);
|
||||
if (num == _buf.Length)
|
||||
{
|
||||
DerOctetString.Encode(_derOut, buf, offset, num);
|
||||
}
|
||||
else
|
||||
{
|
||||
Array.Copy(buf, offset, _buf, _off, num);
|
||||
_off += num;
|
||||
if (_off < _buf.Length)
|
||||
{
|
||||
break;
|
||||
}
|
||||
DerOctetString.Encode(_derOut, _buf, 0, _off);
|
||||
_off = 0;
|
||||
}
|
||||
offset += num;
|
||||
len -= num;
|
||||
}
|
||||
}
|
||||
|
||||
public override void Close()
|
||||
{
|
||||
if (_off != 0)
|
||||
{
|
||||
DerOctetString.Encode(_derOut, _buf, 0, _off);
|
||||
}
|
||||
_gen.WriteBerEnd();
|
||||
base.Close();
|
||||
}
|
||||
}
|
||||
|
||||
public BerOctetStringGenerator(Stream outStream)
|
||||
: base(outStream)
|
||||
{
|
||||
WriteBerHeader(36);
|
||||
}
|
||||
|
||||
public BerOctetStringGenerator(Stream outStream, int tagNo, bool isExplicit)
|
||||
: base(outStream, tagNo, isExplicit)
|
||||
{
|
||||
WriteBerHeader(36);
|
||||
}
|
||||
|
||||
public Stream GetOctetOutputStream()
|
||||
{
|
||||
return GetOctetOutputStream(new byte[1000]);
|
||||
}
|
||||
|
||||
public Stream GetOctetOutputStream(int bufSize)
|
||||
{
|
||||
if (bufSize >= 1)
|
||||
{
|
||||
return GetOctetOutputStream(new byte[bufSize]);
|
||||
}
|
||||
return GetOctetOutputStream();
|
||||
}
|
||||
|
||||
public Stream GetOctetOutputStream(byte[] buf)
|
||||
{
|
||||
return new BufferedBerOctetStream(this, buf);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
using System.IO;
|
||||
using Org.BouncyCastle.Utilities.IO;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1;
|
||||
|
||||
public class BerOctetStringParser : Asn1OctetStringParser, IAsn1Convertible
|
||||
{
|
||||
private readonly Asn1StreamParser _parser;
|
||||
|
||||
internal BerOctetStringParser(Asn1StreamParser parser)
|
||||
{
|
||||
_parser = parser;
|
||||
}
|
||||
|
||||
public Stream GetOctetStream()
|
||||
{
|
||||
return new ConstructedOctetStream(_parser);
|
||||
}
|
||||
|
||||
public Asn1Object ToAsn1Object()
|
||||
{
|
||||
try
|
||||
{
|
||||
return new BerOctetString(Streams.ReadAll(GetOctetStream()));
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
throw new Asn1ParsingException("IOException converting stream to byte array: " + ex.Message, ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1;
|
||||
|
||||
public class BerOutputStream : DerOutputStream
|
||||
{
|
||||
public BerOutputStream(Stream os)
|
||||
: base(os)
|
||||
{
|
||||
}
|
||||
|
||||
[Obsolete("Use version taking an Asn1Encodable arg instead")]
|
||||
public override void WriteObject(object obj)
|
||||
{
|
||||
if (obj == null)
|
||||
{
|
||||
WriteNull();
|
||||
return;
|
||||
}
|
||||
if (obj is Asn1Object)
|
||||
{
|
||||
((Asn1Object)obj).Encode(this);
|
||||
return;
|
||||
}
|
||||
if (obj is Asn1Encodable)
|
||||
{
|
||||
((Asn1Encodable)obj).ToAsn1Object().Encode(this);
|
||||
return;
|
||||
}
|
||||
throw new IOException("object not BerEncodable");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1;
|
||||
|
||||
public class BerSequence : DerSequence
|
||||
{
|
||||
public new static readonly BerSequence Empty = new BerSequence();
|
||||
|
||||
public new static BerSequence FromVector(Asn1EncodableVector v)
|
||||
{
|
||||
if (v.Count >= 1)
|
||||
{
|
||||
return new BerSequence(v);
|
||||
}
|
||||
return Empty;
|
||||
}
|
||||
|
||||
public BerSequence()
|
||||
{
|
||||
}
|
||||
|
||||
public BerSequence(Asn1Encodable obj)
|
||||
: base(obj)
|
||||
{
|
||||
}
|
||||
|
||||
public BerSequence(params Asn1Encodable[] v)
|
||||
: base(v)
|
||||
{
|
||||
}
|
||||
|
||||
public BerSequence(Asn1EncodableVector v)
|
||||
: base(v)
|
||||
{
|
||||
}
|
||||
|
||||
internal override void Encode(DerOutputStream derOut)
|
||||
{
|
||||
if (derOut is Asn1OutputStream || derOut is BerOutputStream)
|
||||
{
|
||||
derOut.WriteByte(48);
|
||||
derOut.WriteByte(128);
|
||||
{
|
||||
IEnumerator enumerator = GetEnumerator();
|
||||
try
|
||||
{
|
||||
while (enumerator.MoveNext())
|
||||
{
|
||||
Asn1Encodable obj = (Asn1Encodable)enumerator.Current;
|
||||
derOut.WriteObject(obj);
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
IDisposable disposable = enumerator as IDisposable;
|
||||
if (disposable != null)
|
||||
{
|
||||
disposable.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
derOut.WriteByte(0);
|
||||
derOut.WriteByte(0);
|
||||
}
|
||||
else
|
||||
{
|
||||
base.Encode(derOut);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
using System.IO;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1;
|
||||
|
||||
public class BerSequenceGenerator : BerGenerator
|
||||
{
|
||||
public BerSequenceGenerator(Stream outStream)
|
||||
: base(outStream)
|
||||
{
|
||||
WriteBerHeader(48);
|
||||
}
|
||||
|
||||
public BerSequenceGenerator(Stream outStream, int tagNo, bool isExplicit)
|
||||
: base(outStream, tagNo, isExplicit)
|
||||
{
|
||||
WriteBerHeader(48);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
namespace Org.BouncyCastle.Asn1;
|
||||
|
||||
public class BerSequenceParser : Asn1SequenceParser, IAsn1Convertible
|
||||
{
|
||||
private readonly Asn1StreamParser _parser;
|
||||
|
||||
internal BerSequenceParser(Asn1StreamParser parser)
|
||||
{
|
||||
_parser = parser;
|
||||
}
|
||||
|
||||
public IAsn1Convertible ReadObject()
|
||||
{
|
||||
return _parser.ReadObject();
|
||||
}
|
||||
|
||||
public Asn1Object ToAsn1Object()
|
||||
{
|
||||
return new BerSequence(_parser.ReadVector());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1;
|
||||
|
||||
public class BerSet : DerSet
|
||||
{
|
||||
public new static readonly BerSet Empty = new BerSet();
|
||||
|
||||
public new static BerSet FromVector(Asn1EncodableVector v)
|
||||
{
|
||||
if (v.Count >= 1)
|
||||
{
|
||||
return new BerSet(v);
|
||||
}
|
||||
return Empty;
|
||||
}
|
||||
|
||||
internal new static BerSet FromVector(Asn1EncodableVector v, bool needsSorting)
|
||||
{
|
||||
if (v.Count >= 1)
|
||||
{
|
||||
return new BerSet(v, needsSorting);
|
||||
}
|
||||
return Empty;
|
||||
}
|
||||
|
||||
public BerSet()
|
||||
{
|
||||
}
|
||||
|
||||
public BerSet(Asn1Encodable obj)
|
||||
: base(obj)
|
||||
{
|
||||
}
|
||||
|
||||
public BerSet(Asn1EncodableVector v)
|
||||
: base(v, needsSorting: false)
|
||||
{
|
||||
}
|
||||
|
||||
internal BerSet(Asn1EncodableVector v, bool needsSorting)
|
||||
: base(v, needsSorting)
|
||||
{
|
||||
}
|
||||
|
||||
internal override void Encode(DerOutputStream derOut)
|
||||
{
|
||||
if (derOut is Asn1OutputStream || derOut is BerOutputStream)
|
||||
{
|
||||
derOut.WriteByte(49);
|
||||
derOut.WriteByte(128);
|
||||
{
|
||||
IEnumerator enumerator = GetEnumerator();
|
||||
try
|
||||
{
|
||||
while (enumerator.MoveNext())
|
||||
{
|
||||
Asn1Encodable obj = (Asn1Encodable)enumerator.Current;
|
||||
derOut.WriteObject(obj);
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
IDisposable disposable = enumerator as IDisposable;
|
||||
if (disposable != null)
|
||||
{
|
||||
disposable.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
derOut.WriteByte(0);
|
||||
derOut.WriteByte(0);
|
||||
}
|
||||
else
|
||||
{
|
||||
base.Encode(derOut);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
using System.IO;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1;
|
||||
|
||||
public class BerSetGenerator : BerGenerator
|
||||
{
|
||||
public BerSetGenerator(Stream outStream)
|
||||
: base(outStream)
|
||||
{
|
||||
WriteBerHeader(49);
|
||||
}
|
||||
|
||||
public BerSetGenerator(Stream outStream, int tagNo, bool isExplicit)
|
||||
: base(outStream, tagNo, isExplicit)
|
||||
{
|
||||
WriteBerHeader(49);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
namespace Org.BouncyCastle.Asn1;
|
||||
|
||||
public class BerSetParser : Asn1SetParser, IAsn1Convertible
|
||||
{
|
||||
private readonly Asn1StreamParser _parser;
|
||||
|
||||
internal BerSetParser(Asn1StreamParser parser)
|
||||
{
|
||||
_parser = parser;
|
||||
}
|
||||
|
||||
public IAsn1Convertible ReadObject()
|
||||
{
|
||||
return _parser.ReadObject();
|
||||
}
|
||||
|
||||
public Asn1Object ToAsn1Object()
|
||||
{
|
||||
return new BerSet(_parser.ReadVector(), needsSorting: false);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
using System.Collections;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1;
|
||||
|
||||
public class BerTaggedObject : DerTaggedObject
|
||||
{
|
||||
public BerTaggedObject(int tagNo, Asn1Encodable obj)
|
||||
: base(tagNo, obj)
|
||||
{
|
||||
}
|
||||
|
||||
public BerTaggedObject(bool explicitly, int tagNo, Asn1Encodable obj)
|
||||
: base(explicitly, tagNo, obj)
|
||||
{
|
||||
}
|
||||
|
||||
public BerTaggedObject(int tagNo)
|
||||
: base(explicitly: false, tagNo, BerSequence.Empty)
|
||||
{
|
||||
}
|
||||
|
||||
internal override void Encode(DerOutputStream derOut)
|
||||
{
|
||||
if (derOut is Asn1OutputStream || derOut is BerOutputStream)
|
||||
{
|
||||
derOut.WriteTag(160, tagNo);
|
||||
derOut.WriteByte(128);
|
||||
if (!IsEmpty())
|
||||
{
|
||||
if (!explicitly)
|
||||
{
|
||||
IEnumerable enumerable;
|
||||
if (obj is Asn1OctetString)
|
||||
{
|
||||
if (obj is BerOctetString)
|
||||
{
|
||||
enumerable = (BerOctetString)obj;
|
||||
}
|
||||
else
|
||||
{
|
||||
Asn1OctetString asn1OctetString = (Asn1OctetString)obj;
|
||||
enumerable = new BerOctetString(asn1OctetString.GetOctets());
|
||||
}
|
||||
}
|
||||
else if (obj is Asn1Sequence)
|
||||
{
|
||||
enumerable = (Asn1Sequence)obj;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!(obj is Asn1Set))
|
||||
{
|
||||
throw Platform.CreateNotImplementedException(Platform.GetTypeName(obj));
|
||||
}
|
||||
enumerable = (Asn1Set)obj;
|
||||
}
|
||||
foreach (Asn1Encodable item in enumerable)
|
||||
{
|
||||
derOut.WriteObject(item);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
derOut.WriteObject(obj);
|
||||
}
|
||||
}
|
||||
derOut.WriteByte(0);
|
||||
derOut.WriteByte(0);
|
||||
}
|
||||
else
|
||||
{
|
||||
base.Encode(derOut);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1;
|
||||
|
||||
public class BerTaggedObjectParser : Asn1TaggedObjectParser, IAsn1Convertible
|
||||
{
|
||||
private bool _constructed;
|
||||
|
||||
private int _tagNumber;
|
||||
|
||||
private Asn1StreamParser _parser;
|
||||
|
||||
public bool IsConstructed => _constructed;
|
||||
|
||||
public int TagNo => _tagNumber;
|
||||
|
||||
[Obsolete]
|
||||
internal BerTaggedObjectParser(int baseTag, int tagNumber, Stream contentStream)
|
||||
: this((baseTag & 0x20) != 0, tagNumber, new Asn1StreamParser(contentStream))
|
||||
{
|
||||
}
|
||||
|
||||
internal BerTaggedObjectParser(bool constructed, int tagNumber, Asn1StreamParser parser)
|
||||
{
|
||||
_constructed = constructed;
|
||||
_tagNumber = tagNumber;
|
||||
_parser = parser;
|
||||
}
|
||||
|
||||
public IAsn1Convertible GetObjectParser(int tag, bool isExplicit)
|
||||
{
|
||||
if (isExplicit)
|
||||
{
|
||||
if (!_constructed)
|
||||
{
|
||||
throw new IOException("Explicit tags must be constructed (see X.690 8.14.2)");
|
||||
}
|
||||
return _parser.ReadObject();
|
||||
}
|
||||
return _parser.ReadImplicit(_constructed, tag);
|
||||
}
|
||||
|
||||
public Asn1Object ToAsn1Object()
|
||||
{
|
||||
try
|
||||
{
|
||||
return _parser.ReadTaggedObject(_constructed, _tagNumber);
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
throw new Asn1ParsingException(ex.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
namespace Org.BouncyCastle.Asn1.Bsi;
|
||||
|
||||
public abstract class BsiObjectIdentifiers
|
||||
{
|
||||
public static readonly DerObjectIdentifier bsi_de = new DerObjectIdentifier("0.4.0.127.0.7");
|
||||
|
||||
public static readonly DerObjectIdentifier id_ecc = bsi_de.Branch("1.1");
|
||||
|
||||
public static readonly DerObjectIdentifier ecdsa_plain_signatures = id_ecc.Branch("4.1");
|
||||
|
||||
public static readonly DerObjectIdentifier ecdsa_plain_SHA1 = ecdsa_plain_signatures.Branch("1");
|
||||
|
||||
public static readonly DerObjectIdentifier ecdsa_plain_SHA224 = ecdsa_plain_signatures.Branch("2");
|
||||
|
||||
public static readonly DerObjectIdentifier ecdsa_plain_SHA256 = ecdsa_plain_signatures.Branch("3");
|
||||
|
||||
public static readonly DerObjectIdentifier ecdsa_plain_SHA384 = ecdsa_plain_signatures.Branch("4");
|
||||
|
||||
public static readonly DerObjectIdentifier ecdsa_plain_SHA512 = ecdsa_plain_signatures.Branch("5");
|
||||
|
||||
public static readonly DerObjectIdentifier ecdsa_plain_RIPEMD160 = ecdsa_plain_signatures.Branch("6");
|
||||
|
||||
public static readonly DerObjectIdentifier algorithm = bsi_de.Branch("1");
|
||||
|
||||
public static readonly DerObjectIdentifier ecka_eg = id_ecc.Branch("5.1");
|
||||
|
||||
public static readonly DerObjectIdentifier ecka_eg_X963kdf = ecka_eg.Branch("1");
|
||||
|
||||
public static readonly DerObjectIdentifier ecka_eg_X963kdf_SHA1 = ecka_eg_X963kdf.Branch("1");
|
||||
|
||||
public static readonly DerObjectIdentifier ecka_eg_X963kdf_SHA224 = ecka_eg_X963kdf.Branch("2");
|
||||
|
||||
public static readonly DerObjectIdentifier ecka_eg_X963kdf_SHA256 = ecka_eg_X963kdf.Branch("3");
|
||||
|
||||
public static readonly DerObjectIdentifier ecka_eg_X963kdf_SHA384 = ecka_eg_X963kdf.Branch("4");
|
||||
|
||||
public static readonly DerObjectIdentifier ecka_eg_X963kdf_SHA512 = ecka_eg_X963kdf.Branch("5");
|
||||
|
||||
public static readonly DerObjectIdentifier ecka_eg_X963kdf_RIPEMD160 = ecka_eg_X963kdf.Branch("6");
|
||||
|
||||
public static readonly DerObjectIdentifier ecka_eg_SessionKDF = ecka_eg.Branch("2");
|
||||
|
||||
public static readonly DerObjectIdentifier ecka_eg_SessionKDF_3DES = ecka_eg_SessionKDF.Branch("1");
|
||||
|
||||
public static readonly DerObjectIdentifier ecka_eg_SessionKDF_AES128 = ecka_eg_SessionKDF.Branch("2");
|
||||
|
||||
public static readonly DerObjectIdentifier ecka_eg_SessionKDF_AES192 = ecka_eg_SessionKDF.Branch("3");
|
||||
|
||||
public static readonly DerObjectIdentifier ecka_eg_SessionKDF_AES256 = ecka_eg_SessionKDF.Branch("4");
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1.Cmp;
|
||||
|
||||
public class CAKeyUpdAnnContent : Asn1Encodable
|
||||
{
|
||||
private readonly CmpCertificate oldWithNew;
|
||||
|
||||
private readonly CmpCertificate newWithOld;
|
||||
|
||||
private readonly CmpCertificate newWithNew;
|
||||
|
||||
public virtual CmpCertificate OldWithNew => oldWithNew;
|
||||
|
||||
public virtual CmpCertificate NewWithOld => newWithOld;
|
||||
|
||||
public virtual CmpCertificate NewWithNew => newWithNew;
|
||||
|
||||
private CAKeyUpdAnnContent(Asn1Sequence seq)
|
||||
{
|
||||
oldWithNew = CmpCertificate.GetInstance(seq[0]);
|
||||
newWithOld = CmpCertificate.GetInstance(seq[1]);
|
||||
newWithNew = CmpCertificate.GetInstance(seq[2]);
|
||||
}
|
||||
|
||||
public static CAKeyUpdAnnContent GetInstance(object obj)
|
||||
{
|
||||
if (obj is CAKeyUpdAnnContent)
|
||||
{
|
||||
return (CAKeyUpdAnnContent)obj;
|
||||
}
|
||||
if (obj is Asn1Sequence)
|
||||
{
|
||||
return new CAKeyUpdAnnContent((Asn1Sequence)obj);
|
||||
}
|
||||
throw new ArgumentException("Invalid object: " + Platform.GetTypeName(obj), "obj");
|
||||
}
|
||||
|
||||
public override Asn1Object ToAsn1Object()
|
||||
{
|
||||
return new DerSequence(oldWithNew, newWithOld, newWithNew);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1.Cmp;
|
||||
|
||||
public class CertConfirmContent : Asn1Encodable
|
||||
{
|
||||
private readonly Asn1Sequence content;
|
||||
|
||||
private CertConfirmContent(Asn1Sequence seq)
|
||||
{
|
||||
content = seq;
|
||||
}
|
||||
|
||||
public static CertConfirmContent GetInstance(object obj)
|
||||
{
|
||||
if (obj is CertConfirmContent)
|
||||
{
|
||||
return (CertConfirmContent)obj;
|
||||
}
|
||||
if (obj is Asn1Sequence)
|
||||
{
|
||||
return new CertConfirmContent((Asn1Sequence)obj);
|
||||
}
|
||||
throw new ArgumentException("Invalid object: " + Platform.GetTypeName(obj), "obj");
|
||||
}
|
||||
|
||||
public virtual CertStatus[] ToCertStatusArray()
|
||||
{
|
||||
CertStatus[] array = new CertStatus[content.Count];
|
||||
for (int i = 0; i != array.Length; i++)
|
||||
{
|
||||
array[i] = CertStatus.GetInstance(content[i]);
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public override Asn1Object ToAsn1Object()
|
||||
{
|
||||
return content;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Asn1.Crmf;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1.Cmp;
|
||||
|
||||
public class CertOrEncCert : Asn1Encodable, IAsn1Choice
|
||||
{
|
||||
private readonly CmpCertificate certificate;
|
||||
|
||||
private readonly EncryptedValue encryptedCert;
|
||||
|
||||
public virtual CmpCertificate Certificate => certificate;
|
||||
|
||||
public virtual EncryptedValue EncryptedCert => encryptedCert;
|
||||
|
||||
private CertOrEncCert(Asn1TaggedObject tagged)
|
||||
{
|
||||
if (tagged.TagNo == 0)
|
||||
{
|
||||
certificate = CmpCertificate.GetInstance(tagged.GetObject());
|
||||
return;
|
||||
}
|
||||
if (tagged.TagNo == 1)
|
||||
{
|
||||
encryptedCert = EncryptedValue.GetInstance(tagged.GetObject());
|
||||
return;
|
||||
}
|
||||
throw new ArgumentException("unknown tag: " + tagged.TagNo, "tagged");
|
||||
}
|
||||
|
||||
public static CertOrEncCert GetInstance(object obj)
|
||||
{
|
||||
if (obj is CertOrEncCert)
|
||||
{
|
||||
return (CertOrEncCert)obj;
|
||||
}
|
||||
if (obj is Asn1TaggedObject)
|
||||
{
|
||||
return new CertOrEncCert((Asn1TaggedObject)obj);
|
||||
}
|
||||
throw new ArgumentException("Invalid object: " + Platform.GetTypeName(obj), "obj");
|
||||
}
|
||||
|
||||
public CertOrEncCert(CmpCertificate certificate)
|
||||
{
|
||||
if (certificate == null)
|
||||
{
|
||||
throw new ArgumentNullException("certificate");
|
||||
}
|
||||
this.certificate = certificate;
|
||||
}
|
||||
|
||||
public CertOrEncCert(EncryptedValue encryptedCert)
|
||||
{
|
||||
if (encryptedCert == null)
|
||||
{
|
||||
throw new ArgumentNullException("encryptedCert");
|
||||
}
|
||||
this.encryptedCert = encryptedCert;
|
||||
}
|
||||
|
||||
public override Asn1Object ToAsn1Object()
|
||||
{
|
||||
if (certificate != null)
|
||||
{
|
||||
return new DerTaggedObject(explicitly: true, 0, certificate);
|
||||
}
|
||||
return new DerTaggedObject(explicitly: true, 1, encryptedCert);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1.Cmp;
|
||||
|
||||
public class CertRepMessage : Asn1Encodable
|
||||
{
|
||||
private readonly Asn1Sequence caPubs;
|
||||
|
||||
private readonly Asn1Sequence response;
|
||||
|
||||
private CertRepMessage(Asn1Sequence seq)
|
||||
{
|
||||
int index = 0;
|
||||
if (seq.Count > 1)
|
||||
{
|
||||
caPubs = Asn1Sequence.GetInstance((Asn1TaggedObject)seq[index++], explicitly: true);
|
||||
}
|
||||
response = Asn1Sequence.GetInstance(seq[index]);
|
||||
}
|
||||
|
||||
public static CertRepMessage GetInstance(object obj)
|
||||
{
|
||||
if (obj is CertRepMessage)
|
||||
{
|
||||
return (CertRepMessage)obj;
|
||||
}
|
||||
if (obj is Asn1Sequence)
|
||||
{
|
||||
return new CertRepMessage((Asn1Sequence)obj);
|
||||
}
|
||||
throw new ArgumentException("Invalid object: " + Platform.GetTypeName(obj), "obj");
|
||||
}
|
||||
|
||||
public CertRepMessage(CmpCertificate[] caPubs, CertResponse[] response)
|
||||
{
|
||||
if (response == null)
|
||||
{
|
||||
throw new ArgumentNullException("response");
|
||||
}
|
||||
if (caPubs != null)
|
||||
{
|
||||
this.caPubs = new DerSequence(caPubs);
|
||||
}
|
||||
this.response = new DerSequence(response);
|
||||
}
|
||||
|
||||
public virtual CmpCertificate[] GetCAPubs()
|
||||
{
|
||||
if (caPubs == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
CmpCertificate[] array = new CmpCertificate[caPubs.Count];
|
||||
for (int i = 0; i != array.Length; i++)
|
||||
{
|
||||
array[i] = CmpCertificate.GetInstance(caPubs[i]);
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public virtual CertResponse[] GetResponse()
|
||||
{
|
||||
CertResponse[] array = new CertResponse[response.Count];
|
||||
for (int i = 0; i != array.Length; i++)
|
||||
{
|
||||
array[i] = CertResponse.GetInstance(response[i]);
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public override Asn1Object ToAsn1Object()
|
||||
{
|
||||
Asn1EncodableVector asn1EncodableVector = new Asn1EncodableVector();
|
||||
if (caPubs != null)
|
||||
{
|
||||
asn1EncodableVector.Add(new DerTaggedObject(explicitly: true, 1, caPubs));
|
||||
}
|
||||
asn1EncodableVector.Add(response);
|
||||
return new DerSequence(asn1EncodableVector);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1.Cmp;
|
||||
|
||||
public class CertResponse : Asn1Encodable
|
||||
{
|
||||
private readonly DerInteger certReqId;
|
||||
|
||||
private readonly PkiStatusInfo status;
|
||||
|
||||
private readonly CertifiedKeyPair certifiedKeyPair;
|
||||
|
||||
private readonly Asn1OctetString rspInfo;
|
||||
|
||||
public virtual DerInteger CertReqID => certReqId;
|
||||
|
||||
public virtual PkiStatusInfo Status => status;
|
||||
|
||||
public virtual CertifiedKeyPair CertifiedKeyPair => certifiedKeyPair;
|
||||
|
||||
private CertResponse(Asn1Sequence seq)
|
||||
{
|
||||
certReqId = DerInteger.GetInstance(seq[0]);
|
||||
status = PkiStatusInfo.GetInstance(seq[1]);
|
||||
if (seq.Count < 3)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (seq.Count == 3)
|
||||
{
|
||||
Asn1Encodable asn1Encodable = seq[2];
|
||||
if (asn1Encodable is Asn1OctetString)
|
||||
{
|
||||
rspInfo = Asn1OctetString.GetInstance(asn1Encodable);
|
||||
}
|
||||
else
|
||||
{
|
||||
certifiedKeyPair = CertifiedKeyPair.GetInstance(asn1Encodable);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
certifiedKeyPair = CertifiedKeyPair.GetInstance(seq[2]);
|
||||
rspInfo = Asn1OctetString.GetInstance(seq[3]);
|
||||
}
|
||||
}
|
||||
|
||||
public static CertResponse GetInstance(object obj)
|
||||
{
|
||||
if (obj is CertResponse)
|
||||
{
|
||||
return (CertResponse)obj;
|
||||
}
|
||||
if (obj is Asn1Sequence)
|
||||
{
|
||||
return new CertResponse((Asn1Sequence)obj);
|
||||
}
|
||||
throw new ArgumentException("Invalid object: " + Platform.GetTypeName(obj), "obj");
|
||||
}
|
||||
|
||||
public CertResponse(DerInteger certReqId, PkiStatusInfo status)
|
||||
: this(certReqId, status, null, null)
|
||||
{
|
||||
}
|
||||
|
||||
public CertResponse(DerInteger certReqId, PkiStatusInfo status, CertifiedKeyPair certifiedKeyPair, Asn1OctetString rspInfo)
|
||||
{
|
||||
if (certReqId == null)
|
||||
{
|
||||
throw new ArgumentNullException("certReqId");
|
||||
}
|
||||
if (status == null)
|
||||
{
|
||||
throw new ArgumentNullException("status");
|
||||
}
|
||||
this.certReqId = certReqId;
|
||||
this.status = status;
|
||||
this.certifiedKeyPair = certifiedKeyPair;
|
||||
this.rspInfo = rspInfo;
|
||||
}
|
||||
|
||||
public override Asn1Object ToAsn1Object()
|
||||
{
|
||||
Asn1EncodableVector asn1EncodableVector = new Asn1EncodableVector(certReqId, status);
|
||||
asn1EncodableVector.AddOptional(certifiedKeyPair, rspInfo);
|
||||
return new DerSequence(asn1EncodableVector);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Math;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1.Cmp;
|
||||
|
||||
public class CertStatus : Asn1Encodable
|
||||
{
|
||||
private readonly Asn1OctetString certHash;
|
||||
|
||||
private readonly DerInteger certReqId;
|
||||
|
||||
private readonly PkiStatusInfo statusInfo;
|
||||
|
||||
public virtual Asn1OctetString CertHash => certHash;
|
||||
|
||||
public virtual DerInteger CertReqID => certReqId;
|
||||
|
||||
public virtual PkiStatusInfo StatusInfo => statusInfo;
|
||||
|
||||
private CertStatus(Asn1Sequence seq)
|
||||
{
|
||||
certHash = Asn1OctetString.GetInstance(seq[0]);
|
||||
certReqId = DerInteger.GetInstance(seq[1]);
|
||||
if (seq.Count > 2)
|
||||
{
|
||||
statusInfo = PkiStatusInfo.GetInstance(seq[2]);
|
||||
}
|
||||
}
|
||||
|
||||
public CertStatus(byte[] certHash, BigInteger certReqId)
|
||||
{
|
||||
this.certHash = new DerOctetString(certHash);
|
||||
this.certReqId = new DerInteger(certReqId);
|
||||
}
|
||||
|
||||
public CertStatus(byte[] certHash, BigInteger certReqId, PkiStatusInfo statusInfo)
|
||||
{
|
||||
this.certHash = new DerOctetString(certHash);
|
||||
this.certReqId = new DerInteger(certReqId);
|
||||
this.statusInfo = statusInfo;
|
||||
}
|
||||
|
||||
public static CertStatus GetInstance(object obj)
|
||||
{
|
||||
if (obj is CertStatus)
|
||||
{
|
||||
return (CertStatus)obj;
|
||||
}
|
||||
if (obj is Asn1Sequence)
|
||||
{
|
||||
return new CertStatus((Asn1Sequence)obj);
|
||||
}
|
||||
throw new ArgumentException("Invalid object: " + Platform.GetTypeName(obj), "obj");
|
||||
}
|
||||
|
||||
public override Asn1Object ToAsn1Object()
|
||||
{
|
||||
Asn1EncodableVector asn1EncodableVector = new Asn1EncodableVector(certHash, certReqId);
|
||||
asn1EncodableVector.AddOptional(statusInfo);
|
||||
return new DerSequence(asn1EncodableVector);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Asn1.Crmf;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1.Cmp;
|
||||
|
||||
public class CertifiedKeyPair : Asn1Encodable
|
||||
{
|
||||
private readonly CertOrEncCert certOrEncCert;
|
||||
|
||||
private readonly EncryptedValue privateKey;
|
||||
|
||||
private readonly PkiPublicationInfo publicationInfo;
|
||||
|
||||
public virtual CertOrEncCert CertOrEncCert => certOrEncCert;
|
||||
|
||||
public virtual EncryptedValue PrivateKey => privateKey;
|
||||
|
||||
public virtual PkiPublicationInfo PublicationInfo => publicationInfo;
|
||||
|
||||
private CertifiedKeyPair(Asn1Sequence seq)
|
||||
{
|
||||
certOrEncCert = CertOrEncCert.GetInstance(seq[0]);
|
||||
if (seq.Count < 2)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (seq.Count == 2)
|
||||
{
|
||||
Asn1TaggedObject instance = Asn1TaggedObject.GetInstance(seq[1]);
|
||||
if (instance.TagNo == 0)
|
||||
{
|
||||
privateKey = EncryptedValue.GetInstance(instance.GetObject());
|
||||
}
|
||||
else
|
||||
{
|
||||
publicationInfo = PkiPublicationInfo.GetInstance(instance.GetObject());
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
privateKey = EncryptedValue.GetInstance(Asn1TaggedObject.GetInstance(seq[1]));
|
||||
publicationInfo = PkiPublicationInfo.GetInstance(Asn1TaggedObject.GetInstance(seq[2]));
|
||||
}
|
||||
}
|
||||
|
||||
public static CertifiedKeyPair GetInstance(object obj)
|
||||
{
|
||||
if (obj is CertifiedKeyPair)
|
||||
{
|
||||
return (CertifiedKeyPair)obj;
|
||||
}
|
||||
if (obj is Asn1Sequence)
|
||||
{
|
||||
return new CertifiedKeyPair((Asn1Sequence)obj);
|
||||
}
|
||||
throw new ArgumentException("Invalid object: " + Platform.GetTypeName(obj), "obj");
|
||||
}
|
||||
|
||||
public CertifiedKeyPair(CertOrEncCert certOrEncCert)
|
||||
: this(certOrEncCert, null, null)
|
||||
{
|
||||
}
|
||||
|
||||
public CertifiedKeyPair(CertOrEncCert certOrEncCert, EncryptedValue privateKey, PkiPublicationInfo publicationInfo)
|
||||
{
|
||||
if (certOrEncCert == null)
|
||||
{
|
||||
throw new ArgumentNullException("certOrEncCert");
|
||||
}
|
||||
this.certOrEncCert = certOrEncCert;
|
||||
this.privateKey = privateKey;
|
||||
this.publicationInfo = publicationInfo;
|
||||
}
|
||||
|
||||
public override Asn1Object ToAsn1Object()
|
||||
{
|
||||
Asn1EncodableVector asn1EncodableVector = new Asn1EncodableVector(certOrEncCert);
|
||||
if (privateKey != null)
|
||||
{
|
||||
asn1EncodableVector.Add(new DerTaggedObject(explicitly: true, 0, privateKey));
|
||||
}
|
||||
if (publicationInfo != null)
|
||||
{
|
||||
asn1EncodableVector.Add(new DerTaggedObject(explicitly: true, 1, publicationInfo));
|
||||
}
|
||||
return new DerSequence(asn1EncodableVector);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Asn1.X509;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1.Cmp;
|
||||
|
||||
public class Challenge : Asn1Encodable
|
||||
{
|
||||
private readonly AlgorithmIdentifier owf;
|
||||
|
||||
private readonly Asn1OctetString witness;
|
||||
|
||||
private readonly Asn1OctetString challenge;
|
||||
|
||||
public virtual AlgorithmIdentifier Owf => owf;
|
||||
|
||||
private Challenge(Asn1Sequence seq)
|
||||
{
|
||||
int index = 0;
|
||||
if (seq.Count == 3)
|
||||
{
|
||||
owf = AlgorithmIdentifier.GetInstance(seq[index++]);
|
||||
}
|
||||
witness = Asn1OctetString.GetInstance(seq[index++]);
|
||||
challenge = Asn1OctetString.GetInstance(seq[index]);
|
||||
}
|
||||
|
||||
public static Challenge GetInstance(object obj)
|
||||
{
|
||||
if (obj is Challenge)
|
||||
{
|
||||
return (Challenge)obj;
|
||||
}
|
||||
if (obj is Asn1Sequence)
|
||||
{
|
||||
return new Challenge((Asn1Sequence)obj);
|
||||
}
|
||||
throw new ArgumentException("Invalid object: " + Platform.GetTypeName(obj), "obj");
|
||||
}
|
||||
|
||||
public override Asn1Object ToAsn1Object()
|
||||
{
|
||||
Asn1EncodableVector asn1EncodableVector = new Asn1EncodableVector();
|
||||
asn1EncodableVector.AddOptional(owf);
|
||||
asn1EncodableVector.Add(witness);
|
||||
asn1EncodableVector.Add(challenge);
|
||||
return new DerSequence(asn1EncodableVector);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Asn1.X509;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1.Cmp;
|
||||
|
||||
public class CmpCertificate : Asn1Encodable, IAsn1Choice
|
||||
{
|
||||
private readonly X509CertificateStructure x509v3PKCert;
|
||||
|
||||
private readonly AttributeCertificate x509v2AttrCert;
|
||||
|
||||
public virtual bool IsX509v3PKCert => x509v3PKCert != null;
|
||||
|
||||
public virtual X509CertificateStructure X509v3PKCert => x509v3PKCert;
|
||||
|
||||
public virtual AttributeCertificate X509v2AttrCert => x509v2AttrCert;
|
||||
|
||||
public CmpCertificate(AttributeCertificate x509v2AttrCert)
|
||||
{
|
||||
this.x509v2AttrCert = x509v2AttrCert;
|
||||
}
|
||||
|
||||
public CmpCertificate(X509CertificateStructure x509v3PKCert)
|
||||
{
|
||||
if (x509v3PKCert.Version != 3)
|
||||
{
|
||||
throw new ArgumentException("only version 3 certificates allowed", "x509v3PKCert");
|
||||
}
|
||||
this.x509v3PKCert = x509v3PKCert;
|
||||
}
|
||||
|
||||
public static CmpCertificate GetInstance(object obj)
|
||||
{
|
||||
if (obj is CmpCertificate)
|
||||
{
|
||||
return (CmpCertificate)obj;
|
||||
}
|
||||
if (obj is Asn1Sequence)
|
||||
{
|
||||
return new CmpCertificate(X509CertificateStructure.GetInstance(obj));
|
||||
}
|
||||
if (obj is Asn1TaggedObject)
|
||||
{
|
||||
return new CmpCertificate(AttributeCertificate.GetInstance(((Asn1TaggedObject)obj).GetObject()));
|
||||
}
|
||||
throw new ArgumentException("Invalid object: " + Platform.GetTypeName(obj), "obj");
|
||||
}
|
||||
|
||||
public override Asn1Object ToAsn1Object()
|
||||
{
|
||||
if (x509v2AttrCert != null)
|
||||
{
|
||||
return new DerTaggedObject(explicitly: true, 1, x509v2AttrCert);
|
||||
}
|
||||
return x509v3PKCert.ToAsn1Object();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
namespace Org.BouncyCastle.Asn1.Cmp;
|
||||
|
||||
public abstract class CmpObjectIdentifiers
|
||||
{
|
||||
public static readonly DerObjectIdentifier passwordBasedMac = new DerObjectIdentifier("1.2.840.113533.7.66.13");
|
||||
|
||||
public static readonly DerObjectIdentifier dhBasedMac = new DerObjectIdentifier("1.2.840.113533.7.66.30");
|
||||
|
||||
public static readonly DerObjectIdentifier it_caProtEncCert = new DerObjectIdentifier("1.3.6.1.5.5.7.4.1");
|
||||
|
||||
public static readonly DerObjectIdentifier it_signKeyPairTypes = new DerObjectIdentifier("1.3.6.1.5.5.7.4.2");
|
||||
|
||||
public static readonly DerObjectIdentifier it_encKeyPairTypes = new DerObjectIdentifier("1.3.6.1.5.5.7.4.3");
|
||||
|
||||
public static readonly DerObjectIdentifier it_preferredSymAlg = new DerObjectIdentifier("1.3.6.1.5.5.7.4.4");
|
||||
|
||||
public static readonly DerObjectIdentifier it_caKeyUpdateInfo = new DerObjectIdentifier("1.3.6.1.5.5.7.4.5");
|
||||
|
||||
public static readonly DerObjectIdentifier it_currentCRL = new DerObjectIdentifier("1.3.6.1.5.5.7.4.6");
|
||||
|
||||
public static readonly DerObjectIdentifier it_unsupportedOIDs = new DerObjectIdentifier("1.3.6.1.5.5.7.4.7");
|
||||
|
||||
public static readonly DerObjectIdentifier it_keyPairParamReq = new DerObjectIdentifier("1.3.6.1.5.5.7.4.10");
|
||||
|
||||
public static readonly DerObjectIdentifier it_keyPairParamRep = new DerObjectIdentifier("1.3.6.1.5.5.7.4.11");
|
||||
|
||||
public static readonly DerObjectIdentifier it_revPassphrase = new DerObjectIdentifier("1.3.6.1.5.5.7.4.12");
|
||||
|
||||
public static readonly DerObjectIdentifier it_implicitConfirm = new DerObjectIdentifier("1.3.6.1.5.5.7.4.13");
|
||||
|
||||
public static readonly DerObjectIdentifier it_confirmWaitTime = new DerObjectIdentifier("1.3.6.1.5.5.7.4.14");
|
||||
|
||||
public static readonly DerObjectIdentifier it_origPKIMessage = new DerObjectIdentifier("1.3.6.1.5.5.7.4.15");
|
||||
|
||||
public static readonly DerObjectIdentifier it_suppLangTags = new DerObjectIdentifier("1.3.6.1.5.5.7.4.16");
|
||||
|
||||
public static readonly DerObjectIdentifier regCtrl_regToken = new DerObjectIdentifier("1.3.6.1.5.5.7.5.1.1");
|
||||
|
||||
public static readonly DerObjectIdentifier regCtrl_authenticator = new DerObjectIdentifier("1.3.6.1.5.5.7.5.1.2");
|
||||
|
||||
public static readonly DerObjectIdentifier regCtrl_pkiPublicationInfo = new DerObjectIdentifier("1.3.6.1.5.5.7.5.1.3");
|
||||
|
||||
public static readonly DerObjectIdentifier regCtrl_pkiArchiveOptions = new DerObjectIdentifier("1.3.6.1.5.5.7.5.1.4");
|
||||
|
||||
public static readonly DerObjectIdentifier regCtrl_oldCertID = new DerObjectIdentifier("1.3.6.1.5.5.7.5.1.5");
|
||||
|
||||
public static readonly DerObjectIdentifier regCtrl_protocolEncrKey = new DerObjectIdentifier("1.3.6.1.5.5.7.5.1.6");
|
||||
|
||||
public static readonly DerObjectIdentifier regCtrl_altCertTemplate = new DerObjectIdentifier("1.3.6.1.5.5.7.5.1.7");
|
||||
|
||||
public static readonly DerObjectIdentifier regInfo_utf8Pairs = new DerObjectIdentifier("1.3.6.1.5.5.7.5.2.1");
|
||||
|
||||
public static readonly DerObjectIdentifier regInfo_certReq = new DerObjectIdentifier("1.3.6.1.5.5.7.5.2.2");
|
||||
|
||||
public static readonly DerObjectIdentifier ct_encKeyWithID = new DerObjectIdentifier("1.2.840.113549.1.9.16.1.21");
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Asn1.X509;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1.Cmp;
|
||||
|
||||
public class CrlAnnContent : Asn1Encodable
|
||||
{
|
||||
private readonly Asn1Sequence content;
|
||||
|
||||
private CrlAnnContent(Asn1Sequence seq)
|
||||
{
|
||||
content = seq;
|
||||
}
|
||||
|
||||
public static CrlAnnContent GetInstance(object obj)
|
||||
{
|
||||
if (obj is CrlAnnContent)
|
||||
{
|
||||
return (CrlAnnContent)obj;
|
||||
}
|
||||
if (obj is Asn1Sequence)
|
||||
{
|
||||
return new CrlAnnContent((Asn1Sequence)obj);
|
||||
}
|
||||
throw new ArgumentException("Invalid object: " + Platform.GetTypeName(obj), "obj");
|
||||
}
|
||||
|
||||
public virtual CertificateList[] ToCertificateListArray()
|
||||
{
|
||||
CertificateList[] array = new CertificateList[content.Count];
|
||||
for (int i = 0; i != array.Length; i++)
|
||||
{
|
||||
array[i] = CertificateList.GetInstance(content[i]);
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public override Asn1Object ToAsn1Object()
|
||||
{
|
||||
return content;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1.Cmp;
|
||||
|
||||
public class ErrorMsgContent : Asn1Encodable
|
||||
{
|
||||
private readonly PkiStatusInfo pkiStatusInfo;
|
||||
|
||||
private readonly DerInteger errorCode;
|
||||
|
||||
private readonly PkiFreeText errorDetails;
|
||||
|
||||
public virtual PkiStatusInfo PkiStatusInfo => pkiStatusInfo;
|
||||
|
||||
public virtual DerInteger ErrorCode => errorCode;
|
||||
|
||||
public virtual PkiFreeText ErrorDetails => errorDetails;
|
||||
|
||||
private ErrorMsgContent(Asn1Sequence seq)
|
||||
{
|
||||
pkiStatusInfo = PkiStatusInfo.GetInstance(seq[0]);
|
||||
for (int i = 1; i < seq.Count; i++)
|
||||
{
|
||||
Asn1Encodable asn1Encodable = seq[i];
|
||||
if (asn1Encodable is DerInteger)
|
||||
{
|
||||
errorCode = DerInteger.GetInstance(asn1Encodable);
|
||||
}
|
||||
else
|
||||
{
|
||||
errorDetails = PkiFreeText.GetInstance(asn1Encodable);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static ErrorMsgContent GetInstance(object obj)
|
||||
{
|
||||
if (obj is ErrorMsgContent)
|
||||
{
|
||||
return (ErrorMsgContent)obj;
|
||||
}
|
||||
if (obj is Asn1Sequence)
|
||||
{
|
||||
return new ErrorMsgContent((Asn1Sequence)obj);
|
||||
}
|
||||
throw new ArgumentException("Invalid object: " + Platform.GetTypeName(obj), "obj");
|
||||
}
|
||||
|
||||
public ErrorMsgContent(PkiStatusInfo pkiStatusInfo)
|
||||
: this(pkiStatusInfo, null, null)
|
||||
{
|
||||
}
|
||||
|
||||
public ErrorMsgContent(PkiStatusInfo pkiStatusInfo, DerInteger errorCode, PkiFreeText errorDetails)
|
||||
{
|
||||
if (pkiStatusInfo == null)
|
||||
{
|
||||
throw new ArgumentNullException("pkiStatusInfo");
|
||||
}
|
||||
this.pkiStatusInfo = pkiStatusInfo;
|
||||
this.errorCode = errorCode;
|
||||
this.errorDetails = errorDetails;
|
||||
}
|
||||
|
||||
public override Asn1Object ToAsn1Object()
|
||||
{
|
||||
Asn1EncodableVector asn1EncodableVector = new Asn1EncodableVector(pkiStatusInfo);
|
||||
asn1EncodableVector.AddOptional(errorCode, errorDetails);
|
||||
return new DerSequence(asn1EncodableVector);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1.Cmp;
|
||||
|
||||
public class GenMsgContent : Asn1Encodable
|
||||
{
|
||||
private readonly Asn1Sequence content;
|
||||
|
||||
private GenMsgContent(Asn1Sequence seq)
|
||||
{
|
||||
content = seq;
|
||||
}
|
||||
|
||||
public static GenMsgContent GetInstance(object obj)
|
||||
{
|
||||
if (obj is GenMsgContent)
|
||||
{
|
||||
return (GenMsgContent)obj;
|
||||
}
|
||||
if (obj is Asn1Sequence)
|
||||
{
|
||||
return new GenMsgContent((Asn1Sequence)obj);
|
||||
}
|
||||
throw new ArgumentException("Invalid object: " + Platform.GetTypeName(obj), "obj");
|
||||
}
|
||||
|
||||
public GenMsgContent(params InfoTypeAndValue[] itv)
|
||||
{
|
||||
content = new DerSequence(itv);
|
||||
}
|
||||
|
||||
public virtual InfoTypeAndValue[] ToInfoTypeAndValueArray()
|
||||
{
|
||||
InfoTypeAndValue[] array = new InfoTypeAndValue[content.Count];
|
||||
for (int i = 0; i != array.Length; i++)
|
||||
{
|
||||
array[i] = InfoTypeAndValue.GetInstance(content[i]);
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public override Asn1Object ToAsn1Object()
|
||||
{
|
||||
return content;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1.Cmp;
|
||||
|
||||
public class GenRepContent : Asn1Encodable
|
||||
{
|
||||
private readonly Asn1Sequence content;
|
||||
|
||||
private GenRepContent(Asn1Sequence seq)
|
||||
{
|
||||
content = seq;
|
||||
}
|
||||
|
||||
public static GenRepContent GetInstance(object obj)
|
||||
{
|
||||
if (obj is GenRepContent)
|
||||
{
|
||||
return (GenRepContent)obj;
|
||||
}
|
||||
if (obj is Asn1Sequence)
|
||||
{
|
||||
return new GenRepContent((Asn1Sequence)obj);
|
||||
}
|
||||
throw new ArgumentException("Invalid object: " + Platform.GetTypeName(obj), "obj");
|
||||
}
|
||||
|
||||
public GenRepContent(params InfoTypeAndValue[] itv)
|
||||
{
|
||||
content = new DerSequence(itv);
|
||||
}
|
||||
|
||||
public virtual InfoTypeAndValue[] ToInfoTypeAndValueArray()
|
||||
{
|
||||
InfoTypeAndValue[] array = new InfoTypeAndValue[content.Count];
|
||||
for (int i = 0; i != array.Length; i++)
|
||||
{
|
||||
array[i] = InfoTypeAndValue.GetInstance(content[i]);
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public override Asn1Object ToAsn1Object()
|
||||
{
|
||||
return content;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1.Cmp;
|
||||
|
||||
public class InfoTypeAndValue : Asn1Encodable
|
||||
{
|
||||
private readonly DerObjectIdentifier infoType;
|
||||
|
||||
private readonly Asn1Encodable infoValue;
|
||||
|
||||
public virtual DerObjectIdentifier InfoType => infoType;
|
||||
|
||||
public virtual Asn1Encodable InfoValue => infoValue;
|
||||
|
||||
private InfoTypeAndValue(Asn1Sequence seq)
|
||||
{
|
||||
infoType = DerObjectIdentifier.GetInstance(seq[0]);
|
||||
if (seq.Count > 1)
|
||||
{
|
||||
infoValue = seq[1];
|
||||
}
|
||||
}
|
||||
|
||||
public static InfoTypeAndValue GetInstance(object obj)
|
||||
{
|
||||
if (obj is InfoTypeAndValue)
|
||||
{
|
||||
return (InfoTypeAndValue)obj;
|
||||
}
|
||||
if (obj is Asn1Sequence)
|
||||
{
|
||||
return new InfoTypeAndValue((Asn1Sequence)obj);
|
||||
}
|
||||
throw new ArgumentException("Invalid object: " + Platform.GetTypeName(obj), "obj");
|
||||
}
|
||||
|
||||
public InfoTypeAndValue(DerObjectIdentifier infoType)
|
||||
{
|
||||
this.infoType = infoType;
|
||||
infoValue = null;
|
||||
}
|
||||
|
||||
public InfoTypeAndValue(DerObjectIdentifier infoType, Asn1Encodable optionalValue)
|
||||
{
|
||||
this.infoType = infoType;
|
||||
infoValue = optionalValue;
|
||||
}
|
||||
|
||||
public override Asn1Object ToAsn1Object()
|
||||
{
|
||||
Asn1EncodableVector asn1EncodableVector = new Asn1EncodableVector(infoType);
|
||||
if (infoValue != null)
|
||||
{
|
||||
asn1EncodableVector.Add(infoValue);
|
||||
}
|
||||
return new DerSequence(asn1EncodableVector);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1.Cmp;
|
||||
|
||||
public class KeyRecRepContent : Asn1Encodable
|
||||
{
|
||||
private readonly PkiStatusInfo status;
|
||||
|
||||
private readonly CmpCertificate newSigCert;
|
||||
|
||||
private readonly Asn1Sequence caCerts;
|
||||
|
||||
private readonly Asn1Sequence keyPairHist;
|
||||
|
||||
public virtual PkiStatusInfo Status => status;
|
||||
|
||||
public virtual CmpCertificate NewSigCert => newSigCert;
|
||||
|
||||
private KeyRecRepContent(Asn1Sequence seq)
|
||||
{
|
||||
status = PkiStatusInfo.GetInstance(seq[0]);
|
||||
for (int i = 1; i < seq.Count; i++)
|
||||
{
|
||||
Asn1TaggedObject instance = Asn1TaggedObject.GetInstance(seq[i]);
|
||||
switch (instance.TagNo)
|
||||
{
|
||||
case 0:
|
||||
newSigCert = CmpCertificate.GetInstance(instance.GetObject());
|
||||
break;
|
||||
case 1:
|
||||
caCerts = Asn1Sequence.GetInstance(instance.GetObject());
|
||||
break;
|
||||
case 2:
|
||||
keyPairHist = Asn1Sequence.GetInstance(instance.GetObject());
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentException("unknown tag number: " + instance.TagNo, "seq");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static KeyRecRepContent GetInstance(object obj)
|
||||
{
|
||||
if (obj is KeyRecRepContent)
|
||||
{
|
||||
return (KeyRecRepContent)obj;
|
||||
}
|
||||
if (obj is Asn1Sequence)
|
||||
{
|
||||
return new KeyRecRepContent((Asn1Sequence)obj);
|
||||
}
|
||||
throw new ArgumentException("Invalid object: " + Platform.GetTypeName(obj), "obj");
|
||||
}
|
||||
|
||||
public virtual CmpCertificate[] GetCACerts()
|
||||
{
|
||||
if (caCerts == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
CmpCertificate[] array = new CmpCertificate[caCerts.Count];
|
||||
for (int i = 0; i != array.Length; i++)
|
||||
{
|
||||
array[i] = CmpCertificate.GetInstance(caCerts[i]);
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public virtual CertifiedKeyPair[] GetKeyPairHist()
|
||||
{
|
||||
if (keyPairHist == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
CertifiedKeyPair[] array = new CertifiedKeyPair[keyPairHist.Count];
|
||||
for (int i = 0; i != array.Length; i++)
|
||||
{
|
||||
array[i] = CertifiedKeyPair.GetInstance(keyPairHist[i]);
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public override Asn1Object ToAsn1Object()
|
||||
{
|
||||
Asn1EncodableVector v = new Asn1EncodableVector(status);
|
||||
AddOptional(v, 0, newSigCert);
|
||||
AddOptional(v, 1, caCerts);
|
||||
AddOptional(v, 2, keyPairHist);
|
||||
return new DerSequence(v);
|
||||
}
|
||||
|
||||
private void AddOptional(Asn1EncodableVector v, int tagNo, Asn1Encodable obj)
|
||||
{
|
||||
if (obj != null)
|
||||
{
|
||||
v.Add(new DerTaggedObject(explicitly: true, tagNo, obj));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Asn1.Crmf;
|
||||
using Org.BouncyCastle.Asn1.X509;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1.Cmp;
|
||||
|
||||
public class OobCertHash : Asn1Encodable
|
||||
{
|
||||
private readonly AlgorithmIdentifier hashAlg;
|
||||
|
||||
private readonly CertId certId;
|
||||
|
||||
private readonly DerBitString hashVal;
|
||||
|
||||
public virtual AlgorithmIdentifier HashAlg => hashAlg;
|
||||
|
||||
public virtual CertId CertID => certId;
|
||||
|
||||
private OobCertHash(Asn1Sequence seq)
|
||||
{
|
||||
int num = seq.Count - 1;
|
||||
hashVal = DerBitString.GetInstance(seq[num--]);
|
||||
for (int num2 = num; num2 >= 0; num2--)
|
||||
{
|
||||
Asn1TaggedObject asn1TaggedObject = (Asn1TaggedObject)seq[num2];
|
||||
if (asn1TaggedObject.TagNo == 0)
|
||||
{
|
||||
hashAlg = AlgorithmIdentifier.GetInstance(asn1TaggedObject, explicitly: true);
|
||||
}
|
||||
else
|
||||
{
|
||||
certId = CertId.GetInstance(asn1TaggedObject, isExplicit: true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static OobCertHash GetInstance(object obj)
|
||||
{
|
||||
if (obj is OobCertHash)
|
||||
{
|
||||
return (OobCertHash)obj;
|
||||
}
|
||||
if (obj is Asn1Sequence)
|
||||
{
|
||||
return new OobCertHash((Asn1Sequence)obj);
|
||||
}
|
||||
throw new ArgumentException("Invalid object: " + Platform.GetTypeName(obj), "obj");
|
||||
}
|
||||
|
||||
public override Asn1Object ToAsn1Object()
|
||||
{
|
||||
Asn1EncodableVector asn1EncodableVector = new Asn1EncodableVector();
|
||||
AddOptional(asn1EncodableVector, 0, hashAlg);
|
||||
AddOptional(asn1EncodableVector, 1, certId);
|
||||
asn1EncodableVector.Add(hashVal);
|
||||
return new DerSequence(asn1EncodableVector);
|
||||
}
|
||||
|
||||
private void AddOptional(Asn1EncodableVector v, int tagNo, Asn1Encodable obj)
|
||||
{
|
||||
if (obj != null)
|
||||
{
|
||||
v.Add(new DerTaggedObject(explicitly: true, tagNo, obj));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Asn1.X509;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1.Cmp;
|
||||
|
||||
public class PbmParameter : Asn1Encodable
|
||||
{
|
||||
private Asn1OctetString salt;
|
||||
|
||||
private AlgorithmIdentifier owf;
|
||||
|
||||
private DerInteger iterationCount;
|
||||
|
||||
private AlgorithmIdentifier mac;
|
||||
|
||||
public virtual Asn1OctetString Salt => salt;
|
||||
|
||||
public virtual AlgorithmIdentifier Owf => owf;
|
||||
|
||||
public virtual DerInteger IterationCount => iterationCount;
|
||||
|
||||
public virtual AlgorithmIdentifier Mac => mac;
|
||||
|
||||
private PbmParameter(Asn1Sequence seq)
|
||||
{
|
||||
salt = Asn1OctetString.GetInstance(seq[0]);
|
||||
owf = AlgorithmIdentifier.GetInstance(seq[1]);
|
||||
iterationCount = DerInteger.GetInstance(seq[2]);
|
||||
mac = AlgorithmIdentifier.GetInstance(seq[3]);
|
||||
}
|
||||
|
||||
public static PbmParameter GetInstance(object obj)
|
||||
{
|
||||
if (obj is PbmParameter)
|
||||
{
|
||||
return (PbmParameter)obj;
|
||||
}
|
||||
if (obj is Asn1Sequence)
|
||||
{
|
||||
return new PbmParameter((Asn1Sequence)obj);
|
||||
}
|
||||
throw new ArgumentException("Invalid object: " + Platform.GetTypeName(obj), "obj");
|
||||
}
|
||||
|
||||
public PbmParameter(byte[] salt, AlgorithmIdentifier owf, int iterationCount, AlgorithmIdentifier mac)
|
||||
: this(new DerOctetString(salt), owf, new DerInteger(iterationCount), mac)
|
||||
{
|
||||
}
|
||||
|
||||
public PbmParameter(Asn1OctetString salt, AlgorithmIdentifier owf, DerInteger iterationCount, AlgorithmIdentifier mac)
|
||||
{
|
||||
this.salt = salt;
|
||||
this.owf = owf;
|
||||
this.iterationCount = iterationCount;
|
||||
this.mac = mac;
|
||||
}
|
||||
|
||||
public override Asn1Object ToAsn1Object()
|
||||
{
|
||||
return new DerSequence(salt, owf, iterationCount, mac);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,136 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Asn1.Crmf;
|
||||
using Org.BouncyCastle.Asn1.Pkcs;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1.Cmp;
|
||||
|
||||
public class PkiBody : Asn1Encodable, IAsn1Choice
|
||||
{
|
||||
public const int TYPE_INIT_REQ = 0;
|
||||
|
||||
public const int TYPE_INIT_REP = 1;
|
||||
|
||||
public const int TYPE_CERT_REQ = 2;
|
||||
|
||||
public const int TYPE_CERT_REP = 3;
|
||||
|
||||
public const int TYPE_P10_CERT_REQ = 4;
|
||||
|
||||
public const int TYPE_POPO_CHALL = 5;
|
||||
|
||||
public const int TYPE_POPO_REP = 6;
|
||||
|
||||
public const int TYPE_KEY_UPDATE_REQ = 7;
|
||||
|
||||
public const int TYPE_KEY_UPDATE_REP = 8;
|
||||
|
||||
public const int TYPE_KEY_RECOVERY_REQ = 9;
|
||||
|
||||
public const int TYPE_KEY_RECOVERY_REP = 10;
|
||||
|
||||
public const int TYPE_REVOCATION_REQ = 11;
|
||||
|
||||
public const int TYPE_REVOCATION_REP = 12;
|
||||
|
||||
public const int TYPE_CROSS_CERT_REQ = 13;
|
||||
|
||||
public const int TYPE_CROSS_CERT_REP = 14;
|
||||
|
||||
public const int TYPE_CA_KEY_UPDATE_ANN = 15;
|
||||
|
||||
public const int TYPE_CERT_ANN = 16;
|
||||
|
||||
public const int TYPE_REVOCATION_ANN = 17;
|
||||
|
||||
public const int TYPE_CRL_ANN = 18;
|
||||
|
||||
public const int TYPE_CONFIRM = 19;
|
||||
|
||||
public const int TYPE_NESTED = 20;
|
||||
|
||||
public const int TYPE_GEN_MSG = 21;
|
||||
|
||||
public const int TYPE_GEN_REP = 22;
|
||||
|
||||
public const int TYPE_ERROR = 23;
|
||||
|
||||
public const int TYPE_CERT_CONFIRM = 24;
|
||||
|
||||
public const int TYPE_POLL_REQ = 25;
|
||||
|
||||
public const int TYPE_POLL_REP = 26;
|
||||
|
||||
private int tagNo;
|
||||
|
||||
private Asn1Encodable body;
|
||||
|
||||
public virtual int Type => tagNo;
|
||||
|
||||
public virtual Asn1Encodable Content => body;
|
||||
|
||||
public static PkiBody GetInstance(object obj)
|
||||
{
|
||||
if (obj is PkiBody)
|
||||
{
|
||||
return (PkiBody)obj;
|
||||
}
|
||||
if (obj is Asn1TaggedObject)
|
||||
{
|
||||
return new PkiBody((Asn1TaggedObject)obj);
|
||||
}
|
||||
throw new ArgumentException("Invalid object: " + Platform.GetTypeName(obj), "obj");
|
||||
}
|
||||
|
||||
private PkiBody(Asn1TaggedObject tagged)
|
||||
{
|
||||
tagNo = tagged.TagNo;
|
||||
body = GetBodyForType(tagNo, tagged.GetObject());
|
||||
}
|
||||
|
||||
public PkiBody(int type, Asn1Encodable content)
|
||||
{
|
||||
tagNo = type;
|
||||
body = GetBodyForType(type, content);
|
||||
}
|
||||
|
||||
private static Asn1Encodable GetBodyForType(int type, Asn1Encodable o)
|
||||
{
|
||||
return type switch
|
||||
{
|
||||
0 => CertReqMessages.GetInstance(o),
|
||||
1 => CertRepMessage.GetInstance(o),
|
||||
2 => CertReqMessages.GetInstance(o),
|
||||
3 => CertRepMessage.GetInstance(o),
|
||||
4 => CertificationRequest.GetInstance(o),
|
||||
5 => PopoDecKeyChallContent.GetInstance(o),
|
||||
6 => PopoDecKeyRespContent.GetInstance(o),
|
||||
7 => CertReqMessages.GetInstance(o),
|
||||
8 => CertRepMessage.GetInstance(o),
|
||||
9 => CertReqMessages.GetInstance(o),
|
||||
10 => KeyRecRepContent.GetInstance(o),
|
||||
11 => RevReqContent.GetInstance(o),
|
||||
12 => RevRepContent.GetInstance(o),
|
||||
13 => CertReqMessages.GetInstance(o),
|
||||
14 => CertRepMessage.GetInstance(o),
|
||||
15 => CAKeyUpdAnnContent.GetInstance(o),
|
||||
16 => CmpCertificate.GetInstance(o),
|
||||
17 => RevAnnContent.GetInstance(o),
|
||||
18 => CrlAnnContent.GetInstance(o),
|
||||
19 => PkiConfirmContent.GetInstance(o),
|
||||
20 => PkiMessages.GetInstance(o),
|
||||
21 => GenMsgContent.GetInstance(o),
|
||||
22 => GenRepContent.GetInstance(o),
|
||||
23 => ErrorMsgContent.GetInstance(o),
|
||||
24 => CertConfirmContent.GetInstance(o),
|
||||
25 => PollReqContent.GetInstance(o),
|
||||
26 => PollRepContent.GetInstance(o),
|
||||
_ => throw new ArgumentException("unknown tag number: " + type, "type"),
|
||||
};
|
||||
}
|
||||
|
||||
public override Asn1Object ToAsn1Object()
|
||||
{
|
||||
return new DerTaggedObject(explicitly: true, tagNo, body);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1.Cmp;
|
||||
|
||||
public class PkiConfirmContent : Asn1Encodable
|
||||
{
|
||||
public static PkiConfirmContent GetInstance(object obj)
|
||||
{
|
||||
if (obj is PkiConfirmContent)
|
||||
{
|
||||
return (PkiConfirmContent)obj;
|
||||
}
|
||||
if (obj is Asn1Null)
|
||||
{
|
||||
return new PkiConfirmContent();
|
||||
}
|
||||
throw new ArgumentException("Invalid object: " + Platform.GetTypeName(obj), "obj");
|
||||
}
|
||||
|
||||
public override Asn1Object ToAsn1Object()
|
||||
{
|
||||
return DerNull.Instance;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
namespace Org.BouncyCastle.Asn1.Cmp;
|
||||
|
||||
public class PkiFailureInfo : DerBitString
|
||||
{
|
||||
public const int BadAlg = 128;
|
||||
|
||||
public const int BadMessageCheck = 64;
|
||||
|
||||
public const int BadRequest = 32;
|
||||
|
||||
public const int BadTime = 16;
|
||||
|
||||
public const int BadCertId = 8;
|
||||
|
||||
public const int BadDataFormat = 4;
|
||||
|
||||
public const int WrongAuthority = 2;
|
||||
|
||||
public const int IncorrectData = 1;
|
||||
|
||||
public const int MissingTimeStamp = 32768;
|
||||
|
||||
public const int BadPop = 16384;
|
||||
|
||||
public const int CertRevoked = 8192;
|
||||
|
||||
public const int CertConfirmed = 4096;
|
||||
|
||||
public const int WrongIntegrity = 2048;
|
||||
|
||||
public const int BadRecipientNonce = 1024;
|
||||
|
||||
public const int TimeNotAvailable = 512;
|
||||
|
||||
public const int UnacceptedPolicy = 256;
|
||||
|
||||
public const int UnacceptedExtension = 8388608;
|
||||
|
||||
public const int AddInfoNotAvailable = 4194304;
|
||||
|
||||
public const int BadSenderNonce = 2097152;
|
||||
|
||||
public const int BadCertTemplate = 1048576;
|
||||
|
||||
public const int SignerNotTrusted = 524288;
|
||||
|
||||
public const int TransactionIdInUse = 262144;
|
||||
|
||||
public const int UnsupportedVersion = 131072;
|
||||
|
||||
public const int NotAuthorized = 65536;
|
||||
|
||||
public const int SystemUnavail = int.MinValue;
|
||||
|
||||
public const int SystemFailure = 1073741824;
|
||||
|
||||
public const int DuplicateCertReq = 536870912;
|
||||
|
||||
public PkiFailureInfo(int info)
|
||||
: base(info)
|
||||
{
|
||||
}
|
||||
|
||||
public PkiFailureInfo(DerBitString info)
|
||||
: base(info.GetBytes(), info.PadBits)
|
||||
{
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return "PkiFailureInfo: 0x" + IntValue.ToString("X");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1.Cmp;
|
||||
|
||||
public class PkiFreeText : Asn1Encodable
|
||||
{
|
||||
internal Asn1Sequence strings;
|
||||
|
||||
[Obsolete("Use 'Count' property instead")]
|
||||
public int Size => strings.Count;
|
||||
|
||||
public int Count => strings.Count;
|
||||
|
||||
public DerUtf8String this[int index] => (DerUtf8String)strings[index];
|
||||
|
||||
public static PkiFreeText GetInstance(Asn1TaggedObject obj, bool isExplicit)
|
||||
{
|
||||
return GetInstance(Asn1Sequence.GetInstance(obj, isExplicit));
|
||||
}
|
||||
|
||||
public static PkiFreeText GetInstance(object obj)
|
||||
{
|
||||
if (obj is PkiFreeText)
|
||||
{
|
||||
return (PkiFreeText)obj;
|
||||
}
|
||||
if (obj is Asn1Sequence)
|
||||
{
|
||||
return new PkiFreeText((Asn1Sequence)obj);
|
||||
}
|
||||
throw new ArgumentException("Unknown object in factory: " + Platform.GetTypeName(obj), "obj");
|
||||
}
|
||||
|
||||
public PkiFreeText(Asn1Sequence seq)
|
||||
{
|
||||
foreach (object item in seq)
|
||||
{
|
||||
if (!(item is DerUtf8String))
|
||||
{
|
||||
throw new ArgumentException("attempt to insert non UTF8 STRING into PkiFreeText");
|
||||
}
|
||||
}
|
||||
strings = seq;
|
||||
}
|
||||
|
||||
public PkiFreeText(DerUtf8String p)
|
||||
{
|
||||
strings = new DerSequence(p);
|
||||
}
|
||||
|
||||
[Obsolete("Use 'object[index]' syntax instead")]
|
||||
public DerUtf8String GetStringAt(int index)
|
||||
{
|
||||
return this[index];
|
||||
}
|
||||
|
||||
public override Asn1Object ToAsn1Object()
|
||||
{
|
||||
return strings;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,165 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Asn1.X509;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1.Cmp;
|
||||
|
||||
public class PkiHeader : Asn1Encodable
|
||||
{
|
||||
public static readonly GeneralName NULL_NAME = new GeneralName(X509Name.GetInstance(new DerSequence()));
|
||||
|
||||
public static readonly int CMP_1999 = 1;
|
||||
|
||||
public static readonly int CMP_2000 = 2;
|
||||
|
||||
private readonly DerInteger pvno;
|
||||
|
||||
private readonly GeneralName sender;
|
||||
|
||||
private readonly GeneralName recipient;
|
||||
|
||||
private readonly DerGeneralizedTime messageTime;
|
||||
|
||||
private readonly AlgorithmIdentifier protectionAlg;
|
||||
|
||||
private readonly Asn1OctetString senderKID;
|
||||
|
||||
private readonly Asn1OctetString recipKID;
|
||||
|
||||
private readonly Asn1OctetString transactionID;
|
||||
|
||||
private readonly Asn1OctetString senderNonce;
|
||||
|
||||
private readonly Asn1OctetString recipNonce;
|
||||
|
||||
private readonly PkiFreeText freeText;
|
||||
|
||||
private readonly Asn1Sequence generalInfo;
|
||||
|
||||
public virtual DerInteger Pvno => pvno;
|
||||
|
||||
public virtual GeneralName Sender => sender;
|
||||
|
||||
public virtual GeneralName Recipient => recipient;
|
||||
|
||||
public virtual DerGeneralizedTime MessageTime => messageTime;
|
||||
|
||||
public virtual AlgorithmIdentifier ProtectionAlg => protectionAlg;
|
||||
|
||||
public virtual Asn1OctetString SenderKID => senderKID;
|
||||
|
||||
public virtual Asn1OctetString RecipKID => recipKID;
|
||||
|
||||
public virtual Asn1OctetString TransactionID => transactionID;
|
||||
|
||||
public virtual Asn1OctetString SenderNonce => senderNonce;
|
||||
|
||||
public virtual Asn1OctetString RecipNonce => recipNonce;
|
||||
|
||||
public virtual PkiFreeText FreeText => freeText;
|
||||
|
||||
private PkiHeader(Asn1Sequence seq)
|
||||
{
|
||||
pvno = DerInteger.GetInstance(seq[0]);
|
||||
sender = GeneralName.GetInstance(seq[1]);
|
||||
recipient = GeneralName.GetInstance(seq[2]);
|
||||
for (int i = 3; i < seq.Count; i++)
|
||||
{
|
||||
Asn1TaggedObject asn1TaggedObject = (Asn1TaggedObject)seq[i];
|
||||
switch (asn1TaggedObject.TagNo)
|
||||
{
|
||||
case 0:
|
||||
messageTime = DerGeneralizedTime.GetInstance(asn1TaggedObject, isExplicit: true);
|
||||
break;
|
||||
case 1:
|
||||
protectionAlg = AlgorithmIdentifier.GetInstance(asn1TaggedObject, explicitly: true);
|
||||
break;
|
||||
case 2:
|
||||
senderKID = Asn1OctetString.GetInstance(asn1TaggedObject, isExplicit: true);
|
||||
break;
|
||||
case 3:
|
||||
recipKID = Asn1OctetString.GetInstance(asn1TaggedObject, isExplicit: true);
|
||||
break;
|
||||
case 4:
|
||||
transactionID = Asn1OctetString.GetInstance(asn1TaggedObject, isExplicit: true);
|
||||
break;
|
||||
case 5:
|
||||
senderNonce = Asn1OctetString.GetInstance(asn1TaggedObject, isExplicit: true);
|
||||
break;
|
||||
case 6:
|
||||
recipNonce = Asn1OctetString.GetInstance(asn1TaggedObject, isExplicit: true);
|
||||
break;
|
||||
case 7:
|
||||
freeText = PkiFreeText.GetInstance(asn1TaggedObject, isExplicit: true);
|
||||
break;
|
||||
case 8:
|
||||
generalInfo = Asn1Sequence.GetInstance(asn1TaggedObject, explicitly: true);
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentException("unknown tag number: " + asn1TaggedObject.TagNo, "seq");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static PkiHeader GetInstance(object obj)
|
||||
{
|
||||
if (obj is PkiHeader)
|
||||
{
|
||||
return (PkiHeader)obj;
|
||||
}
|
||||
if (obj is Asn1Sequence)
|
||||
{
|
||||
return new PkiHeader((Asn1Sequence)obj);
|
||||
}
|
||||
throw new ArgumentException("Invalid object: " + Platform.GetTypeName(obj), "obj");
|
||||
}
|
||||
|
||||
public PkiHeader(int pvno, GeneralName sender, GeneralName recipient)
|
||||
: this(new DerInteger(pvno), sender, recipient)
|
||||
{
|
||||
}
|
||||
|
||||
private PkiHeader(DerInteger pvno, GeneralName sender, GeneralName recipient)
|
||||
{
|
||||
this.pvno = pvno;
|
||||
this.sender = sender;
|
||||
this.recipient = recipient;
|
||||
}
|
||||
|
||||
public virtual InfoTypeAndValue[] GetGeneralInfo()
|
||||
{
|
||||
if (generalInfo == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
InfoTypeAndValue[] array = new InfoTypeAndValue[generalInfo.Count];
|
||||
for (int i = 0; i < array.Length; i++)
|
||||
{
|
||||
array[i] = InfoTypeAndValue.GetInstance(generalInfo[i]);
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public override Asn1Object ToAsn1Object()
|
||||
{
|
||||
Asn1EncodableVector v = new Asn1EncodableVector(pvno, sender, recipient);
|
||||
AddOptional(v, 0, messageTime);
|
||||
AddOptional(v, 1, protectionAlg);
|
||||
AddOptional(v, 2, senderKID);
|
||||
AddOptional(v, 3, recipKID);
|
||||
AddOptional(v, 4, transactionID);
|
||||
AddOptional(v, 5, senderNonce);
|
||||
AddOptional(v, 6, recipNonce);
|
||||
AddOptional(v, 7, freeText);
|
||||
AddOptional(v, 8, generalInfo);
|
||||
return new DerSequence(v);
|
||||
}
|
||||
|
||||
private static void AddOptional(Asn1EncodableVector v, int tagNo, Asn1Encodable obj)
|
||||
{
|
||||
if (obj != null)
|
||||
{
|
||||
v.Add(new DerTaggedObject(explicitly: true, tagNo, obj));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,183 @@
|
||||
using Org.BouncyCastle.Asn1.X509;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1.Cmp;
|
||||
|
||||
public class PkiHeaderBuilder
|
||||
{
|
||||
private DerInteger pvno;
|
||||
|
||||
private GeneralName sender;
|
||||
|
||||
private GeneralName recipient;
|
||||
|
||||
private DerGeneralizedTime messageTime;
|
||||
|
||||
private AlgorithmIdentifier protectionAlg;
|
||||
|
||||
private Asn1OctetString senderKID;
|
||||
|
||||
private Asn1OctetString recipKID;
|
||||
|
||||
private Asn1OctetString transactionID;
|
||||
|
||||
private Asn1OctetString senderNonce;
|
||||
|
||||
private Asn1OctetString recipNonce;
|
||||
|
||||
private PkiFreeText freeText;
|
||||
|
||||
private Asn1Sequence generalInfo;
|
||||
|
||||
public PkiHeaderBuilder(int pvno, GeneralName sender, GeneralName recipient)
|
||||
: this(new DerInteger(pvno), sender, recipient)
|
||||
{
|
||||
}
|
||||
|
||||
private PkiHeaderBuilder(DerInteger pvno, GeneralName sender, GeneralName recipient)
|
||||
{
|
||||
this.pvno = pvno;
|
||||
this.sender = sender;
|
||||
this.recipient = recipient;
|
||||
}
|
||||
|
||||
public virtual PkiHeaderBuilder SetMessageTime(DerGeneralizedTime time)
|
||||
{
|
||||
messageTime = time;
|
||||
return this;
|
||||
}
|
||||
|
||||
public virtual PkiHeaderBuilder SetProtectionAlg(AlgorithmIdentifier aid)
|
||||
{
|
||||
protectionAlg = aid;
|
||||
return this;
|
||||
}
|
||||
|
||||
public virtual PkiHeaderBuilder SetSenderKID(byte[] kid)
|
||||
{
|
||||
return SetSenderKID((kid == null) ? null : new DerOctetString(kid));
|
||||
}
|
||||
|
||||
public virtual PkiHeaderBuilder SetSenderKID(Asn1OctetString kid)
|
||||
{
|
||||
senderKID = kid;
|
||||
return this;
|
||||
}
|
||||
|
||||
public virtual PkiHeaderBuilder SetRecipKID(byte[] kid)
|
||||
{
|
||||
return SetRecipKID((kid == null) ? null : new DerOctetString(kid));
|
||||
}
|
||||
|
||||
public virtual PkiHeaderBuilder SetRecipKID(DerOctetString kid)
|
||||
{
|
||||
recipKID = kid;
|
||||
return this;
|
||||
}
|
||||
|
||||
public virtual PkiHeaderBuilder SetTransactionID(byte[] tid)
|
||||
{
|
||||
return SetTransactionID((tid == null) ? null : new DerOctetString(tid));
|
||||
}
|
||||
|
||||
public virtual PkiHeaderBuilder SetTransactionID(Asn1OctetString tid)
|
||||
{
|
||||
transactionID = tid;
|
||||
return this;
|
||||
}
|
||||
|
||||
public virtual PkiHeaderBuilder SetSenderNonce(byte[] nonce)
|
||||
{
|
||||
return SetSenderNonce((nonce == null) ? null : new DerOctetString(nonce));
|
||||
}
|
||||
|
||||
public virtual PkiHeaderBuilder SetSenderNonce(Asn1OctetString nonce)
|
||||
{
|
||||
senderNonce = nonce;
|
||||
return this;
|
||||
}
|
||||
|
||||
public virtual PkiHeaderBuilder SetRecipNonce(byte[] nonce)
|
||||
{
|
||||
return SetRecipNonce((nonce == null) ? null : new DerOctetString(nonce));
|
||||
}
|
||||
|
||||
public virtual PkiHeaderBuilder SetRecipNonce(Asn1OctetString nonce)
|
||||
{
|
||||
recipNonce = nonce;
|
||||
return this;
|
||||
}
|
||||
|
||||
public virtual PkiHeaderBuilder SetFreeText(PkiFreeText text)
|
||||
{
|
||||
freeText = text;
|
||||
return this;
|
||||
}
|
||||
|
||||
public virtual PkiHeaderBuilder SetGeneralInfo(InfoTypeAndValue genInfo)
|
||||
{
|
||||
return SetGeneralInfo(MakeGeneralInfoSeq(genInfo));
|
||||
}
|
||||
|
||||
public virtual PkiHeaderBuilder SetGeneralInfo(InfoTypeAndValue[] genInfos)
|
||||
{
|
||||
return SetGeneralInfo(MakeGeneralInfoSeq(genInfos));
|
||||
}
|
||||
|
||||
public virtual PkiHeaderBuilder SetGeneralInfo(Asn1Sequence seqOfInfoTypeAndValue)
|
||||
{
|
||||
generalInfo = seqOfInfoTypeAndValue;
|
||||
return this;
|
||||
}
|
||||
|
||||
private static Asn1Sequence MakeGeneralInfoSeq(InfoTypeAndValue generalInfo)
|
||||
{
|
||||
return new DerSequence(generalInfo);
|
||||
}
|
||||
|
||||
private static Asn1Sequence MakeGeneralInfoSeq(InfoTypeAndValue[] generalInfos)
|
||||
{
|
||||
Asn1Sequence result = null;
|
||||
if (generalInfos != null)
|
||||
{
|
||||
Asn1EncodableVector asn1EncodableVector = new Asn1EncodableVector();
|
||||
for (int i = 0; i < generalInfos.Length; i++)
|
||||
{
|
||||
asn1EncodableVector.Add(generalInfos[i]);
|
||||
}
|
||||
result = new DerSequence(asn1EncodableVector);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public virtual PkiHeader Build()
|
||||
{
|
||||
Asn1EncodableVector v = new Asn1EncodableVector(pvno, sender, recipient);
|
||||
AddOptional(v, 0, messageTime);
|
||||
AddOptional(v, 1, protectionAlg);
|
||||
AddOptional(v, 2, senderKID);
|
||||
AddOptional(v, 3, recipKID);
|
||||
AddOptional(v, 4, transactionID);
|
||||
AddOptional(v, 5, senderNonce);
|
||||
AddOptional(v, 6, recipNonce);
|
||||
AddOptional(v, 7, freeText);
|
||||
AddOptional(v, 8, generalInfo);
|
||||
messageTime = null;
|
||||
protectionAlg = null;
|
||||
senderKID = null;
|
||||
recipKID = null;
|
||||
transactionID = null;
|
||||
senderNonce = null;
|
||||
recipNonce = null;
|
||||
freeText = null;
|
||||
generalInfo = null;
|
||||
return PkiHeader.GetInstance(new DerSequence(v));
|
||||
}
|
||||
|
||||
private void AddOptional(Asn1EncodableVector v, int tagNo, Asn1Encodable obj)
|
||||
{
|
||||
if (obj != null)
|
||||
{
|
||||
v.Add(new DerTaggedObject(explicitly: true, tagNo, obj));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
namespace Org.BouncyCastle.Asn1.Cmp;
|
||||
|
||||
public class PkiMessage : Asn1Encodable
|
||||
{
|
||||
private readonly PkiHeader header;
|
||||
|
||||
private readonly PkiBody body;
|
||||
|
||||
private readonly DerBitString protection;
|
||||
|
||||
private readonly Asn1Sequence extraCerts;
|
||||
|
||||
public virtual PkiHeader Header => header;
|
||||
|
||||
public virtual PkiBody Body => body;
|
||||
|
||||
public virtual DerBitString Protection => protection;
|
||||
|
||||
private PkiMessage(Asn1Sequence seq)
|
||||
{
|
||||
header = PkiHeader.GetInstance(seq[0]);
|
||||
body = PkiBody.GetInstance(seq[1]);
|
||||
for (int i = 2; i < seq.Count; i++)
|
||||
{
|
||||
Asn1TaggedObject asn1TaggedObject = (Asn1TaggedObject)seq[i].ToAsn1Object();
|
||||
if (asn1TaggedObject.TagNo == 0)
|
||||
{
|
||||
protection = DerBitString.GetInstance(asn1TaggedObject, isExplicit: true);
|
||||
}
|
||||
else
|
||||
{
|
||||
extraCerts = Asn1Sequence.GetInstance(asn1TaggedObject, explicitly: true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static PkiMessage GetInstance(object obj)
|
||||
{
|
||||
if (obj is PkiMessage)
|
||||
{
|
||||
return (PkiMessage)obj;
|
||||
}
|
||||
if (obj != null)
|
||||
{
|
||||
return new PkiMessage(Asn1Sequence.GetInstance(obj));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public PkiMessage(PkiHeader header, PkiBody body, DerBitString protection, CmpCertificate[] extraCerts)
|
||||
{
|
||||
this.header = header;
|
||||
this.body = body;
|
||||
this.protection = protection;
|
||||
if (extraCerts != null)
|
||||
{
|
||||
this.extraCerts = new DerSequence(extraCerts);
|
||||
}
|
||||
}
|
||||
|
||||
public PkiMessage(PkiHeader header, PkiBody body, DerBitString protection)
|
||||
: this(header, body, protection, null)
|
||||
{
|
||||
}
|
||||
|
||||
public PkiMessage(PkiHeader header, PkiBody body)
|
||||
: this(header, body, null, null)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual CmpCertificate[] GetExtraCerts()
|
||||
{
|
||||
if (extraCerts == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
CmpCertificate[] array = new CmpCertificate[extraCerts.Count];
|
||||
for (int i = 0; i < array.Length; i++)
|
||||
{
|
||||
array[i] = CmpCertificate.GetInstance(extraCerts[i]);
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public override Asn1Object ToAsn1Object()
|
||||
{
|
||||
Asn1EncodableVector v = new Asn1EncodableVector(header, body);
|
||||
AddOptional(v, 0, protection);
|
||||
AddOptional(v, 1, extraCerts);
|
||||
return new DerSequence(v);
|
||||
}
|
||||
|
||||
private static void AddOptional(Asn1EncodableVector v, int tagNo, Asn1Encodable obj)
|
||||
{
|
||||
if (obj != null)
|
||||
{
|
||||
v.Add(new DerTaggedObject(explicitly: true, tagNo, obj));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1.Cmp;
|
||||
|
||||
public class PkiMessages : Asn1Encodable
|
||||
{
|
||||
private Asn1Sequence content;
|
||||
|
||||
private PkiMessages(Asn1Sequence seq)
|
||||
{
|
||||
content = seq;
|
||||
}
|
||||
|
||||
public static PkiMessages GetInstance(object obj)
|
||||
{
|
||||
if (obj is PkiMessages)
|
||||
{
|
||||
return (PkiMessages)obj;
|
||||
}
|
||||
if (obj is Asn1Sequence)
|
||||
{
|
||||
return new PkiMessages((Asn1Sequence)obj);
|
||||
}
|
||||
throw new ArgumentException("Invalid object: " + Platform.GetTypeName(obj), "obj");
|
||||
}
|
||||
|
||||
public PkiMessages(params PkiMessage[] msgs)
|
||||
{
|
||||
content = new DerSequence(msgs);
|
||||
}
|
||||
|
||||
public virtual PkiMessage[] ToPkiMessageArray()
|
||||
{
|
||||
PkiMessage[] array = new PkiMessage[content.Count];
|
||||
for (int i = 0; i != array.Length; i++)
|
||||
{
|
||||
array[i] = PkiMessage.GetInstance(content[i]);
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public override Asn1Object ToAsn1Object()
|
||||
{
|
||||
return content;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
namespace Org.BouncyCastle.Asn1.Cmp;
|
||||
|
||||
public enum PkiStatus
|
||||
{
|
||||
Granted,
|
||||
GrantedWithMods,
|
||||
Rejection,
|
||||
Waiting,
|
||||
RevocationWarning,
|
||||
RevocationNotification,
|
||||
KeyUpdateWarning
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Math;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1.Cmp;
|
||||
|
||||
public class PkiStatusEncodable : Asn1Encodable
|
||||
{
|
||||
public static readonly PkiStatusEncodable granted = new PkiStatusEncodable(PkiStatus.Granted);
|
||||
|
||||
public static readonly PkiStatusEncodable grantedWithMods = new PkiStatusEncodable(PkiStatus.GrantedWithMods);
|
||||
|
||||
public static readonly PkiStatusEncodable rejection = new PkiStatusEncodable(PkiStatus.Rejection);
|
||||
|
||||
public static readonly PkiStatusEncodable waiting = new PkiStatusEncodable(PkiStatus.Waiting);
|
||||
|
||||
public static readonly PkiStatusEncodable revocationWarning = new PkiStatusEncodable(PkiStatus.RevocationWarning);
|
||||
|
||||
public static readonly PkiStatusEncodable revocationNotification = new PkiStatusEncodable(PkiStatus.RevocationNotification);
|
||||
|
||||
public static readonly PkiStatusEncodable keyUpdateWaiting = new PkiStatusEncodable(PkiStatus.KeyUpdateWarning);
|
||||
|
||||
private readonly DerInteger status;
|
||||
|
||||
public virtual BigInteger Value => status.Value;
|
||||
|
||||
private PkiStatusEncodable(PkiStatus status)
|
||||
: this(new DerInteger((int)status))
|
||||
{
|
||||
}
|
||||
|
||||
private PkiStatusEncodable(DerInteger status)
|
||||
{
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
public static PkiStatusEncodable GetInstance(object obj)
|
||||
{
|
||||
if (obj is PkiStatusEncodable)
|
||||
{
|
||||
return (PkiStatusEncodable)obj;
|
||||
}
|
||||
if (obj is DerInteger)
|
||||
{
|
||||
return new PkiStatusEncodable((DerInteger)obj);
|
||||
}
|
||||
throw new ArgumentException("Invalid object: " + Platform.GetTypeName(obj), "obj");
|
||||
}
|
||||
|
||||
public override Asn1Object ToAsn1Object()
|
||||
{
|
||||
return status;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Math;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1.Cmp;
|
||||
|
||||
public class PkiStatusInfo : Asn1Encodable
|
||||
{
|
||||
private DerInteger status;
|
||||
|
||||
private PkiFreeText statusString;
|
||||
|
||||
private DerBitString failInfo;
|
||||
|
||||
public BigInteger Status => status.Value;
|
||||
|
||||
public PkiFreeText StatusString => statusString;
|
||||
|
||||
public DerBitString FailInfo => failInfo;
|
||||
|
||||
public static PkiStatusInfo GetInstance(Asn1TaggedObject obj, bool isExplicit)
|
||||
{
|
||||
return GetInstance(Asn1Sequence.GetInstance(obj, isExplicit));
|
||||
}
|
||||
|
||||
public static PkiStatusInfo GetInstance(object obj)
|
||||
{
|
||||
if (obj is PkiStatusInfo)
|
||||
{
|
||||
return (PkiStatusInfo)obj;
|
||||
}
|
||||
if (obj is Asn1Sequence)
|
||||
{
|
||||
return new PkiStatusInfo((Asn1Sequence)obj);
|
||||
}
|
||||
throw new ArgumentException("Unknown object in factory: " + Platform.GetTypeName(obj), "obj");
|
||||
}
|
||||
|
||||
public PkiStatusInfo(Asn1Sequence seq)
|
||||
{
|
||||
status = DerInteger.GetInstance(seq[0]);
|
||||
statusString = null;
|
||||
failInfo = null;
|
||||
if (seq.Count > 2)
|
||||
{
|
||||
statusString = PkiFreeText.GetInstance(seq[1]);
|
||||
failInfo = DerBitString.GetInstance(seq[2]);
|
||||
}
|
||||
else if (seq.Count > 1)
|
||||
{
|
||||
object obj = seq[1];
|
||||
if (obj is DerBitString)
|
||||
{
|
||||
failInfo = DerBitString.GetInstance(obj);
|
||||
}
|
||||
else
|
||||
{
|
||||
statusString = PkiFreeText.GetInstance(obj);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public PkiStatusInfo(int status)
|
||||
{
|
||||
this.status = new DerInteger(status);
|
||||
}
|
||||
|
||||
public PkiStatusInfo(int status, PkiFreeText statusString)
|
||||
{
|
||||
this.status = new DerInteger(status);
|
||||
this.statusString = statusString;
|
||||
}
|
||||
|
||||
public PkiStatusInfo(int status, PkiFreeText statusString, PkiFailureInfo failInfo)
|
||||
{
|
||||
this.status = new DerInteger(status);
|
||||
this.statusString = statusString;
|
||||
this.failInfo = failInfo;
|
||||
}
|
||||
|
||||
public override Asn1Object ToAsn1Object()
|
||||
{
|
||||
Asn1EncodableVector asn1EncodableVector = new Asn1EncodableVector(status);
|
||||
if (statusString != null)
|
||||
{
|
||||
asn1EncodableVector.Add(statusString);
|
||||
}
|
||||
if (failInfo != null)
|
||||
{
|
||||
asn1EncodableVector.Add(failInfo);
|
||||
}
|
||||
return new DerSequence(asn1EncodableVector);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1.Cmp;
|
||||
|
||||
public class PollRepContent : Asn1Encodable
|
||||
{
|
||||
private readonly DerInteger certReqId;
|
||||
|
||||
private readonly DerInteger checkAfter;
|
||||
|
||||
private readonly PkiFreeText reason;
|
||||
|
||||
public virtual DerInteger CertReqID => certReqId;
|
||||
|
||||
public virtual DerInteger CheckAfter => checkAfter;
|
||||
|
||||
public virtual PkiFreeText Reason => reason;
|
||||
|
||||
private PollRepContent(Asn1Sequence seq)
|
||||
{
|
||||
certReqId = DerInteger.GetInstance(seq[0]);
|
||||
checkAfter = DerInteger.GetInstance(seq[1]);
|
||||
if (seq.Count > 2)
|
||||
{
|
||||
reason = PkiFreeText.GetInstance(seq[2]);
|
||||
}
|
||||
}
|
||||
|
||||
public static PollRepContent GetInstance(object obj)
|
||||
{
|
||||
if (obj is PollRepContent)
|
||||
{
|
||||
return (PollRepContent)obj;
|
||||
}
|
||||
if (obj is Asn1Sequence)
|
||||
{
|
||||
return new PollRepContent((Asn1Sequence)obj);
|
||||
}
|
||||
throw new ArgumentException("Invalid object: " + Platform.GetTypeName(obj), "obj");
|
||||
}
|
||||
|
||||
public PollRepContent(DerInteger certReqId, DerInteger checkAfter)
|
||||
{
|
||||
this.certReqId = certReqId;
|
||||
this.checkAfter = checkAfter;
|
||||
reason = null;
|
||||
}
|
||||
|
||||
public PollRepContent(DerInteger certReqId, DerInteger checkAfter, PkiFreeText reason)
|
||||
{
|
||||
this.certReqId = certReqId;
|
||||
this.checkAfter = checkAfter;
|
||||
this.reason = reason;
|
||||
}
|
||||
|
||||
public override Asn1Object ToAsn1Object()
|
||||
{
|
||||
Asn1EncodableVector asn1EncodableVector = new Asn1EncodableVector(certReqId, checkAfter);
|
||||
asn1EncodableVector.AddOptional(reason);
|
||||
return new DerSequence(asn1EncodableVector);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1.Cmp;
|
||||
|
||||
public class PollReqContent : Asn1Encodable
|
||||
{
|
||||
private readonly Asn1Sequence content;
|
||||
|
||||
private PollReqContent(Asn1Sequence seq)
|
||||
{
|
||||
content = seq;
|
||||
}
|
||||
|
||||
public static PollReqContent GetInstance(object obj)
|
||||
{
|
||||
if (obj is PollReqContent)
|
||||
{
|
||||
return (PollReqContent)obj;
|
||||
}
|
||||
if (obj is Asn1Sequence)
|
||||
{
|
||||
return new PollReqContent((Asn1Sequence)obj);
|
||||
}
|
||||
throw new ArgumentException("Invalid object: " + Platform.GetTypeName(obj), "obj");
|
||||
}
|
||||
|
||||
public virtual DerInteger[][] GetCertReqIDs()
|
||||
{
|
||||
DerInteger[][] array = new DerInteger[content.Count][];
|
||||
for (int i = 0; i != array.Length; i++)
|
||||
{
|
||||
array[i] = SequenceToDerIntegerArray((Asn1Sequence)content[i]);
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
private static DerInteger[] SequenceToDerIntegerArray(Asn1Sequence seq)
|
||||
{
|
||||
DerInteger[] array = new DerInteger[seq.Count];
|
||||
for (int i = 0; i != array.Length; i++)
|
||||
{
|
||||
array[i] = DerInteger.GetInstance(seq[i]);
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public override Asn1Object ToAsn1Object()
|
||||
{
|
||||
return content;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1.Cmp;
|
||||
|
||||
public class PopoDecKeyChallContent : Asn1Encodable
|
||||
{
|
||||
private readonly Asn1Sequence content;
|
||||
|
||||
private PopoDecKeyChallContent(Asn1Sequence seq)
|
||||
{
|
||||
content = seq;
|
||||
}
|
||||
|
||||
public static PopoDecKeyChallContent GetInstance(object obj)
|
||||
{
|
||||
if (obj is PopoDecKeyChallContent)
|
||||
{
|
||||
return (PopoDecKeyChallContent)obj;
|
||||
}
|
||||
if (obj is Asn1Sequence)
|
||||
{
|
||||
return new PopoDecKeyChallContent((Asn1Sequence)obj);
|
||||
}
|
||||
throw new ArgumentException("Invalid object: " + Platform.GetTypeName(obj), "obj");
|
||||
}
|
||||
|
||||
public virtual Challenge[] ToChallengeArray()
|
||||
{
|
||||
Challenge[] array = new Challenge[content.Count];
|
||||
for (int i = 0; i != array.Length; i++)
|
||||
{
|
||||
array[i] = Challenge.GetInstance(content[i]);
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public override Asn1Object ToAsn1Object()
|
||||
{
|
||||
return content;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1.Cmp;
|
||||
|
||||
public class PopoDecKeyRespContent : Asn1Encodable
|
||||
{
|
||||
private readonly Asn1Sequence content;
|
||||
|
||||
private PopoDecKeyRespContent(Asn1Sequence seq)
|
||||
{
|
||||
content = seq;
|
||||
}
|
||||
|
||||
public static PopoDecKeyRespContent GetInstance(object obj)
|
||||
{
|
||||
if (obj is PopoDecKeyRespContent)
|
||||
{
|
||||
return (PopoDecKeyRespContent)obj;
|
||||
}
|
||||
if (obj is Asn1Sequence)
|
||||
{
|
||||
return new PopoDecKeyRespContent((Asn1Sequence)obj);
|
||||
}
|
||||
throw new ArgumentException("Invalid object: " + Platform.GetTypeName(obj), "obj");
|
||||
}
|
||||
|
||||
public virtual DerInteger[] ToDerIntegerArray()
|
||||
{
|
||||
DerInteger[] array = new DerInteger[content.Count];
|
||||
for (int i = 0; i != array.Length; i++)
|
||||
{
|
||||
array[i] = DerInteger.GetInstance(content[i]);
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public override Asn1Object ToAsn1Object()
|
||||
{
|
||||
return content;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1.Cmp;
|
||||
|
||||
public class ProtectedPart : Asn1Encodable
|
||||
{
|
||||
private readonly PkiHeader header;
|
||||
|
||||
private readonly PkiBody body;
|
||||
|
||||
public virtual PkiHeader Header => header;
|
||||
|
||||
public virtual PkiBody Body => body;
|
||||
|
||||
private ProtectedPart(Asn1Sequence seq)
|
||||
{
|
||||
header = PkiHeader.GetInstance(seq[0]);
|
||||
body = PkiBody.GetInstance(seq[1]);
|
||||
}
|
||||
|
||||
public static ProtectedPart GetInstance(object obj)
|
||||
{
|
||||
if (obj is ProtectedPart)
|
||||
{
|
||||
return (ProtectedPart)obj;
|
||||
}
|
||||
if (obj is Asn1Sequence)
|
||||
{
|
||||
return new ProtectedPart((Asn1Sequence)obj);
|
||||
}
|
||||
throw new ArgumentException("Invalid object: " + Platform.GetTypeName(obj), "obj");
|
||||
}
|
||||
|
||||
public ProtectedPart(PkiHeader header, PkiBody body)
|
||||
{
|
||||
this.header = header;
|
||||
this.body = body;
|
||||
}
|
||||
|
||||
public override Asn1Object ToAsn1Object()
|
||||
{
|
||||
return new DerSequence(header, body);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Asn1.Crmf;
|
||||
using Org.BouncyCastle.Asn1.X509;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1.Cmp;
|
||||
|
||||
public class RevAnnContent : Asn1Encodable
|
||||
{
|
||||
private readonly PkiStatusEncodable status;
|
||||
|
||||
private readonly CertId certId;
|
||||
|
||||
private readonly DerGeneralizedTime willBeRevokedAt;
|
||||
|
||||
private readonly DerGeneralizedTime badSinceDate;
|
||||
|
||||
private readonly X509Extensions crlDetails;
|
||||
|
||||
public virtual PkiStatusEncodable Status => status;
|
||||
|
||||
public virtual CertId CertID => certId;
|
||||
|
||||
public virtual DerGeneralizedTime WillBeRevokedAt => willBeRevokedAt;
|
||||
|
||||
public virtual DerGeneralizedTime BadSinceDate => badSinceDate;
|
||||
|
||||
public virtual X509Extensions CrlDetails => crlDetails;
|
||||
|
||||
private RevAnnContent(Asn1Sequence seq)
|
||||
{
|
||||
status = PkiStatusEncodable.GetInstance(seq[0]);
|
||||
certId = CertId.GetInstance(seq[1]);
|
||||
willBeRevokedAt = DerGeneralizedTime.GetInstance(seq[2]);
|
||||
badSinceDate = DerGeneralizedTime.GetInstance(seq[3]);
|
||||
if (seq.Count > 4)
|
||||
{
|
||||
crlDetails = X509Extensions.GetInstance(seq[4]);
|
||||
}
|
||||
}
|
||||
|
||||
public static RevAnnContent GetInstance(object obj)
|
||||
{
|
||||
if (obj is RevAnnContent)
|
||||
{
|
||||
return (RevAnnContent)obj;
|
||||
}
|
||||
if (obj is Asn1Sequence)
|
||||
{
|
||||
return new RevAnnContent((Asn1Sequence)obj);
|
||||
}
|
||||
throw new ArgumentException("Invalid object: " + Platform.GetTypeName(obj), "obj");
|
||||
}
|
||||
|
||||
public override Asn1Object ToAsn1Object()
|
||||
{
|
||||
Asn1EncodableVector asn1EncodableVector = new Asn1EncodableVector(status, certId, willBeRevokedAt, badSinceDate);
|
||||
asn1EncodableVector.AddOptional(crlDetails);
|
||||
return new DerSequence(asn1EncodableVector);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Asn1.Crmf;
|
||||
using Org.BouncyCastle.Asn1.X509;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1.Cmp;
|
||||
|
||||
public class RevDetails : Asn1Encodable
|
||||
{
|
||||
private readonly CertTemplate certDetails;
|
||||
|
||||
private readonly X509Extensions crlEntryDetails;
|
||||
|
||||
public virtual CertTemplate CertDetails => certDetails;
|
||||
|
||||
public virtual X509Extensions CrlEntryDetails => crlEntryDetails;
|
||||
|
||||
private RevDetails(Asn1Sequence seq)
|
||||
{
|
||||
certDetails = CertTemplate.GetInstance(seq[0]);
|
||||
crlEntryDetails = ((seq.Count <= 1) ? null : X509Extensions.GetInstance(seq[1]));
|
||||
}
|
||||
|
||||
public static RevDetails GetInstance(object obj)
|
||||
{
|
||||
if (obj is RevDetails)
|
||||
{
|
||||
return (RevDetails)obj;
|
||||
}
|
||||
if (obj is Asn1Sequence)
|
||||
{
|
||||
return new RevDetails((Asn1Sequence)obj);
|
||||
}
|
||||
throw new ArgumentException("Invalid object: " + Platform.GetTypeName(obj), "obj");
|
||||
}
|
||||
|
||||
public RevDetails(CertTemplate certDetails)
|
||||
: this(certDetails, null)
|
||||
{
|
||||
}
|
||||
|
||||
public RevDetails(CertTemplate certDetails, X509Extensions crlEntryDetails)
|
||||
{
|
||||
this.certDetails = certDetails;
|
||||
this.crlEntryDetails = crlEntryDetails;
|
||||
}
|
||||
|
||||
public override Asn1Object ToAsn1Object()
|
||||
{
|
||||
Asn1EncodableVector asn1EncodableVector = new Asn1EncodableVector(certDetails);
|
||||
asn1EncodableVector.AddOptional(crlEntryDetails);
|
||||
return new DerSequence(asn1EncodableVector);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Asn1.Crmf;
|
||||
using Org.BouncyCastle.Asn1.X509;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1.Cmp;
|
||||
|
||||
public class RevRepContent : Asn1Encodable
|
||||
{
|
||||
private readonly Asn1Sequence status;
|
||||
|
||||
private readonly Asn1Sequence revCerts;
|
||||
|
||||
private readonly Asn1Sequence crls;
|
||||
|
||||
private RevRepContent(Asn1Sequence seq)
|
||||
{
|
||||
status = Asn1Sequence.GetInstance(seq[0]);
|
||||
for (int i = 1; i < seq.Count; i++)
|
||||
{
|
||||
Asn1TaggedObject instance = Asn1TaggedObject.GetInstance(seq[i]);
|
||||
if (instance.TagNo == 0)
|
||||
{
|
||||
revCerts = Asn1Sequence.GetInstance(instance, explicitly: true);
|
||||
}
|
||||
else
|
||||
{
|
||||
crls = Asn1Sequence.GetInstance(instance, explicitly: true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static RevRepContent GetInstance(object obj)
|
||||
{
|
||||
if (obj is RevRepContent)
|
||||
{
|
||||
return (RevRepContent)obj;
|
||||
}
|
||||
if (obj is Asn1Sequence)
|
||||
{
|
||||
return new RevRepContent((Asn1Sequence)obj);
|
||||
}
|
||||
throw new ArgumentException("Invalid object: " + Platform.GetTypeName(obj), "obj");
|
||||
}
|
||||
|
||||
public virtual PkiStatusInfo[] GetStatus()
|
||||
{
|
||||
PkiStatusInfo[] array = new PkiStatusInfo[status.Count];
|
||||
for (int i = 0; i != array.Length; i++)
|
||||
{
|
||||
array[i] = PkiStatusInfo.GetInstance(status[i]);
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public virtual CertId[] GetRevCerts()
|
||||
{
|
||||
if (revCerts == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
CertId[] array = new CertId[revCerts.Count];
|
||||
for (int i = 0; i != array.Length; i++)
|
||||
{
|
||||
array[i] = CertId.GetInstance(revCerts[i]);
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public virtual CertificateList[] GetCrls()
|
||||
{
|
||||
if (crls == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
CertificateList[] array = new CertificateList[crls.Count];
|
||||
for (int i = 0; i != array.Length; i++)
|
||||
{
|
||||
array[i] = CertificateList.GetInstance(crls[i]);
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public override Asn1Object ToAsn1Object()
|
||||
{
|
||||
Asn1EncodableVector v = new Asn1EncodableVector(status);
|
||||
AddOptional(v, 0, revCerts);
|
||||
AddOptional(v, 1, crls);
|
||||
return new DerSequence(v);
|
||||
}
|
||||
|
||||
private void AddOptional(Asn1EncodableVector v, int tagNo, Asn1Encodable obj)
|
||||
{
|
||||
if (obj != null)
|
||||
{
|
||||
v.Add(new DerTaggedObject(explicitly: true, tagNo, obj));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Asn1.Crmf;
|
||||
using Org.BouncyCastle.Asn1.X509;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1.Cmp;
|
||||
|
||||
public class RevRepContentBuilder
|
||||
{
|
||||
private readonly Asn1EncodableVector status = new Asn1EncodableVector();
|
||||
|
||||
private readonly Asn1EncodableVector revCerts = new Asn1EncodableVector();
|
||||
|
||||
private readonly Asn1EncodableVector crls = new Asn1EncodableVector();
|
||||
|
||||
public virtual RevRepContentBuilder Add(PkiStatusInfo status)
|
||||
{
|
||||
this.status.Add(status);
|
||||
return this;
|
||||
}
|
||||
|
||||
public virtual RevRepContentBuilder Add(PkiStatusInfo status, CertId certId)
|
||||
{
|
||||
if (this.status.Count != revCerts.Count)
|
||||
{
|
||||
throw new InvalidOperationException("status and revCerts sequence must be in common order");
|
||||
}
|
||||
this.status.Add(status);
|
||||
revCerts.Add(certId);
|
||||
return this;
|
||||
}
|
||||
|
||||
public virtual RevRepContentBuilder AddCrl(CertificateList crl)
|
||||
{
|
||||
crls.Add(crl);
|
||||
return this;
|
||||
}
|
||||
|
||||
public virtual RevRepContent Build()
|
||||
{
|
||||
Asn1EncodableVector asn1EncodableVector = new Asn1EncodableVector();
|
||||
asn1EncodableVector.Add(new DerSequence(status));
|
||||
if (revCerts.Count != 0)
|
||||
{
|
||||
asn1EncodableVector.Add(new DerTaggedObject(explicitly: true, 0, new DerSequence(revCerts)));
|
||||
}
|
||||
if (crls.Count != 0)
|
||||
{
|
||||
asn1EncodableVector.Add(new DerTaggedObject(explicitly: true, 1, new DerSequence(crls)));
|
||||
}
|
||||
return RevRepContent.GetInstance(new DerSequence(asn1EncodableVector));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1.Cmp;
|
||||
|
||||
public class RevReqContent : Asn1Encodable
|
||||
{
|
||||
private readonly Asn1Sequence content;
|
||||
|
||||
private RevReqContent(Asn1Sequence seq)
|
||||
{
|
||||
content = seq;
|
||||
}
|
||||
|
||||
public static RevReqContent GetInstance(object obj)
|
||||
{
|
||||
if (obj is RevReqContent)
|
||||
{
|
||||
return (RevReqContent)obj;
|
||||
}
|
||||
if (obj is Asn1Sequence)
|
||||
{
|
||||
return new RevReqContent((Asn1Sequence)obj);
|
||||
}
|
||||
throw new ArgumentException("Invalid object: " + Platform.GetTypeName(obj), "obj");
|
||||
}
|
||||
|
||||
public RevReqContent(params RevDetails[] revDetails)
|
||||
{
|
||||
content = new DerSequence(revDetails);
|
||||
}
|
||||
|
||||
public virtual RevDetails[] ToRevDetailsArray()
|
||||
{
|
||||
RevDetails[] array = new RevDetails[content.Count];
|
||||
for (int i = 0; i != array.Length; i++)
|
||||
{
|
||||
array[i] = RevDetails.GetInstance(content[i]);
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public override Asn1Object ToAsn1Object()
|
||||
{
|
||||
return content;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1.Cms;
|
||||
|
||||
public class Attribute : Asn1Encodable
|
||||
{
|
||||
private DerObjectIdentifier attrType;
|
||||
|
||||
private Asn1Set attrValues;
|
||||
|
||||
public DerObjectIdentifier AttrType => attrType;
|
||||
|
||||
public Asn1Set AttrValues => attrValues;
|
||||
|
||||
public static Attribute GetInstance(object obj)
|
||||
{
|
||||
if (obj == null || obj is Attribute)
|
||||
{
|
||||
return (Attribute)obj;
|
||||
}
|
||||
if (obj is Asn1Sequence)
|
||||
{
|
||||
return new Attribute((Asn1Sequence)obj);
|
||||
}
|
||||
throw new ArgumentException("unknown object in factory: " + Platform.GetTypeName(obj), "obj");
|
||||
}
|
||||
|
||||
public Attribute(Asn1Sequence seq)
|
||||
{
|
||||
attrType = (DerObjectIdentifier)seq[0];
|
||||
attrValues = (Asn1Set)seq[1];
|
||||
}
|
||||
|
||||
public Attribute(DerObjectIdentifier attrType, Asn1Set attrValues)
|
||||
{
|
||||
this.attrType = attrType;
|
||||
this.attrValues = attrValues;
|
||||
}
|
||||
|
||||
public override Asn1Object ToAsn1Object()
|
||||
{
|
||||
return new DerSequence(attrType, attrValues);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,170 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1.Cms;
|
||||
|
||||
public class AttributeTable
|
||||
{
|
||||
private readonly IDictionary attributes;
|
||||
|
||||
public Attribute this[DerObjectIdentifier oid]
|
||||
{
|
||||
get
|
||||
{
|
||||
object obj = attributes[oid];
|
||||
if (obj is IList)
|
||||
{
|
||||
return (Attribute)((IList)obj)[0];
|
||||
}
|
||||
return (Attribute)obj;
|
||||
}
|
||||
}
|
||||
|
||||
public int Count
|
||||
{
|
||||
get
|
||||
{
|
||||
int num = 0;
|
||||
foreach (object value in attributes.Values)
|
||||
{
|
||||
num = ((!(value is IList)) ? (num + 1) : (num + ((IList)value).Count));
|
||||
}
|
||||
return num;
|
||||
}
|
||||
}
|
||||
|
||||
[Obsolete]
|
||||
public AttributeTable(Hashtable attrs)
|
||||
{
|
||||
attributes = Platform.CreateHashtable(attrs);
|
||||
}
|
||||
|
||||
public AttributeTable(IDictionary attrs)
|
||||
{
|
||||
attributes = Platform.CreateHashtable(attrs);
|
||||
}
|
||||
|
||||
public AttributeTable(Asn1EncodableVector v)
|
||||
{
|
||||
attributes = Platform.CreateHashtable(v.Count);
|
||||
foreach (Asn1Encodable item in v)
|
||||
{
|
||||
Attribute instance = Attribute.GetInstance(item);
|
||||
AddAttribute(instance);
|
||||
}
|
||||
}
|
||||
|
||||
public AttributeTable(Asn1Set s)
|
||||
{
|
||||
attributes = Platform.CreateHashtable(s.Count);
|
||||
for (int i = 0; i != s.Count; i++)
|
||||
{
|
||||
Attribute instance = Attribute.GetInstance(s[i]);
|
||||
AddAttribute(instance);
|
||||
}
|
||||
}
|
||||
|
||||
public AttributeTable(Attributes attrs)
|
||||
: this(Asn1Set.GetInstance(attrs.ToAsn1Object()))
|
||||
{
|
||||
}
|
||||
|
||||
private void AddAttribute(Attribute a)
|
||||
{
|
||||
DerObjectIdentifier attrType = a.AttrType;
|
||||
object obj = attributes[attrType];
|
||||
if (obj == null)
|
||||
{
|
||||
attributes[attrType] = a;
|
||||
return;
|
||||
}
|
||||
IList list;
|
||||
if (obj is Attribute)
|
||||
{
|
||||
list = Platform.CreateArrayList();
|
||||
list.Add(obj);
|
||||
list.Add(a);
|
||||
}
|
||||
else
|
||||
{
|
||||
list = (IList)obj;
|
||||
list.Add(a);
|
||||
}
|
||||
attributes[attrType] = list;
|
||||
}
|
||||
|
||||
[Obsolete("Use 'object[oid]' syntax instead")]
|
||||
public Attribute Get(DerObjectIdentifier oid)
|
||||
{
|
||||
return this[oid];
|
||||
}
|
||||
|
||||
public Asn1EncodableVector GetAll(DerObjectIdentifier oid)
|
||||
{
|
||||
Asn1EncodableVector asn1EncodableVector = new Asn1EncodableVector();
|
||||
object obj = attributes[oid];
|
||||
if (obj is IList)
|
||||
{
|
||||
foreach (Attribute item in (IList)obj)
|
||||
{
|
||||
asn1EncodableVector.Add(item);
|
||||
}
|
||||
}
|
||||
else if (obj != null)
|
||||
{
|
||||
asn1EncodableVector.Add((Attribute)obj);
|
||||
}
|
||||
return asn1EncodableVector;
|
||||
}
|
||||
|
||||
public IDictionary ToDictionary()
|
||||
{
|
||||
return Platform.CreateHashtable(attributes);
|
||||
}
|
||||
|
||||
[Obsolete("Use 'ToDictionary' instead")]
|
||||
public Hashtable ToHashtable()
|
||||
{
|
||||
return new Hashtable(attributes);
|
||||
}
|
||||
|
||||
public Asn1EncodableVector ToAsn1EncodableVector()
|
||||
{
|
||||
Asn1EncodableVector asn1EncodableVector = new Asn1EncodableVector();
|
||||
foreach (object value in attributes.Values)
|
||||
{
|
||||
if (value is IList)
|
||||
{
|
||||
foreach (object item in (IList)value)
|
||||
{
|
||||
asn1EncodableVector.Add(Attribute.GetInstance(item));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
asn1EncodableVector.Add(Attribute.GetInstance(value));
|
||||
}
|
||||
}
|
||||
return asn1EncodableVector;
|
||||
}
|
||||
|
||||
public Attributes ToAttributes()
|
||||
{
|
||||
return new Attributes(ToAsn1EncodableVector());
|
||||
}
|
||||
|
||||
public AttributeTable Add(DerObjectIdentifier attrType, Asn1Encodable attrValue)
|
||||
{
|
||||
AttributeTable attributeTable = new AttributeTable(attributes);
|
||||
attributeTable.AddAttribute(new Attribute(attrType, new DerSet(attrValue)));
|
||||
return attributeTable;
|
||||
}
|
||||
|
||||
public AttributeTable Remove(DerObjectIdentifier attrType)
|
||||
{
|
||||
AttributeTable attributeTable = new AttributeTable(attributes);
|
||||
attributeTable.attributes.Remove(attrType);
|
||||
return attributeTable;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
namespace Org.BouncyCastle.Asn1.Cms;
|
||||
|
||||
public class Attributes : Asn1Encodable
|
||||
{
|
||||
private readonly Asn1Set attributes;
|
||||
|
||||
private Attributes(Asn1Set attributes)
|
||||
{
|
||||
this.attributes = attributes;
|
||||
}
|
||||
|
||||
public Attributes(Asn1EncodableVector v)
|
||||
{
|
||||
attributes = new BerSet(v);
|
||||
}
|
||||
|
||||
public static Attributes GetInstance(object obj)
|
||||
{
|
||||
if (obj is Attributes)
|
||||
{
|
||||
return (Attributes)obj;
|
||||
}
|
||||
if (obj != null)
|
||||
{
|
||||
return new Attributes(Asn1Set.GetInstance(obj));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public virtual Attribute[] GetAttributes()
|
||||
{
|
||||
Attribute[] array = new Attribute[attributes.Count];
|
||||
for (int i = 0; i != array.Length; i++)
|
||||
{
|
||||
array[i] = Attribute.GetInstance(attributes[i]);
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public override Asn1Object ToAsn1Object()
|
||||
{
|
||||
return attributes;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1.Cms;
|
||||
|
||||
public class AuthEnvelopedData : Asn1Encodable
|
||||
{
|
||||
private DerInteger version;
|
||||
|
||||
private OriginatorInfo originatorInfo;
|
||||
|
||||
private Asn1Set recipientInfos;
|
||||
|
||||
private EncryptedContentInfo authEncryptedContentInfo;
|
||||
|
||||
private Asn1Set authAttrs;
|
||||
|
||||
private Asn1OctetString mac;
|
||||
|
||||
private Asn1Set unauthAttrs;
|
||||
|
||||
public DerInteger Version => version;
|
||||
|
||||
public OriginatorInfo OriginatorInfo => originatorInfo;
|
||||
|
||||
public Asn1Set RecipientInfos => recipientInfos;
|
||||
|
||||
public EncryptedContentInfo AuthEncryptedContentInfo => authEncryptedContentInfo;
|
||||
|
||||
public Asn1Set AuthAttrs => authAttrs;
|
||||
|
||||
public Asn1OctetString Mac => mac;
|
||||
|
||||
public Asn1Set UnauthAttrs => unauthAttrs;
|
||||
|
||||
public AuthEnvelopedData(OriginatorInfo originatorInfo, Asn1Set recipientInfos, EncryptedContentInfo authEncryptedContentInfo, Asn1Set authAttrs, Asn1OctetString mac, Asn1Set unauthAttrs)
|
||||
{
|
||||
version = new DerInteger(0);
|
||||
this.originatorInfo = originatorInfo;
|
||||
this.recipientInfos = recipientInfos;
|
||||
this.authEncryptedContentInfo = authEncryptedContentInfo;
|
||||
this.authAttrs = authAttrs;
|
||||
this.mac = mac;
|
||||
this.unauthAttrs = unauthAttrs;
|
||||
}
|
||||
|
||||
private AuthEnvelopedData(Asn1Sequence seq)
|
||||
{
|
||||
int num = 0;
|
||||
Asn1Object asn1Object = seq[num++].ToAsn1Object();
|
||||
version = (DerInteger)asn1Object;
|
||||
asn1Object = seq[num++].ToAsn1Object();
|
||||
if (asn1Object is Asn1TaggedObject)
|
||||
{
|
||||
originatorInfo = OriginatorInfo.GetInstance((Asn1TaggedObject)asn1Object, explicitly: false);
|
||||
asn1Object = seq[num++].ToAsn1Object();
|
||||
}
|
||||
recipientInfos = Asn1Set.GetInstance(asn1Object);
|
||||
asn1Object = seq[num++].ToAsn1Object();
|
||||
authEncryptedContentInfo = EncryptedContentInfo.GetInstance(asn1Object);
|
||||
asn1Object = seq[num++].ToAsn1Object();
|
||||
if (asn1Object is Asn1TaggedObject)
|
||||
{
|
||||
authAttrs = Asn1Set.GetInstance((Asn1TaggedObject)asn1Object, explicitly: false);
|
||||
asn1Object = seq[num++].ToAsn1Object();
|
||||
}
|
||||
mac = Asn1OctetString.GetInstance(asn1Object);
|
||||
if (seq.Count > num)
|
||||
{
|
||||
asn1Object = seq[num++].ToAsn1Object();
|
||||
unauthAttrs = Asn1Set.GetInstance((Asn1TaggedObject)asn1Object, explicitly: false);
|
||||
}
|
||||
}
|
||||
|
||||
public static AuthEnvelopedData GetInstance(Asn1TaggedObject obj, bool isExplicit)
|
||||
{
|
||||
return GetInstance(Asn1Sequence.GetInstance(obj, isExplicit));
|
||||
}
|
||||
|
||||
public static AuthEnvelopedData GetInstance(object obj)
|
||||
{
|
||||
if (obj == null || obj is AuthEnvelopedData)
|
||||
{
|
||||
return (AuthEnvelopedData)obj;
|
||||
}
|
||||
if (obj is Asn1Sequence)
|
||||
{
|
||||
return new AuthEnvelopedData((Asn1Sequence)obj);
|
||||
}
|
||||
throw new ArgumentException("Invalid AuthEnvelopedData: " + Platform.GetTypeName(obj));
|
||||
}
|
||||
|
||||
public override Asn1Object ToAsn1Object()
|
||||
{
|
||||
Asn1EncodableVector asn1EncodableVector = new Asn1EncodableVector(version);
|
||||
if (originatorInfo != null)
|
||||
{
|
||||
asn1EncodableVector.Add(new DerTaggedObject(explicitly: false, 0, originatorInfo));
|
||||
}
|
||||
asn1EncodableVector.Add(recipientInfos, authEncryptedContentInfo);
|
||||
if (authAttrs != null)
|
||||
{
|
||||
asn1EncodableVector.Add(new DerTaggedObject(explicitly: false, 1, authAttrs));
|
||||
}
|
||||
asn1EncodableVector.Add(mac);
|
||||
if (unauthAttrs != null)
|
||||
{
|
||||
asn1EncodableVector.Add(new DerTaggedObject(explicitly: false, 2, unauthAttrs));
|
||||
}
|
||||
return new BerSequence(asn1EncodableVector);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
namespace Org.BouncyCastle.Asn1.Cms;
|
||||
|
||||
public class AuthEnvelopedDataParser
|
||||
{
|
||||
private Asn1SequenceParser seq;
|
||||
|
||||
private DerInteger version;
|
||||
|
||||
private IAsn1Convertible nextObject;
|
||||
|
||||
private bool originatorInfoCalled;
|
||||
|
||||
public DerInteger Version => version;
|
||||
|
||||
public AuthEnvelopedDataParser(Asn1SequenceParser seq)
|
||||
{
|
||||
this.seq = seq;
|
||||
version = (DerInteger)seq.ReadObject();
|
||||
}
|
||||
|
||||
public OriginatorInfo GetOriginatorInfo()
|
||||
{
|
||||
originatorInfoCalled = true;
|
||||
if (nextObject == null)
|
||||
{
|
||||
nextObject = seq.ReadObject();
|
||||
}
|
||||
if (nextObject is Asn1TaggedObjectParser && ((Asn1TaggedObjectParser)nextObject).TagNo == 0)
|
||||
{
|
||||
Asn1SequenceParser asn1SequenceParser = (Asn1SequenceParser)((Asn1TaggedObjectParser)nextObject).GetObjectParser(16, isExplicit: false);
|
||||
nextObject = null;
|
||||
return OriginatorInfo.GetInstance(asn1SequenceParser.ToAsn1Object());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public Asn1SetParser GetRecipientInfos()
|
||||
{
|
||||
if (!originatorInfoCalled)
|
||||
{
|
||||
GetOriginatorInfo();
|
||||
}
|
||||
if (nextObject == null)
|
||||
{
|
||||
nextObject = seq.ReadObject();
|
||||
}
|
||||
Asn1SetParser result = (Asn1SetParser)nextObject;
|
||||
nextObject = null;
|
||||
return result;
|
||||
}
|
||||
|
||||
public EncryptedContentInfoParser GetAuthEncryptedContentInfo()
|
||||
{
|
||||
if (nextObject == null)
|
||||
{
|
||||
nextObject = seq.ReadObject();
|
||||
}
|
||||
if (nextObject != null)
|
||||
{
|
||||
Asn1SequenceParser asn1SequenceParser = (Asn1SequenceParser)nextObject;
|
||||
nextObject = null;
|
||||
return new EncryptedContentInfoParser(asn1SequenceParser);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public Asn1SetParser GetAuthAttrs()
|
||||
{
|
||||
if (nextObject == null)
|
||||
{
|
||||
nextObject = seq.ReadObject();
|
||||
}
|
||||
if (nextObject is Asn1TaggedObjectParser)
|
||||
{
|
||||
IAsn1Convertible asn1Convertible = nextObject;
|
||||
nextObject = null;
|
||||
return (Asn1SetParser)((Asn1TaggedObjectParser)asn1Convertible).GetObjectParser(17, isExplicit: false);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public Asn1OctetString GetMac()
|
||||
{
|
||||
if (nextObject == null)
|
||||
{
|
||||
nextObject = seq.ReadObject();
|
||||
}
|
||||
IAsn1Convertible asn1Convertible = nextObject;
|
||||
nextObject = null;
|
||||
return Asn1OctetString.GetInstance(asn1Convertible.ToAsn1Object());
|
||||
}
|
||||
|
||||
public Asn1SetParser GetUnauthAttrs()
|
||||
{
|
||||
if (nextObject == null)
|
||||
{
|
||||
nextObject = seq.ReadObject();
|
||||
}
|
||||
if (nextObject != null)
|
||||
{
|
||||
IAsn1Convertible asn1Convertible = nextObject;
|
||||
nextObject = null;
|
||||
return (Asn1SetParser)((Asn1TaggedObjectParser)asn1Convertible).GetObjectParser(17, isExplicit: false);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,174 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Asn1.X509;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1.Cms;
|
||||
|
||||
public class AuthenticatedData : Asn1Encodable
|
||||
{
|
||||
private DerInteger version;
|
||||
|
||||
private OriginatorInfo originatorInfo;
|
||||
|
||||
private Asn1Set recipientInfos;
|
||||
|
||||
private AlgorithmIdentifier macAlgorithm;
|
||||
|
||||
private AlgorithmIdentifier digestAlgorithm;
|
||||
|
||||
private ContentInfo encapsulatedContentInfo;
|
||||
|
||||
private Asn1Set authAttrs;
|
||||
|
||||
private Asn1OctetString mac;
|
||||
|
||||
private Asn1Set unauthAttrs;
|
||||
|
||||
public DerInteger Version => version;
|
||||
|
||||
public OriginatorInfo OriginatorInfo => originatorInfo;
|
||||
|
||||
public Asn1Set RecipientInfos => recipientInfos;
|
||||
|
||||
public AlgorithmIdentifier MacAlgorithm => macAlgorithm;
|
||||
|
||||
public AlgorithmIdentifier DigestAlgorithm => digestAlgorithm;
|
||||
|
||||
public ContentInfo EncapsulatedContentInfo => encapsulatedContentInfo;
|
||||
|
||||
public Asn1Set AuthAttrs => authAttrs;
|
||||
|
||||
public Asn1OctetString Mac => mac;
|
||||
|
||||
public Asn1Set UnauthAttrs => unauthAttrs;
|
||||
|
||||
public AuthenticatedData(OriginatorInfo originatorInfo, Asn1Set recipientInfos, AlgorithmIdentifier macAlgorithm, AlgorithmIdentifier digestAlgorithm, ContentInfo encapsulatedContent, Asn1Set authAttrs, Asn1OctetString mac, Asn1Set unauthAttrs)
|
||||
{
|
||||
if ((digestAlgorithm != null || authAttrs != null) && (digestAlgorithm == null || authAttrs == null))
|
||||
{
|
||||
throw new ArgumentException("digestAlgorithm and authAttrs must be set together");
|
||||
}
|
||||
version = new DerInteger(CalculateVersion(originatorInfo));
|
||||
this.originatorInfo = originatorInfo;
|
||||
this.macAlgorithm = macAlgorithm;
|
||||
this.digestAlgorithm = digestAlgorithm;
|
||||
this.recipientInfos = recipientInfos;
|
||||
encapsulatedContentInfo = encapsulatedContent;
|
||||
this.authAttrs = authAttrs;
|
||||
this.mac = mac;
|
||||
this.unauthAttrs = unauthAttrs;
|
||||
}
|
||||
|
||||
private AuthenticatedData(Asn1Sequence seq)
|
||||
{
|
||||
int num = 0;
|
||||
version = (DerInteger)seq[num++];
|
||||
Asn1Encodable asn1Encodable = seq[num++];
|
||||
if (asn1Encodable is Asn1TaggedObject)
|
||||
{
|
||||
originatorInfo = OriginatorInfo.GetInstance((Asn1TaggedObject)asn1Encodable, explicitly: false);
|
||||
asn1Encodable = seq[num++];
|
||||
}
|
||||
recipientInfos = Asn1Set.GetInstance(asn1Encodable);
|
||||
macAlgorithm = AlgorithmIdentifier.GetInstance(seq[num++]);
|
||||
asn1Encodable = seq[num++];
|
||||
if (asn1Encodable is Asn1TaggedObject)
|
||||
{
|
||||
digestAlgorithm = AlgorithmIdentifier.GetInstance((Asn1TaggedObject)asn1Encodable, explicitly: false);
|
||||
asn1Encodable = seq[num++];
|
||||
}
|
||||
encapsulatedContentInfo = ContentInfo.GetInstance(asn1Encodable);
|
||||
asn1Encodable = seq[num++];
|
||||
if (asn1Encodable is Asn1TaggedObject)
|
||||
{
|
||||
authAttrs = Asn1Set.GetInstance((Asn1TaggedObject)asn1Encodable, explicitly: false);
|
||||
asn1Encodable = seq[num++];
|
||||
}
|
||||
mac = Asn1OctetString.GetInstance(asn1Encodable);
|
||||
if (seq.Count > num)
|
||||
{
|
||||
unauthAttrs = Asn1Set.GetInstance((Asn1TaggedObject)seq[num], explicitly: false);
|
||||
}
|
||||
}
|
||||
|
||||
public static AuthenticatedData GetInstance(Asn1TaggedObject obj, bool isExplicit)
|
||||
{
|
||||
return GetInstance(Asn1Sequence.GetInstance(obj, isExplicit));
|
||||
}
|
||||
|
||||
public static AuthenticatedData GetInstance(object obj)
|
||||
{
|
||||
if (obj == null || obj is AuthenticatedData)
|
||||
{
|
||||
return (AuthenticatedData)obj;
|
||||
}
|
||||
if (obj is Asn1Sequence)
|
||||
{
|
||||
return new AuthenticatedData((Asn1Sequence)obj);
|
||||
}
|
||||
throw new ArgumentException("Invalid AuthenticatedData: " + Platform.GetTypeName(obj));
|
||||
}
|
||||
|
||||
public override Asn1Object ToAsn1Object()
|
||||
{
|
||||
Asn1EncodableVector asn1EncodableVector = new Asn1EncodableVector(version);
|
||||
if (originatorInfo != null)
|
||||
{
|
||||
asn1EncodableVector.Add(new DerTaggedObject(explicitly: false, 0, originatorInfo));
|
||||
}
|
||||
asn1EncodableVector.Add(recipientInfos, macAlgorithm);
|
||||
if (digestAlgorithm != null)
|
||||
{
|
||||
asn1EncodableVector.Add(new DerTaggedObject(explicitly: false, 1, digestAlgorithm));
|
||||
}
|
||||
asn1EncodableVector.Add(encapsulatedContentInfo);
|
||||
if (authAttrs != null)
|
||||
{
|
||||
asn1EncodableVector.Add(new DerTaggedObject(explicitly: false, 2, authAttrs));
|
||||
}
|
||||
asn1EncodableVector.Add(mac);
|
||||
if (unauthAttrs != null)
|
||||
{
|
||||
asn1EncodableVector.Add(new DerTaggedObject(explicitly: false, 3, unauthAttrs));
|
||||
}
|
||||
return new BerSequence(asn1EncodableVector);
|
||||
}
|
||||
|
||||
public static int CalculateVersion(OriginatorInfo origInfo)
|
||||
{
|
||||
if (origInfo == null)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
int result = 0;
|
||||
foreach (object certificate in origInfo.Certificates)
|
||||
{
|
||||
if (certificate is Asn1TaggedObject)
|
||||
{
|
||||
Asn1TaggedObject asn1TaggedObject = (Asn1TaggedObject)certificate;
|
||||
if (asn1TaggedObject.TagNo == 2)
|
||||
{
|
||||
result = 1;
|
||||
}
|
||||
else if (asn1TaggedObject.TagNo == 3)
|
||||
{
|
||||
result = 3;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
foreach (object crl in origInfo.Crls)
|
||||
{
|
||||
if (crl is Asn1TaggedObject)
|
||||
{
|
||||
Asn1TaggedObject asn1TaggedObject2 = (Asn1TaggedObject)crl;
|
||||
if (asn1TaggedObject2.TagNo == 1)
|
||||
{
|
||||
result = 3;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,139 @@
|
||||
using Org.BouncyCastle.Asn1.X509;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1.Cms;
|
||||
|
||||
public class AuthenticatedDataParser
|
||||
{
|
||||
private Asn1SequenceParser seq;
|
||||
|
||||
private DerInteger version;
|
||||
|
||||
private IAsn1Convertible nextObject;
|
||||
|
||||
private bool originatorInfoCalled;
|
||||
|
||||
public DerInteger Version => version;
|
||||
|
||||
public AuthenticatedDataParser(Asn1SequenceParser seq)
|
||||
{
|
||||
this.seq = seq;
|
||||
version = (DerInteger)seq.ReadObject();
|
||||
}
|
||||
|
||||
public OriginatorInfo GetOriginatorInfo()
|
||||
{
|
||||
originatorInfoCalled = true;
|
||||
if (nextObject == null)
|
||||
{
|
||||
nextObject = seq.ReadObject();
|
||||
}
|
||||
if (nextObject is Asn1TaggedObjectParser && ((Asn1TaggedObjectParser)nextObject).TagNo == 0)
|
||||
{
|
||||
Asn1SequenceParser asn1SequenceParser = (Asn1SequenceParser)((Asn1TaggedObjectParser)nextObject).GetObjectParser(16, isExplicit: false);
|
||||
nextObject = null;
|
||||
return OriginatorInfo.GetInstance(asn1SequenceParser.ToAsn1Object());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public Asn1SetParser GetRecipientInfos()
|
||||
{
|
||||
if (!originatorInfoCalled)
|
||||
{
|
||||
GetOriginatorInfo();
|
||||
}
|
||||
if (nextObject == null)
|
||||
{
|
||||
nextObject = seq.ReadObject();
|
||||
}
|
||||
Asn1SetParser result = (Asn1SetParser)nextObject;
|
||||
nextObject = null;
|
||||
return result;
|
||||
}
|
||||
|
||||
public AlgorithmIdentifier GetMacAlgorithm()
|
||||
{
|
||||
if (nextObject == null)
|
||||
{
|
||||
nextObject = seq.ReadObject();
|
||||
}
|
||||
if (nextObject != null)
|
||||
{
|
||||
Asn1SequenceParser asn1SequenceParser = (Asn1SequenceParser)nextObject;
|
||||
nextObject = null;
|
||||
return AlgorithmIdentifier.GetInstance(asn1SequenceParser.ToAsn1Object());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public AlgorithmIdentifier GetDigestAlgorithm()
|
||||
{
|
||||
if (nextObject == null)
|
||||
{
|
||||
nextObject = seq.ReadObject();
|
||||
}
|
||||
if (nextObject is Asn1TaggedObjectParser)
|
||||
{
|
||||
AlgorithmIdentifier instance = AlgorithmIdentifier.GetInstance((Asn1TaggedObject)nextObject.ToAsn1Object(), explicitly: false);
|
||||
nextObject = null;
|
||||
return instance;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public ContentInfoParser GetEnapsulatedContentInfo()
|
||||
{
|
||||
if (nextObject == null)
|
||||
{
|
||||
nextObject = seq.ReadObject();
|
||||
}
|
||||
if (nextObject != null)
|
||||
{
|
||||
Asn1SequenceParser asn1SequenceParser = (Asn1SequenceParser)nextObject;
|
||||
nextObject = null;
|
||||
return new ContentInfoParser(asn1SequenceParser);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public Asn1SetParser GetAuthAttrs()
|
||||
{
|
||||
if (nextObject == null)
|
||||
{
|
||||
nextObject = seq.ReadObject();
|
||||
}
|
||||
if (nextObject is Asn1TaggedObjectParser)
|
||||
{
|
||||
IAsn1Convertible asn1Convertible = nextObject;
|
||||
nextObject = null;
|
||||
return (Asn1SetParser)((Asn1TaggedObjectParser)asn1Convertible).GetObjectParser(17, isExplicit: false);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public Asn1OctetString GetMac()
|
||||
{
|
||||
if (nextObject == null)
|
||||
{
|
||||
nextObject = seq.ReadObject();
|
||||
}
|
||||
IAsn1Convertible asn1Convertible = nextObject;
|
||||
nextObject = null;
|
||||
return Asn1OctetString.GetInstance(asn1Convertible.ToAsn1Object());
|
||||
}
|
||||
|
||||
public Asn1SetParser GetUnauthAttrs()
|
||||
{
|
||||
if (nextObject == null)
|
||||
{
|
||||
nextObject = seq.ReadObject();
|
||||
}
|
||||
if (nextObject != null)
|
||||
{
|
||||
IAsn1Convertible asn1Convertible = nextObject;
|
||||
nextObject = null;
|
||||
return (Asn1SetParser)((Asn1TaggedObjectParser)asn1Convertible).GetObjectParser(17, isExplicit: false);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
using Org.BouncyCastle.Asn1.Pkcs;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1.Cms;
|
||||
|
||||
public abstract class CmsAttributes
|
||||
{
|
||||
public static readonly DerObjectIdentifier ContentType = PkcsObjectIdentifiers.Pkcs9AtContentType;
|
||||
|
||||
public static readonly DerObjectIdentifier MessageDigest = PkcsObjectIdentifiers.Pkcs9AtMessageDigest;
|
||||
|
||||
public static readonly DerObjectIdentifier SigningTime = PkcsObjectIdentifiers.Pkcs9AtSigningTime;
|
||||
|
||||
public static readonly DerObjectIdentifier CounterSignature = PkcsObjectIdentifiers.Pkcs9AtCounterSignature;
|
||||
|
||||
public static readonly DerObjectIdentifier ContentHint = PkcsObjectIdentifiers.IdAAContentHint;
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
using Org.BouncyCastle.Asn1.Pkcs;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1.Cms;
|
||||
|
||||
public abstract class CmsObjectIdentifiers
|
||||
{
|
||||
public static readonly DerObjectIdentifier Data = PkcsObjectIdentifiers.Data;
|
||||
|
||||
public static readonly DerObjectIdentifier SignedData = PkcsObjectIdentifiers.SignedData;
|
||||
|
||||
public static readonly DerObjectIdentifier EnvelopedData = PkcsObjectIdentifiers.EnvelopedData;
|
||||
|
||||
public static readonly DerObjectIdentifier SignedAndEnvelopedData = PkcsObjectIdentifiers.SignedAndEnvelopedData;
|
||||
|
||||
public static readonly DerObjectIdentifier DigestedData = PkcsObjectIdentifiers.DigestedData;
|
||||
|
||||
public static readonly DerObjectIdentifier EncryptedData = PkcsObjectIdentifiers.EncryptedData;
|
||||
|
||||
public static readonly DerObjectIdentifier AuthenticatedData = PkcsObjectIdentifiers.IdCTAuthData;
|
||||
|
||||
public static readonly DerObjectIdentifier CompressedData = PkcsObjectIdentifiers.IdCTCompressedData;
|
||||
|
||||
public static readonly DerObjectIdentifier AuthEnvelopedData = PkcsObjectIdentifiers.IdCTAuthEnvelopedData;
|
||||
|
||||
public static readonly DerObjectIdentifier timestampedData = PkcsObjectIdentifiers.IdCTTimestampedData;
|
||||
|
||||
public static readonly DerObjectIdentifier id_ri = new DerObjectIdentifier("1.3.6.1.5.5.7.16");
|
||||
|
||||
public static readonly DerObjectIdentifier id_ri_ocsp_response = id_ri.Branch("2");
|
||||
|
||||
public static readonly DerObjectIdentifier id_ri_scvp = id_ri.Branch("4");
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Asn1.X509;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1.Cms;
|
||||
|
||||
public class CompressedData : Asn1Encodable
|
||||
{
|
||||
private DerInteger version;
|
||||
|
||||
private AlgorithmIdentifier compressionAlgorithm;
|
||||
|
||||
private ContentInfo encapContentInfo;
|
||||
|
||||
public DerInteger Version => version;
|
||||
|
||||
public AlgorithmIdentifier CompressionAlgorithmIdentifier => compressionAlgorithm;
|
||||
|
||||
public ContentInfo EncapContentInfo => encapContentInfo;
|
||||
|
||||
public CompressedData(AlgorithmIdentifier compressionAlgorithm, ContentInfo encapContentInfo)
|
||||
{
|
||||
version = new DerInteger(0);
|
||||
this.compressionAlgorithm = compressionAlgorithm;
|
||||
this.encapContentInfo = encapContentInfo;
|
||||
}
|
||||
|
||||
public CompressedData(Asn1Sequence seq)
|
||||
{
|
||||
version = (DerInteger)seq[0];
|
||||
compressionAlgorithm = AlgorithmIdentifier.GetInstance(seq[1]);
|
||||
encapContentInfo = ContentInfo.GetInstance(seq[2]);
|
||||
}
|
||||
|
||||
public static CompressedData GetInstance(Asn1TaggedObject ato, bool explicitly)
|
||||
{
|
||||
return GetInstance(Asn1Sequence.GetInstance(ato, explicitly));
|
||||
}
|
||||
|
||||
public static CompressedData GetInstance(object obj)
|
||||
{
|
||||
if (obj == null || obj is CompressedData)
|
||||
{
|
||||
return (CompressedData)obj;
|
||||
}
|
||||
if (obj is Asn1Sequence)
|
||||
{
|
||||
return new CompressedData((Asn1Sequence)obj);
|
||||
}
|
||||
throw new ArgumentException("Invalid CompressedData: " + Platform.GetTypeName(obj));
|
||||
}
|
||||
|
||||
public override Asn1Object ToAsn1Object()
|
||||
{
|
||||
return new BerSequence(version, compressionAlgorithm, encapContentInfo);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
using Org.BouncyCastle.Asn1.X509;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1.Cms;
|
||||
|
||||
public class CompressedDataParser
|
||||
{
|
||||
private DerInteger _version;
|
||||
|
||||
private AlgorithmIdentifier _compressionAlgorithm;
|
||||
|
||||
private ContentInfoParser _encapContentInfo;
|
||||
|
||||
public DerInteger Version => _version;
|
||||
|
||||
public AlgorithmIdentifier CompressionAlgorithmIdentifier => _compressionAlgorithm;
|
||||
|
||||
public CompressedDataParser(Asn1SequenceParser seq)
|
||||
{
|
||||
_version = (DerInteger)seq.ReadObject();
|
||||
_compressionAlgorithm = AlgorithmIdentifier.GetInstance(seq.ReadObject().ToAsn1Object());
|
||||
_encapContentInfo = new ContentInfoParser((Asn1SequenceParser)seq.ReadObject());
|
||||
}
|
||||
|
||||
public ContentInfoParser GetEncapContentInfo()
|
||||
{
|
||||
return _encapContentInfo;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1.Cms;
|
||||
|
||||
public class ContentInfo : Asn1Encodable
|
||||
{
|
||||
private readonly DerObjectIdentifier contentType;
|
||||
|
||||
private readonly Asn1Encodable content;
|
||||
|
||||
public DerObjectIdentifier ContentType => contentType;
|
||||
|
||||
public Asn1Encodable Content => content;
|
||||
|
||||
public static ContentInfo GetInstance(object obj)
|
||||
{
|
||||
if (obj == null || obj is ContentInfo)
|
||||
{
|
||||
return (ContentInfo)obj;
|
||||
}
|
||||
if (obj is Asn1Sequence)
|
||||
{
|
||||
return new ContentInfo((Asn1Sequence)obj);
|
||||
}
|
||||
throw new ArgumentException("unknown object in factory: " + Platform.GetTypeName(obj));
|
||||
}
|
||||
|
||||
public static ContentInfo GetInstance(Asn1TaggedObject obj, bool isExplicit)
|
||||
{
|
||||
return GetInstance(Asn1Sequence.GetInstance(obj, isExplicit));
|
||||
}
|
||||
|
||||
private ContentInfo(Asn1Sequence seq)
|
||||
{
|
||||
if (seq.Count < 1 || seq.Count > 2)
|
||||
{
|
||||
throw new ArgumentException("Bad sequence size: " + seq.Count, "seq");
|
||||
}
|
||||
contentType = (DerObjectIdentifier)seq[0];
|
||||
if (seq.Count > 1)
|
||||
{
|
||||
Asn1TaggedObject asn1TaggedObject = (Asn1TaggedObject)seq[1];
|
||||
if (!asn1TaggedObject.IsExplicit() || asn1TaggedObject.TagNo != 0)
|
||||
{
|
||||
throw new ArgumentException("Bad tag for 'content'", "seq");
|
||||
}
|
||||
content = asn1TaggedObject.GetObject();
|
||||
}
|
||||
}
|
||||
|
||||
public ContentInfo(DerObjectIdentifier contentType, Asn1Encodable content)
|
||||
{
|
||||
this.contentType = contentType;
|
||||
this.content = content;
|
||||
}
|
||||
|
||||
public override Asn1Object ToAsn1Object()
|
||||
{
|
||||
Asn1EncodableVector asn1EncodableVector = new Asn1EncodableVector(contentType);
|
||||
if (content != null)
|
||||
{
|
||||
asn1EncodableVector.Add(new BerTaggedObject(0, content));
|
||||
}
|
||||
return new BerSequence(asn1EncodableVector);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
namespace Org.BouncyCastle.Asn1.Cms;
|
||||
|
||||
public class ContentInfoParser
|
||||
{
|
||||
private DerObjectIdentifier contentType;
|
||||
|
||||
private Asn1TaggedObjectParser content;
|
||||
|
||||
public DerObjectIdentifier ContentType => contentType;
|
||||
|
||||
public ContentInfoParser(Asn1SequenceParser seq)
|
||||
{
|
||||
contentType = (DerObjectIdentifier)seq.ReadObject();
|
||||
content = (Asn1TaggedObjectParser)seq.ReadObject();
|
||||
}
|
||||
|
||||
public IAsn1Convertible GetContent(int tag)
|
||||
{
|
||||
if (content == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return content.GetObjectParser(tag, isExplicit: true);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1.Cms.Ecc;
|
||||
|
||||
public class MQVuserKeyingMaterial : Asn1Encodable
|
||||
{
|
||||
private OriginatorPublicKey ephemeralPublicKey;
|
||||
|
||||
private Asn1OctetString addedukm;
|
||||
|
||||
public OriginatorPublicKey EphemeralPublicKey => ephemeralPublicKey;
|
||||
|
||||
public Asn1OctetString AddedUkm => addedukm;
|
||||
|
||||
public MQVuserKeyingMaterial(OriginatorPublicKey ephemeralPublicKey, Asn1OctetString addedukm)
|
||||
{
|
||||
this.ephemeralPublicKey = ephemeralPublicKey;
|
||||
this.addedukm = addedukm;
|
||||
}
|
||||
|
||||
private MQVuserKeyingMaterial(Asn1Sequence seq)
|
||||
{
|
||||
ephemeralPublicKey = OriginatorPublicKey.GetInstance(seq[0]);
|
||||
if (seq.Count > 1)
|
||||
{
|
||||
addedukm = Asn1OctetString.GetInstance((Asn1TaggedObject)seq[1], isExplicit: true);
|
||||
}
|
||||
}
|
||||
|
||||
public static MQVuserKeyingMaterial GetInstance(Asn1TaggedObject obj, bool isExplicit)
|
||||
{
|
||||
return GetInstance(Asn1Sequence.GetInstance(obj, isExplicit));
|
||||
}
|
||||
|
||||
public static MQVuserKeyingMaterial GetInstance(object obj)
|
||||
{
|
||||
if (obj == null || obj is MQVuserKeyingMaterial)
|
||||
{
|
||||
return (MQVuserKeyingMaterial)obj;
|
||||
}
|
||||
if (obj is Asn1Sequence)
|
||||
{
|
||||
return new MQVuserKeyingMaterial((Asn1Sequence)obj);
|
||||
}
|
||||
throw new ArgumentException("Invalid MQVuserKeyingMaterial: " + Platform.GetTypeName(obj));
|
||||
}
|
||||
|
||||
public override Asn1Object ToAsn1Object()
|
||||
{
|
||||
Asn1EncodableVector asn1EncodableVector = new Asn1EncodableVector(ephemeralPublicKey);
|
||||
if (addedukm != null)
|
||||
{
|
||||
asn1EncodableVector.Add(new DerTaggedObject(explicitly: true, 0, addedukm));
|
||||
}
|
||||
return new DerSequence(asn1EncodableVector);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Asn1.X509;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1.Cms;
|
||||
|
||||
public class EncryptedContentInfo : Asn1Encodable
|
||||
{
|
||||
private DerObjectIdentifier contentType;
|
||||
|
||||
private AlgorithmIdentifier contentEncryptionAlgorithm;
|
||||
|
||||
private Asn1OctetString encryptedContent;
|
||||
|
||||
public DerObjectIdentifier ContentType => contentType;
|
||||
|
||||
public AlgorithmIdentifier ContentEncryptionAlgorithm => contentEncryptionAlgorithm;
|
||||
|
||||
public Asn1OctetString EncryptedContent => encryptedContent;
|
||||
|
||||
public EncryptedContentInfo(DerObjectIdentifier contentType, AlgorithmIdentifier contentEncryptionAlgorithm, Asn1OctetString encryptedContent)
|
||||
{
|
||||
this.contentType = contentType;
|
||||
this.contentEncryptionAlgorithm = contentEncryptionAlgorithm;
|
||||
this.encryptedContent = encryptedContent;
|
||||
}
|
||||
|
||||
public EncryptedContentInfo(Asn1Sequence seq)
|
||||
{
|
||||
contentType = (DerObjectIdentifier)seq[0];
|
||||
contentEncryptionAlgorithm = AlgorithmIdentifier.GetInstance(seq[1]);
|
||||
if (seq.Count > 2)
|
||||
{
|
||||
encryptedContent = Asn1OctetString.GetInstance((Asn1TaggedObject)seq[2], isExplicit: false);
|
||||
}
|
||||
}
|
||||
|
||||
public static EncryptedContentInfo GetInstance(object obj)
|
||||
{
|
||||
if (obj == null || obj is EncryptedContentInfo)
|
||||
{
|
||||
return (EncryptedContentInfo)obj;
|
||||
}
|
||||
if (obj is Asn1Sequence)
|
||||
{
|
||||
return new EncryptedContentInfo((Asn1Sequence)obj);
|
||||
}
|
||||
throw new ArgumentException("Invalid EncryptedContentInfo: " + Platform.GetTypeName(obj));
|
||||
}
|
||||
|
||||
public override Asn1Object ToAsn1Object()
|
||||
{
|
||||
Asn1EncodableVector asn1EncodableVector = new Asn1EncodableVector(contentType, contentEncryptionAlgorithm);
|
||||
if (encryptedContent != null)
|
||||
{
|
||||
asn1EncodableVector.Add(new BerTaggedObject(explicitly: false, 0, encryptedContent));
|
||||
}
|
||||
return new BerSequence(asn1EncodableVector);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
using Org.BouncyCastle.Asn1.X509;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1.Cms;
|
||||
|
||||
public class EncryptedContentInfoParser
|
||||
{
|
||||
private DerObjectIdentifier _contentType;
|
||||
|
||||
private AlgorithmIdentifier _contentEncryptionAlgorithm;
|
||||
|
||||
private Asn1TaggedObjectParser _encryptedContent;
|
||||
|
||||
public DerObjectIdentifier ContentType => _contentType;
|
||||
|
||||
public AlgorithmIdentifier ContentEncryptionAlgorithm => _contentEncryptionAlgorithm;
|
||||
|
||||
public EncryptedContentInfoParser(Asn1SequenceParser seq)
|
||||
{
|
||||
_contentType = (DerObjectIdentifier)seq.ReadObject();
|
||||
_contentEncryptionAlgorithm = AlgorithmIdentifier.GetInstance(seq.ReadObject().ToAsn1Object());
|
||||
_encryptedContent = (Asn1TaggedObjectParser)seq.ReadObject();
|
||||
}
|
||||
|
||||
public IAsn1Convertible GetEncryptedContent(int tag)
|
||||
{
|
||||
return _encryptedContent.GetObjectParser(tag, isExplicit: false);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1.Cms;
|
||||
|
||||
public class EncryptedData : Asn1Encodable
|
||||
{
|
||||
private readonly DerInteger version;
|
||||
|
||||
private readonly EncryptedContentInfo encryptedContentInfo;
|
||||
|
||||
private readonly Asn1Set unprotectedAttrs;
|
||||
|
||||
public virtual DerInteger Version => version;
|
||||
|
||||
public virtual EncryptedContentInfo EncryptedContentInfo => encryptedContentInfo;
|
||||
|
||||
public virtual Asn1Set UnprotectedAttrs => unprotectedAttrs;
|
||||
|
||||
public static EncryptedData GetInstance(object obj)
|
||||
{
|
||||
if (obj is EncryptedData)
|
||||
{
|
||||
return (EncryptedData)obj;
|
||||
}
|
||||
if (obj is Asn1Sequence)
|
||||
{
|
||||
return new EncryptedData((Asn1Sequence)obj);
|
||||
}
|
||||
throw new ArgumentException("Invalid EncryptedData: " + Platform.GetTypeName(obj));
|
||||
}
|
||||
|
||||
public EncryptedData(EncryptedContentInfo encInfo)
|
||||
: this(encInfo, null)
|
||||
{
|
||||
}
|
||||
|
||||
public EncryptedData(EncryptedContentInfo encInfo, Asn1Set unprotectedAttrs)
|
||||
{
|
||||
if (encInfo == null)
|
||||
{
|
||||
throw new ArgumentNullException("encInfo");
|
||||
}
|
||||
version = new DerInteger((unprotectedAttrs != null) ? 2 : 0);
|
||||
encryptedContentInfo = encInfo;
|
||||
this.unprotectedAttrs = unprotectedAttrs;
|
||||
}
|
||||
|
||||
private EncryptedData(Asn1Sequence seq)
|
||||
{
|
||||
if (seq == null)
|
||||
{
|
||||
throw new ArgumentNullException("seq");
|
||||
}
|
||||
if (seq.Count < 2 || seq.Count > 3)
|
||||
{
|
||||
throw new ArgumentException("Bad sequence size: " + seq.Count, "seq");
|
||||
}
|
||||
version = DerInteger.GetInstance(seq[0]);
|
||||
encryptedContentInfo = EncryptedContentInfo.GetInstance(seq[1]);
|
||||
if (seq.Count > 2)
|
||||
{
|
||||
unprotectedAttrs = Asn1Set.GetInstance((Asn1TaggedObject)seq[2], explicitly: false);
|
||||
}
|
||||
}
|
||||
|
||||
public override Asn1Object ToAsn1Object()
|
||||
{
|
||||
Asn1EncodableVector asn1EncodableVector = new Asn1EncodableVector(version, encryptedContentInfo);
|
||||
if (unprotectedAttrs != null)
|
||||
{
|
||||
asn1EncodableVector.Add(new BerTaggedObject(explicitly: false, 1, unprotectedAttrs));
|
||||
}
|
||||
return new BerSequence(asn1EncodableVector);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
using System;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1.Cms;
|
||||
|
||||
public class EnvelopedData : Asn1Encodable
|
||||
{
|
||||
private DerInteger version;
|
||||
|
||||
private OriginatorInfo originatorInfo;
|
||||
|
||||
private Asn1Set recipientInfos;
|
||||
|
||||
private EncryptedContentInfo encryptedContentInfo;
|
||||
|
||||
private Asn1Set unprotectedAttrs;
|
||||
|
||||
public DerInteger Version => version;
|
||||
|
||||
public OriginatorInfo OriginatorInfo => originatorInfo;
|
||||
|
||||
public Asn1Set RecipientInfos => recipientInfos;
|
||||
|
||||
public EncryptedContentInfo EncryptedContentInfo => encryptedContentInfo;
|
||||
|
||||
public Asn1Set UnprotectedAttrs => unprotectedAttrs;
|
||||
|
||||
public EnvelopedData(OriginatorInfo originatorInfo, Asn1Set recipientInfos, EncryptedContentInfo encryptedContentInfo, Asn1Set unprotectedAttrs)
|
||||
{
|
||||
version = new DerInteger(CalculateVersion(originatorInfo, recipientInfos, unprotectedAttrs));
|
||||
this.originatorInfo = originatorInfo;
|
||||
this.recipientInfos = recipientInfos;
|
||||
this.encryptedContentInfo = encryptedContentInfo;
|
||||
this.unprotectedAttrs = unprotectedAttrs;
|
||||
}
|
||||
|
||||
public EnvelopedData(OriginatorInfo originatorInfo, Asn1Set recipientInfos, EncryptedContentInfo encryptedContentInfo, Attributes unprotectedAttrs)
|
||||
{
|
||||
version = new DerInteger(CalculateVersion(originatorInfo, recipientInfos, Asn1Set.GetInstance(unprotectedAttrs)));
|
||||
this.originatorInfo = originatorInfo;
|
||||
this.recipientInfos = recipientInfos;
|
||||
this.encryptedContentInfo = encryptedContentInfo;
|
||||
this.unprotectedAttrs = Asn1Set.GetInstance(unprotectedAttrs);
|
||||
}
|
||||
|
||||
[Obsolete("Use 'GetInstance' instead")]
|
||||
public EnvelopedData(Asn1Sequence seq)
|
||||
{
|
||||
int num = 0;
|
||||
version = (DerInteger)seq[num++];
|
||||
object obj = seq[num++];
|
||||
if (obj is Asn1TaggedObject)
|
||||
{
|
||||
originatorInfo = OriginatorInfo.GetInstance((Asn1TaggedObject)obj, explicitly: false);
|
||||
obj = seq[num++];
|
||||
}
|
||||
recipientInfos = Asn1Set.GetInstance(obj);
|
||||
encryptedContentInfo = EncryptedContentInfo.GetInstance(seq[num++]);
|
||||
if (seq.Count > num)
|
||||
{
|
||||
unprotectedAttrs = Asn1Set.GetInstance((Asn1TaggedObject)seq[num], explicitly: false);
|
||||
}
|
||||
}
|
||||
|
||||
public static EnvelopedData GetInstance(Asn1TaggedObject obj, bool explicitly)
|
||||
{
|
||||
return GetInstance(Asn1Sequence.GetInstance(obj, explicitly));
|
||||
}
|
||||
|
||||
public static EnvelopedData GetInstance(object obj)
|
||||
{
|
||||
if (obj is EnvelopedData)
|
||||
{
|
||||
return (EnvelopedData)obj;
|
||||
}
|
||||
if (obj == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return new EnvelopedData(Asn1Sequence.GetInstance(obj));
|
||||
}
|
||||
|
||||
public override Asn1Object ToAsn1Object()
|
||||
{
|
||||
Asn1EncodableVector asn1EncodableVector = new Asn1EncodableVector(version);
|
||||
if (originatorInfo != null)
|
||||
{
|
||||
asn1EncodableVector.Add(new DerTaggedObject(explicitly: false, 0, originatorInfo));
|
||||
}
|
||||
asn1EncodableVector.Add(recipientInfos, encryptedContentInfo);
|
||||
if (unprotectedAttrs != null)
|
||||
{
|
||||
asn1EncodableVector.Add(new DerTaggedObject(explicitly: false, 1, unprotectedAttrs));
|
||||
}
|
||||
return new BerSequence(asn1EncodableVector);
|
||||
}
|
||||
|
||||
public static int CalculateVersion(OriginatorInfo originatorInfo, Asn1Set recipientInfos, Asn1Set unprotectedAttrs)
|
||||
{
|
||||
if (originatorInfo != null || unprotectedAttrs != null)
|
||||
{
|
||||
return 2;
|
||||
}
|
||||
foreach (object recipientInfo in recipientInfos)
|
||||
{
|
||||
RecipientInfo instance = RecipientInfo.GetInstance(recipientInfo);
|
||||
if (instance.Version.Value.IntValue != 0)
|
||||
{
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
namespace Org.BouncyCastle.Asn1.Cms;
|
||||
|
||||
public class EnvelopedDataParser
|
||||
{
|
||||
private Asn1SequenceParser _seq;
|
||||
|
||||
private DerInteger _version;
|
||||
|
||||
private IAsn1Convertible _nextObject;
|
||||
|
||||
private bool _originatorInfoCalled;
|
||||
|
||||
public DerInteger Version => _version;
|
||||
|
||||
public EnvelopedDataParser(Asn1SequenceParser seq)
|
||||
{
|
||||
_seq = seq;
|
||||
_version = (DerInteger)seq.ReadObject();
|
||||
}
|
||||
|
||||
public OriginatorInfo GetOriginatorInfo()
|
||||
{
|
||||
_originatorInfoCalled = true;
|
||||
if (_nextObject == null)
|
||||
{
|
||||
_nextObject = _seq.ReadObject();
|
||||
}
|
||||
if (_nextObject is Asn1TaggedObjectParser && ((Asn1TaggedObjectParser)_nextObject).TagNo == 0)
|
||||
{
|
||||
Asn1SequenceParser asn1SequenceParser = (Asn1SequenceParser)((Asn1TaggedObjectParser)_nextObject).GetObjectParser(16, isExplicit: false);
|
||||
_nextObject = null;
|
||||
return OriginatorInfo.GetInstance(asn1SequenceParser.ToAsn1Object());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public Asn1SetParser GetRecipientInfos()
|
||||
{
|
||||
if (!_originatorInfoCalled)
|
||||
{
|
||||
GetOriginatorInfo();
|
||||
}
|
||||
if (_nextObject == null)
|
||||
{
|
||||
_nextObject = _seq.ReadObject();
|
||||
}
|
||||
Asn1SetParser result = (Asn1SetParser)_nextObject;
|
||||
_nextObject = null;
|
||||
return result;
|
||||
}
|
||||
|
||||
public EncryptedContentInfoParser GetEncryptedContentInfo()
|
||||
{
|
||||
if (_nextObject == null)
|
||||
{
|
||||
_nextObject = _seq.ReadObject();
|
||||
}
|
||||
if (_nextObject != null)
|
||||
{
|
||||
Asn1SequenceParser seq = (Asn1SequenceParser)_nextObject;
|
||||
_nextObject = null;
|
||||
return new EncryptedContentInfoParser(seq);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public Asn1SetParser GetUnprotectedAttrs()
|
||||
{
|
||||
if (_nextObject == null)
|
||||
{
|
||||
_nextObject = _seq.ReadObject();
|
||||
}
|
||||
if (_nextObject != null)
|
||||
{
|
||||
IAsn1Convertible nextObject = _nextObject;
|
||||
_nextObject = null;
|
||||
return (Asn1SetParser)((Asn1TaggedObjectParser)nextObject).GetObjectParser(17, isExplicit: false);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1.Cms;
|
||||
|
||||
public class Evidence : Asn1Encodable, IAsn1Choice
|
||||
{
|
||||
private TimeStampTokenEvidence tstEvidence;
|
||||
|
||||
public virtual TimeStampTokenEvidence TstEvidence => tstEvidence;
|
||||
|
||||
public Evidence(TimeStampTokenEvidence tstEvidence)
|
||||
{
|
||||
this.tstEvidence = tstEvidence;
|
||||
}
|
||||
|
||||
private Evidence(Asn1TaggedObject tagged)
|
||||
{
|
||||
if (tagged.TagNo == 0)
|
||||
{
|
||||
tstEvidence = TimeStampTokenEvidence.GetInstance(tagged, isExplicit: false);
|
||||
}
|
||||
}
|
||||
|
||||
public static Evidence GetInstance(object obj)
|
||||
{
|
||||
if (obj is Evidence)
|
||||
{
|
||||
return (Evidence)obj;
|
||||
}
|
||||
if (obj is Asn1TaggedObject)
|
||||
{
|
||||
return new Evidence(Asn1TaggedObject.GetInstance(obj));
|
||||
}
|
||||
throw new ArgumentException("Unknown object in GetInstance: " + Platform.GetTypeName(obj), "obj");
|
||||
}
|
||||
|
||||
public override Asn1Object ToAsn1Object()
|
||||
{
|
||||
if (tstEvidence != null)
|
||||
{
|
||||
return new DerTaggedObject(explicitly: false, 0, tstEvidence);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Asn1.X509;
|
||||
using Org.BouncyCastle.Math;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1.Cms;
|
||||
|
||||
public class IssuerAndSerialNumber : Asn1Encodable
|
||||
{
|
||||
private X509Name name;
|
||||
|
||||
private DerInteger serialNumber;
|
||||
|
||||
public X509Name Name => name;
|
||||
|
||||
public DerInteger SerialNumber => serialNumber;
|
||||
|
||||
public static IssuerAndSerialNumber GetInstance(object obj)
|
||||
{
|
||||
if (obj == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
if (obj is IssuerAndSerialNumber result)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
return new IssuerAndSerialNumber(Asn1Sequence.GetInstance(obj));
|
||||
}
|
||||
|
||||
[Obsolete("Use GetInstance() instead")]
|
||||
public IssuerAndSerialNumber(Asn1Sequence seq)
|
||||
{
|
||||
name = X509Name.GetInstance(seq[0]);
|
||||
serialNumber = (DerInteger)seq[1];
|
||||
}
|
||||
|
||||
public IssuerAndSerialNumber(X509Name name, BigInteger serialNumber)
|
||||
{
|
||||
this.name = name;
|
||||
this.serialNumber = new DerInteger(serialNumber);
|
||||
}
|
||||
|
||||
public IssuerAndSerialNumber(X509Name name, DerInteger serialNumber)
|
||||
{
|
||||
this.name = name;
|
||||
this.serialNumber = serialNumber;
|
||||
}
|
||||
|
||||
public override Asn1Object ToAsn1Object()
|
||||
{
|
||||
return new DerSequence(name, serialNumber);
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user