Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/security/rsa/TestKeyFactory.java
41149 views
1
/*
2
* Copyright (c) 2003, 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
24
/**
25
* @test
26
* @bug 4853305 8023980 8254717
27
* @summary Test KeyFactory of the new RSA provider
28
* @author Andreas Sterbenz
29
*/
30
31
import java.io.*;
32
import java.util.*;
33
34
import java.security.*;
35
import java.security.interfaces.*;
36
import java.security.spec.*;
37
38
public class TestKeyFactory {
39
40
private final static String BASE = System.getProperty("test.src", ".");
41
42
private static final char[] password = "test12".toCharArray();
43
44
private static final String PKCS1_PRIV_STR =
45
// the BASE64 string between -----BEGIN RSA PRIVATE KEY-----
46
// and -----END RSA PRIVATE KEY-----
47
"MIIEowIBAAKCAQEA0OIArlYES4X1XMTLDordtN/XIWFE1wvhl40RsHWM2n99+Stp" +
48
"CCJCcUb5FJ2/kefj/XRwB6p5IMpIZrHZqC8XXzlX5fpiFaSu2xnk17oWUKoErW27" +
49
"Stm098pU2RoUxW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Rgk+xpJNl" +
65
"0154SQCNvKPghJiw6aDFSifkytA01tp9/a8QWCwF433RjiFPsoekjvHQ6Y34dofO" +
66
"xDhf7lwJKPBFCrfYIqocklECgYAIPI9OHHGP8NKw94UJ0fX/WGug5sHVbQ9sWvOy" +
67
"KLMBlxLMxqFadlUaOpvVZvdxnX++ktajwpGxJDhX9OWWsYGobm1buB7N1E1Prrg+" +
68
"gt0RWpMhZa3Xeb/8Jorr2Lfo8sWK0LQyTE8hQCSIthfoWL9FeJJn/GKF/dSj8kxU" +
69
"0QIGMQKBgG/8U/zZ87DzfXS81P1p+CmH474wmou4KD2/zXp/lDR9+dlIUeijlIbU" +
70
"P6Y5xJvT33Y40giW9irShgDHjZgw0ap11K3b2HzLImdPEaBiENo735rpLs8WLK9H" +
71
"+yeRbiP2y9To7sTihm9Jrkctzp6sqFtKyye1+S21X1tMz8NGfXen";
72
73
private static final String PKCS1_PUB_STR =
74
// the BASE64 string between -----BEGIN RSA PUBLIC KEY-----
75
// and -----END RSA PUBLIC KEY-----
76
"MIIBCgKCAQEA0OIArlYES4X1XMTLDordtN/XIWFE1wvhl40RsHWM2n99+StpCCJC" +
77
"cUb5FJ2/kefj/XRwB6p5IMpIZrHZqC8XXzlX5fpiFaSu2xnk17oWUKoErW27Stm0" +
78
"98pU2RoUxWPKVl+42a8iVp8tijNElBNFALCGi0zXOhcTxMh0q1Wk0UhMJqamv5Yn" +
79
"CKmT4THwwGYn/KeK3M7Qa+o5MoVBHLbeT9LJgEmSluVzIh44Lh6weX0bw72P8X2p" +
80
"raOhbzg2B343MqS/rMLw6On+0i7ccEgp23vX9G5w85q4A5FSIrk4S/pyv5sOrwjC" +
81
"QKBW1TS0/2iB9zNkFMj5/+h7l2oqTT7sSQIDAQAB";
82
83
84
private static final PrivateKey P1_PRIV;
85
private static final PublicKey P1_PUB;
86
87
static {
88
byte[] encodedPriv = Base64.getDecoder().decode(PKCS1_PRIV_STR);
89
P1_PRIV = new PrivateKey() {
90
@Override
91
public String getAlgorithm() {
92
return "RSA";
93
}
94
@Override
95
public String getFormat() {
96
return "PKCS#1";
97
}
98
@Override
99
public byte[] getEncoded() {
100
return encodedPriv.clone();
101
}
102
};
103
byte[] encodedPub = Base64.getDecoder().decode(PKCS1_PUB_STR);
104
P1_PUB = new PublicKey() {
105
@Override
106
public String getAlgorithm() {
107
return "RSA";
108
}
109
@Override
110
public String getFormat() {
111
return "PKCS#1";
112
}
113
@Override
114
public byte[] getEncoded() {
115
return encodedPub.clone();
116
}
117
};
118
}
119
120
static KeyStore getKeyStore() throws Exception {
121
InputStream in = new FileInputStream(new File(BASE, "rsakeys.ks"));
122
KeyStore ks = KeyStore.getInstance("JKS");
123
ks.load(in, password);
124
in.close();
125
return ks;
126
}
127
128
/**
129
* Test that key1 (reference key) and key2 (key to be tested) are
130
* equivalent
131
*/
132
private static void testKey(Key key1, Key key2) throws Exception {
133
if (key2.getAlgorithm().equals("RSA") == false) {
134
throw new Exception("Algorithm not RSA");
135
}
136
if (key1 instanceof PublicKey) {
137
if (key2.getFormat().equals("X.509") == false) {
138
throw new Exception("Format not X.509");
139
}
140
} else if (key1 instanceof PrivateKey) {
141
if (key2.getFormat().equals("PKCS#8") == false) {
142
throw new Exception("Format not PKCS#8: " + key2.getFormat());
143
}
144
}
145
// skip equals check when key1 is custom key
146
if (key1 != P1_PRIV && key1 != P1_PUB) {
147
if (!key1.equals(key2)) {
148
throw new Exception("Keys not equal");
149
}
150
}
151
152
// only compare encodings if keys are of the same format
153
if (key1.getFormat().equals(key2.getFormat()) &&
154
!Arrays.equals(key1.getEncoded(), key2.getEncoded())) {
155
throw new Exception("Encodings not equal");
156
}
157
}
158
159
private static void testPublic(KeyFactory kf, PublicKey key)
160
throws Exception {
161
System.out.println("Testing " + (key == P1_PUB? "PKCS#1" : "") +
162
" public key...");
163
164
PublicKey key2 = (PublicKey)kf.translateKey(key);
165
KeySpec rsaSpec = kf.getKeySpec(key, RSAPublicKeySpec.class);
166
PublicKey key3 = kf.generatePublic(rsaSpec);
167
KeySpec x509Spec = kf.getKeySpec(key, X509EncodedKeySpec.class);
168
PublicKey key4 = kf.generatePublic(x509Spec);
169
if (key != P1_PUB) {
170
testKey(key, key);
171
}
172
testKey(key, key2);
173
testKey(key, key3);
174
testKey(key, key4);
175
176
if (key.getFormat().equalsIgnoreCase("X.509")) {
177
KeySpec x509Spec2 = new X509EncodedKeySpec(key.getEncoded());
178
PublicKey key5 = kf.generatePublic(x509Spec2);
179
testKey(key, key5);
180
}
181
}
182
183
private static void testPrivate(KeyFactory kf, PrivateKey key)
184
throws Exception {
185
System.out.println("Testing " + (key == P1_PRIV? "PKCS#1" : "") +
186
" private key...");
187
PrivateKey key2 = (PrivateKey)kf.translateKey(key);
188
KeySpec rsaSpec = kf.getKeySpec(key, RSAPrivateCrtKeySpec.class);
189
PrivateKey key3 = kf.generatePrivate(rsaSpec);
190
KeySpec pkcs8Spec = kf.getKeySpec(key, PKCS8EncodedKeySpec.class);
191
PrivateKey key4 = kf.generatePrivate(pkcs8Spec);
192
if (key != P1_PRIV) {
193
testKey(key, key);
194
}
195
testKey(key, key2);
196
testKey(key, key3);
197
testKey(key, key4);
198
if (key.getFormat().equalsIgnoreCase("PKCS#8")) {
199
KeySpec pkcs8Spec2 = new PKCS8EncodedKeySpec(key.getEncoded());
200
PrivateKey key5 = kf.generatePrivate(pkcs8Spec2);
201
testKey(key, key5);
202
}
203
204
KeySpec rsaSpec2 = kf.getKeySpec(key, RSAPrivateKeySpec.class);
205
PrivateKey key6 = kf.generatePrivate(rsaSpec2);
206
testKey(key6, key6);
207
if (key instanceof RSAPrivateCrtKey) {
208
RSAPrivateCrtKey rsaKey = (RSAPrivateCrtKey)key;
209
KeySpec rsaSpec3 = new RSAPrivateCrtKeySpec(rsaKey.getModulus(),
210
rsaKey.getPublicExponent(), rsaKey.getPrivateExponent(), rsaKey.getPrimeP(), rsaKey.getPrimeQ(),
211
rsaKey.getPrimeExponentP(), rsaKey.getPrimeExponentQ(), rsaKey.getCrtCoefficient(), rsaKey.getParams());
212
PrivateKey key7 = kf.generatePrivate(rsaSpec3);
213
testKey(key6, key7);
214
}
215
}
216
217
private static void test(KeyFactory kf, Key key) throws Exception {
218
if (key.getAlgorithm().equals("RSA") == false) {
219
System.out.println("Not an RSA key, ignoring");
220
}
221
if (key instanceof PublicKey) {
222
testPublic(kf, (PublicKey)key);
223
} else if (key instanceof PrivateKey) {
224
testPrivate(kf, (PrivateKey)key);
225
}
226
}
227
228
public static void main(String[] args) throws Exception {
229
long start = System.currentTimeMillis();
230
KeyStore ks = getKeyStore();
231
KeyFactory kf = KeyFactory.getInstance("RSA", "SunRsaSign");
232
for (Enumeration e = ks.aliases(); e.hasMoreElements(); ) {
233
String alias = (String)e.nextElement();
234
Key key = null;
235
if (ks.isKeyEntry(alias)) {
236
test(kf, ks.getKey(alias, password));
237
test(kf, ks.getCertificate(alias).getPublicKey());
238
}
239
}
240
// repeat the test w/ PKCS#1 RSA Private Key
241
test(kf, P1_PRIV);
242
test(kf, P1_PUB);
243
244
long stop = System.currentTimeMillis();
245
System.out.println("All tests passed (" + (stop - start) + " ms).");
246
}
247
}
248
249