Path: blob/master/test/jdk/javax/net/ssl/TLS/JSSEServer.java
41152 views
/*1* Copyright (c) 2010, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.io.IOException;24import java.io.InputStream;25import java.io.OutputStream;26import javax.net.ssl.KeyManager;27import javax.net.ssl.SSLContext;28import javax.net.ssl.SSLServerSocket;29import javax.net.ssl.SSLServerSocketFactory;30import javax.net.ssl.SSLSocket;31import javax.net.ssl.TrustManager;3233public class JSSEServer extends CipherTestUtils.Server {3435private final SSLServerSocket serverSocket;36private static volatile boolean closeServer = false;3738JSSEServer(CipherTestUtils cipherTest, int serverPort,39String protocol, String cipherSuite) throws Exception {40super(cipherTest);41SSLContext serverContext = SSLContext.getInstance("TLS");42serverContext.init(new KeyManager[]{cipherTest.getServerKeyManager()},43new TrustManager[]{cipherTest.getServerTrustManager()},44CipherTestUtils.secureRandom);45SSLServerSocketFactory factory =46(SSLServerSocketFactory)serverContext.getServerSocketFactory();47serverSocket =48(SSLServerSocket) factory.createServerSocket(serverPort);49serverSocket.setEnabledProtocols(protocol.split(","));50serverSocket.setEnabledCipherSuites(cipherSuite.split(","));5152CipherTestUtils.printInfo(serverSocket);53}5455@Override56public void run() {57System.out.println("JSSE Server listening on port " + getPort());58while (!closeServer) {59try (final SSLSocket socket = (SSLSocket) serverSocket.accept()) {60socket.setSoTimeout(CipherTestUtils.TIMEOUT);6162try (InputStream in = socket.getInputStream();63OutputStream out = socket.getOutputStream()) {64handleRequest(in, out);65out.flush();66} catch (IOException e) {67CipherTestUtils.addFailure(e);68System.out.println("Got IOException:");69e.printStackTrace(System.out);70}71} catch (Exception e) {72CipherTestUtils.addFailure(e);73System.out.println("Exception:");74e.printStackTrace(System.out);75}76}77}7879int getPort() {80return serverSocket.getLocalPort();81}8283@Override84public void close() throws IOException {85closeServer = true;86if (serverSocket != null && !serverSocket.isClosed()) {87serverSocket.close();88}89}90}919293