Search in sources :

Example 21 with PKIXBuilderParameters

use of java.security.cert.PKIXBuilderParameters in project robovm by robovm.

the class PKIXCertPathBuilderSpi method engineBuild.

/**
     * Build and validate a CertPath using the given parameter.
     * 
     * @param params PKIXBuilderParameters object containing all information to
     *            build the CertPath
     */
public CertPathBuilderResult engineBuild(CertPathParameters params) throws CertPathBuilderException, InvalidAlgorithmParameterException {
    if (!(params instanceof PKIXBuilderParameters) && !(params instanceof ExtendedPKIXBuilderParameters)) {
        throw new InvalidAlgorithmParameterException("Parameters must be an instance of " + PKIXBuilderParameters.class.getName() + " or " + ExtendedPKIXBuilderParameters.class.getName() + ".");
    }
    ExtendedPKIXBuilderParameters pkixParams = null;
    if (params instanceof ExtendedPKIXBuilderParameters) {
        pkixParams = (ExtendedPKIXBuilderParameters) params;
    } else {
        pkixParams = (ExtendedPKIXBuilderParameters) ExtendedPKIXBuilderParameters.getInstance((PKIXBuilderParameters) params);
    }
    Collection targets;
    Iterator targetIter;
    List certPathList = new ArrayList();
    X509Certificate cert;
    // search target certificates
    Selector certSelect = pkixParams.getTargetConstraints();
    if (!(certSelect instanceof X509CertStoreSelector)) {
        throw new CertPathBuilderException("TargetConstraints must be an instance of " + X509CertStoreSelector.class.getName() + " for " + this.getClass().getName() + " class.");
    }
    try {
        targets = CertPathValidatorUtilities.findCertificates((X509CertStoreSelector) certSelect, pkixParams.getStores());
        targets.addAll(CertPathValidatorUtilities.findCertificates((X509CertStoreSelector) certSelect, pkixParams.getCertStores()));
    } catch (AnnotatedException e) {
        throw new ExtCertPathBuilderException("Error finding target certificate.", e);
    }
    if (targets.isEmpty()) {
        throw new CertPathBuilderException("No certificate found matching targetContraints.");
    }
    CertPathBuilderResult result = null;
    // check all potential target certificates
    targetIter = targets.iterator();
    while (targetIter.hasNext() && result == null) {
        cert = (X509Certificate) targetIter.next();
        result = build(cert, pkixParams, certPathList);
    }
    if (result == null && certPathException != null) {
        if (certPathException instanceof AnnotatedException) {
            throw new CertPathBuilderException(certPathException.getMessage(), certPathException.getCause());
        }
        throw new CertPathBuilderException("Possible certificate chain could not be validated.", certPathException);
    }
    if (result == null && certPathException == null) {
        throw new CertPathBuilderException("Unable to find certificate chain.");
    }
    return result;
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) ExtendedPKIXBuilderParameters(org.bouncycastle.x509.ExtendedPKIXBuilderParameters) ExtendedPKIXBuilderParameters(org.bouncycastle.x509.ExtendedPKIXBuilderParameters) PKIXBuilderParameters(java.security.cert.PKIXBuilderParameters) X509CertStoreSelector(org.bouncycastle.x509.X509CertStoreSelector) CertPathBuilderResult(java.security.cert.CertPathBuilderResult) PKIXCertPathBuilderResult(java.security.cert.PKIXCertPathBuilderResult) ArrayList(java.util.ArrayList) X509Certificate(java.security.cert.X509Certificate) ExtCertPathBuilderException(org.bouncycastle.jce.exception.ExtCertPathBuilderException) CertPathBuilderException(java.security.cert.CertPathBuilderException) Iterator(java.util.Iterator) ExtCertPathBuilderException(org.bouncycastle.jce.exception.ExtCertPathBuilderException) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List) Selector(org.bouncycastle.util.Selector) X509CertStoreSelector(org.bouncycastle.x509.X509CertStoreSelector)

Example 22 with PKIXBuilderParameters

use of java.security.cert.PKIXBuilderParameters in project robovm by robovm.

the class ExtendedPKIXBuilderParameters method setParams.

/**
     * Can alse handle <code>ExtendedPKIXBuilderParameters</code> and
     * <code>PKIXBuilderParameters</code>.
     * 
     * @param params Parameters to set.
     * @see org.bouncycastle.x509.ExtendedPKIXParameters#setParams(java.security.cert.PKIXParameters)
     */
protected void setParams(PKIXParameters params) {
    super.setParams(params);
    if (params instanceof ExtendedPKIXBuilderParameters) {
        ExtendedPKIXBuilderParameters _params = (ExtendedPKIXBuilderParameters) params;
        maxPathLength = _params.maxPathLength;
        excludedCerts = new HashSet(_params.excludedCerts);
    }
    if (params instanceof PKIXBuilderParameters) {
        PKIXBuilderParameters _params = (PKIXBuilderParameters) params;
        maxPathLength = _params.getMaxPathLength();
    }
}
Also used : PKIXBuilderParameters(java.security.cert.PKIXBuilderParameters) HashSet(java.util.HashSet)

Example 23 with PKIXBuilderParameters

use of java.security.cert.PKIXBuilderParameters in project robovm by robovm.

the class myTrustManagerFactory method test_initLjavax_net_ssl_ManagerFactoryParameters.

/**
     * Test for <code>init(ManagerFactoryParameters params)</code>
     * Assertion:
     * throws InvalidAlgorithmParameterException when params is null
     */
@KnownFailure("ManagerFactoryParameters object is not supported " + "and InvalidAlgorithmParameterException was thrown.")
public void test_initLjavax_net_ssl_ManagerFactoryParameters() throws Exception {
    ManagerFactoryParameters par = null;
    TrustManagerFactory[] trustMF = createTMFac();
    assertNotNull("TrustManagerFactory objects were not created", trustMF);
    for (int i = 0; i < trustMF.length; i++) {
        try {
            trustMF[i].init(par);
            fail("InvalidAlgorithmParameterException must be thrown");
        } catch (InvalidAlgorithmParameterException e) {
        }
    }
    String keyAlg = "DSA";
    String validCaNameRfc2253 = ("CN=Test CA," + "OU=Testing Division," + "O=Test It All," + "L=Test Town," + "ST=Testifornia," + "C=Testland");
    try {
        KeyStore kStore = KeyStore.getInstance(KeyStore.getDefaultType());
        kStore.load(null, null);
        PublicKey pk = new TestKeyPair(keyAlg).getPublic();
        TrustAnchor ta = new TrustAnchor(validCaNameRfc2253, pk, getFullEncoding());
        Set<TrustAnchor> trustAnchors = new HashSet<TrustAnchor>();
        trustAnchors.add(ta);
        X509CertSelector xcs = new X509CertSelector();
        PKIXBuilderParameters pkixBP = new PKIXBuilderParameters(trustAnchors, xcs);
        CertPathTrustManagerParameters cptmp = new CertPathTrustManagerParameters(pkixBP);
        TrustManagerFactory tmf = TrustManagerFactory.getInstance(getDefaultAlgorithm());
        try {
            tmf.init(cptmp);
        } catch (Exception ex) {
            fail(ex + " was thrown for init(ManagerFactoryParameters spec)");
        }
    } catch (Exception e) {
        fail("Unexpected exception for configuration: " + e);
    }
}
Also used : TestKeyPair(org.apache.harmony.security.tests.support.TestKeyPair) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) PublicKey(java.security.PublicKey) PKIXBuilderParameters(java.security.cert.PKIXBuilderParameters) CertPathTrustManagerParameters(javax.net.ssl.CertPathTrustManagerParameters) TrustAnchor(java.security.cert.TrustAnchor) X509CertSelector(java.security.cert.X509CertSelector) KeyStore(java.security.KeyStore) KeyStoreException(java.security.KeyStoreException) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) NoSuchProviderException(java.security.NoSuchProviderException) TrustManagerFactory(javax.net.ssl.TrustManagerFactory) ManagerFactoryParameters(javax.net.ssl.ManagerFactoryParameters) HashSet(java.util.HashSet) KnownFailure(dalvik.annotation.KnownFailure)

Example 24 with PKIXBuilderParameters

use of java.security.cert.PKIXBuilderParameters in project robovm by robovm.

the class X509CertSelectorTest method buildCertPath.

private CertPath buildCertPath() throws InvalidAlgorithmParameterException {
    PKIXCertPathBuilderResult result = null;
    PKIXBuilderParameters buildParams = new PKIXBuilderParameters(Collections.singleton(new TrustAnchor(rootCertificate, null)), theCertSelector);
    try {
        result = (PKIXCertPathBuilderResult) builder.build(buildParams);
    } catch (CertPathBuilderException e) {
        return null;
    }
    return result.getCertPath();
}
Also used : PKIXBuilderParameters(java.security.cert.PKIXBuilderParameters) CertPathBuilderException(java.security.cert.CertPathBuilderException) PKIXCertPathBuilderResult(java.security.cert.PKIXCertPathBuilderResult) TrustAnchor(java.security.cert.TrustAnchor)

Example 25 with PKIXBuilderParameters

use of java.security.cert.PKIXBuilderParameters in project robovm by robovm.

the class PKIXBuilderParametersTest method testPKIXBuilderParametersKeyStoreCertSelector01.

/**
     * Test #1 for <code>PKIXBuilderParameters(KeyStore, CertSelector)</code>
     * constructor<br>
     * Assertion: <code>NullPointerException</code> - if the
     * <code>keystore</code> is <code>null</code>
     */
public final void testPKIXBuilderParametersKeyStoreCertSelector01() throws Exception {
    try {
        new PKIXBuilderParameters((KeyStore) null, new X509CertSelector());
        fail("NullPointerException expected");
    } catch (NullPointerException e) {
    // expected
    }
}
Also used : PKIXBuilderParameters(java.security.cert.PKIXBuilderParameters) X509CertSelector(java.security.cert.X509CertSelector)

Aggregations

PKIXBuilderParameters (java.security.cert.PKIXBuilderParameters)34 X509CertSelector (java.security.cert.X509CertSelector)24 X509Certificate (java.security.cert.X509Certificate)18 CollectionCertStoreParameters (java.security.cert.CollectionCertStoreParameters)13 TrustAnchor (java.security.cert.TrustAnchor)13 ArrayList (java.util.ArrayList)10 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)9 KeyStore (java.security.KeyStore)9 CertPathBuilder (java.security.cert.CertPathBuilder)9 HashSet (java.util.HashSet)9 CertStore (java.security.cert.CertStore)8 CertPathBuilderException (java.security.cert.CertPathBuilderException)7 CertPathBuilderResult (java.security.cert.CertPathBuilderResult)7 PKIXCertPathBuilderResult (java.security.cert.PKIXCertPathBuilderResult)7 ByteArrayInputStream (java.io.ByteArrayInputStream)6 CertificateFactory (java.security.cert.CertificateFactory)6 IOException (java.io.IOException)5 KeyStoreException (java.security.KeyStoreException)5 Certificate (java.security.cert.Certificate)5 CertificateException (java.security.cert.CertificateException)5