author | jnimeh |
Wed, 06 Jun 2018 23:53:47 -0700 | |
branch | JDK-8145252-TLS13-branch |
changeset 56686 | 07dc566630ee |
parent 56542 | 56aaa6cb3693 |
permissions | -rw-r--r-- |
30904 | 1 |
/* |
56542 | 2 |
* Copyright (c) 1996, 2018, Oracle and/or its affiliates. All rights reserved. |
30904 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
||
26 |
package sun.security.ssl; |
|
27 |
||
28 |
import java.io.*; |
|
29 |
import java.nio.*; |
|
30 |
import java.util.*; |
|
31 |
import javax.net.ssl.*; |
|
56542 | 32 |
import sun.security.ssl.SSLCipher.SSLWriteCipher; |
30904 | 33 |
|
34 |
/** |
|
35 |
* DTLS {@code OutputRecord} implementation for {@code SSLEngine}. |
|
36 |
*/ |
|
37 |
final class DTLSOutputRecord extends OutputRecord implements DTLSRecord { |
|
38 |
||
39 |
private DTLSFragmenter fragmenter = null; |
|
40 |
||
41 |
int writeEpoch; |
|
42 |
||
43 |
int prevWriteEpoch; |
|
44 |
Authenticator prevWriteAuthenticator; |
|
56542 | 45 |
SSLWriteCipher prevWriteCipher; |
30904 | 46 |
|
56542 | 47 |
private final LinkedList<RecordMemo> alertMemos = new LinkedList<>(); |
30904 | 48 |
|
56542 | 49 |
DTLSOutputRecord(HandshakeHash handshakeHash) { |
50 |
super(handshakeHash, SSLWriteCipher.nullDTlsWriteCipher()); |
|
30904 | 51 |
|
52 |
this.writeEpoch = 0; |
|
53 |
this.prevWriteEpoch = 0; |
|
56542 | 54 |
this.prevWriteCipher = SSLWriteCipher.nullDTlsWriteCipher(); |
30904 | 55 |
|
56 |
this.packetSize = DTLSRecord.maxRecordSize; |
|
56542 | 57 |
this.protocolVersion = ProtocolVersion.NONE; |
58 |
} |
|
59 |
||
60 |
@Override |
|
61 |
void initHandshaker() { |
|
62 |
// clean up |
|
63 |
fragmenter = null; |
|
30904 | 64 |
} |
65 |
||
66 |
@Override |
|
56542 | 67 |
void finishHandshake() { |
56686
07dc566630ee
Address TLS 1.3 comments output stream classes, go through JsseJce class to get Mac instance.
jnimeh
parents:
56542
diff
changeset
|
68 |
// Nothing to do here currently. |
56542 | 69 |
} |
30904 | 70 |
|
56542 | 71 |
@Override |
72 |
void changeWriteCiphers(SSLWriteCipher writeCipher, |
|
73 |
boolean useChangeCipherSpec) throws IOException { |
|
74 |
if (useChangeCipherSpec) { |
|
75 |
encodeChangeCipherSpec(); |
|
76 |
} |
|
30904 | 77 |
|
78 |
prevWriteCipher.dispose(); |
|
79 |
||
80 |
this.prevWriteCipher = this.writeCipher; |
|
81 |
this.prevWriteEpoch = this.writeEpoch; |
|
82 |
||
83 |
this.writeCipher = writeCipher; |
|
84 |
this.writeEpoch++; |
|
85 |
||
86 |
this.isFirstAppOutputRecord = true; |
|
87 |
||
88 |
// set the epoch number |
|
56542 | 89 |
this.writeCipher.authenticator.setEpochNumber(this.writeEpoch); |
30904 | 90 |
} |
91 |
||
92 |
@Override |
|
93 |
void encodeAlert(byte level, byte description) throws IOException { |
|
94 |
RecordMemo memo = new RecordMemo(); |
|
95 |
||
56542 | 96 |
memo.contentType = ContentType.ALERT.id; |
30904 | 97 |
memo.majorVersion = protocolVersion.major; |
98 |
memo.minorVersion = protocolVersion.minor; |
|
99 |
memo.encodeEpoch = writeEpoch; |
|
100 |
memo.encodeCipher = writeCipher; |
|
101 |
||
102 |
memo.fragment = new byte[2]; |
|
103 |
memo.fragment[0] = level; |
|
104 |
memo.fragment[1] = description; |
|
105 |
||
106 |
alertMemos.add(memo); |
|
107 |
} |
|
108 |
||
109 |
@Override |
|
110 |
void encodeChangeCipherSpec() throws IOException { |
|
111 |
if (fragmenter == null) { |
|
112 |
fragmenter = new DTLSFragmenter(); |
|
113 |
} |
|
114 |
fragmenter.queueUpChangeCipherSpec(); |
|
115 |
} |
|
116 |
||
117 |
@Override |
|
118 |
void encodeHandshake(byte[] source, |
|
119 |
int offset, int length) throws IOException { |
|
120 |
if (firstMessage) { |
|
121 |
firstMessage = false; |
|
122 |
} |
|
123 |
||
124 |
if (fragmenter == null) { |
|
125 |
fragmenter = new DTLSFragmenter(); |
|
126 |
} |
|
127 |
||
128 |
fragmenter.queueUpHandshake(source, offset, length); |
|
129 |
} |
|
130 |
||
131 |
@Override |
|
56542 | 132 |
Ciphertext encode( |
133 |
ByteBuffer[] srcs, int srcsOffset, int srcsLength, |
|
134 |
ByteBuffer[] dsts, int dstsOffset, int dstsLength) throws IOException { |
|
135 |
return encode(srcs, srcsOffset, srcsLength, dsts[0]); |
|
136 |
} |
|
137 |
||
138 |
private Ciphertext encode(ByteBuffer[] sources, int offset, int length, |
|
30904 | 139 |
ByteBuffer destination) throws IOException { |
140 |
||
56542 | 141 |
if (writeCipher.authenticator.seqNumOverflow()) { |
142 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl")) { |
|
143 |
SSLLogger.fine( |
|
144 |
"sequence number extremely close to overflow " + |
|
30904 | 145 |
"(2^64-1 packets). Closing connection."); |
146 |
} |
|
147 |
||
148 |
throw new SSLHandshakeException("sequence number overflow"); |
|
149 |
} |
|
150 |
||
56542 | 151 |
// Don't process the incoming record until all of the buffered records |
152 |
// get handled. May need retransmission if no sources specified. |
|
153 |
if (!isEmpty() || sources == null || sources.length == 0) { |
|
154 |
Ciphertext ct = acquireCiphertext(destination); |
|
155 |
if (ct != null) { |
|
156 |
return ct; |
|
157 |
} |
|
30904 | 158 |
} |
159 |
||
56542 | 160 |
if (sources == null || sources.length == 0) { |
161 |
return null; |
|
162 |
} |
|
163 |
||
164 |
int srcsRemains = 0; |
|
165 |
for (int i = offset; i < offset + length; i++) { |
|
166 |
srcsRemains += sources[i].remaining(); |
|
167 |
} |
|
168 |
||
169 |
if (srcsRemains == 0) { |
|
170 |
return null; |
|
171 |
} |
|
172 |
||
173 |
// not apply to handshake message |
|
30904 | 174 |
int fragLen; |
175 |
if (packetSize > 0) { |
|
176 |
fragLen = Math.min(maxRecordSize, packetSize); |
|
177 |
fragLen = writeCipher.calculateFragmentSize( |
|
56542 | 178 |
fragLen, headerSize); |
30904 | 179 |
|
180 |
fragLen = Math.min(fragLen, Record.maxDataSize); |
|
181 |
} else { |
|
182 |
fragLen = Record.maxDataSize; |
|
183 |
} |
|
184 |
||
185 |
if (fragmentSize > 0) { |
|
186 |
fragLen = Math.min(fragLen, fragmentSize); |
|
187 |
} |
|
188 |
||
189 |
int dstPos = destination.position(); |
|
190 |
int dstLim = destination.limit(); |
|
191 |
int dstContent = dstPos + headerSize + |
|
192 |
writeCipher.getExplicitNonceSize(); |
|
193 |
destination.position(dstContent); |
|
194 |
||
195 |
int remains = Math.min(fragLen, destination.remaining()); |
|
196 |
fragLen = 0; |
|
197 |
int srcsLen = offset + length; |
|
198 |
for (int i = offset; (i < srcsLen) && (remains > 0); i++) { |
|
199 |
int amount = Math.min(sources[i].remaining(), remains); |
|
200 |
int srcLimit = sources[i].limit(); |
|
201 |
sources[i].limit(sources[i].position() + amount); |
|
202 |
destination.put(sources[i]); |
|
203 |
sources[i].limit(srcLimit); // restore the limit |
|
204 |
remains -= amount; |
|
205 |
fragLen += amount; |
|
206 |
} |
|
207 |
||
208 |
destination.limit(destination.position()); |
|
209 |
destination.position(dstContent); |
|
210 |
||
56542 | 211 |
if (SSLLogger.isOn && SSLLogger.isOn("record")) { |
212 |
SSLLogger.fine( |
|
213 |
"WRITE: " + protocolVersion + " " + |
|
214 |
ContentType.APPLICATION_DATA.name + |
|
30904 | 215 |
", length = " + destination.remaining()); |
216 |
} |
|
217 |
||
218 |
// Encrypt the fragment and wrap up a record. |
|
56542 | 219 |
long recordSN = encrypt(writeCipher, |
220 |
ContentType.APPLICATION_DATA.id, destination, |
|
30904 | 221 |
dstPos, dstLim, headerSize, |
56542 | 222 |
protocolVersion); |
30904 | 223 |
|
56542 | 224 |
if (SSLLogger.isOn && SSLLogger.isOn("packet")) { |
30904 | 225 |
ByteBuffer temporary = destination.duplicate(); |
226 |
temporary.limit(temporary.position()); |
|
227 |
temporary.position(dstPos); |
|
56542 | 228 |
SSLLogger.fine("Raw write", temporary); |
30904 | 229 |
} |
230 |
||
231 |
// remain the limit unchanged |
|
232 |
destination.limit(dstLim); |
|
233 |
||
56542 | 234 |
return new Ciphertext(ContentType.APPLICATION_DATA.id, |
235 |
SSLHandshake.NOT_APPLICABLE.id, recordSN); |
|
30904 | 236 |
} |
237 |
||
56542 | 238 |
private Ciphertext acquireCiphertext( |
239 |
ByteBuffer destination) throws IOException { |
|
30904 | 240 |
if (alertMemos != null && !alertMemos.isEmpty()) { |
241 |
RecordMemo memo = alertMemos.pop(); |
|
242 |
||
243 |
int dstPos = destination.position(); |
|
244 |
int dstLim = destination.limit(); |
|
245 |
int dstContent = dstPos + headerSize + |
|
246 |
writeCipher.getExplicitNonceSize(); |
|
247 |
destination.position(dstContent); |
|
248 |
||
249 |
destination.put(memo.fragment); |
|
250 |
||
251 |
destination.limit(destination.position()); |
|
252 |
destination.position(dstContent); |
|
253 |
||
56542 | 254 |
if (SSLLogger.isOn && SSLLogger.isOn("record")) { |
255 |
SSLLogger.fine( |
|
256 |
"WRITE: " + protocolVersion + " " + |
|
257 |
ContentType.ALERT.name + |
|
30904 | 258 |
", length = " + destination.remaining()); |
259 |
} |
|
260 |
||
261 |
// Encrypt the fragment and wrap up a record. |
|
56542 | 262 |
long recordSN = encrypt(memo.encodeCipher, |
263 |
ContentType.ALERT.id, |
|
264 |
destination, dstPos, dstLim, headerSize, |
|
30904 | 265 |
ProtocolVersion.valueOf(memo.majorVersion, |
56542 | 266 |
memo.minorVersion)); |
30904 | 267 |
|
56542 | 268 |
if (SSLLogger.isOn && SSLLogger.isOn("packet")) { |
30904 | 269 |
ByteBuffer temporary = destination.duplicate(); |
270 |
temporary.limit(temporary.position()); |
|
271 |
temporary.position(dstPos); |
|
56542 | 272 |
SSLLogger.fine("Raw write", temporary); |
30904 | 273 |
} |
274 |
||
275 |
// remain the limit unchanged |
|
276 |
destination.limit(dstLim); |
|
277 |
||
56542 | 278 |
return new Ciphertext(ContentType.ALERT.id, |
279 |
SSLHandshake.NOT_APPLICABLE.id, recordSN); |
|
30904 | 280 |
} |
281 |
||
282 |
if (fragmenter != null) { |
|
283 |
return fragmenter.acquireCiphertext(destination); |
|
284 |
} |
|
285 |
||
286 |
return null; |
|
287 |
} |
|
288 |
||
289 |
@Override |
|
290 |
boolean isEmpty() { |
|
291 |
return ((fragmenter == null) || fragmenter.isEmpty()) && |
|
292 |
((alertMemos == null) || alertMemos.isEmpty()); |
|
293 |
} |
|
294 |
||
295 |
@Override |
|
41820 | 296 |
void launchRetransmission() { |
297 |
// Note: Please don't retransmit if there are handshake messages |
|
298 |
// or alerts waiting in the queue. |
|
299 |
if (((alertMemos == null) || alertMemos.isEmpty()) && |
|
300 |
(fragmenter != null) && fragmenter.isRetransmittable()) { |
|
301 |
fragmenter.setRetransmission(); |
|
302 |
} |
|
303 |
} |
|
304 |
||
30904 | 305 |
// buffered record fragment |
306 |
private static class RecordMemo { |
|
307 |
byte contentType; |
|
308 |
byte majorVersion; |
|
309 |
byte minorVersion; |
|
310 |
int encodeEpoch; |
|
56542 | 311 |
SSLWriteCipher encodeCipher; |
30904 | 312 |
|
313 |
byte[] fragment; |
|
314 |
} |
|
315 |
||
316 |
private static class HandshakeMemo extends RecordMemo { |
|
317 |
byte handshakeType; |
|
318 |
int messageSequence; |
|
319 |
int acquireOffset; |
|
320 |
} |
|
321 |
||
322 |
private final class DTLSFragmenter { |
|
56542 | 323 |
private final LinkedList<RecordMemo> handshakeMemos = |
324 |
new LinkedList<>(); |
|
30904 | 325 |
private int acquireIndex = 0; |
326 |
private int messageSequence = 0; |
|
327 |
private boolean flightIsReady = false; |
|
328 |
||
329 |
// Per section 4.1.1, RFC 6347: |
|
330 |
// |
|
331 |
// If repeated retransmissions do not result in a response, and the |
|
332 |
// PMTU is unknown, subsequent retransmissions SHOULD back off to a |
|
333 |
// smaller record size, fragmenting the handshake message as |
|
334 |
// appropriate. |
|
335 |
// |
|
336 |
// In this implementation, two times of retransmits would be attempted |
|
337 |
// before backing off. The back off is supported only if the packet |
|
338 |
// size is bigger than 256 bytes. |
|
339 |
private int retransmits = 2; // attemps of retransmits |
|
340 |
||
341 |
void queueUpChangeCipherSpec() { |
|
342 |
||
343 |
// Cleanup if a new flight starts. |
|
344 |
if (flightIsReady) { |
|
345 |
handshakeMemos.clear(); |
|
346 |
acquireIndex = 0; |
|
347 |
flightIsReady = false; |
|
348 |
} |
|
349 |
||
350 |
RecordMemo memo = new RecordMemo(); |
|
351 |
||
56542 | 352 |
memo.contentType = ContentType.CHANGE_CIPHER_SPEC.id; |
30904 | 353 |
memo.majorVersion = protocolVersion.major; |
354 |
memo.minorVersion = protocolVersion.minor; |
|
355 |
memo.encodeEpoch = writeEpoch; |
|
356 |
memo.encodeCipher = writeCipher; |
|
357 |
||
358 |
memo.fragment = new byte[1]; |
|
359 |
memo.fragment[0] = 1; |
|
360 |
||
361 |
handshakeMemos.add(memo); |
|
362 |
} |
|
363 |
||
364 |
void queueUpHandshake(byte[] buf, |
|
365 |
int offset, int length) throws IOException { |
|
366 |
||
367 |
// Cleanup if a new flight starts. |
|
368 |
if (flightIsReady) { |
|
369 |
handshakeMemos.clear(); |
|
370 |
acquireIndex = 0; |
|
371 |
flightIsReady = false; |
|
372 |
} |
|
373 |
||
374 |
HandshakeMemo memo = new HandshakeMemo(); |
|
375 |
||
56542 | 376 |
memo.contentType = ContentType.HANDSHAKE.id; |
30904 | 377 |
memo.majorVersion = protocolVersion.major; |
378 |
memo.minorVersion = protocolVersion.minor; |
|
379 |
memo.encodeEpoch = writeEpoch; |
|
380 |
memo.encodeCipher = writeCipher; |
|
381 |
||
382 |
memo.handshakeType = buf[offset]; |
|
383 |
memo.messageSequence = messageSequence++; |
|
384 |
memo.acquireOffset = 0; |
|
385 |
memo.fragment = new byte[length - 4]; // 4: header size |
|
386 |
// 1: HandshakeType |
|
387 |
// 3: message length |
|
388 |
System.arraycopy(buf, offset + 4, memo.fragment, 0, length - 4); |
|
389 |
||
390 |
handshakeHashing(memo, memo.fragment); |
|
391 |
handshakeMemos.add(memo); |
|
392 |
||
56542 | 393 |
if ((memo.handshakeType == SSLHandshake.CLIENT_HELLO.id) || |
394 |
(memo.handshakeType == SSLHandshake.HELLO_REQUEST.id) || |
|
30904 | 395 |
(memo.handshakeType == |
56542 | 396 |
SSLHandshake.HELLO_VERIFY_REQUEST.id) || |
397 |
(memo.handshakeType == SSLHandshake.SERVER_HELLO_DONE.id) || |
|
398 |
(memo.handshakeType == SSLHandshake.FINISHED.id)) { |
|
30904 | 399 |
|
400 |
flightIsReady = true; |
|
401 |
} |
|
402 |
} |
|
403 |
||
404 |
Ciphertext acquireCiphertext(ByteBuffer dstBuf) throws IOException { |
|
405 |
if (isEmpty()) { |
|
406 |
if (isRetransmittable()) { |
|
407 |
setRetransmission(); // configure for retransmission |
|
408 |
} else { |
|
409 |
return null; |
|
410 |
} |
|
411 |
} |
|
412 |
||
413 |
RecordMemo memo = handshakeMemos.get(acquireIndex); |
|
414 |
HandshakeMemo hsMemo = null; |
|
56542 | 415 |
if (memo.contentType == ContentType.HANDSHAKE.id) { |
30904 | 416 |
hsMemo = (HandshakeMemo)memo; |
417 |
} |
|
418 |
||
419 |
// ChangeCipherSpec message is pretty small. Don't worry about |
|
420 |
// the fragmentation of ChangeCipherSpec record. |
|
421 |
int fragLen; |
|
422 |
if (packetSize > 0) { |
|
423 |
fragLen = Math.min(maxRecordSize, packetSize); |
|
424 |
fragLen = memo.encodeCipher.calculateFragmentSize( |
|
56542 | 425 |
fragLen, 25); // 25: header size |
30904 | 426 |
// 13: DTLS record |
427 |
// 12: DTLS handshake message |
|
428 |
fragLen = Math.min(fragLen, Record.maxDataSize); |
|
429 |
} else { |
|
430 |
fragLen = Record.maxDataSize; |
|
431 |
} |
|
432 |
||
433 |
if (fragmentSize > 0) { |
|
434 |
fragLen = Math.min(fragLen, fragmentSize); |
|
435 |
} |
|
436 |
||
437 |
int dstPos = dstBuf.position(); |
|
438 |
int dstLim = dstBuf.limit(); |
|
439 |
int dstContent = dstPos + headerSize + |
|
440 |
memo.encodeCipher.getExplicitNonceSize(); |
|
441 |
dstBuf.position(dstContent); |
|
442 |
||
443 |
if (hsMemo != null) { |
|
444 |
fragLen = Math.min(fragLen, |
|
445 |
(hsMemo.fragment.length - hsMemo.acquireOffset)); |
|
446 |
||
447 |
dstBuf.put(hsMemo.handshakeType); |
|
448 |
dstBuf.put((byte)((hsMemo.fragment.length >> 16) & 0xFF)); |
|
449 |
dstBuf.put((byte)((hsMemo.fragment.length >> 8) & 0xFF)); |
|
450 |
dstBuf.put((byte)(hsMemo.fragment.length & 0xFF)); |
|
451 |
dstBuf.put((byte)((hsMemo.messageSequence >> 8) & 0xFF)); |
|
452 |
dstBuf.put((byte)(hsMemo.messageSequence & 0xFF)); |
|
453 |
dstBuf.put((byte)((hsMemo.acquireOffset >> 16) & 0xFF)); |
|
454 |
dstBuf.put((byte)((hsMemo.acquireOffset >> 8) & 0xFF)); |
|
455 |
dstBuf.put((byte)(hsMemo.acquireOffset & 0xFF)); |
|
456 |
dstBuf.put((byte)((fragLen >> 16) & 0xFF)); |
|
457 |
dstBuf.put((byte)((fragLen >> 8) & 0xFF)); |
|
458 |
dstBuf.put((byte)(fragLen & 0xFF)); |
|
459 |
dstBuf.put(hsMemo.fragment, hsMemo.acquireOffset, fragLen); |
|
460 |
} else { |
|
461 |
fragLen = Math.min(fragLen, memo.fragment.length); |
|
462 |
dstBuf.put(memo.fragment, 0, fragLen); |
|
463 |
} |
|
464 |
||
465 |
dstBuf.limit(dstBuf.position()); |
|
466 |
dstBuf.position(dstContent); |
|
467 |
||
56542 | 468 |
if (SSLLogger.isOn && SSLLogger.isOn("record")) { |
469 |
SSLLogger.fine( |
|
470 |
"WRITE: " + protocolVersion + " " + |
|
471 |
ContentType.nameOf(memo.contentType) + |
|
30904 | 472 |
", length = " + dstBuf.remaining()); |
473 |
} |
|
474 |
||
475 |
// Encrypt the fragment and wrap up a record. |
|
56542 | 476 |
long recordSN = encrypt(memo.encodeCipher, |
30904 | 477 |
memo.contentType, dstBuf, |
478 |
dstPos, dstLim, headerSize, |
|
479 |
ProtocolVersion.valueOf(memo.majorVersion, |
|
56542 | 480 |
memo.minorVersion)); |
30904 | 481 |
|
56542 | 482 |
if (SSLLogger.isOn && SSLLogger.isOn("packet")) { |
30904 | 483 |
ByteBuffer temporary = dstBuf.duplicate(); |
484 |
temporary.limit(temporary.position()); |
|
485 |
temporary.position(dstPos); |
|
56542 | 486 |
SSLLogger.fine( |
487 |
"Raw write (" + temporary.remaining() + ")", temporary); |
|
30904 | 488 |
} |
489 |
||
490 |
// remain the limit unchanged |
|
491 |
dstBuf.limit(dstLim); |
|
492 |
||
493 |
// Reset the fragmentation offset. |
|
494 |
if (hsMemo != null) { |
|
495 |
hsMemo.acquireOffset += fragLen; |
|
496 |
if (hsMemo.acquireOffset == hsMemo.fragment.length) { |
|
497 |
acquireIndex++; |
|
498 |
} |
|
499 |
||
56542 | 500 |
return new Ciphertext(hsMemo.contentType, |
501 |
hsMemo.handshakeType, recordSN); |
|
30904 | 502 |
} else { |
503 |
acquireIndex++; |
|
56542 | 504 |
return new Ciphertext(ContentType.CHANGE_CIPHER_SPEC.id, |
505 |
SSLHandshake.NOT_APPLICABLE.id, recordSN); |
|
30904 | 506 |
} |
507 |
} |
|
508 |
||
509 |
private void handshakeHashing(HandshakeMemo hsFrag, byte[] hsBody) { |
|
510 |
||
511 |
byte hsType = hsFrag.handshakeType; |
|
56542 | 512 |
if (!handshakeHash.isHashable(hsType)) { |
30904 | 513 |
// omitted from handshake hash computation |
514 |
return; |
|
515 |
} |
|
516 |
||
517 |
// calculate the DTLS header |
|
518 |
byte[] temporary = new byte[12]; // 12: handshake header size |
|
519 |
||
520 |
// Handshake.msg_type |
|
521 |
temporary[0] = hsFrag.handshakeType; |
|
522 |
||
523 |
// Handshake.length |
|
524 |
temporary[1] = (byte)((hsBody.length >> 16) & 0xFF); |
|
525 |
temporary[2] = (byte)((hsBody.length >> 8) & 0xFF); |
|
526 |
temporary[3] = (byte)(hsBody.length & 0xFF); |
|
527 |
||
528 |
// Handshake.message_seq |
|
529 |
temporary[4] = (byte)((hsFrag.messageSequence >> 8) & 0xFF); |
|
530 |
temporary[5] = (byte)(hsFrag.messageSequence & 0xFF); |
|
531 |
||
532 |
// Handshake.fragment_offset |
|
533 |
temporary[6] = 0; |
|
534 |
temporary[7] = 0; |
|
535 |
temporary[8] = 0; |
|
536 |
||
537 |
// Handshake.fragment_length |
|
538 |
temporary[9] = temporary[1]; |
|
539 |
temporary[10] = temporary[2]; |
|
540 |
temporary[11] = temporary[3]; |
|
541 |
||
56542 | 542 |
handshakeHash.deliver(temporary, 0, 12); |
543 |
handshakeHash.deliver(hsBody, 0, hsBody.length); |
|
30904 | 544 |
} |
545 |
||
546 |
boolean isEmpty() { |
|
547 |
if (!flightIsReady || handshakeMemos.isEmpty() || |
|
548 |
acquireIndex >= handshakeMemos.size()) { |
|
549 |
return true; |
|
550 |
} |
|
551 |
||
552 |
return false; |
|
553 |
} |
|
554 |
||
555 |
boolean isRetransmittable() { |
|
556 |
return (flightIsReady && !handshakeMemos.isEmpty() && |
|
557 |
(acquireIndex >= handshakeMemos.size())); |
|
558 |
} |
|
559 |
||
560 |
private void setRetransmission() { |
|
561 |
acquireIndex = 0; |
|
562 |
for (RecordMemo memo : handshakeMemos) { |
|
563 |
if (memo instanceof HandshakeMemo) { |
|
564 |
HandshakeMemo hmemo = (HandshakeMemo)memo; |
|
565 |
hmemo.acquireOffset = 0; |
|
566 |
} |
|
567 |
} |
|
568 |
||
569 |
// Shrink packet size if: |
|
570 |
// 1. maximum fragment size is allowed, in which case the packet |
|
571 |
// size is configured bigger than maxRecordSize; |
|
572 |
// 2. maximum packet is bigger than 256 bytes; |
|
573 |
// 3. two times of retransmits have been attempted. |
|
574 |
if ((packetSize <= maxRecordSize) && |
|
575 |
(packetSize > 256) && ((retransmits--) <= 0)) { |
|
576 |
||
577 |
// shrink packet size |
|
578 |
shrinkPacketSize(); |
|
579 |
retransmits = 2; // attemps of retransmits |
|
580 |
} |
|
581 |
} |
|
582 |
||
583 |
private void shrinkPacketSize() { |
|
584 |
packetSize = Math.max(256, packetSize / 2); |
|
585 |
} |
|
586 |
} |
|
587 |
} |