Search in sources :

Example 36 with SSLSocketFactory

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

the class SSLSocketTest method test_SSLSocket_setSoTimeout_wrapper.

public void test_SSLSocket_setSoTimeout_wrapper() throws Exception {
    if (StandardNames.IS_RI) {
        // RI cannot handle this case
        return;
    }
    ServerSocket listening = new ServerSocket(0);
    // setSoTimeout applies to read, not connect, so connect first
    Socket underlying = new Socket(listening.getInetAddress(), listening.getLocalPort());
    Socket server = listening.accept();
    SSLSocketFactory sf = (SSLSocketFactory) SSLSocketFactory.getDefault();
    Socket clientWrapping = sf.createSocket(underlying, null, -1, false);
    underlying.setSoTimeout(1);
    try {
        clientWrapping.getInputStream().read();
        fail();
    } catch (SocketTimeoutException expected) {
    }
    clientWrapping.close();
    server.close();
    underlying.close();
    listening.close();
}
Also used : SocketTimeoutException(java.net.SocketTimeoutException) ServerSocket(java.net.ServerSocket) SSLServerSocket(javax.net.ssl.SSLServerSocket) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) Socket(java.net.Socket) SSLSocket(javax.net.ssl.SSLSocket) ServerSocket(java.net.ServerSocket) SSLServerSocket(javax.net.ssl.SSLServerSocket)

Example 37 with SSLSocketFactory

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

the class SSLContextSpiTest method test_commonTest_01.

/**
     * SSLContextSpi#engineGetClientSessionContext()
     * SSLContextSpi#engineGetServerSessionContext()
     * SSLContextSpi#engineGetServerSocketFactory()
     * SSLContextSpi#engineGetSocketFactory()
     * Verify exception when SSLContextSpi object wasn't initialiazed.
     */
public void test_commonTest_01() {
    SSLContextSpiImpl ssl = new SSLContextSpiImpl();
    try {
        SSLSessionContext slsc = ssl.engineGetClientSessionContext();
        fail("RuntimeException wasn't thrown");
    } catch (RuntimeException re) {
        String str = re.getMessage();
        if (!str.equals("Not initialiazed"))
            fail("Incorrect exception message: " + str);
    } catch (Exception e) {
        fail("Incorrect exception " + e + " was thrown");
    }
    try {
        SSLSessionContext slsc = ssl.engineGetServerSessionContext();
        fail("RuntimeException wasn't thrown");
    } catch (RuntimeException re) {
        String str = re.getMessage();
        if (!str.equals("Not initialiazed"))
            fail("Incorrect exception message: " + str);
    } catch (Exception e) {
        fail("Incorrect exception " + e + " was thrown");
    }
    try {
        SSLServerSocketFactory sssf = ssl.engineGetServerSocketFactory();
        fail("RuntimeException wasn't thrown");
    } catch (RuntimeException re) {
        String str = re.getMessage();
        if (!str.equals("Not initialiazed"))
            fail("Incorrect exception message: " + str);
    } catch (Exception e) {
        fail("Incorrect exception " + e + " was thrown");
    }
    try {
        SSLSocketFactory ssf = ssl.engineGetSocketFactory();
        fail("RuntimeException wasn't thrown");
    } catch (RuntimeException re) {
        String str = re.getMessage();
        if (!str.equals("Not initialiazed"))
            fail("Incorrect exception message: " + str);
    } catch (Exception e) {
        fail("Incorrect exception " + e + " was thrown");
    }
}
Also used : SSLSessionContext(javax.net.ssl.SSLSessionContext) SSLServerSocketFactory(javax.net.ssl.SSLServerSocketFactory) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) SSLContextSpiImpl(org.apache.harmony.xnet.tests.support.SSLContextSpiImpl) KeyManagementException(java.security.KeyManagementException)

Example 38 with SSLSocketFactory

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

the class myHostnameVerifier method test_getSSLSocketFactory.

/**
     * javax.net.ssl.HttpsURLConnection#getSSLSocketFactory()
     */
public final void test_getSSLSocketFactory() {
    HttpsURLConnection con = new MyHttpsURLConnection(null);
    SSLSocketFactory sf = con.getSSLSocketFactory();
    if (!sf.equals(SSLSocketFactory.getDefault())) {
        fail("incorrect DefaultSSLSocketFactory");
    }
}
Also used : SSLSocketFactory(javax.net.ssl.SSLSocketFactory) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Example 39 with SSLSocketFactory

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

the class myHostnameVerifier method test_setDefaultSSLSocketFactory.

/**
     * javax.net.ssl.HttpsURLConnection#setDefaultSSLSocketFactory()
     */
public final void test_setDefaultSSLSocketFactory() {
    try {
        HttpsURLConnection.setDefaultSSLSocketFactory(null);
        fail("No expected IllegalArgumentException");
    } catch (IllegalArgumentException expected) {
    }
    SSLSocketFactory ssf = (SSLSocketFactory) SSLSocketFactory.getDefault();
    HttpsURLConnection.setDefaultSSLSocketFactory(ssf);
}
Also used : SSLSocketFactory(javax.net.ssl.SSLSocketFactory)

Example 40 with SSLSocketFactory

use of javax.net.ssl.SSLSocketFactory in project ion by koush.

the class ConscryptMiddleware method initialize.

public static void initialize(Context context) {
    try {
        synchronized (lock) {
            if (initialized)
                return;
            initialized = true;
            // GMS Conscrypt is already initialized, from outside ion. Leave it alone.
            if (Security.getProvider(GMS_PROVIDER) != null) {
                success = true;
                return;
            }
            SSLContext originalDefaultContext = SSLContext.getDefault();
            SSLSocketFactory originalDefaultSSLSocketFactory = HttpsURLConnection.getDefaultSSLSocketFactory();
            try {
                Class<?> providerInstaller = Class.forName("com.google.android.gms.security.ProviderInstaller");
                Method mInsertProvider = providerInstaller.getDeclaredMethod("installIfNeeded", Context.class);
                mInsertProvider.invoke(null, context);
            } catch (Throwable ignored) {
                Context gms = context.createPackageContext("com.google.android.gms", Context.CONTEXT_INCLUDE_CODE | Context.CONTEXT_IGNORE_SECURITY);
                gms.getClassLoader().loadClass("com.google.android.gms.common.security.ProviderInstallerImpl").getMethod("insertProvider", Context.class).invoke(null, context);
            }
            Provider[] providers = Security.getProviders();
            Provider provider = Security.getProvider(GMS_PROVIDER);
            Security.removeProvider(GMS_PROVIDER);
            Security.insertProviderAt(provider, providers.length);
            SSLContext.setDefault(originalDefaultContext);
            HttpsURLConnection.setDefaultSSLSocketFactory(originalDefaultSSLSocketFactory);
            success = true;
        }
    } catch (Exception e) {
        Log.w(LOGTAG, "Conscrypt initialization failed.");
    }
}
Also used : Context(android.content.Context) SSLContext(javax.net.ssl.SSLContext) SSLContext(javax.net.ssl.SSLContext) Method(java.lang.reflect.Method) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) Provider(java.security.Provider)

Aggregations

SSLSocketFactory (javax.net.ssl.SSLSocketFactory)191 SSLSocket (javax.net.ssl.SSLSocket)69 SSLContext (javax.net.ssl.SSLContext)57 IOException (java.io.IOException)45 Socket (java.net.Socket)37 Test (org.junit.Test)33 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)29 HostnameVerifier (javax.net.ssl.HostnameVerifier)27 URL (java.net.URL)23 KeyManagementException (java.security.KeyManagementException)20 OutputStream (java.io.OutputStream)19 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)19 InputStream (java.io.InputStream)18 CertificateException (java.security.cert.CertificateException)17 HttpURLConnection (java.net.HttpURLConnection)15 InetSocketAddress (java.net.InetSocketAddress)15 X509TrustManager (javax.net.ssl.X509TrustManager)15 OkHttpClient (okhttp3.OkHttpClient)14 SSLParameters (javax.net.ssl.SSLParameters)13 TrustManager (javax.net.ssl.TrustManager)13