1 /*
   2  * Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import jdk.test.lib.process.OutputAnalyzer;
  25 import jdk.test.lib.util.JarUtils;
  26 
  27 /**
  28  * @test
  29  * @bug 8024302 8026037
  30  * @summary Checks if jarsigner prints appropriate warnings
  31  * @library /test/lib ../
  32  * @build jdk.test.lib.util.JarUtils
  33  * @run main MultipleWarningsTest
  34  */
  35 public class MultipleWarningsTest extends Test {
  36 
  37     /**
  38      * The test signs and verifies a jar that:
  39      *   - contains entries whose signer certificate has expired
  40      *   - contains entries whose signer certificate's ExtendedKeyUsage
  41      *     extension doesn't allow code signing
  42      *   - contains unsigned entries which have not been integrity-checked
  43      *   - contains signed entries which are not signed by the specified alias
  44      * Warning messages are expected.
  45      */
  46     public static void main(String[] args) throws Throwable {
  47         MultipleWarningsTest test = new MultipleWarningsTest();
  48         test.start();
  49     }
  50 
  51     private void start() throws Throwable {
  52         Utils.createFiles(FIRST_FILE, SECOND_FILE);
  53 
  54         // create a jar file that contains one class file
  55         JarUtils.createJar(UNSIGNED_JARFILE, FIRST_FILE);
  56 
  57         createAlias(CA_KEY_ALIAS);
  58 
  59         // create first expired certificate
  60         // whose ExtendedKeyUsage extension does not allow code signing
  61         createAlias(FIRST_KEY_ALIAS);
  62         issueCert(
  63                 FIRST_KEY_ALIAS,
  64                 "-ext", "ExtendedkeyUsage=serverAuth",
  65                 "-startdate", "-" + VALIDITY * 2 + "d",
  66                 "-validity", Integer.toString(VALIDITY));
  67 
  68         // create second expired certificate
  69         // whose KeyUsage extension does not allow code signing
  70         createAlias(SECOND_KEY_ALIAS);
  71         issueCert(
  72                 SECOND_KEY_ALIAS,
  73                 "-ext", "ExtendedkeyUsage=serverAuth",
  74                 "-startdate", "-" + VALIDITY * 2 + "d",
  75                 "-validity", Integer.toString(VALIDITY));
  76 
  77         // sign jar with first key
  78         OutputAnalyzer analyzer = jarsigner(
  79                 "-keystore", KEYSTORE,
  80                 "-storepass", PASSWORD,
  81                 "-keypass", PASSWORD,
  82                 "-signedjar", SIGNED_JARFILE,
  83                 UNSIGNED_JARFILE,
  84                 FIRST_KEY_ALIAS);
  85 
  86         checkSigning(analyzer, HAS_EXPIRED_CERT_SIGNING_WARNING,
  87                 BAD_EXTENDED_KEY_USAGE_SIGNING_WARNING);
  88 
  89         // add a second class to created jar, so it contains unsigned entry
  90         JarUtils.updateJar(SIGNED_JARFILE, UPDATED_SIGNED_JARFILE, SECOND_FILE);
  91 
  92         // verify jar with second key
  93         analyzer = jarsigner(
  94                 "-verify",
  95                 "-keystore", KEYSTORE,
  96                 "-storepass", PASSWORD,
  97                 "-keypass", PASSWORD,
  98                 UPDATED_SIGNED_JARFILE,
  99                 SECOND_KEY_ALIAS);
 100 
 101         checkVerifying(analyzer, 0, BAD_EXTENDED_KEY_USAGE_VERIFYING_WARNING,
 102                 HAS_EXPIRED_CERT_VERIFYING_WARNING,
 103                 HAS_UNSIGNED_ENTRY_VERIFYING_WARNING,
 104                 NOT_SIGNED_BY_ALIAS_VERIFYING_WARNING);
 105 
 106         // verify jar with second key in strict mode
 107         analyzer = jarsigner(
 108                 "-verify",
 109                 "-strict",
 110                 "-keystore", KEYSTORE,
 111                 "-storepass", PASSWORD,
 112                 "-keypass", PASSWORD,
 113                 UPDATED_SIGNED_JARFILE,
 114                 SECOND_KEY_ALIAS);
 115 
 116         int expectedExitCode = HAS_EXPIRED_CERT_EXIT_CODE
 117                 + BAD_EXTENDED_KEY_USAGE_EXIT_CODE
 118                 + HAS_UNSIGNED_ENTRY_EXIT_CODE
 119                 + NOT_SIGNED_BY_ALIAS_EXIT_CODE;
 120         checkVerifying(analyzer, expectedExitCode,
 121                 BAD_EXTENDED_KEY_USAGE_VERIFYING_WARNING,
 122                 HAS_EXPIRED_CERT_VERIFYING_WARNING,
 123                 HAS_UNSIGNED_ENTRY_VERIFYING_WARNING,
 124                 NOT_SIGNED_BY_ALIAS_VERIFYING_WARNING);
 125 
 126         // verify jar with non-exisiting alias
 127         analyzer = jarsigner(
 128                 "-verify",
 129                 "-keystore", KEYSTORE,
 130                 "-storepass", PASSWORD,
 131                 "-keypass", PASSWORD,
 132                 UPDATED_SIGNED_JARFILE,
 133                 "bogus");
 134 
 135         checkVerifying(analyzer, 0, BAD_EXTENDED_KEY_USAGE_VERIFYING_WARNING,
 136                 HAS_EXPIRED_CERT_VERIFYING_WARNING,
 137                 HAS_UNSIGNED_ENTRY_VERIFYING_WARNING,
 138                 NOT_SIGNED_BY_ALIAS_VERIFYING_WARNING);
 139 
 140         // verify jar with non-exisiting alias in strict mode
 141         analyzer = jarsigner(
 142                 "-verify",
 143                 "-strict",
 144                 "-keystore", KEYSTORE,
 145                 "-storepass", PASSWORD,
 146                 "-keypass", PASSWORD,
 147                 UPDATED_SIGNED_JARFILE,
 148                 "bogus");
 149 
 150         checkVerifying(analyzer, expectedExitCode,
 151                 BAD_EXTENDED_KEY_USAGE_VERIFYING_WARNING,
 152                 HAS_EXPIRED_CERT_VERIFYING_WARNING,
 153                 HAS_UNSIGNED_ENTRY_VERIFYING_WARNING,
 154                 NOT_SIGNED_BY_ALIAS_VERIFYING_WARNING);
 155 
 156         System.out.println("Test passed");
 157     }
 158 
 159 }