Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/pkcs11/Signature/KeyAndParamCheckForPSS.java
38855 views
1
/*
2
* Copyright (c) 2019, 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.*;
24
import java.security.interfaces.*;
25
import java.security.spec.*;
26
27
/**
28
* @test
29
* @bug 8080462
30
* @summary Ensure that PSS key and params check are implemented properly
31
* regardless of call sequence
32
* @library ..
33
* @modules jdk.crypto.cryptoki
34
* @run main KeyAndParamCheckForPSS
35
*/
36
public class KeyAndParamCheckForPSS extends PKCS11Test {
37
38
/**
39
* ALGORITHM name, fixed as RSA for PKCS11
40
*/
41
private static final String KEYALG = "RSA";
42
private static final String SIGALG = "RSASSA-PSS";
43
44
public static void main(String[] args) throws Exception {
45
main(new KeyAndParamCheckForPSS(), args);
46
}
47
48
@Override
49
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;
57
}
58
// NOTE: key length >= (digest length + 2) in bytes
59
// otherwise, even salt length = 0 would not work
60
runTest(p, 1024, "SHA-384");
61
runTest(p, 1040, "SHA-512");
62
}
63
64
private void runTest(Provider p, int keySize, String hashAlg)
65
throws Exception {
66
System.out.println("Testing [" + keySize + " " + hashAlg + "]");
67
68
// create a key pair with the supplied size
69
KeyPairGenerator kpg = KeyPairGenerator.getInstance(KEYALG, p);
70
kpg.initialize(keySize);
71
KeyPair kp = kpg.generateKeyPair();
72
73
int bigSaltLen = keySize/8 - 14;
74
AlgorithmParameterSpec paramsBad = new PSSParameterSpec(hashAlg,
75
"MGF1", new MGF1ParameterSpec(hashAlg), bigSaltLen, 1);
76
AlgorithmParameterSpec paramsGood = new PSSParameterSpec(hashAlg,
77
"MGF1", new MGF1ParameterSpec(hashAlg), 0, 1);
78
79
PrivateKey priv = kp.getPrivate();
80
PublicKey pub = kp.getPublic();
81
82
// test#1 - setParameter then initSign
83
Signature sig = Signature.getInstance("RSASSA-PSS", p);
84
sig.setParameter(paramsBad);
85
try {
86
sig.initSign(priv);
87
throw new RuntimeException("Expected IKE not thrown");
88
} catch (InvalidKeyException ike) {
89
System.out.println("test#1: got expected IKE");
90
}
91
sig.setParameter(paramsGood);
92
sig.initSign(priv);
93
System.out.println("test#1: pass");
94
95
// test#2 - setParameter then initVerify
96
sig = Signature.getInstance("RSASSA-PSS", p);
97
sig.setParameter(paramsBad);
98
try {
99
sig.initVerify(pub);
100
throw new RuntimeException("Expected IKE not thrown");
101
} catch (InvalidKeyException ike) {
102
System.out.println("test#2: got expected IKE");
103
}
104
sig.setParameter(paramsGood);
105
sig.initVerify(pub);
106
System.out.println("test#2: pass");
107
108
// test#3 - initSign, then setParameter
109
sig = Signature.getInstance("RSASSA-PSS", p);
110
sig.initSign(priv);
111
try {
112
sig.setParameter(paramsBad);
113
throw new RuntimeException("Expected IAPE not thrown");
114
} catch (InvalidAlgorithmParameterException iape) {
115
System.out.println("test#3: got expected IAPE");
116
}
117
sig.setParameter(paramsGood);
118
System.out.println("test#3: pass");
119
120
// test#4 - initVerify, then setParameter
121
sig = Signature.getInstance("RSASSA-PSS", p);
122
sig.initVerify(pub);
123
try {
124
sig.setParameter(paramsBad);
125
throw new RuntimeException("Expected IAPE not thrown");
126
} catch (InvalidAlgorithmParameterException iape) {
127
System.out.println("test#4: got expected IAPE");
128
}
129
sig.setParameter(paramsGood);
130
System.out.println("test#4: pass");
131
}
132
}
133
134