Example usage for javax.net.ssl SSLContext init

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

Introduction

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

Prototype

public final void init(KeyManager[] km, TrustManager[] tm, SecureRandom random) throws KeyManagementException 

Source Link

Document

Initializes this context.

Usage

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);
    this.setWebSocketFactory(new DefaultSSLWebSocketClientFactory(sslContext));

    myLogSigner = CcApi.getInstance(project).wsLogSigner();
    myConsoleView = consoleView;/*from w w  w .j  av  a  2  s .co m*/
}

From source file:estacionamento.util.HTTPUtil.java

/**
 *
 * @return/*from www  .j  a va 2s.  co m*/
 * @throws NoSuchAlgorithmException
 * @throws KeyManagementException
 */
public CloseableHttpClient createHttpClient() throws NoSuchAlgorithmException, KeyManagementException {
    TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
        @Override
        public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType)
                throws CertificateException {

        }

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

        }

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

    // Trust own CA and all self-signed certs
    SSLContext sslcontext = SSLContext.getInstance("TLS");
    sslcontext.init(null, trustAllCerts, new SecureRandom());

    // Allow TLSv1 protocol only
    SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[] { "TLSv1" },
            null, SSLConnectionSocketFactory.getDefaultHostnameVerifier());

    CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();

    return httpclient;
}

From source file:com.sparkplatform.api.core.HttpClientTest.java

@Test
public void testSSL() throws Exception {

    SSLContext sslContext = SSLContext.getInstance("TLS");
    sslContext.init(null, new TrustManager[] { new ConnectionApacheHttps.FullTrustManager() }, null);
    HttpClient c = new DefaultHttpClient();
    //SSLSocketFactory sf = new SSLSocketFactory(sslContext,SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
    SSLSocketFactory sf = SSLSocketFactory.getSocketFactory();
    @SuppressWarnings("deprecation")
    Scheme https = new Scheme("https", sf, 443);
    c.getConnectionManager().getSchemeRegistry().register(https);
    HttpHost h = new HttpHost("api.flexmls.com", 443, "https");

    HttpRequest r = new HttpGet("/v1/");
    HttpResponse rs = c.execute(h, r);//from  w  w  w .  ja  v  a  2s. co  m

    assertEquals(404, rs.getStatusLine().getStatusCode());
    String s = readString(rs.getEntity().getContent());
    assertEquals(s, "{\"D\":{\"Success\":false,\"Code\":404,\"Message\":\"Not Found\"}}");
}

From source file:com.ibm.iotf.client.AbstractClient.java

static SSLSocketFactory getSocketFactory(final String caCrtFile, final String crtFile, final String keyFile,
        final String password) throws IOException, KeyStoreException, NoSuchAlgorithmException,
        CertificateException, UnrecoverableKeyException, KeyManagementException {
    Security.addProvider(new BouncyCastleProvider());
    X509Certificate caCert = null;

    if (caCrtFile != null) {
        // load CA certificate
        PEMReader reader = new PEMReader(
                new InputStreamReader(new ByteArrayInputStream(Files.readAllBytes(Paths.get(caCrtFile)))));
        caCert = (X509Certificate) reader.readObject();
        reader.close();/*from   w w  w . j  ava2 s.  c o m*/
    } else {
        ClassLoader classLoader = AbstractClient.class.getClassLoader();
        PEMReader reader = new PEMReader(
                new InputStreamReader(classLoader.getResource(SERVER_MESSAGING_PEM).openStream()));
        caCert = (X509Certificate) reader.readObject();
        reader.close();
    }

    PEMReader reader = new PEMReader(
            new InputStreamReader(new ByteArrayInputStream(Files.readAllBytes(Paths.get(crtFile)))));
    X509Certificate cert = (X509Certificate) reader.readObject();
    reader.close();

    // load client private key
    reader = new PEMReader(
            new InputStreamReader(new ByteArrayInputStream(Files.readAllBytes(Paths.get(keyFile)))));
    KeyPair key = (KeyPair) reader.readObject();
    reader.close();

    TrustManagerFactory tmf = null;
    if (caCert != null) {
        // CA certificate is used to authenticate server
        KeyStore caKs = KeyStore.getInstance("JKS");
        //caKs.load(null, null);
        caKs.load(null, null);
        caKs.setCertificateEntry("ca-certificate", caCert);
        tmf = TrustManagerFactory.getInstance("PKIX");
        tmf.init(caKs);
    }
    // client key and certificates are sent to server so it can authenticate us
    KeyStore ks = KeyStore.getInstance("JKS");
    ks.load(null, null);
    ks.setCertificateEntry("certificate", cert);
    ks.setKeyEntry("private-key", key.getPrivate(), password.toCharArray(),
            new java.security.cert.Certificate[] { cert });
    KeyManagerFactory kmf = KeyManagerFactory.getInstance("PKIX");
    kmf.init(ks, password.toCharArray());

    // finally, create SSL socket factory
    SSLContext context = SSLContext.getInstance("TLSv1.2");
    if (tmf != null) {
        context.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
    } else {
        context.init(kmf.getKeyManagers(), null, null);
    }

    return context.getSocketFactory();
}

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);//from   ww w  .  java 2s. co  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.wudaosoft.net.httpclient.SSLContextBuilder.java

public SSLContext buildPKCS12() {

    Args.notEmpty(password, "password");
    Args.notNull(cert, "cert");

    char[] pwd = password.toCharArray();

    try {/*w  w w  .  j  ava2  s . co m*/
        KeyStore ks = KeyStore.getInstance("PKCS12");

        ks.load(cert.openStream(), pwd);

        //  & ?
        KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
        kmf.init(ks, pwd);

        //  SSLContext
        SSLContext sslContext = SSLContext.getInstance("TLS");
        sslContext.init(kmf.getKeyManagers(), null, new SecureRandom());

        return sslContext;
    } catch (Exception e) {
        if (e instanceof RuntimeException)
            throw (RuntimeException) e;
        throw new RuntimeException(e);
    }
}

From source file:com.lolay.android.security.OpenSSLSocketFactory.java

private SSLContext getContext() {
    if (context == null) {
        try {//from ww  w. j a v a2s .c o m
            SSLContext context = SSLContext.getInstance("SSL");
            context.init(null, new TrustManager[] { new OpenX509TrustManager() }, null);
            this.context = context;
        } catch (NoSuchAlgorithmException e) {
            //            throw new IOException(e);
        } catch (KeyManagementException e) {
            //            throw new IOException(e);
        }
    }

    return this.context;
}

From source file:com.vmware.aurora.vc.vcservice.TlsSocketFactory.java

private SSLContext createEasySSLContext() {
    try {//from  www . j a  va  2  s  .c  o  m
        SSLContext context = SSLContext.getInstance("TLS");
        context.init(null, trustManagers, null);
        return context;
    } catch (Exception e) {
        LOG.error(e.getMessage(), e);
        throw new HttpClientError(e.toString());
    }
}

From source file:com.wso2.identity.oauth.sample.OAuth2ServiceClient.java

private void enableSelfSignedCertificates(ServiceClient client) {
    String protocol = "SSL";
    try {//from   w ww. j  a v  a  2 s .c  o  m
        SSLContext sslCtx = SSLContext.getInstance(protocol);
        sslCtx.init(null, new TrustManager[] { new TrustAllTrustManager() }, null);
        client.getOptions().setProperty(HTTPConstants.CUSTOM_PROTOCOL_HANDLER,
                new Protocol("https", (ProtocolSocketFactory) new SSLProtocolSocketFactory(sslCtx), 443));
    } catch (KeyManagementException e) {
        throw new IllegalStateException("Key management exception for '" + protocol + "' SSL context", e);
    } catch (NoSuchAlgorithmException e) {
        throw new IllegalStateException("Unable to get SSL context for '" + protocol + "' protocol", e);
    }

}