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/rsa/SignatureTest.java
38839 views
1
/*
2
* Copyright (c) 2015, 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
import java.security.*;
24
import java.security.interfaces.RSAPrivateKey;
25
import java.security.interfaces.RSAPublicKey;
26
import java.security.spec.*;
27
import java.util.*;
28
import static javax.crypto.Cipher.PRIVATE_KEY;
29
import static javax.crypto.Cipher.PUBLIC_KEY;
30
import jdk.testlibrary.RandomFactory;
31
32
import jdk.test.lib.SigTestUtil;
33
import static jdk.test.lib.SigTestUtil.SignatureType;
34
35
/**
36
* @test
37
* @bug 8044199 8146293
38
* @summary Create a signature for RSA and get its signed data. re-initiate
39
* the signature with the public key. The signature can be verified
40
* by acquired signed data.
41
* @library /lib
42
* @key randomness
43
* @library ../../../lib/testlibrary
44
* @run main SignatureTest MD2withRSA 512
45
* @run main SignatureTest MD5withRSA 512
46
* @run main SignatureTest SHA1withRSA 512
47
* @run main SignatureTest SHA256withRSA 512
48
* @run main SignatureTest MD2withRSA 768
49
* @run main SignatureTest MD5withRSA 768
50
* @run main SignatureTest SHA1withRSA 768
51
* @run main SignatureTest SHA256withRSA 768
52
* @run main SignatureTest MD2withRSA 1024
53
* @run main SignatureTest MD5withRSA 1024
54
* @run main SignatureTest SHA1withRSA 1024
55
* @run main SignatureTest SHA256withRSA 1024
56
* @run main SignatureTest MD2withRSA 2048
57
* @run main SignatureTest MD5withRSA 2048
58
* @run main SignatureTest SHA1withRSA 2048
59
* @run main SignatureTest SHA256withRSA 2048
60
* @run main/timeout=240 SignatureTest MD2withRSA 4096
61
* @run main/timeout=240 SignatureTest MD5withRSA 4096
62
* @run main/timeout=240 SignatureTest SHA1withRSA 4096
63
* @run main/timeout=240 SignatureTest SHA256withRSA 4096
64
* @run main/timeout=240 SignatureTest MD2withRSA 5120
65
* @run main/timeout=240 SignatureTest MD5withRSA 5120
66
* @run main/timeout=240 SignatureTest SHA1withRSA 5120
67
* @run main/timeout=240 SignatureTest SHA256withRSA 5120
68
* @run main/timeout=480 SignatureTest MD2withRSA 6144
69
* @run main/timeout=480 SignatureTest MD5withRSA 6144
70
* @run main/timeout=480 SignatureTest SHA1withRSA 6144
71
* @run main/timeout=480 SignatureTest SHA256withRSA 6144
72
*/
73
public class SignatureTest {
74
/**
75
* ALGORITHM name, fixed as RSA.
76
*/
77
private static final String KEYALG = "RSA";
78
79
/**
80
* JDK default RSA Provider.
81
*/
82
private static final String PROVIDER = "SunRsaSign";
83
84
/**
85
* How much times signature updated.
86
*/
87
private static final int UPDATE_TIMES_FIFTY = 50;
88
89
/**
90
* How much times signature initial updated.
91
*/
92
private static final int UPDATE_TIMES_HUNDRED = 100;
93
94
public static void main(String[] args) throws Exception {
95
String testAlg = args[0];
96
int keySize = Integer.parseInt(args[1]);
97
Iterable<String> md_alg_pkcs15 =
98
SigTestUtil.getDigestAlgorithms(SignatureType.RSA, keySize);
99
100
Iterable<String> md_alg_pss =
101
SigTestUtil.getDigestAlgorithms(SignatureType.RSASSA_PSS, keySize);
102
103
byte[] data = new byte[100];
104
RandomFactory.getRandom().nextBytes(data);
105
106
// create a key pair
107
KeyPair kpair = generateKeys(KEYALG, keySize);
108
Key[] privs = manipulateKey(PRIVATE_KEY, kpair.getPrivate());
109
Key[] pubs = manipulateKey(PUBLIC_KEY, kpair.getPublic());
110
111
test(SignatureType.RSA, md_alg_pkcs15, privs, pubs, data);
112
test(SignatureType.RSASSA_PSS, md_alg_pss, privs, pubs, data);
113
}
114
115
private static void test(SignatureType type, Iterable<String> digestAlgs,
116
Key[] privs, Key[] pubs, byte[] data) throws RuntimeException {
117
118
// For signature algorithm, create and verify a signature
119
120
Arrays.stream(privs).forEach(priv
121
-> Arrays.stream(pubs).forEach(pub
122
-> digestAlgs.forEach(digestAlg -> {
123
try {
124
AlgorithmParameterSpec sigParams =
125
SigTestUtil.generateDefaultParameter(type, digestAlg);
126
String sigAlg = SigTestUtil.generateSigAlg(type, digestAlg);
127
checkSignature(data, (PublicKey) pub, (PrivateKey) priv,
128
sigAlg, sigParams);
129
} catch (NoSuchAlgorithmException | InvalidKeyException |
130
SignatureException | NoSuchProviderException |
131
InvalidAlgorithmParameterException ex) {
132
throw new RuntimeException(ex);
133
}
134
}
135
)));
136
}
137
138
private static KeyPair generateKeys(String keyalg, int size)
139
throws NoSuchAlgorithmException {
140
KeyPairGenerator kpg = KeyPairGenerator.getInstance(keyalg);
141
kpg.initialize(size);
142
return kpg.generateKeyPair();
143
}
144
145
private static Key[] manipulateKey(int type, Key key)
146
throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchProviderException {
147
KeyFactory kf = KeyFactory.getInstance(KEYALG, PROVIDER);
148
149
switch (type) {
150
case PUBLIC_KEY:
151
try {
152
kf.getKeySpec(key, RSAPrivateKeySpec.class);
153
throw new RuntimeException("Expected InvalidKeySpecException "
154
+ "not thrown");
155
} catch (InvalidKeySpecException expected) {
156
}
157
158
return new Key[]{
159
kf.generatePublic(kf.getKeySpec(key, RSAPublicKeySpec.class)),
160
kf.generatePublic(new X509EncodedKeySpec(key.getEncoded())),
161
kf.generatePublic(new RSAPublicKeySpec(
162
((RSAPublicKey) key).getModulus(),
163
((RSAPublicKey) key).getPublicExponent()))
164
};
165
case PRIVATE_KEY:
166
try {
167
kf.getKeySpec(key, RSAPublicKeySpec.class);
168
throw new RuntimeException("Expected InvalidKeySpecException"
169
+ " not thrown");
170
} catch (InvalidKeySpecException expected) {
171
}
172
return new Key[]{
173
kf.generatePrivate(kf.getKeySpec(key,
174
RSAPrivateKeySpec.class)),
175
kf.generatePrivate(new PKCS8EncodedKeySpec(
176
key.getEncoded())),
177
kf.generatePrivate(new RSAPrivateKeySpec(((RSAPrivateKey) key).getModulus(),
178
((RSAPrivateKey) key).getPrivateExponent()))
179
};
180
}
181
throw new RuntimeException("We shouldn't reach here");
182
}
183
184
private static void checkSignature(byte[] data, PublicKey pub,
185
PrivateKey priv, String sigAlg, AlgorithmParameterSpec sigParams)
186
throws NoSuchAlgorithmException, InvalidKeyException,
187
SignatureException, NoSuchProviderException,
188
InvalidAlgorithmParameterException {
189
System.out.println("Testing " + sigAlg);
190
Signature sig = Signature.getInstance(sigAlg, PROVIDER);
191
sig.setParameter(sigParams);
192
193
sig.initSign(priv);
194
for (int i = 0; i < UPDATE_TIMES_HUNDRED; i++) {
195
sig.update(data);
196
}
197
byte[] signedData = sig.sign();
198
199
// Make sure signature verifies with original data
200
sig.setParameter(sigParams);
201
sig.initVerify(pub);
202
for (int i = 0; i < UPDATE_TIMES_HUNDRED; i++) {
203
sig.update(data);
204
}
205
if (!sig.verify(signedData)) {
206
throw new RuntimeException("Failed to verify " + sigAlg
207
+ " signature");
208
}
209
210
// Make sure signature does NOT verify when the original data
211
// has changed
212
sig.initVerify(pub);
213
for (int i = 0; i < UPDATE_TIMES_FIFTY; i++) {
214
sig.update(data);
215
}
216
217
if (sig.verify(signedData)) {
218
throw new RuntimeException("Failed to detect bad " + sigAlg
219
+ " signature");
220
}
221
}
222
}
223
224