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