author | valeriep |
Tue, 08 May 2012 17:57:48 -0700 | |
changeset 12685 | 8a448b5b9006 |
parent 10336 | 0bb1999251f8 |
child 15010 | ec6b49ce42b1 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
2 |
* Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package com.sun.crypto.provider; |
|
27 |
||
28 |
import java.security.*; |
|
29 |
import java.security.spec.*; |
|
30 |
import javax.crypto.*; |
|
31 |
import javax.crypto.spec.*; |
|
32 |
||
33 |
/** |
|
34 |
* This class implements the CMS DESede KeyWrap algorithm as defined |
|
35 |
* in <a href=http://www.w3.org/TR/xmlenc-core/#sec-Alg-SymmetricKeyWrap> |
|
36 |
* "XML Encryption Syntax and Processing" section 5.6.2 |
|
37 |
* "CMS Triple DES Key Wrap". |
|
38 |
* Note: only <code>CBC</code> mode and <code>NoPadding</code> padding |
|
39 |
* scheme can be used for this algorithm. |
|
40 |
* |
|
41 |
* @author Valerie Peng |
|
42 |
* |
|
43 |
* |
|
44 |
* @see DESedeCipher |
|
45 |
*/ |
|
46 |
public final class DESedeWrapCipher extends CipherSpi { |
|
47 |
||
48 |
private static final byte[] IV2 = { |
|
49 |
(byte) 0x4a, (byte) 0xdd, (byte) 0xa2, (byte) 0x2c, |
|
50 |
(byte) 0x79, (byte) 0xe8, (byte) 0x21, (byte) 0x05 |
|
51 |
}; |
|
52 |
||
53 |
/* |
|
54 |
* internal cipher object which does the real work. |
|
55 |
*/ |
|
56 |
private FeedbackCipher cipher; |
|
57 |
||
58 |
/* |
|
59 |
* iv for (re-)initializing the internal cipher object. |
|
60 |
*/ |
|
61 |
private byte[] iv = null; |
|
62 |
||
63 |
/* |
|
64 |
* key for re-initializing the internal cipher object. |
|
65 |
*/ |
|
66 |
private Key cipherKey = null; |
|
67 |
||
68 |
/* |
|
69 |
* are we encrypting or decrypting? |
|
70 |
*/ |
|
71 |
private boolean decrypting = false; |
|
72 |
||
73 |
/** |
|
74 |
* Creates an instance of CMS DESede KeyWrap cipher with default |
|
75 |
* mode, i.e. "CBC" and padding scheme, i.e. "NoPadding". |
|
76 |
*/ |
|
77 |
public DESedeWrapCipher() { |
|
78 |
cipher = new CipherBlockChaining(new DESedeCrypt()); |
|
79 |
} |
|
80 |
||
81 |
/** |
|
82 |
* Sets the mode of this cipher. Only "CBC" mode is accepted for this |
|
83 |
* cipher. |
|
84 |
* |
|
85 |
* @param mode the cipher mode. |
|
86 |
* |
|
87 |
* @exception NoSuchAlgorithmException if the requested cipher mode |
|
88 |
* is not "CBC". |
|
89 |
*/ |
|
90 |
protected void engineSetMode(String mode) |
|
91 |
throws NoSuchAlgorithmException { |
|
92 |
if (!mode.equalsIgnoreCase("CBC")) { |
|
93 |
throw new NoSuchAlgorithmException(mode + " cannot be used"); |
|
94 |
} |
|
95 |
} |
|
96 |
||
97 |
/** |
|
98 |
* Sets the padding mechanism of this cipher. Only "NoPadding" schmem |
|
99 |
* is accepted for this cipher. |
|
100 |
* |
|
101 |
* @param padding the padding mechanism. |
|
102 |
* |
|
103 |
* @exception NoSuchPaddingException if the requested padding mechanism |
|
104 |
* is not "NoPadding". |
|
105 |
*/ |
|
106 |
protected void engineSetPadding(String padding) |
|
107 |
throws NoSuchPaddingException { |
|
108 |
if (!padding.equalsIgnoreCase("NoPadding")) { |
|
109 |
throw new NoSuchPaddingException(padding + " cannot be used"); |
|
110 |
} |
|
111 |
} |
|
112 |
||
113 |
/** |
|
114 |
* Returns the block size (in bytes), i.e. 8 bytes. |
|
115 |
* |
|
116 |
* @return the block size (in bytes), i.e. 8 bytes. |
|
117 |
*/ |
|
118 |
protected int engineGetBlockSize() { |
|
119 |
return DESConstants.DES_BLOCK_SIZE; |
|
120 |
} |
|
121 |
||
122 |
/** |
|
123 |
* Returns the length in bytes that an output buffer would need to be |
|
124 |
* given the input length <code>inputLen</code> (in bytes). |
|
125 |
* |
|
126 |
* <p>The actual output length of the next <code>update</code> or |
|
127 |
* <code>doFinal</code> call may be smaller than the length returned |
|
128 |
* by this method. |
|
129 |
* |
|
130 |
* @param inputLen the input length (in bytes). |
|
131 |
* |
|
132 |
* @return the required output buffer size (in bytes). |
|
133 |
*/ |
|
134 |
protected int engineGetOutputSize(int inputLen) { |
|
135 |
// can only return an upper-limit if not initialized yet. |
|
136 |
int result = 0; |
|
137 |
if (decrypting) { |
|
138 |
result = inputLen - 16; |
|
139 |
} else { |
|
140 |
result = inputLen + 16; |
|
141 |
} |
|
142 |
return (result < 0? 0:result); |
|
143 |
} |
|
144 |
||
145 |
/** |
|
146 |
* Returns the initialization vector (IV) in a new buffer. |
|
147 |
* |
|
148 |
* @return the initialization vector, or null if the underlying |
|
149 |
* algorithm does not use an IV, or if the IV has not yet |
|
150 |
* been set. |
|
151 |
*/ |
|
152 |
protected byte[] engineGetIV() { |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
153 |
return (iv == null) ? null : iv.clone(); |
2 | 154 |
} |
155 |
||
156 |
/** |
|
157 |
* Initializes this cipher with a key and a source of randomness. |
|
158 |
* |
|
159 |
* <p>The cipher only supports the following two operation modes:<b> |
|
160 |
* Cipher.WRAP_MODE, and <b> |
|
161 |
* Cipher.UNWRAP_MODE. |
|
162 |
* <p>For modes other than the above two, UnsupportedOperationException |
|
163 |
* will be thrown. |
|
164 |
* <p>If this cipher requires an initialization vector (IV), it will get |
|
165 |
* it from <code>random</code>. |
|
166 |
* |
|
167 |
* @param opmode the operation mode of this cipher. Only |
|
168 |
* <code>WRAP_MODE</code> or <code>UNWRAP_MODE</code>) are accepted. |
|
169 |
* @param key the secret key. |
|
170 |
* @param random the source of randomness. |
|
171 |
* |
|
172 |
* @exception InvalidKeyException if the given key is inappropriate |
|
173 |
* or if parameters are required but not supplied. |
|
174 |
*/ |
|
175 |
protected void engineInit(int opmode, Key key, SecureRandom random) |
|
176 |
throws InvalidKeyException { |
|
177 |
try { |
|
178 |
engineInit(opmode, key, (AlgorithmParameterSpec) null, random); |
|
179 |
} catch (InvalidAlgorithmParameterException iape) { |
|
180 |
// should never happen |
|
181 |
InvalidKeyException ike = |
|
182 |
new InvalidKeyException("Parameters required"); |
|
183 |
ike.initCause(iape); |
|
184 |
throw ike; |
|
185 |
} |
|
186 |
} |
|
187 |
||
188 |
/** |
|
189 |
* Initializes this cipher with a key, a set of algorithm parameters, |
|
190 |
* and a source of randomness. |
|
191 |
* |
|
192 |
* <p>The cipher only supports the following two operation modes:<b> |
|
193 |
* Cipher.WRAP_MODE, and <b> |
|
194 |
* Cipher.UNWRAP_MODE. |
|
195 |
* <p>For modes other than the above two, UnsupportedOperationException |
|
196 |
* will be thrown. |
|
197 |
* <p>If this cipher requires an initialization vector (IV), it will get |
|
198 |
* it from <code>random</code>. |
|
199 |
* |
|
200 |
* @param opmode the operation mode of this cipher. Only |
|
201 |
* <code>WRAP_MODE</code> or <code>UNWRAP_MODE</code>) are accepted. |
|
202 |
* @param key the secret key. |
|
203 |
* @param params the algorithm parameters. |
|
204 |
* @param random the source of randomness. |
|
205 |
* |
|
206 |
* @exception InvalidKeyException if the given key is inappropriate. |
|
207 |
* @exception InvalidAlgorithmParameterException if the given algorithm |
|
208 |
* parameters are inappropriate for this cipher. |
|
209 |
*/ |
|
210 |
protected void engineInit(int opmode, Key key, |
|
211 |
AlgorithmParameterSpec params, |
|
212 |
SecureRandom random) |
|
213 |
throws InvalidKeyException, InvalidAlgorithmParameterException { |
|
214 |
byte[] currIv = null; |
|
215 |
if (opmode == Cipher.WRAP_MODE) { |
|
216 |
decrypting = false; |
|
217 |
if (params == null) { |
|
218 |
iv = new byte[8]; |
|
219 |
if (random == null) { |
|
220 |
random = SunJCE.RANDOM; |
|
221 |
} |
|
222 |
random.nextBytes(iv); |
|
223 |
} |
|
224 |
else if (params instanceof IvParameterSpec) { |
|
225 |
iv = ((IvParameterSpec) params).getIV(); |
|
226 |
} else { |
|
227 |
throw new InvalidAlgorithmParameterException |
|
228 |
("Wrong parameter type: IV expected"); |
|
229 |
} |
|
230 |
currIv = iv; |
|
231 |
} else if (opmode == Cipher.UNWRAP_MODE) { |
|
232 |
if (params != null) { |
|
233 |
throw new InvalidAlgorithmParameterException |
|
234 |
("No parameter accepted for unwrapping keys"); |
|
235 |
} |
|
236 |
iv = null; |
|
237 |
decrypting = true; |
|
238 |
currIv = IV2; |
|
239 |
} else { |
|
240 |
throw new UnsupportedOperationException("This cipher can " + |
|
241 |
"only be used for key wrapping and unwrapping"); |
|
242 |
} |
|
243 |
cipher.init(decrypting, key.getAlgorithm(), key.getEncoded(), |
|
244 |
currIv); |
|
245 |
cipherKey = key; |
|
246 |
} |
|
247 |
||
248 |
/** |
|
249 |
* Initializes this cipher with a key, a set of algorithm parameters, |
|
250 |
* and a source of randomness. |
|
251 |
* |
|
252 |
* <p>The cipher only supports the following two operation modes:<b> |
|
253 |
* Cipher.WRAP_MODE, and <b> |
|
254 |
* Cipher.UNWRAP_MODE. |
|
255 |
* <p>For modes other than the above two, UnsupportedOperationException |
|
256 |
* will be thrown. |
|
257 |
* <p>If this cipher requires an initialization vector (IV), it will get |
|
258 |
* it from <code>random</code>. |
|
259 |
* |
|
260 |
* @param opmode the operation mode of this cipher. Only |
|
261 |
* <code>WRAP_MODE</code> or <code>UNWRAP_MODE</code>) are accepted. |
|
262 |
* @param key the secret key. |
|
263 |
* @param params the algorithm parameters. |
|
264 |
* @param random the source of randomness. |
|
265 |
* |
|
266 |
* @exception InvalidKeyException if the given key is inappropriate. |
|
267 |
* @exception InvalidAlgorithmParameterException if the given algorithm |
|
268 |
* parameters are inappropriate for this cipher. |
|
269 |
*/ |
|
270 |
protected void engineInit(int opmode, Key key, |
|
271 |
AlgorithmParameters params, |
|
272 |
SecureRandom random) |
|
273 |
throws InvalidKeyException, InvalidAlgorithmParameterException { |
|
274 |
IvParameterSpec ivSpec = null; |
|
275 |
if (params != null) { |
|
276 |
try { |
|
277 |
DESedeParameters paramsEng = new DESedeParameters(); |
|
278 |
paramsEng.engineInit(params.getEncoded()); |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
279 |
ivSpec = paramsEng.engineGetParameterSpec(IvParameterSpec.class); |
2 | 280 |
} catch (Exception ex) { |
281 |
InvalidAlgorithmParameterException iape = |
|
282 |
new InvalidAlgorithmParameterException |
|
283 |
("Wrong parameter type: IV expected"); |
|
284 |
iape.initCause(ex); |
|
285 |
throw iape; |
|
286 |
} |
|
287 |
} |
|
288 |
engineInit(opmode, key, ivSpec, random); |
|
289 |
} |
|
290 |
||
291 |
/** |
|
292 |
* This operation is not supported by this cipher. |
|
293 |
* Since it's impossible to initialize this cipher given the |
|
294 |
* current Cipher.engineInit(...) implementation, |
|
295 |
* IllegalStateException will always be thrown upon invocation. |
|
296 |
* |
|
297 |
* @param in the input buffer. |
|
298 |
* @param inOffset the offset in <code>in</code> where the input |
|
299 |
* starts. |
|
300 |
* @param inLen the input length. |
|
301 |
* |
|
302 |
* @return n/a. |
|
303 |
* |
|
304 |
* @exception IllegalStateException upon invocation of this method. |
|
305 |
*/ |
|
306 |
protected byte[] engineUpdate(byte[] in, int inOffset, int inLen) { |
|
307 |
throw new IllegalStateException("Cipher has not been initialized"); |
|
308 |
} |
|
309 |
||
310 |
/** |
|
311 |
* This operation is not supported by this cipher. |
|
312 |
* Since it's impossible to initialize this cipher given the |
|
313 |
* current Cipher.engineInit(...) implementation, |
|
314 |
* IllegalStateException will always be thrown upon invocation. |
|
315 |
* |
|
316 |
* @param in the input buffer. |
|
317 |
* @param inOffset the offset in <code>in</code> where the input |
|
318 |
* starts. |
|
319 |
* @param inLen the input length. |
|
320 |
* @param out the buffer for the result. |
|
321 |
* @param outOffset the offset in <code>out</code> where the result |
|
322 |
* is stored. |
|
323 |
* |
|
324 |
* @return n/a. |
|
325 |
* |
|
326 |
* @exception IllegalStateException upon invocation of this method. |
|
327 |
*/ |
|
328 |
protected int engineUpdate(byte[] in, int inOffset, int inLen, |
|
329 |
byte[] out, int outOffset) |
|
330 |
throws ShortBufferException { |
|
331 |
throw new IllegalStateException("Cipher has not been initialized"); |
|
332 |
} |
|
333 |
||
334 |
/** |
|
335 |
* This operation is not supported by this cipher. |
|
336 |
* Since it's impossible to initialize this cipher given the |
|
337 |
* current Cipher.engineInit(...) implementation, |
|
338 |
* IllegalStateException will always be thrown upon invocation. |
|
339 |
* |
|
340 |
* @param in the input buffer. |
|
341 |
* @param inOffset the offset in <code>in</code> where the input |
|
342 |
* starts. |
|
343 |
* @param inLen the input length. |
|
344 |
* |
|
345 |
* @return the new buffer with the result. |
|
346 |
* |
|
347 |
* @exception IllegalStateException upon invocation of this method. |
|
348 |
*/ |
|
349 |
protected byte[] engineDoFinal(byte[] in, int inOffset, int inLen) |
|
350 |
throws IllegalBlockSizeException, BadPaddingException { |
|
351 |
throw new IllegalStateException("Cipher has not been initialized"); |
|
352 |
} |
|
353 |
||
354 |
/** |
|
355 |
* This operation is not supported by this cipher. |
|
356 |
* Since it's impossible to initialize this cipher given the |
|
357 |
* current Cipher.engineInit(...) implementation, |
|
358 |
* IllegalStateException will always be thrown upon invocation. |
|
359 |
* |
|
360 |
* @param in the input buffer. |
|
361 |
* @param inOffset the offset in <code>in</code> where the input |
|
362 |
* starts. |
|
363 |
* @param inLen the input length. |
|
364 |
* @param out the buffer for the result. |
|
365 |
* @param outOffset the ofset in <code>out</code> where the result |
|
366 |
* is stored. |
|
367 |
* |
|
368 |
* @return the number of bytes stored in <code>out</code>. |
|
369 |
* |
|
370 |
* @exception IllegalStateException upon invocation of this method. |
|
371 |
*/ |
|
372 |
protected int engineDoFinal(byte[] input, int inputOffset, int inputLen, |
|
373 |
byte[] output, int outputOffset) |
|
374 |
throws IllegalBlockSizeException, ShortBufferException, |
|
375 |
BadPaddingException { |
|
376 |
throw new IllegalStateException("Cipher has not been initialized"); |
|
377 |
} |
|
378 |
||
379 |
/** |
|
380 |
* Returns the parameters used with this cipher. |
|
381 |
* Note that null maybe returned if this cipher does not use any |
|
382 |
* parameters or when it has not be set, e.g. initialized with |
|
383 |
* UNWRAP_MODE but wrapped key data has not been given. |
|
384 |
* |
|
385 |
* @return the parameters used with this cipher; can be null. |
|
386 |
*/ |
|
387 |
protected AlgorithmParameters engineGetParameters() { |
|
388 |
AlgorithmParameters params = null; |
|
389 |
if (iv != null) { |
|
390 |
String algo = cipherKey.getAlgorithm(); |
|
391 |
try { |
|
392 |
params = AlgorithmParameters.getInstance(algo, "SunJCE"); |
|
393 |
} catch (NoSuchAlgorithmException nsae) { |
|
394 |
// should never happen |
|
395 |
throw new RuntimeException("Cannot find " + algo + |
|
396 |
" AlgorithmParameters implementation in SunJCE provider"); |
|
397 |
} catch (NoSuchProviderException nspe) { |
|
398 |
// should never happen |
|
399 |
throw new RuntimeException("Cannot find SunJCE provider"); |
|
400 |
} |
|
401 |
try { |
|
402 |
params.init(new IvParameterSpec(iv)); |
|
403 |
} catch (InvalidParameterSpecException ipse) { |
|
404 |
// should never happen |
|
405 |
throw new RuntimeException("IvParameterSpec not supported"); |
|
406 |
} |
|
407 |
} |
|
408 |
return params; |
|
409 |
} |
|
410 |
||
411 |
/** |
|
412 |
* Returns the key size of the given key object in number of bits. |
|
413 |
* This cipher always return the same key size as the DESede ciphers. |
|
414 |
* |
|
415 |
* @param key the key object. |
|
416 |
* |
|
417 |
* @return the "effective" key size of the given key object. |
|
418 |
* |
|
419 |
* @exception InvalidKeyException if <code>key</code> is invalid. |
|
420 |
*/ |
|
421 |
protected int engineGetKeySize(Key key) throws InvalidKeyException { |
|
422 |
byte[] encoded = key.getEncoded(); |
|
423 |
if (encoded.length != 24) { |
|
424 |
throw new InvalidKeyException("Invalid key length: " + |
|
425 |
encoded.length + " bytes"); |
|
426 |
} |
|
427 |
// Return the effective key length |
|
428 |
return 112; |
|
429 |
} |
|
430 |
||
431 |
/** |
|
432 |
* Wrap a key. |
|
433 |
* |
|
434 |
* @param key the key to be wrapped. |
|
435 |
* |
|
436 |
* @return the wrapped key. |
|
437 |
* |
|
438 |
* @exception IllegalBlockSizeException if this cipher is a block |
|
439 |
* cipher, no padding has been requested, and the length of the |
|
440 |
* encoding of the key to be wrapped is not a |
|
441 |
* multiple of the block size. |
|
442 |
* |
|
443 |
* @exception InvalidKeyException if it is impossible or unsafe to |
|
444 |
* wrap the key with this cipher (e.g., a hardware protected key is |
|
445 |
* being passed to a software only cipher). |
|
446 |
*/ |
|
447 |
protected byte[] engineWrap(Key key) |
|
448 |
throws IllegalBlockSizeException, InvalidKeyException { |
|
449 |
byte[] keyVal = key.getEncoded(); |
|
450 |
if ((keyVal == null) || (keyVal.length == 0)) { |
|
451 |
throw new InvalidKeyException("Cannot get an encoding of " + |
|
452 |
"the key to be wrapped"); |
|
453 |
} |
|
454 |
||
455 |
byte[] cks = getChecksum(keyVal); |
|
456 |
byte[] out = new byte[iv.length + keyVal.length + cks.length]; |
|
457 |
||
458 |
System.arraycopy(keyVal, 0, out, iv.length, keyVal.length); |
|
459 |
System.arraycopy(cks, 0, out, iv.length+keyVal.length, cks.length); |
|
460 |
cipher.encrypt(out, iv.length, keyVal.length+cks.length, |
|
461 |
out, iv.length); |
|
462 |
||
463 |
System.arraycopy(iv, 0, out, 0, iv.length); |
|
464 |
// reverse the array content |
|
465 |
for (int i = 0; i < out.length/2; i++) { |
|
466 |
byte temp = out[i]; |
|
467 |
out[i] = out[out.length-1-i]; |
|
468 |
out[out.length-1-i] = temp; |
|
469 |
} |
|
470 |
try { |
|
471 |
cipher.init(false, cipherKey.getAlgorithm(), |
|
472 |
cipherKey.getEncoded(), IV2); |
|
473 |
} catch (InvalidKeyException ike) { |
|
474 |
// should never happen |
|
475 |
throw new RuntimeException("Internal cipher key is corrupted"); |
|
476 |
} |
|
477 |
cipher.encrypt(out, 0, out.length, out, 0); |
|
478 |
||
479 |
// restore cipher state to prior to this call |
|
480 |
try { |
|
481 |
cipher.init(decrypting, cipherKey.getAlgorithm(), |
|
482 |
cipherKey.getEncoded(), iv); |
|
483 |
} catch (InvalidKeyException ike) { |
|
484 |
// should never happen |
|
485 |
throw new RuntimeException("Internal cipher key is corrupted"); |
|
486 |
} |
|
487 |
return out; |
|
488 |
} |
|
489 |
||
490 |
/** |
|
491 |
* Unwrap a previously wrapped key. |
|
492 |
* |
|
493 |
* @param wrappedKey the key to be unwrapped. |
|
494 |
* |
|
495 |
* @param wrappedKeyAlgorithm the algorithm the wrapped key is for. |
|
496 |
* |
|
497 |
* @param wrappedKeyType the type of the wrapped key. |
|
498 |
* This is one of <code>Cipher.SECRET_KEY</code>, |
|
499 |
* <code>Cipher.PRIVATE_KEY</code>, or <code>Cipher.PUBLIC_KEY</code>. |
|
500 |
* |
|
501 |
* @return the unwrapped key. |
|
502 |
* |
|
503 |
* @exception NoSuchAlgorithmException if no installed providers |
|
504 |
* can create keys of type <code>wrappedKeyType</code> for the |
|
505 |
* <code>wrappedKeyAlgorithm</code>. |
|
506 |
* |
|
507 |
* @exception InvalidKeyException if <code>wrappedKey</code> does not |
|
508 |
* represent a wrapped key of type <code>wrappedKeyType</code> for |
|
509 |
* the <code>wrappedKeyAlgorithm</code>. |
|
510 |
*/ |
|
511 |
protected Key engineUnwrap(byte[] wrappedKey, |
|
512 |
String wrappedKeyAlgorithm, |
|
513 |
int wrappedKeyType) |
|
514 |
throws InvalidKeyException, NoSuchAlgorithmException { |
|
515 |
if (wrappedKey.length == 0) { |
|
516 |
throw new InvalidKeyException("The wrapped key is empty"); |
|
517 |
} |
|
518 |
byte[] buffer = new byte[wrappedKey.length]; |
|
519 |
cipher.decrypt(wrappedKey, 0, wrappedKey.length, buffer, 0); |
|
520 |
||
521 |
// reverse array content |
|
522 |
for (int i = 0; i < buffer.length/2; i++) { |
|
523 |
byte temp = buffer[i]; |
|
524 |
buffer[i] = buffer[buffer.length-1-i]; |
|
525 |
buffer[buffer.length-1-i] = temp; |
|
526 |
} |
|
527 |
iv = new byte[IV2.length]; |
|
528 |
System.arraycopy(buffer, 0, iv, 0, iv.length); |
|
529 |
cipher.init(true, cipherKey.getAlgorithm(), cipherKey.getEncoded(), |
|
530 |
iv); |
|
531 |
cipher.decrypt(buffer, iv.length, buffer.length-iv.length, |
|
532 |
buffer, iv.length); |
|
533 |
int origLen = buffer.length - iv.length - 8; |
|
534 |
byte[] cks = getChecksum(buffer, iv.length, origLen); |
|
535 |
int offset = iv.length + origLen; |
|
536 |
for (int i = 0; i < cks.length; i++) { |
|
537 |
if (buffer[offset + i] != cks[i]) { |
|
538 |
throw new InvalidKeyException("Checksum comparison failed"); |
|
539 |
} |
|
540 |
} |
|
541 |
// restore cipher state to prior to this call |
|
542 |
cipher.init(decrypting, cipherKey.getAlgorithm(), |
|
543 |
cipherKey.getEncoded(), IV2); |
|
544 |
byte[] out = new byte[origLen]; |
|
545 |
System.arraycopy(buffer, iv.length, out, 0, out.length); |
|
546 |
return ConstructKeys.constructKey(out, wrappedKeyAlgorithm, |
|
547 |
wrappedKeyType); |
|
548 |
} |
|
549 |
||
550 |
private static final byte[] getChecksum(byte[] in) { |
|
551 |
return getChecksum(in, 0, in.length); |
|
552 |
} |
|
553 |
private static final byte[] getChecksum(byte[] in, int offset, int len) { |
|
554 |
MessageDigest md = null; |
|
555 |
try { |
|
556 |
md = MessageDigest.getInstance("SHA1"); |
|
557 |
} catch (NoSuchAlgorithmException nsae) { |
|
558 |
throw new RuntimeException("SHA1 message digest not available"); |
|
559 |
} |
|
560 |
md.update(in, offset, len); |
|
561 |
byte[] cks = new byte[8]; |
|
562 |
System.arraycopy(md.digest(), 0, cks, 0, cks.length); |
|
563 |
return cks; |
|
564 |
} |
|
565 |
} |