1 /*
   2  * Copyright (c) 2002, 2018, 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         serverSocket.setEnabledCipherSuites(factory.getSupportedCipherSuites());
  55         serverSocket.setWantClientAuth(true);
  56     }
  57 
  58     public void run() {
  59         System.out.println("JSSE Server listening on port " + CipherTest.serverPort);
  60         Executor exec = Executors.newFixedThreadPool
  61                             (cipherTest.THREADS, DaemonThreadFactory.INSTANCE);
  62         try {
  63             while (true) {
  64                 final SSLSocket socket = (SSLSocket)serverSocket.accept();
  65                 socket.setSoTimeout(CipherTest.TIMEOUT);
  66                 Runnable r = new Runnable() {
  67                     public void run() {
  68                         try {
  69                             InputStream in = socket.getInputStream();
  70                             OutputStream out = socket.getOutputStream();
  71                             handleRequest(in, out);
  72                             out.flush();
  73                             socket.close();
  74                             socket.getSession().invalidate();
  75                         } catch (IOException e) {
  76                             cipherTest.setFailed();
  77                             e.printStackTrace();
  78                         } finally {
  79                             if (socket != null) {
  80                                 try {
  81                                     socket.close();
  82                                 } catch (IOException e) {
  83                                     cipherTest.setFailed();
  84                                     System.out.println("Exception closing socket on server side:");
  85                                     e.printStackTrace();
  86                                 }
  87                             }
  88                         }
  89                     }
  90                 };
  91                 exec.execute(r);
  92             }
  93         } catch (IOException e) {
  94             cipherTest.setFailed();
  95             e.printStackTrace();
  96             //
  97         }
  98     }
  99 
 100 }
--- EOF ---