Path: blob/master/test/jdk/javax/net/ssl/TLSCommon/interop/JdkServer.java
41154 views
/*1* Copyright (c) 2020, 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.net.SocketException;25import java.util.ArrayList;26import java.util.List;2728import javax.net.ssl.SNIHostName;29import javax.net.ssl.SNIMatcher;30import javax.net.ssl.SSLContext;31import javax.net.ssl.SSLParameters;32import javax.net.ssl.SSLServerSocket;33import javax.net.ssl.SSLServerSocketFactory;34import javax.net.ssl.SSLSession;35import javax.net.ssl.SSLSocket;3637/*38* A JDK server based on SSLServerSocket.39*/40public class JdkServer extends AbstractServer {4142protected final String response;4344protected final SSLServerSocket serverSocket;4546protected final SSLContext context;47private SSLSocket socket;4849public JdkServer(Builder builder) throws Exception {50response = builder.getMessage();5152context = Utilities.createSSLContext(builder.getCertTuple());53SSLServerSocketFactory serverFactory = context.getServerSocketFactory();54serverSocket55= (SSLServerSocket) serverFactory.createServerSocket(builder.getPort());56configServerSocket(builder);57}5859protected void configServerSocket(Builder builder) throws SocketException {60serverSocket.setSoTimeout(builder.getTimeout() * 1000);61if (builder.getProtocols() != null) {62serverSocket.setEnabledProtocols(Utilities.enumsToStrs(protocol -> {63return JdkUtils.protocol((Protocol) protocol);64}, builder.getProtocols()));65}66if (builder.getCipherSuites() != null) {67serverSocket.setEnabledCipherSuites(68Utilities.enumsToStrs(builder.getCipherSuites()));69}70serverSocket.setNeedClientAuth(builder.getClientAuth());71SSLParameters sslParams = serverSocket.getSSLParameters();72if (builder.getServerNames() != null) {73List<SNIMatcher> matchers = new ArrayList<>();74for(String bufServerName : builder.getServerNames()) {75matchers.add(SNIHostName.createSNIMatcher(bufServerName));76}77sslParams.setSNIMatchers(matchers);78}79if (builder.getAppProtocols() != null) {80sslParams.setApplicationProtocols(builder.getAppProtocols());81for (String appProtocol : sslParams.getApplicationProtocols()) {82System.out.println("appProtocol: " + appProtocol);83}84}85serverSocket.setSSLParameters(sslParams);86}8788public static class Builder extends AbstractServer.Builder {8990@Override91public JdkServer build() throws Exception {92return new JdkServer(this);93}94}9596@Override97public Product getProduct() {98return Jdk.DEFAULT;99}100101@Override102public int getPort() {103return serverSocket.getLocalPort();104}105106@Override107public void accept() throws IOException {108while (true) {109try (SSLSocket socket = (SSLSocket) serverSocket.accept()) {110this.socket = socket;111System.out.println("Server accepted connection");112113String request = Utilities.readIn(socket.getInputStream());114System.out.printf("Server received request: [%s]%n", request);115116if (response != null) {117Utilities.writeOut(socket.getOutputStream(), response);118System.out.printf("Server sent response: [%s]%n", response);119}120}121}122}123124private synchronized SSLSocket getSocket() {125return socket;126}127128public SSLSession getSession() {129return getSocket().getSession();130}131132@Override133public String getNegoAppProtocol() throws SSLTestException {134return getSocket().getApplicationProtocol();135}136137@Override138public boolean isAlive() {139return !serverSocket.isClosed();140}141142@Override143public void close() throws IOException {144if (!serverSocket.isClosed()) {145serverSocket.close();146}147}148}149150151