8265500: Some impls of javax.crypto.Cipher.init() do not throw UnsupportedOperationExc for unsupported modes

Reviewed-by: xuelei
This commit is contained in:
Valerie Peng 2021-06-17 23:27:54 +00:00
parent 9130b8a9d7
commit 80dc262e81
8 changed files with 302 additions and 53 deletions

View file

@ -179,14 +179,16 @@ public final class ARCFOURCipher extends CipherSpi {
init(opmode, key);
}
// init method. Check opmode and key, then call init(byte[]).
// init method. Check key, then call init(byte[]).
private void init(int opmode, Key key) throws InvalidKeyException {
// Cipher.init() already checks opmode to be:
// ENCRYPT_MODE/DECRYPT_MODE/WRAP_MODE/UNWRAP_MODE
if (lastKey != null) {
Arrays.fill(lastKey, (byte)0);
}
if ((opmode < Cipher.ENCRYPT_MODE) || (opmode > Cipher.UNWRAP_MODE)) {
throw new InvalidKeyException("Unknown opmode: " + opmode);
}
lastKey = getEncodedKey(key);
init(lastKey);
}

View file

@ -535,12 +535,11 @@ abstract class ChaCha20Cipher extends CipherSpi {
*/
private void init(int opmode, Key key, byte[] newNonce)
throws InvalidKeyException {
// Cipher.init() already checks opmode to be:
// ENCRYPT_MODE/DECRYPT_MODE/WRAP_MODE/UNWRAP_MODE
if ((opmode == Cipher.WRAP_MODE) || (opmode == Cipher.UNWRAP_MODE)) {
throw new UnsupportedOperationException(
"WRAP_MODE and UNWRAP_MODE are not currently supported");
} else if ((opmode != Cipher.ENCRYPT_MODE) &&
(opmode != Cipher.DECRYPT_MODE)) {
throw new InvalidKeyException("Unknown opmode: " + opmode);
}
// Make sure that the provided key and nonce are unique before

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -261,7 +261,8 @@ public final class RSACipher extends CipherSpi {
encrypt = false;
break;
default:
throw new InvalidKeyException("Unknown mode: " + opmode);
// should never happen; checked by Cipher.init()
throw new AssertionError("Unknown mode: " + opmode);
}
RSAKey rsaKey = RSAKeyFactory.toRSAKey(key);
if (rsaKey instanceof RSAPublicKey) {