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 static javax.net.ssl.StandardConstants.SNI_HOST_NAME;
20 
21 import java.util.Collections;
22 import java.util.List;
23 import javax.net.ssl.SNIHostName;
24 import javax.net.ssl.SNIServerName;
25 import javax.net.ssl.SSLEngine;
26 import javax.net.ssl.SSLParameters;
27 import javax.net.ssl.SSLSession;
28 
29 /**
30  * Utility methods supported on Java 8+.
31  */
32 final class Java8PlatformUtil {
setSSLParameters( SSLParameters params, SSLParametersImpl impl, AbstractConscryptSocket socket)33     static void setSSLParameters(
34             SSLParameters params, SSLParametersImpl impl, AbstractConscryptSocket socket) {
35         impl.setEndpointIdentificationAlgorithm(params.getEndpointIdentificationAlgorithm());
36         impl.setUseCipherSuitesOrder(params.getUseCipherSuitesOrder());
37         List<SNIServerName> serverNames = params.getServerNames();
38 
39         if (serverNames != null) {
40             for (SNIServerName serverName : serverNames) {
41                 if (serverName.getType() == SNI_HOST_NAME) {
42                     socket.setHostname(((SNIHostName) serverName).getAsciiName());
43                     break;
44                 }
45             }
46         }
47     }
48 
getSSLParameters( SSLParameters params, SSLParametersImpl impl, AbstractConscryptSocket socket)49     static void getSSLParameters(
50             SSLParameters params, SSLParametersImpl impl, AbstractConscryptSocket socket) {
51         params.setEndpointIdentificationAlgorithm(impl.getEndpointIdentificationAlgorithm());
52         params.setUseCipherSuitesOrder(impl.getUseCipherSuitesOrder());
53         if (impl.getUseSni() && AddressUtils.isValidSniHostname(socket.getHostname())) {
54             params.setServerNames(Collections.singletonList(
55                     (SNIServerName) new SNIHostName(socket.getHostname())));
56         }
57     }
58 
setSSLParameters( SSLParameters params, SSLParametersImpl impl, ConscryptEngine engine)59     static void setSSLParameters(
60             SSLParameters params, SSLParametersImpl impl, ConscryptEngine engine) {
61         impl.setEndpointIdentificationAlgorithm(params.getEndpointIdentificationAlgorithm());
62         impl.setUseCipherSuitesOrder(params.getUseCipherSuitesOrder());
63         List<SNIServerName> serverNames = params.getServerNames();
64 
65         if (serverNames != null) {
66             for (SNIServerName serverName : serverNames) {
67                 if (serverName.getType() == SNI_HOST_NAME) {
68                     engine.setHostname(((SNIHostName) serverName).getAsciiName());
69                     break;
70                 }
71             }
72         }
73     }
getSSLParameters( SSLParameters params, SSLParametersImpl impl, ConscryptEngine engine)74     static void getSSLParameters(
75             SSLParameters params, SSLParametersImpl impl, ConscryptEngine engine) {
76         params.setEndpointIdentificationAlgorithm(impl.getEndpointIdentificationAlgorithm());
77         params.setUseCipherSuitesOrder(impl.getUseCipherSuitesOrder());
78         if (impl.getUseSni() && AddressUtils.isValidSniHostname(engine.getHostname())) {
79             params.setServerNames(Collections.singletonList(
80                     (SNIServerName) new SNIHostName(engine.getHostname())));
81         }
82     }
83 
wrapEngine(ConscryptEngine engine)84     static SSLEngine wrapEngine(ConscryptEngine engine) {
85         return new Java8EngineWrapper(engine);
86     }
87 
unwrapEngine(SSLEngine engine)88     static SSLEngine unwrapEngine(SSLEngine engine) {
89         return Java8EngineWrapper.getDelegate(engine);
90     }
91 
wrapSSLSession(ExternalSession sslSession)92     static SSLSession wrapSSLSession(ExternalSession sslSession) {
93         return new Java8ExtendedSSLSession(sslSession);
94     }
95 
Java8PlatformUtil()96     private Java8PlatformUtil() {}
97 }
98