1 /*
2  * Copyright (C) 2018 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 android.net.ssl;
18 
19 import com.android.org.conscrypt.Conscrypt;
20 import javax.net.ssl.SSLSocket;
21 import libcore.util.NonNull;
22 
23 /**
24  * Static utility methods for accessing additional functionality of supported instances of
25  * {@link SSLSocket}.  Sockets from the platform TLS provider will be compatible with all
26  * methods in this class.
27  */
28 public class SSLSockets {
SSLSockets()29     private SSLSockets() {}
30 
31     /**
32      * Returns whether the given socket can be used with the methods in this class.  In general,
33      * only sockets from the platform TLS provider are supported.
34      */
isSupportedSocket(@onNull SSLSocket socket)35     public static boolean isSupportedSocket(@NonNull SSLSocket socket) {
36         return Conscrypt.isConscrypt(socket);
37     }
38 
checkSupported(@onNull SSLSocket s)39     private static void checkSupported(@NonNull SSLSocket s) {
40         if (!isSupportedSocket(s)) {
41             throw new IllegalArgumentException("Socket is not a supported socket.");
42         }
43     }
44 
45     /**
46      * Enables or disables the use of session tickets.
47      *
48      * <p>This function must be called before the handshake is started or it will have no effect.
49      *
50      * @param socket the socket
51      * @param useSessionTickets whether to enable or disable the use of session tickets
52      * @throws IllegalArgumentException if the given socket is not a platform socket
53      */
setUseSessionTickets(@onNull SSLSocket socket, boolean useSessionTickets)54     public static void setUseSessionTickets(@NonNull SSLSocket socket, boolean useSessionTickets) {
55         checkSupported(socket);
56         Conscrypt.setUseSessionTickets(socket, useSessionTickets);
57     }
58 }
59