Path: blob/master/test/jdk/javax/net/ssl/compatibility/JdkInfoUtils.java
41152 views
/*1* Copyright (c) 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.security.NoSuchAlgorithmException;2425import javax.net.ssl.SSLContext;26import javax.net.ssl.SSLParameters;2728/*29* This class is used for returning some specific JDK information.30*/31public class JdkInfoUtils {3233public static final String JAVA_RUNTIME_VERSION = "javaRuntimeVersion";34public static final String SUPPORTED_PROTOCOLS = "supportedProtocols";35public static final String ENABLED_PROTOCOLS = "enabledProtocols";36public static final String SUPPORTED_CIPHER_SUITES = "supportedCipherSuites";37public static final String ENABLED_CIPHER_SUITES = "enabledCipherSuites";38public static final String SUPPORTS_SNI = "supportsSNI";39public static final String SUPPORTS_ALPN = "supportsALPN";4041// Returns the JDK build version.42public static String javaRuntimeVersion() {43return System.getProperty("java.runtime.version");44}4546private static String supportedProtocols()47throws NoSuchAlgorithmException {48String[] protocols = SSLContext.getDefault()49.createSSLEngine().getSupportedProtocols();50return Utilities.join(Utilities.VALUE_DELIMITER, protocols).toString();51}5253private static String enabledProtocols()54throws NoSuchAlgorithmException {55String[] protocols = SSLContext.getDefault()56.createSSLEngine().getEnabledProtocols();57return Utilities.join(Utilities.VALUE_DELIMITER, protocols).toString();58}5960private static String supportedCipherSuites()61throws NoSuchAlgorithmException {62String[] supportedCipherSuites = SSLContext.getDefault()63.createSSLEngine().getSupportedCipherSuites();64return Utilities.join(Utilities.VALUE_DELIMITER, supportedCipherSuites)65.toString();66}6768private static String enabledCipherSuites()69throws NoSuchAlgorithmException {70String[] enabledCipherSuites = SSLContext.getDefault()71.createSSLEngine().getEnabledCipherSuites();72return Utilities.join(Utilities.VALUE_DELIMITER, enabledCipherSuites)73.toString();74}7576// Checks if SNI is supported by the JDK build.77private static boolean supportsSNI() {78boolean isSupported = true;79try {80SSLParameters.class.getMethod("getServerNames");81} catch (NoSuchMethodException e) {82isSupported = false;83}84return isSupported;85}8687// Checks if ALPN is supported by the JDK build.88private static boolean supportsALPN() {89boolean isSupported = true;90try {91SSLParameters.class.getMethod("getApplicationProtocols");92} catch (NoSuchMethodException e) {93isSupported = false;94}95return isSupported;96}9798public static void main(String[] args) throws NoSuchAlgorithmException {99System.out.print(Utilities.join(Utilities.PARAM_DELIMITER,100attr(JAVA_RUNTIME_VERSION, javaRuntimeVersion()),101attr(SUPPORTED_PROTOCOLS, supportedProtocols()),102attr(ENABLED_PROTOCOLS, enabledProtocols()),103attr(SUPPORTED_CIPHER_SUITES, supportedCipherSuites()),104attr(ENABLED_CIPHER_SUITES, enabledCipherSuites()),105attr(SUPPORTS_SNI, supportsSNI()),106attr(SUPPORTS_ALPN, supportsALPN())));107}108109private static String attr(String name, Object value) {110return name + "=" + String.valueOf(value);111}112}113114115