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.keycloak.truststore.JSSETruststoreConfigurator.java

public javax.net.ssl.SSLSocketFactory getSSLSocketFactory() {
    if (provider == null) {
        return null;
    }//from w w  w. j a  v a2s.  co m

    if (sslFactory == null) {
        synchronized (this) {
            if (sslFactory == null) {
                try {
                    SSLContext sslctx = SSLContext.getInstance("TLS");
                    sslctx.init(null, getTrustManagers(), null);
                    sslFactory = sslctx.getSocketFactory();
                } catch (Exception e) {
                    throw new RuntimeException("Failed to initialize SSLContext: ", e);
                }
            }
        }
    }
    return sslFactory;
}

From source file:com.servoy.j2db.util.SecuritySupport.java

public static SSLContext getSSLContext(Properties settings) throws Exception {

    // set up key manager to do server authentication
    SSLContext ctx = SSLContext.getInstance("TLS"); //$NON-NLS-1$
    KeyManagerFactory kmf = null;
    try {/*from   w  w  w .  j av a  2s  .c om*/
        kmf = KeyManagerFactory.getInstance("SunX509"); //$NON-NLS-1$
    } catch (Exception e) {
        Debug.log("couldn't get SunX509, now trying ibm");
        kmf = KeyManagerFactory.getInstance("IbmX509"); //$NON-NLS-1$
    }

    initKeyStoreAndPassphrase(settings);

    kmf.init(keyStore, passphrase);
    ctx.init(kmf.getKeyManagers(), null, null);

    return ctx;

}

From source file:com.intel.cosbench.client.http.HttpClientUtil.java

@SuppressWarnings({ "deprecation" })
private static SSLSocketFactory createSSLSocketFactory() {
    try {/*  ww w.j av a  2 s .  c o  m*/
        SSLContext ctx = SSLContext.getInstance("TLS");
        X509TrustManager tm = new X509TrustManager() {
            @Override
            public void checkClientTrusted(X509Certificate[] xcs, String string) throws CertificateException {
            }

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

            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }
        };
        ctx.init(null, new X509TrustManager[] { tm }, null);
        String[] enabled = { "SSL_RSA_WITH_NULL_MD5", "SSL_RSA_WITH_NULL_SHA" };
        ctx.createSSLEngine().setEnabledCipherSuites(enabled);

        SSLSocketFactory ssf = new SSLSocketFactory(ctx);
        ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

        return ssf;
    } catch (Exception ex) {
        ex.printStackTrace();
        return null;
    }
}

From source file:com.grantedbyme.example.ServletUtils.java

public static GrantedByMe getSDK(HttpServlet context) throws IOException {
    // read private key
    String privateKey = null;/* ww  w  .j a  va  2  s . c o m*/
    InputStream privateKeyInputStream = context.getClass().getResourceAsStream("/private_key.pem");
    try {
        privateKey = IOUtils.toString(privateKeyInputStream);
    } finally {
        privateKeyInputStream.close();
    }
    // read server key
    String serverKey = null;
    InputStream serverKeyInputStream = context.getClass().getResourceAsStream("/server_key.pem");
    try {
        serverKey = IOUtils.toString(serverKeyInputStream);
    } finally {
        serverKeyInputStream.close();
    }
    // _log(serverKey);
    // initialize BouncyCastle security provider
    Security.insertProviderAt(new org.bouncycastle.jce.provider.BouncyCastleProvider(), 0);
    // create sdk
    GrantedByMe sdk = new GrantedByMe(privateKey, serverKey);
    // Create a trust manager that does not validate certificate chains
    TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            return null;
        }

        public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) {
            //No need to implement.
        }

        public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) {
            //No need to implement.
        }
    } };
    // Install the all-trusting trust manager
    try {
        SSLContext sc = SSLContext.getInstance("SSL");
        sc.init(null, trustAllCerts, new java.security.SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
    } catch (Exception e) {
        e.printStackTrace();
    }
    // set SDK parameters
    sdk.apiURL = "https://api-dev.grantedby.me/v1/service/";
    //sdk.isDebug = true;
    return sdk;
}

From source file:com.cleverCloud.cleverIdea.utils.WebSocketCore.java

public WebSocketCore(URI uri, @NotNull Project project, ConsoleView consoleView)
        throws NoSuchAlgorithmException, KeyManagementException {
    super(uri, new Draft_10(), null, 0);

    SSLContext sslContext = SSLContext.getInstance("TLS");
    sslContext.init(null, null, null);/*from   w w  w  . jav a2s.c  om*/
    this.setWebSocketFactory(new DefaultSSLWebSocketClientFactory(sslContext));

    myLogSigner = CcApi.getInstance(project).wsLogSigner();
    myConsoleView = consoleView;
}

From source file:org.everit.osgi.authentication.cas.tests.SecureHttpClient.java

public SecureHttpClient(final String principal, final BundleContext bundleContext) throws Exception {
    this.principal = principal;

    httpClientContext = HttpClientContext.create();
    httpClientContext.setCookieStore(new BasicCookieStore());

    KeyStore trustStore = KeyStore.getInstance("jks");
    trustStore.load(bundleContext.getBundle().getResource("/jetty-keystore").openStream(),
            "changeit".toCharArray());

    TrustManagerFactory trustManagerFactory = TrustManagerFactory
            .getInstance(TrustManagerFactory.getDefaultAlgorithm());
    trustManagerFactory.init(trustStore);
    TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();

    SSLContext sslContext = SSLContext.getInstance("TLS");
    sslContext.init(null, trustManagers, new SecureRandom());

    httpClient = HttpClientBuilder.create().setSslcontext(sslContext)
            .setRedirectStrategy(new DefaultRedirectStrategy()).build();
}

From source file:com.wiyun.engine.network.TrustAllSSLSocketFactory.java

public TrustAllSSLSocketFactory()
        throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException, UnrecoverableKeyException {
    super(null);/* w w  w . j av a  2s  . c  o m*/
    try {
        SSLContext sslcontext = SSLContext.getInstance("TLS");
        sslcontext.init(null, new TrustManager[] { new TrustAllManager() }, null);
        factory = sslcontext.getSocketFactory();
        setHostnameVerifier(new AllowAllHostnameVerifier());
    } catch (Exception ex) {
    }
}

From source file:com.mani.fileupload.http.EasySSLSocketFactory.java

private static SSLContext createEasySSLContext() throws IOException {
    try {//from   w ww. j  av a  2s . c o  m

        // Client should send the valid key to Server 
        InputStream clientStream = null;
        char[] password = null;

        clientStream = FileUploadApplication.getContext().getResources().openRawResource(R.raw.client);
        password = "fileupload".toCharArray();

        KeyStore keyStore = KeyStore.getInstance("PKCS12");
        keyStore.load(clientStream, password);

        KeyManagerFactory keyManagerFactory = KeyManagerFactory
                .getInstance(KeyManagerFactory.getDefaultAlgorithm());
        keyManagerFactory.init(keyStore, password);

        // CA key obtained from server.
        KeyStore trustStore = KeyStore.getInstance("BKS");
        InputStream instream = null;
        instream = FileUploadApplication.getContext().getResources().openRawResource(R.raw.ca);

        try {
            trustStore.load(instream, "casecret".toCharArray());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                instream.close();
            } catch (Exception ignore) {
            }
        }

        String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
        TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
        tmf.init(trustStore);

        // Create an SSLContext that uses our TrustManager
        SSLContext context = SSLContext.getInstance("TLS");
        context.init(keyManagerFactory.getKeyManagers(), tmf.getTrustManagers(), null);

        return context;
    } catch (Exception e) {
        e.printStackTrace();
        throw new IOException(e.getMessage());
    }
}

From source file:org.ksoap2.transport.ServiceConnectionSE.java

public ServiceConnectionSE(String url) throws IOException {
    // //from ww w  .  ja  va2s  .  c  om
    try {
        SSLContext sContext = SSLContext.getInstance("SSL");
        sContext.init(null, trustAllCerts, new java.security.SecureRandom());

        HttpsURLConnection.setDefaultSSLSocketFactory(sContext.getSocketFactory());
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (KeyManagementException e) {
        e.printStackTrace();
    }

    connection = (HttpsURLConnection) new URL(url).openConnection();
    ((HttpsURLConnection) connection).setHostnameVerifier(new AllowAllHostnameVerifier());
    connection.setUseCaches(false);
    connection.setDoOutput(true);
    connection.setDoInput(true);
}