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