Skip to content

Commit 864d0fd

Browse files
committedJan 9, 2025
8347289: HKDF delayed provider selection failed with non-extractable PRK
Reviewed-by: valeriep Backport-of: db7fa6a
1 parent ff9b8e4 commit 864d0fd

File tree

2 files changed

+108
-4
lines changed

2 files changed

+108
-4
lines changed
 

‎src/java.base/share/classes/com/sun/crypto/provider/HKDFKeyDerivation.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2024, 2025, 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
@@ -180,8 +180,8 @@ protected byte[] engineDeriveData(AlgorithmParameterSpec derivationSpec)
180180
} else if (derivationSpec instanceof HKDFParameterSpec.Expand anExpand) {
181181
// set this value in the "if"
182182
if ((pseudoRandomKey = anExpand.prk().getEncoded()) == null) {
183-
throw new AssertionError(
184-
"PRK is required for HKDFParameterSpec.Expand");
183+
throw new InvalidAlgorithmParameterException(
184+
"Cannot retrieve PRK for HKDFParameterSpec.Expand");
185185
}
186186
// set this value in the "if"
187187
if ((info = anExpand.info()) == null) {
@@ -411,4 +411,4 @@ public HKDFSHA512(KDFParameters kdfParameters)
411411
}
412412
}
413413

414-
}
414+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* Copyright (c) 2025, Oracle and/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 8347289
27+
* @summary make sure DPS works when non-extractable PRK is provided
28+
* @library /test/lib /test/jdk/security/unsignedjce
29+
* @build java.base/javax.crypto.ProviderVerifier
30+
* @enablePreview
31+
* @run main/othervm HKDFDelayedPRK
32+
*/
33+
34+
import jdk.test.lib.Asserts;
35+
36+
import javax.crypto.KDF;
37+
import javax.crypto.KDFParameters;
38+
import javax.crypto.KDFSpi;
39+
import javax.crypto.SecretKey;
40+
import javax.crypto.spec.HKDFParameterSpec;
41+
import javax.crypto.spec.SecretKeySpec;
42+
import java.security.InvalidAlgorithmParameterException;
43+
import java.security.Provider;
44+
import java.security.Security;
45+
import java.security.spec.AlgorithmParameterSpec;
46+
47+
public class HKDFDelayedPRK {
48+
public static void main(String[] args) throws Exception {
49+
// This is a fake non-extractable key
50+
var prk = new SecretKey() {
51+
@Override
52+
public String getAlgorithm() {
53+
return "PRK";
54+
}
55+
56+
@Override
57+
public String getFormat() {
58+
return null;
59+
}
60+
61+
@Override
62+
public byte[] getEncoded() {
63+
return null;
64+
}
65+
};
66+
67+
Security.addProvider(new ProviderImpl());
68+
var kdf = KDF.getInstance("HKDF-SHA256");
69+
kdf.deriveData(HKDFParameterSpec.expandOnly(prk, null, 32));
70+
71+
// Confirms our own omnipotent impl is selected
72+
Asserts.assertEquals("P", kdf.getProviderName());
73+
}
74+
75+
public static class ProviderImpl extends Provider {
76+
public ProviderImpl() {
77+
super("P", "1", "info");
78+
put("KDF.HKDF-SHA256", KDFImpl.class.getName());
79+
}
80+
}
81+
82+
// This HKDF impl accepts everything
83+
public static class KDFImpl extends KDFSpi {
84+
85+
public KDFImpl(KDFParameters params) throws InvalidAlgorithmParameterException {
86+
super(params);
87+
}
88+
89+
@Override
90+
protected KDFParameters engineGetParameters() {
91+
return null;
92+
}
93+
94+
@Override
95+
protected SecretKey engineDeriveKey(String alg, AlgorithmParameterSpec dummy) {
96+
return new SecretKeySpec(new byte[32], alg);
97+
}
98+
99+
@Override
100+
protected byte[] engineDeriveData(AlgorithmParameterSpec dummy) {
101+
return new byte[32];
102+
}
103+
}
104+
}

1 commit comments

Comments
 (1)

openjdk-notifier[bot] commented on Jan 9, 2025

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