author | xuelei |
Fri, 22 Apr 2016 09:27:35 +0000 | |
changeset 37601 | 6517589dcc94 |
parent 36641 | cbff2fd02f9e |
child 38865 | 429c08bd6158 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
36641
cbff2fd02f9e
8149169: SSLSocketInputRecord.decodeInputRecord buffer overflow
clanger
parents:
34958
diff
changeset
|
2 |
* Copyright (c) 1996, 2016, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 24 |
*/ |
25 |
||
26 |
||
27 |
package sun.security.ssl; |
|
28 |
||
29 |
import java.io.*; |
|
30904 | 30 |
import java.nio.*; |
2 | 31 |
import java.net.*; |
32 |
import java.security.GeneralSecurityException; |
|
33 |
import java.security.AccessController; |
|
34 |
import java.security.AccessControlContext; |
|
35 |
import java.security.PrivilegedAction; |
|
7043 | 36 |
import java.security.AlgorithmConstraints; |
2 | 37 |
import java.util.*; |
100
01ef29ca378f
6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
2
diff
changeset
|
38 |
import java.util.concurrent.TimeUnit; |
01ef29ca378f
6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
2
diff
changeset
|
39 |
import java.util.concurrent.locks.ReentrantLock; |
2 | 40 |
|
41 |
import javax.crypto.BadPaddingException; |
|
42 |
import javax.net.ssl.*; |
|
43 |
||
32834
e1dca5fe4de3
8137056: Move SharedSecrets and interface friends out of sun.misc
chegar
parents:
32649
diff
changeset
|
44 |
import jdk.internal.misc.JavaNetInetAddressAccess; |
e1dca5fe4de3
8137056: Move SharedSecrets and interface friends out of sun.misc
chegar
parents:
32649
diff
changeset
|
45 |
import jdk.internal.misc.SharedSecrets; |
31687 | 46 |
|
2 | 47 |
/** |
48 |
* Implementation of an SSL socket. This is a normal connection type |
|
49 |
* socket, implementing SSL over some lower level socket, such as TCP. |
|
50 |
* Because it is layered over some lower level socket, it MUST override |
|
51 |
* all default socket methods. |
|
52 |
* |
|
53 |
* <P> This API offers a non-traditional option for establishing SSL |
|
54 |
* connections. You may first establish the connection directly, then pass |
|
55 |
* that connection to the SSL socket constructor with a flag saying which |
|
56 |
* role should be taken in the handshake protocol. (The two ends of the |
|
57 |
* connection must not choose the same role!) This allows setup of SSL |
|
58 |
* proxying or tunneling, and also allows the kind of "role reversal" |
|
59 |
* that is required for most FTP data transfers. |
|
60 |
* |
|
61 |
* @see javax.net.ssl.SSLSocket |
|
62 |
* @see SSLServerSocket |
|
63 |
* |
|
64 |
* @author David Brownell |
|
65 |
*/ |
|
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
31918
diff
changeset
|
66 |
public final class SSLSocketImpl extends BaseSSLSocketImpl { |
2 | 67 |
|
68 |
/* |
|
69 |
* ERROR HANDLING GUIDELINES |
|
70 |
* (which exceptions to throw and catch and which not to throw and catch) |
|
71 |
* |
|
72 |
* . if there is an IOException (SocketException) when accessing the |
|
73 |
* underlying Socket, pass it through |
|
74 |
* |
|
75 |
* . do not throw IOExceptions, throw SSLExceptions (or a subclass) |
|
76 |
* |
|
77 |
* . for internal errors (things that indicate a bug in JSSE or a |
|
78 |
* grossly misconfigured J2RE), throw either an SSLException or |
|
79 |
* a RuntimeException at your convenience. |
|
80 |
* |
|
81 |
* . handshaking code (Handshaker or HandshakeMessage) should generally |
|
82 |
* pass through exceptions, but can handle them if they know what to |
|
83 |
* do. |
|
84 |
* |
|
85 |
* . exception chaining should be used for all new code. If you happen |
|
86 |
* to touch old code that does not use chaining, you should change it. |
|
87 |
* |
|
88 |
* . there is a top level exception handler that sits at all entry |
|
89 |
* points from application code to SSLSocket read/write code. It |
|
90 |
* makes sure that all errors are handled (see handleException()). |
|
91 |
* |
|
92 |
* . JSSE internal code should generally not call close(), call |
|
93 |
* closeInternal(). |
|
94 |
*/ |
|
95 |
||
96 |
/* |
|
97 |
* There's a state machine associated with each connection, which |
|
98 |
* among other roles serves to negotiate session changes. |
|
99 |
* |
|
100 |
* - START with constructor, until the TCP connection's around. |
|
101 |
* - HANDSHAKE picks session parameters before allowing traffic. |
|
102 |
* There are many substates due to sequencing requirements |
|
103 |
* for handshake messages. |
|
104 |
* - DATA may be transmitted. |
|
105 |
* - RENEGOTIATE state allows concurrent data and handshaking |
|
106 |
* traffic ("same" substates as HANDSHAKE), and terminates |
|
107 |
* in selection of new session (and connection) parameters |
|
108 |
* - ERROR state immediately precedes abortive disconnect. |
|
109 |
* - SENT_CLOSE sent a close_notify to the peer. For layered, |
|
110 |
* non-autoclose socket, must now read close_notify |
|
111 |
* from peer before closing the connection. For nonlayered or |
|
112 |
* non-autoclose socket, close connection and go onto |
|
113 |
* cs_CLOSED state. |
|
114 |
* - CLOSED after sending close_notify alert, & socket is closed. |
|
115 |
* SSL connection objects are not reused. |
|
116 |
* - APP_CLOSED once the application calls close(). Then it behaves like |
|
117 |
* a closed socket, e.g.. getInputStream() throws an Exception. |
|
118 |
* |
|
119 |
* State affects what SSL record types may legally be sent: |
|
120 |
* |
|
121 |
* - Handshake ... only in HANDSHAKE and RENEGOTIATE states |
|
122 |
* - App Data ... only in DATA and RENEGOTIATE states |
|
123 |
* - Alert ... in HANDSHAKE, DATA, RENEGOTIATE |
|
124 |
* |
|
125 |
* Re what may be received: same as what may be sent, except that |
|
126 |
* HandshakeRequest handshaking messages can come from servers even |
|
127 |
* in the application data state, to request entry to RENEGOTIATE. |
|
128 |
* |
|
129 |
* The state machine within HANDSHAKE and RENEGOTIATE states controls |
|
130 |
* the pending session, not the connection state, until the change |
|
131 |
* cipher spec and "Finished" handshake messages are processed and |
|
132 |
* make the "new" session become the current one. |
|
133 |
* |
|
134 |
* NOTE: details of the SMs always need to be nailed down better. |
|
135 |
* The text above illustrates the core ideas. |
|
136 |
* |
|
137 |
* +---->-------+------>--------->-------+ |
|
138 |
* | | | |
|
139 |
* <-----< ^ ^ <-----< v |
|
140 |
*START>----->HANDSHAKE>----->DATA>----->RENEGOTIATE SENT_CLOSE |
|
141 |
* v v v | | |
|
142 |
* | | | | v |
|
143 |
* +------------+---------------+ v ERROR |
|
144 |
* | | | |
|
145 |
* v | | |
|
146 |
* ERROR>------>----->CLOSED<--------<----+-- + |
|
147 |
* | |
|
148 |
* v |
|
149 |
* APP_CLOSED |
|
150 |
* |
|
28059
e576535359cc
8067377: My hobby: caning, then then canning, the the can-can
martin
parents:
25859
diff
changeset
|
151 |
* ALSO, note that the purpose of handshaking (renegotiation is |
2 | 152 |
* included) is to assign a different, and perhaps new, session to |
153 |
* the connection. The SSLv3 spec is a bit confusing on that new |
|
154 |
* protocol feature. |
|
155 |
*/ |
|
156 |
private static final int cs_START = 0; |
|
157 |
private static final int cs_HANDSHAKE = 1; |
|
158 |
private static final int cs_DATA = 2; |
|
159 |
private static final int cs_RENEGOTIATE = 3; |
|
160 |
private static final int cs_ERROR = 4; |
|
30904 | 161 |
private static final int cs_SENT_CLOSE = 5; |
2 | 162 |
private static final int cs_CLOSED = 6; |
163 |
private static final int cs_APP_CLOSED = 7; |
|
164 |
||
165 |
/* |
|
166 |
* Drives the protocol state machine. |
|
167 |
*/ |
|
19223
e27cda06fe6a
8013809: deadlock in SSLSocketImpl between between write and close
xuelei
parents:
16913
diff
changeset
|
168 |
private volatile int connectionState; |
2 | 169 |
|
170 |
/* |
|
171 |
* Flag indicating if the next record we receive MUST be a Finished |
|
172 |
* message. Temporarily set during the handshake to ensure that |
|
173 |
* a change cipher spec message is followed by a finished message. |
|
174 |
*/ |
|
175 |
private boolean expectingFinished; |
|
176 |
||
177 |
/* |
|
178 |
* For improved diagnostics, we detail connection closure |
|
179 |
* If the socket is closed (connectionState >= cs_ERROR), |
|
180 |
* closeReason != null indicates if the socket was closed |
|
181 |
* because of an error or because or normal shutdown. |
|
182 |
*/ |
|
183 |
private SSLException closeReason; |
|
184 |
||
185 |
/* |
|
186 |
* Per-connection private state that doesn't change when the |
|
187 |
* session is changed. |
|
188 |
*/ |
|
30904 | 189 |
private ClientAuthType doClientAuth = |
190 |
ClientAuthType.CLIENT_AUTH_NONE; |
|
2 | 191 |
private boolean roleIsServer; |
192 |
private boolean enableSessionCreation = true; |
|
193 |
private String host; |
|
194 |
private boolean autoClose = true; |
|
195 |
private AccessControlContext acc; |
|
196 |
||
7039 | 197 |
// The cipher suites enabled for use on this connection. |
198 |
private CipherSuiteList enabledCipherSuites; |
|
199 |
||
7043 | 200 |
// The endpoint identification protocol |
201 |
private String identificationProtocol = null; |
|
202 |
||
203 |
// The cryptographic algorithm constraints |
|
204 |
private AlgorithmConstraints algorithmConstraints = null; |
|
2 | 205 |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
206 |
// The server name indication and matchers |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
207 |
List<SNIServerName> serverNames = |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
208 |
Collections.<SNIServerName>emptyList(); |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
209 |
Collection<SNIMatcher> sniMatchers = |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
210 |
Collections.<SNIMatcher>emptyList(); |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
211 |
|
37601
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
212 |
// Is the serverNames set to empty with SSLParameters.setServerNames()? |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
213 |
private boolean noSniExtension = false; |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
214 |
|
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
215 |
// Is the sniMatchers set to empty with SSLParameters.setSNIMatchers()? |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
216 |
private boolean noSniMatcher = false; |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
217 |
|
34380
2b2609379881
8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
32834
diff
changeset
|
218 |
// Configured application protocol values |
2b2609379881
8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
32834
diff
changeset
|
219 |
String[] applicationProtocols = new String[0]; |
2b2609379881
8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
32834
diff
changeset
|
220 |
|
2b2609379881
8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
32834
diff
changeset
|
221 |
// Negotiated application protocol value. |
2b2609379881
8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
32834
diff
changeset
|
222 |
// |
2b2609379881
8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
32834
diff
changeset
|
223 |
// The value under negotiation will be obtained from handshaker. |
2b2609379881
8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
32834
diff
changeset
|
224 |
String applicationProtocol = null; |
2b2609379881
8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
32834
diff
changeset
|
225 |
|
2 | 226 |
/* |
227 |
* READ ME * READ ME * READ ME * READ ME * READ ME * READ ME * |
|
228 |
* IMPORTANT STUFF TO UNDERSTANDING THE SYNCHRONIZATION ISSUES. |
|
229 |
* READ ME * READ ME * READ ME * READ ME * READ ME * READ ME * |
|
230 |
* |
|
231 |
* There are several locks here. |
|
232 |
* |
|
233 |
* The primary lock is the per-instance lock used by |
|
234 |
* synchronized(this) and the synchronized methods. It controls all |
|
235 |
* access to things such as the connection state and variables which |
|
236 |
* affect handshaking. If we are inside a synchronized method, we |
|
237 |
* can access the state directly, otherwise, we must use the |
|
238 |
* synchronized equivalents. |
|
239 |
* |
|
240 |
* The handshakeLock is used to ensure that only one thread performs |
|
241 |
* the *complete initial* handshake. If someone is handshaking, any |
|
242 |
* stray application or startHandshake() requests who find the |
|
243 |
* connection state is cs_HANDSHAKE will stall on handshakeLock |
|
244 |
* until handshaking is done. Once the handshake is done, we either |
|
245 |
* succeeded or failed, but we can never go back to the cs_HANDSHAKE |
|
246 |
* or cs_START state again. |
|
247 |
* |
|
248 |
* Note that the read/write() calls here in SSLSocketImpl are not |
|
249 |
* obviously synchronized. In fact, it's very nonintuitive, and |
|
250 |
* requires careful examination of code paths. Grab some coffee, |
|
251 |
* and be careful with any code changes. |
|
252 |
* |
|
253 |
* There can be only three threads active at a time in the I/O |
|
254 |
* subsection of this class. |
|
255 |
* 1. startHandshake |
|
256 |
* 2. AppInputStream |
|
257 |
* 3. AppOutputStream |
|
258 |
* One thread could call startHandshake(). |
|
259 |
* AppInputStream/AppOutputStream read() and write() calls are each |
|
260 |
* synchronized on 'this' in their respective classes, so only one |
|
261 |
* app. thread will be doing a SSLSocketImpl.read() or .write()'s at |
|
262 |
* a time. |
|
263 |
* |
|
264 |
* If handshaking is required (state cs_HANDSHAKE), and |
|
265 |
* getConnectionState() for some/all threads returns cs_HANDSHAKE, |
|
266 |
* only one can grab the handshakeLock, and the rest will stall |
|
267 |
* either on getConnectionState(), or on the handshakeLock if they |
|
268 |
* happen to successfully race through the getConnectionState(). |
|
269 |
* |
|
270 |
* If a writer is doing the initial handshaking, it must create a |
|
271 |
* temporary reader to read the responses from the other side. As a |
|
272 |
* side-effect, the writer's reader will have priority over any |
|
273 |
* other reader. However, the writer's reader is not allowed to |
|
274 |
* consume any application data. When handshakeLock is finally |
|
275 |
* released, we either have a cs_DATA connection, or a |
|
276 |
* cs_CLOSED/cs_ERROR socket. |
|
277 |
* |
|
278 |
* The writeLock is held while writing on a socket connection and |
|
279 |
* also to protect the MAC and cipher for their direction. The |
|
280 |
* writeLock is package private for Handshaker which holds it while |
|
281 |
* writing the ChangeCipherSpec message. |
|
282 |
* |
|
283 |
* To avoid the problem of a thread trying to change operational |
|
284 |
* modes on a socket while handshaking is going on, we synchronize |
|
285 |
* on 'this'. If handshaking has not started yet, we tell the |
|
286 |
* handshaker to change its mode. If handshaking has started, |
|
287 |
* we simply store that request until the next pending session |
|
288 |
* is created, at which time the new handshaker's state is set. |
|
289 |
* |
|
290 |
* The readLock is held during readRecord(), which is responsible |
|
30904 | 291 |
* for reading an SSLInputRecord, decrypting it, and processing it. |
2 | 292 |
* The readLock ensures that these three steps are done atomically |
30904 | 293 |
* and that once started, no other thread can block on SSLInputRecord.read. |
2 | 294 |
* This is necessary so that processing of close_notify alerts |
295 |
* from the peer are handled properly. |
|
296 |
*/ |
|
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
31918
diff
changeset
|
297 |
private final Object handshakeLock = new Object(); |
6856 | 298 |
final ReentrantLock writeLock = new ReentrantLock(); |
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
31918
diff
changeset
|
299 |
private final Object readLock = new Object(); |
2 | 300 |
|
30904 | 301 |
InputRecord inputRecord; |
302 |
OutputRecord outputRecord; |
|
2 | 303 |
|
304 |
/* |
|
6856 | 305 |
* security parameters for secure renegotiation. |
306 |
*/ |
|
307 |
private boolean secureRenegotiation; |
|
308 |
private byte[] clientVerifyData; |
|
309 |
private byte[] serverVerifyData; |
|
310 |
||
311 |
/* |
|
2 | 312 |
* The authentication context holds all information used to establish |
313 |
* who this end of the connection is (certificate chains, private keys, |
|
314 |
* etc) and who is trusted (e.g. as CAs or websites). |
|
315 |
*/ |
|
316 |
private SSLContextImpl sslContext; |
|
317 |
||
318 |
||
319 |
/* |
|
320 |
* This connection is one of (potentially) many associated with |
|
321 |
* any given session. The output of the handshake protocol is a |
|
322 |
* new session ... although all the protocol description talks |
|
323 |
* about changing the cipher spec (and it does change), in fact |
|
324 |
* that's incidental since it's done by changing everything that |
|
325 |
* is associated with a session at the same time. (TLS/IETF may |
|
326 |
* change that to add client authentication w/o new key exchg.) |
|
327 |
*/ |
|
7043 | 328 |
private Handshaker handshaker; |
329 |
private SSLSessionImpl sess; |
|
330 |
private volatile SSLSessionImpl handshakeSession; |
|
2 | 331 |
|
332 |
||
333 |
/* |
|
334 |
* If anyone wants to get notified about handshake completions, |
|
335 |
* they'll show up on this list. |
|
336 |
*/ |
|
337 |
private HashMap<HandshakeCompletedListener, AccessControlContext> |
|
338 |
handshakeListeners; |
|
339 |
||
340 |
/* |
|
341 |
* Reuse the same internal input/output streams. |
|
342 |
*/ |
|
343 |
private InputStream sockInput; |
|
344 |
private OutputStream sockOutput; |
|
345 |
||
346 |
||
347 |
/* |
|
348 |
* These input and output streams block their data in SSL records, |
|
349 |
* and usually arrange integrity and privacy protection for those |
|
350 |
* records. The guts of the SSL protocol are wrapped up in these |
|
351 |
* streams, and in the handshaking that establishes the details of |
|
352 |
* that integrity and privacy protection. |
|
353 |
*/ |
|
354 |
private AppInputStream input; |
|
355 |
private AppOutputStream output; |
|
356 |
||
357 |
/* |
|
7039 | 358 |
* The protocol versions enabled for use on this connection. |
359 |
* |
|
360 |
* Note: we support a pseudo protocol called SSLv2Hello which when |
|
361 |
* set will result in an SSL v2 Hello being sent with SSL (version 3.0) |
|
362 |
* or TLS (version 3.1, 3.2, etc.) version info. |
|
2 | 363 |
*/ |
364 |
private ProtocolList enabledProtocols; |
|
365 |
||
366 |
/* |
|
367 |
* The SSL version associated with this connection. |
|
368 |
*/ |
|
30904 | 369 |
private ProtocolVersion protocolVersion = ProtocolVersion.DEFAULT_TLS; |
2 | 370 |
|
371 |
/* Class and subclass dynamic debugging support */ |
|
372 |
private static final Debug debug = Debug.getInstance("ssl"); |
|
373 |
||
10915 | 374 |
/* |
19823
f0fd09e20528
7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents:
19223
diff
changeset
|
375 |
* Whether local cipher suites preference in server side should be |
f0fd09e20528
7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents:
19223
diff
changeset
|
376 |
* honored during handshaking? |
f0fd09e20528
7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents:
19223
diff
changeset
|
377 |
*/ |
f0fd09e20528
7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents:
19223
diff
changeset
|
378 |
private boolean preferLocalCipherSuites = false; |
f0fd09e20528
7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents:
19223
diff
changeset
|
379 |
|
30904 | 380 |
/* |
381 |
* The maximum expected network packet size for SSL/TLS/DTLS records. |
|
382 |
*/ |
|
383 |
private int maximumPacketSize = 0; |
|
384 |
||
31687 | 385 |
/* |
386 |
* Is the local name service trustworthy? |
|
387 |
* |
|
388 |
* If the local name service is not trustworthy, reverse host name |
|
389 |
* resolution should not be performed for endpoint identification. |
|
390 |
*/ |
|
391 |
static final boolean trustNameService = |
|
392 |
Debug.getBooleanProperty("jdk.tls.trustNameService", false); |
|
393 |
||
2 | 394 |
// |
395 |
// CONSTRUCTORS AND INITIALIZATION CODE |
|
396 |
// |
|
397 |
||
398 |
/** |
|
399 |
* Constructs an SSL connection to a named host at a specified port, |
|
400 |
* using the authentication context provided. This endpoint acts as |
|
401 |
* the client, and may rejoin an existing SSL session if appropriate. |
|
402 |
* |
|
403 |
* @param context authentication context to use |
|
404 |
* @param host name of the host with which to connect |
|
405 |
* @param port number of the server's port |
|
406 |
*/ |
|
407 |
SSLSocketImpl(SSLContextImpl context, String host, int port) |
|
408 |
throws IOException, UnknownHostException { |
|
409 |
super(); |
|
410 |
this.host = host; |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
411 |
this.serverNames = |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
412 |
Utilities.addToSNIServerNameList(this.serverNames, this.host); |
2 | 413 |
init(context, false); |
2068
cdbc5929b91e
5067458: Loopback SSLSocketImpl createSocket is throwing an exception
xuelei
parents:
1763
diff
changeset
|
414 |
SocketAddress socketAddress = |
cdbc5929b91e
5067458: Loopback SSLSocketImpl createSocket is throwing an exception
xuelei
parents:
1763
diff
changeset
|
415 |
host != null ? new InetSocketAddress(host, port) : |
cdbc5929b91e
5067458: Loopback SSLSocketImpl createSocket is throwing an exception
xuelei
parents:
1763
diff
changeset
|
416 |
new InetSocketAddress(InetAddress.getByName(null), port); |
2 | 417 |
connect(socketAddress, 0); |
418 |
} |
|
419 |
||
420 |
||
421 |
/** |
|
422 |
* Constructs an SSL connection to a server at a specified address. |
|
423 |
* and TCP port, using the authentication context provided. This |
|
424 |
* endpoint acts as the client, and may rejoin an existing SSL session |
|
425 |
* if appropriate. |
|
426 |
* |
|
427 |
* @param context authentication context to use |
|
428 |
* @param address the server's host |
|
429 |
* @param port its port |
|
430 |
*/ |
|
431 |
SSLSocketImpl(SSLContextImpl context, InetAddress host, int port) |
|
432 |
throws IOException { |
|
433 |
super(); |
|
434 |
init(context, false); |
|
435 |
SocketAddress socketAddress = new InetSocketAddress(host, port); |
|
436 |
connect(socketAddress, 0); |
|
437 |
} |
|
438 |
||
439 |
/** |
|
440 |
* Constructs an SSL connection to a named host at a specified port, |
|
441 |
* using the authentication context provided. This endpoint acts as |
|
442 |
* the client, and may rejoin an existing SSL session if appropriate. |
|
443 |
* |
|
444 |
* @param context authentication context to use |
|
445 |
* @param host name of the host with which to connect |
|
446 |
* @param port number of the server's port |
|
447 |
* @param localAddr the local address the socket is bound to |
|
448 |
* @param localPort the local port the socket is bound to |
|
449 |
*/ |
|
450 |
SSLSocketImpl(SSLContextImpl context, String host, int port, |
|
451 |
InetAddress localAddr, int localPort) |
|
452 |
throws IOException, UnknownHostException { |
|
453 |
super(); |
|
454 |
this.host = host; |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
455 |
this.serverNames = |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
456 |
Utilities.addToSNIServerNameList(this.serverNames, this.host); |
2 | 457 |
init(context, false); |
458 |
bind(new InetSocketAddress(localAddr, localPort)); |
|
2068
cdbc5929b91e
5067458: Loopback SSLSocketImpl createSocket is throwing an exception
xuelei
parents:
1763
diff
changeset
|
459 |
SocketAddress socketAddress = |
cdbc5929b91e
5067458: Loopback SSLSocketImpl createSocket is throwing an exception
xuelei
parents:
1763
diff
changeset
|
460 |
host != null ? new InetSocketAddress(host, port) : |
cdbc5929b91e
5067458: Loopback SSLSocketImpl createSocket is throwing an exception
xuelei
parents:
1763
diff
changeset
|
461 |
new InetSocketAddress(InetAddress.getByName(null), port); |
2 | 462 |
connect(socketAddress, 0); |
463 |
} |
|
464 |
||
465 |
||
466 |
/** |
|
467 |
* Constructs an SSL connection to a server at a specified address. |
|
468 |
* and TCP port, using the authentication context provided. This |
|
469 |
* endpoint acts as the client, and may rejoin an existing SSL session |
|
470 |
* if appropriate. |
|
471 |
* |
|
472 |
* @param context authentication context to use |
|
473 |
* @param address the server's host |
|
474 |
* @param port its port |
|
475 |
* @param localAddr the local address the socket is bound to |
|
476 |
* @param localPort the local port the socket is bound to |
|
477 |
*/ |
|
478 |
SSLSocketImpl(SSLContextImpl context, InetAddress host, int port, |
|
479 |
InetAddress localAddr, int localPort) |
|
480 |
throws IOException { |
|
481 |
super(); |
|
482 |
init(context, false); |
|
483 |
bind(new InetSocketAddress(localAddr, localPort)); |
|
484 |
SocketAddress socketAddress = new InetSocketAddress(host, port); |
|
485 |
connect(socketAddress, 0); |
|
486 |
} |
|
487 |
||
488 |
/* |
|
489 |
* Package-private constructor used ONLY by SSLServerSocket. The |
|
490 |
* java.net package accepts the TCP connection after this call is |
|
491 |
* made. This just initializes handshake state to use "server mode", |
|
492 |
* giving control over the use of SSL client authentication. |
|
493 |
*/ |
|
494 |
SSLSocketImpl(SSLContextImpl context, boolean serverMode, |
|
30904 | 495 |
CipherSuiteList suites, ClientAuthType clientAuth, |
7043 | 496 |
boolean sessionCreation, ProtocolList protocols, |
497 |
String identificationProtocol, |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
498 |
AlgorithmConstraints algorithmConstraints, |
19823
f0fd09e20528
7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents:
19223
diff
changeset
|
499 |
Collection<SNIMatcher> sniMatchers, |
f0fd09e20528
7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents:
19223
diff
changeset
|
500 |
boolean preferLocalCipherSuites) throws IOException { |
7043 | 501 |
|
2 | 502 |
super(); |
503 |
doClientAuth = clientAuth; |
|
504 |
enableSessionCreation = sessionCreation; |
|
7043 | 505 |
this.identificationProtocol = identificationProtocol; |
506 |
this.algorithmConstraints = algorithmConstraints; |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
507 |
this.sniMatchers = sniMatchers; |
19823
f0fd09e20528
7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents:
19223
diff
changeset
|
508 |
this.preferLocalCipherSuites = preferLocalCipherSuites; |
2 | 509 |
init(context, serverMode); |
510 |
||
511 |
/* |
|
512 |
* Override what was picked out for us. |
|
513 |
*/ |
|
514 |
enabledCipherSuites = suites; |
|
515 |
enabledProtocols = protocols; |
|
516 |
} |
|
517 |
||
518 |
||
519 |
/** |
|
520 |
* Package-private constructor used to instantiate an unconnected |
|
521 |
* socket. The java.net package will connect it, either when the |
|
522 |
* connect() call is made by the application. This instance is |
|
523 |
* meant to set handshake state to use "client mode". |
|
524 |
*/ |
|
525 |
SSLSocketImpl(SSLContextImpl context) { |
|
526 |
super(); |
|
527 |
init(context, false); |
|
528 |
} |
|
529 |
||
530 |
||
531 |
/** |
|
532 |
* Layer SSL traffic over an existing connection, rather than creating |
|
533 |
* a new connection. The existing connection may be used only for SSL |
|
534 |
* traffic (using this SSLSocket) until the SSLSocket.close() call |
|
535 |
* returns. However, if a protocol error is detected, that existing |
|
536 |
* connection is automatically closed. |
|
537 |
* |
|
538 |
* <P> This particular constructor always uses the socket in the |
|
539 |
* role of an SSL client. It may be useful in cases which start |
|
540 |
* using SSL after some initial data transfers, for example in some |
|
541 |
* SSL tunneling applications or as part of some kinds of application |
|
542 |
* protocols which negotiate use of a SSL based security. |
|
543 |
* |
|
544 |
* @param sock the existing connection |
|
545 |
* @param context the authentication context to use |
|
546 |
*/ |
|
547 |
SSLSocketImpl(SSLContextImpl context, Socket sock, String host, |
|
548 |
int port, boolean autoClose) throws IOException { |
|
549 |
super(sock); |
|
550 |
// We always layer over a connected socket |
|
551 |
if (!sock.isConnected()) { |
|
552 |
throw new SocketException("Underlying socket is not connected"); |
|
553 |
} |
|
554 |
this.host = host; |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
555 |
this.serverNames = |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
556 |
Utilities.addToSNIServerNameList(this.serverNames, this.host); |
2 | 557 |
init(context, false); |
558 |
this.autoClose = autoClose; |
|
559 |
doneConnect(); |
|
560 |
} |
|
561 |
||
562 |
/** |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
563 |
* Creates a server mode {@link Socket} layered over an |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
564 |
* existing connected socket, and is able to read data which has |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
565 |
* already been consumed/removed from the {@link Socket}'s |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
566 |
* underlying {@link InputStream}. |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
567 |
*/ |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
568 |
SSLSocketImpl(SSLContextImpl context, Socket sock, |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
569 |
InputStream consumed, boolean autoClose) throws IOException { |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
570 |
super(sock, consumed); |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
571 |
// We always layer over a connected socket |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
572 |
if (!sock.isConnected()) { |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
573 |
throw new SocketException("Underlying socket is not connected"); |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
574 |
} |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
575 |
|
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
576 |
// In server mode, it is not necessary to set host and serverNames. |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
577 |
// Otherwise, would require a reverse DNS lookup to get the hostname. |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
578 |
|
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
579 |
init(context, true); |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
580 |
this.autoClose = autoClose; |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
581 |
doneConnect(); |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
582 |
} |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
583 |
|
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
584 |
/** |
2 | 585 |
* Initializes the client socket. |
586 |
*/ |
|
587 |
private void init(SSLContextImpl context, boolean isServer) { |
|
588 |
sslContext = context; |
|
589 |
sess = SSLSessionImpl.nullSession; |
|
7043 | 590 |
handshakeSession = null; |
2 | 591 |
|
592 |
/* |
|
593 |
* role is as specified, state is START until after |
|
594 |
* the low level connection's established. |
|
595 |
*/ |
|
596 |
roleIsServer = isServer; |
|
597 |
connectionState = cs_START; |
|
598 |
||
6856 | 599 |
// initial security parameters for secure renegotiation |
600 |
secureRenegotiation = false; |
|
601 |
clientVerifyData = new byte[0]; |
|
602 |
serverVerifyData = new byte[0]; |
|
603 |
||
9246
c459f79af46b
6976117: SSLContext.getInstance("TLSv1.1") returns SSLEngines/SSLSockets without TLSv1.1 enabled
xuelei
parents:
7043
diff
changeset
|
604 |
enabledCipherSuites = |
c459f79af46b
6976117: SSLContext.getInstance("TLSv1.1") returns SSLEngines/SSLSockets without TLSv1.1 enabled
xuelei
parents:
7043
diff
changeset
|
605 |
sslContext.getDefaultCipherSuiteList(roleIsServer); |
c459f79af46b
6976117: SSLContext.getInstance("TLSv1.1") returns SSLEngines/SSLSockets without TLSv1.1 enabled
xuelei
parents:
7043
diff
changeset
|
606 |
enabledProtocols = |
c459f79af46b
6976117: SSLContext.getInstance("TLSv1.1") returns SSLEngines/SSLSockets without TLSv1.1 enabled
xuelei
parents:
7043
diff
changeset
|
607 |
sslContext.getDefaultProtocolList(roleIsServer); |
c459f79af46b
6976117: SSLContext.getInstance("TLSv1.1") returns SSLEngines/SSLSockets without TLSv1.1 enabled
xuelei
parents:
7043
diff
changeset
|
608 |
|
30904 | 609 |
inputRecord = new SSLSocketInputRecord();; |
610 |
outputRecord = new SSLSocketOutputRecord(); |
|
611 |
||
612 |
maximumPacketSize = outputRecord.getMaxPacketSize(); |
|
2 | 613 |
|
614 |
// save the acc |
|
615 |
acc = AccessController.getContext(); |
|
616 |
||
617 |
input = new AppInputStream(this); |
|
618 |
output = new AppOutputStream(this); |
|
619 |
} |
|
620 |
||
621 |
/** |
|
622 |
* Connects this socket to the server with a specified timeout |
|
623 |
* value. |
|
624 |
* |
|
625 |
* This method is either called on an unconnected SSLSocketImpl by the |
|
626 |
* application, or it is called in the constructor of a regular |
|
627 |
* SSLSocketImpl. If we are layering on top on another socket, then |
|
628 |
* this method should not be called, because we assume that the |
|
629 |
* underlying socket is already connected by the time it is passed to |
|
630 |
* us. |
|
631 |
* |
|
632 |
* @param endpoint the <code>SocketAddress</code> |
|
633 |
* @param timeout the timeout value to be used, 0 is no timeout |
|
634 |
* @throws IOException if an error occurs during the connection |
|
635 |
* @throws SocketTimeoutException if timeout expires before connecting |
|
636 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
637 |
@Override |
2 | 638 |
public void connect(SocketAddress endpoint, int timeout) |
639 |
throws IOException { |
|
640 |
||
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
641 |
if (isLayered()) { |
2 | 642 |
throw new SocketException("Already connected"); |
643 |
} |
|
644 |
||
645 |
if (!(endpoint instanceof InetSocketAddress)) { |
|
646 |
throw new SocketException( |
|
647 |
"Cannot handle non-Inet socket addresses."); |
|
648 |
} |
|
649 |
||
650 |
super.connect(endpoint, timeout); |
|
37601
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
651 |
|
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
652 |
if (host == null || host.length() == 0) { |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
653 |
useImplicitHost(false); |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
654 |
} |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
655 |
|
2 | 656 |
doneConnect(); |
657 |
} |
|
658 |
||
659 |
/** |
|
660 |
* Initialize the handshaker and socket streams. |
|
661 |
* |
|
662 |
* Called by connect, the layered constructor, and SSLServerSocket. |
|
663 |
*/ |
|
664 |
void doneConnect() throws IOException { |
|
665 |
/* |
|
666 |
* Save the input and output streams. May be done only after |
|
667 |
* java.net actually connects using the socket "self", else |
|
668 |
* we get some pretty bizarre failure modes. |
|
669 |
*/ |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
670 |
sockInput = super.getInputStream(); |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
671 |
sockOutput = super.getOutputStream(); |
2 | 672 |
|
30904 | 673 |
inputRecord.setDeliverStream(sockOutput); |
674 |
outputRecord.setDeliverStream(sockOutput); |
|
675 |
||
2 | 676 |
/* |
677 |
* Move to handshaking state, with pending session initialized |
|
678 |
* to defaults and the appropriate kind of handshaker set up. |
|
679 |
*/ |
|
680 |
initHandshaker(); |
|
681 |
} |
|
682 |
||
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
31918
diff
changeset
|
683 |
private synchronized int getConnectionState() { |
2 | 684 |
return connectionState; |
685 |
} |
|
686 |
||
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
31918
diff
changeset
|
687 |
private synchronized void setConnectionState(int state) { |
2 | 688 |
connectionState = state; |
689 |
} |
|
690 |
||
691 |
AccessControlContext getAcc() { |
|
692 |
return acc; |
|
693 |
} |
|
694 |
||
695 |
// |
|
696 |
// READING AND WRITING RECORDS |
|
697 |
// |
|
698 |
||
699 |
/* |
|
30904 | 700 |
* Application data record output. |
12428 | 701 |
* |
30904 | 702 |
* Application data can't be sent until the first handshake establishes |
703 |
* a session. |
|
12428 | 704 |
*/ |
30904 | 705 |
void writeRecord(byte[] source, int offset, int length) throws IOException { |
2 | 706 |
/* |
707 |
* The loop is in case of HANDSHAKE --> ERROR transitions, etc |
|
708 |
*/ |
|
30904 | 709 |
// Don't bother to check the emptiness of source applicatoin data |
710 |
// before the security connection established. |
|
711 |
for (boolean readyForApp = false; !readyForApp;) { |
|
2 | 712 |
/* |
713 |
* Not all states support passing application data. We |
|
714 |
* synchronize access to the connection state, so that |
|
715 |
* synchronous handshakes can complete cleanly. |
|
716 |
*/ |
|
717 |
switch (getConnectionState()) { |
|
718 |
||
30904 | 719 |
/* |
720 |
* We've deferred the initial handshaking till just now, |
|
721 |
* when presumably a thread's decided it's OK to block for |
|
722 |
* longish periods of time for I/O purposes (as well as |
|
723 |
* configured the cipher suites it wants to use). |
|
724 |
*/ |
|
725 |
case cs_HANDSHAKE: |
|
726 |
performInitialHandshake(); |
|
727 |
break; |
|
2 | 728 |
|
30904 | 729 |
case cs_DATA: |
730 |
case cs_RENEGOTIATE: |
|
731 |
readyForApp = true; |
|
732 |
break; |
|
2 | 733 |
|
30904 | 734 |
case cs_ERROR: |
735 |
fatal(Alerts.alert_close_notify, |
|
736 |
"error while writing to socket"); |
|
737 |
break; // dummy |
|
2 | 738 |
|
30904 | 739 |
case cs_SENT_CLOSE: |
740 |
case cs_CLOSED: |
|
741 |
case cs_APP_CLOSED: |
|
742 |
// we should never get here (check in AppOutputStream) |
|
743 |
// this is just a fallback |
|
744 |
if (closeReason != null) { |
|
745 |
throw closeReason; |
|
746 |
} else { |
|
747 |
throw new SocketException("Socket closed"); |
|
748 |
} |
|
2 | 749 |
|
30904 | 750 |
/* |
751 |
* Else something's goofy in this state machine's use. |
|
752 |
*/ |
|
753 |
default: |
|
754 |
throw new SSLProtocolException( |
|
755 |
"State error, send app data"); |
|
2 | 756 |
} |
757 |
} |
|
758 |
||
759 |
// |
|
760 |
// Don't bother to really write empty records. We went this |
|
761 |
// far to drive the handshake machinery, for correctness; not |
|
762 |
// writing empty records improves performance by cutting CPU |
|
763 |
// time and network resource usage. However, some protocol |
|
764 |
// implementations are fragile and don't like to see empty |
|
765 |
// records, so this also increases robustness. |
|
766 |
// |
|
30904 | 767 |
if (length > 0) { |
34528
279258fb670b
8141651: Deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
34380
diff
changeset
|
768 |
IOException ioe = null; |
279258fb670b
8141651: Deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
34380
diff
changeset
|
769 |
byte description = 0; // 0: never used, make the compiler happy |
30904 | 770 |
writeLock.lock(); |
771 |
try { |
|
772 |
outputRecord.deliver(source, offset, length); |
|
773 |
} catch (SSLHandshakeException she) { |
|
774 |
// may be record sequence number overflow |
|
34528
279258fb670b
8141651: Deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
34380
diff
changeset
|
775 |
description = Alerts.alert_handshake_failure; |
279258fb670b
8141651: Deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
34380
diff
changeset
|
776 |
ioe = she; |
30904 | 777 |
} catch (IOException e) { |
34528
279258fb670b
8141651: Deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
34380
diff
changeset
|
778 |
description = Alerts.alert_unexpected_message; |
279258fb670b
8141651: Deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
34380
diff
changeset
|
779 |
ioe = e; |
30904 | 780 |
} finally { |
781 |
writeLock.unlock(); |
|
2 | 782 |
} |
34528
279258fb670b
8141651: Deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
34380
diff
changeset
|
783 |
|
279258fb670b
8141651: Deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
34380
diff
changeset
|
784 |
// Be care of deadlock. Please don't place the call to fatal() |
279258fb670b
8141651: Deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
34380
diff
changeset
|
785 |
// into the writeLock locked block. |
279258fb670b
8141651: Deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
34380
diff
changeset
|
786 |
if (ioe != null) { |
279258fb670b
8141651: Deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
34380
diff
changeset
|
787 |
fatal(description, ioe); |
279258fb670b
8141651: Deadlock in sun.security.ssl.SSLSocketImpl
xuelei
parents:
34380
diff
changeset
|
788 |
} |
2 | 789 |
} |
7039 | 790 |
|
791 |
/* |
|
792 |
* Check the sequence number state |
|
793 |
* |
|
794 |
* Note that in order to maintain the connection I/O |
|
795 |
* properly, we check the sequence number after the last |
|
796 |
* record writing process. As we request renegotiation |
|
797 |
* or close the connection for wrapped sequence number |
|
798 |
* when there is enough sequence number space left to |
|
799 |
* handle a few more records, so the sequence number |
|
800 |
* of the last record cannot be wrapped. |
|
30904 | 801 |
* |
802 |
* Don't bother to kickstart the renegotiation when the |
|
803 |
* local is asking for it. |
|
7039 | 804 |
*/ |
30904 | 805 |
if ((connectionState == cs_DATA) && outputRecord.seqNumIsHuge()) { |
806 |
/* |
|
807 |
* Ask for renegotiation when need to renew sequence number. |
|
808 |
* |
|
809 |
* Don't bother to kickstart the renegotiation when the local is |
|
810 |
* asking for it. |
|
811 |
*/ |
|
812 |
if (debug != null && Debug.isOn("ssl")) { |
|
813 |
System.out.println(Thread.currentThread().getName() + |
|
814 |
", request renegotiation " + |
|
815 |
"to avoid sequence number overflow"); |
|
816 |
} |
|
10915 | 817 |
|
30904 | 818 |
startHandshake(); |
10915 | 819 |
} |
100
01ef29ca378f
6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
2
diff
changeset
|
820 |
} |
01ef29ca378f
6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
2
diff
changeset
|
821 |
|
10915 | 822 |
/* |
30904 | 823 |
* Alert record output. |
10915 | 824 |
*/ |
30904 | 825 |
void writeAlert(byte level, byte description) throws IOException { |
826 |
||
827 |
// If the record is a close notify alert, we need to honor |
|
828 |
// socket option SO_LINGER. Note that we will try to send |
|
829 |
// the close notify even if the SO_LINGER set to zero. |
|
830 |
if ((description == Alerts.alert_close_notify) && getSoLinger() >= 0) { |
|
831 |
||
832 |
// keep and clear the current thread interruption status. |
|
833 |
boolean interrupted = Thread.interrupted(); |
|
834 |
try { |
|
835 |
if (writeLock.tryLock(getSoLinger(), TimeUnit.SECONDS)) { |
|
836 |
try { |
|
837 |
outputRecord.encodeAlert(level, description); |
|
838 |
} finally { |
|
839 |
writeLock.unlock(); |
|
840 |
} |
|
841 |
} else { |
|
842 |
SSLException ssle = new SSLException( |
|
843 |
"SO_LINGER timeout," + |
|
844 |
" close_notify message cannot be sent."); |
|
845 |
||
846 |
||
847 |
// For layered, non-autoclose sockets, we are not |
|
848 |
// able to bring them into a usable state, so we |
|
849 |
// treat it as fatal error. |
|
850 |
if (isLayered() && !autoClose) { |
|
851 |
// Note that the alert description is |
|
852 |
// specified as -1, so no message will be send |
|
853 |
// to peer anymore. |
|
854 |
fatal((byte)(-1), ssle); |
|
855 |
} else if ((debug != null) && Debug.isOn("ssl")) { |
|
856 |
System.out.println( |
|
857 |
Thread.currentThread().getName() + |
|
858 |
", received Exception: " + ssle); |
|
859 |
} |
|
860 |
||
861 |
// RFC2246 requires that the session becomes |
|
862 |
// unresumable if any connection is terminated |
|
863 |
// without proper close_notify messages with |
|
864 |
// level equal to warning. |
|
865 |
// |
|
866 |
// RFC4346 no longer requires that a session not be |
|
867 |
// resumed if failure to properly close a connection. |
|
868 |
// |
|
869 |
// We choose to make the session unresumable if |
|
870 |
// failed to send the close_notify message. |
|
871 |
// |
|
872 |
sess.invalidate(); |
|
873 |
} |
|
874 |
} catch (InterruptedException ie) { |
|
875 |
// keep interrupted status |
|
876 |
interrupted = true; |
|
877 |
} |
|
878 |
||
879 |
// restore the interrupted status |
|
880 |
if (interrupted) { |
|
881 |
Thread.currentThread().interrupt(); |
|
882 |
} |
|
883 |
} else { |
|
884 |
writeLock.lock(); |
|
885 |
try { |
|
886 |
outputRecord.encodeAlert(level, description); |
|
887 |
} finally { |
|
888 |
writeLock.unlock(); |
|
889 |
} |
|
890 |
} |
|
891 |
||
892 |
// Don't bother to check sequence number overlap here. If sequence |
|
893 |
// number is huge, there should be enough sequence number space to |
|
894 |
// request renegotiation in next application data read and write. |
|
895 |
} |
|
896 |
||
897 |
||
898 |
int bytesInCompletePacket() throws IOException { |
|
899 |
if (getConnectionState() == cs_HANDSHAKE) { |
|
900 |
performInitialHandshake(); |
|
901 |
} |
|
902 |
||
903 |
synchronized (readLock) { |
|
904 |
int state = getConnectionState(); |
|
905 |
if ((state == cs_CLOSED) || |
|
906 |
(state == cs_ERROR) || (state == cs_APP_CLOSED)) { |
|
907 |
return -1; |
|
908 |
} |
|
909 |
||
910 |
try { |
|
911 |
return inputRecord.bytesInCompletePacket(sockInput); |
|
912 |
} catch (EOFException eofe) { |
|
913 |
boolean handshaking = (connectionState <= cs_HANDSHAKE); |
|
914 |
boolean rethrow = requireCloseNotify || handshaking; |
|
915 |
if ((debug != null) && Debug.isOn("ssl")) { |
|
916 |
System.out.println(Thread.currentThread().getName() + |
|
917 |
", received EOFException: " |
|
918 |
+ (rethrow ? "error" : "ignored")); |
|
919 |
} |
|
920 |
||
921 |
if (!rethrow) { |
|
922 |
// treat as if we had received a close_notify |
|
923 |
closeInternal(false); |
|
924 |
} else { |
|
925 |
SSLException e; |
|
926 |
if (handshaking) { |
|
927 |
e = new SSLHandshakeException( |
|
928 |
"Remote host terminated the handshake"); |
|
929 |
} else { |
|
930 |
e = new SSLProtocolException( |
|
931 |
"Remote host terminated the handshake"); |
|
932 |
} |
|
933 |
e.initCause(eofe); |
|
934 |
throw e; |
|
935 |
} |
|
936 |
} |
|
937 |
||
938 |
return -1; |
|
10915 | 939 |
} |
940 |
} |
|
2 | 941 |
|
30904 | 942 |
// the caller have synchronized readLock |
943 |
void expectingFinishFlight() { |
|
944 |
inputRecord.expectingFinishFlight(); |
|
945 |
} |
|
946 |
||
2 | 947 |
/* |
30904 | 948 |
* Read an application data record. |
949 |
* |
|
950 |
* Alerts and handshake messages are internally handled directly. |
|
2 | 951 |
*/ |
30904 | 952 |
int readRecord(ByteBuffer buffer) throws IOException { |
2 | 953 |
if (getConnectionState() == cs_HANDSHAKE) { |
954 |
performInitialHandshake(); |
|
955 |
} |
|
30904 | 956 |
|
957 |
return readRecord(buffer, true); |
|
2 | 958 |
} |
959 |
||
30904 | 960 |
/* |
961 |
* Read a record, no application data input required. |
|
962 |
* |
|
963 |
* Alerts and handshake messages are internally handled directly. |
|
964 |
*/ |
|
965 |
int readRecord(boolean needAppData) throws IOException { |
|
966 |
return readRecord(null, needAppData); |
|
967 |
} |
|
2 | 968 |
|
969 |
/* |
|
970 |
* Clear the pipeline of records from the peer, optionally returning |
|
971 |
* application data. Caller is responsible for knowing that it's |
|
972 |
* possible to do this kind of clearing, if they don't want app |
|
973 |
* data -- e.g. since it's the initial SSL handshake. |
|
974 |
* |
|
975 |
* Don't synchronize (this) during a blocking read() since it |
|
976 |
* protects data which is accessed on the write side as well. |
|
977 |
*/ |
|
30904 | 978 |
private int readRecord(ByteBuffer buffer, boolean needAppData) |
2 | 979 |
throws IOException { |
980 |
int state; |
|
981 |
||
30904 | 982 |
// readLock protects reading and processing of an SSLInputRecord. |
2 | 983 |
// It keeps the reading from sockInput and processing of the record |
984 |
// atomic so that no two threads can be blocked on the |
|
985 |
// read from the same input stream at the same time. |
|
986 |
// This is required for example when a reader thread is |
|
987 |
// blocked on the read and another thread is trying to |
|
988 |
// close the socket. For a non-autoclose, layered socket, |
|
989 |
// the thread performing the close needs to read the close_notify. |
|
990 |
// |
|
991 |
// Use readLock instead of 'this' for locking because |
|
992 |
// 'this' also protects data accessed during writing. |
|
30904 | 993 |
synchronized (readLock) { |
2 | 994 |
/* |
30904 | 995 |
* Read and handle records ... return application data |
996 |
* ONLY if it's needed. |
|
2 | 997 |
*/ |
30904 | 998 |
Plaintext plainText = null; |
999 |
while (((state = getConnectionState()) != cs_CLOSED) && |
|
1000 |
(state != cs_ERROR) && (state != cs_APP_CLOSED)) { |
|
36641
cbff2fd02f9e
8149169: SSLSocketInputRecord.decodeInputRecord buffer overflow
clanger
parents:
34958
diff
changeset
|
1001 |
|
cbff2fd02f9e
8149169: SSLSocketInputRecord.decodeInputRecord buffer overflow
clanger
parents:
34958
diff
changeset
|
1002 |
/* |
cbff2fd02f9e
8149169: SSLSocketInputRecord.decodeInputRecord buffer overflow
clanger
parents:
34958
diff
changeset
|
1003 |
* clean the buffer and check if it is too small, e.g. because |
cbff2fd02f9e
8149169: SSLSocketInputRecord.decodeInputRecord buffer overflow
clanger
parents:
34958
diff
changeset
|
1004 |
* the AppInputStream did not have the chance to see the |
cbff2fd02f9e
8149169: SSLSocketInputRecord.decodeInputRecord buffer overflow
clanger
parents:
34958
diff
changeset
|
1005 |
* current packet length but rather something like that of the |
cbff2fd02f9e
8149169: SSLSocketInputRecord.decodeInputRecord buffer overflow
clanger
parents:
34958
diff
changeset
|
1006 |
* handshake before. In that case we return 0 at this point to |
cbff2fd02f9e
8149169: SSLSocketInputRecord.decodeInputRecord buffer overflow
clanger
parents:
34958
diff
changeset
|
1007 |
* give the caller the chance to adjust the buffer. |
cbff2fd02f9e
8149169: SSLSocketInputRecord.decodeInputRecord buffer overflow
clanger
parents:
34958
diff
changeset
|
1008 |
*/ |
30904 | 1009 |
if (buffer != null) { |
1010 |
buffer.clear(); |
|
36641
cbff2fd02f9e
8149169: SSLSocketInputRecord.decodeInputRecord buffer overflow
clanger
parents:
34958
diff
changeset
|
1011 |
|
cbff2fd02f9e
8149169: SSLSocketInputRecord.decodeInputRecord buffer overflow
clanger
parents:
34958
diff
changeset
|
1012 |
if (buffer.remaining() < |
cbff2fd02f9e
8149169: SSLSocketInputRecord.decodeInputRecord buffer overflow
clanger
parents:
34958
diff
changeset
|
1013 |
inputRecord.bytesInCompletePacket(sockInput)) { |
cbff2fd02f9e
8149169: SSLSocketInputRecord.decodeInputRecord buffer overflow
clanger
parents:
34958
diff
changeset
|
1014 |
return 0; |
cbff2fd02f9e
8149169: SSLSocketInputRecord.decodeInputRecord buffer overflow
clanger
parents:
34958
diff
changeset
|
1015 |
} |
30904 | 1016 |
} |
2 | 1017 |
|
30904 | 1018 |
/* |
1019 |
* Read a record ... maybe emitting an alert if we get a |
|
1020 |
* comprehensible but unsupported "hello" message during |
|
1021 |
* format checking (e.g. V2). |
|
1022 |
*/ |
|
1023 |
try { |
|
1024 |
plainText = inputRecord.decode(sockInput, buffer); |
|
1025 |
} catch (BadPaddingException bpe) { |
|
1026 |
byte alertType = (state != cs_DATA) ? |
|
1027 |
Alerts.alert_handshake_failure : |
|
1028 |
Alerts.alert_bad_record_mac; |
|
1029 |
fatal(alertType, bpe.getMessage(), bpe); |
|
1030 |
} catch (SSLProtocolException spe) { |
|
1031 |
try { |
|
1032 |
fatal(Alerts.alert_unexpected_message, spe); |
|
1033 |
} catch (IOException x) { |
|
1034 |
// discard this exception, throw the original exception |
|
1035 |
} |
|
1036 |
throw spe; |
|
1037 |
} catch (SSLHandshakeException she) { |
|
1038 |
// may be record sequence number overflow |
|
1039 |
fatal(Alerts.alert_handshake_failure, she); |
|
1040 |
} catch (EOFException eof) { |
|
1041 |
boolean handshaking = (connectionState <= cs_HANDSHAKE); |
|
1042 |
boolean rethrow = requireCloseNotify || handshaking; |
|
1043 |
if ((debug != null) && Debug.isOn("ssl")) { |
|
1044 |
System.out.println(Thread.currentThread().getName() + |
|
1045 |
", received EOFException: " |
|
1046 |
+ (rethrow ? "error" : "ignored")); |
|
1047 |
} |
|
1048 |
if (rethrow) { |
|
1049 |
SSLException e; |
|
1050 |
if (handshaking) { |
|
1051 |
e = new SSLHandshakeException( |
|
1052 |
"Remote host terminated the handshake"); |
|
1053 |
} else { |
|
1054 |
e = new SSLProtocolException( |
|
1055 |
"Remote host terminated the connection"); |
|
5182 | 1056 |
} |
30904 | 1057 |
e.initCause(eof); |
1058 |
throw e; |
|
1059 |
} else { |
|
1060 |
// treat as if we had received a close_notify |
|
1061 |
closeInternal(false); |
|
2 | 1062 |
continue; |
1063 |
} |
|
30904 | 1064 |
} |
1065 |
||
1066 |
// PlainText should never be null. Process input record. |
|
1067 |
int volume = processInputRecord(plainText, needAppData); |
|
1068 |
||
1069 |
if (plainText.contentType == Record.ct_application_data) { |
|
1070 |
return volume; |
|
1071 |
} |
|
1072 |
||
1073 |
if (plainText.contentType == Record.ct_handshake) { |
|
1074 |
if (!needAppData && connectionState == cs_DATA) { |
|
1075 |
return volume; |
|
1076 |
} // otherwise, need to read more for app data. |
|
1077 |
} |
|
2 | 1078 |
|
30904 | 1079 |
// continue to read more net data |
1080 |
} // while |
|
2 | 1081 |
|
30904 | 1082 |
// |
1083 |
// couldn't read, due to some kind of error |
|
1084 |
// |
|
1085 |
return -1; |
|
1086 |
} // readLock synchronization |
|
1087 |
} |
|
1088 |
||
1089 |
/* |
|
1090 |
* Process the plainText input record. |
|
1091 |
*/ |
|
1092 |
private synchronized int processInputRecord( |
|
1093 |
Plaintext plainText, boolean needAppData) throws IOException { |
|
2 | 1094 |
|
30904 | 1095 |
/* |
1096 |
* Process the record. |
|
1097 |
*/ |
|
1098 |
int volume = 0; // no application data |
|
1099 |
switch (plainText.contentType) { |
|
1100 |
case Record.ct_handshake: |
|
1101 |
/* |
|
1102 |
* Handshake messages always go to a pending session |
|
1103 |
* handshaker ... if there isn't one, create one. This |
|
1104 |
* must work asynchronously, for renegotiation. |
|
1105 |
* |
|
1106 |
* NOTE that handshaking will either resume a session |
|
1107 |
* which was in the cache (and which might have other |
|
1108 |
* connections in it already), or else will start a new |
|
1109 |
* session (new keys exchanged) with just this connection |
|
1110 |
* in it. |
|
1111 |
*/ |
|
1112 |
initHandshaker(); |
|
1113 |
if (!handshaker.activated()) { |
|
1114 |
// prior to handshaking, activate the handshake |
|
1115 |
if (connectionState == cs_RENEGOTIATE) { |
|
1116 |
// don't use SSLv2Hello when renegotiating |
|
1117 |
handshaker.activate(protocolVersion); |
|
1118 |
} else { |
|
1119 |
handshaker.activate(null); |
|
1120 |
} |
|
1121 |
} |
|
2 | 1122 |
|
30904 | 1123 |
/* |
1124 |
* process the handshake record ... may contain just |
|
1125 |
* a partial handshake message or multiple messages. |
|
1126 |
* |
|
1127 |
* The handshaker state machine will ensure that it's |
|
1128 |
* a finished message. |
|
1129 |
*/ |
|
1130 |
handshaker.processRecord(plainText.fragment, expectingFinished); |
|
1131 |
expectingFinished = false; |
|
1132 |
||
1133 |
if (handshaker.invalidated) { |
|
1134 |
handshaker = null; |
|
1135 |
inputRecord.setHandshakeHash(null); |
|
1136 |
outputRecord.setHandshakeHash(null); |
|
1137 |
||
1138 |
// if state is cs_RENEGOTIATE, revert it to cs_DATA |
|
1139 |
if (connectionState == cs_RENEGOTIATE) { |
|
1140 |
connectionState = cs_DATA; |
|
2 | 1141 |
} |
30904 | 1142 |
} else if (handshaker.isDone()) { |
1143 |
// reset the parameters for secure renegotiation. |
|
1144 |
secureRenegotiation = |
|
1145 |
handshaker.isSecureRenegotiation(); |
|
1146 |
clientVerifyData = handshaker.getClientVerifyData(); |
|
1147 |
serverVerifyData = handshaker.getServerVerifyData(); |
|
34380
2b2609379881
8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
32834
diff
changeset
|
1148 |
// set connection ALPN value |
2b2609379881
8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
32834
diff
changeset
|
1149 |
applicationProtocol = |
2b2609379881
8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
32834
diff
changeset
|
1150 |
handshaker.getHandshakeApplicationProtocol(); |
2 | 1151 |
|
30904 | 1152 |
sess = handshaker.getSession(); |
1153 |
handshakeSession = null; |
|
1154 |
handshaker = null; |
|
1155 |
inputRecord.setHandshakeHash(null); |
|
1156 |
outputRecord.setHandshakeHash(null); |
|
1157 |
connectionState = cs_DATA; |
|
28543 | 1158 |
|
2 | 1159 |
// |
30904 | 1160 |
// Tell folk about handshake completion, but do |
1161 |
// it in a separate thread. |
|
2 | 1162 |
// |
30904 | 1163 |
if (handshakeListeners != null) { |
1164 |
HandshakeCompletedEvent event = |
|
1165 |
new HandshakeCompletedEvent(this, sess); |
|
1166 |
||
34716
7477a052aecc
8056152: API to create Threads that do not inherit inheritable thread-local initial values
chegar
parents:
34528
diff
changeset
|
1167 |
Thread thread = new Thread( |
7477a052aecc
8056152: API to create Threads that do not inherit inheritable thread-local initial values
chegar
parents:
34528
diff
changeset
|
1168 |
null, |
30904 | 1169 |
new NotifyHandshake( |
1170 |
handshakeListeners.entrySet(), event), |
|
34716
7477a052aecc
8056152: API to create Threads that do not inherit inheritable thread-local initial values
chegar
parents:
34528
diff
changeset
|
1171 |
"HandshakeCompletedNotify-Thread", |
7477a052aecc
8056152: API to create Threads that do not inherit inheritable thread-local initial values
chegar
parents:
34528
diff
changeset
|
1172 |
0, |
7477a052aecc
8056152: API to create Threads that do not inherit inheritable thread-local initial values
chegar
parents:
34528
diff
changeset
|
1173 |
false); |
30904 | 1174 |
thread.start(); |
1175 |
} |
|
1176 |
} |
|
1177 |
||
1178 |
break; |
|
2 | 1179 |
|
30904 | 1180 |
case Record.ct_application_data: |
1181 |
if (connectionState != cs_DATA |
|
1182 |
&& connectionState != cs_RENEGOTIATE |
|
1183 |
&& connectionState != cs_SENT_CLOSE) { |
|
1184 |
throw new SSLProtocolException( |
|
1185 |
"Data received in non-data state: " + |
|
1186 |
connectionState); |
|
1187 |
} |
|
1188 |
if (expectingFinished) { |
|
1189 |
throw new SSLProtocolException |
|
1190 |
("Expecting finished message, received data"); |
|
1191 |
} |
|
1192 |
if (!needAppData) { |
|
1193 |
throw new SSLException("Discarding app data"); |
|
1194 |
} |
|
1195 |
||
1196 |
volume = plainText.fragment.remaining(); |
|
1197 |
break; |
|
1198 |
||
1199 |
case Record.ct_alert: |
|
1200 |
recvAlert(plainText.fragment); |
|
1201 |
break; |
|
7039 | 1202 |
|
30904 | 1203 |
case Record.ct_change_cipher_spec: |
1204 |
if ((connectionState != cs_HANDSHAKE |
|
1205 |
&& connectionState != cs_RENEGOTIATE)) { |
|
1206 |
// For the CCS message arriving in the wrong state |
|
1207 |
fatal(Alerts.alert_unexpected_message, |
|
1208 |
"illegal change cipher spec msg, conn state = " |
|
1209 |
+ connectionState); |
|
1210 |
} else if (plainText.fragment.remaining() != 1 |
|
1211 |
|| plainText.fragment.get() != 1) { |
|
1212 |
// For structural/content issues with the CCS |
|
1213 |
fatal(Alerts.alert_unexpected_message, |
|
1214 |
"Malformed change cipher spec msg"); |
|
1215 |
} |
|
7039 | 1216 |
|
30904 | 1217 |
// |
1218 |
// The first message after a change_cipher_spec |
|
1219 |
// record MUST be a "Finished" handshake record, |
|
1220 |
// else it's a protocol violation. We force this |
|
1221 |
// to be checked by a minor tweak to the state |
|
1222 |
// machine. |
|
1223 |
// |
|
1224 |
handshaker.receiveChangeCipherSpec(); |
|
2 | 1225 |
|
30904 | 1226 |
CipherBox readCipher; |
1227 |
Authenticator readAuthenticator; |
|
1228 |
try { |
|
1229 |
readCipher = handshaker.newReadCipher(); |
|
1230 |
readAuthenticator = handshaker.newReadAuthenticator(); |
|
1231 |
} catch (GeneralSecurityException e) { |
|
1232 |
// can't happen |
|
1233 |
throw new SSLException("Algorithm missing: ", e); |
|
1234 |
} |
|
1235 |
inputRecord.changeReadCiphers(readAuthenticator, readCipher); |
|
2 | 1236 |
|
30904 | 1237 |
// next message MUST be a finished message |
1238 |
expectingFinished = true; |
|
1239 |
||
1240 |
break; |
|
7039 | 1241 |
|
30904 | 1242 |
default: |
1243 |
// |
|
1244 |
// TLS requires that unrecognized records be ignored. |
|
1245 |
// |
|
1246 |
if (debug != null && Debug.isOn("ssl")) { |
|
1247 |
System.out.println(Thread.currentThread().getName() + |
|
1248 |
", Received record type: " + plainText.contentType); |
|
1249 |
} |
|
1250 |
break; |
|
7039 | 1251 |
} |
1252 |
||
1253 |
/* |
|
30904 | 1254 |
* Check the sequence number state |
1255 |
* |
|
1256 |
* Note that in order to maintain the connection I/O |
|
1257 |
* properly, we check the sequence number after the last |
|
1258 |
* record reading process. As we request renegotiation |
|
1259 |
* or close the connection for wrapped sequence number |
|
1260 |
* when there is enough sequence number space left to |
|
1261 |
* handle a few more records, so the sequence number |
|
1262 |
* of the last record cannot be wrapped. |
|
1263 |
* |
|
1264 |
* Don't bother to kickstart the renegotiation when the |
|
1265 |
* local is asking for it. |
|
7039 | 1266 |
*/ |
30904 | 1267 |
if ((connectionState == cs_DATA) && inputRecord.seqNumIsHuge()) { |
7039 | 1268 |
/* |
30904 | 1269 |
* Ask for renegotiation when need to renew sequence number. |
1270 |
* |
|
1271 |
* Don't bother to kickstart the renegotiation when the local is |
|
1272 |
* asking for it. |
|
7039 | 1273 |
*/ |
1274 |
if (debug != null && Debug.isOn("ssl")) { |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
1275 |
System.out.println(Thread.currentThread().getName() + |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
1276 |
", request renegotiation " + |
7039 | 1277 |
"to avoid sequence number overflow"); |
1278 |
} |
|
1279 |
||
1280 |
startHandshake(); |
|
1281 |
} |
|
30904 | 1282 |
|
1283 |
return volume; |
|
7039 | 1284 |
} |
1285 |
||
30904 | 1286 |
|
2 | 1287 |
// |
1288 |
// HANDSHAKE RELATED CODE |
|
1289 |
// |
|
1290 |
||
1291 |
/** |
|
1292 |
* Return the AppInputStream. For use by Handshaker only. |
|
1293 |
*/ |
|
1294 |
AppInputStream getAppInputStream() { |
|
1295 |
return input; |
|
1296 |
} |
|
1297 |
||
1298 |
/** |
|
7039 | 1299 |
* Return the AppOutputStream. For use by Handshaker only. |
2 | 1300 |
*/ |
7039 | 1301 |
AppOutputStream getAppOutputStream() { |
1302 |
return output; |
|
2 | 1303 |
} |
1304 |
||
1305 |
/** |
|
1306 |
* Initialize the handshaker object. This means: |
|
1307 |
* |
|
1308 |
* . if a handshake is already in progress (state is cs_HANDSHAKE |
|
1309 |
* or cs_RENEGOTIATE), do nothing and return |
|
1310 |
* |
|
1311 |
* . if the socket is already closed, throw an Exception (internal error) |
|
1312 |
* |
|
1313 |
* . otherwise (cs_START or cs_DATA), create the appropriate handshaker |
|
7039 | 1314 |
* object, and advance the connection state (to cs_HANDSHAKE or |
1315 |
* cs_RENEGOTIATE, respectively). |
|
2 | 1316 |
* |
1317 |
* This method is called right after a new socket is created, when |
|
1318 |
* starting renegotiation, or when changing client/ server mode of the |
|
1319 |
* socket. |
|
1320 |
*/ |
|
1321 |
private void initHandshaker() { |
|
1322 |
switch (connectionState) { |
|
1323 |
||
1324 |
// |
|
1325 |
// Starting a new handshake. |
|
1326 |
// |
|
1327 |
case cs_START: |
|
1328 |
case cs_DATA: |
|
1329 |
break; |
|
1330 |
||
1331 |
// |
|
1332 |
// We're already in the middle of a handshake. |
|
1333 |
// |
|
1334 |
case cs_HANDSHAKE: |
|
1335 |
case cs_RENEGOTIATE: |
|
1336 |
return; |
|
1337 |
||
1338 |
// |
|
1339 |
// Anyone allowed to call this routine is required to |
|
1340 |
// do so ONLY if the connection state is reasonable... |
|
1341 |
// |
|
1342 |
default: |
|
1343 |
throw new IllegalStateException("Internal error"); |
|
1344 |
} |
|
1345 |
||
1346 |
// state is either cs_START or cs_DATA |
|
1347 |
if (connectionState == cs_START) { |
|
1348 |
connectionState = cs_HANDSHAKE; |
|
1349 |
} else { // cs_DATA |
|
1350 |
connectionState = cs_RENEGOTIATE; |
|
1351 |
} |
|
34380
2b2609379881
8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
32834
diff
changeset
|
1352 |
|
2 | 1353 |
if (roleIsServer) { |
5182 | 1354 |
handshaker = new ServerHandshaker(this, sslContext, |
6856 | 1355 |
enabledProtocols, doClientAuth, |
1356 |
protocolVersion, connectionState == cs_HANDSHAKE, |
|
1357 |
secureRenegotiation, clientVerifyData, serverVerifyData); |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
1358 |
handshaker.setSNIMatchers(sniMatchers); |
19823
f0fd09e20528
7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents:
19223
diff
changeset
|
1359 |
handshaker.setUseCipherSuitesOrder(preferLocalCipherSuites); |
2 | 1360 |
} else { |
5182 | 1361 |
handshaker = new ClientHandshaker(this, sslContext, |
6856 | 1362 |
enabledProtocols, |
1363 |
protocolVersion, connectionState == cs_HANDSHAKE, |
|
1364 |
secureRenegotiation, clientVerifyData, serverVerifyData); |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
1365 |
handshaker.setSNIServerNames(serverNames); |
2 | 1366 |
} |
30904 | 1367 |
handshaker.setMaximumPacketSize(maximumPacketSize); |
7039 | 1368 |
handshaker.setEnabledCipherSuites(enabledCipherSuites); |
2 | 1369 |
handshaker.setEnableSessionCreation(enableSessionCreation); |
34380
2b2609379881
8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
32834
diff
changeset
|
1370 |
handshaker.setApplicationProtocols(applicationProtocols); |
2 | 1371 |
} |
1372 |
||
1373 |
/** |
|
1374 |
* Synchronously perform the initial handshake. |
|
1375 |
* |
|
1376 |
* If the handshake is already in progress, this method blocks until it |
|
1377 |
* is completed. If the initial handshake has already been completed, |
|
1378 |
* it returns immediately. |
|
1379 |
*/ |
|
1380 |
private void performInitialHandshake() throws IOException { |
|
1381 |
// use handshakeLock and the state check to make sure only |
|
1382 |
// one thread performs the handshake |
|
1383 |
synchronized (handshakeLock) { |
|
1384 |
if (getConnectionState() == cs_HANDSHAKE) { |
|
7039 | 1385 |
kickstartHandshake(); |
1386 |
||
2 | 1387 |
/* |
30904 | 1388 |
* All initial handshaking goes through this operation |
1389 |
* until we have a valid SSL connection. |
|
2 | 1390 |
* |
30904 | 1391 |
* Handle handshake messages only, need no application data. |
2 | 1392 |
*/ |
30904 | 1393 |
readRecord(false); |
2 | 1394 |
} |
1395 |
} |
|
1396 |
} |
|
1397 |
||
1398 |
/** |
|
1399 |
* Starts an SSL handshake on this connection. |
|
1400 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
1401 |
@Override |
2 | 1402 |
public void startHandshake() throws IOException { |
1403 |
// start an ssl handshake that could be resumed from timeout exception |
|
1404 |
startHandshake(true); |
|
1405 |
} |
|
1406 |
||
1407 |
/** |
|
1408 |
* Starts an ssl handshake on this connection. |
|
1409 |
* |
|
1410 |
* @param resumable indicates the handshake process is resumable from a |
|
1411 |
* certain exception. If <code>resumable</code>, the socket will |
|
1412 |
* be reserved for exceptions like timeout; otherwise, the socket |
|
1413 |
* will be closed, no further communications could be done. |
|
1414 |
*/ |
|
1415 |
private void startHandshake(boolean resumable) throws IOException { |
|
1416 |
checkWrite(); |
|
1417 |
try { |
|
1418 |
if (getConnectionState() == cs_HANDSHAKE) { |
|
1419 |
// do initial handshake |
|
1420 |
performInitialHandshake(); |
|
1421 |
} else { |
|
1422 |
// start renegotiation |
|
1423 |
kickstartHandshake(); |
|
1424 |
} |
|
1425 |
} catch (Exception e) { |
|
1426 |
// shutdown and rethrow (wrapped) exception as appropriate |
|
1427 |
handleException(e, resumable); |
|
1428 |
} |
|
1429 |
} |
|
1430 |
||
1431 |
/** |
|
1432 |
* Kickstart the handshake if it is not already in progress. |
|
1433 |
* This means: |
|
1434 |
* |
|
1435 |
* . if handshaking is already underway, do nothing and return |
|
1436 |
* |
|
1437 |
* . if the socket is not connected or already closed, throw an |
|
1438 |
* Exception. |
|
1439 |
* |
|
1440 |
* . otherwise, call initHandshake() to initialize the handshaker |
|
1441 |
* object and progress the state. Then, send the initial |
|
1442 |
* handshaking message if appropriate (always on clients and |
|
1443 |
* on servers when renegotiating). |
|
1444 |
*/ |
|
1445 |
private synchronized void kickstartHandshake() throws IOException { |
|
7039 | 1446 |
|
2 | 1447 |
switch (connectionState) { |
1448 |
||
1449 |
case cs_HANDSHAKE: |
|
1450 |
// handshaker already setup, proceed |
|
1451 |
break; |
|
1452 |
||
1453 |
case cs_DATA: |
|
6856 | 1454 |
if (!secureRenegotiation && !Handshaker.allowUnsafeRenegotiation) { |
1455 |
throw new SSLHandshakeException( |
|
1456 |
"Insecure renegotiation is not allowed"); |
|
1457 |
} |
|
1458 |
||
1459 |
if (!secureRenegotiation) { |
|
1460 |
if (debug != null && Debug.isOn("handshake")) { |
|
1461 |
System.out.println( |
|
1462 |
"Warning: Using insecure renegotiation"); |
|
1463 |
} |
|
5182 | 1464 |
} |
1465 |
||
2 | 1466 |
// initialize the handshaker, move to cs_RENEGOTIATE |
1467 |
initHandshaker(); |
|
1468 |
break; |
|
1469 |
||
1470 |
case cs_RENEGOTIATE: |
|
1471 |
// handshaking already in progress, return |
|
1472 |
return; |
|
1473 |
||
1474 |
/* |
|
1475 |
* The only way to get a socket in the state is when |
|
1476 |
* you have an unconnected socket. |
|
1477 |
*/ |
|
1478 |
case cs_START: |
|
1479 |
throw new SocketException( |
|
1480 |
"handshaking attempted on unconnected socket"); |
|
1481 |
||
1482 |
default: |
|
1483 |
throw new SocketException("connection is closed"); |
|
1484 |
} |
|
1485 |
||
1486 |
// |
|
1487 |
// Kickstart handshake state machine if we need to ... |
|
1488 |
// |
|
1489 |
// Note that handshaker.kickstart() writes the message |
|
1490 |
// to its HandshakeOutStream, which calls back into |
|
1491 |
// SSLSocketImpl.writeRecord() to send it. |
|
1492 |
// |
|
7039 | 1493 |
if (!handshaker.activated()) { |
1494 |
// prior to handshaking, activate the handshake |
|
1495 |
if (connectionState == cs_RENEGOTIATE) { |
|
1496 |
// don't use SSLv2Hello when renegotiating |
|
1497 |
handshaker.activate(protocolVersion); |
|
1498 |
} else { |
|
1499 |
handshaker.activate(null); |
|
1500 |
} |
|
1501 |
||
2 | 1502 |
if (handshaker instanceof ClientHandshaker) { |
1503 |
// send client hello |
|
1504 |
handshaker.kickstart(); |
|
1505 |
} else { |
|
1506 |
if (connectionState == cs_HANDSHAKE) { |
|
1507 |
// initial handshake, no kickstart message to send |
|
1508 |
} else { |
|
1509 |
// we want to renegotiate, send hello request |
|
1510 |
handshaker.kickstart(); |
|
1511 |
} |
|
1512 |
} |
|
1513 |
} |
|
1514 |
} |
|
1515 |
||
1516 |
// |
|
1517 |
// CLOSURE RELATED CALLS |
|
1518 |
// |
|
1519 |
||
1520 |
/** |
|
1521 |
* Return whether the socket has been explicitly closed by the application. |
|
1522 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
1523 |
@Override |
2 | 1524 |
public boolean isClosed() { |
19223
e27cda06fe6a
8013809: deadlock in SSLSocketImpl between between write and close
xuelei
parents:
16913
diff
changeset
|
1525 |
return connectionState == cs_APP_CLOSED; |
2 | 1526 |
} |
1527 |
||
1528 |
/** |
|
1529 |
* Return whether we have reached end-of-file. |
|
1530 |
* |
|
1531 |
* If the socket is not connected, has been shutdown because of an error |
|
1532 |
* or has been closed, throw an Exception. |
|
1533 |
*/ |
|
1534 |
boolean checkEOF() throws IOException { |
|
1535 |
switch (getConnectionState()) { |
|
1536 |
case cs_START: |
|
1537 |
throw new SocketException("Socket is not connected"); |
|
1538 |
||
1539 |
case cs_HANDSHAKE: |
|
1540 |
case cs_DATA: |
|
1541 |
case cs_RENEGOTIATE: |
|
1542 |
case cs_SENT_CLOSE: |
|
1543 |
return false; |
|
1544 |
||
1545 |
case cs_APP_CLOSED: |
|
1546 |
throw new SocketException("Socket is closed"); |
|
1547 |
||
1548 |
case cs_ERROR: |
|
1549 |
case cs_CLOSED: |
|
1550 |
default: |
|
1551 |
// either closed because of error, or normal EOF |
|
1552 |
if (closeReason == null) { |
|
1553 |
return true; |
|
1554 |
} |
|
1555 |
IOException e = new SSLException |
|
1556 |
("Connection has been shutdown: " + closeReason); |
|
1557 |
e.initCause(closeReason); |
|
1558 |
throw e; |
|
1559 |
||
1560 |
} |
|
1561 |
} |
|
1562 |
||
1563 |
/** |
|
1564 |
* Check if we can write data to this socket. If not, throw an IOException. |
|
1565 |
*/ |
|
1566 |
void checkWrite() throws IOException { |
|
1567 |
if (checkEOF() || (getConnectionState() == cs_SENT_CLOSE)) { |
|
1568 |
// we are at EOF, write must throw Exception |
|
1569 |
throw new SocketException("Connection closed by remote host"); |
|
1570 |
} |
|
1571 |
} |
|
1572 |
||
30904 | 1573 |
private void closeSocket() throws IOException { |
1580 | 1574 |
|
2 | 1575 |
if ((debug != null) && Debug.isOn("ssl")) { |
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
1576 |
System.out.println(Thread.currentThread().getName() + |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
1577 |
", called closeSocket()"); |
2 | 1578 |
} |
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
1579 |
|
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
1580 |
super.close(); |
2 | 1581 |
} |
1582 |
||
9514
bdb24db75fe8
7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents:
9246
diff
changeset
|
1583 |
private void closeSocket(boolean selfInitiated) throws IOException { |
bdb24db75fe8
7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents:
9246
diff
changeset
|
1584 |
if ((debug != null) && Debug.isOn("ssl")) { |
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
1585 |
System.out.println(Thread.currentThread().getName() + |
11018 | 1586 |
", called closeSocket(" + selfInitiated + ")"); |
9514
bdb24db75fe8
7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents:
9246
diff
changeset
|
1587 |
} |
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
1588 |
if (!isLayered() || autoClose) { |
9514
bdb24db75fe8
7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents:
9246
diff
changeset
|
1589 |
super.close(); |
bdb24db75fe8
7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents:
9246
diff
changeset
|
1590 |
} else if (selfInitiated) { |
bdb24db75fe8
7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents:
9246
diff
changeset
|
1591 |
// layered && non-autoclose |
bdb24db75fe8
7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents:
9246
diff
changeset
|
1592 |
// read close_notify alert to clear input stream |
bdb24db75fe8
7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents:
9246
diff
changeset
|
1593 |
waitForClose(false); |
bdb24db75fe8
7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents:
9246
diff
changeset
|
1594 |
} |
bdb24db75fe8
7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents:
9246
diff
changeset
|
1595 |
} |
bdb24db75fe8
7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents:
9246
diff
changeset
|
1596 |
|
2 | 1597 |
/* |
1598 |
* Closing the connection is tricky ... we can't officially close the |
|
1599 |
* connection until we know the other end is ready to go away too, |
|
1600 |
* and if ever the connection gets aborted we must forget session |
|
1601 |
* state (it becomes invalid). |
|
1602 |
*/ |
|
1603 |
||
1604 |
/** |
|
1605 |
* Closes the SSL connection. SSL includes an application level |
|
1606 |
* shutdown handshake; you should close SSL sockets explicitly |
|
1607 |
* rather than leaving it for finalization, so that your remote |
|
1608 |
* peer does not experience a protocol error. |
|
1609 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
1610 |
@Override |
2 | 1611 |
public void close() throws IOException { |
1612 |
if ((debug != null) && Debug.isOn("ssl")) { |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
1613 |
System.out.println(Thread.currentThread().getName() + |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
1614 |
", called close()"); |
2 | 1615 |
} |
1616 |
closeInternal(true); // caller is initiating close |
|
30904 | 1617 |
|
1618 |
// Clearup the resources. |
|
1619 |
try { |
|
1620 |
synchronized (readLock) { |
|
1621 |
inputRecord.close(); |
|
1622 |
} |
|
1623 |
||
1624 |
writeLock.lock(); |
|
1625 |
try { |
|
1626 |
outputRecord.close(); |
|
1627 |
} finally { |
|
1628 |
writeLock.unlock(); |
|
1629 |
} |
|
1630 |
} catch (IOException ioe) { |
|
1631 |
// ignore |
|
1632 |
} |
|
1633 |
||
2 | 1634 |
setConnectionState(cs_APP_CLOSED); |
1635 |
} |
|
1636 |
||
1637 |
/** |
|
1638 |
* Don't synchronize the whole method because waitForClose() |
|
1639 |
* (which calls readRecord()) might be called. |
|
1640 |
* |
|
1641 |
* @param selfInitiated Indicates which party initiated the close. |
|
1642 |
* If selfInitiated, this side is initiating a close; for layered and |
|
1643 |
* non-autoclose socket, wait for close_notify response. |
|
1644 |
* If !selfInitiated, peer sent close_notify; we reciprocate but |
|
1645 |
* no need to wait for response. |
|
1646 |
*/ |
|
1647 |
private void closeInternal(boolean selfInitiated) throws IOException { |
|
1648 |
if ((debug != null) && Debug.isOn("ssl")) { |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
1649 |
System.out.println(Thread.currentThread().getName() + |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
1650 |
", called closeInternal(" + selfInitiated + ")"); |
2 | 1651 |
} |
1652 |
||
1653 |
int state = getConnectionState(); |
|
9514
bdb24db75fe8
7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents:
9246
diff
changeset
|
1654 |
boolean closeSocketCalled = false; |
bdb24db75fe8
7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents:
9246
diff
changeset
|
1655 |
Throwable cachedThrowable = null; |
2 | 1656 |
try { |
1657 |
switch (state) { |
|
1658 |
case cs_START: |
|
13370
785cedd026db
7179879: SSLSocket connect times out instead of throwing socket closed exception
coffeys
parents:
12428
diff
changeset
|
1659 |
// unconnected socket or handshaking has not been initialized |
785cedd026db
7179879: SSLSocket connect times out instead of throwing socket closed exception
coffeys
parents:
12428
diff
changeset
|
1660 |
closeSocket(selfInitiated); |
2 | 1661 |
break; |
1662 |
||
1663 |
/* |
|
1664 |
* If we're closing down due to error, we already sent (or else |
|
1665 |
* received) the fatal alert ... no niceties, blow the connection |
|
1666 |
* away as quickly as possible (even if we didn't allocate the |
|
1667 |
* socket ourselves; it's unusable, regardless). |
|
1668 |
*/ |
|
1669 |
case cs_ERROR: |
|
1670 |
closeSocket(); |
|
1671 |
break; |
|
1672 |
||
1673 |
/* |
|
1674 |
* Sometimes close() gets called more than once. |
|
1675 |
*/ |
|
1676 |
case cs_CLOSED: |
|
1677 |
case cs_APP_CLOSED: |
|
1678 |
break; |
|
1679 |
||
1680 |
/* |
|
1681 |
* Otherwise we indicate clean termination. |
|
1682 |
*/ |
|
1683 |
// case cs_HANDSHAKE: |
|
1684 |
// case cs_DATA: |
|
1685 |
// case cs_RENEGOTIATE: |
|
1686 |
// case cs_SENT_CLOSE: |
|
1687 |
default: |
|
1688 |
synchronized (this) { |
|
1689 |
if (((state = getConnectionState()) == cs_CLOSED) || |
|
1690 |
(state == cs_ERROR) || (state == cs_APP_CLOSED)) { |
|
1691 |
return; // connection was closed while we waited |
|
1692 |
} |
|
1693 |
if (state != cs_SENT_CLOSE) { |
|
9514
bdb24db75fe8
7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents:
9246
diff
changeset
|
1694 |
try { |
bdb24db75fe8
7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents:
9246
diff
changeset
|
1695 |
warning(Alerts.alert_close_notify); |
bdb24db75fe8
7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents:
9246
diff
changeset
|
1696 |
connectionState = cs_SENT_CLOSE; |
bdb24db75fe8
7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents:
9246
diff
changeset
|
1697 |
} catch (Throwable th) { |
bdb24db75fe8
7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents:
9246
diff
changeset
|
1698 |
// we need to ensure socket is closed out |
bdb24db75fe8
7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents:
9246
diff
changeset
|
1699 |
// if we encounter any errors. |
bdb24db75fe8
7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents:
9246
diff
changeset
|
1700 |
connectionState = cs_ERROR; |
bdb24db75fe8
7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents:
9246
diff
changeset
|
1701 |
// cache this for later use |
bdb24db75fe8
7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents:
9246
diff
changeset
|
1702 |
cachedThrowable = th; |
bdb24db75fe8
7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents:
9246
diff
changeset
|
1703 |
closeSocketCalled = true; |
bdb24db75fe8
7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents:
9246
diff
changeset
|
1704 |
closeSocket(selfInitiated); |
bdb24db75fe8
7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents:
9246
diff
changeset
|
1705 |
} |
2 | 1706 |
} |
1707 |
} |
|
1708 |
// If state was cs_SENT_CLOSE before, we don't do the actual |
|
1709 |
// closing since it is already in progress. |
|
1710 |
if (state == cs_SENT_CLOSE) { |
|
1711 |
if (debug != null && Debug.isOn("ssl")) { |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
1712 |
System.out.println(Thread.currentThread().getName() + |
2 | 1713 |
", close invoked again; state = " + |
1714 |
getConnectionState()); |
|
1715 |
} |
|
1716 |
if (selfInitiated == false) { |
|
1717 |
// We were called because a close_notify message was |
|
1718 |
// received. This may be due to another thread calling |
|
1719 |
// read() or due to our call to waitForClose() below. |
|
1720 |
// In either case, just return. |
|
1721 |
return; |
|
1722 |
} |
|
1723 |
// Another thread explicitly called close(). We need to |
|
1724 |
// wait for the closing to complete before returning. |
|
1725 |
synchronized (this) { |
|
1726 |
while (connectionState < cs_CLOSED) { |
|
1727 |
try { |
|
1728 |
this.wait(); |
|
1729 |
} catch (InterruptedException e) { |
|
1730 |
// ignore |
|
1731 |
} |
|
1732 |
} |
|
1733 |
} |
|
1734 |
if ((debug != null) && Debug.isOn("ssl")) { |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
1735 |
System.out.println(Thread.currentThread().getName() + |
2 | 1736 |
", after primary close; state = " + |
1737 |
getConnectionState()); |
|
1738 |
} |
|
1739 |
return; |
|
1740 |
} |
|
1741 |
||
9514
bdb24db75fe8
7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents:
9246
diff
changeset
|
1742 |
if (!closeSocketCalled) { |
bdb24db75fe8
7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents:
9246
diff
changeset
|
1743 |
closeSocketCalled = true; |
bdb24db75fe8
7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents:
9246
diff
changeset
|
1744 |
closeSocket(selfInitiated); |
2 | 1745 |
} |
1746 |
||
1747 |
break; |
|
1748 |
} |
|
1749 |
} finally { |
|
1750 |
synchronized (this) { |
|
1751 |
// Upon exit from this method, the state is always >= cs_CLOSED |
|
1752 |
connectionState = (connectionState == cs_APP_CLOSED) |
|
1753 |
? cs_APP_CLOSED : cs_CLOSED; |
|
1754 |
// notify any threads waiting for the closing to finish |
|
1755 |
this.notifyAll(); |
|
1756 |
} |
|
30904 | 1757 |
|
9514
bdb24db75fe8
7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents:
9246
diff
changeset
|
1758 |
if (cachedThrowable != null) { |
bdb24db75fe8
7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents:
9246
diff
changeset
|
1759 |
/* |
bdb24db75fe8
7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents:
9246
diff
changeset
|
1760 |
* Rethrow the error to the calling method |
bdb24db75fe8
7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents:
9246
diff
changeset
|
1761 |
* The Throwable caught can only be an Error or RuntimeException |
bdb24db75fe8
7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents:
9246
diff
changeset
|
1762 |
*/ |
30904 | 1763 |
if (cachedThrowable instanceof Error) { |
1764 |
throw (Error)cachedThrowable; |
|
1765 |
} else if (cachedThrowable instanceof RuntimeException) { |
|
1766 |
throw (RuntimeException)cachedThrowable; |
|
1767 |
} // Otherwise, unlikely |
|
9514
bdb24db75fe8
7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents:
9246
diff
changeset
|
1768 |
} |
2 | 1769 |
} |
1770 |
} |
|
1771 |
||
1772 |
/** |
|
1773 |
* Reads a close_notify or a fatal alert from the input stream. |
|
1774 |
* Keep reading records until we get a close_notify or until |
|
1775 |
* the connection is otherwise closed. The close_notify or alert |
|
1776 |
* might be read by another reader, |
|
1777 |
* which will then process the close and set the connection state. |
|
1778 |
*/ |
|
1779 |
void waitForClose(boolean rethrow) throws IOException { |
|
1780 |
if (debug != null && Debug.isOn("ssl")) { |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
1781 |
System.out.println(Thread.currentThread().getName() + |
2 | 1782 |
", waiting for close_notify or alert: state " |
1783 |
+ getConnectionState()); |
|
1784 |
} |
|
1785 |
||
1786 |
try { |
|
1787 |
int state; |
|
1788 |
||
1789 |
while (((state = getConnectionState()) != cs_CLOSED) && |
|
1790 |
(state != cs_ERROR) && (state != cs_APP_CLOSED)) { |
|
1791 |
||
1792 |
// Ask for app data and then throw it away |
|
1793 |
try { |
|
30904 | 1794 |
readRecord(true); |
2 | 1795 |
} catch (SocketTimeoutException e) { |
1796 |
// if time out, ignore the exception and continue |
|
1797 |
} |
|
1798 |
} |
|
1799 |
} catch (IOException e) { |
|
1800 |
if (debug != null && Debug.isOn("ssl")) { |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
1801 |
System.out.println(Thread.currentThread().getName() + |
2 | 1802 |
", Exception while waiting for close " +e); |
1803 |
} |
|
1804 |
if (rethrow) { |
|
1805 |
throw e; // pass exception up |
|
1806 |
} |
|
1807 |
} |
|
1808 |
} |
|
1809 |
||
1810 |
// |
|
1811 |
// EXCEPTION AND ALERT HANDLING |
|
1812 |
// |
|
1813 |
||
1814 |
/** |
|
1815 |
* Handle an exception. This method is called by top level exception |
|
1816 |
* handlers (in read(), write()) to make sure we always shutdown the |
|
1817 |
* connection correctly and do not pass runtime exception to the |
|
1818 |
* application. |
|
1819 |
*/ |
|
1820 |
void handleException(Exception e) throws IOException { |
|
1821 |
handleException(e, true); |
|
1822 |
} |
|
1823 |
||
1824 |
/** |
|
1825 |
* Handle an exception. This method is called by top level exception |
|
1826 |
* handlers (in read(), write(), startHandshake()) to make sure we |
|
1827 |
* always shutdown the connection correctly and do not pass runtime |
|
1828 |
* exception to the application. |
|
1829 |
* |
|
1830 |
* This method never returns normally, it always throws an IOException. |
|
1831 |
* |
|
1832 |
* We first check if the socket has already been shutdown because of an |
|
1833 |
* error. If so, we just rethrow the exception. If the socket has not |
|
1834 |
* been shutdown, we sent a fatal alert and remember the exception. |
|
1835 |
* |
|
1836 |
* @param e the Exception |
|
1837 |
* @param resumable indicates the caller process is resumable from the |
|
1838 |
* exception. If <code>resumable</code>, the socket will be |
|
1839 |
* reserved for exceptions like timeout; otherwise, the socket |
|
1840 |
* will be closed, no further communications could be done. |
|
1841 |
*/ |
|
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
31918
diff
changeset
|
1842 |
private synchronized void handleException(Exception e, boolean resumable) |
2 | 1843 |
throws IOException { |
1844 |
if ((debug != null) && Debug.isOn("ssl")) { |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
1845 |
System.out.println(Thread.currentThread().getName() + |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
1846 |
", handling exception: " + e.toString()); |
2 | 1847 |
} |
1848 |
||
1849 |
// don't close the Socket in case of timeouts or interrupts if |
|
1850 |
// the process is resumable. |
|
1851 |
if (e instanceof InterruptedIOException && resumable) { |
|
1852 |
throw (IOException)e; |
|
1853 |
} |
|
1854 |
||
1855 |
// if we've already shutdown because of an error, |
|
1856 |
// there is nothing to do except rethrow the exception |
|
1857 |
if (closeReason != null) { |
|
1858 |
if (e instanceof IOException) { // includes SSLException |
|
1859 |
throw (IOException)e; |
|
1860 |
} else { |
|
1861 |
// this is odd, not an IOException. |
|
1862 |
// normally, this should not happen |
|
1863 |
// if closeReason has been already been set |
|
1864 |
throw Alerts.getSSLException(Alerts.alert_internal_error, e, |
|
1865 |
"Unexpected exception"); |
|
1866 |
} |
|
1867 |
} |
|
1868 |
||
1869 |
// need to perform error shutdown |
|
1870 |
boolean isSSLException = (e instanceof SSLException); |
|
30904 | 1871 |
if ((!isSSLException) && (e instanceof IOException)) { |
2 | 1872 |
// IOException from the socket |
1873 |
// this means the TCP connection is already dead |
|
1874 |
// we call fatal just to set the error status |
|
1875 |
try { |
|
1876 |
fatal(Alerts.alert_unexpected_message, e); |
|
1877 |
} catch (IOException ee) { |
|
1878 |
// ignore (IOException wrapped in SSLException) |
|
1879 |
} |
|
1880 |
// rethrow original IOException |
|
1881 |
throw (IOException)e; |
|
1882 |
} |
|
1883 |
||
1884 |
// must be SSLException or RuntimeException |
|
1885 |
byte alertType; |
|
1886 |
if (isSSLException) { |
|
1887 |
if (e instanceof SSLHandshakeException) { |
|
1888 |
alertType = Alerts.alert_handshake_failure; |
|
1889 |
} else { |
|
1890 |
alertType = Alerts.alert_unexpected_message; |
|
1891 |
} |
|
1892 |
} else { |
|
1893 |
alertType = Alerts.alert_internal_error; |
|
1894 |
} |
|
1895 |
fatal(alertType, e); |
|
1896 |
} |
|
1897 |
||
1898 |
/* |
|
1899 |
* Send a warning alert. |
|
1900 |
*/ |
|
1901 |
void warning(byte description) { |
|
1902 |
sendAlert(Alerts.alert_warning, description); |
|
1903 |
} |
|
1904 |
||
1905 |
synchronized void fatal(byte description, String diagnostic) |
|
1906 |
throws IOException { |
|
1907 |
fatal(description, diagnostic, null); |
|
1908 |
} |
|
1909 |
||
1910 |
synchronized void fatal(byte description, Throwable cause) |
|
1911 |
throws IOException { |
|
1912 |
fatal(description, null, cause); |
|
1913 |
} |
|
1914 |
||
1915 |
/* |
|
1916 |
* Send a fatal alert, and throw an exception so that callers will |
|
1917 |
* need to stand on their heads to accidentally continue processing. |
|
1918 |
*/ |
|
1919 |
synchronized void fatal(byte description, String diagnostic, |
|
1920 |
Throwable cause) throws IOException { |
|
30904 | 1921 |
|
1922 |
// Be care of deadlock. Please don't synchronize readLock. |
|
1923 |
try { |
|
1924 |
inputRecord.close(); |
|
1925 |
} catch (IOException ioe) { |
|
1926 |
// ignore |
|
2 | 1927 |
} |
30904 | 1928 |
|
2 | 1929 |
sess.invalidate(); |
7043 | 1930 |
if (handshakeSession != null) { |
1931 |
handshakeSession.invalidate(); |
|
1932 |
} |
|
2 | 1933 |
|
1934 |
int oldState = connectionState; |
|
9514
bdb24db75fe8
7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents:
9246
diff
changeset
|
1935 |
if (connectionState < cs_ERROR) { |
bdb24db75fe8
7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents:
9246
diff
changeset
|
1936 |
connectionState = cs_ERROR; |
bdb24db75fe8
7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents:
9246
diff
changeset
|
1937 |
} |
2 | 1938 |
|
1939 |
/* |
|
1940 |
* Has there been an error received yet? If not, remember it. |
|
1941 |
* By RFC 2246, we don't bother waiting for a response. |
|
1942 |
* Fatal errors require immediate shutdown. |
|
1943 |
*/ |
|
1944 |
if (closeReason == null) { |
|
1945 |
/* |
|
1946 |
* Try to clear the kernel buffer to avoid TCP connection resets. |
|
1947 |
*/ |
|
1948 |
if (oldState == cs_HANDSHAKE) { |
|
1949 |
sockInput.skip(sockInput.available()); |
|
1950 |
} |
|
100
01ef29ca378f
6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
2
diff
changeset
|
1951 |
|
01ef29ca378f
6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
2
diff
changeset
|
1952 |
// If the description equals -1, the alert won't be sent to peer. |
01ef29ca378f
6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
2
diff
changeset
|
1953 |
if (description != -1) { |
01ef29ca378f
6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
2
diff
changeset
|
1954 |
sendAlert(Alerts.alert_fatal, description); |
01ef29ca378f
6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
2
diff
changeset
|
1955 |
} |
2 | 1956 |
if (cause instanceof SSLException) { // only true if != null |
1957 |
closeReason = (SSLException)cause; |
|
1958 |
} else { |
|
1959 |
closeReason = |
|
1960 |
Alerts.getSSLException(description, cause, diagnostic); |
|
1961 |
} |
|
1962 |
} |
|
1963 |
||
1964 |
/* |
|
1965 |
* Clean up our side. |
|
1966 |
*/ |
|
1967 |
closeSocket(); |
|
1763
0a6b65d56746
6750401: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes,with PCKS11 provider
wetmore
parents:
1580
diff
changeset
|
1968 |
|
30904 | 1969 |
// Be care of deadlock. Please don't synchronize writeLock. |
1970 |
try { |
|
1971 |
outputRecord.close(); |
|
1972 |
} catch (IOException ioe) { |
|
1973 |
// ignore |
|
9514
bdb24db75fe8
7025227: SSLSocketImpl does not close the TCP layer socket if a close notify cannot be sent to the peer
coffeys
parents:
9246
diff
changeset
|
1974 |
} |
1763
0a6b65d56746
6750401: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes,with PCKS11 provider
wetmore
parents:
1580
diff
changeset
|
1975 |
|
2 | 1976 |
throw closeReason; |
1977 |
} |
|
1978 |
||
1979 |
||
1980 |
/* |
|
1981 |
* Process an incoming alert ... caller must already have synchronized |
|
1982 |
* access to "this". |
|
1983 |
*/ |
|
30904 | 1984 |
private void recvAlert(ByteBuffer fragment) throws IOException { |
1985 |
byte level = fragment.get(); |
|
1986 |
byte description = fragment.get(); |
|
1987 |
||
2 | 1988 |
if (description == -1) { // check for short message |
1989 |
fatal(Alerts.alert_illegal_parameter, "Short alert message"); |
|
1990 |
} |
|
1991 |
||
1992 |
if (debug != null && (Debug.isOn("record") || |
|
1993 |
Debug.isOn("handshake"))) { |
|
1994 |
synchronized (System.out) { |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
1995 |
System.out.print(Thread.currentThread().getName()); |
2 | 1996 |
System.out.print(", RECV " + protocolVersion + " ALERT: "); |
1997 |
if (level == Alerts.alert_fatal) { |
|
1998 |
System.out.print("fatal, "); |
|
1999 |
} else if (level == Alerts.alert_warning) { |
|
2000 |
System.out.print("warning, "); |
|
2001 |
} else { |
|
2002 |
System.out.print("<level " + (0x0ff & level) + ">, "); |
|
2003 |
} |
|
2004 |
System.out.println(Alerts.alertDescription(description)); |
|
2005 |
} |
|
2006 |
} |
|
2007 |
||
2008 |
if (level == Alerts.alert_warning) { |
|
2009 |
if (description == Alerts.alert_close_notify) { |
|
2010 |
if (connectionState == cs_HANDSHAKE) { |
|
2011 |
fatal(Alerts.alert_unexpected_message, |
|
2012 |
"Received close_notify during handshake"); |
|
2013 |
} else { |
|
2014 |
closeInternal(false); // reply to close |
|
2015 |
} |
|
2016 |
} else { |
|
2017 |
||
2018 |
// |
|
2019 |
// The other legal warnings relate to certificates, |
|
2020 |
// e.g. no_certificate, bad_certificate, etc; these |
|
2021 |
// are important to the handshaking code, which can |
|
2022 |
// also handle illegal protocol alerts if needed. |
|
2023 |
// |
|
2024 |
if (handshaker != null) { |
|
2025 |
handshaker.handshakeAlert(description); |
|
2026 |
} |
|
2027 |
} |
|
2028 |
} else { // fatal or unknown level |
|
2029 |
String reason = "Received fatal alert: " |
|
2030 |
+ Alerts.alertDescription(description); |
|
2031 |
if (closeReason == null) { |
|
2032 |
closeReason = Alerts.getSSLException(description, reason); |
|
2033 |
} |
|
2034 |
fatal(Alerts.alert_unexpected_message, reason); |
|
2035 |
} |
|
2036 |
} |
|
2037 |
||
2038 |
||
2039 |
/* |
|
2040 |
* Emit alerts. Caller must have synchronized with "this". |
|
2041 |
*/ |
|
2042 |
private void sendAlert(byte level, byte description) { |
|
7039 | 2043 |
// the connectionState cannot be cs_START |
100
01ef29ca378f
6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
2
diff
changeset
|
2044 |
if (connectionState >= cs_SENT_CLOSE) { |
2 | 2045 |
return; |
2046 |
} |
|
2047 |
||
7039 | 2048 |
// For initial handshaking, don't send alert message to peer if |
2049 |
// handshaker has not started. |
|
30904 | 2050 |
// |
2051 |
// Shall we send an fatal alter to terminate the connection gracefully? |
|
2052 |
if (connectionState <= cs_HANDSHAKE && |
|
2053 |
(handshaker == null || !handshaker.started() || |
|
2054 |
!handshaker.activated())) { |
|
7039 | 2055 |
return; |
2056 |
} |
|
2057 |
||
2 | 2058 |
boolean useDebug = debug != null && Debug.isOn("ssl"); |
2059 |
if (useDebug) { |
|
2060 |
synchronized (System.out) { |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
2061 |
System.out.print(Thread.currentThread().getName()); |
2 | 2062 |
System.out.print(", SEND " + protocolVersion + " ALERT: "); |
2063 |
if (level == Alerts.alert_fatal) { |
|
2064 |
System.out.print("fatal, "); |
|
2065 |
} else if (level == Alerts.alert_warning) { |
|
2066 |
System.out.print("warning, "); |
|
2067 |
} else { |
|
2068 |
System.out.print("<level = " + (0x0ff & level) + ">, "); |
|
2069 |
} |
|
2070 |
System.out.println("description = " |
|
2071 |
+ Alerts.alertDescription(description)); |
|
2072 |
} |
|
2073 |
} |
|
2074 |
||
2075 |
try { |
|
30904 | 2076 |
writeAlert(level, description); |
2 | 2077 |
} catch (IOException e) { |
2078 |
if (useDebug) { |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
2079 |
System.out.println(Thread.currentThread().getName() + |
2 | 2080 |
", Exception sending alert: " + e); |
2081 |
} |
|
2082 |
} |
|
2083 |
} |
|
2084 |
||
2085 |
// |
|
2086 |
// VARIOUS OTHER METHODS |
|
2087 |
// |
|
2088 |
||
2089 |
// used by Handshaker |
|
30904 | 2090 |
void changeWriteCiphers() throws IOException { |
2091 |
Authenticator writeAuthenticator; |
|
2092 |
CipherBox writeCipher; |
|
2 | 2093 |
try { |
2094 |
writeCipher = handshaker.newWriteCipher(); |
|
16913 | 2095 |
writeAuthenticator = handshaker.newWriteAuthenticator(); |
2 | 2096 |
} catch (GeneralSecurityException e) { |
2097 |
// "can't happen" |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
10128
diff
changeset
|
2098 |
throw new SSLException("Algorithm missing: ", e); |
2 | 2099 |
} |
30904 | 2100 |
outputRecord.changeWriteCiphers(writeAuthenticator, writeCipher); |
2 | 2101 |
} |
2102 |
||
2103 |
/* |
|
2104 |
* Updates the SSL version associated with this connection. |
|
2105 |
* Called from Handshaker once it has determined the negotiated version. |
|
2106 |
*/ |
|
2107 |
synchronized void setVersion(ProtocolVersion protocolVersion) { |
|
2108 |
this.protocolVersion = protocolVersion; |
|
30904 | 2109 |
outputRecord.setVersion(protocolVersion); |
2 | 2110 |
} |
2111 |
||
37601
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
2112 |
// |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
2113 |
// ONLY used by ClientHandshaker for the server hostname during handshaking |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
2114 |
// |
2 | 2115 |
synchronized String getHost() { |
2068
cdbc5929b91e
5067458: Loopback SSLSocketImpl createSocket is throwing an exception
xuelei
parents:
1763
diff
changeset
|
2116 |
// Note that the host may be null or empty for localhost. |
cdbc5929b91e
5067458: Loopback SSLSocketImpl createSocket is throwing an exception
xuelei
parents:
1763
diff
changeset
|
2117 |
if (host == null || host.length() == 0) { |
37601
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
2118 |
useImplicitHost(true); |
2 | 2119 |
} |
31687 | 2120 |
|
2 | 2121 |
return host; |
2122 |
} |
|
2123 |
||
31687 | 2124 |
/* |
37601
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
2125 |
* Try to set and use the implicit specified hostname |
31687 | 2126 |
*/ |
37601
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
2127 |
private synchronized void useImplicitHost(boolean noSniUpdate) { |
31687 | 2128 |
|
37601
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
2129 |
// Note: If the local name service is not trustworthy, reverse |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
2130 |
// host name resolution should not be performed for endpoint |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
2131 |
// identification. Use the application original specified |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
2132 |
// hostname or IP address instead. |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
2133 |
|
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
2134 |
// Get the original hostname via jdk.internal.misc.SharedSecrets |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
2135 |
InetAddress inetAddress = getInetAddress(); |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
2136 |
if (inetAddress == null) { // not connected |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
2137 |
return; |
31687 | 2138 |
} |
2139 |
||
37601
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
2140 |
JavaNetInetAddressAccess jna = |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
2141 |
SharedSecrets.getJavaNetInetAddressAccess(); |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
2142 |
String originalHostname = jna.getOriginalHostName(inetAddress); |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
2143 |
if ((originalHostname != null) && |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
2144 |
(originalHostname.length() != 0)) { |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
2145 |
|
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
2146 |
host = originalHostname; |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
2147 |
if (!noSniUpdate && serverNames.isEmpty() && !noSniExtension) { |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
2148 |
serverNames = |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
2149 |
Utilities.addToSNIServerNameList(serverNames, host); |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
2150 |
|
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
2151 |
if (!roleIsServer && |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
2152 |
(handshaker != null) && !handshaker.started()) { |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
2153 |
handshaker.setSNIServerNames(serverNames); |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
2154 |
} |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
2155 |
} |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
2156 |
|
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
2157 |
return; |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
2158 |
} |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
2159 |
|
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
2160 |
// No explicitly specified hostname, no server name indication. |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
2161 |
if (!trustNameService) { |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
2162 |
// The local name service is not trustworthy, use IP address. |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
2163 |
host = inetAddress.getHostAddress(); |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
2164 |
} else { |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
2165 |
// Use the underlying reverse host name resolution service. |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
2166 |
host = getInetAddress().getHostName(); |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
2167 |
} |
31687 | 2168 |
} |
2169 |
||
5162
0dbedf4fdb8c
6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents:
2068
diff
changeset
|
2170 |
// ONLY used by HttpsClient to setup the URI specified hostname |
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
2171 |
// |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
2172 |
// Please NOTE that this method MUST be called before calling to |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
2173 |
// SSLSocket.setSSLParameters(). Otherwise, the {@code host} parameter |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
2174 |
// may override SNIHostName in the customized server name indication. |
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
31918
diff
changeset
|
2175 |
public synchronized void setHost(String host) { |
5162
0dbedf4fdb8c
6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents:
2068
diff
changeset
|
2176 |
this.host = host; |
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
2177 |
this.serverNames = |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
2178 |
Utilities.addToSNIServerNameList(this.serverNames, this.host); |
37601
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
2179 |
|
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
2180 |
if (!roleIsServer && (handshaker != null) && !handshaker.started()) { |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
2181 |
handshaker.setSNIServerNames(serverNames); |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
2182 |
} |
5162
0dbedf4fdb8c
6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents:
2068
diff
changeset
|
2183 |
} |
0dbedf4fdb8c
6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents:
2068
diff
changeset
|
2184 |
|
2 | 2185 |
/** |
2186 |
* Gets an input stream to read from the peer on the other side. |
|
2187 |
* Data read from this stream was always integrity protected in |
|
2188 |
* transit, and will usually have been confidentiality protected. |
|
2189 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
2190 |
@Override |
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
31918
diff
changeset
|
2191 |
public synchronized InputStream getInputStream() throws IOException { |
2 | 2192 |
if (isClosed()) { |
2193 |
throw new SocketException("Socket is closed"); |
|
2194 |
} |
|
2195 |
||
2196 |
/* |
|
2197 |
* Can't call isConnected() here, because the Handshakers |
|
2198 |
* do some initialization before we actually connect. |
|
2199 |
*/ |
|
2200 |
if (connectionState == cs_START) { |
|
2201 |
throw new SocketException("Socket is not connected"); |
|
2202 |
} |
|
2203 |
||
2204 |
return input; |
|
2205 |
} |
|
2206 |
||
2207 |
/** |
|
2208 |
* Gets an output stream to write to the peer on the other side. |
|
2209 |
* Data written on this stream is always integrity protected, and |
|
2210 |
* will usually be confidentiality protected. |
|
2211 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
2212 |
@Override |
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
31918
diff
changeset
|
2213 |
public synchronized OutputStream getOutputStream() throws IOException { |
2 | 2214 |
if (isClosed()) { |
2215 |
throw new SocketException("Socket is closed"); |
|
2216 |
} |
|
2217 |
||
2218 |
/* |
|
2219 |
* Can't call isConnected() here, because the Handshakers |
|
2220 |
* do some initialization before we actually connect. |
|
2221 |
*/ |
|
2222 |
if (connectionState == cs_START) { |
|
2223 |
throw new SocketException("Socket is not connected"); |
|
2224 |
} |
|
2225 |
||
2226 |
return output; |
|
2227 |
} |
|
2228 |
||
2229 |
/** |
|
28059
e576535359cc
8067377: My hobby: caning, then then canning, the the can-can
martin
parents:
25859
diff
changeset
|
2230 |
* Returns the SSL Session in use by this connection. These can |
2 | 2231 |
* be long lived, and frequently correspond to an entire login session |
2232 |
* for some user. |
|
2233 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
2234 |
@Override |
2 | 2235 |
public SSLSession getSession() { |
2236 |
/* |
|
2237 |
* Force a synchronous handshake, if appropriate. |
|
2238 |
*/ |
|
2239 |
if (getConnectionState() == cs_HANDSHAKE) { |
|
2240 |
try { |
|
2241 |
// start handshaking, if failed, the connection will be closed. |
|
2242 |
startHandshake(false); |
|
2243 |
} catch (IOException e) { |
|
2244 |
// handshake failed. log and return a nullSession |
|
2245 |
if (debug != null && Debug.isOn("handshake")) { |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
2246 |
System.out.println(Thread.currentThread().getName() + |
2 | 2247 |
", IOException in getSession(): " + e); |
2248 |
} |
|
2249 |
} |
|
2250 |
} |
|
2251 |
synchronized (this) { |
|
2252 |
return sess; |
|
2253 |
} |
|
2254 |
} |
|
2255 |
||
7043 | 2256 |
@Override |
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
31918
diff
changeset
|
2257 |
public synchronized SSLSession getHandshakeSession() { |
7043 | 2258 |
return handshakeSession; |
2259 |
} |
|
2260 |
||
2261 |
synchronized void setHandshakeSession(SSLSessionImpl session) { |
|
30904 | 2262 |
// update the fragment size, which may be negotiated during handshaking |
2263 |
inputRecord.changeFragmentSize(session.getNegotiatedMaxFragSize()); |
|
2264 |
outputRecord.changeFragmentSize(session.getNegotiatedMaxFragSize()); |
|
2265 |
||
7043 | 2266 |
handshakeSession = session; |
2267 |
} |
|
2268 |
||
2 | 2269 |
/** |
2270 |
* Controls whether new connections may cause creation of new SSL |
|
2271 |
* sessions. |
|
2272 |
* |
|
2273 |
* As long as handshaking has not started, we can change |
|
2274 |
* whether we enable session creations. Otherwise, |
|
2275 |
* we will need to wait for the next handshake. |
|
2276 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
2277 |
@Override |
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
31918
diff
changeset
|
2278 |
public synchronized void setEnableSessionCreation(boolean flag) { |
2 | 2279 |
enableSessionCreation = flag; |
2280 |
||
7039 | 2281 |
if ((handshaker != null) && !handshaker.activated()) { |
2 | 2282 |
handshaker.setEnableSessionCreation(enableSessionCreation); |
2283 |
} |
|
2284 |
} |
|
2285 |
||
2286 |
/** |
|
2287 |
* Returns true if new connections may cause creation of new SSL |
|
2288 |
* sessions. |
|
2289 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
2290 |
@Override |
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
31918
diff
changeset
|
2291 |
public synchronized boolean getEnableSessionCreation() { |
2 | 2292 |
return enableSessionCreation; |
2293 |
} |
|
2294 |
||
2295 |
||
2296 |
/** |
|
2297 |
* Sets the flag controlling whether a server mode socket |
|
2298 |
* *REQUIRES* SSL client authentication. |
|
2299 |
* |
|
2300 |
* As long as handshaking has not started, we can change |
|
2301 |
* whether client authentication is needed. Otherwise, |
|
2302 |
* we will need to wait for the next handshake. |
|
2303 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
2304 |
@Override |
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
31918
diff
changeset
|
2305 |
public synchronized void setNeedClientAuth(boolean flag) { |
30904 | 2306 |
doClientAuth = (flag ? ClientAuthType.CLIENT_AUTH_REQUIRED : |
2307 |
ClientAuthType.CLIENT_AUTH_NONE); |
|
2 | 2308 |
|
2309 |
if ((handshaker != null) && |
|
2310 |
(handshaker instanceof ServerHandshaker) && |
|
7039 | 2311 |
!handshaker.activated()) { |
2 | 2312 |
((ServerHandshaker) handshaker).setClientAuth(doClientAuth); |
2313 |
} |
|
2314 |
} |
|
2315 |
||
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
2316 |
@Override |
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
31918
diff
changeset
|
2317 |
public synchronized boolean getNeedClientAuth() { |
30904 | 2318 |
return (doClientAuth == ClientAuthType.CLIENT_AUTH_REQUIRED); |
2 | 2319 |
} |
2320 |
||
2321 |
/** |
|
2322 |
* Sets the flag controlling whether a server mode socket |
|
2323 |
* *REQUESTS* SSL client authentication. |
|
2324 |
* |
|
2325 |
* As long as handshaking has not started, we can change |
|
2326 |
* whether client authentication is requested. Otherwise, |
|
2327 |
* we will need to wait for the next handshake. |
|
2328 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
2329 |
@Override |
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
31918
diff
changeset
|
2330 |
public synchronized void setWantClientAuth(boolean flag) { |
30904 | 2331 |
doClientAuth = (flag ? ClientAuthType.CLIENT_AUTH_REQUESTED : |
2332 |
ClientAuthType.CLIENT_AUTH_NONE); |
|
2 | 2333 |
|
2334 |
if ((handshaker != null) && |
|
2335 |
(handshaker instanceof ServerHandshaker) && |
|
7039 | 2336 |
!handshaker.activated()) { |
2 | 2337 |
((ServerHandshaker) handshaker).setClientAuth(doClientAuth); |
2338 |
} |
|
2339 |
} |
|
2340 |
||
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
2341 |
@Override |
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
31918
diff
changeset
|
2342 |
public synchronized boolean getWantClientAuth() { |
30904 | 2343 |
return (doClientAuth == ClientAuthType.CLIENT_AUTH_REQUESTED); |
2 | 2344 |
} |
2345 |
||
2346 |
||
2347 |
/** |
|
2348 |
* Sets the flag controlling whether the socket is in SSL |
|
2349 |
* client or server mode. Must be called before any SSL |
|
2350 |
* traffic has started. |
|
2351 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
2352 |
@Override |
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
10128
diff
changeset
|
2353 |
@SuppressWarnings("fallthrough") |
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
31918
diff
changeset
|
2354 |
public synchronized void setUseClientMode(boolean flag) { |
2 | 2355 |
switch (connectionState) { |
2356 |
||
2357 |
case cs_START: |
|
7039 | 2358 |
/* |
2359 |
* If we need to change the socket mode and the enabled |
|
30904 | 2360 |
* protocols and cipher suites haven't specifically been |
2361 |
* set by the user, change them to the corresponding |
|
2362 |
* default ones. |
|
7039 | 2363 |
*/ |
30904 | 2364 |
if (roleIsServer != (!flag)) { |
2365 |
if (sslContext.isDefaultProtocolList(enabledProtocols)) { |
|
2366 |
enabledProtocols = |
|
2367 |
sslContext.getDefaultProtocolList(!flag); |
|
2368 |
} |
|
2369 |
||
2370 |
if (sslContext.isDefaultCipherSuiteList(enabledCipherSuites)) { |
|
2371 |
enabledCipherSuites = |
|
2372 |
sslContext.getDefaultCipherSuiteList(!flag); |
|
2373 |
} |
|
7039 | 2374 |
} |
30904 | 2375 |
|
2 | 2376 |
roleIsServer = !flag; |
2377 |
break; |
|
2378 |
||
2379 |
case cs_HANDSHAKE: |
|
2380 |
/* |
|
2381 |
* If we have a handshaker, but haven't started |
|
2382 |
* SSL traffic, we can throw away our current |
|
2383 |
* handshaker, and start from scratch. Don't |
|
2384 |
* need to call doneConnect() again, we already |
|
2385 |
* have the streams. |
|
2386 |
*/ |
|
2387 |
assert(handshaker != null); |
|
7039 | 2388 |
if (!handshaker.activated()) { |
2389 |
/* |
|
2390 |
* If we need to change the socket mode and the enabled |
|
30904 | 2391 |
* protocols and cipher suites haven't specifically been |
2392 |
* set by the user, change them to the corresponding |
|
2393 |
* default ones. |
|
7039 | 2394 |
*/ |
30904 | 2395 |
if (roleIsServer != (!flag)) { |
2396 |
if (sslContext.isDefaultProtocolList(enabledProtocols)) { |
|
2397 |
enabledProtocols = |
|
2398 |
sslContext.getDefaultProtocolList(!flag); |
|
2399 |
} |
|
2400 |
||
2401 |
if (sslContext.isDefaultCipherSuiteList( |
|
2402 |
enabledCipherSuites)) { |
|
2403 |
enabledCipherSuites = |
|
2404 |
sslContext.getDefaultCipherSuiteList(!flag); |
|
2405 |
} |
|
7039 | 2406 |
} |
30904 | 2407 |
|
2 | 2408 |
roleIsServer = !flag; |
2409 |
connectionState = cs_START; |
|
2410 |
initHandshaker(); |
|
2411 |
break; |
|
2412 |
} |
|
2413 |
||
2414 |
// If handshake has started, that's an error. Fall through... |
|
2415 |
||
2416 |
default: |
|
2417 |
if (debug != null && Debug.isOn("ssl")) { |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
2418 |
System.out.println(Thread.currentThread().getName() + |
2 | 2419 |
", setUseClientMode() invoked in state = " + |
2420 |
connectionState); |
|
2421 |
} |
|
2422 |
throw new IllegalArgumentException( |
|
2423 |
"Cannot change mode after SSL traffic has started"); |
|
2424 |
} |
|
2425 |
} |
|
2426 |
||
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
2427 |
@Override |
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
31918
diff
changeset
|
2428 |
public synchronized boolean getUseClientMode() { |
2 | 2429 |
return !roleIsServer; |
2430 |
} |
|
2431 |
||
2432 |
||
2433 |
/** |
|
2434 |
* Returns the names of the cipher suites which could be enabled for use |
|
2435 |
* on an SSL connection. Normally, only a subset of these will actually |
|
2436 |
* be enabled by default, since this list may include cipher suites which |
|
2437 |
* do not support the mutual authentication of servers and clients, or |
|
2438 |
* which do not protect data confidentiality. Servers may also need |
|
2439 |
* certain kinds of certificates to use certain cipher suites. |
|
2440 |
* |
|
2441 |
* @return an array of cipher suite names |
|
2442 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
2443 |
@Override |
2 | 2444 |
public String[] getSupportedCipherSuites() { |
13815 | 2445 |
return sslContext.getSupportedCipherSuiteList().toStringArray(); |
2 | 2446 |
} |
2447 |
||
2448 |
/** |
|
2449 |
* Controls which particular cipher suites are enabled for use on |
|
2450 |
* this connection. The cipher suites must have been listed by |
|
2451 |
* getCipherSuites() as being supported. Even if a suite has been |
|
2452 |
* enabled, it might never be used if no peer supports it or the |
|
2453 |
* requisite certificates (and private keys) are not available. |
|
2454 |
* |
|
2455 |
* @param suites Names of all the cipher suites to enable. |
|
2456 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
2457 |
@Override |
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
31918
diff
changeset
|
2458 |
public synchronized void setEnabledCipherSuites(String[] suites) { |
2 | 2459 |
enabledCipherSuites = new CipherSuiteList(suites); |
7039 | 2460 |
if ((handshaker != null) && !handshaker.activated()) { |
2461 |
handshaker.setEnabledCipherSuites(enabledCipherSuites); |
|
2 | 2462 |
} |
2463 |
} |
|
2464 |
||
2465 |
/** |
|
2466 |
* Returns the names of the SSL cipher suites which are currently enabled |
|
2467 |
* for use on this connection. When an SSL socket is first created, |
|
2468 |
* all enabled cipher suites <em>(a)</em> protect data confidentiality, |
|
2469 |
* by traffic encryption, and <em>(b)</em> can mutually authenticate |
|
2470 |
* both clients and servers. Thus, in some environments, this value |
|
2471 |
* might be empty. |
|
2472 |
* |
|
2473 |
* @return an array of cipher suite names |
|
2474 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
2475 |
@Override |
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
31918
diff
changeset
|
2476 |
public synchronized String[] getEnabledCipherSuites() { |
2 | 2477 |
return enabledCipherSuites.toStringArray(); |
2478 |
} |
|
2479 |
||
2480 |
||
2481 |
/** |
|
2482 |
* Returns the protocols that are supported by this implementation. |
|
2483 |
* A subset of the supported protocols may be enabled for this connection |
|
7043 | 2484 |
* @return an array of protocol names. |
2 | 2485 |
*/ |
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
2486 |
@Override |
2 | 2487 |
public String[] getSupportedProtocols() { |
9246
c459f79af46b
6976117: SSLContext.getInstance("TLSv1.1") returns SSLEngines/SSLSockets without TLSv1.1 enabled
xuelei
parents:
7043
diff
changeset
|
2488 |
return sslContext.getSuportedProtocolList().toStringArray(); |
2 | 2489 |
} |
2490 |
||
2491 |
/** |
|
2492 |
* Controls which protocols are enabled for use on |
|
2493 |
* this connection. The protocols must have been listed by |
|
2494 |
* getSupportedProtocols() as being supported. |
|
2495 |
* |
|
2496 |
* @param protocols protocols to enable. |
|
2497 |
* @exception IllegalArgumentException when one of the protocols |
|
2498 |
* named by the parameter is not supported. |
|
2499 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
2500 |
@Override |
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
31918
diff
changeset
|
2501 |
public synchronized void setEnabledProtocols(String[] protocols) { |
2 | 2502 |
enabledProtocols = new ProtocolList(protocols); |
7039 | 2503 |
if ((handshaker != null) && !handshaker.activated()) { |
2 | 2504 |
handshaker.setEnabledProtocols(enabledProtocols); |
2505 |
} |
|
2506 |
} |
|
2507 |
||
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
2508 |
@Override |
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
31918
diff
changeset
|
2509 |
public synchronized String[] getEnabledProtocols() { |
2 | 2510 |
return enabledProtocols.toStringArray(); |
2511 |
} |
|
2512 |
||
2513 |
/** |
|
2514 |
* Assigns the socket timeout. |
|
2515 |
* @see java.net.Socket#setSoTimeout |
|
2516 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
2517 |
@Override |
2 | 2518 |
public void setSoTimeout(int timeout) throws SocketException { |
2519 |
if ((debug != null) && Debug.isOn("ssl")) { |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
2520 |
System.out.println(Thread.currentThread().getName() + |
2 | 2521 |
", setSoTimeout(" + timeout + ") called"); |
2522 |
} |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
2523 |
|
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
2524 |
super.setSoTimeout(timeout); |
2 | 2525 |
} |
2526 |
||
2527 |
/** |
|
2528 |
* Registers an event listener to receive notifications that an |
|
2529 |
* SSL handshake has completed on this connection. |
|
2530 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
2531 |
@Override |
2 | 2532 |
public synchronized void addHandshakeCompletedListener( |
2533 |
HandshakeCompletedListener listener) { |
|
2534 |
if (listener == null) { |
|
2535 |
throw new IllegalArgumentException("listener is null"); |
|
2536 |
} |
|
2537 |
if (handshakeListeners == null) { |
|
2538 |
handshakeListeners = new |
|
2539 |
HashMap<HandshakeCompletedListener, AccessControlContext>(4); |
|
2540 |
} |
|
2541 |
handshakeListeners.put(listener, AccessController.getContext()); |
|
2542 |
} |
|
2543 |
||
2544 |
||
2545 |
/** |
|
2546 |
* Removes a previously registered handshake completion listener. |
|
2547 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
2548 |
@Override |
2 | 2549 |
public synchronized void removeHandshakeCompletedListener( |
2550 |
HandshakeCompletedListener listener) { |
|
2551 |
if (handshakeListeners == null) { |
|
2552 |
throw new IllegalArgumentException("no listeners"); |
|
2553 |
} |
|
2554 |
if (handshakeListeners.remove(listener) == null) { |
|
2555 |
throw new IllegalArgumentException("listener not registered"); |
|
2556 |
} |
|
2557 |
if (handshakeListeners.isEmpty()) { |
|
2558 |
handshakeListeners = null; |
|
2559 |
} |
|
2560 |
} |
|
2561 |
||
2562 |
/** |
|
7043 | 2563 |
* Returns the SSLParameters in effect for this SSLSocket. |
2 | 2564 |
*/ |
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
2565 |
@Override |
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
31918
diff
changeset
|
2566 |
public synchronized SSLParameters getSSLParameters() { |
7043 | 2567 |
SSLParameters params = super.getSSLParameters(); |
2568 |
||
2569 |
// the super implementation does not handle the following parameters |
|
2570 |
params.setEndpointIdentificationAlgorithm(identificationProtocol); |
|
2571 |
params.setAlgorithmConstraints(algorithmConstraints); |
|
37601
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
2572 |
|
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
2573 |
if (sniMatchers.isEmpty() && !noSniMatcher) { |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
2574 |
// 'null' indicates none has been set |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
2575 |
params.setSNIMatchers(null); |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
2576 |
} else { |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
2577 |
params.setSNIMatchers(sniMatchers); |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
2578 |
} |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
2579 |
|
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
2580 |
if (serverNames.isEmpty() && !noSniExtension) { |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
2581 |
// 'null' indicates none has been set |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
2582 |
params.setServerNames(null); |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
2583 |
} else { |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
2584 |
params.setServerNames(serverNames); |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
2585 |
} |
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
2586 |
|
19823
f0fd09e20528
7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents:
19223
diff
changeset
|
2587 |
params.setUseCipherSuitesOrder(preferLocalCipherSuites); |
30904 | 2588 |
params.setMaximumPacketSize(maximumPacketSize); |
34380
2b2609379881
8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
32834
diff
changeset
|
2589 |
params.setApplicationProtocols(applicationProtocols); |
30904 | 2590 |
|
2591 |
// DTLS handshake retransmissions parameter does not apply here. |
|
7043 | 2592 |
|
2593 |
return params; |
|
2 | 2594 |
} |
2595 |
||
2596 |
/** |
|
7043 | 2597 |
* Applies SSLParameters to this socket. |
2 | 2598 |
*/ |
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
2599 |
@Override |
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
31918
diff
changeset
|
2600 |
public synchronized void setSSLParameters(SSLParameters params) { |
7043 | 2601 |
super.setSSLParameters(params); |
2602 |
||
2603 |
// the super implementation does not handle the following parameters |
|
2604 |
identificationProtocol = params.getEndpointIdentificationAlgorithm(); |
|
2605 |
algorithmConstraints = params.getAlgorithmConstraints(); |
|
19823
f0fd09e20528
7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents:
19223
diff
changeset
|
2606 |
preferLocalCipherSuites = params.getUseCipherSuitesOrder(); |
30904 | 2607 |
maximumPacketSize = params.getMaximumPacketSize(); |
2608 |
||
2609 |
// DTLS handshake retransmissions parameter does not apply here. |
|
2610 |
||
2611 |
if (maximumPacketSize != 0) { |
|
2612 |
outputRecord.changePacketSize(maximumPacketSize); |
|
2613 |
} else { |
|
2614 |
// use the implicit maximum packet size. |
|
2615 |
maximumPacketSize = outputRecord.getMaxPacketSize(); |
|
2616 |
} |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
2617 |
|
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
2618 |
List<SNIServerName> sniNames = params.getServerNames(); |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
2619 |
if (sniNames != null) { |
37601
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
2620 |
noSniExtension = sniNames.isEmpty(); |
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
2621 |
serverNames = sniNames; |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
2622 |
} |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
2623 |
|
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
2624 |
Collection<SNIMatcher> matchers = params.getSNIMatchers(); |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
2625 |
if (matchers != null) { |
37601
6517589dcc94
8144566: Custom HostnameVerifier disables SNI extension
xuelei
parents:
36641
diff
changeset
|
2626 |
noSniMatcher = matchers.isEmpty(); |
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
2627 |
sniMatchers = matchers; |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
2628 |
} |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
2629 |
|
34380
2b2609379881
8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
32834
diff
changeset
|
2630 |
applicationProtocols = params.getApplicationProtocols(); |
2b2609379881
8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
32834
diff
changeset
|
2631 |
|
7043 | 2632 |
if ((handshaker != null) && !handshaker.started()) { |
2633 |
handshaker.setIdentificationProtocol(identificationProtocol); |
|
2634 |
handshaker.setAlgorithmConstraints(algorithmConstraints); |
|
30904 | 2635 |
handshaker.setMaximumPacketSize(maximumPacketSize); |
34380
2b2609379881
8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
32834
diff
changeset
|
2636 |
handshaker.setApplicationProtocols(applicationProtocols); |
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
2637 |
if (roleIsServer) { |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
2638 |
handshaker.setSNIMatchers(sniMatchers); |
19823
f0fd09e20528
7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents:
19223
diff
changeset
|
2639 |
handshaker.setUseCipherSuitesOrder(preferLocalCipherSuites); |
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
2640 |
} else { |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
2641 |
handshaker.setSNIServerNames(serverNames); |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
2642 |
} |
7043 | 2643 |
} |
2 | 2644 |
} |
2645 |
||
34380
2b2609379881
8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
32834
diff
changeset
|
2646 |
@Override |
2b2609379881
8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
32834
diff
changeset
|
2647 |
public synchronized String getApplicationProtocol() { |
2b2609379881
8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
32834
diff
changeset
|
2648 |
return applicationProtocol; |
2b2609379881
8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
32834
diff
changeset
|
2649 |
} |
2b2609379881
8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
32834
diff
changeset
|
2650 |
|
2b2609379881
8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
32834
diff
changeset
|
2651 |
@Override |
2b2609379881
8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
32834
diff
changeset
|
2652 |
public synchronized String getHandshakeApplicationProtocol() { |
34958
82de431119cd
8145849: ALPN: getHandshakeApplicationProtocol() always return null
wetmore
parents:
34716
diff
changeset
|
2653 |
if ((handshaker != null) && handshaker.started()) { |
34380
2b2609379881
8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
32834
diff
changeset
|
2654 |
return handshaker.getHandshakeApplicationProtocol(); |
2b2609379881
8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
32834
diff
changeset
|
2655 |
} |
2b2609379881
8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
32834
diff
changeset
|
2656 |
return null; |
2b2609379881
8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
32834
diff
changeset
|
2657 |
} |
2b2609379881
8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
32834
diff
changeset
|
2658 |
|
2 | 2659 |
// |
2660 |
// We allocate a separate thread to deliver handshake completion |
|
2661 |
// events. This ensures that the notifications don't block the |
|
2662 |
// protocol state machine. |
|
2663 |
// |
|
29923
e3ee0996bedb
8042332: Enhance thread contexts in security libraries
valeriep
parents:
28550
diff
changeset
|
2664 |
private static class NotifyHandshake implements Runnable { |
2 | 2665 |
|
2666 |
private Set<Map.Entry<HandshakeCompletedListener,AccessControlContext>> |
|
2667 |
targets; // who gets notified |
|
2668 |
private HandshakeCompletedEvent event; // the notification |
|
2669 |
||
29923
e3ee0996bedb
8042332: Enhance thread contexts in security libraries
valeriep
parents:
28550
diff
changeset
|
2670 |
NotifyHandshake( |
2 | 2671 |
Set<Map.Entry<HandshakeCompletedListener,AccessControlContext>> |
2672 |
entrySet, HandshakeCompletedEvent e) { |
|
2673 |
||
10128
f505a8514bd7
7065972: Some race condition may happen in SSLSocketImpl class
xuelei
parents:
9514
diff
changeset
|
2674 |
targets = new HashSet<>(entrySet); // clone the entry set |
2 | 2675 |
event = e; |
2676 |
} |
|
2677 |
||
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
2678 |
@Override |
2 | 2679 |
public void run() { |
10128
f505a8514bd7
7065972: Some race condition may happen in SSLSocketImpl class
xuelei
parents:
9514
diff
changeset
|
2680 |
// Don't need to synchronize, as it only runs in one thread. |
2 | 2681 |
for (Map.Entry<HandshakeCompletedListener,AccessControlContext> |
2682 |
entry : targets) { |
|
2683 |
||
2684 |
final HandshakeCompletedListener l = entry.getKey(); |
|
2685 |
AccessControlContext acc = entry.getValue(); |
|
2686 |
AccessController.doPrivileged(new PrivilegedAction<Void>() { |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
2687 |
@Override |
2 | 2688 |
public Void run() { |
2689 |
l.handshakeCompleted(event); |
|
2690 |
return null; |
|
2691 |
} |
|
2692 |
}, acc); |
|
2693 |
} |
|
2694 |
} |
|
2695 |
} |
|
2696 |
||
2697 |
/** |
|
2698 |
* Returns a printable representation of this end of the connection. |
|
2699 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
2700 |
@Override |
2 | 2701 |
public String toString() { |
24969
afa6934dd8e8
8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents:
21278
diff
changeset
|
2702 |
StringBuilder retval = new StringBuilder(80); |
2 | 2703 |
|
2704 |
retval.append(Integer.toHexString(hashCode())); |
|
2705 |
retval.append("["); |
|
2706 |
retval.append(sess.getCipherSuite()); |
|
2707 |
retval.append(": "); |
|
2708 |
||
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
2709 |
retval.append(super.toString()); |
2 | 2710 |
retval.append("]"); |
2711 |
||
2712 |
return retval.toString(); |
|
2713 |
} |
|
2714 |
} |