Search in sources :

Example 6 with HostnameVerifier

use of javax.net.ssl.HostnameVerifier in project OpenGrok by OpenGrok.

the class Query method createHttpsUrlConnection.

private HttpsURLConnection createHttpsUrlConnection(URL url) {
    try {
        System.setProperty("jsse.enableSNIExtension", "false");
        TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {

            @Override
            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                return null;
            }

            @Override
            public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) {
            }

            @Override
            public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) {
            }
        } };
        SSLContext sc = SSLContext.getInstance("SSL");
        sc.init(null, trustAllCerts, new java.security.SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
        HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {

            @Override
            public boolean verify(String arg0, SSLSession arg1) {
                return true;
            }
        });
        return (HttpsURLConnection) url.openConnection();
    } catch (Exception ex) {
        handleException(ex);
    }
    return null;
}
Also used : SSLSession(javax.net.ssl.SSLSession) SSLContext(javax.net.ssl.SSLContext) ParseException(org.json.simple.parser.ParseException) TrustManager(javax.net.ssl.TrustManager) X509TrustManager(javax.net.ssl.X509TrustManager) HostnameVerifier(javax.net.ssl.HostnameVerifier) X509TrustManager(javax.net.ssl.X509TrustManager) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Example 7 with HostnameVerifier

use of javax.net.ssl.HostnameVerifier in project jersey by jersey.

the class HttpUrlConnector method secureConnection.

/**
     * Secure connection if necessary.
     * <p/>
     * Provided implementation sets {@link HostnameVerifier} and {@link SSLSocketFactory} to give connection, if that
     * is an instance of {@link HttpsURLConnection}.
     *
     * @param client client associated with this client runtime.
     * @param uc     http connection to be secured.
     */
protected void secureConnection(final JerseyClient client, final HttpURLConnection uc) {
    if (uc instanceof HttpsURLConnection) {
        HttpsURLConnection suc = (HttpsURLConnection) uc;
        final HostnameVerifier verifier = client.getHostnameVerifier();
        if (verifier != null) {
            suc.setHostnameVerifier(verifier);
        }
        if (HttpsURLConnection.getDefaultSSLSocketFactory() == suc.getSSLSocketFactory()) {
            // indicates that the custom socket factory was not set
            suc.setSSLSocketFactory(sslSocketFactory.get());
        }
    }
}
Also used : HttpsURLConnection(javax.net.ssl.HttpsURLConnection) HostnameVerifier(javax.net.ssl.HostnameVerifier)

Example 8 with HostnameVerifier

use of javax.net.ssl.HostnameVerifier in project jersey by jersey.

the class SslFilterTest method testCustomHostameVerificationFail.

@Test
public void testCustomHostameVerificationFail() throws Throwable {
    CountDownLatch latch = new CountDownLatch(1);
    SslEchoServer server = new SslEchoServer();
    try {
        server.start();
        HostnameVerifier verifier = new HostnameVerifier() {

            @Override
            public boolean verify(String s, SSLSession sslSession) {
                return false;
            }
        };
        openClientSocket("localhost", ByteBuffer.allocate(0), latch, verifier);
        fail();
    } catch (SSLException e) {
    // expected
    } finally {
        server.stop();
    }
}
Also used : SSLSession(javax.net.ssl.SSLSession) CountDownLatch(java.util.concurrent.CountDownLatch) SSLException(javax.net.ssl.SSLException) HostnameVerifier(javax.net.ssl.HostnameVerifier) Test(org.junit.Test)

Example 9 with HostnameVerifier

use of javax.net.ssl.HostnameVerifier in project apjp by jvansteirteghem.

the class HTTPSRequest method open.

public void open() throws HTTPSRequestException {
    try {
        url = new URL(APJP.APJP_REMOTE_HTTPS_SERVER_REQUEST_URL[i]);
        Proxy proxy = Proxy.NO_PROXY;
        if (url.getProtocol().equalsIgnoreCase("HTTP") == true) {
            if (APJP.APJP_HTTP_PROXY_SERVER_ADDRESS.equalsIgnoreCase("") == false) {
                proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(APJP.APJP_HTTP_PROXY_SERVER_ADDRESS, APJP.APJP_HTTP_PROXY_SERVER_PORT));
            }
        } else {
            if (url.getProtocol().equalsIgnoreCase("HTTPS") == true) {
                if (APJP.APJP_HTTPS_PROXY_SERVER_ADDRESS.equalsIgnoreCase("") == false) {
                    proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(APJP.APJP_HTTPS_PROXY_SERVER_ADDRESS, APJP.APJP_HTTPS_PROXY_SERVER_PORT));
                }
            }
        }
        urlConnection = url.openConnection(proxy);
        if (urlConnection instanceof HttpsURLConnection) {
            ((HttpsURLConnection) urlConnection).setHostnameVerifier(new HostnameVerifier() {

                public boolean verify(String hostname, SSLSession sslSession) {
                    String value1 = APJP.APJP_REMOTE_HTTPS_SERVER_REQUEST_URL[i];
                    String[] values1 = value1.split("/", -1);
                    String value2 = values1[2];
                    String[] values2 = value2.split(":");
                    String value3 = values2[0];
                    if (value3.equalsIgnoreCase(hostname)) {
                        return true;
                    } else {
                        return false;
                    }
                }
            });
        }
        if (url.getProtocol().equalsIgnoreCase("HTTP") == true) {
            if (APJP.APJP_HTTP_PROXY_SERVER_ADDRESS.equalsIgnoreCase("") == false && APJP.APJP_HTTP_PROXY_SERVER_USERNAME.equalsIgnoreCase("") == false) {
                urlConnection.setRequestProperty("Proxy-Authorization", "Basic " + new String(BASE64.encode((APJP.APJP_HTTP_PROXY_SERVER_USERNAME + ":" + APJP.APJP_HTTP_PROXY_SERVER_PASSWORD).getBytes())));
            }
        } else {
            if (url.getProtocol().equalsIgnoreCase("HTTPS") == true) {
                if (APJP.APJP_HTTPS_PROXY_SERVER_ADDRESS.equalsIgnoreCase("") == false && APJP.APJP_HTTPS_PROXY_SERVER_USERNAME.equalsIgnoreCase("") == false) {
                    urlConnection.setRequestProperty("Proxy-Authorization", "Basic " + new String(BASE64.encode((APJP.APJP_HTTPS_PROXY_SERVER_USERNAME + ":" + APJP.APJP_HTTPS_PROXY_SERVER_PASSWORD).getBytes())));
                }
            }
        }
        for (int j = 0; j < APJP.APJP_REMOTE_HTTPS_SERVER_REQUEST_PROPERTY_KEY[i].length; j = j + 1) {
            if (APJP.APJP_REMOTE_HTTPS_SERVER_REQUEST_PROPERTY_KEY[i][j].equalsIgnoreCase("") == false) {
                urlConnection.setRequestProperty(APJP.APJP_REMOTE_HTTPS_SERVER_REQUEST_PROPERTY_KEY[i][j], APJP.APJP_REMOTE_HTTPS_SERVER_REQUEST_PROPERTY_VALUE[i][j]);
            }
        }
        urlConnection.setDoOutput(true);
        urlConnection.setDoInput(true);
        urlConnection.connect();
    } catch (Exception e) {
        throw new HTTPSRequestException("HTTPS_REQUEST/OPEN", e);
    }
}
Also used : Proxy(java.net.Proxy) InetSocketAddress(java.net.InetSocketAddress) SSLSession(javax.net.ssl.SSLSession) URL(java.net.URL) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) HostnameVerifier(javax.net.ssl.HostnameVerifier)

Example 10 with HostnameVerifier

use of javax.net.ssl.HostnameVerifier in project custom-cert-https by nelenkov.

the class MainActivity method urlConnConnect.

private void urlConnConnect() {
    new GetHtmlTask() {

        @Override
        protected String doInBackground(Void... arg0) {
            try {
                boolean useClientAuth = useClientAuthCb.isChecked();
                SSLContext sslCtx = createSslContext(useClientAuth);
                URL url = new URL(useClientAuth ? CLIENT_AUTH_URL : SERVER_AUTH_URL);
                HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
                urlConnection.setUseCaches(false);
                urlConnection.setRequestProperty("Connection", "close");
                urlConnection.setConnectTimeout(TIMEOUT);
                urlConnection.setReadTimeout(TIMEOUT);
                urlConnection.setSSLSocketFactory(sslCtx.getSocketFactory());
                HostnameVerifier verifier = urlConnection.getHostnameVerifier();
                Log.d(TAG, "hostname verifier: " + verifier.getClass().getName());
                try {
                    urlConnection.connect();
                    if (urlConnection.getResponseCode() != HttpURLConnection.HTTP_OK) {
                        return urlConnection.getResponseMessage();
                    }
                    return readLines(urlConnection.getInputStream(), urlConnection.getContentEncoding());
                } finally {
                    urlConnection.disconnect();
                }
            } catch (Exception e) {
                Log.d(TAG, "Error: " + e.getMessage(), e);
                error = e;
                return null;
            }
        }
    }.execute();
}
Also used : SSLContext(javax.net.ssl.SSLContext) URL(java.net.URL) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) GeneralSecurityException(java.security.GeneralSecurityException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) HostnameVerifier(javax.net.ssl.HostnameVerifier) BrowserCompatHostnameVerifier(org.apache.http.conn.ssl.BrowserCompatHostnameVerifier)

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