author | asmotrak |
Tue, 26 Jan 2016 13:32:07 -0800 | |
changeset 35379 | 1e8e336ef66b |
parent 26958 | cb5cbde6e7a4 |
child 40975 | 680639c9b307 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
35379
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
26958
diff
changeset
|
2 |
* Copyright (c) 2005, 2016, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 22 |
*/ |
23 |
||
24 |
/** |
|
25 |
* @test |
|
26958 | 26 |
* @bug 6298106 6275523 6420252 8059627 |
2 | 27 |
* @summary make sure we can access the NSS trust anchor module |
28 |
* @author Andreas Sterbenz |
|
29 |
* @library .. |
|
10328 | 30 |
* @run main/othervm TrustAnchors |
35379
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
26958
diff
changeset
|
31 |
* @run main/othervm TrustAnchors sm policy |
2 | 32 |
*/ |
33 |
||
35379
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
26958
diff
changeset
|
34 |
import java.io.File; |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
26958
diff
changeset
|
35 |
import java.security.KeyStore; |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
26958
diff
changeset
|
36 |
import java.security.Provider; |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
26958
diff
changeset
|
37 |
import java.security.Security; |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
26958
diff
changeset
|
38 |
import java.security.cert.X509Certificate; |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
26958
diff
changeset
|
39 |
import java.util.Collection; |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
26958
diff
changeset
|
40 |
import java.util.Collections; |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
26958
diff
changeset
|
41 |
import java.util.TreeSet; |
2 | 42 |
|
43 |
public class TrustAnchors extends SecmodTest { |
|
44 |
||
45 |
public static void main(String[] args) throws Exception { |
|
46 |
if (initSecmod() == false) { |
|
47 |
return; |
|
48 |
} |
|
49 |
||
26958 | 50 |
// our secmod.db file says nssckbi.*so*, so NSS does not find the |
51 |
// *DLL* on Windows nor the *DYLIB* on Mac OSX. |
|
52 |
String osName = System.getProperty("os.name").toLowerCase(); |
|
53 |
if (osName.startsWith("win") || osName.startsWith("mac")) { |
|
54 |
System.out.println("Test currently does not work on " + osName + |
|
55 |
", skipping"); |
|
2 | 56 |
return; |
57 |
} |
|
58 |
||
59 |
String configName = BASE + SEP + "nsstrust.cfg"; |
|
60 |
Provider p = getSunPKCS11(configName); |
|
61 |
||
62 |
System.out.println(p); |
|
63 |
Security.addProvider(p); |
|
35379
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
26958
diff
changeset
|
64 |
|
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
26958
diff
changeset
|
65 |
if (args.length > 1 && "sm".equals(args[0])) { |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
26958
diff
changeset
|
66 |
System.setProperty("java.security.policy", |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
26958
diff
changeset
|
67 |
BASE + File.separator + args[1]); |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
26958
diff
changeset
|
68 |
System.setSecurityManager(new SecurityManager()); |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
26958
diff
changeset
|
69 |
} |
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
26958
diff
changeset
|
70 |
|
2 | 71 |
KeyStore ks = KeyStore.getInstance("PKCS11", p); |
72 |
ks.load(null, null); |
|
35379
1e8e336ef66b
8144539: Update PKCS11 tests to run with security manager
asmotrak
parents:
26958
diff
changeset
|
73 |
Collection<String> aliases = new TreeSet<>(Collections.list(ks.aliases())); |
2 | 74 |
System.out.println("entries: " + aliases.size()); |
75 |
System.out.println(aliases); |
|
76 |
||
77 |
for (String alias : aliases) { |
|
78 |
if (ks.isCertificateEntry(alias) == false) { |
|
79 |
throw new Exception("not trusted: " + alias); |
|
80 |
} |
|
81 |
X509Certificate cert = (X509Certificate)ks.getCertificate(alias); |
|
82 |
// verify self-signed certs |
|
83 |
if (cert.getSubjectX500Principal().equals(cert.getIssuerX500Principal())) { |
|
84 |
System.out.print("."); |
|
85 |
cert.verify(cert.getPublicKey()); |
|
86 |
} else { |
|
87 |
System.out.print("-"); |
|
88 |
} |
|
89 |
} |
|
90 |
||
91 |
System.out.println(); |
|
92 |
System.out.println("OK"); |
|
93 |
} |
|
94 |
||
95 |
} |