Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/make/src/classes/build/tools/generatecacerts/GenerateCacerts.java
32287 views
1
/*
2
* Copyright (c) 2020, 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
package build.tools.generatecacerts;
27
28
import java.io.DataOutputStream;
29
import java.io.FileOutputStream;
30
import java.io.IOException;
31
import java.io.InputStream;
32
import java.io.OutputStream;
33
import java.io.UnsupportedEncodingException;
34
import java.nio.file.DirectoryStream;
35
import java.nio.file.Files;
36
import java.nio.file.Path;
37
import java.nio.file.Paths;
38
import java.security.DigestOutputStream;
39
import java.security.MessageDigest;
40
import java.security.NoSuchAlgorithmException;
41
import java.security.cert.CertificateException;
42
import java.security.cert.CertificateFactory;
43
import java.security.cert.X509Certificate;
44
import java.util.Arrays;
45
import java.util.SortedSet;
46
import java.util.TreeSet;
47
48
/**
49
* Generate cacerts
50
* args[0]: Full path string to the directory that contains CA certs
51
* args[1]: Full path string to the generated cacerts
52
*/
53
public class GenerateCacerts {
54
public static void main(String[] args) throws Exception {
55
try (FileOutputStream fos = new FileOutputStream(args[1])) {
56
store(args[0], fos, "changeit".toCharArray());
57
}
58
}
59
60
// The following code is copied from JavaKeyStore.java.
61
62
private static final int MAGIC = 0xfeedfeed;
63
private static final int VERSION_2 = 0x02;
64
65
// This method is a simplified version of JavaKeyStore::engineStore.
66
// A new "dir" argument is added. All cert names in "dir" is collected into
67
// a sorted array. Each cert is stored with a creation date set to its
68
// notBefore value. Thus the output is determined as long as the certs
69
// are the same.
70
public static void store(String dir, OutputStream stream, char[] password)
71
throws IOException, NoSuchAlgorithmException, CertificateException
72
{
73
byte[] encoded; // the certificate encoding
74
CertificateFactory cf = CertificateFactory.getInstance("X509");
75
76
MessageDigest md = getPreKeyedHash(password);
77
DataOutputStream dos
78
= new DataOutputStream(new DigestOutputStream(stream, md));
79
80
dos.writeInt(MAGIC);
81
// always write the latest version
82
dos.writeInt(VERSION_2);
83
84
// All file names in dir sorted.
85
// README is excluded. Name starting with "." excluded.
86
SortedSet<String> entries = new TreeSet<String>();
87
try (DirectoryStream<Path> ds = Files.newDirectoryStream(Paths.get(dir))) {
88
for (Path p : ds) {
89
String fName = p.getFileName().toString();
90
if (!fName.equals("README") && !fName.startsWith(".")) {
91
entries.add(fName);
92
}
93
}
94
}
95
96
dos.writeInt(entries.size());
97
98
for (String entry : entries) {
99
100
String alias = entry + " [jdk]";
101
X509Certificate cert;
102
try (InputStream fis = Files.newInputStream(Paths.get(dir, entry))) {
103
cert = (X509Certificate) cf.generateCertificate(fis);
104
}
105
106
dos.writeInt(2);
107
108
// Write the alias
109
dos.writeUTF(alias);
110
111
// Write the (entry creation) date, which is notBefore of the cert
112
dos.writeLong(cert.getNotBefore().getTime());
113
114
// Write the trusted certificate
115
encoded = cert.getEncoded();
116
dos.writeUTF(cert.getType());
117
dos.writeInt(encoded.length);
118
dos.write(encoded);
119
}
120
121
/*
122
* Write the keyed hash which is used to detect tampering with
123
* the keystore (such as deleting or modifying key or
124
* certificate entries).
125
*/
126
byte[] digest = md.digest();
127
128
dos.write(digest);
129
dos.flush();
130
}
131
132
private static MessageDigest getPreKeyedHash(char[] password)
133
throws NoSuchAlgorithmException, UnsupportedEncodingException
134
{
135
136
MessageDigest md = MessageDigest.getInstance("SHA");
137
byte[] passwdBytes = convertToBytes(password);
138
md.update(passwdBytes);
139
Arrays.fill(passwdBytes, (byte) 0x00);
140
md.update("Mighty Aphrodite".getBytes("UTF8"));
141
return md;
142
}
143
144
private static byte[] convertToBytes(char[] password) {
145
int i, j;
146
byte[] passwdBytes = new byte[password.length * 2];
147
for (i=0, j=0; i<password.length; i++) {
148
passwdBytes[j++] = (byte)(password[i] >> 8);
149
passwdBytes[j++] = (byte)password[i];
150
}
151
return passwdBytes;
152
}
153
}
154
155