Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/tools/jarsigner/Warning.java
38853 views
/*1* Copyright (c) 2015, 2020, 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 jdk.testlibrary.JDKToolLauncher;24import jdk.testlibrary.JarUtils;25import jdk.testlibrary.OutputAnalyzer;26import jdk.testlibrary.ProcessTools;2728import java.nio.file.Files;29import java.nio.file.Paths;30import java.util.Arrays;3132/**33* @test34* @bug 8024302 8026037 813013235* @summary warnings, errors and -strict36* @library /lib/testlibrary37*/38public class Warning {3940public static void main(String[] args) throws Throwable {4142Files.deleteIfExists(Paths.get("ks"));4344newCert("ca", "-validity 365000", "-ext bc:c");4546recreateJar();4748newCert("a");49run("jarsigner", "a.jar a")50.shouldContain("is self-signed");51run("jarsigner", "a.jar a -strict")52.shouldContain("is self-signed")53.shouldHaveExitValue(4);54// Trusted entry can be self-signed without a warning55run("jarsigner", "-verify a.jar")56.shouldNotContain("is self-signed")57.shouldNotContain("not signed by alias in this keystore");58run("keytool", "-delete -alias a");59// otherwise a warning will be shown60run("jarsigner", "-verify a.jar")61.shouldContain("is self-signed")62.shouldContain("not signed by alias in this keystore");6364recreateJar();6566newCert("b");67issueCert("b");68run("jarsigner", "a.jar b")69.shouldNotContain("is self-signed");70run("jarsigner", "-verify a.jar")71.shouldNotContain("is self-signed");7273run("jarsigner", "a.jar b -digestalg MD5")74.shouldContain("-digestalg option is considered a security risk and is disabled.");75run("jarsigner", "a.jar b -digestalg MD5 -strict")76.shouldHaveExitValue(4)77.shouldContain("-digestalg option is considered a security risk and is disabled.");78run("jarsigner", "a.jar b -sigalg MD5withRSA")79.shouldContain("-sigalg option is considered a security risk and is disabled.");8081issueCert("b", "-sigalg MD5withRSA");82run("jarsigner", "a.jar b")83.shouldMatch("chain is invalid. Reason:.*MD5withRSA");8485recreateJar();8687newCert("c", "-keysize 512");88issueCert("c");89run("jarsigner", "a.jar c")90.shouldContain("chain is invalid. " +91"Reason: Algorithm constraints check failed");9293recreateJar();9495newCert("s1"); issueCert("s1", "-startdate 2000/01/01 -validity 36525");96run("jarsigner", "a.jar s1")97.shouldHaveExitValue(0)98.shouldContain("Warning:")99.shouldNotContain("Error:")100.shouldContain("timestamp").shouldContain("2100-01-01")101.shouldNotContain("with signer errors");102run("jarsigner", "a.jar s1 -strict")103.shouldHaveExitValue(0)104.shouldContain("Warning:")105.shouldNotContain("Error:")106.shouldContain("timestamp").shouldContain("2100-01-01")107.shouldNotContain("with signer errors");108run("jarsigner", "a.jar s1 -verify")109.shouldHaveExitValue(0)110.shouldContain("Warning:")111.shouldNotContain("Error:")112.shouldContain("timestamp").shouldContain("2100-01-01")113.shouldNotContain("with signer errors");114run("jarsigner", "a.jar s1 -verify -strict")115.shouldHaveExitValue(0)116.shouldContain("Warning:")117.shouldNotContain("Error:")118.shouldContain("timestamp").shouldContain("2100-01-01")119.shouldNotContain("with signer errors");120121recreateJar();122123newCert("s2"); issueCert("s2", "-validity 100");124run("jarsigner", "a.jar s2")125.shouldHaveExitValue(0)126.shouldContain("Warning:")127.shouldNotContain("Error:")128.shouldContain("timestamp")129.shouldContain("will expire")130.shouldNotContain("with signer errors");131run("jarsigner", "a.jar s2 -strict")132.shouldHaveExitValue(0)133.shouldContain("Warning:")134.shouldNotContain("Error:")135.shouldContain("timestamp")136.shouldContain("will expire")137.shouldNotContain("with signer errors");138run("jarsigner", "a.jar s2 -verify")139.shouldHaveExitValue(0)140.shouldContain("Warning:")141.shouldNotContain("Error:")142.shouldContain("timestamp")143.shouldContain("will expire")144.shouldNotContain("with signer errors");145run("jarsigner", "a.jar s2 -verify -strict")146.shouldHaveExitValue(0)147.shouldContain("Warning:")148.shouldNotContain("Error:")149.shouldContain("timestamp")150.shouldContain("will expire")151.shouldNotContain("with signer errors");152153recreateJar();154155newCert("s3"); issueCert("s3", "-startdate -200d -validity 100");156run("jarsigner", "a.jar s3")157.shouldHaveExitValue(0)158.shouldContain("Warning:")159.shouldContain("has expired")160.shouldNotContain("with signer errors")161.shouldNotContain("Error:");162run("jarsigner", "a.jar s3 -strict")163.shouldHaveExitValue(4)164.shouldContain("with signer errors")165.shouldMatch("(?s).*Error:.*has expired.*Warning:.*");166run("jarsigner", "a.jar s3 -verify")167.shouldHaveExitValue(0)168.shouldContain("Warning:")169.shouldNotContain("with signer errors")170.shouldNotContain("Error:");171run("jarsigner", "a.jar s3 -verify -strict")172.shouldHaveExitValue(4)173.shouldContain("with signer errors")174.shouldMatch("(?s).*Error:.*has expired.*Warning:.*");175}176177// Creates a new jar without signature178static void recreateJar() throws Exception {179JarUtils.createJar("a.jar", "ks");180}181182// Creates a self-signed cert for alias with zero or more -genkey options183static void newCert(String alias, String... more) throws Throwable {184String args = "-genkeypair -alias " + alias + " -dname CN=" + alias;185for (String s: more) {186args += " " + s;187}188run("keytool", args).shouldHaveExitValue(0);189}190191// Asks ca to issue a cert to alias with zero or more -gencert options192static void issueCert(String alias, String...more) throws Throwable {193String req = run("keytool", "-certreq -alias " + alias)194.shouldHaveExitValue(0).getStdout();195String args = "-gencert -alias ca -rfc";196for (String s: more) {197args += " " + s;198}199String cert = run("keytool", args, req)200.shouldHaveExitValue(0).getStdout();201run("keytool", "-import -alias " + alias, cert).shouldHaveExitValue(0);202}203204// Runs a java tool with command line arguments205static OutputAnalyzer run(String command, String args)206throws Throwable {207return run(command, args, null);208}209210// Runs a java tool with command line arguments and an optional input block211static OutputAnalyzer run(String command, String args, String input)212throws Throwable {213JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK(command);214launcher.addVMArg("-Duser.language=en").addVMArg("-Duser.country=US");215switch (command) {216case "keytool":217for (String s: new String[] {218"-keystore", "ks", "-storepass", "changeit",219"-storetype", "jks",220"-keypass", "changeit", "-keyalg", "rsa", "-debug"}) {221launcher.addToolArg(s);222}223break;224case "jarsigner":225for (String s: new String[] {226"-keystore", "ks", "-storepass", "changeit",227"-storetype", "jks"}) {228launcher.addToolArg(s);229}230break;231}232for (String arg: args.split(" ")) {233launcher.addToolArg(arg);234}235String[] cmd = launcher.getCommand();236ProcessBuilder pb = new ProcessBuilder(cmd);237OutputAnalyzer out = ProcessTools.executeProcess(pb, input);238System.out.println("======================");239System.out.println(Arrays.toString(cmd));240String msg = " stdout: [" + out.getStdout() + "];\n"241+ " stderr: [" + out.getStderr() + "]\n"242+ " exitValue = " + out.getExitValue() + "\n";243System.out.println(msg);244return out;245}246}247248249250