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