init commit
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1.Cms;
|
||||
|
||||
public class KekIdentifier : Asn1Encodable
|
||||
{
|
||||
private Asn1OctetString keyIdentifier;
|
||||
|
||||
private DerGeneralizedTime date;
|
||||
|
||||
private OtherKeyAttribute other;
|
||||
|
||||
public Asn1OctetString KeyIdentifier => keyIdentifier;
|
||||
|
||||
public DerGeneralizedTime Date => date;
|
||||
|
||||
public OtherKeyAttribute Other => other;
|
||||
|
||||
public KekIdentifier(byte[] keyIdentifier, DerGeneralizedTime date, OtherKeyAttribute other)
|
||||
{
|
||||
this.keyIdentifier = new DerOctetString(keyIdentifier);
|
||||
this.date = date;
|
||||
this.other = other;
|
||||
}
|
||||
|
||||
public KekIdentifier(Asn1Sequence seq)
|
||||
{
|
||||
keyIdentifier = (Asn1OctetString)seq[0];
|
||||
switch (seq.Count)
|
||||
{
|
||||
case 2:
|
||||
if (seq[1] is DerGeneralizedTime)
|
||||
{
|
||||
date = (DerGeneralizedTime)seq[1];
|
||||
}
|
||||
else
|
||||
{
|
||||
other = OtherKeyAttribute.GetInstance(seq[2]);
|
||||
}
|
||||
break;
|
||||
case 3:
|
||||
date = (DerGeneralizedTime)seq[1];
|
||||
other = OtherKeyAttribute.GetInstance(seq[2]);
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentException("Invalid KekIdentifier");
|
||||
case 1:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public static KekIdentifier GetInstance(Asn1TaggedObject obj, bool explicitly)
|
||||
{
|
||||
return GetInstance(Asn1Sequence.GetInstance(obj, explicitly));
|
||||
}
|
||||
|
||||
public static KekIdentifier GetInstance(object obj)
|
||||
{
|
||||
if (obj == null || obj is KekIdentifier)
|
||||
{
|
||||
return (KekIdentifier)obj;
|
||||
}
|
||||
if (obj is Asn1Sequence)
|
||||
{
|
||||
return new KekIdentifier((Asn1Sequence)obj);
|
||||
}
|
||||
throw new ArgumentException("Invalid KekIdentifier: " + Platform.GetTypeName(obj));
|
||||
}
|
||||
|
||||
public override Asn1Object ToAsn1Object()
|
||||
{
|
||||
Asn1EncodableVector asn1EncodableVector = new Asn1EncodableVector(keyIdentifier);
|
||||
asn1EncodableVector.AddOptional(date, other);
|
||||
return new DerSequence(asn1EncodableVector);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Asn1.X509;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1.Cms;
|
||||
|
||||
public class KekRecipientInfo : Asn1Encodable
|
||||
{
|
||||
private DerInteger version;
|
||||
|
||||
private KekIdentifier kekID;
|
||||
|
||||
private AlgorithmIdentifier keyEncryptionAlgorithm;
|
||||
|
||||
private Asn1OctetString encryptedKey;
|
||||
|
||||
public DerInteger Version => version;
|
||||
|
||||
public KekIdentifier KekID => kekID;
|
||||
|
||||
public AlgorithmIdentifier KeyEncryptionAlgorithm => keyEncryptionAlgorithm;
|
||||
|
||||
public Asn1OctetString EncryptedKey => encryptedKey;
|
||||
|
||||
public KekRecipientInfo(KekIdentifier kekID, AlgorithmIdentifier keyEncryptionAlgorithm, Asn1OctetString encryptedKey)
|
||||
{
|
||||
version = new DerInteger(4);
|
||||
this.kekID = kekID;
|
||||
this.keyEncryptionAlgorithm = keyEncryptionAlgorithm;
|
||||
this.encryptedKey = encryptedKey;
|
||||
}
|
||||
|
||||
public KekRecipientInfo(Asn1Sequence seq)
|
||||
{
|
||||
version = (DerInteger)seq[0];
|
||||
kekID = KekIdentifier.GetInstance(seq[1]);
|
||||
keyEncryptionAlgorithm = AlgorithmIdentifier.GetInstance(seq[2]);
|
||||
encryptedKey = (Asn1OctetString)seq[3];
|
||||
}
|
||||
|
||||
public static KekRecipientInfo GetInstance(Asn1TaggedObject obj, bool explicitly)
|
||||
{
|
||||
return GetInstance(Asn1Sequence.GetInstance(obj, explicitly));
|
||||
}
|
||||
|
||||
public static KekRecipientInfo GetInstance(object obj)
|
||||
{
|
||||
if (obj == null || obj is KekRecipientInfo)
|
||||
{
|
||||
return (KekRecipientInfo)obj;
|
||||
}
|
||||
if (obj is Asn1Sequence)
|
||||
{
|
||||
return new KekRecipientInfo((Asn1Sequence)obj);
|
||||
}
|
||||
throw new ArgumentException("Invalid KekRecipientInfo: " + Platform.GetTypeName(obj));
|
||||
}
|
||||
|
||||
public override Asn1Object ToAsn1Object()
|
||||
{
|
||||
return new DerSequence(version, kekID, keyEncryptionAlgorithm, encryptedKey);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1.Cms;
|
||||
|
||||
public class KeyAgreeRecipientIdentifier : Asn1Encodable, IAsn1Choice
|
||||
{
|
||||
private readonly IssuerAndSerialNumber issuerSerial;
|
||||
|
||||
private readonly RecipientKeyIdentifier rKeyID;
|
||||
|
||||
public IssuerAndSerialNumber IssuerAndSerialNumber => issuerSerial;
|
||||
|
||||
public RecipientKeyIdentifier RKeyID => rKeyID;
|
||||
|
||||
public static KeyAgreeRecipientIdentifier GetInstance(Asn1TaggedObject obj, bool isExplicit)
|
||||
{
|
||||
return GetInstance(Asn1Sequence.GetInstance(obj, isExplicit));
|
||||
}
|
||||
|
||||
public static KeyAgreeRecipientIdentifier GetInstance(object obj)
|
||||
{
|
||||
if (obj == null || obj is KeyAgreeRecipientIdentifier)
|
||||
{
|
||||
return (KeyAgreeRecipientIdentifier)obj;
|
||||
}
|
||||
if (obj is Asn1Sequence)
|
||||
{
|
||||
return new KeyAgreeRecipientIdentifier(IssuerAndSerialNumber.GetInstance(obj));
|
||||
}
|
||||
if (obj is Asn1TaggedObject && ((Asn1TaggedObject)obj).TagNo == 0)
|
||||
{
|
||||
return new KeyAgreeRecipientIdentifier(RecipientKeyIdentifier.GetInstance((Asn1TaggedObject)obj, explicitly: false));
|
||||
}
|
||||
throw new ArgumentException("Invalid KeyAgreeRecipientIdentifier: " + Platform.GetTypeName(obj), "obj");
|
||||
}
|
||||
|
||||
public KeyAgreeRecipientIdentifier(IssuerAndSerialNumber issuerSerial)
|
||||
{
|
||||
this.issuerSerial = issuerSerial;
|
||||
}
|
||||
|
||||
public KeyAgreeRecipientIdentifier(RecipientKeyIdentifier rKeyID)
|
||||
{
|
||||
this.rKeyID = rKeyID;
|
||||
}
|
||||
|
||||
public override Asn1Object ToAsn1Object()
|
||||
{
|
||||
if (issuerSerial != null)
|
||||
{
|
||||
return issuerSerial.ToAsn1Object();
|
||||
}
|
||||
return new DerTaggedObject(explicitly: false, 0, rKeyID);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Asn1.X509;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1.Cms;
|
||||
|
||||
public class KeyAgreeRecipientInfo : Asn1Encodable
|
||||
{
|
||||
private DerInteger version;
|
||||
|
||||
private OriginatorIdentifierOrKey originator;
|
||||
|
||||
private Asn1OctetString ukm;
|
||||
|
||||
private AlgorithmIdentifier keyEncryptionAlgorithm;
|
||||
|
||||
private Asn1Sequence recipientEncryptedKeys;
|
||||
|
||||
public DerInteger Version => version;
|
||||
|
||||
public OriginatorIdentifierOrKey Originator => originator;
|
||||
|
||||
public Asn1OctetString UserKeyingMaterial => ukm;
|
||||
|
||||
public AlgorithmIdentifier KeyEncryptionAlgorithm => keyEncryptionAlgorithm;
|
||||
|
||||
public Asn1Sequence RecipientEncryptedKeys => recipientEncryptedKeys;
|
||||
|
||||
public KeyAgreeRecipientInfo(OriginatorIdentifierOrKey originator, Asn1OctetString ukm, AlgorithmIdentifier keyEncryptionAlgorithm, Asn1Sequence recipientEncryptedKeys)
|
||||
{
|
||||
version = new DerInteger(3);
|
||||
this.originator = originator;
|
||||
this.ukm = ukm;
|
||||
this.keyEncryptionAlgorithm = keyEncryptionAlgorithm;
|
||||
this.recipientEncryptedKeys = recipientEncryptedKeys;
|
||||
}
|
||||
|
||||
public KeyAgreeRecipientInfo(Asn1Sequence seq)
|
||||
{
|
||||
int index = 0;
|
||||
version = (DerInteger)seq[index++];
|
||||
originator = OriginatorIdentifierOrKey.GetInstance((Asn1TaggedObject)seq[index++], explicitly: true);
|
||||
if (seq[index] is Asn1TaggedObject)
|
||||
{
|
||||
ukm = Asn1OctetString.GetInstance((Asn1TaggedObject)seq[index++], isExplicit: true);
|
||||
}
|
||||
keyEncryptionAlgorithm = AlgorithmIdentifier.GetInstance(seq[index++]);
|
||||
recipientEncryptedKeys = (Asn1Sequence)seq[index++];
|
||||
}
|
||||
|
||||
public static KeyAgreeRecipientInfo GetInstance(Asn1TaggedObject obj, bool explicitly)
|
||||
{
|
||||
return GetInstance(Asn1Sequence.GetInstance(obj, explicitly));
|
||||
}
|
||||
|
||||
public static KeyAgreeRecipientInfo GetInstance(object obj)
|
||||
{
|
||||
if (obj == null || obj is KeyAgreeRecipientInfo)
|
||||
{
|
||||
return (KeyAgreeRecipientInfo)obj;
|
||||
}
|
||||
if (obj is Asn1Sequence)
|
||||
{
|
||||
return new KeyAgreeRecipientInfo((Asn1Sequence)obj);
|
||||
}
|
||||
throw new ArgumentException("Illegal object in KeyAgreeRecipientInfo: " + Platform.GetTypeName(obj));
|
||||
}
|
||||
|
||||
public override Asn1Object ToAsn1Object()
|
||||
{
|
||||
Asn1EncodableVector asn1EncodableVector = new Asn1EncodableVector(version, new DerTaggedObject(explicitly: true, 0, originator));
|
||||
if (ukm != null)
|
||||
{
|
||||
asn1EncodableVector.Add(new DerTaggedObject(explicitly: true, 1, ukm));
|
||||
}
|
||||
asn1EncodableVector.Add(keyEncryptionAlgorithm, recipientEncryptedKeys);
|
||||
return new DerSequence(asn1EncodableVector);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Asn1.X509;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1.Cms;
|
||||
|
||||
public class KeyTransRecipientInfo : Asn1Encodable
|
||||
{
|
||||
private DerInteger version;
|
||||
|
||||
private RecipientIdentifier rid;
|
||||
|
||||
private AlgorithmIdentifier keyEncryptionAlgorithm;
|
||||
|
||||
private Asn1OctetString encryptedKey;
|
||||
|
||||
public DerInteger Version => version;
|
||||
|
||||
public RecipientIdentifier RecipientIdentifier => rid;
|
||||
|
||||
public AlgorithmIdentifier KeyEncryptionAlgorithm => keyEncryptionAlgorithm;
|
||||
|
||||
public Asn1OctetString EncryptedKey => encryptedKey;
|
||||
|
||||
public KeyTransRecipientInfo(RecipientIdentifier rid, AlgorithmIdentifier keyEncryptionAlgorithm, Asn1OctetString encryptedKey)
|
||||
{
|
||||
if (rid.ToAsn1Object() is Asn1TaggedObject)
|
||||
{
|
||||
version = new DerInteger(2);
|
||||
}
|
||||
else
|
||||
{
|
||||
version = new DerInteger(0);
|
||||
}
|
||||
this.rid = rid;
|
||||
this.keyEncryptionAlgorithm = keyEncryptionAlgorithm;
|
||||
this.encryptedKey = encryptedKey;
|
||||
}
|
||||
|
||||
public KeyTransRecipientInfo(Asn1Sequence seq)
|
||||
{
|
||||
version = (DerInteger)seq[0];
|
||||
rid = RecipientIdentifier.GetInstance(seq[1]);
|
||||
keyEncryptionAlgorithm = AlgorithmIdentifier.GetInstance(seq[2]);
|
||||
encryptedKey = (Asn1OctetString)seq[3];
|
||||
}
|
||||
|
||||
public static KeyTransRecipientInfo GetInstance(object obj)
|
||||
{
|
||||
if (obj == null || obj is KeyTransRecipientInfo)
|
||||
{
|
||||
return (KeyTransRecipientInfo)obj;
|
||||
}
|
||||
if (obj is Asn1Sequence)
|
||||
{
|
||||
return new KeyTransRecipientInfo((Asn1Sequence)obj);
|
||||
}
|
||||
throw new ArgumentException("Illegal object in KeyTransRecipientInfo: " + Platform.GetTypeName(obj));
|
||||
}
|
||||
|
||||
public override Asn1Object ToAsn1Object()
|
||||
{
|
||||
return new DerSequence(version, rid, keyEncryptionAlgorithm, encryptedKey);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
namespace Org.BouncyCastle.Asn1.Cms;
|
||||
|
||||
public class MetaData : Asn1Encodable
|
||||
{
|
||||
private DerBoolean hashProtected;
|
||||
|
||||
private DerUtf8String fileName;
|
||||
|
||||
private DerIA5String mediaType;
|
||||
|
||||
private Attributes otherMetaData;
|
||||
|
||||
public virtual bool IsHashProtected => hashProtected.IsTrue;
|
||||
|
||||
public virtual DerUtf8String FileName => fileName;
|
||||
|
||||
public virtual DerIA5String MediaType => mediaType;
|
||||
|
||||
public virtual Attributes OtherMetaData => otherMetaData;
|
||||
|
||||
public MetaData(DerBoolean hashProtected, DerUtf8String fileName, DerIA5String mediaType, Attributes otherMetaData)
|
||||
{
|
||||
this.hashProtected = hashProtected;
|
||||
this.fileName = fileName;
|
||||
this.mediaType = mediaType;
|
||||
this.otherMetaData = otherMetaData;
|
||||
}
|
||||
|
||||
private MetaData(Asn1Sequence seq)
|
||||
{
|
||||
hashProtected = DerBoolean.GetInstance(seq[0]);
|
||||
int num = 1;
|
||||
if (num < seq.Count && seq[num] is DerUtf8String)
|
||||
{
|
||||
fileName = DerUtf8String.GetInstance(seq[num++]);
|
||||
}
|
||||
if (num < seq.Count && seq[num] is DerIA5String)
|
||||
{
|
||||
mediaType = DerIA5String.GetInstance(seq[num++]);
|
||||
}
|
||||
if (num < seq.Count)
|
||||
{
|
||||
otherMetaData = Attributes.GetInstance(seq[num++]);
|
||||
}
|
||||
}
|
||||
|
||||
public static MetaData GetInstance(object obj)
|
||||
{
|
||||
if (obj is MetaData)
|
||||
{
|
||||
return (MetaData)obj;
|
||||
}
|
||||
if (obj != null)
|
||||
{
|
||||
return new MetaData(Asn1Sequence.GetInstance(obj));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public override Asn1Object ToAsn1Object()
|
||||
{
|
||||
Asn1EncodableVector asn1EncodableVector = new Asn1EncodableVector(hashProtected);
|
||||
asn1EncodableVector.AddOptional(fileName, mediaType, otherMetaData);
|
||||
return new DerSequence(asn1EncodableVector);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Asn1.X509;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1.Cms;
|
||||
|
||||
public class OriginatorIdentifierOrKey : Asn1Encodable, IAsn1Choice
|
||||
{
|
||||
private Asn1Encodable id;
|
||||
|
||||
public Asn1Encodable ID => id;
|
||||
|
||||
public IssuerAndSerialNumber IssuerAndSerialNumber
|
||||
{
|
||||
get
|
||||
{
|
||||
if (id is IssuerAndSerialNumber)
|
||||
{
|
||||
return (IssuerAndSerialNumber)id;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public SubjectKeyIdentifier SubjectKeyIdentifier
|
||||
{
|
||||
get
|
||||
{
|
||||
if (id is Asn1TaggedObject && ((Asn1TaggedObject)id).TagNo == 0)
|
||||
{
|
||||
return SubjectKeyIdentifier.GetInstance((Asn1TaggedObject)id, explicitly: false);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
[Obsolete("Use 'OriginatorPublicKey' property")]
|
||||
public OriginatorPublicKey OriginatorKey => OriginatorPublicKey;
|
||||
|
||||
public OriginatorPublicKey OriginatorPublicKey
|
||||
{
|
||||
get
|
||||
{
|
||||
if (id is Asn1TaggedObject && ((Asn1TaggedObject)id).TagNo == 1)
|
||||
{
|
||||
return OriginatorPublicKey.GetInstance((Asn1TaggedObject)id, explicitly: false);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public OriginatorIdentifierOrKey(IssuerAndSerialNumber id)
|
||||
{
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
[Obsolete("Use version taking a 'SubjectKeyIdentifier'")]
|
||||
public OriginatorIdentifierOrKey(Asn1OctetString id)
|
||||
: this(new SubjectKeyIdentifier(id))
|
||||
{
|
||||
}
|
||||
|
||||
public OriginatorIdentifierOrKey(SubjectKeyIdentifier id)
|
||||
{
|
||||
this.id = new DerTaggedObject(explicitly: false, 0, id);
|
||||
}
|
||||
|
||||
public OriginatorIdentifierOrKey(OriginatorPublicKey id)
|
||||
{
|
||||
this.id = new DerTaggedObject(explicitly: false, 1, id);
|
||||
}
|
||||
|
||||
[Obsolete("Use more specific version")]
|
||||
public OriginatorIdentifierOrKey(Asn1Object id)
|
||||
{
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
private OriginatorIdentifierOrKey(Asn1TaggedObject id)
|
||||
{
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public static OriginatorIdentifierOrKey GetInstance(Asn1TaggedObject o, bool explicitly)
|
||||
{
|
||||
if (!explicitly)
|
||||
{
|
||||
throw new ArgumentException("Can't implicitly tag OriginatorIdentifierOrKey");
|
||||
}
|
||||
return GetInstance(o.GetObject());
|
||||
}
|
||||
|
||||
public static OriginatorIdentifierOrKey GetInstance(object o)
|
||||
{
|
||||
if (o == null || o is OriginatorIdentifierOrKey)
|
||||
{
|
||||
return (OriginatorIdentifierOrKey)o;
|
||||
}
|
||||
if (o is IssuerAndSerialNumber)
|
||||
{
|
||||
return new OriginatorIdentifierOrKey((IssuerAndSerialNumber)o);
|
||||
}
|
||||
if (o is SubjectKeyIdentifier)
|
||||
{
|
||||
return new OriginatorIdentifierOrKey((SubjectKeyIdentifier)o);
|
||||
}
|
||||
if (o is OriginatorPublicKey)
|
||||
{
|
||||
return new OriginatorIdentifierOrKey((OriginatorPublicKey)o);
|
||||
}
|
||||
if (o is Asn1TaggedObject)
|
||||
{
|
||||
return new OriginatorIdentifierOrKey((Asn1TaggedObject)o);
|
||||
}
|
||||
throw new ArgumentException("Invalid OriginatorIdentifierOrKey: " + Platform.GetTypeName(o));
|
||||
}
|
||||
|
||||
public override Asn1Object ToAsn1Object()
|
||||
{
|
||||
return id.ToAsn1Object();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1.Cms;
|
||||
|
||||
public class OriginatorInfo : Asn1Encodable
|
||||
{
|
||||
private Asn1Set certs;
|
||||
|
||||
private Asn1Set crls;
|
||||
|
||||
public Asn1Set Certificates => certs;
|
||||
|
||||
public Asn1Set Crls => crls;
|
||||
|
||||
public OriginatorInfo(Asn1Set certs, Asn1Set crls)
|
||||
{
|
||||
this.certs = certs;
|
||||
this.crls = crls;
|
||||
}
|
||||
|
||||
public OriginatorInfo(Asn1Sequence seq)
|
||||
{
|
||||
switch (seq.Count)
|
||||
{
|
||||
case 1:
|
||||
{
|
||||
Asn1TaggedObject asn1TaggedObject = (Asn1TaggedObject)seq[0];
|
||||
switch (asn1TaggedObject.TagNo)
|
||||
{
|
||||
case 0:
|
||||
certs = Asn1Set.GetInstance(asn1TaggedObject, explicitly: false);
|
||||
break;
|
||||
case 1:
|
||||
crls = Asn1Set.GetInstance(asn1TaggedObject, explicitly: false);
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentException("Bad tag in OriginatorInfo: " + asn1TaggedObject.TagNo);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 2:
|
||||
certs = Asn1Set.GetInstance((Asn1TaggedObject)seq[0], explicitly: false);
|
||||
crls = Asn1Set.GetInstance((Asn1TaggedObject)seq[1], explicitly: false);
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentException("OriginatorInfo too big");
|
||||
case 0:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public static OriginatorInfo GetInstance(Asn1TaggedObject obj, bool explicitly)
|
||||
{
|
||||
return GetInstance(Asn1Sequence.GetInstance(obj, explicitly));
|
||||
}
|
||||
|
||||
public static OriginatorInfo GetInstance(object obj)
|
||||
{
|
||||
if (obj == null || obj is OriginatorInfo)
|
||||
{
|
||||
return (OriginatorInfo)obj;
|
||||
}
|
||||
if (obj is Asn1Sequence)
|
||||
{
|
||||
return new OriginatorInfo((Asn1Sequence)obj);
|
||||
}
|
||||
throw new ArgumentException("Invalid OriginatorInfo: " + Platform.GetTypeName(obj));
|
||||
}
|
||||
|
||||
public override Asn1Object ToAsn1Object()
|
||||
{
|
||||
Asn1EncodableVector asn1EncodableVector = new Asn1EncodableVector();
|
||||
if (certs != null)
|
||||
{
|
||||
asn1EncodableVector.Add(new DerTaggedObject(explicitly: false, 0, certs));
|
||||
}
|
||||
if (crls != null)
|
||||
{
|
||||
asn1EncodableVector.Add(new DerTaggedObject(explicitly: false, 1, crls));
|
||||
}
|
||||
return new DerSequence(asn1EncodableVector);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Asn1.X509;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1.Cms;
|
||||
|
||||
public class OriginatorPublicKey : Asn1Encodable
|
||||
{
|
||||
private readonly AlgorithmIdentifier mAlgorithm;
|
||||
|
||||
private readonly DerBitString mPublicKey;
|
||||
|
||||
public AlgorithmIdentifier Algorithm => mAlgorithm;
|
||||
|
||||
public DerBitString PublicKey => mPublicKey;
|
||||
|
||||
public OriginatorPublicKey(AlgorithmIdentifier algorithm, byte[] publicKey)
|
||||
{
|
||||
mAlgorithm = algorithm;
|
||||
mPublicKey = new DerBitString(publicKey);
|
||||
}
|
||||
|
||||
[Obsolete("Use 'GetInstance' instead")]
|
||||
public OriginatorPublicKey(Asn1Sequence seq)
|
||||
{
|
||||
mAlgorithm = AlgorithmIdentifier.GetInstance(seq[0]);
|
||||
mPublicKey = DerBitString.GetInstance(seq[1]);
|
||||
}
|
||||
|
||||
public static OriginatorPublicKey GetInstance(Asn1TaggedObject obj, bool explicitly)
|
||||
{
|
||||
return GetInstance(Asn1Sequence.GetInstance(obj, explicitly));
|
||||
}
|
||||
|
||||
public static OriginatorPublicKey GetInstance(object obj)
|
||||
{
|
||||
if (obj == null || obj is OriginatorPublicKey)
|
||||
{
|
||||
return (OriginatorPublicKey)obj;
|
||||
}
|
||||
if (obj is Asn1Sequence)
|
||||
{
|
||||
return new OriginatorPublicKey(Asn1Sequence.GetInstance(obj));
|
||||
}
|
||||
throw new ArgumentException("Invalid OriginatorPublicKey: " + Platform.GetTypeName(obj));
|
||||
}
|
||||
|
||||
public override Asn1Object ToAsn1Object()
|
||||
{
|
||||
return new DerSequence(mAlgorithm, mPublicKey);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1.Cms;
|
||||
|
||||
public class OtherKeyAttribute : Asn1Encodable
|
||||
{
|
||||
private DerObjectIdentifier keyAttrId;
|
||||
|
||||
private Asn1Encodable keyAttr;
|
||||
|
||||
public DerObjectIdentifier KeyAttrId => keyAttrId;
|
||||
|
||||
public Asn1Encodable KeyAttr => keyAttr;
|
||||
|
||||
public static OtherKeyAttribute GetInstance(object obj)
|
||||
{
|
||||
if (obj == null || obj is OtherKeyAttribute)
|
||||
{
|
||||
return (OtherKeyAttribute)obj;
|
||||
}
|
||||
if (obj is Asn1Sequence)
|
||||
{
|
||||
return new OtherKeyAttribute((Asn1Sequence)obj);
|
||||
}
|
||||
throw new ArgumentException("unknown object in factory: " + Platform.GetTypeName(obj), "obj");
|
||||
}
|
||||
|
||||
public OtherKeyAttribute(Asn1Sequence seq)
|
||||
{
|
||||
keyAttrId = (DerObjectIdentifier)seq[0];
|
||||
keyAttr = seq[1];
|
||||
}
|
||||
|
||||
public OtherKeyAttribute(DerObjectIdentifier keyAttrId, Asn1Encodable keyAttr)
|
||||
{
|
||||
this.keyAttrId = keyAttrId;
|
||||
this.keyAttr = keyAttr;
|
||||
}
|
||||
|
||||
public override Asn1Object ToAsn1Object()
|
||||
{
|
||||
return new DerSequence(keyAttrId, keyAttr);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
using System;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1.Cms;
|
||||
|
||||
public class OtherRecipientInfo : Asn1Encodable
|
||||
{
|
||||
private readonly DerObjectIdentifier oriType;
|
||||
|
||||
private readonly Asn1Encodable oriValue;
|
||||
|
||||
public virtual DerObjectIdentifier OriType => oriType;
|
||||
|
||||
public virtual Asn1Encodable OriValue => oriValue;
|
||||
|
||||
public OtherRecipientInfo(DerObjectIdentifier oriType, Asn1Encodable oriValue)
|
||||
{
|
||||
this.oriType = oriType;
|
||||
this.oriValue = oriValue;
|
||||
}
|
||||
|
||||
[Obsolete("Use GetInstance() instead")]
|
||||
public OtherRecipientInfo(Asn1Sequence seq)
|
||||
{
|
||||
oriType = DerObjectIdentifier.GetInstance(seq[0]);
|
||||
oriValue = seq[1];
|
||||
}
|
||||
|
||||
public static OtherRecipientInfo GetInstance(Asn1TaggedObject obj, bool explicitly)
|
||||
{
|
||||
return GetInstance(Asn1Sequence.GetInstance(obj, explicitly));
|
||||
}
|
||||
|
||||
public static OtherRecipientInfo GetInstance(object obj)
|
||||
{
|
||||
if (obj == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
if (obj is OtherRecipientInfo result)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
return new OtherRecipientInfo(Asn1Sequence.GetInstance(obj));
|
||||
}
|
||||
|
||||
public override Asn1Object ToAsn1Object()
|
||||
{
|
||||
return new DerSequence(oriType, oriValue);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
namespace Org.BouncyCastle.Asn1.Cms;
|
||||
|
||||
public class OtherRevocationInfoFormat : Asn1Encodable
|
||||
{
|
||||
private readonly DerObjectIdentifier otherRevInfoFormat;
|
||||
|
||||
private readonly Asn1Encodable otherRevInfo;
|
||||
|
||||
public virtual DerObjectIdentifier InfoFormat => otherRevInfoFormat;
|
||||
|
||||
public virtual Asn1Encodable Info => otherRevInfo;
|
||||
|
||||
public OtherRevocationInfoFormat(DerObjectIdentifier otherRevInfoFormat, Asn1Encodable otherRevInfo)
|
||||
{
|
||||
this.otherRevInfoFormat = otherRevInfoFormat;
|
||||
this.otherRevInfo = otherRevInfo;
|
||||
}
|
||||
|
||||
private OtherRevocationInfoFormat(Asn1Sequence seq)
|
||||
{
|
||||
otherRevInfoFormat = DerObjectIdentifier.GetInstance(seq[0]);
|
||||
otherRevInfo = seq[1];
|
||||
}
|
||||
|
||||
public static OtherRevocationInfoFormat GetInstance(Asn1TaggedObject obj, bool isExplicit)
|
||||
{
|
||||
return GetInstance(Asn1Sequence.GetInstance(obj, isExplicit));
|
||||
}
|
||||
|
||||
public static OtherRevocationInfoFormat GetInstance(object obj)
|
||||
{
|
||||
if (obj is OtherRevocationInfoFormat)
|
||||
{
|
||||
return (OtherRevocationInfoFormat)obj;
|
||||
}
|
||||
if (obj != null)
|
||||
{
|
||||
return new OtherRevocationInfoFormat(Asn1Sequence.GetInstance(obj));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public override Asn1Object ToAsn1Object()
|
||||
{
|
||||
return new DerSequence(otherRevInfoFormat, otherRevInfo);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Asn1.X509;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1.Cms;
|
||||
|
||||
public class PasswordRecipientInfo : Asn1Encodable
|
||||
{
|
||||
private readonly DerInteger version;
|
||||
|
||||
private readonly AlgorithmIdentifier keyDerivationAlgorithm;
|
||||
|
||||
private readonly AlgorithmIdentifier keyEncryptionAlgorithm;
|
||||
|
||||
private readonly Asn1OctetString encryptedKey;
|
||||
|
||||
public DerInteger Version => version;
|
||||
|
||||
public AlgorithmIdentifier KeyDerivationAlgorithm => keyDerivationAlgorithm;
|
||||
|
||||
public AlgorithmIdentifier KeyEncryptionAlgorithm => keyEncryptionAlgorithm;
|
||||
|
||||
public Asn1OctetString EncryptedKey => encryptedKey;
|
||||
|
||||
public PasswordRecipientInfo(AlgorithmIdentifier keyEncryptionAlgorithm, Asn1OctetString encryptedKey)
|
||||
{
|
||||
version = new DerInteger(0);
|
||||
this.keyEncryptionAlgorithm = keyEncryptionAlgorithm;
|
||||
this.encryptedKey = encryptedKey;
|
||||
}
|
||||
|
||||
public PasswordRecipientInfo(AlgorithmIdentifier keyDerivationAlgorithm, AlgorithmIdentifier keyEncryptionAlgorithm, Asn1OctetString encryptedKey)
|
||||
{
|
||||
version = new DerInteger(0);
|
||||
this.keyDerivationAlgorithm = keyDerivationAlgorithm;
|
||||
this.keyEncryptionAlgorithm = keyEncryptionAlgorithm;
|
||||
this.encryptedKey = encryptedKey;
|
||||
}
|
||||
|
||||
public PasswordRecipientInfo(Asn1Sequence seq)
|
||||
{
|
||||
version = (DerInteger)seq[0];
|
||||
if (seq[1] is Asn1TaggedObject)
|
||||
{
|
||||
keyDerivationAlgorithm = AlgorithmIdentifier.GetInstance((Asn1TaggedObject)seq[1], explicitly: false);
|
||||
keyEncryptionAlgorithm = AlgorithmIdentifier.GetInstance(seq[2]);
|
||||
encryptedKey = (Asn1OctetString)seq[3];
|
||||
}
|
||||
else
|
||||
{
|
||||
keyEncryptionAlgorithm = AlgorithmIdentifier.GetInstance(seq[1]);
|
||||
encryptedKey = (Asn1OctetString)seq[2];
|
||||
}
|
||||
}
|
||||
|
||||
public static PasswordRecipientInfo GetInstance(Asn1TaggedObject obj, bool explicitly)
|
||||
{
|
||||
return GetInstance(Asn1Sequence.GetInstance(obj, explicitly));
|
||||
}
|
||||
|
||||
public static PasswordRecipientInfo GetInstance(object obj)
|
||||
{
|
||||
if (obj == null || obj is PasswordRecipientInfo)
|
||||
{
|
||||
return (PasswordRecipientInfo)obj;
|
||||
}
|
||||
if (obj is Asn1Sequence)
|
||||
{
|
||||
return new PasswordRecipientInfo((Asn1Sequence)obj);
|
||||
}
|
||||
throw new ArgumentException("Invalid PasswordRecipientInfo: " + Platform.GetTypeName(obj));
|
||||
}
|
||||
|
||||
public override Asn1Object ToAsn1Object()
|
||||
{
|
||||
Asn1EncodableVector asn1EncodableVector = new Asn1EncodableVector(version);
|
||||
if (keyDerivationAlgorithm != null)
|
||||
{
|
||||
asn1EncodableVector.Add(new DerTaggedObject(explicitly: false, 0, keyDerivationAlgorithm));
|
||||
}
|
||||
asn1EncodableVector.Add(keyEncryptionAlgorithm, encryptedKey);
|
||||
return new DerSequence(asn1EncodableVector);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1.Cms;
|
||||
|
||||
public class RecipientEncryptedKey : Asn1Encodable
|
||||
{
|
||||
private readonly KeyAgreeRecipientIdentifier identifier;
|
||||
|
||||
private readonly Asn1OctetString encryptedKey;
|
||||
|
||||
public KeyAgreeRecipientIdentifier Identifier => identifier;
|
||||
|
||||
public Asn1OctetString EncryptedKey => encryptedKey;
|
||||
|
||||
private RecipientEncryptedKey(Asn1Sequence seq)
|
||||
{
|
||||
identifier = KeyAgreeRecipientIdentifier.GetInstance(seq[0]);
|
||||
encryptedKey = (Asn1OctetString)seq[1];
|
||||
}
|
||||
|
||||
public static RecipientEncryptedKey GetInstance(Asn1TaggedObject obj, bool isExplicit)
|
||||
{
|
||||
return GetInstance(Asn1Sequence.GetInstance(obj, isExplicit));
|
||||
}
|
||||
|
||||
public static RecipientEncryptedKey GetInstance(object obj)
|
||||
{
|
||||
if (obj == null || obj is RecipientEncryptedKey)
|
||||
{
|
||||
return (RecipientEncryptedKey)obj;
|
||||
}
|
||||
if (obj is Asn1Sequence)
|
||||
{
|
||||
return new RecipientEncryptedKey((Asn1Sequence)obj);
|
||||
}
|
||||
throw new ArgumentException("Invalid RecipientEncryptedKey: " + Platform.GetTypeName(obj), "obj");
|
||||
}
|
||||
|
||||
public RecipientEncryptedKey(KeyAgreeRecipientIdentifier id, Asn1OctetString encryptedKey)
|
||||
{
|
||||
identifier = id;
|
||||
this.encryptedKey = encryptedKey;
|
||||
}
|
||||
|
||||
public override Asn1Object ToAsn1Object()
|
||||
{
|
||||
return new DerSequence(identifier, encryptedKey);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1.Cms;
|
||||
|
||||
public class RecipientIdentifier : Asn1Encodable, IAsn1Choice
|
||||
{
|
||||
private Asn1Encodable id;
|
||||
|
||||
public bool IsTagged => id is Asn1TaggedObject;
|
||||
|
||||
public Asn1Encodable ID
|
||||
{
|
||||
get
|
||||
{
|
||||
if (id is Asn1TaggedObject)
|
||||
{
|
||||
return Asn1OctetString.GetInstance((Asn1TaggedObject)id, isExplicit: false);
|
||||
}
|
||||
return IssuerAndSerialNumber.GetInstance(id);
|
||||
}
|
||||
}
|
||||
|
||||
public RecipientIdentifier(IssuerAndSerialNumber id)
|
||||
{
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public RecipientIdentifier(Asn1OctetString id)
|
||||
{
|
||||
this.id = new DerTaggedObject(explicitly: false, 0, id);
|
||||
}
|
||||
|
||||
public RecipientIdentifier(Asn1Object id)
|
||||
{
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public static RecipientIdentifier GetInstance(object o)
|
||||
{
|
||||
if (o == null || o is RecipientIdentifier)
|
||||
{
|
||||
return (RecipientIdentifier)o;
|
||||
}
|
||||
if (o is IssuerAndSerialNumber)
|
||||
{
|
||||
return new RecipientIdentifier((IssuerAndSerialNumber)o);
|
||||
}
|
||||
if (o is Asn1OctetString)
|
||||
{
|
||||
return new RecipientIdentifier((Asn1OctetString)o);
|
||||
}
|
||||
if (o is Asn1Object)
|
||||
{
|
||||
return new RecipientIdentifier((Asn1Object)o);
|
||||
}
|
||||
throw new ArgumentException("Illegal object in RecipientIdentifier: " + Platform.GetTypeName(o));
|
||||
}
|
||||
|
||||
public override Asn1Object ToAsn1Object()
|
||||
{
|
||||
return id.ToAsn1Object();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1.Cms;
|
||||
|
||||
public class RecipientInfo : Asn1Encodable, IAsn1Choice
|
||||
{
|
||||
internal Asn1Encodable info;
|
||||
|
||||
public DerInteger Version
|
||||
{
|
||||
get
|
||||
{
|
||||
if (info is Asn1TaggedObject)
|
||||
{
|
||||
Asn1TaggedObject asn1TaggedObject = (Asn1TaggedObject)info;
|
||||
return asn1TaggedObject.TagNo switch
|
||||
{
|
||||
1 => KeyAgreeRecipientInfo.GetInstance(asn1TaggedObject, explicitly: false).Version,
|
||||
2 => GetKekInfo(asn1TaggedObject).Version,
|
||||
3 => PasswordRecipientInfo.GetInstance(asn1TaggedObject, explicitly: false).Version,
|
||||
4 => new DerInteger(0),
|
||||
_ => throw new InvalidOperationException("unknown tag"),
|
||||
};
|
||||
}
|
||||
return KeyTransRecipientInfo.GetInstance(info).Version;
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsTagged => info is Asn1TaggedObject;
|
||||
|
||||
public Asn1Encodable Info
|
||||
{
|
||||
get
|
||||
{
|
||||
if (info is Asn1TaggedObject)
|
||||
{
|
||||
Asn1TaggedObject asn1TaggedObject = (Asn1TaggedObject)info;
|
||||
return asn1TaggedObject.TagNo switch
|
||||
{
|
||||
1 => KeyAgreeRecipientInfo.GetInstance(asn1TaggedObject, explicitly: false),
|
||||
2 => GetKekInfo(asn1TaggedObject),
|
||||
3 => PasswordRecipientInfo.GetInstance(asn1TaggedObject, explicitly: false),
|
||||
4 => OtherRecipientInfo.GetInstance(asn1TaggedObject, explicitly: false),
|
||||
_ => throw new InvalidOperationException("unknown tag"),
|
||||
};
|
||||
}
|
||||
return KeyTransRecipientInfo.GetInstance(info);
|
||||
}
|
||||
}
|
||||
|
||||
public RecipientInfo(KeyTransRecipientInfo info)
|
||||
{
|
||||
this.info = info;
|
||||
}
|
||||
|
||||
public RecipientInfo(KeyAgreeRecipientInfo info)
|
||||
{
|
||||
this.info = new DerTaggedObject(explicitly: false, 1, info);
|
||||
}
|
||||
|
||||
public RecipientInfo(KekRecipientInfo info)
|
||||
{
|
||||
this.info = new DerTaggedObject(explicitly: false, 2, info);
|
||||
}
|
||||
|
||||
public RecipientInfo(PasswordRecipientInfo info)
|
||||
{
|
||||
this.info = new DerTaggedObject(explicitly: false, 3, info);
|
||||
}
|
||||
|
||||
public RecipientInfo(OtherRecipientInfo info)
|
||||
{
|
||||
this.info = new DerTaggedObject(explicitly: false, 4, info);
|
||||
}
|
||||
|
||||
public RecipientInfo(Asn1Object info)
|
||||
{
|
||||
this.info = info;
|
||||
}
|
||||
|
||||
public static RecipientInfo GetInstance(object o)
|
||||
{
|
||||
if (o == null || o is RecipientInfo)
|
||||
{
|
||||
return (RecipientInfo)o;
|
||||
}
|
||||
if (o is Asn1Sequence)
|
||||
{
|
||||
return new RecipientInfo((Asn1Sequence)o);
|
||||
}
|
||||
if (o is Asn1TaggedObject)
|
||||
{
|
||||
return new RecipientInfo((Asn1TaggedObject)o);
|
||||
}
|
||||
throw new ArgumentException("unknown object in factory: " + Platform.GetTypeName(o));
|
||||
}
|
||||
|
||||
private KekRecipientInfo GetKekInfo(Asn1TaggedObject o)
|
||||
{
|
||||
return KekRecipientInfo.GetInstance(o, o.IsExplicit());
|
||||
}
|
||||
|
||||
public override Asn1Object ToAsn1Object()
|
||||
{
|
||||
return info.ToAsn1Object();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1.Cms;
|
||||
|
||||
public class RecipientKeyIdentifier : Asn1Encodable
|
||||
{
|
||||
private Asn1OctetString subjectKeyIdentifier;
|
||||
|
||||
private DerGeneralizedTime date;
|
||||
|
||||
private OtherKeyAttribute other;
|
||||
|
||||
public Asn1OctetString SubjectKeyIdentifier => subjectKeyIdentifier;
|
||||
|
||||
public DerGeneralizedTime Date => date;
|
||||
|
||||
public OtherKeyAttribute OtherKeyAttribute => other;
|
||||
|
||||
public RecipientKeyIdentifier(Asn1OctetString subjectKeyIdentifier, DerGeneralizedTime date, OtherKeyAttribute other)
|
||||
{
|
||||
this.subjectKeyIdentifier = subjectKeyIdentifier;
|
||||
this.date = date;
|
||||
this.other = other;
|
||||
}
|
||||
|
||||
public RecipientKeyIdentifier(byte[] subjectKeyIdentifier)
|
||||
: this(subjectKeyIdentifier, null, null)
|
||||
{
|
||||
}
|
||||
|
||||
public RecipientKeyIdentifier(byte[] subjectKeyIdentifier, DerGeneralizedTime date, OtherKeyAttribute other)
|
||||
{
|
||||
this.subjectKeyIdentifier = new DerOctetString(subjectKeyIdentifier);
|
||||
this.date = date;
|
||||
this.other = other;
|
||||
}
|
||||
|
||||
public RecipientKeyIdentifier(Asn1Sequence seq)
|
||||
{
|
||||
subjectKeyIdentifier = Asn1OctetString.GetInstance(seq[0]);
|
||||
switch (seq.Count)
|
||||
{
|
||||
case 2:
|
||||
if (seq[1] is DerGeneralizedTime)
|
||||
{
|
||||
date = (DerGeneralizedTime)seq[1];
|
||||
}
|
||||
else
|
||||
{
|
||||
other = OtherKeyAttribute.GetInstance(seq[2]);
|
||||
}
|
||||
break;
|
||||
case 3:
|
||||
date = (DerGeneralizedTime)seq[1];
|
||||
other = OtherKeyAttribute.GetInstance(seq[2]);
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentException("Invalid RecipientKeyIdentifier");
|
||||
case 1:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public static RecipientKeyIdentifier GetInstance(Asn1TaggedObject ato, bool explicitly)
|
||||
{
|
||||
return GetInstance(Asn1Sequence.GetInstance(ato, explicitly));
|
||||
}
|
||||
|
||||
public static RecipientKeyIdentifier GetInstance(object obj)
|
||||
{
|
||||
if (obj == null || obj is RecipientKeyIdentifier)
|
||||
{
|
||||
return (RecipientKeyIdentifier)obj;
|
||||
}
|
||||
if (obj is Asn1Sequence)
|
||||
{
|
||||
return new RecipientKeyIdentifier((Asn1Sequence)obj);
|
||||
}
|
||||
throw new ArgumentException("Invalid RecipientKeyIdentifier: " + Platform.GetTypeName(obj));
|
||||
}
|
||||
|
||||
public override Asn1Object ToAsn1Object()
|
||||
{
|
||||
Asn1EncodableVector asn1EncodableVector = new Asn1EncodableVector(subjectKeyIdentifier);
|
||||
asn1EncodableVector.AddOptional(date, other);
|
||||
return new DerSequence(asn1EncodableVector);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
namespace Org.BouncyCastle.Asn1.Cms;
|
||||
|
||||
public class ScvpReqRes : Asn1Encodable
|
||||
{
|
||||
private readonly ContentInfo request;
|
||||
|
||||
private readonly ContentInfo response;
|
||||
|
||||
public virtual ContentInfo Request => request;
|
||||
|
||||
public virtual ContentInfo Response => response;
|
||||
|
||||
public static ScvpReqRes GetInstance(object obj)
|
||||
{
|
||||
if (obj is ScvpReqRes)
|
||||
{
|
||||
return (ScvpReqRes)obj;
|
||||
}
|
||||
if (obj != null)
|
||||
{
|
||||
return new ScvpReqRes(Asn1Sequence.GetInstance(obj));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private ScvpReqRes(Asn1Sequence seq)
|
||||
{
|
||||
if (seq[0] is Asn1TaggedObject)
|
||||
{
|
||||
request = ContentInfo.GetInstance(Asn1TaggedObject.GetInstance(seq[0]), isExplicit: true);
|
||||
response = ContentInfo.GetInstance(seq[1]);
|
||||
}
|
||||
else
|
||||
{
|
||||
request = null;
|
||||
response = ContentInfo.GetInstance(seq[0]);
|
||||
}
|
||||
}
|
||||
|
||||
public ScvpReqRes(ContentInfo response)
|
||||
: this(null, response)
|
||||
{
|
||||
}
|
||||
|
||||
public ScvpReqRes(ContentInfo request, ContentInfo response)
|
||||
{
|
||||
this.request = request;
|
||||
this.response = response;
|
||||
}
|
||||
|
||||
public override Asn1Object ToAsn1Object()
|
||||
{
|
||||
Asn1EncodableVector asn1EncodableVector = new Asn1EncodableVector();
|
||||
if (request != null)
|
||||
{
|
||||
asn1EncodableVector.Add(new DerTaggedObject(explicitly: true, 0, request));
|
||||
}
|
||||
asn1EncodableVector.Add(response);
|
||||
return new DerSequence(asn1EncodableVector);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,206 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1.Cms;
|
||||
|
||||
public class SignedData : Asn1Encodable
|
||||
{
|
||||
private static readonly DerInteger Version1 = new DerInteger(1);
|
||||
|
||||
private static readonly DerInteger Version3 = new DerInteger(3);
|
||||
|
||||
private static readonly DerInteger Version4 = new DerInteger(4);
|
||||
|
||||
private static readonly DerInteger Version5 = new DerInteger(5);
|
||||
|
||||
private readonly DerInteger version;
|
||||
|
||||
private readonly Asn1Set digestAlgorithms;
|
||||
|
||||
private readonly ContentInfo contentInfo;
|
||||
|
||||
private readonly Asn1Set certificates;
|
||||
|
||||
private readonly Asn1Set crls;
|
||||
|
||||
private readonly Asn1Set signerInfos;
|
||||
|
||||
private readonly bool certsBer;
|
||||
|
||||
private readonly bool crlsBer;
|
||||
|
||||
public DerInteger Version => version;
|
||||
|
||||
public Asn1Set DigestAlgorithms => digestAlgorithms;
|
||||
|
||||
public ContentInfo EncapContentInfo => contentInfo;
|
||||
|
||||
public Asn1Set Certificates => certificates;
|
||||
|
||||
public Asn1Set CRLs => crls;
|
||||
|
||||
public Asn1Set SignerInfos => signerInfos;
|
||||
|
||||
public static SignedData GetInstance(object obj)
|
||||
{
|
||||
if (obj is SignedData)
|
||||
{
|
||||
return (SignedData)obj;
|
||||
}
|
||||
if (obj is Asn1Sequence)
|
||||
{
|
||||
return new SignedData((Asn1Sequence)obj);
|
||||
}
|
||||
throw new ArgumentException("Unknown object in factory: " + Platform.GetTypeName(obj), "obj");
|
||||
}
|
||||
|
||||
public SignedData(Asn1Set digestAlgorithms, ContentInfo contentInfo, Asn1Set certificates, Asn1Set crls, Asn1Set signerInfos)
|
||||
{
|
||||
version = CalculateVersion(contentInfo.ContentType, certificates, crls, signerInfos);
|
||||
this.digestAlgorithms = digestAlgorithms;
|
||||
this.contentInfo = contentInfo;
|
||||
this.certificates = certificates;
|
||||
this.crls = crls;
|
||||
this.signerInfos = signerInfos;
|
||||
crlsBer = crls is BerSet;
|
||||
certsBer = certificates is BerSet;
|
||||
}
|
||||
|
||||
private DerInteger CalculateVersion(DerObjectIdentifier contentOid, Asn1Set certs, Asn1Set crls, Asn1Set signerInfs)
|
||||
{
|
||||
bool flag = false;
|
||||
bool flag2 = false;
|
||||
bool flag3 = false;
|
||||
bool flag4 = false;
|
||||
if (certs != null)
|
||||
{
|
||||
foreach (object cert in certs)
|
||||
{
|
||||
if (cert is Asn1TaggedObject)
|
||||
{
|
||||
Asn1TaggedObject asn1TaggedObject = (Asn1TaggedObject)cert;
|
||||
if (asn1TaggedObject.TagNo == 1)
|
||||
{
|
||||
flag3 = true;
|
||||
}
|
||||
else if (asn1TaggedObject.TagNo == 2)
|
||||
{
|
||||
flag4 = true;
|
||||
}
|
||||
else if (asn1TaggedObject.TagNo == 3)
|
||||
{
|
||||
flag = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (flag)
|
||||
{
|
||||
return Version5;
|
||||
}
|
||||
if (crls != null)
|
||||
{
|
||||
foreach (object crl in crls)
|
||||
{
|
||||
if (crl is Asn1TaggedObject)
|
||||
{
|
||||
flag2 = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (flag2)
|
||||
{
|
||||
return Version5;
|
||||
}
|
||||
if (flag4)
|
||||
{
|
||||
return Version4;
|
||||
}
|
||||
if (flag3 || !CmsObjectIdentifiers.Data.Equals(contentOid) || CheckForVersion3(signerInfs))
|
||||
{
|
||||
return Version3;
|
||||
}
|
||||
return Version1;
|
||||
}
|
||||
|
||||
private bool CheckForVersion3(Asn1Set signerInfs)
|
||||
{
|
||||
foreach (object signerInf in signerInfs)
|
||||
{
|
||||
SignerInfo instance = SignerInfo.GetInstance(signerInf);
|
||||
if (instance.Version.Value.IntValue == 3)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private SignedData(Asn1Sequence seq)
|
||||
{
|
||||
IEnumerator enumerator = seq.GetEnumerator();
|
||||
enumerator.MoveNext();
|
||||
version = (DerInteger)enumerator.Current;
|
||||
enumerator.MoveNext();
|
||||
digestAlgorithms = (Asn1Set)enumerator.Current;
|
||||
enumerator.MoveNext();
|
||||
contentInfo = ContentInfo.GetInstance(enumerator.Current);
|
||||
while (enumerator.MoveNext())
|
||||
{
|
||||
Asn1Object asn1Object = (Asn1Object)enumerator.Current;
|
||||
if (asn1Object is Asn1TaggedObject)
|
||||
{
|
||||
Asn1TaggedObject asn1TaggedObject = (Asn1TaggedObject)asn1Object;
|
||||
switch (asn1TaggedObject.TagNo)
|
||||
{
|
||||
case 0:
|
||||
certsBer = asn1TaggedObject is BerTaggedObject;
|
||||
certificates = Asn1Set.GetInstance(asn1TaggedObject, explicitly: false);
|
||||
break;
|
||||
case 1:
|
||||
crlsBer = asn1TaggedObject is BerTaggedObject;
|
||||
crls = Asn1Set.GetInstance(asn1TaggedObject, explicitly: false);
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentException("unknown tag value " + asn1TaggedObject.TagNo);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
signerInfos = (Asn1Set)asn1Object;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override Asn1Object ToAsn1Object()
|
||||
{
|
||||
Asn1EncodableVector asn1EncodableVector = new Asn1EncodableVector(version, digestAlgorithms, contentInfo);
|
||||
if (certificates != null)
|
||||
{
|
||||
if (certsBer)
|
||||
{
|
||||
asn1EncodableVector.Add(new BerTaggedObject(explicitly: false, 0, certificates));
|
||||
}
|
||||
else
|
||||
{
|
||||
asn1EncodableVector.Add(new DerTaggedObject(explicitly: false, 0, certificates));
|
||||
}
|
||||
}
|
||||
if (crls != null)
|
||||
{
|
||||
if (crlsBer)
|
||||
{
|
||||
asn1EncodableVector.Add(new BerTaggedObject(explicitly: false, 1, crls));
|
||||
}
|
||||
else
|
||||
{
|
||||
asn1EncodableVector.Add(new DerTaggedObject(explicitly: false, 1, crls));
|
||||
}
|
||||
}
|
||||
asn1EncodableVector.Add(signerInfos);
|
||||
return new BerSequence(asn1EncodableVector);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
using System.IO;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1.Cms;
|
||||
|
||||
public class SignedDataParser
|
||||
{
|
||||
private Asn1SequenceParser _seq;
|
||||
|
||||
private DerInteger _version;
|
||||
|
||||
private object _nextObject;
|
||||
|
||||
private bool _certsCalled;
|
||||
|
||||
private bool _crlsCalled;
|
||||
|
||||
public DerInteger Version => _version;
|
||||
|
||||
public static SignedDataParser GetInstance(object o)
|
||||
{
|
||||
if (o is Asn1Sequence)
|
||||
{
|
||||
return new SignedDataParser(((Asn1Sequence)o).Parser);
|
||||
}
|
||||
if (o is Asn1SequenceParser)
|
||||
{
|
||||
return new SignedDataParser((Asn1SequenceParser)o);
|
||||
}
|
||||
throw new IOException("unknown object encountered: " + Platform.GetTypeName(o));
|
||||
}
|
||||
|
||||
public SignedDataParser(Asn1SequenceParser seq)
|
||||
{
|
||||
_seq = seq;
|
||||
_version = (DerInteger)seq.ReadObject();
|
||||
}
|
||||
|
||||
public Asn1SetParser GetDigestAlgorithms()
|
||||
{
|
||||
return (Asn1SetParser)_seq.ReadObject();
|
||||
}
|
||||
|
||||
public ContentInfoParser GetEncapContentInfo()
|
||||
{
|
||||
return new ContentInfoParser((Asn1SequenceParser)_seq.ReadObject());
|
||||
}
|
||||
|
||||
public Asn1SetParser GetCertificates()
|
||||
{
|
||||
_certsCalled = true;
|
||||
_nextObject = _seq.ReadObject();
|
||||
if (_nextObject is Asn1TaggedObjectParser && ((Asn1TaggedObjectParser)_nextObject).TagNo == 0)
|
||||
{
|
||||
Asn1SetParser result = (Asn1SetParser)((Asn1TaggedObjectParser)_nextObject).GetObjectParser(17, isExplicit: false);
|
||||
_nextObject = null;
|
||||
return result;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public Asn1SetParser GetCrls()
|
||||
{
|
||||
if (!_certsCalled)
|
||||
{
|
||||
throw new IOException("GetCerts() has not been called.");
|
||||
}
|
||||
_crlsCalled = true;
|
||||
if (_nextObject == null)
|
||||
{
|
||||
_nextObject = _seq.ReadObject();
|
||||
}
|
||||
if (_nextObject is Asn1TaggedObjectParser && ((Asn1TaggedObjectParser)_nextObject).TagNo == 1)
|
||||
{
|
||||
Asn1SetParser result = (Asn1SetParser)((Asn1TaggedObjectParser)_nextObject).GetObjectParser(17, isExplicit: false);
|
||||
_nextObject = null;
|
||||
return result;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public Asn1SetParser GetSignerInfos()
|
||||
{
|
||||
if (!_certsCalled || !_crlsCalled)
|
||||
{
|
||||
throw new IOException("GetCerts() and/or GetCrls() has not been called.");
|
||||
}
|
||||
if (_nextObject == null)
|
||||
{
|
||||
_nextObject = _seq.ReadObject();
|
||||
}
|
||||
return (Asn1SetParser)_nextObject;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1.Cms;
|
||||
|
||||
public class SignerIdentifier : Asn1Encodable, IAsn1Choice
|
||||
{
|
||||
private Asn1Encodable id;
|
||||
|
||||
public bool IsTagged => id is Asn1TaggedObject;
|
||||
|
||||
public Asn1Encodable ID
|
||||
{
|
||||
get
|
||||
{
|
||||
if (id is Asn1TaggedObject)
|
||||
{
|
||||
return Asn1OctetString.GetInstance((Asn1TaggedObject)id, isExplicit: false);
|
||||
}
|
||||
return id;
|
||||
}
|
||||
}
|
||||
|
||||
public SignerIdentifier(IssuerAndSerialNumber id)
|
||||
{
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public SignerIdentifier(Asn1OctetString id)
|
||||
{
|
||||
this.id = new DerTaggedObject(explicitly: false, 0, id);
|
||||
}
|
||||
|
||||
public SignerIdentifier(Asn1Object id)
|
||||
{
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public static SignerIdentifier GetInstance(object o)
|
||||
{
|
||||
if (o == null || o is SignerIdentifier)
|
||||
{
|
||||
return (SignerIdentifier)o;
|
||||
}
|
||||
if (o is IssuerAndSerialNumber)
|
||||
{
|
||||
return new SignerIdentifier((IssuerAndSerialNumber)o);
|
||||
}
|
||||
if (o is Asn1OctetString)
|
||||
{
|
||||
return new SignerIdentifier((Asn1OctetString)o);
|
||||
}
|
||||
if (o is Asn1Object)
|
||||
{
|
||||
return new SignerIdentifier((Asn1Object)o);
|
||||
}
|
||||
throw new ArgumentException("Illegal object in SignerIdentifier: " + Platform.GetTypeName(o));
|
||||
}
|
||||
|
||||
public override Asn1Object ToAsn1Object()
|
||||
{
|
||||
return id.ToAsn1Object();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using Org.BouncyCastle.Asn1.X509;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1.Cms;
|
||||
|
||||
public class SignerInfo : Asn1Encodable
|
||||
{
|
||||
private DerInteger version;
|
||||
|
||||
private SignerIdentifier sid;
|
||||
|
||||
private AlgorithmIdentifier digAlgorithm;
|
||||
|
||||
private Asn1Set authenticatedAttributes;
|
||||
|
||||
private AlgorithmIdentifier digEncryptionAlgorithm;
|
||||
|
||||
private Asn1OctetString encryptedDigest;
|
||||
|
||||
private Asn1Set unauthenticatedAttributes;
|
||||
|
||||
public DerInteger Version => version;
|
||||
|
||||
public SignerIdentifier SignerID => sid;
|
||||
|
||||
public Asn1Set AuthenticatedAttributes => authenticatedAttributes;
|
||||
|
||||
public AlgorithmIdentifier DigestAlgorithm => digAlgorithm;
|
||||
|
||||
public Asn1OctetString EncryptedDigest => encryptedDigest;
|
||||
|
||||
public AlgorithmIdentifier DigestEncryptionAlgorithm => digEncryptionAlgorithm;
|
||||
|
||||
public Asn1Set UnauthenticatedAttributes => unauthenticatedAttributes;
|
||||
|
||||
public static SignerInfo GetInstance(object obj)
|
||||
{
|
||||
if (obj == null || obj is SignerInfo)
|
||||
{
|
||||
return (SignerInfo)obj;
|
||||
}
|
||||
if (obj is Asn1Sequence)
|
||||
{
|
||||
return new SignerInfo((Asn1Sequence)obj);
|
||||
}
|
||||
throw new ArgumentException("Unknown object in factory: " + Platform.GetTypeName(obj), "obj");
|
||||
}
|
||||
|
||||
public SignerInfo(SignerIdentifier sid, AlgorithmIdentifier digAlgorithm, Asn1Set authenticatedAttributes, AlgorithmIdentifier digEncryptionAlgorithm, Asn1OctetString encryptedDigest, Asn1Set unauthenticatedAttributes)
|
||||
{
|
||||
version = new DerInteger((!sid.IsTagged) ? 1 : 3);
|
||||
this.sid = sid;
|
||||
this.digAlgorithm = digAlgorithm;
|
||||
this.authenticatedAttributes = authenticatedAttributes;
|
||||
this.digEncryptionAlgorithm = digEncryptionAlgorithm;
|
||||
this.encryptedDigest = encryptedDigest;
|
||||
this.unauthenticatedAttributes = unauthenticatedAttributes;
|
||||
}
|
||||
|
||||
public SignerInfo(SignerIdentifier sid, AlgorithmIdentifier digAlgorithm, Attributes authenticatedAttributes, AlgorithmIdentifier digEncryptionAlgorithm, Asn1OctetString encryptedDigest, Attributes unauthenticatedAttributes)
|
||||
{
|
||||
version = new DerInteger((!sid.IsTagged) ? 1 : 3);
|
||||
this.sid = sid;
|
||||
this.digAlgorithm = digAlgorithm;
|
||||
this.authenticatedAttributes = Asn1Set.GetInstance(authenticatedAttributes);
|
||||
this.digEncryptionAlgorithm = digEncryptionAlgorithm;
|
||||
this.encryptedDigest = encryptedDigest;
|
||||
this.unauthenticatedAttributes = Asn1Set.GetInstance(unauthenticatedAttributes);
|
||||
}
|
||||
|
||||
[Obsolete("Use 'GetInstance' instead")]
|
||||
public SignerInfo(Asn1Sequence seq)
|
||||
{
|
||||
IEnumerator enumerator = seq.GetEnumerator();
|
||||
enumerator.MoveNext();
|
||||
version = (DerInteger)enumerator.Current;
|
||||
enumerator.MoveNext();
|
||||
sid = SignerIdentifier.GetInstance(enumerator.Current);
|
||||
enumerator.MoveNext();
|
||||
digAlgorithm = AlgorithmIdentifier.GetInstance(enumerator.Current);
|
||||
enumerator.MoveNext();
|
||||
object current = enumerator.Current;
|
||||
if (current is Asn1TaggedObject)
|
||||
{
|
||||
authenticatedAttributes = Asn1Set.GetInstance((Asn1TaggedObject)current, explicitly: false);
|
||||
enumerator.MoveNext();
|
||||
digEncryptionAlgorithm = AlgorithmIdentifier.GetInstance(enumerator.Current);
|
||||
}
|
||||
else
|
||||
{
|
||||
authenticatedAttributes = null;
|
||||
digEncryptionAlgorithm = AlgorithmIdentifier.GetInstance(current);
|
||||
}
|
||||
enumerator.MoveNext();
|
||||
encryptedDigest = Asn1OctetString.GetInstance(enumerator.Current);
|
||||
if (enumerator.MoveNext())
|
||||
{
|
||||
unauthenticatedAttributes = Asn1Set.GetInstance((Asn1TaggedObject)enumerator.Current, explicitly: false);
|
||||
}
|
||||
else
|
||||
{
|
||||
unauthenticatedAttributes = null;
|
||||
}
|
||||
}
|
||||
|
||||
public override Asn1Object ToAsn1Object()
|
||||
{
|
||||
Asn1EncodableVector asn1EncodableVector = new Asn1EncodableVector(version, sid, digAlgorithm);
|
||||
if (authenticatedAttributes != null)
|
||||
{
|
||||
asn1EncodableVector.Add(new DerTaggedObject(explicitly: false, 0, authenticatedAttributes));
|
||||
}
|
||||
asn1EncodableVector.Add(digEncryptionAlgorithm, encryptedDigest);
|
||||
if (unauthenticatedAttributes != null)
|
||||
{
|
||||
asn1EncodableVector.Add(new DerTaggedObject(explicitly: false, 1, unauthenticatedAttributes));
|
||||
}
|
||||
return new DerSequence(asn1EncodableVector);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1.Cms;
|
||||
|
||||
public class Time : Asn1Encodable, IAsn1Choice
|
||||
{
|
||||
private readonly Asn1Object time;
|
||||
|
||||
public string TimeString
|
||||
{
|
||||
get
|
||||
{
|
||||
if (time is DerUtcTime)
|
||||
{
|
||||
return ((DerUtcTime)time).AdjustedTimeString;
|
||||
}
|
||||
return ((DerGeneralizedTime)time).GetTime();
|
||||
}
|
||||
}
|
||||
|
||||
public DateTime Date
|
||||
{
|
||||
get
|
||||
{
|
||||
try
|
||||
{
|
||||
if (time is DerUtcTime)
|
||||
{
|
||||
return ((DerUtcTime)time).ToAdjustedDateTime();
|
||||
}
|
||||
return ((DerGeneralizedTime)time).ToDateTime();
|
||||
}
|
||||
catch (FormatException ex)
|
||||
{
|
||||
throw new InvalidOperationException("invalid date string: " + ex.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static Time GetInstance(Asn1TaggedObject obj, bool explicitly)
|
||||
{
|
||||
return GetInstance(obj.GetObject());
|
||||
}
|
||||
|
||||
public Time(Asn1Object time)
|
||||
{
|
||||
if (time == null)
|
||||
{
|
||||
throw new ArgumentNullException("time");
|
||||
}
|
||||
if (!(time is DerUtcTime) && !(time is DerGeneralizedTime))
|
||||
{
|
||||
throw new ArgumentException("unknown object passed to Time");
|
||||
}
|
||||
this.time = time;
|
||||
}
|
||||
|
||||
public Time(DateTime date)
|
||||
{
|
||||
string text = date.ToString("yyyyMMddHHmmss", CultureInfo.InvariantCulture) + "Z";
|
||||
int num = int.Parse(text.Substring(0, 4));
|
||||
if (num < 1950 || num > 2049)
|
||||
{
|
||||
time = new DerGeneralizedTime(text);
|
||||
}
|
||||
else
|
||||
{
|
||||
time = new DerUtcTime(text.Substring(2));
|
||||
}
|
||||
}
|
||||
|
||||
public static Time GetInstance(object obj)
|
||||
{
|
||||
if (obj == null || obj is Time)
|
||||
{
|
||||
return (Time)obj;
|
||||
}
|
||||
if (obj is DerUtcTime)
|
||||
{
|
||||
return new Time((DerUtcTime)obj);
|
||||
}
|
||||
if (obj is DerGeneralizedTime)
|
||||
{
|
||||
return new Time((DerGeneralizedTime)obj);
|
||||
}
|
||||
throw new ArgumentException("unknown object in factory: " + Platform.GetTypeName(obj), "obj");
|
||||
}
|
||||
|
||||
public override Asn1Object ToAsn1Object()
|
||||
{
|
||||
return time;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
using Org.BouncyCastle.Asn1.X509;
|
||||
|
||||
namespace Org.BouncyCastle.Asn1.Cms;
|
||||
|
||||
public class TimeStampAndCrl : Asn1Encodable
|
||||
{
|
||||
private ContentInfo timeStamp;
|
||||
|
||||
private CertificateList crl;
|
||||
|
||||
public virtual ContentInfo TimeStampToken => timeStamp;
|
||||
|
||||
public virtual CertificateList Crl => crl;
|
||||
|
||||
public TimeStampAndCrl(ContentInfo timeStamp)
|
||||
{
|
||||
this.timeStamp = timeStamp;
|
||||
}
|
||||
|
||||
private TimeStampAndCrl(Asn1Sequence seq)
|
||||
{
|
||||
timeStamp = ContentInfo.GetInstance(seq[0]);
|
||||
if (seq.Count == 2)
|
||||
{
|
||||
crl = CertificateList.GetInstance(seq[1]);
|
||||
}
|
||||
}
|
||||
|
||||
public static TimeStampAndCrl GetInstance(object obj)
|
||||
{
|
||||
if (obj is TimeStampAndCrl)
|
||||
{
|
||||
return (TimeStampAndCrl)obj;
|
||||
}
|
||||
if (obj != null)
|
||||
{
|
||||
return new TimeStampAndCrl(Asn1Sequence.GetInstance(obj));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public override Asn1Object ToAsn1Object()
|
||||
{
|
||||
Asn1EncodableVector asn1EncodableVector = new Asn1EncodableVector(timeStamp);
|
||||
asn1EncodableVector.AddOptional(crl);
|
||||
return new DerSequence(asn1EncodableVector);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
namespace Org.BouncyCastle.Asn1.Cms;
|
||||
|
||||
public class TimeStampTokenEvidence : Asn1Encodable
|
||||
{
|
||||
private TimeStampAndCrl[] timeStampAndCrls;
|
||||
|
||||
public TimeStampTokenEvidence(TimeStampAndCrl[] timeStampAndCrls)
|
||||
{
|
||||
this.timeStampAndCrls = timeStampAndCrls;
|
||||
}
|
||||
|
||||
public TimeStampTokenEvidence(TimeStampAndCrl timeStampAndCrl)
|
||||
{
|
||||
timeStampAndCrls = new TimeStampAndCrl[1] { timeStampAndCrl };
|
||||
}
|
||||
|
||||
private TimeStampTokenEvidence(Asn1Sequence seq)
|
||||
{
|
||||
timeStampAndCrls = new TimeStampAndCrl[seq.Count];
|
||||
int num = 0;
|
||||
foreach (Asn1Encodable item in seq)
|
||||
{
|
||||
timeStampAndCrls[num++] = TimeStampAndCrl.GetInstance(item.ToAsn1Object());
|
||||
}
|
||||
}
|
||||
|
||||
public static TimeStampTokenEvidence GetInstance(Asn1TaggedObject tagged, bool isExplicit)
|
||||
{
|
||||
return GetInstance(Asn1Sequence.GetInstance(tagged, isExplicit));
|
||||
}
|
||||
|
||||
public static TimeStampTokenEvidence GetInstance(object obj)
|
||||
{
|
||||
if (obj is TimeStampTokenEvidence)
|
||||
{
|
||||
return (TimeStampTokenEvidence)obj;
|
||||
}
|
||||
if (obj != null)
|
||||
{
|
||||
return new TimeStampTokenEvidence(Asn1Sequence.GetInstance(obj));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public virtual TimeStampAndCrl[] ToTimeStampAndCrlArray()
|
||||
{
|
||||
return (TimeStampAndCrl[])timeStampAndCrls.Clone();
|
||||
}
|
||||
|
||||
public override Asn1Object ToAsn1Object()
|
||||
{
|
||||
return new DerSequence(timeStampAndCrls);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
namespace Org.BouncyCastle.Asn1.Cms;
|
||||
|
||||
public class TimeStampedData : Asn1Encodable
|
||||
{
|
||||
private DerInteger version;
|
||||
|
||||
private DerIA5String dataUri;
|
||||
|
||||
private MetaData metaData;
|
||||
|
||||
private Asn1OctetString content;
|
||||
|
||||
private Evidence temporalEvidence;
|
||||
|
||||
public virtual DerIA5String DataUri => dataUri;
|
||||
|
||||
public MetaData MetaData => metaData;
|
||||
|
||||
public Asn1OctetString Content => content;
|
||||
|
||||
public Evidence TemporalEvidence => temporalEvidence;
|
||||
|
||||
public TimeStampedData(DerIA5String dataUri, MetaData metaData, Asn1OctetString content, Evidence temporalEvidence)
|
||||
{
|
||||
version = new DerInteger(1);
|
||||
this.dataUri = dataUri;
|
||||
this.metaData = metaData;
|
||||
this.content = content;
|
||||
this.temporalEvidence = temporalEvidence;
|
||||
}
|
||||
|
||||
private TimeStampedData(Asn1Sequence seq)
|
||||
{
|
||||
version = DerInteger.GetInstance(seq[0]);
|
||||
int index = 1;
|
||||
if (seq[index] is DerIA5String)
|
||||
{
|
||||
dataUri = DerIA5String.GetInstance(seq[index++]);
|
||||
}
|
||||
if (seq[index] is MetaData || seq[index] is Asn1Sequence)
|
||||
{
|
||||
metaData = MetaData.GetInstance(seq[index++]);
|
||||
}
|
||||
if (seq[index] is Asn1OctetString)
|
||||
{
|
||||
content = Asn1OctetString.GetInstance(seq[index++]);
|
||||
}
|
||||
temporalEvidence = Evidence.GetInstance(seq[index]);
|
||||
}
|
||||
|
||||
public static TimeStampedData GetInstance(object obj)
|
||||
{
|
||||
if (obj is TimeStampedData)
|
||||
{
|
||||
return (TimeStampedData)obj;
|
||||
}
|
||||
if (obj != null)
|
||||
{
|
||||
return new TimeStampedData(Asn1Sequence.GetInstance(obj));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public override Asn1Object ToAsn1Object()
|
||||
{
|
||||
Asn1EncodableVector asn1EncodableVector = new Asn1EncodableVector(version);
|
||||
asn1EncodableVector.AddOptional(dataUri, metaData, content);
|
||||
asn1EncodableVector.Add(temporalEvidence);
|
||||
return new BerSequence(asn1EncodableVector);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
namespace Org.BouncyCastle.Asn1.Cms;
|
||||
|
||||
public class TimeStampedDataParser
|
||||
{
|
||||
private DerInteger version;
|
||||
|
||||
private DerIA5String dataUri;
|
||||
|
||||
private MetaData metaData;
|
||||
|
||||
private Asn1OctetStringParser content;
|
||||
|
||||
private Evidence temporalEvidence;
|
||||
|
||||
private Asn1SequenceParser parser;
|
||||
|
||||
public virtual DerIA5String DataUri => dataUri;
|
||||
|
||||
public virtual MetaData MetaData => metaData;
|
||||
|
||||
public virtual Asn1OctetStringParser Content => content;
|
||||
|
||||
private TimeStampedDataParser(Asn1SequenceParser parser)
|
||||
{
|
||||
this.parser = parser;
|
||||
version = DerInteger.GetInstance(parser.ReadObject());
|
||||
Asn1Object asn1Object = parser.ReadObject().ToAsn1Object();
|
||||
if (asn1Object is DerIA5String)
|
||||
{
|
||||
dataUri = DerIA5String.GetInstance(asn1Object);
|
||||
asn1Object = parser.ReadObject().ToAsn1Object();
|
||||
}
|
||||
if (asn1Object is Asn1SequenceParser)
|
||||
{
|
||||
metaData = MetaData.GetInstance(asn1Object.ToAsn1Object());
|
||||
asn1Object = parser.ReadObject().ToAsn1Object();
|
||||
}
|
||||
if (asn1Object is Asn1OctetStringParser)
|
||||
{
|
||||
content = (Asn1OctetStringParser)asn1Object;
|
||||
}
|
||||
}
|
||||
|
||||
public static TimeStampedDataParser GetInstance(object obj)
|
||||
{
|
||||
if (obj is Asn1Sequence)
|
||||
{
|
||||
return new TimeStampedDataParser(((Asn1Sequence)obj).Parser);
|
||||
}
|
||||
if (obj is Asn1SequenceParser)
|
||||
{
|
||||
return new TimeStampedDataParser((Asn1SequenceParser)obj);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public virtual Evidence GetTemporalEvidence()
|
||||
{
|
||||
if (temporalEvidence == null)
|
||||
{
|
||||
temporalEvidence = Evidence.GetInstance(parser.ReadObject().ToAsn1Object());
|
||||
}
|
||||
return temporalEvidence;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user