Path: blob/master/test/jdk/sun/security/tools/jarsigner/RestrictedAlgo.java
41152 views
/*1* Copyright (c) 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 java.util.ArrayList;24import java.util.Arrays;25import java.util.List;26import java.io.File;27import java.nio.file.Files;28import java.nio.file.Path;29import java.nio.file.Paths;30import jdk.test.lib.SecurityTools;31import jdk.test.lib.util.JarUtils;32import jdk.test.lib.process.OutputAnalyzer;3334/**35* @test36* @bug 824874537* @summary Test key generation and jar signing with disabled algorithms and38* key sizes, with and without entries in jdk.jar.disabledAlgorithms,39* jdk.certpath.disabledAlgorithms40* @library /test/lib41* @run main/othervm RestrictedAlgo RESTRICT42* @run main/othervm RestrictedAlgo NO_RESTRICT43*/4445public class RestrictedAlgo {4647private static final String KEYSTORE = "keystore.jks";48private static final String PASSWORD = "password";49private static final String SIGNED_JARFILE = "signed.jar";50private static final String UNSIGNED_JARFILE = "unsigned.jar";51private static final String SECURITY_FILE = "java.security";52private static final String NO_RESTRICT = "-J-Djava.security.properties="53+ SECURITY_FILE;54private static final String FIRST_FILE = "first.txt";55private static final String WARNING = "Warning:";56private static final String SECURITY_WARNING =57".* is considered a security risk and is disabled.";5859private static String algoStatus;6061public static void main(String[] args) throws Exception {6263algoStatus = args[0];64// create a jar file that contains one file65JarUtils.createJarFile(Path.of(UNSIGNED_JARFILE), Path.of("."),66new File(FIRST_FILE).exists() ? Paths.get(FIRST_FILE)67: Files.createFile(Paths.get(FIRST_FILE)));68if (!isAlgoRestricted()) {69// An alternative security properties70Files.writeString(Files.createFile(Paths.get(SECURITY_FILE)),71"jdk.certpath.disabledAlgorithms=\n"72+ "jdk.jar.disabledAlgorithms=\n"73+ "jdk.security.legacyAlgorithms=");74}7576System.out.println("\nTesting sigalg MD2\n");77test("RSA", "MD2withRSA", "SigAlgMD2", "SHA256", true);7879System.out.println("\nTesting sigalg MD5\n");80test("RSA", "MD5withRSA", "SigAlgMD5", "SHA256", true);8182System.out.println("\nTesting digestalg MD2\n");83test("RSA", "SHA256withRSA", "DigestAlgMD2", "MD2", false);8485System.out.println("\nTesting digestalg MD5\n");86test("RSA", "SHA256withRSA", "DigestAlgMD5", "MD5", false);8788System.out.println("\nTesting RSA Keysize: RSA keySize < 1024\n");89test("RSA", "SHA256withRSA", "KeySizeRSA", "SHA256", true,90"-keysize", "512");9192System.out.println("\nTesting DSA Keysize: DSA keySize < 1024\n");93test("DSA", "SHA256withDSA", "KeySizeDSA", "SHA256", true,94"-keysize", "512");95}9697private static void test(String keyAlg, String sigAlg, String aliasPrefix,98String digestAlg, boolean isKeyToolVerify,99String... addKeyToolArgs) throws Exception {100101String alias = aliasPrefix + "_" + algoStatus;102testKeytool(keyAlg, sigAlg, alias, isKeyToolVerify, addKeyToolArgs);103testJarSignerSigning(sigAlg, alias, digestAlg);104testJarSignerVerification();105}106107private static void testKeytool(String keyAlg, String sigAlg, String alias,108boolean isKeyToolVerify, String... additionalCmdArgs)109throws Exception {110111System.out.println("Testing Keytool\n");112List<String> cmd = prepareCommand(113"-genkeypair",114"-keystore", KEYSTORE,115"-storepass", PASSWORD,116"-dname", "CN=Test",117"-ext", "bc:c",118"-keyalg", keyAlg,119"-sigalg", sigAlg,120"-alias", alias);121for (String additionalCMDArg : additionalCmdArgs) {122cmd.add(additionalCMDArg);123}124125OutputAnalyzer analyzer = SecurityTools.keytool(cmd)126.shouldHaveExitValue(0);127if (isKeyToolVerify) {128verifyAnalyzer(analyzer);129}130}131132private static void testJarSignerSigning(String sigAlg, String alias,133String digestAlg) throws Exception {134135System.out.println("\nTesting JarSigner Signing\n");136List<String> cmd = prepareCommand(137"-keystore", KEYSTORE,138"-storepass", PASSWORD,139"-sigalg", sigAlg,140"-digestalg", digestAlg,141"-signedjar", SIGNED_JARFILE,142UNSIGNED_JARFILE,143alias);144145OutputAnalyzer analyzer = SecurityTools.jarsigner(cmd)146.shouldHaveExitValue(0);147148verifyAnalyzer(analyzer);149}150151private static void testJarSignerVerification()152throws Exception {153154System.out.println("\nTesting JarSigner Verification\n");155List<String> cmd = prepareCommand(156"-verify",157SIGNED_JARFILE);158159OutputAnalyzer analyzer = SecurityTools.jarsigner(cmd)160.shouldHaveExitValue(0);161162if (isAlgoRestricted()) {163analyzer.shouldContain("The jar will be treated as unsigned,"164+ " because it is signed with a weak algorithm that "165+ "is now disabled.");166} else {167analyzer.shouldContain("jar verified.");168}169}170171private static List<String> prepareCommand(String... options) {172List<String> cmd = new ArrayList<>();173cmd.addAll(Arrays.asList(options));174if (!isAlgoRestricted()) {175cmd.add(NO_RESTRICT);176}177return cmd;178}179180private static void verifyAnalyzer(OutputAnalyzer analyzer) {181if (isAlgoRestricted()) {182analyzer.shouldContain(WARNING)183.shouldMatch(SECURITY_WARNING);184} else {185analyzer.shouldNotMatch(SECURITY_WARNING);186}187}188189private static boolean isAlgoRestricted() {190return ("RESTRICT".equals(algoStatus)) ? true : false;191}192}193194195