Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/security/tools/KeyStoreUtil.java
38830 views
1
/*
2
* Copyright (c) 2005, 2017, 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 sun.security.tools;
27
28
import java.io.BufferedReader;
29
import java.io.File;
30
import java.io.FileInputStream;
31
import java.io.IOException;
32
import java.io.InputStreamReader;
33
34
import java.net.URL;
35
36
import java.security.KeyStore;
37
38
import java.security.cert.X509Certificate;
39
import java.text.Collator;
40
41
import java.util.Locale;
42
43
/**
44
* <p> This class provides several utilities to <code>KeyStore</code>.
45
*
46
* @since 1.6.0
47
*/
48
public class KeyStoreUtil {
49
50
private KeyStoreUtil() {
51
// this class is not meant to be instantiated
52
}
53
54
private static final String JKS = "jks";
55
56
private static final Collator collator = Collator.getInstance();
57
static {
58
// this is for case insensitive string comparisons
59
collator.setStrength(Collator.PRIMARY);
60
};
61
62
/**
63
* Returns true if the certificate is self-signed, false otherwise.
64
*/
65
public static boolean isSelfSigned(X509Certificate cert) {
66
return signedBy(cert, cert);
67
}
68
69
public static boolean signedBy(X509Certificate end, X509Certificate ca) {
70
if (!ca.getSubjectX500Principal().equals(end.getIssuerX500Principal())) {
71
return false;
72
}
73
try {
74
end.verify(ca.getPublicKey());
75
return true;
76
} catch (Exception e) {
77
return false;
78
}
79
}
80
81
/**
82
* Returns true if KeyStore has a password. This is true except for
83
* MSCAPI KeyStores
84
*/
85
public static boolean isWindowsKeyStore(String storetype) {
86
return storetype != null
87
&& (storetype.equalsIgnoreCase("Windows-MY")
88
|| storetype.equalsIgnoreCase("Windows-ROOT"));
89
}
90
91
/**
92
* Returns standard-looking names for storetype
93
*/
94
public static String niceStoreTypeName(String storetype) {
95
if (storetype.equalsIgnoreCase("Windows-MY")) {
96
return "Windows-MY";
97
} else if(storetype.equalsIgnoreCase("Windows-ROOT")) {
98
return "Windows-ROOT";
99
} else {
100
return storetype.toUpperCase(Locale.ENGLISH);
101
}
102
}
103
104
/**
105
* Returns the keystore with the configured CA certificates.
106
*/
107
public static KeyStore getCacertsKeyStore()
108
throws Exception
109
{
110
String sep = File.separator;
111
File file = new File(System.getProperty("java.home") + sep
112
+ "lib" + sep + "security" + sep
113
+ "cacerts");
114
if (!file.exists()) {
115
return null;
116
}
117
KeyStore caks = null;
118
try (FileInputStream fis = new FileInputStream(file)) {
119
caks = KeyStore.getInstance(JKS);
120
caks.load(fis, null);
121
}
122
return caks;
123
}
124
125
public static char[] getPassWithModifier(String modifier, String arg,
126
java.util.ResourceBundle rb) {
127
if (modifier == null) {
128
return arg.toCharArray();
129
} else if (collator.compare(modifier, "env") == 0) {
130
String value = System.getenv(arg);
131
if (value == null) {
132
System.err.println(rb.getString(
133
"Cannot.find.environment.variable.") + arg);
134
return null;
135
} else {
136
return value.toCharArray();
137
}
138
} else if (collator.compare(modifier, "file") == 0) {
139
try {
140
URL url = null;
141
try {
142
url = new URL(arg);
143
} catch (java.net.MalformedURLException mue) {
144
File f = new File(arg);
145
if (f.exists()) {
146
url = f.toURI().toURL();
147
} else {
148
System.err.println(rb.getString(
149
"Cannot.find.file.") + arg);
150
return null;
151
}
152
}
153
154
try (BufferedReader br =
155
new BufferedReader(new InputStreamReader(
156
url.openStream()))) {
157
String value = br.readLine();
158
159
if (value == null) {
160
return new char[0];
161
}
162
163
return value.toCharArray();
164
}
165
} catch (IOException ioe) {
166
System.err.println(ioe);
167
return null;
168
}
169
} else {
170
System.err.println(rb.getString("Unknown.password.type.") +
171
modifier);
172
return null;
173
}
174
}
175
}
176
177