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