All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.spongycastle.cert.path.CertPath Maven / Gradle / Ivy

Go to download

Spongy Castle is a package-rename (org.bouncycastle.* to org.spongycastle.*) of Bouncy Castle intended for the Android platform. Android unfortunately ships with a stripped-down version of Bouncy Castle, which prevents easy upgrades - Spongy Castle overcomes this and provides a full, up-to-date version of the Bouncy Castle cryptographic libs.

There is a newer version: 1.54.0.0
Show newest version
package org.spongycastle.cert.path;

import org.spongycastle.cert.X509CertificateHolder;

public class CertPath
{
    private final X509CertificateHolder[] certificates;

    public CertPath(X509CertificateHolder[] certificates)
    {
        this.certificates = copyArray(certificates);
    }

    public X509CertificateHolder[] getCertificates()
    {
        return copyArray(certificates);
    }

    public CertPathValidationResult validate(CertPathValidation[] ruleSet)
    {
        CertPathValidationContext context = new CertPathValidationContext(CertPathUtils.getCriticalExtensionsOIDs(certificates));

        for (int i = 0; i != ruleSet.length; i++)
        {
            for (int j = certificates.length - 1; j >= 0; j--)
            {
                try
                {
                    context.setIsEndEntity(j == 0);
                    ruleSet[i].validate(context, certificates[j]);
                }
                catch (CertPathValidationException e)
                {   // TODO: introduce object to hold (i and e)
                    return new CertPathValidationResult(context, j, i, e);
                }
            }
        }

        return new CertPathValidationResult(context);
    }

    public CertPathValidationResult evaluate(CertPathValidation[] ruleSet)
    {
        CertPathValidationContext context = new CertPathValidationContext(CertPathUtils.getCriticalExtensionsOIDs(certificates));

        CertPathValidationResultBuilder builder = new CertPathValidationResultBuilder();

        for (int i = 0; i != ruleSet.length; i++)
        {
            for (int j = certificates.length - 1; j >= 0; j--)
            {
                try
                {
                    context.setIsEndEntity(j == 0);
                    ruleSet[i].validate(context, certificates[j]);
                }
                catch (CertPathValidationException e)
                {
                   builder.addException(e);
                }
            }
        }

        return builder.build();
    }

    private X509CertificateHolder[] copyArray(X509CertificateHolder[] array)
    {
        X509CertificateHolder[] rv = new X509CertificateHolder[array.length];

        System.arraycopy(array, 0, rv, 0, rv.length);

        return rv;
    }

    public int length()
    {
        return certificates.length;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy