Search in sources :

Example 36 with HostnameVerifier

use of javax.net.ssl.HostnameVerifier in project OpenAM by OpenRock.

the class AMSetupUtils method openConnection.

private static HttpURLConnection openConnection(String urlString) throws IOException {
    URL url = new URL(urlString);
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    if (url.getProtocol().equals(HTTPS)) {
        HttpsURLConnection sslConnection = (HttpsURLConnection) connection;
        sslConnection.setHostnameVerifier(new HostnameVerifier() {

            public boolean verify(String hostname, SSLSession session) {
                return true;
            }
        });
    }
    return connection;
}
Also used : HttpURLConnection(java.net.HttpURLConnection) SSLSession(javax.net.ssl.SSLSession) URL(java.net.URL) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) HostnameVerifier(javax.net.ssl.HostnameVerifier)

Example 37 with HostnameVerifier

use of javax.net.ssl.HostnameVerifier in project CloudStack-archive by CloudStack-extras.

the class VmwareContext method getHTTPConnection.

public HttpURLConnection getHTTPConnection(String urlString, String httpMethod) throws Exception {
    String cookieString = getServiceCookie();
    HostnameVerifier hv = new HostnameVerifier() {

        public boolean verify(String urlHostName, SSLSession session) {
            return true;
        }
    };
    HttpsURLConnection.setDefaultHostnameVerifier(hv);
    URL url = new URL(urlString);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setDoInput(true);
    conn.setDoOutput(true);
    conn.setAllowUserInteraction(true);
    conn.setRequestProperty(org.apache.axis.transport.http.HTTPConstants.HEADER_COOKIE, cookieString);
    conn.setRequestMethod(httpMethod);
    connectWithRetry(conn);
    return conn;
}
Also used : HttpURLConnection(java.net.HttpURLConnection) SSLSession(javax.net.ssl.SSLSession) URL(java.net.URL) HostnameVerifier(javax.net.ssl.HostnameVerifier)

Example 38 with HostnameVerifier

use of javax.net.ssl.HostnameVerifier in project cubrid-manager by CUBRID.

the class ClientHttp method setUpConnection.

/**
	 * Set up a http client
	 *
	 * @throws UnknownHostException a possible exception
	 * @throws IOException a possible exception
	 */
private void setUpConnection() {
    tearDownConnection();
    this.requestUrl = "https://" + hostAddress + ":" + port + METHOD;
    // support https
    try {
        // KeyStore trustStore =
        // KeyStore.getInstance(KeyStore.getDefaultType());
        // instream = new FileInputStream(new File("cm.keystore"));
        // trustStore.load(instream, "admin1".toCharArray());
        // SSLSocketFactory socketFactory = new
        // SSLSocketFactory(trustStore);
        // Scheme sch = new Scheme("https", 443, socketFactory);
        // this.httpClient.getConnectionManager().getSchemeRegistry().register(sch);
        X509TrustManager tm = new X509TrustManager() {

            public void checkClientTrusted(X509Certificate[] xcs, String string) throws CertificateException {
            }

            public void checkServerTrusted(X509Certificate[] xcs, String string) throws CertificateException {
            }

            public X509Certificate[] getAcceptedIssuers() {
                return new X509Certificate[0];
            }
        };
        SSLContext ctx = SSLContext.getInstance("TLS");
        ctx.init(null, new TrustManager[] { tm }, new SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(ctx.getSocketFactory());
        HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {

            public boolean verify(String hostname, SSLSession session) {
                return true;
            }
        });
        URL url = new URL(requestUrl);
        conn = (HttpsURLConnection) url.openConnection();
        conn.setRequestMethod("POST");
        conn.setConnectTimeout(timeout);
        conn.setDoInput(true);
        conn.setDoOutput(true);
        conn.setRequestProperty("Content-Type", "application/json");
    } catch (Exception e) {
        LOGGER.error("Make to support HTTPS failed.", e);
    }
}
Also used : X509TrustManager(javax.net.ssl.X509TrustManager) SSLSession(javax.net.ssl.SSLSession) SecureRandom(java.security.SecureRandom) SSLContext(javax.net.ssl.SSLContext) X509Certificate(java.security.cert.X509Certificate) URL(java.net.URL) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) UnknownHostException(java.net.UnknownHostException) HostnameVerifier(javax.net.ssl.HostnameVerifier)

Example 39 with HostnameVerifier

use of javax.net.ssl.HostnameVerifier in project geode by apache.

the class ConnectCommandWithHttpAndSSLDUnitTest method connect.

@Override
protected void connect(final String host, final int jmxPort, final int httpPort, final HeadlessGfsh shell) {
    assertNotNull(host);
    assertNotNull(shell);
    final CommandStringBuilder command = new CommandStringBuilder(CONNECT);
    String endpoint;
    // This is for testing purpose only. If we remove this piece of code we will
    // get a java.security.cert.CertificateException
    // as matching hostname can not be obtained in all test environment.
    HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {

        @Override
        public boolean verify(String string, SSLSession ssls) {
            return true;
        }
    });
    endpoint = "https://" + host + ":" + httpPort + urlContext + "/v1";
    command.addOption(CONNECT__USE_HTTP, Boolean.TRUE.toString());
    command.addOption(CONNECT__URL, endpoint);
    command.addOption(CONNECT__USE_SSL, Boolean.TRUE.toString());
    if (sslInfoHolder.get().getProperty(CONNECT__KEY_STORE) != null) {
        command.addOption(CONNECT__KEY_STORE, sslInfoHolder.get().getProperty(CONNECT__KEY_STORE));
    }
    if (sslInfoHolder.get().getProperty(CONNECT__KEY_STORE_PASSWORD) != null) {
        command.addOption(CONNECT__KEY_STORE_PASSWORD, sslInfoHolder.get().getProperty(CONNECT__KEY_STORE_PASSWORD));
    }
    if (sslInfoHolder.get().getProperty(CONNECT__TRUST_STORE) != null) {
        command.addOption(CONNECT__TRUST_STORE, sslInfoHolder.get().getProperty(CONNECT__TRUST_STORE));
    }
    if (sslInfoHolder.get().getProperty(CONNECT__TRUST_STORE_PASSWORD) != null) {
        command.addOption(CONNECT__TRUST_STORE_PASSWORD, sslInfoHolder.get().getProperty(CONNECT__TRUST_STORE_PASSWORD));
    }
    if (sslInfoHolder.get().getProperty(CONNECT__SSL_PROTOCOLS) != null) {
        command.addOption(CONNECT__SSL_PROTOCOLS, sslInfoHolder.get().getProperty(CONNECT__SSL_PROTOCOLS));
    }
    if (sslInfoHolder.get().getProperty(CONNECT__SSL_CIPHERS) != null) {
        command.addOption(CONNECT__SSL_CIPHERS, sslInfoHolder.get().getProperty(CONNECT__SSL_CIPHERS));
    }
    CommandResult result = executeCommand(shell, command.toString());
    if (!shell.isConnectedAndReady()) {
        fail("Connect command failed to connect to manager " + endpoint + " result=" + commandResultToString(result));
    }
    info("Successfully connected to managing node using HTTPS");
    assertEquals(true, shell.isConnectedAndReady());
}
Also used : CommandStringBuilder(org.apache.geode.management.internal.cli.util.CommandStringBuilder) SSLSession(javax.net.ssl.SSLSession) HostnameVerifier(javax.net.ssl.HostnameVerifier) CommandResult(org.apache.geode.management.internal.cli.result.CommandResult)

Example 40 with HostnameVerifier

use of javax.net.ssl.HostnameVerifier in project maven-plugins by apache.

the class ProjectInfoReportUtils method getURLConnection.

/**
     * @param url not null
     * @param project not null
     * @param settings not null
     * @return the url connection with auth if required. Don't check the certificate if SSL scheme.
     * @throws IOException if any
     */
private static URLConnection getURLConnection(URL url, MavenProject project, Settings settings) throws IOException {
    URLConnection conn = url.openConnection();
    conn.setConnectTimeout(TIMEOUT);
    conn.setReadTimeout(TIMEOUT);
    //@formatter:off
    if (settings.getServers() != null && !settings.getServers().isEmpty() && project != null && project.getDistributionManagement() != null && (project.getDistributionManagement().getRepository() != null || project.getDistributionManagement().getSnapshotRepository() != null) && (StringUtils.isNotEmpty(project.getDistributionManagement().getRepository().getUrl()) || StringUtils.isNotEmpty(project.getDistributionManagement().getSnapshotRepository().getUrl()))) //@formatter:on
    {
        Server server = null;
        if (url.toString().contains(project.getDistributionManagement().getRepository().getUrl())) {
            server = settings.getServer(project.getDistributionManagement().getRepository().getId());
        }
        if (server == null && url.toString().contains(project.getDistributionManagement().getSnapshotRepository().getUrl())) {
            server = settings.getServer(project.getDistributionManagement().getSnapshotRepository().getId());
        }
        if (server != null && StringUtils.isNotEmpty(server.getUsername()) && StringUtils.isNotEmpty(server.getPassword())) {
            String up = server.getUsername().trim() + ":" + server.getPassword().trim();
            String upEncoded = new String(Base64.encodeBase64Chunked(up.getBytes())).trim();
            conn.setRequestProperty("Authorization", "Basic " + upEncoded);
        }
    }
    if (conn instanceof HttpsURLConnection) {
        HostnameVerifier hostnameverifier = new HostnameVerifier() {

            /** {@inheritDoc} */
            public boolean verify(String urlHostName, SSLSession session) {
                return true;
            }
        };
        ((HttpsURLConnection) conn).setHostnameVerifier(hostnameverifier);
        TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {

            /** {@inheritDoc} */
            public void checkClientTrusted(final X509Certificate[] chain, final String authType) {
            }

            /** {@inheritDoc} */
            public void checkServerTrusted(final X509Certificate[] chain, final String authType) {
            }

            /** {@inheritDoc} */
            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }
        } };
        try {
            SSLContext sslContext = SSLContext.getInstance("SSL");
            sslContext.init(null, trustAllCerts, new SecureRandom());
            SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
            ((HttpsURLConnection) conn).setSSLSocketFactory(sslSocketFactory);
        } catch (NoSuchAlgorithmException e1) {
        // ignore
        } catch (KeyManagementException e) {
        // ignore
        }
    }
    return conn;
}
Also used : Server(org.apache.maven.settings.Server) SSLSession(javax.net.ssl.SSLSession) SecureRandom(java.security.SecureRandom) SSLContext(javax.net.ssl.SSLContext) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) URLConnection(java.net.URLConnection) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) X509Certificate(java.security.cert.X509Certificate) KeyManagementException(java.security.KeyManagementException) HostnameVerifier(javax.net.ssl.HostnameVerifier) TrustManager(javax.net.ssl.TrustManager) X509TrustManager(javax.net.ssl.X509TrustManager) X509TrustManager(javax.net.ssl.X509TrustManager) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

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