author | chegar |
Sun, 17 Aug 2014 15:54:13 +0100 | |
changeset 25859 | 3317bb8137f4 |
parent 21278 | jdk/src/share/classes/sun/security/ssl/SSLEngineImpl.java@ef8a3a2a72f2 |
child 28059 | e576535359cc |
permissions | -rw-r--r-- |
2 | 1 |
/* |
16113 | 2 |
* Copyright (c) 2003, 2013, 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 |
package sun.security.ssl; |
|
27 |
||
28 |
import java.io.*; |
|
29 |
import java.nio.*; |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
30 |
import java.util.*; |
2 | 31 |
import java.security.*; |
32 |
||
33 |
import javax.crypto.BadPaddingException; |
|
34 |
||
35 |
import javax.net.ssl.*; |
|
36 |
import javax.net.ssl.SSLEngineResult.*; |
|
37 |
||
38 |
/** |
|
39 |
* Implementation of an non-blocking SSLEngine. |
|
40 |
* |
|
41 |
* *Currently*, the SSLEngine code exists in parallel with the current |
|
42 |
* SSLSocket. As such, the current implementation is using legacy code |
|
43 |
* with many of the same abstractions. However, it varies in many |
|
44 |
* areas, most dramatically in the IO handling. |
|
45 |
* |
|
46 |
* There are three main I/O threads that can be existing in parallel: |
|
47 |
* wrap(), unwrap(), and beginHandshake(). We are encouraging users to |
|
48 |
* not call multiple instances of wrap or unwrap, because the data could |
|
49 |
* appear to flow out of the SSLEngine in a non-sequential order. We |
|
50 |
* take all steps we can to at least make sure the ordering remains |
|
51 |
* consistent, but once the calls returns, anything can happen. For |
|
52 |
* example, thread1 and thread2 both call wrap, thread1 gets the first |
|
53 |
* packet, thread2 gets the second packet, but thread2 gets control back |
|
54 |
* before thread1, and sends the data. The receiving side would see an |
|
55 |
* out-of-order error. |
|
56 |
* |
|
57 |
* Handshaking is still done the same way as SSLSocket using the normal |
|
58 |
* InputStream/OutputStream abstactions. We create |
|
59 |
* ClientHandshakers/ServerHandshakers, which produce/consume the |
|
60 |
* handshaking data. The transfer of the data is largely handled by the |
|
61 |
* HandshakeInStream/HandshakeOutStreams. Lastly, the |
|
62 |
* InputRecord/OutputRecords still have the same functionality, except |
|
63 |
* that they are overridden with EngineInputRecord/EngineOutputRecord, |
|
64 |
* which provide SSLEngine-specific functionality. |
|
65 |
* |
|
66 |
* Some of the major differences are: |
|
67 |
* |
|
68 |
* EngineInputRecord/EngineOutputRecord/EngineWriter: |
|
69 |
* |
|
70 |
* In order to avoid writing whole new control flows for |
|
71 |
* handshaking, and to reuse most of the same code, we kept most |
|
72 |
* of the actual handshake code the same. As usual, reading |
|
73 |
* handshake data may trigger output of more handshake data, so |
|
74 |
* what we do is write this data to internal buffers, and wait for |
|
75 |
* wrap() to be called to give that data a ride. |
|
76 |
* |
|
77 |
* All data is routed through |
|
78 |
* EngineInputRecord/EngineOutputRecord. However, all handshake |
|
79 |
* data (ct_alert/ct_change_cipher_spec/ct_handshake) are passed |
|
80 |
* through to the the underlying InputRecord/OutputRecord, and |
|
81 |
* the data uses the internal buffers. |
|
82 |
* |
|
83 |
* Application data is handled slightly different, we copy the data |
|
84 |
* directly from the src to the dst buffers, and do all operations |
|
85 |
* on those buffers, saving the overhead of multiple copies. |
|
86 |
* |
|
87 |
* In the case of an inbound record, unwrap passes the inbound |
|
88 |
* ByteBuffer to the InputRecord. If the data is handshake data, |
|
89 |
* the data is read into the InputRecord's internal buffer. If |
|
90 |
* the data is application data, the data is decoded directly into |
|
91 |
* the dst buffer. |
|
92 |
* |
|
93 |
* In the case of an outbound record, when the write to the |
|
94 |
* "real" OutputStream's would normally take place, instead we |
|
95 |
* call back up to the EngineOutputRecord's version of |
|
96 |
* writeBuffer, at which time we capture the resulting output in a |
|
97 |
* ByteBuffer, and send that back to the EngineWriter for internal |
|
98 |
* storage. |
|
99 |
* |
|
100 |
* EngineWriter is responsible for "handling" all outbound |
|
101 |
* data, be it handshake or app data, and for returning the data |
|
102 |
* to wrap() in the proper order. |
|
103 |
* |
|
104 |
* ClientHandshaker/ServerHandshaker/Handshaker: |
|
105 |
* Methods which relied on SSLSocket now have work on either |
|
106 |
* SSLSockets or SSLEngines. |
|
107 |
* |
|
108 |
* @author Brad Wetmore |
|
109 |
*/ |
|
110 |
final public class SSLEngineImpl extends SSLEngine { |
|
111 |
||
112 |
// |
|
113 |
// Fields and global comments |
|
114 |
// |
|
115 |
||
116 |
/* |
|
117 |
* There's a state machine associated with each connection, which |
|
118 |
* among other roles serves to negotiate session changes. |
|
119 |
* |
|
120 |
* - START with constructor, until the TCP connection's around. |
|
121 |
* - HANDSHAKE picks session parameters before allowing traffic. |
|
122 |
* There are many substates due to sequencing requirements |
|
123 |
* for handshake messages. |
|
124 |
* - DATA may be transmitted. |
|
125 |
* - RENEGOTIATE state allows concurrent data and handshaking |
|
126 |
* traffic ("same" substates as HANDSHAKE), and terminates |
|
127 |
* in selection of new session (and connection) parameters |
|
128 |
* - ERROR state immediately precedes abortive disconnect. |
|
129 |
* - CLOSED when one side closes down, used to start the shutdown |
|
130 |
* process. SSL connection objects are not reused. |
|
131 |
* |
|
132 |
* State affects what SSL record types may legally be sent: |
|
133 |
* |
|
134 |
* - Handshake ... only in HANDSHAKE and RENEGOTIATE states |
|
135 |
* - App Data ... only in DATA and RENEGOTIATE states |
|
136 |
* - Alert ... in HANDSHAKE, DATA, RENEGOTIATE |
|
137 |
* |
|
138 |
* Re what may be received: same as what may be sent, except that |
|
139 |
* HandshakeRequest handshaking messages can come from servers even |
|
140 |
* in the application data state, to request entry to RENEGOTIATE. |
|
141 |
* |
|
142 |
* The state machine within HANDSHAKE and RENEGOTIATE states controls |
|
143 |
* the pending session, not the connection state, until the change |
|
144 |
* cipher spec and "Finished" handshake messages are processed and |
|
145 |
* make the "new" session become the current one. |
|
146 |
* |
|
147 |
* NOTE: details of the SMs always need to be nailed down better. |
|
148 |
* The text above illustrates the core ideas. |
|
149 |
* |
|
150 |
* +---->-------+------>--------->-------+ |
|
151 |
* | | | |
|
152 |
* <-----< ^ ^ <-----< | |
|
153 |
*START>----->HANDSHAKE>----->DATA>----->RENEGOTIATE | |
|
154 |
* v v v | |
|
155 |
* | | | | |
|
156 |
* +------------+---------------+ | |
|
157 |
* | | |
|
158 |
* v | |
|
159 |
* ERROR>------>----->CLOSED<--------<----+ |
|
160 |
* |
|
161 |
* ALSO, note that the the purpose of handshaking (renegotiation is |
|
162 |
* included) is to assign a different, and perhaps new, session to |
|
163 |
* the connection. The SSLv3 spec is a bit confusing on that new |
|
164 |
* protocol feature. |
|
165 |
*/ |
|
166 |
private int connectionState; |
|
167 |
||
168 |
private static final int cs_START = 0; |
|
169 |
private static final int cs_HANDSHAKE = 1; |
|
170 |
private static final int cs_DATA = 2; |
|
171 |
private static final int cs_RENEGOTIATE = 3; |
|
172 |
private static final int cs_ERROR = 4; |
|
173 |
private static final int cs_CLOSED = 6; |
|
174 |
||
175 |
/* |
|
176 |
* Once we're in state cs_CLOSED, we can continue to |
|
177 |
* wrap/unwrap until we finish sending/receiving the messages |
|
178 |
* for close_notify. EngineWriter handles outboundDone. |
|
179 |
*/ |
|
180 |
private boolean inboundDone = false; |
|
181 |
||
182 |
EngineWriter writer; |
|
183 |
||
184 |
/* |
|
185 |
* The authentication context holds all information used to establish |
|
186 |
* who this end of the connection is (certificate chains, private keys, |
|
187 |
* etc) and who is trusted (e.g. as CAs or websites). |
|
188 |
*/ |
|
189 |
private SSLContextImpl sslContext; |
|
190 |
||
191 |
/* |
|
192 |
* This connection is one of (potentially) many associated with |
|
193 |
* any given session. The output of the handshake protocol is a |
|
194 |
* new session ... although all the protocol description talks |
|
195 |
* about changing the cipher spec (and it does change), in fact |
|
196 |
* that's incidental since it's done by changing everything that |
|
197 |
* is associated with a session at the same time. (TLS/IETF may |
|
198 |
* change that to add client authentication w/o new key exchg.) |
|
199 |
*/ |
|
7043 | 200 |
private Handshaker handshaker; |
201 |
private SSLSessionImpl sess; |
|
202 |
private volatile SSLSessionImpl handshakeSession; |
|
203 |
||
2 | 204 |
|
205 |
/* |
|
206 |
* Client authentication be off, requested, or required. |
|
207 |
* |
|
208 |
* This will be used by both this class and SSLSocket's variants. |
|
209 |
*/ |
|
210 |
static final byte clauth_none = 0; |
|
211 |
static final byte clauth_requested = 1; |
|
212 |
static final byte clauth_required = 2; |
|
213 |
||
214 |
/* |
|
215 |
* Flag indicating if the next record we receive MUST be a Finished |
|
216 |
* message. Temporarily set during the handshake to ensure that |
|
217 |
* a change cipher spec message is followed by a finished message. |
|
218 |
*/ |
|
219 |
private boolean expectingFinished; |
|
220 |
||
221 |
||
222 |
/* |
|
223 |
* If someone tries to closeInbound() (say at End-Of-Stream) |
|
224 |
* our engine having received a close_notify, we need to |
|
225 |
* notify the app that we may have a truncation attack underway. |
|
226 |
*/ |
|
227 |
private boolean recvCN; |
|
228 |
||
229 |
/* |
|
230 |
* For improved diagnostics, we detail connection closure |
|
231 |
* If the engine is closed (connectionState >= cs_ERROR), |
|
232 |
* closeReason != null indicates if the engine was closed |
|
233 |
* because of an error or because or normal shutdown. |
|
234 |
*/ |
|
235 |
private SSLException closeReason; |
|
236 |
||
237 |
/* |
|
238 |
* Per-connection private state that doesn't change when the |
|
239 |
* session is changed. |
|
240 |
*/ |
|
241 |
private byte doClientAuth; |
|
242 |
private boolean enableSessionCreation = true; |
|
243 |
EngineInputRecord inputRecord; |
|
244 |
EngineOutputRecord outputRecord; |
|
245 |
private AccessControlContext acc; |
|
246 |
||
7039 | 247 |
// The cipher suites enabled for use on this connection. |
248 |
private CipherSuiteList enabledCipherSuites; |
|
249 |
||
7043 | 250 |
// the endpoint identification protocol |
251 |
private String identificationProtocol = null; |
|
252 |
||
253 |
// The cryptographic algorithm constraints |
|
254 |
private AlgorithmConstraints algorithmConstraints = null; |
|
2 | 255 |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
256 |
// The server name indication and matchers |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
257 |
List<SNIServerName> serverNames = |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
258 |
Collections.<SNIServerName>emptyList(); |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
259 |
Collection<SNIMatcher> sniMatchers = |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
260 |
Collections.<SNIMatcher>emptyList(); |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
261 |
|
2 | 262 |
// Have we been told whether we're client or server? |
263 |
private boolean serverModeSet = false; |
|
264 |
private boolean roleIsServer; |
|
265 |
||
266 |
/* |
|
7039 | 267 |
* The protocol versions enabled for use on this connection. |
268 |
* |
|
269 |
* Note: we support a pseudo protocol called SSLv2Hello which when |
|
270 |
* set will result in an SSL v2 Hello being sent with SSL (version 3.0) |
|
271 |
* or TLS (version 3.1, 3.2, etc.) version info. |
|
2 | 272 |
*/ |
273 |
private ProtocolList enabledProtocols; |
|
274 |
||
275 |
/* |
|
276 |
* The SSL version associated with this connection. |
|
277 |
*/ |
|
278 |
private ProtocolVersion protocolVersion = ProtocolVersion.DEFAULT; |
|
279 |
||
280 |
/* |
|
281 |
* Crypto state that's reinitialized when the session changes. |
|
282 |
*/ |
|
16913 | 283 |
private Authenticator readAuthenticator, writeAuthenticator; |
2 | 284 |
private CipherBox readCipher, writeCipher; |
285 |
// NOTE: compression state would be saved here |
|
286 |
||
6856 | 287 |
/* |
288 |
* security parameters for secure renegotiation. |
|
289 |
*/ |
|
290 |
private boolean secureRenegotiation; |
|
291 |
private byte[] clientVerifyData; |
|
292 |
private byte[] serverVerifyData; |
|
2 | 293 |
|
294 |
/* |
|
295 |
* READ ME * READ ME * READ ME * READ ME * READ ME * READ ME * |
|
296 |
* IMPORTANT STUFF TO UNDERSTANDING THE SYNCHRONIZATION ISSUES. |
|
297 |
* READ ME * READ ME * READ ME * READ ME * READ ME * READ ME * |
|
298 |
* |
|
299 |
* There are several locks here. |
|
300 |
* |
|
301 |
* The primary lock is the per-instance lock used by |
|
302 |
* synchronized(this) and the synchronized methods. It controls all |
|
303 |
* access to things such as the connection state and variables which |
|
304 |
* affect handshaking. If we are inside a synchronized method, we |
|
305 |
* can access the state directly, otherwise, we must use the |
|
306 |
* synchronized equivalents. |
|
307 |
* |
|
308 |
* Note that we must never acquire the <code>this</code> lock after |
|
309 |
* <code>writeLock</code> or run the risk of deadlock. |
|
310 |
* |
|
311 |
* Grab some coffee, and be careful with any code changes. |
|
312 |
*/ |
|
313 |
private Object wrapLock; |
|
314 |
private Object unwrapLock; |
|
315 |
Object writeLock; |
|
316 |
||
317 |
/* |
|
10915 | 318 |
* Is it the first application record to write? |
319 |
*/ |
|
320 |
private boolean isFirstAppOutputRecord = true; |
|
321 |
||
322 |
/* |
|
19823
f0fd09e20528
7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents:
16913
diff
changeset
|
323 |
* Whether local cipher suites preference in server side should be |
f0fd09e20528
7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents:
16913
diff
changeset
|
324 |
* honored during handshaking? |
f0fd09e20528
7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents:
16913
diff
changeset
|
325 |
*/ |
f0fd09e20528
7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents:
16913
diff
changeset
|
326 |
private boolean preferLocalCipherSuites = false; |
f0fd09e20528
7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents:
16913
diff
changeset
|
327 |
|
f0fd09e20528
7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents:
16913
diff
changeset
|
328 |
/* |
2 | 329 |
* Class and subclass dynamic debugging support |
330 |
*/ |
|
331 |
private static final Debug debug = Debug.getInstance("ssl"); |
|
332 |
||
333 |
// |
|
334 |
// Initialization/Constructors |
|
335 |
// |
|
336 |
||
337 |
/** |
|
338 |
* Constructor for an SSLEngine from SSLContext, without |
|
339 |
* host/port hints. This Engine will not be able to cache |
|
340 |
* sessions, but must renegotiate everything by hand. |
|
341 |
*/ |
|
342 |
SSLEngineImpl(SSLContextImpl ctx) { |
|
343 |
super(); |
|
344 |
init(ctx); |
|
345 |
} |
|
346 |
||
347 |
/** |
|
348 |
* Constructor for an SSLEngine from SSLContext. |
|
349 |
*/ |
|
350 |
SSLEngineImpl(SSLContextImpl ctx, String host, int port) { |
|
351 |
super(host, port); |
|
352 |
init(ctx); |
|
353 |
} |
|
354 |
||
355 |
/** |
|
356 |
* Initializes the Engine |
|
357 |
*/ |
|
358 |
private void init(SSLContextImpl ctx) { |
|
359 |
if (debug != null && Debug.isOn("ssl")) { |
|
360 |
System.out.println("Using SSLEngineImpl."); |
|
361 |
} |
|
362 |
||
363 |
sslContext = ctx; |
|
364 |
sess = SSLSessionImpl.nullSession; |
|
7043 | 365 |
handshakeSession = null; |
2 | 366 |
|
367 |
/* |
|
368 |
* State is cs_START until we initialize the handshaker. |
|
369 |
* |
|
370 |
* Apps using SSLEngine are probably going to be server. |
|
371 |
* Somewhat arbitrary choice. |
|
372 |
*/ |
|
373 |
roleIsServer = true; |
|
374 |
connectionState = cs_START; |
|
375 |
||
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
376 |
// default server name indication |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
377 |
serverNames = |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
378 |
Utilities.addToSNIServerNameList(serverNames, getPeerHost()); |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
379 |
|
2 | 380 |
/* |
381 |
* default read and write side cipher and MAC support |
|
382 |
* |
|
383 |
* Note: compression support would go here too |
|
384 |
*/ |
|
385 |
readCipher = CipherBox.NULL; |
|
16913 | 386 |
readAuthenticator = MAC.NULL; |
2 | 387 |
writeCipher = CipherBox.NULL; |
16913 | 388 |
writeAuthenticator = MAC.NULL; |
2 | 389 |
|
6856 | 390 |
// default security parameters for secure renegotiation |
391 |
secureRenegotiation = false; |
|
392 |
clientVerifyData = new byte[0]; |
|
393 |
serverVerifyData = new byte[0]; |
|
394 |
||
9246
c459f79af46b
6976117: SSLContext.getInstance("TLSv1.1") returns SSLEngines/SSLSockets without TLSv1.1 enabled
xuelei
parents:
7043
diff
changeset
|
395 |
enabledCipherSuites = |
c459f79af46b
6976117: SSLContext.getInstance("TLSv1.1") returns SSLEngines/SSLSockets without TLSv1.1 enabled
xuelei
parents:
7043
diff
changeset
|
396 |
sslContext.getDefaultCipherSuiteList(roleIsServer); |
c459f79af46b
6976117: SSLContext.getInstance("TLSv1.1") returns SSLEngines/SSLSockets without TLSv1.1 enabled
xuelei
parents:
7043
diff
changeset
|
397 |
enabledProtocols = |
c459f79af46b
6976117: SSLContext.getInstance("TLSv1.1") returns SSLEngines/SSLSockets without TLSv1.1 enabled
xuelei
parents:
7043
diff
changeset
|
398 |
sslContext.getDefaultProtocolList(roleIsServer); |
2 | 399 |
|
400 |
wrapLock = new Object(); |
|
401 |
unwrapLock = new Object(); |
|
402 |
writeLock = new Object(); |
|
403 |
||
404 |
/* |
|
405 |
* Save the Access Control Context. This will be used later |
|
406 |
* for a couple of things, including providing a context to |
|
407 |
* run tasks in, and for determining which credentials |
|
408 |
* to use for Subject based (JAAS) decisions |
|
409 |
*/ |
|
410 |
acc = AccessController.getContext(); |
|
411 |
||
412 |
/* |
|
413 |
* All outbound application data goes through this OutputRecord, |
|
414 |
* other data goes through their respective records created |
|
415 |
* elsewhere. All inbound data goes through this one |
|
416 |
* input record. |
|
417 |
*/ |
|
418 |
outputRecord = |
|
419 |
new EngineOutputRecord(Record.ct_application_data, this); |
|
420 |
inputRecord = new EngineInputRecord(this); |
|
421 |
inputRecord.enableFormatChecks(); |
|
422 |
||
423 |
writer = new EngineWriter(); |
|
424 |
} |
|
425 |
||
426 |
/** |
|
427 |
* Initialize the handshaker object. This means: |
|
428 |
* |
|
429 |
* . if a handshake is already in progress (state is cs_HANDSHAKE |
|
430 |
* or cs_RENEGOTIATE), do nothing and return |
|
431 |
* |
|
432 |
* . if the engine is already closed, throw an Exception (internal error) |
|
433 |
* |
|
434 |
* . otherwise (cs_START or cs_DATA), create the appropriate handshaker |
|
7039 | 435 |
* object and advance the connection state (to cs_HANDSHAKE or |
436 |
* cs_RENEGOTIATE, respectively). |
|
2 | 437 |
* |
438 |
* This method is called right after a new engine is created, when |
|
439 |
* starting renegotiation, or when changing client/server mode of the |
|
440 |
* engine. |
|
441 |
*/ |
|
442 |
private void initHandshaker() { |
|
443 |
switch (connectionState) { |
|
444 |
||
445 |
// |
|
446 |
// Starting a new handshake. |
|
447 |
// |
|
448 |
case cs_START: |
|
449 |
case cs_DATA: |
|
450 |
break; |
|
451 |
||
452 |
// |
|
453 |
// We're already in the middle of a handshake. |
|
454 |
// |
|
455 |
case cs_HANDSHAKE: |
|
456 |
case cs_RENEGOTIATE: |
|
457 |
return; |
|
458 |
||
459 |
// |
|
460 |
// Anyone allowed to call this routine is required to |
|
461 |
// do so ONLY if the connection state is reasonable... |
|
462 |
// |
|
463 |
default: |
|
464 |
throw new IllegalStateException("Internal error"); |
|
465 |
} |
|
466 |
||
467 |
// state is either cs_START or cs_DATA |
|
468 |
if (connectionState == cs_START) { |
|
469 |
connectionState = cs_HANDSHAKE; |
|
470 |
} else { // cs_DATA |
|
471 |
connectionState = cs_RENEGOTIATE; |
|
472 |
} |
|
473 |
if (roleIsServer) { |
|
5182 | 474 |
handshaker = new ServerHandshaker(this, sslContext, |
6856 | 475 |
enabledProtocols, doClientAuth, |
476 |
protocolVersion, connectionState == cs_HANDSHAKE, |
|
477 |
secureRenegotiation, clientVerifyData, serverVerifyData); |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
478 |
handshaker.setSNIMatchers(sniMatchers); |
19823
f0fd09e20528
7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents:
16913
diff
changeset
|
479 |
handshaker.setUseCipherSuitesOrder(preferLocalCipherSuites); |
2 | 480 |
} else { |
5182 | 481 |
handshaker = new ClientHandshaker(this, sslContext, |
6856 | 482 |
enabledProtocols, |
483 |
protocolVersion, connectionState == cs_HANDSHAKE, |
|
484 |
secureRenegotiation, clientVerifyData, serverVerifyData); |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
485 |
handshaker.setSNIServerNames(serverNames); |
2 | 486 |
} |
7039 | 487 |
handshaker.setEnabledCipherSuites(enabledCipherSuites); |
2 | 488 |
handshaker.setEnableSessionCreation(enableSessionCreation); |
489 |
} |
|
490 |
||
491 |
/* |
|
492 |
* Report the current status of the Handshaker |
|
493 |
*/ |
|
494 |
private HandshakeStatus getHSStatus(HandshakeStatus hss) { |
|
495 |
||
496 |
if (hss != null) { |
|
497 |
return hss; |
|
498 |
} |
|
499 |
||
500 |
synchronized (this) { |
|
501 |
if (writer.hasOutboundData()) { |
|
502 |
return HandshakeStatus.NEED_WRAP; |
|
503 |
} else if (handshaker != null) { |
|
504 |
if (handshaker.taskOutstanding()) { |
|
505 |
return HandshakeStatus.NEED_TASK; |
|
506 |
} else { |
|
507 |
return HandshakeStatus.NEED_UNWRAP; |
|
508 |
} |
|
509 |
} else if (connectionState == cs_CLOSED) { |
|
510 |
/* |
|
511 |
* Special case where we're closing, but |
|
512 |
* still need the close_notify before we |
|
513 |
* can officially be closed. |
|
514 |
* |
|
515 |
* Note isOutboundDone is taken care of by |
|
516 |
* hasOutboundData() above. |
|
517 |
*/ |
|
518 |
if (!isInboundDone()) { |
|
519 |
return HandshakeStatus.NEED_UNWRAP; |
|
520 |
} // else not handshaking |
|
521 |
} |
|
522 |
||
523 |
return HandshakeStatus.NOT_HANDSHAKING; |
|
524 |
} |
|
525 |
} |
|
526 |
||
527 |
synchronized private void checkTaskThrown() throws SSLException { |
|
528 |
if (handshaker != null) { |
|
529 |
handshaker.checkThrown(); |
|
530 |
} |
|
531 |
} |
|
532 |
||
533 |
// |
|
534 |
// Handshaking and connection state code |
|
535 |
// |
|
536 |
||
537 |
/* |
|
538 |
* Provides "this" synchronization for connection state. |
|
539 |
* Otherwise, you can access it directly. |
|
540 |
*/ |
|
541 |
synchronized private int getConnectionState() { |
|
542 |
return connectionState; |
|
543 |
} |
|
544 |
||
545 |
synchronized private void setConnectionState(int state) { |
|
546 |
connectionState = state; |
|
547 |
} |
|
548 |
||
549 |
/* |
|
550 |
* Get the Access Control Context. |
|
551 |
* |
|
552 |
* Used for a known context to |
|
553 |
* run tasks in, and for determining which credentials |
|
554 |
* to use for Subject-based (JAAS) decisions. |
|
555 |
*/ |
|
556 |
AccessControlContext getAcc() { |
|
557 |
return acc; |
|
558 |
} |
|
559 |
||
560 |
/* |
|
561 |
* Is a handshake currently underway? |
|
562 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
563 |
@Override |
2 | 564 |
public SSLEngineResult.HandshakeStatus getHandshakeStatus() { |
565 |
return getHSStatus(null); |
|
566 |
} |
|
567 |
||
568 |
/* |
|
569 |
* When a connection finishes handshaking by enabling use of a newly |
|
570 |
* negotiated session, each end learns about it in two halves (read, |
|
571 |
* and write). When both read and write ciphers have changed, and the |
|
572 |
* last handshake message has been read, the connection has joined |
|
573 |
* (rejoined) the new session. |
|
574 |
* |
|
575 |
* NOTE: The SSLv3 spec is rather unclear on the concepts here. |
|
576 |
* Sessions don't change once they're established (including cipher |
|
577 |
* suite and master secret) but connections can join them (and leave |
|
578 |
* them). They're created by handshaking, though sometime handshaking |
|
579 |
* causes connections to join up with pre-established sessions. |
|
580 |
* |
|
581 |
* Synchronized on "this" from readRecord. |
|
582 |
*/ |
|
583 |
private void changeReadCiphers() throws SSLException { |
|
584 |
if (connectionState != cs_HANDSHAKE |
|
585 |
&& connectionState != cs_RENEGOTIATE) { |
|
586 |
throw new SSLProtocolException( |
|
587 |
"State error, change cipher specs"); |
|
588 |
} |
|
589 |
||
590 |
// ... create decompressor |
|
591 |
||
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:
2
diff
changeset
|
592 |
CipherBox oldCipher = readCipher; |
0a6b65d56746
6750401: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes,with PCKS11 provider
wetmore
parents:
2
diff
changeset
|
593 |
|
2 | 594 |
try { |
595 |
readCipher = handshaker.newReadCipher(); |
|
16913 | 596 |
readAuthenticator = handshaker.newReadAuthenticator(); |
2 | 597 |
} catch (GeneralSecurityException e) { |
598 |
// "can't happen" |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9246
diff
changeset
|
599 |
throw new SSLException("Algorithm missing: ", e); |
2 | 600 |
} |
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:
2
diff
changeset
|
601 |
|
0a6b65d56746
6750401: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes,with PCKS11 provider
wetmore
parents:
2
diff
changeset
|
602 |
/* |
0a6b65d56746
6750401: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes,with PCKS11 provider
wetmore
parents:
2
diff
changeset
|
603 |
* Dispose of any intermediate state in the underlying cipher. |
0a6b65d56746
6750401: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes,with PCKS11 provider
wetmore
parents:
2
diff
changeset
|
604 |
* For PKCS11 ciphers, this will release any attached sessions, |
0a6b65d56746
6750401: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes,with PCKS11 provider
wetmore
parents:
2
diff
changeset
|
605 |
* and thus make finalization faster. |
0a6b65d56746
6750401: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes,with PCKS11 provider
wetmore
parents:
2
diff
changeset
|
606 |
* |
0a6b65d56746
6750401: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes,with PCKS11 provider
wetmore
parents:
2
diff
changeset
|
607 |
* Since MAC's doFinal() is called for every SSL/TLS packet, it's |
0a6b65d56746
6750401: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes,with PCKS11 provider
wetmore
parents:
2
diff
changeset
|
608 |
* not necessary to do the same with MAC's. |
0a6b65d56746
6750401: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes,with PCKS11 provider
wetmore
parents:
2
diff
changeset
|
609 |
*/ |
0a6b65d56746
6750401: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes,with PCKS11 provider
wetmore
parents:
2
diff
changeset
|
610 |
oldCipher.dispose(); |
2 | 611 |
} |
612 |
||
613 |
/* |
|
614 |
* used by Handshaker to change the active write cipher, follows |
|
615 |
* the output of the CCS message. |
|
616 |
* |
|
617 |
* Also synchronized on "this" from readRecord/delegatedTask. |
|
618 |
*/ |
|
619 |
void changeWriteCiphers() throws SSLException { |
|
620 |
if (connectionState != cs_HANDSHAKE |
|
621 |
&& connectionState != cs_RENEGOTIATE) { |
|
622 |
throw new SSLProtocolException( |
|
623 |
"State error, change cipher specs"); |
|
624 |
} |
|
625 |
||
626 |
// ... create compressor |
|
627 |
||
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:
2
diff
changeset
|
628 |
CipherBox oldCipher = writeCipher; |
0a6b65d56746
6750401: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes,with PCKS11 provider
wetmore
parents:
2
diff
changeset
|
629 |
|
2 | 630 |
try { |
631 |
writeCipher = handshaker.newWriteCipher(); |
|
16913 | 632 |
writeAuthenticator = handshaker.newWriteAuthenticator(); |
2 | 633 |
} catch (GeneralSecurityException e) { |
634 |
// "can't happen" |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9246
diff
changeset
|
635 |
throw new SSLException("Algorithm missing: ", e); |
2 | 636 |
} |
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:
2
diff
changeset
|
637 |
|
0a6b65d56746
6750401: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes,with PCKS11 provider
wetmore
parents:
2
diff
changeset
|
638 |
// See comment above. |
0a6b65d56746
6750401: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes,with PCKS11 provider
wetmore
parents:
2
diff
changeset
|
639 |
oldCipher.dispose(); |
10915 | 640 |
|
641 |
// reset the flag of the first application record |
|
642 |
isFirstAppOutputRecord = true; |
|
2 | 643 |
} |
644 |
||
645 |
/* |
|
646 |
* Updates the SSL version associated with this connection. |
|
647 |
* Called from Handshaker once it has determined the negotiated version. |
|
648 |
*/ |
|
649 |
synchronized void setVersion(ProtocolVersion protocolVersion) { |
|
650 |
this.protocolVersion = protocolVersion; |
|
651 |
outputRecord.setVersion(protocolVersion); |
|
652 |
} |
|
653 |
||
654 |
||
655 |
/** |
|
656 |
* Kickstart the handshake if it is not already in progress. |
|
657 |
* This means: |
|
658 |
* |
|
659 |
* . if handshaking is already underway, do nothing and return |
|
660 |
* |
|
661 |
* . if the engine is not connected or already closed, throw an |
|
662 |
* Exception. |
|
663 |
* |
|
664 |
* . otherwise, call initHandshake() to initialize the handshaker |
|
665 |
* object and progress the state. Then, send the initial |
|
666 |
* handshaking message if appropriate (always on clients and |
|
667 |
* on servers when renegotiating). |
|
668 |
*/ |
|
669 |
private synchronized void kickstartHandshake() throws IOException { |
|
670 |
switch (connectionState) { |
|
671 |
||
672 |
case cs_START: |
|
673 |
if (!serverModeSet) { |
|
674 |
throw new IllegalStateException( |
|
675 |
"Client/Server mode not yet set."); |
|
676 |
} |
|
677 |
initHandshaker(); |
|
678 |
break; |
|
679 |
||
680 |
case cs_HANDSHAKE: |
|
681 |
// handshaker already setup, proceed |
|
682 |
break; |
|
683 |
||
684 |
case cs_DATA: |
|
6856 | 685 |
if (!secureRenegotiation && !Handshaker.allowUnsafeRenegotiation) { |
686 |
throw new SSLHandshakeException( |
|
687 |
"Insecure renegotiation is not allowed"); |
|
688 |
} |
|
689 |
||
690 |
if (!secureRenegotiation) { |
|
691 |
if (debug != null && Debug.isOn("handshake")) { |
|
692 |
System.out.println( |
|
693 |
"Warning: Using insecure renegotiation"); |
|
694 |
} |
|
5182 | 695 |
} |
696 |
||
2 | 697 |
// initialize the handshaker, move to cs_RENEGOTIATE |
698 |
initHandshaker(); |
|
699 |
break; |
|
700 |
||
701 |
case cs_RENEGOTIATE: |
|
702 |
// handshaking already in progress, return |
|
703 |
return; |
|
704 |
||
705 |
default: |
|
706 |
// cs_ERROR/cs_CLOSED |
|
707 |
throw new SSLException("SSLEngine is closing/closed"); |
|
708 |
} |
|
709 |
||
710 |
// |
|
711 |
// Kickstart handshake state machine if we need to ... |
|
712 |
// |
|
713 |
// Note that handshaker.kickstart() writes the message |
|
714 |
// to its HandshakeOutStream, which calls back into |
|
715 |
// SSLSocketImpl.writeRecord() to send it. |
|
716 |
// |
|
7039 | 717 |
if (!handshaker.activated()) { |
718 |
// prior to handshaking, activate the handshake |
|
719 |
if (connectionState == cs_RENEGOTIATE) { |
|
720 |
// don't use SSLv2Hello when renegotiating |
|
721 |
handshaker.activate(protocolVersion); |
|
722 |
} else { |
|
723 |
handshaker.activate(null); |
|
724 |
} |
|
725 |
||
2 | 726 |
if (handshaker instanceof ClientHandshaker) { |
727 |
// send client hello |
|
728 |
handshaker.kickstart(); |
|
729 |
} else { // instanceof ServerHandshaker |
|
730 |
if (connectionState == cs_HANDSHAKE) { |
|
731 |
// initial handshake, no kickstart message to send |
|
732 |
} else { |
|
733 |
// we want to renegotiate, send hello request |
|
734 |
handshaker.kickstart(); |
|
7039 | 735 |
|
2 | 736 |
// hello request is not included in the handshake |
737 |
// hashes, reset them |
|
738 |
handshaker.handshakeHash.reset(); |
|
739 |
} |
|
740 |
} |
|
741 |
} |
|
742 |
} |
|
743 |
||
744 |
/* |
|
745 |
* Start a SSLEngine handshake |
|
746 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
747 |
@Override |
2 | 748 |
public void beginHandshake() throws SSLException { |
749 |
try { |
|
750 |
kickstartHandshake(); |
|
751 |
} catch (Exception e) { |
|
752 |
fatal(Alerts.alert_handshake_failure, |
|
753 |
"Couldn't kickstart handshaking", e); |
|
754 |
} |
|
755 |
} |
|
756 |
||
757 |
||
758 |
// |
|
759 |
// Read/unwrap side |
|
760 |
// |
|
761 |
||
762 |
||
763 |
/** |
|
764 |
* Unwraps a buffer. Does a variety of checks before grabbing |
|
21278 | 765 |
* the unwrapLock, which blocks multiple unwraps from occurring. |
2 | 766 |
*/ |
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
767 |
@Override |
2 | 768 |
public SSLEngineResult unwrap(ByteBuffer netData, ByteBuffer [] appData, |
769 |
int offset, int length) throws SSLException { |
|
770 |
||
771 |
EngineArgs ea = new EngineArgs(netData, appData, offset, length); |
|
772 |
||
773 |
try { |
|
774 |
synchronized (unwrapLock) { |
|
775 |
return readNetRecord(ea); |
|
776 |
} |
|
777 |
} catch (Exception e) { |
|
778 |
/* |
|
779 |
* Don't reset position so it looks like we didn't |
|
780 |
* consume anything. We did consume something, and it |
|
781 |
* got us into this situation, so report that much back. |
|
782 |
* Our days of consuming are now over anyway. |
|
783 |
*/ |
|
784 |
fatal(Alerts.alert_internal_error, |
|
785 |
"problem unwrapping net record", e); |
|
786 |
return null; // make compiler happy |
|
787 |
} finally { |
|
788 |
/* |
|
789 |
* Just in case something failed to reset limits properly. |
|
790 |
*/ |
|
791 |
ea.resetLim(); |
|
792 |
} |
|
793 |
} |
|
794 |
||
795 |
/* |
|
796 |
* Makes additional checks for unwrap, but this time more |
|
797 |
* specific to this packet and the current state of the machine. |
|
798 |
*/ |
|
799 |
private SSLEngineResult readNetRecord(EngineArgs ea) throws IOException { |
|
800 |
||
801 |
Status status = null; |
|
802 |
HandshakeStatus hsStatus = null; |
|
803 |
||
804 |
/* |
|
805 |
* See if the handshaker needs to report back some SSLException. |
|
806 |
*/ |
|
807 |
checkTaskThrown(); |
|
808 |
||
809 |
/* |
|
810 |
* Check if we are closing/closed. |
|
811 |
*/ |
|
812 |
if (isInboundDone()) { |
|
813 |
return new SSLEngineResult(Status.CLOSED, getHSStatus(null), 0, 0); |
|
814 |
} |
|
815 |
||
816 |
/* |
|
817 |
* If we're still in cs_HANDSHAKE, make sure it's been |
|
818 |
* started. |
|
819 |
*/ |
|
820 |
synchronized (this) { |
|
821 |
if ((connectionState == cs_HANDSHAKE) || |
|
822 |
(connectionState == cs_START)) { |
|
823 |
kickstartHandshake(); |
|
824 |
||
825 |
/* |
|
826 |
* If there's still outbound data to flush, we |
|
827 |
* can return without trying to unwrap anything. |
|
828 |
*/ |
|
829 |
hsStatus = getHSStatus(null); |
|
830 |
||
831 |
if (hsStatus == HandshakeStatus.NEED_WRAP) { |
|
832 |
return new SSLEngineResult(Status.OK, hsStatus, 0, 0); |
|
833 |
} |
|
834 |
} |
|
835 |
} |
|
836 |
||
837 |
/* |
|
838 |
* Grab a copy of this if it doesn't already exist, |
|
839 |
* and we can use it several places before anything major |
|
840 |
* happens on this side. Races aren't critical |
|
841 |
* here. |
|
842 |
*/ |
|
843 |
if (hsStatus == null) { |
|
844 |
hsStatus = getHSStatus(null); |
|
845 |
} |
|
846 |
||
847 |
/* |
|
848 |
* If we have a task outstanding, this *MUST* be done before |
|
849 |
* doing any more unwrapping, because we could be in the middle |
|
850 |
* of receiving a handshake message, for example, a finished |
|
851 |
* message which would change the ciphers. |
|
852 |
*/ |
|
853 |
if (hsStatus == HandshakeStatus.NEED_TASK) { |
|
854 |
return new SSLEngineResult( |
|
855 |
Status.OK, hsStatus, 0, 0); |
|
856 |
} |
|
857 |
||
858 |
/* |
|
859 |
* Check the packet to make sure enough is here. |
|
860 |
* This will also indirectly check for 0 len packets. |
|
861 |
*/ |
|
862 |
int packetLen = inputRecord.bytesInCompletePacket(ea.netData); |
|
863 |
||
864 |
// Is this packet bigger than SSL/TLS normally allows? |
|
865 |
if (packetLen > sess.getPacketBufferSize()) { |
|
866 |
if (packetLen > Record.maxLargeRecordSize) { |
|
867 |
throw new SSLProtocolException( |
|
868 |
"Input SSL/TLS record too big: max = " + |
|
869 |
Record.maxLargeRecordSize + |
|
870 |
" len = " + packetLen); |
|
871 |
} else { |
|
872 |
// Expand the expected maximum packet/application buffer |
|
873 |
// sizes. |
|
874 |
sess.expandBufferSizes(); |
|
875 |
} |
|
876 |
} |
|
877 |
||
878 |
/* |
|
879 |
* Check for OVERFLOW. |
|
880 |
* |
|
881 |
* To be considered: We could delay enforcing the application buffer |
|
882 |
* free space requirement until after the initial handshaking. |
|
883 |
*/ |
|
884 |
if ((packetLen - Record.headerSize) > ea.getAppRemaining()) { |
|
885 |
return new SSLEngineResult(Status.BUFFER_OVERFLOW, hsStatus, 0, 0); |
|
886 |
} |
|
887 |
||
888 |
// check for UNDERFLOW. |
|
889 |
if ((packetLen == -1) || (ea.netData.remaining() < packetLen)) { |
|
890 |
return new SSLEngineResult( |
|
891 |
Status.BUFFER_UNDERFLOW, hsStatus, 0, 0); |
|
892 |
} |
|
893 |
||
894 |
/* |
|
895 |
* We're now ready to actually do the read. |
|
896 |
* The only result code we really need to be exactly |
|
897 |
* right is the HS finished, for signaling to |
|
898 |
* HandshakeCompletedListeners. |
|
899 |
*/ |
|
900 |
try { |
|
901 |
hsStatus = readRecord(ea); |
|
902 |
} catch (SSLException e) { |
|
903 |
throw e; |
|
904 |
} catch (IOException e) { |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9246
diff
changeset
|
905 |
throw new SSLException("readRecord", e); |
2 | 906 |
} |
907 |
||
908 |
/* |
|
909 |
* Check the various condition that we could be reporting. |
|
910 |
* |
|
911 |
* It's *possible* something might have happened between the |
|
912 |
* above and now, but it was better to minimally lock "this" |
|
913 |
* during the read process. We'll return the current |
|
914 |
* status, which is more representative of the current state. |
|
915 |
* |
|
916 |
* status above should cover: FINISHED, NEED_TASK |
|
917 |
*/ |
|
918 |
status = (isInboundDone() ? Status.CLOSED : Status.OK); |
|
919 |
hsStatus = getHSStatus(hsStatus); |
|
920 |
||
921 |
return new SSLEngineResult(status, hsStatus, |
|
922 |
ea.deltaNet(), ea.deltaApp()); |
|
923 |
} |
|
924 |
||
925 |
/* |
|
926 |
* Actually do the read record processing. |
|
927 |
* |
|
928 |
* Returns a Status if it can make specific determinations |
|
929 |
* of the engine state. In particular, we need to signal |
|
930 |
* that a handshake just completed. |
|
931 |
* |
|
932 |
* It would be nice to be symmetrical with the write side and move |
|
933 |
* the majority of this to EngineInputRecord, but there's too much |
|
934 |
* SSLEngine state to do that cleanly. It must still live here. |
|
935 |
*/ |
|
936 |
private HandshakeStatus readRecord(EngineArgs ea) throws IOException { |
|
937 |
||
938 |
HandshakeStatus hsStatus = null; |
|
939 |
||
940 |
/* |
|
941 |
* The various operations will return new sliced BB's, |
|
942 |
* this will avoid having to worry about positions and |
|
943 |
* limits in the netBB. |
|
944 |
*/ |
|
945 |
ByteBuffer readBB = null; |
|
946 |
ByteBuffer decryptedBB = null; |
|
947 |
||
948 |
if (getConnectionState() != cs_ERROR) { |
|
949 |
||
950 |
/* |
|
951 |
* Read a record ... maybe emitting an alert if we get a |
|
952 |
* comprehensible but unsupported "hello" message during |
|
953 |
* format checking (e.g. V2). |
|
954 |
*/ |
|
955 |
try { |
|
956 |
readBB = inputRecord.read(ea.netData); |
|
957 |
} catch (IOException e) { |
|
958 |
fatal(Alerts.alert_unexpected_message, e); |
|
959 |
} |
|
960 |
||
961 |
/* |
|
962 |
* The basic SSLv3 record protection involves (optional) |
|
963 |
* encryption for privacy, and an integrity check ensuring |
|
964 |
* data origin authentication. We do them both here, and |
|
965 |
* throw a fatal alert if the integrity check fails. |
|
966 |
*/ |
|
967 |
try { |
|
16913 | 968 |
decryptedBB = inputRecord.decrypt( |
969 |
readAuthenticator, readCipher, readBB); |
|
2 | 970 |
} catch (BadPaddingException e) { |
971 |
byte alertType = (inputRecord.contentType() == |
|
972 |
Record.ct_handshake) ? |
|
973 |
Alerts.alert_handshake_failure : |
|
974 |
Alerts.alert_bad_record_mac; |
|
16113 | 975 |
fatal(alertType, e.getMessage(), e); |
2 | 976 |
} |
977 |
||
978 |
// if (!inputRecord.decompress(c)) |
|
979 |
// fatal(Alerts.alert_decompression_failure, |
|
980 |
// "decompression failure"); |
|
981 |
||
982 |
||
983 |
/* |
|
984 |
* Process the record. |
|
985 |
*/ |
|
986 |
||
987 |
synchronized (this) { |
|
988 |
switch (inputRecord.contentType()) { |
|
989 |
case Record.ct_handshake: |
|
990 |
/* |
|
991 |
* Handshake messages always go to a pending session |
|
992 |
* handshaker ... if there isn't one, create one. This |
|
993 |
* must work asynchronously, for renegotiation. |
|
994 |
* |
|
995 |
* NOTE that handshaking will either resume a session |
|
996 |
* which was in the cache (and which might have other |
|
997 |
* connections in it already), or else will start a new |
|
998 |
* session (new keys exchanged) with just this connection |
|
999 |
* in it. |
|
1000 |
*/ |
|
1001 |
initHandshaker(); |
|
7039 | 1002 |
if (!handshaker.activated()) { |
1003 |
// prior to handshaking, activate the handshake |
|
1004 |
if (connectionState == cs_RENEGOTIATE) { |
|
1005 |
// don't use SSLv2Hello when renegotiating |
|
1006 |
handshaker.activate(protocolVersion); |
|
1007 |
} else { |
|
1008 |
handshaker.activate(null); |
|
1009 |
} |
|
1010 |
} |
|
2 | 1011 |
|
1012 |
/* |
|
1013 |
* process the handshake record ... may contain just |
|
1014 |
* a partial handshake message or multiple messages. |
|
1015 |
* |
|
1016 |
* The handshaker state machine will ensure that it's |
|
1017 |
* a finished message. |
|
1018 |
*/ |
|
1019 |
handshaker.process_record(inputRecord, expectingFinished); |
|
1020 |
expectingFinished = false; |
|
1021 |
||
5182 | 1022 |
if (handshaker.invalidated) { |
1023 |
handshaker = null; |
|
1024 |
// if state is cs_RENEGOTIATE, revert it to cs_DATA |
|
1025 |
if (connectionState == cs_RENEGOTIATE) { |
|
1026 |
connectionState = cs_DATA; |
|
1027 |
} |
|
1028 |
} else if (handshaker.isDone()) { |
|
6856 | 1029 |
// reset the parameters for secure renegotiation. |
1030 |
secureRenegotiation = |
|
1031 |
handshaker.isSecureRenegotiation(); |
|
1032 |
clientVerifyData = handshaker.getClientVerifyData(); |
|
1033 |
serverVerifyData = handshaker.getServerVerifyData(); |
|
1034 |
||
2 | 1035 |
sess = handshaker.getSession(); |
7043 | 1036 |
handshakeSession = null; |
2 | 1037 |
if (!writer.hasOutboundData()) { |
1038 |
hsStatus = HandshakeStatus.FINISHED; |
|
1039 |
} |
|
1040 |
handshaker = null; |
|
1041 |
connectionState = cs_DATA; |
|
1042 |
||
1043 |
// No handshakeListeners here. That's a |
|
1044 |
// SSLSocket thing. |
|
1045 |
} else if (handshaker.taskOutstanding()) { |
|
1046 |
hsStatus = HandshakeStatus.NEED_TASK; |
|
1047 |
} |
|
1048 |
break; |
|
1049 |
||
1050 |
case Record.ct_application_data: |
|
1051 |
// Pass this right back up to the application. |
|
1052 |
if ((connectionState != cs_DATA) |
|
1053 |
&& (connectionState != cs_RENEGOTIATE) |
|
1054 |
&& (connectionState != cs_CLOSED)) { |
|
1055 |
throw new SSLProtocolException( |
|
1056 |
"Data received in non-data state: " + |
|
1057 |
connectionState); |
|
1058 |
} |
|
1059 |
||
1060 |
if (expectingFinished) { |
|
1061 |
throw new SSLProtocolException |
|
1062 |
("Expecting finished message, received data"); |
|
1063 |
} |
|
1064 |
||
1065 |
/* |
|
1066 |
* Don't return data once the inbound side is |
|
1067 |
* closed. |
|
1068 |
*/ |
|
1069 |
if (!inboundDone) { |
|
1070 |
ea.scatter(decryptedBB.slice()); |
|
1071 |
} |
|
1072 |
break; |
|
1073 |
||
1074 |
case Record.ct_alert: |
|
1075 |
recvAlert(); |
|
1076 |
break; |
|
1077 |
||
1078 |
case Record.ct_change_cipher_spec: |
|
1079 |
if ((connectionState != cs_HANDSHAKE |
|
1080 |
&& connectionState != cs_RENEGOTIATE) |
|
1081 |
|| inputRecord.available() != 1 |
|
1082 |
|| inputRecord.read() != 1) { |
|
1083 |
fatal(Alerts.alert_unexpected_message, |
|
1084 |
"illegal change cipher spec msg, state = " |
|
1085 |
+ connectionState); |
|
1086 |
} |
|
1087 |
||
1088 |
// |
|
1089 |
// The first message after a change_cipher_spec |
|
1090 |
// record MUST be a "Finished" handshake record, |
|
1091 |
// else it's a protocol violation. We force this |
|
1092 |
// to be checked by a minor tweak to the state |
|
1093 |
// machine. |
|
1094 |
// |
|
1095 |
changeReadCiphers(); |
|
1096 |
// next message MUST be a finished message |
|
1097 |
expectingFinished = true; |
|
1098 |
break; |
|
1099 |
||
1100 |
default: |
|
1101 |
// |
|
1102 |
// TLS requires that unrecognized records be ignored. |
|
1103 |
// |
|
1104 |
if (debug != null && Debug.isOn("ssl")) { |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
1105 |
System.out.println(Thread.currentThread().getName() + |
2 | 1106 |
", Received record type: " |
1107 |
+ inputRecord.contentType()); |
|
1108 |
} |
|
1109 |
break; |
|
1110 |
} // switch |
|
7039 | 1111 |
|
1112 |
/* |
|
1113 |
* We only need to check the sequence number state for |
|
1114 |
* non-handshaking record. |
|
1115 |
* |
|
1116 |
* Note that in order to maintain the handshake status |
|
1117 |
* properly, we check the sequence number after the last |
|
1118 |
* record reading process. As we request renegotiation |
|
1119 |
* or close the connection for wrapped sequence number |
|
1120 |
* when there is enough sequence number space left to |
|
1121 |
* handle a few more records, so the sequence number |
|
1122 |
* of the last record cannot be wrapped. |
|
1123 |
*/ |
|
13031
a410394dc617
7166487: checkSequenceNumber method never called within readRecord of SSLEngineImpl
xuelei
parents:
11684
diff
changeset
|
1124 |
hsStatus = getHSStatus(hsStatus); |
7039 | 1125 |
if (connectionState < cs_ERROR && !isInboundDone() && |
1126 |
(hsStatus == HandshakeStatus.NOT_HANDSHAKING)) { |
|
16913 | 1127 |
if (checkSequenceNumber(readAuthenticator, |
7039 | 1128 |
inputRecord.contentType())) { |
1129 |
hsStatus = getHSStatus(null); |
|
1130 |
} |
|
1131 |
} |
|
2 | 1132 |
} // synchronized (this) |
1133 |
} |
|
1134 |
||
1135 |
return hsStatus; |
|
1136 |
} |
|
1137 |
||
1138 |
||
1139 |
// |
|
1140 |
// write/wrap side |
|
1141 |
// |
|
1142 |
||
1143 |
||
1144 |
/** |
|
1145 |
* Wraps a buffer. Does a variety of checks before grabbing |
|
21278 | 1146 |
* the wrapLock, which blocks multiple wraps from occurring. |
2 | 1147 |
*/ |
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
1148 |
@Override |
2 | 1149 |
public SSLEngineResult wrap(ByteBuffer [] appData, |
1150 |
int offset, int length, ByteBuffer netData) throws SSLException { |
|
1151 |
||
1152 |
EngineArgs ea = new EngineArgs(appData, offset, length, netData); |
|
1153 |
||
1154 |
/* |
|
1155 |
* We can be smarter about using smaller buffer sizes later. |
|
1156 |
* For now, force it to be large enough to handle any |
|
1157 |
* valid SSL/TLS record. |
|
1158 |
*/ |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9246
diff
changeset
|
1159 |
if (netData.remaining() < EngineOutputRecord.maxRecordSize) { |
2 | 1160 |
return new SSLEngineResult( |
1161 |
Status.BUFFER_OVERFLOW, getHSStatus(null), 0, 0); |
|
1162 |
} |
|
1163 |
||
1164 |
try { |
|
1165 |
synchronized (wrapLock) { |
|
1166 |
return writeAppRecord(ea); |
|
1167 |
} |
|
1168 |
} catch (Exception e) { |
|
1169 |
ea.resetPos(); |
|
1170 |
||
1171 |
fatal(Alerts.alert_internal_error, |
|
11684 | 1172 |
"problem wrapping app data", e); |
2 | 1173 |
return null; // make compiler happy |
1174 |
} finally { |
|
1175 |
/* |
|
1176 |
* Just in case something didn't reset limits properly. |
|
1177 |
*/ |
|
1178 |
ea.resetLim(); |
|
1179 |
} |
|
1180 |
} |
|
1181 |
||
1182 |
/* |
|
1183 |
* Makes additional checks for unwrap, but this time more |
|
1184 |
* specific to this packet and the current state of the machine. |
|
1185 |
*/ |
|
1186 |
private SSLEngineResult writeAppRecord(EngineArgs ea) throws IOException { |
|
1187 |
||
1188 |
Status status = null; |
|
1189 |
HandshakeStatus hsStatus = null; |
|
1190 |
||
1191 |
/* |
|
1192 |
* See if the handshaker needs to report back some SSLException. |
|
1193 |
*/ |
|
1194 |
checkTaskThrown(); |
|
1195 |
||
1196 |
/* |
|
1197 |
* short circuit if we're closed/closing. |
|
1198 |
*/ |
|
1199 |
if (writer.isOutboundDone()) { |
|
1200 |
return new SSLEngineResult(Status.CLOSED, getHSStatus(null), 0, 0); |
|
1201 |
} |
|
1202 |
||
1203 |
/* |
|
1204 |
* If we're still in cs_HANDSHAKE, make sure it's been |
|
1205 |
* started. |
|
1206 |
*/ |
|
1207 |
synchronized (this) { |
|
1208 |
if ((connectionState == cs_HANDSHAKE) || |
|
1209 |
(connectionState == cs_START)) { |
|
1210 |
kickstartHandshake(); |
|
1211 |
||
1212 |
/* |
|
1213 |
* If there's no HS data available to write, we can return |
|
1214 |
* without trying to wrap anything. |
|
1215 |
*/ |
|
1216 |
hsStatus = getHSStatus(null); |
|
1217 |
||
1218 |
if (hsStatus == HandshakeStatus.NEED_UNWRAP) { |
|
1219 |
return new SSLEngineResult(Status.OK, hsStatus, 0, 0); |
|
1220 |
} |
|
1221 |
} |
|
1222 |
} |
|
1223 |
||
1224 |
/* |
|
1225 |
* Grab a copy of this if it doesn't already exist, |
|
1226 |
* and we can use it several places before anything major |
|
1227 |
* happens on this side. Races aren't critical |
|
1228 |
* here. |
|
1229 |
*/ |
|
1230 |
if (hsStatus == null) { |
|
1231 |
hsStatus = getHSStatus(null); |
|
1232 |
} |
|
1233 |
||
1234 |
/* |
|
1235 |
* If we have a task outstanding, this *MUST* be done before |
|
1236 |
* doing any more wrapping, because we could be in the middle |
|
1237 |
* of receiving a handshake message, for example, a finished |
|
1238 |
* message which would change the ciphers. |
|
1239 |
*/ |
|
1240 |
if (hsStatus == HandshakeStatus.NEED_TASK) { |
|
1241 |
return new SSLEngineResult( |
|
1242 |
Status.OK, hsStatus, 0, 0); |
|
1243 |
} |
|
1244 |
||
1245 |
/* |
|
1246 |
* This will obtain any waiting outbound data, or will |
|
1247 |
* process the outbound appData. |
|
1248 |
*/ |
|
1249 |
try { |
|
1250 |
synchronized (writeLock) { |
|
1251 |
hsStatus = writeRecord(outputRecord, ea); |
|
1252 |
} |
|
1253 |
} catch (SSLException e) { |
|
1254 |
throw e; |
|
1255 |
} catch (IOException e) { |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9246
diff
changeset
|
1256 |
throw new SSLException("Write problems", e); |
2 | 1257 |
} |
1258 |
||
1259 |
/* |
|
1260 |
* writeRecord might have reported some status. |
|
1261 |
* Now check for the remaining cases. |
|
1262 |
* |
|
1263 |
* status above should cover: NEED_WRAP/FINISHED |
|
1264 |
*/ |
|
1265 |
status = (isOutboundDone() ? Status.CLOSED : Status.OK); |
|
1266 |
hsStatus = getHSStatus(hsStatus); |
|
1267 |
||
1268 |
return new SSLEngineResult(status, hsStatus, |
|
1269 |
ea.deltaApp(), ea.deltaNet()); |
|
1270 |
} |
|
1271 |
||
1272 |
/* |
|
1273 |
* Central point to write/get all of the outgoing data. |
|
1274 |
*/ |
|
1275 |
private HandshakeStatus writeRecord(EngineOutputRecord eor, |
|
1276 |
EngineArgs ea) throws IOException { |
|
1277 |
||
1278 |
// eventually compress as well. |
|
7039 | 1279 |
HandshakeStatus hsStatus = |
16913 | 1280 |
writer.writeRecord(eor, ea, writeAuthenticator, writeCipher); |
7039 | 1281 |
|
1282 |
/* |
|
1283 |
* We only need to check the sequence number state for |
|
1284 |
* non-handshaking record. |
|
1285 |
* |
|
1286 |
* Note that in order to maintain the handshake status |
|
1287 |
* properly, we check the sequence number after the last |
|
1288 |
* record writing process. As we request renegotiation |
|
1289 |
* or close the connection for wrapped sequence number |
|
1290 |
* when there is enough sequence number space left to |
|
1291 |
* handle a few more records, so the sequence number |
|
1292 |
* of the last record cannot be wrapped. |
|
1293 |
*/ |
|
13031
a410394dc617
7166487: checkSequenceNumber method never called within readRecord of SSLEngineImpl
xuelei
parents:
11684
diff
changeset
|
1294 |
hsStatus = getHSStatus(hsStatus); |
7039 | 1295 |
if (connectionState < cs_ERROR && !isOutboundDone() && |
1296 |
(hsStatus == HandshakeStatus.NOT_HANDSHAKING)) { |
|
16913 | 1297 |
if (checkSequenceNumber(writeAuthenticator, eor.contentType())) { |
7039 | 1298 |
hsStatus = getHSStatus(null); |
1299 |
} |
|
1300 |
} |
|
1301 |
||
10915 | 1302 |
/* |
1303 |
* turn off the flag of the first application record if we really |
|
1304 |
* consumed at least byte. |
|
1305 |
*/ |
|
1306 |
if (isFirstAppOutputRecord && ea.deltaApp() > 0) { |
|
1307 |
isFirstAppOutputRecord = false; |
|
1308 |
} |
|
1309 |
||
7039 | 1310 |
return hsStatus; |
2 | 1311 |
} |
1312 |
||
1313 |
/* |
|
10915 | 1314 |
* Need to split the payload except the following cases: |
1315 |
* |
|
1316 |
* 1. protocol version is TLS 1.1 or later; |
|
1317 |
* 2. bulk cipher does not use CBC mode, including null bulk cipher suites. |
|
1318 |
* 3. the payload is the first application record of a freshly |
|
1319 |
* negotiated TLS session. |
|
1320 |
* 4. the CBC protection is disabled; |
|
1321 |
* |
|
1322 |
* More details, please refer to |
|
1323 |
* EngineOutputRecord.write(EngineArgs, MAC, CipherBox). |
|
1324 |
*/ |
|
1325 |
boolean needToSplitPayload(CipherBox cipher, ProtocolVersion protocol) { |
|
1326 |
return (protocol.v <= ProtocolVersion.TLS10.v) && |
|
1327 |
cipher.isCBCMode() && !isFirstAppOutputRecord && |
|
1328 |
Record.enableCBCProtection; |
|
1329 |
} |
|
1330 |
||
1331 |
/* |
|
2 | 1332 |
* Non-application OutputRecords go through here. |
1333 |
*/ |
|
1334 |
void writeRecord(EngineOutputRecord eor) throws IOException { |
|
1335 |
// eventually compress as well. |
|
16913 | 1336 |
writer.writeRecord(eor, writeAuthenticator, writeCipher); |
7039 | 1337 |
|
1338 |
/* |
|
1339 |
* Check the sequence number state |
|
1340 |
* |
|
1341 |
* Note that in order to maintain the connection I/O |
|
1342 |
* properly, we check the sequence number after the last |
|
1343 |
* record writing process. As we request renegotiation |
|
1344 |
* or close the connection for wrapped sequence number |
|
1345 |
* when there is enough sequence number space left to |
|
1346 |
* handle a few more records, so the sequence number |
|
1347 |
* of the last record cannot be wrapped. |
|
1348 |
*/ |
|
1349 |
if ((connectionState < cs_ERROR) && !isOutboundDone()) { |
|
16913 | 1350 |
checkSequenceNumber(writeAuthenticator, eor.contentType()); |
7039 | 1351 |
} |
2 | 1352 |
} |
1353 |
||
1354 |
// |
|
1355 |
// Close code |
|
1356 |
// |
|
1357 |
||
1358 |
/** |
|
7039 | 1359 |
* Check the sequence number state |
1360 |
* |
|
1361 |
* RFC 4346 states that, "Sequence numbers are of type uint64 and |
|
1362 |
* may not exceed 2^64-1. Sequence numbers do not wrap. If a TLS |
|
1363 |
* implementation would need to wrap a sequence number, it must |
|
1364 |
* renegotiate instead." |
|
1365 |
* |
|
1366 |
* Return true if the handshake status may be changed. |
|
1367 |
*/ |
|
16913 | 1368 |
private boolean checkSequenceNumber(Authenticator authenticator, byte type) |
7039 | 1369 |
throws IOException { |
1370 |
||
1371 |
/* |
|
1372 |
* Don't bother to check the sequence number for error or |
|
1373 |
* closed connections, or NULL MAC |
|
1374 |
*/ |
|
16913 | 1375 |
if (connectionState >= cs_ERROR || authenticator == MAC.NULL) { |
7039 | 1376 |
return false; |
1377 |
} |
|
1378 |
||
1379 |
/* |
|
1380 |
* Conservatively, close the connection immediately when the |
|
1381 |
* sequence number is close to overflow |
|
1382 |
*/ |
|
16913 | 1383 |
if (authenticator.seqNumOverflow()) { |
7039 | 1384 |
/* |
1385 |
* TLS protocols do not define a error alert for sequence |
|
1386 |
* number overflow. We use handshake_failure error alert |
|
1387 |
* for handshaking and bad_record_mac for other records. |
|
1388 |
*/ |
|
1389 |
if (debug != null && Debug.isOn("ssl")) { |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
1390 |
System.out.println(Thread.currentThread().getName() + |
7039 | 1391 |
", sequence number extremely close to overflow " + |
1392 |
"(2^64-1 packets). Closing connection."); |
|
1393 |
} |
|
1394 |
||
1395 |
fatal(Alerts.alert_handshake_failure, "sequence number overflow"); |
|
1396 |
||
1397 |
return true; // make the compiler happy |
|
1398 |
} |
|
1399 |
||
1400 |
/* |
|
1401 |
* Ask for renegotiation when need to renew sequence number. |
|
1402 |
* |
|
1403 |
* Don't bother to kickstart the renegotiation when the local is |
|
1404 |
* asking for it. |
|
1405 |
*/ |
|
16913 | 1406 |
if ((type != Record.ct_handshake) && authenticator.seqNumIsHuge()) { |
7039 | 1407 |
if (debug != null && Debug.isOn("ssl")) { |
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
1408 |
System.out.println(Thread.currentThread().getName() + |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
1409 |
", request renegotiation " + |
7039 | 1410 |
"to avoid sequence number overflow"); |
1411 |
} |
|
1412 |
||
1413 |
beginHandshake(); |
|
1414 |
return true; |
|
1415 |
} |
|
1416 |
||
1417 |
return false; |
|
1418 |
} |
|
1419 |
||
1420 |
/** |
|
2 | 1421 |
* Signals that no more outbound application data will be sent |
1422 |
* on this <code>SSLEngine</code>. |
|
1423 |
*/ |
|
1424 |
private void closeOutboundInternal() { |
|
1425 |
||
1426 |
if ((debug != null) && Debug.isOn("ssl")) { |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
1427 |
System.out.println(Thread.currentThread().getName() + |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
1428 |
", closeOutboundInternal()"); |
2 | 1429 |
} |
1430 |
||
1431 |
/* |
|
1432 |
* Already closed, ignore |
|
1433 |
*/ |
|
1434 |
if (writer.isOutboundDone()) { |
|
1435 |
return; |
|
1436 |
} |
|
1437 |
||
1438 |
switch (connectionState) { |
|
1439 |
||
1440 |
/* |
|
1441 |
* If we haven't even started yet, don't bother reading inbound. |
|
1442 |
*/ |
|
1443 |
case cs_START: |
|
1444 |
writer.closeOutbound(); |
|
1445 |
inboundDone = true; |
|
1446 |
break; |
|
1447 |
||
1448 |
case cs_ERROR: |
|
1449 |
case cs_CLOSED: |
|
1450 |
break; |
|
1451 |
||
1452 |
/* |
|
1453 |
* Otherwise we indicate clean termination. |
|
1454 |
*/ |
|
1455 |
// case cs_HANDSHAKE: |
|
1456 |
// case cs_DATA: |
|
1457 |
// case cs_RENEGOTIATE: |
|
1458 |
default: |
|
1459 |
warning(Alerts.alert_close_notify); |
|
1460 |
writer.closeOutbound(); |
|
1461 |
break; |
|
1462 |
} |
|
1463 |
||
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:
2
diff
changeset
|
1464 |
// See comment in changeReadCiphers() |
0a6b65d56746
6750401: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes,with PCKS11 provider
wetmore
parents:
2
diff
changeset
|
1465 |
writeCipher.dispose(); |
0a6b65d56746
6750401: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes,with PCKS11 provider
wetmore
parents:
2
diff
changeset
|
1466 |
|
2 | 1467 |
connectionState = cs_CLOSED; |
1468 |
} |
|
1469 |
||
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
1470 |
@Override |
2 | 1471 |
synchronized public void closeOutbound() { |
1472 |
/* |
|
1473 |
* Dump out a close_notify to the remote side |
|
1474 |
*/ |
|
1475 |
if ((debug != null) && Debug.isOn("ssl")) { |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
1476 |
System.out.println(Thread.currentThread().getName() + |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
1477 |
", called closeOutbound()"); |
2 | 1478 |
} |
1479 |
||
1480 |
closeOutboundInternal(); |
|
1481 |
} |
|
1482 |
||
1483 |
/** |
|
1484 |
* Returns the outbound application data closure state |
|
1485 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
1486 |
@Override |
2 | 1487 |
public boolean isOutboundDone() { |
1488 |
return writer.isOutboundDone(); |
|
1489 |
} |
|
1490 |
||
1491 |
/** |
|
1492 |
* Signals that no more inbound network data will be sent |
|
1493 |
* to this <code>SSLEngine</code>. |
|
1494 |
*/ |
|
1495 |
private void closeInboundInternal() { |
|
1496 |
||
1497 |
if ((debug != null) && Debug.isOn("ssl")) { |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
1498 |
System.out.println(Thread.currentThread().getName() + |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
1499 |
", closeInboundInternal()"); |
2 | 1500 |
} |
1501 |
||
1502 |
/* |
|
1503 |
* Already closed, ignore |
|
1504 |
*/ |
|
1505 |
if (inboundDone) { |
|
1506 |
return; |
|
1507 |
} |
|
1508 |
||
1509 |
closeOutboundInternal(); |
|
1510 |
inboundDone = true; |
|
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:
2
diff
changeset
|
1511 |
|
0a6b65d56746
6750401: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes,with PCKS11 provider
wetmore
parents:
2
diff
changeset
|
1512 |
// See comment in changeReadCiphers() |
0a6b65d56746
6750401: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes,with PCKS11 provider
wetmore
parents:
2
diff
changeset
|
1513 |
readCipher.dispose(); |
0a6b65d56746
6750401: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes,with PCKS11 provider
wetmore
parents:
2
diff
changeset
|
1514 |
|
2 | 1515 |
connectionState = cs_CLOSED; |
1516 |
} |
|
1517 |
||
1518 |
/* |
|
1519 |
* Close the inbound side of the connection. We grab the |
|
1520 |
* lock here, and do the real work in the internal verison. |
|
1521 |
* We do check for truncation attacks. |
|
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 |
synchronized public void closeInbound() throws SSLException { |
1525 |
/* |
|
1526 |
* Currently closes the outbound side as well. The IETF TLS |
|
1527 |
* working group has expressed the opinion that 1/2 open |
|
1528 |
* connections are not allowed by the spec. May change |
|
1529 |
* someday in the future. |
|
1530 |
*/ |
|
1531 |
if ((debug != null) && Debug.isOn("ssl")) { |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
1532 |
System.out.println(Thread.currentThread().getName() + |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
1533 |
", called closeInbound()"); |
2 | 1534 |
} |
1535 |
||
1536 |
/* |
|
1537 |
* No need to throw an Exception if we haven't even started yet. |
|
1538 |
*/ |
|
1539 |
if ((connectionState != cs_START) && !recvCN) { |
|
1540 |
recvCN = true; // Only receive the Exception once |
|
1541 |
fatal(Alerts.alert_internal_error, |
|
1542 |
"Inbound closed before receiving peer's close_notify: " + |
|
1543 |
"possible truncation attack?"); |
|
1544 |
} else { |
|
1545 |
/* |
|
1546 |
* Currently, this is a no-op, but in case we change |
|
1547 |
* the close inbound code later. |
|
1548 |
*/ |
|
1549 |
closeInboundInternal(); |
|
1550 |
} |
|
1551 |
} |
|
1552 |
||
1553 |
/** |
|
1554 |
* Returns the network inbound data closure state |
|
1555 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
1556 |
@Override |
2 | 1557 |
synchronized public boolean isInboundDone() { |
1558 |
return inboundDone; |
|
1559 |
} |
|
1560 |
||
1561 |
||
1562 |
// |
|
1563 |
// Misc stuff |
|
1564 |
// |
|
1565 |
||
1566 |
||
1567 |
/** |
|
1568 |
* Returns the current <code>SSLSession</code> for this |
|
1569 |
* <code>SSLEngine</code> |
|
1570 |
* <P> |
|
1571 |
* These can be long lived, and frequently correspond to an |
|
1572 |
* entire login session for some user. |
|
1573 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
1574 |
@Override |
2 | 1575 |
synchronized public SSLSession getSession() { |
1576 |
return sess; |
|
1577 |
} |
|
1578 |
||
7043 | 1579 |
@Override |
1580 |
synchronized public SSLSession getHandshakeSession() { |
|
1581 |
return handshakeSession; |
|
1582 |
} |
|
1583 |
||
1584 |
synchronized void setHandshakeSession(SSLSessionImpl session) { |
|
1585 |
handshakeSession = session; |
|
1586 |
} |
|
1587 |
||
2 | 1588 |
/** |
1589 |
* Returns a delegated <code>Runnable</code> task for |
|
1590 |
* this <code>SSLEngine</code>. |
|
1591 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
1592 |
@Override |
2 | 1593 |
synchronized public Runnable getDelegatedTask() { |
1594 |
if (handshaker != null) { |
|
1595 |
return handshaker.getTask(); |
|
1596 |
} |
|
1597 |
return null; |
|
1598 |
} |
|
1599 |
||
1600 |
||
1601 |
// |
|
1602 |
// EXCEPTION AND ALERT HANDLING |
|
1603 |
// |
|
1604 |
||
1605 |
/* |
|
1606 |
* Send a warning alert. |
|
1607 |
*/ |
|
1608 |
void warning(byte description) { |
|
1609 |
sendAlert(Alerts.alert_warning, description); |
|
1610 |
} |
|
1611 |
||
1612 |
synchronized void fatal(byte description, String diagnostic) |
|
1613 |
throws SSLException { |
|
1614 |
fatal(description, diagnostic, null); |
|
1615 |
} |
|
1616 |
||
1617 |
synchronized void fatal(byte description, Throwable cause) |
|
1618 |
throws SSLException { |
|
1619 |
fatal(description, null, cause); |
|
1620 |
} |
|
1621 |
||
1622 |
/* |
|
1623 |
* We've got a fatal error here, so start the shutdown process. |
|
1624 |
* |
|
1625 |
* Because of the way the code was written, we have some code |
|
1626 |
* calling fatal directly when the "description" is known |
|
1627 |
* and some throwing Exceptions which are then caught by higher |
|
1628 |
* levels which then call here. This code needs to determine |
|
1629 |
* if one of the lower levels has already started the process. |
|
1630 |
* |
|
1631 |
* We won't worry about Error's, if we have one of those, |
|
1632 |
* we're in worse trouble. Note: the networking code doesn't |
|
1633 |
* deal with Errors either. |
|
1634 |
*/ |
|
1635 |
synchronized void fatal(byte description, String diagnostic, |
|
1636 |
Throwable cause) throws SSLException { |
|
1637 |
||
1638 |
/* |
|
1639 |
* If we have no further information, make a general-purpose |
|
1640 |
* message for folks to see. We generally have one or the other. |
|
1641 |
*/ |
|
1642 |
if (diagnostic == null) { |
|
1643 |
diagnostic = "General SSLEngine problem"; |
|
1644 |
} |
|
1645 |
if (cause == null) { |
|
1646 |
cause = Alerts.getSSLException(description, cause, diagnostic); |
|
1647 |
} |
|
1648 |
||
1649 |
/* |
|
1650 |
* If we've already shutdown because of an error, |
|
1651 |
* there is nothing we can do except rethrow the exception. |
|
1652 |
* |
|
1653 |
* Most exceptions seen here will be SSLExceptions. |
|
1654 |
* We may find the occasional Exception which hasn't been |
|
1655 |
* converted to a SSLException, so we'll do it here. |
|
1656 |
*/ |
|
1657 |
if (closeReason != null) { |
|
1658 |
if ((debug != null) && Debug.isOn("ssl")) { |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
1659 |
System.out.println(Thread.currentThread().getName() + |
2 | 1660 |
", fatal: engine already closed. Rethrowing " + |
1661 |
cause.toString()); |
|
1662 |
} |
|
1663 |
if (cause instanceof RuntimeException) { |
|
1664 |
throw (RuntimeException)cause; |
|
1665 |
} else if (cause instanceof SSLException) { |
|
1666 |
throw (SSLException)cause; |
|
1667 |
} else if (cause instanceof Exception) { |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9246
diff
changeset
|
1668 |
throw new SSLException("fatal SSLEngine condition", cause); |
2 | 1669 |
} |
1670 |
} |
|
1671 |
||
1672 |
if ((debug != null) && Debug.isOn("ssl")) { |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
1673 |
System.out.println(Thread.currentThread().getName() |
2 | 1674 |
+ ", fatal error: " + description + |
1675 |
": " + diagnostic + "\n" + cause.toString()); |
|
1676 |
} |
|
1677 |
||
1678 |
/* |
|
1679 |
* Ok, this engine's going down. |
|
1680 |
*/ |
|
1681 |
int oldState = connectionState; |
|
1682 |
connectionState = cs_ERROR; |
|
1683 |
||
1684 |
inboundDone = true; |
|
1685 |
||
1686 |
sess.invalidate(); |
|
7043 | 1687 |
if (handshakeSession != null) { |
1688 |
handshakeSession.invalidate(); |
|
1689 |
} |
|
2 | 1690 |
|
1691 |
/* |
|
1692 |
* If we haven't even started handshaking yet, no need |
|
1693 |
* to generate the fatal close alert. |
|
1694 |
*/ |
|
1695 |
if (oldState != cs_START) { |
|
1696 |
sendAlert(Alerts.alert_fatal, description); |
|
1697 |
} |
|
1698 |
||
1699 |
if (cause instanceof SSLException) { // only true if != null |
|
1700 |
closeReason = (SSLException)cause; |
|
1701 |
} else { |
|
1702 |
/* |
|
1703 |
* Including RuntimeExceptions, but we'll throw those |
|
1704 |
* down below. The closeReason isn't used again, |
|
1705 |
* except for null checks. |
|
1706 |
*/ |
|
1707 |
closeReason = |
|
1708 |
Alerts.getSSLException(description, cause, diagnostic); |
|
1709 |
} |
|
1710 |
||
1711 |
writer.closeOutbound(); |
|
1712 |
||
1713 |
connectionState = cs_CLOSED; |
|
1714 |
||
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:
2
diff
changeset
|
1715 |
// See comment in changeReadCiphers() |
0a6b65d56746
6750401: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes,with PCKS11 provider
wetmore
parents:
2
diff
changeset
|
1716 |
readCipher.dispose(); |
0a6b65d56746
6750401: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes,with PCKS11 provider
wetmore
parents:
2
diff
changeset
|
1717 |
writeCipher.dispose(); |
0a6b65d56746
6750401: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes,with PCKS11 provider
wetmore
parents:
2
diff
changeset
|
1718 |
|
2 | 1719 |
if (cause instanceof RuntimeException) { |
1720 |
throw (RuntimeException)cause; |
|
1721 |
} else { |
|
1722 |
throw closeReason; |
|
1723 |
} |
|
1724 |
} |
|
1725 |
||
1726 |
/* |
|
1727 |
* Process an incoming alert ... caller must already have synchronized |
|
1728 |
* access to "this". |
|
1729 |
*/ |
|
1730 |
private void recvAlert() throws IOException { |
|
1731 |
byte level = (byte)inputRecord.read(); |
|
1732 |
byte description = (byte)inputRecord.read(); |
|
1733 |
if (description == -1) { // check for short message |
|
1734 |
fatal(Alerts.alert_illegal_parameter, "Short alert message"); |
|
1735 |
} |
|
1736 |
||
1737 |
if (debug != null && (Debug.isOn("record") || |
|
1738 |
Debug.isOn("handshake"))) { |
|
1739 |
synchronized (System.out) { |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
1740 |
System.out.print(Thread.currentThread().getName()); |
2 | 1741 |
System.out.print(", RECV " + protocolVersion + " ALERT: "); |
1742 |
if (level == Alerts.alert_fatal) { |
|
1743 |
System.out.print("fatal, "); |
|
1744 |
} else if (level == Alerts.alert_warning) { |
|
1745 |
System.out.print("warning, "); |
|
1746 |
} else { |
|
1747 |
System.out.print("<level " + (0x0ff & level) + ">, "); |
|
1748 |
} |
|
1749 |
System.out.println(Alerts.alertDescription(description)); |
|
1750 |
} |
|
1751 |
} |
|
1752 |
||
1753 |
if (level == Alerts.alert_warning) { |
|
1754 |
if (description == Alerts.alert_close_notify) { |
|
1755 |
if (connectionState == cs_HANDSHAKE) { |
|
1756 |
fatal(Alerts.alert_unexpected_message, |
|
1757 |
"Received close_notify during handshake"); |
|
1758 |
} else { |
|
1759 |
recvCN = true; |
|
1760 |
closeInboundInternal(); // reply to close |
|
1761 |
} |
|
1762 |
} else { |
|
1763 |
||
1764 |
// |
|
1765 |
// The other legal warnings relate to certificates, |
|
1766 |
// e.g. no_certificate, bad_certificate, etc; these |
|
1767 |
// are important to the handshaking code, which can |
|
1768 |
// also handle illegal protocol alerts if needed. |
|
1769 |
// |
|
1770 |
if (handshaker != null) { |
|
1771 |
handshaker.handshakeAlert(description); |
|
1772 |
} |
|
1773 |
} |
|
1774 |
} else { // fatal or unknown level |
|
1775 |
String reason = "Received fatal alert: " |
|
1776 |
+ Alerts.alertDescription(description); |
|
1777 |
if (closeReason == null) { |
|
1778 |
closeReason = Alerts.getSSLException(description, reason); |
|
1779 |
} |
|
1780 |
fatal(Alerts.alert_unexpected_message, reason); |
|
1781 |
} |
|
1782 |
} |
|
1783 |
||
1784 |
||
1785 |
/* |
|
1786 |
* Emit alerts. Caller must have synchronized with "this". |
|
1787 |
*/ |
|
1788 |
private void sendAlert(byte level, byte description) { |
|
7039 | 1789 |
// the connectionState cannot be cs_START |
2 | 1790 |
if (connectionState >= cs_CLOSED) { |
1791 |
return; |
|
1792 |
} |
|
1793 |
||
7039 | 1794 |
// For initial handshaking, don't send alert message to peer if |
1795 |
// handshaker has not started. |
|
1796 |
if (connectionState == cs_HANDSHAKE && |
|
1797 |
(handshaker == null || !handshaker.started())) { |
|
1798 |
return; |
|
1799 |
} |
|
1800 |
||
2 | 1801 |
EngineOutputRecord r = new EngineOutputRecord(Record.ct_alert, this); |
1802 |
r.setVersion(protocolVersion); |
|
1803 |
||
1804 |
boolean useDebug = debug != null && Debug.isOn("ssl"); |
|
1805 |
if (useDebug) { |
|
1806 |
synchronized (System.out) { |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
1807 |
System.out.print(Thread.currentThread().getName()); |
2 | 1808 |
System.out.print(", SEND " + protocolVersion + " ALERT: "); |
1809 |
if (level == Alerts.alert_fatal) { |
|
1810 |
System.out.print("fatal, "); |
|
1811 |
} else if (level == Alerts.alert_warning) { |
|
1812 |
System.out.print("warning, "); |
|
1813 |
} else { |
|
1814 |
System.out.print("<level = " + (0x0ff & level) + ">, "); |
|
1815 |
} |
|
1816 |
System.out.println("description = " |
|
1817 |
+ Alerts.alertDescription(description)); |
|
1818 |
} |
|
1819 |
} |
|
1820 |
||
1821 |
r.write(level); |
|
1822 |
r.write(description); |
|
1823 |
try { |
|
1824 |
writeRecord(r); |
|
1825 |
} catch (IOException e) { |
|
1826 |
if (useDebug) { |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
1827 |
System.out.println(Thread.currentThread().getName() + |
2 | 1828 |
", Exception sending alert: " + e); |
1829 |
} |
|
1830 |
} |
|
1831 |
} |
|
1832 |
||
1833 |
||
1834 |
// |
|
1835 |
// VARIOUS OTHER METHODS (COMMON TO SSLSocket) |
|
1836 |
// |
|
1837 |
||
1838 |
||
1839 |
/** |
|
1840 |
* Controls whether new connections may cause creation of new SSL |
|
1841 |
* sessions. |
|
1842 |
* |
|
1843 |
* As long as handshaking has not started, we can change |
|
1844 |
* whether we enable session creations. Otherwise, |
|
1845 |
* we will need to wait for the next handshake. |
|
1846 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
1847 |
@Override |
2 | 1848 |
synchronized public void setEnableSessionCreation(boolean flag) { |
1849 |
enableSessionCreation = flag; |
|
1850 |
||
7039 | 1851 |
if ((handshaker != null) && !handshaker.activated()) { |
2 | 1852 |
handshaker.setEnableSessionCreation(enableSessionCreation); |
1853 |
} |
|
1854 |
} |
|
1855 |
||
1856 |
/** |
|
1857 |
* Returns true if new connections may cause creation of new SSL |
|
1858 |
* sessions. |
|
1859 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
1860 |
@Override |
2 | 1861 |
synchronized public boolean getEnableSessionCreation() { |
1862 |
return enableSessionCreation; |
|
1863 |
} |
|
1864 |
||
1865 |
||
1866 |
/** |
|
1867 |
* Sets the flag controlling whether a server mode engine |
|
1868 |
* *REQUIRES* SSL client authentication. |
|
1869 |
* |
|
1870 |
* As long as handshaking has not started, we can change |
|
1871 |
* whether client authentication is needed. Otherwise, |
|
1872 |
* we will need to wait for the next handshake. |
|
1873 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
1874 |
@Override |
2 | 1875 |
synchronized public void setNeedClientAuth(boolean flag) { |
1876 |
doClientAuth = (flag ? |
|
1877 |
SSLEngineImpl.clauth_required : SSLEngineImpl.clauth_none); |
|
1878 |
||
1879 |
if ((handshaker != null) && |
|
1880 |
(handshaker instanceof ServerHandshaker) && |
|
7039 | 1881 |
!handshaker.activated()) { |
2 | 1882 |
((ServerHandshaker) handshaker).setClientAuth(doClientAuth); |
1883 |
} |
|
1884 |
} |
|
1885 |
||
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
1886 |
@Override |
2 | 1887 |
synchronized public boolean getNeedClientAuth() { |
1888 |
return (doClientAuth == SSLEngineImpl.clauth_required); |
|
1889 |
} |
|
1890 |
||
1891 |
/** |
|
1892 |
* Sets the flag controlling whether a server mode engine |
|
1893 |
* *REQUESTS* SSL client authentication. |
|
1894 |
* |
|
1895 |
* As long as handshaking has not started, we can change |
|
1896 |
* whether client authentication is requested. Otherwise, |
|
1897 |
* we will need to wait for the next handshake. |
|
1898 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
1899 |
@Override |
2 | 1900 |
synchronized public void setWantClientAuth(boolean flag) { |
1901 |
doClientAuth = (flag ? |
|
1902 |
SSLEngineImpl.clauth_requested : SSLEngineImpl.clauth_none); |
|
1903 |
||
1904 |
if ((handshaker != null) && |
|
1905 |
(handshaker instanceof ServerHandshaker) && |
|
7039 | 1906 |
!handshaker.activated()) { |
2 | 1907 |
((ServerHandshaker) handshaker).setClientAuth(doClientAuth); |
1908 |
} |
|
1909 |
} |
|
1910 |
||
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
1911 |
@Override |
2 | 1912 |
synchronized public boolean getWantClientAuth() { |
1913 |
return (doClientAuth == SSLEngineImpl.clauth_requested); |
|
1914 |
} |
|
1915 |
||
1916 |
||
1917 |
/** |
|
1918 |
* Sets the flag controlling whether the engine is in SSL |
|
1919 |
* client or server mode. Must be called before any SSL |
|
1920 |
* traffic has started. |
|
1921 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
1922 |
@Override |
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9246
diff
changeset
|
1923 |
@SuppressWarnings("fallthrough") |
2 | 1924 |
synchronized public void setUseClientMode(boolean flag) { |
1925 |
switch (connectionState) { |
|
1926 |
||
1927 |
case cs_START: |
|
7039 | 1928 |
/* |
1929 |
* If we need to change the engine mode and the enabled |
|
1930 |
* protocols haven't specifically been set by the user, |
|
1931 |
* change them to the corresponding default ones. |
|
1932 |
*/ |
|
1933 |
if (roleIsServer != (!flag) && |
|
9246
c459f79af46b
6976117: SSLContext.getInstance("TLSv1.1") returns SSLEngines/SSLSockets without TLSv1.1 enabled
xuelei
parents:
7043
diff
changeset
|
1934 |
sslContext.isDefaultProtocolList(enabledProtocols)) { |
c459f79af46b
6976117: SSLContext.getInstance("TLSv1.1") returns SSLEngines/SSLSockets without TLSv1.1 enabled
xuelei
parents:
7043
diff
changeset
|
1935 |
enabledProtocols = sslContext.getDefaultProtocolList(!flag); |
7039 | 1936 |
} |
1937 |
||
2 | 1938 |
roleIsServer = !flag; |
1939 |
serverModeSet = true; |
|
1940 |
break; |
|
1941 |
||
1942 |
case cs_HANDSHAKE: |
|
1943 |
/* |
|
1944 |
* If we have a handshaker, but haven't started |
|
1945 |
* SSL traffic, we can throw away our current |
|
1946 |
* handshaker, and start from scratch. Don't |
|
1947 |
* need to call doneConnect() again, we already |
|
1948 |
* have the streams. |
|
1949 |
*/ |
|
1950 |
assert(handshaker != null); |
|
7039 | 1951 |
if (!handshaker.activated()) { |
1952 |
/* |
|
1953 |
* If we need to change the engine mode and the enabled |
|
1954 |
* protocols haven't specifically been set by the user, |
|
1955 |
* change them to the corresponding default ones. |
|
1956 |
*/ |
|
1957 |
if (roleIsServer != (!flag) && |
|
9246
c459f79af46b
6976117: SSLContext.getInstance("TLSv1.1") returns SSLEngines/SSLSockets without TLSv1.1 enabled
xuelei
parents:
7043
diff
changeset
|
1958 |
sslContext.isDefaultProtocolList(enabledProtocols)) { |
c459f79af46b
6976117: SSLContext.getInstance("TLSv1.1") returns SSLEngines/SSLSockets without TLSv1.1 enabled
xuelei
parents:
7043
diff
changeset
|
1959 |
enabledProtocols = sslContext.getDefaultProtocolList(!flag); |
7039 | 1960 |
} |
1961 |
||
2 | 1962 |
roleIsServer = !flag; |
1963 |
connectionState = cs_START; |
|
1964 |
initHandshaker(); |
|
1965 |
break; |
|
1966 |
} |
|
1967 |
||
1968 |
// If handshake has started, that's an error. Fall through... |
|
1969 |
||
1970 |
default: |
|
1971 |
if (debug != null && Debug.isOn("ssl")) { |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
1972 |
System.out.println(Thread.currentThread().getName() + |
2 | 1973 |
", setUseClientMode() invoked in state = " + |
1974 |
connectionState); |
|
1975 |
} |
|
1976 |
||
1977 |
/* |
|
1978 |
* We can let them continue if they catch this correctly, |
|
1979 |
* we don't need to shut this down. |
|
1980 |
*/ |
|
1981 |
throw new IllegalArgumentException( |
|
1982 |
"Cannot change mode after SSL traffic has started"); |
|
1983 |
} |
|
1984 |
} |
|
1985 |
||
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
1986 |
@Override |
2 | 1987 |
synchronized public boolean getUseClientMode() { |
1988 |
return !roleIsServer; |
|
1989 |
} |
|
1990 |
||
1991 |
||
1992 |
/** |
|
1993 |
* Returns the names of the cipher suites which could be enabled for use |
|
1994 |
* on an SSL connection. Normally, only a subset of these will actually |
|
1995 |
* be enabled by default, since this list may include cipher suites which |
|
1996 |
* do not support the mutual authentication of servers and clients, or |
|
1997 |
* which do not protect data confidentiality. Servers may also need |
|
1998 |
* certain kinds of certificates to use certain cipher suites. |
|
1999 |
* |
|
2000 |
* @return an array of cipher suite names |
|
2001 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
2002 |
@Override |
2 | 2003 |
public String[] getSupportedCipherSuites() { |
13815 | 2004 |
return sslContext.getSupportedCipherSuiteList().toStringArray(); |
2 | 2005 |
} |
2006 |
||
2007 |
/** |
|
2008 |
* Controls which particular cipher suites are enabled for use on |
|
2009 |
* this connection. The cipher suites must have been listed by |
|
2010 |
* getCipherSuites() as being supported. Even if a suite has been |
|
2011 |
* enabled, it might never be used if no peer supports it or the |
|
2012 |
* requisite certificates (and private keys) are not available. |
|
2013 |
* |
|
2014 |
* @param suites Names of all the cipher suites to enable. |
|
2015 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
2016 |
@Override |
2 | 2017 |
synchronized public void setEnabledCipherSuites(String[] suites) { |
2018 |
enabledCipherSuites = new CipherSuiteList(suites); |
|
7039 | 2019 |
if ((handshaker != null) && !handshaker.activated()) { |
2020 |
handshaker.setEnabledCipherSuites(enabledCipherSuites); |
|
2 | 2021 |
} |
2022 |
} |
|
2023 |
||
2024 |
/** |
|
2025 |
* Returns the names of the SSL cipher suites which are currently enabled |
|
2026 |
* for use on this connection. When an SSL engine is first created, |
|
2027 |
* all enabled cipher suites <em>(a)</em> protect data confidentiality, |
|
2028 |
* by traffic encryption, and <em>(b)</em> can mutually authenticate |
|
2029 |
* both clients and servers. Thus, in some environments, this value |
|
2030 |
* might be empty. |
|
2031 |
* |
|
2032 |
* @return an array of cipher suite names |
|
2033 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
2034 |
@Override |
2 | 2035 |
synchronized public String[] getEnabledCipherSuites() { |
2036 |
return enabledCipherSuites.toStringArray(); |
|
2037 |
} |
|
2038 |
||
2039 |
||
2040 |
/** |
|
2041 |
* Returns the protocols that are supported by this implementation. |
|
2042 |
* A subset of the supported protocols may be enabled for this connection |
|
7043 | 2043 |
* @return an array of protocol names. |
2 | 2044 |
*/ |
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
2045 |
@Override |
2 | 2046 |
public String[] getSupportedProtocols() { |
9246
c459f79af46b
6976117: SSLContext.getInstance("TLSv1.1") returns SSLEngines/SSLSockets without TLSv1.1 enabled
xuelei
parents:
7043
diff
changeset
|
2047 |
return sslContext.getSuportedProtocolList().toStringArray(); |
2 | 2048 |
} |
2049 |
||
2050 |
/** |
|
2051 |
* Controls which protocols are enabled for use on |
|
2052 |
* this connection. The protocols must have been listed by |
|
2053 |
* getSupportedProtocols() as being supported. |
|
2054 |
* |
|
2055 |
* @param protocols protocols to enable. |
|
2056 |
* @exception IllegalArgumentException when one of the protocols |
|
2057 |
* named by the parameter is not supported. |
|
2058 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
2059 |
@Override |
2 | 2060 |
synchronized public void setEnabledProtocols(String[] protocols) { |
2061 |
enabledProtocols = new ProtocolList(protocols); |
|
7039 | 2062 |
if ((handshaker != null) && !handshaker.activated()) { |
2 | 2063 |
handshaker.setEnabledProtocols(enabledProtocols); |
2064 |
} |
|
2065 |
} |
|
2066 |
||
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
2067 |
@Override |
2 | 2068 |
synchronized public String[] getEnabledProtocols() { |
2069 |
return enabledProtocols.toStringArray(); |
|
2070 |
} |
|
2071 |
||
2072 |
/** |
|
7043 | 2073 |
* Returns the SSLParameters in effect for this SSLEngine. |
2 | 2074 |
*/ |
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
2075 |
@Override |
7043 | 2076 |
synchronized public SSLParameters getSSLParameters() { |
2077 |
SSLParameters params = super.getSSLParameters(); |
|
2078 |
||
2079 |
// the super implementation does not handle the following parameters |
|
2080 |
params.setEndpointIdentificationAlgorithm(identificationProtocol); |
|
2081 |
params.setAlgorithmConstraints(algorithmConstraints); |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
2082 |
params.setSNIMatchers(sniMatchers); |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
2083 |
params.setServerNames(serverNames); |
19823
f0fd09e20528
7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents:
16913
diff
changeset
|
2084 |
params.setUseCipherSuitesOrder(preferLocalCipherSuites); |
7043 | 2085 |
|
2086 |
return params; |
|
2 | 2087 |
} |
2088 |
||
2089 |
/** |
|
7043 | 2090 |
* Applies SSLParameters to this engine. |
2 | 2091 |
*/ |
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
2092 |
@Override |
7043 | 2093 |
synchronized public void setSSLParameters(SSLParameters params) { |
2094 |
super.setSSLParameters(params); |
|
2095 |
||
2096 |
// the super implementation does not handle the following parameters |
|
2097 |
identificationProtocol = params.getEndpointIdentificationAlgorithm(); |
|
2098 |
algorithmConstraints = params.getAlgorithmConstraints(); |
|
19823
f0fd09e20528
7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents:
16913
diff
changeset
|
2099 |
preferLocalCipherSuites = params.getUseCipherSuitesOrder(); |
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
2100 |
|
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
2101 |
List<SNIServerName> sniNames = params.getServerNames(); |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
2102 |
if (sniNames != null) { |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
2103 |
serverNames = sniNames; |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
2104 |
} |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
2105 |
|
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
2106 |
Collection<SNIMatcher> matchers = params.getSNIMatchers(); |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
2107 |
if (matchers != null) { |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
2108 |
sniMatchers = matchers; |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
2109 |
} |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
2110 |
|
7043 | 2111 |
if ((handshaker != null) && !handshaker.started()) { |
2112 |
handshaker.setIdentificationProtocol(identificationProtocol); |
|
2113 |
handshaker.setAlgorithmConstraints(algorithmConstraints); |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
2114 |
if (roleIsServer) { |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
2115 |
handshaker.setSNIMatchers(sniMatchers); |
19823
f0fd09e20528
7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents:
16913
diff
changeset
|
2116 |
handshaker.setUseCipherSuitesOrder(preferLocalCipherSuites); |
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
2117 |
} else { |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
2118 |
handshaker.setSNIServerNames(serverNames); |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
13815
diff
changeset
|
2119 |
} |
7043 | 2120 |
} |
2 | 2121 |
} |
2122 |
||
2123 |
/** |
|
2124 |
* Returns a printable representation of this end of the connection. |
|
2125 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
2126 |
@Override |
2 | 2127 |
public String toString() { |
2128 |
StringBuilder retval = new StringBuilder(80); |
|
2129 |
||
2130 |
retval.append(Integer.toHexString(hashCode())); |
|
2131 |
retval.append("["); |
|
2132 |
retval.append("SSLEngine[hostname="); |
|
2133 |
String host = getPeerHost(); |
|
2134 |
retval.append((host == null) ? "null" : host); |
|
2135 |
retval.append(" port="); |
|
2136 |
retval.append(Integer.toString(getPeerPort())); |
|
2137 |
retval.append("] "); |
|
2138 |
retval.append(getSession().getCipherSuite()); |
|
2139 |
retval.append("]"); |
|
2140 |
||
2141 |
return retval.toString(); |
|
2142 |
} |
|
2143 |
} |