author | asmotrak |
Tue, 26 Jan 2016 13:32:07 -0800 | |
changeset 35379 | 1e8e336ef66b |
parent 32635 | d7e4ba3c2e0d |
child 40975 | 680639c9b307 |
permissions | -rw-r--r-- |
32635 | 1 |
/* |
35379
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
32635
diff
changeset
|
2 |
* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. |
32635 | 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 |
||
35379
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
32635
diff
changeset
|
24 |
import java.io.File; |
32635 | 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 |
|
35379
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
32635
diff
changeset
|
39 |
* @run main/othervm LoadKeystore sm policy |
32635 | 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 |
||
35379
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
32635
diff
changeset
|
55 |
if (args.length > 1 && "sm".equals(args[0])) { |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
32635
diff
changeset
|
56 |
System.setProperty("java.security.policy", |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
32635
diff
changeset
|
57 |
BASE + File.separator + args[1]); |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
32635
diff
changeset
|
58 |
System.setSecurityManager(new SecurityManager()); |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
32635
diff
changeset
|
59 |
} |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
32635
diff
changeset
|
60 |
|
32635 | 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 |
} |