Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/tools/jarsigner/warnings/Test.java
38860 views
/*1* Copyright (c) 2013, 2018, 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.OutputAnalyzer;24import jdk.testlibrary.ProcessTools;2526import java.util.ArrayList;27import java.util.Arrays;28import java.util.List;2930/**31* Base class.32*/33public abstract class Test {3435static final String TEST_SOURCES = System.getProperty("test.src", ".");36static final String TEST_CLASSES = System.getProperty("test.classes");37static final String FS = System.getProperty("file.separator");38static final String JAVA_HOME = System.getProperty("test.jdk");39static final String KEYTOOL = JAVA_HOME + FS + "bin" + FS + "keytool";40static final String JARSIGNER = JAVA_HOME + FS + "bin" + FS + "jarsigner";41static final String UNSIGNED_JARFILE = "unsigned.jar";42static final String SIGNED_JARFILE = "signed.jar";43static final String UPDATED_SIGNED_JARFILE = "updated_signed.jar";44static final String FIRST_FILE = "first.txt";45static final String SECOND_FILE = "second.txt";46static final String PASSWORD = "password";47static final String FIRST_KEY_KEYSTORE = "first_key.jks";48static final String KEYSTORE = "keystore.jks";49static final String FIRST_KEY_ALIAS = "first";50static final String SECOND_KEY_ALIAS = "second";51static final String KEY_ALG = "RSA";52static final String KEY_ALIAS = "alias";53static final String CERT_REQUEST_FILENAME = "test.req";54static final String CERT_FILENAME = "test.crt";55static final String CA_KEY_ALIAS = "ca";56static final String CA2_KEY_ALIAS = "ca2";57static final int KEY_SIZE = 2048;58static final int TIMEOUT = 6 * 60 * 1000; // in millis59static final int VALIDITY = 365;6061static final String WARNING = "Warning:";62static final String WARNING_OR_ERROR = "(Warning|Error):";6364static final String CHAIN_NOT_VALIDATED_VERIFYING_WARNING65= "This jar contains entries "66+ "whose certificate chain is invalid.";6768static final String ALIAS_NOT_IN_STORE_VERIFYING_WARNING69= "This jar contains signed entries "70+ "that are not signed by alias in this keystore.";7172static final String BAD_EXTENDED_KEY_USAGE_SIGNING_WARNING73= "The signer certificate's ExtendedKeyUsage extension "74+ "doesn't allow code signing.";7576static final String BAD_EXTENDED_KEY_USAGE_VERIFYING_WARNING77= "This jar contains entries whose signer certificate's "78+ "ExtendedKeyUsage extension doesn't allow code signing.";7980static final String BAD_KEY_USAGE_SIGNING_WARNING81= "The signer certificate's KeyUsage extension "82+ "doesn't allow code signing.";8384static final String BAD_KEY_USAGE_VERIFYING_WARNING85= "This jar contains entries whose signer certificate's KeyUsage "86+ "extension doesn't allow code signing.";8788static final String BAD_NETSCAPE_CERT_TYPE_SIGNING_WARNING89= "The signer certificate's NetscapeCertType extension "90+ "doesn't allow code signing.";9192static final String BAD_NETSCAPE_CERT_TYPE_VERIFYING_WARNING93= "This jar contains entries "94+ "whose signer certificate's NetscapeCertType extension "95+ "doesn't allow code signing.";9697static final String CHAIN_NOT_VALIDATED_SIGNING_WARNING98= "The signer's certificate chain is invalid.";99100static final String HAS_EXPIRING_CERT_SIGNING_WARNING101= "The signer certificate will expire within six months.";102103static final String HAS_EXPIRING_CERT_VERIFYING_WARNING104= "This jar contains entries "105+ "whose signer certificate will expire within six months.";106107static final String HAS_EXPIRED_CERT_SIGNING_WARNING108= "The signer certificate has expired.";109110static final String HAS_EXPIRED_CERT_VERIFYING_WARNING111= "This jar contains entries whose signer certificate has expired.";112113static final String HAS_UNSIGNED_ENTRY_VERIFYING_WARNING114= "This jar contains unsigned entries "115+ "which have not been integrity-checked.";116117static final String NOT_SIGNED_BY_ALIAS_VERIFYING_WARNING118= "This jar contains signed entries "119+ "which are not signed by the specified alias(es).";120121static final String NO_TIMESTAMP_SIGNING_WARN_TEMPLATE122= "No -tsa or -tsacert is provided "123+ "and this jar is not timestamped. "124+ "Without a timestamp, users may not be able to validate this jar "125+ "after the signer certificate's expiration date "126+ "(%1$tY-%1$tm-%1$td) or after any future revocation date.";127128static final String NO_TIMESTAMP_VERIFYING_WARN_TEMPLATE129= "This jar contains signatures that do not include a timestamp. "130+ "Without a timestamp, users may not be able to validate this jar "131+ "after any of the signer certificates expire "132+ "(as early as %1$tY-%1$tm-%1$td).";133134static final String NOT_YET_VALID_CERT_SIGNING_WARNING135= "The signer certificate is not yet valid.";136137static final String NOT_YET_VALID_CERT_VERIFYING_WARNING138= "This jar contains entries "139+ "whose signer certificate is not yet valid.";140141static final String JAR_SIGNED = "jar signed.";142143static final String JAR_VERIFIED = "jar verified.";144145static final String JAR_VERIFIED_WITH_SIGNER_ERRORS146= "jar verified, with signer errors.";147148static final int CHAIN_NOT_VALIDATED_EXIT_CODE = 4;149static final int HAS_EXPIRED_CERT_EXIT_CODE = 4;150static final int BAD_KEY_USAGE_EXIT_CODE = 8;151static final int BAD_EXTENDED_KEY_USAGE_EXIT_CODE = 8;152static final int BAD_NETSCAPE_CERT_TYPE_EXIT_CODE = 8;153static final int HAS_UNSIGNED_ENTRY_EXIT_CODE = 16;154static final int ALIAS_NOT_IN_STORE_EXIT_CODE = 32;155static final int NOT_SIGNED_BY_ALIAS_EXIT_CODE = 32;156157protected void createAlias(String alias, String ... options)158throws Throwable {159List<String> cmd = new ArrayList<>();160cmd.addAll(Arrays.asList(161"-genkeypair",162"-alias", alias,163"-keyalg", KEY_ALG,164"-keysize", Integer.toString(KEY_SIZE),165"-keystore", KEYSTORE,166"-storepass", PASSWORD,167"-keypass", PASSWORD,168"-dname", "CN=" + alias));169cmd.addAll(Arrays.asList(options));170171keytool(cmd.toArray(new String[cmd.size()]))172.shouldHaveExitValue(0);173}174175protected void issueCert(String alias, String ... options)176throws Throwable {177keytool("-certreq",178"-alias", alias,179"-keystore", KEYSTORE,180"-storepass", PASSWORD,181"-keypass", PASSWORD,182"-file", alias + ".req")183.shouldHaveExitValue(0);184185List<String> cmd = new ArrayList<>();186cmd.addAll(Arrays.asList(187"-gencert",188"-alias", CA_KEY_ALIAS,189"-infile", alias + ".req",190"-outfile", alias + ".cert",191"-keystore", KEYSTORE,192"-storepass", PASSWORD,193"-keypass", PASSWORD,194"-file", alias + ".req"));195cmd.addAll(Arrays.asList(options));196197keytool(cmd.toArray(new String[cmd.size()]))198.shouldHaveExitValue(0);199200keytool("-importcert",201"-alias", alias,202"-keystore", KEYSTORE,203"-storepass", PASSWORD,204"-keypass", PASSWORD,205"-file", alias + ".cert")206.shouldHaveExitValue(0);207}208209protected void checkVerifying(OutputAnalyzer analyzer, int expectedExitCode,210String... warnings) {211analyzer.shouldHaveExitValue(expectedExitCode);212int count = 0;213for (String warning : warnings) {214if (warning.startsWith("!")) {215analyzer.shouldNotContain(warning.substring(1));216} else {217count++;218analyzer.shouldContain(warning);219}220}221if (count > 0) {222analyzer.shouldMatch(WARNING_OR_ERROR);223}224if (expectedExitCode == 0) {225analyzer.shouldContain(JAR_VERIFIED);226} else {227analyzer.shouldContain(JAR_VERIFIED_WITH_SIGNER_ERRORS);228}229}230231protected void checkSigning(OutputAnalyzer analyzer, String... warnings) {232analyzer.shouldHaveExitValue(0);233int count = 0;234for (String warning : warnings) {235if (warning.startsWith("!")) {236analyzer.shouldNotContain(warning.substring(1));237} else {238count++;239analyzer.shouldContain(warning);240}241}242if (count > 0) {243analyzer.shouldMatch(WARNING_OR_ERROR);244}245analyzer.shouldContain(JAR_SIGNED);246}247}248249250