Search in sources :

Example 81 with KeyStore

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

the class HttpsURLConnectionTest method getContext.

/**
     * Builds and returns the context used for secure socket creation.
     */
private static SSLContext getContext() throws Exception {
    String type = KeyStore.getDefaultType();
    String keyStore = getKeyStoreFileName();
    File keyStoreFile = new File(keyStore);
    FileInputStream fis = new FileInputStream(keyStoreFile);
    KeyStore ks = KeyStore.getInstance(type);
    ks.load(fis, KS_PASSWORD.toCharArray());
    fis.close();
    if (DO_LOG && false) {
        TestKeyStore.dump("HttpsURLConnection.getContext", ks, KS_PASSWORD.toCharArray());
    }
    String kmfAlgorithm = KeyManagerFactory.getDefaultAlgorithm();
    KeyManagerFactory kmf = KeyManagerFactory.getInstance(kmfAlgorithm);
    kmf.init(ks, KS_PASSWORD.toCharArray());
    KeyManager[] keyManagers = kmf.getKeyManagers();
    String tmfAlgorthm = TrustManagerFactory.getDefaultAlgorithm();
    TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorthm);
    tmf.init(ks);
    TrustManager[] trustManagers = tmf.getTrustManagers();
    if (DO_LOG) {
        trustManagers = TestTrustManager.wrap(trustManagers);
    }
    SSLContext ctx = SSLContext.getInstance("TLSv1");
    ctx.init(keyManagers, trustManagers, null);
    return ctx;
}
Also used : TrustManagerFactory(javax.net.ssl.TrustManagerFactory) SSLContext(javax.net.ssl.SSLContext) File(java.io.File) TestKeyStore(libcore.java.security.TestKeyStore) KeyStore(java.security.KeyStore) KeyManager(javax.net.ssl.KeyManager) FileInputStream(java.io.FileInputStream) KeyManagerFactory(javax.net.ssl.KeyManagerFactory) TrustManager(javax.net.ssl.TrustManager) TestTrustManager(libcore.javax.net.ssl.TestTrustManager)

Example 82 with KeyStore

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

the class MyProvider method test_engineInit_02.

/**
     * @throws InvalidAlgorithmParameterException
     * @throws NoSuchAlgorithmException
     * javax.net.ssl.TrustManagerFactorySpi#engineInit(ManagerFactoryParameters spec)
     */
public void test_engineInit_02() throws InvalidAlgorithmParameterException, NoSuchAlgorithmException {
    factory.reset();
    Provider provider = new MyProvider();
    TrustManagerFactory tmf = TrustManagerFactory.getInstance("MyTMF", provider);
    Parameters pr = null;
    try {
        KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
        ks.load(null, null);
        pr = new Parameters(ks);
        tmf.init(pr);
    } catch (Exception e) {
        fail("Unexpected exception " + e.toString());
    }
    assertTrue(factory.isEngineInitCalled());
    assertEquals(pr, factory.getSpec());
    factory.reset();
    tmf.init((ManagerFactoryParameters) null);
    assertTrue(factory.isEngineInitCalled());
    assertNull(factory.getSpec());
}
Also used : Parameters(org.apache.harmony.xnet.tests.support.MyTrustManagerFactorySpi.Parameters) ManagerFactoryParameters(javax.net.ssl.ManagerFactoryParameters) TrustManagerFactory(javax.net.ssl.TrustManagerFactory) KeyStore(java.security.KeyStore) KeyStoreException(java.security.KeyStoreException) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Provider(java.security.Provider)

Example 83 with KeyStore

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

the class MyProvider method test_engineGetTrustManagers.

/**
     * @throws NoSuchAlgorithmException
     * javax.net.ssl.TrustManagerFactorySpi#engineGetTrustManagers()
     */
public void test_engineGetTrustManagers() throws NoSuchAlgorithmException {
    factory.reset();
    Provider provider = new MyProvider();
    TrustManagerFactory tmf = TrustManagerFactory.getInstance("MyTMF", provider);
    TrustManager[] tm = tmf.getTrustManagers();
    assertTrue(factory.isEngineGetTrustManagersCalled());
    factory.reset();
    try {
        KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
        ks.load(null, null);
        tmf.init(ks);
        tm = tmf.getTrustManagers();
        assertTrue(factory.isEngineGetTrustManagersCalled());
    } catch (Exception e) {
        fail("Unexpected exception " + e.toString());
    }
}
Also used : TrustManagerFactory(javax.net.ssl.TrustManagerFactory) KeyStore(java.security.KeyStore) KeyStoreException(java.security.KeyStoreException) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Provider(java.security.Provider) TrustManager(javax.net.ssl.TrustManager)

Example 84 with KeyStore

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

the class myTrustManagerFactory method test_initLjava_security_KeyStore_01.

/**
     * Test for <code>init(KeyStore keyStore)</code>
     * Assertion: call method with null parameter
     */
public void test_initLjava_security_KeyStore_01() throws Exception {
    KeyStore ksNull = null;
    TrustManagerFactory[] trustMF = createTMFac();
    assertNotNull("TrustManagerFactory objects were not created", trustMF);
    // null parameter
    try {
        trustMF[0].init(ksNull);
    } catch (Exception ex) {
        fail(ex + " unexpected exception was thrown for null parameter");
    }
}
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)

Example 85 with KeyStore

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

Aggregations

KeyStore (java.security.KeyStore)738 IOException (java.io.IOException)190 X509Certificate (java.security.cert.X509Certificate)189 FileInputStream (java.io.FileInputStream)163 KeyStoreException (java.security.KeyStoreException)151 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)138 InputStream (java.io.InputStream)125 Certificate (java.security.cert.Certificate)124 TrustManagerFactory (javax.net.ssl.TrustManagerFactory)119 KeyManagerFactory (javax.net.ssl.KeyManagerFactory)114 SSLContext (javax.net.ssl.SSLContext)112 PrivateKey (java.security.PrivateKey)94 CertificateException (java.security.cert.CertificateException)94 File (java.io.File)82 ByteArrayInputStream (java.io.ByteArrayInputStream)75 CertificateFactory (java.security.cert.CertificateFactory)75 Key (java.security.Key)61 UnrecoverableKeyException (java.security.UnrecoverableKeyException)55 TrustManager (javax.net.ssl.TrustManager)47 KeyManagementException (java.security.KeyManagementException)40