Path: blob/master/test/jdk/java/security/Policy/SignedJar/SignedJarTest.java
41154 views
/*1* Copyright (c) 2015, 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.io.File;24import java.nio.file.Files;25import java.nio.file.Paths;26import java.security.AccessControlException;27import java.security.AccessController;28import java.security.Permission;29import java.security.PrivilegedAction;30import jdk.test.lib.process.ProcessTools;3132/**33* @test34* @bug 8048360 824256535* @summary test policy entry with signedBy alias36* @library /test/lib37* @run main/othervm SignedJarTest38*/39public class SignedJarTest {4041private static final String FS = File.separator;42private static final String JAVA_HOME = System.getProperty("test.jdk");43private static final String TESTCLASSES = System.getProperty("test.classes", "");44private static final String TESTSRC = System.getProperty("test.src", "");45private static final String KEYTOOL = JAVA_HOME + FS + "bin" + FS + "keytool";46private static final String JAR = JAVA_HOME + FS + "bin" + FS + "jar";47private static final String JARSIGNER = JAVA_HOME + FS + "bin" + FS + "jarsigner";48private static final String PASSWORD = "password";49private static final String PWDFILE = "keypass";50private static final String POLICY1 = "SignedJarTest_1.policy";51private static final String POLICY2 = "SignedJarTest_2.policy";52private static final String KEYSTORE1 = "both.jks";53private static final String KEYSTORE2 = "first.jks";54private static final String SECPROPS = TESTSRC + FS + "java.security";5556public static void main(String args[]) throws Throwable {57//copy PrivilegeTest.class, policy files and keystore password file into current direcotry58Files.copy(Paths.get(TESTCLASSES, "PrivilegeTest.class"), Paths.get("PrivilegeTest.class"));59Files.copy(Paths.get(TESTSRC, POLICY1), Paths.get(POLICY1));60Files.copy(Paths.get(TESTSRC, POLICY2), Paths.get(POLICY2));61Files.copy(Paths.get(TESTSRC, PWDFILE), Paths.get(PWDFILE));6263//create Jar file64ProcessTools.executeCommand(JAR, "-cvf", "test.jar", "PrivilegeTest.class");6566//Creating first key , keystore both.jks67ProcessTools.executeCommand(KEYTOOL,68"-genkey",69"-keyalg", "DSA",70"-alias", "first",71"-keystore", KEYSTORE1,72"-keypass", PASSWORD,73"-dname", "cn=First",74"-storepass", PASSWORD75).shouldHaveExitValue(0);7677//Creating Second key, keystore both.jks78ProcessTools.executeCommand(KEYTOOL,79"-genkey",80"-keyalg", "DSA",81// "-storetype","JKS",82"-alias", "second",83"-keystore", KEYSTORE1,84"-keypass", PASSWORD,85"-dname", "cn=Second",86"-storepass", PASSWORD87).shouldHaveExitValue(0);8889//copy both.jks to first.jks, remove second Keypair from first.jks90Files.copy(Paths.get(KEYSTORE1), Paths.get(KEYSTORE2));91ProcessTools.executeCommand(KEYTOOL,92"-delete",93"-keystore", KEYSTORE2,94"-alias", "second",95"-storepass", PASSWORD96).shouldHaveExitValue(0);9798//sign jar with first key, first.jar is only signed by first signer99ProcessTools.executeCommand(JARSIGNER,100"-keystore", KEYSTORE1,101"-storepass", PASSWORD,102"-keypass", PASSWORD,103"-signedjar", "first.jar", "test.jar",104"first").shouldHaveExitValue(0);105106//sign jar with second key, both.jar is signed by first and second signer107ProcessTools.executeCommand(JARSIGNER,108"-keystore", KEYSTORE1,109"-storepass", PASSWORD,110"-keypass", PASSWORD,111"-signedjar", "both.jar", "first.jar",112"second").shouldHaveExitValue(0);113114//test case 1115//setIO permission granted to code that was signed by first signer116//setFactory permission granted to code that was signed by second signer117//Keystore that contains both first and second keypairs118//code was singed by first signer119//Expect AccessControlException for setFactory permission120System.out.println("Test Case 1");121//copy policy file into current directory122String[] cmd = constructCMD("first.jar", POLICY1, "false", "true");123ProcessTools.executeTestJvm(cmd).shouldHaveExitValue(0);124125//test case 2, test with both.jar126//setIO permission granted to code that was signed by first signer127//setFactory permission granted to code that was signed by second signer128//Keystore that contains both first and second keypairs129//code was singed by first signer and second signer130//Expect no AccessControlException131System.out.println("Test Case 2");132cmd = constructCMD("both.jar", POLICY1, "false", "false");133ProcessTools.executeTestJvm(cmd).shouldHaveExitValue(0);134135//test case 3136//setIO permission granted to code that was signed by first signer137//setFactory permission granted to code that was signed by second signer138//Keystore that contains only first keypairs139//code was singed by first signer and second signer140//Expect AccessControlException for setFactory permission141System.out.println("Test Case 3");142cmd = constructCMD("both.jar", POLICY2, "false", "true");143ProcessTools.executeTestJvm(cmd).shouldHaveExitValue(0);144145}146147private static String[] constructCMD(String classpath, String policy, String arg1, String arg2) {148String[] cmd = {149"-classpath", classpath,150"-Djava.security.manager",151"-Djava.security.policy=" + policy,152"-Djava.security.properties=" + SECPROPS,153"PrivilegeTest",154arg1, arg2};155return cmd;156}157}158159class PrivilegeTest {160161private static final Permission PERM1 = new RuntimePermission("setIO");162private static final Permission PERM2 = new RuntimePermission("setFactory");163164public static void main(String args[]) {165boolean expectException1 = Boolean.parseBoolean(args[0]);166boolean expectException2 = Boolean.parseBoolean(args[1]);167test(PERM1, expectException1);168test(PERM2, expectException2);169}170171public static void test(Permission perm, boolean expectException) {172boolean getException = (Boolean) AccessController.doPrivileged((PrivilegedAction) () -> {173try {174AccessController.checkPermission(perm);175return (Boolean) false;176} catch (AccessControlException ex) {177return (Boolean) true;178}179});180181if (expectException ^ getException) {182String message = "Check Permission :" + perm + "\n ExpectException = "183+ expectException + "\n getException = " + getException;184throw new RuntimeException(message);185}186187}188189}190191192