List of usage examples for javax.net.ssl SSLContext getSocketFactory
public final SSLSocketFactory getSocketFactory()
From source file:com.ct855.util.HttpsClientUtil.java
public static String testIt(String https_url, Map<String, String> map, String method) throws NoSuchAlgorithmException, NoSuchProviderException, KeyManagementException { //SSLContext?? TrustManager[] trustAllCerts = new TrustManager[] { new MyX509TrustManager() }; SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE"); sslContext.init(null, trustAllCerts, new java.security.SecureRandom()); //SSLContextSSLSocketFactory SSLSocketFactory ssf = sslContext.getSocketFactory(); URL url;/*from w w w . j av a2 s.c om*/ try { url = new URL(https_url); HttpsURLConnection con = (HttpsURLConnection) url.openConnection(); con.setRequestMethod(method); for (Map.Entry<String, String> entry : map.entrySet()) { con.setRequestProperty(entry.getKey(), entry.getValue()); } con.setSSLSocketFactory(ssf); //dumpl all cert info //print_https_cert(con); //dump all the content return print_content(con); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; }
From source file:org.apache.chemistry.shell.Main.java
private static void acceptSelfSignedCertificates() { TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() { public X509Certificate[] getAcceptedIssuers() { return null; }//from w ww . j av a 2 s .c om public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException { } public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException { } } }; try { SSLContext sc = SSLContext.getInstance("SSL"); sc.init(null, trustAllCerts, new java.security.SecureRandom()); HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); } catch (Exception e) { e.printStackTrace(); System.exit(1); } }
From source file:com.ct855.util.HttpsClientUtil.java
public static String postUrl(String url, Map<String, String> params) throws IOException, NoSuchAlgorithmException, KeyManagementException, NoSuchProviderException { //SSLContext?? TrustManager[] trustAllCerts = new TrustManager[] { new MyX509TrustManager() }; SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE"); sslContext.init(null, trustAllCerts, new java.security.SecureRandom()); //SSLContextSSLSocketFactory SSLSocketFactory ssf = sslContext.getSocketFactory(); String data = ""; for (String key : params.keySet()) { data += "&" + URLEncoder.encode(key, "UTF-8") + "=" + URLEncoder.encode(params.get(key), "UTF-8"); }//from www.j a v a 2s . c om data = data.substring(1); System.out.println("postUrl=>data:" + data); URL aURL = new java.net.URL(url); HttpsURLConnection aConnection = (HttpsURLConnection) aURL.openConnection(); aConnection.setSSLSocketFactory(ssf); aConnection.setDoOutput(true); aConnection.setDoInput(true); aConnection.setRequestMethod("POST"); OutputStreamWriter streamToAuthorize = new java.io.OutputStreamWriter(aConnection.getOutputStream()); streamToAuthorize.write(data); streamToAuthorize.flush(); streamToAuthorize.close(); InputStream resultStream = aConnection.getInputStream(); BufferedReader aReader = new java.io.BufferedReader(new java.io.InputStreamReader(resultStream)); StringBuffer aResponse = new StringBuffer(); String aLine = aReader.readLine(); while (aLine != null) { aResponse.append(aLine + "\n"); aLine = aReader.readLine(); } resultStream.close(); return aResponse.toString(); }
From source file:com.wso2telco.identity.application.authentication.endpoint.util.MutualSSLClient.java
/** * create basic SSL connection factory/*from ww w . j a v a 2 s. co m*/ * * @throws java.security.NoSuchAlgorithmException * @throws java.security.KeyStoreException * @throws java.security.KeyManagementException * @throws java.io.IOException * @throws java.security.UnrecoverableKeyException */ public static void initMutualSSLConnection() throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException, IOException, UnrecoverableKeyException { KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KEY_MANAGER_TYPE); keyManagerFactory.init(keyStore, keyStorePassword.toCharArray()); TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TRUST_MANAGER_TYPE); trustManagerFactory.init(trustStore); SSLContext sslContext = SSLContext.getInstance(PROTOCOL); sslContext.init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(), null); sslSocketFactory = sslContext.getSocketFactory(); }
From source file:com.wx.kernel.util.HttpKit.java
private static SSLSocketFactory initSSLSocketFactory() { try {//w w w . java2s . c om TrustManager[] tm = { new HttpKit().new TrustAnyTrustManager() }; SSLContext sslContext = SSLContext.getInstance("TLS", "SunJSSE"); sslContext.init(null, tm, new java.security.SecureRandom()); return sslContext.getSocketFactory(); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:org.kontalk.client.ClientHTTPConnection.java
public static SSLSocketFactory setupSSLSocketFactory(Context context, PrivateKey privateKey, X509Certificate certificate, boolean acceptAnyCertificate) throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException, KeyManagementException, UnrecoverableKeyException, NoSuchProviderException { // in-memory keystore KeyManager[] km = null;/*from w w w . j a v a 2s . co m*/ if (privateKey != null && certificate != null) { KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType()); keystore.load(null, null); keystore.setKeyEntry("private", privateKey, null, new Certificate[] { certificate }); // key managers KeyManagerFactory kmFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); kmFactory.init(keystore, null); km = kmFactory.getKeyManagers(); } // trust managers TrustManager[] tm; if (acceptAnyCertificate) { tm = new TrustManager[] { new X509TrustManager() { @Override public X509Certificate[] getAcceptedIssuers() { return null; } @Override public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { } @Override public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { } } }; } else { // load merged truststore (system + internal) KeyStore trustStore = InternalTrustStore.getTrustStore(context); // builtin keystore TrustManagerFactory tmFactory = TrustManagerFactory .getInstance(TrustManagerFactory.getDefaultAlgorithm()); tmFactory.init(trustStore); tm = tmFactory.getTrustManagers(); } SSLContext ctx = SSLContext.getInstance("TLSv1"); ctx.init(km, tm, null); return new TlsOnlySocketFactory(ctx.getSocketFactory(), true); }
From source file:io.hops.security.HopsUtil.java
/** * Set the default HTTPS trust policy to trust anything. * * NOTE: Use it only during development or use it wisely! */// ww w .java 2s.c o m public static void trustAllHTTPS() { try { final SSLContext sslContext = SSLContext.getInstance("TLSv1.2"); sslContext.init(null, trustAll, null); HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory()); HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() { @Override public boolean verify(String s, SSLSession sslSession) { return true; } }); } catch (GeneralSecurityException ex) { throw new IllegalStateException("Could not initialize SSLContext for CRL fetcher", ex); } }
From source file:com.example.zch.imspeak.utils.OtherUtils.java
public static void trustAllHttpsURLConnection() { // Create a trust manager that does not validate certificate chains if (sslSocketFactory == null) { TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() { @Override//from ww w . ja va2 s . c om public X509Certificate[] getAcceptedIssuers() { return null; } @Override public void checkClientTrusted(X509Certificate[] certs, String authType) { } @Override public void checkServerTrusted(X509Certificate[] certs, String authType) { } } }; try { SSLContext sslContext = SSLContext.getInstance("TLS"); sslContext.init(null, trustAllCerts, null); sslSocketFactory = sslContext.getSocketFactory(); } catch (Throwable e) { LogUtils.e(e.getMessage(), e); } } if (sslSocketFactory != null) { HttpsURLConnection.setDefaultSSLSocketFactory(sslSocketFactory); HttpsURLConnection.setDefaultHostnameVerifier( org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); } }
From source file:com.lidroid.util.OtherUtils.java
public static void trustAllHttpsURLConnection() { // Create a trust manager that does not validate certificate chains if (sslSocketFactory == null) { TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() { @Override/* w ww . ja v a 2 s .c o m*/ public java.security.cert.X509Certificate[] getAcceptedIssuers() { return null; } @Override public void checkClientTrusted(X509Certificate[] certs, String authType) { } @Override public void checkServerTrusted(X509Certificate[] certs, String authType) { } } }; try { SSLContext sslContext = SSLContext.getInstance("TLS"); sslContext.init(null, trustAllCerts, null); sslSocketFactory = sslContext.getSocketFactory(); } catch (Throwable e) { Logger.e(e.getMessage(), e); } } if (sslSocketFactory != null) { HttpsURLConnection.setDefaultSSLSocketFactory(sslSocketFactory); HttpsURLConnection.setDefaultHostnameVerifier( org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); } }
From source file:Main.java
public static SSLSocketFactory setCertificates(InputStream... certificates) { try {//from ww w. jav a2 s . c o m CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509"); KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); keyStore.load(null); int index = 0; for (InputStream certificate : certificates) { String certificateAlias = Integer.toString(index++); keyStore.setCertificateEntry(certificateAlias, certificateFactory.generateCertificate(certificate)); try { if (certificate != null) certificate.close(); } catch (IOException e) { } } SSLContext sslContext = SSLContext.getInstance("TLS"); TrustManagerFactory trustManagerFactory = TrustManagerFactory .getInstance(TrustManagerFactory.getDefaultAlgorithm()); trustManagerFactory.init(keyStore); sslContext.init(null, trustManagerFactory.getTrustManagers(), new SecureRandom()); socketFactory = sslContext.getSocketFactory(); } catch (Exception e) { e.printStackTrace(); } return socketFactory; }