Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/security/KeyStore/PKCS12/Utils.java
41153 views
1
/*
2
* Copyright (c) 2012, 2018, 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.FileInputStream;
27
import java.io.FileOutputStream;
28
import java.io.IOException;
29
import java.security.KeyStore;
30
import java.security.KeyStoreException;
31
import java.security.NoSuchAlgorithmException;
32
import java.security.cert.CertificateException;
33
import java.util.Arrays;
34
import java.util.List;
35
import jdk.test.lib.process.ProcessTools;
36
import jdk.test.lib.process.OutputAnalyzer;
37
import jdk.test.lib.JDKToolFinder;
38
import static java.lang.System.out;
39
import java.util.ArrayList;
40
41
/**
42
* Helper class for creating keystore and executing keytool commands
43
*/
44
public class Utils {
45
public enum KeyStoreType {
46
jks, pkcs12;
47
}
48
public static final String DEFAULT_DNAME
49
= "CN=TestKey, T=FuncTestCertKey, O=Oracle, OU=JDKSQE, C=US";
50
public static final String DEFAULT_PASSWD = "passwd";
51
public static final String RSA = "rsa";
52
public static final String JAVA_HOME = System.getProperty("java.home");
53
public static final String KEYTOOL = "keytool";
54
private static final int SUCCESS_EXIT_CODE = 0;
55
56
public static OutputAnalyzer executeKeytoolCommand(String[] command) {
57
return executeKeytoolCommand(command, SUCCESS_EXIT_CODE);
58
}
59
60
public static OutputAnalyzer executeKeytoolCommand(String[] command,
61
int exitCode) {
62
String[] keytoolCmd = new String[command.length + 1];
63
OutputAnalyzer output = null;
64
try {
65
keytoolCmd[0] = JDKToolFinder.getJDKTool(KEYTOOL);
66
System.arraycopy(command, 0, keytoolCmd, 1, command.length);
67
output = ProcessTools.executeCommand(keytoolCmd);
68
output.shouldHaveExitValue(exitCode);
69
out.println("Executed keytool command sucessfully:"
70
+ Arrays.toString(keytoolCmd));
71
} catch (Throwable e) {
72
e.printStackTrace(System.err);
73
throw new RuntimeException("Keytool Command execution failed : "
74
+ Arrays.toString(keytoolCmd), e);
75
}
76
return output;
77
}
78
79
public static void createKeyStore(KeyStoreType type, String name,
80
String alias) {
81
createKeyStore(DEFAULT_DNAME, type, name, alias, RSA);
82
}
83
84
public static void createKeyStore(String dName, KeyStoreType type,
85
String name, String alias, String algorithm,
86
String... optionalArgs) {
87
createKeyStore(dName, type, name, alias, algorithm, optionalArgs,
88
SUCCESS_EXIT_CODE);
89
}
90
91
public static void createKeyStore(String dName, KeyStoreType type,
92
String name, String alias, String algorithm,
93
String[] optionalArgs, int exitCode) {
94
String[] command = new String[]{"-debug", "-genkeypair", "-alias",
95
alias, "-keystore", name, "-dname", dName, "-storepass",
96
DEFAULT_PASSWD, "-keypass", DEFAULT_PASSWD, "-validity", "7300",
97
"-keyalg", algorithm, "-storetype", type.name()};
98
if (optionalArgs != null && optionalArgs.length > 0) {
99
List<String> commandArgs = new ArrayList<>(Arrays.asList(command));
100
List<String> temp = Arrays.asList(optionalArgs);
101
commandArgs.addAll(temp);
102
if (!commandArgs.contains(("-keysize"))) {
103
commandArgs.add("-keysize");
104
commandArgs.add("1024");
105
}
106
command = commandArgs.toArray(new String[commandArgs.size()]);
107
}
108
executeKeytoolCommand(command, exitCode);
109
}
110
111
public static void exportCert(KeyStoreType type, String name,
112
String alias, String cert) {
113
String[] command = {"-debug", "-exportcert", "-keystore", name,
114
"-storetype", type.name(), "-storepass", DEFAULT_PASSWD, "-alias",
115
alias,"-file",cert,"-noprompt"};
116
executeKeytoolCommand(command);
117
}
118
119
public static KeyStore loadKeyStore(String file, KeyStoreType type,
120
char[] passwd)
121
throws IOException, KeyStoreException,
122
NoSuchAlgorithmException, CertificateException {
123
KeyStore ks = KeyStore.getInstance(type.name());
124
try (FileInputStream fin = new FileInputStream(file)) {
125
ks.load(fin, passwd);
126
}
127
return ks;
128
}
129
130
public static void saveKeyStore(KeyStore ks, String file, char[] passwd)
131
throws IOException, KeyStoreException, NoSuchAlgorithmException,
132
CertificateException {
133
try (FileOutputStream fout = new FileOutputStream(file)) {
134
ks.store(fout, passwd);
135
}
136
}
137
}
138
139