author | wetmore |
Tue, 12 Mar 2013 15:31:49 -0700 | |
changeset 16067 | 36055e4b5305 |
parent 16045 | 9d08c3b9a6a0 |
child 16126 | aad71cf676d7 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
16067 | 2 |
* Copyright (c) 1996, 2011, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
||
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 |
/** |
|
16067 | 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 |
/** |
|
16067 | 115 |
* Is the cipher of CBC mode? |
10915 | 116 |
*/ |
16067 | 117 |
private final boolean isCBCMode; |
10915 | 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; |
|
16067 | 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); |
|
16067 | 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; |
|
16067 | 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 |
||
16067 | 174 |
cipher.init(mode, key, iv, random); |
7039 | 175 |
|
16067 | 176 |
// Do not call getBlockSize until after init() |
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; |
|
2 | 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 { |
16067 | 238 |
if (blockSize != 0) { |
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 |
} |
|
16067 | 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()); |
|
2 | 275 |
} |
16067 | 276 |
return newLen; |
2 | 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 |
*/ |
|
16067 | 291 |
int encrypt(ByteBuffer bb) { |
2 | 292 |
|
293 |
int len = bb.remaining(); |
|
294 |
||
295 |
if (cipher == null) { |
|
296 |
bb.position(bb.limit()); |
|
297 |
return len; |
|
298 |
} |
|
299 |
||
16067 | 300 |
try { |
301 |
int pos = bb.position(); |
|
2 | 302 |
|
16067 | 303 |
if (blockSize != 0) { |
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); |
|
7039 | 309 |
|
16067 | 310 |
// move forward the plaintext |
311 |
byte[] buf = null; |
|
312 |
int limit = bb.limit(); |
|
313 |
if (bb.hasArray()) { |
|
314 |
int arrayOffset = bb.arrayOffset(); |
|
315 |
buf = bb.array(); |
|
316 |
System.arraycopy(buf, arrayOffset + pos, |
|
317 |
buf, arrayOffset + pos + prefix.length, |
|
318 |
limit - pos); |
|
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); |
|
7039 | 328 |
|
16067 | 329 |
// prefix the plaintext |
330 |
bb.put(prefix); |
|
331 |
bb.position(pos); |
|
7039 | 332 |
} |
16067 | 333 |
|
334 |
// addPadding adjusts pos/limit |
|
335 |
len = addPadding(bb, blockSize); |
|
336 |
bb.position(pos); |
|
2 | 337 |
} |
16067 | 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); |
|
2 | 352 |
} |
353 |
||
16067 | 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 |
||
2 | 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; |
|
16067 | 370 |
} catch (ShortBufferException e) { |
371 |
RuntimeException exc = new RuntimeException(e.toString()); |
|
372 |
exc.initCause(e); |
|
373 |
throw exc; |
|
2 | 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 |
*/ |
395 |
int decrypt(byte[] buf, int offset, int len) throws BadPaddingException { |
|
396 |
if (cipher == null) { |
|
397 |
return len; |
|
398 |
} |
|
7039 | 399 |
|
2 | 400 |
try { |
16067 | 401 |
int newLen = cipher.update(buf, offset, len, buf, offset); |
402 |
if (newLen != len) { |
|
403 |
// catch BouncyCastle buffering error |
|
404 |
throw new RuntimeException("Cipher buffering error " + |
|
405 |
"in JCE provider " + cipher.getProvider().getName()); |
|
2 | 406 |
} |
407 |
if (debug != null && Debug.isOn("plaintext")) { |
|
408 |
try { |
|
409 |
HexDumpEncoder hd = new HexDumpEncoder(); |
|
410 |
||
411 |
System.out.println( |
|
412 |
"Padded plaintext after DECRYPTION: len = " |
|
413 |
+ newLen); |
|
414 |
hd.encodeBuffer( |
|
415 |
new ByteArrayInputStream(buf, offset, newLen), |
|
416 |
System.out); |
|
417 |
} catch (IOException e) { } |
|
418 |
} |
|
16067 | 419 |
if (blockSize != 0) { |
2 | 420 |
newLen = removePadding(buf, offset, newLen, |
421 |
blockSize, protocolVersion); |
|
7039 | 422 |
|
423 |
if (protocolVersion.v >= ProtocolVersion.TLS11.v) { |
|
424 |
if (newLen < blockSize) { |
|
425 |
throw new BadPaddingException("invalid explicit IV"); |
|
426 |
} |
|
16067 | 427 |
|
428 |
// discards the first cipher block, the IV component. |
|
429 |
System.arraycopy(buf, offset + blockSize, |
|
430 |
buf, offset, newLen - blockSize); |
|
431 |
||
432 |
newLen -= blockSize; |
|
7039 | 433 |
} |
2 | 434 |
} |
435 |
return newLen; |
|
436 |
} catch (ShortBufferException e) { |
|
437 |
throw new ArrayIndexOutOfBoundsException(e.toString()); |
|
438 |
} |
|
439 |
} |
|
440 |
||
441 |
||
442 |
/* |
|
443 |
* Decrypts a block of data, returning the size of the |
|
444 |
* resulting block if padding was required. position and limit |
|
445 |
* point to the end of the decrypted/depadded data. The initial |
|
446 |
* limit and new limit may be different, given we may |
|
447 |
* have stripped off some padding bytes. |
|
7039 | 448 |
* |
449 |
* @see decrypt(byte[], int, int) |
|
2 | 450 |
*/ |
451 |
int decrypt(ByteBuffer bb) throws BadPaddingException { |
|
452 |
||
453 |
int len = bb.remaining(); |
|
454 |
||
455 |
if (cipher == null) { |
|
456 |
bb.position(bb.limit()); |
|
457 |
return len; |
|
458 |
} |
|
459 |
||
460 |
try { |
|
461 |
/* |
|
462 |
* Decrypt "in-place". |
|
463 |
*/ |
|
464 |
int pos = bb.position(); |
|
465 |
ByteBuffer dup = bb.duplicate(); |
|
16067 | 466 |
int newLen = cipher.update(dup, bb); |
467 |
if (newLen != len) { |
|
468 |
// catch BouncyCastle buffering error |
|
469 |
throw new RuntimeException("Cipher buffering error " + |
|
470 |
"in JCE provider " + cipher.getProvider().getName()); |
|
2 | 471 |
} |
472 |
||
473 |
if (debug != null && Debug.isOn("plaintext")) { |
|
16067 | 474 |
bb.position(pos); |
2 | 475 |
try { |
476 |
HexDumpEncoder hd = new HexDumpEncoder(); |
|
477 |
||
478 |
System.out.println( |
|
479 |
"Padded plaintext after DECRYPTION: len = " |
|
480 |
+ newLen); |
|
481 |
||
16067 | 482 |
hd.encodeBuffer(bb, System.out); |
2 | 483 |
} catch (IOException e) { } |
484 |
} |
|
485 |
||
486 |
/* |
|
487 |
* Remove the block padding. |
|
488 |
*/ |
|
16067 | 489 |
if (blockSize != 0) { |
2 | 490 |
bb.position(pos); |
491 |
newLen = removePadding(bb, blockSize, protocolVersion); |
|
7039 | 492 |
|
493 |
if (protocolVersion.v >= ProtocolVersion.TLS11.v) { |
|
494 |
if (newLen < blockSize) { |
|
495 |
throw new BadPaddingException("invalid explicit IV"); |
|
496 |
} |
|
497 |
||
16067 | 498 |
// discards the first cipher block, the IV component. |
499 |
byte[] buf = null; |
|
500 |
int limit = bb.limit(); |
|
501 |
if (bb.hasArray()) { |
|
502 |
int arrayOffset = bb.arrayOffset(); |
|
503 |
buf = bb.array(); |
|
504 |
System.arraycopy(buf, arrayOffset + pos + blockSize, |
|
505 |
buf, arrayOffset + pos, limit - pos - blockSize); |
|
506 |
bb.limit(limit - blockSize); |
|
507 |
} else { |
|
508 |
buf = new byte[limit - pos - blockSize]; |
|
509 |
bb.position(pos + blockSize); |
|
510 |
bb.get(buf); |
|
511 |
bb.position(pos); |
|
512 |
bb.put(buf); |
|
513 |
bb.limit(limit - blockSize); |
|
514 |
} |
|
515 |
||
7039 | 516 |
// reset the position to the end of the decrypted data |
16067 | 517 |
limit = bb.limit(); |
518 |
bb.position(limit); |
|
7039 | 519 |
} |
2 | 520 |
} |
521 |
return newLen; |
|
522 |
} catch (ShortBufferException e) { |
|
16067 | 523 |
RuntimeException exc = new RuntimeException(e.toString()); |
524 |
exc.initCause(e); |
|
525 |
throw exc; |
|
2 | 526 |
} |
527 |
} |
|
528 |
||
529 |
private static int addPadding(byte[] buf, int offset, int len, |
|
530 |
int blockSize) { |
|
531 |
int newlen = len + 1; |
|
532 |
byte pad; |
|
533 |
int i; |
|
534 |
||
535 |
if ((newlen % blockSize) != 0) { |
|
536 |
newlen += blockSize - 1; |
|
537 |
newlen -= newlen % blockSize; |
|
538 |
} |
|
539 |
pad = (byte) (newlen - len); |
|
540 |
||
541 |
if (buf.length < (newlen + offset)) { |
|
542 |
throw new IllegalArgumentException("no space to pad buffer"); |
|
543 |
} |
|
544 |
||
545 |
/* |
|
546 |
* TLS version of the padding works for both SSLv3 and TLSv1 |
|
547 |
*/ |
|
548 |
for (i = 0, offset += len; i < pad; i++) { |
|
549 |
buf [offset++] = (byte) (pad - 1); |
|
550 |
} |
|
551 |
return newlen; |
|
552 |
} |
|
553 |
||
554 |
/* |
|
555 |
* Apply the padding to the buffer. |
|
556 |
* |
|
557 |
* Limit is advanced to the new buffer length. |
|
558 |
* Position is equal to limit. |
|
559 |
*/ |
|
560 |
private static int addPadding(ByteBuffer bb, int blockSize) { |
|
561 |
||
562 |
int len = bb.remaining(); |
|
563 |
int offset = bb.position(); |
|
564 |
||
565 |
int newlen = len + 1; |
|
566 |
byte pad; |
|
567 |
int i; |
|
568 |
||
569 |
if ((newlen % blockSize) != 0) { |
|
570 |
newlen += blockSize - 1; |
|
571 |
newlen -= newlen % blockSize; |
|
572 |
} |
|
573 |
pad = (byte) (newlen - len); |
|
574 |
||
575 |
/* |
|
576 |
* Update the limit to what will be padded. |
|
577 |
*/ |
|
578 |
bb.limit(newlen + offset); |
|
579 |
||
580 |
/* |
|
581 |
* TLS version of the padding works for both SSLv3 and TLSv1 |
|
582 |
*/ |
|
583 |
for (i = 0, offset += len; i < pad; i++) { |
|
584 |
bb.put(offset++, (byte) (pad - 1)); |
|
585 |
} |
|
586 |
||
587 |
bb.position(offset); |
|
588 |
bb.limit(offset); |
|
589 |
||
590 |
return newlen; |
|
591 |
} |
|
592 |
||
593 |
||
594 |
/* |
|
595 |
* Typical TLS padding format for a 64 bit block cipher is as follows: |
|
596 |
* xx xx xx xx xx xx xx 00 |
|
597 |
* xx xx xx xx xx xx 01 01 |
|
598 |
* ... |
|
599 |
* xx 06 06 06 06 06 06 06 |
|
600 |
* 07 07 07 07 07 07 07 07 |
|
601 |
* TLS also allows any amount of padding from 1 and 256 bytes as long |
|
602 |
* as it makes the data a multiple of the block size |
|
603 |
*/ |
|
604 |
private static int removePadding(byte[] buf, int offset, int len, |
|
605 |
int blockSize, ProtocolVersion protocolVersion) |
|
606 |
throws BadPaddingException { |
|
607 |
// last byte is length byte (i.e. actual padding length - 1) |
|
608 |
int padOffset = offset + len - 1; |
|
609 |
int pad = buf[padOffset] & 0x0ff; |
|
610 |
||
611 |
int newlen = len - (pad + 1); |
|
612 |
if (newlen < 0) { |
|
613 |
throw new BadPaddingException("Padding length invalid: " + pad); |
|
614 |
} |
|
615 |
||
616 |
if (protocolVersion.v >= ProtocolVersion.TLS10.v) { |
|
617 |
for (int i = 1; i <= pad; i++) { |
|
618 |
int val = buf[padOffset - i] & 0xff; |
|
619 |
if (val != pad) { |
|
620 |
throw new BadPaddingException |
|
621 |
("Invalid TLS padding: " + val); |
|
622 |
} |
|
623 |
} |
|
624 |
} else { // SSLv3 |
|
625 |
// SSLv3 requires 0 <= length byte < block size |
|
626 |
// some implementations do 1 <= length byte <= block size, |
|
627 |
// so accept that as well |
|
628 |
// v3 does not require any particular value for the other bytes |
|
629 |
if (pad > blockSize) { |
|
630 |
throw new BadPaddingException("Invalid SSLv3 padding: " + pad); |
|
631 |
} |
|
632 |
} |
|
633 |
return newlen; |
|
634 |
} |
|
635 |
||
636 |
/* |
|
637 |
* Position/limit is equal the removed padding. |
|
638 |
*/ |
|
639 |
private static int removePadding(ByteBuffer bb, |
|
640 |
int blockSize, ProtocolVersion protocolVersion) |
|
641 |
throws BadPaddingException { |
|
642 |
||
643 |
int len = bb.remaining(); |
|
644 |
int offset = bb.position(); |
|
645 |
||
646 |
// last byte is length byte (i.e. actual padding length - 1) |
|
647 |
int padOffset = offset + len - 1; |
|
648 |
int pad = bb.get(padOffset) & 0x0ff; |
|
649 |
||
650 |
int newlen = len - (pad + 1); |
|
651 |
if (newlen < 0) { |
|
652 |
throw new BadPaddingException("Padding length invalid: " + pad); |
|
653 |
} |
|
654 |
||
655 |
/* |
|
656 |
* We could zero the padding area, but not much useful |
|
657 |
* information there. |
|
658 |
*/ |
|
659 |
if (protocolVersion.v >= ProtocolVersion.TLS10.v) { |
|
660 |
bb.put(padOffset, (byte)0); // zero the padding. |
|
661 |
for (int i = 1; i <= pad; i++) { |
|
662 |
int val = bb.get(padOffset - i) & 0xff; |
|
663 |
if (val != pad) { |
|
664 |
throw new BadPaddingException |
|
665 |
("Invalid TLS padding: " + val); |
|
666 |
} |
|
667 |
} |
|
668 |
} else { // SSLv3 |
|
669 |
// SSLv3 requires 0 <= length byte < block size |
|
670 |
// some implementations do 1 <= length byte <= block size, |
|
671 |
// so accept that as well |
|
672 |
// v3 does not require any particular value for the other bytes |
|
673 |
if (pad > blockSize) { |
|
674 |
throw new BadPaddingException("Invalid SSLv3 padding: " + pad); |
|
675 |
} |
|
676 |
} |
|
677 |
||
678 |
/* |
|
679 |
* Reset buffer limit to remove padding. |
|
680 |
*/ |
|
681 |
bb.position(offset + newlen); |
|
682 |
bb.limit(offset + newlen); |
|
683 |
||
684 |
return newlen; |
|
685 |
} |
|
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
|
686 |
|
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
|
687 |
/* |
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
|
688 |
* 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
|
689 |
* 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
|
690 |
* 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
|
691 |
*/ |
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
|
692 |
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
|
693 |
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
|
694 |
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
|
695 |
// 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
|
696 |
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
|
697 |
} |
16067 | 698 |
} catch (GeneralSecurityException e) { |
699 |
// swallow for now. |
|
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
|
700 |
} |
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
|
701 |
} |
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
|
702 |
|
10915 | 703 |
/* |
704 |
* Does the cipher use CBC mode? |
|
705 |
* |
|
706 |
* @return true if the cipher use CBC mode, false otherwise. |
|
707 |
*/ |
|
708 |
boolean isCBCMode() { |
|
16067 | 709 |
return isCBCMode; |
10915 | 710 |
} |
2 | 711 |
} |