init commit
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
using System;
|
||||
|
||||
namespace Org.BouncyCastle.X509.Store;
|
||||
|
||||
public interface IX509Selector : ICloneable
|
||||
{
|
||||
bool Match(object obj);
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
using System.Collections;
|
||||
|
||||
namespace Org.BouncyCastle.X509.Store;
|
||||
|
||||
public interface IX509Store
|
||||
{
|
||||
ICollection GetMatches(IX509Selector selector);
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
namespace Org.BouncyCastle.X509.Store;
|
||||
|
||||
public interface IX509StoreParameters
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
using System;
|
||||
|
||||
namespace Org.BouncyCastle.X509.Store;
|
||||
|
||||
[Serializable]
|
||||
public class NoSuchStoreException : X509StoreException
|
||||
{
|
||||
public NoSuchStoreException()
|
||||
{
|
||||
}
|
||||
|
||||
public NoSuchStoreException(string message)
|
||||
: base(message)
|
||||
{
|
||||
}
|
||||
|
||||
public NoSuchStoreException(string message, Exception e)
|
||||
: base(message, e)
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,280 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using Org.BouncyCastle.Asn1;
|
||||
using Org.BouncyCastle.Asn1.X509;
|
||||
using Org.BouncyCastle.Math;
|
||||
using Org.BouncyCastle.Utilities.Collections;
|
||||
using Org.BouncyCastle.Utilities.Date;
|
||||
using Org.BouncyCastle.X509.Extension;
|
||||
|
||||
namespace Org.BouncyCastle.X509.Store;
|
||||
|
||||
public class X509AttrCertStoreSelector : IX509Selector, ICloneable
|
||||
{
|
||||
private IX509AttributeCertificate attributeCert;
|
||||
|
||||
private DateTimeObject attributeCertificateValid;
|
||||
|
||||
private AttributeCertificateHolder holder;
|
||||
|
||||
private AttributeCertificateIssuer issuer;
|
||||
|
||||
private BigInteger serialNumber;
|
||||
|
||||
private ISet targetNames = new HashSet();
|
||||
|
||||
private ISet targetGroups = new HashSet();
|
||||
|
||||
public IX509AttributeCertificate AttributeCert
|
||||
{
|
||||
get
|
||||
{
|
||||
return attributeCert;
|
||||
}
|
||||
set
|
||||
{
|
||||
attributeCert = value;
|
||||
}
|
||||
}
|
||||
|
||||
[Obsolete("Use AttributeCertificateValid instead")]
|
||||
public DateTimeObject AttribueCertificateValid
|
||||
{
|
||||
get
|
||||
{
|
||||
return attributeCertificateValid;
|
||||
}
|
||||
set
|
||||
{
|
||||
attributeCertificateValid = value;
|
||||
}
|
||||
}
|
||||
|
||||
public DateTimeObject AttributeCertificateValid
|
||||
{
|
||||
get
|
||||
{
|
||||
return attributeCertificateValid;
|
||||
}
|
||||
set
|
||||
{
|
||||
attributeCertificateValid = value;
|
||||
}
|
||||
}
|
||||
|
||||
public AttributeCertificateHolder Holder
|
||||
{
|
||||
get
|
||||
{
|
||||
return holder;
|
||||
}
|
||||
set
|
||||
{
|
||||
holder = value;
|
||||
}
|
||||
}
|
||||
|
||||
public AttributeCertificateIssuer Issuer
|
||||
{
|
||||
get
|
||||
{
|
||||
return issuer;
|
||||
}
|
||||
set
|
||||
{
|
||||
issuer = value;
|
||||
}
|
||||
}
|
||||
|
||||
public BigInteger SerialNumber
|
||||
{
|
||||
get
|
||||
{
|
||||
return serialNumber;
|
||||
}
|
||||
set
|
||||
{
|
||||
serialNumber = value;
|
||||
}
|
||||
}
|
||||
|
||||
public X509AttrCertStoreSelector()
|
||||
{
|
||||
}
|
||||
|
||||
private X509AttrCertStoreSelector(X509AttrCertStoreSelector o)
|
||||
{
|
||||
attributeCert = o.attributeCert;
|
||||
attributeCertificateValid = o.attributeCertificateValid;
|
||||
holder = o.holder;
|
||||
issuer = o.issuer;
|
||||
serialNumber = o.serialNumber;
|
||||
targetGroups = new HashSet(o.targetGroups);
|
||||
targetNames = new HashSet(o.targetNames);
|
||||
}
|
||||
|
||||
public bool Match(object obj)
|
||||
{
|
||||
if (obj == null)
|
||||
{
|
||||
throw new ArgumentNullException("obj");
|
||||
}
|
||||
if (!(obj is IX509AttributeCertificate iX509AttributeCertificate))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (attributeCert != null && !attributeCert.Equals(iX509AttributeCertificate))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (serialNumber != null && !iX509AttributeCertificate.SerialNumber.Equals(serialNumber))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (holder != null && !iX509AttributeCertificate.Holder.Equals(holder))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (issuer != null && !iX509AttributeCertificate.Issuer.Equals(issuer))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (attributeCertificateValid != null && !iX509AttributeCertificate.IsValid(attributeCertificateValid.Value))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (targetNames.Count > 0 || targetGroups.Count > 0)
|
||||
{
|
||||
Asn1OctetString extensionValue = iX509AttributeCertificate.GetExtensionValue(X509Extensions.TargetInformation);
|
||||
if (extensionValue != null)
|
||||
{
|
||||
TargetInformation instance;
|
||||
try
|
||||
{
|
||||
instance = TargetInformation.GetInstance(X509ExtensionUtilities.FromExtensionValue(extensionValue));
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
Targets[] targetsObjects = instance.GetTargetsObjects();
|
||||
if (targetNames.Count > 0)
|
||||
{
|
||||
bool flag = false;
|
||||
for (int i = 0; i < targetsObjects.Length; i++)
|
||||
{
|
||||
if (flag)
|
||||
{
|
||||
break;
|
||||
}
|
||||
Target[] targets = targetsObjects[i].GetTargets();
|
||||
for (int j = 0; j < targets.Length; j++)
|
||||
{
|
||||
GeneralName targetName = targets[j].TargetName;
|
||||
if (targetName != null && targetNames.Contains(targetName))
|
||||
{
|
||||
flag = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!flag)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (targetGroups.Count > 0)
|
||||
{
|
||||
bool flag2 = false;
|
||||
for (int k = 0; k < targetsObjects.Length; k++)
|
||||
{
|
||||
if (flag2)
|
||||
{
|
||||
break;
|
||||
}
|
||||
Target[] targets2 = targetsObjects[k].GetTargets();
|
||||
for (int l = 0; l < targets2.Length; l++)
|
||||
{
|
||||
GeneralName targetGroup = targets2[l].TargetGroup;
|
||||
if (targetGroup != null && targetGroups.Contains(targetGroup))
|
||||
{
|
||||
flag2 = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!flag2)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public object Clone()
|
||||
{
|
||||
return new X509AttrCertStoreSelector(this);
|
||||
}
|
||||
|
||||
public void AddTargetName(GeneralName name)
|
||||
{
|
||||
targetNames.Add(name);
|
||||
}
|
||||
|
||||
public void AddTargetName(byte[] name)
|
||||
{
|
||||
AddTargetName(GeneralName.GetInstance(Asn1Object.FromByteArray(name)));
|
||||
}
|
||||
|
||||
public void SetTargetNames(IEnumerable names)
|
||||
{
|
||||
targetNames = ExtractGeneralNames(names);
|
||||
}
|
||||
|
||||
public IEnumerable GetTargetNames()
|
||||
{
|
||||
return new EnumerableProxy(targetNames);
|
||||
}
|
||||
|
||||
public void AddTargetGroup(GeneralName group)
|
||||
{
|
||||
targetGroups.Add(group);
|
||||
}
|
||||
|
||||
public void AddTargetGroup(byte[] name)
|
||||
{
|
||||
AddTargetGroup(GeneralName.GetInstance(Asn1Object.FromByteArray(name)));
|
||||
}
|
||||
|
||||
public void SetTargetGroups(IEnumerable names)
|
||||
{
|
||||
targetGroups = ExtractGeneralNames(names);
|
||||
}
|
||||
|
||||
public IEnumerable GetTargetGroups()
|
||||
{
|
||||
return new EnumerableProxy(targetGroups);
|
||||
}
|
||||
|
||||
private ISet ExtractGeneralNames(IEnumerable names)
|
||||
{
|
||||
ISet set = new HashSet();
|
||||
if (names != null)
|
||||
{
|
||||
foreach (object name in names)
|
||||
{
|
||||
if (name is GeneralName)
|
||||
{
|
||||
set.Add(name);
|
||||
}
|
||||
else
|
||||
{
|
||||
set.Add(GeneralName.GetInstance(Asn1Object.FromByteArray((byte[])name)));
|
||||
}
|
||||
}
|
||||
}
|
||||
return set;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
using System;
|
||||
|
||||
namespace Org.BouncyCastle.X509.Store;
|
||||
|
||||
public class X509CertPairStoreSelector : IX509Selector, ICloneable
|
||||
{
|
||||
private X509CertificatePair certPair;
|
||||
|
||||
private X509CertStoreSelector forwardSelector;
|
||||
|
||||
private X509CertStoreSelector reverseSelector;
|
||||
|
||||
public X509CertificatePair CertPair
|
||||
{
|
||||
get
|
||||
{
|
||||
return certPair;
|
||||
}
|
||||
set
|
||||
{
|
||||
certPair = value;
|
||||
}
|
||||
}
|
||||
|
||||
public X509CertStoreSelector ForwardSelector
|
||||
{
|
||||
get
|
||||
{
|
||||
return CloneSelector(forwardSelector);
|
||||
}
|
||||
set
|
||||
{
|
||||
forwardSelector = CloneSelector(value);
|
||||
}
|
||||
}
|
||||
|
||||
public X509CertStoreSelector ReverseSelector
|
||||
{
|
||||
get
|
||||
{
|
||||
return CloneSelector(reverseSelector);
|
||||
}
|
||||
set
|
||||
{
|
||||
reverseSelector = CloneSelector(value);
|
||||
}
|
||||
}
|
||||
|
||||
private static X509CertStoreSelector CloneSelector(X509CertStoreSelector s)
|
||||
{
|
||||
if (s != null)
|
||||
{
|
||||
return (X509CertStoreSelector)s.Clone();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public X509CertPairStoreSelector()
|
||||
{
|
||||
}
|
||||
|
||||
private X509CertPairStoreSelector(X509CertPairStoreSelector o)
|
||||
{
|
||||
certPair = o.CertPair;
|
||||
forwardSelector = o.ForwardSelector;
|
||||
reverseSelector = o.ReverseSelector;
|
||||
}
|
||||
|
||||
public bool Match(object obj)
|
||||
{
|
||||
if (obj == null)
|
||||
{
|
||||
throw new ArgumentNullException("obj");
|
||||
}
|
||||
if (!(obj is X509CertificatePair x509CertificatePair))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (certPair != null && !certPair.Equals(x509CertificatePair))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (forwardSelector != null && !forwardSelector.Match(x509CertificatePair.Forward))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (reverseSelector != null && !reverseSelector.Match(x509CertificatePair.Reverse))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public object Clone()
|
||||
{
|
||||
return new X509CertPairStoreSelector(this);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,454 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using Org.BouncyCastle.Asn1;
|
||||
using Org.BouncyCastle.Asn1.X509;
|
||||
using Org.BouncyCastle.Math;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
using Org.BouncyCastle.Utilities.Collections;
|
||||
using Org.BouncyCastle.Utilities.Date;
|
||||
using Org.BouncyCastle.X509.Extension;
|
||||
|
||||
namespace Org.BouncyCastle.X509.Store;
|
||||
|
||||
public class X509CertStoreSelector : IX509Selector, ICloneable
|
||||
{
|
||||
private byte[] authorityKeyIdentifier;
|
||||
|
||||
private int basicConstraints = -1;
|
||||
|
||||
private X509Certificate certificate;
|
||||
|
||||
private DateTimeObject certificateValid;
|
||||
|
||||
private ISet extendedKeyUsage;
|
||||
|
||||
private bool ignoreX509NameOrdering;
|
||||
|
||||
private X509Name issuer;
|
||||
|
||||
private bool[] keyUsage;
|
||||
|
||||
private ISet policy;
|
||||
|
||||
private DateTimeObject privateKeyValid;
|
||||
|
||||
private BigInteger serialNumber;
|
||||
|
||||
private X509Name subject;
|
||||
|
||||
private byte[] subjectKeyIdentifier;
|
||||
|
||||
private SubjectPublicKeyInfo subjectPublicKey;
|
||||
|
||||
private DerObjectIdentifier subjectPublicKeyAlgID;
|
||||
|
||||
public byte[] AuthorityKeyIdentifier
|
||||
{
|
||||
get
|
||||
{
|
||||
return Arrays.Clone(authorityKeyIdentifier);
|
||||
}
|
||||
set
|
||||
{
|
||||
authorityKeyIdentifier = Arrays.Clone(value);
|
||||
}
|
||||
}
|
||||
|
||||
public int BasicConstraints
|
||||
{
|
||||
get
|
||||
{
|
||||
return basicConstraints;
|
||||
}
|
||||
set
|
||||
{
|
||||
if (value < -2)
|
||||
{
|
||||
throw new ArgumentException("value can't be less than -2", "value");
|
||||
}
|
||||
basicConstraints = value;
|
||||
}
|
||||
}
|
||||
|
||||
public X509Certificate Certificate
|
||||
{
|
||||
get
|
||||
{
|
||||
return certificate;
|
||||
}
|
||||
set
|
||||
{
|
||||
certificate = value;
|
||||
}
|
||||
}
|
||||
|
||||
public DateTimeObject CertificateValid
|
||||
{
|
||||
get
|
||||
{
|
||||
return certificateValid;
|
||||
}
|
||||
set
|
||||
{
|
||||
certificateValid = value;
|
||||
}
|
||||
}
|
||||
|
||||
public ISet ExtendedKeyUsage
|
||||
{
|
||||
get
|
||||
{
|
||||
return CopySet(extendedKeyUsage);
|
||||
}
|
||||
set
|
||||
{
|
||||
extendedKeyUsage = CopySet(value);
|
||||
}
|
||||
}
|
||||
|
||||
public bool IgnoreX509NameOrdering
|
||||
{
|
||||
get
|
||||
{
|
||||
return ignoreX509NameOrdering;
|
||||
}
|
||||
set
|
||||
{
|
||||
ignoreX509NameOrdering = value;
|
||||
}
|
||||
}
|
||||
|
||||
public X509Name Issuer
|
||||
{
|
||||
get
|
||||
{
|
||||
return issuer;
|
||||
}
|
||||
set
|
||||
{
|
||||
issuer = value;
|
||||
}
|
||||
}
|
||||
|
||||
[Obsolete("Avoid working with X509Name objects in string form")]
|
||||
public string IssuerAsString
|
||||
{
|
||||
get
|
||||
{
|
||||
if (issuer == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return issuer.ToString();
|
||||
}
|
||||
}
|
||||
|
||||
public bool[] KeyUsage
|
||||
{
|
||||
get
|
||||
{
|
||||
return CopyBoolArray(keyUsage);
|
||||
}
|
||||
set
|
||||
{
|
||||
keyUsage = CopyBoolArray(value);
|
||||
}
|
||||
}
|
||||
|
||||
public ISet Policy
|
||||
{
|
||||
get
|
||||
{
|
||||
return CopySet(policy);
|
||||
}
|
||||
set
|
||||
{
|
||||
policy = CopySet(value);
|
||||
}
|
||||
}
|
||||
|
||||
public DateTimeObject PrivateKeyValid
|
||||
{
|
||||
get
|
||||
{
|
||||
return privateKeyValid;
|
||||
}
|
||||
set
|
||||
{
|
||||
privateKeyValid = value;
|
||||
}
|
||||
}
|
||||
|
||||
public BigInteger SerialNumber
|
||||
{
|
||||
get
|
||||
{
|
||||
return serialNumber;
|
||||
}
|
||||
set
|
||||
{
|
||||
serialNumber = value;
|
||||
}
|
||||
}
|
||||
|
||||
public X509Name Subject
|
||||
{
|
||||
get
|
||||
{
|
||||
return subject;
|
||||
}
|
||||
set
|
||||
{
|
||||
subject = value;
|
||||
}
|
||||
}
|
||||
|
||||
[Obsolete("Avoid working with X509Name objects in string form")]
|
||||
public string SubjectAsString
|
||||
{
|
||||
get
|
||||
{
|
||||
if (subject == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return subject.ToString();
|
||||
}
|
||||
}
|
||||
|
||||
public byte[] SubjectKeyIdentifier
|
||||
{
|
||||
get
|
||||
{
|
||||
return Arrays.Clone(subjectKeyIdentifier);
|
||||
}
|
||||
set
|
||||
{
|
||||
subjectKeyIdentifier = Arrays.Clone(value);
|
||||
}
|
||||
}
|
||||
|
||||
public SubjectPublicKeyInfo SubjectPublicKey
|
||||
{
|
||||
get
|
||||
{
|
||||
return subjectPublicKey;
|
||||
}
|
||||
set
|
||||
{
|
||||
subjectPublicKey = value;
|
||||
}
|
||||
}
|
||||
|
||||
public DerObjectIdentifier SubjectPublicKeyAlgID
|
||||
{
|
||||
get
|
||||
{
|
||||
return subjectPublicKeyAlgID;
|
||||
}
|
||||
set
|
||||
{
|
||||
subjectPublicKeyAlgID = value;
|
||||
}
|
||||
}
|
||||
|
||||
public X509CertStoreSelector()
|
||||
{
|
||||
}
|
||||
|
||||
public X509CertStoreSelector(X509CertStoreSelector o)
|
||||
{
|
||||
authorityKeyIdentifier = o.AuthorityKeyIdentifier;
|
||||
basicConstraints = o.BasicConstraints;
|
||||
certificate = o.Certificate;
|
||||
certificateValid = o.CertificateValid;
|
||||
extendedKeyUsage = o.ExtendedKeyUsage;
|
||||
ignoreX509NameOrdering = o.IgnoreX509NameOrdering;
|
||||
issuer = o.Issuer;
|
||||
keyUsage = o.KeyUsage;
|
||||
policy = o.Policy;
|
||||
privateKeyValid = o.PrivateKeyValid;
|
||||
serialNumber = o.SerialNumber;
|
||||
subject = o.Subject;
|
||||
subjectKeyIdentifier = o.SubjectKeyIdentifier;
|
||||
subjectPublicKey = o.SubjectPublicKey;
|
||||
subjectPublicKeyAlgID = o.SubjectPublicKeyAlgID;
|
||||
}
|
||||
|
||||
public virtual object Clone()
|
||||
{
|
||||
return new X509CertStoreSelector(this);
|
||||
}
|
||||
|
||||
public virtual bool Match(object obj)
|
||||
{
|
||||
if (!(obj is X509Certificate x509Certificate))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (!MatchExtension(authorityKeyIdentifier, x509Certificate, X509Extensions.AuthorityKeyIdentifier))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (basicConstraints != -1)
|
||||
{
|
||||
int num = x509Certificate.GetBasicConstraints();
|
||||
if (basicConstraints == -2)
|
||||
{
|
||||
if (num != -1)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else if (num < basicConstraints)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (certificate != null && !certificate.Equals(x509Certificate))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (certificateValid != null && !x509Certificate.IsValid(certificateValid.Value))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (extendedKeyUsage != null)
|
||||
{
|
||||
IList list = x509Certificate.GetExtendedKeyUsage();
|
||||
if (list != null)
|
||||
{
|
||||
foreach (DerObjectIdentifier item in extendedKeyUsage)
|
||||
{
|
||||
if (!list.Contains(item.Id))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (issuer != null && !issuer.Equivalent(x509Certificate.IssuerDN, !ignoreX509NameOrdering))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (keyUsage != null)
|
||||
{
|
||||
bool[] array = x509Certificate.GetKeyUsage();
|
||||
if (array != null)
|
||||
{
|
||||
for (int i = 0; i < 9; i++)
|
||||
{
|
||||
if (keyUsage[i] && !array[i])
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (policy != null)
|
||||
{
|
||||
Asn1OctetString extensionValue = x509Certificate.GetExtensionValue(X509Extensions.CertificatePolicies);
|
||||
if (extensionValue == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
Asn1Sequence instance = Asn1Sequence.GetInstance(X509ExtensionUtilities.FromExtensionValue(extensionValue));
|
||||
if (policy.Count < 1 && instance.Count < 1)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
bool flag = false;
|
||||
foreach (PolicyInformation item2 in instance)
|
||||
{
|
||||
if (policy.Contains(item2.PolicyIdentifier))
|
||||
{
|
||||
flag = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!flag)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (privateKeyValid != null)
|
||||
{
|
||||
Asn1OctetString extensionValue2 = x509Certificate.GetExtensionValue(X509Extensions.PrivateKeyUsagePeriod);
|
||||
if (extensionValue2 == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
PrivateKeyUsagePeriod instance2 = PrivateKeyUsagePeriod.GetInstance(X509ExtensionUtilities.FromExtensionValue(extensionValue2));
|
||||
DateTime value = privateKeyValid.Value;
|
||||
DateTime dateTime = instance2.NotAfter.ToDateTime();
|
||||
DateTime dateTime2 = instance2.NotBefore.ToDateTime();
|
||||
if (value.CompareTo((object?)dateTime) > 0 || value.CompareTo((object?)dateTime2) < 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (serialNumber != null && !serialNumber.Equals(x509Certificate.SerialNumber))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (subject != null && !subject.Equivalent(x509Certificate.SubjectDN, !ignoreX509NameOrdering))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (!MatchExtension(subjectKeyIdentifier, x509Certificate, X509Extensions.SubjectKeyIdentifier))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (subjectPublicKey != null && !subjectPublicKey.Equals(GetSubjectPublicKey(x509Certificate)))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (subjectPublicKeyAlgID != null && !subjectPublicKeyAlgID.Equals(GetSubjectPublicKey(x509Certificate).AlgorithmID))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
internal static bool IssuersMatch(X509Name a, X509Name b)
|
||||
{
|
||||
return a?.Equivalent(b, inOrder: true) ?? (b == null);
|
||||
}
|
||||
|
||||
private static bool[] CopyBoolArray(bool[] b)
|
||||
{
|
||||
if (b != null)
|
||||
{
|
||||
return (bool[])b.Clone();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static ISet CopySet(ISet s)
|
||||
{
|
||||
if (s != null)
|
||||
{
|
||||
return new HashSet(s);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static SubjectPublicKeyInfo GetSubjectPublicKey(X509Certificate c)
|
||||
{
|
||||
return SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(c.GetPublicKey());
|
||||
}
|
||||
|
||||
private static bool MatchExtension(byte[] b, X509Certificate c, DerObjectIdentifier oid)
|
||||
{
|
||||
if (b == null)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
Asn1OctetString extensionValue = c.GetExtensionValue(oid);
|
||||
if (extensionValue == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return Arrays.AreEqual(b, extensionValue.GetOctets());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
using System.Collections;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.X509.Store;
|
||||
|
||||
internal class X509CollectionStore : IX509Store
|
||||
{
|
||||
private ICollection _local;
|
||||
|
||||
internal X509CollectionStore(ICollection collection)
|
||||
{
|
||||
_local = Platform.CreateArrayList(collection);
|
||||
}
|
||||
|
||||
public ICollection GetMatches(IX509Selector selector)
|
||||
{
|
||||
if (selector == null)
|
||||
{
|
||||
return Platform.CreateArrayList(_local);
|
||||
}
|
||||
IList list = Platform.CreateArrayList();
|
||||
foreach (object item in _local)
|
||||
{
|
||||
if (selector.Match(item))
|
||||
{
|
||||
list.Add(item);
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Text;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.X509.Store;
|
||||
|
||||
public class X509CollectionStoreParameters : IX509StoreParameters
|
||||
{
|
||||
private readonly IList collection;
|
||||
|
||||
public X509CollectionStoreParameters(ICollection collection)
|
||||
{
|
||||
if (collection == null)
|
||||
{
|
||||
throw new ArgumentNullException("collection");
|
||||
}
|
||||
this.collection = Platform.CreateArrayList(collection);
|
||||
}
|
||||
|
||||
public ICollection GetCollection()
|
||||
{
|
||||
return Platform.CreateArrayList(collection);
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
stringBuilder.Append("X509CollectionStoreParameters: [\n");
|
||||
stringBuilder.Append(string.Concat(" collection: ", collection, "\n"));
|
||||
stringBuilder.Append("]");
|
||||
return stringBuilder.ToString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,290 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using Org.BouncyCastle.Asn1;
|
||||
using Org.BouncyCastle.Asn1.X509;
|
||||
using Org.BouncyCastle.Math;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
using Org.BouncyCastle.Utilities.Date;
|
||||
using Org.BouncyCastle.X509.Extension;
|
||||
|
||||
namespace Org.BouncyCastle.X509.Store;
|
||||
|
||||
public class X509CrlStoreSelector : IX509Selector, ICloneable
|
||||
{
|
||||
private X509Certificate certificateChecking;
|
||||
|
||||
private DateTimeObject dateAndTime;
|
||||
|
||||
private ICollection issuers;
|
||||
|
||||
private BigInteger maxCrlNumber;
|
||||
|
||||
private BigInteger minCrlNumber;
|
||||
|
||||
private IX509AttributeCertificate attrCertChecking;
|
||||
|
||||
private bool completeCrlEnabled;
|
||||
|
||||
private bool deltaCrlIndicatorEnabled;
|
||||
|
||||
private byte[] issuingDistributionPoint;
|
||||
|
||||
private bool issuingDistributionPointEnabled;
|
||||
|
||||
private BigInteger maxBaseCrlNumber;
|
||||
|
||||
public X509Certificate CertificateChecking
|
||||
{
|
||||
get
|
||||
{
|
||||
return certificateChecking;
|
||||
}
|
||||
set
|
||||
{
|
||||
certificateChecking = value;
|
||||
}
|
||||
}
|
||||
|
||||
public DateTimeObject DateAndTime
|
||||
{
|
||||
get
|
||||
{
|
||||
return dateAndTime;
|
||||
}
|
||||
set
|
||||
{
|
||||
dateAndTime = value;
|
||||
}
|
||||
}
|
||||
|
||||
public ICollection Issuers
|
||||
{
|
||||
get
|
||||
{
|
||||
return Platform.CreateArrayList(issuers);
|
||||
}
|
||||
set
|
||||
{
|
||||
issuers = Platform.CreateArrayList(value);
|
||||
}
|
||||
}
|
||||
|
||||
public BigInteger MaxCrlNumber
|
||||
{
|
||||
get
|
||||
{
|
||||
return maxCrlNumber;
|
||||
}
|
||||
set
|
||||
{
|
||||
maxCrlNumber = value;
|
||||
}
|
||||
}
|
||||
|
||||
public BigInteger MinCrlNumber
|
||||
{
|
||||
get
|
||||
{
|
||||
return minCrlNumber;
|
||||
}
|
||||
set
|
||||
{
|
||||
minCrlNumber = value;
|
||||
}
|
||||
}
|
||||
|
||||
public IX509AttributeCertificate AttrCertChecking
|
||||
{
|
||||
get
|
||||
{
|
||||
return attrCertChecking;
|
||||
}
|
||||
set
|
||||
{
|
||||
attrCertChecking = value;
|
||||
}
|
||||
}
|
||||
|
||||
public bool CompleteCrlEnabled
|
||||
{
|
||||
get
|
||||
{
|
||||
return completeCrlEnabled;
|
||||
}
|
||||
set
|
||||
{
|
||||
completeCrlEnabled = value;
|
||||
}
|
||||
}
|
||||
|
||||
public bool DeltaCrlIndicatorEnabled
|
||||
{
|
||||
get
|
||||
{
|
||||
return deltaCrlIndicatorEnabled;
|
||||
}
|
||||
set
|
||||
{
|
||||
deltaCrlIndicatorEnabled = value;
|
||||
}
|
||||
}
|
||||
|
||||
public byte[] IssuingDistributionPoint
|
||||
{
|
||||
get
|
||||
{
|
||||
return Arrays.Clone(issuingDistributionPoint);
|
||||
}
|
||||
set
|
||||
{
|
||||
issuingDistributionPoint = Arrays.Clone(value);
|
||||
}
|
||||
}
|
||||
|
||||
public bool IssuingDistributionPointEnabled
|
||||
{
|
||||
get
|
||||
{
|
||||
return issuingDistributionPointEnabled;
|
||||
}
|
||||
set
|
||||
{
|
||||
issuingDistributionPointEnabled = value;
|
||||
}
|
||||
}
|
||||
|
||||
public BigInteger MaxBaseCrlNumber
|
||||
{
|
||||
get
|
||||
{
|
||||
return maxBaseCrlNumber;
|
||||
}
|
||||
set
|
||||
{
|
||||
maxBaseCrlNumber = value;
|
||||
}
|
||||
}
|
||||
|
||||
public X509CrlStoreSelector()
|
||||
{
|
||||
}
|
||||
|
||||
public X509CrlStoreSelector(X509CrlStoreSelector o)
|
||||
{
|
||||
certificateChecking = o.CertificateChecking;
|
||||
dateAndTime = o.DateAndTime;
|
||||
issuers = o.Issuers;
|
||||
maxCrlNumber = o.MaxCrlNumber;
|
||||
minCrlNumber = o.MinCrlNumber;
|
||||
deltaCrlIndicatorEnabled = o.DeltaCrlIndicatorEnabled;
|
||||
completeCrlEnabled = o.CompleteCrlEnabled;
|
||||
maxBaseCrlNumber = o.MaxBaseCrlNumber;
|
||||
attrCertChecking = o.AttrCertChecking;
|
||||
issuingDistributionPointEnabled = o.IssuingDistributionPointEnabled;
|
||||
issuingDistributionPoint = o.IssuingDistributionPoint;
|
||||
}
|
||||
|
||||
public virtual object Clone()
|
||||
{
|
||||
return new X509CrlStoreSelector(this);
|
||||
}
|
||||
|
||||
public virtual bool Match(object obj)
|
||||
{
|
||||
if (!(obj is X509Crl x509Crl))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (dateAndTime != null)
|
||||
{
|
||||
DateTime value = dateAndTime.Value;
|
||||
DateTime thisUpdate = x509Crl.ThisUpdate;
|
||||
DateTimeObject nextUpdate = x509Crl.NextUpdate;
|
||||
if (value.CompareTo((object?)thisUpdate) < 0 || nextUpdate == null || value.CompareTo((object?)nextUpdate.Value) >= 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (issuers != null)
|
||||
{
|
||||
X509Name issuerDN = x509Crl.IssuerDN;
|
||||
bool flag = false;
|
||||
foreach (X509Name issuer in issuers)
|
||||
{
|
||||
if (issuer.Equivalent(issuerDN, inOrder: true))
|
||||
{
|
||||
flag = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!flag)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (maxCrlNumber != null || minCrlNumber != null)
|
||||
{
|
||||
Asn1OctetString extensionValue = x509Crl.GetExtensionValue(X509Extensions.CrlNumber);
|
||||
if (extensionValue == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
BigInteger positiveValue = DerInteger.GetInstance(X509ExtensionUtilities.FromExtensionValue(extensionValue)).PositiveValue;
|
||||
if (maxCrlNumber != null && positiveValue.CompareTo(maxCrlNumber) > 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (minCrlNumber != null && positiveValue.CompareTo(minCrlNumber) < 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
DerInteger derInteger = null;
|
||||
try
|
||||
{
|
||||
Asn1OctetString extensionValue2 = x509Crl.GetExtensionValue(X509Extensions.DeltaCrlIndicator);
|
||||
if (extensionValue2 != null)
|
||||
{
|
||||
derInteger = DerInteger.GetInstance(X509ExtensionUtilities.FromExtensionValue(extensionValue2));
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (derInteger == null)
|
||||
{
|
||||
if (DeltaCrlIndicatorEnabled)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (CompleteCrlEnabled)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (maxBaseCrlNumber != null && derInteger.PositiveValue.CompareTo(maxBaseCrlNumber) > 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (issuingDistributionPointEnabled)
|
||||
{
|
||||
Asn1OctetString extensionValue3 = x509Crl.GetExtensionValue(X509Extensions.IssuingDistributionPoint);
|
||||
if (issuingDistributionPoint == null)
|
||||
{
|
||||
if (extensionValue3 != null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else if (!Arrays.AreEqual(extensionValue3.GetOctets(), issuingDistributionPoint))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
using System;
|
||||
|
||||
namespace Org.BouncyCastle.X509.Store;
|
||||
|
||||
[Serializable]
|
||||
public class X509StoreException : Exception
|
||||
{
|
||||
public X509StoreException()
|
||||
{
|
||||
}
|
||||
|
||||
public X509StoreException(string message)
|
||||
: base(message)
|
||||
{
|
||||
}
|
||||
|
||||
public X509StoreException(string message, Exception e)
|
||||
: base(message, e)
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.X509.Store;
|
||||
|
||||
public sealed class X509StoreFactory
|
||||
{
|
||||
private X509StoreFactory()
|
||||
{
|
||||
}
|
||||
|
||||
public static IX509Store Create(string type, IX509StoreParameters parameters)
|
||||
{
|
||||
if (type == null)
|
||||
{
|
||||
throw new ArgumentNullException("type");
|
||||
}
|
||||
string[] array = Platform.ToUpperInvariant(type).Split(new char[1] { '/' });
|
||||
if (array.Length < 2)
|
||||
{
|
||||
throw new ArgumentException("type");
|
||||
}
|
||||
if (array[1] != "COLLECTION")
|
||||
{
|
||||
throw new NoSuchStoreException("X.509 store type '" + type + "' not available.");
|
||||
}
|
||||
X509CollectionStoreParameters x509CollectionStoreParameters = (X509CollectionStoreParameters)parameters;
|
||||
ICollection collection = x509CollectionStoreParameters.GetCollection();
|
||||
switch (array[0])
|
||||
{
|
||||
case "ATTRIBUTECERTIFICATE":
|
||||
checkCorrectType(collection, typeof(IX509AttributeCertificate));
|
||||
break;
|
||||
case "CERTIFICATE":
|
||||
checkCorrectType(collection, typeof(X509Certificate));
|
||||
break;
|
||||
case "CERTIFICATEPAIR":
|
||||
checkCorrectType(collection, typeof(X509CertificatePair));
|
||||
break;
|
||||
case "CRL":
|
||||
checkCorrectType(collection, typeof(X509Crl));
|
||||
break;
|
||||
default:
|
||||
throw new NoSuchStoreException("X.509 store type '" + type + "' not available.");
|
||||
}
|
||||
return new X509CollectionStore(collection);
|
||||
}
|
||||
|
||||
private static void checkCorrectType(ICollection coll, Type t)
|
||||
{
|
||||
foreach (object item in coll)
|
||||
{
|
||||
if (!t.IsInstanceOfType(item))
|
||||
{
|
||||
throw new InvalidCastException("Can't cast object to type: " + t.FullName);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user