1 /* 2 * Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 import java.io.File; 27 import java.io.FileInputStream; 28 import java.io.FileNotFoundException; 29 import java.io.IOException; 30 import java.security.KeyStore; 31 import java.security.KeyStoreException; 32 import java.security.NoSuchAlgorithmException; 33 import java.security.cert.Certificate; 34 import java.security.cert.CertificateException; 35 import java.security.cert.CertificateFactory; 36 import static java.lang.System.err; 37 import static java.lang.System.out; 38 39 /** 40 * @test 41 * @bug 8048830 42 * @summary Test imports certificate from file to PKCS12 keystore store it as 43 * trusted certificate Check import errors (must be not errors) & check keystore 44 * content after import 45 * @library /lib/testlibrary ../ 46 * @library /test/lib 47 * @run main StoreTrustedCertAPITest 48 */ 49 public class StoreTrustedCertAPITest { 50 private static final char[] PASSWORD = "passwd".toCharArray(); 51 private static final String ALIAS = "testkey_stcapi"; 52 private static final String WORKING_DIRECTORY = System.getProperty( 53 "test.classes", "." + File.separator); 54 private static final String CERT_PATH = WORKING_DIRECTORY + File.separator 55 + "cert.data"; 56 private static final String KEYSTORE_PATH = WORKING_DIRECTORY 57 + File.separator + "ks.pkcs12"; 58 59 /** 60 * Test logic (environment has set up) 61 */ 62 private void runTest() throws FileNotFoundException, CertificateException, 63 KeyStoreException, IOException, NoSuchAlgorithmException { 64 Certificate cert; 65 CertificateFactory cf; 66 try (FileInputStream fi = new FileInputStream(CERT_PATH)) { 67 cf = CertificateFactory.getInstance("X.509"); 68 cert = cf.generateCertificate(fi); 69 KeyStore ks = KeyStore.getInstance( 70 Utils.KeyStoreType.pkcs12.name()); 71 ks.load(null, null); 72 ks.setCertificateEntry(ALIAS, cert); 73 Utils.saveKeyStore(ks, KEYSTORE_PATH, PASSWORD); 74 ks = Utils.loadKeyStore(KEYSTORE_PATH, Utils.KeyStoreType.pkcs12, 75 PASSWORD); 76 final Certificate ksCert = ks.getCertificate(ALIAS); 77 if (!ksCert.equals(cert)) { 78 err.println("Orig cert: " + cert.toString()); 79 err.println("Cert from keystore: " + ksCert.toString()); 80 throw new RuntimeException("Certificates don't match"); 81 } 82 } 83 } 84 85 public static void main(String[] args) throws Exception { 86 StoreTrustedCertAPITest test = new StoreTrustedCertAPITest(); 87 test.setUp(); 88 test.runTest(); 89 out.println("Test Passed"); 90 } 91 92 private void setUp() { 93 Utils.createKeyStore(Utils.KeyStoreType.pkcs12, KEYSTORE_PATH, ALIAS); 94 Utils.exportCert(Utils.KeyStoreType.pkcs12, KEYSTORE_PATH, 95 ALIAS, CERT_PATH); 96 new File(KEYSTORE_PATH).delete(); 97 } 98 }