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/TestKeyPairGenerator.java
38855 views
1
/*
2
* Copyright (c) 2003, 2018, 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 Verify that the RSA KeyPairGenerator works
28
* @author Andreas Sterbenz
29
* @library ..
30
* @run main/othervm TestKeyPairGenerator
31
* @run main/othervm TestKeyPairGenerator sm TestKeyPairGenerator.policy
32
* @key intermittent randomness
33
*/
34
35
import java.math.BigInteger;
36
import java.security.KeyPair;
37
import java.security.KeyPairGenerator;
38
import java.security.PrivateKey;
39
import java.security.Provider;
40
import java.security.PublicKey;
41
import java.security.Signature;
42
import java.security.interfaces.RSAPrivateCrtKey;
43
import java.security.interfaces.RSAPublicKey;
44
import java.security.spec.RSAKeyGenParameterSpec;
45
import java.util.Random;
46
47
public class TestKeyPairGenerator extends PKCS11Test {
48
49
private static Provider provider;
50
51
private static byte[] data;
52
53
private static void testSignature(String algorithm, PrivateKey privateKey,
54
PublicKey publicKey) throws Exception {
55
System.out.println("Testing " + algorithm + "...");
56
Signature s = Signature.getInstance(algorithm, provider);
57
s.initSign(privateKey);
58
s.update(data);
59
byte[] sig = s.sign();
60
s.initVerify(publicKey);
61
s.update(data);
62
boolean result = s.verify(sig);
63
if (result == false) {
64
throw new Exception("Verification failed");
65
}
66
}
67
68
private static void test(PrivateKey privateKey, PublicKey publicKey) throws Exception {
69
testSignature("MD2withRSA", privateKey, publicKey);
70
testSignature("MD5withRSA", privateKey, publicKey);
71
testSignature("SHA1withRSA", privateKey, publicKey);
72
testSignature("SHA224withRSA", privateKey, publicKey);
73
testSignature("SHA256withRSA", privateKey, publicKey);
74
RSAPublicKey rsaKey = (RSAPublicKey)publicKey;
75
if (rsaKey.getModulus().bitLength() > 512) {
76
// for SHA384 and SHA512 the data is too long for 512 bit keys
77
testSignature("SHA384withRSA", privateKey, publicKey);
78
testSignature("SHA512withRSA", privateKey, publicKey);
79
}
80
}
81
82
// regression test for 4865198
83
private static void testInvalidSignature(KeyPair kp1, KeyPair kp2) throws Exception {
84
System.out.println("Testing signature with incorrect key...");
85
Signature sig = Signature.getInstance("MD5withRSA", provider);
86
sig.initSign(kp1.getPrivate());
87
byte[] data = new byte[100];
88
sig.update(data);
89
byte[] signature = sig.sign();
90
sig.initVerify(kp1.getPublic());
91
sig.update(data);
92
if (sig.verify(signature) == false) {
93
throw new Exception("verification failed");
94
}
95
sig.initVerify(kp2.getPublic());
96
sig.update(data);
97
// verify needs to return false and not throw an Exception
98
if (sig.verify(signature)) {
99
throw new Exception("verification unexpectedly succeeded");
100
}
101
}
102
103
public static void main(String[] args) throws Exception {
104
main(new TestKeyPairGenerator(), args);
105
}
106
107
@Override
108
public void main(Provider p) throws Exception {
109
long start = System.currentTimeMillis();
110
provider = p;
111
data = new byte[2048];
112
// keypair generation is very slow, test only a few short keys
113
int[] keyLengths = {512, 512, 1024};
114
BigInteger[] pubExps = {null, RSAKeyGenParameterSpec.F4, null};
115
KeyPair[] keyPairs = new KeyPair[3];
116
new Random().nextBytes(data);
117
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", provider);
118
for (int i = 0; i < keyLengths.length; i++) {
119
int len = keyLengths[i];
120
BigInteger exp = pubExps[i];
121
System.out.println("Generating " + len + " bit keypair...");
122
if (exp == null) {
123
kpg.initialize(len);
124
} else {
125
kpg.initialize(new RSAKeyGenParameterSpec(len, exp));
126
}
127
KeyPair kp = kpg.generateKeyPair();
128
keyPairs[i] = kp;
129
RSAPublicKey publicKey = (RSAPublicKey)kp.getPublic();
130
System.out.println(publicKey);
131
RSAPrivateCrtKey privateKey = (RSAPrivateCrtKey)kp.getPrivate();
132
if (publicKey.getModulus().equals(privateKey.getModulus()) == false) {
133
throw new Exception("Moduli do not match");
134
}
135
if (publicKey.getPublicExponent().equals(privateKey.getPublicExponent()) == false) {
136
throw new Exception("Exponents do not match");
137
}
138
int keyLen = publicKey.getModulus().bitLength();
139
if ((keyLen > len) || (keyLen < len - 1)) {
140
throw new Exception("Incorrect key length: " + keyLen);
141
}
142
if (exp != null) {
143
if (exp.equals(publicKey.getPublicExponent()) == false) {
144
throw new Exception("Incorrect exponent");
145
}
146
}
147
test(privateKey, publicKey);
148
}
149
testInvalidSignature(keyPairs[0], keyPairs[1]);
150
testInvalidSignature(keyPairs[0], keyPairs[2]);
151
long stop = System.currentTimeMillis();
152
System.out.println("All tests passed (" + (stop - start) + " ms).");
153
}
154
}
155
156