J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2005 Sun Microsystems, Inc. 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. |
| 8 | * |
| 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | * accompanied this code). |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License version |
| 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
| 19 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /* |
| 25 | * @test |
| 26 | * @bug 6284489 |
| 27 | * @summary Confirm that JarEntry.getCertificates identifies signed entries. |
| 28 | */ |
| 29 | |
| 30 | import java.io.*; |
| 31 | import java.net.URL; |
| 32 | import java.security.CodeSigner; |
| 33 | import java.security.cert.Certificate; |
| 34 | import java.util.Enumeration; |
| 35 | import java.util.jar.*; |
| 36 | |
| 37 | /* |
| 38 | * Confirm that the signed entries in a JAR file are identified correctly |
| 39 | * when JarEntry.getCertificates is used to extract the signer's certificates. |
| 40 | * |
| 41 | * NOTE: the original problem occurred only when entries were extracted using |
| 42 | * the JarInputStream.getNextJarEntry method; it never occurred when |
| 43 | * entries were extracted using the JarFile.entries method. |
| 44 | */ |
| 45 | public class ScanSignedJar { |
| 46 | |
| 47 | private static final String JAR_LOCATION = |
| 48 | "file:" + |
| 49 | System.getProperty("test.src", ".") + |
| 50 | System.getProperty("file.separator") + |
| 51 | "signed.jar"; |
| 52 | |
| 53 | public static void main(String[] args) throws Exception { |
| 54 | |
| 55 | System.out.println("Opening " + JAR_LOCATION + "..."); |
| 56 | JarInputStream inStream = |
| 57 | new JarInputStream(new URL(JAR_LOCATION).openStream(), true); |
| 58 | JarEntry entry; |
| 59 | byte[] buffer = new byte[1024]; |
| 60 | |
| 61 | while ((entry = inStream.getNextJarEntry()) != null) { |
| 62 | |
| 63 | // need to read the entry's data to see the certs. |
| 64 | while(inStream.read(buffer) != -1) |
| 65 | ; |
| 66 | |
| 67 | String name = entry.getName(); |
| 68 | long size = entry.getSize(); |
| 69 | Certificate[] certificates = entry.getCertificates(); |
| 70 | CodeSigner[] signers = entry.getCodeSigners(); |
| 71 | |
| 72 | if (signers == null && certificates == null) { |
| 73 | System.out.println("[unsigned]\t" + name + "\t(" + size + |
| 74 | " bytes)"); |
| 75 | } else if (signers != null && certificates != null) { |
| 76 | System.out.println("[" + signers.length + |
| 77 | (signers.length == 1 ? " signer" : " signers") + "]\t" + |
| 78 | name + "\t(" + size + " bytes)"); |
| 79 | } else { |
| 80 | System.out.println("[*ERROR*]\t" + name + "\t(" + size + |
| 81 | " bytes)"); |
| 82 | throw new Exception("Cannot determine whether the entry is " + |
| 83 | "signed or unsigned (signers[] doesn't match certs[])."); |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | } |