1 /*
 2  * Copyright (c) 2023, 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 package jdk.httpclient.test.lib.common;
24 
25 import java.net.InetAddress;
26 import java.security.AccessController;
27 import java.security.PrivilegedAction;
28 import java.util.List;
29 
30 import javax.net.ssl.SNIMatcher;
31 import javax.net.ssl.SSLContext;
32 import javax.net.ssl.SSLParameters;
33 
34 import com.sun.net.httpserver.HttpsConfigurator;
35 import com.sun.net.httpserver.HttpsParameters;
36 
37 /**
38  * A {@link HttpsConfigurator} that can be used with the HTTP1 test server over HTTPS.
39  * This configurator {@link #configure(HttpsParameters) configures} the server's
40  * {@link HttpsParameters} with the necessary {@link SSLParameters} including a
41  * {@link SNIMatcher}
42  */
43 public final class TestServerConfigurator extends HttpsConfigurator {
44 
45     private final InetAddress serverAddr;
46 
47     /**
48      * Creates a Https configuration, with the given {@link SSLContext}.
49      *
50      * @param serverAddr the address to which the server is bound
51      * @param context    the {@code SSLContext} to use for this configurator
52      * @throws NullPointerException if no {@code SSLContext} supplied
53      */
54     public TestServerConfigurator(final InetAddress serverAddr, final SSLContext context) {
55         super(context);
56         this.serverAddr = serverAddr;
57     }
58 
59     @Override
60     public void configure(final HttpsParameters params) {
61         final SSLParameters sslParams = getSSLContext().getDefaultSSLParameters();
62         @SuppressWarnings("removal") final SecurityManager sm = System.getSecurityManager();
63         final String hostname;
64         if (sm == null) {
65             hostname = serverAddr.getHostName();
66         } else {
67             final PrivilegedAction<String> action = () -> serverAddr.getHostName();
68             hostname = AccessController.doPrivileged(action);
69         }
70         final List<SNIMatcher> sniMatchers = List.of(new ServerNameMatcher(hostname));
71         sslParams.setSNIMatchers(sniMatchers);
72         // configure the server with these custom SSLParameters
73         params.setSSLParameters(sslParams);
74     }
75 }