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.util.concurrent.Executor;
  28 import java.util.concurrent.Executors;
  29 
  30 import javax.net.ssl.KeyManager;
  31 import javax.net.ssl.SSLContext;
  32 import javax.net.ssl.SSLServerSocket;
  33 import javax.net.ssl.SSLServerSocketFactory;
  34 import javax.net.ssl.SSLSocket;
  35 import javax.net.ssl.TrustManager;
  36 
  37 class JSSEServer extends CipherTest.Server {
  38 
  39     SSLServerSocket serverSocket;
  40 
  41     JSSEServer(CipherTest cipherTest) throws Exception {
  42         super(cipherTest);
  43         SSLContext serverContext = SSLContext.getInstance("TLS");
  44         serverContext.init(
  45                 new KeyManager[] { CipherTest.keyManager },
  46                 new TrustManager[] { CipherTest.trustManager },
  47                 CipherTest.secureRandom);
  48 
  49         SSLServerSocketFactory factory
  50                 = (SSLServerSocketFactory) serverContext.getServerSocketFactory();
  51         serverSocket
  52                 = (SSLServerSocket) factory.createServerSocket(CipherTest.serverPort);
  53         CipherTest.serverPort = serverSocket.getLocalPort();
  54 
  55         // JDK-8190492: Enable all supported protocols on server side to test SSLv3
  56         serverSocket.setEnabledProtocols(serverSocket.getSupportedProtocols());
  57 
  58         serverSocket.setEnabledCipherSuites(factory.getSupportedCipherSuites());
  59         serverSocket.setWantClientAuth(true);
  60     }
  61 
  62     public void run() {
  63         System.out.println("JSSE Server listening on port " + CipherTest.serverPort);
  64         Executor exec = Executors.newFixedThreadPool
  65                             (cipherTest.THREADS, DaemonThreadFactory.INSTANCE);
  66         try {
  67             while (true) {
  68                 final SSLSocket socket = (SSLSocket)serverSocket.accept();
  69                 socket.setSoTimeout(CipherTest.TIMEOUT);
  70                 Runnable r = new Runnable() {
  71                     public void run() {
  72                         try {
  73                             InputStream in = socket.getInputStream();
  74                             OutputStream out = socket.getOutputStream();
  75                             handleRequest(in, out);
  76                             out.flush();
  77                             socket.close();
  78                             socket.getSession().invalidate();
  79                         } catch (IOException e) {
  80                             cipherTest.setFailed();
  81                             e.printStackTrace();
  82                         } finally {
  83                             if (socket != null) {
  84                                 try {
  85                                     socket.close();
  86                                 } catch (IOException e) {
  87                                     cipherTest.setFailed();
  88                                     System.out.println("Exception closing socket on server side:");
  89                                     e.printStackTrace();
  90                                 }
  91                             }
  92                         }
  93                     }
  94                 };
  95                 exec.execute(r);
  96             }
  97         } catch (IOException e) {
  98             cipherTest.setFailed();
  99             e.printStackTrace();
 100             //
 101         }
 102     }
 103 
 104 }
--- EOF ---