Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/pkcs11/Secmod/AddTrustedCert.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 629810626* @summary make sure we can add a trusted cert to the NSS KeyStore module27* @author Andreas Sterbenz28* @library ..29* @run main/othervm AddTrustedCert30* @run main/othervm AddTrustedCert sm policy31*/3233import java.io.File;34import java.io.FileInputStream;35import java.io.InputStream;36import java.security.KeyStore;37import java.security.KeyStore.TrustedCertificateEntry;38import java.security.Provider;39import java.security.Security;40import java.security.cert.CertificateFactory;41import java.security.cert.X509Certificate;42import java.util.Collection;43import java.util.Collections;44import java.util.TreeSet;4546public class AddTrustedCert extends SecmodTest {4748public static void main(String[] args) throws Exception {49if (initSecmod() == false) {50return;51}5253X509Certificate cert;54try (InputStream in = new FileInputStream(BASE + SEP + "anchor.cer")) {55CertificateFactory factory =56CertificateFactory.getInstance("X.509");57cert = (X509Certificate)factory.generateCertificate(in);58}5960String configName = BASE + SEP + "nss.cfg";61Provider p = getSunPKCS11(configName);6263System.out.println(p);64Security.addProvider(p);6566if (args.length > 1 && "sm".equals(args[0])) {67System.setProperty("java.security.policy",68BASE + File.separator + args[1]);69System.setSecurityManager(new SecurityManager());70}7172KeyStore ks = KeyStore.getInstance(PKCS11, p);73ks.load(null, password);74Collection<String> aliases = new TreeSet<>(Collections.list(75ks.aliases()));76System.out.println("entries: " + aliases.size());77System.out.println(aliases);78int size1 = aliases.size();7980String alias = "anchor";81if (ks.containsAlias(alias)) {82throw new Exception("Alias exists: " + alias);83}8485ks.setCertificateEntry(alias, cert);86KeyStore.Entry first = ks.getEntry(alias, null);87System.out.println("first entry = " + first);88if (!ks.entryInstanceOf(alias, TrustedCertificateEntry.class)) {89throw new Exception("Unexpected first entry type: " + first);90}9192ks.setCertificateEntry(alias, cert);93KeyStore.Entry second = ks.getEntry(alias, null);94System.out.println("second entry = " + second);95if (!ks.entryInstanceOf(alias, TrustedCertificateEntry.class)) {96throw new Exception("Unexpected second entry type: "97+ second);98}99100aliases = new TreeSet<>(Collections.list(ks.aliases()));101System.out.println("entries: " + aliases.size());102System.out.println(aliases);103int size2 = aliases.size();104105if ((size2 != size1 + 1) || (aliases.contains(alias) == false)) {106throw new Exception("Trusted cert not added");107}108X509Certificate cert2 = (X509Certificate)ks.getCertificate(alias);109if (cert.equals(cert2) == false) {110throw new Exception("KeyStore returned incorrect certificate");111}112113ks.deleteEntry(alias);114if (ks.containsAlias(alias)) {115throw new Exception("Alias still exists: " + alias);116}117118System.out.println("OK");119}120121}122123124