private PkixCertPathBuilderResult Build(IX509AttributeCertificate attrCert, X509Certificate tbvCert, PkixBuilderParameters pkixParams, IList tbvPath)
    {
        if (tbvPath.Contains(tbvCert))
        {
            return(null);
        }
        if (pkixParams.GetExcludedCerts().Contains(tbvCert))
        {
            return(null);
        }
        if (pkixParams.MaxPathLength != -1 && tbvPath.Count - 1 > pkixParams.MaxPathLength)
        {
            return(null);
        }
        tbvPath.Add(tbvCert);
        PkixCertPathBuilderResult pkixCertPathBuilderResult = null;
        PkixAttrCertPathValidator pkixAttrCertPathValidator = new PkixAttrCertPathValidator();

        try
        {
            if (PkixCertPathValidatorUtilities.FindTrustAnchor(tbvCert, pkixParams.GetTrustAnchors()) != null)
            {
                PkixCertPath certPath = new PkixCertPath(tbvPath);
                PkixCertPathValidatorResult pkixCertPathValidatorResult;
                try
                {
                    pkixCertPathValidatorResult = pkixAttrCertPathValidator.Validate(certPath, pkixParams);
                }
                catch (Exception innerException)
                {
                    throw new Exception("Certification path could not be validated.", innerException);
                }
                return(new PkixCertPathBuilderResult(certPath, pkixCertPathValidatorResult.TrustAnchor, pkixCertPathValidatorResult.PolicyTree, pkixCertPathValidatorResult.SubjectPublicKey));
            }
            try
            {
                PkixCertPathValidatorUtilities.AddAdditionalStoresFromAltNames(tbvCert, pkixParams);
            }
            catch (CertificateParsingException innerException2)
            {
                throw new Exception("No additional X.509 stores can be added from certificate locations.", innerException2);
            }
            ISet set = new HashSet();
            try
            {
                set.AddAll(PkixCertPathValidatorUtilities.FindIssuerCerts(tbvCert, pkixParams));
            }
            catch (Exception innerException3)
            {
                throw new Exception("Cannot find issuer certificate for certificate in certification path.", innerException3);
            }
            if (set.IsEmpty)
            {
                throw new Exception("No issuer certificate for certificate in certification path found.");
            }
            foreach (X509Certificate item in set)
            {
                if (!PkixCertPathValidatorUtilities.IsSelfIssued(item))
                {
                    pkixCertPathBuilderResult = Build(attrCert, item, pkixParams, tbvPath);
                    if (pkixCertPathBuilderResult != null)
                    {
                        break;
                    }
                }
            }
        }
        catch (Exception innerException4)
        {
            certPathException = new Exception("No valid certification path could be build.", innerException4);
        }
        if (pkixCertPathBuilderResult == null)
        {
            tbvPath.Remove(tbvCert);
        }
        return(pkixCertPathBuilderResult);
    }