Skip to content

Commit 6dc4d89

Browse files
author
Valerie Peng
committed
7181214: Need specify SKF translateKey(SecurityKey) method requires instance of PBEKey for PBKDF2 algorithms
Reviewed-by: xuelei, weijun
1 parent 2afb4c3 commit 6dc4d89

File tree

2 files changed

+57
-17
lines changed

2 files changed

+57
-17
lines changed

src/java.base/share/classes/com/sun/crypto/provider/PBKDF2Core.java

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2005, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2005, 2022, 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
@@ -63,11 +63,11 @@ abstract class PBKDF2Core extends SecretKeyFactorySpi {
6363
protected SecretKey engineGenerateSecret(KeySpec keySpec)
6464
throws InvalidKeySpecException
6565
{
66-
if (!(keySpec instanceof PBEKeySpec)) {
67-
throw new InvalidKeySpecException("Invalid key spec");
66+
if (keySpec instanceof PBEKeySpec ks) {
67+
return new PBKDF2KeyImpl(ks, prfAlgo);
68+
} else {
69+
throw new InvalidKeySpecException("Only PBEKeySpec is accepted");
6870
}
69-
PBEKeySpec ks = (PBEKeySpec) keySpec;
70-
return new PBKDF2KeyImpl(ks, prfAlgo);
7171
}
7272

7373
/**
@@ -89,12 +89,10 @@ protected SecretKey engineGenerateSecret(KeySpec keySpec)
8989
*/
9090
protected KeySpec engineGetKeySpec(SecretKey key, Class<?> keySpecCl)
9191
throws InvalidKeySpecException {
92-
if (key instanceof javax.crypto.interfaces.PBEKey) {
92+
if (key instanceof javax.crypto.interfaces.PBEKey pKey) {
9393
// Check if requested key spec is amongst the valid ones
9494
if ((keySpecCl != null)
9595
&& keySpecCl.isAssignableFrom(PBEKeySpec.class)) {
96-
javax.crypto.interfaces.PBEKey pKey =
97-
(javax.crypto.interfaces.PBEKey) key;
9896
char[] passwd = pKey.getPassword();
9997
byte[] encoded = pKey.getEncoded();
10098
try {
@@ -107,11 +105,11 @@ protected KeySpec engineGetKeySpec(SecretKey key, Class<?> keySpecCl)
107105
Arrays.fill(encoded, (byte)0);
108106
}
109107
} else {
110-
throw new InvalidKeySpecException("Invalid key spec");
108+
throw new InvalidKeySpecException
109+
("Only PBEKeySpec is accepted");
111110
}
112111
} else {
113-
throw new InvalidKeySpecException("Invalid key " +
114-
"format/algorithm");
112+
throw new InvalidKeySpecException("Only PBEKey is accepted");
115113
}
116114
}
117115

@@ -138,9 +136,7 @@ protected SecretKey engineTranslateKey(SecretKey key)
138136
return key;
139137
}
140138
// Check if key implements the PBEKey
141-
if (key instanceof javax.crypto.interfaces.PBEKey) {
142-
javax.crypto.interfaces.PBEKey pKey =
143-
(javax.crypto.interfaces.PBEKey) key;
139+
if (key instanceof javax.crypto.interfaces.PBEKey pKey) {
144140
char[] password = pKey.getPassword();
145141
byte[] encoding = pKey.getEncoded();
146142
PBEKeySpec spec =
@@ -160,9 +156,12 @@ protected SecretKey engineTranslateKey(SecretKey key)
160156
}
161157
Arrays.fill(encoding, (byte)0);
162158
}
159+
} else {
160+
throw new InvalidKeyException("Only PBEKey is accepted");
163161
}
164162
}
165-
throw new InvalidKeyException("Invalid key format/algorithm");
163+
throw new InvalidKeyException("Only PBKDF2With" + prfAlgo +
164+
" key with RAW format is accepted");
166165
}
167166

168167
public static final class HmacSHA1 extends PBKDF2Core {

test/jdk/com/sun/crypto/provider/Cipher/PBE/PBKDF2Translate.java

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2012, 2022, 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
@@ -30,6 +30,7 @@
3030
import javax.crypto.SecretKeyFactory;
3131
import javax.crypto.interfaces.PBEKey;
3232
import javax.crypto.spec.PBEKeySpec;
33+
import javax.crypto.spec.SecretKeySpec;
3334

3435
/**
3536
* @test
@@ -68,7 +69,8 @@ public static void main(String[] args) throws Exception {
6869
try {
6970
if (!theTest.testMyOwnSecretKey()
7071
|| !theTest.generateAndTranslateKey()
71-
|| !theTest.translateSpoiledKey()) {
72+
|| !theTest.translateSpoiledKey()
73+
|| !theTest.testGeneralSecretKey()) {
7274
// we don't want to set failed to false
7375
failed = true;
7476
}
@@ -188,6 +190,45 @@ public boolean translateSpoiledKey() throws NoSuchAlgorithmException,
188190
return false;
189191
}
190192

193+
/**
194+
* The test case scenario implemented in the method: - create a general
195+
* secret key (does not implement PBEKey) - try calling
196+
* translate and getKeySpec methods and see if the expected
197+
* InvalidKeyException and InvalidKeySpecException is thrown.
198+
*
199+
* @return true if the expected Exception occurred; false - otherwise
200+
* @throws NoSuchAlgorithmException
201+
*/
202+
public boolean testGeneralSecretKey() throws NoSuchAlgorithmException {
203+
SecretKey key = new SecretKeySpec("random#s".getBytes(), algoToTest);
204+
SecretKeyFactory skf = SecretKeyFactory.getInstance(algoToTest);
205+
try {
206+
skf.translateKey(key);
207+
System.out.println("Error: expected IKE not thrown");
208+
return false;
209+
} catch (InvalidKeyException e) {
210+
if (e.getMessage().indexOf("PBEKey") == -1) {
211+
System.out.println("Error: IKE message should " +
212+
"indicate that PBEKey is required");
213+
return false;
214+
}
215+
}
216+
217+
try {
218+
skf.getKeySpec(key, PBEKeySpec.class);
219+
System.out.println("Error: expected IKSE not thrown");
220+
return false;
221+
} catch (InvalidKeySpecException e) {
222+
if (e.getMessage().indexOf("PBEKey") == -1) {
223+
System.out.println("Error: IKSE message should " +
224+
"indicate that PBEKey is required");
225+
return false;
226+
}
227+
}
228+
229+
return true;
230+
}
231+
191232
/**
192233
* Generate a PBKDF2 secret key using given algorithm.
193234
*

0 commit comments

Comments
 (0)