Search in sources :

Example 71 with HttpsURLConnection

use of javax.net.ssl.HttpsURLConnection in project ignite by apache.

the class TcpDiscoveryKubernetesIpFinder method getRegisteredAddresses.

/**
 * {@inheritDoc}
 */
@Override
public Collection<InetSocketAddress> getRegisteredAddresses() throws IgniteSpiException {
    init();
    Collection<InetSocketAddress> addrs = new ArrayList<>();
    try {
        if (log.isDebugEnabled())
            log.debug("Getting Apache Ignite endpoints from: " + url);
        HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
        conn.setHostnameVerifier(trustAllHosts);
        conn.setSSLSocketFactory(ctx.getSocketFactory());
        conn.addRequestProperty("Authorization", "Bearer " + serviceAccountToken(accountToken));
        // Sending the request and processing a response.
        ObjectMapper mapper = new ObjectMapper();
        Endpoints endpoints = mapper.readValue(conn.getInputStream(), Endpoints.class);
        if (endpoints != null) {
            if (endpoints.subsets != null && !endpoints.subsets.isEmpty()) {
                for (Subset subset : endpoints.subsets) {
                    if (subset.addresses != null && !subset.addresses.isEmpty()) {
                        for (Address address : subset.addresses) {
                            addrs.add(new InetSocketAddress(address.ip, 0));
                            if (log.isDebugEnabled())
                                log.debug("Added an address to the list: " + address.ip);
                        }
                    }
                }
            }
        }
    } catch (Exception e) {
        throw new IgniteSpiException("Failed to retrieve Ignite pods IP addresses.", e);
    }
    return addrs;
}
Also used : InetSocketAddress(java.net.InetSocketAddress) InetSocketAddress(java.net.InetSocketAddress) ArrayList(java.util.ArrayList) IgniteSpiException(org.apache.ignite.spi.IgniteSpiException) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) IgniteSpiException(org.apache.ignite.spi.IgniteSpiException) IgniteInterruptedCheckedException(org.apache.ignite.internal.IgniteInterruptedCheckedException) IOException(java.io.IOException)

Example 72 with HttpsURLConnection

use of javax.net.ssl.HttpsURLConnection in project aware-client by denzilferreira.

the class SSLManager method getRemoteCertificateExpiration.

/**
 * Based on https://www.experts-exchange.com/questions/27668989/Getting-SSL-Certificate-expiry-date.html
 * Improved to wait 5 seconds for the connection
 * @param url
 * @return
 */
public static Date getRemoteCertificateExpiration(URL url) {
    try {
        HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
        // 5 seconds to connect
        conn.setConnectTimeout(5000);
        // 10 seconds to acknowledge the response
        conn.setReadTimeout(10000);
        long now = System.currentTimeMillis();
        while (conn.getResponseCode() != HttpsURLConnection.HTTP_OK || now - System.currentTimeMillis() <= 5000) {
        // noop - wait up to 5 seconds to retrieve the certificate
        }
        // retrieve the N-length signing chain for the server certificates
        // certs[0] is the server's certificate
        // certs[1] - certs[N-1] are the intermediate authorities that signed the cert
        // certs[N] is the root certificate authority of the chain
        Certificate[] certs = conn.getServerCertificates();
        if (certs.length > 0 && certs[0] instanceof X509Certificate) {
            // certs[0] is an X.509 certificate, return its "notAfter" date
            return ((X509Certificate) certs[0]).getNotAfter();
        }
        // connection is not HTTPS or server is not signed with an X.509 certificate, return null
        return null;
    } catch (SSLPeerUnverifiedException spue) {
        // connection to server is not verified, unable to get certificates
        Log.d(Aware.TAG, "Certificates: " + spue.getMessage());
        return null;
    } catch (IllegalStateException ise) {
        // shouldn't get here -- indicates attempt to get certificates before
        // connection is established
        Log.d(Aware.TAG, "Certificates: " + ise.getMessage());
        return null;
    } catch (IOException ioe) {
        // error connecting to URL -- this must be caught last since
        // other exceptions are subclasses of IOException
        Log.d(Aware.TAG, "Certificates: " + ioe.getMessage());
        return null;
    }
}
Also used : SSLPeerUnverifiedException(javax.net.ssl.SSLPeerUnverifiedException) IOException(java.io.IOException) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) X509Certificate(java.security.cert.X509Certificate) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate)

Example 73 with HttpsURLConnection

use of javax.net.ssl.HttpsURLConnection in project cxf by apache.

the class SSLv3Test method testSSLv3ServerNotAllowedByDefault.

@org.junit.Test
public void testSSLv3ServerNotAllowedByDefault() throws Exception {
    SpringBusFactory bf = new SpringBusFactory();
    URL busFile = SSLv3Test.class.getResource("sslv3-client.xml");
    Bus bus = bf.createBus(busFile.toString());
    BusFactory.setDefaultBus(bus);
    BusFactory.setThreadDefaultBus(bus);
    System.setProperty("https.protocols", "SSLv3");
    URL service = new URL("https://localhost:" + PORT);
    HttpsURLConnection connection = (HttpsURLConnection) service.openConnection();
    connection.setHostnameVerifier(new DisableCNCheckVerifier());
    SSLContext sslContext = SSLContext.getInstance("SSL");
    KeyStore trustedCertStore = KeyStore.getInstance("jks");
    try (InputStream keystore = ClassLoaderUtils.getResourceAsStream("keys/Truststore.jks", SSLv3Test.class)) {
        trustedCertStore.load(keystore, null);
    }
    TrustManagerFactory tmf = TrustManagerFactory.getInstance("PKIX");
    tmf.init(trustedCertStore);
    TrustManager[] trustManagers = tmf.getTrustManagers();
    sslContext.init(null, trustManagers, new java.security.SecureRandom());
    connection.setSSLSocketFactory(sslContext.getSocketFactory());
    try {
        connection.connect();
        fail("Failure expected on an SSLv3 connection attempt");
    } catch (IOException ex) {
    // expected
    }
    System.clearProperty("https.protocols");
    bus.shutdown(true);
}
Also used : Bus(org.apache.cxf.Bus) InputStream(java.io.InputStream) SSLContext(javax.net.ssl.SSLContext) IOException(java.io.IOException) KeyStore(java.security.KeyStore) URL(java.net.URL) TrustManager(javax.net.ssl.TrustManager) SpringBusFactory(org.apache.cxf.bus.spring.SpringBusFactory) TrustManagerFactory(javax.net.ssl.TrustManagerFactory) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Example 74 with HttpsURLConnection

use of javax.net.ssl.HttpsURLConnection in project cxf by apache.

the class SSLv3Test method testSSLv3ServerAllowed.

@org.junit.Test
public void testSSLv3ServerAllowed() throws Exception {
    // Doesn't work with IBM JDK
    if ("IBM Corporation".equals(System.getProperty("java.vendor"))) {
        return;
    }
    SpringBusFactory bf = new SpringBusFactory();
    URL busFile = SSLv3Test.class.getResource("sslv3-client.xml");
    Bus bus = bf.createBus(busFile.toString());
    BusFactory.setDefaultBus(bus);
    BusFactory.setThreadDefaultBus(bus);
    System.setProperty("https.protocols", "SSLv3");
    URL service = new URL("https://localhost:" + PORT2);
    HttpsURLConnection connection = (HttpsURLConnection) service.openConnection();
    connection.setHostnameVerifier(new DisableCNCheckVerifier());
    SSLContext sslContext = SSLContext.getInstance("SSL");
    KeyStore trustedCertStore = KeyStore.getInstance("jks");
    try (InputStream keystore = ClassLoaderUtils.getResourceAsStream("keys/Truststore.jks", SSLv3Test.class)) {
        trustedCertStore.load(keystore, null);
    }
    TrustManagerFactory tmf = TrustManagerFactory.getInstance("PKIX");
    tmf.init(trustedCertStore);
    TrustManager[] trustManagers = tmf.getTrustManagers();
    sslContext.init(null, trustManagers, new java.security.SecureRandom());
    connection.setSSLSocketFactory(sslContext.getSocketFactory());
    connection.connect();
    connection.disconnect();
    System.clearProperty("https.protocols");
    bus.shutdown(true);
}
Also used : Bus(org.apache.cxf.Bus) InputStream(java.io.InputStream) SSLContext(javax.net.ssl.SSLContext) KeyStore(java.security.KeyStore) URL(java.net.URL) TrustManager(javax.net.ssl.TrustManager) SpringBusFactory(org.apache.cxf.bus.spring.SpringBusFactory) TrustManagerFactory(javax.net.ssl.TrustManagerFactory) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Example 75 with HttpsURLConnection

use of javax.net.ssl.HttpsURLConnection 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)

Aggregations

HttpsURLConnection (javax.net.ssl.HttpsURLConnection)522 URL (java.net.URL)310 IOException (java.io.IOException)177 HttpURLConnection (java.net.HttpURLConnection)128 InputStreamReader (java.io.InputStreamReader)93 InputStream (java.io.InputStream)89 Test (org.junit.Test)83 BufferedReader (java.io.BufferedReader)78 SSLContext (javax.net.ssl.SSLContext)70 OutputStream (java.io.OutputStream)54 HostnameVerifier (javax.net.ssl.HostnameVerifier)50 MalformedURLException (java.net.MalformedURLException)48 URLConnection (java.net.URLConnection)47 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)47 ByteArrayOutputStream (java.io.ByteArrayOutputStream)46 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)37 HashMap (java.util.HashMap)34 DataOutputStream (java.io.DataOutputStream)32 KeyManagementException (java.security.KeyManagementException)32 JSONObject (org.json.JSONObject)29