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