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