List of usage examples for javax.net.ssl SSLContext init
public final void init(KeyManager[] km, TrustManager[] tm, SecureRandom random) throws KeyManagementException
From source file:com.jms.notify.utils.httpclient.SimpleHttpUtils.java
/** * * @param urlConn/*from ww w . j a va 2 s .c om*/ * @param sslVerify * @param hostnameVerify * @param trustCertFactory * @param clientKeyFactory */ private static void setSSLSocketFactory(HttpURLConnection urlConn, boolean sslVerify, boolean hostnameVerify, TrustKeyStore trustCertFactory, ClientKeyStore clientKeyFactory) { try { SSLSocketFactory socketFactory = null; if (trustCertFactory != null || clientKeyFactory != null || !sslVerify) { SSLContext sc = SSLContext.getInstance("SSL"); TrustManager[] trustManagers = null; KeyManager[] keyManagers = null; if (trustCertFactory != null) { trustManagers = trustCertFactory.getTrustManagerFactory().getTrustManagers(); } if (clientKeyFactory != null) { keyManagers = clientKeyFactory.getKeyManagerFactory().getKeyManagers(); } if (!sslVerify) { trustManagers = trustAnyManagers; hostnameVerify = false; } sc.init(keyManagers, trustManagers, new java.security.SecureRandom()); socketFactory = sc.getSocketFactory(); } if (urlConn instanceof HttpsURLConnection) { HttpsURLConnection httpsUrlCon = (HttpsURLConnection) urlConn; if (socketFactory != null) { httpsUrlCon.setSSLSocketFactory(socketFactory); } //??hostname if (!hostnameVerify) { httpsUrlCon.setHostnameVerifier(new TrustAnyHostnameVerifier()); } } if (urlConn instanceof com.sun.net.ssl.HttpsURLConnection) { com.sun.net.ssl.HttpsURLConnection httpsUrlCon = (com.sun.net.ssl.HttpsURLConnection) urlConn; if (socketFactory != null) { httpsUrlCon.setSSLSocketFactory(socketFactory); } //??hostname if (!hostnameVerify) { httpsUrlCon.setHostnameVerifier(new TrustAnyHostnameVerifierOld()); } } } catch (Exception e) { logger.error(e.getMessage(), e); } }
From source file:com.qpark.eip.core.spring.security.https.HttpsRequester.java
@PostConstruct public void init() throws Exception { if (this.trustManager == null) { // HTTP AUTH if (this.httpAuthUser != null && this.httpAuthUser.length() > 0) { this.httpAuthBase64 = new String(Base64.encode(new StringBuffer(256).append(this.httpAuthUser) .append(":").append(this.httpAuthPwd == null ? "" : this.httpAuthPwd).toString() .getBytes("UTF-8")), "UTF-8"); }//from w w w . ja va 2 s . co m // Keystore handler trust manager Resource keystore = null; if (this.keystoreSource == null) { Assert.isNull(this.keystoreSource); } else { if (this.keystoreSource.startsWith("classpath:")) { keystore = new ClassPathResource(this.keystoreSource); } else { keystore = new FileSystemResource(this.keystoreSource); } } if (keystore == null) { Assert.isNull(keystore); } this.trustManager = new EipX509TrustManager(); this.trustManager.setKeystore(keystore); this.trustManager.setKeystorePassword(new String(this.keystorePwd)); this.trustManager.init(); } // SSL Context SSLContext ctx = SSLContext.getInstance("TLS"); ctx.init(null, new TrustManager[] { this.trustManager }, null); SSLContext.setDefault(ctx); }
From source file:org.mifos.module.sms.provider.RestAdapterProvider.java
@SuppressWarnings("unused") public OkHttpClient createClient() { final OkHttpClient client = new OkHttpClient(); final TrustManager[] certs = new TrustManager[] { new X509TrustManager() { @Override//from ww w. jav a 2 s. co m public X509Certificate[] getAcceptedIssuers() { return null; } @Override public void checkServerTrusted(final X509Certificate[] chain, final String authType) throws CertificateException { } @Override public void checkClientTrusted(final X509Certificate[] chain, final String authType) throws CertificateException { } } }; SSLContext ctx = null; try { ctx = SSLContext.getInstance("TLS"); ctx.init(null, certs, new SecureRandom()); } catch (final java.security.GeneralSecurityException ex) { } try { final HostnameVerifier hostnameVerifier = new HostnameVerifier() { @Override public boolean verify(final String hostname, final SSLSession session) { return true; } }; client.setHostnameVerifier(hostnameVerifier); client.setSslSocketFactory(ctx.getSocketFactory()); } catch (final Exception e) { } return client; }
From source file:org.getcomposer.core.packagist.Downloader.java
private void registerSSLContext(HttpClient client) { try {/*from w w w . ja v a 2 s. c o m*/ X509TrustManager tm = new ComposerTrustManager(); SSLContext ctx = SSLContext.getInstance("TLS"); ctx.init(null, new TrustManager[] { tm }, null); SSLSocketFactory ssf = new SSLSocketFactory(ctx, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); ClientConnectionManager ccm = client.getConnectionManager(); SchemeRegistry sr = ccm.getSchemeRegistry(); sr.register(new Scheme("https", 443, ssf)); } catch (Exception e) { // TODO: handle exception } }
From source file:org.xdi.net.SslDefaultHttpClient.java
private SSLSocketFactory newSslSocketFactory() { try {/*w ww . j a va 2 s. c om*/ TrustManager[] trustManagers = this.trustManagers; if (useTrustManager) { trustManagers = getTrustManagers(); } KeyManager[] keyManagers = null; if (useKeyManager) { keyManagers = getKeyManagers(); } SSLContext ctx = SSLContext.getInstance("TLS"); ctx.init(keyManagers, trustManagers, new SecureRandom()); // Pass the keystore to the SSLSocketFactory SSLSocketFactory sf = new SSLSocketFactory(ctx, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); return sf; } catch (Exception ex) { throw new IllegalArgumentException("Failed to load keystore", ex); } }
From source file:talkeeg.httpserver.HttpServer.java
private NHttpConnectionFactory<DefaultNHttpServerConnection> createConnectionFactory() { NHttpConnectionFactory<DefaultNHttpServerConnection> connFactory; if (config.isUseTLS()) { try {//from www. j a v a 2 s. c om KeyStore keystore = KeyStore.getInstance("jks"); char[] password = new char[0]; keystore.load(null, password); final X509Certificate certificate = certManager.getCertificate(OwnedKeyType.USER); KeyStore.PrivateKeyEntry entry = new KeyStore.PrivateKeyEntry( ownedKeysManager.getPrivateKey(OwnedKeyType.USER), new Certificate[] { certificate }); keystore.setEntry("", entry, new KeyStore.PasswordProtection(password)); KeyManagerFactory kmfactory = KeyManagerFactory .getInstance(KeyManagerFactory.getDefaultAlgorithm()); kmfactory.init(keystore, password); final KeyManager[] keymanagers = kmfactory.getKeyManagers(); SSLContext sslcontext = SSLContext.getInstance("TLS"); sslcontext.init(keymanagers, null, null); connFactory = new SSLNHttpServerConnectionFactory(sslcontext, null, ConnectionConfig.DEFAULT); } catch (Exception e) { throw new RuntimeException("Can not initialise SSL.", e); } } else { connFactory = new DefaultNHttpServerConnectionFactory(ConnectionConfig.DEFAULT); } return connFactory; }
From source file:org.pluroid.pluroium.HttpClientFactory.java
/** * Constructor/*from w ww . j av a 2 s . c o m*/ */ public MySSLSocketFactory() { if (m_sslSocketFactory == null) { try { SSLContext sc = SSLContext.getInstance("TLS"); sc.init(null, null, null); m_sslSocketFactory = sc.getSocketFactory(); } catch (Exception ex) { } } }