List of usage examples for javax.net.ssl SSLContext getInstance
public static SSLContext getInstance(String protocol) throws NoSuchAlgorithmException
From source file:ddf.security.common.util.CommonSSLFactory.java
/** * Creates a new SSLSocketFactory from a truststore and keystore. This is used during SSL * communication./*from w w w.j a v a 2 s .c o m*/ * * @param trustStoreLoc * File path to the truststore. * @param trustStorePass * Password to the truststore. * @param keyStoreLoc * File path to the keystore. * @param keyStorePass * Password to the keystore. * @return new SSLSocketFactory instance containing the trust and key stores. * @throws IOException */ public static SSLSocketFactory createSocket(String trustStoreLoc, String trustStorePass, String keyStoreLoc, String keyStorePass) throws IOException { String methodName = "createSocket"; logger.debug("ENTERING: " + methodName); try { logger.debug("trustStoreLoc = " + trustStoreLoc); FileInputStream trustFIS = new FileInputStream(trustStoreLoc); logger.debug("keyStoreLoc = " + keyStoreLoc); FileInputStream keyFIS = new FileInputStream(keyStoreLoc); // truststore stuff KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); try { logger.debug("Loading trustStore"); trustStore.load(trustFIS, trustStorePass.toCharArray()); } catch (CertificateException e) { throw new IOException("Unable to load certificates from truststore. " + trustStoreLoc, e); } finally { IOUtils.closeQuietly(trustFIS); } TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); tmf.init(trustStore); logger.debug("trust manager factory initialized"); // keystore stuff KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); try { logger.debug("Loading keyStore"); keyStore.load(keyFIS, keyStorePass.toCharArray()); } catch (CertificateException e) { throw new IOException("Unable to load certificates from keystore. " + keyStoreLoc, e); } finally { IOUtils.closeQuietly(keyFIS); } KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); kmf.init(keyStore, keyStorePass.toCharArray()); logger.debug("key manager factory initialized"); // ssl context SSLContext sslCtx = SSLContext.getInstance("TLS"); sslCtx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null); sslCtx.getDefaultSSLParameters().setNeedClientAuth(true); sslCtx.getDefaultSSLParameters().setWantClientAuth(true); logger.debug(exiting + methodName); return sslCtx.getSocketFactory(); } catch (KeyManagementException e) { logger.debug(exiting + methodName); throw new IOException("Unable to initialize the SSL context.", e); } catch (NoSuchAlgorithmException e) { logger.debug(exiting + methodName); throw new IOException( "Problems creating SSL socket. Usually this is " + "referring to the certificate sent by the server not being trusted by the client.", e); } catch (UnrecoverableKeyException e) { logger.debug(exiting + methodName); throw new IOException("Unable to load keystore. " + keyStoreLoc, e); } catch (KeyStoreException e) { logger.debug(exiting + methodName); throw new IOException("Unable to read keystore. " + keyStoreLoc, e); } }
From source file:com.owncloud.android.authenticator.EasySSLSocketFactory.java
private static SSLContext createEasySSLContext() { Log.d(TAG, "Creating Easy SSL Context"); try {/*from w w w . java2 s .com*/ SSLContext context = SSLContext.getInstance("TLS"); context.init(null, new TrustManager[] { new EasyX509TrustManager(null) }, null); return context; } catch (Exception er) { Log.e(TAG, er.getMessage() + ""); throw new HttpClientError(er.toString()); } }
From source file:com.wareninja.opensource.common.wsrequest.HttpUtils.java
/** * Open an URL connection. If HTTPS, accepts any certificate even if not * valid, and connects to any host name. * /*w w w. j av a 2s .c o m*/ * @param url * The destination URL, HTTP or HTTPS. * @return The URLConnection. * @throws IOException * @throws NoSuchAlgorithmException * @throws KeyManagementException */ public static URLConnection getConnection(URL url) throws IOException, NoSuchAlgorithmException, KeyManagementException { URLConnection conn = url.openConnection(); if (conn instanceof HttpsURLConnection) { // Trust all certificates SSLContext context = SSLContext.getInstance("TLS"); context.init(new KeyManager[0], TRUST_MANAGER, new SecureRandom()); SSLSocketFactory socketFactory = context.getSocketFactory(); ((HttpsURLConnection) conn).setSSLSocketFactory(socketFactory); // Allow all hostnames ((HttpsURLConnection) conn).setHostnameVerifier(HOSTNAME_VERIFIER); } conn.setConnectTimeout(SOCKET_TIMEOUT); conn.setReadTimeout(SOCKET_TIMEOUT); return conn; }
From source file:es.tsb.ltba.nomhad.httpclient.NomhadHttpClient.java
/** * Authentication//from w w w. java 2 s. c om * * @param base * the client to be configured * @return the authentication-enabled client */ private static DefaultHttpClient wrapClient(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; } }
From source file:cz.vity.freerapid.plugins.webclient.ssl.EasySSLProtocolSocketFactory.java
private static SSLContext createEasySSLContext() { try {//from w ww . j a va2s. com SSLContext context = SSLContext.getInstance("SSL"); context.init(null, new TrustManager[] { new EasyX509TrustManager(null) }, null); return context; } catch (Exception e) { LogUtils.processException(logger, e); throw new HttpClientError(e.toString()); } }
From source file:at.gv.egiz.bku.spring.SSLSocketFactoryBean.java
@Override public Object getObject() throws Exception { PKITrustManager pkiTrustManager = new PKITrustManager(); pkiTrustManager.setConfiguration(configurationFacade.configuration); pkiTrustManager.setPkiProfile(pkiProfile); SSLContext sslContext = SSLContext.getInstance(configurationFacade.getSslProtocol()); sslContext.init(null, new TrustManager[] { pkiTrustManager }, null); return sslContext.getSocketFactory(); }
From source file:org.jasig.portal.security.provider.saml.SSLSecurityImpl.java
public SSLSocketFactory getSSLSocketFactory() { try {//from ww w .ja v a 2s. c om //Implementation taken from SSLSocketFactory constructor with added support for a trust-all //trust store if no trustStore is explicitly configured and public keys are available KeyManager[] keymanagers = null; if (keyStore != null) { keymanagers = createKeyManagers(keyStore, keyStorePass); } TrustManager[] trustmanagers = null; if (trustStore != null) { trustmanagers = createTrustManagers(trustStore); } else if (publicKeys != null) { trustmanagers = new TrustManager[] { TrustAllX509TrustManager.INSTANCE }; } final SSLContext sslcontext = SSLContext.getInstance(SSLSocketFactory.TLS); sslcontext.init(keymanagers, trustmanagers, null); if (publicKeys != null) { return new PublicKeyVerifyingSSLSocketFactory(sslcontext, publicKeys); } return new SSLSocketFactory(sslcontext); } catch (Exception ex) { throw new DelegatedAuthenticationRuntimeException( "Error dealing with SSL. See stack trace for details.", ex); } }
From source file:de.undercouch.gradle.tasks.download.internal.DefaultHttpClientFactory.java
private SSLConnectionSocketFactory getInsecureSSLSocketFactory() { if (insecureSSLSocketFactory == null) { SSLContext sc;//from w ww. j a v a 2 s. c o m try { sc = SSLContext.getInstance("SSL"); sc.init(null, INSECURE_TRUST_MANAGERS, new SecureRandom()); insecureSSLSocketFactory = new SSLConnectionSocketFactory(sc, INSECURE_HOSTNAME_VERIFIER); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } catch (KeyManagementException e) { throw new RuntimeException(e); } } return insecureSSLSocketFactory; }
From source file:com.ericsson.gerrit.plugins.highavailability.forwarder.rest.HttpClientProvider.java
private static SSLContext buildSslContext() { try {/*from w ww . ja va 2s . c o m*/ TrustManager[] trustAllCerts = new TrustManager[] { new DummyX509TrustManager() }; SSLContext context = SSLContext.getInstance("TLS"); context.init(null, trustAllCerts, null); return context; } catch (KeyManagementException | NoSuchAlgorithmException e) { log.warn("Error building SSLContext object", e); return null; } }
From source file:com.thesocialcoin.networking.SSL.EasySSLSocketFactory.java
private static SSLContext createEasySSLContext() throws IOException { try {// w ww. ja va 2 s.c om // Client should authenticate itself with the valid certificate to Server. InputStream clientStream = App.getAppContext().getResources() .openRawResource(R.raw.production_test_client); char[] password = "XXXXXXXXXXXXX".toCharArray(); KeyStore keyStore = KeyStore.getInstance("PKCS12"); keyStore.load(clientStream, password); KeyManagerFactory keyManagerFactory = KeyManagerFactory .getInstance(KeyManagerFactory.getDefaultAlgorithm()); keyManagerFactory.init(keyStore, password); // Client should also add the CA certificate obtained from server and create TrustManager from it for the client to validate the // identity of the server. KeyStore trustStore = KeyStore.getInstance("BKS"); InputStream instream = null; instream = App.getAppContext().getResources().openRawResource(R.raw.production_test_ca); try { trustStore.load(instream, "XXXXXXXX".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 & Keystore SSLContext context = SSLContext.getInstance("TLS"); context.init(keyManagerFactory.getKeyManagers(), tmf.getTrustManagers(), null); return context; } catch (Exception e) { e.printStackTrace(); throw new IOException(e.getMessage()); } }