Search in sources :

Example 21 with KeyManagementException

use of java.security.KeyManagementException in project robovm by robovm.

the class IdentityScope2Test method test_removeIdentityLjava_security_Identity.

/**
     * java.security.IdentityScope#removeIdentity(java.security.Identity)
     */
public void test_removeIdentityLjava_security_Identity() throws Exception {
    IdentityScopeSubclass sub = new IdentityScopeSubclass("test", new IdentityScopeSubclass());
    Identity id = new IdentitySubclass();
    id.setPublicKey(getPubKey());
    sub.addIdentity(id);
    sub.removeIdentity(id);
    try {
        sub.removeIdentity(id);
        fail("KeyManagementException should have been thrown");
    } catch (KeyManagementException expected) {
    }
}
Also used : IdentitySubclass(org.apache.harmony.security.tests.java.security.Identity2Test.IdentitySubclass) Identity(java.security.Identity) KeyManagementException(java.security.KeyManagementException)

Example 22 with KeyManagementException

use of java.security.KeyManagementException in project robovm by robovm.

the class SSLParametersImpl method createDefaultKeyManager.

private static X509KeyManager createDefaultKeyManager() throws KeyManagementException {
    try {
        String algorithm = KeyManagerFactory.getDefaultAlgorithm();
        KeyManagerFactory kmf = KeyManagerFactory.getInstance(algorithm);
        kmf.init(null, null);
        KeyManager[] kms = kmf.getKeyManagers();
        return findX509KeyManager(kms);
    } catch (NoSuchAlgorithmException e) {
        throw new KeyManagementException(e);
    } catch (KeyStoreException e) {
        throw new KeyManagementException(e);
    } catch (UnrecoverableKeyException e) {
        throw new KeyManagementException(e);
    }
}
Also used : UnrecoverableKeyException(java.security.UnrecoverableKeyException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyStoreException(java.security.KeyStoreException) X509KeyManager(javax.net.ssl.X509KeyManager) KeyManager(javax.net.ssl.KeyManager) KeyManagementException(java.security.KeyManagementException) KeyManagerFactory(javax.net.ssl.KeyManagerFactory)

Example 23 with KeyManagementException

use of java.security.KeyManagementException 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 24 with KeyManagementException

use of java.security.KeyManagementException 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 25 with KeyManagementException

use of java.security.KeyManagementException 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)

Aggregations

KeyManagementException (java.security.KeyManagementException)132 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)86 SSLContext (javax.net.ssl.SSLContext)65 KeyStoreException (java.security.KeyStoreException)43 TrustManager (javax.net.ssl.TrustManager)39 IOException (java.io.IOException)38 CertificateException (java.security.cert.CertificateException)23 X509TrustManager (javax.net.ssl.X509TrustManager)22 SecureRandom (java.security.SecureRandom)21 X509Certificate (java.security.cert.X509Certificate)19 UnrecoverableKeyException (java.security.UnrecoverableKeyException)18 KeyManager (javax.net.ssl.KeyManager)18 TrustManagerFactory (javax.net.ssl.TrustManagerFactory)16 KeyStore (java.security.KeyStore)13 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)13 KeyManagerFactory (javax.net.ssl.KeyManagerFactory)10 HostnameVerifier (javax.net.ssl.HostnameVerifier)9 NoSuchProviderException (java.security.NoSuchProviderException)7 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)7 SSLSocket (javax.net.ssl.SSLSocket)7