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) 2002, 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 represents password-based encryption as defined by the PKCS #5 |
|
35 |
* standard. |
|
36 |
* |
|
37 |
* @author Jan Luehe |
|
38 |
* |
|
39 |
* |
|
40 |
* @see javax.crypto.Cipher |
|
41 |
*/ |
|
42 |
final class PBECipherCore { |
|
43 |
||
44 |
// the encapsulated DES cipher |
|
45 |
private CipherCore cipher; |
|
46 |
private MessageDigest md; |
|
47 |
private int blkSize; |
|
48 |
private String algo = null; |
|
49 |
private byte[] salt = null; |
|
50 |
private int iCount = 10; |
|
51 |
||
52 |
/** |
|
53 |
* Creates an instance of PBE Cipher using the specified CipherSpi |
|
54 |
* instance. |
|
55 |
* |
|
56 |
*/ |
|
57 |
PBECipherCore(String cipherAlg) throws NoSuchAlgorithmException, |
|
58 |
NoSuchPaddingException { |
|
59 |
algo = cipherAlg; |
|
60 |
if (algo.equals("DES")) { |
|
61 |
cipher = new CipherCore(new DESCrypt(), |
|
62 |
DESConstants.DES_BLOCK_SIZE); |
|
63 |
} else if (algo.equals("DESede")) { |
|
64 |
||
65 |
cipher = new CipherCore(new DESedeCrypt(), |
|
66 |
DESConstants.DES_BLOCK_SIZE); |
|
67 |
} else { |
|
68 |
throw new NoSuchAlgorithmException("No Cipher implementation " + |
|
69 |
"for PBEWithMD5And" + algo); |
|
70 |
} |
|
71 |
cipher.setMode("CBC"); |
|
72 |
cipher.setPadding("PKCS5Padding"); |
|
73 |
// get instance of MD5 |
|
74 |
md = MessageDigest.getInstance("MD5"); |
|
75 |
} |
|
76 |
||
77 |
/** |
|
78 |
* Sets the mode of this cipher. This algorithm can only be run in CBC |
|
79 |
* mode. |
|
80 |
* |
|
81 |
* @param mode the cipher mode |
|
82 |
* |
|
83 |
* @exception NoSuchAlgorithmException if the requested cipher mode is |
|
84 |
* invalid |
|
85 |
*/ |
|
86 |
void setMode(String mode) throws NoSuchAlgorithmException { |
|
87 |
cipher.setMode(mode); |
|
88 |
} |
|
89 |
||
90 |
/** |
|
91 |
* Sets the padding mechanism of this cipher. This algorithm only uses |
|
92 |
* PKCS #5 padding. |
|
93 |
* |
|
94 |
* @param padding the padding mechanism |
|
95 |
* |
|
96 |
* @exception NoSuchPaddingException if the requested padding mechanism |
|
97 |
* is invalid |
|
98 |
*/ |
|
99 |
void setPadding(String paddingScheme) throws NoSuchPaddingException { |
|
100 |
cipher.setPadding(paddingScheme); |
|
101 |
} |
|
102 |
||
103 |
/** |
|
104 |
* Returns the block size (in bytes). |
|
105 |
* |
|
106 |
* @return the block size (in bytes) |
|
107 |
*/ |
|
108 |
int getBlockSize() { |
|
109 |
return DESConstants.DES_BLOCK_SIZE; |
|
110 |
} |
|
111 |
||
112 |
/** |
|
113 |
* Returns the length in bytes that an output buffer would need to be in |
|
114 |
* order to hold the result of the next <code>update</code> or |
|
115 |
* <code>doFinal</code> operation, given the input length |
|
116 |
* <code>inputLen</code> (in bytes). |
|
117 |
* |
|
118 |
* <p>This call takes into account any unprocessed (buffered) data from a |
|
119 |
* previous <code>update</code> call, and padding. |
|
120 |
* |
|
121 |
* <p>The actual output length of the next <code>update</code> or |
|
122 |
* <code>doFinal</code> call may be smaller than the length returned by |
|
123 |
* this method. |
|
124 |
* |
|
125 |
* @param inputLen the input length (in bytes) |
|
126 |
* |
|
127 |
* @return the required output buffer size (in bytes) |
|
128 |
* |
|
129 |
*/ |
|
130 |
int getOutputSize(int inputLen) { |
|
131 |
return cipher.getOutputSize(inputLen); |
|
132 |
} |
|
133 |
||
134 |
/** |
|
135 |
* Returns the initialization vector (IV) in a new buffer. |
|
136 |
* |
|
137 |
* <p> This is useful in the case where a random IV has been created |
|
138 |
* (see <a href = "#init">init</a>), |
|
139 |
* or in the context of password-based encryption or |
|
140 |
* decryption, where the IV is derived from a user-supplied password. |
|
141 |
* |
|
142 |
* @return the initialization vector in a new buffer, or null if the |
|
143 |
* underlying algorithm does not use an IV, or if the IV has not yet |
|
144 |
* been set. |
|
145 |
*/ |
|
146 |
byte[] getIV() { |
|
147 |
return cipher.getIV(); |
|
148 |
} |
|
149 |
||
150 |
/** |
|
151 |
* Returns the parameters used with this cipher. |
|
152 |
* |
|
153 |
* <p>The returned parameters may be the same that were used to initialize |
|
154 |
* this cipher, or may contain the default set of parameters or a set of |
|
155 |
* randomly generated parameters used by the underlying cipher |
|
156 |
* implementation (provided that the underlying cipher implementation |
|
157 |
* uses a default set of parameters or creates new parameters if it needs |
|
158 |
* parameters but was not initialized with any). |
|
159 |
* |
|
160 |
* @return the parameters used with this cipher, or null if this cipher |
|
161 |
* does not use any parameters. |
|
162 |
*/ |
|
163 |
AlgorithmParameters getParameters() { |
|
164 |
AlgorithmParameters params = null; |
|
165 |
if (salt == null) { |
|
166 |
salt = new byte[8]; |
|
167 |
SunJCE.RANDOM.nextBytes(salt); |
|
168 |
} |
|
169 |
PBEParameterSpec pbeSpec = new PBEParameterSpec(salt, iCount); |
|
170 |
try { |
|
171 |
params = AlgorithmParameters.getInstance("PBEWithMD5And" + |
|
172 |
(algo.equalsIgnoreCase("DES")? "DES":"TripleDES"), "SunJCE"); |
|
173 |
} catch (NoSuchAlgorithmException nsae) { |
|
174 |
// should never happen |
|
175 |
throw new RuntimeException("SunJCE called, but not configured"); |
|
176 |
} catch (NoSuchProviderException nspe) { |
|
177 |
// should never happen |
|
178 |
throw new RuntimeException("SunJCE called, but not configured"); |
|
179 |
} |
|
180 |
try { |
|
181 |
params.init(pbeSpec); |
|
182 |
} catch (InvalidParameterSpecException ipse) { |
|
183 |
// should never happen |
|
184 |
throw new RuntimeException("PBEParameterSpec not supported"); |
|
185 |
} |
|
186 |
return params; |
|
187 |
} |
|
188 |
||
189 |
/** |
|
190 |
* Initializes this cipher with a key, a set of |
|
191 |
* algorithm parameters, and a source of randomness. |
|
192 |
* The cipher is initialized for one of the following four operations: |
|
193 |
* encryption, decryption, key wrapping or key unwrapping, depending on |
|
194 |
* the value of <code>opmode</code>. |
|
195 |
* |
|
196 |
* <p>If this cipher (including its underlying feedback or padding scheme) |
|
197 |
* requires any random bytes, it will get them from <code>random</code>. |
|
198 |
* |
|
199 |
* @param opmode the operation mode of this cipher (this is one of |
|
200 |
* the following: |
|
201 |
* <code>ENCRYPT_MODE</code>, <code>DECRYPT_MODE</code>), |
|
202 |
* <code>WRAP_MODE</code> or <code>UNWRAP_MODE</code>) |
|
203 |
* @param key the encryption key |
|
204 |
* @param params the algorithm parameters |
|
205 |
* @param random the source of randomness |
|
206 |
* |
|
207 |
* @exception InvalidKeyException if the given key is inappropriate for |
|
208 |
* initializing this cipher |
|
209 |
* @exception InvalidAlgorithmParameterException if the given algorithm |
|
210 |
* parameters are inappropriate for this cipher |
|
211 |
*/ |
|
212 |
void init(int opmode, Key key, AlgorithmParameterSpec params, |
|
213 |
SecureRandom random) |
|
214 |
throws InvalidKeyException, InvalidAlgorithmParameterException { |
|
215 |
if (((opmode == Cipher.DECRYPT_MODE) || |
|
216 |
(opmode == Cipher.UNWRAP_MODE)) && (params == null)) { |
|
217 |
throw new InvalidAlgorithmParameterException("Parameters " |
|
218 |
+ "missing"); |
|
219 |
} |
|
220 |
if ((key == null) || |
|
221 |
(key.getEncoded() == null) || |
|
222 |
!(key.getAlgorithm().regionMatches(true, 0, "PBE", 0, 3))) { |
|
223 |
throw new InvalidKeyException("Missing password"); |
|
224 |
} |
|
225 |
||
226 |
if (params == null) { |
|
227 |
// create random salt and use default iteration count |
|
228 |
salt = new byte[8]; |
|
229 |
random.nextBytes(salt); |
|
230 |
} else { |
|
231 |
if (!(params instanceof PBEParameterSpec)) { |
|
232 |
throw new InvalidAlgorithmParameterException |
|
233 |
("Wrong parameter type: PBE expected"); |
|
234 |
} |
|
235 |
salt = ((PBEParameterSpec) params).getSalt(); |
|
236 |
// salt must be 8 bytes long (by definition) |
|
237 |
if (salt.length != 8) { |
|
238 |
throw new InvalidAlgorithmParameterException |
|
239 |
("Salt must be 8 bytes long"); |
|
240 |
} |
|
241 |
iCount = ((PBEParameterSpec) params).getIterationCount(); |
|
242 |
if (iCount <= 0) { |
|
243 |
throw new InvalidAlgorithmParameterException |
|
244 |
("IterationCount must be a positive number"); |
|
245 |
} |
|
246 |
} |
|
247 |
||
248 |
byte[] derivedKey = deriveCipherKey(key); |
|
249 |
// use all but the last 8 bytes as the key value |
|
250 |
SecretKeySpec cipherKey = new SecretKeySpec(derivedKey, 0, |
|
251 |
derivedKey.length-8, algo); |
|
252 |
// use the last 8 bytes as the IV |
|
253 |
IvParameterSpec ivSpec = new IvParameterSpec(derivedKey, |
|
254 |
derivedKey.length-8, |
|
255 |
8); |
|
256 |
// initialize the underlying cipher |
|
257 |
cipher.init(opmode, cipherKey, ivSpec, random); |
|
258 |
} |
|
259 |
||
260 |
private byte[] deriveCipherKey(Key key) { |
|
261 |
||
262 |
byte[] result = null; |
|
263 |
byte[] passwdBytes = key.getEncoded(); |
|
264 |
||
265 |
if (algo.equals("DES")) { |
|
266 |
// P || S (password concatenated with salt) |
|
267 |
byte[] concat = new byte[passwdBytes.length + salt.length]; |
|
268 |
System.arraycopy(passwdBytes, 0, concat, 0, passwdBytes.length); |
|
269 |
java.util.Arrays.fill(passwdBytes, (byte)0x00); |
|
270 |
System.arraycopy(salt, 0, concat, passwdBytes.length, salt.length); |
|
271 |
||
272 |
// digest P || S with c iterations |
|
273 |
byte[] toBeHashed = concat; |
|
274 |
for (int i = 0; i < iCount; i++) { |
|
275 |
md.update(toBeHashed); |
|
276 |
toBeHashed = md.digest(); // this resets the digest |
|
277 |
} |
|
278 |
java.util.Arrays.fill(concat, (byte)0x00); |
|
279 |
result = toBeHashed; |
|
280 |
} else if (algo.equals("DESede")) { |
|
281 |
// if the 2 salt halves are the same, invert one of them |
|
282 |
int i; |
|
283 |
for (i=0; i<4; i++) { |
|
284 |
if (salt[i] != salt[i+4]) |
|
285 |
break; |
|
286 |
} |
|
287 |
if (i==4) { // same, invert 1st half |
|
288 |
for (i=0; i<2; i++) { |
|
289 |
byte tmp = salt[i]; |
|
290 |
salt[i] = salt[3-i]; |
|
291 |
salt[3-1] = tmp; |
|
292 |
} |
|
293 |
} |
|
294 |
||
295 |
// Now digest each half (concatenated with password). For each |
|
296 |
// half, go through the loop as many times as specified by the |
|
297 |
// iteration count parameter (inner for loop). |
|
298 |
// Concatenate the output from each digest round with the |
|
299 |
// password, and use the result as the input to the next digest |
|
300 |
// operation. |
|
301 |
byte[] kBytes = null; |
|
302 |
IvParameterSpec iv = null; |
|
303 |
byte[] toBeHashed = null; |
|
304 |
result = new byte[DESedeKeySpec.DES_EDE_KEY_LEN + |
|
305 |
DESConstants.DES_BLOCK_SIZE]; |
|
306 |
for (i = 0; i < 2; i++) { |
|
307 |
toBeHashed = new byte[salt.length/2]; |
|
308 |
System.arraycopy(salt, i*(salt.length/2), toBeHashed, 0, |
|
309 |
toBeHashed.length); |
|
310 |
for (int j=0; j < iCount; j++) { |
|
311 |
md.update(toBeHashed); |
|
312 |
md.update(passwdBytes); |
|
313 |
toBeHashed = md.digest(); // this resets the digest |
|
314 |
} |
|
315 |
System.arraycopy(toBeHashed, 0, result, i*16, |
|
316 |
toBeHashed.length); |
|
317 |
} |
|
318 |
} |
|
319 |
return result; |
|
320 |
} |
|
321 |
||
322 |
void init(int opmode, Key key, AlgorithmParameters params, |
|
323 |
SecureRandom random) |
|
324 |
throws InvalidKeyException, InvalidAlgorithmParameterException { |
|
325 |
PBEParameterSpec pbeSpec = null; |
|
326 |
if (params != null) { |
|
327 |
try { |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
328 |
pbeSpec = params.getParameterSpec(PBEParameterSpec.class); |
2 | 329 |
} catch (InvalidParameterSpecException ipse) { |
330 |
throw new InvalidAlgorithmParameterException("Wrong parameter " |
|
331 |
+ "type: PBE " |
|
332 |
+ "expected"); |
|
333 |
} |
|
334 |
} |
|
335 |
init(opmode, key, pbeSpec, random); |
|
336 |
} |
|
337 |
||
338 |
/** |
|
339 |
* Continues a multiple-part encryption or decryption operation |
|
340 |
* (depending on how this cipher was initialized), processing another data |
|
341 |
* part. |
|
342 |
* |
|
343 |
* <p>The first <code>inputLen</code> bytes in the <code>input</code> |
|
344 |
* buffer, starting at <code>inputOffset</code>, are processed, and the |
|
345 |
* result is stored in a new buffer. |
|
346 |
* |
|
347 |
* @param input the input buffer |
|
348 |
* @param inputOffset the offset in <code>input</code> where the input |
|
349 |
* starts |
|
350 |
* @param inputLen the input length |
|
351 |
* |
|
352 |
* @return the new buffer with the result |
|
353 |
* |
|
354 |
*/ |
|
355 |
byte[] update(byte[] input, int inputOffset, int inputLen) { |
|
356 |
return cipher.update(input, inputOffset, inputLen); |
|
357 |
} |
|
358 |
||
359 |
/** |
|
360 |
* Continues a multiple-part encryption or decryption operation |
|
361 |
* (depending on how this cipher was initialized), processing another data |
|
362 |
* part. |
|
363 |
* |
|
364 |
* <p>The first <code>inputLen</code> bytes in the <code>input</code> |
|
365 |
* buffer, starting at <code>inputOffset</code>, are processed, and the |
|
366 |
* result is stored in the <code>output</code> buffer, starting at |
|
367 |
* <code>outputOffset</code>. |
|
368 |
* |
|
369 |
* @param input the input buffer |
|
370 |
* @param inputOffset the offset in <code>input</code> where the input |
|
371 |
* starts |
|
372 |
* @param inputLen the input length |
|
373 |
* @param output the buffer for the result |
|
374 |
* @param outputOffset the offset in <code>output</code> where the result |
|
375 |
* is stored |
|
376 |
* |
|
377 |
* @return the number of bytes stored in <code>output</code> |
|
378 |
* |
|
379 |
* @exception ShortBufferException if the given output buffer is too small |
|
380 |
* to hold the result |
|
381 |
*/ |
|
382 |
int update(byte[] input, int inputOffset, int inputLen, |
|
383 |
byte[] output, int outputOffset) |
|
384 |
throws ShortBufferException { |
|
385 |
return cipher.update(input, inputOffset, inputLen, |
|
386 |
output, outputOffset); |
|
387 |
} |
|
388 |
||
389 |
/** |
|
390 |
* Encrypts or decrypts data in a single-part operation, |
|
391 |
* or finishes a multiple-part operation. |
|
392 |
* The data is encrypted or decrypted, depending on how this cipher was |
|
393 |
* initialized. |
|
394 |
* |
|
395 |
* <p>The first <code>inputLen</code> bytes in the <code>input</code> |
|
396 |
* buffer, starting at <code>inputOffset</code>, and any input bytes that |
|
397 |
* may have been buffered during a previous <code>update</code> operation, |
|
398 |
* are processed, with padding (if requested) being applied. |
|
399 |
* The result is stored in a new buffer. |
|
400 |
* |
|
401 |
* <p>The cipher is reset to its initial state (uninitialized) after this |
|
402 |
* call. |
|
403 |
* |
|
404 |
* @param input the input buffer |
|
405 |
* @param inputOffset the offset in <code>input</code> where the input |
|
406 |
* starts |
|
407 |
* @param inputLen the input length |
|
408 |
* |
|
409 |
* @return the new buffer with the result |
|
410 |
* |
|
411 |
* @exception IllegalBlockSizeException if this cipher is a block cipher, |
|
412 |
* no padding has been requested (only in encryption mode), and the total |
|
413 |
* input length of the data processed by this cipher is not a multiple of |
|
414 |
* block size |
|
415 |
* @exception BadPaddingException if decrypting and padding is choosen, |
|
416 |
* but the last input data does not have proper padding bytes. |
|
417 |
*/ |
|
418 |
byte[] doFinal(byte[] input, int inputOffset, int inputLen) |
|
419 |
throws IllegalBlockSizeException, BadPaddingException { |
|
420 |
return cipher.doFinal(input, inputOffset, inputLen); |
|
421 |
} |
|
422 |
||
423 |
/** |
|
424 |
* Encrypts or decrypts data in a single-part operation, |
|
425 |
* or finishes a multiple-part operation. |
|
426 |
* The data is encrypted or decrypted, depending on how this cipher was |
|
427 |
* initialized. |
|
428 |
* |
|
429 |
* <p>The first <code>inputLen</code> bytes in the <code>input</code> |
|
430 |
* buffer, starting at <code>inputOffset</code>, and any input bytes that |
|
431 |
* may have been buffered during a previous <code>update</code> operation, |
|
432 |
* are processed, with padding (if requested) being applied. |
|
433 |
* The result is stored in the <code>output</code> buffer, starting at |
|
434 |
* <code>outputOffset</code>. |
|
435 |
* |
|
436 |
* <p>The cipher is reset to its initial state (uninitialized) after this |
|
437 |
* call. |
|
438 |
* |
|
439 |
* @param input the input buffer |
|
440 |
* @param inputOffset the offset in <code>input</code> where the input |
|
441 |
* starts |
|
442 |
* @param inputLen the input length |
|
443 |
* @param output the buffer for the result |
|
444 |
* @param outputOffset the offset in <code>output</code> where the result |
|
445 |
* is stored |
|
446 |
* |
|
447 |
* @return the number of bytes stored in <code>output</code> |
|
448 |
* |
|
449 |
* @exception IllegalBlockSizeException if this cipher is a block cipher, |
|
450 |
* no padding has been requested (only in encryption mode), and the total |
|
451 |
* input length of the data processed by this cipher is not a multiple of |
|
452 |
* block size |
|
453 |
* @exception ShortBufferException if the given output buffer is too small |
|
454 |
* to hold the result |
|
455 |
* @exception BadPaddingException if decrypting and padding is choosen, |
|
456 |
* but the last input data does not have proper padding bytes. |
|
457 |
*/ |
|
458 |
int doFinal(byte[] input, int inputOffset, int inputLen, |
|
459 |
byte[] output, int outputOffset) |
|
460 |
throws ShortBufferException, IllegalBlockSizeException, |
|
461 |
BadPaddingException { |
|
462 |
return cipher.doFinal(input, inputOffset, inputLen, |
|
463 |
output, outputOffset); |
|
464 |
} |
|
465 |
||
466 |
/** |
|
467 |
* Wrap a key. |
|
468 |
* |
|
469 |
* @param key the key to be wrapped. |
|
470 |
* |
|
471 |
* @return the wrapped key. |
|
472 |
* |
|
473 |
* @exception IllegalBlockSizeException if this cipher is a block |
|
474 |
* cipher, no padding has been requested, and the length of the |
|
475 |
* encoding of the key to be wrapped is not a |
|
476 |
* multiple of the block size. |
|
477 |
* |
|
478 |
* @exception InvalidKeyException if it is impossible or unsafe to |
|
479 |
* wrap the key with this cipher (e.g., a hardware protected key is |
|
480 |
* being passed to a software only cipher). |
|
481 |
*/ |
|
482 |
byte[] wrap(Key key) |
|
483 |
throws IllegalBlockSizeException, InvalidKeyException { |
|
484 |
byte[] result = null; |
|
485 |
||
486 |
try { |
|
487 |
byte[] encodedKey = key.getEncoded(); |
|
488 |
if ((encodedKey == null) || (encodedKey.length == 0)) { |
|
489 |
throw new InvalidKeyException("Cannot get an encoding of " + |
|
490 |
"the key to be wrapped"); |
|
491 |
} |
|
492 |
||
493 |
result = doFinal(encodedKey, 0, encodedKey.length); |
|
494 |
} catch (BadPaddingException e) { |
|
495 |
// Should never happen |
|
496 |
} |
|
497 |
||
498 |
return result; |
|
499 |
} |
|
500 |
||
501 |
/** |
|
502 |
* Unwrap a previously wrapped key. |
|
503 |
* |
|
504 |
* @param wrappedKey the key to be unwrapped. |
|
505 |
* |
|
506 |
* @param wrappedKeyAlgorithm the algorithm the wrapped key is for. |
|
507 |
* |
|
508 |
* @param wrappedKeyType the type of the wrapped key. |
|
509 |
* This is one of <code>Cipher.SECRET_KEY</code>, |
|
510 |
* <code>Cipher.PRIVATE_KEY</code>, or <code>Cipher.PUBLIC_KEY</code>. |
|
511 |
* |
|
512 |
* @return the unwrapped key. |
|
513 |
* |
|
514 |
* @exception NoSuchAlgorithmException if no installed providers |
|
515 |
* can create keys of type <code>wrappedKeyType</code> for the |
|
516 |
* <code>wrappedKeyAlgorithm</code>. |
|
517 |
* |
|
518 |
* @exception InvalidKeyException if <code>wrappedKey</code> does not |
|
519 |
* represent a wrapped key of type <code>wrappedKeyType</code> for |
|
520 |
* the <code>wrappedKeyAlgorithm</code>. |
|
521 |
*/ |
|
522 |
Key unwrap(byte[] wrappedKey, |
|
523 |
String wrappedKeyAlgorithm, |
|
524 |
int wrappedKeyType) |
|
525 |
throws InvalidKeyException, NoSuchAlgorithmException { |
|
526 |
byte[] encodedKey; |
|
527 |
try { |
|
528 |
encodedKey = doFinal(wrappedKey, 0, wrappedKey.length); |
|
529 |
} catch (BadPaddingException ePadding) { |
|
530 |
throw new InvalidKeyException("The wrapped key is not padded " + |
|
531 |
"correctly"); |
|
532 |
} catch (IllegalBlockSizeException eBlockSize) { |
|
533 |
throw new InvalidKeyException("The wrapped key does not have " + |
|
534 |
"the correct length"); |
|
535 |
} |
|
536 |
return ConstructKeys.constructKey(encodedKey, wrappedKeyAlgorithm, |
|
537 |
wrappedKeyType); |
|
538 |
} |
|
539 |
} |