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/SigInteropPSS.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
24
import java.security.*;
25
import java.security.spec.*;
26
import java.security.interfaces.*;
27
28
/*
29
* @test
30
* @bug 8080462
31
* @summary testing interoperability of PSS signatures of PKCS11 provider
32
* against SunRsaSign provider
33
* @library ..
34
* @modules jdk.crypto.cryptoki
35
* @run main/othervm SigInteropPSS
36
*/
37
public class SigInteropPSS extends PKCS11Test {
38
39
private static final byte[] MSG =
40
"Interoperability test between SunRsaSign and SunPKCS11".getBytes();
41
42
private static final String[] DIGESTS = {
43
"SHA-224", "SHA-256", "SHA-384", "SHA-512"
44
};
45
46
public static void main(String[] args) throws Exception {
47
main(new SigInteropPSS(), args);
48
}
49
50
@Override
51
public void main(Provider p) throws Exception {
52
Signature sigPkcs11;
53
try {
54
sigPkcs11 = Signature.getInstance("RSASSA-PSS", p);
55
} catch (NoSuchAlgorithmException e) {
56
System.out.println("Skip testing RSASSA-PSS" +
57
" due to no support");
58
return;
59
}
60
61
Signature sigSunRsaSign =
62
Signature.getInstance("RSASSA-PSS", "SunRsaSign");
63
64
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", p);
65
kpg.initialize(3072);
66
KeyPair kp = kpg.generateKeyPair();
67
boolean status;
68
try {
69
status = runTest(sigSunRsaSign, sigPkcs11, kp);
70
status &= runTest(sigPkcs11, sigSunRsaSign, kp);
71
} catch (Exception e) {
72
System.out.println("Unexpected exception: " + e);
73
e.printStackTrace(System.out);
74
status = false;
75
}
76
77
if (!status) {
78
throw new RuntimeException("One or more test failed");
79
}
80
System.out.println("Test passed");
81
}
82
83
static boolean runTest(Signature signer, Signature verifier, KeyPair kp) throws Exception {
84
System.out.println("\tSign using " + signer.getProvider().getName());
85
System.out.println("\tVerify using " + verifier.getProvider().getName());
86
87
boolean status;
88
for (String digestAlg : DIGESTS) {
89
System.out.println("\tDigest = " + digestAlg);
90
PSSParameterSpec params = new PSSParameterSpec(digestAlg, "MGF1",
91
new MGF1ParameterSpec(digestAlg), 0, 1);
92
try {
93
signer.setParameter(params);
94
signer.initSign(kp.getPrivate());
95
verifier.setParameter(params);
96
verifier.initVerify(kp.getPublic());
97
} catch (Exception e) {
98
System.out.println("\tERROR: unexpected ex during init" + e);
99
status = false;
100
continue;
101
}
102
try {
103
signer.update(MSG);
104
byte[] sigBytes = signer.sign();
105
verifier.update(MSG);
106
boolean isValid = verifier.verify(sigBytes);
107
if (isValid) {
108
System.out.println("\tPSS Signature verified");
109
} else {
110
System.out.println("\tERROR verifying PSS Signature");
111
status = false;
112
}
113
} catch (Exception e) {
114
System.out.println("\tERROR: unexpected ex" + e);
115
e.printStackTrace();
116
status = false;
117
}
118
}
119
return true;
120
}
121
}
122
123