Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/share/classes/sun/security/util/AnchorCertificates.java
41159 views
1
/*
2
* Copyright (c) 2016, 2021, 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.util;
27
28
import java.io.File;
29
import java.io.FileInputStream;
30
import java.security.AccessController;
31
import java.security.KeyStore;
32
import java.security.PrivilegedAction;
33
import java.security.cert.X509Certificate;
34
import java.util.Collections;
35
import java.util.Enumeration;
36
import java.util.HashSet;
37
import java.util.Set;
38
39
import javax.security.auth.x500.X500Principal;
40
import sun.security.x509.X509CertImpl;
41
42
/**
43
* The purpose of this class is to determine the trust anchor certificates is in
44
* the cacerts file. This is used for PKIX CertPath checking.
45
*/
46
public class AnchorCertificates {
47
48
private static final Debug debug = Debug.getInstance("certpath");
49
private static final String HASH = "SHA-256";
50
private static Set<String> certs = Collections.emptySet();
51
private static Set<X500Principal> certIssuers = Collections.emptySet();
52
53
static {
54
@SuppressWarnings("removal")
55
var dummy = AccessController.doPrivileged(new PrivilegedAction<>() {
56
@Override
57
public Void run() {
58
File f = new File(FilePaths.cacerts());
59
try {
60
KeyStore cacerts;
61
cacerts = KeyStore.getInstance("JKS");
62
try (FileInputStream fis = new FileInputStream(f)) {
63
cacerts.load(fis, null);
64
certs = new HashSet<>();
65
certIssuers = new HashSet<>();
66
Enumeration<String> list = cacerts.aliases();
67
while (list.hasMoreElements()) {
68
String alias = list.nextElement();
69
// Check if this cert is labeled a trust anchor.
70
if (alias.contains(" [jdk")) {
71
X509Certificate cert = (X509Certificate) cacerts
72
.getCertificate(alias);
73
certs.add(X509CertImpl.getFingerprint(HASH, cert));
74
certIssuers.add(cert.getSubjectX500Principal());
75
}
76
}
77
}
78
} catch (Exception e) {
79
if (debug != null) {
80
debug.println("Error parsing cacerts");
81
e.printStackTrace();
82
}
83
}
84
return null;
85
}
86
});
87
}
88
89
/**
90
* Checks if a certificate is a JDK trust anchor.
91
*
92
* @param cert the certificate to check
93
* @return true if the certificate is a JDK trust anchor
94
*/
95
public static boolean contains(X509Certificate cert) {
96
String key = X509CertImpl.getFingerprint(HASH, cert);
97
boolean result = certs.contains(key);
98
if (result && debug != null) {
99
debug.println("AnchorCertificate.contains: matched " +
100
cert.getSubjectX500Principal());
101
}
102
return result;
103
}
104
105
/**
106
* Checks if a JDK trust anchor is the issuer of a certificate.
107
*
108
* @param cert the certificate to check
109
* @return true if the certificate is issued by a trust anchor
110
*/
111
public static boolean issuerOf(X509Certificate cert) {
112
return certIssuers.contains(cert.getIssuerX500Principal());
113
}
114
115
private AnchorCertificates() {}
116
}
117
118