Path: blob/jdk8u272-b10-aarch32-20201026/jdk/test/lib/security/SecurityUtils.java
48785 views
/*1* Copyright (c) 2018, 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.io.FileInputStream;25import java.security.KeyStore;26import java.security.Security;27import java.util.Arrays;28import java.util.Collections;29import java.util.List;30import java.util.stream.Collectors;3132/**33* Common library for various security test helper functions.34*/35public final class SecurityUtils {3637private static String getCacerts() {38String sep = File.separator;39return System.getProperty("java.home") + sep40+ "lib" + sep + "security" + sep + "cacerts";41}4243/**44* Returns the cacerts keystore with the configured CA certificates.45*/46public static KeyStore getCacertsKeyStore() throws Exception {47File file = new File(getCacerts());48if (!file.exists()) {49return null;50}5152KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());53try (FileInputStream fis = new FileInputStream(file)) {54ks.load(fis, null);55}56return ks;57}5859/**60* Removes the specified protocols from the jdk.tls.disabledAlgorithms61* security property.62*/63public static void removeFromDisabledTlsAlgs(String... protocols) {64List<String> protocolsList = Arrays.asList(protocols);65protocolsList = Collections.unmodifiableList(protocolsList);66removeFromDisabledAlgs("jdk.tls.disabledAlgorithms",67protocolsList);68}6970private static void removeFromDisabledAlgs(String prop, List<String> algs) {71String value = Security.getProperty(prop);72value = Arrays.stream(value.split(","))73.map(s -> s.trim())74.filter(s -> !algs.contains(s))75.collect(Collectors.joining(","));76Security.setProperty(prop, value);77}7879private SecurityUtils() {}80}818283