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/TestKeyFactory.java
38855 views
1
/*
2
* Copyright (c) 2003, 2016, 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 Test KeyFactory of the new RSA provider
28
* @author Andreas Sterbenz
29
* @library ..
30
* @run main/othervm TestKeyFactory
31
* @run main/othervm TestKeyFactory sm rsakeys.ks.policy
32
*/
33
34
import java.io.*;
35
import java.util.*;
36
37
import java.security.*;
38
import java.security.spec.*;
39
40
public class TestKeyFactory extends PKCS11Test {
41
42
private static final char[] password = "test12".toCharArray();
43
44
static KeyStore getKeyStore() throws Exception {
45
KeyStore ks;
46
try (InputStream in = new FileInputStream(new File(BASE, "rsakeys.ks"))) {
47
ks = KeyStore.getInstance("JKS");
48
ks.load(in, password);
49
}
50
return ks;
51
}
52
53
/**
54
* Test that key1 (reference key) and key2 (key to be tested) are
55
* equivalent
56
*/
57
private static void testKey(Key key1, Key key2) throws Exception {
58
if (key2.getAlgorithm().equals("RSA") == false) {
59
throw new Exception("Algorithm not RSA");
60
}
61
if (key1 instanceof PublicKey) {
62
if (key2.getFormat().equals("X.509") == false) {
63
throw new Exception("Format not X.509");
64
}
65
} else if (key1 instanceof PrivateKey) {
66
if (key2.getFormat().equals("PKCS#8") == false) {
67
throw new Exception("Format not PKCS#8");
68
}
69
}
70
if (key1.equals(key2) == false) {
71
throw new Exception("Keys not equal");
72
}
73
if (Arrays.equals(key1.getEncoded(), key2.getEncoded()) == false) {
74
throw new Exception("Encodings not equal");
75
}
76
}
77
78
private static void testPublic(KeyFactory kf, PublicKey key) throws Exception {
79
System.out.println("Testing public key...");
80
PublicKey key2 = (PublicKey)kf.translateKey(key);
81
KeySpec rsaSpec = kf.getKeySpec(key, RSAPublicKeySpec.class);
82
PublicKey key3 = kf.generatePublic(rsaSpec);
83
KeySpec x509Spec = kf.getKeySpec(key, X509EncodedKeySpec.class);
84
PublicKey key4 = kf.generatePublic(x509Spec);
85
KeySpec x509Spec2 = new X509EncodedKeySpec(key.getEncoded());
86
PublicKey key5 = kf.generatePublic(x509Spec2);
87
testKey(key, key);
88
testKey(key, key2);
89
testKey(key, key3);
90
testKey(key, key4);
91
testKey(key, key5);
92
}
93
94
private static void testPrivate(KeyFactory kf, PrivateKey key) throws Exception {
95
System.out.println("Testing private key...");
96
PrivateKey key2 = (PrivateKey)kf.translateKey(key);
97
KeySpec rsaSpec = kf.getKeySpec(key, RSAPrivateCrtKeySpec.class);
98
PrivateKey key3 = kf.generatePrivate(rsaSpec);
99
KeySpec pkcs8Spec = kf.getKeySpec(key, PKCS8EncodedKeySpec.class);
100
PrivateKey key4 = kf.generatePrivate(pkcs8Spec);
101
KeySpec pkcs8Spec2 = new PKCS8EncodedKeySpec(key.getEncoded());
102
PrivateKey key5 = kf.generatePrivate(pkcs8Spec2);
103
testKey(key, key);
104
testKey(key, key2);
105
testKey(key, key3);
106
testKey(key, key4);
107
testKey(key, key5);
108
109
// XXX PKCS#11 providers may not support non-CRT keys (e.g. NSS)
110
// KeySpec rsaSpec2 = kf.getKeySpec(key, RSAPrivateKeySpec.class);
111
// PrivateKey key6 = kf.generatePrivate(rsaSpec2);
112
// RSAPrivateKey rsaKey = (RSAPrivateKey)key;
113
// KeySpec rsaSpec3 = new RSAPrivateKeySpec(rsaKey.getModulus(), rsaKey.getPrivateExponent());
114
// PrivateKey key7 = kf.generatePrivate(rsaSpec3);
115
// testKey(key6, key6);
116
// testKey(key6, key7);
117
}
118
119
private static void test(KeyFactory kf, Key key) throws Exception {
120
if (key.getAlgorithm().equals("RSA") == false) {
121
System.out.println("Not an RSA key, ignoring");
122
}
123
if (key instanceof PublicKey) {
124
testPublic(kf, (PublicKey)key);
125
} else if (key instanceof PrivateKey) {
126
testPrivate(kf, (PrivateKey)key);
127
}
128
}
129
130
public static void main(String[] args) throws Exception {
131
main(new TestKeyFactory(), args);
132
}
133
134
@Override
135
public void main(Provider p) throws Exception {
136
long start = System.currentTimeMillis();
137
KeyStore ks = getKeyStore();
138
KeyFactory kf = KeyFactory.getInstance("RSA", p);
139
for (Enumeration e = ks.aliases(); e.hasMoreElements(); ) {
140
String alias = (String)e.nextElement();
141
Key key = null;
142
if (ks.isKeyEntry(alias)) {
143
test(kf, ks.getKey(alias, password));
144
test(kf, ks.getCertificate(alias).getPublicKey());
145
}
146
}
147
long stop = System.currentTimeMillis();
148
System.out.println("All tests passed (" + (stop - start) + " ms).");
149
}
150
}
151
152