blob: 0a1ff3d0da70c4fd7e6af443c4a58ed42eb59a93 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2005-2006 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any 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 */
31
32import java.util.*;
33
34import java.security.*;
35import java.security.KeyStore.*;
36import java.security.cert.*;
37
38public class TrustAnchors extends SecmodTest {
39
40 public static void main(String[] args) throws Exception {
41 if (initSecmod() == false) {
42 return;
43 }
44
45 if (System.getProperty("os.name").toLowerCase().startsWith("win")) {
46 // our secmod.db file says nssckbi.*so*, so NSS does not find the
47 // *DLL* on windows.
48 System.out.println("Test currently does not work on Windows, skipping");
49 return;
50 }
51
52 String configName = BASE + SEP + "nsstrust.cfg";
53 Provider p = getSunPKCS11(configName);
54
55 System.out.println(p);
56 Security.addProvider(p);
57 KeyStore ks = KeyStore.getInstance("PKCS11", p);
58 ks.load(null, null);
59 Collection<String> aliases = new TreeSet<String>(Collections.list(ks.aliases()));
60 System.out.println("entries: " + aliases.size());
61 System.out.println(aliases);
62
63 for (String alias : aliases) {
64 if (ks.isCertificateEntry(alias) == false) {
65 throw new Exception("not trusted: " + alias);
66 }
67 X509Certificate cert = (X509Certificate)ks.getCertificate(alias);
68 // verify self-signed certs
69 if (cert.getSubjectX500Principal().equals(cert.getIssuerX500Principal())) {
70 System.out.print(".");
71 cert.verify(cert.getPublicKey());
72 } else {
73 System.out.print("-");
74 }
75 }
76
77 System.out.println();
78 System.out.println("OK");
79 }
80
81}