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 jdk.testlibrary.OutputAnalyzer; 28 import static java.lang.System.out; 29 30 /** 31 * @test 32 * @bug 8048830 33 * @summary Tests for creating pkcs12 keystore with various algorithms 34 * @library /lib/testlibrary ../ 35 * @run main KeytoolWriteP12Test 36 */ 37 public class KeytoolWriteP12Test { 38 private static final String ALIAS = "pkcs12testCA"; 39 private static final Utils.KeyStoreType PKCS12 = Utils.KeyStoreType.pkcs12; 40 private static final int FAILED_EXIT_CODE = 1; 41 private static final String CERT_FILE_NAME = "cert.data"; 42 private static final String DNAME = "CN=PKCS12 Test CA, OU=Security SQE, " 43 + "O=JavaSoft, C=US"; 44 private static final String WORKING_DIRECTORY = System. 45 getProperty("test.classes", "." + File.separator); 46 private enum Algorithm { 47 DSA, RSA, ECC 48 }; 49 private void run() { 50 out.println("Running DSA Test"); 51 keytoolListTest("kt_DSA.p12", Algorithm.DSA); 52 out.println("DSA Test passed"); 53 54 out.println("Running RSA Test"); 55 final String rsaKeyStoreName = "kt_RSA_MD5.p12"; 56 keytoolListTest(rsaKeyStoreName, Algorithm.RSA); 57 out.println("RSA Test passed"); 58 59 out.println("Running RSA and Signing Algorithm SHA1withRSA Test"); 60 keytoolListTest("kt_RSA_SHA1.p12", Algorithm.RSA, 61 "-sigalg", "SHA1withRSA"); 62 out.println("RSA and Signing Algorithm SHA1withRSA Test Passed"); 63 64 out.println("Running Keysize 256 Test"); 65 keytoolListNegativeTest("kt_DSA_256.p12", Algorithm.DSA, "-keysize", 66 "256"); 67 out.println("Keysize 256 Test Passed"); 68 69 out.println("Running Keysize 1023 Test"); 70 keytoolListTest("kt_RSA_MD5_1023.p12", Algorithm.RSA, "-keysize", 71 "1023"); 72 out.println("Keysize 1023 Test Passed"); 73 out.println("Running Export certificate Test"); 74 exportTest(rsaKeyStoreName); 75 out.println("Export certificate Test Passed"); 76 } 77 78 private void exportTest(String keyStore) { 79 final String keyStoreName = WORKING_DIRECTORY + File.separator 80 + keyStore; 81 deleteKeyStoreFile(keyStoreName); 82 Utils.createKeyStore(DNAME, PKCS12, keyStore, ALIAS, 83 Algorithm.RSA.name()); 84 final String certFilePath = WORKING_DIRECTORY + File.separator 85 + CERT_FILE_NAME; 86 Utils.exportCert(PKCS12, keyStore, 87 ALIAS, certFilePath); 88 final String[] command = new String[]{"-debug", "-printcert", "-v", 89 "-file", certFilePath}; 90 Utils.executeKeytoolCommand(command); 91 } 92 93 private void keytoolListTest(String keyStore, Algorithm algorithm, 94 String ...optionalArgs) { 95 final String keyStoreName = WORKING_DIRECTORY + File.separator 96 + keyStore; 97 final String[] command = new String[]{"-debug", "-list", "-v", "-alias", 98 ALIAS, "-keystore", keyStoreName, "-storetype", "pkcs12", 99 "-storepass", Utils.DEFAULT_PASSWD}; 100 deleteKeyStoreFile(keyStoreName); 101 Utils.createKeyStore(DNAME, PKCS12, keyStoreName, ALIAS, 102 algorithm.name(), optionalArgs); 103 OutputAnalyzer output = Utils.executeKeytoolCommand(command); 104 output.shouldContain(DNAME); 105 } 106 107 private void keytoolListNegativeTest(String keyStore, Algorithm algorithm, 108 String... optionalArgs) { 109 final String keyStoreName = WORKING_DIRECTORY + File.separator 110 + keyStore; 111 deleteKeyStoreFile(keyStoreName); 112 Utils.createKeyStore(DNAME, PKCS12, keyStoreName, ALIAS, 113 algorithm.name(), optionalArgs, FAILED_EXIT_CODE); 114 } 115 116 public static void main(String[] args) { 117 KeytoolWriteP12Test test = new KeytoolWriteP12Test(); 118 test.run(); 119 out.println("Test Passed"); 120 } 121 122 private void deleteKeyStoreFile(String fileName) { 123 File file = new File(fileName); 124 if (file.exists()) { 125 file.delete(); 126 } 127 } 128 }