Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/pkcs11/fips/TrustManagerTest.java
38855 views
/*1* Copyright (c) 2005, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 632364726* @summary Verify that the SunJSSE trustmanager works correctly in FIPS mode27* @author Andreas Sterbenz28* @library ..29* @run main/othervm TrustManagerTest30* @run main/othervm TrustManagerTest sm TrustManagerTest.policy31*/3233import java.io.File;34import java.io.FileInputStream;35import java.io.InputStream;36import java.security.KeyStore;37import java.security.Policy;38import java.security.Provider;39import java.security.Security;40import java.security.URIParameter;41import java.security.cert.CertificateFactory;42import java.security.cert.X509Certificate;43import javax.net.ssl.TrustManagerFactory;44import javax.net.ssl.X509TrustManager;4546// This test belongs more in JSSE than here, but the JSSE workspace does not47// have the NSS test infrastructure. It will live here for the time being.4849public class TrustManagerTest extends SecmodTest {5051public static void main(String[] args) throws Exception {52if (initSecmod() == false) {53return;54}5556if ("sparc".equals(System.getProperty("os.arch")) == false) {57// we have not updated other platforms with the proper NSS libraries yet58System.out.println("Test currently works only on solaris-sparc, skipping");59return;60}6162String configName = BASE + SEP + "fips.cfg";63Provider p = getSunPKCS11(configName);6465System.out.println(p);66Security.addProvider(p);6768Security.removeProvider("SunJSSE");69Provider jsse = new com.sun.net.ssl.internal.ssl.Provider(p);70Security.addProvider(jsse);71System.out.println(jsse.getInfo());7273KeyStore ks = KeyStore.getInstance("PKCS11", p);74ks.load(null, "test12".toCharArray());7576X509Certificate server = loadCertificate("certs/server.cer");77X509Certificate ca = loadCertificate("certs/ca.cer");78X509Certificate anchor = loadCertificate("certs/anchor.cer");7980if (args.length > 1 && "sm".equals(args[0])) {81Policy.setPolicy(Policy.getInstance("JavaPolicy",82new URIParameter(new File(BASE, args[1]).toURI())));83System.setSecurityManager(new SecurityManager());84}8586KeyStore trustStore = KeyStore.getInstance("JKS");87trustStore.load(null, null);88trustStore.setCertificateEntry("anchor", anchor);8990TrustManagerFactory tmf = TrustManagerFactory.getInstance("PKIX");91tmf.init(trustStore);9293X509TrustManager tm = (X509TrustManager)tmf.getTrustManagers()[0];9495X509Certificate[] chain = {server, ca, anchor};9697tm.checkServerTrusted(chain, "RSA");9899System.out.println("OK");100}101102private static X509Certificate loadCertificate(String name) throws Exception {103try (InputStream in = new FileInputStream(BASE + SEP + name)) {104return (X509Certificate) CertificateFactory.getInstance("X.509")105.generateCertificate(in);106}107}108109}110111112