Path: blob/master/test/jdk/sun/security/pkcs11/Secmod/TrustAnchors.java
41153 views
/*1* Copyright (c) 2005, 2018, 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 6298106 6275523 6420252 805962726* @summary make sure we can access the NSS trust anchor module27* @author Andreas Sterbenz28* @library /test/lib ..29* @modules jdk.crypto.cryptoki30* @run main/othervm TrustAnchors31* @run main/othervm -Djava.security.manager=allow TrustAnchors sm policy32*/3334import java.io.File;35import java.security.KeyStore;36import java.security.Provider;37import java.security.Security;38import java.security.cert.X509Certificate;39import java.util.Collection;40import java.util.Collections;41import java.util.TreeSet;4243public class TrustAnchors extends SecmodTest {4445public static void main(String[] args) throws Exception {46if (initSecmod() == false) {47return;48}4950// our secmod.db file says nssckbi.*so*, so NSS does not find the51// *DLL* on Windows nor the *DYLIB* on Mac OSX.52String osName = System.getProperty("os.name").toLowerCase();53if (osName.startsWith("win") || osName.startsWith("mac")) {54System.out.println("Test currently does not work on " + osName +55", skipping");56return;57}5859String configName = BASE + SEP + "nsstrust.cfg";60Provider p = getSunPKCS11(configName);6162System.out.println(p);63Security.addProvider(p);6465if (args.length > 1 && "sm".equals(args[0])) {66System.setProperty("java.security.policy",67BASE + File.separator + args[1]);68System.setSecurityManager(new SecurityManager());69}7071KeyStore ks = KeyStore.getInstance("PKCS11", p);72ks.load(null, null);73Collection<String> aliases = new TreeSet<>(Collections.list(ks.aliases()));74System.out.println("entries: " + aliases.size());75System.out.println(aliases);7677for (String alias : aliases) {78if (ks.isCertificateEntry(alias) == false) {79throw new Exception("not trusted: " + alias);80}81X509Certificate cert = (X509Certificate)ks.getCertificate(alias);82// verify self-signed certs83if (cert.getSubjectX500Principal().equals(cert.getIssuerX500Principal())) {84System.out.print(".");85cert.verify(cert.getPublicKey());86} else {87System.out.print("-");88}89}9091System.out.println();92System.out.println("OK");93}9495}969798