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/JarConstraintsParameters.java
41159 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 sun.security.util;
27
28
import java.security.CodeSigner;
29
import java.security.Key;
30
import java.security.Timestamp;
31
import java.security.cert.CertPath;
32
import java.security.cert.X509Certificate;
33
import java.util.Date;
34
import java.util.HashSet;
35
import java.util.List;
36
import java.util.Set;
37
import sun.security.util.AnchorCertificates;
38
import sun.security.util.ConstraintsParameters;
39
import sun.security.validator.Validator;
40
41
/**
42
* This class contains parameters for checking signed JARs against
43
* constraints specified in the jdk.jar.disabledAlgorithms security
44
* property.
45
*/
46
public class JarConstraintsParameters implements ConstraintsParameters {
47
48
// true if chain is anchored by a JDK root CA
49
private boolean anchorIsJdkCA;
50
private boolean anchorIsJdkCASet;
51
// The timestamp of the signed JAR file, if timestamped
52
private Date timestamp;
53
// The keys of the signers
54
private final Set<Key> keys;
55
// The certs in the signers' chains that are issued by the trust anchor
56
private final Set<X509Certificate> certsIssuedByAnchor;
57
// The extended exception message
58
private String message;
59
60
/**
61
* Create a JarConstraintsParameters.
62
*
63
* @param signers the CodeSigners that signed the JAR
64
*/
65
public JarConstraintsParameters(CodeSigner[] signers) {
66
this.keys = new HashSet<>();
67
this.certsIssuedByAnchor = new HashSet<>();
68
Date latestTimestamp = null;
69
boolean skipTimestamp = false;
70
71
// Iterate over the signers and extract the keys, the latest
72
// timestamp, and the last certificate of each chain which can be
73
// used for checking if the signer's certificate chains back to a
74
// JDK root CA
75
for (CodeSigner signer : signers) {
76
init(signer.getSignerCertPath());
77
Timestamp timestamp = signer.getTimestamp();
78
if (timestamp == null) {
79
// this means one of the signers doesn't have a timestamp
80
// and the JAR should be treated as if it isn't timestamped
81
latestTimestamp = null;
82
skipTimestamp = true;
83
} else {
84
// add the key and last cert of TSA too
85
init(timestamp.getSignerCertPath());
86
if (!skipTimestamp) {
87
Date timestampDate = timestamp.getTimestamp();
88
if (latestTimestamp == null) {
89
latestTimestamp = timestampDate;
90
} else {
91
if (latestTimestamp.before(timestampDate)) {
92
latestTimestamp = timestampDate;
93
}
94
}
95
}
96
}
97
}
98
this.timestamp = latestTimestamp;
99
}
100
101
// extract last certificate and key from chain
102
private void init(CertPath cp) {
103
@SuppressWarnings("unchecked")
104
List<X509Certificate> chain =
105
(List<X509Certificate>)cp.getCertificates();
106
if (!chain.isEmpty()) {
107
this.certsIssuedByAnchor.add(chain.get(chain.size() - 1));
108
this.keys.add(chain.get(0).getPublicKey());
109
}
110
}
111
112
@Override
113
public String getVariant() {
114
return Validator.VAR_GENERIC;
115
}
116
117
/**
118
* Since loading the cacerts keystore can be an expensive operation,
119
* this is only performed if this method is called during a "jdkCA"
120
* constraints check of a disabled algorithm, and the result is cached.
121
*
122
* @return true if at least one of the certificates are issued by a
123
* JDK root CA
124
*/
125
@Override
126
public boolean anchorIsJdkCA() {
127
if (anchorIsJdkCASet) {
128
return anchorIsJdkCA;
129
}
130
for (X509Certificate cert : certsIssuedByAnchor) {
131
if (AnchorCertificates.issuerOf(cert)) {
132
anchorIsJdkCA = true;
133
break;
134
}
135
}
136
anchorIsJdkCASet = true;
137
return anchorIsJdkCA;
138
}
139
140
@Override
141
public Date getDate() {
142
return timestamp;
143
}
144
145
@Override
146
public Set<Key> getKeys() {
147
return keys;
148
}
149
150
/**
151
* Sets the extended error message. Note: this should be used
152
* carefully as it is specific to the attribute/entry/file being checked.
153
*
154
* @param file the name of the signature related file being verified
155
* @param target the attribute containing the algorithm that is being
156
* checked
157
*/
158
public void setExtendedExceptionMsg(String file, String target) {
159
message = " used" + (target != null ? " with " + target : "") +
160
" in " + file + " file.";
161
}
162
163
@Override
164
public String extendedExceptionMsg() {
165
return message;
166
}
167
168
@Override
169
public String toString() {
170
StringBuilder sb = new StringBuilder("[\n");
171
sb.append("\n Variant: ").append(getVariant());
172
sb.append("\n Certs Issued by Anchor:");
173
for (X509Certificate cert : certsIssuedByAnchor) {
174
sb.append("\n Cert Issuer: ")
175
.append(cert.getIssuerX500Principal());
176
sb.append("\n Cert Subject: ")
177
.append(cert.getSubjectX500Principal());
178
}
179
for (Key key : keys) {
180
sb.append("\n Key: ").append(key.getAlgorithm());
181
}
182
if (timestamp != null) {
183
sb.append("\n Timestamp: ").append(timestamp);
184
}
185
sb.append("\n]");
186
return sb.toString();
187
}
188
}
189
190