Path: blob/master/test/jdk/sun/security/pkcs11/Secmod/AddTrustedCert.java
41153 views
/*1* Copyright (c) 2005, 2019, 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 629810626* @summary make sure we can add a trusted cert to the NSS KeyStore module27* @author Andreas Sterbenz28* @library /test/lib ..29* @modules jdk.crypto.cryptoki30* @run main/othervm AddTrustedCert31* @run main/othervm -Djava.security.manager=allow AddTrustedCert sm policy32*/3334import java.io.File;35import java.io.FileInputStream;36import java.io.InputStream;37import java.security.KeyStore;38import java.security.KeyStore.TrustedCertificateEntry;39import java.security.Provider;40import java.security.Security;41import java.security.cert.CertificateFactory;42import java.security.cert.X509Certificate;43import java.util.Collection;44import java.util.Collections;45import java.util.TreeSet;4647public class AddTrustedCert extends SecmodTest {4849public static void main(String[] args) throws Exception {50if (args.length > 1 && "sm".equals(args[0])) {51System.setProperty("java.security.policy",52BASE + File.separator + args[1]);53}5455if (initSecmod() == false) {56return;57}5859X509Certificate cert;60try (InputStream in = new FileInputStream(BASE + SEP + "anchor.cer")) {61CertificateFactory factory =62CertificateFactory.getInstance("X.509");63cert = (X509Certificate)factory.generateCertificate(in);64}6566String configName = BASE + SEP + "nss.cfg";67Provider p = getSunPKCS11(configName);6869if (improperNSSVersion(p)) {70System.out.println(71"Skip test due to improper NSS version in [3.28, 3.35). "72+ "See JDK-8180837 for more detatils.");73return;74}7576System.out.println(p);77Security.addProvider(p);7879if (args.length > 1 && "sm".equals(args[0])) {80System.setProperty("java.security.policy",81BASE + File.separator + args[1]);82System.setSecurityManager(new SecurityManager());83}8485KeyStore ks = KeyStore.getInstance(PKCS11, p);86ks.load(null, password);87Collection<String> aliases = new TreeSet<>(Collections.list(88ks.aliases()));89System.out.println("entries: " + aliases.size());90System.out.println(aliases);91int size1 = aliases.size();9293String alias = "anchor";94if (ks.containsAlias(alias)) {95throw new Exception("Alias exists: " + alias);96}9798ks.setCertificateEntry(alias, cert);99KeyStore.Entry first = ks.getEntry(alias, null);100System.out.println("first entry = " + first);101if (!ks.entryInstanceOf(alias, TrustedCertificateEntry.class)) {102throw new Exception("Unexpected first entry type: " + first);103}104105ks.setCertificateEntry(alias, cert);106KeyStore.Entry second = ks.getEntry(alias, null);107System.out.println("second entry = " + second);108if (!ks.entryInstanceOf(alias, TrustedCertificateEntry.class)) {109throw new Exception("Unexpected second entry type: "110+ second);111}112113aliases = new TreeSet<>(Collections.list(ks.aliases()));114System.out.println("entries: " + aliases.size());115System.out.println(aliases);116int size2 = aliases.size();117118if ((size2 != size1 + 1) || (aliases.contains(alias) == false)) {119throw new Exception("Trusted cert not added");120}121X509Certificate cert2 = (X509Certificate)ks.getCertificate(alias);122if (cert.equals(cert2) == false) {123throw new Exception("KeyStore returned incorrect certificate");124}125126ks.deleteEntry(alias);127if (ks.containsAlias(alias)) {128throw new Exception("Alias still exists: " + alias);129}130131System.out.println("OK");132}133134private static boolean improperNSSVersion(Provider p) {135double nssVersion = getNSSVersion();136if (p.getName().equalsIgnoreCase("SunPKCS11-NSSKeyStore")137&& nssVersion >= 3.28 && nssVersion < 3.35) {138return true;139}140141return false;142}143}144145146