Search in sources :

Example 61 with CertificateException

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

the class SSLSocketTest method test_SSLSocket_TrustManagerRuntimeException.

public void test_SSLSocket_TrustManagerRuntimeException() throws Exception {
    TestSSLContext c = TestSSLContext.create();
    SSLContext clientContext = SSLContext.getInstance("TLS");
    X509TrustManager trustManager = new X509TrustManager() {

        @Override
        public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
            throw new AssertionError();
        }

        @Override
        public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
            // throw a RuntimeException from custom TrustManager
            throw new RuntimeException();
        }

        @Override
        public X509Certificate[] getAcceptedIssuers() {
            throw new AssertionError();
        }
    };
    clientContext.init(null, new TrustManager[] { trustManager }, null);
    SSLSocket client = (SSLSocket) clientContext.getSocketFactory().createSocket(c.host, c.port);
    final SSLSocket server = (SSLSocket) c.serverSocket.accept();
    ExecutorService executor = Executors.newSingleThreadExecutor();
    Future<Void> future = executor.submit(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            server.startHandshake();
            return null;
        }
    });
    executor.shutdown();
    try {
        client.startHandshake();
        fail();
    } catch (SSLHandshakeException expected) {
    // before we would get a RuntimeException from checkServerTrusted.
    }
    future.get();
    client.close();
    server.close();
    c.close();
}
Also used : SSLSocket(javax.net.ssl.SSLSocket) SSLContext(javax.net.ssl.SSLContext) X509Certificate(java.security.cert.X509Certificate) SocketException(java.net.SocketException) SocketTimeoutException(java.net.SocketTimeoutException) SSLProtocolException(javax.net.ssl.SSLProtocolException) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) SSLException(javax.net.ssl.SSLException) SSLPeerUnverifiedException(javax.net.ssl.SSLPeerUnverifiedException) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) X509TrustManager(javax.net.ssl.X509TrustManager) ExecutorService(java.util.concurrent.ExecutorService)

Example 62 with CertificateException

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

the class TrustManagerFactoryTest method test_X509TrustManager.

private void test_X509TrustManager(X509TrustManager tm) throws Exception {
    for (String keyType : KEY_TYPES) {
        X509Certificate[] issuers = tm.getAcceptedIssuers();
        assertNotNull(issuers);
        assertTrue(issuers.length > 1);
        assertNotSame(issuers, tm.getAcceptedIssuers());
        boolean defaultTrustManager = // RI de-duplicates certs from TrustedCertificateEntry and PrivateKeyEntry
        issuers.length > (StandardNames.IS_RI ? 1 : 2) * KEY_TYPES.length;
        String keyAlgName = TestKeyStore.keyAlgorithm(keyType);
        String sigAlgName = TestKeyStore.signatureAlgorithm(keyType);
        PrivateKeyEntry pke = getTestKeyStore().getPrivateKey(keyAlgName, sigAlgName);
        X509Certificate[] chain = (X509Certificate[]) pke.getCertificateChain();
        if (defaultTrustManager) {
            try {
                tm.checkClientTrusted(chain, keyType);
                fail();
            } catch (CertificateException expected) {
            }
            try {
                tm.checkServerTrusted(chain, keyType);
                fail();
            } catch (CertificateException expected) {
            }
        } else {
            tm.checkClientTrusted(chain, keyType);
            tm.checkServerTrusted(chain, keyType);
        }
    }
}
Also used : CertificateException(java.security.cert.CertificateException) PrivateKeyEntry(java.security.KeyStore.PrivateKeyEntry) X509Certificate(java.security.cert.X509Certificate)

Example 63 with CertificateException

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

the class KeyStore4Test method testLoadLoadStoreParameter.

public void testLoadLoadStoreParameter() {
    try {
        keyStore.load(null);
        fail("expected NoSuchAlgorithmException");
    } catch (NoSuchAlgorithmException e) {
    // ok
    } catch (CertificateException e) {
        fail("unexpected exception: " + e);
    } catch (IOException e) {
        fail("unexpected exception: " + e);
    }
    try {
        keyStore.load(new KeyStore.LoadStoreParameter() {

            public ProtectionParameter getProtectionParameter() {
                return new KeyStore.PasswordProtection("PASSWORD".toCharArray());
            }
        });
    } catch (NoSuchAlgorithmException e) {
        fail("unexpected exception: " + e);
    } catch (CertificateException e) {
        fail("unexpected exception: " + e);
    } catch (IOException e) {
        fail("unexpected exception: " + e);
    }
    try {
        keyStore.load(new KeyStore.LoadStoreParameter() {

            public ProtectionParameter getProtectionParameter() {
                return null;
            }
        });
        fail("expected NoSuchAlgorithmException");
    } catch (NoSuchAlgorithmException e) {
    // ok
    } catch (CertificateException e) {
        fail("unexpected exception: " + e);
    } catch (IOException e) {
        fail("unexpected exception: " + e);
    }
    try {
        keyStore.load(new KeyStore.LoadStoreParameter() {

            public ProtectionParameter getProtectionParameter() {
                return new KeyStore.ProtectionParameter() {
                };
            }
        });
        fail("expected CertificateException");
    } catch (NoSuchAlgorithmException e) {
        fail("unexpected exception: " + e);
    } catch (CertificateException e) {
    // ok
    } catch (IOException e) {
        fail("unexpected exception: " + e);
    }
}
Also used : CertificateException(java.security.cert.CertificateException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) KeyStore(java.security.KeyStore) ProtectionParameter(java.security.KeyStore.ProtectionParameter) ProtectionParameter(java.security.KeyStore.ProtectionParameter)

Example 64 with CertificateException

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

the class myHostnameVerifier method getServerCertificates.

/*
     * @see javax.net.ssl.HttpsURLConnection#getServerCertificates()
     */
public Certificate[] getServerCertificates() throws SSLPeerUnverifiedException {
    try {
        CertificateFactory cf = CertificateFactory.getInstance(typeDone);
        byte[] barr = TestUtils.getX509Certificate_v3();
        ByteArrayInputStream bis = new ByteArrayInputStream(barr);
        Certificate cert = cf.generateCertificate(bis);
        return new Certificate[] { cert };
    } catch (CertificateException se) {
        throw new SSLPeerUnverifiedException("No server's end-entity certificate");
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) SSLPeerUnverifiedException(javax.net.ssl.SSLPeerUnverifiedException) CertificateException(java.security.cert.CertificateException) CertificateFactory(java.security.cert.CertificateFactory) Certificate(java.security.cert.Certificate)

Example 65 with CertificateException

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

the class CertificateFactory2Test method GetInstance01.

/**
     * Test for <code>getInstance(String type)</code> method
     * Assertions:
     * throws NullPointerException when type is null
     * throws CertificateException when type is not available
     * returns CertificateFactory object
     */
public void GetInstance01(boolean mode) throws CertificateException, CRLException {
    try {
        CertificateFactory.getInstance(null);
        fail("NullPointerException or CertificateException must be thrown when type is null");
    } catch (CertificateException e) {
    } catch (NullPointerException e) {
    }
    for (int i = 0; i < invalidValues.length; i++) {
        try {
            CertificateFactory.getInstance(invalidValues[i]);
            fail("CertificateException must be thrown (type: ".concat(invalidValues[i]).concat(")"));
        } catch (CertificateException e) {
        }
    }
    CertificateFactory cerF;
    for (int i = 0; i < validValues.length; i++) {
        cerF = CertificateFactory.getInstance(validValues[i]);
        assertEquals("Incorrect type", cerF.getType(), validValues[i]);
        assertEquals("Incorrect provider", cerF.getProvider(), mProv);
        checkResult(cerF, mode);
    }
}
Also used : CertificateException(java.security.cert.CertificateException) CertificateFactory(java.security.cert.CertificateFactory)

Aggregations

CertificateException (java.security.cert.CertificateException)456 IOException (java.io.IOException)221 X509Certificate (java.security.cert.X509Certificate)215 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)141 KeyStoreException (java.security.KeyStoreException)123 CertificateFactory (java.security.cert.CertificateFactory)103 ByteArrayInputStream (java.io.ByteArrayInputStream)97 Certificate (java.security.cert.Certificate)75 KeyStore (java.security.KeyStore)58 InputStream (java.io.InputStream)55 UnrecoverableKeyException (java.security.UnrecoverableKeyException)53 ArrayList (java.util.ArrayList)49 InvalidKeyException (java.security.InvalidKeyException)44 X509TrustManager (javax.net.ssl.X509TrustManager)41 SSLContext (javax.net.ssl.SSLContext)36 FileInputStream (java.io.FileInputStream)34 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)34 RemoteException (android.os.RemoteException)33 FileNotFoundException (java.io.FileNotFoundException)30 KeyManagementException (java.security.KeyManagementException)30