Skip to content

Commit 394eac8

Browse files
author
Valerie Peng
committed
8295425: Match the default priv exp length between SunPKCS11 and other JDK providers
Reviewed-by: weijun
1 parent 6e19387 commit 394eac8

File tree

2 files changed

+130
-10
lines changed

2 files changed

+130
-10
lines changed

Diff for: src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/P11KeyPairGenerator.java

+12-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2003, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2003, 2023, 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
@@ -373,31 +373,33 @@ public KeyPair generateKeyPair() {
373373
case "DH" -> {
374374
keyType = CKK_DH;
375375
DHParameterSpec dhParams;
376-
int privateBits;
376+
int privateBits = 0;
377377
if (params == null) {
378378
try {
379379
dhParams = ParameterCache.getDHParameterSpec
380380
(keySize, random);
381+
privateBits = getDefDHPrivateExpSize(dhParams);
381382
} catch (GeneralSecurityException e) {
382383
throw new ProviderException
383384
("Could not generate DH parameters", e);
384385
}
385-
privateBits = 0;
386386
} else {
387387
dhParams = (DHParameterSpec) params;
388388
privateBits = dhParams.getL();
389-
}
390-
if (privateBits <= 0) {
391-
// XXX find better defaults
392-
privateBits = (keySize >= 1024) ? 768 : 512;
389+
if (privateBits < 0) {
390+
// invalid, override with JDK defaults
391+
privateBits = getDefDHPrivateExpSize(dhParams);
392+
}
393393
}
394394
publicKeyTemplate = new CK_ATTRIBUTE[]{
395395
new CK_ATTRIBUTE(CKA_PRIME, dhParams.getP()),
396396
new CK_ATTRIBUTE(CKA_BASE, dhParams.getG())
397397
};
398-
privateKeyTemplate = new CK_ATTRIBUTE[]{
399-
new CK_ATTRIBUTE(CKA_VALUE_BITS, privateBits),
400-
};
398+
privateKeyTemplate = (privateBits != 0 ?
399+
new CK_ATTRIBUTE[]{
400+
new CK_ATTRIBUTE(CKA_VALUE_BITS, privateBits),
401+
} :
402+
new CK_ATTRIBUTE[]{});
401403
}
402404
case "EC" -> {
403405
keyType = CKK_EC;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
* Copyright (c) 2023, 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+
import java.security.KeyPair;
25+
import java.security.KeyPairGenerator;
26+
import java.security.Provider;
27+
import java.security.PrivateKey;
28+
import javax.crypto.spec.DHParameterSpec;
29+
import javax.crypto.interfaces.DHPrivateKey;
30+
import sun.security.util.SecurityProviderConstants;
31+
import sun.security.provider.ParameterCache;
32+
33+
/**
34+
* @test
35+
* @bug 8295425
36+
* @modules java.base/sun.security.provider java.base/sun.security.util
37+
* @library /test/lib ..
38+
* @run main TestDefaultDHPrivateExpSize
39+
* @summary This test verifies the DH private exponent size for SunPKCS11
40+
* provider.
41+
*/
42+
43+
public class TestDefaultDHPrivateExpSize extends PKCS11Test {
44+
45+
@Override
46+
public void main(Provider p) throws Exception {
47+
System.out.println("Testing " + p.getName());
48+
49+
if (p.getService("KeyPairGenerator", "DH") == null) {
50+
System.out.println("Skip, no support for DH KeyPairGenerator");
51+
return;
52+
}
53+
54+
KeyPairGenerator kpg = KeyPairGenerator.getInstance("DH", p);
55+
// try common DH key sizes with built-in primes
56+
int[] cachedSizes = { 2048, 3072, 4096, 6144, 8192 };
57+
for (int ks : cachedSizes) {
58+
// use keysize which uses JDK default parameters w/ JDK
59+
// default lSize
60+
kpg.initialize(ks);
61+
int expectedL = SecurityProviderConstants.getDefDHPrivateExpSize
62+
(ParameterCache.getCachedDHParameterSpec(ks));
63+
System.out.println("Test against built-in DH " + ks +
64+
"-bit parameters, expectedL = " + expectedL);
65+
DHParameterSpec spec = generateAndCheck(kpg, ks, expectedL);
66+
67+
// use custom DH parameters w/o lSize
68+
DHParameterSpec spec2 = new DHParameterSpec(spec.getP(),
69+
spec.getG());
70+
kpg.initialize(spec2);
71+
System.out.println("Test against user DH " + ks +
72+
"-bit parameters, expectedL = " + spec2.getL());
73+
74+
generateAndCheck(kpg, ks, spec2.getL());
75+
76+
// use custom DH parameters w/ lSize
77+
expectedL += 2;
78+
spec2 = new DHParameterSpec(spec.getP(), spec.getG(), expectedL);
79+
kpg.initialize(spec2);
80+
System.out.println("Test against user DH " + ks +
81+
"-bit parameters, expectedL = " + spec2.getL());
82+
generateAndCheck(kpg, ks, expectedL);
83+
}
84+
}
85+
86+
// initialize the specified 'kpg' with 'initParam', then check
87+
// the parameters associated with the generated key against 'initParam'
88+
// and return the actual private exponent length.
89+
private static DHParameterSpec generateAndCheck(KeyPairGenerator kpg,
90+
int expKeySize, int expL) {
91+
92+
DHPrivateKey dhPriv = (DHPrivateKey) kpg.generateKeyPair().getPrivate();
93+
DHParameterSpec generated = dhPriv.getParams();
94+
// check the params associated with the key as that's what we
95+
// have control over
96+
if ((generated.getP().bitLength() != expKeySize) ||
97+
generated.getL()!= expL) {
98+
new RuntimeException("Error: size check failed, got " +
99+
generated.getP().bitLength() + " and " + generated.getL());
100+
}
101+
102+
// Known NSS Issue/limitation: NSS ignores the supplied L value when
103+
// generating the DH private key
104+
int actualL = dhPriv.getX().bitLength();
105+
System.out.println("INFO: actual L = " + actualL);
106+
/*
107+
if (expLSize != 0 && actualL != expLSize) {
108+
throw new RuntimeException("ERROR: actual L mismatches, got "
109+
+ actualL + " vs expect " + expLSize);
110+
}
111+
*/
112+
return generated;
113+
}
114+
115+
public static void main(String[] args) throws Exception {
116+
main(new TestDefaultDHPrivateExpSize(), args);
117+
}
118+
}

0 commit comments

Comments
 (0)