46157
|
1 |
/*
|
49765
|
2 |
* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
|
46157
|
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. Oracle designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
|
23 |
* questions.
|
|
24 |
*/
|
|
25 |
|
49765
|
26 |
package jdk.internal.net.http;
|
46157
|
27 |
|
|
28 |
import java.net.InetSocketAddress;
|
48083
|
29 |
import java.util.Arrays;
|
|
30 |
import java.util.List;
|
46157
|
31 |
import java.util.concurrent.CompletableFuture;
|
48083
|
32 |
import javax.net.ssl.SNIHostName;
|
|
33 |
import javax.net.ssl.SSLContext;
|
46157
|
34 |
import javax.net.ssl.SSLEngine;
|
48083
|
35 |
import javax.net.ssl.SSLParameters;
|
|
36 |
|
49765
|
37 |
import jdk.internal.net.http.common.SSLTube;
|
|
38 |
import jdk.internal.net.http.common.Log;
|
|
39 |
import jdk.internal.net.http.common.Utils;
|
|
40 |
import static jdk.internal.net.http.common.Utils.ServerName;
|
46157
|
41 |
|
|
42 |
/**
|
|
43 |
* Asynchronous version of SSLConnection.
|
|
44 |
*
|
|
45 |
* There are two concrete implementations of this class: AsyncSSLConnection
|
|
46 |
* and AsyncSSLTunnelConnection.
|
|
47 |
* This abstraction is useful when downgrading from HTTP/2 to HTTP/1.1 over
|
|
48 |
* an SSL connection. See ExchangeImpl::get in the case where an ALPNException
|
|
49 |
* is thrown.
|
|
50 |
*
|
|
51 |
* Note: An AsyncSSLConnection wraps a PlainHttpConnection, while an
|
|
52 |
* AsyncSSLTunnelConnection wraps a PlainTunnelingConnection.
|
|
53 |
* If both these wrapped classes where made to inherit from a
|
|
54 |
* common abstraction then it might be possible to merge
|
|
55 |
* AsyncSSLConnection and AsyncSSLTunnelConnection back into
|
|
56 |
* a single class - and simply use different factory methods to
|
|
57 |
* create different wrappees, but this is left up for further cleanup.
|
|
58 |
*
|
|
59 |
*/
|
|
60 |
abstract class AbstractAsyncSSLConnection extends HttpConnection
|
48083
|
61 |
{
|
|
62 |
protected final SSLEngine engine;
|
|
63 |
protected final String serverName;
|
|
64 |
protected final SSLParameters sslParameters;
|
46157
|
65 |
|
49765
|
66 |
// Setting this property disables HTTPS hostname verification. Use with care.
|
|
67 |
private static final boolean disableHostnameVerification
|
|
68 |
= Utils.isHostnameVerificationDisabled();
|
|
69 |
|
48083
|
70 |
AbstractAsyncSSLConnection(InetSocketAddress addr,
|
|
71 |
HttpClientImpl client,
|
49765
|
72 |
ServerName serverName, int port,
|
48083
|
73 |
String[] alpn) {
|
46157
|
74 |
super(addr, client);
|
49765
|
75 |
this.serverName = serverName.getName();
|
48083
|
76 |
SSLContext context = client.theSSLContext();
|
|
77 |
sslParameters = createSSLParameters(client, serverName, alpn);
|
|
78 |
Log.logParams(sslParameters);
|
49765
|
79 |
engine = createEngine(context, serverName.getName(), port, sslParameters);
|
48083
|
80 |
}
|
|
81 |
|
|
82 |
abstract HttpConnection plainConnection();
|
|
83 |
abstract SSLTube getConnectionFlow();
|
|
84 |
|
|
85 |
final CompletableFuture<String> getALPN() {
|
|
86 |
assert connected();
|
|
87 |
return getConnectionFlow().getALPN();
|
46157
|
88 |
}
|
|
89 |
|
48083
|
90 |
final SSLEngine getEngine() { return engine; }
|
|
91 |
|
|
92 |
private static SSLParameters createSSLParameters(HttpClientImpl client,
|
49765
|
93 |
ServerName serverName,
|
48083
|
94 |
String[] alpn) {
|
|
95 |
SSLParameters sslp = client.sslParameters();
|
|
96 |
SSLParameters sslParameters = Utils.copySSLParameters(sslp);
|
49765
|
97 |
if (!disableHostnameVerification)
|
|
98 |
sslParameters.setEndpointIdentificationAlgorithm("HTTPS");
|
48083
|
99 |
if (alpn != null) {
|
|
100 |
Log.logSSL("AbstractAsyncSSLConnection: Setting application protocols: {0}",
|
|
101 |
Arrays.toString(alpn));
|
|
102 |
sslParameters.setApplicationProtocols(alpn);
|
|
103 |
} else {
|
|
104 |
Log.logSSL("AbstractAsyncSSLConnection: no applications set!");
|
|
105 |
}
|
49765
|
106 |
if (!serverName.isLiteral()) {
|
|
107 |
String name = serverName.getName();
|
|
108 |
if (name != null && name.length() > 0) {
|
|
109 |
sslParameters.setServerNames(List.of(new SNIHostName(name)));
|
|
110 |
}
|
48083
|
111 |
}
|
|
112 |
return sslParameters;
|
|
113 |
}
|
|
114 |
|
49765
|
115 |
private static SSLEngine createEngine(SSLContext context, String serverName, int port,
|
48083
|
116 |
SSLParameters sslParameters) {
|
49765
|
117 |
SSLEngine engine = context.createSSLEngine(serverName, port);
|
48083
|
118 |
engine.setUseClientMode(true);
|
|
119 |
engine.setSSLParameters(sslParameters);
|
|
120 |
return engine;
|
|
121 |
}
|
46157
|
122 |
|
|
123 |
@Override
|
|
124 |
final boolean isSecure() {
|
|
125 |
return true;
|
|
126 |
}
|
|
127 |
|
|
128 |
}
|