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);
    }