Search in sources :

Example 6 with SSLSocketFactory

use of javax.net.ssl.SSLSocketFactory in project tomcat by apache.

the class TestSsl method testRenegotiateWorks.

@Test
public void testRenegotiateWorks() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    Assume.assumeTrue("SSL renegotiation has to be supported for this test", TesterSupport.isClientRenegotiationSupported(getTomcatInstance()));
    Context root = tomcat.addContext("", TEMP_DIR);
    Wrapper w = Tomcat.addServlet(root, "tester", new TesterServlet());
    w.setAsyncSupported(true);
    root.addServletMappingDecoded("/", "tester");
    TesterSupport.initSsl(tomcat);
    tomcat.start();
    SSLContext sslCtx = SSLContext.getInstance("TLS");
    sslCtx.init(null, TesterSupport.getTrustManagers(), null);
    SSLSocketFactory socketFactory = sslCtx.getSocketFactory();
    SSLSocket socket = (SSLSocket) socketFactory.createSocket("localhost", getPort());
    OutputStream os = socket.getOutputStream();
    InputStream is = socket.getInputStream();
    Reader r = new InputStreamReader(is);
    doRequest(os, r);
    TesterHandshakeListener listener = new TesterHandshakeListener();
    socket.addHandshakeCompletedListener(listener);
    socket.startHandshake();
    // One request should be sufficient
    int requestCount = 0;
    int listenerComplete = 0;
    try {
        while (requestCount < 10) {
            requestCount++;
            doRequest(os, r);
            if (listener.isComplete() && listenerComplete == 0) {
                listenerComplete = requestCount;
            }
        }
    } catch (AssertionError | IOException e) {
        String message = "Failed on request number " + requestCount + " after startHandshake(). " + e.getMessage();
        log.error(message, e);
        Assert.fail(message);
    }
    Assert.assertTrue(listener.isComplete());
    System.out.println("Renegotiation completed after " + listenerComplete + " requests");
}
Also used : SSLContext(javax.net.ssl.SSLContext) Context(org.apache.catalina.Context) Wrapper(org.apache.catalina.Wrapper) Tomcat(org.apache.catalina.startup.Tomcat) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) SSLSocket(javax.net.ssl.SSLSocket) OutputStream(java.io.OutputStream) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) SSLContext(javax.net.ssl.SSLContext) IOException(java.io.IOException) TesterServlet(org.apache.catalina.startup.TesterServlet) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 7 with SSLSocketFactory

use of javax.net.ssl.SSLSocketFactory in project UltimateAndroid by cymcsg.

the class HttpsUtils method sendWithSSlSocket.

/**
     * @deprecated
     */
public static void sendWithSSlSocket(String urlLink) {
    SSLSocketFactory sslsocketfactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
    URL url = null;
    try {
        url = new URL(urlLink);
        HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
        conn.setSSLSocketFactory(sslsocketfactory);
        InputStream inputstream = conn.getInputStream();
        InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
        BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
        String string = null;
        while ((string = bufferedreader.readLine()) != null) {
            System.out.println("Received " + string);
        }
    } catch (Exception e) {
        e.printStackTrace();
        Logs.e(e, "");
    }
}
Also used : SSLSocketFactory(javax.net.ssl.SSLSocketFactory) URL(java.net.URL) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) KeyManagementException(java.security.KeyManagementException) KeyStoreException(java.security.KeyStoreException) CertificateException(java.security.cert.CertificateException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 8 with SSLSocketFactory

use of javax.net.ssl.SSLSocketFactory in project AndroidNetworkDemo by dodocat.

the class RequestManager method newRequestQueue.

private RequestQueue newRequestQueue(Context context) {
    RequestQueue requestQueue;
    try {
        String[] hosts = { "kyfw.12306.cn" };
        int[] certRes = { R.raw.kyfw };
        String[] certPass = { "asdfqaz" };
        socketFactoryMap = new Hashtable<>(hosts.length);
        for (int i = 0; i < certRes.length; i++) {
            int res = certRes[i];
            String password = certPass[i];
            SSLSocketFactory sslSocketFactory = createSSLSocketFactory(context, res, password);
            socketFactoryMap.put(hosts[i], sslSocketFactory);
        }
        HurlStack stack = new SelfSignSslOkHttpStack(socketFactoryMap);
        requestQueue = Volley.newRequestQueue(context, stack);
        requestQueue.start();
    } catch (KeyStoreException | CertificateException | NoSuchAlgorithmException | KeyManagementException | IOException e) {
        throw new RuntimeException(e);
    }
    return requestQueue;
}
Also used : HurlStack(com.android.volley.toolbox.HurlStack) CertificateException(java.security.cert.CertificateException) KeyStoreException(java.security.KeyStoreException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) KeyManagementException(java.security.KeyManagementException) RequestQueue(com.android.volley.RequestQueue) SSLSocketFactory(javax.net.ssl.SSLSocketFactory)

Example 9 with SSLSocketFactory

use of javax.net.ssl.SSLSocketFactory in project jetty.project by eclipse.

the class SslContextFactory method newSslSocket.

public SSLSocket newSslSocket() throws IOException {
    checkIsStarted();
    SSLContext context = getSslContext();
    SSLSocketFactory factory = context.getSocketFactory();
    SSLSocket socket = (SSLSocket) factory.createSocket();
    socket.setSSLParameters(customize(socket.getSSLParameters()));
    return socket;
}
Also used : SSLSocket(javax.net.ssl.SSLSocket) SSLContext(javax.net.ssl.SSLContext) SSLSocketFactory(javax.net.ssl.SSLSocketFactory)

Example 10 with SSLSocketFactory

use of javax.net.ssl.SSLSocketFactory in project jetty.project by eclipse.

the class ConnectHandlerSSLTest method wrapSocket.

private SSLSocket wrapSocket(Socket socket) throws Exception {
    SSLContext sslContext = sslContextFactory.getSslContext();
    SSLSocketFactory socketFactory = sslContext.getSocketFactory();
    SSLSocket sslSocket = (SSLSocket) socketFactory.createSocket(socket, socket.getInetAddress().getHostAddress(), socket.getPort(), true);
    sslSocket.setUseClientMode(true);
    sslSocket.startHandshake();
    return sslSocket;
}
Also used : SSLSocket(javax.net.ssl.SSLSocket) SSLContext(javax.net.ssl.SSLContext) SSLSocketFactory(javax.net.ssl.SSLSocketFactory)

Aggregations

SSLSocketFactory (javax.net.ssl.SSLSocketFactory)191 SSLSocket (javax.net.ssl.SSLSocket)69 SSLContext (javax.net.ssl.SSLContext)57 IOException (java.io.IOException)45 Socket (java.net.Socket)37 Test (org.junit.Test)33 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)29 HostnameVerifier (javax.net.ssl.HostnameVerifier)27 URL (java.net.URL)23 KeyManagementException (java.security.KeyManagementException)20 OutputStream (java.io.OutputStream)19 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)19 InputStream (java.io.InputStream)18 CertificateException (java.security.cert.CertificateException)17 HttpURLConnection (java.net.HttpURLConnection)15 InetSocketAddress (java.net.InetSocketAddress)15 X509TrustManager (javax.net.ssl.X509TrustManager)15 OkHttpClient (okhttp3.OkHttpClient)14 SSLParameters (javax.net.ssl.SSLParameters)13 TrustManager (javax.net.ssl.TrustManager)13