author | chegar |
Mon, 16 Apr 2018 16:44:12 +0100 | |
branch | http-client-branch |
changeset 56437 | f8b3f053cfbb |
parent 56368 | c10279a27b41 |
child 56451 | 9585061fdb04 |
permissions | -rw-r--r-- |
46157 | 1 |
/* |
56079
d23b02f37fce
http-client-branch: more remaining impl types to internal
chegar
parents:
55973
diff
changeset
|
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 |
||
56092
fd85b2bf2b0d
http-client-branch: move implementation to jdk.internal.net.http
chegar
parents:
56089
diff
changeset
|
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 |
||
56092
fd85b2bf2b0d
http-client-branch: move implementation to jdk.internal.net.http
chegar
parents:
56089
diff
changeset
|
37 |
import jdk.internal.net.http.common.SSLTube; |
fd85b2bf2b0d
http-client-branch: move implementation to jdk.internal.net.http
chegar
parents:
56089
diff
changeset
|
38 |
import jdk.internal.net.http.common.Log; |
fd85b2bf2b0d
http-client-branch: move implementation to jdk.internal.net.http
chegar
parents:
56089
diff
changeset
|
39 |
import jdk.internal.net.http.common.Utils; |
56368
c10279a27b41
http-client-branch: Fixed 8200351: problem verifying certificate of literal IP address connection
michaelm
parents:
56335
diff
changeset
|
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 |
|
56137
dd867826d55b
http-client-branch: added system property to disable hostname verification for testing
michaelm
parents:
56126
diff
changeset
|
66 |
// Setting this property disables HTTPS hostname verification. Use with care. |
56437
f8b3f053cfbb
http-client-branch: review comment - update debug statements as per new internal DebugLogger
chegar
parents:
56368
diff
changeset
|
67 |
private static final boolean disableHostnameVerification |
f8b3f053cfbb
http-client-branch: review comment - update debug statements as per new internal DebugLogger
chegar
parents:
56368
diff
changeset
|
68 |
= Utils.isHostnameVerificationDisabled(); |
56137
dd867826d55b
http-client-branch: added system property to disable hostname verification for testing
michaelm
parents:
56126
diff
changeset
|
69 |
|
48083 | 70 |
AbstractAsyncSSLConnection(InetSocketAddress addr, |
71 |
HttpClientImpl client, |
|
56368
c10279a27b41
http-client-branch: Fixed 8200351: problem verifying certificate of literal IP address connection
michaelm
parents:
56335
diff
changeset
|
72 |
ServerName serverName, int port, |
48083 | 73 |
String[] alpn) { |
46157 | 74 |
super(addr, client); |
56368
c10279a27b41
http-client-branch: Fixed 8200351: problem verifying certificate of literal IP address connection
michaelm
parents:
56335
diff
changeset
|
75 |
this.serverName = serverName.getName(); |
48083 | 76 |
SSLContext context = client.theSSLContext(); |
77 |
sslParameters = createSSLParameters(client, serverName, alpn); |
|
78 |
Log.logParams(sslParameters); |
|
56368
c10279a27b41
http-client-branch: Fixed 8200351: problem verifying certificate of literal IP address connection
michaelm
parents:
56335
diff
changeset
|
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, |
|
56368
c10279a27b41
http-client-branch: Fixed 8200351: problem verifying certificate of literal IP address connection
michaelm
parents:
56335
diff
changeset
|
93 |
ServerName serverName, |
48083 | 94 |
String[] alpn) { |
95 |
SSLParameters sslp = client.sslParameters(); |
|
96 |
SSLParameters sslParameters = Utils.copySSLParameters(sslp); |
|
56137
dd867826d55b
http-client-branch: added system property to disable hostname verification for testing
michaelm
parents:
56126
diff
changeset
|
97 |
if (!disableHostnameVerification) |
dd867826d55b
http-client-branch: added system property to disable hostname verification for testing
michaelm
parents:
56126
diff
changeset
|
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 |
} |
|
56368
c10279a27b41
http-client-branch: Fixed 8200351: problem verifying certificate of literal IP address connection
michaelm
parents:
56335
diff
changeset
|
106 |
if (!serverName.isLiteral()) { |
c10279a27b41
http-client-branch: Fixed 8200351: problem verifying certificate of literal IP address connection
michaelm
parents:
56335
diff
changeset
|
107 |
String name = serverName.getName(); |
c10279a27b41
http-client-branch: Fixed 8200351: problem verifying certificate of literal IP address connection
michaelm
parents:
56335
diff
changeset
|
108 |
if (name != null && name.length() > 0) { |
c10279a27b41
http-client-branch: Fixed 8200351: problem verifying certificate of literal IP address connection
michaelm
parents:
56335
diff
changeset
|
109 |
sslParameters.setServerNames(List.of(new SNIHostName(name))); |
c10279a27b41
http-client-branch: Fixed 8200351: problem verifying certificate of literal IP address connection
michaelm
parents:
56335
diff
changeset
|
110 |
} |
48083 | 111 |
} |
112 |
return sslParameters; |
|
113 |
} |
|
114 |
||
56126
86e628130926
http-client-branch: fixed TLS hostname checking issue, SSL session reuse, and changed HttpResponse to return SSLSession
michaelm
parents:
56092
diff
changeset
|
115 |
private static SSLEngine createEngine(SSLContext context, String serverName, int port, |
48083 | 116 |
SSLParameters sslParameters) { |
56126
86e628130926
http-client-branch: fixed TLS hostname checking issue, SSL session reuse, and changed HttpResponse to return SSLSession
michaelm
parents:
56092
diff
changeset
|
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 |
} |