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