Search in sources :

Example 6 with SSLSocket

use of javax.net.ssl.SSLSocket in project hbase by apache.

the class SslRMIClientSocketFactorySecure method createSocket.

@Override
public Socket createSocket(String host, int port) throws IOException {
    SSLSocket socket = (SSLSocket) super.createSocket(host, port);
    ArrayList<String> secureProtocols = new ArrayList<>();
    for (String p : socket.getEnabledProtocols()) {
        if (!p.contains("SSLv3")) {
            secureProtocols.add(p);
        }
    }
    socket.setEnabledProtocols(secureProtocols.toArray(new String[secureProtocols.size()]));
    return socket;
}
Also used : SSLSocket(javax.net.ssl.SSLSocket) ArrayList(java.util.ArrayList)

Example 7 with SSLSocket

use of javax.net.ssl.SSLSocket in project kafka by apache.

the class EchoServer method run.

@Override
public void run() {
    try {
        while (true) {
            final Socket socket = serverSocket.accept();
            sockets.add(socket);
            Thread thread = new Thread() {

                @Override
                public void run() {
                    try {
                        DataInputStream input = new DataInputStream(socket.getInputStream());
                        DataOutputStream output = new DataOutputStream(socket.getOutputStream());
                        while (socket.isConnected() && !socket.isClosed()) {
                            int size = input.readInt();
                            if (renegotiate.get()) {
                                renegotiate.set(false);
                                ((SSLSocket) socket).startHandshake();
                            }
                            byte[] bytes = new byte[size];
                            input.readFully(bytes);
                            output.writeInt(size);
                            output.write(bytes);
                            output.flush();
                        }
                    } catch (IOException e) {
                    // ignore
                    } finally {
                        try {
                            socket.close();
                        } catch (IOException e) {
                        // ignore
                        }
                    }
                }
            };
            thread.start();
            threads.add(thread);
        }
    } catch (IOException e) {
    // ignore
    }
}
Also used : DataOutputStream(java.io.DataOutputStream) SSLSocket(javax.net.ssl.SSLSocket) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) Socket(java.net.Socket) SSLSocket(javax.net.ssl.SSLSocket) ServerSocket(java.net.ServerSocket)

Example 8 with SSLSocket

use of javax.net.ssl.SSLSocket 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 9 with SSLSocket

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

the class JDK9ALPNTest method testClientNotSupportingALPNServerSpeaksDefaultProtocol.

@Test
public void testClientNotSupportingALPNServerSpeaksDefaultProtocol() throws Exception {
    startServer(new AbstractHandler.ErrorDispatchHandler() {

        @Override
        protected void doNonErrorHandle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
        }
    });
    SslContextFactory sslContextFactory = new SslContextFactory(true);
    sslContextFactory.start();
    SSLContext sslContext = sslContextFactory.getSslContext();
    try (SSLSocket client = (SSLSocket) sslContext.getSocketFactory().createSocket("localhost", connector.getLocalPort())) {
        client.setUseClientMode(true);
        client.setSoTimeout(5000);
        client.startHandshake();
        OutputStream output = client.getOutputStream();
        output.write(("" + "GET / HTTP/1.1\r\n" + "Host: localhost\r\n" + "Connection: close\r\n" + "\r\n" + "").getBytes(StandardCharsets.UTF_8));
        output.flush();
        InputStream input = client.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8));
        String line = reader.readLine();
        Assert.assertTrue(line.contains(" 200 "));
        while (true) {
            if (reader.readLine() == null)
                break;
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) SSLSocket(javax.net.ssl.SSLSocket) OutputStream(java.io.OutputStream) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) SSLContext(javax.net.ssl.SSLContext) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) BufferedReader(java.io.BufferedReader) Test(org.junit.Test)

Example 10 with SSLSocket

use of javax.net.ssl.SSLSocket 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)

Aggregations

SSLSocket (javax.net.ssl.SSLSocket)326 IOException (java.io.IOException)101 Test (org.junit.Test)62 SSLContext (javax.net.ssl.SSLContext)59 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)59 Socket (java.net.Socket)57 OutputStream (java.io.OutputStream)50 InetSocketAddress (java.net.InetSocketAddress)39 CertificateException (java.security.cert.CertificateException)33 SSLException (javax.net.ssl.SSLException)32 SSLSession (javax.net.ssl.SSLSession)31 InputStream (java.io.InputStream)30 SSLPeerUnverifiedException (javax.net.ssl.SSLPeerUnverifiedException)30 SSLServerSocket (javax.net.ssl.SSLServerSocket)27 SocketTimeoutException (java.net.SocketTimeoutException)24 SocketException (java.net.SocketException)23 ServerSocket (java.net.ServerSocket)22 UnknownHostException (java.net.UnknownHostException)21 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)21 InputStreamReader (java.io.InputStreamReader)19