Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/net/ssl/compatibility/JdkUtils.java
38853 views
/*1* Copyright (c) 2017, 2019, 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.security.NoSuchAlgorithmException;2425import javax.net.ssl.SSLContext;26import javax.net.ssl.SSLParameters;27import javax.net.ssl.SSLSocketFactory;2829/*30* This class is used for returning some specific JDK information.31*/32public class JdkUtils {3334public static final String JAVA_RUNTIME_VERSION = "javaRuntimeVersion";35public static final String SUPPORTED_PROTOCOLS = "supportedProtocols";36public static final String SUPPORTED_CIPHER_SUITES = "supportedCipherSuites";37public static final String SUPPORTS_SNI = "supportsSNI";38public static final String SUPPORTS_ALPN = "supportsALPN";3940// Returns the JDK build version.41public static String javaRuntimeVersion() {42return System.getProperty("java.runtime.version");43}4445private static String supportedProtocols() {46StringBuilder protocols = new StringBuilder();47for (String protocol : new String[] {48"TLSv1", "TLSv1.1", "TLSv1.2", "TLSv1.3" }) {49if (supportsProtocol(protocol)) {50protocols.append(protocol).append(Utils.VALUE_DELIMITER);51}52}53return protocols.toString().substring(540, protocols.toString().length() - 1);55}5657private static boolean supportsProtocol(String protocol) {58boolean supported = true;59try {60SSLContext.getInstance(protocol);61} catch (NoSuchAlgorithmException e) {62supported = false;63}64return supported;65}6667private static String supportedCipherSuites() {68StringBuilder cipherSuites = new StringBuilder();69String[] supportedCipherSuites = ((SSLSocketFactory) SSLSocketFactory70.getDefault()).getSupportedCipherSuites();71for (int i = 0; i < supportedCipherSuites.length - 1; i++) {72cipherSuites.append(supportedCipherSuites[i])73.append(Utils.VALUE_DELIMITER);74}75cipherSuites.append(76supportedCipherSuites[supportedCipherSuites.length - 1]);77return cipherSuites.toString();78}7980// Checks if SNI is supported by the JDK build.81private static boolean supportsSNI() {82boolean isSupported = true;83try {84SSLParameters.class.getMethod("getServerNames");85} catch (NoSuchMethodException e) {86isSupported = false;87}88return isSupported;89}9091// Checks if ALPN is supported by the JDK build.92private static boolean supportsALPN() {93boolean isSupported = true;94try {95SSLParameters.class.getMethod("getApplicationProtocols");96} catch (NoSuchMethodException e) {97isSupported = false;98}99return isSupported;100}101102public static void main(String[] args) throws NoSuchAlgorithmException {103System.out.print(Utils.join(Utils.PARAM_DELIMITER,104attr(JAVA_RUNTIME_VERSION, javaRuntimeVersion()),105attr(SUPPORTED_PROTOCOLS, supportedProtocols()),106attr(SUPPORTED_CIPHER_SUITES, supportedCipherSuites()),107attr(SUPPORTS_SNI, supportsSNI()),108attr(SUPPORTS_ALPN, supportsALPN())));109}110111private static String attr(String name, Object value) {112return name + "=" + String.valueOf(value);113}114}115116117