Path: blob/master/test/jdk/sun/security/tools/jarsigner/DigestDontIgnoreCase.java
41152 views
/*1* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.io.IOException;24import java.io.OutputStream;25import java.nio.file.Files;26import java.nio.file.Path;27import java.util.Arrays;28import java.util.Map;29import java.util.jar.JarFile;30import java.util.jar.JarOutputStream;31import java.util.jar.JarEntry;32import jdk.test.lib.SecurityTools;33import org.testng.annotations.Test;34import org.testng.annotations.BeforeClass;3536import static java.nio.charset.StandardCharsets.UTF_8;3738/**39* @test40* @bug 821737541* @library /test/lib42* @run testng DigestDontIgnoreCase43* @summary Check that existing manifest digest entries are taken for valid44* only if they match the actual digest value also taking upper and lower45* case of the base64 encoded form of the digests into account.46*/47/*48* <pre>mfDigest.equalsIgnoreCase(base64Digests[i])</pre>49* previously in JarSigner.java on on line 98550* @see jdk.security.jarsigner.JarSigner#updateDigests51*/52public class DigestDontIgnoreCase {5354static final String KEYSTORE_FILENAME = "test.jks";5556static final String DUMMY_FILE1 = "dummy1.txt";57static final byte[] DUMMY_CONTENTS1 = DUMMY_FILE1.getBytes(UTF_8);58static final String DUMMY_FILE2 = "dummy2.txt";59static final byte[] DUMMY_CONTENTS2 = DUMMY_FILE2.getBytes(UTF_8);6061byte[] goodSignedManifest;6263@BeforeClass64public void prepareCertificate() throws Exception {65SecurityTools.keytool("-genkeypair -keyalg DSA -keystore "66+ KEYSTORE_FILENAME + " -storepass changeit -keypass changeit"67+ " -alias a -dname CN=X").shouldHaveExitValue(0);68}6970void prepareJarFile(String filename, Map<String, byte[]> contents)71throws IOException {72try (OutputStream out = Files.newOutputStream(Path.of(filename));73JarOutputStream jos = new JarOutputStream(out)) {74for (Map.Entry<String, byte[]> entry : contents.entrySet()) {75JarEntry je = new JarEntry(entry.getKey());76jos.putNextEntry(je);77jos.write(entry.getValue());78jos.closeEntry();79}80}81}8283@BeforeClass(dependsOnMethods = "prepareCertificate")84public void prepareGoodSignedManifest() throws Exception {85String filename = "prepare.jar";86prepareJarFile(filename, Map.of(DUMMY_FILE1, DUMMY_CONTENTS1));87SecurityTools.jarsigner("-keystore " + KEYSTORE_FILENAME +88" -storepass changeit -verbose -debug " + filename + " a")89.shouldHaveExitValue(0);90goodSignedManifest = Utils.readJarManifestBytes(filename);91Utils.echoManifest(goodSignedManifest,92"reference manifest with one file signed");93}9495void testWithManifest(String filename, byte[] manifestBytes)96throws Exception {97Utils.echoManifest(manifestBytes,98"going to test " + filename + " with manifest");99prepareJarFile(filename, Map.of(100JarFile.MANIFEST_NAME, manifestBytes,101DUMMY_FILE1, DUMMY_CONTENTS1, // with digest already in manifest102DUMMY_FILE2, DUMMY_CONTENTS2)); // causes manifest update103Utils.echoManifest(Utils.readJarManifestBytes(filename),104filename + " created with manifest");105SecurityTools.jarsigner("-keystore " + KEYSTORE_FILENAME +106" -storepass changeit -debug -verbose " + filename + " a")107.shouldHaveExitValue(0);108Utils.echoManifest(Utils.readJarManifestBytes(filename),109filename + " signed resulting in manifest");110SecurityTools.jarsigner("-verify -strict -keystore " +111KEYSTORE_FILENAME + " -storepass changeit -debug -verbose " +112filename + " a").shouldHaveExitValue(0);113}114115@Test116public void verifyDigestGoodCase() throws Exception {117testWithManifest("good.jar", goodSignedManifest);118}119120@Test121public void testDigestHeaderNameCase() throws Exception {122byte[] mfBadHeader = new String(goodSignedManifest, UTF_8).123replace("SHA-256-Digest", "sha-256-dIGEST").getBytes(UTF_8);124testWithManifest("switch-header-name-case.jar", mfBadHeader);125}126127@Test128public void testDigestWrongCase() throws Exception {129byte[] mfBadDigest = switchCase(goodSignedManifest, "Digest");130testWithManifest("switch-digest-case.jar", mfBadDigest);131}132133byte[] switchCase(byte[] manifest, String attrName) {134byte[] wrongCase = Arrays.copyOf(manifest, manifest.length);135byte[] name = (attrName + ":").getBytes(UTF_8);136int matched = 0; // number of bytes before position i matching attrName137for (int i = 0; i < wrongCase.length; i++) {138if (wrongCase[i] == '\r' &&139(i == wrongCase.length - 1 || wrongCase[i + 1] == '\n')) {140continue;141} else if ((wrongCase[i] == '\r' || wrongCase[i] == '\n')142&& (i == wrongCase.length - 1 || wrongCase[i + 1] != ' ')) {143matched = 0;144} else if (matched == name.length) {145wrongCase[i] = switchCase(wrongCase[i]);146} else if (name[matched] == wrongCase[i]) {147matched++;148} else {149matched = 0;150}151}152return wrongCase;153}154155byte switchCase(byte c) {156if (c >= 'A' && c <= 'Z') {157return (byte) ('a' + (c - 'A'));158} else if (c >= 'a' && c <= 'z') {159return (byte) ('A' + (c - 'a'));160} else {161return c;162}163}164165}166167168