bc-java
bc-java copied to clipboard
RSA DigestSignatureSpi returns true from supportsParameter for PKCS#8 encoded ECPrivateKey
In JDK 21 java.security.Signature method the provider chooser algorithm contains this code:
// if provider says it does not support this key, ignore it
if (key != null && s.supportsParameter(key) == false) {
continue;
}
org.bouncycastle.jcajce.provider.asymmetric.rsa.DigestSignatureSpi$SHA1 returns true for PKCS#8 encoded ECPrivateKey
The algorithm choose that provider and in the provider init method throws an exception because it is not a RSAPrivateKey
protected void engineInitSign(
PrivateKey privateKey)
throws InvalidKeyException
{
if (!(privateKey instanceof RSAPrivateKey))
{
throw new InvalidKeyException("Supplied key (" + getType(privateKey) + ") is not a RSAPrivateKey instance");
}
CipherParameters param = RSAUtil.generatePrivateKeyParameter((RSAPrivateKey)privateKey);
digest.reset();
cipher.init(true, param);
}
Edited for formatting by @cipherboy.
Yes, it appears the default implementation checks the format before it checks the key class, returning true if it matches. I don't think it makes sense for the JVM to be doing a format check here, all PrivateKeys will have the format PKCS#8, it should only be checking the key class.