Skip to content

Commit d6101df

Browse files
author
Paul Hohensee
committedDec 18, 2022
8254717: isAssignableFrom checks in KeyFactorySpi.engineGetKeySpec appear to be backwards
Reviewed-by: andrew Backport-of: a777e82cd81fb5f02b09c4917b2a44f94c603939
1 parent 99ff02e commit d6101df

15 files changed

+126
-54
lines changed
 

‎jdk/src/share/classes/com/sun/crypto/provider/DESKeyFactory.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -106,7 +106,7 @@ protected KeySpec engineGetKeySpec(SecretKey key, Class<?> keySpec)
106106

107107
// Check if requested key spec is amongst the valid ones
108108
if ((keySpec != null) &&
109-
DESKeySpec.class.isAssignableFrom(keySpec)) {
109+
keySpec.isAssignableFrom(DESKeySpec.class)) {
110110
return new DESKeySpec(key.getEncoded());
111111

112112
} else {

‎jdk/src/share/classes/com/sun/crypto/provider/DESedeKeyFactory.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -102,7 +102,7 @@ protected KeySpec engineGetKeySpec(SecretKey key, Class<?> keySpec)
102102
&& (key.getFormat().equalsIgnoreCase("RAW"))) {
103103

104104
// Check if requested key spec is amongst the valid ones
105-
if (DESedeKeySpec.class.isAssignableFrom(keySpec)) {
105+
if (keySpec.isAssignableFrom(DESedeKeySpec.class)) {
106106
return new DESedeKeySpec(key.getEncoded());
107107

108108
} else {

‎jdk/src/share/classes/com/sun/crypto/provider/DHKeyFactory.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -145,15 +145,15 @@ T engineGetKeySpec(Key key, Class<T> keySpec)
145145

146146
if (key instanceof javax.crypto.interfaces.DHPublicKey) {
147147

148-
if (DHPublicKeySpec.class.isAssignableFrom(keySpec)) {
148+
if (keySpec.isAssignableFrom(DHPublicKeySpec.class)) {
149149
javax.crypto.interfaces.DHPublicKey dhPubKey
150150
= (javax.crypto.interfaces.DHPublicKey) key;
151151
params = dhPubKey.getParams();
152152
return keySpec.cast(new DHPublicKeySpec(dhPubKey.getY(),
153153
params.getP(),
154154
params.getG()));
155155

156-
} else if (X509EncodedKeySpec.class.isAssignableFrom(keySpec)) {
156+
} else if (keySpec.isAssignableFrom(X509EncodedKeySpec.class)) {
157157
return keySpec.cast(new X509EncodedKeySpec(key.getEncoded()));
158158

159159
} else {
@@ -163,15 +163,15 @@ T engineGetKeySpec(Key key, Class<T> keySpec)
163163

164164
} else if (key instanceof javax.crypto.interfaces.DHPrivateKey) {
165165

166-
if (DHPrivateKeySpec.class.isAssignableFrom(keySpec)) {
166+
if (keySpec.isAssignableFrom(DHPrivateKeySpec.class)) {
167167
javax.crypto.interfaces.DHPrivateKey dhPrivKey
168168
= (javax.crypto.interfaces.DHPrivateKey)key;
169169
params = dhPrivKey.getParams();
170170
return keySpec.cast(new DHPrivateKeySpec(dhPrivKey.getX(),
171171
params.getP(),
172172
params.getG()));
173173

174-
} else if (PKCS8EncodedKeySpec.class.isAssignableFrom(keySpec)) {
174+
} else if (keySpec.isAssignableFrom(PKCS8EncodedKeySpec.class)) {
175175
return keySpec.cast(new PKCS8EncodedKeySpec(key.getEncoded()));
176176

177177
} else {

‎jdk/src/share/classes/sun/security/ec/ECKeyFactory.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2006, 2012, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2006, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -256,22 +256,22 @@ protected <T extends KeySpec> T engineGetKeySpec(Key key, Class<T> keySpec)
256256
}
257257
if (key instanceof ECPublicKey) {
258258
ECPublicKey ecKey = (ECPublicKey)key;
259-
if (ECPublicKeySpec.class.isAssignableFrom(keySpec)) {
259+
if (keySpec.isAssignableFrom(ECPublicKeySpec.class)) {
260260
return keySpec.cast(new ECPublicKeySpec(
261261
ecKey.getW(),
262262
ecKey.getParams()
263263
));
264-
} else if (X509EncodedKeySpec.class.isAssignableFrom(keySpec)) {
264+
} else if (keySpec.isAssignableFrom(X509EncodedKeySpec.class)) {
265265
return keySpec.cast(new X509EncodedKeySpec(key.getEncoded()));
266266
} else {
267267
throw new InvalidKeySpecException
268268
("KeySpec must be ECPublicKeySpec or "
269269
+ "X509EncodedKeySpec for EC public keys");
270270
}
271271
} else if (key instanceof ECPrivateKey) {
272-
if (PKCS8EncodedKeySpec.class.isAssignableFrom(keySpec)) {
272+
if (keySpec.isAssignableFrom(PKCS8EncodedKeySpec.class)) {
273273
return keySpec.cast(new PKCS8EncodedKeySpec(key.getEncoded()));
274-
} else if (ECPrivateKeySpec.class.isAssignableFrom(keySpec)) {
274+
} else if (keySpec.isAssignableFrom(ECPrivateKeySpec.class)) {
275275
ECPrivateKey ecKey = (ECPrivateKey)key;
276276
return keySpec.cast(new ECPrivateKeySpec(
277277
ecKey.getS(),

‎jdk/src/share/classes/sun/security/pkcs11/P11DHKeyFactory.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -214,7 +214,7 @@ private PrivateKey generatePrivate(BigInteger x, BigInteger p,
214214

215215
<T extends KeySpec> T implGetPublicKeySpec(P11Key key, Class<T> keySpec,
216216
Session[] session) throws PKCS11Exception, InvalidKeySpecException {
217-
if (DHPublicKeySpec.class.isAssignableFrom(keySpec)) {
217+
if (keySpec.isAssignableFrom(DHPublicKeySpec.class)) {
218218
session[0] = token.getObjSession();
219219
CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {
220220
new CK_ATTRIBUTE(CKA_VALUE),
@@ -241,7 +241,7 @@ <T extends KeySpec> T implGetPublicKeySpec(P11Key key, Class<T> keySpec,
241241

242242
<T extends KeySpec> T implGetPrivateKeySpec(P11Key key, Class<T> keySpec,
243243
Session[] session) throws PKCS11Exception, InvalidKeySpecException {
244-
if (DHPrivateKeySpec.class.isAssignableFrom(keySpec)) {
244+
if (keySpec.isAssignableFrom(DHPrivateKeySpec.class)) {
245245
session[0] = token.getObjSession();
246246
CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {
247247
new CK_ATTRIBUTE(CKA_VALUE),

‎jdk/src/share/classes/sun/security/pkcs11/P11DSAKeyFactory.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -210,7 +210,7 @@ private PrivateKey generatePrivate(BigInteger x, BigInteger p,
210210

211211
<T extends KeySpec> T implGetPublicKeySpec(P11Key key, Class<T> keySpec,
212212
Session[] session) throws PKCS11Exception, InvalidKeySpecException {
213-
if (DSAPublicKeySpec.class.isAssignableFrom(keySpec)) {
213+
if (keySpec.isAssignableFrom(DSAPublicKeySpec.class)) {
214214
session[0] = token.getObjSession();
215215
CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {
216216
new CK_ATTRIBUTE(CKA_VALUE),
@@ -239,7 +239,7 @@ <T extends KeySpec> T implGetPublicKeySpec(P11Key key, Class<T> keySpec,
239239

240240
<T extends KeySpec> T implGetPrivateKeySpec(P11Key key, Class<T> keySpec,
241241
Session[] session) throws PKCS11Exception, InvalidKeySpecException {
242-
if (DSAPrivateKeySpec.class.isAssignableFrom(keySpec)) {
242+
if (keySpec.isAssignableFrom(DSAPrivateKeySpec.class)) {
243243
session[0] = token.getObjSession();
244244
CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {
245245
new CK_ATTRIBUTE(CKA_VALUE),

‎jdk/src/share/classes/sun/security/pkcs11/P11ECKeyFactory.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2006, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2006, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -284,7 +284,7 @@ private PrivateKey generatePrivate(BigInteger s, ECParameterSpec params)
284284

285285
<T extends KeySpec> T implGetPublicKeySpec(P11Key key, Class<T> keySpec,
286286
Session[] session) throws PKCS11Exception, InvalidKeySpecException {
287-
if (ECPublicKeySpec.class.isAssignableFrom(keySpec)) {
287+
if (keySpec.isAssignableFrom(ECPublicKeySpec.class)) {
288288
session[0] = token.getObjSession();
289289
CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {
290290
new CK_ATTRIBUTE(CKA_EC_POINT),
@@ -309,7 +309,7 @@ <T extends KeySpec> T implGetPublicKeySpec(P11Key key, Class<T> keySpec,
309309

310310
<T extends KeySpec> T implGetPrivateKeySpec(P11Key key, Class<T> keySpec,
311311
Session[] session) throws PKCS11Exception, InvalidKeySpecException {
312-
if (ECPrivateKeySpec.class.isAssignableFrom(keySpec)) {
312+
if (keySpec.isAssignableFrom(ECPrivateKeySpec.class)) {
313313
session[0] = token.getObjSession();
314314
CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {
315315
new CK_ATTRIBUTE(CKA_VALUE),

‎jdk/src/share/classes/sun/security/pkcs11/P11KeyFactory.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -73,8 +73,8 @@ protected final <T extends KeySpec> T engineGetKeySpec(Key key, Class<T> keySpec
7373
("key and keySpec must not be null");
7474
}
7575
// delegate to our Java based providers for PKCS#8 and X.509
76-
if (PKCS8EncodedKeySpec.class.isAssignableFrom(keySpec)
77-
|| X509EncodedKeySpec.class.isAssignableFrom(keySpec)) {
76+
if (keySpec.isAssignableFrom(PKCS8EncodedKeySpec.class)
77+
|| keySpec.isAssignableFrom(X509EncodedKeySpec.class)) {
7878
try {
7979
return implGetSoftwareFactory().getKeySpec(key, keySpec);
8080
} catch (GeneralSecurityException e) {

‎jdk/src/share/classes/sun/security/pkcs11/P11RSAKeyFactory.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ private PrivateKey generatePrivate(BigInteger n, BigInteger e,
257257

258258
<T extends KeySpec> T implGetPublicKeySpec(P11Key key, Class<T> keySpec,
259259
Session[] session) throws PKCS11Exception, InvalidKeySpecException {
260-
if (RSAPublicKeySpec.class.isAssignableFrom(keySpec)) {
260+
if (keySpec.isAssignableFrom(RSAPublicKeySpec.class)) {
261261
session[0] = token.getObjSession();
262262
CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {
263263
new CK_ATTRIBUTE(CKA_MODULUS),
@@ -282,7 +282,7 @@ <T extends KeySpec> T implGetPublicKeySpec(P11Key key, Class<T> keySpec,
282282

283283
<T extends KeySpec> T implGetPrivateKeySpec(P11Key key, Class<T> keySpec,
284284
Session[] session) throws PKCS11Exception, InvalidKeySpecException {
285-
if (RSAPrivateCrtKeySpec.class.isAssignableFrom(keySpec)) {
285+
if (keySpec.isAssignableFrom(RSAPrivateCrtKeySpec.class)) {
286286
session[0] = token.getObjSession();
287287
CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {
288288
new CK_ATTRIBUTE(CKA_MODULUS),
@@ -312,7 +312,7 @@ <T extends KeySpec> T implGetPrivateKeySpec(P11Key key, Class<T> keySpec,
312312
attributes[7].getBigInteger()
313313
);
314314
return keySpec.cast(spec);
315-
} else if (RSAPrivateKeySpec.class.isAssignableFrom(keySpec)) {
315+
} else if (keySpec.isAssignableFrom(RSAPrivateKeySpec.class)) {
316316
session[0] = token.getObjSession();
317317
CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {
318318
new CK_ATTRIBUTE(CKA_MODULUS),

‎jdk/src/share/classes/sun/security/pkcs11/P11SecretKeyFactory.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -330,19 +330,19 @@ protected KeySpec engineGetKeySpec(SecretKey key, Class<?> keySpec)
330330
throw new InvalidKeySpecException
331331
("key and keySpec must not be null");
332332
}
333-
if (SecretKeySpec.class.isAssignableFrom(keySpec)) {
333+
if (keySpec.isAssignableFrom(SecretKeySpec.class)) {
334334
return new SecretKeySpec(getKeyBytes(key), algorithm);
335335
} else if (algorithm.equalsIgnoreCase("DES")) {
336336
try {
337-
if (DESKeySpec.class.isAssignableFrom(keySpec)) {
337+
if (keySpec.isAssignableFrom(DESKeySpec.class)) {
338338
return new DESKeySpec(getKeyBytes(key));
339339
}
340340
} catch (InvalidKeyException e) {
341341
throw new InvalidKeySpecException(e);
342342
}
343343
} else if (algorithm.equalsIgnoreCase("DESede")) {
344344
try {
345-
if (DESedeKeySpec.class.isAssignableFrom(keySpec)) {
345+
if (keySpec.isAssignableFrom(DESedeKeySpec.class)) {
346346
return new DESedeKeySpec(getKeyBytes(key));
347347
}
348348
} catch (InvalidKeyException e) {

‎jdk/src/share/classes/sun/security/provider/DSAKeyFactory.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -187,7 +187,7 @@ T engineGetKeySpec(Key key, Class<T> keySpec)
187187
Class<?> x509KeySpec = Class.forName
188188
("java.security.spec.X509EncodedKeySpec");
189189

190-
if (dsaPubKeySpec.isAssignableFrom(keySpec)) {
190+
if (keySpec.isAssignableFrom(dsaPubKeySpec)) {
191191
java.security.interfaces.DSAPublicKey dsaPubKey
192192
= (java.security.interfaces.DSAPublicKey)key;
193193
params = dsaPubKey.getParams();
@@ -196,7 +196,7 @@ T engineGetKeySpec(Key key, Class<T> keySpec)
196196
params.getQ(),
197197
params.getG()));
198198

199-
} else if (x509KeySpec.isAssignableFrom(keySpec)) {
199+
} else if (keySpec.isAssignableFrom(x509KeySpec)) {
200200
return keySpec.cast(new X509EncodedKeySpec(key.getEncoded()));
201201

202202
} else {
@@ -212,7 +212,7 @@ T engineGetKeySpec(Key key, Class<T> keySpec)
212212
Class<?> pkcs8KeySpec = Class.forName
213213
("java.security.spec.PKCS8EncodedKeySpec");
214214

215-
if (dsaPrivKeySpec.isAssignableFrom(keySpec)) {
215+
if (keySpec.isAssignableFrom(dsaPrivKeySpec)) {
216216
java.security.interfaces.DSAPrivateKey dsaPrivKey
217217
= (java.security.interfaces.DSAPrivateKey)key;
218218
params = dsaPrivKey.getParams();
@@ -221,7 +221,7 @@ T engineGetKeySpec(Key key, Class<T> keySpec)
221221
params.getQ(),
222222
params.getG()));
223223

224-
} else if (pkcs8KeySpec.isAssignableFrom(keySpec)) {
224+
} else if (keySpec.isAssignableFrom(pkcs8KeySpec)) {
225225
return keySpec.cast(new PKCS8EncodedKeySpec(key.getEncoded()));
226226

227227
} else {

‎jdk/src/share/classes/sun/security/rsa/RSAKeyFactory.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -404,23 +404,23 @@ protected <T extends KeySpec> T engineGetKeySpec(Key key, Class<T> keySpec)
404404
}
405405
if (key instanceof RSAPublicKey) {
406406
RSAPublicKey rsaKey = (RSAPublicKey)key;
407-
if (RSA_PUB_KEYSPEC_CLS.isAssignableFrom(keySpec)) {
407+
if (keySpec.isAssignableFrom(RSA_PUB_KEYSPEC_CLS)) {
408408
return keySpec.cast(new RSAPublicKeySpec(
409409
rsaKey.getModulus(),
410410
rsaKey.getPublicExponent(),
411411
rsaKey.getParams()
412412
));
413-
} else if (X509_KEYSPEC_CLS.isAssignableFrom(keySpec)) {
413+
} else if (keySpec.isAssignableFrom(X509_KEYSPEC_CLS)) {
414414
return keySpec.cast(new X509EncodedKeySpec(key.getEncoded()));
415415
} else {
416416
throw new InvalidKeySpecException
417417
("KeySpec must be RSAPublicKeySpec or "
418418
+ "X509EncodedKeySpec for RSA public keys");
419419
}
420420
} else if (key instanceof RSAPrivateKey) {
421-
if (PKCS8_KEYSPEC_CLS.isAssignableFrom(keySpec)) {
421+
if (keySpec.isAssignableFrom(PKCS8_KEYSPEC_CLS)) {
422422
return keySpec.cast(new PKCS8EncodedKeySpec(key.getEncoded()));
423-
} else if (RSA_PRIVCRT_KEYSPEC_CLS.isAssignableFrom(keySpec)) {
423+
} else if (keySpec.isAssignableFrom(RSA_PRIVCRT_KEYSPEC_CLS)) {
424424
if (key instanceof RSAPrivateCrtKey) {
425425
RSAPrivateCrtKey crtKey = (RSAPrivateCrtKey)key;
426426
return keySpec.cast(new RSAPrivateCrtKeySpec(
@@ -438,7 +438,7 @@ protected <T extends KeySpec> T engineGetKeySpec(Key key, Class<T> keySpec)
438438
throw new InvalidKeySpecException
439439
("RSAPrivateCrtKeySpec can only be used with CRT keys");
440440
}
441-
} else if (RSA_PRIV_KEYSPEC_CLS.isAssignableFrom(keySpec)) {
441+
} else if (keySpec.isAssignableFrom(RSA_PRIV_KEYSPEC_CLS)) {
442442
RSAPrivateKey rsaKey = (RSAPrivateKey)key;
443443
return keySpec.cast(new RSAPrivateKeySpec(
444444
rsaKey.getModulus(),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright (c) 2021, Amazon.com, Inc. or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/**
25+
* @test
26+
* @bug 8254717
27+
* @summary isAssignableFrom checks in KeyFactorySpi.engineGetKeySpec appear to be backwards.
28+
* @author Greg Rubin, Ziyi Luo
29+
*/
30+
31+
import java.security.KeyFactory;
32+
import java.security.KeyPair;
33+
import java.security.KeyPairGenerator;
34+
import java.security.spec.*;
35+
36+
public class KeyFactoryGetKeySpecForInvalidSpec {
37+
public static void main(String[] args) throws Exception {
38+
KeyPairGenerator kg = KeyPairGenerator.getInstance("RSA");
39+
kg.initialize(2048);
40+
KeyPair pair = kg.generateKeyPair();
41+
42+
KeyFactory factory = KeyFactory.getInstance("RSA");
43+
44+
// Since RSAPrivateCrtKeySpec inherits from RSAPrivateKeySpec, we'd expect this next line to return an instance of RSAPrivateKeySpec
45+
// (because the private key has CRT parts).
46+
KeySpec spec = factory.getKeySpec(pair.getPrivate(), RSAPrivateKeySpec.class);
47+
if (!(spec instanceof RSAPrivateCrtKeySpec)) {
48+
throw new Exception("Spec should be an instance of RSAPrivateCrtKeySpec");
49+
}
50+
51+
// This next line should give an InvalidKeySpec exception
52+
try {
53+
spec = factory.getKeySpec(pair.getPublic(), FakeX509Spec.class);
54+
throw new Exception("InvalidKeySpecException is expected but not thrown");
55+
} catch (final ClassCastException ex) {
56+
throw new Exception("InvalidKeySpecException is expected ClassCastException is thrown", ex);
57+
} catch (final InvalidKeySpecException ex) {
58+
// Pass
59+
}
60+
}
61+
62+
public static class FakeX509Spec extends X509EncodedKeySpec {
63+
public FakeX509Spec(byte[] encodedKey) {
64+
super(encodedKey);
65+
}
66+
}
67+
}

‎jdk/test/sun/security/rsa/TestKeyFactory.java

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -23,7 +23,7 @@
2323

2424
/**
2525
* @test
26-
* @bug 4853305
26+
* @bug 4853305 8254717
2727
* @summary Test KeyFactory of the new RSA provider
2828
* @author Andreas Sterbenz
2929
*/
@@ -107,11 +107,15 @@ private static void testPrivate(KeyFactory kf, PrivateKey key) throws Exception
107107

108108
KeySpec rsaSpec2 = kf.getKeySpec(key, RSAPrivateKeySpec.class);
109109
PrivateKey key6 = kf.generatePrivate(rsaSpec2);
110-
RSAPrivateKey rsaKey = (RSAPrivateKey)key;
111-
KeySpec rsaSpec3 = new RSAPrivateKeySpec(rsaKey.getModulus(), rsaKey.getPrivateExponent());
112-
PrivateKey key7 = kf.generatePrivate(rsaSpec3);
113110
testKey(key6, key6);
114-
testKey(key6, key7);
111+
if (key instanceof RSAPrivateCrtKey) {
112+
RSAPrivateCrtKey rsaKey = (RSAPrivateCrtKey)key;
113+
KeySpec rsaSpec3 = new RSAPrivateCrtKeySpec(rsaKey.getModulus(),
114+
rsaKey.getPublicExponent(), rsaKey.getPrivateExponent(), rsaKey.getPrimeP(), rsaKey.getPrimeQ(),
115+
rsaKey.getPrimeExponentP(), rsaKey.getPrimeExponentQ(), rsaKey.getCrtCoefficient(), rsaKey.getParams());
116+
PrivateKey key7 = kf.generatePrivate(rsaSpec3);
117+
testKey(key6, key7);
118+
}
115119
}
116120

117121
private static void test(KeyFactory kf, Key key) throws Exception {

‎jdk/test/sun/security/rsa/pss/TestPSSKeySupport.java

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -23,7 +23,7 @@
2323

2424
/**
2525
* @test
26-
* @bug 8146293 8242556
26+
* @bug 8146293 8242556 8254717
2727
* @summary Test RSASSA-PSS Key related support such as KeyPairGenerator
2828
* and KeyFactory of the SunRsaSign provider
2929
*/
@@ -98,9 +98,10 @@ private static void testPrivate(KeyFactory kf, PrivateKey key) throws Exception
9898

9999
KeySpec rsaSpec2 = kf.getKeySpec(key, RSAPrivateKeySpec.class);
100100
PrivateKey key6 = kf.generatePrivate(rsaSpec2);
101-
RSAPrivateKey rsaKey = (RSAPrivateKey)key;
102-
KeySpec rsaSpec3 = new RSAPrivateKeySpec(rsaKey.getModulus(),
103-
rsaKey.getPrivateExponent(), rsaKey.getParams());
101+
RSAPrivateCrtKey rsaKey = (RSAPrivateCrtKey)key;
102+
KeySpec rsaSpec3 = new RSAPrivateCrtKeySpec(rsaKey.getModulus(),
103+
rsaKey.getPublicExponent(), rsaKey.getPrivateExponent(), rsaKey.getPrimeP(), rsaKey.getPrimeQ(),
104+
rsaKey.getPrimeExponentP(), rsaKey.getPrimeExponentQ(), rsaKey.getCrtCoefficient(), rsaKey.getParams());
104105
PrivateKey key7 = kf.generatePrivate(rsaSpec3);
105106
testKey(key6, key6);
106107
testKey(key6, key7);

1 commit comments

Comments
 (1)

openjdk-notifier[bot] commented on Dec 18, 2022

@openjdk-notifier[bot]
Please sign in to comment.