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/rsa/TestSignatures.java
38855 views
1
/*
2
* Copyright (c) 2003, 2016, 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 4856966
27
* @summary Test signing/verifying using all the signature algorithms
28
* @author Andreas Sterbenz
29
* @library ..
30
* @key randomness
31
* @run main/othervm TestSignatures
32
* @run main/othervm TestSignatures sm rsakeys.ks.policy
33
*/
34
35
import java.io.File;
36
import java.io.FileInputStream;
37
import java.io.InputStream;
38
import java.security.KeyFactory;
39
import java.security.KeyStore;
40
import java.security.PrivateKey;
41
import java.security.Provider;
42
import java.security.PublicKey;
43
import java.security.Signature;
44
import java.security.interfaces.RSAPublicKey;
45
import java.util.Enumeration;
46
import java.util.Random;
47
48
public class TestSignatures extends PKCS11Test {
49
50
private static final char[] password = "test12".toCharArray();
51
52
private static Provider provider;
53
54
private static byte[] data;
55
56
static KeyStore getKeyStore() throws Exception {
57
KeyStore ks;
58
try (InputStream in = new FileInputStream(new File(BASE, "rsakeys.ks"))) {
59
ks = KeyStore.getInstance("JKS");
60
ks.load(in, password);
61
}
62
return ks;
63
}
64
65
private static void testSignature(String algorithm, PrivateKey privateKey,
66
PublicKey publicKey) throws Exception {
67
System.out.println("Testing " + algorithm + "...");
68
Signature s = Signature.getInstance(algorithm, provider);
69
s.initSign(privateKey);
70
s.update(data);
71
byte[] sig = s.sign();
72
s.initVerify(publicKey);
73
s.update(data);
74
boolean result;
75
result = s.verify(sig);
76
if (result == false) {
77
throw new Exception("Verification 1 failed");
78
}
79
s.update(data);
80
result = s.verify(sig);
81
if (result == false) {
82
throw new Exception("Verification 2 failed");
83
}
84
result = s.verify(sig);
85
if (result == true) {
86
throw new Exception("Verification 3 succeeded");
87
}
88
}
89
90
private static void test(PrivateKey privateKey, PublicKey publicKey)
91
throws Exception {
92
testSignature("MD2withRSA", privateKey, publicKey);
93
testSignature("MD5withRSA", privateKey, publicKey);
94
testSignature("SHA1withRSA", privateKey, publicKey);
95
testSignature("SHA224withRSA", privateKey, publicKey);
96
testSignature("SHA256withRSA", privateKey, publicKey);
97
RSAPublicKey rsaKey = (RSAPublicKey)publicKey;
98
if (rsaKey.getModulus().bitLength() > 512) {
99
// for SHA384 and SHA512 the data is too long for 512 bit keys
100
testSignature("SHA384withRSA", privateKey, publicKey);
101
testSignature("SHA512withRSA", privateKey, publicKey);
102
}
103
}
104
105
public static void main(String[] args) throws Exception {
106
main(new TestSignatures(), args);
107
}
108
109
@Override
110
public void main(Provider p) throws Exception {
111
long start = System.currentTimeMillis();
112
provider = p;
113
data = new byte[2048];
114
new Random().nextBytes(data);
115
KeyStore ks = getKeyStore();
116
KeyFactory kf = KeyFactory.getInstance("RSA", provider);
117
for (Enumeration e = ks.aliases(); e.hasMoreElements(); ) {
118
String alias = (String)e.nextElement();
119
if (ks.isKeyEntry(alias)) {
120
System.out.println("* Key " + alias + "...");
121
PrivateKey privateKey = (PrivateKey)ks.getKey(alias, password);
122
PublicKey publicKey = ks.getCertificate(alias).getPublicKey();
123
privateKey = (PrivateKey)kf.translateKey(privateKey);
124
publicKey = (PublicKey)kf.translateKey(publicKey);
125
test(privateKey, publicKey);
126
}
127
}
128
long stop = System.currentTimeMillis();
129
System.out.println("All tests passed (" + (stop - start) + " ms).");
130
}
131
}
132
133