Path: blob/master/test/jdk/java/security/KeyStore/PKCS12/StoreTrustedCertAPITest.java
41153 views
/*1* Copyright (c) 2012, 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. 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 ../45* @library /test/lib46* @run main StoreTrustedCertAPITest47*/48public class StoreTrustedCertAPITest {49private static final char[] PASSWORD = "passwd".toCharArray();50private static final String ALIAS = "testkey_stcapi";51private static final String WORKING_DIRECTORY = System.getProperty(52"test.classes", "." + File.separator);53private static final String CERT_PATH = WORKING_DIRECTORY + File.separator54+ "cert.data";55private static final String KEYSTORE_PATH = WORKING_DIRECTORY56+ File.separator + "ks.pkcs12";5758/**59* Test logic (environment has set up)60*/61private void runTest() throws FileNotFoundException, CertificateException,62KeyStoreException, IOException, NoSuchAlgorithmException {63Certificate cert;64CertificateFactory cf;65try (FileInputStream fi = new FileInputStream(CERT_PATH)) {66cf = CertificateFactory.getInstance("X.509");67cert = cf.generateCertificate(fi);68KeyStore ks = KeyStore.getInstance(69Utils.KeyStoreType.pkcs12.name());70ks.load(null, null);71ks.setCertificateEntry(ALIAS, cert);72Utils.saveKeyStore(ks, KEYSTORE_PATH, PASSWORD);73ks = Utils.loadKeyStore(KEYSTORE_PATH, Utils.KeyStoreType.pkcs12,74PASSWORD);75final Certificate ksCert = ks.getCertificate(ALIAS);76if (!ksCert.equals(cert)) {77err.println("Orig cert: " + cert.toString());78err.println("Cert from keystore: " + ksCert.toString());79throw new RuntimeException("Certificates don't match");80}81}82}8384public static void main(String[] args) throws Exception {85StoreTrustedCertAPITest test = new StoreTrustedCertAPITest();86test.setUp();87test.runTest();88out.println("Test Passed");89}9091private void setUp() {92Utils.createKeyStore(Utils.KeyStoreType.pkcs12, KEYSTORE_PATH, ALIAS);93Utils.exportCert(Utils.KeyStoreType.pkcs12, KEYSTORE_PATH,94ALIAS, CERT_PATH);95new File(KEYSTORE_PATH).delete();96}97}9899100