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