Ejemplo n.º 1
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);
    }
    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);
    }
    internal static PkixCertPath ProcessAttrCert1(IX509AttributeCertificate attrCert, PkixParameters pkixParams)
    {
        PkixCertPathBuilderResult pkixCertPathBuilderResult = null;
        ISet set = new HashSet();

        if (attrCert.Holder.GetIssuer() != null)
        {
            X509CertStoreSelector x509CertStoreSelector = new X509CertStoreSelector();
            x509CertStoreSelector.SerialNumber = attrCert.Holder.SerialNumber;
            X509Name[] issuer = attrCert.Holder.GetIssuer();
            for (int i = 0; i < issuer.Length; i++)
            {
                try
                {
                    x509CertStoreSelector.Issuer = issuer[i];
                    set.AddAll(PkixCertPathValidatorUtilities.FindCertificates(x509CertStoreSelector, pkixParams.GetStores()));
                }
                catch (Exception cause)
                {
                    throw new PkixCertPathValidatorException("Public key certificate for attribute certificate cannot be searched.", cause);
                }
            }
            if (set.IsEmpty)
            {
                throw new PkixCertPathValidatorException("Public key certificate specified in base certificate ID for attribute certificate cannot be found.");
            }
        }
        if (attrCert.Holder.GetEntityNames() != null)
        {
            X509CertStoreSelector x509CertStoreSelector2 = new X509CertStoreSelector();
            X509Name[]            entityNames            = attrCert.Holder.GetEntityNames();
            for (int j = 0; j < entityNames.Length; j++)
            {
                try
                {
                    x509CertStoreSelector2.Issuer = entityNames[j];
                    set.AddAll(PkixCertPathValidatorUtilities.FindCertificates(x509CertStoreSelector2, pkixParams.GetStores()));
                }
                catch (Exception cause2)
                {
                    throw new PkixCertPathValidatorException("Public key certificate for attribute certificate cannot be searched.", cause2);
                }
            }
            if (set.IsEmpty)
            {
                throw new PkixCertPathValidatorException("Public key certificate specified in entity name for attribute certificate cannot be found.");
            }
        }
        PkixBuilderParameters          instance = PkixBuilderParameters.GetInstance(pkixParams);
        PkixCertPathValidatorException ex       = null;

        foreach (X509Certificate item in set)
        {
            X509CertStoreSelector x509CertStoreSelector3 = new X509CertStoreSelector();
            x509CertStoreSelector3.Certificate = item;
            instance.SetTargetConstraints(x509CertStoreSelector3);
            PkixCertPathBuilder pkixCertPathBuilder = new PkixCertPathBuilder();
            try
            {
                pkixCertPathBuilderResult = pkixCertPathBuilder.Build(PkixBuilderParameters.GetInstance(instance));
            }
            catch (PkixCertPathBuilderException cause3)
            {
                ex = new PkixCertPathValidatorException("Certification path for public key certificate of attribute certificate could not be build.", cause3);
            }
        }
        if (ex != null)
        {
            throw ex;
        }
        return(pkixCertPathBuilderResult.CertPath);
    }