226 protected void engineInit(int opmode, Key key,
227 AlgorithmParameters params, SecureRandom random)
228 throws InvalidKeyException, InvalidAlgorithmParameterException {
229 if (params == null) {
230 init(opmode, key, random, null);
231 } else {
232 try {
233 OAEPParameterSpec spec =
234 params.getParameterSpec(OAEPParameterSpec.class);
235 init(opmode, key, random, spec);
236 } catch (InvalidParameterSpecException ipse) {
237 InvalidAlgorithmParameterException iape =
238 new InvalidAlgorithmParameterException("Wrong parameter");
239 iape.initCause(ipse);
240 throw iape;
241 }
242 }
243 }
244
245 // initialize this cipher
246 private void init(int opmode, Key key, SecureRandom random,
247 AlgorithmParameterSpec params)
248 throws InvalidKeyException, InvalidAlgorithmParameterException {
249 boolean encrypt;
250 switch (opmode) {
251 case Cipher.ENCRYPT_MODE:
252 case Cipher.WRAP_MODE:
253 encrypt = true;
254 break;
255 case Cipher.DECRYPT_MODE:
256 case Cipher.UNWRAP_MODE:
257 encrypt = false;
258 break;
259 default:
260 throw new InvalidKeyException("Unknown mode: " + opmode);
261 }
262 RSAKey rsaKey = RSAKeyFactory.toRSAKey(key);
263 if (key instanceof RSAPublicKey) {
264 mode = encrypt ? MODE_ENCRYPT : MODE_VERIFY;
265 publicKey = (RSAPublicKey)key;
407 // see JCE spec
408 protected byte[] engineWrap(Key key) throws InvalidKeyException,
409 IllegalBlockSizeException {
410 byte[] encoded = key.getEncoded();
411 if ((encoded == null) || (encoded.length == 0)) {
412 throw new InvalidKeyException("Could not obtain encoded key");
413 }
414 if (encoded.length > buffer.length) {
415 throw new InvalidKeyException("Key is too long for wrapping");
416 }
417 update(encoded, 0, encoded.length);
418 try {
419 return doFinal();
420 } catch (BadPaddingException e) {
421 // should not occur
422 throw new InvalidKeyException("Wrapping failed", e);
423 }
424 }
425
426 // see JCE spec
427 protected Key engineUnwrap(byte[] wrappedKey, String algorithm,
428 int type) throws InvalidKeyException, NoSuchAlgorithmException {
429 if (wrappedKey.length > buffer.length) {
430 throw new InvalidKeyException("Key is too long for unwrapping");
431 }
432
433 boolean isTlsRsaPremasterSecret =
434 algorithm.equals("TlsRsaPremasterSecret");
435 Exception failover = null;
436 byte[] encoded = null;
437
438 update(wrappedKey, 0, wrappedKey.length);
439 try {
440 encoded = doFinal();
441 } catch (BadPaddingException e) {
442 if (isTlsRsaPremasterSecret) {
443 failover = e;
444 } else {
445 throw new InvalidKeyException("Unwrapping failed", e);
446 }
|
226 protected void engineInit(int opmode, Key key,
227 AlgorithmParameters params, SecureRandom random)
228 throws InvalidKeyException, InvalidAlgorithmParameterException {
229 if (params == null) {
230 init(opmode, key, random, null);
231 } else {
232 try {
233 OAEPParameterSpec spec =
234 params.getParameterSpec(OAEPParameterSpec.class);
235 init(opmode, key, random, spec);
236 } catch (InvalidParameterSpecException ipse) {
237 InvalidAlgorithmParameterException iape =
238 new InvalidAlgorithmParameterException("Wrong parameter");
239 iape.initCause(ipse);
240 throw iape;
241 }
242 }
243 }
244
245 // initialize this cipher
246 @SuppressWarnings("deprecation")
247 private void init(int opmode, Key key, SecureRandom random,
248 AlgorithmParameterSpec params)
249 throws InvalidKeyException, InvalidAlgorithmParameterException {
250 boolean encrypt;
251 switch (opmode) {
252 case Cipher.ENCRYPT_MODE:
253 case Cipher.WRAP_MODE:
254 encrypt = true;
255 break;
256 case Cipher.DECRYPT_MODE:
257 case Cipher.UNWRAP_MODE:
258 encrypt = false;
259 break;
260 default:
261 throw new InvalidKeyException("Unknown mode: " + opmode);
262 }
263 RSAKey rsaKey = RSAKeyFactory.toRSAKey(key);
264 if (key instanceof RSAPublicKey) {
265 mode = encrypt ? MODE_ENCRYPT : MODE_VERIFY;
266 publicKey = (RSAPublicKey)key;
408 // see JCE spec
409 protected byte[] engineWrap(Key key) throws InvalidKeyException,
410 IllegalBlockSizeException {
411 byte[] encoded = key.getEncoded();
412 if ((encoded == null) || (encoded.length == 0)) {
413 throw new InvalidKeyException("Could not obtain encoded key");
414 }
415 if (encoded.length > buffer.length) {
416 throw new InvalidKeyException("Key is too long for wrapping");
417 }
418 update(encoded, 0, encoded.length);
419 try {
420 return doFinal();
421 } catch (BadPaddingException e) {
422 // should not occur
423 throw new InvalidKeyException("Wrapping failed", e);
424 }
425 }
426
427 // see JCE spec
428 @SuppressWarnings("deprecation")
429 protected Key engineUnwrap(byte[] wrappedKey, String algorithm,
430 int type) throws InvalidKeyException, NoSuchAlgorithmException {
431 if (wrappedKey.length > buffer.length) {
432 throw new InvalidKeyException("Key is too long for unwrapping");
433 }
434
435 boolean isTlsRsaPremasterSecret =
436 algorithm.equals("TlsRsaPremasterSecret");
437 Exception failover = null;
438 byte[] encoded = null;
439
440 update(wrappedKey, 0, wrappedKey.length);
441 try {
442 encoded = doFinal();
443 } catch (BadPaddingException e) {
444 if (isTlsRsaPremasterSecret) {
445 failover = e;
446 } else {
447 throw new InvalidKeyException("Unwrapping failed", e);
448 }
|