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