19066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/*
29066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Copyright (C) 2008 The Android Open Source Project
39066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
49066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
59066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * you may not use this file except in compliance with the License.
69066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * You may obtain a copy of the License at
79066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
89066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
99066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Unless required by applicable law or agreed to in writing, software
119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * See the License for the specific language governing permissions and
149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * limitations under the License.
159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */
169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectpackage android.net;
189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.os.SystemProperties;
209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.util.Log;
21aa67f684ff43c81e3280c846245ec6ebe907787eJohn Reck
22aa67f684ff43c81e3280c846245ec6ebe907787eJohn Reckimport com.android.internal.os.RoSystemProperties;
2312e752225aa96888358294be0d725d499a1c9f03Kenny Rootimport com.android.org.conscrypt.OpenSSLContextImpl;
2412e752225aa96888358294be0d725d499a1c9f03Kenny Rootimport com.android.org.conscrypt.OpenSSLSocketImpl;
2512e752225aa96888358294be0d725d499a1c9f03Kenny Rootimport com.android.org.conscrypt.SSLClientSessionCache;
269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport java.io.IOException;
279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport java.net.InetAddress;
289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport java.net.Socket;
297ab7a8b582b29d34ec0fdbd0c727e225f350bb30Brian Carlstromimport java.net.SocketException;
309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport java.security.KeyManagementException;
31ac5eb03a7c317e21573155b88641f4f1daef2eb9Alex Klyubinimport java.security.PrivateKey;
329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport java.security.cert.X509Certificate;
339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport javax.net.SocketFactory;
347fc93c36ae235115727296780dbc35101622bbd4Dan Egnorimport javax.net.ssl.HostnameVerifier;
357fc93c36ae235115727296780dbc35101622bbd4Dan Egnorimport javax.net.ssl.HttpsURLConnection;
361b52806e21270ccbe90d27f3dd93cbee1a81d09eBen Komaloimport javax.net.ssl.KeyManager;
377fc93c36ae235115727296780dbc35101622bbd4Dan Egnorimport javax.net.ssl.SSLException;
387fc93c36ae235115727296780dbc35101622bbd4Dan Egnorimport javax.net.ssl.SSLPeerUnverifiedException;
397fc93c36ae235115727296780dbc35101622bbd4Dan Egnorimport javax.net.ssl.SSLSession;
409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport javax.net.ssl.SSLSocket;
419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport javax.net.ssl.SSLSocketFactory;
429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport javax.net.ssl.TrustManager;
439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport javax.net.ssl.X509TrustManager;
449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/**
4660586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor * SSLSocketFactory implementation with several extra features:
477fc93c36ae235115727296780dbc35101622bbd4Dan Egnor *
4860586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor * <ul>
4960586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor * <li>Timeout specification for SSL handshake operations
507fc93c36ae235115727296780dbc35101622bbd4Dan Egnor * <li>Hostname verification in most cases (see WARNINGs below)
5160586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor * <li>Optional SSL session caching with {@link SSLSessionCache}
529d4b57545300c6de1722094404ae09bf0f6be937Dan Egnor * <li>Optionally bypass all SSL certificate checks
5360586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor * </ul>
547fc93c36ae235115727296780dbc35101622bbd4Dan Egnor *
557fc93c36ae235115727296780dbc35101622bbd4Dan Egnor * The handshake timeout does not apply to actual TCP socket connection.
567fc93c36ae235115727296780dbc35101622bbd4Dan Egnor * If you want a connection timeout as well, use {@link #createSocket()}
577fc93c36ae235115727296780dbc35101622bbd4Dan Egnor * and {@link Socket#connect(SocketAddress, int)}, after which you
587fc93c36ae235115727296780dbc35101622bbd4Dan Egnor * must verify the identity of the server you are connected to.
597fc93c36ae235115727296780dbc35101622bbd4Dan Egnor *
607fc93c36ae235115727296780dbc35101622bbd4Dan Egnor * <p class="caution"><b>Most {@link SSLSocketFactory} implementations do not
617fc93c36ae235115727296780dbc35101622bbd4Dan Egnor * verify the server's identity, allowing man-in-the-middle attacks.</b>
627fc93c36ae235115727296780dbc35101622bbd4Dan Egnor * This implementation does check the server's certificate hostname, but only
637fc93c36ae235115727296780dbc35101622bbd4Dan Egnor * for createSocket variants that specify a hostname.  When using methods that
647fc93c36ae235115727296780dbc35101622bbd4Dan Egnor * use {@link InetAddress} or which return an unconnected socket, you MUST
657fc93c36ae235115727296780dbc35101622bbd4Dan Egnor * verify the server's identity yourself to ensure a secure connection.</p>
667fc93c36ae235115727296780dbc35101622bbd4Dan Egnor *
677fc93c36ae235115727296780dbc35101622bbd4Dan Egnor * <p>One way to verify the server's identity is to use
687fc93c36ae235115727296780dbc35101622bbd4Dan Egnor * {@link HttpsURLConnection#getDefaultHostnameVerifier()} to get a
697fc93c36ae235115727296780dbc35101622bbd4Dan Egnor * {@link HostnameVerifier} to verify the certificate hostname.
707fc93c36ae235115727296780dbc35101622bbd4Dan Egnor *
717fc93c36ae235115727296780dbc35101622bbd4Dan Egnor * <p>On development devices, "setprop socket.relaxsslcheck yes" bypasses all
727fc93c36ae235115727296780dbc35101622bbd4Dan Egnor * SSL certificate and hostname checks for testing purposes.  This setting
737fc93c36ae235115727296780dbc35101622bbd4Dan Egnor * requires root access.
749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */
759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectpublic class SSLCertificateSocketFactory extends SSLSocketFactory {
7660586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor    private static final String TAG = "SSLCertificateSocketFactory";
779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7860586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor    private static final TrustManager[] INSECURE_TRUST_MANAGER = new TrustManager[] {
799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        new X509TrustManager() {
8060586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor            public X509Certificate[] getAcceptedIssuers() { return null; }
8160586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor            public void checkClientTrusted(X509Certificate[] certs, String authType) { }
8260586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor            public void checkServerTrusted(X509Certificate[] certs, String authType) { }
839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    };
859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
8660586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor    private SSLSocketFactory mInsecureFactory = null;
8760586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor    private SSLSocketFactory mSecureFactory = null;
881b52806e21270ccbe90d27f3dd93cbee1a81d09eBen Komalo    private TrustManager[] mTrustManagers = null;
891b52806e21270ccbe90d27f3dd93cbee1a81d09eBen Komalo    private KeyManager[] mKeyManagers = null;
90f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson    private byte[] mNpnProtocols = null;
91100d7290264338c6536739abd59879aaaa812537Kenny Root    private byte[] mAlpnProtocols = null;
92ac5eb03a7c317e21573155b88641f4f1daef2eb9Alex Klyubin    private PrivateKey mChannelIdPrivateKey = null;
939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
9460586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor    private final int mHandshakeTimeoutMillis;
9560586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor    private final SSLClientSessionCache mSessionCache;
969d4b57545300c6de1722094404ae09bf0f6be937Dan Egnor    private final boolean mSecure;
979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
9860586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor    /** @deprecated Use {@link #getDefault(int)} instead. */
9928c742573ccaeb55c16bc02fb25fdd86b8d1f76aJesse Wilson    @Deprecated
10060586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor    public SSLCertificateSocketFactory(int handshakeTimeoutMillis) {
1019d4b57545300c6de1722094404ae09bf0f6be937Dan Egnor        this(handshakeTimeoutMillis, null, true);
1029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1049d4b57545300c6de1722094404ae09bf0f6be937Dan Egnor    private SSLCertificateSocketFactory(
105992f238d13fff7c21b60ef6958784a4ed2156784Brian Carlstrom            int handshakeTimeoutMillis, SSLSessionCache cache, boolean secure) {
10660586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor        mHandshakeTimeoutMillis = handshakeTimeoutMillis;
10760586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor        mSessionCache = cache == null ? null : cache.mSessionCache;
1089d4b57545300c6de1722094404ae09bf0f6be937Dan Egnor        mSecure = secure;
1099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
1129d4b57545300c6de1722094404ae09bf0f6be937Dan Egnor     * Returns a new socket factory instance with an optional handshake timeout.
1139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
11460586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor     * @param handshakeTimeoutMillis to use for SSL connection handshake, or 0
11560586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor     *         for none.  The socket timeout is reset to 0 after the handshake.
1167fc93c36ae235115727296780dbc35101622bbd4Dan Egnor     * @return a new SSLSocketFactory with the specified parameters
1179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
11860586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor    public static SocketFactory getDefault(int handshakeTimeoutMillis) {
1199d4b57545300c6de1722094404ae09bf0f6be937Dan Egnor        return new SSLCertificateSocketFactory(handshakeTimeoutMillis, null, true);
1209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
1239d4b57545300c6de1722094404ae09bf0f6be937Dan Egnor     * Returns a new socket factory instance with an optional handshake timeout
1249d4b57545300c6de1722094404ae09bf0f6be937Dan Egnor     * and SSL session cache.
1259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
12660586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor     * @param handshakeTimeoutMillis to use for SSL connection handshake, or 0
12760586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor     *         for none.  The socket timeout is reset to 0 after the handshake.
128ff5569948fda346d95d4615de6578f82d9614be3Jesse Wilson     * @param cache The {@link SSLSessionCache} to use, or null for no cache.
1297fc93c36ae235115727296780dbc35101622bbd4Dan Egnor     * @return a new SSLSocketFactory with the specified parameters
13060586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor     */
1319d4b57545300c6de1722094404ae09bf0f6be937Dan Egnor    public static SSLSocketFactory getDefault(int handshakeTimeoutMillis, SSLSessionCache cache) {
1329d4b57545300c6de1722094404ae09bf0f6be937Dan Egnor        return new SSLCertificateSocketFactory(handshakeTimeoutMillis, cache, true);
1339d4b57545300c6de1722094404ae09bf0f6be937Dan Egnor    }
1349d4b57545300c6de1722094404ae09bf0f6be937Dan Egnor
1359d4b57545300c6de1722094404ae09bf0f6be937Dan Egnor    /**
1369d4b57545300c6de1722094404ae09bf0f6be937Dan Egnor     * Returns a new instance of a socket factory with all SSL security checks
1379d4b57545300c6de1722094404ae09bf0f6be937Dan Egnor     * disabled, using an optional handshake timeout and SSL session cache.
1387fc93c36ae235115727296780dbc35101622bbd4Dan Egnor     *
1397fc93c36ae235115727296780dbc35101622bbd4Dan Egnor     * <p class="caution"><b>Warning:</b> Sockets created using this factory
140e19ca078bf1778a344366672de020e63a80252a9Kenny Root     * are vulnerable to man-in-the-middle attacks!</p>
1419d4b57545300c6de1722094404ae09bf0f6be937Dan Egnor     *
1429d4b57545300c6de1722094404ae09bf0f6be937Dan Egnor     * @param handshakeTimeoutMillis to use for SSL connection handshake, or 0
1439d4b57545300c6de1722094404ae09bf0f6be937Dan Egnor     *         for none.  The socket timeout is reset to 0 after the handshake.
144ff5569948fda346d95d4615de6578f82d9614be3Jesse Wilson     * @param cache The {@link SSLSessionCache} to use, or null for no cache.
1457fc93c36ae235115727296780dbc35101622bbd4Dan Egnor     * @return an insecure SSLSocketFactory with the specified parameters
1469d4b57545300c6de1722094404ae09bf0f6be937Dan Egnor     */
1479d4b57545300c6de1722094404ae09bf0f6be937Dan Egnor    public static SSLSocketFactory getInsecure(int handshakeTimeoutMillis, SSLSessionCache cache) {
1489d4b57545300c6de1722094404ae09bf0f6be937Dan Egnor        return new SSLCertificateSocketFactory(handshakeTimeoutMillis, cache, false);
1499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
15160586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor    /**
15260586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor     * Returns a socket factory (also named SSLSocketFactory, but in a different
15360586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor     * namespace) for use with the Apache HTTP stack.
15460586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor     *
15560586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor     * @param handshakeTimeoutMillis to use for SSL connection handshake, or 0
15660586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor     *         for none.  The socket timeout is reset to 0 after the handshake.
157ff5569948fda346d95d4615de6578f82d9614be3Jesse Wilson     * @param cache The {@link SSLSessionCache} to use, or null for no cache.
15860586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor     * @return a new SocketFactory with the specified parameters
159823675fdbb7f974b8e2fa9fbb71774b32487582dNarayan Kamath     *
160823675fdbb7f974b8e2fa9fbb71774b32487582dNarayan Kamath     * @deprecated Use {@link #getDefault()} along with a {@link javax.net.ssl.HttpsURLConnection}
161823675fdbb7f974b8e2fa9fbb71774b32487582dNarayan Kamath     *     instead. The Apache HTTP client is no longer maintained and may be removed in a future
162823675fdbb7f974b8e2fa9fbb71774b32487582dNarayan Kamath     *     release. Please visit <a href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html">this webpage</a>
163823675fdbb7f974b8e2fa9fbb71774b32487582dNarayan Kamath     *     for further details.
164406e1ed9883010928cfb42246cfd2710ebf3da74Narayan Kamath     *
165406e1ed9883010928cfb42246cfd2710ebf3da74Narayan Kamath     * @removed
16660586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor     */
167823675fdbb7f974b8e2fa9fbb71774b32487582dNarayan Kamath    @Deprecated
16860586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor    public static org.apache.http.conn.ssl.SSLSocketFactory getHttpSocketFactory(
169ff5569948fda346d95d4615de6578f82d9614be3Jesse Wilson            int handshakeTimeoutMillis, SSLSessionCache cache) {
17060586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor        return new org.apache.http.conn.ssl.SSLSocketFactory(
1719d4b57545300c6de1722094404ae09bf0f6be937Dan Egnor                new SSLCertificateSocketFactory(handshakeTimeoutMillis, cache, true));
1729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1747fc93c36ae235115727296780dbc35101622bbd4Dan Egnor    /**
1757fc93c36ae235115727296780dbc35101622bbd4Dan Egnor     * Verify the hostname of the certificate used by the other end of a
1767fc93c36ae235115727296780dbc35101622bbd4Dan Egnor     * connected socket.  You MUST call this if you did not supply a hostname
1777fc93c36ae235115727296780dbc35101622bbd4Dan Egnor     * to {@link #createSocket()}.  It is harmless to call this method
1787fc93c36ae235115727296780dbc35101622bbd4Dan Egnor     * redundantly if the hostname has already been verified.
1797fc93c36ae235115727296780dbc35101622bbd4Dan Egnor     *
1807fc93c36ae235115727296780dbc35101622bbd4Dan Egnor     * <p>Wildcard certificates are allowed to verify any matching hostname,
1817fc93c36ae235115727296780dbc35101622bbd4Dan Egnor     * so "foo.bar.example.com" is verified if the peer has a certificate
1827fc93c36ae235115727296780dbc35101622bbd4Dan Egnor     * for "*.example.com".
1837fc93c36ae235115727296780dbc35101622bbd4Dan Egnor     *
1847fc93c36ae235115727296780dbc35101622bbd4Dan Egnor     * @param socket An SSL socket which has been connected to a server
1857fc93c36ae235115727296780dbc35101622bbd4Dan Egnor     * @param hostname The expected hostname of the remote server
1867fc93c36ae235115727296780dbc35101622bbd4Dan Egnor     * @throws IOException if something goes wrong handshaking with the server
1877fc93c36ae235115727296780dbc35101622bbd4Dan Egnor     * @throws SSLPeerUnverifiedException if the server cannot prove its identity
1887fc93c36ae235115727296780dbc35101622bbd4Dan Egnor     *
1897fc93c36ae235115727296780dbc35101622bbd4Dan Egnor     * @hide
1907fc93c36ae235115727296780dbc35101622bbd4Dan Egnor     */
1917fc93c36ae235115727296780dbc35101622bbd4Dan Egnor    public static void verifyHostname(Socket socket, String hostname) throws IOException {
1927fc93c36ae235115727296780dbc35101622bbd4Dan Egnor        if (!(socket instanceof SSLSocket)) {
1937fc93c36ae235115727296780dbc35101622bbd4Dan Egnor            throw new IllegalArgumentException("Attempt to verify non-SSL socket");
1947fc93c36ae235115727296780dbc35101622bbd4Dan Egnor        }
1957fc93c36ae235115727296780dbc35101622bbd4Dan Egnor
1967fc93c36ae235115727296780dbc35101622bbd4Dan Egnor        if (!isSslCheckRelaxed()) {
1977fc93c36ae235115727296780dbc35101622bbd4Dan Egnor            // The code at the start of OpenSSLSocketImpl.startHandshake()
1987fc93c36ae235115727296780dbc35101622bbd4Dan Egnor            // ensures that the call is idempotent, so we can safely call it.
1997fc93c36ae235115727296780dbc35101622bbd4Dan Egnor            SSLSocket ssl = (SSLSocket) socket;
2007fc93c36ae235115727296780dbc35101622bbd4Dan Egnor            ssl.startHandshake();
2017fc93c36ae235115727296780dbc35101622bbd4Dan Egnor
2027fc93c36ae235115727296780dbc35101622bbd4Dan Egnor            SSLSession session = ssl.getSession();
2037fc93c36ae235115727296780dbc35101622bbd4Dan Egnor            if (session == null) {
2047fc93c36ae235115727296780dbc35101622bbd4Dan Egnor                throw new SSLException("Cannot verify SSL socket without session");
2057fc93c36ae235115727296780dbc35101622bbd4Dan Egnor            }
206928ee1e48fa89302d02fdf8a8a2c7315d7195e7cKenny Root            if (!HttpsURLConnection.getDefaultHostnameVerifier().verify(hostname, session)) {
2077fc93c36ae235115727296780dbc35101622bbd4Dan Egnor                throw new SSLPeerUnverifiedException("Cannot verify hostname: " + hostname);
2087fc93c36ae235115727296780dbc35101622bbd4Dan Egnor            }
2097fc93c36ae235115727296780dbc35101622bbd4Dan Egnor        }
2107fc93c36ae235115727296780dbc35101622bbd4Dan Egnor    }
2117fc93c36ae235115727296780dbc35101622bbd4Dan Egnor
2121b52806e21270ccbe90d27f3dd93cbee1a81d09eBen Komalo    private SSLSocketFactory makeSocketFactory(
2131b52806e21270ccbe90d27f3dd93cbee1a81d09eBen Komalo            KeyManager[] keyManagers, TrustManager[] trustManagers) {
21460586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor        try {
2158a970637208207955fb6a719bd82902384b3c743Kenny Root            OpenSSLContextImpl sslContext = OpenSSLContextImpl.getPreferred();
2161b52806e21270ccbe90d27f3dd93cbee1a81d09eBen Komalo            sslContext.engineInit(keyManagers, trustManagers, null);
2172c42c8fbaf02be1f3ea6298077128d0c419526f0Brian Carlstrom            sslContext.engineGetClientSessionContext().setPersistentCache(mSessionCache);
21860586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor            return sslContext.engineGetSocketFactory();
21960586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor        } catch (KeyManagementException e) {
22060586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor            Log.wtf(TAG, e);
22160586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor            return (SSLSocketFactory) SSLSocketFactory.getDefault();  // Fallback
2229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
22360586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor    }
2249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2257fc93c36ae235115727296780dbc35101622bbd4Dan Egnor    private static boolean isSslCheckRelaxed() {
226aa67f684ff43c81e3280c846245ec6ebe907787eJohn Reck        return RoSystemProperties.DEBUGGABLE &&
227aa67f684ff43c81e3280c846245ec6ebe907787eJohn Reck            SystemProperties.getBoolean("socket.relaxsslcheck", false);
2287fc93c36ae235115727296780dbc35101622bbd4Dan Egnor    }
2297fc93c36ae235115727296780dbc35101622bbd4Dan Egnor
23060586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor    private synchronized SSLSocketFactory getDelegate() {
2319d4b57545300c6de1722094404ae09bf0f6be937Dan Egnor        // Relax the SSL check if instructed (for this factory, or systemwide)
2327fc93c36ae235115727296780dbc35101622bbd4Dan Egnor        if (!mSecure || isSslCheckRelaxed()) {
23360586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor            if (mInsecureFactory == null) {
2349d4b57545300c6de1722094404ae09bf0f6be937Dan Egnor                if (mSecure) {
2359d4b57545300c6de1722094404ae09bf0f6be937Dan Egnor                    Log.w(TAG, "*** BYPASSING SSL SECURITY CHECKS (socket.relaxsslcheck=yes) ***");
236e19ca078bf1778a344366672de020e63a80252a9Kenny Root                } else {
237e19ca078bf1778a344366672de020e63a80252a9Kenny Root                    Log.w(TAG, "Bypassing SSL security checks at caller's request");
2389d4b57545300c6de1722094404ae09bf0f6be937Dan Egnor                }
2391b52806e21270ccbe90d27f3dd93cbee1a81d09eBen Komalo                mInsecureFactory = makeSocketFactory(mKeyManagers, INSECURE_TRUST_MANAGER);
2409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
24160586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor            return mInsecureFactory;
24260586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor        } else {
24360586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor            if (mSecureFactory == null) {
2441b52806e21270ccbe90d27f3dd93cbee1a81d09eBen Komalo                mSecureFactory = makeSocketFactory(mKeyManagers, mTrustManagers);
2459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
24660586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor            return mSecureFactory;
2479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
2489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2507fc93c36ae235115727296780dbc35101622bbd4Dan Egnor    /**
2511b52806e21270ccbe90d27f3dd93cbee1a81d09eBen Komalo     * Sets the {@link TrustManager}s to be used for connections made by this factory.
2521b52806e21270ccbe90d27f3dd93cbee1a81d09eBen Komalo     */
2531b52806e21270ccbe90d27f3dd93cbee1a81d09eBen Komalo    public void setTrustManagers(TrustManager[] trustManager) {
2541b52806e21270ccbe90d27f3dd93cbee1a81d09eBen Komalo        mTrustManagers = trustManager;
2551b52806e21270ccbe90d27f3dd93cbee1a81d09eBen Komalo
2561b52806e21270ccbe90d27f3dd93cbee1a81d09eBen Komalo        // Clear out all cached secure factories since configurations have changed.
2571b52806e21270ccbe90d27f3dd93cbee1a81d09eBen Komalo        mSecureFactory = null;
2581b52806e21270ccbe90d27f3dd93cbee1a81d09eBen Komalo        // Note - insecure factories only ever use the INSECURE_TRUST_MANAGER so they need not
2591b52806e21270ccbe90d27f3dd93cbee1a81d09eBen Komalo        // be cleared out here.
2601b52806e21270ccbe90d27f3dd93cbee1a81d09eBen Komalo    }
2611b52806e21270ccbe90d27f3dd93cbee1a81d09eBen Komalo
2621b52806e21270ccbe90d27f3dd93cbee1a81d09eBen Komalo    /**
263f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson     * Sets the <a href="http://technotes.googlecode.com/git/nextprotoneg.html">Next
264f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson     * Protocol Negotiation (NPN)</a> protocols that this peer is interested in.
265f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson     *
266f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson     * <p>For servers this is the sequence of protocols to advertise as
267f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson     * supported, in order of preference. This list is sent unencrypted to
268f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson     * all clients that support NPN.
269f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson     *
270f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson     * <p>For clients this is a list of supported protocols to match against the
271f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson     * server's list. If there is no protocol supported by both client and
272f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson     * server then the first protocol in the client's list will be selected.
273f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson     * The order of the client's protocols is otherwise insignificant.
274f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson     *
2752108ead7f125536874d6de6ca1c0c4cffbf61b44Jesse Wilson     * @param npnProtocols a non-empty list of protocol byte arrays. All arrays
2762108ead7f125536874d6de6ca1c0c4cffbf61b44Jesse Wilson     *     must be non-empty and of length less than 256.
277f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson     */
278f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson    public void setNpnProtocols(byte[][] npnProtocols) {
279100d7290264338c6536739abd59879aaaa812537Kenny Root        this.mNpnProtocols = toLengthPrefixedList(npnProtocols);
280100d7290264338c6536739abd59879aaaa812537Kenny Root    }
281100d7290264338c6536739abd59879aaaa812537Kenny Root
282100d7290264338c6536739abd59879aaaa812537Kenny Root    /**
283100d7290264338c6536739abd59879aaaa812537Kenny Root     * Sets the
284100d7290264338c6536739abd59879aaaa812537Kenny Root     * <a href="http://tools.ietf.org/html/draft-ietf-tls-applayerprotoneg-01">
285100d7290264338c6536739abd59879aaaa812537Kenny Root     * Application Layer Protocol Negotiation (ALPN)</a> protocols that this peer
286100d7290264338c6536739abd59879aaaa812537Kenny Root     * is interested in.
287100d7290264338c6536739abd59879aaaa812537Kenny Root     *
288100d7290264338c6536739abd59879aaaa812537Kenny Root     * <p>For servers this is the sequence of protocols to advertise as
289100d7290264338c6536739abd59879aaaa812537Kenny Root     * supported, in order of preference. This list is sent unencrypted to
290100d7290264338c6536739abd59879aaaa812537Kenny Root     * all clients that support ALPN.
291100d7290264338c6536739abd59879aaaa812537Kenny Root     *
292100d7290264338c6536739abd59879aaaa812537Kenny Root     * <p>For clients this is a list of supported protocols to match against the
293100d7290264338c6536739abd59879aaaa812537Kenny Root     * server's list. If there is no protocol supported by both client and
294100d7290264338c6536739abd59879aaaa812537Kenny Root     * server then the first protocol in the client's list will be selected.
295100d7290264338c6536739abd59879aaaa812537Kenny Root     * The order of the client's protocols is otherwise insignificant.
296100d7290264338c6536739abd59879aaaa812537Kenny Root     *
297100d7290264338c6536739abd59879aaaa812537Kenny Root     * @param protocols a non-empty list of protocol byte arrays. All arrays
298100d7290264338c6536739abd59879aaaa812537Kenny Root     *     must be non-empty and of length less than 256.
299100d7290264338c6536739abd59879aaaa812537Kenny Root     * @hide
300100d7290264338c6536739abd59879aaaa812537Kenny Root     */
301100d7290264338c6536739abd59879aaaa812537Kenny Root    public void setAlpnProtocols(byte[][] protocols) {
302100d7290264338c6536739abd59879aaaa812537Kenny Root        this.mAlpnProtocols = toLengthPrefixedList(protocols);
303f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson    }
304f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson
305f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson    /**
306f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson     * Returns an array containing the concatenation of length-prefixed byte
307f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson     * strings.
308f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson     */
309100d7290264338c6536739abd59879aaaa812537Kenny Root    static byte[] toLengthPrefixedList(byte[]... items) {
310100d7290264338c6536739abd59879aaaa812537Kenny Root        if (items.length == 0) {
311100d7290264338c6536739abd59879aaaa812537Kenny Root            throw new IllegalArgumentException("items.length == 0");
3122108ead7f125536874d6de6ca1c0c4cffbf61b44Jesse Wilson        }
313f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson        int totalLength = 0;
314100d7290264338c6536739abd59879aaaa812537Kenny Root        for (byte[] s : items) {
315f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson            if (s.length == 0 || s.length > 255) {
316f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson                throw new IllegalArgumentException("s.length == 0 || s.length > 255: " + s.length);
317f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson            }
318f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson            totalLength += 1 + s.length;
319f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson        }
320f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson        byte[] result = new byte[totalLength];
321f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson        int pos = 0;
322100d7290264338c6536739abd59879aaaa812537Kenny Root        for (byte[] s : items) {
323f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson            result[pos++] = (byte) s.length;
324f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson            for (byte b : s) {
325f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson                result[pos++] = b;
326f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson            }
327f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson        }
328f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson        return result;
329f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson    }
330f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson
331f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson    /**
332f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson     * Returns the <a href="http://technotes.googlecode.com/git/nextprotoneg.html">Next
333f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson     * Protocol Negotiation (NPN)</a> protocol selected by client and server, or
334f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson     * null if no protocol was negotiated.
335f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson     *
336f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson     * @param socket a socket created by this factory.
337b4db962da0fecd9a6f2714148bbdea023610842fNarayan Kamath     * @throws IllegalArgumentException if the socket was not created by this factory.
338f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson     */
339f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson    public byte[] getNpnSelectedProtocol(Socket socket) {
340b4db962da0fecd9a6f2714148bbdea023610842fNarayan Kamath        return castToOpenSSLSocket(socket).getNpnSelectedProtocol();
341f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson    }
342f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson
343f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson    /**
344100d7290264338c6536739abd59879aaaa812537Kenny Root     * Returns the
345100d7290264338c6536739abd59879aaaa812537Kenny Root     * <a href="http://tools.ietf.org/html/draft-ietf-tls-applayerprotoneg-01">Application
346100d7290264338c6536739abd59879aaaa812537Kenny Root     * Layer Protocol Negotiation (ALPN)</a> protocol selected by client and server, or null
347100d7290264338c6536739abd59879aaaa812537Kenny Root     * if no protocol was negotiated.
348100d7290264338c6536739abd59879aaaa812537Kenny Root     *
349100d7290264338c6536739abd59879aaaa812537Kenny Root     * @param socket a socket created by this factory.
350100d7290264338c6536739abd59879aaaa812537Kenny Root     * @throws IllegalArgumentException if the socket was not created by this factory.
351100d7290264338c6536739abd59879aaaa812537Kenny Root     * @hide
352100d7290264338c6536739abd59879aaaa812537Kenny Root     */
353100d7290264338c6536739abd59879aaaa812537Kenny Root    public byte[] getAlpnSelectedProtocol(Socket socket) {
354100d7290264338c6536739abd59879aaaa812537Kenny Root        return castToOpenSSLSocket(socket).getAlpnSelectedProtocol();
355100d7290264338c6536739abd59879aaaa812537Kenny Root    }
356100d7290264338c6536739abd59879aaaa812537Kenny Root
357100d7290264338c6536739abd59879aaaa812537Kenny Root    /**
3581b52806e21270ccbe90d27f3dd93cbee1a81d09eBen Komalo     * Sets the {@link KeyManager}s to be used for connections made by this factory.
3591b52806e21270ccbe90d27f3dd93cbee1a81d09eBen Komalo     */
3601b52806e21270ccbe90d27f3dd93cbee1a81d09eBen Komalo    public void setKeyManagers(KeyManager[] keyManagers) {
3611b52806e21270ccbe90d27f3dd93cbee1a81d09eBen Komalo        mKeyManagers = keyManagers;
3621b52806e21270ccbe90d27f3dd93cbee1a81d09eBen Komalo
3631b52806e21270ccbe90d27f3dd93cbee1a81d09eBen Komalo        // Clear out any existing cached factories since configurations have changed.
3641b52806e21270ccbe90d27f3dd93cbee1a81d09eBen Komalo        mSecureFactory = null;
3651b52806e21270ccbe90d27f3dd93cbee1a81d09eBen Komalo        mInsecureFactory = null;
3661b52806e21270ccbe90d27f3dd93cbee1a81d09eBen Komalo    }
3671b52806e21270ccbe90d27f3dd93cbee1a81d09eBen Komalo
368b4db962da0fecd9a6f2714148bbdea023610842fNarayan Kamath    /**
369ac5eb03a7c317e21573155b88641f4f1daef2eb9Alex Klyubin     * Sets the private key to be used for TLS Channel ID by connections made by this
3704ef6c9b6a16c9b65699705aaa64977fc60dd3331Alex Klyubin     * factory.
3714ef6c9b6a16c9b65699705aaa64977fc60dd3331Alex Klyubin     *
3724ef6c9b6a16c9b65699705aaa64977fc60dd3331Alex Klyubin     * @param privateKey private key (enables TLS Channel ID) or {@code null} for no key (disables
3734ef6c9b6a16c9b65699705aaa64977fc60dd3331Alex Klyubin     *        TLS Channel ID). The private key has to be an Elliptic Curve (EC) key based on the
3744ef6c9b6a16c9b65699705aaa64977fc60dd3331Alex Klyubin     *        NIST P-256 curve (aka SECG secp256r1 or ANSI X9.62 prime256v1).
3754ef6c9b6a16c9b65699705aaa64977fc60dd3331Alex Klyubin     *
3764ef6c9b6a16c9b65699705aaa64977fc60dd3331Alex Klyubin     * @hide
3774ef6c9b6a16c9b65699705aaa64977fc60dd3331Alex Klyubin     */
378ac5eb03a7c317e21573155b88641f4f1daef2eb9Alex Klyubin    public void setChannelIdPrivateKey(PrivateKey privateKey) {
3794ef6c9b6a16c9b65699705aaa64977fc60dd3331Alex Klyubin        mChannelIdPrivateKey = privateKey;
3804ef6c9b6a16c9b65699705aaa64977fc60dd3331Alex Klyubin    }
3814ef6c9b6a16c9b65699705aaa64977fc60dd3331Alex Klyubin
3824ef6c9b6a16c9b65699705aaa64977fc60dd3331Alex Klyubin    /**
383b4db962da0fecd9a6f2714148bbdea023610842fNarayan Kamath     * Enables <a href="http://tools.ietf.org/html/rfc5077#section-3.2">session ticket</a>
384b4db962da0fecd9a6f2714148bbdea023610842fNarayan Kamath     * support on the given socket.
385b4db962da0fecd9a6f2714148bbdea023610842fNarayan Kamath     *
386b4db962da0fecd9a6f2714148bbdea023610842fNarayan Kamath     * @param socket a socket created by this factory
387b4db962da0fecd9a6f2714148bbdea023610842fNarayan Kamath     * @param useSessionTickets {@code true} to enable session ticket support on this socket.
388b4db962da0fecd9a6f2714148bbdea023610842fNarayan Kamath     * @throws IllegalArgumentException if the socket was not created by this factory.
389b4db962da0fecd9a6f2714148bbdea023610842fNarayan Kamath     */
390b4db962da0fecd9a6f2714148bbdea023610842fNarayan Kamath    public void setUseSessionTickets(Socket socket, boolean useSessionTickets) {
391b4db962da0fecd9a6f2714148bbdea023610842fNarayan Kamath        castToOpenSSLSocket(socket).setUseSessionTickets(useSessionTickets);
392b4db962da0fecd9a6f2714148bbdea023610842fNarayan Kamath    }
393b4db962da0fecd9a6f2714148bbdea023610842fNarayan Kamath
394b4db962da0fecd9a6f2714148bbdea023610842fNarayan Kamath    /**
395b4db962da0fecd9a6f2714148bbdea023610842fNarayan Kamath     * Turns on <a href="http://tools.ietf.org/html/rfc6066#section-3">Server
396b4db962da0fecd9a6f2714148bbdea023610842fNarayan Kamath     * Name Indication (SNI)</a> on a given socket.
397b4db962da0fecd9a6f2714148bbdea023610842fNarayan Kamath     *
398b4db962da0fecd9a6f2714148bbdea023610842fNarayan Kamath     * @param socket a socket created by this factory.
399b4db962da0fecd9a6f2714148bbdea023610842fNarayan Kamath     * @param hostName the desired SNI hostname, null to disable.
400b4db962da0fecd9a6f2714148bbdea023610842fNarayan Kamath     * @throws IllegalArgumentException if the socket was not created by this factory.
401b4db962da0fecd9a6f2714148bbdea023610842fNarayan Kamath     */
402b4db962da0fecd9a6f2714148bbdea023610842fNarayan Kamath    public void setHostname(Socket socket, String hostName) {
403b4db962da0fecd9a6f2714148bbdea023610842fNarayan Kamath        castToOpenSSLSocket(socket).setHostname(hostName);
404b4db962da0fecd9a6f2714148bbdea023610842fNarayan Kamath    }
405b4db962da0fecd9a6f2714148bbdea023610842fNarayan Kamath
4067ab7a8b582b29d34ec0fdbd0c727e225f350bb30Brian Carlstrom    /**
4077ab7a8b582b29d34ec0fdbd0c727e225f350bb30Brian Carlstrom     * Sets this socket's SO_SNDTIMEO write timeout in milliseconds.
4087ab7a8b582b29d34ec0fdbd0c727e225f350bb30Brian Carlstrom     * Use 0 for no timeout.
4097ab7a8b582b29d34ec0fdbd0c727e225f350bb30Brian Carlstrom     * To take effect, this option must be set before the blocking method was called.
4107ab7a8b582b29d34ec0fdbd0c727e225f350bb30Brian Carlstrom     *
4117ab7a8b582b29d34ec0fdbd0c727e225f350bb30Brian Carlstrom     * @param socket a socket created by this factory.
412992f238d13fff7c21b60ef6958784a4ed2156784Brian Carlstrom     * @param timeout the desired write timeout in milliseconds.
4137ab7a8b582b29d34ec0fdbd0c727e225f350bb30Brian Carlstrom     * @throws IllegalArgumentException if the socket was not created by this factory.
414992f238d13fff7c21b60ef6958784a4ed2156784Brian Carlstrom     *
415992f238d13fff7c21b60ef6958784a4ed2156784Brian Carlstrom     * @hide
4167ab7a8b582b29d34ec0fdbd0c727e225f350bb30Brian Carlstrom     */
4177ab7a8b582b29d34ec0fdbd0c727e225f350bb30Brian Carlstrom    public void setSoWriteTimeout(Socket socket, int writeTimeoutMilliseconds)
4187ab7a8b582b29d34ec0fdbd0c727e225f350bb30Brian Carlstrom            throws SocketException {
4197ab7a8b582b29d34ec0fdbd0c727e225f350bb30Brian Carlstrom        castToOpenSSLSocket(socket).setSoWriteTimeout(writeTimeoutMilliseconds);
4207ab7a8b582b29d34ec0fdbd0c727e225f350bb30Brian Carlstrom    }
4217ab7a8b582b29d34ec0fdbd0c727e225f350bb30Brian Carlstrom
422b4db962da0fecd9a6f2714148bbdea023610842fNarayan Kamath    private static OpenSSLSocketImpl castToOpenSSLSocket(Socket socket) {
423b4db962da0fecd9a6f2714148bbdea023610842fNarayan Kamath        if (!(socket instanceof OpenSSLSocketImpl)) {
424b4db962da0fecd9a6f2714148bbdea023610842fNarayan Kamath            throw new IllegalArgumentException("Socket not created by this factory: "
425b4db962da0fecd9a6f2714148bbdea023610842fNarayan Kamath                    + socket);
426b4db962da0fecd9a6f2714148bbdea023610842fNarayan Kamath        }
427b4db962da0fecd9a6f2714148bbdea023610842fNarayan Kamath
428b4db962da0fecd9a6f2714148bbdea023610842fNarayan Kamath        return (OpenSSLSocketImpl) socket;
429b4db962da0fecd9a6f2714148bbdea023610842fNarayan Kamath    }
4301b52806e21270ccbe90d27f3dd93cbee1a81d09eBen Komalo
4311b52806e21270ccbe90d27f3dd93cbee1a81d09eBen Komalo    /**
4327fc93c36ae235115727296780dbc35101622bbd4Dan Egnor     * {@inheritDoc}
4337fc93c36ae235115727296780dbc35101622bbd4Dan Egnor     *
434df27c0c26209fe04de332497beafcafc1fbaad2bAndrew Stadler     * <p>This method verifies the peer's certificate hostname after connecting
435df27c0c26209fe04de332497beafcafc1fbaad2bAndrew Stadler     * (unless created with {@link #getInsecure(int, SSLSessionCache)}).
4367fc93c36ae235115727296780dbc35101622bbd4Dan Egnor     */
43760586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor    @Override
43860586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor    public Socket createSocket(Socket k, String host, int port, boolean close) throws IOException {
43960586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor        OpenSSLSocketImpl s = (OpenSSLSocketImpl) getDelegate().createSocket(k, host, port, close);
440f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson        s.setNpnProtocols(mNpnProtocols);
441100d7290264338c6536739abd59879aaaa812537Kenny Root        s.setAlpnProtocols(mAlpnProtocols);
44260586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor        s.setHandshakeTimeout(mHandshakeTimeoutMillis);
4434ef6c9b6a16c9b65699705aaa64977fc60dd3331Alex Klyubin        s.setChannelIdPrivateKey(mChannelIdPrivateKey);
444df27c0c26209fe04de332497beafcafc1fbaad2bAndrew Stadler        if (mSecure) {
445df27c0c26209fe04de332497beafcafc1fbaad2bAndrew Stadler            verifyHostname(s, host);
446df27c0c26209fe04de332497beafcafc1fbaad2bAndrew Stadler        }
44760586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor        return s;
4489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
4499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4507fc93c36ae235115727296780dbc35101622bbd4Dan Egnor    /**
4517fc93c36ae235115727296780dbc35101622bbd4Dan Egnor     * Creates a new socket which is not connected to any remote host.
4527fc93c36ae235115727296780dbc35101622bbd4Dan Egnor     * You must use {@link Socket#connect} to connect the socket.
4537fc93c36ae235115727296780dbc35101622bbd4Dan Egnor     *
4547fc93c36ae235115727296780dbc35101622bbd4Dan Egnor     * <p class="caution"><b>Warning:</b> Hostname verification is not performed
4557fc93c36ae235115727296780dbc35101622bbd4Dan Egnor     * with this method.  You MUST verify the server's identity after connecting
4567fc93c36ae235115727296780dbc35101622bbd4Dan Egnor     * the socket to avoid man-in-the-middle attacks.</p>
4577fc93c36ae235115727296780dbc35101622bbd4Dan Egnor     */
45860586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor    @Override
45960586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor    public Socket createSocket() throws IOException {
46060586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor        OpenSSLSocketImpl s = (OpenSSLSocketImpl) getDelegate().createSocket();
461f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson        s.setNpnProtocols(mNpnProtocols);
462100d7290264338c6536739abd59879aaaa812537Kenny Root        s.setAlpnProtocols(mAlpnProtocols);
46360586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor        s.setHandshakeTimeout(mHandshakeTimeoutMillis);
4644ef6c9b6a16c9b65699705aaa64977fc60dd3331Alex Klyubin        s.setChannelIdPrivateKey(mChannelIdPrivateKey);
46560586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor        return s;
4669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
4679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4687fc93c36ae235115727296780dbc35101622bbd4Dan Egnor    /**
4697fc93c36ae235115727296780dbc35101622bbd4Dan Egnor     * {@inheritDoc}
4707fc93c36ae235115727296780dbc35101622bbd4Dan Egnor     *
4717fc93c36ae235115727296780dbc35101622bbd4Dan Egnor     * <p class="caution"><b>Warning:</b> Hostname verification is not performed
4727fc93c36ae235115727296780dbc35101622bbd4Dan Egnor     * with this method.  You MUST verify the server's identity after connecting
4737fc93c36ae235115727296780dbc35101622bbd4Dan Egnor     * the socket to avoid man-in-the-middle attacks.</p>
4747fc93c36ae235115727296780dbc35101622bbd4Dan Egnor     */
47560586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor    @Override
47660586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor    public Socket createSocket(InetAddress addr, int port, InetAddress localAddr, int localPort)
47760586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor            throws IOException {
47860586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor        OpenSSLSocketImpl s = (OpenSSLSocketImpl) getDelegate().createSocket(
47960586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor                addr, port, localAddr, localPort);
480f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson        s.setNpnProtocols(mNpnProtocols);
481100d7290264338c6536739abd59879aaaa812537Kenny Root        s.setAlpnProtocols(mAlpnProtocols);
48260586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor        s.setHandshakeTimeout(mHandshakeTimeoutMillis);
4834ef6c9b6a16c9b65699705aaa64977fc60dd3331Alex Klyubin        s.setChannelIdPrivateKey(mChannelIdPrivateKey);
48460586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor        return s;
4859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
4869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4877fc93c36ae235115727296780dbc35101622bbd4Dan Egnor    /**
4887fc93c36ae235115727296780dbc35101622bbd4Dan Egnor     * {@inheritDoc}
4897fc93c36ae235115727296780dbc35101622bbd4Dan Egnor     *
4907fc93c36ae235115727296780dbc35101622bbd4Dan Egnor     * <p class="caution"><b>Warning:</b> Hostname verification is not performed
4917fc93c36ae235115727296780dbc35101622bbd4Dan Egnor     * with this method.  You MUST verify the server's identity after connecting
4927fc93c36ae235115727296780dbc35101622bbd4Dan Egnor     * the socket to avoid man-in-the-middle attacks.</p>
4937fc93c36ae235115727296780dbc35101622bbd4Dan Egnor     */
49460586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor    @Override
49560586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor    public Socket createSocket(InetAddress addr, int port) throws IOException {
49660586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor        OpenSSLSocketImpl s = (OpenSSLSocketImpl) getDelegate().createSocket(addr, port);
497f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson        s.setNpnProtocols(mNpnProtocols);
498100d7290264338c6536739abd59879aaaa812537Kenny Root        s.setAlpnProtocols(mAlpnProtocols);
49960586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor        s.setHandshakeTimeout(mHandshakeTimeoutMillis);
5004ef6c9b6a16c9b65699705aaa64977fc60dd3331Alex Klyubin        s.setChannelIdPrivateKey(mChannelIdPrivateKey);
50160586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor        return s;
5029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
5039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5047fc93c36ae235115727296780dbc35101622bbd4Dan Egnor    /**
5057fc93c36ae235115727296780dbc35101622bbd4Dan Egnor     * {@inheritDoc}
5067fc93c36ae235115727296780dbc35101622bbd4Dan Egnor     *
507df27c0c26209fe04de332497beafcafc1fbaad2bAndrew Stadler     * <p>This method verifies the peer's certificate hostname after connecting
508df27c0c26209fe04de332497beafcafc1fbaad2bAndrew Stadler     * (unless created with {@link #getInsecure(int, SSLSessionCache)}).
5097fc93c36ae235115727296780dbc35101622bbd4Dan Egnor     */
51060586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor    @Override
51160586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor    public Socket createSocket(String host, int port, InetAddress localAddr, int localPort)
51260586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor            throws IOException {
51360586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor        OpenSSLSocketImpl s = (OpenSSLSocketImpl) getDelegate().createSocket(
51460586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor                host, port, localAddr, localPort);
515f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson        s.setNpnProtocols(mNpnProtocols);
516100d7290264338c6536739abd59879aaaa812537Kenny Root        s.setAlpnProtocols(mAlpnProtocols);
51760586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor        s.setHandshakeTimeout(mHandshakeTimeoutMillis);
5184ef6c9b6a16c9b65699705aaa64977fc60dd3331Alex Klyubin        s.setChannelIdPrivateKey(mChannelIdPrivateKey);
519df27c0c26209fe04de332497beafcafc1fbaad2bAndrew Stadler        if (mSecure) {
520df27c0c26209fe04de332497beafcafc1fbaad2bAndrew Stadler            verifyHostname(s, host);
521df27c0c26209fe04de332497beafcafc1fbaad2bAndrew Stadler        }
52260586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor        return s;
52360586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor    }
5249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5257fc93c36ae235115727296780dbc35101622bbd4Dan Egnor    /**
5267fc93c36ae235115727296780dbc35101622bbd4Dan Egnor     * {@inheritDoc}
5277fc93c36ae235115727296780dbc35101622bbd4Dan Egnor     *
528df27c0c26209fe04de332497beafcafc1fbaad2bAndrew Stadler     * <p>This method verifies the peer's certificate hostname after connecting
529df27c0c26209fe04de332497beafcafc1fbaad2bAndrew Stadler     * (unless created with {@link #getInsecure(int, SSLSessionCache)}).
5307fc93c36ae235115727296780dbc35101622bbd4Dan Egnor     */
53160586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor    @Override
53260586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor    public Socket createSocket(String host, int port) throws IOException {
53360586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor        OpenSSLSocketImpl s = (OpenSSLSocketImpl) getDelegate().createSocket(host, port);
534f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson        s.setNpnProtocols(mNpnProtocols);
535100d7290264338c6536739abd59879aaaa812537Kenny Root        s.setAlpnProtocols(mAlpnProtocols);
53660586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor        s.setHandshakeTimeout(mHandshakeTimeoutMillis);
5374ef6c9b6a16c9b65699705aaa64977fc60dd3331Alex Klyubin        s.setChannelIdPrivateKey(mChannelIdPrivateKey);
538df27c0c26209fe04de332497beafcafc1fbaad2bAndrew Stadler        if (mSecure) {
539df27c0c26209fe04de332497beafcafc1fbaad2bAndrew Stadler            verifyHostname(s, host);
540df27c0c26209fe04de332497beafcafc1fbaad2bAndrew Stadler        }
54160586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor        return s;
5429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
5439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
54460586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor    @Override
5459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public String[] getDefaultCipherSuites() {
546019118af67c60448030540deca37d972c8839d38Alex Klyubin        return getDelegate().getDefaultCipherSuites();
5479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
5489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
54960586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor    @Override
5509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public String[] getSupportedCipherSuites() {
55160586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor        return getDelegate().getSupportedCipherSuites();
5529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
5539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
554