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.obiba.mica.config.ssl.SslContextFactoryImpl.java

@Override
public SSLContext createSslContext() {
    KeyStoreManager keystore = prepareServerKeystore();
    try {// w  w  w. ja va  2  s.  c o  m
        SSLContext ctx = SSLContext.getInstance("TLSv1.2");
        ctx.init(new KeyManager[] { new X509ExtendedKeyManagerImpl(keystore) }, null, null);
        return ctx;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:ru.musicplayer.androidclient.network.EasySSLSocketFactory.java

private static SSLContext createEasySSLContext() throws IOException {
    try {// w  w  w .ja v  a 2s .  com
        SSLContext context = SSLContext.getInstance("TLS");
        context.init(null, new TrustManager[] { new EasyTrustManager(null) }, null);
        return context;
    } catch (Exception e) {
        throw new IOException(e.getMessage());
    }
}

From source file:ti.modules.titanium.network.NonValidatingSSLSocketFactory.java

public NonValidatingSSLSocketFactory() {
    try {// w  w w .  j a  v  a2s  .c  om
        SSLContext context = SSLContext.getInstance("TLS");
        TrustManager managers[] = new TrustManager[] { new NonValidatingTrustManager() };
        context.init(null, managers, new SecureRandom());
        sslFactory = context.getSocketFactory();
    } catch (Exception e) {
        Log.e(TAG, e.getMessage(), e);
    }

}

From source file:com.linkedin.pinot.monitor.util.HttpUtils.java

/**
 * {/* ww  w  .  j av  a  2 s. c  o m*/
 text: "",
 attachments: [{
 title: "",
 description: "??",
 url: "",
 color: "warning|info|primary|error|muted|success"
 }]
 displayUser: {
 name: "??",
 avatarUrl: "??"
 }
 }
 * @param text
 * @return
 */

public static void postMonitorData(String text) {
    SSLContext sslContext = null;
    HttpClient client = new DefaultHttpClient();
    try {
        sslContext = SSLContext.getInstance("SSL");
        sslContext.init(null, new TrustManager[] { new X509TrustManager() {
            @Override
            public void checkClientTrusted(java.security.cert.X509Certificate[] x509Certificates, String s)
                    throws java.security.cert.CertificateException {

            }

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

            }

            @Override
            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }
        } }, new SecureRandom());
    } catch (Exception e) {
        e.printStackTrace();
    }

    SSLSocketFactory ssf = new SSLSocketFactory(sslContext, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
    ClientConnectionManager ccm = client.getConnectionManager();
    SchemeRegistry sr = ccm.getSchemeRegistry();
    sr.register(new Scheme("https", 443, ssf));

    HttpPost httpPost = new HttpPost("https://hooks.pubu.im/services/1d2d2rwn8wb6sx");

    Map<String, Object> map = new HashMap<String, Object>();
    Map<String, String> sender = new HashMap<String, String>();
    sender.put("name", "Monitor");
    map.put("displayUser", sender);
    List<String> list = new ArrayList<String>();
    map.put("attachments", list);

    try {
        map.put("text", text);
        InputStreamEntity httpentity = new InputStreamEntity(
                new ByteArrayInputStream(mapper.writeValueAsBytes(map)), mapper.writeValueAsBytes(map).length);
        httpPost.setEntity(httpentity);
        httpPost.addHeader("Content-Type", "application/json");
        HttpResponse response = client.execute(httpPost);
        String result = EntityUtils.toString(response.getEntity());
        System.out.println(result);

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        //
    }

}

From source file:com.payu.sdk.helper.WebClientDevWrapper.java

/**
 * Wraps a default and secure httpClient
 *
 * @param base the original httpClient/*from  w w w  .  ja  v a 2  s .c  o m*/
 * @return the hhtpClient wrapped
 * @throws ConnectionException
 */
public static HttpClient wrapClient(HttpClient base) throws ConnectionException {
    try {
        SSLContext ctx = SSLContext.getInstance(Constants.SSL_PROVIDER);

        X509TrustManager tm = new X509TrustManager() {

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

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

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

        };

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

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

        ClientConnectionManager ccm = base.getConnectionManager();
        SchemeRegistry sr = ccm.getSchemeRegistry();

        sr.register(new Scheme("https", Constants.HTTPS_PORT, ssf));
        return new DefaultHttpClient(ccm, base.getParams());
    } catch (Exception ex) {
        throw new ConnectionException("Invalid SSL connection", ex);
    }
}

From source file:at.bitfire.davdroid.webdav.TlsSniSocketFactory.java

private static SSLContext initializedContext(SSLContext sslContext) {
    verifyAndroidContextSet();//from  w  w w . j  a v  a2  s  .  c  o  m
    if (sslContext == null) {
        try {
            sslContext = SSLContext.getInstance("TLS");
        } catch (NoSuchAlgorithmException e) {
            Log.wtf(TAG, "TLS not supported: " + e.getMessage());
            throw new RuntimeException("No support for TLS!");
        }
    }
    try {
        sslContext.init(null, MemorizingTrustManager.getInstanceList(androidContext), null);
    } catch (KeyManagementException e) {
        Log.wtf(TAG, "Ignoring unexpected KeyManagementException: " + e.getMessage());
    }
    return sslContext;
}

From source file:dtu.ds.warnme.app.ws.client.https.ssl.SimpleSSLSocketFactory.java

@SuppressLint("TrulyRandom")
public SimpleSSLSocketFactory(KeyStore truststore)
        throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, UnrecoverableKeyException {
    super(truststore);
    sslContext = SSLContext.getInstance("SSL");
    x509TrustManager = new SimpleX509TrustManager();
    sslContext.init(null, new TrustManager[] { x509TrustManager }, new SecureRandom());
}

From source file:com.jaspersoft.ireport.jasperserver.ws.IReportSSLSocketFactory.java

private static SSLContext createIRSSLContext() {

    try {//w w w  . ja va  2 s .co  m

        SSLContext context = SSLContext.getInstance("SSL");

        context.init(null, new TrustManager[] { new IReportTrustManager() }, null);

        return context;

    } catch (Exception ex) {

        ex.printStackTrace();

        throw new HttpClientError(ex.toString());

    }

}

From source file:info.guardianproject.cacert.CustomTrust.java

public CustomTrust(Context context, int rawResource, String password) throws IOException, KeyStoreException,
        KeyManagementException, NoSuchAlgorithmException, CertificateException {

    // Setup the SSL context to use the truststore
    ssl_ctx = SSLContext.getInstance("TLS");

    // Setup truststore
    KeyStore ksCACert = KeyStore.getInstance("BKS");
    TrustManagerFactory trustManagerFactory = TrustManagerFactory
            .getInstance(TrustManagerFactory.getDefaultAlgorithm());
    InputStream trustStoreStream = context.getResources().openRawResource(rawResource);
    ksCACert.load(trustStoreStream, password.toCharArray());

    //init factory with custom cacert
    trustManagerFactory.init(ksCACert);/*from w w w  . j  a v  a  2  s  .c o m*/
    Log.d("SSL", "CACerts " + ksCACert.size());
    Log.d("SSL", "trustManagerFactory " + trustManagerFactory.getTrustManagers().length);

    // Setup client keystore
    /*
    KeyStore keyStore = KeyStore.getInstance("BKS");
    KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
    InputStream keyStoreStream = context.getResources().openRawResource(R.raw.clientkeystore);
    keyStore.load(keyStoreStream, "testtest".toCharArray());
    keyManagerFactory.init(keyStore, "testtest".toCharArray());
    Log.d("SSL", "Key " + keyStore.size());
            
    Log.d("SSL", "keyManagerFactory " + keyManagerFactory.getKeyManagers().length);
    */

    //nothing implemented yet
    SecureRandom secRand = SecureRandom.getInstance(RANDOM_ALGORITHM);

    ssl_ctx.init(null, trustManagerFactory.getTrustManagers(), secRand);

    socketFactory = (SSLSocketFactory) ssl_ctx.getSocketFactory();

}

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

/**
 * Returns https ready client./*from w w w.j a  va  2 s . co m*/
 *
 * @param base
 * @return
 */
public static HttpClient wrapHttpsClient(HttpClient base) {
    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);
        SSLSocketFactory ssf = new SSLSocketFactory(ctx, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        ClientConnectionManager ccm = base.getConnectionManager();
        SchemeRegistry sr = ccm.getSchemeRegistry();
        sr.register(new Scheme("https", 443, ssf));
        return new DefaultHttpClient(ccm, base.getParams());
    } catch (Exception ex) {
        ex.printStackTrace();
        return null;
    }
}