Path: blob/master/test/jdk/sun/security/tools/jarsigner/LineBrokenMultiByteCharacter.java
41152 views
/*1* Copyright (c) 2017, 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*/2223/*24* @test25* @bug 669540226* @summary verify signatures of jars containing classes with names27* with multi-byte unicode characters broken across lines28* @library /test/lib29*/3031import java.io.IOException;32import java.io.InputStream;33import java.nio.file.Files;34import java.nio.file.Paths;35import java.util.jar.JarFile;36import java.util.jar.Attributes.Name;37import java.util.jar.JarEntry;3839import static java.nio.charset.StandardCharsets.UTF_8;4041import jdk.test.lib.SecurityTools;42import jdk.test.lib.util.JarUtils;4344public class LineBrokenMultiByteCharacter {4546/**47* this name will break across lines in MANIFEST.MF at the48* middle of a two-byte utf-8 encoded character due to its e acute letter49* at its exact position.50*51* because no file with such a name exists {@link JarUtils} will add the52* name itself as contents to the jar entry which would have contained a53* compiled class in the original bug. For this test, the contents of the54* files contained in the jar file is not important as long as they get55* signed.56*57* @see #verifyClassNameLineBroken(JarFile, String)58*/59static final String testClassName =60"LineBrokenMultiByteCharacterA1234567890B1234567890C123456789D1234\u00E9xyz.class";6162static final String anotherName =63"LineBrokenMultiByteCharacterA1234567890B1234567890C123456789D1234567890.class";6465static final String alias = "a";66static final String keystoreFileName = "test.jks";67static final String manifestFileName = "MANIFEST.MF";6869public static void main(String[] args) throws Exception {70prepare();7172testSignJar("test.jar");73testSignJarNoManifest("test-no-manifest.jar");74testSignJarUpdate("test-update.jar", "test-updated.jar");75}7677static void prepare() throws Exception {78SecurityTools.keytool("-keystore", keystoreFileName, "-genkeypair",79"-keyalg", "dsa",80"-storepass", "changeit", "-keypass", "changeit", "-storetype",81"JKS", "-alias", alias, "-dname", "CN=X", "-validity", "366")82.shouldHaveExitValue(0);8384Files.write(Paths.get(manifestFileName), (Name.85MANIFEST_VERSION.toString() + ": 1.0\r\n").getBytes(UTF_8));86}8788static void testSignJar(String jarFileName) throws Exception {89JarUtils.createJar(jarFileName, manifestFileName, testClassName);90verifyJarSignature(jarFileName);91}9293static void testSignJarNoManifest(String jarFileName) throws Exception {94JarUtils.createJar(jarFileName, testClassName);95verifyJarSignature(jarFileName);96}9798static void testSignJarUpdate(99String initialFileName, String updatedFileName) throws Exception {100JarUtils.createJar(initialFileName, manifestFileName, anotherName);101SecurityTools.jarsigner("-keystore", keystoreFileName, "-storetype",102"JKS", "-storepass", "changeit", "-debug", initialFileName,103alias).shouldHaveExitValue(0);104JarUtils.updateJar(initialFileName, updatedFileName, testClassName);105verifyJarSignature(updatedFileName);106}107108static void verifyJarSignature(String jarFileName) throws Exception {109// actually sign the jar110SecurityTools.jarsigner("-keystore", keystoreFileName, "-storetype",111"JKS", "-storepass", "changeit", "-debug", jarFileName, alias)112.shouldHaveExitValue(0);113114try (115JarFile jar = new JarFile(jarFileName);116) {117verifyClassNameLineBroken(jar, testClassName);118verifyCodeSigners(jar, jar.getJarEntry(testClassName));119}120}121122/**123* it would be too easy to miss the actual test case by just renaming an124* identifier so that the multi-byte encoded character would not any longer125* be broken across a line break.126*127* this check here verifies that the actual test case is tested based on128* the manifest and not based on the signature file because at the moment,129* the signature file does not even contain the desired entry at all.130*131* this relies on {@link java.util.jar.Manifest} breaking lines unaware132* of bytes that belong to the same multi-byte utf characters.133*/134static void verifyClassNameLineBroken(JarFile jar, String className)135throws IOException {136byte[] eAcute = "\u00E9".getBytes(UTF_8);137byte[] eAcuteBroken =138new byte[] {eAcute[0], '\r', '\n', ' ', eAcute[1]};139140if (jar.getManifest().getAttributes(className) == null) {141throw new AssertionError(className + " not found in manifest");142}143144JarEntry manifestEntry = jar.getJarEntry(JarFile.MANIFEST_NAME);145try (146InputStream manifestIs = jar.getInputStream(manifestEntry);147) {148int bytesMatched = 0;149for (int b = manifestIs.read(); b > -1; b = manifestIs.read()) {150if ((byte) b == eAcuteBroken[bytesMatched]) {151bytesMatched++;152if (bytesMatched == eAcuteBroken.length) {153break;154}155} else {156bytesMatched = 0;157}158}159if (bytesMatched < eAcuteBroken.length) {160throw new AssertionError("self-test failed: multi-byte "161+ "utf-8 character not broken across lines");162}163}164}165166static void verifyCodeSigners(JarFile jar, JarEntry jarEntry)167throws IOException {168// codeSigners is initialized only after the entry has been read169try (170InputStream inputStream = jar.getInputStream(jarEntry);171) {172inputStream.readAllBytes();173}174175// a check for the presence of code signers is sufficient to check176// bug JDK-6695402. no need to also verify the actual code signers177// attributes here.178if (jarEntry.getCodeSigners() == null179|| jarEntry.getCodeSigners().length == 0) {180throw new AssertionError(181"no signing certificate found for " + jarEntry.getName());182}183}184185}186187188