1 /*
2  * Copyright (c) 2013, 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.
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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 /*
25  * @test
26  *  @bug 8021788
27  *  @summary JarInputStream doesn't provide certificates for some file under META-INF
28  */
29 
30 import java.util.jar.*;
31 import java.io.*;
32 import java.util.zip.ZipEntry;
33 import java.util.zip.ZipOutputStream;
34 
35 public class ExtraFileInMetaInf {
main(String args[])36     public static void main(String args[]) throws Exception {
37 
38         // Create a zip file with 2 entries
39         try (ZipOutputStream zos =
40                      new ZipOutputStream(new FileOutputStream("x.jar"))) {
41             zos.putNextEntry(new ZipEntry("META-INF/SUB/file"));
42             zos.write(new byte[10]);
43             zos.putNextEntry(new ZipEntry("x"));
44             zos.write(new byte[10]);
45             zos.close();
46         }
47 
48         // Sign it
49         new File("ks").delete();
50         sun.security.tools.keytool.Main.main(
51                 ("-keystore ks -storepass changeit -keypass changeit " +
52                         "-keyalg rsa -alias a -dname CN=A -genkeypair").split(" "));
53         sun.security.tools.jarsigner.Main.main(
54                 "-keystore ks -storepass changeit x.jar a".split(" "));
55 
56         // Check if the entries are signed
57         try (JarInputStream jis =
58                      new JarInputStream(new FileInputStream("x.jar"))) {
59             JarEntry je;
60             while ((je = jis.getNextJarEntry()) != null) {
61                 String name = je.toString();
62                 if (name.equals("META-INF/SUB/file") || name.equals("x")) {
63                     while (jis.read(new byte[1000]) >= 0);
64                     if (je.getCertificates() == null) {
65                         throw new Exception(name + " not signed");
66                     }
67                 }
68             }
69         }
70     }
71 }
72