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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import java.io.File;
  25 import java.io.IOException;
  26 import java.nio.file.Files;
  27 import java.nio.file.StandardOpenOption;
  28 import java.util.Base64;
  29 import jdk.testlibrary.OutputAnalyzer;
  30 import static java.lang.System.out;
  31 import java.nio.file.Paths;
  32 import java.util.List;
  33 
  34 /**
  35  * @test
  36  * @bug 8048830
  37  * @summary Test for PKCS12 keystore list , export commands. Refer README for
  38  * keystore files information
  39  * @library /lib/testlibrary ../
  40  * @run main KeytoolReaderP12Test
  41  */
  42 public class KeytoolReaderP12Test {
  43     private static final String WORKING_DIRECTORY = System.getProperty(
  44             "test.classes", "."+ File.separator);
  45     //private static final String KS_PASSWD = "pass";
  46     private static final String KS_PASSWD = "storepass";
  47     private static final String CERT_CHAIN_PASSWD = "password";
  48     private static final String SOURCE_DIRECTORY =
  49             System.getProperty("test.src", "." + File.separator);
  50 
  51     public static void main(String[] args) throws Exception {
  52         List<String> expectedValues = null;
  53         out.println("Self signed test");
  54         expectedValues = Files.readAllLines(Paths.get(SOURCE_DIRECTORY,
  55                 "api_private_key.p12_expected.data"));
  56         readTest("api_private_key.p12.data", KS_PASSWD, expectedValues);
  57         out.println("Self signed test Passed");
  58 
  59         out.println("private key with selfsigned cert, key pair not match");
  60         expectedValues = Files.readAllLines(Paths.get(SOURCE_DIRECTORY,
  61                 "api_private_key_not_match.p12_expected.data"));
  62         readTest("api_private_key_not_match.p12.data", KS_PASSWD,
  63                 expectedValues);
  64         out.println("private key with selfsigned cert, key pair "
  65                 + "not match passed");
  66 
  67         out.println("cert chain test");
  68         expectedValues = Files.readAllLines(Paths.get(SOURCE_DIRECTORY,
  69                 "api_cert_chain.p12_expected.data"));
  70         readTest("api_cert_chain.p12.data", CERT_CHAIN_PASSWD, expectedValues);
  71         out.println("cert chain test passed");
  72 
  73         out.println("IE self test");
  74         expectedValues = Files.readAllLines(Paths.get(SOURCE_DIRECTORY,
  75                 "ie_self.pfx.pem"));
  76         exportTest("ie_self.pfx.data", "pkcs12testenduser1",
  77                 KS_PASSWD, expectedValues);
  78         out.println("IE self test passed");
  79 
  80         out.println("IE chain test");
  81         expectedValues = Files.readAllLines(Paths.get(SOURCE_DIRECTORY,
  82                 "ie_chain.pfx.pem"));
  83         exportTest("ie_chain.pfx.data", "servercert",
  84                 CERT_CHAIN_PASSWD, expectedValues);
  85         out.println("IE chain test passed");
  86 
  87         out.println("Netscape self");
  88         expectedValues = Files.readAllLines(Paths.get(SOURCE_DIRECTORY,
  89                 "netscape_self.p12.pem"));
  90         exportTest("netscape_self.p12.data", "pkcs12testenduser1",
  91                 KS_PASSWD, expectedValues);
  92         out.println("Netscape self passed");
  93 
  94         out.println("Mozilla self test");
  95         expectedValues = Files.readAllLines(Paths.get(SOURCE_DIRECTORY,
  96                 "mozilla_self.p12.pem"));
  97         exportTest("mozilla_self.p12.data", "pkcs12testenduser1",
  98                 KS_PASSWD, expectedValues);
  99         out.println("Mozilla self test passed");
 100 
 101         out.println("Openssl test");
 102         expectedValues = Files.readAllLines(Paths.get(SOURCE_DIRECTORY,
 103                 "openssl.p12.pem"));
 104         exportTest("openssl.p12.data", "servercert", CERT_CHAIN_PASSWD, expectedValues);
 105         out.println("openssl test passed");
 106 
 107         out.println("with different keystore and entrykey password");
 108         expectedValues = Files.readAllLines(Paths.get(SOURCE_DIRECTORY,
 109                 "api_two_pass.p12_expected.data"));
 110         readTest("api_two_pass.p12.data", KS_PASSWD,
 111                 expectedValues);
 112         out.println("two pass test passed");
 113     }
 114 
 115     private static void readTest(String name, String password,
 116             List<String> expectedValues)
 117             throws IOException {
 118         convertToPFX(name);
 119         final String[] command = new String[]{"-debug", "-list", "-v",
 120             "-keystore", WORKING_DIRECTORY + File.separator + name,
 121             "-storetype", "pkcs12", "-storepass", password};
 122         runAndValidate(command, expectedValues);
 123     }
 124 
 125     private static void exportTest(String name, String alias,
 126             String password, List<String> expectedValues)
 127             throws IOException {
 128         convertToPFX(name);
 129         final String[] command = new String[]{"-debug", "-export", "-alias",
 130             alias, "-keystore", WORKING_DIRECTORY + File.separator + name,
 131             "-storepass", password, "-storetype", "pkcs12", "-rfc"};
 132         runAndValidate(command, expectedValues);
 133     }
 134 
 135     private static void runAndValidate(String[] command,
 136             List<String> expectedValues) throws IOException {
 137         OutputAnalyzer output = Utils.executeKeytoolCommand(command);
 138         if (expectedValues != null) {
 139             expectedValues.stream().forEach(line -> {
 140                 output.shouldContain(line);
 141             });
 142         }
 143     }
 144 
 145     /**
 146      * Decodes the base64 encoded keystore and writes into new file
 147      * @param name base64 encoded keystore name
 148      */
 149     private static void convertToPFX(String name) throws IOException{
 150         File base64File = new File(SOURCE_DIRECTORY, name);
 151         File pkcs12File = new File(WORKING_DIRECTORY, name);
 152         byte[] input = Files.readAllBytes(base64File.toPath());
 153         Files.write(pkcs12File.toPath(), Base64.getMimeDecoder().
 154                 decode(input), StandardOpenOption.CREATE);
 155     }
 156 }