Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/security/util/PolicyUtil.java
38830 views
/*1* Copyright (c) 2004, 2006, 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. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.security.util;2627import java.io.*;28import java.net.*;29import java.security.*;30import java.util.Arrays;3132import sun.net.www.ParseUtil;333435/**36* A utility class for getting a KeyStore instance from policy information.37* In addition, a supporting getInputStream method.38*39*/40public class PolicyUtil {4142// standard PKCS11 KeyStore type43private static final String P11KEYSTORE = "PKCS11";4445// reserved word46private static final String NONE = "NONE";4748/*49* Fast path reading from file urls in order to avoid calling50* FileURLConnection.connect() which can be quite slow the first time51* it is called. We really should clean up FileURLConnection so that52* this is not a problem but in the meantime this fix helps reduce53* start up time noticeably for the new launcher. -- DAC54*/55public static InputStream getInputStream(URL url) throws IOException {56if ("file".equals(url.getProtocol())) {57String path = url.getFile().replace('/', File.separatorChar);58path = ParseUtil.decode(path);59return new FileInputStream(path);60} else {61return url.openStream();62}63}6465/**66* this is intended for use by policytool and the policy parser to67* instantiate a KeyStore from the information in the GUI/policy file68*/69public static KeyStore getKeyStore70(URL policyUrl, // URL of policy file71String keyStoreName, // input: keyStore URL72String keyStoreType, // input: keyStore type73String keyStoreProvider, // input: keyStore provider74String storePassURL, // input: keyStore password75Debug debug)76throws KeyStoreException, MalformedURLException, IOException,77NoSuchProviderException, NoSuchAlgorithmException,78java.security.cert.CertificateException {7980if (keyStoreName == null) {81throw new IllegalArgumentException("null KeyStore name");82}8384char[] keyStorePassword = null;85try {86KeyStore ks;87if (keyStoreType == null) {88keyStoreType = KeyStore.getDefaultType();89}9091if (P11KEYSTORE.equalsIgnoreCase(keyStoreType) &&92!NONE.equals(keyStoreName)) {93throw new IllegalArgumentException94("Invalid value (" +95keyStoreName +96") for keystore URL. If the keystore type is \"" +97P11KEYSTORE +98"\", the keystore url must be \"" +99NONE +100"\"");101}102103if (keyStoreProvider != null) {104ks = KeyStore.getInstance(keyStoreType, keyStoreProvider);105} else {106ks = KeyStore.getInstance(keyStoreType);107}108109if (storePassURL != null) {110URL passURL;111try {112passURL = new URL(storePassURL);113// absolute URL114} catch (MalformedURLException e) {115// relative URL116if (policyUrl == null) {117throw e;118}119passURL = new URL(policyUrl, storePassURL);120}121122if (debug != null) {123debug.println("reading password"+passURL);124}125126InputStream in = null;127try {128in = passURL.openStream();129keyStorePassword = Password.readPassword(in);130} finally {131if (in != null) {132in.close();133}134}135}136137if (NONE.equals(keyStoreName)) {138ks.load(null, keyStorePassword);139return ks;140} else {141/*142* location of keystore is specified as absolute URL in policy143* file, or is relative to URL of policy file144*/145URL keyStoreUrl = null;146try {147keyStoreUrl = new URL(keyStoreName);148// absolute URL149} catch (MalformedURLException e) {150// relative URL151if (policyUrl == null) {152throw e;153}154keyStoreUrl = new URL(policyUrl, keyStoreName);155}156157if (debug != null) {158debug.println("reading keystore"+keyStoreUrl);159}160161InputStream inStream = null;162try {163inStream =164new BufferedInputStream(getInputStream(keyStoreUrl));165ks.load(inStream, keyStorePassword);166} finally {167inStream.close();168}169return ks;170}171} finally {172if (keyStorePassword != null) {173Arrays.fill(keyStorePassword, ' ');174}175}176}177}178179180