Path: blob/master/test/jdk/sun/security/provider/PolicyParser/TokenStore.java
41152 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* @modules java.base/sun.security.provider28*/2930import java.io.*;31import java.util.*;32import sun.security.provider.*;3334public class TokenStore {3536private static final String POLICY_NO_STORE =37"grant { permission java.security.AllPermission; };";3839private static final String POLICY_URL =40"keystore \"file:${test.src}${/}TokenStore.keystore\";" +41"grant signedby \"POLICY_URL\" {" +42" permission java.security.AllPermission;" +43"};" ;4445private static final String POLICY_URL_T =46"keystore \"file:${test.src}${/}TokenStore.keystore\", \"JKS\";"+47"grant signedby \"POLICY_URL_T\" {" +48" permission java.security.AllPermission;" +49"};" ;5051private static final String POLICY_URL_T_P =52"keystore \"file:${test.src}${/}TokenStore.keystore\"," +53" \"JKS\", \"SUN\";" +54"grant signedby \"POLICY_URL_T_P\" {" +55" permission java.security.AllPermission;" +56"};" ;5758private static final String POLICY_URL_PWD =59"keystore \"file:${test.src}${/}TokenStore.keystore\";" +60"keystorePasswordURL \"file:${test.src}${/}TokenStore.pwd\";" +61"grant signedby \"POLICY_URL\" {" +62" permission java.security.AllPermission;" +63"};" ;6465private static final String POLICY_URL_T_P_PWD =66"keystore \"file:${test.src}${/}TokenStore.keystore\"," +67" \"JKS\", \"SUN\";" +68"keystorePasswordURL \"file:${test.src}${/}TokenStore.pwd\";" +69"grant signedby \"POLICY_URL_T_P\" {" +70" permission java.security.AllPermission;" +71"};" ;7273private static final String POLICY_PASS_NO_STORE =74"keystorePasswordURL \"file:${test.src}${/}TokenStore.pwd\";" +75"grant signedby \"POLICY_URL_T_P\" {" +76" permission java.security.AllPermission;" +77"};" ;7879public static void main(String[] args) throws Exception {8081// test no key store in policy8283PolicyParser p = new PolicyParser();84p.read(new StringReader(POLICY_NO_STORE));85doNoStore(p);86StringWriter sw = new StringWriter();87p.write(sw);88PolicyParser newP = new PolicyParser();89newP.read(new StringReader(sw.toString()));90doNoStore(p);9192// test policy keystore + URL9394p = new PolicyParser();95p.read(new StringReader(POLICY_URL));96doURL(p, true);97sw = new StringWriter();98p.write(sw);99newP = new PolicyParser();100newP.read(new StringReader(sw.toString()));101doURL(p, true);102103// test policy keystore + URL + type104105p = new PolicyParser();106p.read(new StringReader(POLICY_URL_T));107doURL_T(p, true);108sw = new StringWriter();109p.write(sw);110newP = new PolicyParser();111newP.read(new StringReader(sw.toString()));112doURL_T(p, true);113114// test policy keystore + URL + type + provider115116p = new PolicyParser();117p.read(new StringReader(POLICY_URL_T_P));118doURL_T_P(p, true);119sw = new StringWriter();120p.write(sw);121newP = new PolicyParser();122newP.read(new StringReader(sw.toString()));123doURL_T_P(p, true);124125// test policy keystore + URL + password126127p = new PolicyParser();128p.read(new StringReader(POLICY_URL_PWD));129doURL(p, false);130doPwd(p);131sw = new StringWriter();132p.write(sw);133newP = new PolicyParser();134newP.read(new StringReader(sw.toString()));135doURL(p, false);136doPwd(p);137138// test policy keystore + URL + type + provider + password139140p = new PolicyParser();141p.read(new StringReader(POLICY_URL_T_P_PWD));142doURL_T_P(p, false);143doPwd(p);144sw = new StringWriter();145p.write(sw);146newP = new PolicyParser();147newP.read(new StringReader(sw.toString()));148doURL_T_P(p, false);149doPwd(p);150151// test policy password with no keystore152p = new PolicyParser();153try {154p.read(new StringReader(POLICY_PASS_NO_STORE));155throw new SecurityException("expected parsing exception");156} catch (PolicyParser.ParsingException pe) {157// good158}159160}161162private static void checkPerm(PolicyParser p) throws Exception {163Enumeration e = p.grantElements();164boolean foundOne = false;165while (e.hasMoreElements()) {166PolicyParser.GrantEntry ge = (PolicyParser.GrantEntry)167e.nextElement();168if (ge.permissionEntries == null) {169throw new SecurityException("expected non-null perms");170} else {171foundOne = true;172}173}174if (!foundOne) {175throw new SecurityException("expected non-null grant entries");176}177}178179private static void doNoStore(PolicyParser p) throws Exception {180if (p.getKeyStoreUrl() != null ||181p.getKeyStoreType() != null ||182p.getKeyStoreProvider() != null ||183p.getStorePassURL() != null) {184throw new SecurityException("expected null keystore");185}186checkPerm(p);187}188189private static void doURL(PolicyParser p, boolean checkPwd)190throws Exception {191if (p.getKeyStoreUrl() == null ||192!(p.getKeyStoreUrl().endsWith("TokenStore.keystore")) ||193p.getKeyStoreType() != null ||194p.getKeyStoreProvider() != null) {195throw new SecurityException("invalid keystore values");196}197if (checkPwd) {198if (p.getStorePassURL() != null) {199throw new SecurityException("invalid keystore values");200}201}202checkPerm(p);203}204205private static void doURL_T(PolicyParser p, boolean checkPwd)206throws Exception {207if (p.getKeyStoreUrl() == null ||208!(p.getKeyStoreUrl().endsWith("TokenStore.keystore")) ||209p.getKeyStoreType() == null ||210!(p.getKeyStoreType().equals("JKS")) ||211p.getKeyStoreProvider() != null) {212throw new SecurityException("invalid keystore values");213}214if (checkPwd) {215if (p.getStorePassURL() != null) {216throw new SecurityException("invalid keystore values");217}218}219checkPerm(p);220}221222private static void doURL_T_P(PolicyParser p, boolean checkPwd)223throws Exception {224if (p.getKeyStoreUrl() == null ||225!(p.getKeyStoreUrl().endsWith("TokenStore.keystore")) ||226p.getKeyStoreType() == null ||227!(p.getKeyStoreType().equals("JKS")) ||228p.getKeyStoreProvider() == null ||229!(p.getKeyStoreProvider().equals("SUN"))) {230throw new SecurityException("invalid keystore values");231}232if (checkPwd) {233if (p.getStorePassURL() != null) {234throw new SecurityException("invalid keystore values");235}236}237checkPerm(p);238}239240private static void doPwd(PolicyParser p) throws Exception {241if (p.getStorePassURL() == null ||242!(p.getStorePassURL().endsWith("TokenStore.pwd"))) {243throw new SecurityException("invalid password values");244}245}246}247248249