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