blob: c82316e81a649dcbbead74d8d7ea4053657a4d0a [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2005 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
27 * @summary make sure we can add a trusted cert to the NSS KeyStore module
28 * @author Andreas Sterbenz
29 * @library ..
30 */
31
32import java.io.*;
33import java.util.*;
34
35import java.security.*;
36import java.security.KeyStore.*;
37import java.security.cert.*;
38
39public class AddTrustedCert extends SecmodTest {
40
41 public static void main(String[] args) throws Exception {
42 if (initSecmod() == false) {
43 return;
44 }
45
46 InputStream in = new FileInputStream(BASE + SEP + "anchor.cer");
47 CertificateFactory factory = CertificateFactory.getInstance("X.509");
48 X509Certificate cert = (X509Certificate)factory.generateCertificate(in);
49 in.close();
50// System.out.println(cert);
51
52 String configName = BASE + SEP + "nss.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, password);
59 Collection<String> aliases = new TreeSet<String>(Collections.list(ks.aliases()));
60 System.out.println("entries: " + aliases.size());
61 System.out.println(aliases);
62 int size1 = aliases.size();
63
64 String alias = "anchor";
65 ks.setCertificateEntry(alias, cert);
66 ks.setCertificateEntry(alias, cert);
67
68 aliases = new TreeSet<String>(Collections.list(ks.aliases()));
69 System.out.println("entries: " + aliases.size());
70 System.out.println(aliases);
71 int size2 = aliases.size();
72
73 if ((size2 != size1 + 1) || (aliases.contains(alias) == false)) {
74 throw new Exception("Trusted cert not added");
75 }
76 X509Certificate cert2 = (X509Certificate)ks.getCertificate(alias);
77 if (cert.equals(cert2) == false) {
78 throw new Exception("KeyStore returned incorrect certificate");
79 }
80
81 System.out.println("OK");
82
83 }
84
85}