Skip to content

Commit 1c598c2

Browse files
author
Valerie Peng
committed
8312428: PKCS11 tests fail with NSS 3.91
Reviewed-by: ssahoo, rhalade
1 parent b2728cb commit 1c598c2

File tree

4 files changed

+208
-115
lines changed

4 files changed

+208
-115
lines changed

test/jdk/sun/security/pkcs11/MessageDigest/TestCloning.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2012, 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
@@ -39,6 +39,8 @@
3939
import java.util.Random;
4040
import java.util.List;
4141

42+
import jtreg.SkippedException;
43+
4244
public class TestCloning extends PKCS11Test {
4345

4446
public static void main(String[] args) throws Exception {
@@ -57,15 +59,31 @@ public void main(Provider p) throws Exception {
5759
r.nextBytes(data1);
5860
r.nextBytes(data2);
5961
System.out.println("Testing against provider " + p.getName());
62+
63+
boolean skipTest = true;
64+
6065
for (String alg : ALGS) {
61-
System.out.println("Testing " + alg);
66+
System.out.println("Digest algo: " + alg);
6267
MessageDigest md = MessageDigest.getInstance(alg, p);
63-
md = testCloning(md, p);
68+
try {
69+
md = testCloning(md, p);;
70+
} catch (CloneNotSupportedException cnse) {
71+
// skip test if clone isn't supported
72+
System.out.println("=> Clone not supported; skip!");
73+
continue;
74+
}
75+
76+
// start testing below
77+
skipTest = false;
78+
6479
// repeat the test again after generating digest once
6580
for (int j = 0; j < 10; j++) {
6681
md = testCloning(md, p);
6782
}
6883
}
84+
if (skipTest) {
85+
throw new SkippedException("Test Skipped!");
86+
}
6987
}
7088

7189
private static MessageDigest testCloning(MessageDigest mdObj, Provider p)
@@ -125,4 +143,3 @@ private static void check(byte[] d1, byte[] d2, String copyName)
125143
}
126144
}
127145
}
128-
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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+
import java.security.KeyPair;
24+
import java.security.KeyPairGenerator;
25+
import java.security.MessageDigest;
26+
import java.security.NoSuchAlgorithmException;
27+
import java.security.Provider;
28+
import java.security.Signature;
29+
30+
public class PSSUtil {
31+
32+
/**
33+
* ALGORITHM name, fixed as RSA for PKCS11
34+
*/
35+
private static final String KEYALG = "RSA";
36+
private static final String SIGALG = "RSASSA-PSS";
37+
38+
public static enum AlgoSupport {
39+
NO, MAYBE, YES
40+
};
41+
42+
public static boolean isSignatureSupported(Provider p) {
43+
try {
44+
Signature.getInstance(SIGALG, p);
45+
return true;
46+
} catch (NoSuchAlgorithmException e) {
47+
System.out.println("Skip testing " + SIGALG +
48+
" due to no support");
49+
return false;
50+
}
51+
}
52+
53+
public static AlgoSupport isHashSupported(Provider p, String... hashAlgs) {
54+
55+
AlgoSupport status = AlgoSupport.YES;
56+
for (String h : hashAlgs) {
57+
String sigAlg = (h.startsWith("SHA3-") ?
58+
h : h.replace("-", "")) + "with" + SIGALG;
59+
try {
60+
Signature.getInstance(sigAlg, p);
61+
// Yes, proceed to check next hash algorithm
62+
continue;
63+
} catch (NoSuchAlgorithmException e) {
64+
// continue trying other checks
65+
}
66+
try {
67+
MessageDigest.getInstance(h, p);
68+
status = AlgoSupport.MAYBE;
69+
} catch (NoSuchAlgorithmException e) {
70+
// if not supported as a standalone digest algo, chance of it
71+
// being supported by PSS is very very low
72+
return AlgoSupport.NO;
73+
}
74+
}
75+
return status;
76+
}
77+
78+
public static KeyPair generateKeys(Provider p, int size)
79+
throws NoSuchAlgorithmException {
80+
KeyPairGenerator kpg = KeyPairGenerator.getInstance(KEYALG, p);
81+
kpg.initialize(size);
82+
return kpg.generateKeyPair();
83+
}
84+
}
Lines changed: 56 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2019, 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
@@ -24,6 +24,8 @@
2424
import java.security.interfaces.*;
2525
import java.security.spec.*;
2626

27+
import jtreg.SkippedException;
28+
2729
/**
2830
* @test
2931
* @bug 8080462 8226651 8242332
@@ -35,25 +37,19 @@
3537
*/
3638
public class KeyAndParamCheckForPSS extends PKCS11Test {
3739

38-
/**
39-
* ALGORITHM name, fixed as RSA for PKCS11
40-
*/
41-
private static final String KEYALG = "RSA";
4240
private static final String SIGALG = "RSASSA-PSS";
4341

4442
public static void main(String[] args) throws Exception {
4543
main(new KeyAndParamCheckForPSS(), args);
4644
}
4745

46+
private static boolean skipTest = true;
47+
4848
@Override
4949
public void main(Provider p) throws Exception {
50-
Signature sig;
51-
try {
52-
sig = Signature.getInstance(SIGALG, p);
53-
} catch (NoSuchAlgorithmException e) {
54-
System.out.println("Skip testing RSASSA-PSS" +
55-
" due to no support");
56-
return;
50+
if (!PSSUtil.isSignatureSupported(p)) {
51+
throw new SkippedException("Skip due to no support for " +
52+
SIGALG);
5753
}
5854

5955
// NOTE: key length >= (digest length + 2) in bytes
@@ -76,27 +72,26 @@ public void main(Provider p) throws Exception {
7672
runTest(p, 1040, "SHA3-512", "SHA3-256");
7773
runTest(p, 1040, "SHA3-512", "SHA3-384");
7874
runTest(p, 1040, "SHA3-512", "SHA3-512");
75+
76+
if (skipTest) {
77+
throw new SkippedException("Test Skipped");
78+
}
7979
}
8080

81-
private void runTest(Provider p, int keySize, String hashAlg,
81+
private static void runTest(Provider p, int keySize, String hashAlg,
8282
String mgfHashAlg) throws Exception {
8383

84-
// skip further test if this provider does not support hashAlg or
85-
// mgfHashAlg
86-
try {
87-
MessageDigest.getInstance(hashAlg, p);
88-
MessageDigest.getInstance(mgfHashAlg, p);
89-
} catch (NoSuchAlgorithmException nsae) {
90-
System.out.println("No support for " + hashAlg + ", skip");
84+
System.out.println("Testing " + hashAlg + " and MGF1" + mgfHashAlg);
85+
PSSUtil.AlgoSupport s = PSSUtil.isHashSupported(p, hashAlg, mgfHashAlg);
86+
if (s == PSSUtil.AlgoSupport.NO) {
87+
System.out.println("=> Skip; no support");
9188
return;
9289
}
9390

94-
System.out.println("Testing [" + keySize + " " + hashAlg + "]");
91+
Signature sig = Signature.getInstance(SIGALG, p);
9592

9693
// create a key pair with the supplied size
97-
KeyPairGenerator kpg = KeyPairGenerator.getInstance(KEYALG, p);
98-
kpg.initialize(keySize);
99-
KeyPair kp = kpg.generateKeyPair();
94+
KeyPair kp = PSSUtil.generateKeys(p, keySize);
10095

10196
int bigSaltLen = keySize/8 - 14;
10297
AlgorithmParameterSpec paramsBad = new PSSParameterSpec(hashAlg,
@@ -108,58 +103,71 @@ private void runTest(Provider p, int keySize, String hashAlg,
108103
PublicKey pub = kp.getPublic();
109104

110105
// test#1 - setParameter then initSign
111-
Signature sig = Signature.getInstance("RSASSA-PSS", p);
112-
sig.setParameter(paramsBad);
106+
sig = Signature.getInstance(SIGALG, p);
107+
try {
108+
sig.setParameter(paramsGood);
109+
sig.initSign(priv);
110+
// algorithm support confirmed
111+
skipTest = false;
112+
} catch (Exception ex) {
113+
if (s == PSSUtil.AlgoSupport.MAYBE) {
114+
// confirmed to be unsupported; skip the rest of the test
115+
System.out.println("=> Skip; no PSS support");
116+
return;
117+
} else {
118+
throw new RuntimeException("Unexpected Exception", ex);
119+
}
120+
}
121+
122+
sig = Signature.getInstance(SIGALG, p);
113123
try {
124+
sig.setParameter(paramsBad);
114125
sig.initSign(priv);
115126
throw new RuntimeException("Expected IKE not thrown");
116127
} catch (InvalidKeyException ike) {
117-
System.out.println("test#1: got expected IKE");
128+
// expected
118129
}
119130

131+
// test#2 - setParameter then initVerify
132+
sig = Signature.getInstance(SIGALG, p);
120133
sig.setParameter(paramsGood);
121-
sig.initSign(priv);
122-
System.out.println("test#1: pass");
134+
sig.initVerify(pub);
123135

124-
// test#2 - setParameter then initVerify
125-
sig = Signature.getInstance("RSASSA-PSS", p);
126-
sig.setParameter(paramsBad);
136+
sig = Signature.getInstance(SIGALG, p);
127137
try {
138+
sig.setParameter(paramsBad);
128139
sig.initVerify(pub);
129140
throw new RuntimeException("Expected IKE not thrown");
130141
} catch (InvalidKeyException ike) {
131-
System.out.println("test#2: got expected IKE");
142+
// expected
132143
}
133144

134-
sig.setParameter(paramsGood);
135-
sig.initVerify(pub);
136-
137-
System.out.println("test#2: pass");
138-
139145
// test#3 - initSign, then setParameter
140-
sig = Signature.getInstance("RSASSA-PSS", p);
146+
sig = Signature.getInstance(SIGALG, p);
141147
sig.initSign(priv);
148+
sig.setParameter(paramsGood);
149+
150+
sig = Signature.getInstance(SIGALG, p);
142151
try {
152+
sig.initSign(priv);
143153
sig.setParameter(paramsBad);
144154
throw new RuntimeException("Expected IAPE not thrown");
145155
} catch (InvalidAlgorithmParameterException iape) {
146-
System.out.println("test#3: got expected IAPE");
156+
// expected
147157
}
148158

149-
sig.setParameter(paramsGood);
150-
System.out.println("test#3: pass");
151-
152159
// test#4 - initVerify, then setParameter
153-
sig = Signature.getInstance("RSASSA-PSS", p);
160+
sig = Signature.getInstance(SIGALG, p);
161+
sig.setParameter(paramsGood);
154162
sig.initVerify(pub);
163+
164+
sig = Signature.getInstance(SIGALG, p);
155165
try {
166+
sig.initVerify(pub);
156167
sig.setParameter(paramsBad);
157168
throw new RuntimeException("Expected IAPE not thrown");
158169
} catch (InvalidAlgorithmParameterException iape) {
159-
System.out.println("test#4: got expected IAPE");
170+
// expected
160171
}
161-
162-
sig.setParameter(paramsGood);
163-
System.out.println("test#4: pass");
164172
}
165173
}

0 commit comments

Comments
 (0)