Path: blob/master/test/jdk/sun/security/tools/jarsigner/CheckSignerCertChain.java
41152 views
/*1* Copyright (c) 2021, 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 8259401 826622526* @summary Check certificates in signer's cert chain to see if warning emitted27* @library /test/lib28*/2930import jdk.test.lib.SecurityTools;31import jdk.test.lib.process.OutputAnalyzer;32import jdk.test.lib.util.JarUtils;3334import java.nio.file.Files;35import java.nio.file.Path;36import java.nio.file.Paths;3738public class CheckSignerCertChain {3940private static final String JAVA_SECURITY_FILE = "java.security";4142static OutputAnalyzer kt(String cmd, String ks) throws Exception {43return SecurityTools.keytool("-storepass changeit " + cmd +44" -keystore " + ks);45}4647static void gencert(String owner, String cmd) throws Exception {48kt("-certreq -alias " + owner + " -file tmp.req", "ks");49kt("-gencert -infile tmp.req -outfile tmp.cert " + cmd, "ks");50kt("-importcert -alias " + owner + " -file tmp.cert", "ks");51}5253public static void main(String[] args) throws Exception {5455// root certificate using SHA1withRSA and 1024-bit key56System.out.println("Generating a root cert using SHA1withRSA and 1024-bit key");57kt("-genkeypair -keyalg rsa -alias ca -dname CN=CA -ext bc:c " +58"-keysize 1024 -sigalg SHA1withRSA", "ks");59kt("-genkeypair -keyalg rsa -alias ca1 -dname CN=CA1", "ks");60kt("-genkeypair -keyalg rsa -alias e1 -dname CN=E1", "ks");6162// intermediate certificate using SHA1withRSA and 2048-bit key63System.out.println("Generating an intermediate cert using SHA1withRSA and 2048-bit key");64gencert("ca1", "-alias ca -ext san=dns:ca1 -ext bc:c " +65"-sigalg SHA1withRSA ");6667// end entity certificate using SHA256withRSA and 2048-bit key68System.out.println("Generating an end entity cert using SHA256withRSA and 2048-bit key");69gencert("e1", "-alias ca1 -ext san=dns:e1 ");7071JarUtils.createJarFile(Path.of("a.jar"), Path.of("."), Path.of("ks"));7273SecurityTools.jarsigner("-keystore ks -storepass changeit " +74"-signedjar signeda.jar " +75"-sigalg SHA256withRSA " +76"-verbose" +77" a.jar e1")78.shouldContain("Signature algorithm: SHA1withRSA (weak), 2048-bit key")79// For trusted cert, warning should be generated for its weak 1024-bit80// key, but not for its SHA1withRSA algorithm.81.shouldContain("Signature algorithm: SHA1withRSA, 1024-bit key (weak)")82.shouldHaveExitValue(0);8384kt("-exportcert -alias ca -rfc -file cacert", "ks");85kt("-importcert -noprompt -file cacert", "caks");8687SecurityTools.jarsigner("-verify -certs signeda.jar " +88"-keystore caks -storepass changeit -verbose -debug")89.shouldContain("Signature algorithm: SHA1withRSA (weak), 2048-bit key")90// For trusted cert, warning should be generated for its weak 1024-bit91// key, but not for its SHA1withRSA algorithm.92.shouldContain("Signature algorithm: SHA1withRSA, 1024-bit key (weak)")93.shouldHaveExitValue(0);9495/*96* Generate a non-self-signed certificate using MD5withRSA as its signature97* algorithm to sign a JAR file.98*/99kt("-genkeypair -keyalg rsa -alias cacert -dname CN=CACERT -ext bc:c ", "ks");100kt("-genkeypair -keyalg rsa -alias ee -dname CN=EE -ext bc:c ", "ks");101gencert("ee", "-alias cacert -ext san=dns:ee -sigalg MD5withRSA");102103Files.writeString(Files.createFile(Paths.get(JAVA_SECURITY_FILE)),104"jdk.certpath.disabledAlgorithms=\n" +105"jdk.jar.disabledAlgorithms=MD5\n");106107SecurityTools.jarsigner("-keystore ks -storepass changeit " +108"-signedjar signeda.jar " +109"-verbose " +110"-J-Djava.security.properties=" +111JAVA_SECURITY_FILE +112" a.jar ee")113.shouldNotContain("Signature algorithm: MD5withRSA (disabled), 2048-bit key")114.shouldContain("Signature algorithm: SHA256withRSA, 2048-bit key")115.shouldNotContain("Invalid certificate chain: Algorithm constraints check failed on signature algorithm: MD5withRSA")116.shouldHaveExitValue(0);117118Files.deleteIfExists(Paths.get(JAVA_SECURITY_FILE));119Files.writeString(Files.createFile(Paths.get(JAVA_SECURITY_FILE)),120"jdk.certpath.disabledAlgorithms=MD5\n" +121"jdk.jar.disabledAlgorithms=\n");122123SecurityTools.jarsigner("-keystore ks -storepass changeit " +124"-signedjar signeda.jar " +125"-verbose " +126"-J-Djava.security.properties=" +127JAVA_SECURITY_FILE +128" a.jar ee")129.shouldContain("Signature algorithm: MD5withRSA (disabled), 2048-bit key")130.shouldContain("Signature algorithm: SHA256withRSA, 2048-bit key")131.shouldContain("Invalid certificate chain: Algorithm constraints check failed on signature algorithm: MD5withRSA")132.shouldHaveExitValue(0);133134kt("-exportcert -alias cacert -rfc -file cacert", "ks");135kt("-importcert -noprompt -file cacert", "caks1");136137SecurityTools.jarsigner("-verify -certs signeda.jar " +138"-keystore caks1 -storepass changeit -verbose -debug")139.shouldContain("Signature algorithm: MD5withRSA (disabled), 2048-bit key")140.shouldContain("Signature algorithm: SHA256withRSA, 2048-bit key")141.shouldContain("Invalid certificate chain: Algorithm constraints check failed on signature algorithm: MD5withRSA")142.shouldHaveExitValue(0);143}144}145146147