Path: blob/master/test/jdk/sun/security/tools/keytool/ReadJar.java
41152 views
/*1* Copyright (c) 2016, 2017, 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 6890872 816888226* @summary keytool -printcert to recognize signed jar files27* @library /test/lib28* @build jdk.test.lib.SecurityTools29* jdk.test.lib.util.JarUtils30* jdk.test.lib.Utils31* jdk.test.lib.Asserts32* jdk.test.lib.JDKToolFinder33* jdk.test.lib.JDKToolLauncher34* jdk.test.lib.Platform35* jdk.test.lib.process.*36* @run main ReadJar37*/3839import java.nio.file.Files;40import java.nio.file.Paths;41import jdk.test.lib.SecurityTools;42import jdk.test.lib.process.OutputAnalyzer;43import jdk.test.lib.util.JarUtils;4445public class ReadJar {4647public static void main(String[] args) throws Throwable {48testWithMD5();49}5051// make sure that -printcert option works52// if a weak algorithm was used for signing a jar53private static void testWithMD5() throws Throwable {54// create jar files55JarUtils.createJar("test_md5.jar", "test");56JarUtils.createJar("test_rsa.jar", "test");5758// create a keystore and generate keys for jar signing59Files.deleteIfExists(Paths.get("keystore"));6061OutputAnalyzer out = SecurityTools.keytool("-genkeypair "62+ "-keystore keystore -storepass password "63+ "-keypass password -keyalg rsa -alias rsa_alias -dname CN=A");64System.out.println(out.getOutput());65out.shouldHaveExitValue(0);6667out = SecurityTools.jarsigner("-keystore keystore -storepass password "68+ "test_rsa.jar rsa_alias");69System.out.println(out.getOutput());70out.shouldHaveExitValue(0);7172printCert("test_rsa.jar");7374out = SecurityTools.jarsigner("-keystore keystore -storepass password "75+ "-sigalg MD5withRSA -digestalg MD5 test_md5.jar rsa_alias");76System.out.println(out.getOutput());77out.shouldHaveExitValue(0);7879printCert("test_md5.jar");80}8182private static void printCert(String jar) throws Throwable {83OutputAnalyzer out = SecurityTools.keytool("-printcert -jarfile " + jar);84System.out.println(out.getOutput());85out.shouldHaveExitValue(0);86out.shouldNotContain("Not a signed jar file");8788out = SecurityTools.keytool("-printcert -rfc -jarfile " + jar);89System.out.println(out.getOutput());90out.shouldHaveExitValue(0);91out.shouldNotContain("Not a signed jar file");92}93}949596