|
| 1 | +package io.quarkus.deployment.pkg; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.io.InputStream; |
| 5 | +import java.nio.file.Files; |
| 6 | +import java.nio.file.Path; |
| 7 | +import java.util.Enumeration; |
| 8 | +import java.util.Map; |
| 9 | +import java.util.function.Predicate; |
| 10 | +import java.util.jar.Attributes; |
| 11 | +import java.util.jar.JarEntry; |
| 12 | +import java.util.jar.JarFile; |
| 13 | +import java.util.jar.JarOutputStream; |
| 14 | +import java.util.jar.Manifest; |
| 15 | + |
| 16 | +import org.jboss.logging.Logger; |
| 17 | + |
| 18 | +/** |
| 19 | + * JarUnsigner is used to remove the signature from a jar file. |
| 20 | + */ |
| 21 | +public final class JarUnsigner { |
| 22 | + |
| 23 | + private static final Logger log = Logger.getLogger(JarUnsigner.class); |
| 24 | + |
| 25 | + private JarUnsigner() { |
| 26 | + // utility class |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * Unsigns a jar file by removing the signature entries. |
| 31 | + * If the JAR is not signed, it will simply copy the original JAR to the target path. |
| 32 | + * |
| 33 | + * @param jarPath the path to the jar file to unsign |
| 34 | + * @param targetPath the path to the target jar file |
| 35 | + * @throws IOException if an I/O error occurs |
| 36 | + */ |
| 37 | + public static void unsignJar(Path jarPath, Path targetPath) throws IOException { |
| 38 | + try (JarFile in = new JarFile(jarPath.toFile(), false)) { |
| 39 | + Manifest manifest = in.getManifest(); |
| 40 | + boolean signed; |
| 41 | + if (manifest != null) { |
| 42 | + Map<String, Attributes> entries = manifest.getEntries(); |
| 43 | + signed = !entries.isEmpty(); |
| 44 | + // Remove signature entries |
| 45 | + entries.clear(); |
| 46 | + } else { |
| 47 | + signed = false; |
| 48 | + manifest = new Manifest(); |
| 49 | + } |
| 50 | + if (!signed) { |
| 51 | + in.close(); |
| 52 | + log.debugf("JAR %s is not signed, skipping unsigning", jarPath); |
| 53 | + Files.copy(jarPath, targetPath, java.nio.file.StandardCopyOption.REPLACE_EXISTING); |
| 54 | + } else { |
| 55 | + log.debugf("JAR %s is signed, removing signature", jarPath); |
| 56 | + // Reusing buffer for performance reasons |
| 57 | + byte[] buffer = new byte[10000]; |
| 58 | + try (JarOutputStream out = new JarOutputStream(Files.newOutputStream(targetPath))) { |
| 59 | + JarEntry manifestEntry = new JarEntry(JarFile.MANIFEST_NAME); |
| 60 | + // Set manifest time to epoch to always make the same jar |
| 61 | + manifestEntry.setTime(0); |
| 62 | + out.putNextEntry(manifestEntry); |
| 63 | + manifest.write(out); |
| 64 | + out.closeEntry(); |
| 65 | + Enumeration<JarEntry> entries = in.entries(); |
| 66 | + while (entries.hasMoreElements()) { |
| 67 | + JarEntry entry = entries.nextElement(); |
| 68 | + String entryName = entry.getName(); |
| 69 | + if (!entryName.equals(JarFile.MANIFEST_NAME) |
| 70 | + && !entryName.equals("META-INF/INDEX.LIST") |
| 71 | + && !isSignatureFile(entryName)) { |
| 72 | + entry.setCompressedSize(-1); |
| 73 | + out.putNextEntry(entry); |
| 74 | + try (InputStream inStream = in.getInputStream(entry)) { |
| 75 | + int r; |
| 76 | + while ((r = inStream.read(buffer)) > 0) { |
| 77 | + out.write(buffer, 0, r); |
| 78 | + } |
| 79 | + } finally { |
| 80 | + out.closeEntry(); |
| 81 | + } |
| 82 | + } else { |
| 83 | + log.debugf("Removed %s from %s", entryName, jarPath); |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + // let's make sure we keep the original timestamp |
| 89 | + Files.setLastModifiedTime(targetPath, Files.getLastModifiedTime(jarPath)); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Unsigns a jar file by removing the signature entries. |
| 95 | + * |
| 96 | + * @param jarPath the path to the jar file to unsign |
| 97 | + * @param targetPath the path to the target jar file |
| 98 | + * @param includePredicate a predicate to determine which entries to include in the target jar |
| 99 | + * @throws IOException if an I/O error occurs |
| 100 | + */ |
| 101 | + public static void unsignJar(Path jarPath, Path targetPath, Predicate<String> includePredicate) throws IOException { |
| 102 | + // Reusing buffer for performance reasons |
| 103 | + byte[] buffer = new byte[10000]; |
| 104 | + try (JarFile in = new JarFile(jarPath.toFile(), false)) { |
| 105 | + Manifest manifest = in.getManifest(); |
| 106 | + boolean signed; |
| 107 | + if (manifest != null) { |
| 108 | + Map<String, Attributes> entries = manifest.getEntries(); |
| 109 | + signed = !entries.isEmpty(); |
| 110 | + // Remove signature entries |
| 111 | + entries.clear(); |
| 112 | + } else { |
| 113 | + signed = false; |
| 114 | + manifest = new Manifest(); |
| 115 | + } |
| 116 | + if (signed) { |
| 117 | + log.debugf("JAR %s is signed, removing signature", jarPath); |
| 118 | + } |
| 119 | + try (JarOutputStream out = new JarOutputStream(Files.newOutputStream(targetPath))) { |
| 120 | + JarEntry manifestEntry = new JarEntry(JarFile.MANIFEST_NAME); |
| 121 | + // Set manifest time to epoch to always make the same jar |
| 122 | + manifestEntry.setTime(0); |
| 123 | + out.putNextEntry(manifestEntry); |
| 124 | + manifest.write(out); |
| 125 | + out.closeEntry(); |
| 126 | + Enumeration<JarEntry> entries = in.entries(); |
| 127 | + while (entries.hasMoreElements()) { |
| 128 | + JarEntry entry = entries.nextElement(); |
| 129 | + String entryName = entry.getName(); |
| 130 | + if (includePredicate.test(entryName) |
| 131 | + && !entryName.equals(JarFile.MANIFEST_NAME) |
| 132 | + && !entryName.equals("META-INF/INDEX.LIST") |
| 133 | + && !isSignatureFile(entryName)) { |
| 134 | + entry.setCompressedSize(-1); |
| 135 | + out.putNextEntry(entry); |
| 136 | + try (InputStream inStream = in.getInputStream(entry)) { |
| 137 | + int r; |
| 138 | + while ((r = inStream.read(buffer)) > 0) { |
| 139 | + out.write(buffer, 0, r); |
| 140 | + } |
| 141 | + } finally { |
| 142 | + out.closeEntry(); |
| 143 | + } |
| 144 | + } else { |
| 145 | + log.debugf("Removed %s from %s", entryName, jarPath); |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + // let's make sure we keep the original timestamp |
| 150 | + Files.setLastModifiedTime(targetPath, Files.getLastModifiedTime(jarPath)); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + private static boolean isSignatureFile(String entry) { |
| 155 | + entry = entry.toUpperCase(); |
| 156 | + if (entry.startsWith("META-INF/") && entry.indexOf('/', "META-INF/".length()) == -1) { |
| 157 | + return entry.endsWith(".SF") |
| 158 | + || entry.endsWith(".DSA") |
| 159 | + || entry.endsWith(".RSA") |
| 160 | + || entry.endsWith(".EC"); |
| 161 | + } |
| 162 | + return false; |
| 163 | + } |
| 164 | +} |
0 commit comments