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.daoke.mobileserver.test.TestHttps.java
public static String doPost(String url, String ctype, byte[] content, int connectTimeout, int readTimeout) throws Exception { HttpsURLConnection conn = null; OutputStream out = null;/*w w w . ja v a2 s .c om*/ String rsp = null; try { try { SSLContext ctx = SSLContext.getInstance("TLS"); ctx.init(new KeyManager[0], new TrustManager[] { new DefaultTrustManager() }, new SecureRandom()); SSLContext.setDefault(ctx); conn = getConnection(new URL(url), METHOD_POST, ctype); conn.setHostnameVerifier(new HostnameVerifier() { @Override public boolean verify(String hostname, SSLSession session) { return true; } }); conn.setConnectTimeout(connectTimeout); conn.setReadTimeout(readTimeout); } catch (Exception e) { log.error("GET_CONNECTOIN_ERROR, URL = " + url, e); throw e; } try { out = conn.getOutputStream(); out.write(content); rsp = getResponseAsString(conn); } catch (IOException e) { log.error("REQUEST_RESPONSE_ERROR, URL = " + url, e); throw e; } } finally { if (out != null) { out.close(); } if (conn != null) { conn.disconnect(); } } return rsp; }
From source file:mobi.jenkinsci.ci.client.TrustAllSSLSocketFactory.java
private static SSLContext createEasySSLContext() throws IOException { try {/*from ww w . j a va 2 s . c o m*/ SSLContext context = SSLContext.getInstance("TLS"); context.init(null, new TrustManager[] { new AllowAllX509TrustManager() }, null); return context; } catch (Exception e) { throw new IOException(e.getMessage()); } }
From source file:com.alibaba.antx.config.resource.http.EasySSLProtocolSocketFactory.java
private static SSLContext createEasySSLContext() { try {//from w w w . j a v a 2 s. c o m SSLContext context = SSLContext.getInstance("SSL"); context.init(null, new TrustManager[] { new EasyX509TrustManager(null) }, null); return context; } catch (Exception e) { LOG.error(e.getMessage(), e); throw new HttpClientError(e.toString()); } }
From source file:ddf.security.common.util.CommonSSLFactory.java
/** * Creates a new SSLSocketFactory from a truststore and keystore. This is used during SSL * communication.//ww w .j av a2s.com * * @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:org.apache.juneau.rest.test.TestMicroservice.java
static SSLConnectionSocketFactory getSSLSocketFactory() throws Exception { SSLContext sslContext = SSLContext.getInstance("SSL"); TrustManager tm = new SimpleX509TrustManager(true); sslContext.init(null, new TrustManager[] { tm }, new SecureRandom()); return new SSLConnectionSocketFactory(sslContext, new NoopHostnameVerifier()); }
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;// w w w . ja v a 2 s. com 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:com.mid.util.ssl.EasySSLProtocolSocketFactory.java
private static SSLContext createEasySSLContext() { try {/*from w w w. ja v a 2 s.c om*/ SSLContext context = SSLContext.getInstance("SSL"); context.init(null, new TrustManager[] { new EasyX509TrustManager() }, null); return context; } catch (Exception e) { LOG.error(e.getMessage(), e); throw new HttpClientError(e.toString()); } }
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 ww w .ja v a 2 s .co m 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:org.openremote.android.console.net.SelfCertificateSSLSocketFactory.java
/** * Creates a new SelfCertificateSSLSocket object. * // w ww .j a va 2 s. co m * @return the SSL context * * @throws IOException Signals that an I/O exception has occurred. */ private static SSLContext createEasySSLContext(Context context) throws IOException { TrustManager easyTrustManager = new X509TrustManager() { @Override public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException { } @Override public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException { } @Override public X509Certificate[] getAcceptedIssuers() { return null; } }; try { ORKeyStore keystore = ORKeyStore.getInstance(context); KeyManager[] managers = null; //keystore.fillKeyStore(); //keystore.saveKeyStore(); if (!keystore.isEmpty()) { KeyManagerFactory keyManagerFactory = KeyManagerFactory .getInstance(KeyManagerFactory.getDefaultAlgorithm()); keyManagerFactory.init(keystore.getKeyStore(), "password".toCharArray()); managers = keyManagerFactory.getKeyManagers(); } SSLContext sslcontext = SSLContext.getInstance("TLS"); sslcontext.init(managers, new TrustManager[] { easyTrustManager }, null); return sslcontext; } catch (Exception e) { throw new IOException(e.getMessage()); } }
From source file:flex.messaging.services.http.httpclient.EasySSLProtocolSocketFactory.java
private static SSLContext createEasySSLContext() { try {// w w w . j a va 2 s. c om SSLContext context = SSLContext.getInstance("SSL"); context.init(null, new TrustManager[] { new EasyX509TrustManager(null) }, null); return context; } catch (Exception e) { if (Trace.ssl) { Trace.trace(e.getMessage()); } throw new HttpClientError(e.toString()); } }