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