Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/security/KeyStore/PKCS12/StoreTrustedCertKeytool.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.File;
27
import java.io.IOException;
28
import java.security.KeyStore;
29
import java.security.KeyStoreException;
30
import java.security.NoSuchAlgorithmException;
31
import java.security.cert.CertificateException;
32
import java.security.cert.X509Certificate;
33
import jdk.test.lib.process.OutputAnalyzer;
34
import static java.lang.System.out;
35
36
/**
37
* @test
38
* @bug 8048830
39
* @summary Tests keytool command imports certificate , list keystore, print
40
* certificate and import password help.
41
* @library ../
42
* @library /test/lib
43
* @run main StoreTrustedCertKeytool
44
*/
45
public class StoreTrustedCertKeytool {
46
private static final String PASSWORD = "passwd";
47
private static final String ALIAS = "testkey_stckey";
48
private static final String FILE_SEPARATOR = File.separator;
49
private static final String WORKING_DIRECTORY = System.getProperty(
50
"test.classes", "." + FILE_SEPARATOR);
51
private static final String CERT_PATH = WORKING_DIRECTORY
52
+ FILE_SEPARATOR
53
+ "cert.data";
54
private static final String KEYSTORE_PATH = WORKING_DIRECTORY
55
+ FILE_SEPARATOR + "ks.pkcs12";
56
57
protected void run() throws IOException, KeyStoreException,
58
NoSuchAlgorithmException, CertificateException {
59
setUp();
60
importCert();
61
out.println("Import Cert test passed");
62
listCerts();
63
out.println("listCerts test passed");
64
printCert();
65
out.println("print cert test passed");
66
helpImportPassword();
67
out.println("help import test passed");
68
}
69
70
private void importCert() {
71
final String[] command = new String[]{"-debug", "-importcert",
72
"-alias", ALIAS, "-file", CERT_PATH, "-noprompt", "-keystore",
73
KEYSTORE_PATH, "-storetype", "pkcs12", "-storepass", PASSWORD};
74
// If the keystore exists delete it.
75
File keystoreFile = new File(KEYSTORE_PATH);
76
if (keystoreFile.exists()) {
77
keystoreFile.delete();
78
}
79
Utils.executeKeytoolCommand(command);
80
}
81
82
private void listCerts() throws IOException, KeyStoreException,
83
NoSuchAlgorithmException, CertificateException {
84
final String[] command = new String[]{"-debug", "-list", "-v",
85
"-alias", ALIAS, "-keystore", KEYSTORE_PATH, "-storetype", "pkcs12",
86
"-storepass", PASSWORD};
87
OutputAnalyzer output = Utils.executeKeytoolCommand(command);
88
if (output == null) {
89
throw new RuntimeException("Keystore print fails");
90
}
91
X509Certificate ksCert = null;
92
final KeyStore ks = Utils.loadKeyStore(KEYSTORE_PATH,
93
Utils.KeyStoreType.pkcs12, PASSWORD.toCharArray());
94
ksCert = (X509Certificate) ks.getCertificate(ALIAS);
95
96
if (ksCert == null) {
97
throw new RuntimeException("Certificate " + ALIAS
98
+ " not found in Keystore " + KEYSTORE_PATH);
99
}
100
String serialNumber = ksCert.getSerialNumber().toString(16);
101
output.shouldContain(serialNumber);
102
}
103
104
private void printCert() {
105
final String[] command = new String[]{"-debug", "-printcert",
106
"-file", CERT_PATH};
107
Utils.executeKeytoolCommand(command);
108
109
}
110
111
private void helpImportPassword() {
112
final String[] command = new String[]{"-debug", "-help",
113
"-importpassword"};
114
Utils.executeKeytoolCommand(command);
115
}
116
117
public static void main(String[] args) throws Exception {
118
final StoreTrustedCertKeytool test = new StoreTrustedCertKeytool();
119
test.run();
120
out.println("Test Passed");
121
}
122
123
private void setUp() {
124
Utils.createKeyStore(Utils.KeyStoreType.pkcs12, KEYSTORE_PATH, ALIAS);
125
Utils.exportCert(Utils.KeyStoreType.pkcs12, KEYSTORE_PATH, ALIAS,
126
CERT_PATH);
127
new File(KEYSTORE_PATH).delete();
128
}
129
}
130
131