public void afterConnect() throws IOException, UnknownHostException { if (!isCachedConnection()) { SSLSocket s = null; SSLSocketFactory factory; factory = sslSocketFactory; try { if (!(serverSocket instanceof SSLSocket)) { s = (SSLSocket) factory.createSocket(serverSocket, host, port, true); } else { s = (SSLSocket) serverSocket; } } catch (IOException ex) { // If we fail to connect through the tunnel, try it // locally, as a last resort. If this doesn't work, // throw the original exception. try { s = (SSLSocket) factory.createSocket(host, port); } catch (IOException ignored) { throw ex; } } SSLSocketFactoryImpl.checkCreate(s); // // Force handshaking, so that we get any authentication. // Register a handshake callback so our session state tracks any // later session renegotiations. // String[] protocols = getProtocols(); String[] ciphers = getCipherSuites(); if (protocols != null) s.setEnabledProtocols(protocols); if (ciphers != null) s.setEnabledCipherSuites(ciphers); s.addHandshakeCompletedListener(this); s.startHandshake(); session = s.getSession(); // change the serverSocket and serverOutput serverSocket = s; try { serverOutput = new PrintStream( new BufferedOutputStream(serverSocket.getOutputStream()), false, encoding); } catch (UnsupportedEncodingException e) { throw new InternalError(encoding + " encoding not found"); } // check URL spoofing checkURLSpoofing(hv); } else { // if we are reusing a cached https session, // we don't need to do handshaking etc. But we do need to // set the ssl session session = ((SSLSocket) serverSocket).getSession(); } }
// Server loop private static void serverLoop() throws Exception { // Check if client is connected if (hidOut == null && !busy) { // Busy busy = true; // Notify // System.out.println("Awaiting HID client..."); // No client is connected - accept connection SSLSocket client = (SSLSocket) server.accept(); // Require auth // client.setNeedClientAuth(true); // Enable ciphers client.setEnabledCipherSuites(client.getEnabledCipherSuites()); // Add a handshake listener client.addHandshakeCompletedListener( new HandshakeCompletedListener() { @Override public void handshakeCompleted(HandshakeCompletedEvent event) { handleClient(event); } }); // Start handshake client.startHandshake(); } else { // Check client status try { // System.out.print("Checking HID client status..."); if (!connected) { // System.out.println(Color.YELLOW + " Connecting..." + // Color.RESET); return; } // Send a ping to the client hidOut.write(0x01); // System.out.println(Color.GREEN + " Connected!" + Color.RESET); Thread.sleep(1000); } catch (Exception e) { // System.out.println(Color.RED + " Not Connected!" + Color.RESET); // Reset variables hidClient = null; hidOut = null; busy = false; connected = false; // Reset display Display.readyMessage = Display.defaultReadyMessage; Display.ready(); // Notify System.out.println(Color.BLUE + "NOTICE: " + Color.RESET + "HID client has disconnected"); } } }
/* * Define the server side of the test. * * If the server prematurely exits, serverReady will be set to true * to avoid infinite hangs. */ void doServerSide() throws Exception { SSLServerSocketFactory sslssf = (SSLServerSocketFactory) SSLServerSocketFactory.getDefault(); SSLServerSocket sslServerSocket = (SSLServerSocket) sslssf.createServerSocket(serverPort); serverPort = sslServerSocket.getLocalPort(); /* * Signal Client, we're ready for his connect. */ serverReady = true; SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept(); sslSocket.addHandshakeCompletedListener(this); InputStream sslIS = sslSocket.getInputStream(); OutputStream sslOS = sslSocket.getOutputStream(); for (int i = 0; i < 10; i++) { sslIS.read(); sslOS.write(85); sslOS.flush(); } System.out.println("invalidating"); sslSocket.getSession().invalidate(); System.out.println("starting new handshake"); sslSocket.startHandshake(); for (int i = 0; i < 10; i++) { System.out.println("sending/receiving data, iteration: " + i); sslIS.read(); sslOS.write(85); sslOS.flush(); } sslSocket.close(); }
public void addHandshakeCompletedListener(final HandshakeCompletedListener listener) { delegate.addHandshakeCompletedListener(listener); }
public JSSESupport(SSLSocket sock) throws RemoteException { startManagers(); ssl = sock; session = sock.getSession(); sock.addHandshakeCompletedListener(listener); }