Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/pkcs11/Secmod/TrustAnchors.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 6298106 6275523 642025226* @summary make sure we can access the NSS trust anchor module27* @author Andreas Sterbenz28* @library ..29* @run main/othervm TrustAnchors30* @run main/othervm TrustAnchors sm policy31*/3233import java.io.File;34import java.security.KeyStore;35import java.security.Provider;36import java.security.Security;37import java.security.cert.X509Certificate;38import java.util.Collection;39import java.util.Collections;40import java.util.TreeSet;4142public class TrustAnchors extends SecmodTest {4344public static void main(String[] args) throws Exception {45if (initSecmod() == false) {46return;47}4849if (System.getProperty("os.name").toLowerCase().startsWith("win")) {50// our secmod.db file says nssckbi.*so*, so NSS does not find the51// *DLL* on windows.52System.out.println("Test currently does not work on Windows, skipping");53return;54}5556String configName = BASE + SEP + "nsstrust.cfg";57Provider p = getSunPKCS11(configName);5859System.out.println(p);60Security.addProvider(p);6162if (args.length > 1 && "sm".equals(args[0])) {63System.setProperty("java.security.policy",64BASE + File.separator + args[1]);65System.setSecurityManager(new SecurityManager());66}6768KeyStore ks = KeyStore.getInstance("PKCS11", p);69ks.load(null, null);70Collection<String> aliases = new TreeSet<>(Collections.list(ks.aliases()));71System.out.println("entries: " + aliases.size());72System.out.println(aliases);7374for (String alias : aliases) {75if (ks.isCertificateEntry(alias) == false) {76throw new Exception("not trusted: " + alias);77}78X509Certificate cert = (X509Certificate)ks.getCertificate(alias);79// verify self-signed certs80if (cert.getSubjectX500Principal().equals(cert.getIssuerX500Principal())) {81System.out.print(".");82cert.verify(cert.getPublicKey());83} else {84System.out.print("-");85}86}8788System.out.println();89System.out.println("OK");90}9192}939495