Skip to content

Commit

Permalink
the change
Browse files Browse the repository at this point in the history
  • Loading branch information
wangweij committed Jun 13, 2024
1 parent 5230786 commit c94a34f
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -197,6 +197,7 @@ public ExitException(int errorCode) {
private boolean hasExpiringCert = false;
private boolean hasExpiringTsaCert = false;
private boolean noTimestamp = true;
private boolean removedEntriesFound = false;

// Expiration date. The value could be null if signed by a trusted cert.
private Date expireDate = null;
Expand Down Expand Up @@ -735,6 +736,7 @@ void verifyJar(String jarName)
Map<String,PKCS7> sigMap = new HashMap<>();
Map<String,String> sigNameMap = new HashMap<>();
Map<String,String> unparsableSignatures = new HashMap<>();
Map<String,Set<String>> entriesInSF = new HashMap<>();

try {
jf = new JarFile(jarName, true);
Expand Down Expand Up @@ -782,6 +784,7 @@ void verifyJar(String jarName)
break;
}
}
entriesInSF.put(alias, sf.getEntries().keySet());
if (!found) {
unparsableSignatures.putIfAbsent(alias,
String.format(
Expand Down Expand Up @@ -881,6 +884,9 @@ void verifyJar(String jarName)
sb.append('\n');
}
}
for (var signed : entriesInSF.values()) {
signed.remove(name);
}
} else if (showcerts && !verbose.equals("all")) {
// Print no info for unsigned entries when -verbose:all,
// to be consistent with old behavior.
Expand Down Expand Up @@ -1076,6 +1082,13 @@ void verifyJar(String jarName)
if (verbose != null) {
System.out.println(history);
}
var signed = entriesInSF.get(s);
if (!signed.isEmpty()) {
if (verbose != null) {
System.out.println(rb.getString("history.removed.entries") + signed);
}
removedEntriesFound = true;
}
} else {
unparsableSignatures.putIfAbsent(s, String.format(
rb.getString("history.nobk"), s));
Expand Down Expand Up @@ -1311,6 +1324,9 @@ private void displayMessagesAndResult(boolean isSigning) {
}
}

if (removedEntriesFound) {
warnings.add(rb.getString("removed.entries.found"));
}
if (extraAttrsDetected) {
warnings.add(rb.getString("extra.attributes.detected"));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -164,6 +164,7 @@ public class Resources extends java.util.ListResourceBundle {

{"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"},
{"history.without.ts", "- Signed by \"%1$s\"\n Digest algorithm: %2$s\n Signature algorithm: %3$s, %4$s"},
{"history.removed.entries", " Warning: nonexistent signed entries: "},
{"history.unparsable", "- Unparsable signature-related file %s"},
{"history.nosf", "- Missing signature-related file META-INF/%s.SF"},
{"history.nobk", "- Missing block file for signature-related file META-INF/%s.SF"},
Expand All @@ -178,6 +179,7 @@ public class Resources extends java.util.ListResourceBundle {
{"key.bit.disabled", "%d-bit key (disabled)"},
{"key.bit.eccurve.disabled", "%1$d-bit %2$s key (disabled)"},
{"unknown.size", "unknown size"},
{"removed.entries.found", "Nonexistent signed entries detected. See details in -verbose output."},
{"extra.attributes.detected", "POSIX file permission and/or symlink attributes detected. These attributes are ignored when signing and are not protected by the signature."},

{"jarsigner.", "jarsigner: "},
Expand Down
64 changes: 64 additions & 0 deletions test/jdk/sun/security/tools/jarsigner/RemovedFiles.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

/*
* @test
* @bug 8309841
* @summary Jarsigner should print a warning if an entry is removed
* @library /test/lib
*/

import jdk.test.lib.SecurityTools;
import jdk.test.lib.util.JarUtils;

import java.nio.file.Files;
import java.nio.file.Path;

public class RemovedFiles {
public static void main(String[] args) throws Exception {
JarUtils.createJarFile(
Path.of("a.jar"),
Path.of("."),
Files.writeString(Path.of("a"), "a"),
Files.writeString(Path.of("b"), "b"));
SecurityTools.keytool("-genkeypair -storepass changeit -keystore ks -alias x -dname CN=x -keyalg ed25519");
SecurityTools.jarsigner("-storepass changeit -keystore ks a.jar x");

// All is fine at the beginning.
SecurityTools.jarsigner("-verify a.jar")
.shouldNotContain("Nonexistent signed entries detected. See details in -verbose output.");

// Remove an entry after signing. There will be a warning.
JarUtils.deleteEntries(Path.of("a.jar"), "a");
SecurityTools.jarsigner("-verify a.jar")
.shouldContain("Nonexistent signed entries detected. See details in -verbose output.");
SecurityTools.jarsigner("-verify -verbose a.jar")
.shouldContain("Nonexistent signed entries detected. See details in -verbose output.")
.shouldContain("Warning: nonexistent signed entries: [a]");

// Re-sign will not clear the warning.
SecurityTools.jarsigner("-storepass changeit -keystore ks a.jar x");
SecurityTools.jarsigner("-verify a.jar")
.shouldContain("Nonexistent signed entries detected. See details in -verbose output.");
}
}
41 changes: 40 additions & 1 deletion test/lib/jdk/test/lib/util/JarUtils.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -320,6 +320,45 @@ public static void updateManifest(String src, String dest, Manifest man)
updateJar(src, dest, Map.of(JarFile.MANIFEST_NAME, bout.toByteArray()));
}

/**
* Remove entries from a ZIP file.
*
* Each entry can be a name or a name ending with "*".
*/
public static void deleteEntries(Path jarfile, String... patterns)
throws IOException
{
Path tmpfile = Files.createTempFile("jar", "jar");

try (OutputStream out = Files.newOutputStream(tmpfile);
JarOutputStream jos = new JarOutputStream(out)) {
try (JarFile jf = new JarFile(jarfile.toString())) {
Enumeration<JarEntry> jentries = jf.entries();
top: while (jentries.hasMoreElements()) {
JarEntry jentry = jentries.nextElement();
String name = jentry.getName();
for (String pattern : patterns) {
if (pattern.endsWith("*")) {
if (name.startsWith(pattern.substring(
0, pattern.length() - 1))) {
continue top;
}
} else {
if (name.equals(pattern)) {
continue top;
}
}
}
jos.putNextEntry(copyEntry(jentry));
jf.getInputStream(jentry).transferTo(jos);
}
}
}

// replace the original JAR file
Files.move(tmpfile, jarfile, StandardCopyOption.REPLACE_EXISTING);
}

private static void updateEntry(JarOutputStream jos, String name, Object content)
throws IOException {
if (content instanceof Boolean) {
Expand Down

0 comments on commit c94a34f

Please sign in to comment.