Skip to content

Commit b256b1a

Browse files
Roman Marchenkognu-andrew
authored andcommitted
8309841: Jarsigner should print a warning if an entry is removed
Reviewed-by: andrew Backport-of: bdfb41f977258831e4b0ceaef5d016d095ab6e7f
1 parent 69d9a2f commit b256b1a

File tree

5 files changed

+246
-1
lines changed

5 files changed

+246
-1
lines changed

jdk/src/share/classes/sun/security/tools/jarsigner/Main.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ public static void main(String args[]) throws Exception {
181181
private boolean hasExpiringCert = false;
182182
private boolean hasExpiringTsaCert = false;
183183
private boolean noTimestamp = true;
184+
private boolean hasNonexistentEntries = false;
184185

185186
// Expiration date. The value could be null if signed by a trusted cert.
186187
private Date expireDate = null;
@@ -635,6 +636,7 @@ void verifyJar(String jarName)
635636
Map<String,PKCS7> sigMap = new HashMap<>();
636637
Map<String,String> sigNameMap = new HashMap<>();
637638
Map<String,String> unparsableSignatures = new HashMap<>();
639+
Map<String,Set<String>> entriesInSF = new HashMap<>();
638640

639641
try {
640642
jf = new JarFile(jarName, true);
@@ -671,6 +673,7 @@ void verifyJar(String jarName)
671673
break;
672674
}
673675
}
676+
entriesInSF.put(alias, sf.getEntries().keySet());
674677
if (!found) {
675678
unparsableSignatures.putIfAbsent(alias,
676679
String.format(
@@ -769,6 +772,9 @@ void verifyJar(String jarName)
769772
sb.append('\n');
770773
}
771774
}
775+
for (Set<String> signed : entriesInSF.values()) {
776+
signed.remove(name);
777+
}
772778
} else if (showcerts && !verbose.equals("all")) {
773779
// Print no info for unsigned entries when -verbose:all,
774780
// to be consistent with old behavior.
@@ -951,6 +957,13 @@ void verifyJar(String jarName)
951957
if (verbose != null) {
952958
System.out.println(history);
953959
}
960+
Set<String> signed = entriesInSF.get(s);
961+
if (!signed.isEmpty()) {
962+
if (verbose != null) {
963+
System.out.println(rb.getString("history.nonexistent.entries") + signed);
964+
}
965+
hasNonexistentEntries = true;
966+
}
954967
} else {
955968
unparsableSignatures.putIfAbsent(s, String.format(
956969
rb.getString("history.nobk"), s));
@@ -1180,6 +1193,7 @@ private void displayMessagesAndResult(boolean isSigning) {
11801193
if (hasExpiringCert ||
11811194
(hasExpiringTsaCert && expireDate != null) ||
11821195
(noTimestamp && expireDate != null) ||
1196+
hasNonexistentEntries ||
11831197
(hasExpiredTsaCert && signerNotExpired)) {
11841198

11851199
if (hasExpiredTsaCert && signerNotExpired) {
@@ -1217,6 +1231,9 @@ private void displayMessagesAndResult(boolean isSigning) {
12171231
: "no.timestamp.verifying"), expireDate));
12181232
}
12191233
}
1234+
if (hasNonexistentEntries) {
1235+
warnings.add(rb.getString("nonexistent.entries.found"));
1236+
}
12201237
}
12211238

12221239
System.out.println(result);

jdk/src/share/classes/sun/security/tools/jarsigner/Resources.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ public class Resources extends java.util.ListResourceBundle {
155155

156156
{"history.with.ts", "- Signed by \"%1$s\"\n Digest algorithm: %2$s\n Signature algorithm: %3$s, %4$s\n Timestamped by \"%6$s\" on %5$tc\n Timestamp digest algorithm: %7$s\n Timestamp signature algorithm: %8$s, %9$s"},
157157
{"history.without.ts", "- Signed by \"%1$s\"\n Digest algorithm: %2$s\n Signature algorithm: %3$s, %4$s"},
158+
{"history.nonexistent.entries", " Warning: nonexistent signed entries: "},
158159
{"history.unparsable", "- Unparsable signature-related file %s"},
159160
{"history.nosf", "- Missing signature-related file META-INF/%s.SF"},
160161
{"history.nobk", "- Missing block file for signature-related file META-INF/%s.SF"},
@@ -165,6 +166,7 @@ public class Resources extends java.util.ListResourceBundle {
165166
{"key.bit.weak", "%d-bit key (weak)"},
166167
{"key.bit.disabled", "%d-bit key (disabled)"},
167168
{"unknown.size", "unknown size"},
169+
{"nonexistent.entries.found", "This jar contains signed entries for files that do not exist. See the -verbose output for more details."},
168170

169171
{"jarsigner.", "jarsigner: "},
170172
{"signature.filename.must.consist.of.the.following.characters.A.Z.0.9.or.",
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright (c) 2024, 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+
/* @test
25+
* @bug 8309841
26+
* @summary Unit Test for a common Test API in jdk.testlibrary.JarUtils
27+
* @library /lib/testlibrary
28+
*/
29+
30+
import jdk.testlibrary.Asserts;
31+
import jdk.testlibrary.JarUtils;
32+
33+
import java.io.IOException;
34+
import java.nio.charset.StandardCharsets;
35+
import java.nio.file.Files;
36+
import java.nio.file.Paths;
37+
import java.util.Arrays;
38+
import java.util.HashSet;
39+
import java.util.jar.JarEntry;
40+
import java.util.jar.JarFile;
41+
import java.util.stream.Collectors;
42+
43+
public class JarUtilsTest {
44+
public static void main(String[] args) throws Exception {
45+
Files.createDirectory(Paths.get("bx"));
46+
JarUtils.createJarFile(Paths.get("a.jar"),
47+
Paths.get("."),
48+
Files.write(Paths.get("a"), "".getBytes(StandardCharsets.UTF_8)),
49+
Files.write(Paths.get("b1"), "".getBytes(StandardCharsets.UTF_8)),
50+
Files.write(Paths.get("b2"), "".getBytes(StandardCharsets.UTF_8)),
51+
Files.write(Paths.get("bx/x"), "".getBytes(StandardCharsets.UTF_8)),
52+
Files.write(Paths.get("c"), "".getBytes(StandardCharsets.UTF_8)),
53+
Files.write(Paths.get("e1"), "".getBytes(StandardCharsets.UTF_8)),
54+
Files.write(Paths.get("e2"), "".getBytes(StandardCharsets.UTF_8)));
55+
checkContent("a", "b1", "b2", "bx/x", "c", "e1", "e2");
56+
57+
JarUtils.deleteEntries(Paths.get("a.jar"), "a");
58+
checkContent("b1", "b2", "bx/x", "c", "e1", "e2");
59+
60+
// Note: b* covers everything starting with b, even bx/x
61+
JarUtils.deleteEntries(Paths.get("a.jar"), "b*");
62+
checkContent("c", "e1", "e2");
63+
64+
// d* does not match
65+
JarUtils.deleteEntries(Paths.get("a.jar"), "d*");
66+
checkContent("c", "e1", "e2");
67+
68+
// multiple patterns
69+
JarUtils.deleteEntries(Paths.get("a.jar"), "d*", "e*");
70+
checkContent("c");
71+
}
72+
73+
static void checkContent(String... expected) throws IOException {
74+
try (JarFile jf = new JarFile("a.jar")) {
75+
Asserts.assertEquals(new HashSet<>(Arrays.asList(expected)),
76+
jf.stream().map(JarEntry::getName).collect(Collectors.toSet()));
77+
}
78+
}
79+
}

jdk/test/lib/testlibrary/jdk/testlibrary/JarUtils.java

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2015, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2015, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -34,6 +34,7 @@
3434
import java.nio.file.InvalidPathException;
3535
import java.nio.file.Path;
3636
import java.nio.file.Paths;
37+
import java.nio.file.StandardCopyOption;
3738
import java.util.ArrayList;
3839
import java.util.Enumeration;
3940
import java.util.HashMap;
@@ -247,6 +248,57 @@ public static void updateManifest(String src, String dest, Manifest man)
247248
updateJar(src, dest, map);
248249
}
249250

251+
/**
252+
* Remove entries from a ZIP file.
253+
*
254+
* Each entry can be a name or a name ending with "*".
255+
*
256+
* @return number of removed entries
257+
* @throws IOException if there is any I/O error
258+
*/
259+
public static int deleteEntries(Path jarfile, String... patterns)
260+
throws IOException {
261+
Path tmpfile = Files.createTempFile("jar", "jar");
262+
int count = 0;
263+
264+
try (OutputStream out = Files.newOutputStream(tmpfile);
265+
JarOutputStream jos = new JarOutputStream(out)) {
266+
try (JarFile jf = new JarFile(jarfile.toString())) {
267+
Enumeration<JarEntry> jentries = jf.entries();
268+
top: while (jentries.hasMoreElements()) {
269+
JarEntry jentry = jentries.nextElement();
270+
String name = jentry.getName();
271+
for (String pattern : patterns) {
272+
if (pattern.endsWith("*")) {
273+
if (name.startsWith(pattern.substring(
274+
0, pattern.length() - 1))) {
275+
// Go directly to next entry. This
276+
// one is not written into `jos` and
277+
// therefore removed.
278+
count++;
279+
continue top;
280+
}
281+
} else {
282+
if (name.equals(pattern)) {
283+
// Same as above
284+
count++;
285+
continue top;
286+
}
287+
}
288+
}
289+
// No pattern matched, file retained
290+
jos.putNextEntry(copyEntry(jentry));
291+
Utils.transferTo(jf.getInputStream(jentry), jos);
292+
}
293+
}
294+
}
295+
296+
// replace the original JAR file
297+
Files.move(tmpfile, jarfile, StandardCopyOption.REPLACE_EXISTING);
298+
299+
return count;
300+
}
301+
250302
private static void updateEntry(JarOutputStream jos, String name, Object content)
251303
throws IOException {
252304
if (content instanceof Boolean) {
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* Copyright (c) 2024, 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 8309841
27+
* @summary Jarsigner should print a warning if an entry is removed
28+
* @library /lib/testlibrary
29+
*/
30+
31+
import jdk.testlibrary.SecurityTools;
32+
import jdk.testlibrary.JarUtils;
33+
34+
import java.nio.charset.StandardCharsets;
35+
import java.nio.file.Files;
36+
import java.nio.file.Paths;
37+
import java.util.jar.Attributes;
38+
import java.util.jar.Manifest;
39+
40+
public class RemovedFiles {
41+
42+
private static final String NONEXISTENT_ENTRIES_FOUND
43+
= "This jar contains signed entries for files that do not exist. See the -verbose output for more details.";
44+
45+
public static void main(String[] args) throws Exception {
46+
JarUtils.createJarFile(
47+
Paths.get("a.jar"),
48+
Paths.get("."),
49+
Files.write(Paths.get("a"), "a".getBytes(StandardCharsets.UTF_8)),
50+
Files.write(Paths.get("b"), "b".getBytes(StandardCharsets.UTF_8)));
51+
SecurityTools.keytool("-genkeypair -storepass changeit -keystore ks -alias x -dname CN=x -keyalg RSA");
52+
SecurityTools.jarsigner("-storepass changeit -keystore ks a.jar x");
53+
54+
// All is fine at the beginning.
55+
SecurityTools.jarsigner("-verify a.jar")
56+
.shouldNotContain(NONEXISTENT_ENTRIES_FOUND);
57+
58+
// Remove an entry after signing. There will be a warning.
59+
JarUtils.deleteEntries(Paths.get("a.jar"), "a");
60+
SecurityTools.jarsigner("-verify a.jar")
61+
.shouldContain(NONEXISTENT_ENTRIES_FOUND);
62+
SecurityTools.jarsigner("-verify -verbose a.jar")
63+
.shouldContain(NONEXISTENT_ENTRIES_FOUND)
64+
.shouldContain("Warning: nonexistent signed entries: [a]");
65+
66+
// Remove one more entry.
67+
JarUtils.deleteEntries(Paths.get("a.jar"), "b");
68+
SecurityTools.jarsigner("-verify a.jar")
69+
.shouldContain(NONEXISTENT_ENTRIES_FOUND);
70+
SecurityTools.jarsigner("-verify -verbose a.jar")
71+
.shouldContain(NONEXISTENT_ENTRIES_FOUND)
72+
.shouldContain("Warning: nonexistent signed entries: [a, b]");
73+
74+
// Re-sign will not clear the warning.
75+
SecurityTools.jarsigner("-storepass changeit -keystore ks a.jar x");
76+
SecurityTools.jarsigner("-verify a.jar")
77+
.shouldContain(NONEXISTENT_ENTRIES_FOUND);
78+
79+
// Unfortunately, if there is a non-file entry in manifest, there will be
80+
// a false alarm. See https://bugs.openjdk.org/browse/JDK-8334261.
81+
Manifest man = new Manifest();
82+
man.getMainAttributes().putValue("Manifest-Version", "1.0");
83+
man.getEntries().computeIfAbsent("Hello", key -> new Attributes())
84+
.putValue("Foo", "Bar");
85+
JarUtils.createJarFile(Paths.get("b.jar"),
86+
man,
87+
Paths.get("."),
88+
Paths.get("a"));
89+
SecurityTools.jarsigner("-storepass changeit -keystore ks b.jar x");
90+
SecurityTools.jarsigner("-verbose -verify b.jar")
91+
.shouldContain("Warning: nonexistent signed entries: [Hello]")
92+
.shouldContain(NONEXISTENT_ENTRIES_FOUND);
93+
94+
}
95+
}

0 commit comments

Comments
 (0)