1 /*
   2  * Copyright (c) 2003, 2012, 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 4924188
  27  * @summary sign a JAR file that has entry names with non-ASCII characters.
  28  */
  29 
  30 import sun.security.tools.*;
  31 import java.io.*;
  32 import java.util.*;
  33 import java.util.jar.*;
  34 import java.security.cert.Certificate;
  35 
  36 public class JarSigningNonAscii {
  37 
  38     private static String jarFile;
  39     private static String keystore;
  40 
  41     public static void main(String[] args) throws Exception {
  42 
  43         String srcDir = System.getProperty("test.src", ".");
  44         String destDir = System.getProperty("test.classes", ".");
  45         String unsignedJar = srcDir + "/JarSigning_RU.jar";
  46         String signedJar = destDir + "/JarSigning_RU.signed.jar";
  47         String keystore = srcDir + "/JarSigning.keystore";
  48 
  49         // remove signed jar if it exists
  50         try {
  51             File removeMe = new File(signedJar);
  52             removeMe.delete();
  53         } catch (Exception e) {
  54             // ignore
  55             e.printStackTrace();
  56         }
  57 
  58         // sign the provided jar file
  59         String[] jsArgs = {
  60                         "-keystore", keystore,
  61                         "-storepass", "bbbbbb",
  62                         "-signedJar", signedJar,
  63                         unsignedJar, "b"
  64                         };
  65         sun.security.tools.jarsigner.Main.main(jsArgs);
  66 
  67         //  verify the signed jar file
  68 
  69         /**
  70          * can not do this because JarSigner calls System.exit
  71          * with an exit code that jtreg does not like
  72          *
  73         String[] vArgs = {
  74                 "-verify",
  75                 "-keystore", keystore,
  76                 "-storepass", "bbbbbb",
  77                 "-verbose",
  78                 signedJar
  79                 };
  80         JarSigner.main(vArgs);
  81         */
  82 
  83         JarEntry je;
  84         JarFile jf = new JarFile(signedJar, true);
  85 
  86         Vector entriesVec = new Vector();
  87         byte[] buffer = new byte[8192];
  88 
  89         Enumeration entries = jf.entries();
  90         while (entries.hasMoreElements()) {
  91             je = (JarEntry)entries.nextElement();
  92             entriesVec.addElement(je);
  93             InputStream is = jf.getInputStream(je);
  94             int n;
  95             while ((n = is.read(buffer, 0, buffer.length)) != -1) {
  96                 // we just read. this will throw a SecurityException
  97                 // if  a signature/digest check fails.
  98             }
  99             is.close();
 100         }
 101         jf.close();
 102         Manifest man = jf.getManifest();
 103         int isSignedCount = 0;
 104         if (man != null) {
 105             Enumeration e = entriesVec.elements();
 106             while (e.hasMoreElements()) {
 107                 je = (JarEntry) e.nextElement();
 108                 String name = je.getName();
 109                 Certificate[] certs = je.getCertificates();
 110                 if ((certs != null) && (certs.length > 0)) {
 111                     isSignedCount++;
 112                 }
 113             }
 114         }
 115 
 116         if (isSignedCount != 4) {
 117             throw new SecurityException("error signing JAR file");
 118         }
 119 
 120         System.out.println("jar verified");
 121     }
 122 }