Search in sources :

Example 6 with ConnectionSpec

use of okhttp3.ConnectionSpec in project kripton by xcesco.

the class NetworkClient method instance.

public static OkHttpClient instance() {
    if (client == null) {
        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        logging.setLevel(HttpLoggingInterceptor.Level.BASIC);
        /* ConnectionSpec spec = new
                    ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS)
                    .tlsVersions(TlsVersion.TLS_1_2)
                    .cipherSuites(
                            CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
                            CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
                            CipherSuite.TLS_DHE_RSA_WITH_AES_128_GCM_SHA256)
                    .build();*/
        client = (new OkHttpClient.Builder()).readTimeout(60, TimeUnit.SECONDS).connectTimeout(60, TimeUnit.SECONDS).addInterceptor(logging).build();
    }
    return client;
}
Also used : HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor)

Example 7 with ConnectionSpec

use of okhttp3.ConnectionSpec in project opacclient by opacapp.

the class HttpClientFactory method getOkHttpClientBuilder.

protected OkHttpClient.Builder getOkHttpClientBuilder(boolean customssl, boolean tls_only, boolean allCipherSuites) {
    OkHttpClient.Builder builder = new OkHttpClient.Builder();
    CookieManager cookieManager = new CookieManager();
    cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
    builder.cookieJar(new JavaNetCookieJar(cookieManager));
    builder.addNetworkInterceptor(new CustomRedirectInterceptor());
    builder.connectTimeout(60, TimeUnit.SECONDS);
    builder.readTimeout(60, TimeUnit.SECONDS);
    builder.writeTimeout(60, TimeUnit.SECONDS);
    if (customssl && ssl_store_path != null) {
        try {
            if (trust_store == null) {
                trust_store = getKeyStore();
            }
            X509TrustManager trustManager = new AdditionalKeyStoresSSLSocketFactory.AdditionalKeyStoresTrustManager(trust_store);
            SSLSocketFactory sf = AdditionalKeyStoresSSLSocketFactory.createForOkHttp(trustManager);
            if (allCipherSuites) {
                sf = new AllCiphersProxySocketFactory(sf);
            }
            sf = new TLS12ProxySocketFactory(sf);
            builder.sslSocketFactory(sf, trustManager);
            List<ConnectionSpec> connectionSpecs = new ArrayList<ConnectionSpec>();
            connectionSpecs.add(ConnectionSpec.MODERN_TLS);
            connectionSpecs.add(new ConnectionSpec.Builder(ConnectionSpec.COMPATIBLE_TLS).allEnabledCipherSuites().build());
            if (!tls_only) {
                connectionSpecs.add(new ConnectionSpec.Builder(ConnectionSpec.COMPATIBLE_TLS).tlsVersions(TlsVersion.SSL_3_0, TlsVersion.TLS_1_0).allEnabledCipherSuites().build());
            } else if (allCipherSuites) {
                connectionSpecs.add(new ConnectionSpec.Builder(ConnectionSpec.COMPATIBLE_TLS).allEnabledCipherSuites().build());
            }
            connectionSpecs.add(ConnectionSpec.CLEARTEXT);
            builder.connectionSpecs(connectionSpecs);
            return builder;
        } catch (Exception e) {
            e.printStackTrace();
            return builder;
        }
    } else {
        try {
            X509TrustManager trustManager = getSystemDefaultTrustManager();
            SSLSocketFactory socketFactory = getSystemDefaultSSLSocketFactory(trustManager);
            builder.sslSocketFactory(new TLS12ProxySocketFactory(socketFactory), trustManager);
        } catch (NoSuchAlgorithmException | KeyStoreException | KeyManagementException ignored) {
        }
        return builder;
    }
}
Also used : JavaNetCookieJar(okhttp3.JavaNetCookieJar) OkHttpClient(okhttp3.OkHttpClient) ConnectionSpec(okhttp3.ConnectionSpec) RegistryBuilder(org.apache.http.config.RegistryBuilder) RequestBuilder(org.apache.http.client.methods.RequestBuilder) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) ArrayList(java.util.ArrayList) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyStoreException(java.security.KeyStoreException) ProtocolException(org.apache.http.ProtocolException) URISyntaxException(java.net.URISyntaxException) KeyStoreException(java.security.KeyStoreException) KeyManagementException(java.security.KeyManagementException) FileNotFoundException(java.io.FileNotFoundException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) CircularRedirectException(org.apache.http.client.CircularRedirectException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) KeyManagementException(java.security.KeyManagementException) X509TrustManager(javax.net.ssl.X509TrustManager) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) CookieManager(java.net.CookieManager)

Example 8 with ConnectionSpec

use of okhttp3.ConnectionSpec in project okhttp by square.

the class CipherSuiteTest method applyIntersectionRetainsTlsPrefixes.

@Test
public void applyIntersectionRetainsTlsPrefixes() throws Exception {
    FakeSslSocket socket = new FakeSslSocket();
    socket.setEnabledProtocols(new String[] { "TLSv1" });
    socket.setSupportedCipherSuites(new String[] { "TLS_A", "TLS_B", "TLS_C", "TLS_D", "TLS_E" });
    socket.setEnabledCipherSuites(new String[] { "TLS_A", "TLS_B", "TLS_C" });
    ConnectionSpec connectionSpec = new ConnectionSpec.Builder(true).tlsVersions(TlsVersion.TLS_1_0).cipherSuites("SSL_A", "SSL_C", "SSL_E").build();
    applyConnectionSpec(connectionSpec, socket, false);
    assertArrayEquals(new String[] { "TLS_A", "TLS_C" }, socket.enabledCipherSuites);
}
Also used : Internal.applyConnectionSpec(okhttp3.internal.Internal.applyConnectionSpec) Test(org.junit.jupiter.api.Test)

Example 9 with ConnectionSpec

use of okhttp3.ConnectionSpec in project okhttp by square.

the class CipherSuiteTest method applyIntersectionAddsSslScsvForFallback.

@Test
public void applyIntersectionAddsSslScsvForFallback() throws Exception {
    FakeSslSocket socket = new FakeSslSocket();
    socket.setEnabledProtocols(new String[] { "TLSv1" });
    socket.setSupportedCipherSuites(new String[] { "SSL_A", "SSL_FALLBACK_SCSV" });
    socket.setEnabledCipherSuites(new String[] { "SSL_A" });
    ConnectionSpec connectionSpec = new ConnectionSpec.Builder(true).tlsVersions(TlsVersion.TLS_1_0).cipherSuites("SSL_A").build();
    applyConnectionSpec(connectionSpec, socket, true);
    assertArrayEquals(new String[] { "SSL_A", "SSL_FALLBACK_SCSV" }, socket.enabledCipherSuites);
}
Also used : Internal.applyConnectionSpec(okhttp3.internal.Internal.applyConnectionSpec) Test(org.junit.jupiter.api.Test)

Example 10 with ConnectionSpec

use of okhttp3.ConnectionSpec in project okhttp by square.

the class CipherSuiteTest method applyIntersectionAddsTlsScsvForFallback.

@Test
public void applyIntersectionAddsTlsScsvForFallback() throws Exception {
    FakeSslSocket socket = new FakeSslSocket();
    socket.setEnabledProtocols(new String[] { "TLSv1" });
    socket.setSupportedCipherSuites(new String[] { "TLS_A", "TLS_FALLBACK_SCSV" });
    socket.setEnabledCipherSuites(new String[] { "TLS_A" });
    ConnectionSpec connectionSpec = new ConnectionSpec.Builder(true).tlsVersions(TlsVersion.TLS_1_0).cipherSuites("TLS_A").build();
    applyConnectionSpec(connectionSpec, socket, true);
    assertArrayEquals(new String[] { "TLS_A", "TLS_FALLBACK_SCSV" }, socket.enabledCipherSuites);
}
Also used : Internal.applyConnectionSpec(okhttp3.internal.Internal.applyConnectionSpec) Test(org.junit.jupiter.api.Test)

Aggregations

ConnectionSpec (okhttp3.ConnectionSpec)18 Internal.applyConnectionSpec (okhttp3.internal.Internal.applyConnectionSpec)18 Test (org.junit.jupiter.api.Test)18 ArrayList (java.util.ArrayList)11 SSLSocket (javax.net.ssl.SSLSocket)9 X509TrustManager (javax.net.ssl.X509TrustManager)9 OkHttpClient (okhttp3.OkHttpClient)7 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)6 SSLContext (javax.net.ssl.SSLContext)5 TrustManager (javax.net.ssl.TrustManager)5 TrustManagerFactory (javax.net.ssl.TrustManagerFactory)5 IOException (java.io.IOException)4 KeyManagementException (java.security.KeyManagementException)3 KeyStoreException (java.security.KeyStoreException)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)3 NonNull (android.support.annotation.NonNull)2 UnknownServiceException (java.net.UnknownServiceException)2 HttpLoggingInterceptor (okhttp3.logging.HttpLoggingInterceptor)2 GsonBuilder (com.google.gson.GsonBuilder)1 FileNotFoundException (java.io.FileNotFoundException)1