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