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/ManifestEntryVerifier.java
41159 views
1
/*
2
* Copyright (c) 1997, 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.*;
29
import java.io.*;
30
import java.util.*;
31
import java.util.jar.*;
32
33
import sun.security.jca.Providers;
34
import sun.security.util.DisabledAlgorithmConstraints;
35
import sun.security.util.JarConstraintsParameters;
36
37
/**
38
* This class is used to verify each entry in a jar file with its
39
* manifest value.
40
*/
41
42
public class ManifestEntryVerifier {
43
44
private static final Debug debug = Debug.getInstance("jar");
45
46
/**
47
* Holder class to lazily load Sun provider. NOTE: if
48
* Providers.getSunProvider returned a cached provider, we could avoid the
49
* need for caching the provider with this holder class; we should try to
50
* revisit this in JDK 8.
51
*/
52
private static class SunProviderHolder {
53
private static final Provider instance = Providers.getSunProvider();
54
}
55
56
/** the created digest objects */
57
HashMap<String, MessageDigest> createdDigests;
58
59
/** the digests in use for a given entry*/
60
ArrayList<MessageDigest> digests;
61
62
/** the manifest hashes for the digests in use */
63
ArrayList<byte[]> manifestHashes;
64
65
private String name = null;
66
private Manifest man;
67
68
private boolean skip = true;
69
70
private JarEntry entry;
71
72
private CodeSigner[] signers = null;
73
74
/**
75
* Create a new ManifestEntryVerifier object.
76
*/
77
public ManifestEntryVerifier(Manifest man)
78
{
79
createdDigests = new HashMap<>(11);
80
digests = new ArrayList<>();
81
manifestHashes = new ArrayList<>();
82
this.man = man;
83
}
84
85
/**
86
* Find the hashes in the
87
* manifest for this entry, save them, and set the MessageDigest
88
* objects to calculate the hashes on the fly. If name is
89
* null it signifies that update/verify should ignore this entry.
90
*/
91
public void setEntry(String name, JarEntry entry)
92
throws IOException
93
{
94
digests.clear();
95
manifestHashes.clear();
96
this.name = name;
97
this.entry = entry;
98
99
skip = true;
100
signers = null;
101
102
if (man == null || name == null) {
103
return;
104
}
105
106
/* get the headers from the manifest for this entry */
107
/* if there aren't any, we can't verify any digests for this entry */
108
109
skip = false;
110
111
Attributes attr = man.getAttributes(name);
112
if (attr == null) {
113
// ugh. we should be able to remove this at some point.
114
// there are broken jars floating around with ./name and /name
115
// in the manifest, and "name" in the zip/jar file.
116
attr = man.getAttributes("./"+name);
117
if (attr == null) {
118
attr = man.getAttributes("/"+name);
119
if (attr == null)
120
return;
121
}
122
}
123
124
for (Map.Entry<Object,Object> se : attr.entrySet()) {
125
String key = se.getKey().toString();
126
127
if (key.toUpperCase(Locale.ENGLISH).endsWith("-DIGEST")) {
128
// 7 is length of "-Digest"
129
String algorithm = key.substring(0, key.length()-7);
130
131
MessageDigest digest = createdDigests.get(algorithm);
132
133
if (digest == null) {
134
try {
135
136
digest = MessageDigest.getInstance
137
(algorithm, SunProviderHolder.instance);
138
createdDigests.put(algorithm, digest);
139
} catch (NoSuchAlgorithmException nsae) {
140
// ignore
141
}
142
}
143
144
if (digest != null) {
145
digest.reset();
146
digests.add(digest);
147
manifestHashes.add(
148
Base64.getMimeDecoder().decode((String)se.getValue()));
149
}
150
}
151
}
152
}
153
154
/**
155
* update the digests for the digests we are interested in
156
*/
157
public void update(byte buffer) {
158
if (skip) return;
159
160
for (int i=0; i < digests.size(); i++) {
161
digests.get(i).update(buffer);
162
}
163
}
164
165
/**
166
* update the digests for the digests we are interested in
167
*/
168
public void update(byte[] buffer, int off, int len) {
169
if (skip) return;
170
171
for (int i=0; i < digests.size(); i++) {
172
digests.get(i).update(buffer, off, len);
173
}
174
}
175
176
/**
177
* get the JarEntry for this object
178
*/
179
public JarEntry getEntry()
180
{
181
return entry;
182
}
183
184
/**
185
* go through all the digests, calculating the final digest
186
* and comparing it to the one in the manifest. If this is
187
* the first time we have verified this object, remove its
188
* code signers from sigFileSigners and place in verifiedSigners.
189
*
190
*
191
*/
192
public CodeSigner[] verify(Hashtable<String, CodeSigner[]> verifiedSigners,
193
Hashtable<String, CodeSigner[]> sigFileSigners)
194
throws JarException
195
{
196
if (skip) {
197
return null;
198
}
199
200
if (digests.isEmpty()) {
201
throw new SecurityException("digest missing for " + name);
202
}
203
204
if (signers != null) {
205
return signers;
206
}
207
208
JarConstraintsParameters params =
209
getParams(verifiedSigners, sigFileSigners);
210
211
for (int i=0; i < digests.size(); i++) {
212
213
MessageDigest digest = digests.get(i);
214
if (params != null) {
215
try {
216
params.setExtendedExceptionMsg(JarFile.MANIFEST_NAME,
217
name + " entry");
218
DisabledAlgorithmConstraints.jarConstraints()
219
.permits(digest.getAlgorithm(), params);
220
} catch (GeneralSecurityException e) {
221
if (debug != null) {
222
debug.println("Digest algorithm is restricted: " + e);
223
}
224
return null;
225
}
226
}
227
byte [] manHash = manifestHashes.get(i);
228
byte [] theHash = digest.digest();
229
230
if (debug != null) {
231
debug.println("Manifest Entry: " +
232
name + " digest=" + digest.getAlgorithm());
233
debug.println(" manifest " + HexFormat.of().formatHex(manHash));
234
debug.println(" computed " + HexFormat.of().formatHex(theHash));
235
debug.println();
236
}
237
238
if (!MessageDigest.isEqual(theHash, manHash))
239
throw new SecurityException(digest.getAlgorithm()+
240
" digest error for "+name);
241
}
242
243
// take it out of sigFileSigners and put it in verifiedSigners...
244
signers = sigFileSigners.remove(name);
245
if (signers != null) {
246
verifiedSigners.put(name, signers);
247
}
248
return signers;
249
}
250
251
/**
252
* Get constraints parameters for JAR. The constraints should be
253
* checked against all code signers. Returns the parameters,
254
* or null if the signers for this entry have already been checked.
255
*/
256
private JarConstraintsParameters getParams(
257
Map<String, CodeSigner[]> verifiedSigners,
258
Map<String, CodeSigner[]> sigFileSigners) {
259
260
// verifiedSigners is usually preloaded with the Manifest's signers.
261
// If verifiedSigners contains the Manifest, then it will have all of
262
// the signers of the JAR. But if it doesn't then we need to fallback
263
// and check verifiedSigners to see if the signers of this entry have
264
// been checked already.
265
if (verifiedSigners.containsKey(JarFile.MANIFEST_NAME)) {
266
if (verifiedSigners.size() > 1) {
267
// this means we already checked it previously
268
return null;
269
} else {
270
return new JarConstraintsParameters(
271
verifiedSigners.get(JarFile.MANIFEST_NAME));
272
}
273
} else {
274
CodeSigner[] signers = sigFileSigners.get(name);
275
if (verifiedSigners.containsValue(signers)) {
276
return null;
277
} else {
278
return new JarConstraintsParameters(signers);
279
}
280
}
281
}
282
}
283
284
285