1 /* 2 * Copyright 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package org.conscrypt; 18 19 import java.lang.reflect.Method; 20 import javax.net.ssl.SSLParameters; 21 22 /** 23 * Utility methods supported on Java 9+. 24 */ 25 final class Java9PlatformUtil { 26 // TODO(nmittler): Remove reflection once we require Java 9 for building. 27 private static final Method SSL_PARAMETERS_GET_APPLICATION_PROTOCOLS_METHOD; 28 private static final Method SSL_PARAMETERS_SET_APPLICATION_PROTOCOLS_METHOD; 29 30 static { 31 Class<?> sslParameters = SSLParameters.class; 32 Method getApplicationProtocolsMethod; 33 Method setApplicationProtocolsMethod; 34 try { 35 getApplicationProtocolsMethod = sslParameters.getMethod("getApplicationProtocols"); 36 setApplicationProtocolsMethod = 37 sslParameters.getMethod("setApplicationProtocols", String[].class); 38 } catch (NoSuchMethodException e) { 39 getApplicationProtocolsMethod = null; 40 setApplicationProtocolsMethod = null; 41 } 42 43 SSL_PARAMETERS_GET_APPLICATION_PROTOCOLS_METHOD = getApplicationProtocolsMethod; 44 SSL_PARAMETERS_SET_APPLICATION_PROTOCOLS_METHOD = setApplicationProtocolsMethod; 45 } 46 47 static void setSSLParameters( 48 SSLParameters src, SSLParametersImpl dest, AbstractConscryptSocket socket) { 49 Java8PlatformUtil.setSSLParameters(src, dest, socket); 50 51 dest.setApplicationProtocols(getApplicationProtocols(src)); 52 } 53 54 static void getSSLParameters( 55 SSLParameters dest, SSLParametersImpl src, AbstractConscryptSocket socket) { 56 Java8PlatformUtil.getSSLParameters(dest, src, socket); 57 58 setApplicationProtocols(dest, src.getApplicationProtocols()); 59 } 60 61 static void setSSLParameters( 62 SSLParameters src, SSLParametersImpl dest, ConscryptEngine engine) { 63 Java8PlatformUtil.setSSLParameters(src, dest, engine); 64 65 dest.setApplicationProtocols(getApplicationProtocols(src)); 66 } 67 68 static void getSSLParameters( 69 SSLParameters dest, SSLParametersImpl src, ConscryptEngine engine) { 70 Java8PlatformUtil.getSSLParameters(dest, src, engine); 71 72 setApplicationProtocols(dest, src.getApplicationProtocols()); 73 } 74 75 private static String[] getApplicationProtocols(SSLParameters params) { 76 if (SSL_PARAMETERS_GET_APPLICATION_PROTOCOLS_METHOD != null) { 77 try { 78 return (String[]) SSL_PARAMETERS_GET_APPLICATION_PROTOCOLS_METHOD.invoke(params); 79 } catch (ReflectiveOperationException ignored) { 80 // TODO(nmittler): Should we throw here? 81 } 82 } 83 return EmptyArray.STRING; 84 } 85 86 private static void setApplicationProtocols(SSLParameters params, String[] protocols) { 87 if (SSL_PARAMETERS_SET_APPLICATION_PROTOCOLS_METHOD != null) { 88 try { 89 SSL_PARAMETERS_SET_APPLICATION_PROTOCOLS_METHOD.invoke(params, (Object) protocols); 90 } catch (ReflectiveOperationException ignored) { 91 // TODO(nmittler): Should we throw here? 92 } 93 } 94 } 95 96 private Java9PlatformUtil() {} 97 } 98