Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/security/KeyStore/PKCS12/KeytoolReaderP12Test.java
38828 views
/*1* Copyright (c) 2012, 2016, 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.testlibrary.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 /lib/testlibrary ../39* @run main KeytoolReaderP12Test40*/41public class KeytoolReaderP12Test {42private static final String WORKING_DIRECTORY = System.getProperty(43"test.classes", "."+ File.separator);44private static final String KS_PASSWD = "storepass";45private static final String CERT_CHAIN_PASSWD = "password";46private static final String SOURCE_DIRECTORY =47System.getProperty("test.src", "." + File.separator);4849public static void main(String[] args) throws Exception {50List<String> expectedValues = null;51out.println("Self signed test");52expectedValues = Files.readAllLines(Paths.get(SOURCE_DIRECTORY,53"api_private_key.p12_expected.data"));54readTest("api_private_key.p12.data", KS_PASSWD, expectedValues);55out.println("Self signed test Passed");5657out.println("private key with selfsigned cert, key pair not match");58expectedValues = Files.readAllLines(Paths.get(SOURCE_DIRECTORY,59"api_private_key_not_match.p12_expected.data"));60readTest("api_private_key_not_match.p12.data", KS_PASSWD,61expectedValues);62out.println("private key with selfsigned cert, key pair "63+ "not match passed");6465out.println("cert chain test");66expectedValues = Files.readAllLines(Paths.get(SOURCE_DIRECTORY,67"api_cert_chain.p12_expected.data"));68readTest("api_cert_chain.p12.data", CERT_CHAIN_PASSWD, expectedValues);69out.println("cert chain test passed");7071out.println("IE self test");72expectedValues = Files.readAllLines(Paths.get(SOURCE_DIRECTORY,73"ie_self.pfx.pem"));74exportTest("ie_self.pfx.data", "pkcs12testenduser1",75KS_PASSWD, expectedValues);76out.println("IE self test passed");7778out.println("IE chain test");79expectedValues = Files.readAllLines(Paths.get(SOURCE_DIRECTORY,80"ie_chain.pfx.pem"));81exportTest("ie_chain.pfx.data", "servercert",82CERT_CHAIN_PASSWD, expectedValues);83out.println("IE chain test passed");8485out.println("Netscape self");86expectedValues = Files.readAllLines(Paths.get(SOURCE_DIRECTORY,87"netscape_self.p12.pem"));88exportTest("netscape_self.p12.data", "pkcs12testenduser1",89KS_PASSWD, expectedValues);90out.println("Netscape self passed");9192out.println("Mozilla self test");93expectedValues = Files.readAllLines(Paths.get(SOURCE_DIRECTORY,94"mozilla_self.p12.pem"));95exportTest("mozilla_self.p12.data", "pkcs12testenduser1",96KS_PASSWD, expectedValues);97out.println("Mozilla self test passed");9899out.println("Openssl test");100expectedValues = Files.readAllLines(Paths.get(SOURCE_DIRECTORY,101"openssl.p12.pem"));102exportTest("openssl.p12.data", "servercert", CERT_CHAIN_PASSWD, expectedValues);103out.println("openssl test passed");104105out.println("with different keystore and entrykey password");106expectedValues = Files.readAllLines(Paths.get(SOURCE_DIRECTORY,107"api_two_pass.p12_expected.data"));108readTest("api_two_pass.p12.data", KS_PASSWD,109expectedValues);110out.println("two pass test passed");111}112113private static void readTest(String name, String password,114List<String> expectedValues)115throws IOException {116convertToPFX(name);117final String[] command = new String[]{"-debug", "-list", "-v",118"-keystore", WORKING_DIRECTORY + File.separator + name,119"-storetype", "pkcs12", "-storepass", password};120runAndValidate(command, expectedValues);121}122123private static void exportTest(String name, String alias,124String password, List<String> expectedValues)125throws IOException {126convertToPFX(name);127final String[] command = new String[]{"-debug", "-export", "-alias",128alias, "-keystore", WORKING_DIRECTORY + File.separator + name,129"-storepass", password, "-storetype", "pkcs12", "-rfc"};130runAndValidate(command, expectedValues);131}132133private static void runAndValidate(String[] command,134List<String> expectedValues) throws IOException {135OutputAnalyzer output = Utils.executeKeytoolCommand(command);136if (expectedValues != null) {137expectedValues.stream().forEach(line -> {138output.shouldContain(line);139});140}141}142143/**144* Decodes the base64 encoded keystore and writes into new file145* @param name base64 encoded keystore name146*/147private static void convertToPFX(String name) throws IOException{148File base64File = new File(SOURCE_DIRECTORY, name);149File pkcs12File = new File(WORKING_DIRECTORY, name);150byte[] input = Files.readAllBytes(base64File.toPath());151Files.write(pkcs12File.toPath(), Base64.getMimeDecoder().152decode(input), StandardOpenOption.CREATE);153}154}155156157