ScanSignedJar.java revision 2362
4632N/A/*
4632N/A * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
4632N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4632N/A *
4632N/A * This code is free software; you can redistribute it and/or modify it
4632N/A * under the terms of the GNU General Public License version 2 only, as
4632N/A * published by the Free Software Foundation.
4632N/A *
4632N/A * This code is distributed in the hope that it will be useful, but WITHOUT
4632N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4632N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
4632N/A * version 2 for more details (a copy is included in the LICENSE file that
4632N/A * accompanied this code).
4632N/A *
4632N/A * You should have received a copy of the GNU General Public License version
4632N/A * 2 along with this work; if not, write to the Free Software Foundation,
4632N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4632N/A *
4632N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4632N/A * or visit www.oracle.com if you need additional information or have any
4632N/A * questions.
4632N/A */
4632N/A
4632N/A/*
4632N/A * @test
4632N/A * @bug 6284489
4632N/A * @summary Confirm that JarEntry.getCertificates identifies signed entries.
4632N/A */
4632N/A
4632N/Aimport java.io.*;
4632N/Aimport java.net.URL;
4632N/Aimport java.security.CodeSigner;
4632N/Aimport java.security.cert.Certificate;
4632N/Aimport java.util.Enumeration;
4632N/Aimport java.util.jar.*;
4632N/A
4632N/A/*
4632N/A * Confirm that the signed entries in a JAR file are identified correctly
4632N/A * when JarEntry.getCertificates is used to extract the signer's certificates.
4632N/A *
4632N/A * NOTE: the original problem occurred only when entries were extracted using
4632N/A * the JarInputStream.getNextJarEntry method; it never occurred when
4632N/A * entries were extracted using the JarFile.entries method.
4632N/A */
4632N/Apublic class ScanSignedJar {
4632N/A
4632N/A private static final String JAR_LOCATION =
4632N/A "file:" +
4632N/A System.getProperty("test.src", ".") +
4632N/A System.getProperty("file.separator") +
4632N/A "signed.jar";
4632N/A
4632N/A public static void main(String[] args) throws Exception {
4632N/A
4632N/A System.out.println("Opening " + JAR_LOCATION + "...");
4632N/A JarInputStream inStream =
4632N/A new JarInputStream(new URL(JAR_LOCATION).openStream(), true);
4632N/A JarEntry entry;
4632N/A byte[] buffer = new byte[1024];
4632N/A
4632N/A while ((entry = inStream.getNextJarEntry()) != null) {
4632N/A
4632N/A // need to read the entry's data to see the certs.
4632N/A while(inStream.read(buffer) != -1)
4632N/A ;
4632N/A
4632N/A String name = entry.getName();
4632N/A long size = entry.getSize();
4632N/A Certificate[] certificates = entry.getCertificates();
4632N/A CodeSigner[] signers = entry.getCodeSigners();
4632N/A
4632N/A if (signers == null && certificates == null) {
4632N/A System.out.println("[unsigned]\t" + name + "\t(" + size +
4632N/A " bytes)");
4632N/A } else if (signers != null && certificates != null) {
4632N/A System.out.println("[" + signers.length +
4632N/A (signers.length == 1 ? " signer" : " signers") + "]\t" +
4632N/A name + "\t(" + size + " bytes)");
4632N/A } else {
4632N/A System.out.println("[*ERROR*]\t" + name + "\t(" + size +
4632N/A " bytes)");
4632N/A throw new Exception("Cannot determine whether the entry is " +
4632N/A "signed or unsigned (signers[] doesn't match certs[]).");
4632N/A }
4632N/A }
4632N/A }
4632N/A}
4632N/A