Example usage for javax.net.ssl SSLContext getInstance

List of usage examples for javax.net.ssl SSLContext getInstance

Introduction

In this page you can find the example usage for javax.net.ssl SSLContext getInstance.

Prototype

public static SSLContext getInstance(String protocol) throws NoSuchAlgorithmException 

Source Link

Document

Returns a SSLContext object that implements the specified secure socket protocol.

Usage

From source file:org.apache.falcon.request.BaseRequest.java

public static SSLContext getSslContext() throws Exception {
    SSLContext sslContext = SSLContext.getInstance("SSL");
    sslContext.init(null, new TrustManager[] { TrustManagerUtils.getValidateServerCertificateTrustManager() },
            new SecureRandom());
    return sslContext;
}

From source file:hochschuledarmstadt.photostream_tools.AndroidSocket.java

public static SSLContext createSslContext() throws KeyManagementException, NoSuchAlgorithmException {

    SSLContext sslContext = SSLContext.getInstance("TLS");

    TrustManager tm = new X509TrustManager() {

        public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
        }//from  w  w w . j a  v a 2s  .com

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

        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }
    };

    sslContext.init(null, new TrustManager[] { tm }, null);

    return sslContext;
}

From source file:org.openremote.android.console.net.SelfCertificateSSLSocketFactory.java

/**
 * Creates a new SelfCertificateSSLSocket object.
 * /*from w  ww  .j  a  v a  2 s. c  o m*/
 * @return the SSL context
 * 
 * @throws IOException Signals that an I/O exception has occurred.
 */
private static SSLContext createEasySSLContext(Context context) throws IOException {
    TrustManager easyTrustManager = new X509TrustManager() {
        @Override
        public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
        }

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

        @Override
        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }
    };

    try {
        ORKeyStore keystore = ORKeyStore.getInstance(context);
        KeyManager[] managers = null;

        //keystore.fillKeyStore();
        //keystore.saveKeyStore();

        if (!keystore.isEmpty()) {
            KeyManagerFactory keyManagerFactory = KeyManagerFactory
                    .getInstance(KeyManagerFactory.getDefaultAlgorithm());
            keyManagerFactory.init(keystore.getKeyStore(), "password".toCharArray());

            managers = keyManagerFactory.getKeyManagers();
        }

        SSLContext sslcontext = SSLContext.getInstance("TLS");
        sslcontext.init(managers, new TrustManager[] { easyTrustManager }, null);
        return sslcontext;
    } catch (Exception e) {
        throw new IOException(e.getMessage());
    }
}

From source file:com.camel.trainreserve.JDKHttpsClient.java

public static ByteArrayOutputStream doGetImg(String url, String cookieStr) {
    InputStream in = null;//from w w  w  .jav  a  2  s  .c o  m
    ByteArrayOutputStream outStream = null;
    try {
        SSLContext sc = SSLContext.getInstance("SSL");
        sc.init(null, new TrustManager[] { new DefaultTrustManager() }, new SecureRandom());

        URL console = new URL(url);
        HttpsURLConnection conn = (HttpsURLConnection) console.openConnection();
        conn.setRequestProperty("Cookie", cookieStr);
        conn.setSSLSocketFactory(sc.getSocketFactory());
        conn.setHostnameVerifier(new TrustAnyHostnameVerifier());
        conn.connect();
        in = conn.getInputStream();
        outStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len = 0;
        while ((len = in.read(buffer)) != -1) {
            outStream.write(buffer, 0, len);
        }
        conn.disconnect();
    } catch (NoSuchAlgorithmException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (KeyManagementException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        try {
            in.close();
        } catch (Exception e) {
        }
    }
    return outStream;
}

From source file:com.terradue.warhol.auth.ssl.SslAuthenticationConfiguration.java

@Override
public void configure(Builder httpClientConfig, SslAuthentication authentication) {
    // client//from   ww  w  . jav a2 s. co  m
    KeyManager[] keyManagers;
    if (authentication.getProxyCertificate() != null) {
        keyManagers = fromSslProxy(authentication.getProxyCertificate());
    } else if (authentication.getPublicCertificate() != null && authentication.getPrivateKey() != null) {
        keyManagers = fromSslKeyAndCertificate(authentication.getPublicCertificate(),
                authentication.getPrivateKey(), authentication.getPassword());
    } else {
        keyManagers = new KeyManager[] {};
    }

    // server
    TrustManager[] trustManagers;
    if (authentication.isCheckCertificate()) {
        trustManagers = new TrustManager[] {};
    } else {
        trustManagers = new TrustManager[] { new RelaxedTrustManager() };
    }

    SSLContext context = null;
    try {
        context = SSLContext.getInstance("TLS");
        context.init(keyManagers, trustManagers, null);
        httpClientConfig.setSSLContext(context);
    } catch (Exception e) {
        throw new IllegalStateException("Impossible to initialize SSL context", e);
    }
}

From source file:com.denimgroup.threadfix.remote.AcceptAllTrustManager.java

private SSLContext createAcceptAllSSLContext() {
    try {/*from   w  w w.  j a v a 2s . co  m*/
        AcceptAllTrustManager acceptAllTrustManager = new AcceptAllTrustManager();
        SSLContext context = SSLContext.getInstance("TLS");
        context.init(null, new AcceptAllTrustManager[] { acceptAllTrustManager }, null);
        return context;
    } catch (KeyManagementException e) {
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:org.jboss.as.test.http.util.TestHttpClientUtils.java

/**
 *@param credentialsProvider optional cred provider
 * @return client that doesn't verify https connections
 *//*w  ww  .  j  a v a2s  . c o  m*/
public static CloseableHttpClient getHttpsClient(CredentialsProvider credentialsProvider) {
    try {
        SSLContext ctx = SSLContext.getInstance("TLS");
        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 null;
            }
        };
        ctx.init(null, new TrustManager[] { tm }, null);

        ctx.init(null, new TrustManager[] { tm }, null);

        SSLConnectionSocketFactory sslConnectionFactory = new SSLConnectionSocketFactory(ctx,
                new NoopHostnameVerifier());

        Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
                .register("https", sslConnectionFactory).build();
        HttpClientConnectionManager ccm = new BasicHttpClientConnectionManager(registry);
        HttpClientBuilder builder = HttpClientBuilder.create().setSSLSocketFactory(sslConnectionFactory)
                .setSSLHostnameVerifier(new NoopHostnameVerifier()).setConnectionManager(ccm);

        if (credentialsProvider != null) {
            builder.setDefaultCredentialsProvider(credentialsProvider);
        }
        return builder.build();
    } catch (Exception ex) {
        ex.printStackTrace();
        return null;
    }
}

From source file:it.infn.mw.iam.config.X509TrustConfig.java

@Bean
public SSLContext sslContext() {

    try {// w  w  w.  ja  v a  2s.  com
        SSLContext context = SSLContext.getInstance("TLSv1");

        X509TrustManager tm = SocketFactoryCreator.getSSLTrustManager(certificateValidator());
        SecureRandom r = new SecureRandom();
        context.init(null, new TrustManager[] { tm }, r);

        return context;

    } catch (NoSuchAlgorithmException | KeyManagementException e) {
        throw new RuntimeException(e);
    }

}

From source file:eu.prestoprime.p4gui.connection.P4HttpClient.java

public P4HttpClient(String userID) {
    HttpParams params = new BasicHttpParams();

    // setup SSL/*from w  w  w.j  av  a  2s  .  com*/
    try {
        SSLContext sslcontext = SSLContext.getInstance("TLS");
        sslcontext.init(null, new TrustManager[] { easyTrustManager }, null);

        SSLSocketFactory sf = new SSLSocketFactory(sslcontext, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        params.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 1000L);
        SSLSocket socket = (SSLSocket) sf.createSocket(params);
        socket.setEnabledCipherSuites(new String[] { "SSL_RSA_WITH_RC4_128_MD5" });

        Scheme sch = new Scheme("https", 443, sf);
        this.getConnectionManager().getSchemeRegistry().register(sch);
    } catch (IOException | KeyManagementException | NoSuchAlgorithmException e) {
        logger.error("Unable to create SSL handler for HttpClient...");
        e.printStackTrace();
    }

    // save userID
    this.userID = userID;
}

From source file:com.odoo.core.rpc.http.OdooSafeClient.java

private static SSLSocketFactory getSecureConnectionSetting() {
    TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
        @Override//from ww w.  j a v  a 2s .c  o  m
        public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType)
                throws java.security.cert.CertificateException {

        }

        @Override
        public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType)
                throws java.security.cert.CertificateException {

        }

        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            return new java.security.cert.X509Certificate[] {};
        }

    } };
    SSLSocketFactory ssf = null;
    try {
        SSLContext sc = SSLContext.getInstance("TLS");
        sc.init(null, trustAllCerts, null);
        ssf = new OSSLSocketFactory(sc);
        ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
    } catch (Exception ea) {
        ea.printStackTrace();
    }
    return ssf;
}