|
| 1 | +/* |
| 2 | + * Copyright (c) 2022, 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 8278851 |
| 27 | + * @summary Check that jar entry with at least one non-disabled digest |
| 28 | + * algorithm in manifest is treated as signed |
| 29 | + * @library /lib/testlibrary /lib/security |
| 30 | + * @build jdk.testlibrary.JarUtils |
| 31 | + * jdk.testlibrary.Utils |
| 32 | + * @run main/othervm JarWithOneNonDisabledDigestAlg |
| 33 | + */ |
| 34 | + |
| 35 | +import java.io.InputStream; |
| 36 | +import java.io.FileInputStream; |
| 37 | +import java.io.FileOutputStream; |
| 38 | +import java.nio.file.Files; |
| 39 | +import java.nio.file.Path; |
| 40 | +import java.nio.file.Paths; |
| 41 | +import java.nio.file.StandardCopyOption; |
| 42 | +import java.security.CodeSigner; |
| 43 | +import java.security.KeyStore; |
| 44 | +import java.util.Arrays; |
| 45 | +import java.util.Enumeration; |
| 46 | +import java.util.List; |
| 47 | +import java.util.Locale; |
| 48 | +import java.util.jar.JarEntry; |
| 49 | +import java.util.jar.JarFile; |
| 50 | +import java.util.zip.ZipFile; |
| 51 | + |
| 52 | +import jdk.testlibrary.JarUtils; |
| 53 | + |
| 54 | +public class JarWithOneNonDisabledDigestAlg { |
| 55 | + |
| 56 | + private static final String PASS = "changeit"; |
| 57 | + private static final String TESTFILE1 = "testfile1"; |
| 58 | + private static final String TESTFILE2 = "testfile2"; |
| 59 | + |
| 60 | + public static void main(String[] args) throws Exception { |
| 61 | + SecurityUtils.removeFromDisabledAlgs("jdk.jar.disabledAlgorithms", |
| 62 | + Arrays.asList("SHA1")); |
| 63 | + Files.write(Paths.get(TESTFILE1), TESTFILE1.getBytes()); |
| 64 | + JarUtils.createJarFile(Paths.get("unsigned.jar"), Paths.get("."), |
| 65 | + Paths.get(TESTFILE1)); |
| 66 | + |
| 67 | + genkeypair("-alias SHA1 -sigalg SHA1withRSA"); |
| 68 | + genkeypair("-alias SHA256 -sigalg SHA256withRSA"); |
| 69 | + |
| 70 | + // Sign JAR twice with same signer but different digest algorithms |
| 71 | + // so that each entry in manifest file contains two digest values. |
| 72 | + signJarFile("SHA1", "MD5", "unsigned.jar", "signed.jar"); |
| 73 | + signJarFile("SHA1", "SHA1", "signed.jar", "signed2.jar"); |
| 74 | + checkThatJarIsSigned("signed2.jar", false); |
| 75 | + |
| 76 | + // add another file to the JAR |
| 77 | + Files.write(Paths.get(TESTFILE2), "testFile2".getBytes()); |
| 78 | + JarUtils.updateJar("signed2.jar", "signed3.jar", TESTFILE2); |
| 79 | + Files.move(Paths.get("signed3.jar"), Paths.get("signed2.jar"), StandardCopyOption.REPLACE_EXISTING); |
| 80 | + |
| 81 | + // Sign again with different signer (SHA256) and SHA-1 digestalg. |
| 82 | + // TESTFILE1 should have two signers and TESTFILE2 should have one |
| 83 | + // signer. |
| 84 | + signJarFile("SHA256", "SHA1", "signed2.jar", "multi-signed.jar"); |
| 85 | + |
| 86 | + checkThatJarIsSigned("multi-signed.jar", true); |
| 87 | + } |
| 88 | + |
| 89 | + private static KeyStore.PrivateKeyEntry getEntry(KeyStore ks, String alias) |
| 90 | + throws Exception { |
| 91 | + |
| 92 | + return (KeyStore.PrivateKeyEntry) |
| 93 | + ks.getEntry(alias, |
| 94 | + new KeyStore.PasswordProtection(PASS.toCharArray())); |
| 95 | + } |
| 96 | + |
| 97 | + private static void genkeypair(String cmd) throws Exception { |
| 98 | + cmd = "-genkeypair -keystore keystore -storepass " + PASS + |
| 99 | + " -keypass " + PASS + " -keyalg rsa -dname CN=Duke " + cmd; |
| 100 | + sun.security.tools.keytool.Main.main(cmd.split(" ")); |
| 101 | + } |
| 102 | + |
| 103 | + private static void signJarFile(String alias, |
| 104 | + String digestAlg, String inputFile, String outputFile) |
| 105 | + throws Exception { |
| 106 | + |
| 107 | + // create a backup file as the signed file will be in-place |
| 108 | + Path from = Paths.get(inputFile); |
| 109 | + Path backup = Files.createTempFile(inputFile, "jar"); |
| 110 | + Path output = Paths.get(outputFile); |
| 111 | + |
| 112 | + Files.copy(from, backup, StandardCopyOption.REPLACE_EXISTING); |
| 113 | + |
| 114 | + // sign the jar |
| 115 | + sun.security.tools.jarsigner.Main.main( |
| 116 | + ("-keystore keystore -storepass " + PASS + |
| 117 | + " -digestalg " + digestAlg + " " + inputFile + " " + |
| 118 | + alias).split(" ")); |
| 119 | + |
| 120 | + Files.copy(from, output, StandardCopyOption.REPLACE_EXISTING); |
| 121 | + Files.copy(backup, from, StandardCopyOption.REPLACE_EXISTING); |
| 122 | + } |
| 123 | + |
| 124 | + private static void checkThatJarIsSigned(String jarFile, boolean multi) |
| 125 | + throws Exception { |
| 126 | + |
| 127 | + try (JarFile jf = new JarFile(jarFile, true)) { |
| 128 | + Enumeration<JarEntry> entries = jf.entries(); |
| 129 | + while (entries.hasMoreElements()) { |
| 130 | + JarEntry entry = entries.nextElement(); |
| 131 | + if (entry.isDirectory() || isSigningRelated(entry.getName())) { |
| 132 | + continue; |
| 133 | + } |
| 134 | + InputStream is = jf.getInputStream(entry); |
| 135 | + while (is.read() != -1); |
| 136 | + CodeSigner[] signers = entry.getCodeSigners(); |
| 137 | + if (signers == null) { |
| 138 | + throw new Exception("JarEntry " + entry.getName() + |
| 139 | + " is not signed"); |
| 140 | + } else if (multi) { |
| 141 | + if (entry.getName().equals(TESTFILE1) && |
| 142 | + signers.length != 2) { |
| 143 | + throw new Exception("Unexpected number of signers " + |
| 144 | + "for " + entry.getName() + ": " + signers.length); |
| 145 | + } else if (entry.getName().equals(TESTFILE2) && |
| 146 | + signers.length != 1) { |
| 147 | + throw new Exception("Unexpected number of signers " + |
| 148 | + "for " + entry.getName() + ": " + signers.length); |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + private static boolean isSigningRelated(String name) { |
| 156 | + name = name.toUpperCase(Locale.ENGLISH); |
| 157 | + if (!name.startsWith("META-INF/")) { |
| 158 | + return false; |
| 159 | + } |
| 160 | + name = name.substring(9); |
| 161 | + if (name.indexOf('/') != -1) { |
| 162 | + return false; |
| 163 | + } |
| 164 | + return name.endsWith(".SF") |
| 165 | + || name.endsWith(".DSA") |
| 166 | + || name.endsWith(".RSA") |
| 167 | + || name.endsWith(".EC") |
| 168 | + || name.equals("MANIFEST.MF"); |
| 169 | + } |
| 170 | +} |
0 commit comments