blob: c99d5b5e585298de95d6526151328b476eec3b9f [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 6323647
27 * @summary Verify that the SunJSSE trustmanager works correctly in FIPS mode
28 * @author Andreas Sterbenz
29 * @library ..
30 */
31
32import java.io.*;
33import java.util.*;
34
35import java.security.*;
36import java.security.cert.*;
37
38import javax.net.ssl.*;
39
40// This test belongs more in JSSE than here, but the JSSE workspace does not
41// have the NSS test infrastructure. It will live here for the time being.
42
43public class TrustManagerTest extends SecmodTest {
44
45 public static void main(String[] args) throws Exception {
46 if (initSecmod() == false) {
47 return;
48 }
49
50 if ("sparc".equals(System.getProperty("os.arch")) == false) {
51 // we have not updated other platforms with the proper NSS libraries yet
52 System.out.println("Test currently works only on solaris-sparc, skipping");
53 return;
54 }
55
56 String configName = BASE + SEP + "fips.cfg";
57 Provider p = getSunPKCS11(configName);
58
59 System.out.println(p);
60 Security.addProvider(p);
61
62 Security.removeProvider("SunJSSE");
63 Provider jsse = new com.sun.net.ssl.internal.ssl.Provider(p);
64 Security.addProvider(jsse);
65 System.out.println(jsse.getInfo());
66
67 KeyStore ks = KeyStore.getInstance("PKCS11", p);
68 ks.load(null, "test12".toCharArray());
69
70 X509Certificate server = loadCertificate("certs/server.cer");
71 X509Certificate ca = loadCertificate("certs/ca.cer");
72 X509Certificate anchor = loadCertificate("certs/anchor.cer");
73
74 KeyStore trustStore = KeyStore.getInstance("JKS");
75 trustStore.load(null, null);
76 trustStore.setCertificateEntry("anchor", anchor);
77
78 TrustManagerFactory tmf = TrustManagerFactory.getInstance("PKIX");
79 tmf.init(trustStore);
80
81 X509TrustManager tm = (X509TrustManager)tmf.getTrustManagers()[0];
82
83 X509Certificate[] chain = {server, ca, anchor};
84
85 tm.checkServerTrusted(chain, "RSA");
86
87 System.out.println("OK");
88 }
89
90 private static X509Certificate loadCertificate(String name) throws Exception {
91 CertificateFactory cf = CertificateFactory.getInstance("X.509");
92 InputStream in = new FileInputStream(BASE + SEP + name);
93 X509Certificate cert = (X509Certificate)cf.generateCertificate(in);
94 in.close();
95 return cert;
96 }
97
98}