Search in sources :

Example 31 with TrustManager

use of javax.net.ssl.TrustManager in project robovm by robovm.

the class myTrustManagerFactory method test_getTrustManagers.

/**
     * Test for <code>geTrustManagers()</code>
     * @throws KeyStoreException
     * @throws IOException
     * @throws CertificateException
     * @throws NoSuchAlgorithmException
     */
public void test_getTrustManagers() {
    try {
        TrustManagerFactory trustMF = TrustManagerFactory.getInstance(getDefaultAlgorithm());
        KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
        ks.load(null, null);
        trustMF.init(ks);
        TrustManager[] tm = trustMF.getTrustManagers();
        assertNotNull("Result has not be null", tm);
        assertTrue("Length of result TrustManager array should not be 0", (tm.length > 0));
    } catch (Exception ex) {
        fail("Unexpected exception " + ex.toString());
    }
}
Also used : TrustManagerFactory(javax.net.ssl.TrustManagerFactory) 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) TrustManager(javax.net.ssl.TrustManager)

Example 32 with TrustManager

use of javax.net.ssl.TrustManager in project robovm by robovm.

the class SSLParametersImpl method createDefaultTrustManager.

private static X509TrustManager createDefaultTrustManager() throws KeyManagementException {
    try {
        String algorithm = TrustManagerFactory.getDefaultAlgorithm();
        TrustManagerFactory tmf = TrustManagerFactory.getInstance(algorithm);
        tmf.init((KeyStore) null);
        TrustManager[] tms = tmf.getTrustManagers();
        X509TrustManager trustManager = findX509TrustManager(tms);
        return trustManager;
    } catch (NoSuchAlgorithmException e) {
        throw new KeyManagementException(e);
    } catch (KeyStoreException e) {
        throw new KeyManagementException(e);
    }
}
Also used : X509TrustManager(javax.net.ssl.X509TrustManager) TrustManagerFactory(javax.net.ssl.TrustManagerFactory) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyStoreException(java.security.KeyStoreException) KeyManagementException(java.security.KeyManagementException) TrustManager(javax.net.ssl.TrustManager) X509TrustManager(javax.net.ssl.X509TrustManager)

Example 33 with TrustManager

use of javax.net.ssl.TrustManager in project robovm by robovm.

the class MySslContext method test_init$Ljavax_net_ssl_KeyManager$Ljavax_net_ssl_TrustManagerLjava_security_SecureRandom.

/**
      * @throws NoSuchAlgorithmException
     * @throws KeyStoreException
     * @throws FileNotFoundException
     * @throws KeyManagementException
     * javax.net.ssl.SSLContext#
      *     init(javax.net.ssl.KeyManager[], javax.net.ssl.TrustManager[],
      *     java.security.SecureRandom)
      */
public void test_init$Ljavax_net_ssl_KeyManager$Ljavax_net_ssl_TrustManagerLjava_security_SecureRandom() throws Exception {
    if (!DEFSupported)
        fail(NotSupportMsg);
    SSLContextSpi spi = new MySSLContextSpi();
    SSLContext sslContext = new MySslContext(spi, defaultProvider, defaultProtocol);
    try {
        sslContext.createSSLEngine();
        fail("Expected RuntimeException was not thrown");
    } catch (RuntimeException rte) {
    // expected
    }
    try {
        sslContext.init(null, null, null);
        fail("KeyManagementException wasn't thrown");
    } catch (KeyManagementException kme) {
    //expected
    }
    try {
        String tAlg = TrustManagerFactory.getDefaultAlgorithm();
        String kAlg = KeyManagerFactory.getDefaultAlgorithm();
        if (tAlg == null)
            fail("TrustManagerFactory default algorithm is not defined");
        if (kAlg == null)
            fail("KeyManagerFactory default algorithm is not defined");
        KeyManagerFactory kmf = KeyManagerFactory.getInstance(kAlg);
        kmf.init(null, new char[11]);
        TrustManagerFactory tmf = TrustManagerFactory.getInstance(tAlg);
        KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
        tmf.init(ks);
        TrustManager[] tms = tmf.getTrustManagers();
        sslContext.init(kmf.getKeyManagers(), tms, new SecureRandom());
    } catch (Exception e) {
        System.out.println("EE = " + e);
    }
}
Also used : SecureRandom(java.security.SecureRandom) SSLContext(javax.net.ssl.SSLContext) KeyStore(java.security.KeyStore) KeyManagementException(java.security.KeyManagementException) KeyManagementException(java.security.KeyManagementException) KeyStoreException(java.security.KeyStoreException) FileNotFoundException(java.io.FileNotFoundException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) UnrecoverableKeyException(java.security.UnrecoverableKeyException) NoSuchProviderException(java.security.NoSuchProviderException) KeyManagerFactory(javax.net.ssl.KeyManagerFactory) TrustManager(javax.net.ssl.TrustManager) MySSLContextSpi(org.apache.harmony.xnet.tests.support.MySSLContextSpi) MySSLContextSpi(org.apache.harmony.xnet.tests.support.MySSLContextSpi) SSLContextSpi(javax.net.ssl.SSLContextSpi) TrustManagerFactory(javax.net.ssl.TrustManagerFactory)

Example 34 with TrustManager

use of javax.net.ssl.TrustManager in project robovm by robovm.

the class MySslContext method test_getServerSocketFactory.

/**
     * Test for <code>getServerSocketFactory()</code>
     * <code>getSocketFactory()</code>
     * <code>init(KeyManager[] km, TrustManager[] tm, SecureRandom random)</code>
     * methods Assertion: returns correspondent object
     *
     */
public void test_getServerSocketFactory() throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, UnrecoverableKeyException {
    if (!DEFSupported) {
        fail(NotSupportMsg);
        return;
    }
    SSLContext[] sslC = createSSLCon();
    assertNotNull("SSLContext objects were not created", sslC);
    String tAlg = TrustManagerFactory.getDefaultAlgorithm();
    String kAlg = KeyManagerFactory.getDefaultAlgorithm();
    if (tAlg == null) {
        fail("TrustManagerFactory default algorithm is not defined");
        return;
    }
    if (kAlg == null) {
        fail("KeyManagerFactory default algorithm is not defined");
        return;
    }
    KeyManagerFactory kmf = KeyManagerFactory.getInstance(kAlg);
    KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
    try {
        ks.load(null, null);
    } catch (Exception e) {
        fail(e + " was thrown for method load(null, null)");
    }
    kmf.init(ks, new char[10]);
    KeyManager[] kms = kmf.getKeyManagers();
    TrustManagerFactory tmf = TrustManagerFactory.getInstance(tAlg);
    tmf.init(ks);
    TrustManager[] tms = tmf.getTrustManagers();
    for (int i = 0; i < sslC.length; i++) {
        sslC[i].init(kms, tms, new SecureRandom());
        assertNotNull("No SSLServerSocketFactory available", sslC[i].getServerSocketFactory());
        assertNotNull("No SSLSocketFactory available", sslC[i].getSocketFactory());
    }
}
Also used : SecureRandom(java.security.SecureRandom) SSLContext(javax.net.ssl.SSLContext) KeyStore(java.security.KeyStore) KeyManagementException(java.security.KeyManagementException) KeyStoreException(java.security.KeyStoreException) FileNotFoundException(java.io.FileNotFoundException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) UnrecoverableKeyException(java.security.UnrecoverableKeyException) NoSuchProviderException(java.security.NoSuchProviderException) KeyManagerFactory(javax.net.ssl.KeyManagerFactory) TrustManager(javax.net.ssl.TrustManager) TrustManagerFactory(javax.net.ssl.TrustManagerFactory) KeyManager(javax.net.ssl.KeyManager)

Example 35 with TrustManager

use of javax.net.ssl.TrustManager in project robovm by robovm.

the class SSLContextSpiTest method test_commonTest_02.

/**
     * SSLContextSpi#engineCreateSSLEngine()
     * SSLContextSpi#engineCreateSSLEngine(String host, int port)
     * SSLContextSpi#engineGetClientSessionContext()
     * SSLContextSpi#engineGetServerSessionContext()
     * SSLContextSpi#engineGetServerSocketFactory()
     * SSLContextSpi#engineGetSocketFactory()
     */
public void test_commonTest_02() {
    SSLContextSpiImpl ssl = new SSLContextSpiImpl();
    String defaultAlgorithm = Security.getProperty("ssl.KeyManagerFactory.algorithm");
    try {
        KeyManagerFactory kmf = KeyManagerFactory.getInstance(defaultAlgorithm);
        char[] pass = "password".toCharArray();
        kmf.init(null, pass);
        KeyManager[] km = kmf.getKeyManagers();
        defaultAlgorithm = Security.getProperty("ssl.TrustManagerFactory.algorithm");
        TrustManagerFactory trustMF = TrustManagerFactory.getInstance(defaultAlgorithm);
        KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
        ks.load(null, null);
        trustMF.init(ks);
        TrustManager[] tm = trustMF.getTrustManagers();
        SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
        ssl.engineInit(km, tm, sr);
    } catch (Exception ex) {
        fail(ex + " unexpected exception");
    }
    try {
        assertNotNull("Subtest_01: Object is NULL", ssl.engineCreateSSLEngine());
        SSLEngine sleng = ssl.engineCreateSSLEngine("localhost", 1080);
        assertNotNull("Subtest_02: Object is NULL", sleng);
        assertEquals(sleng.getPeerPort(), 1080);
        assertEquals(sleng.getPeerHost(), "localhost");
        assertNull("Subtest_03: Object not NULL", ssl.engineGetClientSessionContext());
        assertNull("Subtest_04: Object not NULL", ssl.engineGetServerSessionContext());
        assertNull("Subtest_05: Object not NULL", ssl.engineGetServerSocketFactory());
        assertNull("Subtest_06: Object not NULL", ssl.engineGetSocketFactory());
    } catch (Exception e) {
        fail("Unexpected exception " + e);
    }
}
Also used : SSLEngine(javax.net.ssl.SSLEngine) SecureRandom(java.security.SecureRandom) KeyStore(java.security.KeyStore) KeyManagementException(java.security.KeyManagementException) KeyManagerFactory(javax.net.ssl.KeyManagerFactory) TrustManager(javax.net.ssl.TrustManager) TrustManagerFactory(javax.net.ssl.TrustManagerFactory) KeyManager(javax.net.ssl.KeyManager) SSLContextSpiImpl(org.apache.harmony.xnet.tests.support.SSLContextSpiImpl)

Aggregations

TrustManager (javax.net.ssl.TrustManager)186 SSLContext (javax.net.ssl.SSLContext)116 X509TrustManager (javax.net.ssl.X509TrustManager)111 TrustManagerFactory (javax.net.ssl.TrustManagerFactory)78 X509Certificate (java.security.cert.X509Certificate)53 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)51 SecureRandom (java.security.SecureRandom)51 IOException (java.io.IOException)48 KeyManagementException (java.security.KeyManagementException)47 KeyStore (java.security.KeyStore)47 KeyManager (javax.net.ssl.KeyManager)44 CertificateException (java.security.cert.CertificateException)36 KeyStoreException (java.security.KeyStoreException)35 KeyManagerFactory (javax.net.ssl.KeyManagerFactory)31 GeneralSecurityException (java.security.GeneralSecurityException)22 HostnameVerifier (javax.net.ssl.HostnameVerifier)19 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)15 InputStream (java.io.InputStream)14 UnrecoverableKeyException (java.security.UnrecoverableKeyException)13 FileInputStream (java.io.FileInputStream)11