1 /* 2 * Copyright (c) 2002, 2017, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 import java.io.IOException; 25 import java.io.InputStream; 26 import java.io.OutputStream; 27 import java.net.SocketTimeoutException; 28 import java.util.concurrent.Executor; 29 import java.util.concurrent.Executors; 30 import java.util.concurrent.TimeUnit; 31 32 import javax.net.ssl.KeyManager; 33 import javax.net.ssl.SSLContext; 34 import javax.net.ssl.SSLServerSocket; 35 import javax.net.ssl.SSLServerSocketFactory; 36 import javax.net.ssl.SSLSocket; 37 import javax.net.ssl.TrustManager; 38 39 class JSSEServer extends CipherTest.Server { 40 41 SSLServerSocket serverSocket; 42 43 JSSEServer(CipherTest cipherTest) throws Exception { 44 super(cipherTest); 45 SSLContext serverContext = SSLContext.getInstance("TLS"); 46 serverContext.init( 47 new KeyManager[] { CipherTest.keyManager }, 48 new TrustManager[] { CipherTest.trustManager }, 49 CipherTest.secureRandom); 50 51 SSLServerSocketFactory factory = (SSLServerSocketFactory)serverContext.getServerSocketFactory(); 52 serverSocket = (SSLServerSocket)factory.createServerSocket(0); 53 serverSocket.setSoTimeout(CipherTest.TIMEOUT); 54 CipherTest.serverPort = serverSocket.getLocalPort(); 55 serverSocket.setEnabledCipherSuites(factory.getSupportedCipherSuites()); 56 serverSocket.setWantClientAuth(true); 57 } 58 59 @Override 60 public void run() { 61 System.out.println("JSSE Server listening on port " + CipherTest.serverPort); 62 Executor exec = Executors.newFixedThreadPool 63 (CipherTest.THREADS, DaemonThreadFactory.INSTANCE); 64 65 try { 66 if (!CipherTest.clientCondition.await(CipherTest.TIMEOUT, 67 TimeUnit.MILLISECONDS)) { 68 System.out.println( 69 "The client is not the expected one or timeout. " 70 + "Ignore in server side."); 71 return; 72 } 73 74 while (true) { 75 final SSLSocket socket = (SSLSocket)serverSocket.accept(); 76 socket.setSoTimeout(CipherTest.TIMEOUT); 77 Runnable r = new Runnable() { 78 @Override 79 public void run() { 80 try { 81 InputStream in = socket.getInputStream(); 82 OutputStream out = socket.getOutputStream(); 83 handleRequest(in, out); 84 out.flush(); 85 socket.close(); 86 socket.getSession().invalidate(); 87 } catch (IOException e) { 88 cipherTest.setFailed(); 89 e.printStackTrace(); 90 } finally { 91 if (socket != null) { 92 try { 93 socket.close(); 94 } catch (IOException e) { 95 cipherTest.setFailed(); 96 System.out.println("Exception closing socket on server side:"); 97 e.printStackTrace(); 98 } 99 } 100 } 101 } 102 }; 103 exec.execute(r); 104 } 105 } catch (SocketTimeoutException ste) { 106 System.out.println("The server got timeout for waiting for the connection, " 107 + "so ignore the test."); 108 } catch (Exception e) { 109 cipherTest.setFailed(); 110 e.printStackTrace(); 111 } 112 } 113 }