init commit

This commit is contained in:
2025-10-09 09:57:24 +09:00
commit 4d551bd74f
6636 changed files with 1218703 additions and 0 deletions

View File

@@ -0,0 +1,50 @@
using System;
using Org.BouncyCastle.Utilities;
namespace Org.BouncyCastle.Asn1.Pkcs;
public class AttributePkcs : Asn1Encodable
{
private readonly DerObjectIdentifier attrType;
private readonly Asn1Set attrValues;
public DerObjectIdentifier AttrType => attrType;
public Asn1Set AttrValues => attrValues;
public static AttributePkcs GetInstance(object obj)
{
AttributePkcs attributePkcs = obj as AttributePkcs;
if (obj == null || attributePkcs != null)
{
return attributePkcs;
}
if (obj is Asn1Sequence seq)
{
return new AttributePkcs(seq);
}
throw new ArgumentException("Unknown object in factory: " + Platform.GetTypeName(obj), "obj");
}
private AttributePkcs(Asn1Sequence seq)
{
if (seq.Count != 2)
{
throw new ArgumentException("Wrong number of elements in sequence", "seq");
}
attrType = DerObjectIdentifier.GetInstance(seq[0]);
attrValues = Asn1Set.GetInstance(seq[1]);
}
public AttributePkcs(DerObjectIdentifier attrType, Asn1Set attrValues)
{
this.attrType = attrType;
this.attrValues = attrValues;
}
public override Asn1Object ToAsn1Object()
{
return new DerSequence(attrType, attrValues);
}
}

View File

@@ -0,0 +1,30 @@
namespace Org.BouncyCastle.Asn1.Pkcs;
public class AuthenticatedSafe : Asn1Encodable
{
private readonly ContentInfo[] info;
public AuthenticatedSafe(Asn1Sequence seq)
{
info = new ContentInfo[seq.Count];
for (int i = 0; i != info.Length; i++)
{
info[i] = ContentInfo.GetInstance(seq[i]);
}
}
public AuthenticatedSafe(ContentInfo[] info)
{
this.info = (ContentInfo[])info.Clone();
}
public ContentInfo[] GetContentInfo()
{
return (ContentInfo[])info.Clone();
}
public override Asn1Object ToAsn1Object()
{
return new BerSequence(info);
}
}

View File

@@ -0,0 +1,35 @@
using System;
namespace Org.BouncyCastle.Asn1.Pkcs;
public class CertBag : Asn1Encodable
{
private readonly DerObjectIdentifier certID;
private readonly Asn1Object certValue;
public DerObjectIdentifier CertID => certID;
public Asn1Object CertValue => certValue;
public CertBag(Asn1Sequence seq)
{
if (seq.Count != 2)
{
throw new ArgumentException("Wrong number of elements in sequence", "seq");
}
certID = DerObjectIdentifier.GetInstance(seq[0]);
certValue = Asn1TaggedObject.GetInstance(seq[1]).GetObject();
}
public CertBag(DerObjectIdentifier certID, Asn1Object certValue)
{
this.certID = certID;
this.certValue = certValue;
}
public override Asn1Object ToAsn1Object()
{
return new DerSequence(certID, new DerTaggedObject(0, certValue));
}
}

View File

@@ -0,0 +1,68 @@
using System;
using Org.BouncyCastle.Asn1.X509;
namespace Org.BouncyCastle.Asn1.Pkcs;
public class CertificationRequest : Asn1Encodable
{
protected CertificationRequestInfo reqInfo;
protected AlgorithmIdentifier sigAlgId;
protected DerBitString sigBits;
public AlgorithmIdentifier SignatureAlgorithm => sigAlgId;
public DerBitString Signature => sigBits;
public static CertificationRequest GetInstance(object obj)
{
if (obj is CertificationRequest)
{
return (CertificationRequest)obj;
}
if (obj != null)
{
return new CertificationRequest((Asn1Sequence)obj);
}
return null;
}
protected CertificationRequest()
{
}
public CertificationRequest(CertificationRequestInfo requestInfo, AlgorithmIdentifier algorithm, DerBitString signature)
{
reqInfo = requestInfo;
sigAlgId = algorithm;
sigBits = signature;
}
[Obsolete("Use 'GetInstance' instead")]
public CertificationRequest(Asn1Sequence seq)
{
if (seq.Count != 3)
{
throw new ArgumentException("Wrong number of elements in sequence", "seq");
}
reqInfo = CertificationRequestInfo.GetInstance(seq[0]);
sigAlgId = AlgorithmIdentifier.GetInstance(seq[1]);
sigBits = DerBitString.GetInstance(seq[2]);
}
public CertificationRequestInfo GetCertificationRequestInfo()
{
return reqInfo;
}
public byte[] GetSignatureOctets()
{
return sigBits.GetOctets();
}
public override Asn1Object ToAsn1Object()
{
return new DerSequence(reqInfo, sigAlgId, sigBits);
}
}

View File

@@ -0,0 +1,92 @@
using System;
using Org.BouncyCastle.Asn1.X509;
namespace Org.BouncyCastle.Asn1.Pkcs;
public class CertificationRequestInfo : Asn1Encodable
{
internal DerInteger version = new DerInteger(0);
internal X509Name subject;
internal SubjectPublicKeyInfo subjectPKInfo;
internal Asn1Set attributes;
public DerInteger Version => version;
public X509Name Subject => subject;
public SubjectPublicKeyInfo SubjectPublicKeyInfo => subjectPKInfo;
public Asn1Set Attributes => attributes;
public static CertificationRequestInfo GetInstance(object obj)
{
if (obj is CertificationRequestInfo)
{
return (CertificationRequestInfo)obj;
}
if (obj != null)
{
return new CertificationRequestInfo(Asn1Sequence.GetInstance(obj));
}
return null;
}
public CertificationRequestInfo(X509Name subject, SubjectPublicKeyInfo pkInfo, Asn1Set attributes)
{
this.subject = subject;
subjectPKInfo = pkInfo;
this.attributes = attributes;
ValidateAttributes(attributes);
if (subject == null || version == null || subjectPKInfo == null)
{
throw new ArgumentException("Not all mandatory fields set in CertificationRequestInfo generator.");
}
}
private CertificationRequestInfo(Asn1Sequence seq)
{
version = (DerInteger)seq[0];
subject = X509Name.GetInstance(seq[1]);
subjectPKInfo = SubjectPublicKeyInfo.GetInstance(seq[2]);
if (seq.Count > 3)
{
DerTaggedObject obj = (DerTaggedObject)seq[3];
attributes = Asn1Set.GetInstance(obj, explicitly: false);
}
ValidateAttributes(attributes);
if (subject == null || version == null || subjectPKInfo == null)
{
throw new ArgumentException("Not all mandatory fields set in CertificationRequestInfo generator.");
}
}
public override Asn1Object ToAsn1Object()
{
Asn1EncodableVector asn1EncodableVector = new Asn1EncodableVector(version, subject, subjectPKInfo);
if (attributes != null)
{
asn1EncodableVector.Add(new DerTaggedObject(explicitly: false, 0, attributes));
}
return new DerSequence(asn1EncodableVector);
}
private static void ValidateAttributes(Asn1Set attributes)
{
if (attributes == null)
{
return;
}
foreach (Asn1Encodable attribute in attributes)
{
Asn1Object obj = attribute.ToAsn1Object();
AttributePkcs instance = AttributePkcs.GetInstance(obj);
if (instance.AttrType.Equals(PkcsObjectIdentifiers.Pkcs9AtChallengePassword) && instance.AttrValues.Count != 1)
{
throw new ArgumentException("challengePassword attribute must have one value");
}
}
}
}

View File

@@ -0,0 +1,50 @@
namespace Org.BouncyCastle.Asn1.Pkcs;
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)
{
return null;
}
if (obj is ContentInfo result)
{
return result;
}
return new ContentInfo(Asn1Sequence.GetInstance(obj));
}
private ContentInfo(Asn1Sequence seq)
{
contentType = (DerObjectIdentifier)seq[0];
if (seq.Count > 1)
{
content = ((Asn1TaggedObject)seq[1]).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);
}
}

View File

@@ -0,0 +1,62 @@
using System.Collections;
using Org.BouncyCastle.Math;
namespace Org.BouncyCastle.Asn1.Pkcs;
public class DHParameter : Asn1Encodable
{
internal DerInteger p;
internal DerInteger g;
internal DerInteger l;
public BigInteger P => p.PositiveValue;
public BigInteger G => g.PositiveValue;
public BigInteger L
{
get
{
if (l != null)
{
return l.PositiveValue;
}
return null;
}
}
public DHParameter(BigInteger p, BigInteger g, int l)
{
this.p = new DerInteger(p);
this.g = new DerInteger(g);
if (l != 0)
{
this.l = new DerInteger(l);
}
}
public DHParameter(Asn1Sequence seq)
{
IEnumerator enumerator = seq.GetEnumerator();
enumerator.MoveNext();
p = (DerInteger)enumerator.Current;
enumerator.MoveNext();
g = (DerInteger)enumerator.Current;
if (enumerator.MoveNext())
{
l = (DerInteger)enumerator.Current;
}
}
public override Asn1Object ToAsn1Object()
{
Asn1EncodableVector asn1EncodableVector = new Asn1EncodableVector(p, g);
if (l != null)
{
asn1EncodableVector.Add(l);
}
return new DerSequence(asn1EncodableVector);
}
}

View File

@@ -0,0 +1,63 @@
using System;
using Org.BouncyCastle.Asn1.X509;
using Org.BouncyCastle.Utilities;
namespace Org.BouncyCastle.Asn1.Pkcs;
public class EncryptedData : Asn1Encodable
{
private readonly Asn1Sequence data;
public DerObjectIdentifier ContentType => (DerObjectIdentifier)data[0];
public AlgorithmIdentifier EncryptionAlgorithm => AlgorithmIdentifier.GetInstance(data[1]);
public Asn1OctetString Content
{
get
{
if (data.Count == 3)
{
DerTaggedObject obj = (DerTaggedObject)data[2];
return Asn1OctetString.GetInstance(obj, isExplicit: false);
}
return null;
}
}
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("Unknown object in factory: " + Platform.GetTypeName(obj), "obj");
}
private EncryptedData(Asn1Sequence seq)
{
if (seq.Count != 2)
{
throw new ArgumentException("Wrong number of elements in sequence", "seq");
}
if (((DerInteger)seq[0]).Value.IntValue != 0)
{
throw new ArgumentException("sequence not version 0");
}
data = (Asn1Sequence)seq[1];
}
public EncryptedData(DerObjectIdentifier contentType, AlgorithmIdentifier encryptionAlgorithm, Asn1Encodable content)
{
data = new BerSequence(contentType, encryptionAlgorithm.ToAsn1Object(), new BerTaggedObject(explicitly: false, 0, content));
}
public override Asn1Object ToAsn1Object()
{
return new BerSequence(new DerInteger(0), data);
}
}

View File

@@ -0,0 +1,53 @@
using System;
using Org.BouncyCastle.Asn1.X509;
using Org.BouncyCastle.Utilities;
namespace Org.BouncyCastle.Asn1.Pkcs;
public class EncryptedPrivateKeyInfo : Asn1Encodable
{
private readonly AlgorithmIdentifier algId;
private readonly Asn1OctetString data;
public AlgorithmIdentifier EncryptionAlgorithm => algId;
private EncryptedPrivateKeyInfo(Asn1Sequence seq)
{
if (seq.Count != 2)
{
throw new ArgumentException("Wrong number of elements in sequence", "seq");
}
algId = AlgorithmIdentifier.GetInstance(seq[0]);
data = Asn1OctetString.GetInstance(seq[1]);
}
public EncryptedPrivateKeyInfo(AlgorithmIdentifier algId, byte[] encoding)
{
this.algId = algId;
data = new DerOctetString(encoding);
}
public static EncryptedPrivateKeyInfo GetInstance(object obj)
{
if (obj is EncryptedPrivateKeyInfo)
{
return (EncryptedPrivateKeyInfo)obj;
}
if (obj is Asn1Sequence)
{
return new EncryptedPrivateKeyInfo((Asn1Sequence)obj);
}
throw new ArgumentException("Unknown object in factory: " + Platform.GetTypeName(obj), "obj");
}
public byte[] GetEncryptedData()
{
return data.GetOctets();
}
public override Asn1Object ToAsn1Object()
{
return new DerSequence(algId, data);
}
}

View File

@@ -0,0 +1,43 @@
using System;
using Org.BouncyCastle.Asn1.X509;
using Org.BouncyCastle.Utilities;
namespace Org.BouncyCastle.Asn1.Pkcs;
public class EncryptionScheme : AlgorithmIdentifier
{
public Asn1Object Asn1Object => Parameters.ToAsn1Object();
public EncryptionScheme(DerObjectIdentifier objectID)
: base(objectID)
{
}
public EncryptionScheme(DerObjectIdentifier objectID, Asn1Encodable parameters)
: base(objectID, parameters)
{
}
internal EncryptionScheme(Asn1Sequence seq)
: this((DerObjectIdentifier)seq[0], seq[1])
{
}
public new static EncryptionScheme GetInstance(object obj)
{
if (obj is EncryptionScheme)
{
return (EncryptionScheme)obj;
}
if (obj is Asn1Sequence)
{
return new EncryptionScheme((Asn1Sequence)obj);
}
throw new ArgumentException("Unknown object in factory: " + Platform.GetTypeName(obj), "obj");
}
public override Asn1Object ToAsn1Object()
{
return new DerSequence(Algorithm, Parameters);
}
}

View File

@@ -0,0 +1,57 @@
using System;
using Org.BouncyCastle.Asn1.X509;
using Org.BouncyCastle.Math;
using Org.BouncyCastle.Utilities;
namespace Org.BouncyCastle.Asn1.Pkcs;
public class IssuerAndSerialNumber : Asn1Encodable
{
private readonly X509Name name;
private readonly DerInteger certSerialNumber;
public X509Name Name => name;
public DerInteger CertificateSerialNumber => certSerialNumber;
public static IssuerAndSerialNumber GetInstance(object obj)
{
if (obj is IssuerAndSerialNumber)
{
return (IssuerAndSerialNumber)obj;
}
if (obj is Asn1Sequence)
{
return new IssuerAndSerialNumber((Asn1Sequence)obj);
}
throw new ArgumentException("Unknown object in factory: " + Platform.GetTypeName(obj), "obj");
}
private IssuerAndSerialNumber(Asn1Sequence seq)
{
if (seq.Count != 2)
{
throw new ArgumentException("Wrong number of elements in sequence", "seq");
}
name = X509Name.GetInstance(seq[0]);
certSerialNumber = DerInteger.GetInstance(seq[1]);
}
public IssuerAndSerialNumber(X509Name name, BigInteger certSerialNumber)
{
this.name = name;
this.certSerialNumber = new DerInteger(certSerialNumber);
}
public IssuerAndSerialNumber(X509Name name, DerInteger certSerialNumber)
{
this.name = name;
this.certSerialNumber = certSerialNumber;
}
public override Asn1Object ToAsn1Object()
{
return new DerSequence(name, certSerialNumber);
}
}

View File

@@ -0,0 +1,16 @@
using Org.BouncyCastle.Asn1.X509;
namespace Org.BouncyCastle.Asn1.Pkcs;
public class KeyDerivationFunc : AlgorithmIdentifier
{
internal KeyDerivationFunc(Asn1Sequence seq)
: base(seq)
{
}
public KeyDerivationFunc(DerObjectIdentifier id, Asn1Encodable parameters)
: base(id, parameters)
{
}
}

View File

@@ -0,0 +1,68 @@
using System;
using Org.BouncyCastle.Asn1.X509;
using Org.BouncyCastle.Math;
using Org.BouncyCastle.Utilities;
namespace Org.BouncyCastle.Asn1.Pkcs;
public class MacData : Asn1Encodable
{
internal DigestInfo digInfo;
internal byte[] salt;
internal BigInteger iterationCount;
public DigestInfo Mac => digInfo;
public BigInteger IterationCount => iterationCount;
public static MacData GetInstance(object obj)
{
if (obj is MacData)
{
return (MacData)obj;
}
if (obj is Asn1Sequence)
{
return new MacData((Asn1Sequence)obj);
}
throw new ArgumentException("Unknown object in factory: " + Platform.GetTypeName(obj), "obj");
}
private MacData(Asn1Sequence seq)
{
digInfo = DigestInfo.GetInstance(seq[0]);
salt = ((Asn1OctetString)seq[1]).GetOctets();
if (seq.Count == 3)
{
iterationCount = ((DerInteger)seq[2]).Value;
}
else
{
iterationCount = BigInteger.One;
}
}
public MacData(DigestInfo digInfo, byte[] salt, int iterationCount)
{
this.digInfo = digInfo;
this.salt = (byte[])salt.Clone();
this.iterationCount = BigInteger.ValueOf(iterationCount);
}
public byte[] GetSalt()
{
return (byte[])salt.Clone();
}
public override Asn1Object ToAsn1Object()
{
Asn1EncodableVector asn1EncodableVector = new Asn1EncodableVector(digInfo, new DerOctetString(salt));
if (!iterationCount.Equals(BigInteger.One))
{
asn1EncodableVector.Add(new DerInteger(iterationCount));
}
return new DerSequence(asn1EncodableVector);
}
}

View File

@@ -0,0 +1,53 @@
using System;
using Org.BouncyCastle.Math;
using Org.BouncyCastle.Utilities;
namespace Org.BouncyCastle.Asn1.Pkcs;
public class PbeParameter : Asn1Encodable
{
private readonly Asn1OctetString salt;
private readonly DerInteger iterationCount;
public BigInteger IterationCount => iterationCount.Value;
public static PbeParameter GetInstance(object obj)
{
if (obj is PbeParameter || obj == null)
{
return (PbeParameter)obj;
}
if (obj is Asn1Sequence)
{
return new PbeParameter((Asn1Sequence)obj);
}
throw new ArgumentException("Unknown object in factory: " + Platform.GetTypeName(obj), "obj");
}
private PbeParameter(Asn1Sequence seq)
{
if (seq.Count != 2)
{
throw new ArgumentException("Wrong number of elements in sequence", "seq");
}
salt = Asn1OctetString.GetInstance(seq[0]);
iterationCount = DerInteger.GetInstance(seq[1]);
}
public PbeParameter(byte[] salt, int iterationCount)
{
this.salt = new DerOctetString(salt);
this.iterationCount = new DerInteger(iterationCount);
}
public byte[] GetSalt()
{
return salt.GetOctets();
}
public override Asn1Object ToAsn1Object()
{
return new DerSequence(salt, iterationCount);
}
}

View File

@@ -0,0 +1,57 @@
using System;
namespace Org.BouncyCastle.Asn1.Pkcs;
public class PbeS2Parameters : Asn1Encodable
{
private readonly KeyDerivationFunc func;
private readonly EncryptionScheme scheme;
public KeyDerivationFunc KeyDerivationFunc => func;
public EncryptionScheme EncryptionScheme => scheme;
public static PbeS2Parameters GetInstance(object obj)
{
if (obj == null)
{
return null;
}
if (obj is PbeS2Parameters result)
{
return result;
}
return new PbeS2Parameters(Asn1Sequence.GetInstance(obj));
}
public PbeS2Parameters(KeyDerivationFunc keyDevFunc, EncryptionScheme encScheme)
{
func = keyDevFunc;
scheme = encScheme;
}
[Obsolete("Use GetInstance() instead")]
public PbeS2Parameters(Asn1Sequence seq)
{
if (seq.Count != 2)
{
throw new ArgumentException("Wrong number of elements in sequence", "seq");
}
Asn1Sequence asn1Sequence = (Asn1Sequence)seq[0].ToAsn1Object();
if (asn1Sequence[0].Equals(PkcsObjectIdentifiers.IdPbkdf2))
{
func = new KeyDerivationFunc(PkcsObjectIdentifiers.IdPbkdf2, Pbkdf2Params.GetInstance(asn1Sequence[1]));
}
else
{
func = new KeyDerivationFunc(asn1Sequence);
}
scheme = EncryptionScheme.GetInstance(seq[1].ToAsn1Object());
}
public override Asn1Object ToAsn1Object()
{
return new DerSequence(func, scheme);
}
}

View File

@@ -0,0 +1,149 @@
using System;
using Org.BouncyCastle.Asn1.X509;
using Org.BouncyCastle.Math;
using Org.BouncyCastle.Utilities;
namespace Org.BouncyCastle.Asn1.Pkcs;
public class Pbkdf2Params : Asn1Encodable
{
private static AlgorithmIdentifier algid_hmacWithSHA1 = new AlgorithmIdentifier(PkcsObjectIdentifiers.IdHmacWithSha1, DerNull.Instance);
private readonly Asn1OctetString octStr;
private readonly DerInteger iterationCount;
private readonly DerInteger keyLength;
private readonly AlgorithmIdentifier prf;
public BigInteger IterationCount => iterationCount.Value;
public BigInteger KeyLength
{
get
{
if (keyLength != null)
{
return keyLength.Value;
}
return null;
}
}
public bool IsDefaultPrf
{
get
{
if (prf != null)
{
return prf.Equals(algid_hmacWithSHA1);
}
return true;
}
}
public AlgorithmIdentifier Prf
{
get
{
if (prf == null)
{
return algid_hmacWithSHA1;
}
return prf;
}
}
public static Pbkdf2Params GetInstance(object obj)
{
if (obj == null || obj is Pbkdf2Params)
{
return (Pbkdf2Params)obj;
}
if (obj is Asn1Sequence)
{
return new Pbkdf2Params((Asn1Sequence)obj);
}
throw new ArgumentException("Unknown object in factory: " + Platform.GetTypeName(obj), "obj");
}
public Pbkdf2Params(Asn1Sequence seq)
{
if (seq.Count < 2 || seq.Count > 4)
{
throw new ArgumentException("Wrong number of elements in sequence", "seq");
}
octStr = (Asn1OctetString)seq[0];
iterationCount = (DerInteger)seq[1];
Asn1Encodable asn1Encodable = null;
Asn1Encodable asn1Encodable2 = null;
if (seq.Count > 3)
{
asn1Encodable = seq[2];
asn1Encodable2 = seq[3];
}
else if (seq.Count > 2)
{
if (seq[2] is DerInteger)
{
asn1Encodable = seq[2];
}
else
{
asn1Encodable2 = seq[2];
}
}
if (asn1Encodable != null)
{
keyLength = (DerInteger)asn1Encodable;
}
if (asn1Encodable2 != null)
{
prf = AlgorithmIdentifier.GetInstance(asn1Encodable2);
}
}
public Pbkdf2Params(byte[] salt, int iterationCount)
{
octStr = new DerOctetString(salt);
this.iterationCount = new DerInteger(iterationCount);
}
public Pbkdf2Params(byte[] salt, int iterationCount, int keyLength)
: this(salt, iterationCount)
{
this.keyLength = new DerInteger(keyLength);
}
public Pbkdf2Params(byte[] salt, int iterationCount, int keyLength, AlgorithmIdentifier prf)
: this(salt, iterationCount, keyLength)
{
this.prf = prf;
}
public Pbkdf2Params(byte[] salt, int iterationCount, AlgorithmIdentifier prf)
: this(salt, iterationCount)
{
this.prf = prf;
}
public byte[] GetSalt()
{
return octStr.GetOctets();
}
public override Asn1Object ToAsn1Object()
{
Asn1EncodableVector asn1EncodableVector = new Asn1EncodableVector(octStr, iterationCount);
if (keyLength != null)
{
asn1EncodableVector.Add(keyLength);
}
if (!IsDefaultPrf)
{
asn1EncodableVector.Add(prf);
}
return new DerSequence(asn1EncodableVector);
}
}

View File

@@ -0,0 +1,45 @@
using System;
using Org.BouncyCastle.Math;
namespace Org.BouncyCastle.Asn1.Pkcs;
public class Pfx : Asn1Encodable
{
private ContentInfo contentInfo;
private MacData macData;
public ContentInfo AuthSafe => contentInfo;
public MacData MacData => macData;
public Pfx(Asn1Sequence seq)
{
BigInteger value = ((DerInteger)seq[0]).Value;
if (value.IntValue != 3)
{
throw new ArgumentException("wrong version for PFX PDU");
}
contentInfo = ContentInfo.GetInstance(seq[1]);
if (seq.Count == 3)
{
macData = MacData.GetInstance(seq[2]);
}
}
public Pfx(ContentInfo contentInfo, MacData macData)
{
this.contentInfo = contentInfo;
this.macData = macData;
}
public override Asn1Object ToAsn1Object()
{
Asn1EncodableVector asn1EncodableVector = new Asn1EncodableVector(new DerInteger(3), contentInfo);
if (macData != null)
{
asn1EncodableVector.Add(macData);
}
return new BerSequence(asn1EncodableVector);
}
}

View File

@@ -0,0 +1,53 @@
using System;
using Org.BouncyCastle.Math;
using Org.BouncyCastle.Utilities;
namespace Org.BouncyCastle.Asn1.Pkcs;
public class Pkcs12PbeParams : Asn1Encodable
{
private readonly DerInteger iterations;
private readonly Asn1OctetString iv;
public BigInteger Iterations => iterations.Value;
public Pkcs12PbeParams(byte[] salt, int iterations)
{
iv = new DerOctetString(salt);
this.iterations = new DerInteger(iterations);
}
private Pkcs12PbeParams(Asn1Sequence seq)
{
if (seq.Count != 2)
{
throw new ArgumentException("Wrong number of elements in sequence", "seq");
}
iv = Asn1OctetString.GetInstance(seq[0]);
iterations = DerInteger.GetInstance(seq[1]);
}
public static Pkcs12PbeParams GetInstance(object obj)
{
if (obj is Pkcs12PbeParams)
{
return (Pkcs12PbeParams)obj;
}
if (obj is Asn1Sequence)
{
return new Pkcs12PbeParams((Asn1Sequence)obj);
}
throw new ArgumentException("Unknown object in factory: " + Platform.GetTypeName(obj), "obj");
}
public byte[] GetIV()
{
return iv.GetOctets();
}
public override Asn1Object ToAsn1Object()
{
return new DerSequence(iv, iterations);
}
}

View File

@@ -0,0 +1,301 @@
using System;
namespace Org.BouncyCastle.Asn1.Pkcs;
public abstract class PkcsObjectIdentifiers
{
public const string Pkcs1 = "1.2.840.113549.1.1";
public const string Pkcs3 = "1.2.840.113549.1.3";
public const string Pkcs5 = "1.2.840.113549.1.5";
public const string EncryptionAlgorithm = "1.2.840.113549.3";
public const string DigestAlgorithm = "1.2.840.113549.2";
public const string Pkcs7 = "1.2.840.113549.1.7";
public const string Pkcs9 = "1.2.840.113549.1.9";
public const string CertTypes = "1.2.840.113549.1.9.22";
public const string CrlTypes = "1.2.840.113549.1.9.23";
public const string IdCT = "1.2.840.113549.1.9.16.1";
public const string IdCti = "1.2.840.113549.1.9.16.6";
public const string IdAA = "1.2.840.113549.1.9.16.2";
public const string IdSpq = "1.2.840.113549.1.9.16.5";
public const string Pkcs12 = "1.2.840.113549.1.12";
public const string BagTypes = "1.2.840.113549.1.12.10.1";
public const string Pkcs12PbeIds = "1.2.840.113549.1.12.1";
internal static readonly DerObjectIdentifier Pkcs1Oid = new DerObjectIdentifier("1.2.840.113549.1.1");
public static readonly DerObjectIdentifier RsaEncryption = Pkcs1Oid.Branch("1");
public static readonly DerObjectIdentifier MD2WithRsaEncryption = Pkcs1Oid.Branch("2");
public static readonly DerObjectIdentifier MD4WithRsaEncryption = Pkcs1Oid.Branch("3");
public static readonly DerObjectIdentifier MD5WithRsaEncryption = Pkcs1Oid.Branch("4");
public static readonly DerObjectIdentifier Sha1WithRsaEncryption = Pkcs1Oid.Branch("5");
public static readonly DerObjectIdentifier SrsaOaepEncryptionSet = Pkcs1Oid.Branch("6");
public static readonly DerObjectIdentifier IdRsaesOaep = Pkcs1Oid.Branch("7");
public static readonly DerObjectIdentifier IdMgf1 = Pkcs1Oid.Branch("8");
public static readonly DerObjectIdentifier IdPSpecified = Pkcs1Oid.Branch("9");
public static readonly DerObjectIdentifier IdRsassaPss = Pkcs1Oid.Branch("10");
public static readonly DerObjectIdentifier Sha256WithRsaEncryption = Pkcs1Oid.Branch("11");
public static readonly DerObjectIdentifier Sha384WithRsaEncryption = Pkcs1Oid.Branch("12");
public static readonly DerObjectIdentifier Sha512WithRsaEncryption = Pkcs1Oid.Branch("13");
public static readonly DerObjectIdentifier Sha224WithRsaEncryption = Pkcs1Oid.Branch("14");
public static readonly DerObjectIdentifier Sha512_224WithRSAEncryption = Pkcs1Oid.Branch("15");
public static readonly DerObjectIdentifier Sha512_256WithRSAEncryption = Pkcs1Oid.Branch("16");
public static readonly DerObjectIdentifier DhKeyAgreement = new DerObjectIdentifier("1.2.840.113549.1.3.1");
public static readonly DerObjectIdentifier PbeWithMD2AndDesCbc = new DerObjectIdentifier("1.2.840.113549.1.5.1");
public static readonly DerObjectIdentifier PbeWithMD2AndRC2Cbc = new DerObjectIdentifier("1.2.840.113549.1.5.4");
public static readonly DerObjectIdentifier PbeWithMD5AndDesCbc = new DerObjectIdentifier("1.2.840.113549.1.5.3");
public static readonly DerObjectIdentifier PbeWithMD5AndRC2Cbc = new DerObjectIdentifier("1.2.840.113549.1.5.6");
public static readonly DerObjectIdentifier PbeWithSha1AndDesCbc = new DerObjectIdentifier("1.2.840.113549.1.5.10");
public static readonly DerObjectIdentifier PbeWithSha1AndRC2Cbc = new DerObjectIdentifier("1.2.840.113549.1.5.11");
public static readonly DerObjectIdentifier IdPbeS2 = new DerObjectIdentifier("1.2.840.113549.1.5.13");
public static readonly DerObjectIdentifier IdPbkdf2 = new DerObjectIdentifier("1.2.840.113549.1.5.12");
public static readonly DerObjectIdentifier DesEde3Cbc = new DerObjectIdentifier("1.2.840.113549.3.7");
public static readonly DerObjectIdentifier RC2Cbc = new DerObjectIdentifier("1.2.840.113549.3.2");
public static readonly DerObjectIdentifier rc4 = new DerObjectIdentifier("1.2.840.113549.3.4");
public static readonly DerObjectIdentifier MD2 = new DerObjectIdentifier("1.2.840.113549.2.2");
public static readonly DerObjectIdentifier MD4 = new DerObjectIdentifier("1.2.840.113549.2.4");
public static readonly DerObjectIdentifier MD5 = new DerObjectIdentifier("1.2.840.113549.2.5");
public static readonly DerObjectIdentifier IdHmacWithSha1 = new DerObjectIdentifier("1.2.840.113549.2.7");
public static readonly DerObjectIdentifier IdHmacWithSha224 = new DerObjectIdentifier("1.2.840.113549.2.8");
public static readonly DerObjectIdentifier IdHmacWithSha256 = new DerObjectIdentifier("1.2.840.113549.2.9");
public static readonly DerObjectIdentifier IdHmacWithSha384 = new DerObjectIdentifier("1.2.840.113549.2.10");
public static readonly DerObjectIdentifier IdHmacWithSha512 = new DerObjectIdentifier("1.2.840.113549.2.11");
public static readonly DerObjectIdentifier Data = new DerObjectIdentifier("1.2.840.113549.1.7.1");
public static readonly DerObjectIdentifier SignedData = new DerObjectIdentifier("1.2.840.113549.1.7.2");
public static readonly DerObjectIdentifier EnvelopedData = new DerObjectIdentifier("1.2.840.113549.1.7.3");
public static readonly DerObjectIdentifier SignedAndEnvelopedData = new DerObjectIdentifier("1.2.840.113549.1.7.4");
public static readonly DerObjectIdentifier DigestedData = new DerObjectIdentifier("1.2.840.113549.1.7.5");
public static readonly DerObjectIdentifier EncryptedData = new DerObjectIdentifier("1.2.840.113549.1.7.6");
public static readonly DerObjectIdentifier Pkcs9AtEmailAddress = new DerObjectIdentifier("1.2.840.113549.1.9.1");
public static readonly DerObjectIdentifier Pkcs9AtUnstructuredName = new DerObjectIdentifier("1.2.840.113549.1.9.2");
public static readonly DerObjectIdentifier Pkcs9AtContentType = new DerObjectIdentifier("1.2.840.113549.1.9.3");
public static readonly DerObjectIdentifier Pkcs9AtMessageDigest = new DerObjectIdentifier("1.2.840.113549.1.9.4");
public static readonly DerObjectIdentifier Pkcs9AtSigningTime = new DerObjectIdentifier("1.2.840.113549.1.9.5");
public static readonly DerObjectIdentifier Pkcs9AtCounterSignature = new DerObjectIdentifier("1.2.840.113549.1.9.6");
public static readonly DerObjectIdentifier Pkcs9AtChallengePassword = new DerObjectIdentifier("1.2.840.113549.1.9.7");
public static readonly DerObjectIdentifier Pkcs9AtUnstructuredAddress = new DerObjectIdentifier("1.2.840.113549.1.9.8");
public static readonly DerObjectIdentifier Pkcs9AtExtendedCertificateAttributes = new DerObjectIdentifier("1.2.840.113549.1.9.9");
public static readonly DerObjectIdentifier Pkcs9AtSigningDescription = new DerObjectIdentifier("1.2.840.113549.1.9.13");
public static readonly DerObjectIdentifier Pkcs9AtExtensionRequest = new DerObjectIdentifier("1.2.840.113549.1.9.14");
public static readonly DerObjectIdentifier Pkcs9AtSmimeCapabilities = new DerObjectIdentifier("1.2.840.113549.1.9.15");
public static readonly DerObjectIdentifier IdSmime = new DerObjectIdentifier("1.2.840.113549.1.9.16");
public static readonly DerObjectIdentifier Pkcs9AtFriendlyName = new DerObjectIdentifier("1.2.840.113549.1.9.20");
public static readonly DerObjectIdentifier Pkcs9AtLocalKeyID = new DerObjectIdentifier("1.2.840.113549.1.9.21");
[Obsolete("Use X509Certificate instead")]
public static readonly DerObjectIdentifier X509CertType = new DerObjectIdentifier("1.2.840.113549.1.9.22.1");
public static readonly DerObjectIdentifier X509Certificate = new DerObjectIdentifier("1.2.840.113549.1.9.22.1");
public static readonly DerObjectIdentifier SdsiCertificate = new DerObjectIdentifier("1.2.840.113549.1.9.22.2");
public static readonly DerObjectIdentifier X509Crl = new DerObjectIdentifier("1.2.840.113549.1.9.23.1");
public static readonly DerObjectIdentifier IdAlg = IdSmime.Branch("3");
public static readonly DerObjectIdentifier IdAlgEsdh = IdAlg.Branch("5");
public static readonly DerObjectIdentifier IdAlgCms3DesWrap = IdAlg.Branch("6");
public static readonly DerObjectIdentifier IdAlgCmsRC2Wrap = IdAlg.Branch("7");
public static readonly DerObjectIdentifier IdAlgPwriKek = IdAlg.Branch("9");
public static readonly DerObjectIdentifier IdAlgSsdh = IdAlg.Branch("10");
public static readonly DerObjectIdentifier IdRsaKem = IdAlg.Branch("14");
public static readonly DerObjectIdentifier PreferSignedData = Pkcs9AtSmimeCapabilities.Branch("1");
public static readonly DerObjectIdentifier CannotDecryptAny = Pkcs9AtSmimeCapabilities.Branch("2");
public static readonly DerObjectIdentifier SmimeCapabilitiesVersions = Pkcs9AtSmimeCapabilities.Branch("3");
public static readonly DerObjectIdentifier IdAAReceiptRequest = IdSmime.Branch("2.1");
public static readonly DerObjectIdentifier IdCTAuthData = new DerObjectIdentifier("1.2.840.113549.1.9.16.1.2");
public static readonly DerObjectIdentifier IdCTTstInfo = new DerObjectIdentifier("1.2.840.113549.1.9.16.1.4");
public static readonly DerObjectIdentifier IdCTCompressedData = new DerObjectIdentifier("1.2.840.113549.1.9.16.1.9");
public static readonly DerObjectIdentifier IdCTAuthEnvelopedData = new DerObjectIdentifier("1.2.840.113549.1.9.16.1.23");
public static readonly DerObjectIdentifier IdCTTimestampedData = new DerObjectIdentifier("1.2.840.113549.1.9.16.1.31");
public static readonly DerObjectIdentifier IdCtiEtsProofOfOrigin = new DerObjectIdentifier("1.2.840.113549.1.9.16.6.1");
public static readonly DerObjectIdentifier IdCtiEtsProofOfReceipt = new DerObjectIdentifier("1.2.840.113549.1.9.16.6.2");
public static readonly DerObjectIdentifier IdCtiEtsProofOfDelivery = new DerObjectIdentifier("1.2.840.113549.1.9.16.6.3");
public static readonly DerObjectIdentifier IdCtiEtsProofOfSender = new DerObjectIdentifier("1.2.840.113549.1.9.16.6.4");
public static readonly DerObjectIdentifier IdCtiEtsProofOfApproval = new DerObjectIdentifier("1.2.840.113549.1.9.16.6.5");
public static readonly DerObjectIdentifier IdCtiEtsProofOfCreation = new DerObjectIdentifier("1.2.840.113549.1.9.16.6.6");
public static readonly DerObjectIdentifier IdAAOid = new DerObjectIdentifier("1.2.840.113549.1.9.16.2");
public static readonly DerObjectIdentifier IdAAContentHint = new DerObjectIdentifier("1.2.840.113549.1.9.16.2.4");
public static readonly DerObjectIdentifier IdAAMsgSigDigest = new DerObjectIdentifier("1.2.840.113549.1.9.16.2.5");
public static readonly DerObjectIdentifier IdAAContentReference = new DerObjectIdentifier("1.2.840.113549.1.9.16.2.10");
public static readonly DerObjectIdentifier IdAAEncrypKeyPref = new DerObjectIdentifier("1.2.840.113549.1.9.16.2.11");
public static readonly DerObjectIdentifier IdAASigningCertificate = new DerObjectIdentifier("1.2.840.113549.1.9.16.2.12");
public static readonly DerObjectIdentifier IdAASigningCertificateV2 = new DerObjectIdentifier("1.2.840.113549.1.9.16.2.47");
public static readonly DerObjectIdentifier IdAAContentIdentifier = new DerObjectIdentifier("1.2.840.113549.1.9.16.2.7");
public static readonly DerObjectIdentifier IdAASignatureTimeStampToken = new DerObjectIdentifier("1.2.840.113549.1.9.16.2.14");
public static readonly DerObjectIdentifier IdAAEtsSigPolicyID = new DerObjectIdentifier("1.2.840.113549.1.9.16.2.15");
public static readonly DerObjectIdentifier IdAAEtsCommitmentType = new DerObjectIdentifier("1.2.840.113549.1.9.16.2.16");
public static readonly DerObjectIdentifier IdAAEtsSignerLocation = new DerObjectIdentifier("1.2.840.113549.1.9.16.2.17");
public static readonly DerObjectIdentifier IdAAEtsSignerAttr = new DerObjectIdentifier("1.2.840.113549.1.9.16.2.18");
public static readonly DerObjectIdentifier IdAAEtsOtherSigCert = new DerObjectIdentifier("1.2.840.113549.1.9.16.2.19");
public static readonly DerObjectIdentifier IdAAEtsContentTimestamp = new DerObjectIdentifier("1.2.840.113549.1.9.16.2.20");
public static readonly DerObjectIdentifier IdAAEtsCertificateRefs = new DerObjectIdentifier("1.2.840.113549.1.9.16.2.21");
public static readonly DerObjectIdentifier IdAAEtsRevocationRefs = new DerObjectIdentifier("1.2.840.113549.1.9.16.2.22");
public static readonly DerObjectIdentifier IdAAEtsCertValues = new DerObjectIdentifier("1.2.840.113549.1.9.16.2.23");
public static readonly DerObjectIdentifier IdAAEtsRevocationValues = new DerObjectIdentifier("1.2.840.113549.1.9.16.2.24");
public static readonly DerObjectIdentifier IdAAEtsEscTimeStamp = new DerObjectIdentifier("1.2.840.113549.1.9.16.2.25");
public static readonly DerObjectIdentifier IdAAEtsCertCrlTimestamp = new DerObjectIdentifier("1.2.840.113549.1.9.16.2.26");
public static readonly DerObjectIdentifier IdAAEtsArchiveTimestamp = new DerObjectIdentifier("1.2.840.113549.1.9.16.2.27");
public static readonly DerObjectIdentifier IdAADecryptKeyID = IdAAOid.Branch("37");
public static readonly DerObjectIdentifier IdAAImplCryptoAlgs = IdAAOid.Branch("38");
public static readonly DerObjectIdentifier IdAAAsymmDecryptKeyID = IdAAOid.Branch("54");
public static readonly DerObjectIdentifier IdAAImplCompressAlgs = IdAAOid.Branch("43");
public static readonly DerObjectIdentifier IdAACommunityIdentifiers = IdAAOid.Branch("40");
[Obsolete("Use 'IdAAEtsSigPolicyID' instead")]
public static readonly DerObjectIdentifier IdAASigPolicyID = IdAAEtsSigPolicyID;
[Obsolete("Use 'IdAAEtsCommitmentType' instead")]
public static readonly DerObjectIdentifier IdAACommitmentType = IdAAEtsCommitmentType;
[Obsolete("Use 'IdAAEtsSignerLocation' instead")]
public static readonly DerObjectIdentifier IdAASignerLocation = IdAAEtsSignerLocation;
[Obsolete("Use 'IdAAEtsOtherSigCert' instead")]
public static readonly DerObjectIdentifier IdAAOtherSigCert = IdAAEtsOtherSigCert;
public static readonly DerObjectIdentifier IdSpqEtsUri = new DerObjectIdentifier("1.2.840.113549.1.9.16.5.1");
public static readonly DerObjectIdentifier IdSpqEtsUNotice = new DerObjectIdentifier("1.2.840.113549.1.9.16.5.2");
public static readonly DerObjectIdentifier KeyBag = new DerObjectIdentifier("1.2.840.113549.1.12.10.1.1");
public static readonly DerObjectIdentifier Pkcs8ShroudedKeyBag = new DerObjectIdentifier("1.2.840.113549.1.12.10.1.2");
public static readonly DerObjectIdentifier CertBag = new DerObjectIdentifier("1.2.840.113549.1.12.10.1.3");
public static readonly DerObjectIdentifier CrlBag = new DerObjectIdentifier("1.2.840.113549.1.12.10.1.4");
public static readonly DerObjectIdentifier SecretBag = new DerObjectIdentifier("1.2.840.113549.1.12.10.1.5");
public static readonly DerObjectIdentifier SafeContentsBag = new DerObjectIdentifier("1.2.840.113549.1.12.10.1.6");
public static readonly DerObjectIdentifier PbeWithShaAnd128BitRC4 = new DerObjectIdentifier("1.2.840.113549.1.12.1.1");
public static readonly DerObjectIdentifier PbeWithShaAnd40BitRC4 = new DerObjectIdentifier("1.2.840.113549.1.12.1.2");
public static readonly DerObjectIdentifier PbeWithShaAnd3KeyTripleDesCbc = new DerObjectIdentifier("1.2.840.113549.1.12.1.3");
public static readonly DerObjectIdentifier PbeWithShaAnd2KeyTripleDesCbc = new DerObjectIdentifier("1.2.840.113549.1.12.1.4");
public static readonly DerObjectIdentifier PbeWithShaAnd128BitRC2Cbc = new DerObjectIdentifier("1.2.840.113549.1.12.1.5");
public static readonly DerObjectIdentifier PbewithShaAnd40BitRC2Cbc = new DerObjectIdentifier("1.2.840.113549.1.12.1.6");
}

View File

@@ -0,0 +1,138 @@
using System;
using System.Collections;
using Org.BouncyCastle.Asn1.X509;
using Org.BouncyCastle.Math;
using Org.BouncyCastle.Utilities.Collections;
namespace Org.BouncyCastle.Asn1.Pkcs;
public class PrivateKeyInfo : Asn1Encodable
{
private readonly DerInteger version;
private readonly AlgorithmIdentifier privateKeyAlgorithm;
private readonly Asn1OctetString privateKey;
private readonly Asn1Set attributes;
private readonly DerBitString publicKey;
public virtual Asn1Set Attributes => attributes;
public virtual bool HasPublicKey => publicKey != null;
public virtual AlgorithmIdentifier PrivateKeyAlgorithm => privateKeyAlgorithm;
public virtual DerBitString PublicKeyData => publicKey;
public static PrivateKeyInfo GetInstance(Asn1TaggedObject obj, bool explicitly)
{
return GetInstance(Asn1Sequence.GetInstance(obj, explicitly));
}
public static PrivateKeyInfo GetInstance(object obj)
{
if (obj == null)
{
return null;
}
if (obj is PrivateKeyInfo)
{
return (PrivateKeyInfo)obj;
}
return new PrivateKeyInfo(Asn1Sequence.GetInstance(obj));
}
private static int GetVersionValue(DerInteger version)
{
BigInteger value = version.Value;
if (value.CompareTo(BigInteger.Zero) < 0 || value.CompareTo(BigInteger.One) > 0)
{
throw new ArgumentException("invalid version for private key info", "version");
}
return value.IntValue;
}
public PrivateKeyInfo(AlgorithmIdentifier privateKeyAlgorithm, Asn1Encodable privateKey)
: this(privateKeyAlgorithm, privateKey, null, null)
{
}
public PrivateKeyInfo(AlgorithmIdentifier privateKeyAlgorithm, Asn1Encodable privateKey, Asn1Set attributes)
: this(privateKeyAlgorithm, privateKey, attributes, null)
{
}
public PrivateKeyInfo(AlgorithmIdentifier privateKeyAlgorithm, Asn1Encodable privateKey, Asn1Set attributes, byte[] publicKey)
{
version = new DerInteger((publicKey != null) ? BigInteger.One : BigInteger.Zero);
this.privateKeyAlgorithm = privateKeyAlgorithm;
this.privateKey = new DerOctetString(privateKey);
this.attributes = attributes;
this.publicKey = ((publicKey == null) ? null : new DerBitString(publicKey));
}
private PrivateKeyInfo(Asn1Sequence seq)
{
IEnumerator enumerator = seq.GetEnumerator();
version = DerInteger.GetInstance(CollectionUtilities.RequireNext(enumerator));
int versionValue = GetVersionValue(version);
privateKeyAlgorithm = AlgorithmIdentifier.GetInstance(CollectionUtilities.RequireNext(enumerator));
privateKey = Asn1OctetString.GetInstance(CollectionUtilities.RequireNext(enumerator));
int num = -1;
while (enumerator.MoveNext())
{
Asn1TaggedObject asn1TaggedObject = (Asn1TaggedObject)enumerator.Current;
int tagNo = asn1TaggedObject.TagNo;
if (tagNo <= num)
{
throw new ArgumentException("invalid optional field in private key info", "seq");
}
num = tagNo;
switch (tagNo)
{
case 0:
attributes = Asn1Set.GetInstance(asn1TaggedObject, explicitly: false);
break;
case 1:
if (versionValue < 1)
{
throw new ArgumentException("'publicKey' requires version v2(1) or later", "seq");
}
publicKey = DerBitString.GetInstance(asn1TaggedObject, isExplicit: false);
break;
default:
throw new ArgumentException("unknown optional field in private key info", "seq");
}
}
}
public virtual Asn1Object ParsePrivateKey()
{
return Asn1Object.FromByteArray(privateKey.GetOctets());
}
public virtual Asn1Object ParsePublicKey()
{
if (publicKey != null)
{
return Asn1Object.FromByteArray(publicKey.GetOctets());
}
return null;
}
public override Asn1Object ToAsn1Object()
{
Asn1EncodableVector asn1EncodableVector = new Asn1EncodableVector(version, privateKeyAlgorithm, privateKey);
if (attributes != null)
{
asn1EncodableVector.Add(new DerTaggedObject(explicitly: false, 0, attributes));
}
if (publicKey != null)
{
asn1EncodableVector.Add(new DerTaggedObject(explicitly: false, 1, publicKey));
}
return new DerSequence(asn1EncodableVector);
}
}

View File

@@ -0,0 +1,71 @@
using System;
using Org.BouncyCastle.Math;
using Org.BouncyCastle.Utilities;
namespace Org.BouncyCastle.Asn1.Pkcs;
public class RC2CbcParameter : Asn1Encodable
{
internal DerInteger version;
internal Asn1OctetString iv;
public BigInteger RC2ParameterVersion
{
get
{
if (version != null)
{
return version.Value;
}
return null;
}
}
public static RC2CbcParameter GetInstance(object obj)
{
if (obj is Asn1Sequence)
{
return new RC2CbcParameter((Asn1Sequence)obj);
}
throw new ArgumentException("Unknown object in factory: " + Platform.GetTypeName(obj), "obj");
}
public RC2CbcParameter(byte[] iv)
{
this.iv = new DerOctetString(iv);
}
public RC2CbcParameter(int parameterVersion, byte[] iv)
{
version = new DerInteger(parameterVersion);
this.iv = new DerOctetString(iv);
}
private RC2CbcParameter(Asn1Sequence seq)
{
if (seq.Count == 1)
{
iv = (Asn1OctetString)seq[0];
return;
}
version = (DerInteger)seq[0];
iv = (Asn1OctetString)seq[1];
}
public byte[] GetIV()
{
return Arrays.Clone(iv.GetOctets());
}
public override Asn1Object ToAsn1Object()
{
Asn1EncodableVector asn1EncodableVector = new Asn1EncodableVector();
if (version != null)
{
asn1EncodableVector.Add(version);
}
asn1EncodableVector.Add(iv);
return new DerSequence(asn1EncodableVector);
}
}

View File

@@ -0,0 +1,92 @@
using System;
using Org.BouncyCastle.Math;
namespace Org.BouncyCastle.Asn1.Pkcs;
public class RsaPrivateKeyStructure : Asn1Encodable
{
private readonly BigInteger modulus;
private readonly BigInteger publicExponent;
private readonly BigInteger privateExponent;
private readonly BigInteger prime1;
private readonly BigInteger prime2;
private readonly BigInteger exponent1;
private readonly BigInteger exponent2;
private readonly BigInteger coefficient;
public BigInteger Modulus => modulus;
public BigInteger PublicExponent => publicExponent;
public BigInteger PrivateExponent => privateExponent;
public BigInteger Prime1 => prime1;
public BigInteger Prime2 => prime2;
public BigInteger Exponent1 => exponent1;
public BigInteger Exponent2 => exponent2;
public BigInteger Coefficient => coefficient;
public static RsaPrivateKeyStructure GetInstance(Asn1TaggedObject obj, bool isExplicit)
{
return GetInstance(Asn1Sequence.GetInstance(obj, isExplicit));
}
public static RsaPrivateKeyStructure GetInstance(object obj)
{
if (obj == null)
{
return null;
}
if (obj is RsaPrivateKeyStructure)
{
return (RsaPrivateKeyStructure)obj;
}
return new RsaPrivateKeyStructure(Asn1Sequence.GetInstance(obj));
}
public RsaPrivateKeyStructure(BigInteger modulus, BigInteger publicExponent, BigInteger privateExponent, BigInteger prime1, BigInteger prime2, BigInteger exponent1, BigInteger exponent2, BigInteger coefficient)
{
this.modulus = modulus;
this.publicExponent = publicExponent;
this.privateExponent = privateExponent;
this.prime1 = prime1;
this.prime2 = prime2;
this.exponent1 = exponent1;
this.exponent2 = exponent2;
this.coefficient = coefficient;
}
[Obsolete("Use 'GetInstance' method(s) instead")]
public RsaPrivateKeyStructure(Asn1Sequence seq)
{
BigInteger value = ((DerInteger)seq[0]).Value;
if (value.IntValue != 0)
{
throw new ArgumentException("wrong version for RSA private key");
}
modulus = ((DerInteger)seq[1]).Value;
publicExponent = ((DerInteger)seq[2]).Value;
privateExponent = ((DerInteger)seq[3]).Value;
prime1 = ((DerInteger)seq[4]).Value;
prime2 = ((DerInteger)seq[5]).Value;
exponent1 = ((DerInteger)seq[6]).Value;
exponent2 = ((DerInteger)seq[7]).Value;
coefficient = ((DerInteger)seq[8]).Value;
}
public override Asn1Object ToAsn1Object()
{
return new DerSequence(new DerInteger(0), new DerInteger(Modulus), new DerInteger(PublicExponent), new DerInteger(PrivateExponent), new DerInteger(Prime1), new DerInteger(Prime2), new DerInteger(Exponent1), new DerInteger(Exponent2), new DerInteger(Coefficient));
}
}

View File

@@ -0,0 +1,97 @@
using System;
using Org.BouncyCastle.Asn1.Oiw;
using Org.BouncyCastle.Asn1.X509;
using Org.BouncyCastle.Utilities;
namespace Org.BouncyCastle.Asn1.Pkcs;
public class RsaesOaepParameters : Asn1Encodable
{
private AlgorithmIdentifier hashAlgorithm;
private AlgorithmIdentifier maskGenAlgorithm;
private AlgorithmIdentifier pSourceAlgorithm;
public static readonly AlgorithmIdentifier DefaultHashAlgorithm = new AlgorithmIdentifier(OiwObjectIdentifiers.IdSha1, DerNull.Instance);
public static readonly AlgorithmIdentifier DefaultMaskGenFunction = new AlgorithmIdentifier(PkcsObjectIdentifiers.IdMgf1, DefaultHashAlgorithm);
public static readonly AlgorithmIdentifier DefaultPSourceAlgorithm = new AlgorithmIdentifier(PkcsObjectIdentifiers.IdPSpecified, new DerOctetString(new byte[0]));
public AlgorithmIdentifier HashAlgorithm => hashAlgorithm;
public AlgorithmIdentifier MaskGenAlgorithm => maskGenAlgorithm;
public AlgorithmIdentifier PSourceAlgorithm => pSourceAlgorithm;
public static RsaesOaepParameters GetInstance(object obj)
{
if (obj is RsaesOaepParameters)
{
return (RsaesOaepParameters)obj;
}
if (obj is Asn1Sequence)
{
return new RsaesOaepParameters((Asn1Sequence)obj);
}
throw new ArgumentException("Unknown object in factory: " + Platform.GetTypeName(obj), "obj");
}
public RsaesOaepParameters()
{
hashAlgorithm = DefaultHashAlgorithm;
maskGenAlgorithm = DefaultMaskGenFunction;
pSourceAlgorithm = DefaultPSourceAlgorithm;
}
public RsaesOaepParameters(AlgorithmIdentifier hashAlgorithm, AlgorithmIdentifier maskGenAlgorithm, AlgorithmIdentifier pSourceAlgorithm)
{
this.hashAlgorithm = hashAlgorithm;
this.maskGenAlgorithm = maskGenAlgorithm;
this.pSourceAlgorithm = pSourceAlgorithm;
}
public RsaesOaepParameters(Asn1Sequence seq)
{
hashAlgorithm = DefaultHashAlgorithm;
maskGenAlgorithm = DefaultMaskGenFunction;
pSourceAlgorithm = DefaultPSourceAlgorithm;
for (int i = 0; i != seq.Count; i++)
{
Asn1TaggedObject asn1TaggedObject = (Asn1TaggedObject)seq[i];
switch (asn1TaggedObject.TagNo)
{
case 0:
hashAlgorithm = AlgorithmIdentifier.GetInstance(asn1TaggedObject, explicitly: true);
break;
case 1:
maskGenAlgorithm = AlgorithmIdentifier.GetInstance(asn1TaggedObject, explicitly: true);
break;
case 2:
pSourceAlgorithm = AlgorithmIdentifier.GetInstance(asn1TaggedObject, explicitly: true);
break;
default:
throw new ArgumentException("unknown tag");
}
}
}
public override Asn1Object ToAsn1Object()
{
Asn1EncodableVector asn1EncodableVector = new Asn1EncodableVector();
if (!hashAlgorithm.Equals(DefaultHashAlgorithm))
{
asn1EncodableVector.Add(new DerTaggedObject(explicitly: true, 0, hashAlgorithm));
}
if (!maskGenAlgorithm.Equals(DefaultMaskGenFunction))
{
asn1EncodableVector.Add(new DerTaggedObject(explicitly: true, 1, maskGenAlgorithm));
}
if (!pSourceAlgorithm.Equals(DefaultPSourceAlgorithm))
{
asn1EncodableVector.Add(new DerTaggedObject(explicitly: true, 2, pSourceAlgorithm));
}
return new DerSequence(asn1EncodableVector);
}
}

View File

@@ -0,0 +1,113 @@
using System;
using Org.BouncyCastle.Asn1.Oiw;
using Org.BouncyCastle.Asn1.X509;
using Org.BouncyCastle.Utilities;
namespace Org.BouncyCastle.Asn1.Pkcs;
public class RsassaPssParameters : Asn1Encodable
{
private AlgorithmIdentifier hashAlgorithm;
private AlgorithmIdentifier maskGenAlgorithm;
private DerInteger saltLength;
private DerInteger trailerField;
public static readonly AlgorithmIdentifier DefaultHashAlgorithm = new AlgorithmIdentifier(OiwObjectIdentifiers.IdSha1, DerNull.Instance);
public static readonly AlgorithmIdentifier DefaultMaskGenFunction = new AlgorithmIdentifier(PkcsObjectIdentifiers.IdMgf1, DefaultHashAlgorithm);
public static readonly DerInteger DefaultSaltLength = new DerInteger(20);
public static readonly DerInteger DefaultTrailerField = new DerInteger(1);
public AlgorithmIdentifier HashAlgorithm => hashAlgorithm;
public AlgorithmIdentifier MaskGenAlgorithm => maskGenAlgorithm;
public DerInteger SaltLength => saltLength;
public DerInteger TrailerField => trailerField;
public static RsassaPssParameters GetInstance(object obj)
{
if (obj == null || obj is RsassaPssParameters)
{
return (RsassaPssParameters)obj;
}
if (obj is Asn1Sequence)
{
return new RsassaPssParameters((Asn1Sequence)obj);
}
throw new ArgumentException("Unknown object in factory: " + Platform.GetTypeName(obj), "obj");
}
public RsassaPssParameters()
{
hashAlgorithm = DefaultHashAlgorithm;
maskGenAlgorithm = DefaultMaskGenFunction;
saltLength = DefaultSaltLength;
trailerField = DefaultTrailerField;
}
public RsassaPssParameters(AlgorithmIdentifier hashAlgorithm, AlgorithmIdentifier maskGenAlgorithm, DerInteger saltLength, DerInteger trailerField)
{
this.hashAlgorithm = hashAlgorithm;
this.maskGenAlgorithm = maskGenAlgorithm;
this.saltLength = saltLength;
this.trailerField = trailerField;
}
public RsassaPssParameters(Asn1Sequence seq)
{
hashAlgorithm = DefaultHashAlgorithm;
maskGenAlgorithm = DefaultMaskGenFunction;
saltLength = DefaultSaltLength;
trailerField = DefaultTrailerField;
for (int i = 0; i != seq.Count; i++)
{
Asn1TaggedObject asn1TaggedObject = (Asn1TaggedObject)seq[i];
switch (asn1TaggedObject.TagNo)
{
case 0:
hashAlgorithm = AlgorithmIdentifier.GetInstance(asn1TaggedObject, explicitly: true);
break;
case 1:
maskGenAlgorithm = AlgorithmIdentifier.GetInstance(asn1TaggedObject, explicitly: true);
break;
case 2:
saltLength = DerInteger.GetInstance(asn1TaggedObject, isExplicit: true);
break;
case 3:
trailerField = DerInteger.GetInstance(asn1TaggedObject, isExplicit: true);
break;
default:
throw new ArgumentException("unknown tag");
}
}
}
public override Asn1Object ToAsn1Object()
{
Asn1EncodableVector asn1EncodableVector = new Asn1EncodableVector();
if (!hashAlgorithm.Equals(DefaultHashAlgorithm))
{
asn1EncodableVector.Add(new DerTaggedObject(explicitly: true, 0, hashAlgorithm));
}
if (!maskGenAlgorithm.Equals(DefaultMaskGenFunction))
{
asn1EncodableVector.Add(new DerTaggedObject(explicitly: true, 1, maskGenAlgorithm));
}
if (!saltLength.Equals(DefaultSaltLength))
{
asn1EncodableVector.Add(new DerTaggedObject(explicitly: true, 2, saltLength));
}
if (!trailerField.Equals(DefaultTrailerField))
{
asn1EncodableVector.Add(new DerTaggedObject(explicitly: true, 3, trailerField));
}
return new DerSequence(asn1EncodableVector);
}
}

View File

@@ -0,0 +1,50 @@
namespace Org.BouncyCastle.Asn1.Pkcs;
public class SafeBag : Asn1Encodable
{
private readonly DerObjectIdentifier bagID;
private readonly Asn1Object bagValue;
private readonly Asn1Set bagAttributes;
public DerObjectIdentifier BagID => bagID;
public Asn1Object BagValue => bagValue;
public Asn1Set BagAttributes => bagAttributes;
public SafeBag(DerObjectIdentifier oid, Asn1Object obj)
{
bagID = oid;
bagValue = obj;
bagAttributes = null;
}
public SafeBag(DerObjectIdentifier oid, Asn1Object obj, Asn1Set bagAttributes)
{
bagID = oid;
bagValue = obj;
this.bagAttributes = bagAttributes;
}
public SafeBag(Asn1Sequence seq)
{
bagID = (DerObjectIdentifier)seq[0];
bagValue = ((DerTaggedObject)seq[1]).GetObject();
if (seq.Count == 3)
{
bagAttributes = (Asn1Set)seq[2];
}
}
public override Asn1Object ToAsn1Object()
{
Asn1EncodableVector asn1EncodableVector = new Asn1EncodableVector(bagID, new DerTaggedObject(0, bagValue));
if (bagAttributes != null)
{
asn1EncodableVector.Add(bagAttributes);
}
return new DerSequence(asn1EncodableVector);
}
}

View File

@@ -0,0 +1,103 @@
using System;
using System.Collections;
namespace Org.BouncyCastle.Asn1.Pkcs;
public class SignedData : Asn1Encodable
{
private readonly DerInteger version;
private readonly Asn1Set digestAlgorithms;
private readonly ContentInfo contentInfo;
private readonly Asn1Set certificates;
private readonly Asn1Set crls;
private readonly Asn1Set signerInfos;
public DerInteger Version => version;
public Asn1Set DigestAlgorithms => digestAlgorithms;
public ContentInfo ContentInfo => contentInfo;
public Asn1Set Certificates => certificates;
public Asn1Set Crls => crls;
public Asn1Set SignerInfos => signerInfos;
public static SignedData GetInstance(object obj)
{
if (obj == null)
{
return null;
}
if (obj is SignedData result)
{
return result;
}
return new SignedData(Asn1Sequence.GetInstance(obj));
}
public SignedData(DerInteger _version, Asn1Set _digestAlgorithms, ContentInfo _contentInfo, Asn1Set _certificates, Asn1Set _crls, Asn1Set _signerInfos)
{
version = _version;
digestAlgorithms = _digestAlgorithms;
contentInfo = _contentInfo;
certificates = _certificates;
crls = _crls;
signerInfos = _signerInfos;
}
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 DerTaggedObject)
{
DerTaggedObject derTaggedObject = (DerTaggedObject)asn1Object;
switch (derTaggedObject.TagNo)
{
case 0:
certificates = Asn1Set.GetInstance(derTaggedObject, explicitly: false);
break;
case 1:
crls = Asn1Set.GetInstance(derTaggedObject, explicitly: false);
break;
default:
throw new ArgumentException("unknown tag value " + derTaggedObject.TagNo);
}
}
else
{
signerInfos = (Asn1Set)asn1Object;
}
}
}
public override Asn1Object ToAsn1Object()
{
Asn1EncodableVector asn1EncodableVector = new Asn1EncodableVector(version, digestAlgorithms, contentInfo);
if (certificates != null)
{
asn1EncodableVector.Add(new DerTaggedObject(explicitly: false, 0, certificates));
}
if (crls != null)
{
asn1EncodableVector.Add(new DerTaggedObject(explicitly: false, 1, crls));
}
asn1EncodableVector.Add(signerInfos);
return new BerSequence(asn1EncodableVector);
}
}

View File

@@ -0,0 +1,110 @@
using System;
using System.Collections;
using Org.BouncyCastle.Asn1.X509;
using Org.BouncyCastle.Utilities;
namespace Org.BouncyCastle.Asn1.Pkcs;
public class SignerInfo : Asn1Encodable
{
private DerInteger version;
private IssuerAndSerialNumber issuerAndSerialNumber;
private AlgorithmIdentifier digAlgorithm;
private Asn1Set authenticatedAttributes;
private AlgorithmIdentifier digEncryptionAlgorithm;
private Asn1OctetString encryptedDigest;
private Asn1Set unauthenticatedAttributes;
public DerInteger Version => version;
public IssuerAndSerialNumber IssuerAndSerialNumber => issuerAndSerialNumber;
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 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(DerInteger version, IssuerAndSerialNumber issuerAndSerialNumber, AlgorithmIdentifier digAlgorithm, Asn1Set authenticatedAttributes, AlgorithmIdentifier digEncryptionAlgorithm, Asn1OctetString encryptedDigest, Asn1Set unauthenticatedAttributes)
{
this.version = version;
this.issuerAndSerialNumber = issuerAndSerialNumber;
this.digAlgorithm = digAlgorithm;
this.authenticatedAttributes = authenticatedAttributes;
this.digEncryptionAlgorithm = digEncryptionAlgorithm;
this.encryptedDigest = encryptedDigest;
this.unauthenticatedAttributes = unauthenticatedAttributes;
}
public SignerInfo(Asn1Sequence seq)
{
IEnumerator enumerator = seq.GetEnumerator();
enumerator.MoveNext();
version = (DerInteger)enumerator.Current;
enumerator.MoveNext();
issuerAndSerialNumber = IssuerAndSerialNumber.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, issuerAndSerialNumber, 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);
}
}