Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/security/KeyStore/PKCS12/StoreTrustedCertAPITest.java
38828 views
/*1* Copyright (c) 2012, 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. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425import java.io.File;26import java.io.FileInputStream;27import java.io.FileNotFoundException;28import java.io.IOException;29import java.security.KeyStore;30import java.security.KeyStoreException;31import java.security.NoSuchAlgorithmException;32import java.security.cert.Certificate;33import java.security.cert.CertificateException;34import java.security.cert.CertificateFactory;35import static java.lang.System.err;36import static java.lang.System.out;3738/**39* @test40* @bug 804883041* @summary Test imports certificate from file to PKCS12 keystore store it as42* trusted certificate Check import errors (must be not errors) & check keystore43* content after import44* @library /lib/testlibrary ../45* @run main StoreTrustedCertAPITest46*/47public class StoreTrustedCertAPITest {48private static final char[] PASSWORD = "passwd".toCharArray();49private static final String ALIAS = "testkey_stcapi";50private static final String WORKING_DIRECTORY = System.getProperty(51"test.classes", "." + File.separator);52private static final String CERT_PATH = WORKING_DIRECTORY + File.separator53+ "cert.data";54private static final String KEYSTORE_PATH = WORKING_DIRECTORY55+ File.separator + "ks.pkcs12";5657/**58* Test logic (environment has set up)59*/60private void runTest() throws FileNotFoundException, CertificateException,61KeyStoreException, IOException, NoSuchAlgorithmException {62Certificate cert;63CertificateFactory cf;64try (FileInputStream fi = new FileInputStream(CERT_PATH)) {65cf = CertificateFactory.getInstance("X.509");66cert = cf.generateCertificate(fi);67KeyStore ks = KeyStore.getInstance(68Utils.KeyStoreType.pkcs12.name());69ks.load(null, null);70ks.setCertificateEntry(ALIAS, cert);71Utils.saveKeyStore(ks, KEYSTORE_PATH, PASSWORD);72ks = Utils.loadKeyStore(KEYSTORE_PATH, Utils.KeyStoreType.pkcs12,73PASSWORD);74final Certificate ksCert = ks.getCertificate(ALIAS);75if (!ksCert.equals(cert)) {76err.println("Orig cert: " + cert.toString());77err.println("Cert from keystore: " + ksCert.toString());78throw new RuntimeException("Certificates don't match");79}80}81}8283public static void main(String[] args) throws Exception {84StoreTrustedCertAPITest test = new StoreTrustedCertAPITest();85test.setUp();86test.runTest();87out.println("Test Passed");88}8990private void setUp() {91Utils.createKeyStore(Utils.KeyStoreType.pkcs12, KEYSTORE_PATH, ALIAS);92Utils.exportCert(Utils.KeyStoreType.pkcs12, KEYSTORE_PATH,93ALIAS, CERT_PATH);94new File(KEYSTORE_PATH).delete();95}96}979899