1 /*
   2  * Copyright (c) 2002, 2019, 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 
  56         // JDK-8190492: Enable all supported protocols on server side to test SSLv3
  57         serverSocket.setEnabledProtocols(serverSocket.getSupportedProtocols());
  58 
  59         serverSocket.setEnabledCipherSuites(factory.getSupportedCipherSuites());
  60         serverSocket.setWantClientAuth(true);
  61     }
  62 
  63     @Override
  64     public void run() {
  65         System.out.println("JSSE Server listening on port " + CipherTest.serverPort);
  66         Executor exec = Executors.newFixedThreadPool
  67                             (CipherTest.THREADS, DaemonThreadFactory.INSTANCE);
  68 
  69         try {
  70             if (!CipherTest.clientCondition.await(CipherTest.TIMEOUT,
  71                     TimeUnit.MILLISECONDS)) {
  72                 System.out.println(
  73                         "The client is not the expected one or timeout. "
  74                                 + "Ignore in server side.");
  75                 return;
  76             }
  77 
  78             while (true) {
  79                 final SSLSocket socket = (SSLSocket)serverSocket.accept();
  80                 socket.setSoTimeout(CipherTest.TIMEOUT);
  81                 Runnable r = new Runnable() {
  82                     @Override
  83                     public void run() {
  84                         try {
  85                             InputStream in = socket.getInputStream();
  86                             OutputStream out = socket.getOutputStream();
  87                             handleRequest(in, out);
  88                             out.flush();
  89                             socket.close();
  90                             socket.getSession().invalidate();
  91                         } catch (IOException e) {
  92                             cipherTest.setFailed();
  93                             e.printStackTrace();
  94                         } finally {
  95                             if (socket != null) {
  96                                 try {
  97                                     socket.close();
  98                                 } catch (IOException e) {
  99                                     cipherTest.setFailed();
 100                                     System.out.println("Exception closing socket on server side:");
 101                                     e.printStackTrace();
 102                                 }
 103                             }
 104                         }
 105                     }
 106                 };
 107                 exec.execute(r);
 108             }
 109         } catch (SocketTimeoutException ste) {
 110             System.out.println("The server got timeout for waiting for the connection, "
 111                     + "so ignore the test.");
 112         } catch (Exception e) {
 113             cipherTest.setFailed();
 114             e.printStackTrace();
 115         }
 116     }
 117 }