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