Path: blob/master/test/jdk/java/security/KeyStore/PKCS12/KeytoolReaderP12Test.java
41153 views
/*1* Copyright (c) 2012, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.io.File;24import java.io.IOException;25import java.nio.file.Files;26import java.nio.file.StandardOpenOption;27import java.util.Base64;28import jdk.test.lib.process.OutputAnalyzer;29import static java.lang.System.out;30import java.nio.file.Paths;31import java.util.List;3233/**34* @test35* @bug 804883036* @summary Test for PKCS12 keystore list , export commands. Refer README for37* keystore files information38* @library ../39* @library /test/lib40* @run main KeytoolReaderP12Test41*/42public class KeytoolReaderP12Test {43private static final String WORKING_DIRECTORY = System.getProperty(44"test.classes", "."+ File.separator);45//private static final String KS_PASSWD = "pass";46private static final String KS_PASSWD = "storepass";47private static final String CERT_CHAIN_PASSWD = "password";48private static final String SOURCE_DIRECTORY =49System.getProperty("test.src", "." + File.separator);5051public static void main(String[] args) throws Exception {52List<String> expectedValues = null;53out.println("Self signed test");54expectedValues = Files.readAllLines(Paths.get(SOURCE_DIRECTORY,55"api_private_key.p12_expected.data"));56readTest("api_private_key.p12.data", KS_PASSWD, expectedValues);57out.println("Self signed test Passed");5859out.println("private key with selfsigned cert, key pair not match");60expectedValues = Files.readAllLines(Paths.get(SOURCE_DIRECTORY,61"api_private_key_not_match.p12_expected.data"));62readTest("api_private_key_not_match.p12.data", KS_PASSWD,63expectedValues);64out.println("private key with selfsigned cert, key pair "65+ "not match passed");6667out.println("cert chain test");68expectedValues = Files.readAllLines(Paths.get(SOURCE_DIRECTORY,69"api_cert_chain.p12_expected.data"));70readTest("api_cert_chain.p12.data", CERT_CHAIN_PASSWD, expectedValues);71out.println("cert chain test passed");7273out.println("IE self test");74expectedValues = Files.readAllLines(Paths.get(SOURCE_DIRECTORY,75"ie_self.pfx.pem"));76exportTest("ie_self.pfx.data", "pkcs12testenduser1",77KS_PASSWD, expectedValues);78out.println("IE self test passed");7980out.println("IE chain test");81expectedValues = Files.readAllLines(Paths.get(SOURCE_DIRECTORY,82"ie_chain.pfx.pem"));83exportTest("ie_chain.pfx.data", "servercert",84CERT_CHAIN_PASSWD, expectedValues);85out.println("IE chain test passed");8687out.println("Netscape self");88expectedValues = Files.readAllLines(Paths.get(SOURCE_DIRECTORY,89"netscape_self.p12.pem"));90exportTest("netscape_self.p12.data", "pkcs12testenduser1",91KS_PASSWD, expectedValues);92out.println("Netscape self passed");9394out.println("Mozilla self test");95expectedValues = Files.readAllLines(Paths.get(SOURCE_DIRECTORY,96"mozilla_self.p12.pem"));97exportTest("mozilla_self.p12.data", "pkcs12testenduser1",98KS_PASSWD, expectedValues);99out.println("Mozilla self test passed");100101out.println("Openssl test");102expectedValues = Files.readAllLines(Paths.get(SOURCE_DIRECTORY,103"openssl.p12.pem"));104exportTest("openssl.p12.data", "servercert", CERT_CHAIN_PASSWD, expectedValues);105out.println("openssl test passed");106107out.println("with different keystore and entrykey password");108expectedValues = Files.readAllLines(Paths.get(SOURCE_DIRECTORY,109"api_two_pass.p12_expected.data"));110readTest("api_two_pass.p12.data", KS_PASSWD,111expectedValues);112out.println("two pass test passed");113}114115private static void readTest(String name, String password,116List<String> expectedValues)117throws IOException {118convertToPFX(name);119final String[] command = new String[]{"-debug", "-list", "-v",120"-keystore", WORKING_DIRECTORY + File.separator + name,121"-storetype", "pkcs12", "-storepass", password};122runAndValidate(command, expectedValues);123}124125private static void exportTest(String name, String alias,126String password, List<String> expectedValues)127throws IOException {128convertToPFX(name);129final String[] command = new String[]{"-debug", "-export", "-alias",130alias, "-keystore", WORKING_DIRECTORY + File.separator + name,131"-storepass", password, "-storetype", "pkcs12", "-rfc"};132runAndValidate(command, expectedValues);133}134135private static void runAndValidate(String[] command,136List<String> expectedValues) throws IOException {137OutputAnalyzer output = Utils.executeKeytoolCommand(command);138if (expectedValues != null) {139expectedValues.stream().forEach(line -> {140output.shouldContain(line);141});142}143}144145/**146* Decodes the base64 encoded keystore and writes into new file147* @param name base64 encoded keystore name148*/149private static void convertToPFX(String name) throws IOException{150File base64File = new File(SOURCE_DIRECTORY, name);151File pkcs12File = new File(WORKING_DIRECTORY, name);152byte[] input = Files.readAllBytes(base64File.toPath());153Files.write(pkcs12File.toPath(), Base64.getMimeDecoder().154decode(input), StandardOpenOption.CREATE);155}156}157158159