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  * @modules java.base/sun.security.tools.keytool
  29  *          jdk.jartool/sun.security.tools.jarsigner
  30  */
  31 
  32 import java.util.jar.*;
  33 import java.io.*;
  34 import java.util.zip.ZipEntry;
  35 import java.util.zip.ZipOutputStream;
  36 
  37 public class ExtraFileInMetaInf {
  38     public static void main(String args[]) throws Exception {
  39 
  40         // Create a zip file with 2 entries
  41         try (ZipOutputStream zos =
  42                      new ZipOutputStream(new FileOutputStream("x.jar"))) {
  43             zos.putNextEntry(new ZipEntry("META-INF/SUB/file"));
  44             zos.write(new byte[10]);
  45             zos.putNextEntry(new ZipEntry("x"));
  46             zos.write(new byte[10]);
  47             zos.close();
  48         }
  49 
  50         // Sign it
  51         new File("ks").delete();
  52         sun.security.tools.keytool.Main.main(
  53                 ("-keystore ks -storepass changeit -keypass changeit " +
  54                         "-keyalg rsa -alias a -dname CN=A -genkeypair").split(" "));
  55         sun.security.tools.jarsigner.Main.main(
  56                 "-keystore ks -storepass changeit x.jar a".split(" "));
  57 
  58         // Check if the entries are signed
  59         try (JarInputStream jis =
  60                      new JarInputStream(new FileInputStream("x.jar"))) {
  61             JarEntry je;
  62             while ((je = jis.getNextJarEntry()) != null) {
  63                 String name = je.toString();
  64                 if (name.equals("META-INF/SUB/file") || name.equals("x")) {
  65                     while (jis.read(new byte[1000]) >= 0);
  66                     if (je.getCertificates() == null) {
  67                         throw new Exception(name + " not signed");
  68                     }
  69                 }
  70             }
  71         }
  72     }
  73 }