Path: blob/master/test/jdk/sun/security/provider/PolicyFile/TokenStore.java
41153 views
/*1* Copyright (c) 2003, 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 491914726* @summary Support for token-based KeyStores27* @modules java.base/sun.security.provider28*/2930import java.io.*;31import java.util.*;32import java.net.*;33import java.security.AllPermission;34import java.security.CodeSource;35import java.security.ProtectionDomain;36import java.security.Permission;37import java.security.KeyStore;38import java.security.cert.*;39import sun.security.provider.*;4041public class TokenStore {4243private static String DIR =44System.getProperty("test.classes", ".") + File.separatorChar;45private static final char[] storePassword = new char[]46{ 'T', 'o', 'k', 'e', 'n', 'S', 't', 'o', 'r', 'e' };474849// policy files that will get written50private static String NO_STORE_FILE = DIR + "TokenStore.NoStore";51private static String URL_FILE = DIR + "TokenStore.Url";52private static String URL_T_FILE = DIR + "TokenStore.UrlT";53private static String URL_T_P_FILE = DIR + "TokenStore.UrlTP";54private static String URL_PWD_FILE = DIR + "TokenStore.UrlPwd";55private static String URL_T_P_PWD_FILE = DIR + "TokenStore.UrlTPPwd";56private static String BADPASS_FILE = DIR + "TokenStore.BadPass";5758private static String RELPASS_FILE =59System.getProperty("test.src", ".") + File.separatorChar +60"TokenStore.RelPassPolicy";6162// protection domains63private static ProtectionDomain NO_STORE_DOMAIN;64private static ProtectionDomain URL_DOMAIN;65private static ProtectionDomain URL_T_DOMAIN;66private static ProtectionDomain URL_T_P_DOMAIN;6768// policy contents written to files69private static final String POLICY_NO_STORE =70"grant { permission java.security.AllPermission; };";7172private static final String POLICY_URL =73"keystore \"file:${test.src}${/}TokenStore.keystore\";" +74"grant signedby \"POLICY_URL\" {" +75" permission java.security.AllPermission;" +76"};" ;7778private static final String POLICY_URL_T =79"keystore \"file:${test.src}${/}TokenStore.keystore\", \"JKS\";"+80"grant signedby \"POLICY_URL_T\" {" +81" permission java.security.AllPermission;" +82"};" ;8384private static final String POLICY_URL_T_P =85"keystore \"file:${test.src}${/}TokenStore.keystore\"," +86" \"JKS\", \"SUN\";" +87"grant signedby \"POLICY_URL_T_P\" {" +88" permission java.security.AllPermission;" +89"};" ;9091private static final String POLICY_URL_PWD =92"keystore \"file:${test.src}${/}TokenStore.keystore\";" +93"keystorePasswordURL \"file:${test.src}${/}TokenStore.pwd\";" +94"grant signedby \"POLICY_URL\" {" +95" permission java.security.AllPermission;" +96"};" ;9798private static final String POLICY_URL_T_P_PWD =99"keystore \"file:${test.src}${/}TokenStore.keystore\"," +100" \"JKS\", \"SUN\";" +101"keystorePasswordURL \"file:${test.src}${/}TokenStore.pwd\";" +102"grant signedby \"POLICY_URL_T_P\" {" +103" permission java.security.AllPermission;" +104"};" ;105106private static final String POLICY_BADPASS =107"keystore \"file:${test.src}${/}TokenStore.keystore\"," +108" \"JKS\", \"SUN\";" +109"keystorePasswordURL \"file:${test.src}${/}TokenStore.java\";" +110"grant signedby \"POLICY_URL_T_P\" {" +111" permission java.security.AllPermission;" +112"};" ;113114private static void init() throws Exception {115116// first write policy files117118PolicyParser pp = new PolicyParser();119pp.read(new StringReader(POLICY_NO_STORE));120pp.write(new FileWriter(NO_STORE_FILE, false));121122pp = new PolicyParser();123pp.read(new StringReader(POLICY_URL));124pp.write(new FileWriter(URL_FILE, false));125126pp = new PolicyParser();127pp.read(new StringReader(POLICY_URL_T));128pp.write(new FileWriter(URL_T_FILE, false));129130pp = new PolicyParser();131pp.read(new StringReader(POLICY_URL_T_P));132pp.write(new FileWriter(URL_T_P_FILE, false));133134pp = new PolicyParser();135pp.read(new StringReader(POLICY_URL_PWD));136pp.write(new FileWriter(URL_PWD_FILE, false));137138pp = new PolicyParser();139pp.read(new StringReader(POLICY_URL_T_P_PWD));140pp.write(new FileWriter(URL_T_P_PWD_FILE, false));141142pp = new PolicyParser();143pp.read(new StringReader(POLICY_BADPASS));144pp.write(new FileWriter(BADPASS_FILE, false));145146// next load keystore data to build PD's147148KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());149ks.load(new FileInputStream150(System.getProperty("test.src", ".") +151File.separatorChar +152"TokenStore.keystore"),153storePassword);154155NO_STORE_DOMAIN = new ProtectionDomain156(new CodeSource(new URL("file:/foo"),157(java.security.cert.Certificate[]) null),158null, // perms159null, // class loader160null); // principals161162Certificate[] chain = (Certificate[])163ks.getCertificateChain("POLICY_URL");164URL_DOMAIN = new ProtectionDomain165(new CodeSource(new URL("file:/foo"), chain),166null, // perms167null, // class loader168null); // principals169170chain = (Certificate[])171ks.getCertificateChain("POLICY_URL_T");172URL_T_DOMAIN = new ProtectionDomain173(new CodeSource(new URL("file:/foo"), chain),174null, // perms175null, // class loader176null); // principals177178chain = (Certificate[])179ks.getCertificateChain("POLICY_URL_T_P");180URL_T_P_DOMAIN = new ProtectionDomain181(new CodeSource(new URL("file:/foo"), chain),182null, // perms183null, // class loader184null); // principals185}186187public static void main(String[] args) throws Exception {188189init();190191// test no key store in policy192193System.setProperty("java.security.policy", "=" + NO_STORE_FILE);194PolicyFile p = new PolicyFile();195checkPerm(p, NO_STORE_DOMAIN);196197// test policy keystore + URL198199System.setProperty("java.security.policy", "=" + URL_FILE);200p = new PolicyFile();201checkPerm(p, URL_DOMAIN);202203// test policy keystore + URL + type204205System.setProperty("java.security.policy", "=" + URL_T_FILE);206p = new PolicyFile();207checkPerm(p, URL_T_DOMAIN);208209// test policy keystore + URL + type + provider210211System.setProperty("java.security.policy", "=" + URL_T_P_FILE);212p = new PolicyFile();213checkPerm(p, URL_T_P_DOMAIN);214215// test policy keystore + URL + password216217System.setProperty("java.security.policy", "=" + URL_FILE);218p = new PolicyFile();219checkPerm(p, URL_DOMAIN);220221// test policy keystore + URL + type + provider + password222223System.setProperty("java.security.policy", "=" + URL_T_P_FILE);224p = new PolicyFile();225checkPerm(p, URL_T_P_DOMAIN);226227// test policy keystore + URL + type + provider + BAD password228229System.setProperty("java.security.policy", "=" + BADPASS_FILE);230p = new PolicyFile();231try {232checkPerm(p, URL_T_P_DOMAIN);233throw new RuntimeException("expected SecurityException");234} catch (SecurityException se) {235// good236//se.printStackTrace();237}238239// test policy keystore + URL + type + provider + RELATIVE password240241System.setProperty("java.security.policy", "=" + RELPASS_FILE);242p = new PolicyFile();243checkPerm(p, URL_T_P_DOMAIN);244}245246private static void checkPerm(PolicyFile p, ProtectionDomain pd)247throws Exception {248boolean foundIt = false;249Enumeration perms = p.getPermissions(pd).elements();250while (perms.hasMoreElements()) {251Permission perm = (Permission)perms.nextElement();252if (perm instanceof AllPermission) {253foundIt = true;254break;255}256}257if (!foundIt) {258throw new SecurityException("expected AllPermission");259}260}261}262263264