1 /* 2 * Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 import java.security.NoSuchAlgorithmException; 25 26 import javax.net.ssl.SSLContext; 27 import javax.net.ssl.SSLParameters; 28 import javax.net.ssl.SSLSocketFactory; 29 30 /* 31 * This class is used for returning some specific JDK information. 32 */ 33 public class JdkUtils { 34 35 public static final String JAVA_RUNTIME_VERSION = "javaRuntimeVersion"; 36 public static final String SUPPORTED_PROTOCOLS = "supportedProtocols"; 37 public static final String SUPPORTED_CIPHER_SUITES = "supportedCipherSuites"; 38 public static final String SUPPORTS_SNI = "supportsSNI"; 39 public static final String SUPPORTS_ALPN = "supportsALPN"; 40 41 // Returns the JDK build version. 42 public static String javaRuntimeVersion() { 43 return System.getProperty("java.runtime.version"); 44 } 45 46 private static String supportedProtocols() { 47 StringBuilder protocols = new StringBuilder(); 48 for (String protocol : new String[] { 49 "TLSv1", "TLSv1.1", "TLSv1.2", "TLSv1.3" }) { 50 if (supportsProtocol(protocol)) { 51 protocols.append(protocol).append(Utils.VALUE_DELIMITER); 52 } 53 } 54 return protocols.toString().substring( 55 0, protocols.toString().length() - 1); 56 } 57 58 private static boolean supportsProtocol(String protocol) { 59 boolean supported = true; 60 try { 61 SSLContext.getInstance(protocol); 62 } catch (NoSuchAlgorithmException e) { 63 supported = false; 64 } 65 return supported; 66 } 67 68 private static String supportedCipherSuites() { 69 StringBuilder cipherSuites = new StringBuilder(); 70 String[] supportedCipherSuites = ((SSLSocketFactory) SSLSocketFactory 71 .getDefault()).getSupportedCipherSuites(); 72 for (int i = 0; i < supportedCipherSuites.length - 1; i++) { 73 cipherSuites.append(supportedCipherSuites[i]) 74 .append(Utils.VALUE_DELIMITER); 75 } 76 cipherSuites.append( 77 supportedCipherSuites[supportedCipherSuites.length - 1]); 78 return cipherSuites.toString(); 79 } 80 81 // Checks if SNI is supported by the JDK build. 82 private static boolean supportsSNI() { 83 boolean isSupported = true; 84 try { 85 SSLParameters.class.getMethod("getServerNames"); 86 } catch (NoSuchMethodException e) { 87 isSupported = false; 88 } 89 return isSupported; 90 } 91 92 // Checks if ALPN is supported by the JDK build. 93 private static boolean supportsALPN() { 94 boolean isSupported = true; 95 try { 96 SSLParameters.class.getMethod("getApplicationProtocols"); 97 } catch (NoSuchMethodException e) { 98 isSupported = false; 99 } 100 return isSupported; 101 } 102 103 public static void main(String[] args) throws NoSuchAlgorithmException { 104 System.out.print(Utils.join(Utils.PARAM_DELIMITER, 105 attr(JAVA_RUNTIME_VERSION, javaRuntimeVersion()), 106 attr(SUPPORTED_PROTOCOLS, supportedProtocols()), 107 attr(SUPPORTED_CIPHER_SUITES, supportedCipherSuites()), 108 attr(SUPPORTS_SNI, supportsSNI()), 109 attr(SUPPORTS_ALPN, supportsALPN()))); 110 } 111 112 private static String attr(String name, Object value) { 113 return name + "=" + String.valueOf(value); 114 } 115 }