author | xuelei |
Thu, 07 Feb 2013 16:05:55 -0800 | |
changeset 16113 | 946ec9b22004 |
parent 14664 | e71aa0962e70 |
child 16126 | aad71cf676d7 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
16113 | 2 |
* Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
||
27 |
package sun.security.ssl; |
|
28 |
||
29 |
import java.io.ByteArrayInputStream; |
|
30 |
import java.io.IOException; |
|
7039 | 31 |
import java.util.Hashtable; |
2 | 32 |
|
33 |
import java.security.*; |
|
34 |
import javax.crypto.*; |
|
35 |
import javax.crypto.spec.IvParameterSpec; |
|
36 |
||
37 |
import java.nio.*; |
|
38 |
||
39 |
import sun.security.ssl.CipherSuite.*; |
|
40 |
import static sun.security.ssl.CipherSuite.*; |
|
41 |
||
42 |
import sun.misc.HexDumpEncoder; |
|
43 |
||
44 |
||
45 |
/** |
|
46 |
* This class handles bulk data enciphering/deciphering for each SSLv3 |
|
47 |
* message. This provides data confidentiality. Stream ciphers (such |
|
48 |
* as RC4) don't need to do padding; block ciphers (e.g. DES) need it. |
|
49 |
* |
|
50 |
* Individual instances are obtained by calling the static method |
|
51 |
* newCipherBox(), which should only be invoked by BulkCipher.newCipher(). |
|
52 |
* |
|
7039 | 53 |
* In RFC 2246, with bock ciphers in CBC mode, the Initialization |
54 |
* Vector (IV) for the first record is generated with the other keys |
|
55 |
* and secrets when the security parameters are set. The IV for |
|
56 |
* subsequent records is the last ciphertext block from the previous |
|
57 |
* record. |
|
58 |
* |
|
59 |
* In RFC 4346, the implicit Initialization Vector (IV) is replaced |
|
60 |
* with an explicit IV to protect against CBC attacks. RFC 4346 |
|
61 |
* recommends two algorithms used to generated the per-record IV. |
|
62 |
* The implementation uses the algorithm (2)(b), as described at |
|
63 |
* section 6.2.3.2 of RFC 4346. |
|
64 |
* |
|
65 |
* The usage of IV in CBC block cipher can be illustrated in |
|
66 |
* the following diagrams. |
|
67 |
* |
|
68 |
* (random) |
|
69 |
* R P1 IV C1 |
|
70 |
* | | | | |
|
71 |
* SIV---+ |-----+ |-... |----- |------ |
|
72 |
* | | | | | | | | |
|
73 |
* +----+ | +----+ | +----+ | +----+ | |
|
74 |
* | Ek | | + Ek + | | Dk | | | Dk | | |
|
75 |
* +----+ | +----+ | +----+ | +----+ | |
|
76 |
* | | | | | | | | |
|
77 |
* |----| |----| SIV--+ |----| |-... |
|
78 |
* | | | | |
|
79 |
* IV C1 R P1 |
|
80 |
* (discard) |
|
81 |
* |
|
82 |
* CBC Encryption CBC Decryption |
|
83 |
* |
|
2 | 84 |
* NOTE that any ciphering involved in key exchange (e.g. with RSA) is |
85 |
* handled separately. |
|
86 |
* |
|
87 |
* @author David Brownell |
|
88 |
* @author Andreas Sterbenz |
|
89 |
*/ |
|
90 |
final class CipherBox { |
|
91 |
||
92 |
// A CipherBox that implements the identity operation |
|
93 |
final static CipherBox NULL = new CipherBox(); |
|
94 |
||
95 |
/* Class and subclass dynamic debugging support */ |
|
96 |
private static final Debug debug = Debug.getInstance("ssl"); |
|
97 |
||
98 |
// the protocol version this cipher conforms to |
|
99 |
private final ProtocolVersion protocolVersion; |
|
100 |
||
101 |
// cipher object |
|
102 |
private final Cipher cipher; |
|
103 |
||
104 |
/** |
|
105 |
* Cipher blocksize, 0 for stream ciphers |
|
106 |
*/ |
|
107 |
private int blockSize; |
|
108 |
||
109 |
/** |
|
7039 | 110 |
* secure random |
111 |
*/ |
|
112 |
private SecureRandom random; |
|
113 |
||
114 |
/** |
|
10915 | 115 |
* Is the cipher of CBC mode? |
116 |
*/ |
|
117 |
private final boolean isCBCMode; |
|
118 |
||
119 |
/** |
|
7039 | 120 |
* Fixed masks of various block size, as the initial decryption IVs |
121 |
* for TLS 1.1 or later. |
|
122 |
* |
|
123 |
* For performance, we do not use random IVs. As the initial decryption |
|
124 |
* IVs will be discarded by TLS decryption processes, so the fixed masks |
|
125 |
* do not hurt cryptographic strength. |
|
126 |
*/ |
|
127 |
private static Hashtable<Integer, IvParameterSpec> masks; |
|
128 |
||
129 |
/** |
|
2 | 130 |
* NULL cipherbox. Identity operation, no encryption. |
131 |
*/ |
|
132 |
private CipherBox() { |
|
133 |
this.protocolVersion = ProtocolVersion.DEFAULT; |
|
134 |
this.cipher = null; |
|
10915 | 135 |
this.isCBCMode = false; |
2 | 136 |
} |
137 |
||
138 |
/** |
|
139 |
* Construct a new CipherBox using the cipher transformation. |
|
140 |
* |
|
141 |
* @exception NoSuchAlgorithmException if no appropriate JCE Cipher |
|
142 |
* implementation could be found. |
|
143 |
*/ |
|
144 |
private CipherBox(ProtocolVersion protocolVersion, BulkCipher bulkCipher, |
|
7039 | 145 |
SecretKey key, IvParameterSpec iv, SecureRandom random, |
146 |
boolean encrypt) throws NoSuchAlgorithmException { |
|
2 | 147 |
try { |
148 |
this.protocolVersion = protocolVersion; |
|
149 |
this.cipher = JsseJce.getCipher(bulkCipher.transformation); |
|
150 |
int mode = encrypt ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE; |
|
7039 | 151 |
|
152 |
if (random == null) { |
|
153 |
random = JsseJce.getSecureRandom(); |
|
154 |
} |
|
155 |
this.random = random; |
|
10915 | 156 |
this.isCBCMode = bulkCipher.isCBCMode; |
7039 | 157 |
|
158 |
/* |
|
159 |
* RFC 4346 recommends two algorithms used to generated the |
|
160 |
* per-record IV. The implementation uses the algorithm (2)(b), |
|
161 |
* as described at section 6.2.3.2 of RFC 4346. |
|
162 |
* |
|
163 |
* As we don't care about the initial IV value for TLS 1.1 or |
|
164 |
* later, so if the "iv" parameter is null, we use the default |
|
165 |
* value generated by Cipher.init() for encryption, and a fixed |
|
166 |
* mask for decryption. |
|
167 |
*/ |
|
168 |
if (iv == null && bulkCipher.ivSize != 0 && |
|
169 |
mode == Cipher.DECRYPT_MODE && |
|
170 |
protocolVersion.v >= ProtocolVersion.TLS11.v) { |
|
171 |
iv = getFixedMask(bulkCipher.ivSize); |
|
172 |
} |
|
173 |
||
174 |
cipher.init(mode, key, iv, random); |
|
175 |
||
176 |
// Do not call getBlockSize until after init() |
|
2 | 177 |
// otherwise we would disrupt JCE delayed provider selection |
178 |
blockSize = cipher.getBlockSize(); |
|
179 |
// some providers implement getBlockSize() incorrectly |
|
180 |
if (blockSize == 1) { |
|
181 |
blockSize = 0; |
|
182 |
} |
|
183 |
} catch (NoSuchAlgorithmException e) { |
|
184 |
throw e; |
|
185 |
} catch (Exception e) { |
|
186 |
throw new NoSuchAlgorithmException |
|
187 |
("Could not create cipher " + bulkCipher, e); |
|
188 |
} catch (ExceptionInInitializerError e) { |
|
189 |
throw new NoSuchAlgorithmException |
|
190 |
("Could not create cipher " + bulkCipher, e); |
|
191 |
} |
|
192 |
} |
|
193 |
||
194 |
/* |
|
195 |
* Factory method to obtain a new CipherBox object. |
|
196 |
*/ |
|
197 |
static CipherBox newCipherBox(ProtocolVersion version, BulkCipher cipher, |
|
7039 | 198 |
SecretKey key, IvParameterSpec iv, SecureRandom random, |
199 |
boolean encrypt) throws NoSuchAlgorithmException { |
|
2 | 200 |
if (cipher.allowed == false) { |
201 |
throw new NoSuchAlgorithmException("Unsupported cipher " + cipher); |
|
202 |
} |
|
7039 | 203 |
|
2 | 204 |
if (cipher == B_NULL) { |
205 |
return NULL; |
|
206 |
} else { |
|
7039 | 207 |
return new CipherBox(version, cipher, key, iv, random, encrypt); |
2 | 208 |
} |
209 |
} |
|
210 |
||
211 |
/* |
|
7039 | 212 |
* Get a fixed mask, as the initial decryption IVs for TLS 1.1 or later. |
213 |
*/ |
|
214 |
private static IvParameterSpec getFixedMask(int ivSize) { |
|
215 |
if (masks == null) { |
|
216 |
masks = new Hashtable<Integer, IvParameterSpec>(5); |
|
217 |
} |
|
218 |
||
219 |
IvParameterSpec iv = masks.get(ivSize); |
|
220 |
if (iv == null) { |
|
221 |
iv = new IvParameterSpec(new byte[ivSize]); |
|
222 |
masks.put(ivSize, iv); |
|
223 |
} |
|
224 |
||
225 |
return iv; |
|
226 |
} |
|
227 |
||
228 |
/* |
|
2 | 229 |
* Encrypts a block of data, returning the size of the |
230 |
* resulting block. |
|
231 |
*/ |
|
232 |
int encrypt(byte[] buf, int offset, int len) { |
|
233 |
if (cipher == null) { |
|
234 |
return len; |
|
235 |
} |
|
7039 | 236 |
|
2 | 237 |
try { |
238 |
if (blockSize != 0) { |
|
7039 | 239 |
// TLSv1.1 needs a IV block |
240 |
if (protocolVersion.v >= ProtocolVersion.TLS11.v) { |
|
241 |
// generate a random number |
|
242 |
byte[] prefix = new byte[blockSize]; |
|
243 |
random.nextBytes(prefix); |
|
244 |
||
245 |
// move forward the plaintext |
|
246 |
System.arraycopy(buf, offset, |
|
247 |
buf, offset + prefix.length, len); |
|
248 |
||
249 |
// prefix the plaintext |
|
250 |
System.arraycopy(prefix, 0, |
|
251 |
buf, offset, prefix.length); |
|
252 |
||
253 |
len += prefix.length; |
|
254 |
} |
|
255 |
||
2 | 256 |
len = addPadding(buf, offset, len, blockSize); |
257 |
} |
|
258 |
if (debug != null && Debug.isOn("plaintext")) { |
|
259 |
try { |
|
260 |
HexDumpEncoder hd = new HexDumpEncoder(); |
|
261 |
||
262 |
System.out.println( |
|
263 |
"Padded plaintext before ENCRYPTION: len = " |
|
264 |
+ len); |
|
265 |
hd.encodeBuffer( |
|
266 |
new ByteArrayInputStream(buf, offset, len), |
|
267 |
System.out); |
|
268 |
} catch (IOException e) { } |
|
269 |
} |
|
270 |
int newLen = cipher.update(buf, offset, len, buf, offset); |
|
271 |
if (newLen != len) { |
|
272 |
// catch BouncyCastle buffering error |
|
273 |
throw new RuntimeException("Cipher buffering error " + |
|
274 |
"in JCE provider " + cipher.getProvider().getName()); |
|
275 |
} |
|
276 |
return newLen; |
|
277 |
} catch (ShortBufferException e) { |
|
278 |
throw new ArrayIndexOutOfBoundsException(e.toString()); |
|
279 |
} |
|
280 |
} |
|
281 |
||
282 |
/* |
|
283 |
* Encrypts a ByteBuffer block of data, returning the size of the |
|
284 |
* resulting block. |
|
285 |
* |
|
286 |
* The byte buffers position and limit initially define the amount |
|
287 |
* to encrypt. On return, the position and limit are |
|
288 |
* set to last position padded/encrypted. The limit may have changed |
|
289 |
* because of the added padding bytes. |
|
290 |
*/ |
|
291 |
int encrypt(ByteBuffer bb) { |
|
292 |
||
293 |
int len = bb.remaining(); |
|
294 |
||
295 |
if (cipher == null) { |
|
296 |
bb.position(bb.limit()); |
|
297 |
return len; |
|
298 |
} |
|
299 |
||
300 |
try { |
|
301 |
int pos = bb.position(); |
|
302 |
||
303 |
if (blockSize != 0) { |
|
7039 | 304 |
// TLSv1.1 needs a IV block |
305 |
if (protocolVersion.v >= ProtocolVersion.TLS11.v) { |
|
306 |
// generate a random number |
|
307 |
byte[] prefix = new byte[blockSize]; |
|
308 |
random.nextBytes(prefix); |
|
309 |
||
310 |
// move forward the plaintext |
|
311 |
byte[] buf = null; |
|
312 |
int limit = bb.limit(); |
|
313 |
if (bb.hasArray()) { |
|
10787
717ef54d7dd3
7031830: bad_record_mac failure on TLSv1.2 enabled connection with SSLEngine
wetmore
parents:
7039
diff
changeset
|
314 |
int arrayOffset = bb.arrayOffset(); |
7039 | 315 |
buf = bb.array(); |
10787
717ef54d7dd3
7031830: bad_record_mac failure on TLSv1.2 enabled connection with SSLEngine
wetmore
parents:
7039
diff
changeset
|
316 |
System.arraycopy(buf, arrayOffset + pos, |
717ef54d7dd3
7031830: bad_record_mac failure on TLSv1.2 enabled connection with SSLEngine
wetmore
parents:
7039
diff
changeset
|
317 |
buf, arrayOffset + pos + prefix.length, |
717ef54d7dd3
7031830: bad_record_mac failure on TLSv1.2 enabled connection with SSLEngine
wetmore
parents:
7039
diff
changeset
|
318 |
limit - pos); |
7039 | 319 |
bb.limit(limit + prefix.length); |
320 |
} else { |
|
321 |
buf = new byte[limit - pos]; |
|
322 |
bb.get(buf, 0, limit - pos); |
|
323 |
bb.position(pos + prefix.length); |
|
324 |
bb.limit(limit + prefix.length); |
|
325 |
bb.put(buf); |
|
326 |
} |
|
327 |
bb.position(pos); |
|
328 |
||
329 |
// prefix the plaintext |
|
330 |
bb.put(prefix); |
|
331 |
bb.position(pos); |
|
332 |
} |
|
333 |
||
2 | 334 |
// addPadding adjusts pos/limit |
335 |
len = addPadding(bb, blockSize); |
|
336 |
bb.position(pos); |
|
337 |
} |
|
338 |
if (debug != null && Debug.isOn("plaintext")) { |
|
339 |
try { |
|
340 |
HexDumpEncoder hd = new HexDumpEncoder(); |
|
341 |
||
342 |
System.out.println( |
|
343 |
"Padded plaintext before ENCRYPTION: len = " |
|
344 |
+ len); |
|
345 |
hd.encodeBuffer(bb, System.out); |
|
346 |
||
347 |
} catch (IOException e) { } |
|
348 |
/* |
|
349 |
* reset back to beginning |
|
350 |
*/ |
|
351 |
bb.position(pos); |
|
352 |
} |
|
353 |
||
354 |
/* |
|
355 |
* Encrypt "in-place". This does not add its own padding. |
|
356 |
*/ |
|
357 |
ByteBuffer dup = bb.duplicate(); |
|
358 |
int newLen = cipher.update(dup, bb); |
|
359 |
||
360 |
if (bb.position() != dup.position()) { |
|
361 |
throw new RuntimeException("bytebuffer padding error"); |
|
362 |
} |
|
363 |
||
364 |
if (newLen != len) { |
|
365 |
// catch BouncyCastle buffering error |
|
366 |
throw new RuntimeException("Cipher buffering error " + |
|
367 |
"in JCE provider " + cipher.getProvider().getName()); |
|
368 |
} |
|
369 |
return newLen; |
|
370 |
} catch (ShortBufferException e) { |
|
371 |
RuntimeException exc = new RuntimeException(e.toString()); |
|
372 |
exc.initCause(e); |
|
373 |
throw exc; |
|
374 |
} |
|
375 |
} |
|
376 |
||
377 |
||
378 |
/* |
|
379 |
* Decrypts a block of data, returning the size of the |
|
380 |
* resulting block if padding was required. |
|
7039 | 381 |
* |
382 |
* For SSLv3 and TLSv1.0, with block ciphers in CBC mode the |
|
383 |
* Initialization Vector (IV) for the first record is generated by |
|
384 |
* the handshake protocol, the IV for subsequent records is the |
|
385 |
* last ciphertext block from the previous record. |
|
386 |
* |
|
387 |
* From TLSv1.1, the implicit IV is replaced with an explicit IV to |
|
388 |
* protect against CBC attacks. |
|
389 |
* |
|
390 |
* Differentiating between bad_record_mac and decryption_failed alerts |
|
391 |
* may permit certain attacks against CBC mode. It is preferable to |
|
392 |
* uniformly use the bad_record_mac alert to hide the specific type of |
|
393 |
* the error. |
|
2 | 394 |
*/ |
16113 | 395 |
int decrypt(byte[] buf, int offset, int len, |
396 |
int tagLen) throws BadPaddingException { |
|
2 | 397 |
if (cipher == null) { |
398 |
return len; |
|
399 |
} |
|
7039 | 400 |
|
2 | 401 |
try { |
402 |
int newLen = cipher.update(buf, offset, len, buf, offset); |
|
403 |
if (newLen != len) { |
|
404 |
// catch BouncyCastle buffering error |
|
405 |
throw new RuntimeException("Cipher buffering error " + |
|
406 |
"in JCE provider " + cipher.getProvider().getName()); |
|
407 |
} |
|
408 |
if (debug != null && Debug.isOn("plaintext")) { |
|
409 |
try { |
|
410 |
HexDumpEncoder hd = new HexDumpEncoder(); |
|
411 |
||
412 |
System.out.println( |
|
413 |
"Padded plaintext after DECRYPTION: len = " |
|
414 |
+ newLen); |
|
415 |
hd.encodeBuffer( |
|
416 |
new ByteArrayInputStream(buf, offset, newLen), |
|
417 |
System.out); |
|
418 |
} catch (IOException e) { } |
|
419 |
} |
|
16113 | 420 |
|
2 | 421 |
if (blockSize != 0) { |
16113 | 422 |
newLen = removePadding( |
423 |
buf, offset, newLen, tagLen, blockSize, protocolVersion); |
|
7039 | 424 |
|
425 |
if (protocolVersion.v >= ProtocolVersion.TLS11.v) { |
|
426 |
if (newLen < blockSize) { |
|
427 |
throw new BadPaddingException("invalid explicit IV"); |
|
428 |
} |
|
429 |
||
430 |
// discards the first cipher block, the IV component. |
|
431 |
System.arraycopy(buf, offset + blockSize, |
|
432 |
buf, offset, newLen - blockSize); |
|
433 |
||
434 |
newLen -= blockSize; |
|
435 |
} |
|
2 | 436 |
} |
437 |
return newLen; |
|
438 |
} catch (ShortBufferException e) { |
|
439 |
throw new ArrayIndexOutOfBoundsException(e.toString()); |
|
440 |
} |
|
441 |
} |
|
442 |
||
443 |
||
444 |
/* |
|
445 |
* Decrypts a block of data, returning the size of the |
|
446 |
* resulting block if padding was required. position and limit |
|
447 |
* point to the end of the decrypted/depadded data. The initial |
|
448 |
* limit and new limit may be different, given we may |
|
449 |
* have stripped off some padding bytes. |
|
7039 | 450 |
* |
451 |
* @see decrypt(byte[], int, int) |
|
2 | 452 |
*/ |
16113 | 453 |
int decrypt(ByteBuffer bb, int tagLen) throws BadPaddingException { |
2 | 454 |
|
455 |
int len = bb.remaining(); |
|
456 |
||
457 |
if (cipher == null) { |
|
458 |
bb.position(bb.limit()); |
|
459 |
return len; |
|
460 |
} |
|
461 |
||
462 |
try { |
|
463 |
/* |
|
464 |
* Decrypt "in-place". |
|
465 |
*/ |
|
466 |
int pos = bb.position(); |
|
467 |
ByteBuffer dup = bb.duplicate(); |
|
468 |
int newLen = cipher.update(dup, bb); |
|
469 |
if (newLen != len) { |
|
470 |
// catch BouncyCastle buffering error |
|
471 |
throw new RuntimeException("Cipher buffering error " + |
|
472 |
"in JCE provider " + cipher.getProvider().getName()); |
|
473 |
} |
|
474 |
||
475 |
if (debug != null && Debug.isOn("plaintext")) { |
|
476 |
try { |
|
477 |
HexDumpEncoder hd = new HexDumpEncoder(); |
|
478 |
||
479 |
System.out.println( |
|
480 |
"Padded plaintext after DECRYPTION: len = " |
|
481 |
+ newLen); |
|
482 |
||
16113 | 483 |
hd.encodeBuffer( |
484 |
(ByteBuffer)bb.duplicate().position(pos), System.out); |
|
2 | 485 |
} catch (IOException e) { } |
486 |
} |
|
487 |
||
488 |
/* |
|
489 |
* Remove the block padding. |
|
490 |
*/ |
|
491 |
if (blockSize != 0) { |
|
492 |
bb.position(pos); |
|
16113 | 493 |
newLen = removePadding( |
494 |
bb, tagLen, blockSize, protocolVersion); |
|
7039 | 495 |
|
496 |
if (protocolVersion.v >= ProtocolVersion.TLS11.v) { |
|
497 |
if (newLen < blockSize) { |
|
498 |
throw new BadPaddingException("invalid explicit IV"); |
|
499 |
} |
|
500 |
||
501 |
// discards the first cipher block, the IV component. |
|
502 |
byte[] buf = null; |
|
503 |
int limit = bb.limit(); |
|
504 |
if (bb.hasArray()) { |
|
10787
717ef54d7dd3
7031830: bad_record_mac failure on TLSv1.2 enabled connection with SSLEngine
wetmore
parents:
7039
diff
changeset
|
505 |
int arrayOffset = bb.arrayOffset(); |
7039 | 506 |
buf = bb.array(); |
10787
717ef54d7dd3
7031830: bad_record_mac failure on TLSv1.2 enabled connection with SSLEngine
wetmore
parents:
7039
diff
changeset
|
507 |
System.arraycopy(buf, arrayOffset + pos + blockSize, |
717ef54d7dd3
7031830: bad_record_mac failure on TLSv1.2 enabled connection with SSLEngine
wetmore
parents:
7039
diff
changeset
|
508 |
buf, arrayOffset + pos, limit - pos - blockSize); |
7039 | 509 |
bb.limit(limit - blockSize); |
510 |
} else { |
|
511 |
buf = new byte[limit - pos - blockSize]; |
|
512 |
bb.position(pos + blockSize); |
|
513 |
bb.get(buf); |
|
514 |
bb.position(pos); |
|
515 |
bb.put(buf); |
|
516 |
bb.limit(limit - blockSize); |
|
517 |
} |
|
518 |
||
519 |
// reset the position to the end of the decrypted data |
|
520 |
limit = bb.limit(); |
|
521 |
bb.position(limit); |
|
522 |
} |
|
2 | 523 |
} |
524 |
return newLen; |
|
525 |
} catch (ShortBufferException e) { |
|
526 |
RuntimeException exc = new RuntimeException(e.toString()); |
|
527 |
exc.initCause(e); |
|
528 |
throw exc; |
|
529 |
} |
|
530 |
} |
|
531 |
||
532 |
private static int addPadding(byte[] buf, int offset, int len, |
|
533 |
int blockSize) { |
|
534 |
int newlen = len + 1; |
|
535 |
byte pad; |
|
536 |
int i; |
|
537 |
||
538 |
if ((newlen % blockSize) != 0) { |
|
539 |
newlen += blockSize - 1; |
|
540 |
newlen -= newlen % blockSize; |
|
541 |
} |
|
542 |
pad = (byte) (newlen - len); |
|
543 |
||
544 |
if (buf.length < (newlen + offset)) { |
|
545 |
throw new IllegalArgumentException("no space to pad buffer"); |
|
546 |
} |
|
547 |
||
548 |
/* |
|
549 |
* TLS version of the padding works for both SSLv3 and TLSv1 |
|
550 |
*/ |
|
551 |
for (i = 0, offset += len; i < pad; i++) { |
|
552 |
buf [offset++] = (byte) (pad - 1); |
|
553 |
} |
|
554 |
return newlen; |
|
555 |
} |
|
556 |
||
557 |
/* |
|
558 |
* Apply the padding to the buffer. |
|
559 |
* |
|
560 |
* Limit is advanced to the new buffer length. |
|
561 |
* Position is equal to limit. |
|
562 |
*/ |
|
563 |
private static int addPadding(ByteBuffer bb, int blockSize) { |
|
564 |
||
565 |
int len = bb.remaining(); |
|
566 |
int offset = bb.position(); |
|
567 |
||
568 |
int newlen = len + 1; |
|
569 |
byte pad; |
|
570 |
int i; |
|
571 |
||
572 |
if ((newlen % blockSize) != 0) { |
|
573 |
newlen += blockSize - 1; |
|
574 |
newlen -= newlen % blockSize; |
|
575 |
} |
|
576 |
pad = (byte) (newlen - len); |
|
577 |
||
578 |
/* |
|
579 |
* Update the limit to what will be padded. |
|
580 |
*/ |
|
581 |
bb.limit(newlen + offset); |
|
582 |
||
583 |
/* |
|
584 |
* TLS version of the padding works for both SSLv3 and TLSv1 |
|
585 |
*/ |
|
586 |
for (i = 0, offset += len; i < pad; i++) { |
|
587 |
bb.put(offset++, (byte) (pad - 1)); |
|
588 |
} |
|
589 |
||
590 |
bb.position(offset); |
|
591 |
bb.limit(offset); |
|
592 |
||
593 |
return newlen; |
|
594 |
} |
|
595 |
||
16113 | 596 |
/* |
597 |
* A constant-time check of the padding. |
|
598 |
* |
|
599 |
* NOTE that we are checking both the padding and the padLen bytes here. |
|
600 |
* |
|
601 |
* The caller MUST ensure that the len parameter is a positive number. |
|
602 |
*/ |
|
603 |
private static int[] checkPadding( |
|
604 |
byte[] buf, int offset, int len, byte pad) { |
|
605 |
||
606 |
if (len <= 0) { |
|
607 |
throw new RuntimeException("padding len must be positive"); |
|
608 |
} |
|
609 |
||
610 |
// An array of hits is used to prevent Hotspot optimization for |
|
611 |
// the purpose of a constant-time check. |
|
612 |
int[] results = {0, 0}; // {missed #, matched #} |
|
613 |
for (int i = 0; i <= 256;) { |
|
614 |
for (int j = 0; j < len && i <= 256; j++, i++) { // j <= i |
|
615 |
if (buf[offset + j] != pad) { |
|
616 |
results[0]++; // mismatched padding data |
|
617 |
} else { |
|
618 |
results[1]++; // matched padding data |
|
619 |
} |
|
620 |
} |
|
621 |
} |
|
622 |
||
623 |
return results; |
|
624 |
} |
|
625 |
||
626 |
/* |
|
627 |
* A constant-time check of the padding. |
|
628 |
* |
|
629 |
* NOTE that we are checking both the padding and the padLen bytes here. |
|
630 |
* |
|
631 |
* The caller MUST ensure that the bb parameter has remaining. |
|
632 |
*/ |
|
633 |
private static int[] checkPadding(ByteBuffer bb, byte pad) { |
|
634 |
||
635 |
if (!bb.hasRemaining()) { |
|
636 |
throw new RuntimeException("hasRemaining() must be positive"); |
|
637 |
} |
|
638 |
||
639 |
// An array of hits is used to prevent Hotspot optimization for |
|
640 |
// the purpose of a constant-time check. |
|
641 |
int[] results = {0, 0}; // {missed #, matched #} |
|
642 |
bb.mark(); |
|
643 |
for (int i = 0; i <= 256; bb.reset()) { |
|
644 |
for (; bb.hasRemaining() && i <= 256; i++) { |
|
645 |
if (bb.get() != pad) { |
|
646 |
results[0]++; // mismatched padding data |
|
647 |
} else { |
|
648 |
results[1]++; // matched padding data |
|
649 |
} |
|
650 |
} |
|
651 |
} |
|
652 |
||
653 |
return results; |
|
654 |
} |
|
2 | 655 |
|
656 |
/* |
|
657 |
* Typical TLS padding format for a 64 bit block cipher is as follows: |
|
658 |
* xx xx xx xx xx xx xx 00 |
|
659 |
* xx xx xx xx xx xx 01 01 |
|
660 |
* ... |
|
661 |
* xx 06 06 06 06 06 06 06 |
|
662 |
* 07 07 07 07 07 07 07 07 |
|
663 |
* TLS also allows any amount of padding from 1 and 256 bytes as long |
|
664 |
* as it makes the data a multiple of the block size |
|
665 |
*/ |
|
666 |
private static int removePadding(byte[] buf, int offset, int len, |
|
16113 | 667 |
int tagLen, int blockSize, |
668 |
ProtocolVersion protocolVersion) throws BadPaddingException { |
|
669 |
||
2 | 670 |
// last byte is length byte (i.e. actual padding length - 1) |
671 |
int padOffset = offset + len - 1; |
|
16113 | 672 |
int padLen = buf[padOffset] & 0xFF; |
2 | 673 |
|
16113 | 674 |
int newLen = len - (padLen + 1); |
675 |
if ((newLen - tagLen) < 0) { |
|
676 |
// If the buffer is not long enough to contain the padding plus |
|
677 |
// a MAC tag, do a dummy constant-time padding check. |
|
678 |
// |
|
679 |
// Note that it is a dummy check, so we won't care about what is |
|
680 |
// the actual padding data. |
|
681 |
checkPadding(buf, offset, len, (byte)(padLen & 0xFF)); |
|
682 |
||
683 |
throw new BadPaddingException("Invalid Padding length: " + padLen); |
|
2 | 684 |
} |
685 |
||
16113 | 686 |
// The padding data should be filled with the padding length value. |
687 |
int[] results = checkPadding(buf, offset + newLen, |
|
688 |
padLen + 1, (byte)(padLen & 0xFF)); |
|
2 | 689 |
if (protocolVersion.v >= ProtocolVersion.TLS10.v) { |
16113 | 690 |
if (results[0] != 0) { // padding data has invalid bytes |
691 |
throw new BadPaddingException("Invalid TLS padding data"); |
|
2 | 692 |
} |
693 |
} else { // SSLv3 |
|
694 |
// SSLv3 requires 0 <= length byte < block size |
|
695 |
// some implementations do 1 <= length byte <= block size, |
|
696 |
// so accept that as well |
|
697 |
// v3 does not require any particular value for the other bytes |
|
16113 | 698 |
if (padLen > blockSize) { |
699 |
throw new BadPaddingException("Invalid SSLv3 padding"); |
|
2 | 700 |
} |
701 |
} |
|
16113 | 702 |
return newLen; |
2 | 703 |
} |
704 |
||
705 |
/* |
|
706 |
* Position/limit is equal the removed padding. |
|
707 |
*/ |
|
708 |
private static int removePadding(ByteBuffer bb, |
|
16113 | 709 |
int tagLen, int blockSize, |
710 |
ProtocolVersion protocolVersion) throws BadPaddingException { |
|
2 | 711 |
|
712 |
int len = bb.remaining(); |
|
713 |
int offset = bb.position(); |
|
714 |
||
715 |
// last byte is length byte (i.e. actual padding length - 1) |
|
716 |
int padOffset = offset + len - 1; |
|
16113 | 717 |
int padLen = bb.get(padOffset) & 0xFF; |
2 | 718 |
|
16113 | 719 |
int newLen = len - (padLen + 1); |
720 |
if ((newLen - tagLen) < 0) { |
|
721 |
// If the buffer is not long enough to contain the padding plus |
|
722 |
// a MAC tag, do a dummy constant-time padding check. |
|
723 |
// |
|
724 |
// Note that it is a dummy check, so we won't care about what is |
|
725 |
// the actual padding data. |
|
726 |
checkPadding(bb.duplicate(), (byte)(padLen & 0xFF)); |
|
727 |
||
728 |
throw new BadPaddingException("Invalid Padding length: " + padLen); |
|
2 | 729 |
} |
730 |
||
16113 | 731 |
// The padding data should be filled with the padding length value. |
732 |
int[] results = checkPadding( |
|
733 |
(ByteBuffer)bb.duplicate().position(offset + newLen), |
|
734 |
(byte)(padLen & 0xFF)); |
|
2 | 735 |
if (protocolVersion.v >= ProtocolVersion.TLS10.v) { |
16113 | 736 |
if (results[0] != 0) { // padding data has invalid bytes |
737 |
throw new BadPaddingException("Invalid TLS padding data"); |
|
2 | 738 |
} |
739 |
} else { // SSLv3 |
|
740 |
// SSLv3 requires 0 <= length byte < block size |
|
741 |
// some implementations do 1 <= length byte <= block size, |
|
742 |
// so accept that as well |
|
743 |
// v3 does not require any particular value for the other bytes |
|
16113 | 744 |
if (padLen > blockSize) { |
745 |
throw new BadPaddingException("Invalid SSLv3 padding"); |
|
2 | 746 |
} |
747 |
} |
|
748 |
||
749 |
/* |
|
750 |
* Reset buffer limit to remove padding. |
|
751 |
*/ |
|
16113 | 752 |
bb.position(offset + newLen); |
753 |
bb.limit(offset + newLen); |
|
2 | 754 |
|
16113 | 755 |
return newLen; |
2 | 756 |
} |
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
|
757 |
|
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
|
758 |
/* |
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
|
759 |
* 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
|
760 |
* For PKCS11 ciphers, this will release any attached sessions, and |
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
|
761 |
* 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
|
762 |
*/ |
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
|
763 |
void 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
|
764 |
try { |
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
|
765 |
if (cipher != null) { |
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
|
766 |
// ignore return value. |
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
|
767 |
cipher.doFinal(); |
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
|
768 |
} |
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
|
769 |
} catch (GeneralSecurityException e) { |
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
|
770 |
// swallow for now. |
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
|
771 |
} |
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
|
772 |
} |
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
|
773 |
|
10915 | 774 |
/* |
775 |
* Does the cipher use CBC mode? |
|
776 |
* |
|
777 |
* @return true if the cipher use CBC mode, false otherwise. |
|
778 |
*/ |
|
779 |
boolean isCBCMode() { |
|
780 |
return isCBCMode; |
|
781 |
} |
|
16113 | 782 |
|
783 |
/** |
|
784 |
* Is the cipher null? |
|
785 |
* |
|
786 |
* @return true if the cipher is null, false otherwise. |
|
787 |
*/ |
|
788 |
boolean isNullCipher() { |
|
789 |
return cipher == null; |
|
790 |
} |
|
791 |
||
792 |
/** |
|
793 |
* Sanity check the length of a fragment before decryption. |
|
794 |
* |
|
795 |
* In CBC mode, check that the fragment length is one or multiple times |
|
796 |
* of the block size of the cipher suite, and is at least one (one is the |
|
797 |
* smallest size of padding in CBC mode) bigger than the tag size of the |
|
798 |
* MAC algorithm except the explicit IV size for TLS 1.1 or later. |
|
799 |
* |
|
800 |
* In non-CBC mode, check that the fragment length is not less than the |
|
801 |
* tag size of the MAC algorithm. |
|
802 |
* |
|
803 |
* @return true if the length of a fragment matches above requirements |
|
804 |
*/ |
|
805 |
boolean sanityCheck(int tagLen, int fragmentLen) { |
|
806 |
if (!isCBCMode) { |
|
807 |
return fragmentLen >= tagLen; |
|
808 |
} |
|
809 |
||
810 |
if ((fragmentLen % blockSize) == 0) { |
|
811 |
int minimal = tagLen + 1; |
|
812 |
minimal = (minimal >= blockSize) ? minimal : blockSize; |
|
813 |
if (protocolVersion.v >= ProtocolVersion.TLS11.v) { |
|
814 |
minimal += blockSize; // plus the size of the explicit IV |
|
815 |
} |
|
816 |
||
817 |
return (fragmentLen >= minimal); |
|
818 |
} |
|
819 |
||
820 |
return false; |
|
821 |
} |
|
822 |
||
2 | 823 |
} |