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/ReadP12Test.java
38828 views
1
/*
2
* Copyright (c) 2003,2014, 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.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
import static java.lang.System.out;
25
import java.io.ByteArrayInputStream;
26
import java.io.File;
27
import java.io.FileInputStream;
28
import java.nio.file.Files;
29
import java.nio.file.Paths;
30
import java.security.Key;
31
import java.security.KeyStore;
32
import java.security.cert.Certificate;
33
import java.security.cert.X509Certificate;
34
import java.util.Base64;
35
import java.util.Enumeration;
36
37
/*
38
* @test
39
* @bug 8048617
40
* @author Bill Situ
41
* @summary Read different types p12 key store to Check the read related APIs.
42
* including following test cases:
43
* ReadP12_IE_Chain: Read p12 key store (contains private key and associated
44
* certificate chain) from IE.
45
* ReadP12_IE_Self: Read p12 key store (contains only private key and
46
* self-signed certificate) from IE.
47
* ReadP12_JDK_Chain: Read p12 key store (contains private key and associated
48
* certificate chain) from JDK
49
* ReadP12_JDK_Self: Read p12 key store (contains only private key and
50
* self-signed certificate) from JDK.
51
* ReadP12_Mozilla_Self: Read p12 key store (contains only private key and
52
* self-signed certificate) from Mozilla.
53
* ReadP12_Mozilla_Chain: Read p12 key store (contains private key and
54
* associated certificate chain) from Mozilla.
55
* ReadP12_Mozilla_TwoEntries: Read p12 key store (contains 2 entries) from
56
* Mozilla.
57
* ReadP12_Netscape_Chain: Read p12 key store (contains private key and
58
* associated certificate chain) from Netscape.
59
* ReadP12_Netscape_Self: Read p12 key store (contains only private key and
60
* self-signed certificate) from Netscape.
61
* ReadP12_Netscape_TwoEntries: Read p12 key store (contains 2 entries) from
62
* Netscape.
63
* ReadP12_OpenSSL: Read p12 key store from OpenSSL.
64
*/
65
66
public class ReadP12Test {
67
68
private final static String IN_KETYSTORE_TYPE = "pkcs12";
69
private final static String IN_KEYSTORE_PRV = "SunJSSE";
70
private final static String IN_STORE_PASS = "pass";
71
72
public static void main(String args[]) throws Exception {
73
74
ReadP12Test jstest = new ReadP12Test();
75
String testCase = "";
76
try {
77
testCase = "ReadP12_IE_Chain";
78
jstest.readTest("ie_chain.pfx.data");
79
80
testCase = "ReadP12_IE_Self";
81
jstest.readTest("ie_self.pfx.data");
82
83
testCase = "ReadP12_JDK_Chain";
84
jstest.readTest("jdk_chain.p12.data");
85
86
testCase = "ReadP12_JDK_Self";
87
jstest.readTest("jdk_self.p12.data");
88
89
testCase = "ReadP12_Mozilla_Chain";
90
jstest.readTest("mozilla_chain.p12.data");
91
92
testCase = "ReadP12_Mozilla_Self";
93
jstest.readTest("mozilla_self.p12.data");
94
95
testCase = "ReadP12_Mozilla_TwoEntries";
96
jstest.readTest("mozilla_twoentries.p12.data");
97
98
testCase = "ReadP12_Netscape_Chain";
99
jstest.readTest("netscape_chain.p12.data");
100
101
testCase = "ReadP12_Netscape_Self";
102
jstest.readTest("netscape_self.p12.data");
103
104
testCase = "ReadP12_Netscape_TwoEntries";
105
jstest.readTest("netscape_twoentries.p12.data");
106
107
testCase = "ReadP12_openssl";
108
jstest.readTest("openssl.p12.data");
109
110
} catch (Exception e) {
111
System.err.println(testCase + ": failed with execption: "
112
+ e.getMessage());
113
throw e;
114
115
}
116
out.println(testCase + ": Pass!!");
117
}
118
119
private void readTest(String inKeyStore) throws Exception {
120
121
KeyStore inputKeyStore;
122
123
// Initialize KeyStore
124
String dir = System.getProperty("test.src", ".");
125
String keystorePath = dir + File.separator + "certs" + File.separator
126
+ "readP12";
127
inputKeyStore = KeyStore
128
.getInstance(IN_KETYSTORE_TYPE, IN_KEYSTORE_PRV);
129
// KeyStore have encoded by Base64.getMimeEncoder().encode(),need decode
130
// first.
131
byte[] input = Files.readAllBytes(Paths.get(keystorePath, inKeyStore));
132
ByteArrayInputStream arrayIn = new ByteArrayInputStream(Base64
133
.getMimeDecoder().decode(input));
134
inputKeyStore.load(arrayIn, IN_STORE_PASS.toCharArray());
135
out.println("Initialize KeyStore : " + inKeyStore + " success");
136
137
out.println("getProvider : " + inputKeyStore.getProvider());
138
out.println("getType : " + inputKeyStore.getType());
139
out.println("getDefaultType : " + KeyStore.getDefaultType());
140
141
int idx = 0;
142
Enumeration<String> e = inputKeyStore.aliases();
143
String alias;
144
while (e.hasMoreElements()) {
145
alias = e.nextElement();
146
out.println("Alias " + idx + " : " + alias);
147
if (inputKeyStore.containsAlias(alias) == false) {
148
throw new RuntimeException("Alias not found");
149
}
150
151
out.println("getCreationDate : "
152
+ inputKeyStore.getCreationDate(alias));
153
154
X509Certificate cert = (X509Certificate) inputKeyStore
155
.getCertificate(alias);
156
out.println("getCertificate : " + cert.getSubjectDN());
157
String retAlias = inputKeyStore.getCertificateAlias(cert);
158
if (!retAlias.equals(alias)) {
159
throw new RuntimeException("Alias mismatch");
160
}
161
out.println("getCertificateAlias : " + retAlias);
162
163
Certificate[] certs = inputKeyStore.getCertificateChain(alias);
164
for (int i = 0; i < certs.length; i++) {
165
out.println("getCertificateChain " + i + " : "
166
+ ((X509Certificate) certs[i]).getSubjectDN());
167
}
168
169
boolean isCertEntry = inputKeyStore.isCertificateEntry(alias);
170
// test KeyStore only contain key pair entries.
171
if (isCertEntry == true) {
172
throw new RuntimeException(
173
"inputKeystore should not be certEntry because test keystore only contain key pair entries.");
174
}
175
176
boolean isKeyEntry = inputKeyStore.isKeyEntry(alias);
177
if (isKeyEntry) {
178
Key key = inputKeyStore.getKey(alias,
179
IN_STORE_PASS.toCharArray());
180
out.println("Key : " + key.toString());
181
} else {
182
throw new RuntimeException("Entry type unknown\n");
183
}
184
idx++;
185
}
186
187
int size = inputKeyStore.size();
188
if (idx != size) {
189
throw new RuntimeException("Size not match");
190
}
191
192
}
193
}
194
195