Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/security/pkcs11/Secmod/LoadKeystore.java
41153 views
1
/*
2
* Copyright (c) 2015, 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 8048622 8134232
27
* @summary Checks that PKCS#11 keystore can't be loaded with wrong password
28
* @library /test/lib ../
29
* @modules jdk.crypto.cryptoki
30
* @run main/othervm LoadKeystore
31
* @run main/othervm -Djava.security.manager=allow LoadKeystore sm policy
32
*/
33
34
import java.io.File;
35
import java.io.IOException;
36
import java.security.KeyStore;
37
import java.security.KeyStoreException;
38
import java.security.Provider;
39
import java.security.Security;
40
import java.security.UnrecoverableKeyException;
41
import java.util.Collections;
42
43
public class LoadKeystore extends SecmodTest {
44
45
public static void main(String[] args) throws Exception {
46
if (args.length > 1 && "sm".equals(args[0])) {
47
System.setProperty("java.security.policy",
48
BASE + File.separator + args[1]);
49
}
50
51
if (!initSecmod()) {
52
return;
53
}
54
55
String configName = BASE + SEP + "nss.cfg";
56
Provider p = getSunPKCS11(configName);
57
58
System.out.println("Add provider " + p);
59
System.out.println();
60
Security.addProvider(p);
61
62
if (args.length > 1 && "sm".equals(args[0])) {
63
System.setSecurityManager(new SecurityManager());
64
}
65
66
try {
67
System.out.println("Load keystore with wrong type");
68
KeyStore.getInstance("unknown", p);
69
throw new RuntimeException("Expected exception not thrown");
70
} catch(KeyStoreException e) {
71
System.out.println("Expected exception: " + e);
72
}
73
74
KeyStore ks = KeyStore.getInstance("PKCS11", p);
75
if (!"PKCS11".equals(ks.getType())) {
76
throw new RuntimeException("Unexpected keystore type: "
77
+ ks.getType());
78
}
79
if (!p.equals(ks.getProvider())) {
80
throw new RuntimeException("Unexpected keystore provider: "
81
+ ks.getProvider());
82
}
83
84
try {
85
System.out.println("Load keystore with wrong password");
86
ks.load(null, "wrong".toCharArray());
87
throw new RuntimeException("Expected exception not thrown");
88
} catch(IOException e) {
89
System.out.println("Expected exception: " + e);
90
Throwable cause = e.getCause();
91
if (!(cause instanceof UnrecoverableKeyException)) {
92
e.printStackTrace(System.out);
93
throw new RuntimeException("Unexpected cause: " + cause);
94
}
95
System.out.println("Expected cause: " + cause);
96
}
97
98
System.out.println("Load keystore with correct password");
99
ks.load(null, password);
100
for (String alias : Collections.list(ks.aliases())) {
101
System.out.println("Alias: " + alias);
102
}
103
104
System.out.println("Test passed");
105
}
106
107
}
108
109