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:org.jboss.as.test.http.util.HttpClientUtils.java
/** * Returns https ready client.//w w w . j a v a 2s.c o 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; } }
From source file:com.servoy.j2db.util.SecuritySupport.java
public static SSLContext getSSLContext(Properties settings) throws Exception { // set up key manager to do server authentication SSLContext ctx = SSLContext.getInstance("TLS"); //$NON-NLS-1$ KeyManagerFactory kmf = null; try {/*ww w . ja v a 2 s . co m*/ kmf = KeyManagerFactory.getInstance("SunX509"); //$NON-NLS-1$ } catch (Exception e) { Debug.log("couldn't get SunX509, now trying ibm"); kmf = KeyManagerFactory.getInstance("IbmX509"); //$NON-NLS-1$ } initKeyStoreAndPassphrase(settings); kmf.init(keyStore, passphrase); ctx.init(kmf.getKeyManagers(), null, null); return ctx; }
From source file:com.simiacryptus.util.Util.java
/** * Cache input stream./*from www . ja va2s . c om*/ * * @param url the url * @param file the file * @return the input stream * @throws IOException the io exception * @throws NoSuchAlgorithmException the no such algorithm exception * @throws KeyStoreException the key store exception * @throws KeyManagementException the key management exception */ public static InputStream cache(String url, String file) throws IOException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException { if (new File(file).exists()) { return new FileInputStream(file); } else { TrustManager[] trustManagers = { new X509TrustManager() { public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[0]; } public void checkClientTrusted(X509Certificate[] certs, String authType) { } public void checkServerTrusted(X509Certificate[] certs, String authType) { } } }; SSLContext ctx = SSLContext.getInstance("TLS"); ctx.init(null, trustManagers, null); SSLSocketFactory sslFactory = ctx.getSocketFactory(); URLConnection urlConnection = new URL(url).openConnection(); if (urlConnection instanceof javax.net.ssl.HttpsURLConnection) { HttpsURLConnection conn = (HttpsURLConnection) urlConnection; conn.setSSLSocketFactory(sslFactory); conn.setRequestMethod("GET"); } InputStream inputStream = urlConnection.getInputStream(); FileOutputStream cache = new FileOutputStream(file); return new TeeInputStream(inputStream, cache); } }
From source file:com.liferay.sync.engine.session.Session.java
public static void setTrustManagers(TrustManager[] trustManagers) throws Exception { SSLContextBuilder sslContextBuilder = SSLContexts.custom(); SSLContext sslContext = sslContextBuilder.build(); sslContext.init(null, trustManagers, new SecureRandom()); _defaultSSLSocketFactory = new SSLConnectionSocketFactory(sslContext, SSLConnectionSocketFactory.getDefaultHostnameVerifier()); }
From source file:com.vmware.identity.openidconnect.client.OIDCClientUtils.java
static HttpResponse sendSecureRequest(HttpRequest httpRequest, KeyStore keyStore) throws OIDCClientException, SSLConnectionException { Validate.notNull(httpRequest, "httpRequest"); Validate.notNull(keyStore, "keyStore"); TrustManagerFactory trustManagerFactory; SSLContext sslContext; try {//from ww w .j a v a2s . c o m trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); trustManagerFactory.init(keyStore); sslContext = SSLContext.getInstance("SSL"); sslContext.init(null, trustManagerFactory.getTrustManagers(), null); } catch (NoSuchAlgorithmException | KeyStoreException | KeyManagementException e) { throw new SSLConnectionException("Failed to build SSL Context: " + e.getMessage(), e); } return sendSecureRequest(httpRequest, sslContext); }
From source file:edu.gmu.isa681.server.Server.java
/** * Creates a TLS server socket factory using the key store and key store password provided to the JVM at runtime. * @return/*from w w w.j a v a 2 s .c o m*/ * @throws GeneralSecurityException If an error occurs while creating the TLS factory. * @throws IOException If an error occurs while reading the key store. * * Adapted from Oracle JSSE docs. */ private static SSLServerSocketFactory getSSLServerSocketFactory() throws GeneralSecurityException, IOException { FileInputStream fis = null; try { SSLServerSocketFactory ssf = null; // set up key manager to do server authentication SSLContext ctx = SSLContext.getInstance("TLS"); KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509"); KeyStore ks = KeyStore.getInstance("JKS"); String keyStore = System.getProperty("javax.net.ssl.keyStore"); String keyStorePassword = System.getProperty("javax.net.ssl.keyStorePassword"); fis = new FileInputStream(keyStore); ks.load(fis, keyStorePassword.toCharArray()); kmf.init(ks, keyStorePassword.toCharArray()); ctx.init(kmf.getKeyManagers(), null, null); ssf = ctx.getServerSocketFactory(); return ssf; } finally { Utils.closeQuitely(fis); } }
From source file:es.tsb.ltba.nomhad.httpclient.NomhadHttpClient.java
/** * Authentication// ww w . j ava 2s . c o m * * @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:com.simiacryptus.util.Util.java
/** * Get input stream./*from w ww . j ava 2 s.c o m*/ * * @param url the url * @return the input stream * @throws NoSuchAlgorithmException the no such algorithm exception * @throws KeyManagementException the key management exception * @throws IOException the io exception */ public static InputStream get(@javax.annotation.Nonnull String url) throws NoSuchAlgorithmException, KeyManagementException, IOException { @javax.annotation.Nonnull final TrustManager[] trustManagers = { new X509TrustManager() { @Override public void checkClientTrusted(final X509Certificate[] certs, final String authType) { } @Override public void checkServerTrusted(final X509Certificate[] certs, final String authType) { } @javax.annotation.Nonnull @Override public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[0]; } } }; @javax.annotation.Nonnull final SSLContext ctx = SSLContext.getInstance("TLS"); ctx.init(null, trustManagers, null); final SSLSocketFactory sslFactory = ctx.getSocketFactory(); final URLConnection urlConnection = new URL(url).openConnection(); if (urlConnection instanceof HttpsURLConnection) { @javax.annotation.Nonnull final HttpsURLConnection conn = (HttpsURLConnection) urlConnection; conn.setSSLSocketFactory(sslFactory); conn.setRequestMethod("GET"); } return urlConnection.getInputStream(); }
From source file:de.unidue.stud.sehawagn.oidcclient.SimpleOIDCClient.java
private static SSLContext getTrustEverybodySSLContext() { // Create a trust manager that does not validate certificate chains TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() { public java.security.cert.X509Certificate[] getAcceptedIssuers() { return null; }//from w w w . j a va 2 s . c om public void checkClientTrusted(X509Certificate[] certs, String authType) { } public void checkServerTrusted(X509Certificate[] certs, String authType) { } } }; SSLContext sc = null; try { sc = SSLContext.getInstance("SSL"); sc.init(null, trustAllCerts, new java.security.SecureRandom()); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (KeyManagementException e) { e.printStackTrace(); } return sc; }
From source file:org.jboss.as.test.http.util.TestHttpClientUtils.java
/** *@param credentialsProvider optional cred provider * @return client that doesn't verify https connections *//*from w w w . jav a 2s .c o m*/ public static CloseableHttpClient getHttpsClient(CredentialsProvider credentialsProvider) { 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); ctx.init(null, new TrustManager[] { tm }, null); SSLConnectionSocketFactory sslConnectionFactory = new SSLConnectionSocketFactory(ctx, new NoopHostnameVerifier()); Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create() .register("https", sslConnectionFactory).build(); HttpClientConnectionManager ccm = new BasicHttpClientConnectionManager(registry); HttpClientBuilder builder = HttpClientBuilder.create().setSSLSocketFactory(sslConnectionFactory) .setSSLHostnameVerifier(new NoopHostnameVerifier()).setConnectionManager(ccm); if (credentialsProvider != null) { builder.setDefaultCredentialsProvider(credentialsProvider); } return builder.build(); } catch (Exception ex) { ex.printStackTrace(); return null; } }