internal static ICollection FindIssuerCerts(X509Certificate cert, PkixBuilderParameters pkixParams)
    {
        X509CertStoreSelector x509CertStoreSelector = new X509CertStoreSelector();
        ISet set = new HashSet();

        try
        {
            x509CertStoreSelector.Subject = cert.IssuerDN;
        }
        catch (IOException innerException)
        {
            throw new Exception("Subject criteria for certificate selector to find issuer certificate could not be set.", innerException);
        }
        try
        {
            set.AddAll(FindCertificates(x509CertStoreSelector, pkixParams.GetStores()));
            set.AddAll(FindCertificates(x509CertStoreSelector, pkixParams.GetAdditionalStores()));
            return(set);
        }
        catch (Exception innerException2)
        {
            throw new Exception("Issuer certificate cannot be searched.", innerException2);
        }
    }
    public virtual PkixCertPathBuilderResult Build(PkixBuilderParameters pkixParams)
    {
        IX509Selector targetConstraints = pkixParams.GetTargetConstraints();

        if (!(targetConstraints is X509AttrCertStoreSelector))
        {
            throw new PkixCertPathBuilderException("TargetConstraints must be an instance of " + typeof(X509AttrCertStoreSelector).FullName + " for " + typeof(PkixAttrCertPathBuilder).FullName + " class.");
        }
        ICollection collection;

        try
        {
            collection = PkixCertPathValidatorUtilities.FindCertificates((X509AttrCertStoreSelector)targetConstraints, pkixParams.GetStores());
        }
        catch (Exception exception)
        {
            throw new PkixCertPathBuilderException("Error finding target attribute certificate.", exception);
        }
        if (collection.Count == 0)
        {
            throw new PkixCertPathBuilderException("No attribute certificate found matching targetContraints.");
        }
        PkixCertPathBuilderResult pkixCertPathBuilderResult = null;

        foreach (IX509AttributeCertificate item in collection)
        {
            X509CertStoreSelector x509CertStoreSelector = new X509CertStoreSelector();
            X509Name[]            principals            = item.Issuer.GetPrincipals();
            ISet set = new HashSet();
            for (int i = 0; i < principals.Length; i++)
            {
                try
                {
                    x509CertStoreSelector.Subject = principals[i];
                    set.AddAll(PkixCertPathValidatorUtilities.FindCertificates(x509CertStoreSelector, pkixParams.GetStores()));
                }
                catch (Exception exception2)
                {
                    throw new PkixCertPathBuilderException("Public key certificate for attribute certificate cannot be searched.", exception2);
                }
            }
            if (set.IsEmpty)
            {
                throw new PkixCertPathBuilderException("Public key certificate for attribute certificate cannot be found.");
            }
            IList tbvPath = Platform.CreateArrayList();
            foreach (X509Certificate item2 in set)
            {
                pkixCertPathBuilderResult = Build(item, item2, pkixParams, tbvPath);
                if (pkixCertPathBuilderResult != null)
                {
                    break;
                }
            }
            if (pkixCertPathBuilderResult != null)
            {
                break;
            }
        }
        if (pkixCertPathBuilderResult == null && certPathException != null)
        {
            throw new PkixCertPathBuilderException("Possible certificate chain could not be validated.", certPathException);
        }
        if (pkixCertPathBuilderResult == null && certPathException == null)
        {
            throw new PkixCertPathBuilderException("Unable to find certificate chain.");
        }
        return(pkixCertPathBuilderResult);
    }
Ejemplo n.º 3
0
    public virtual PkixCertPathBuilderResult Build(PkixBuilderParameters pkixParams)
    {
        IX509Selector targetCertConstraints = pkixParams.GetTargetCertConstraints();

        if (!(targetCertConstraints is X509CertStoreSelector))
        {
            throw new PkixCertPathBuilderException("TargetConstraints must be an instance of " + typeof(X509CertStoreSelector).FullName + " for " + Platform.GetTypeName(this) + " class.");
        }
        ISet set = new HashSet();

        try
        {
            set.AddAll(PkixCertPathValidatorUtilities.FindCertificates((X509CertStoreSelector)targetCertConstraints, pkixParams.GetStores()));
        }
        catch (Exception exception)
        {
            throw new PkixCertPathBuilderException("Error finding target certificate.", exception);
        }
        if (set.IsEmpty)
        {
            throw new PkixCertPathBuilderException("No certificate found matching targetContraints.");
        }
        PkixCertPathBuilderResult pkixCertPathBuilderResult = null;
        IList tbvPath = Platform.CreateArrayList();

        foreach (X509Certificate item in set)
        {
            pkixCertPathBuilderResult = Build(item, pkixParams, tbvPath);
            if (pkixCertPathBuilderResult != null)
            {
                break;
            }
        }
        if (pkixCertPathBuilderResult == null && certPathException != null)
        {
            throw new PkixCertPathBuilderException(certPathException.Message, certPathException.InnerException);
        }
        if (pkixCertPathBuilderResult == null && certPathException == null)
        {
            throw new PkixCertPathBuilderException("Unable to find certificate chain.");
        }
        return(pkixCertPathBuilderResult);
    }