author | michaelm |
Wed, 04 Jul 2018 16:16:24 +0100 | |
changeset 50985 | cd41f34e548c |
parent 49765 | ee6f7a61f3a5 |
child 51364 | 31d9e82b2e64 |
child 56833 | be0819373531 |
permissions | -rw-r--r-- |
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; |
50985
cd41f34e548c
8206001: Enable TLS1.3 by default in Http Client
michaelm
parents:
49765
diff
changeset
|
30 |
import java.util.ArrayDeque; |
48083 | 31 |
import java.util.List; |
46157 | 32 |
import java.util.concurrent.CompletableFuture; |
48083 | 33 |
import javax.net.ssl.SNIHostName; |
34 |
import javax.net.ssl.SSLContext; |
|
46157 | 35 |
import javax.net.ssl.SSLEngine; |
48083 | 36 |
import javax.net.ssl.SSLParameters; |
37 |
||
49765 | 38 |
import jdk.internal.net.http.common.SSLTube; |
39 |
import jdk.internal.net.http.common.Log; |
|
40 |
import jdk.internal.net.http.common.Utils; |
|
41 |
import static jdk.internal.net.http.common.Utils.ServerName; |
|
46157 | 42 |
|
43 |
/** |
|
44 |
* Asynchronous version of SSLConnection. |
|
45 |
* |
|
46 |
* There are two concrete implementations of this class: AsyncSSLConnection |
|
47 |
* and AsyncSSLTunnelConnection. |
|
48 |
* This abstraction is useful when downgrading from HTTP/2 to HTTP/1.1 over |
|
49 |
* an SSL connection. See ExchangeImpl::get in the case where an ALPNException |
|
50 |
* is thrown. |
|
51 |
* |
|
52 |
* Note: An AsyncSSLConnection wraps a PlainHttpConnection, while an |
|
53 |
* AsyncSSLTunnelConnection wraps a PlainTunnelingConnection. |
|
54 |
* If both these wrapped classes where made to inherit from a |
|
55 |
* common abstraction then it might be possible to merge |
|
56 |
* AsyncSSLConnection and AsyncSSLTunnelConnection back into |
|
57 |
* a single class - and simply use different factory methods to |
|
58 |
* create different wrappees, but this is left up for further cleanup. |
|
59 |
* |
|
60 |
*/ |
|
61 |
abstract class AbstractAsyncSSLConnection extends HttpConnection |
|
48083 | 62 |
{ |
63 |
protected final SSLEngine engine; |
|
64 |
protected final String serverName; |
|
65 |
protected final SSLParameters sslParameters; |
|
46157 | 66 |
|
49765 | 67 |
// Setting this property disables HTTPS hostname verification. Use with care. |
68 |
private static final boolean disableHostnameVerification |
|
69 |
= Utils.isHostnameVerificationDisabled(); |
|
70 |
||
48083 | 71 |
AbstractAsyncSSLConnection(InetSocketAddress addr, |
72 |
HttpClientImpl client, |
|
49765 | 73 |
ServerName serverName, int port, |
48083 | 74 |
String[] alpn) { |
46157 | 75 |
super(addr, client); |
49765 | 76 |
this.serverName = serverName.getName(); |
48083 | 77 |
SSLContext context = client.theSSLContext(); |
78 |
sslParameters = createSSLParameters(client, serverName, alpn); |
|
79 |
Log.logParams(sslParameters); |
|
49765 | 80 |
engine = createEngine(context, serverName.getName(), port, sslParameters); |
48083 | 81 |
} |
82 |
||
83 |
abstract HttpConnection plainConnection(); |
|
84 |
abstract SSLTube getConnectionFlow(); |
|
85 |
||
86 |
final CompletableFuture<String> getALPN() { |
|
87 |
assert connected(); |
|
88 |
return getConnectionFlow().getALPN(); |
|
46157 | 89 |
} |
90 |
||
48083 | 91 |
final SSLEngine getEngine() { return engine; } |
92 |
||
50985
cd41f34e548c
8206001: Enable TLS1.3 by default in Http Client
michaelm
parents:
49765
diff
changeset
|
93 |
private static boolean contains(String[] rr, String target) { |
cd41f34e548c
8206001: Enable TLS1.3 by default in Http Client
michaelm
parents:
49765
diff
changeset
|
94 |
for (String s : rr) |
cd41f34e548c
8206001: Enable TLS1.3 by default in Http Client
michaelm
parents:
49765
diff
changeset
|
95 |
if (target.equalsIgnoreCase(s)) |
cd41f34e548c
8206001: Enable TLS1.3 by default in Http Client
michaelm
parents:
49765
diff
changeset
|
96 |
return true; |
cd41f34e548c
8206001: Enable TLS1.3 by default in Http Client
michaelm
parents:
49765
diff
changeset
|
97 |
return false; |
cd41f34e548c
8206001: Enable TLS1.3 by default in Http Client
michaelm
parents:
49765
diff
changeset
|
98 |
} |
cd41f34e548c
8206001: Enable TLS1.3 by default in Http Client
michaelm
parents:
49765
diff
changeset
|
99 |
|
48083 | 100 |
private static SSLParameters createSSLParameters(HttpClientImpl client, |
49765 | 101 |
ServerName serverName, |
48083 | 102 |
String[] alpn) { |
103 |
SSLParameters sslp = client.sslParameters(); |
|
104 |
SSLParameters sslParameters = Utils.copySSLParameters(sslp); |
|
50985
cd41f34e548c
8206001: Enable TLS1.3 by default in Http Client
michaelm
parents:
49765
diff
changeset
|
105 |
// filter out unwanted protocols, if h2 only |
cd41f34e548c
8206001: Enable TLS1.3 by default in Http Client
michaelm
parents:
49765
diff
changeset
|
106 |
if (alpn != null && alpn.length != 0 && !contains(alpn, "http/1.1")) { |
cd41f34e548c
8206001: Enable TLS1.3 by default in Http Client
michaelm
parents:
49765
diff
changeset
|
107 |
ArrayDeque<String> l = new ArrayDeque<>(); |
cd41f34e548c
8206001: Enable TLS1.3 by default in Http Client
michaelm
parents:
49765
diff
changeset
|
108 |
for (String proto : sslParameters.getProtocols()) { |
cd41f34e548c
8206001: Enable TLS1.3 by default in Http Client
michaelm
parents:
49765
diff
changeset
|
109 |
if (!proto.startsWith("SSL") && !proto.endsWith("v1.1") && !proto.endsWith("v1")) { |
cd41f34e548c
8206001: Enable TLS1.3 by default in Http Client
michaelm
parents:
49765
diff
changeset
|
110 |
l.add(proto); |
cd41f34e548c
8206001: Enable TLS1.3 by default in Http Client
michaelm
parents:
49765
diff
changeset
|
111 |
} |
cd41f34e548c
8206001: Enable TLS1.3 by default in Http Client
michaelm
parents:
49765
diff
changeset
|
112 |
} |
cd41f34e548c
8206001: Enable TLS1.3 by default in Http Client
michaelm
parents:
49765
diff
changeset
|
113 |
String[] a1 = l.toArray(new String[0]); |
cd41f34e548c
8206001: Enable TLS1.3 by default in Http Client
michaelm
parents:
49765
diff
changeset
|
114 |
sslParameters.setProtocols(a1); |
cd41f34e548c
8206001: Enable TLS1.3 by default in Http Client
michaelm
parents:
49765
diff
changeset
|
115 |
} |
cd41f34e548c
8206001: Enable TLS1.3 by default in Http Client
michaelm
parents:
49765
diff
changeset
|
116 |
|
49765 | 117 |
if (!disableHostnameVerification) |
118 |
sslParameters.setEndpointIdentificationAlgorithm("HTTPS"); |
|
48083 | 119 |
if (alpn != null) { |
120 |
Log.logSSL("AbstractAsyncSSLConnection: Setting application protocols: {0}", |
|
121 |
Arrays.toString(alpn)); |
|
122 |
sslParameters.setApplicationProtocols(alpn); |
|
123 |
} else { |
|
124 |
Log.logSSL("AbstractAsyncSSLConnection: no applications set!"); |
|
125 |
} |
|
49765 | 126 |
if (!serverName.isLiteral()) { |
127 |
String name = serverName.getName(); |
|
128 |
if (name != null && name.length() > 0) { |
|
129 |
sslParameters.setServerNames(List.of(new SNIHostName(name))); |
|
130 |
} |
|
48083 | 131 |
} |
132 |
return sslParameters; |
|
133 |
} |
|
134 |
||
50985
cd41f34e548c
8206001: Enable TLS1.3 by default in Http Client
michaelm
parents:
49765
diff
changeset
|
135 |
|
49765 | 136 |
private static SSLEngine createEngine(SSLContext context, String serverName, int port, |
48083 | 137 |
SSLParameters sslParameters) { |
49765 | 138 |
SSLEngine engine = context.createSSLEngine(serverName, port); |
48083 | 139 |
engine.setUseClientMode(true); |
50985
cd41f34e548c
8206001: Enable TLS1.3 by default in Http Client
michaelm
parents:
49765
diff
changeset
|
140 |
|
48083 | 141 |
engine.setSSLParameters(sslParameters); |
142 |
return engine; |
|
143 |
} |
|
46157 | 144 |
|
145 |
@Override |
|
146 |
final boolean isSecure() { |
|
147 |
return true; |
|
148 |
} |
|
149 |
||
150 |
} |