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.gluu.oxtrust.ldap.service.LinktrackService.java

public String newLink(@NotEmpty String login, @NotEmpty String password, @NotEmpty String link) {
    TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            return null;
        }// w ww . j  av a2s . co m

        public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) {
        }

        public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) {
        }
    } };

    // 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) {
    }
    HttpClient httpclient = new DefaultHttpClient();
    HttpGet httpget = new HttpGet(String.format(CREATE_LINK_URL_PATTERN, login, password, link));
    HttpResponse response;
    try {
        response = httpclient.execute(httpget);
    } catch (Exception e) {
        log.error(String.format("Exception happened during linktrack link "
                + "creation with username: %s, password: %s," + " link: %s.", login, password, link), e);
        return null;
    }

    String trackedLink = null;
    if (response.getStatusLine().getStatusCode() == 201) {
        try {
            trackedLink = IOUtils.toString(response.getEntity().getContent());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return trackedLink;
}

From source file:org.akita.io._FakeSSLSocketFactory.java

private _FakeSSLSocketFactory() {
    super();//w  ww. ja  va  2 s .c  om
    TrustManager[] tm = new TrustManager[] { new _FakeX509TrustManager() };
    try {
        this.sslcontext = SSLContext.getInstance(SSLSocketFactory.TLS);
        this.sslcontext.init(null, tm, new SecureRandom());
        this.socketfactory = this.sslcontext.getSocketFactory();
    } catch (NoSuchAlgorithmException e) {
    } catch (KeyManagementException e) {
    }
}

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  . ja va 2s.  c o 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.gsf.dowload.nfe.HSProtocolSocketFactory.java

private SSLContext createSSLContext() {
    try {//from   w  w w .  j av a  2s .c  om
        KeyManager[] keyManagers = createKeyManagers();
        TrustManager[] trustManagers = createTrustManagers();
        SSLContext sslContext = SSLContext.getInstance("TLS");
        sslContext.init(keyManagers, trustManagers, null);

        return sslContext;
    } catch (KeyManagementException e) {
        Logger.getLogger(HSProtocolSocketFactory.class.getName()).log(Level.SEVERE, null, e);
    } catch (KeyStoreException e) {
        Logger.getLogger(HSProtocolSocketFactory.class.getName()).log(Level.SEVERE, null, e);
    } catch (NoSuchAlgorithmException e) {
        Logger.getLogger(HSProtocolSocketFactory.class.getName()).log(Level.SEVERE, null, e);
    } catch (CertificateException e) {
        Logger.getLogger(HSProtocolSocketFactory.class.getName()).log(Level.SEVERE, null, e);
    } catch (IOException e) {
        Logger.getLogger(HSProtocolSocketFactory.class.getName()).log(Level.SEVERE, null, e);
    }
    return null;
}

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

private SSLContext createEasySSLContext() {
    try {//from   w  w w .  jav a 2s.  co  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:at.diamonddogs.net.ssl.CustomSSLSocketFactory.java

private SSLContext createCustomSSLContext(KeyStore store) {
    try {//from w ww.  j av  a 2 s  . com
        TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        tmf.init(store);

        SSLContext context = SSLContext.getInstance("TLS");
        context.init(null, CustomX509TrustManager.getWrappedTrustmanager(tmf.getTrustManagers()), null);
        return context;
    } catch (Exception e) {
        LOGGER.error("unable to create ssl context", e);
        return null;
    }
}

From source file:keywhiz.cli.ClientUtils.java

/**
 * Creates a {@link OkHttpClient} to start a TLS connection.
 *
 * @param cookies list of cookies to include in the client.
 * @return new http client./*from w  ww .  j a  va 2 s . co m*/
 */
public static OkHttpClient sslOkHttpClient(List<HttpCookie> cookies) {
    checkNotNull(cookies);

    SSLContext sslContext;
    try {
        sslContext = SSLContext.getInstance("TLSv1.2");

        TrustManagerFactory trustManagerFactory = TrustManagerFactory
                .getInstance(TrustManagerFactory.getDefaultAlgorithm());
        trustManagerFactory.init((KeyStore) null);

        sslContext.init(new KeyManager[0], trustManagerFactory.getTrustManagers(), new SecureRandom());
    } catch (NoSuchAlgorithmException | KeyManagementException | KeyStoreException e) {
        throw Throwables.propagate(e);
    }

    SSLSocketFactory socketFactory = sslContext.getSocketFactory();

    OkHttpClient client = new OkHttpClient().setSslSocketFactory(socketFactory)
            .setConnectionSpecs(Arrays.asList(ConnectionSpec.MODERN_TLS)).setFollowSslRedirects(false);

    client.setRetryOnConnectionFailure(false);
    client.networkInterceptors().add(new XsrfTokenInterceptor("XSRF-TOKEN", "X-XSRF-TOKEN"));
    CookieManager cookieManager = new CookieManager();
    cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
    cookies.forEach(c -> cookieManager.getCookieStore().add(null, c));
    client.setCookieHandler(cookieManager);
    return client;
}

From source file:org.apache.falcon.resource.channel.SecureHTTPChannel.java

@Override
protected Client getClient() throws Exception {
    Properties properties = StartupProperties.get();
    String keyStoreFile = properties.getProperty("keystore.file", "conf/prism.keystore");
    String password = properties.getProperty("keystore.password", "falcon-prism-passwd");
    SSLContext sslContext = SSLContext.getInstance("SSL");
    sslContext.init(/*from   w  w  w .  j  a  v a  2  s  .  com*/
            new KeyManager[] { KeyManagerUtils.createClientKeyManager(new File(keyStoreFile), password) },
            new TrustManager[] { TrustManagerUtils.getValidateServerCertificateTrustManager() },
            new SecureRandom());
    DefaultClientConfig config = new DefaultClientConfig();
    config.getProperties().put(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES,
            new HTTPSProperties(new AllowAllHostnameVerifier(), sslContext));
    LOG.info("Configuring client with " + new File(keyStoreFile).getAbsolutePath());
    return Client.create(config);
}

From source file:com.collabnet.svnedge.net.SslProtocolSocketFactory.java

private SslProtocolSocketFactory() {
    try {//from  w  ww .j  av  a  2s .c  om
        SSLContext sslContext = SSLContext.getInstance("TLS");
        sslContext.init(null, new TrustManager[] { new TrustAllTrustManager() }, null);
        this.socketFactory = sslContext.getSocketFactory();
    } catch (Exception e) {
        log(0, "Could not initialize SSL context", e);
    }
}

From source file:com.allstate.client.ssl.SSLUtils.java

public static SSLSocketFactory getMergedSocketFactory(Security securityOne, Security securityTwo)
        throws GeneralSecurityException {
    X509KeyManager keyManagerOne = getKeyManager(securityOne.getKeyStore(), securityOne.getKeyStorePassword());
    X509KeyManager keyManagerTwo = getKeyManager(securityTwo.getKeyStore(), securityTwo.getKeyStorePassword());

    X509TrustManager trustManager = getMultiTrustManager(getTrustManager(securityOne.getTrustStore()),
            getTrustManager(securityTwo.getTrustStore()));

    SSLContext context = SSLContext.getInstance(securityOne.getSslContextProtocol());
    boolean strictHostVerification = securityOne.isStrictHostVerification()
            && securityTwo.isStrictHostVerification();

    context.init(new KeyManager[] { keyManagerOne, keyManagerTwo }, new TrustManager[] { trustManager },
            new SecureRandom());
    X509HostnameVerifier verifier = strictHostVerification ? SSLSocketFactory.STRICT_HOSTNAME_VERIFIER
            : SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
    return new SSLSocketFactory(context, verifier);
}