Search in sources :

Example 26 with HostnameVerifier

use of javax.net.ssl.HostnameVerifier in project cordova-android-chromeview by thedracle.

the class HttpEngine method connect.

/** Connect to the origin server either directly or via a proxy. */
protected final void connect() throws IOException {
    if (connection != null) {
        return;
    }
    if (routeSelector == null) {
        String uriHost = uri.getHost();
        if (uriHost == null) {
            throw new UnknownHostException(uri.toString());
        }
        SSLSocketFactory sslSocketFactory = null;
        HostnameVerifier hostnameVerifier = null;
        if (uri.getScheme().equalsIgnoreCase("https")) {
            sslSocketFactory = policy.sslSocketFactory;
            hostnameVerifier = policy.hostnameVerifier;
        }
        Address address = new Address(uriHost, getEffectivePort(uri), sslSocketFactory, hostnameVerifier, policy.requestedProxy);
        routeSelector = new RouteSelector(address, uri, policy.proxySelector, policy.connectionPool, Dns.DEFAULT, policy.getFailedRoutes());
    }
    connection = routeSelector.next();
    if (!connection.isConnected()) {
        connection.connect(policy.getConnectTimeout(), policy.getReadTimeout(), getTunnelConfig());
        policy.connectionPool.maybeShare(connection);
        policy.getFailedRoutes().remove(connection.getRoute());
    }
    connected(connection);
    if (connection.getRoute().getProxy() != policy.requestedProxy) {
        // Update the request line if the proxy changed; it may need a host name.
        requestHeaders.getHeaders().setRequestLine(getRequestLine());
    }
}
Also used : UnknownHostException(java.net.UnknownHostException) Address(com.squareup.okhttp.Address) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) HostnameVerifier(javax.net.ssl.HostnameVerifier)

Example 27 with HostnameVerifier

use of javax.net.ssl.HostnameVerifier in project jedis by xetorthio.

the class SSLJedisTest method connectWithShardInfoAndCustomHostnameVerifier.

/**
   * Tests opening an SSL/TLS connection to redis with a custom hostname
   * verifier.
   */
@Test
public void connectWithShardInfoAndCustomHostnameVerifier() {
    final URI uri = URI.create("rediss://localhost:6390");
    final SSLSocketFactory sslSocketFactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
    final SSLParameters sslParameters = new SSLParameters();
    HostnameVerifier hostnameVerifier = new BasicHostnameVerifier();
    JedisShardInfo shardInfo = new JedisShardInfo(uri, sslSocketFactory, sslParameters, hostnameVerifier);
    shardInfo.setPassword("foobared");
    Jedis jedis = new Jedis(shardInfo);
    jedis.get("foo");
    jedis.disconnect();
    jedis.close();
}
Also used : Jedis(redis.clients.jedis.Jedis) SSLParameters(javax.net.ssl.SSLParameters) JedisShardInfo(redis.clients.jedis.JedisShardInfo) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) URI(java.net.URI) HostnameVerifier(javax.net.ssl.HostnameVerifier) Test(org.junit.Test)

Example 28 with HostnameVerifier

use of javax.net.ssl.HostnameVerifier in project NoHttp by yanzhenjie.

the class URLConnectionNetworkExecutor method execute.

@Override
public Network execute(IBasicRequest request) throws Exception {
    URL url = new URL(request.url());
    HttpURLConnection connection;
    Proxy proxy = request.getProxy();
    if (proxy == null)
        connection = (HttpURLConnection) url.openConnection();
    else
        connection = (HttpURLConnection) url.openConnection(proxy);
    connection.setConnectTimeout(request.getConnectTimeout());
    connection.setReadTimeout(request.getReadTimeout());
    connection.setInstanceFollowRedirects(false);
    if (connection instanceof HttpsURLConnection) {
        SSLSocketFactory sslSocketFactory = request.getSSLSocketFactory();
        if (sslSocketFactory != null)
            ((HttpsURLConnection) connection).setSSLSocketFactory(sslSocketFactory);
        HostnameVerifier hostnameVerifier = request.getHostnameVerifier();
        if (hostnameVerifier != null)
            ((HttpsURLConnection) connection).setHostnameVerifier(hostnameVerifier);
    }
    // Base attribute
    connection.setRequestMethod(request.getRequestMethod().toString());
    connection.setDoInput(true);
    boolean isAllowBody = isAllowBody(request.getRequestMethod());
    connection.setDoOutput(isAllowBody);
    // Adds all request header to connection.
    Headers headers = request.headers();
    // To fix bug: accidental EOFException before API 19.
    List<String> values = headers.getValues(Headers.HEAD_KEY_CONNECTION);
    if (values == null || values.size() == 0)
        headers.set(Headers.HEAD_KEY_CONNECTION, Build.VERSION.SDK_INT > AndroidVersion.KITKAT ? Headers.HEAD_VALUE_CONNECTION_KEEP_ALIVE : Headers.HEAD_VALUE_CONNECTION_CLOSE);
    if (isAllowBody) {
        long contentLength = request.getContentLength();
        if (contentLength < Integer.MAX_VALUE)
            connection.setFixedLengthStreamingMode((int) contentLength);
        else if (Build.VERSION.SDK_INT >= AndroidVersion.KITKAT)
            try {
                Class<?> connectionClass = connection.getClass();
                Method setFixedLengthStreamingModeMethod = connectionClass.getMethod("setFixedLengthStreamingMode", long.class);
                setFixedLengthStreamingModeMethod.invoke(connection, contentLength);
            } catch (Throwable e) {
                connection.setChunkedStreamingMode(256 * 1024);
            }
        else
            connection.setChunkedStreamingMode(256 * 1024);
        headers.set(Headers.HEAD_KEY_CONTENT_LENGTH, Long.toString(contentLength));
    }
    Map<String, String> requestHeaders = headers.toRequestHeaders();
    for (Map.Entry<String, String> headerEntry : requestHeaders.entrySet()) {
        String headKey = headerEntry.getKey();
        String headValue = headerEntry.getValue();
        Logger.i(headKey + ": " + headValue);
        connection.setRequestProperty(headKey, headValue);
    }
    // 5. Connect
    connection.connect();
    return new URLConnectionNetwork(connection);
}
Also used : Method(java.lang.reflect.Method) URL(java.net.URL) HostnameVerifier(javax.net.ssl.HostnameVerifier) Proxy(java.net.Proxy) HttpURLConnection(java.net.HttpURLConnection) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) Map(java.util.Map) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Example 29 with HostnameVerifier

use of javax.net.ssl.HostnameVerifier in project CloudReader by youlookwhat.

the class HttpUtils method getUnsafeOkHttpClient.

public OkHttpClient getUnsafeOkHttpClient() {
    try {
        final TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {

            @Override
            public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
            }

            @Override
            public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
            }

            @Override
            public X509Certificate[] getAcceptedIssuers() {
                return new X509Certificate[] {};
            }
        } };
        // Install the all-trusting trust manager
        SSLContext sslContext = SSLContext.getInstance("TLS");
        sslContext.init(null, trustAllCerts, new SecureRandom());
        // Create an ssl socket factory with our all-trusting manager
        SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
        OkHttpClient.Builder okBuilder = new OkHttpClient.Builder();
        okBuilder.readTimeout(20, TimeUnit.SECONDS);
        okBuilder.connectTimeout(10, TimeUnit.SECONDS);
        okBuilder.writeTimeout(20, TimeUnit.SECONDS);
        okBuilder.addInterceptor(new HttpHeadInterceptor());
        okBuilder.addInterceptor(getInterceptor());
        okBuilder.sslSocketFactory(sslSocketFactory);
        okBuilder.hostnameVerifier(new HostnameVerifier() {

            @Override
            public boolean verify(String hostname, SSLSession session) {
                //                    Log.d("HttpUtils", "==come");
                return true;
            }
        });
        return okBuilder.build();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : OkHttpClient(okhttp3.OkHttpClient) GsonBuilder(com.google.gson.GsonBuilder) SSLSession(javax.net.ssl.SSLSession) SecureRandom(java.security.SecureRandom) SSLContext(javax.net.ssl.SSLContext) X509Certificate(java.security.cert.X509Certificate) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) TrustManager(javax.net.ssl.TrustManager) X509TrustManager(javax.net.ssl.X509TrustManager) HostnameVerifier(javax.net.ssl.HostnameVerifier) X509TrustManager(javax.net.ssl.X509TrustManager) SSLSocketFactory(javax.net.ssl.SSLSocketFactory)

Example 30 with HostnameVerifier

use of javax.net.ssl.HostnameVerifier in project jedis by xetorthio.

the class SSLJedisTest method connectWithShardInfoAndCustomHostnameVerifierByIpAddress.

/**
   * Tests opening an SSL/TLS connection to redis with a custom hostname
   * verifier. This test should fail because "127.0.0.1" does not match the
   * certificate subject common name and there are no subject alternative names
   * in the certificate.
   */
@Test
public void connectWithShardInfoAndCustomHostnameVerifierByIpAddress() {
    final URI uri = URI.create("rediss://127.0.0.1:6390");
    final SSLSocketFactory sslSocketFactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
    final SSLParameters sslParameters = new SSLParameters();
    HostnameVerifier hostnameVerifier = new BasicHostnameVerifier();
    JedisShardInfo shardInfo = new JedisShardInfo(uri, sslSocketFactory, sslParameters, hostnameVerifier);
    shardInfo.setPassword("foobared");
    Jedis jedis = new Jedis(shardInfo);
    try {
        jedis.get("foo");
        Assert.fail("The code did not throw the expected JedisConnectionException.");
    } catch (JedisConnectionException e) {
        Assert.assertEquals("The JedisConnectionException does not contain the expected message.", "The connection to '127.0.0.1' failed ssl/tls hostname verification.", e.getMessage());
    }
    try {
        jedis.close();
    } catch (Throwable e1) {
    // Expected.
    }
}
Also used : Jedis(redis.clients.jedis.Jedis) SSLParameters(javax.net.ssl.SSLParameters) JedisShardInfo(redis.clients.jedis.JedisShardInfo) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) URI(java.net.URI) JedisConnectionException(redis.clients.jedis.exceptions.JedisConnectionException) HostnameVerifier(javax.net.ssl.HostnameVerifier) Test(org.junit.Test)

Aggregations

HostnameVerifier (javax.net.ssl.HostnameVerifier)94 SSLSession (javax.net.ssl.SSLSession)41 SSLContext (javax.net.ssl.SSLContext)30 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)27 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)24 TrustManager (javax.net.ssl.TrustManager)19 IOException (java.io.IOException)18 URL (java.net.URL)18 X509Certificate (java.security.cert.X509Certificate)17 X509TrustManager (javax.net.ssl.X509TrustManager)17 Test (org.junit.Test)16 HttpURLConnection (java.net.HttpURLConnection)14 SecureRandom (java.security.SecureRandom)14 InputStream (java.io.InputStream)12 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)10 CertificateException (java.security.cert.CertificateException)10 SSLConnectionSocketFactory (org.apache.http.conn.ssl.SSLConnectionSocketFactory)10 KeyManagementException (java.security.KeyManagementException)9 ConnectionSocketFactory (org.apache.http.conn.socket.ConnectionSocketFactory)9 ByteArrayInputStream (java.io.ByteArrayInputStream)8