Path: blob/master/test/jdk/sun/security/tools/jarsigner/SectionNameContinuedVsLineBreak.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.ByteArrayInputStream;24import java.io.ByteArrayOutputStream;25import java.io.IOException;26import java.nio.file.Files;27import java.nio.file.Path;28import java.util.Map;29import java.util.function.Function;30import java.util.jar.Manifest;31import java.util.jar.JarFile;32import jdk.test.lib.util.JarUtils;33import jdk.test.lib.SecurityTools;34import org.testng.annotations.BeforeTest;35import org.testng.annotations.Test;3637import static java.nio.charset.StandardCharsets.UTF_8;3839/**40* @test41* @bug 821737542* @library /test/lib43* @run testng SectionNameContinuedVsLineBreak44* @summary Checks some specific line break character sequences in section name45* continuation line breaks.46*/47public class SectionNameContinuedVsLineBreak {4849static final String KEYSTORE_FILENAME = "test.jks";5051@BeforeTest52public void prepareCertificate() throws Exception {53SecurityTools.keytool("-genkeypair -keyalg EC -keystore "54+ KEYSTORE_FILENAME + " -storepass changeit -keypass changeit"55+ " -alias a -dname CN=A").shouldHaveExitValue(0);56}5758void manipulateManifestSignAgainA(59String srcJarFilename, String dstJarFilename,60Function<Manifest, byte[]> manifestManipulation) throws Exception {61byte[] manipulatedManifest = manifestManipulation.apply(62new Manifest(new ByteArrayInputStream(63Utils.readJarManifestBytes(srcJarFilename))));64Utils.echoManifest(manipulatedManifest, "manipulated manifest");65JarUtils.updateJar(srcJarFilename, dstJarFilename, Map.of(66JarFile.MANIFEST_NAME, manipulatedManifest));67SecurityTools.jarsigner("-keystore " + KEYSTORE_FILENAME +68" -storepass changeit -verbose -debug " + dstJarFilename + " a")69.shouldHaveExitValue(0);70Utils.echoManifest(Utils.readJarManifestBytes(71dstJarFilename), "manipulated jar signed again with a");72// check assumption that jar is valid at this point73SecurityTools.jarsigner("-verify -keystore " + KEYSTORE_FILENAME +74" -storepass changeit -verbose -debug " + dstJarFilename + " a")75.shouldHaveExitValue(0);76}7778void test(String name, Function<Manifest, byte[]> manifestManipulation,79String jarContentFilename) throws Exception {80String jarFilename1 = "test-" + name + "-step1.jar";81Files.write(Path.of(jarContentFilename),82jarContentFilename.getBytes(UTF_8));83JarUtils.createJarFile(Path.of(jarFilename1), (Manifest)84/* no manifest will let jarsigner create a default one */ null,85Path.of("."), Path.of(jarContentFilename));86SecurityTools.jarsigner("-keystore " + KEYSTORE_FILENAME +87" -storepass changeit -verbose -debug " + jarFilename1 +88" a").shouldHaveExitValue(0);89Utils.echoManifest(Utils.readJarManifestBytes(90jarFilename1), "signed jar");91String jarFilename2 = "test-" + name + "-step2.jar";92manipulateManifestSignAgainA(jarFilename1, jarFilename2,93manifestManipulation);9495SecurityTools.jarsigner("-verify -strict -keystore " +96KEYSTORE_FILENAME + " -storepass changeit -debug -verbose " +97jarFilename2 + " a").shouldHaveExitValue(0);98}99100/**101* Test signing a jar with a manifest that has an entry the name of102* which continued on a continuation line with '\r' as line break before103* the continuation line space ' ' on the line the name starts.104*/105@Test106public void testContinueNameAfterCr() throws Exception {107String filename = "abc";108test("testContinueNameAfterCr", m -> {109String digest = m.getAttributes("abc").getValue("SHA-256-Digest");110m.getEntries().remove("abc");111return (manifestToString(m)112+ "Name: a\r"113+ " bc\r\n"114+ "SHA-256-Digest: " + digest + "\r\n"115+ "\r\n").getBytes(UTF_8);116}, filename);117}118119/**120* Test signing a jar with a manifest that has an entry the name of121* which continued on a continuation line with '\r' as line break before122* the continuation line space ' ' after a first continuation.123*/124@Test125public void testContinueNameAfterCrOnContinuationLine() throws Exception {126String filename = "abc";127test("testContinueNameAfterCr", m -> {128String digest = m.getAttributes("abc").getValue("SHA-256-Digest");129m.getEntries().remove("abc");130return (manifestToString(m)131+ "Name: a\r\n"132+ " b\r"133+ " c\r\n"134+ "SHA-256-Digest: " + digest + "\r\n"135+ "\r\n").getBytes(UTF_8);136}, filename);137}138139/**140* Test signing a jar with a manifest that has an entry the name of141* which continued on a continuation line and terminated with '\r' as line142* break after the name.143*/144@Test145public void testEndNameWithCrOnContinuationLine() throws Exception {146String filename = "abc";147test("testContinueNameAfterCr", m -> {148String digest = m.getAttributes("abc").getValue("SHA-256-Digest");149m.getEntries().remove("abc");150return (manifestToString(m)151+ "Name: a\r\n"152+ " bc\r"153+ "SHA-256-Digest: " + digest + "\r\n"154+ "\r\n").getBytes(UTF_8);155}, filename);156}157158String manifestToString(Manifest mf) {159try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {160mf.write(out);161return out.toString(UTF_8);162} catch (IOException e) {163throw new RuntimeException(e);164}165}166167}168169170