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