Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/pkcs11/sslecc/JSSEServer.java
38855 views
/*1* Copyright (c) 2002, 2017, 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 java.net.SocketTimeoutException;27import java.util.concurrent.Executor;28import java.util.concurrent.Executors;29import java.util.concurrent.TimeUnit;3031import javax.net.ssl.KeyManager;32import javax.net.ssl.SSLContext;33import javax.net.ssl.SSLServerSocket;34import javax.net.ssl.SSLServerSocketFactory;35import javax.net.ssl.SSLSocket;36import javax.net.ssl.TrustManager;3738class JSSEServer extends CipherTest.Server {3940SSLServerSocket serverSocket;4142JSSEServer(CipherTest cipherTest) throws Exception {43super(cipherTest);44SSLContext serverContext = SSLContext.getInstance("TLS");45serverContext.init(46new KeyManager[] { CipherTest.keyManager },47new TrustManager[] { CipherTest.trustManager },48CipherTest.secureRandom);4950SSLServerSocketFactory factory = (SSLServerSocketFactory)serverContext.getServerSocketFactory();51serverSocket = (SSLServerSocket)factory.createServerSocket(0);52serverSocket.setSoTimeout(CipherTest.TIMEOUT);53CipherTest.serverPort = serverSocket.getLocalPort();54serverSocket.setEnabledCipherSuites(factory.getSupportedCipherSuites());55serverSocket.setWantClientAuth(true);56}5758@Override59public void run() {60System.out.println("JSSE Server listening on port " + CipherTest.serverPort);61Executor exec = Executors.newFixedThreadPool62(CipherTest.THREADS, DaemonThreadFactory.INSTANCE);6364try {65if (!CipherTest.clientCondition.await(CipherTest.TIMEOUT,66TimeUnit.MILLISECONDS)) {67System.out.println(68"The client is not the expected one or timeout. "69+ "Ignore in server side.");70return;71}7273while (true) {74final SSLSocket socket = (SSLSocket)serverSocket.accept();75socket.setSoTimeout(CipherTest.TIMEOUT);76Runnable r = new Runnable() {77@Override78public void run() {79try {80InputStream in = socket.getInputStream();81OutputStream out = socket.getOutputStream();82handleRequest(in, out);83out.flush();84socket.close();85socket.getSession().invalidate();86} catch (IOException e) {87cipherTest.setFailed();88e.printStackTrace();89} finally {90if (socket != null) {91try {92socket.close();93} catch (IOException e) {94cipherTest.setFailed();95System.out.println("Exception closing socket on server side:");96e.printStackTrace();97}98}99}100}101};102exec.execute(r);103}104} catch (SocketTimeoutException ste) {105System.out.println("The server got timeout for waiting for the connection, "106+ "so ignore the test.");107} catch (Exception e) {108cipherTest.setFailed();109e.printStackTrace();110}111}112}113114115