public virtual bool Match(
            object obj)
        {
            X509Certificate c = obj as X509Certificate;

            if (c == null)
            {
                return(false);
            }

            if (!MatchExtension(authorityKeyIdentifier, c, X509Extensions.AuthorityKeyIdentifier))
            {
                return(false);
            }

            if (basicConstraints != -1)
            {
                int bc = c.GetBasicConstraints();

                if (basicConstraints == -2)
                {
                    if (bc != -1)
                    {
                        return(false);
                    }
                }
                else
                {
                    if (bc < basicConstraints)
                    {
                        return(false);
                    }
                }
            }

            if (certificate != null && !certificate.Equals(c))
            {
                return(false);
            }

            if (certificateValid != null && !c.IsValid(certificateValid.Value))
            {
                return(false);
            }

            if (extendedKeyUsage != null)
            {
                IList eku = c.GetExtendedKeyUsage();

                // Note: if no extended key usage set, all key purposes are implicitly allowed

                if (eku != null)
                {
                    foreach (DerObjectIdentifier oid in extendedKeyUsage)
                    {
                        if (!eku.Contains(oid.Id))
                        {
                            return(false);
                        }
                    }
                }
            }

            if (issuer != null && !issuer.Equivalent(c.IssuerDN, !ignoreX509NameOrdering))
            {
                return(false);
            }

            if (keyUsage != null)
            {
                bool[] ku = c.GetKeyUsage();

                // Note: if no key usage set, all key purposes are implicitly allowed

                if (ku != null)
                {
                    for (int i = 0; i < 9; ++i)
                    {
                        if (keyUsage[i] && !ku[i])
                        {
                            return(false);
                        }
                    }
                }
            }

            if (policy != null)
            {
                Asn1OctetString extVal = c.GetExtensionValue(X509Extensions.CertificatePolicies);
                if (extVal == null)
                {
                    return(false);
                }

                Asn1Sequence certPolicies = Asn1Sequence.GetInstance(
                    X509ExtensionUtilities.FromExtensionValue(extVal));

                if (policy.Count < 1 && certPolicies.Count < 1)
                {
                    return(false);
                }

                bool found = false;
                foreach (PolicyInformation pi in certPolicies)
                {
                    if (policy.Contains(pi.PolicyIdentifier))
                    {
                        found = true;
                        break;
                    }
                }

                if (!found)
                {
                    return(false);
                }
            }

            if (privateKeyValid != null)
            {
                Asn1OctetString extVal = c.GetExtensionValue(X509Extensions.PrivateKeyUsagePeriod);
                if (extVal == null)
                {
                    return(false);
                }

                PrivateKeyUsagePeriod pkup = PrivateKeyUsagePeriod.GetInstance(
                    X509ExtensionUtilities.FromExtensionValue(extVal));

                DateTime dt        = privateKeyValid.Value;
                DateTime notAfter  = pkup.NotAfter.ToDateTime();
                DateTime notBefore = pkup.NotBefore.ToDateTime();

                if (dt.CompareTo(notAfter) > 0 || dt.CompareTo(notBefore) < 0)
                {
                    return(false);
                }
            }

            if (serialNumber != null && !serialNumber.Equals(c.SerialNumber))
            {
                return(false);
            }

            if (subject != null && !subject.Equivalent(c.SubjectDN, !ignoreX509NameOrdering))
            {
                return(false);
            }

            if (!MatchExtension(subjectKeyIdentifier, c, X509Extensions.SubjectKeyIdentifier))
            {
                return(false);
            }

            if (subjectPublicKey != null && !subjectPublicKey.Equals(GetSubjectPublicKey(c)))
            {
                return(false);
            }

            if (subjectPublicKeyAlgID != null &&
                !subjectPublicKeyAlgID.Equals(GetSubjectPublicKey(c).AlgorithmID))
            {
                return(false);
            }

            return(true);
        }
        public virtual bool Match(object obj)
        {
            X509Certificate x509Certificate = obj as X509Certificate;

            if (x509Certificate == null)
            {
                return(false);
            }
            if (!X509CertStoreSelector.MatchExtension(this.authorityKeyIdentifier, x509Certificate, X509Extensions.AuthorityKeyIdentifier))
            {
                return(false);
            }
            if (this.basicConstraints != -1)
            {
                int num = x509Certificate.GetBasicConstraints();
                if (this.basicConstraints == -2)
                {
                    if (num != -1)
                    {
                        return(false);
                    }
                }
                else if (num < this.basicConstraints)
                {
                    return(false);
                }
            }
            if (this.certificate != null && !this.certificate.Equals(x509Certificate))
            {
                return(false);
            }
            if (this.certificateValid != null && !x509Certificate.IsValid(this.certificateValid.Value))
            {
                return(false);
            }
            if (this.extendedKeyUsage != null)
            {
                IList list = x509Certificate.GetExtendedKeyUsage();
                if (list != null)
                {
                    foreach (DerObjectIdentifier derObjectIdentifier in this.extendedKeyUsage)
                    {
                        if (!list.Contains(derObjectIdentifier.Id))
                        {
                            return(false);
                        }
                    }
                }
            }
            if (this.issuer != null && !this.issuer.Equivalent(x509Certificate.IssuerDN, true))
            {
                return(false);
            }
            if (this.keyUsage != null)
            {
                bool[] array = x509Certificate.GetKeyUsage();
                if (array != null)
                {
                    for (int i = 0; i < 9; i++)
                    {
                        if (this.keyUsage[i] && !array[i])
                        {
                            return(false);
                        }
                    }
                }
            }
            if (this.policy != null)
            {
                Asn1OctetString extensionValue = x509Certificate.GetExtensionValue(X509Extensions.CertificatePolicies);
                if (extensionValue == null)
                {
                    return(false);
                }
                Asn1Sequence instance = Asn1Sequence.GetInstance(X509ExtensionUtilities.FromExtensionValue(extensionValue));
                if (this.policy.Count < 1 && instance.Count < 1)
                {
                    return(false);
                }
                bool flag = false;
                foreach (PolicyInformation policyInformation in instance)
                {
                    if (this.policy.Contains(policyInformation.PolicyIdentifier))
                    {
                        flag = true;
                        break;
                    }
                }
                if (!flag)
                {
                    return(false);
                }
            }
            if (this.privateKeyValid != null)
            {
                Asn1OctetString extensionValue2 = x509Certificate.GetExtensionValue(X509Extensions.PrivateKeyUsagePeriod);
                if (extensionValue2 == null)
                {
                    return(false);
                }
                PrivateKeyUsagePeriod instance2 = PrivateKeyUsagePeriod.GetInstance(X509ExtensionUtilities.FromExtensionValue(extensionValue2));
                DateTime value  = this.privateKeyValid.Value;
                DateTime value2 = instance2.NotAfter.ToDateTime();
                DateTime value3 = instance2.NotBefore.ToDateTime();
                if (value.CompareTo(value2) > 0 || value.CompareTo(value3) < 0)
                {
                    return(false);
                }
            }
            return((this.serialNumber == null || this.serialNumber.Equals(x509Certificate.SerialNumber)) && (this.subject == null || this.subject.Equivalent(x509Certificate.SubjectDN, true)) && X509CertStoreSelector.MatchExtension(this.subjectKeyIdentifier, x509Certificate, X509Extensions.SubjectKeyIdentifier) && (this.subjectPublicKey == null || this.subjectPublicKey.Equals(X509CertStoreSelector.GetSubjectPublicKey(x509Certificate))) && (this.subjectPublicKeyAlgID == null || this.subjectPublicKeyAlgID.Equals(X509CertStoreSelector.GetSubjectPublicKey(x509Certificate).AlgorithmID)));
        }
示例#3
0
    public virtual bool Match(object obj)
    {
        X509Certificate x509Certificate = obj as X509Certificate;

        if (x509Certificate == null)
        {
            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, inOrder: true))
        {
            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, inOrder: true))
        {
            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);
    }
示例#4
0
        public virtual bool Match(object obj)
        {
            X509Certificate x509Certificate = obj as X509Certificate;

            if (x509Certificate == null)
            {
                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)
            {
                global::System.Collections.IList list = x509Certificate.GetExtendedKeyUsage();
                if (list != null)
                {
                    {
                        global::System.Collections.IEnumerator enumerator = ((global::System.Collections.IEnumerable)extendedKeyUsage).GetEnumerator();
                        try
                        {
                            while (enumerator.MoveNext())
                            {
                                DerObjectIdentifier derObjectIdentifier = (DerObjectIdentifier)enumerator.get_Current();
                                if (!list.Contains((object)derObjectIdentifier.Id))
                                {
                                    return(false);
                                }
                            }
                        }
                        finally
                        {
                            global::System.IDisposable disposable = enumerator as global::System.IDisposable;
                            if (disposable != null)
                            {
                                disposable.Dispose();
                            }
                        }
                    }
                }
            }
            if (issuer != null && !issuer.Equivalent(x509Certificate.IssuerDN, inOrder: true))
            {
                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 (((global::System.Collections.ICollection)policy).get_Count() < 1 && instance.Count < 1)
                {
                    return(false);
                }
                bool flag = false;
                {
                    global::System.Collections.IEnumerator enumerator = instance.GetEnumerator();
                    try
                    {
                        while (enumerator.MoveNext())
                        {
                            PolicyInformation policyInformation = (PolicyInformation)enumerator.get_Current();
                            if (policy.Contains(policyInformation.PolicyIdentifier))
                            {
                                flag = true;
                                break;
                            }
                        }
                    }
                    finally
                    {
                        global::System.IDisposable disposable2 = enumerator as global::System.IDisposable;
                        if (disposable2 != null)
                        {
                            disposable2.Dispose();
                        }
                    }
                }
                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));
                global::System.DateTime value     = privateKeyValid.Value;
                global::System.DateTime dateTime  = instance2.NotAfter.ToDateTime();
                global::System.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, inOrder: true))
            {
                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);
        }