Skip to content

Commit 170d9f0

Browse files
committed
Raise InvalidKeyException, rather than let a foreign provider's unchecked exception escape, when a signature, cipher or key-agreement service is initialised with a private key whose parameters are not accessible - a hardware-backed key whose getModulus/getX/getS/getParams throws - across the RSA, DSA, EC, DH and ElGamal key-parameter helpers, relates to github #1440.
1 parent 46f8389 commit 170d9f0

11 files changed

Lines changed: 331 additions & 27 deletions

File tree

‎ant/jdk13.xml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@
273273
<exclude name="**/pqc/**/BitStringTest.java" />
274274
<exclude name="**/pqc/**/QTESLA*.java" />
275275
<exclude name="**/pqc/legacy/**/*.java" />
276+
<exclude name="**/jce/provider/test/HardwareKeyInvalidKeyExceptionTest.java" />
276277
<exclude name="**/jcajce/provider/test/*.java" />
277278
<exclude name="**/jce/provider/test/JceTestUtil.java" />
278279
<exclude name="**/speedy/**" />
@@ -382,6 +383,7 @@
382383
<exclude name="**/jce/provider/test/AEADTest.java" />
383384
<exclude name="**/jce/provider/test/ECEncodingTest.java" />
384385
<exclude name="**/jce/provider/test/CertLocaleTest.java" />
386+
<exclude name="**/jce/provider/test/HardwareKeyInvalidKeyExceptionTest.java" />
385387
<!-- synced from ant/jdk14.xml's prov/src/test/java fileset -->
386388
<exclude name="**/jce/provider/test/JcajceParameterSpecTest.java" />
387389
<exclude name="**/jce/provider/test/XChaCha20Poly1305Test.java" />

‎ant/jdk14.xml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,8 @@
237237
<exclude name="**/jce/provider/test/ChaCha20Poly1305Test.java"/>
238238
<exclude name="**/jce/provider/test/XChaCha20Poly1305Test.java"/>
239239
<exclude name="**/jce/provider/test/PKCS12SecretKeyStoreTest.java"/>
240+
<!-- uses java.security.interfaces.ECPrivateKey (Java 5) -->
241+
<exclude name="**/jce/provider/test/HardwareKeyInvalidKeyExceptionTest.java"/>
240242
<exclude name="**/DetDSATest.java"/>
241243
<exclude name="**/ECDSA5Test.java"/>
242244
<exclude name="**/CRL5Test.java"/>

‎docs/releasenotes.html‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ <h3>2.1.2 Defects Fixed</h3>
131131
<li>None of the javax.crypto.KEM encapsulators destroyed the SecretWithEncapsulation the mechanism handed them, so each left the mechanism's own session key live in the heap for as long as the KEM.Encapsulator object was reachable - ML-KEM, HQC, NTRU, NTRU Prime and SM9 were all affected. Clearing the derived key was not enough to cover it: SecretWithEncapsulationImpl.getSecret() and getEncapsulation() hand back clones of their internal arrays, so KdfUtil.makeKeyBytes clearing the secret clone it is passed never reached the session key inside, and only destroy() does. Each encapsulator now takes the encapsulation and the shared secret and then destroys the SecretWithEncapsulation in a finally block, as the sibling Cipher services in the same packages already did, and as the ISO/IEC 18033-2 FrodoKEM and Classic McEliece encapsulators added in this release do. The order matters, since destroy() clears the encapsulation as well as the session key. Nothing observable changes for a caller - the shared secret and encapsulation returned are the same as before, and are the caller's own copies.</li>
132132
<li>The KEM KeyGenerator services had the same missing key-size check as the javax.crypto.KEM ones described under Additional Features below, and are corrected here for the older API too: a KEMGenerateSpec or KEMExtractSpec built withNoKdf() and given a key size larger than the mechanism's session key overran the shared secret and threw ArrayIndexOutOfBoundsException out of KeyGenerator.generateKey(), which has no way to describe such a failure. The conventional 256-bit request failed that way for FrodoKEM's 976 parameter sets, whose session key is 192 bits, as did any request above 256 bits for ML-KEM, Classic McEliece and the composite KEMs, whose session keys are 256 bits. These services take a KEMKDFSpec rather than a KTSParameterSpec and so never pass through KdfUtil.resolveKemSpec, where the KEM services now have their spec validated up front; the check therefore sits in KdfUtil.makeKeyBytes itself, which covers them and any future caller of it. The request is refused with an IllegalArgumentException naming both sizes rather than quietly met at the shorter length, since a caller that asked for a 256-bit AES key and was handed a 192-bit one has no way of telling. A request the session key can satisfy is unaffected and produces exactly the key material it did before, and the Cipher and KTS key-wrapping paths were never affected - there the requested size is only an upper bound on a wrapping key both parties derive the same way from the same secret, so WrapUtil clamps it to the secret it has.</li>
133133
<li>The BCJSSE provider now only computes active early key share groups for clients offering TLS 1.3+. Previously it was computed for all connections, which was mostly harmless, but could generate misleading log messages (at WARNING level) for servers or pre-TLS1.3 clients where the logged condition was irrelevant (github #2392).</li>
134+
<li>Initialising a BC signature, cipher or key-agreement service with a private key from another provider whose key material is not accessible - a hardware-backed key whose getModulus() / getX() / getS() / getParams() throws, as an IBM CCA RSAPrivateHWKey does with "Hardware error, function getModulus has no meaning in hardware" - let that provider-specific unchecked UnsupportedOperationException escape a method declared to throw only InvalidKeyException, so an mTLS handshake failed with a raw hardware error rather than the documented type. The key-parameter helpers that examine a key through a java.security.interfaces or javax.crypto.interfaces type a foreign provider may implement - RSAUtil, DSAUtil, ECUtil (its java.security.interfaces.ECPrivateKey branch), both DHUtil copies and ElGamalUtil - now catch a failure to read the key's parameters and raise InvalidKeyException with the original exception chained as its cause. A caller, or a JSSE layer that keys its provider fallback on InvalidKeyException, sees the declared type and a clear message. This does not let BC sign or decrypt with a non-exportable hardware key, which is not possible; it makes the refusal in contract. BC's own keys and any exportable key are unaffected (github #1440).</li>
134135
</ul>
135136

136137
<h3>2.1.3 Additional Features and Functionality</h3>

‎prov/src/main/java/org/bouncycastle/jcajce/provider/asymmetric/dsa/DSAUtil.java‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import org.bouncycastle.crypto.params.DSAPrivateKeyParameters;
1717
import org.bouncycastle.internal.asn1.oiw.OIWObjectIdentifiers;
1818
import org.bouncycastle.util.Arrays;
19+
import org.bouncycastle.jcajce.provider.util.SecurityExceptions;
1920
import org.bouncycastle.util.Fingerprint;
2021

2122
/**
@@ -97,10 +98,18 @@ static public AsymmetricKeyParameter generatePrivateKeyParameter(
9798
{
9899
DSAPrivateKey k = (DSAPrivateKey)key;
99100

100-
return new DSAPrivateKeyParameters(k.getX(),
101-
new DSAParameters(k.getParams().getP(), k.getParams().getQ(), k.getParams().getG()));
101+
try
102+
{
103+
return new DSAPrivateKeyParameters(k.getX(),
104+
new DSAParameters(k.getParams().getP(), k.getParams().getQ(), k.getParams().getG()));
105+
}
106+
catch (RuntimeException e)
107+
{
108+
throw SecurityExceptions.invalidKeyException(
109+
"cannot recognise DSA private key - its parameters are not accessible (a hardware-backed key?): " + e.getMessage(), e);
110+
}
102111
}
103-
112+
104113
throw new InvalidKeyException("can't identify DSA private key.");
105114
}
106115

‎prov/src/main/java/org/bouncycastle/jcajce/provider/asymmetric/elgamal/ElGamalUtil.java‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.bouncycastle.crypto.params.ElGamalPrivateKeyParameters;
1313
import org.bouncycastle.crypto.params.ElGamalPublicKeyParameters;
1414
import org.bouncycastle.jce.interfaces.ElGamalPrivateKey;
15+
import org.bouncycastle.jcajce.provider.util.SecurityExceptions;
1516
import org.bouncycastle.jce.interfaces.ElGamalPublicKey;
1617

1718
/**
@@ -39,7 +40,7 @@ else if (key instanceof DHPublicKey)
3940
new ElGamalParameters(k.getParams().getP(), k.getParams().getG()));
4041
}
4142

42-
throw new InvalidKeyException("can't identify public key for El Gamal.");
43+
throw new InvalidKeyException("can't identify public key for ElGamal.");
4344
}
4445

4546
static public AsymmetricKeyParameter generatePrivateKeyParameter(
@@ -57,10 +58,18 @@ else if (key instanceof DHPrivateKey)
5758
{
5859
DHPrivateKey k = (DHPrivateKey)key;
5960

60-
return new ElGamalPrivateKeyParameters(k.getX(),
61-
new ElGamalParameters(k.getParams().getP(), k.getParams().getG()));
61+
try
62+
{
63+
return new ElGamalPrivateKeyParameters(k.getX(),
64+
new ElGamalParameters(k.getParams().getP(), k.getParams().getG()));
65+
}
66+
catch (RuntimeException e)
67+
{
68+
throw SecurityExceptions.invalidKeyException(
69+
"cannot recognise ElGamal private key - its parameters are not accessible (a hardware-backed key?): " + e.getMessage(), e);
70+
}
6271
}
63-
64-
throw new InvalidKeyException("can't identify private key for El Gamal.");
72+
73+
throw new InvalidKeyException("can't identify private key for ElGamal.");
6574
}
6675
}

‎prov/src/main/java/org/bouncycastle/jcajce/provider/asymmetric/rsa/RSAUtil.java‎

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.bouncycastle.jcajce.provider.asymmetric.rsa;
22

33
import java.math.BigInteger;
4+
import java.security.InvalidKeyException;
45
import java.security.interfaces.RSAPrivateCrtKey;
56
import java.security.interfaces.RSAPrivateKey;
67
import java.security.interfaces.RSAPublicKey;
@@ -10,6 +11,7 @@
1011
import org.bouncycastle.asn1.x509.X509ObjectIdentifiers;
1112
import org.bouncycastle.crypto.params.RSAKeyParameters;
1213
import org.bouncycastle.crypto.params.RSAPrivateCrtKeyParameters;
14+
import org.bouncycastle.jcajce.provider.util.SecurityExceptions;
1315
import org.bouncycastle.util.Fingerprint;
1416

1517
/**
@@ -53,25 +55,34 @@ static RSAKeyParameters generatePublicKeyParameter(
5355

5456
static RSAKeyParameters generatePrivateKeyParameter(
5557
RSAPrivateKey key)
58+
throws InvalidKeyException
5659
{
5760
if (key instanceof BCRSAPrivateKey)
5861
{
5962
return ((BCRSAPrivateKey)key).engineGetKeyParameters();
6063
}
6164

62-
if (key instanceof RSAPrivateCrtKey)
65+
try
6366
{
64-
RSAPrivateCrtKey k = (RSAPrivateCrtKey)key;
67+
if (key instanceof RSAPrivateCrtKey)
68+
{
69+
RSAPrivateCrtKey k = (RSAPrivateCrtKey)key;
70+
71+
return new RSAPrivateCrtKeyParameters(k.getModulus(),
72+
k.getPublicExponent(), k.getPrivateExponent(),
73+
k.getPrimeP(), k.getPrimeQ(), k.getPrimeExponentP(), k.getPrimeExponentQ(), k.getCrtCoefficient());
74+
}
75+
else
76+
{
77+
RSAPrivateKey k = key;
6578

66-
return new RSAPrivateCrtKeyParameters(k.getModulus(),
67-
k.getPublicExponent(), k.getPrivateExponent(),
68-
k.getPrimeP(), k.getPrimeQ(), k.getPrimeExponentP(), k.getPrimeExponentQ(), k.getCrtCoefficient());
79+
return new RSAKeyParameters(true, k.getModulus(), k.getPrivateExponent());
80+
}
6981
}
70-
else
82+
catch (RuntimeException e)
7183
{
72-
RSAPrivateKey k = key;
73-
74-
return new RSAKeyParameters(true, k.getModulus(), k.getPrivateExponent());
84+
throw SecurityExceptions.invalidKeyException(
85+
"cannot recognise RSA private key - its parameters are not accessible (a hardware-backed key?): " + e.getMessage(), e);
7586
}
7687
}
7788

‎prov/src/main/java/org/bouncycastle/jcajce/provider/asymmetric/util/DHUtil.java‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.bouncycastle.crypto.params.DHPrivateKeyParameters;
1313
import org.bouncycastle.crypto.params.DHPublicKeyParameters;
1414
import org.bouncycastle.jcajce.provider.asymmetric.dh.BCDHPublicKey;
15+
import org.bouncycastle.jcajce.provider.util.SecurityExceptions;
1516

1617
/**
1718
* utility class for converting jce/jca DH objects
@@ -46,10 +47,18 @@ static public AsymmetricKeyParameter generatePrivateKeyParameter(
4647
{
4748
DHPrivateKey k = (DHPrivateKey)key;
4849

49-
return new DHPrivateKeyParameters(k.getX(),
50-
new DHParameters(k.getParams().getP(), k.getParams().getG(), null, k.getParams().getL()));
50+
try
51+
{
52+
return new DHPrivateKeyParameters(k.getX(),
53+
new DHParameters(k.getParams().getP(), k.getParams().getG(), null, k.getParams().getL()));
54+
}
55+
catch (RuntimeException e)
56+
{
57+
throw SecurityExceptions.invalidKeyException(
58+
"cannot recognise DH private key - its parameters are not accessible (a hardware-backed key?): " + e.getMessage(), e);
59+
}
5160
}
52-
61+
5362
throw new InvalidKeyException("can't identify DH private key.");
5463
}
5564
}

‎prov/src/main/java/org/bouncycastle/jcajce/provider/asymmetric/util/ECUtil.java‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.bouncycastle.math.ec.FixedPointCombMultiplier;
3333
import org.bouncycastle.util.Arrays;
3434
import org.bouncycastle.util.Fingerprint;
35+
import org.bouncycastle.jcajce.provider.util.SecurityExceptions;
3536
import org.bouncycastle.util.Strings;
3637

3738
/**
@@ -252,10 +253,18 @@ public static AsymmetricKeyParameter generatePrivateKeyParameter(
252253
else if (key instanceof java.security.interfaces.ECPrivateKey)
253254
{
254255
java.security.interfaces.ECPrivateKey privKey = (java.security.interfaces.ECPrivateKey)key;
255-
ECParameterSpec s = EC5Util.convertSpec(privKey.getParams());
256-
return new ECPrivateKeyParameters(
257-
privKey.getS(),
258-
new ECDomainParameters(s.getCurve(), s.getG(), s.getN(), s.getH(), s.getSeed()));
256+
try
257+
{
258+
ECParameterSpec s = EC5Util.convertSpec(privKey.getParams());
259+
return new ECPrivateKeyParameters(
260+
privKey.getS(),
261+
new ECDomainParameters(s.getCurve(), s.getG(), s.getN(), s.getH(), s.getSeed()));
262+
}
263+
catch (RuntimeException e)
264+
{
265+
throw SecurityExceptions.invalidKeyException(
266+
"cannot recognise EC private key - its parameters are not accessible (a hardware-backed key?): " + e.getMessage(), e);
267+
}
259268
}
260269
else
261270
{

‎prov/src/main/java/org/bouncycastle/jce/provider/DHUtil.java‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.bouncycastle.crypto.params.DHParameters;
1212
import org.bouncycastle.crypto.params.DHPrivateKeyParameters;
1313
import org.bouncycastle.crypto.params.DHPublicKeyParameters;
14+
import org.bouncycastle.jcajce.provider.util.SecurityExceptions;
1415

1516
/**
1617
* utility class for converting jce/jca DH objects
@@ -41,10 +42,18 @@ static public AsymmetricKeyParameter generatePrivateKeyParameter(
4142
{
4243
DHPrivateKey k = (DHPrivateKey)key;
4344

44-
return new DHPrivateKeyParameters(k.getX(),
45-
new DHParameters(k.getParams().getP(), k.getParams().getG(), null, k.getParams().getL()));
45+
try
46+
{
47+
return new DHPrivateKeyParameters(k.getX(),
48+
new DHParameters(k.getParams().getP(), k.getParams().getG(), null, k.getParams().getL()));
49+
}
50+
catch (RuntimeException e)
51+
{
52+
throw SecurityExceptions.invalidKeyException(
53+
"cannot recognise DH private key - its parameters are not accessible (a hardware-backed key?): " + e.getMessage(), e);
54+
}
4655
}
47-
56+
4857
throw new InvalidKeyException("can't identify DH private key.");
4958
}
5059
}

0 commit comments

Comments
 (0)