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