From bd719256a3ec6509d311a1f25c7be349e6eee7be Mon Sep 17 00:00:00 2001 From: valeriep Date: Fri, 13 Apr 2018 18:07:58 +0000 Subject: [PATCH] 8197441: Signature#initSign/initVerify for an invalid private/public key fails with ClassCastException for SunPKCS11 provider Summary: Rethrow ClassCastException with InvalidKeyException Reviewed-by: ascarpino --- .../sun/security/pkcs11/P11Signature.java | 21 ++++++++++++------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/share/classes/sun/security/pkcs11/P11Signature.java b/src/share/classes/sun/security/pkcs11/P11Signature.java index b09c7b391..48a49f66b 100644 --- a/src/share/classes/sun/security/pkcs11/P11Signature.java +++ b/src/share/classes/sun/security/pkcs11/P11Signature.java @@ -379,14 +379,19 @@ final class P11Signature extends SignatureSpi { if (key instanceof P11Key) { keySize = ((P11Key) key).length(); } else { - if (keyAlgo.equals("RSA")) { - keySize = ((RSAKey) key).getModulus().bitLength(); - } else if (keyAlgo.equals("DSA")) { - keySize = ((DSAKey) key).getParams().getP().bitLength(); - } else if (keyAlgo.equals("EC")) { - keySize = ((ECKey) key).getParams().getCurve().getField().getFieldSize(); - } else { - throw new ProviderException("Error: unsupported algo " + keyAlgo); + try { + if (keyAlgo.equals("RSA")) { + keySize = ((RSAKey) key).getModulus().bitLength(); + } else if (keyAlgo.equals("DSA")) { + keySize = ((DSAKey) key).getParams().getP().bitLength(); + } else if (keyAlgo.equals("EC")) { + keySize = ((ECKey) key).getParams().getCurve().getField().getFieldSize(); + } else { + throw new ProviderException("Error: unsupported algo " + keyAlgo); + } + } catch (ClassCastException cce) { + throw new InvalidKeyException(keyAlgo + + " key must be the right type", cce); } } if ((minKeySize != -1) && (keySize < minKeySize)) { -- GitLab