author | weijun |
Thu, 06 May 2010 13:42:52 +0800 | |
changeset 5462 | cb614e59f7f9 |
parent 4152 | bc36a9f01ac6 |
child 5506 | 202f599c92aa |
child 5606 | 63aa4b61c0a8 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5462
cb614e59f7f9
6890876: jarsigner can add CRL info into signed jar
weijun
parents:
4152
diff
changeset
|
2 |
* Copyright 1997-2010 Sun Microsystems, Inc. All Rights Reserved. |
2 | 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. Sun designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 |
* have any questions. |
|
24 |
*/ |
|
25 |
||
26 |
package java.util.jar; |
|
27 |
||
28 |
import java.io.*; |
|
29 |
import java.util.*; |
|
30 |
import java.security.*; |
|
31 |
import java.security.cert.CertificateException; |
|
32 |
||
33 |
import sun.security.util.ManifestDigester; |
|
34 |
import sun.security.util.ManifestEntryVerifier; |
|
35 |
import sun.security.util.SignatureFileVerifier; |
|
36 |
import sun.security.util.Debug; |
|
37 |
||
38 |
/** |
|
39 |
* |
|
40 |
* @author Roland Schemers |
|
41 |
*/ |
|
42 |
class JarVerifier { |
|
43 |
||
44 |
/* Are we debugging ? */ |
|
45 |
static final Debug debug = Debug.getInstance("jar"); |
|
46 |
||
47 |
/* a table mapping names to code signers, for jar entries that have |
|
48 |
had their actual hashes verified */ |
|
49 |
private Hashtable verifiedSigners; |
|
50 |
||
51 |
/* a table mapping names to code signers, for jar entries that have |
|
4152
bc36a9f01ac6
6870812: enhance security tools to use ECC algorithms
weijun
parents:
4049
diff
changeset
|
52 |
passed the .SF/.DSA/.EC -> MANIFEST check */ |
2 | 53 |
private Hashtable sigFileSigners; |
54 |
||
55 |
/* a hash table to hold .SF bytes */ |
|
56 |
private Hashtable sigFileData; |
|
57 |
||
58 |
/** "queue" of pending PKCS7 blocks that we couldn't parse |
|
59 |
* until we parsed the .SF file */ |
|
60 |
private ArrayList pendingBlocks; |
|
61 |
||
62 |
/* cache of CodeSigner objects */ |
|
63 |
private ArrayList signerCache; |
|
64 |
||
65 |
/* Are we parsing a block? */ |
|
66 |
private boolean parsingBlockOrSF = false; |
|
67 |
||
68 |
/* Are we done parsing META-INF entries? */ |
|
69 |
private boolean parsingMeta = true; |
|
70 |
||
71 |
/* Are there are files to verify? */ |
|
72 |
private boolean anyToVerify = true; |
|
73 |
||
74 |
/* The output stream to use when keeping track of files we are interested |
|
75 |
in */ |
|
76 |
private ByteArrayOutputStream baos; |
|
77 |
||
78 |
/** The ManifestDigester object */ |
|
79 |
private ManifestDigester manDig; |
|
80 |
||
81 |
/** the bytes for the manDig object */ |
|
82 |
byte manifestRawBytes[] = null; |
|
83 |
||
84 |
public JarVerifier(byte rawBytes[]) { |
|
85 |
manifestRawBytes = rawBytes; |
|
86 |
sigFileSigners = new Hashtable(); |
|
87 |
verifiedSigners = new Hashtable(); |
|
88 |
sigFileData = new Hashtable(11); |
|
89 |
pendingBlocks = new ArrayList(); |
|
90 |
baos = new ByteArrayOutputStream(); |
|
91 |
} |
|
92 |
||
93 |
/** |
|
94 |
* This method scans to see which entry we're parsing and |
|
95 |
* keeps various state information depending on what type of |
|
96 |
* file is being parsed. |
|
97 |
*/ |
|
98 |
public void beginEntry(JarEntry je, ManifestEntryVerifier mev) |
|
99 |
throws IOException |
|
100 |
{ |
|
101 |
if (je == null) |
|
102 |
return; |
|
103 |
||
104 |
if (debug != null) { |
|
105 |
debug.println("beginEntry "+je.getName()); |
|
106 |
} |
|
107 |
||
108 |
String name = je.getName(); |
|
109 |
||
110 |
/* |
|
111 |
* Assumptions: |
|
112 |
* 1. The manifest should be the first entry in the META-INF directory. |
|
4152
bc36a9f01ac6
6870812: enhance security tools to use ECC algorithms
weijun
parents:
4049
diff
changeset
|
113 |
* 2. The .SF/.DSA/.EC files follow the manifest, before any normal entries |
2 | 114 |
* 3. Any of the following will throw a SecurityException: |
115 |
* a. digest mismatch between a manifest section and |
|
116 |
* the SF section. |
|
117 |
* b. digest mismatch between the actual jar entry and the manifest |
|
118 |
*/ |
|
119 |
||
120 |
if (parsingMeta) { |
|
121 |
String uname = name.toUpperCase(Locale.ENGLISH); |
|
122 |
if ((uname.startsWith("META-INF/") || |
|
123 |
uname.startsWith("/META-INF/"))) { |
|
124 |
||
125 |
if (je.isDirectory()) { |
|
126 |
mev.setEntry(null, je); |
|
127 |
return; |
|
128 |
} |
|
129 |
||
130 |
if (SignatureFileVerifier.isBlockOrSF(uname)) { |
|
4152
bc36a9f01ac6
6870812: enhance security tools to use ECC algorithms
weijun
parents:
4049
diff
changeset
|
131 |
/* We parse only DSA, RSA or EC PKCS7 blocks. */ |
2 | 132 |
parsingBlockOrSF = true; |
133 |
baos.reset(); |
|
134 |
mev.setEntry(null, je); |
|
135 |
} |
|
136 |
return; |
|
137 |
} |
|
138 |
} |
|
139 |
||
140 |
if (parsingMeta) { |
|
141 |
doneWithMeta(); |
|
142 |
} |
|
143 |
||
144 |
if (je.isDirectory()) { |
|
145 |
mev.setEntry(null, je); |
|
146 |
return; |
|
147 |
} |
|
148 |
||
149 |
// be liberal in what you accept. If the name starts with ./, remove |
|
150 |
// it as we internally canonicalize it with out the ./. |
|
151 |
if (name.startsWith("./")) |
|
152 |
name = name.substring(2); |
|
153 |
||
154 |
// be liberal in what you accept. If the name starts with /, remove |
|
155 |
// it as we internally canonicalize it with out the /. |
|
156 |
if (name.startsWith("/")) |
|
157 |
name = name.substring(1); |
|
158 |
||
159 |
// only set the jev object for entries that have a signature |
|
160 |
if (sigFileSigners.get(name) != null) { |
|
161 |
mev.setEntry(name, je); |
|
162 |
return; |
|
163 |
} |
|
164 |
||
165 |
// don't compute the digest for this entry |
|
166 |
mev.setEntry(null, je); |
|
167 |
||
168 |
return; |
|
169 |
} |
|
170 |
||
171 |
/** |
|
172 |
* update a single byte. |
|
173 |
*/ |
|
174 |
||
175 |
public void update(int b, ManifestEntryVerifier mev) |
|
176 |
throws IOException |
|
177 |
{ |
|
178 |
if (b != -1) { |
|
179 |
if (parsingBlockOrSF) { |
|
180 |
baos.write(b); |
|
181 |
} else { |
|
182 |
mev.update((byte)b); |
|
183 |
} |
|
184 |
} else { |
|
185 |
processEntry(mev); |
|
186 |
} |
|
187 |
} |
|
188 |
||
189 |
/** |
|
190 |
* update an array of bytes. |
|
191 |
*/ |
|
192 |
||
193 |
public void update(int n, byte[] b, int off, int len, |
|
194 |
ManifestEntryVerifier mev) |
|
195 |
throws IOException |
|
196 |
{ |
|
197 |
if (n != -1) { |
|
198 |
if (parsingBlockOrSF) { |
|
199 |
baos.write(b, off, n); |
|
200 |
} else { |
|
201 |
mev.update(b, off, n); |
|
202 |
} |
|
203 |
} else { |
|
204 |
processEntry(mev); |
|
205 |
} |
|
206 |
} |
|
207 |
||
208 |
/** |
|
209 |
* called when we reach the end of entry in one of the read() methods. |
|
210 |
*/ |
|
211 |
private void processEntry(ManifestEntryVerifier mev) |
|
212 |
throws IOException |
|
213 |
{ |
|
214 |
if (!parsingBlockOrSF) { |
|
215 |
JarEntry je = mev.getEntry(); |
|
216 |
if ((je != null) && (je.signers == null)) { |
|
217 |
je.signers = mev.verify(verifiedSigners, sigFileSigners); |
|
218 |
je.certs = mapSignersToCertArray(je.signers); |
|
219 |
} |
|
220 |
} else { |
|
221 |
||
222 |
try { |
|
223 |
parsingBlockOrSF = false; |
|
224 |
||
225 |
if (debug != null) { |
|
226 |
debug.println("processEntry: processing block"); |
|
227 |
} |
|
228 |
||
229 |
String uname = mev.getEntry().getName() |
|
230 |
.toUpperCase(Locale.ENGLISH); |
|
231 |
||
232 |
if (uname.endsWith(".SF")) { |
|
233 |
String key = uname.substring(0, uname.length()-3); |
|
234 |
byte bytes[] = baos.toByteArray(); |
|
235 |
// add to sigFileData in case future blocks need it |
|
236 |
sigFileData.put(key, bytes); |
|
237 |
// check pending blocks, we can now process |
|
238 |
// anyone waiting for this .SF file |
|
239 |
Iterator it = pendingBlocks.iterator(); |
|
240 |
while (it.hasNext()) { |
|
241 |
SignatureFileVerifier sfv = |
|
242 |
(SignatureFileVerifier) it.next(); |
|
243 |
if (sfv.needSignatureFile(key)) { |
|
244 |
if (debug != null) { |
|
245 |
debug.println( |
|
246 |
"processEntry: processing pending block"); |
|
247 |
} |
|
248 |
||
249 |
sfv.setSignatureFile(bytes); |
|
250 |
sfv.process(sigFileSigners); |
|
251 |
} |
|
252 |
} |
|
253 |
return; |
|
254 |
} |
|
255 |
||
256 |
// now we are parsing a signature block file |
|
257 |
||
258 |
String key = uname.substring(0, uname.lastIndexOf(".")); |
|
259 |
||
260 |
if (signerCache == null) |
|
261 |
signerCache = new ArrayList(); |
|
262 |
||
263 |
if (manDig == null) { |
|
264 |
synchronized(manifestRawBytes) { |
|
265 |
if (manDig == null) { |
|
266 |
manDig = new ManifestDigester(manifestRawBytes); |
|
267 |
manifestRawBytes = null; |
|
268 |
} |
|
269 |
} |
|
270 |
} |
|
271 |
||
272 |
SignatureFileVerifier sfv = |
|
273 |
new SignatureFileVerifier(signerCache, |
|
274 |
manDig, uname, baos.toByteArray()); |
|
275 |
||
276 |
if (sfv.needSignatureFileBytes()) { |
|
277 |
// see if we have already parsed an external .SF file |
|
278 |
byte[] bytes = (byte[]) sigFileData.get(key); |
|
279 |
||
280 |
if (bytes == null) { |
|
281 |
// put this block on queue for later processing |
|
282 |
// since we don't have the .SF bytes yet |
|
283 |
// (uname, block); |
|
284 |
if (debug != null) { |
|
285 |
debug.println("adding pending block"); |
|
286 |
} |
|
287 |
pendingBlocks.add(sfv); |
|
288 |
return; |
|
289 |
} else { |
|
290 |
sfv.setSignatureFile(bytes); |
|
291 |
} |
|
292 |
} |
|
293 |
sfv.process(sigFileSigners); |
|
294 |
||
295 |
} catch (IOException ioe) { |
|
4049
f02d7c56d8f8
6883983: JarVerifier dependency on sun.security.pkcs should be removed
alanb
parents:
2
diff
changeset
|
296 |
// e.g. sun.security.pkcs.ParsingException |
2 | 297 |
if (debug != null) debug.println("processEntry caught: "+ioe); |
298 |
// ignore and treat as unsigned |
|
299 |
} catch (SignatureException se) { |
|
300 |
if (debug != null) debug.println("processEntry caught: "+se); |
|
301 |
// ignore and treat as unsigned |
|
302 |
} catch (NoSuchAlgorithmException nsae) { |
|
303 |
if (debug != null) debug.println("processEntry caught: "+nsae); |
|
304 |
// ignore and treat as unsigned |
|
305 |
} catch (CertificateException ce) { |
|
306 |
if (debug != null) debug.println("processEntry caught: "+ce); |
|
307 |
// ignore and treat as unsigned |
|
308 |
} |
|
309 |
} |
|
310 |
} |
|
311 |
||
312 |
/** |
|
313 |
* Return an array of java.security.cert.Certificate objects for |
|
314 |
* the given file in the jar. |
|
315 |
*/ |
|
316 |
public java.security.cert.Certificate[] getCerts(String name) |
|
317 |
{ |
|
318 |
return mapSignersToCertArray(getCodeSigners(name)); |
|
319 |
} |
|
320 |
||
321 |
/** |
|
322 |
* return an array of CodeSigner objects for |
|
323 |
* the given file in the jar. this array is not cloned. |
|
324 |
* |
|
325 |
*/ |
|
326 |
public CodeSigner[] getCodeSigners(String name) |
|
327 |
{ |
|
328 |
return (CodeSigner[])verifiedSigners.get(name); |
|
329 |
} |
|
330 |
||
331 |
/* |
|
332 |
* Convert an array of signers into an array of concatenated certificate |
|
333 |
* arrays. |
|
334 |
*/ |
|
335 |
private static java.security.cert.Certificate[] mapSignersToCertArray( |
|
336 |
CodeSigner[] signers) { |
|
337 |
||
338 |
if (signers != null) { |
|
339 |
ArrayList certChains = new ArrayList(); |
|
340 |
for (int i = 0; i < signers.length; i++) { |
|
341 |
certChains.addAll( |
|
342 |
signers[i].getSignerCertPath().getCertificates()); |
|
343 |
} |
|
344 |
||
345 |
// Convert into a Certificate[] |
|
346 |
return (java.security.cert.Certificate[]) |
|
347 |
certChains.toArray( |
|
348 |
new java.security.cert.Certificate[certChains.size()]); |
|
349 |
} |
|
350 |
return null; |
|
351 |
} |
|
352 |
||
353 |
/** |
|
354 |
* returns true if there no files to verify. |
|
355 |
* should only be called after all the META-INF entries |
|
356 |
* have been processed. |
|
357 |
*/ |
|
358 |
boolean nothingToVerify() |
|
359 |
{ |
|
360 |
return (anyToVerify == false); |
|
361 |
} |
|
362 |
||
363 |
/** |
|
364 |
* called to let us know we have processed all the |
|
365 |
* META-INF entries, and if we re-read one of them, don't |
|
366 |
* re-process it. Also gets rid of any data structures |
|
367 |
* we needed when parsing META-INF entries. |
|
368 |
*/ |
|
369 |
void doneWithMeta() |
|
370 |
{ |
|
371 |
parsingMeta = false; |
|
372 |
anyToVerify = !sigFileSigners.isEmpty(); |
|
373 |
baos = null; |
|
374 |
sigFileData = null; |
|
375 |
pendingBlocks = null; |
|
376 |
signerCache = null; |
|
377 |
manDig = null; |
|
378 |
} |
|
379 |
||
380 |
static class VerifierStream extends java.io.InputStream { |
|
381 |
||
382 |
private InputStream is; |
|
383 |
private JarVerifier jv; |
|
384 |
private ManifestEntryVerifier mev; |
|
385 |
private long numLeft; |
|
386 |
||
387 |
VerifierStream(Manifest man, |
|
388 |
JarEntry je, |
|
389 |
InputStream is, |
|
390 |
JarVerifier jv) throws IOException |
|
391 |
{ |
|
392 |
this.is = is; |
|
393 |
this.jv = jv; |
|
394 |
this.mev = new ManifestEntryVerifier(man); |
|
395 |
this.jv.beginEntry(je, mev); |
|
396 |
this.numLeft = je.getSize(); |
|
397 |
if (this.numLeft == 0) |
|
398 |
this.jv.update(-1, this.mev); |
|
399 |
} |
|
400 |
||
401 |
public int read() throws IOException |
|
402 |
{ |
|
403 |
if (numLeft > 0) { |
|
404 |
int b = is.read(); |
|
405 |
jv.update(b, mev); |
|
406 |
numLeft--; |
|
407 |
if (numLeft == 0) |
|
408 |
jv.update(-1, mev); |
|
409 |
return b; |
|
410 |
} else { |
|
411 |
return -1; |
|
412 |
} |
|
413 |
} |
|
414 |
||
415 |
public int read(byte b[], int off, int len) throws IOException { |
|
416 |
if ((numLeft > 0) && (numLeft < len)) { |
|
417 |
len = (int)numLeft; |
|
418 |
} |
|
419 |
||
420 |
if (numLeft > 0) { |
|
421 |
int n = is.read(b, off, len); |
|
422 |
jv.update(n, b, off, len, mev); |
|
423 |
numLeft -= n; |
|
424 |
if (numLeft == 0) |
|
425 |
jv.update(-1, b, off, len, mev); |
|
426 |
return n; |
|
427 |
} else { |
|
428 |
return -1; |
|
429 |
} |
|
430 |
} |
|
431 |
||
432 |
public void close() |
|
433 |
throws IOException |
|
434 |
{ |
|
435 |
if (is != null) |
|
436 |
is.close(); |
|
437 |
is = null; |
|
438 |
mev = null; |
|
439 |
jv = null; |
|
440 |
} |
|
441 |
||
442 |
public int available() throws IOException { |
|
443 |
return is.available(); |
|
444 |
} |
|
445 |
||
446 |
} |
|
447 |
} |