Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/security/pkcs11/Secmod/AddPrivateKey.java
41153 views
1
/*
2
* Copyright (c) 2006, 2018, 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 6414980
27
* @summary Test that the PKCS#11 KeyStore handles RSA, DSA, and EC keys
28
* @author Andreas Sterbenz
29
* @library /test/lib ..
30
* @modules jdk.crypto.cryptoki
31
* @run main/othervm AddPrivateKey
32
* @run main/othervm -Djava.security.manager=allow AddPrivateKey sm policy
33
*/
34
35
import java.io.File;
36
import java.io.FileInputStream;
37
import java.io.InputStream;
38
import java.security.KeyFactory;
39
import java.security.KeyStore;
40
import java.security.KeyStore.PasswordProtection;
41
import java.security.KeyStore.PrivateKeyEntry;
42
import java.security.KeyStoreException;
43
import java.security.PrivateKey;
44
import java.security.Provider;
45
import java.security.PublicKey;
46
import java.security.Security;
47
import java.security.Signature;
48
import java.security.cert.X509Certificate;
49
import java.util.Arrays;
50
import java.util.Collections;
51
import java.util.List;
52
53
// this test is currently only run for the NSS KeyStore provider, but it
54
// is really a generic KeyStore test so it should be modified to run for
55
// all providers.
56
public class AddPrivateKey extends SecmodTest {
57
58
private static final String ALIAS1 = "entry1";
59
private static final String ALIAS2 = "entry2";
60
private static final String ALIAS3 = "entry3";
61
private static final int MAX_LINE = 85;
62
private static final int DATA_LENGTH = 4096;
63
private static final byte[] DATA = generateData(DATA_LENGTH);
64
65
public static void main(String[] args) throws Exception {
66
if (args.length > 1 && "sm".equals(args[0])) {
67
System.setProperty("java.security.policy",
68
BASE + File.separator + args[1]);
69
}
70
71
if (initSecmod() == false) {
72
return;
73
}
74
75
String configName = BASE + SEP + "nss.cfg";
76
Provider p = getSunPKCS11(configName);
77
78
boolean supportsEC = (p.getService("KeyFactory", "EC") != null);
79
80
System.out.println(p);
81
System.out.println();
82
Security.addProvider(p);
83
84
if (args.length > 1 && "sm".equals(args[0])) {
85
System.setSecurityManager(new SecurityManager());
86
}
87
88
KeyStore ks = KeyStore.getInstance(PKCS11, p);
89
ks.load(null, password);
90
for (String alias : aliases(ks)) {
91
System.out.println("Deleting: " + alias);
92
ks.deleteEntry(alias);
93
}
94
95
KeyStore jks = KeyStore.getInstance("JKS");
96
try (InputStream in = new FileInputStream(BASE + SEP + "keystore.jks")) {
97
char[] jkspass = "passphrase".toCharArray();
98
jks.load(in, jkspass);
99
for (String alias : Collections.list(jks.aliases())) {
100
if (jks.entryInstanceOf(alias, PrivateKeyEntry.class)) {
101
PrivateKeyEntry entry = (PrivateKeyEntry)jks.getEntry(alias,
102
new PasswordProtection(jkspass));
103
String algorithm = entry.getPrivateKey().getAlgorithm();
104
System.out.printf("-Entry %s (%s)%n", alias, algorithm);
105
if ((supportsEC == false) && algorithm.equals("EC")) {
106
System.out.println("EC not supported by provider, "
107
+ "skipping");
108
continue;
109
}
110
if ((supportsEC == false) && algorithm.equals("DSA")) {
111
System.out.println("Provider does not appear to have "
112
+ "CKA_NETSCAPE_DB fix, skipping");
113
continue;
114
}
115
test(p, entry);
116
} // else ignore
117
}
118
}
119
System.out.println("OK");
120
}
121
122
private static List<String> aliases(KeyStore ks) throws KeyStoreException {
123
return Collections.list(ks.aliases());
124
}
125
126
private static void test(Provider p, PrivateKeyEntry entry) throws Exception {
127
PrivateKey key = entry.getPrivateKey();
128
X509Certificate[] chain = (X509Certificate[])entry.getCertificateChain();
129
PublicKey publicKey = chain[0].getPublicKey();
130
System.out.println(toString(key));
131
sign(p, key, publicKey);
132
133
KeyStore ks = KeyStore.getInstance("PKCS11", p);
134
ks.load(null, null);
135
if (ks.size() != 0) {
136
throw new Exception("KeyStore not empty");
137
}
138
List<String> aliases;
139
140
// test 1: add entry
141
ks.setKeyEntry(ALIAS1, key, null, chain);
142
aliases = aliases(ks);
143
if (aliases.size() != 1) {
144
throw new Exception("size not 1: " + aliases);
145
}
146
if (aliases.get(0).equals(ALIAS1) == false) {
147
throw new Exception("alias mismatch: " + aliases);
148
}
149
150
PrivateKey key2 = (PrivateKey)ks.getKey(ALIAS1, null);
151
System.out.println(toString(key2));
152
X509Certificate[] chain2 =
153
(X509Certificate[]) ks.getCertificateChain(ALIAS1);
154
if (Arrays.equals(chain, chain2) == false) {
155
throw new Exception("chain mismatch");
156
}
157
sign(p, key2, publicKey);
158
159
ks.deleteEntry(ALIAS1);
160
if (ks.size() != 0) {
161
throw new Exception("KeyStore not empty");
162
}
163
164
// test 2: translate to session object, then add entry
165
KeyFactory kf = KeyFactory.getInstance(key.getAlgorithm(), p);
166
PrivateKey key3 = (PrivateKey)kf.translateKey(key);
167
System.out.println(toString(key3));
168
sign(p, key3, publicKey);
169
170
ks.setKeyEntry(ALIAS2, key3, null, chain);
171
aliases = aliases(ks);
172
if (aliases.size() != 1) {
173
throw new Exception("size not 1");
174
}
175
if (aliases.get(0).equals(ALIAS2) == false) {
176
throw new Exception("alias mismatch: " + aliases);
177
}
178
179
PrivateKey key4 = (PrivateKey)ks.getKey(ALIAS2, null);
180
System.out.println(toString(key4));
181
X509Certificate[] chain4 = (X509Certificate[])
182
ks.getCertificateChain(ALIAS2);
183
if (Arrays.equals(chain, chain4) == false) {
184
throw new Exception("chain mismatch");
185
}
186
sign(p, key4, publicKey);
187
188
// test 3: change alias
189
ks.setKeyEntry(ALIAS3, key3, null, chain);
190
aliases = aliases(ks);
191
if (aliases.size() != 1) {
192
throw new Exception("size not 1");
193
}
194
if (aliases.get(0).equals(ALIAS3) == false) {
195
throw new Exception("alias mismatch: " + aliases);
196
}
197
198
PrivateKey key5 = (PrivateKey)ks.getKey(ALIAS3, null);
199
System.out.println(toString(key5));
200
X509Certificate[] chain5 = (X509Certificate[])
201
ks.getCertificateChain(ALIAS3);
202
if (Arrays.equals(chain, chain5) == false) {
203
throw new Exception("chain mismatch");
204
}
205
sign(p, key5, publicKey);
206
207
ks.deleteEntry(ALIAS3);
208
if (ks.size() != 0) {
209
throw new Exception("KeyStore not empty");
210
}
211
212
System.out.println("OK");
213
}
214
215
private static void sign(Provider p, PrivateKey privateKey,
216
PublicKey publicKey) throws Exception {
217
String keyAlg = privateKey.getAlgorithm();
218
String alg;
219
switch (keyAlg) {
220
case "RSA":
221
alg = "SHA1withRSA";
222
break;
223
case "DSA":
224
alg = "SHA1withDSA";
225
break;
226
case "EC":
227
alg = "SHA1withECDSA";
228
break;
229
default:
230
throw new Exception("Unknown algorithm " + keyAlg);
231
}
232
Signature s = Signature.getInstance(alg, p);
233
s.initSign(privateKey);
234
s.update(DATA);
235
byte[] sig = s.sign();
236
237
s.initVerify(publicKey);
238
s.update(DATA);
239
if (s.verify(sig) == false) {
240
throw new Exception("Signature did not verify");
241
}
242
}
243
244
private static String toString(Object o) {
245
String s = String.valueOf(o).split("\n")[0];
246
return (s.length() <= MAX_LINE) ? s : s.substring(0, MAX_LINE);
247
}
248
249
}
250
251