Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/pkcs11/ec/ReadPKCS12.java
38855 views
1
/*
2
* Copyright (c) 2006, 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.
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
/**
25
* @test
26
* @bug 6405536
27
* @summary Verify that we can parse ECPrivateKeys from PKCS#12 and use them
28
* @author Andreas Sterbenz
29
* @library ..
30
* @library ../../../../java/security/testlibrary
31
* @key randomness
32
* @run main/othervm ReadPKCS12
33
* @run main/othervm ReadPKCS12 sm policy
34
*/
35
36
import java.io.BufferedReader;
37
import java.io.File;
38
import java.io.FileInputStream;
39
import java.io.FileOutputStream;
40
import java.io.FileReader;
41
import java.io.InputStream;
42
import java.io.OutputStream;
43
import java.security.KeyStore;
44
import java.security.PrivateKey;
45
import java.security.Provider;
46
import java.security.PublicKey;
47
import java.security.Signature;
48
import java.security.cert.Certificate;
49
import java.security.cert.CertificateException;
50
import java.security.cert.CertificateFactory;
51
import java.security.cert.X509Certificate;
52
import java.util.Collections;
53
import java.util.HashMap;
54
import java.util.List;
55
import java.util.Map;
56
import java.util.Random;
57
58
public class ReadPKCS12 extends PKCS11Test {
59
60
private final static boolean COPY = false;
61
62
public static void main(String[] args) throws Exception {
63
main(new ReadPKCS12(), args);
64
}
65
66
@Override
67
public void main(Provider p) throws Exception {
68
if (p.getService("Signature", "SHA1withECDSA") == null) {
69
System.out.println("Provider does not support ECDSA, skipping...");
70
return;
71
}
72
73
/*
74
* PKCS11Test.main will remove this provider if needed
75
*/
76
Providers.setAt(p, 1);
77
78
CertificateFactory factory = CertificateFactory.getInstance("X.509");
79
try {
80
// undocumented way to clear the Sun internal certificate cache
81
factory.generateCertificate(null);
82
} catch (CertificateException e) {
83
// ignore
84
}
85
86
KeyStore ks2;
87
if (COPY) {
88
ks2 = KeyStore.getInstance("JKS");
89
try (InputStream in = new FileInputStream("keystore.old")) {
90
ks2.load(in, "passphrase".toCharArray());
91
}
92
}
93
94
File dir = new File(BASE, "pkcs12");
95
File closedDir = new File(CLOSED_BASE, "pkcs12");
96
97
Map<String,char[]> passwords = new HashMap<>();
98
try (BufferedReader reader = new BufferedReader(
99
new FileReader(new File(BASE, "p12passwords.txt")))) {
100
while (true) {
101
String line = reader.readLine();
102
if (line == null) {
103
break;
104
}
105
line = line.trim();
106
if ((line.length() == 0) || line.startsWith("#")) {
107
continue;
108
}
109
String[] s = line.split(" ");
110
passwords.put(s[0], s[1].toCharArray());
111
}
112
}
113
114
for (File file : concat(dir.listFiles(), closedDir.listFiles())) {
115
String name = file.getName();
116
if (file.isFile() == false) {
117
continue;
118
}
119
System.out.println();
120
System.out.println("Reading " + name + "...");
121
122
char[] password = passwords.get(name);
123
if (password == null) {
124
password = passwords.get("*");
125
}
126
127
KeyStore ks;
128
try (InputStream in = new FileInputStream(file)) {
129
ks = KeyStore.getInstance("PKCS12");
130
ks.load(in, password);
131
}
132
List<String> aliases = Collections.list(ks.aliases());
133
System.out.println("Aliases: " + aliases);
134
135
for (String alias : aliases) {
136
PrivateKey privateKey = (PrivateKey)ks.getKey(alias, password);
137
Certificate[] certs = ks.getCertificateChain(alias);
138
PublicKey publicKey = certs[0].getPublicKey();
139
System.out.println("Certificates: " + certs.length);
140
System.out.println(privateKey);
141
System.out.println(publicKey);
142
if (COPY) {
143
ks2.setKeyEntry(alias, privateKey, "passphrase".toCharArray(), certs);
144
}
145
146
verifyCerts(certs);
147
148
Random random = new Random();
149
byte[] data = new byte[1024];
150
random.nextBytes(data);
151
152
Signature s = Signature.getInstance("SHA1withECDSA");
153
s.initSign(privateKey);
154
s.update(data);
155
byte[] sig = s.sign();
156
157
s.initVerify(publicKey);
158
s.update(data);
159
if (s.verify(sig) == false) {
160
throw new Exception("Signature does not verify");
161
}
162
System.out.println("Verified public/private key match");
163
}
164
}
165
166
if (COPY) {
167
try (OutputStream out = new FileOutputStream("keystore.new")) {
168
ks2.store(out, "passphrase".toCharArray());
169
}
170
}
171
172
System.out.println("OK");
173
}
174
175
private static void verifyCerts(Certificate[] certs) throws Exception {
176
int n = certs.length;
177
for (int i = 0; i < n - 1; i++) {
178
X509Certificate cert = (X509Certificate)certs[i];
179
X509Certificate issuer = (X509Certificate)certs[i + 1];
180
if (cert.getIssuerX500Principal().equals(issuer.getSubjectX500Principal()) == false) {
181
throw new Exception("Certificates do not chain");
182
}
183
cert.verify(issuer.getPublicKey());
184
System.out.println("Verified: " + cert.getSubjectX500Principal());
185
}
186
X509Certificate last = (X509Certificate)certs[n - 1];
187
// if self-signed, verify the final cert
188
if (last.getIssuerX500Principal().equals(last.getSubjectX500Principal())) {
189
last.verify(last.getPublicKey());
190
System.out.println("Verified: " + last.getSubjectX500Principal());
191
}
192
}
193
194
}
195
196