Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/provider/PolicyFile/TokenStore.java
38853 views
/*1* Copyright (c) 2003, 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 491914726* @summary Support for token-based KeyStores27*28* TokenStore.keystore password is "TokenStore"29*/3031import java.io.*;32import java.util.*;33import java.net.*;34import java.security.AllPermission;35import java.security.CodeSource;36import java.security.ProtectionDomain;37import java.security.Permission;38import java.security.KeyStore;39import java.security.cert.*;40import sun.security.provider.*;4142public class TokenStore {4344private static String DIR =45System.getProperty("test.classes", ".") + File.separatorChar;46private static final char[] storePassword = new char[]47{ 'T', 'o', 'k', 'e', 'n', 'S', 't', 'o', 'r', 'e' };484950// policy files that will get written51private static String NO_STORE_FILE = DIR + "TokenStore.NoStore";52private static String URL_FILE = DIR + "TokenStore.Url";53private static String URL_T_FILE = DIR + "TokenStore.UrlT";54private static String URL_T_P_FILE = DIR + "TokenStore.UrlTP";55private static String URL_PWD_FILE = DIR + "TokenStore.UrlPwd";56private static String URL_T_P_PWD_FILE = DIR + "TokenStore.UrlTPPwd";57private static String BADPASS_FILE = DIR + "TokenStore.BadPass";5859private static String RELPASS_FILE =60System.getProperty("test.src", ".") + File.separatorChar +61"TokenStore.RelPassPolicy";6263// protection domains64private static ProtectionDomain NO_STORE_DOMAIN;65private static ProtectionDomain URL_DOMAIN;66private static ProtectionDomain URL_T_DOMAIN;67private static ProtectionDomain URL_T_P_DOMAIN;6869// policy contents written to files70private static final String POLICY_NO_STORE =71"grant { permission java.security.AllPermission; };";7273private static final String POLICY_URL =74"keystore \"file:${test.src}${/}TokenStore.keystore\";" +75"grant signedby \"POLICY_URL\" {" +76" permission java.security.AllPermission;" +77"};" ;7879private static final String POLICY_URL_T =80"keystore \"file:${test.src}${/}TokenStore.keystore\", \"JKS\";"+81"grant signedby \"POLICY_URL_T\" {" +82" permission java.security.AllPermission;" +83"};" ;8485private static final String POLICY_URL_T_P =86"keystore \"file:${test.src}${/}TokenStore.keystore\"," +87" \"JKS\", \"SUN\";" +88"grant signedby \"POLICY_URL_T_P\" {" +89" permission java.security.AllPermission;" +90"};" ;9192private static final String POLICY_URL_PWD =93"keystore \"file:${test.src}${/}TokenStore.keystore\";" +94"keystorePasswordURL \"file:${test.src}${/}TokenStore.pwd\";" +95"grant signedby \"POLICY_URL\" {" +96" permission java.security.AllPermission;" +97"};" ;9899private static final String POLICY_URL_T_P_PWD =100"keystore \"file:${test.src}${/}TokenStore.keystore\"," +101" \"JKS\", \"SUN\";" +102"keystorePasswordURL \"file:${test.src}${/}TokenStore.pwd\";" +103"grant signedby \"POLICY_URL_T_P\" {" +104" permission java.security.AllPermission;" +105"};" ;106107private static final String POLICY_BADPASS =108"keystore \"file:${test.src}${/}TokenStore.keystore\"," +109" \"JKS\", \"SUN\";" +110"keystorePasswordURL \"file:${test.src}${/}TokenStore.java\";" +111"grant signedby \"POLICY_URL_T_P\" {" +112" permission java.security.AllPermission;" +113"};" ;114115private static void init() throws Exception {116117// first write policy files118119PolicyParser pp = new PolicyParser();120pp.read(new StringReader(POLICY_NO_STORE));121pp.write(new FileWriter(NO_STORE_FILE, false));122123pp = new PolicyParser();124pp.read(new StringReader(POLICY_URL));125pp.write(new FileWriter(URL_FILE, false));126127pp = new PolicyParser();128pp.read(new StringReader(POLICY_URL_T));129pp.write(new FileWriter(URL_T_FILE, false));130131pp = new PolicyParser();132pp.read(new StringReader(POLICY_URL_T_P));133pp.write(new FileWriter(URL_T_P_FILE, false));134135pp = new PolicyParser();136pp.read(new StringReader(POLICY_URL_PWD));137pp.write(new FileWriter(URL_PWD_FILE, false));138139pp = new PolicyParser();140pp.read(new StringReader(POLICY_URL_T_P_PWD));141pp.write(new FileWriter(URL_T_P_PWD_FILE, false));142143pp = new PolicyParser();144pp.read(new StringReader(POLICY_BADPASS));145pp.write(new FileWriter(BADPASS_FILE, false));146147// next load keystore data to build PD's148149KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());150ks.load(new FileInputStream151(System.getProperty("test.src", ".") +152File.separatorChar +153"TokenStore.keystore"),154storePassword);155156NO_STORE_DOMAIN = new ProtectionDomain157(new CodeSource(new URL("file:/foo"),158(java.security.cert.Certificate[]) null),159null, // perms160null, // class loader161null); // principals162163Certificate[] chain = (Certificate[])164ks.getCertificateChain("POLICY_URL");165URL_DOMAIN = new ProtectionDomain166(new CodeSource(new URL("file:/foo"), chain),167null, // perms168null, // class loader169null); // principals170171chain = (Certificate[])172ks.getCertificateChain("POLICY_URL_T");173URL_T_DOMAIN = new ProtectionDomain174(new CodeSource(new URL("file:/foo"), chain),175null, // perms176null, // class loader177null); // principals178179chain = (Certificate[])180ks.getCertificateChain("POLICY_URL_T_P");181URL_T_P_DOMAIN = new ProtectionDomain182(new CodeSource(new URL("file:/foo"), chain),183null, // perms184null, // class loader185null); // principals186}187188public static void main(String[] args) throws Exception {189190init();191192// test no key store in policy193194System.setProperty("java.security.policy", "=" + NO_STORE_FILE);195PolicyFile p = new PolicyFile();196checkPerm(p, NO_STORE_DOMAIN);197198// test policy keystore + URL199200System.setProperty("java.security.policy", "=" + URL_FILE);201p = new PolicyFile();202checkPerm(p, URL_DOMAIN);203204// test policy keystore + URL + type205206System.setProperty("java.security.policy", "=" + URL_T_FILE);207p = new PolicyFile();208checkPerm(p, URL_T_DOMAIN);209210// test policy keystore + URL + type + provider211212System.setProperty("java.security.policy", "=" + URL_T_P_FILE);213p = new PolicyFile();214checkPerm(p, URL_T_P_DOMAIN);215216// test policy keystore + URL + password217218System.setProperty("java.security.policy", "=" + URL_FILE);219p = new PolicyFile();220checkPerm(p, URL_DOMAIN);221222// test policy keystore + URL + type + provider + password223224System.setProperty("java.security.policy", "=" + URL_T_P_FILE);225p = new PolicyFile();226checkPerm(p, URL_T_P_DOMAIN);227228// test policy keystore + URL + type + provider + BAD password229230System.setProperty("java.security.policy", "=" + BADPASS_FILE);231p = new PolicyFile();232try {233checkPerm(p, URL_T_P_DOMAIN);234throw new RuntimeException("expected SecurityException");235} catch (SecurityException se) {236// good237//se.printStackTrace();238}239240// test policy keystore + URL + type + provider + RELATIVE password241242System.setProperty("java.security.policy", "=" + RELPASS_FILE);243p = new PolicyFile();244checkPerm(p, URL_T_P_DOMAIN);245}246247private static void checkPerm(PolicyFile p, ProtectionDomain pd)248throws Exception {249boolean foundIt = false;250Enumeration perms = p.getPermissions(pd).elements();251while (perms.hasMoreElements()) {252Permission perm = (Permission)perms.nextElement();253if (!(perm instanceof AllPermission)) {254throw new SecurityException("expected AllPermission");255} else {256foundIt = true;257}258}259if (!foundIt) {260throw new SecurityException("expected AllPermission");261}262}263}264265266