blob: 27096b182c7fb69dd772b2c9a617134dc95db026 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2008 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
17package android.net;
18
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080019import android.os.SystemProperties;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import android.util.Log;
Kenny Root12e75222013-04-23 22:34:24 -070021import com.android.org.conscrypt.OpenSSLContextImpl;
22import com.android.org.conscrypt.OpenSSLSocketImpl;
23import com.android.org.conscrypt.SSLClientSessionCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import java.io.IOException;
25import java.net.InetAddress;
26import java.net.Socket;
Brian Carlstrom7ab7a8b2012-09-21 16:33:50 -070027import java.net.SocketException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import java.security.KeyManagementException;
Alex Klyubinac5eb032013-03-12 10:30:59 -070029import java.security.PrivateKey;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import java.security.cert.X509Certificate;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031import javax.net.SocketFactory;
Dan Egnor7fc93c32010-06-29 17:51:28 -070032import javax.net.ssl.HostnameVerifier;
33import javax.net.ssl.HttpsURLConnection;
Ben Komalo1b528062011-05-03 09:40:15 -070034import javax.net.ssl.KeyManager;
Dan Egnor7fc93c32010-06-29 17:51:28 -070035import javax.net.ssl.SSLException;
36import javax.net.ssl.SSLPeerUnverifiedException;
37import javax.net.ssl.SSLSession;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import javax.net.ssl.SSLSocket;
39import javax.net.ssl.SSLSocketFactory;
40import javax.net.ssl.TrustManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import javax.net.ssl.X509TrustManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042
43/**
Dan Egnor60586f22010-02-08 21:56:38 -080044 * SSLSocketFactory implementation with several extra features:
Dan Egnor7fc93c32010-06-29 17:51:28 -070045 *
Dan Egnor60586f22010-02-08 21:56:38 -080046 * <ul>
47 * <li>Timeout specification for SSL handshake operations
Dan Egnor7fc93c32010-06-29 17:51:28 -070048 * <li>Hostname verification in most cases (see WARNINGs below)
Dan Egnor60586f22010-02-08 21:56:38 -080049 * <li>Optional SSL session caching with {@link SSLSessionCache}
Dan Egnor9d4b5752010-02-13 20:34:51 -080050 * <li>Optionally bypass all SSL certificate checks
Dan Egnor60586f22010-02-08 21:56:38 -080051 * </ul>
Dan Egnor7fc93c32010-06-29 17:51:28 -070052 *
53 * The handshake timeout does not apply to actual TCP socket connection.
54 * If you want a connection timeout as well, use {@link #createSocket()}
55 * and {@link Socket#connect(SocketAddress, int)}, after which you
56 * must verify the identity of the server you are connected to.
57 *
58 * <p class="caution"><b>Most {@link SSLSocketFactory} implementations do not
59 * verify the server's identity, allowing man-in-the-middle attacks.</b>
60 * This implementation does check the server's certificate hostname, but only
61 * for createSocket variants that specify a hostname. When using methods that
62 * use {@link InetAddress} or which return an unconnected socket, you MUST
63 * verify the server's identity yourself to ensure a secure connection.</p>
64 *
65 * <p>One way to verify the server's identity is to use
66 * {@link HttpsURLConnection#getDefaultHostnameVerifier()} to get a
67 * {@link HostnameVerifier} to verify the certificate hostname.
68 *
69 * <p>On development devices, "setprop socket.relaxsslcheck yes" bypasses all
70 * SSL certificate and hostname checks for testing purposes. This setting
71 * requires root access.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072 */
73public class SSLCertificateSocketFactory extends SSLSocketFactory {
Dan Egnor60586f22010-02-08 21:56:38 -080074 private static final String TAG = "SSLCertificateSocketFactory";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075
Dan Egnor60586f22010-02-08 21:56:38 -080076 private static final TrustManager[] INSECURE_TRUST_MANAGER = new TrustManager[] {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077 new X509TrustManager() {
Dan Egnor60586f22010-02-08 21:56:38 -080078 public X509Certificate[] getAcceptedIssuers() { return null; }
79 public void checkClientTrusted(X509Certificate[] certs, String authType) { }
80 public void checkServerTrusted(X509Certificate[] certs, String authType) { }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081 }
82 };
83
Dan Egnor60586f22010-02-08 21:56:38 -080084 private SSLSocketFactory mInsecureFactory = null;
85 private SSLSocketFactory mSecureFactory = null;
Ben Komalo1b528062011-05-03 09:40:15 -070086 private TrustManager[] mTrustManagers = null;
87 private KeyManager[] mKeyManagers = null;
Jesse Wilsonf5fb5e82012-03-23 15:55:03 -040088 private byte[] mNpnProtocols = null;
Kenny Root100d7292013-06-25 12:00:34 -070089 private byte[] mAlpnProtocols = null;
Alex Klyubinac5eb032013-03-12 10:30:59 -070090 private PrivateKey mChannelIdPrivateKey = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091
Dan Egnor60586f22010-02-08 21:56:38 -080092 private final int mHandshakeTimeoutMillis;
93 private final SSLClientSessionCache mSessionCache;
Dan Egnor9d4b5752010-02-13 20:34:51 -080094 private final boolean mSecure;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095
Dan Egnor60586f22010-02-08 21:56:38 -080096 /** @deprecated Use {@link #getDefault(int)} instead. */
Jesse Wilson28c74252010-11-04 14:09:18 -070097 @Deprecated
Dan Egnor60586f22010-02-08 21:56:38 -080098 public SSLCertificateSocketFactory(int handshakeTimeoutMillis) {
Dan Egnor9d4b5752010-02-13 20:34:51 -080099 this(handshakeTimeoutMillis, null, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100 }
101
Dan Egnor9d4b5752010-02-13 20:34:51 -0800102 private SSLCertificateSocketFactory(
Brian Carlstrom992f2382012-09-26 14:33:47 -0700103 int handshakeTimeoutMillis, SSLSessionCache cache, boolean secure) {
Dan Egnor60586f22010-02-08 21:56:38 -0800104 mHandshakeTimeoutMillis = handshakeTimeoutMillis;
105 mSessionCache = cache == null ? null : cache.mSessionCache;
Dan Egnor9d4b5752010-02-13 20:34:51 -0800106 mSecure = secure;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107 }
108
109 /**
Dan Egnor9d4b5752010-02-13 20:34:51 -0800110 * Returns a new socket factory instance with an optional handshake timeout.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111 *
Dan Egnor60586f22010-02-08 21:56:38 -0800112 * @param handshakeTimeoutMillis to use for SSL connection handshake, or 0
113 * for none. The socket timeout is reset to 0 after the handshake.
Dan Egnor7fc93c32010-06-29 17:51:28 -0700114 * @return a new SSLSocketFactory with the specified parameters
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115 */
Dan Egnor60586f22010-02-08 21:56:38 -0800116 public static SocketFactory getDefault(int handshakeTimeoutMillis) {
Dan Egnor9d4b5752010-02-13 20:34:51 -0800117 return new SSLCertificateSocketFactory(handshakeTimeoutMillis, null, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118 }
119
120 /**
Dan Egnor9d4b5752010-02-13 20:34:51 -0800121 * Returns a new socket factory instance with an optional handshake timeout
122 * and SSL session cache.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123 *
Dan Egnor60586f22010-02-08 21:56:38 -0800124 * @param handshakeTimeoutMillis to use for SSL connection handshake, or 0
125 * for none. The socket timeout is reset to 0 after the handshake.
Jesse Wilsonff556992011-03-23 16:43:39 -0700126 * @param cache The {@link SSLSessionCache} to use, or null for no cache.
Dan Egnor7fc93c32010-06-29 17:51:28 -0700127 * @return a new SSLSocketFactory with the specified parameters
Dan Egnor60586f22010-02-08 21:56:38 -0800128 */
Dan Egnor9d4b5752010-02-13 20:34:51 -0800129 public static SSLSocketFactory getDefault(int handshakeTimeoutMillis, SSLSessionCache cache) {
130 return new SSLCertificateSocketFactory(handshakeTimeoutMillis, cache, true);
131 }
132
133 /**
134 * Returns a new instance of a socket factory with all SSL security checks
135 * disabled, using an optional handshake timeout and SSL session cache.
Dan Egnor7fc93c32010-06-29 17:51:28 -0700136 *
137 * <p class="caution"><b>Warning:</b> Sockets created using this factory
Kenny Roote19ca072014-08-13 12:08:48 -0700138 * are vulnerable to man-in-the-middle attacks!</p>
Dan Egnor9d4b5752010-02-13 20:34:51 -0800139 *
140 * @param handshakeTimeoutMillis to use for SSL connection handshake, or 0
141 * for none. The socket timeout is reset to 0 after the handshake.
Jesse Wilsonff556992011-03-23 16:43:39 -0700142 * @param cache The {@link SSLSessionCache} to use, or null for no cache.
Dan Egnor7fc93c32010-06-29 17:51:28 -0700143 * @return an insecure SSLSocketFactory with the specified parameters
Dan Egnor9d4b5752010-02-13 20:34:51 -0800144 */
145 public static SSLSocketFactory getInsecure(int handshakeTimeoutMillis, SSLSessionCache cache) {
146 return new SSLCertificateSocketFactory(handshakeTimeoutMillis, cache, false);
Dan Egnor60586f22010-02-08 21:56:38 -0800147 }
148
149 /**
150 * Returns a socket factory (also named SSLSocketFactory, but in a different
151 * namespace) for use with the Apache HTTP stack.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 *
Dan Egnor60586f22010-02-08 21:56:38 -0800153 * @param handshakeTimeoutMillis to use for SSL connection handshake, or 0
154 * for none. The socket timeout is reset to 0 after the handshake.
Jesse Wilsonff556992011-03-23 16:43:39 -0700155 * @param cache The {@link SSLSessionCache} to use, or null for no cache.
Dan Egnor60586f22010-02-08 21:56:38 -0800156 * @return a new SocketFactory with the specified parameters
Narayan Kamath823675fd2014-10-23 17:35:01 +0100157 *
158 * @deprecated Use {@link #getDefault()} along with a {@link javax.net.ssl.HttpsURLConnection}
159 * instead. The Apache HTTP client is no longer maintained and may be removed in a future
160 * release. Please visit <a href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html">this webpage</a>
161 * for further details.
Narayan Kamath406e1ed2014-12-10 18:55:12 +0000162 *
163 * @removed
Dan Egnor60586f22010-02-08 21:56:38 -0800164 */
Narayan Kamath823675fd2014-10-23 17:35:01 +0100165 @Deprecated
Dan Egnor60586f22010-02-08 21:56:38 -0800166 public static org.apache.http.conn.ssl.SSLSocketFactory getHttpSocketFactory(
Jesse Wilsonff556992011-03-23 16:43:39 -0700167 int handshakeTimeoutMillis, SSLSessionCache cache) {
Dan Egnor60586f22010-02-08 21:56:38 -0800168 return new org.apache.http.conn.ssl.SSLSocketFactory(
Dan Egnor9d4b5752010-02-13 20:34:51 -0800169 new SSLCertificateSocketFactory(handshakeTimeoutMillis, cache, true));
Dan Egnor60586f22010-02-08 21:56:38 -0800170 }
171
Dan Egnor7fc93c32010-06-29 17:51:28 -0700172 /**
173 * Verify the hostname of the certificate used by the other end of a
174 * connected socket. You MUST call this if you did not supply a hostname
175 * to {@link #createSocket()}. It is harmless to call this method
176 * redundantly if the hostname has already been verified.
177 *
178 * <p>Wildcard certificates are allowed to verify any matching hostname,
179 * so "foo.bar.example.com" is verified if the peer has a certificate
180 * for "*.example.com".
181 *
182 * @param socket An SSL socket which has been connected to a server
183 * @param hostname The expected hostname of the remote server
184 * @throws IOException if something goes wrong handshaking with the server
185 * @throws SSLPeerUnverifiedException if the server cannot prove its identity
186 *
187 * @hide
188 */
189 public static void verifyHostname(Socket socket, String hostname) throws IOException {
190 if (!(socket instanceof SSLSocket)) {
191 throw new IllegalArgumentException("Attempt to verify non-SSL socket");
192 }
193
194 if (!isSslCheckRelaxed()) {
195 // The code at the start of OpenSSLSocketImpl.startHandshake()
196 // ensures that the call is idempotent, so we can safely call it.
197 SSLSocket ssl = (SSLSocket) socket;
198 ssl.startHandshake();
199
200 SSLSession session = ssl.getSession();
201 if (session == null) {
202 throw new SSLException("Cannot verify SSL socket without session");
203 }
Kenny Root928ee1e2013-07-23 20:36:03 -0700204 if (!HttpsURLConnection.getDefaultHostnameVerifier().verify(hostname, session)) {
Dan Egnor7fc93c32010-06-29 17:51:28 -0700205 throw new SSLPeerUnverifiedException("Cannot verify hostname: " + hostname);
206 }
207 }
208 }
209
Ben Komalo1b528062011-05-03 09:40:15 -0700210 private SSLSocketFactory makeSocketFactory(
211 KeyManager[] keyManagers, TrustManager[] trustManagers) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212 try {
Kenny Root8a970632014-10-31 10:28:04 -0700213 OpenSSLContextImpl sslContext = OpenSSLContextImpl.getPreferred();
Ben Komalo1b528062011-05-03 09:40:15 -0700214 sslContext.engineInit(keyManagers, trustManagers, null);
Brian Carlstrom2c42c8f2010-09-14 00:11:14 -0700215 sslContext.engineGetClientSessionContext().setPersistentCache(mSessionCache);
Dan Egnor60586f22010-02-08 21:56:38 -0800216 return sslContext.engineGetSocketFactory();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217 } catch (KeyManagementException e) {
Dan Egnor60586f22010-02-08 21:56:38 -0800218 Log.wtf(TAG, e);
219 return (SSLSocketFactory) SSLSocketFactory.getDefault(); // Fallback
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800220 }
221 }
222
Dan Egnor7fc93c32010-06-29 17:51:28 -0700223 private static boolean isSslCheckRelaxed() {
224 return "1".equals(SystemProperties.get("ro.debuggable")) &&
225 "yes".equals(SystemProperties.get("socket.relaxsslcheck"));
226 }
227
Dan Egnor60586f22010-02-08 21:56:38 -0800228 private synchronized SSLSocketFactory getDelegate() {
Dan Egnor9d4b5752010-02-13 20:34:51 -0800229 // Relax the SSL check if instructed (for this factory, or systemwide)
Dan Egnor7fc93c32010-06-29 17:51:28 -0700230 if (!mSecure || isSslCheckRelaxed()) {
Dan Egnor60586f22010-02-08 21:56:38 -0800231 if (mInsecureFactory == null) {
Dan Egnor9d4b5752010-02-13 20:34:51 -0800232 if (mSecure) {
233 Log.w(TAG, "*** BYPASSING SSL SECURITY CHECKS (socket.relaxsslcheck=yes) ***");
Kenny Roote19ca072014-08-13 12:08:48 -0700234 } else {
235 Log.w(TAG, "Bypassing SSL security checks at caller's request");
Dan Egnor9d4b5752010-02-13 20:34:51 -0800236 }
Ben Komalo1b528062011-05-03 09:40:15 -0700237 mInsecureFactory = makeSocketFactory(mKeyManagers, INSECURE_TRUST_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238 }
Dan Egnor60586f22010-02-08 21:56:38 -0800239 return mInsecureFactory;
240 } else {
241 if (mSecureFactory == null) {
Ben Komalo1b528062011-05-03 09:40:15 -0700242 mSecureFactory = makeSocketFactory(mKeyManagers, mTrustManagers);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800243 }
Dan Egnor60586f22010-02-08 21:56:38 -0800244 return mSecureFactory;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245 }
246 }
247
Dan Egnor7fc93c32010-06-29 17:51:28 -0700248 /**
Ben Komalo1b528062011-05-03 09:40:15 -0700249 * Sets the {@link TrustManager}s to be used for connections made by this factory.
Ben Komalo1b528062011-05-03 09:40:15 -0700250 */
251 public void setTrustManagers(TrustManager[] trustManager) {
252 mTrustManagers = trustManager;
253
254 // Clear out all cached secure factories since configurations have changed.
255 mSecureFactory = null;
256 // Note - insecure factories only ever use the INSECURE_TRUST_MANAGER so they need not
257 // be cleared out here.
258 }
259
260 /**
Jesse Wilsonf5fb5e82012-03-23 15:55:03 -0400261 * Sets the <a href="http://technotes.googlecode.com/git/nextprotoneg.html">Next
262 * Protocol Negotiation (NPN)</a> protocols that this peer is interested in.
263 *
264 * <p>For servers this is the sequence of protocols to advertise as
265 * supported, in order of preference. This list is sent unencrypted to
266 * all clients that support NPN.
267 *
268 * <p>For clients this is a list of supported protocols to match against the
269 * server's list. If there is no protocol supported by both client and
270 * server then the first protocol in the client's list will be selected.
271 * The order of the client's protocols is otherwise insignificant.
272 *
Jesse Wilson2108ead2012-05-17 13:46:17 -0400273 * @param npnProtocols a non-empty list of protocol byte arrays. All arrays
274 * must be non-empty and of length less than 256.
Jesse Wilsonf5fb5e82012-03-23 15:55:03 -0400275 */
276 public void setNpnProtocols(byte[][] npnProtocols) {
Kenny Root100d7292013-06-25 12:00:34 -0700277 this.mNpnProtocols = toLengthPrefixedList(npnProtocols);
278 }
279
280 /**
281 * Sets the
282 * <a href="http://tools.ietf.org/html/draft-ietf-tls-applayerprotoneg-01">
283 * Application Layer Protocol Negotiation (ALPN)</a> protocols that this peer
284 * is interested in.
285 *
286 * <p>For servers this is the sequence of protocols to advertise as
287 * supported, in order of preference. This list is sent unencrypted to
288 * all clients that support ALPN.
289 *
290 * <p>For clients this is a list of supported protocols to match against the
291 * server's list. If there is no protocol supported by both client and
292 * server then the first protocol in the client's list will be selected.
293 * The order of the client's protocols is otherwise insignificant.
294 *
295 * @param protocols a non-empty list of protocol byte arrays. All arrays
296 * must be non-empty and of length less than 256.
297 * @hide
298 */
299 public void setAlpnProtocols(byte[][] protocols) {
300 this.mAlpnProtocols = toLengthPrefixedList(protocols);
Jesse Wilsonf5fb5e82012-03-23 15:55:03 -0400301 }
302
303 /**
304 * Returns an array containing the concatenation of length-prefixed byte
305 * strings.
306 */
Kenny Root100d7292013-06-25 12:00:34 -0700307 static byte[] toLengthPrefixedList(byte[]... items) {
308 if (items.length == 0) {
309 throw new IllegalArgumentException("items.length == 0");
Jesse Wilson2108ead2012-05-17 13:46:17 -0400310 }
Jesse Wilsonf5fb5e82012-03-23 15:55:03 -0400311 int totalLength = 0;
Kenny Root100d7292013-06-25 12:00:34 -0700312 for (byte[] s : items) {
Jesse Wilsonf5fb5e82012-03-23 15:55:03 -0400313 if (s.length == 0 || s.length > 255) {
314 throw new IllegalArgumentException("s.length == 0 || s.length > 255: " + s.length);
315 }
316 totalLength += 1 + s.length;
317 }
318 byte[] result = new byte[totalLength];
319 int pos = 0;
Kenny Root100d7292013-06-25 12:00:34 -0700320 for (byte[] s : items) {
Jesse Wilsonf5fb5e82012-03-23 15:55:03 -0400321 result[pos++] = (byte) s.length;
322 for (byte b : s) {
323 result[pos++] = b;
324 }
325 }
326 return result;
327 }
328
329 /**
330 * Returns the <a href="http://technotes.googlecode.com/git/nextprotoneg.html">Next
331 * Protocol Negotiation (NPN)</a> protocol selected by client and server, or
332 * null if no protocol was negotiated.
333 *
334 * @param socket a socket created by this factory.
Narayan Kamathb4db9622012-09-17 17:59:00 +0100335 * @throws IllegalArgumentException if the socket was not created by this factory.
Jesse Wilsonf5fb5e82012-03-23 15:55:03 -0400336 */
337 public byte[] getNpnSelectedProtocol(Socket socket) {
Narayan Kamathb4db9622012-09-17 17:59:00 +0100338 return castToOpenSSLSocket(socket).getNpnSelectedProtocol();
Jesse Wilsonf5fb5e82012-03-23 15:55:03 -0400339 }
340
341 /**
Kenny Root100d7292013-06-25 12:00:34 -0700342 * Returns the
343 * <a href="http://tools.ietf.org/html/draft-ietf-tls-applayerprotoneg-01">Application
344 * Layer Protocol Negotiation (ALPN)</a> protocol selected by client and server, or null
345 * if no protocol was negotiated.
346 *
347 * @param socket a socket created by this factory.
348 * @throws IllegalArgumentException if the socket was not created by this factory.
349 * @hide
350 */
351 public byte[] getAlpnSelectedProtocol(Socket socket) {
352 return castToOpenSSLSocket(socket).getAlpnSelectedProtocol();
353 }
354
355 /**
Ben Komalo1b528062011-05-03 09:40:15 -0700356 * Sets the {@link KeyManager}s to be used for connections made by this factory.
Ben Komalo1b528062011-05-03 09:40:15 -0700357 */
358 public void setKeyManagers(KeyManager[] keyManagers) {
359 mKeyManagers = keyManagers;
360
361 // Clear out any existing cached factories since configurations have changed.
362 mSecureFactory = null;
363 mInsecureFactory = null;
364 }
365
Narayan Kamathb4db9622012-09-17 17:59:00 +0100366 /**
Alex Klyubinac5eb032013-03-12 10:30:59 -0700367 * Sets the private key to be used for TLS Channel ID by connections made by this
Alex Klyubin4ef6c9b2013-01-18 12:50:39 -0800368 * factory.
369 *
370 * @param privateKey private key (enables TLS Channel ID) or {@code null} for no key (disables
371 * TLS Channel ID). The private key has to be an Elliptic Curve (EC) key based on the
372 * NIST P-256 curve (aka SECG secp256r1 or ANSI X9.62 prime256v1).
373 *
374 * @hide
375 */
Alex Klyubinac5eb032013-03-12 10:30:59 -0700376 public void setChannelIdPrivateKey(PrivateKey privateKey) {
Alex Klyubin4ef6c9b2013-01-18 12:50:39 -0800377 mChannelIdPrivateKey = privateKey;
378 }
379
380 /**
Narayan Kamathb4db9622012-09-17 17:59:00 +0100381 * Enables <a href="http://tools.ietf.org/html/rfc5077#section-3.2">session ticket</a>
382 * support on the given socket.
383 *
384 * @param socket a socket created by this factory
385 * @param useSessionTickets {@code true} to enable session ticket support on this socket.
386 * @throws IllegalArgumentException if the socket was not created by this factory.
387 */
388 public void setUseSessionTickets(Socket socket, boolean useSessionTickets) {
389 castToOpenSSLSocket(socket).setUseSessionTickets(useSessionTickets);
390 }
391
392 /**
393 * Turns on <a href="http://tools.ietf.org/html/rfc6066#section-3">Server
394 * Name Indication (SNI)</a> on a given socket.
395 *
396 * @param socket a socket created by this factory.
397 * @param hostName the desired SNI hostname, null to disable.
398 * @throws IllegalArgumentException if the socket was not created by this factory.
399 */
400 public void setHostname(Socket socket, String hostName) {
401 castToOpenSSLSocket(socket).setHostname(hostName);
402 }
403
Brian Carlstrom7ab7a8b2012-09-21 16:33:50 -0700404 /**
405 * Sets this socket's SO_SNDTIMEO write timeout in milliseconds.
406 * Use 0 for no timeout.
407 * To take effect, this option must be set before the blocking method was called.
408 *
409 * @param socket a socket created by this factory.
Brian Carlstrom992f2382012-09-26 14:33:47 -0700410 * @param timeout the desired write timeout in milliseconds.
Brian Carlstrom7ab7a8b2012-09-21 16:33:50 -0700411 * @throws IllegalArgumentException if the socket was not created by this factory.
Brian Carlstrom992f2382012-09-26 14:33:47 -0700412 *
413 * @hide
Brian Carlstrom7ab7a8b2012-09-21 16:33:50 -0700414 */
415 public void setSoWriteTimeout(Socket socket, int writeTimeoutMilliseconds)
416 throws SocketException {
417 castToOpenSSLSocket(socket).setSoWriteTimeout(writeTimeoutMilliseconds);
418 }
419
Narayan Kamathb4db9622012-09-17 17:59:00 +0100420 private static OpenSSLSocketImpl castToOpenSSLSocket(Socket socket) {
421 if (!(socket instanceof OpenSSLSocketImpl)) {
422 throw new IllegalArgumentException("Socket not created by this factory: "
423 + socket);
424 }
425
426 return (OpenSSLSocketImpl) socket;
427 }
Ben Komalo1b528062011-05-03 09:40:15 -0700428
429 /**
Dan Egnor7fc93c32010-06-29 17:51:28 -0700430 * {@inheritDoc}
431 *
Andrew Stadlerdf27c0c2010-07-12 15:31:40 -0700432 * <p>This method verifies the peer's certificate hostname after connecting
433 * (unless created with {@link #getInsecure(int, SSLSessionCache)}).
Dan Egnor7fc93c32010-06-29 17:51:28 -0700434 */
Dan Egnor60586f22010-02-08 21:56:38 -0800435 @Override
436 public Socket createSocket(Socket k, String host, int port, boolean close) throws IOException {
437 OpenSSLSocketImpl s = (OpenSSLSocketImpl) getDelegate().createSocket(k, host, port, close);
Jesse Wilsonf5fb5e82012-03-23 15:55:03 -0400438 s.setNpnProtocols(mNpnProtocols);
Kenny Root100d7292013-06-25 12:00:34 -0700439 s.setAlpnProtocols(mAlpnProtocols);
Dan Egnor60586f22010-02-08 21:56:38 -0800440 s.setHandshakeTimeout(mHandshakeTimeoutMillis);
Alex Klyubin4ef6c9b2013-01-18 12:50:39 -0800441 s.setChannelIdPrivateKey(mChannelIdPrivateKey);
Andrew Stadlerdf27c0c2010-07-12 15:31:40 -0700442 if (mSecure) {
443 verifyHostname(s, host);
444 }
Dan Egnor60586f22010-02-08 21:56:38 -0800445 return s;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800446 }
447
Dan Egnor7fc93c32010-06-29 17:51:28 -0700448 /**
449 * Creates a new socket which is not connected to any remote host.
450 * You must use {@link Socket#connect} to connect the socket.
451 *
452 * <p class="caution"><b>Warning:</b> Hostname verification is not performed
453 * with this method. You MUST verify the server's identity after connecting
454 * the socket to avoid man-in-the-middle attacks.</p>
455 */
Dan Egnor60586f22010-02-08 21:56:38 -0800456 @Override
457 public Socket createSocket() throws IOException {
458 OpenSSLSocketImpl s = (OpenSSLSocketImpl) getDelegate().createSocket();
Jesse Wilsonf5fb5e82012-03-23 15:55:03 -0400459 s.setNpnProtocols(mNpnProtocols);
Kenny Root100d7292013-06-25 12:00:34 -0700460 s.setAlpnProtocols(mAlpnProtocols);
Dan Egnor60586f22010-02-08 21:56:38 -0800461 s.setHandshakeTimeout(mHandshakeTimeoutMillis);
Alex Klyubin4ef6c9b2013-01-18 12:50:39 -0800462 s.setChannelIdPrivateKey(mChannelIdPrivateKey);
Dan Egnor60586f22010-02-08 21:56:38 -0800463 return s;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800464 }
465
Dan Egnor7fc93c32010-06-29 17:51:28 -0700466 /**
467 * {@inheritDoc}
468 *
469 * <p class="caution"><b>Warning:</b> Hostname verification is not performed
470 * with this method. You MUST verify the server's identity after connecting
471 * the socket to avoid man-in-the-middle attacks.</p>
472 */
Dan Egnor60586f22010-02-08 21:56:38 -0800473 @Override
474 public Socket createSocket(InetAddress addr, int port, InetAddress localAddr, int localPort)
475 throws IOException {
476 OpenSSLSocketImpl s = (OpenSSLSocketImpl) getDelegate().createSocket(
477 addr, port, localAddr, localPort);
Jesse Wilsonf5fb5e82012-03-23 15:55:03 -0400478 s.setNpnProtocols(mNpnProtocols);
Kenny Root100d7292013-06-25 12:00:34 -0700479 s.setAlpnProtocols(mAlpnProtocols);
Dan Egnor60586f22010-02-08 21:56:38 -0800480 s.setHandshakeTimeout(mHandshakeTimeoutMillis);
Alex Klyubin4ef6c9b2013-01-18 12:50:39 -0800481 s.setChannelIdPrivateKey(mChannelIdPrivateKey);
Dan Egnor60586f22010-02-08 21:56:38 -0800482 return s;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800483 }
484
Dan Egnor7fc93c32010-06-29 17:51:28 -0700485 /**
486 * {@inheritDoc}
487 *
488 * <p class="caution"><b>Warning:</b> Hostname verification is not performed
489 * with this method. You MUST verify the server's identity after connecting
490 * the socket to avoid man-in-the-middle attacks.</p>
491 */
Dan Egnor60586f22010-02-08 21:56:38 -0800492 @Override
493 public Socket createSocket(InetAddress addr, int port) throws IOException {
494 OpenSSLSocketImpl s = (OpenSSLSocketImpl) getDelegate().createSocket(addr, port);
Jesse Wilsonf5fb5e82012-03-23 15:55:03 -0400495 s.setNpnProtocols(mNpnProtocols);
Kenny Root100d7292013-06-25 12:00:34 -0700496 s.setAlpnProtocols(mAlpnProtocols);
Dan Egnor60586f22010-02-08 21:56:38 -0800497 s.setHandshakeTimeout(mHandshakeTimeoutMillis);
Alex Klyubin4ef6c9b2013-01-18 12:50:39 -0800498 s.setChannelIdPrivateKey(mChannelIdPrivateKey);
Dan Egnor60586f22010-02-08 21:56:38 -0800499 return s;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800500 }
501
Dan Egnor7fc93c32010-06-29 17:51:28 -0700502 /**
503 * {@inheritDoc}
504 *
Andrew Stadlerdf27c0c2010-07-12 15:31:40 -0700505 * <p>This method verifies the peer's certificate hostname after connecting
506 * (unless created with {@link #getInsecure(int, SSLSessionCache)}).
Dan Egnor7fc93c32010-06-29 17:51:28 -0700507 */
Dan Egnor60586f22010-02-08 21:56:38 -0800508 @Override
509 public Socket createSocket(String host, int port, InetAddress localAddr, int localPort)
510 throws IOException {
511 OpenSSLSocketImpl s = (OpenSSLSocketImpl) getDelegate().createSocket(
512 host, port, localAddr, localPort);
Jesse Wilsonf5fb5e82012-03-23 15:55:03 -0400513 s.setNpnProtocols(mNpnProtocols);
Kenny Root100d7292013-06-25 12:00:34 -0700514 s.setAlpnProtocols(mAlpnProtocols);
Dan Egnor60586f22010-02-08 21:56:38 -0800515 s.setHandshakeTimeout(mHandshakeTimeoutMillis);
Alex Klyubin4ef6c9b2013-01-18 12:50:39 -0800516 s.setChannelIdPrivateKey(mChannelIdPrivateKey);
Andrew Stadlerdf27c0c2010-07-12 15:31:40 -0700517 if (mSecure) {
518 verifyHostname(s, host);
519 }
Dan Egnor60586f22010-02-08 21:56:38 -0800520 return s;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800521 }
522
Dan Egnor7fc93c32010-06-29 17:51:28 -0700523 /**
524 * {@inheritDoc}
525 *
Andrew Stadlerdf27c0c2010-07-12 15:31:40 -0700526 * <p>This method verifies the peer's certificate hostname after connecting
527 * (unless created with {@link #getInsecure(int, SSLSessionCache)}).
Dan Egnor7fc93c32010-06-29 17:51:28 -0700528 */
Dan Egnor60586f22010-02-08 21:56:38 -0800529 @Override
530 public Socket createSocket(String host, int port) throws IOException {
531 OpenSSLSocketImpl s = (OpenSSLSocketImpl) getDelegate().createSocket(host, port);
Jesse Wilsonf5fb5e82012-03-23 15:55:03 -0400532 s.setNpnProtocols(mNpnProtocols);
Kenny Root100d7292013-06-25 12:00:34 -0700533 s.setAlpnProtocols(mAlpnProtocols);
Dan Egnor60586f22010-02-08 21:56:38 -0800534 s.setHandshakeTimeout(mHandshakeTimeoutMillis);
Alex Klyubin4ef6c9b2013-01-18 12:50:39 -0800535 s.setChannelIdPrivateKey(mChannelIdPrivateKey);
Andrew Stadlerdf27c0c2010-07-12 15:31:40 -0700536 if (mSecure) {
537 verifyHostname(s, host);
538 }
Dan Egnor60586f22010-02-08 21:56:38 -0800539 return s;
540 }
541
542 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800543 public String[] getDefaultCipherSuites() {
Alex Klyubin019118a2013-10-28 16:16:38 -0700544 return getDelegate().getDefaultCipherSuites();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800545 }
546
Dan Egnor60586f22010-02-08 21:56:38 -0800547 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800548 public String[] getSupportedCipherSuites() {
Dan Egnor60586f22010-02-08 21:56:38 -0800549 return getDelegate().getSupportedCipherSuites();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800550 }
551}