Path: blob/master/test/jdk/java/security/KeyStore/PKCS12/Utils.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. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425import java.io.FileInputStream;26import java.io.FileOutputStream;27import java.io.IOException;28import java.security.KeyStore;29import java.security.KeyStoreException;30import java.security.NoSuchAlgorithmException;31import java.security.cert.CertificateException;32import java.util.Arrays;33import java.util.List;34import jdk.test.lib.process.ProcessTools;35import jdk.test.lib.process.OutputAnalyzer;36import jdk.test.lib.JDKToolFinder;37import static java.lang.System.out;38import java.util.ArrayList;3940/**41* Helper class for creating keystore and executing keytool commands42*/43public class Utils {44public enum KeyStoreType {45jks, pkcs12;46}47public static final String DEFAULT_DNAME48= "CN=TestKey, T=FuncTestCertKey, O=Oracle, OU=JDKSQE, C=US";49public static final String DEFAULT_PASSWD = "passwd";50public static final String RSA = "rsa";51public static final String JAVA_HOME = System.getProperty("java.home");52public static final String KEYTOOL = "keytool";53private static final int SUCCESS_EXIT_CODE = 0;5455public static OutputAnalyzer executeKeytoolCommand(String[] command) {56return executeKeytoolCommand(command, SUCCESS_EXIT_CODE);57}5859public static OutputAnalyzer executeKeytoolCommand(String[] command,60int exitCode) {61String[] keytoolCmd = new String[command.length + 1];62OutputAnalyzer output = null;63try {64keytoolCmd[0] = JDKToolFinder.getJDKTool(KEYTOOL);65System.arraycopy(command, 0, keytoolCmd, 1, command.length);66output = ProcessTools.executeCommand(keytoolCmd);67output.shouldHaveExitValue(exitCode);68out.println("Executed keytool command sucessfully:"69+ Arrays.toString(keytoolCmd));70} catch (Throwable e) {71e.printStackTrace(System.err);72throw new RuntimeException("Keytool Command execution failed : "73+ Arrays.toString(keytoolCmd), e);74}75return output;76}7778public static void createKeyStore(KeyStoreType type, String name,79String alias) {80createKeyStore(DEFAULT_DNAME, type, name, alias, RSA);81}8283public static void createKeyStore(String dName, KeyStoreType type,84String name, String alias, String algorithm,85String... optionalArgs) {86createKeyStore(dName, type, name, alias, algorithm, optionalArgs,87SUCCESS_EXIT_CODE);88}8990public static void createKeyStore(String dName, KeyStoreType type,91String name, String alias, String algorithm,92String[] optionalArgs, int exitCode) {93String[] command = new String[]{"-debug", "-genkeypair", "-alias",94alias, "-keystore", name, "-dname", dName, "-storepass",95DEFAULT_PASSWD, "-keypass", DEFAULT_PASSWD, "-validity", "7300",96"-keyalg", algorithm, "-storetype", type.name()};97if (optionalArgs != null && optionalArgs.length > 0) {98List<String> commandArgs = new ArrayList<>(Arrays.asList(command));99List<String> temp = Arrays.asList(optionalArgs);100commandArgs.addAll(temp);101if (!commandArgs.contains(("-keysize"))) {102commandArgs.add("-keysize");103commandArgs.add("1024");104}105command = commandArgs.toArray(new String[commandArgs.size()]);106}107executeKeytoolCommand(command, exitCode);108}109110public static void exportCert(KeyStoreType type, String name,111String alias, String cert) {112String[] command = {"-debug", "-exportcert", "-keystore", name,113"-storetype", type.name(), "-storepass", DEFAULT_PASSWD, "-alias",114alias,"-file",cert,"-noprompt"};115executeKeytoolCommand(command);116}117118public static KeyStore loadKeyStore(String file, KeyStoreType type,119char[] passwd)120throws IOException, KeyStoreException,121NoSuchAlgorithmException, CertificateException {122KeyStore ks = KeyStore.getInstance(type.name());123try (FileInputStream fin = new FileInputStream(file)) {124ks.load(fin, passwd);125}126return ks;127}128129public static void saveKeyStore(KeyStore ks, String file, char[] passwd)130throws IOException, KeyStoreException, NoSuchAlgorithmException,131CertificateException {132try (FileOutputStream fout = new FileOutputStream(file)) {133ks.store(fout, passwd);134}135}136}137138139