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; 2112e752225aa96888358294be0d725d499a1c9f03Kenny Rootimport com.android.org.conscrypt.OpenSSLContextImpl; 2212e752225aa96888358294be0d725d499a1c9f03Kenny Rootimport com.android.org.conscrypt.OpenSSLSocketImpl; 2312e752225aa96888358294be0d725d499a1c9f03Kenny Rootimport com.android.org.conscrypt.SSLClientSessionCache; 249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport java.io.IOException; 259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport java.net.InetAddress; 269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport java.net.Socket; 277ab7a8b582b29d34ec0fdbd0c727e225f350bb30Brian Carlstromimport java.net.SocketException; 289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport java.security.KeyManagementException; 29ac5eb03a7c317e21573155b88641f4f1daef2eb9Alex Klyubinimport java.security.PrivateKey; 309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport java.security.cert.X509Certificate; 319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport javax.net.SocketFactory; 327fc93c36ae235115727296780dbc35101622bbd4Dan Egnorimport javax.net.ssl.HostnameVerifier; 337fc93c36ae235115727296780dbc35101622bbd4Dan Egnorimport javax.net.ssl.HttpsURLConnection; 341b52806e21270ccbe90d27f3dd93cbee1a81d09eBen Komaloimport javax.net.ssl.KeyManager; 357fc93c36ae235115727296780dbc35101622bbd4Dan Egnorimport javax.net.ssl.SSLException; 367fc93c36ae235115727296780dbc35101622bbd4Dan Egnorimport javax.net.ssl.SSLPeerUnverifiedException; 377fc93c36ae235115727296780dbc35101622bbd4Dan Egnorimport javax.net.ssl.SSLSession; 389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport javax.net.ssl.SSLSocket; 399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport javax.net.ssl.SSLSocketFactory; 409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport javax.net.ssl.TrustManager; 419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport javax.net.ssl.X509TrustManager; 429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/** 4460586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor * SSLSocketFactory implementation with several extra features: 457fc93c36ae235115727296780dbc35101622bbd4Dan Egnor * 4660586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor * <ul> 4760586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor * <li>Timeout specification for SSL handshake operations 487fc93c36ae235115727296780dbc35101622bbd4Dan Egnor * <li>Hostname verification in most cases (see WARNINGs below) 4960586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor * <li>Optional SSL session caching with {@link SSLSessionCache} 509d4b57545300c6de1722094404ae09bf0f6be937Dan Egnor * <li>Optionally bypass all SSL certificate checks 5160586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor * </ul> 527fc93c36ae235115727296780dbc35101622bbd4Dan Egnor * 537fc93c36ae235115727296780dbc35101622bbd4Dan Egnor * The handshake timeout does not apply to actual TCP socket connection. 547fc93c36ae235115727296780dbc35101622bbd4Dan Egnor * If you want a connection timeout as well, use {@link #createSocket()} 557fc93c36ae235115727296780dbc35101622bbd4Dan Egnor * and {@link Socket#connect(SocketAddress, int)}, after which you 567fc93c36ae235115727296780dbc35101622bbd4Dan Egnor * must verify the identity of the server you are connected to. 577fc93c36ae235115727296780dbc35101622bbd4Dan Egnor * 587fc93c36ae235115727296780dbc35101622bbd4Dan Egnor * <p class="caution"><b>Most {@link SSLSocketFactory} implementations do not 597fc93c36ae235115727296780dbc35101622bbd4Dan Egnor * verify the server's identity, allowing man-in-the-middle attacks.</b> 607fc93c36ae235115727296780dbc35101622bbd4Dan Egnor * This implementation does check the server's certificate hostname, but only 617fc93c36ae235115727296780dbc35101622bbd4Dan Egnor * for createSocket variants that specify a hostname. When using methods that 627fc93c36ae235115727296780dbc35101622bbd4Dan Egnor * use {@link InetAddress} or which return an unconnected socket, you MUST 637fc93c36ae235115727296780dbc35101622bbd4Dan Egnor * verify the server's identity yourself to ensure a secure connection.</p> 647fc93c36ae235115727296780dbc35101622bbd4Dan Egnor * 657fc93c36ae235115727296780dbc35101622bbd4Dan Egnor * <p>One way to verify the server's identity is to use 667fc93c36ae235115727296780dbc35101622bbd4Dan Egnor * {@link HttpsURLConnection#getDefaultHostnameVerifier()} to get a 677fc93c36ae235115727296780dbc35101622bbd4Dan Egnor * {@link HostnameVerifier} to verify the certificate hostname. 687fc93c36ae235115727296780dbc35101622bbd4Dan Egnor * 697fc93c36ae235115727296780dbc35101622bbd4Dan Egnor * <p>On development devices, "setprop socket.relaxsslcheck yes" bypasses all 707fc93c36ae235115727296780dbc35101622bbd4Dan Egnor * SSL certificate and hostname checks for testing purposes. This setting 717fc93c36ae235115727296780dbc35101622bbd4Dan Egnor * requires root access. 729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectpublic class SSLCertificateSocketFactory extends SSLSocketFactory { 7460586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor private static final String TAG = "SSLCertificateSocketFactory"; 759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 7660586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor private static final TrustManager[] INSECURE_TRUST_MANAGER = new TrustManager[] { 779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project new X509TrustManager() { 7860586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor public X509Certificate[] getAcceptedIssuers() { return null; } 7960586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor public void checkClientTrusted(X509Certificate[] certs, String authType) { } 8060586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor public void checkServerTrusted(X509Certificate[] certs, String authType) { } 819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project }; 839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 8460586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor private SSLSocketFactory mInsecureFactory = null; 8560586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor private SSLSocketFactory mSecureFactory = null; 861b52806e21270ccbe90d27f3dd93cbee1a81d09eBen Komalo private TrustManager[] mTrustManagers = null; 871b52806e21270ccbe90d27f3dd93cbee1a81d09eBen Komalo private KeyManager[] mKeyManagers = null; 88f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson private byte[] mNpnProtocols = null; 89100d7290264338c6536739abd59879aaaa812537Kenny Root private byte[] mAlpnProtocols = null; 90ac5eb03a7c317e21573155b88641f4f1daef2eb9Alex Klyubin private PrivateKey mChannelIdPrivateKey = null; 919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 9260586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor private final int mHandshakeTimeoutMillis; 9360586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor private final SSLClientSessionCache mSessionCache; 949d4b57545300c6de1722094404ae09bf0f6be937Dan Egnor private final boolean mSecure; 959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 9660586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor /** @deprecated Use {@link #getDefault(int)} instead. */ 9728c742573ccaeb55c16bc02fb25fdd86b8d1f76aJesse Wilson @Deprecated 9860586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor public SSLCertificateSocketFactory(int handshakeTimeoutMillis) { 999d4b57545300c6de1722094404ae09bf0f6be937Dan Egnor this(handshakeTimeoutMillis, null, true); 1009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 1019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 1029d4b57545300c6de1722094404ae09bf0f6be937Dan Egnor private SSLCertificateSocketFactory( 103992f238d13fff7c21b60ef6958784a4ed2156784Brian Carlstrom int handshakeTimeoutMillis, SSLSessionCache cache, boolean secure) { 10460586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor mHandshakeTimeoutMillis = handshakeTimeoutMillis; 10560586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor mSessionCache = cache == null ? null : cache.mSessionCache; 1069d4b57545300c6de1722094404ae09bf0f6be937Dan Egnor mSecure = secure; 1079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 1089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 1099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 1109d4b57545300c6de1722094404ae09bf0f6be937Dan Egnor * Returns a new socket factory instance with an optional handshake timeout. 1119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * 11260586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor * @param handshakeTimeoutMillis to use for SSL connection handshake, or 0 11360586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor * for none. The socket timeout is reset to 0 after the handshake. 1147fc93c36ae235115727296780dbc35101622bbd4Dan Egnor * @return a new SSLSocketFactory with the specified parameters 1159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 11660586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor public static SocketFactory getDefault(int handshakeTimeoutMillis) { 1179d4b57545300c6de1722094404ae09bf0f6be937Dan Egnor return new SSLCertificateSocketFactory(handshakeTimeoutMillis, null, true); 1189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 1199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 1209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 1219d4b57545300c6de1722094404ae09bf0f6be937Dan Egnor * Returns a new socket factory instance with an optional handshake timeout 1229d4b57545300c6de1722094404ae09bf0f6be937Dan Egnor * and SSL session cache. 1239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * 12460586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor * @param handshakeTimeoutMillis to use for SSL connection handshake, or 0 12560586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor * for none. The socket timeout is reset to 0 after the handshake. 126ff5569948fda346d95d4615de6578f82d9614be3Jesse Wilson * @param cache The {@link SSLSessionCache} to use, or null for no cache. 1277fc93c36ae235115727296780dbc35101622bbd4Dan Egnor * @return a new SSLSocketFactory with the specified parameters 12860586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor */ 1299d4b57545300c6de1722094404ae09bf0f6be937Dan Egnor public static SSLSocketFactory getDefault(int handshakeTimeoutMillis, SSLSessionCache cache) { 1309d4b57545300c6de1722094404ae09bf0f6be937Dan Egnor return new SSLCertificateSocketFactory(handshakeTimeoutMillis, cache, true); 1319d4b57545300c6de1722094404ae09bf0f6be937Dan Egnor } 1329d4b57545300c6de1722094404ae09bf0f6be937Dan Egnor 1339d4b57545300c6de1722094404ae09bf0f6be937Dan Egnor /** 1349d4b57545300c6de1722094404ae09bf0f6be937Dan Egnor * Returns a new instance of a socket factory with all SSL security checks 1359d4b57545300c6de1722094404ae09bf0f6be937Dan Egnor * disabled, using an optional handshake timeout and SSL session cache. 1367fc93c36ae235115727296780dbc35101622bbd4Dan Egnor * 1377fc93c36ae235115727296780dbc35101622bbd4Dan Egnor * <p class="caution"><b>Warning:</b> Sockets created using this factory 138e19ca078bf1778a344366672de020e63a80252a9Kenny Root * are vulnerable to man-in-the-middle attacks!</p> 1399d4b57545300c6de1722094404ae09bf0f6be937Dan Egnor * 1409d4b57545300c6de1722094404ae09bf0f6be937Dan Egnor * @param handshakeTimeoutMillis to use for SSL connection handshake, or 0 1419d4b57545300c6de1722094404ae09bf0f6be937Dan Egnor * for none. The socket timeout is reset to 0 after the handshake. 142ff5569948fda346d95d4615de6578f82d9614be3Jesse Wilson * @param cache The {@link SSLSessionCache} to use, or null for no cache. 1437fc93c36ae235115727296780dbc35101622bbd4Dan Egnor * @return an insecure SSLSocketFactory with the specified parameters 1449d4b57545300c6de1722094404ae09bf0f6be937Dan Egnor */ 1459d4b57545300c6de1722094404ae09bf0f6be937Dan Egnor public static SSLSocketFactory getInsecure(int handshakeTimeoutMillis, SSLSessionCache cache) { 1469d4b57545300c6de1722094404ae09bf0f6be937Dan Egnor return new SSLCertificateSocketFactory(handshakeTimeoutMillis, cache, false); 1479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 1489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 14960586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor /** 15060586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor * Returns a socket factory (also named SSLSocketFactory, but in a different 15160586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor * namespace) for use with the Apache HTTP stack. 15260586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor * 15360586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor * @param handshakeTimeoutMillis to use for SSL connection handshake, or 0 15460586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor * for none. The socket timeout is reset to 0 after the handshake. 155ff5569948fda346d95d4615de6578f82d9614be3Jesse Wilson * @param cache The {@link SSLSessionCache} to use, or null for no cache. 15660586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor * @return a new SocketFactory with the specified parameters 157823675fdbb7f974b8e2fa9fbb71774b32487582dNarayan Kamath * 158823675fdbb7f974b8e2fa9fbb71774b32487582dNarayan Kamath * @deprecated Use {@link #getDefault()} along with a {@link javax.net.ssl.HttpsURLConnection} 159823675fdbb7f974b8e2fa9fbb71774b32487582dNarayan Kamath * instead. The Apache HTTP client is no longer maintained and may be removed in a future 160823675fdbb7f974b8e2fa9fbb71774b32487582dNarayan Kamath * release. Please visit <a href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html">this webpage</a> 161823675fdbb7f974b8e2fa9fbb71774b32487582dNarayan Kamath * for further details. 162406e1ed9883010928cfb42246cfd2710ebf3da74Narayan Kamath * 163406e1ed9883010928cfb42246cfd2710ebf3da74Narayan Kamath * @removed 16460586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor */ 165823675fdbb7f974b8e2fa9fbb71774b32487582dNarayan Kamath @Deprecated 16660586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor public static org.apache.http.conn.ssl.SSLSocketFactory getHttpSocketFactory( 167ff5569948fda346d95d4615de6578f82d9614be3Jesse Wilson int handshakeTimeoutMillis, SSLSessionCache cache) { 16860586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor return new org.apache.http.conn.ssl.SSLSocketFactory( 1699d4b57545300c6de1722094404ae09bf0f6be937Dan Egnor new SSLCertificateSocketFactory(handshakeTimeoutMillis, cache, true)); 1709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 1719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 1727fc93c36ae235115727296780dbc35101622bbd4Dan Egnor /** 1737fc93c36ae235115727296780dbc35101622bbd4Dan Egnor * Verify the hostname of the certificate used by the other end of a 1747fc93c36ae235115727296780dbc35101622bbd4Dan Egnor * connected socket. You MUST call this if you did not supply a hostname 1757fc93c36ae235115727296780dbc35101622bbd4Dan Egnor * to {@link #createSocket()}. It is harmless to call this method 1767fc93c36ae235115727296780dbc35101622bbd4Dan Egnor * redundantly if the hostname has already been verified. 1777fc93c36ae235115727296780dbc35101622bbd4Dan Egnor * 1787fc93c36ae235115727296780dbc35101622bbd4Dan Egnor * <p>Wildcard certificates are allowed to verify any matching hostname, 1797fc93c36ae235115727296780dbc35101622bbd4Dan Egnor * so "foo.bar.example.com" is verified if the peer has a certificate 1807fc93c36ae235115727296780dbc35101622bbd4Dan Egnor * for "*.example.com". 1817fc93c36ae235115727296780dbc35101622bbd4Dan Egnor * 1827fc93c36ae235115727296780dbc35101622bbd4Dan Egnor * @param socket An SSL socket which has been connected to a server 1837fc93c36ae235115727296780dbc35101622bbd4Dan Egnor * @param hostname The expected hostname of the remote server 1847fc93c36ae235115727296780dbc35101622bbd4Dan Egnor * @throws IOException if something goes wrong handshaking with the server 1857fc93c36ae235115727296780dbc35101622bbd4Dan Egnor * @throws SSLPeerUnverifiedException if the server cannot prove its identity 1867fc93c36ae235115727296780dbc35101622bbd4Dan Egnor * 1877fc93c36ae235115727296780dbc35101622bbd4Dan Egnor * @hide 1887fc93c36ae235115727296780dbc35101622bbd4Dan Egnor */ 1897fc93c36ae235115727296780dbc35101622bbd4Dan Egnor public static void verifyHostname(Socket socket, String hostname) throws IOException { 1907fc93c36ae235115727296780dbc35101622bbd4Dan Egnor if (!(socket instanceof SSLSocket)) { 1917fc93c36ae235115727296780dbc35101622bbd4Dan Egnor throw new IllegalArgumentException("Attempt to verify non-SSL socket"); 1927fc93c36ae235115727296780dbc35101622bbd4Dan Egnor } 1937fc93c36ae235115727296780dbc35101622bbd4Dan Egnor 1947fc93c36ae235115727296780dbc35101622bbd4Dan Egnor if (!isSslCheckRelaxed()) { 1957fc93c36ae235115727296780dbc35101622bbd4Dan Egnor // The code at the start of OpenSSLSocketImpl.startHandshake() 1967fc93c36ae235115727296780dbc35101622bbd4Dan Egnor // ensures that the call is idempotent, so we can safely call it. 1977fc93c36ae235115727296780dbc35101622bbd4Dan Egnor SSLSocket ssl = (SSLSocket) socket; 1987fc93c36ae235115727296780dbc35101622bbd4Dan Egnor ssl.startHandshake(); 1997fc93c36ae235115727296780dbc35101622bbd4Dan Egnor 2007fc93c36ae235115727296780dbc35101622bbd4Dan Egnor SSLSession session = ssl.getSession(); 2017fc93c36ae235115727296780dbc35101622bbd4Dan Egnor if (session == null) { 2027fc93c36ae235115727296780dbc35101622bbd4Dan Egnor throw new SSLException("Cannot verify SSL socket without session"); 2037fc93c36ae235115727296780dbc35101622bbd4Dan Egnor } 204928ee1e48fa89302d02fdf8a8a2c7315d7195e7cKenny Root if (!HttpsURLConnection.getDefaultHostnameVerifier().verify(hostname, session)) { 2057fc93c36ae235115727296780dbc35101622bbd4Dan Egnor throw new SSLPeerUnverifiedException("Cannot verify hostname: " + hostname); 2067fc93c36ae235115727296780dbc35101622bbd4Dan Egnor } 2077fc93c36ae235115727296780dbc35101622bbd4Dan Egnor } 2087fc93c36ae235115727296780dbc35101622bbd4Dan Egnor } 2097fc93c36ae235115727296780dbc35101622bbd4Dan Egnor 2101b52806e21270ccbe90d27f3dd93cbee1a81d09eBen Komalo private SSLSocketFactory makeSocketFactory( 2111b52806e21270ccbe90d27f3dd93cbee1a81d09eBen Komalo KeyManager[] keyManagers, TrustManager[] trustManagers) { 21260586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor try { 2138a970637208207955fb6a719bd82902384b3c743Kenny Root OpenSSLContextImpl sslContext = OpenSSLContextImpl.getPreferred(); 2141b52806e21270ccbe90d27f3dd93cbee1a81d09eBen Komalo sslContext.engineInit(keyManagers, trustManagers, null); 2152c42c8fbaf02be1f3ea6298077128d0c419526f0Brian Carlstrom sslContext.engineGetClientSessionContext().setPersistentCache(mSessionCache); 21660586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor return sslContext.engineGetSocketFactory(); 21760586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor } catch (KeyManagementException e) { 21860586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor Log.wtf(TAG, e); 21960586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor return (SSLSocketFactory) SSLSocketFactory.getDefault(); // Fallback 2209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 22160586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor } 2229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 2237fc93c36ae235115727296780dbc35101622bbd4Dan Egnor private static boolean isSslCheckRelaxed() { 2247fc93c36ae235115727296780dbc35101622bbd4Dan Egnor return "1".equals(SystemProperties.get("ro.debuggable")) && 2257fc93c36ae235115727296780dbc35101622bbd4Dan Egnor "yes".equals(SystemProperties.get("socket.relaxsslcheck")); 2267fc93c36ae235115727296780dbc35101622bbd4Dan Egnor } 2277fc93c36ae235115727296780dbc35101622bbd4Dan Egnor 22860586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor private synchronized SSLSocketFactory getDelegate() { 2299d4b57545300c6de1722094404ae09bf0f6be937Dan Egnor // Relax the SSL check if instructed (for this factory, or systemwide) 2307fc93c36ae235115727296780dbc35101622bbd4Dan Egnor if (!mSecure || isSslCheckRelaxed()) { 23160586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor if (mInsecureFactory == null) { 2329d4b57545300c6de1722094404ae09bf0f6be937Dan Egnor if (mSecure) { 2339d4b57545300c6de1722094404ae09bf0f6be937Dan Egnor Log.w(TAG, "*** BYPASSING SSL SECURITY CHECKS (socket.relaxsslcheck=yes) ***"); 234e19ca078bf1778a344366672de020e63a80252a9Kenny Root } else { 235e19ca078bf1778a344366672de020e63a80252a9Kenny Root Log.w(TAG, "Bypassing SSL security checks at caller's request"); 2369d4b57545300c6de1722094404ae09bf0f6be937Dan Egnor } 2371b52806e21270ccbe90d27f3dd93cbee1a81d09eBen Komalo mInsecureFactory = makeSocketFactory(mKeyManagers, INSECURE_TRUST_MANAGER); 2389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 23960586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor return mInsecureFactory; 24060586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor } else { 24160586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor if (mSecureFactory == null) { 2421b52806e21270ccbe90d27f3dd93cbee1a81d09eBen Komalo mSecureFactory = makeSocketFactory(mKeyManagers, mTrustManagers); 2439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 24460586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor return mSecureFactory; 2459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 2469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 2479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 2487fc93c36ae235115727296780dbc35101622bbd4Dan Egnor /** 2491b52806e21270ccbe90d27f3dd93cbee1a81d09eBen Komalo * Sets the {@link TrustManager}s to be used for connections made by this factory. 2501b52806e21270ccbe90d27f3dd93cbee1a81d09eBen Komalo */ 2511b52806e21270ccbe90d27f3dd93cbee1a81d09eBen Komalo public void setTrustManagers(TrustManager[] trustManager) { 2521b52806e21270ccbe90d27f3dd93cbee1a81d09eBen Komalo mTrustManagers = trustManager; 2531b52806e21270ccbe90d27f3dd93cbee1a81d09eBen Komalo 2541b52806e21270ccbe90d27f3dd93cbee1a81d09eBen Komalo // Clear out all cached secure factories since configurations have changed. 2551b52806e21270ccbe90d27f3dd93cbee1a81d09eBen Komalo mSecureFactory = null; 2561b52806e21270ccbe90d27f3dd93cbee1a81d09eBen Komalo // Note - insecure factories only ever use the INSECURE_TRUST_MANAGER so they need not 2571b52806e21270ccbe90d27f3dd93cbee1a81d09eBen Komalo // be cleared out here. 2581b52806e21270ccbe90d27f3dd93cbee1a81d09eBen Komalo } 2591b52806e21270ccbe90d27f3dd93cbee1a81d09eBen Komalo 2601b52806e21270ccbe90d27f3dd93cbee1a81d09eBen Komalo /** 261f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson * Sets the <a href="http://technotes.googlecode.com/git/nextprotoneg.html">Next 262f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson * Protocol Negotiation (NPN)</a> protocols that this peer is interested in. 263f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson * 264f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson * <p>For servers this is the sequence of protocols to advertise as 265f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson * supported, in order of preference. This list is sent unencrypted to 266f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson * all clients that support NPN. 267f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson * 268f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson * <p>For clients this is a list of supported protocols to match against the 269f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson * server's list. If there is no protocol supported by both client and 270f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson * server then the first protocol in the client's list will be selected. 271f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson * The order of the client's protocols is otherwise insignificant. 272f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson * 2732108ead7f125536874d6de6ca1c0c4cffbf61b44Jesse Wilson * @param npnProtocols a non-empty list of protocol byte arrays. All arrays 2742108ead7f125536874d6de6ca1c0c4cffbf61b44Jesse Wilson * must be non-empty and of length less than 256. 275f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson */ 276f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson public void setNpnProtocols(byte[][] npnProtocols) { 277100d7290264338c6536739abd59879aaaa812537Kenny Root this.mNpnProtocols = toLengthPrefixedList(npnProtocols); 278100d7290264338c6536739abd59879aaaa812537Kenny Root } 279100d7290264338c6536739abd59879aaaa812537Kenny Root 280100d7290264338c6536739abd59879aaaa812537Kenny Root /** 281100d7290264338c6536739abd59879aaaa812537Kenny Root * Sets the 282100d7290264338c6536739abd59879aaaa812537Kenny Root * <a href="http://tools.ietf.org/html/draft-ietf-tls-applayerprotoneg-01"> 283100d7290264338c6536739abd59879aaaa812537Kenny Root * Application Layer Protocol Negotiation (ALPN)</a> protocols that this peer 284100d7290264338c6536739abd59879aaaa812537Kenny Root * is interested in. 285100d7290264338c6536739abd59879aaaa812537Kenny Root * 286100d7290264338c6536739abd59879aaaa812537Kenny Root * <p>For servers this is the sequence of protocols to advertise as 287100d7290264338c6536739abd59879aaaa812537Kenny Root * supported, in order of preference. This list is sent unencrypted to 288100d7290264338c6536739abd59879aaaa812537Kenny Root * all clients that support ALPN. 289100d7290264338c6536739abd59879aaaa812537Kenny Root * 290100d7290264338c6536739abd59879aaaa812537Kenny Root * <p>For clients this is a list of supported protocols to match against the 291100d7290264338c6536739abd59879aaaa812537Kenny Root * server's list. If there is no protocol supported by both client and 292100d7290264338c6536739abd59879aaaa812537Kenny Root * server then the first protocol in the client's list will be selected. 293100d7290264338c6536739abd59879aaaa812537Kenny Root * The order of the client's protocols is otherwise insignificant. 294100d7290264338c6536739abd59879aaaa812537Kenny Root * 295100d7290264338c6536739abd59879aaaa812537Kenny Root * @param protocols a non-empty list of protocol byte arrays. All arrays 296100d7290264338c6536739abd59879aaaa812537Kenny Root * must be non-empty and of length less than 256. 297100d7290264338c6536739abd59879aaaa812537Kenny Root * @hide 298100d7290264338c6536739abd59879aaaa812537Kenny Root */ 299100d7290264338c6536739abd59879aaaa812537Kenny Root public void setAlpnProtocols(byte[][] protocols) { 300100d7290264338c6536739abd59879aaaa812537Kenny Root this.mAlpnProtocols = toLengthPrefixedList(protocols); 301f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson } 302f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson 303f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson /** 304f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson * Returns an array containing the concatenation of length-prefixed byte 305f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson * strings. 306f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson */ 307100d7290264338c6536739abd59879aaaa812537Kenny Root static byte[] toLengthPrefixedList(byte[]... items) { 308100d7290264338c6536739abd59879aaaa812537Kenny Root if (items.length == 0) { 309100d7290264338c6536739abd59879aaaa812537Kenny Root throw new IllegalArgumentException("items.length == 0"); 3102108ead7f125536874d6de6ca1c0c4cffbf61b44Jesse Wilson } 311f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson int totalLength = 0; 312100d7290264338c6536739abd59879aaaa812537Kenny Root for (byte[] s : items) { 313f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson if (s.length == 0 || s.length > 255) { 314f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson throw new IllegalArgumentException("s.length == 0 || s.length > 255: " + s.length); 315f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson } 316f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson totalLength += 1 + s.length; 317f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson } 318f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson byte[] result = new byte[totalLength]; 319f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson int pos = 0; 320100d7290264338c6536739abd59879aaaa812537Kenny Root for (byte[] s : items) { 321f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson result[pos++] = (byte) s.length; 322f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson for (byte b : s) { 323f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson result[pos++] = b; 324f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson } 325f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson } 326f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson return result; 327f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson } 328f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson 329f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson /** 330f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson * Returns the <a href="http://technotes.googlecode.com/git/nextprotoneg.html">Next 331f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson * Protocol Negotiation (NPN)</a> protocol selected by client and server, or 332f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson * null if no protocol was negotiated. 333f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson * 334f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson * @param socket a socket created by this factory. 335b4db962da0fecd9a6f2714148bbdea023610842fNarayan Kamath * @throws IllegalArgumentException if the socket was not created by this factory. 336f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson */ 337f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson public byte[] getNpnSelectedProtocol(Socket socket) { 338b4db962da0fecd9a6f2714148bbdea023610842fNarayan Kamath return castToOpenSSLSocket(socket).getNpnSelectedProtocol(); 339f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson } 340f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson 341f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson /** 342100d7290264338c6536739abd59879aaaa812537Kenny Root * Returns the 343100d7290264338c6536739abd59879aaaa812537Kenny Root * <a href="http://tools.ietf.org/html/draft-ietf-tls-applayerprotoneg-01">Application 344100d7290264338c6536739abd59879aaaa812537Kenny Root * Layer Protocol Negotiation (ALPN)</a> protocol selected by client and server, or null 345100d7290264338c6536739abd59879aaaa812537Kenny Root * if no protocol was negotiated. 346100d7290264338c6536739abd59879aaaa812537Kenny Root * 347100d7290264338c6536739abd59879aaaa812537Kenny Root * @param socket a socket created by this factory. 348100d7290264338c6536739abd59879aaaa812537Kenny Root * @throws IllegalArgumentException if the socket was not created by this factory. 349100d7290264338c6536739abd59879aaaa812537Kenny Root * @hide 350100d7290264338c6536739abd59879aaaa812537Kenny Root */ 351100d7290264338c6536739abd59879aaaa812537Kenny Root public byte[] getAlpnSelectedProtocol(Socket socket) { 352100d7290264338c6536739abd59879aaaa812537Kenny Root return castToOpenSSLSocket(socket).getAlpnSelectedProtocol(); 353100d7290264338c6536739abd59879aaaa812537Kenny Root } 354100d7290264338c6536739abd59879aaaa812537Kenny Root 355100d7290264338c6536739abd59879aaaa812537Kenny Root /** 3561b52806e21270ccbe90d27f3dd93cbee1a81d09eBen Komalo * Sets the {@link KeyManager}s to be used for connections made by this factory. 3571b52806e21270ccbe90d27f3dd93cbee1a81d09eBen Komalo */ 3581b52806e21270ccbe90d27f3dd93cbee1a81d09eBen Komalo public void setKeyManagers(KeyManager[] keyManagers) { 3591b52806e21270ccbe90d27f3dd93cbee1a81d09eBen Komalo mKeyManagers = keyManagers; 3601b52806e21270ccbe90d27f3dd93cbee1a81d09eBen Komalo 3611b52806e21270ccbe90d27f3dd93cbee1a81d09eBen Komalo // Clear out any existing cached factories since configurations have changed. 3621b52806e21270ccbe90d27f3dd93cbee1a81d09eBen Komalo mSecureFactory = null; 3631b52806e21270ccbe90d27f3dd93cbee1a81d09eBen Komalo mInsecureFactory = null; 3641b52806e21270ccbe90d27f3dd93cbee1a81d09eBen Komalo } 3651b52806e21270ccbe90d27f3dd93cbee1a81d09eBen Komalo 366b4db962da0fecd9a6f2714148bbdea023610842fNarayan Kamath /** 367ac5eb03a7c317e21573155b88641f4f1daef2eb9Alex Klyubin * Sets the private key to be used for TLS Channel ID by connections made by this 3684ef6c9b6a16c9b65699705aaa64977fc60dd3331Alex Klyubin * factory. 3694ef6c9b6a16c9b65699705aaa64977fc60dd3331Alex Klyubin * 3704ef6c9b6a16c9b65699705aaa64977fc60dd3331Alex Klyubin * @param privateKey private key (enables TLS Channel ID) or {@code null} for no key (disables 3714ef6c9b6a16c9b65699705aaa64977fc60dd3331Alex Klyubin * TLS Channel ID). The private key has to be an Elliptic Curve (EC) key based on the 3724ef6c9b6a16c9b65699705aaa64977fc60dd3331Alex Klyubin * NIST P-256 curve (aka SECG secp256r1 or ANSI X9.62 prime256v1). 3734ef6c9b6a16c9b65699705aaa64977fc60dd3331Alex Klyubin * 3744ef6c9b6a16c9b65699705aaa64977fc60dd3331Alex Klyubin * @hide 3754ef6c9b6a16c9b65699705aaa64977fc60dd3331Alex Klyubin */ 376ac5eb03a7c317e21573155b88641f4f1daef2eb9Alex Klyubin public void setChannelIdPrivateKey(PrivateKey privateKey) { 3774ef6c9b6a16c9b65699705aaa64977fc60dd3331Alex Klyubin mChannelIdPrivateKey = privateKey; 3784ef6c9b6a16c9b65699705aaa64977fc60dd3331Alex Klyubin } 3794ef6c9b6a16c9b65699705aaa64977fc60dd3331Alex Klyubin 3804ef6c9b6a16c9b65699705aaa64977fc60dd3331Alex Klyubin /** 381b4db962da0fecd9a6f2714148bbdea023610842fNarayan Kamath * Enables <a href="http://tools.ietf.org/html/rfc5077#section-3.2">session ticket</a> 382b4db962da0fecd9a6f2714148bbdea023610842fNarayan Kamath * support on the given socket. 383b4db962da0fecd9a6f2714148bbdea023610842fNarayan Kamath * 384b4db962da0fecd9a6f2714148bbdea023610842fNarayan Kamath * @param socket a socket created by this factory 385b4db962da0fecd9a6f2714148bbdea023610842fNarayan Kamath * @param useSessionTickets {@code true} to enable session ticket support on this socket. 386b4db962da0fecd9a6f2714148bbdea023610842fNarayan Kamath * @throws IllegalArgumentException if the socket was not created by this factory. 387b4db962da0fecd9a6f2714148bbdea023610842fNarayan Kamath */ 388b4db962da0fecd9a6f2714148bbdea023610842fNarayan Kamath public void setUseSessionTickets(Socket socket, boolean useSessionTickets) { 389b4db962da0fecd9a6f2714148bbdea023610842fNarayan Kamath castToOpenSSLSocket(socket).setUseSessionTickets(useSessionTickets); 390b4db962da0fecd9a6f2714148bbdea023610842fNarayan Kamath } 391b4db962da0fecd9a6f2714148bbdea023610842fNarayan Kamath 392b4db962da0fecd9a6f2714148bbdea023610842fNarayan Kamath /** 393b4db962da0fecd9a6f2714148bbdea023610842fNarayan Kamath * Turns on <a href="http://tools.ietf.org/html/rfc6066#section-3">Server 394b4db962da0fecd9a6f2714148bbdea023610842fNarayan Kamath * Name Indication (SNI)</a> on a given socket. 395b4db962da0fecd9a6f2714148bbdea023610842fNarayan Kamath * 396b4db962da0fecd9a6f2714148bbdea023610842fNarayan Kamath * @param socket a socket created by this factory. 397b4db962da0fecd9a6f2714148bbdea023610842fNarayan Kamath * @param hostName the desired SNI hostname, null to disable. 398b4db962da0fecd9a6f2714148bbdea023610842fNarayan Kamath * @throws IllegalArgumentException if the socket was not created by this factory. 399b4db962da0fecd9a6f2714148bbdea023610842fNarayan Kamath */ 400b4db962da0fecd9a6f2714148bbdea023610842fNarayan Kamath public void setHostname(Socket socket, String hostName) { 401b4db962da0fecd9a6f2714148bbdea023610842fNarayan Kamath castToOpenSSLSocket(socket).setHostname(hostName); 402b4db962da0fecd9a6f2714148bbdea023610842fNarayan Kamath } 403b4db962da0fecd9a6f2714148bbdea023610842fNarayan Kamath 4047ab7a8b582b29d34ec0fdbd0c727e225f350bb30Brian Carlstrom /** 4057ab7a8b582b29d34ec0fdbd0c727e225f350bb30Brian Carlstrom * Sets this socket's SO_SNDTIMEO write timeout in milliseconds. 4067ab7a8b582b29d34ec0fdbd0c727e225f350bb30Brian Carlstrom * Use 0 for no timeout. 4077ab7a8b582b29d34ec0fdbd0c727e225f350bb30Brian Carlstrom * To take effect, this option must be set before the blocking method was called. 4087ab7a8b582b29d34ec0fdbd0c727e225f350bb30Brian Carlstrom * 4097ab7a8b582b29d34ec0fdbd0c727e225f350bb30Brian Carlstrom * @param socket a socket created by this factory. 410992f238d13fff7c21b60ef6958784a4ed2156784Brian Carlstrom * @param timeout the desired write timeout in milliseconds. 4117ab7a8b582b29d34ec0fdbd0c727e225f350bb30Brian Carlstrom * @throws IllegalArgumentException if the socket was not created by this factory. 412992f238d13fff7c21b60ef6958784a4ed2156784Brian Carlstrom * 413992f238d13fff7c21b60ef6958784a4ed2156784Brian Carlstrom * @hide 4147ab7a8b582b29d34ec0fdbd0c727e225f350bb30Brian Carlstrom */ 4157ab7a8b582b29d34ec0fdbd0c727e225f350bb30Brian Carlstrom public void setSoWriteTimeout(Socket socket, int writeTimeoutMilliseconds) 4167ab7a8b582b29d34ec0fdbd0c727e225f350bb30Brian Carlstrom throws SocketException { 4177ab7a8b582b29d34ec0fdbd0c727e225f350bb30Brian Carlstrom castToOpenSSLSocket(socket).setSoWriteTimeout(writeTimeoutMilliseconds); 4187ab7a8b582b29d34ec0fdbd0c727e225f350bb30Brian Carlstrom } 4197ab7a8b582b29d34ec0fdbd0c727e225f350bb30Brian Carlstrom 420b4db962da0fecd9a6f2714148bbdea023610842fNarayan Kamath private static OpenSSLSocketImpl castToOpenSSLSocket(Socket socket) { 421b4db962da0fecd9a6f2714148bbdea023610842fNarayan Kamath if (!(socket instanceof OpenSSLSocketImpl)) { 422b4db962da0fecd9a6f2714148bbdea023610842fNarayan Kamath throw new IllegalArgumentException("Socket not created by this factory: " 423b4db962da0fecd9a6f2714148bbdea023610842fNarayan Kamath + socket); 424b4db962da0fecd9a6f2714148bbdea023610842fNarayan Kamath } 425b4db962da0fecd9a6f2714148bbdea023610842fNarayan Kamath 426b4db962da0fecd9a6f2714148bbdea023610842fNarayan Kamath return (OpenSSLSocketImpl) socket; 427b4db962da0fecd9a6f2714148bbdea023610842fNarayan Kamath } 4281b52806e21270ccbe90d27f3dd93cbee1a81d09eBen Komalo 4291b52806e21270ccbe90d27f3dd93cbee1a81d09eBen Komalo /** 4307fc93c36ae235115727296780dbc35101622bbd4Dan Egnor * {@inheritDoc} 4317fc93c36ae235115727296780dbc35101622bbd4Dan Egnor * 432df27c0c26209fe04de332497beafcafc1fbaad2bAndrew Stadler * <p>This method verifies the peer's certificate hostname after connecting 433df27c0c26209fe04de332497beafcafc1fbaad2bAndrew Stadler * (unless created with {@link #getInsecure(int, SSLSessionCache)}). 4347fc93c36ae235115727296780dbc35101622bbd4Dan Egnor */ 43560586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor @Override 43660586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor public Socket createSocket(Socket k, String host, int port, boolean close) throws IOException { 43760586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor OpenSSLSocketImpl s = (OpenSSLSocketImpl) getDelegate().createSocket(k, host, port, close); 438f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson s.setNpnProtocols(mNpnProtocols); 439100d7290264338c6536739abd59879aaaa812537Kenny Root s.setAlpnProtocols(mAlpnProtocols); 44060586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor s.setHandshakeTimeout(mHandshakeTimeoutMillis); 4414ef6c9b6a16c9b65699705aaa64977fc60dd3331Alex Klyubin s.setChannelIdPrivateKey(mChannelIdPrivateKey); 442df27c0c26209fe04de332497beafcafc1fbaad2bAndrew Stadler if (mSecure) { 443df27c0c26209fe04de332497beafcafc1fbaad2bAndrew Stadler verifyHostname(s, host); 444df27c0c26209fe04de332497beafcafc1fbaad2bAndrew Stadler } 44560586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor return s; 4469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 4479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 4487fc93c36ae235115727296780dbc35101622bbd4Dan Egnor /** 4497fc93c36ae235115727296780dbc35101622bbd4Dan Egnor * Creates a new socket which is not connected to any remote host. 4507fc93c36ae235115727296780dbc35101622bbd4Dan Egnor * You must use {@link Socket#connect} to connect the socket. 4517fc93c36ae235115727296780dbc35101622bbd4Dan Egnor * 4527fc93c36ae235115727296780dbc35101622bbd4Dan Egnor * <p class="caution"><b>Warning:</b> Hostname verification is not performed 4537fc93c36ae235115727296780dbc35101622bbd4Dan Egnor * with this method. You MUST verify the server's identity after connecting 4547fc93c36ae235115727296780dbc35101622bbd4Dan Egnor * the socket to avoid man-in-the-middle attacks.</p> 4557fc93c36ae235115727296780dbc35101622bbd4Dan Egnor */ 45660586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor @Override 45760586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor public Socket createSocket() throws IOException { 45860586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor OpenSSLSocketImpl s = (OpenSSLSocketImpl) getDelegate().createSocket(); 459f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson s.setNpnProtocols(mNpnProtocols); 460100d7290264338c6536739abd59879aaaa812537Kenny Root s.setAlpnProtocols(mAlpnProtocols); 46160586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor s.setHandshakeTimeout(mHandshakeTimeoutMillis); 4624ef6c9b6a16c9b65699705aaa64977fc60dd3331Alex Klyubin s.setChannelIdPrivateKey(mChannelIdPrivateKey); 46360586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor return s; 4649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 4659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 4667fc93c36ae235115727296780dbc35101622bbd4Dan Egnor /** 4677fc93c36ae235115727296780dbc35101622bbd4Dan Egnor * {@inheritDoc} 4687fc93c36ae235115727296780dbc35101622bbd4Dan Egnor * 4697fc93c36ae235115727296780dbc35101622bbd4Dan Egnor * <p class="caution"><b>Warning:</b> Hostname verification is not performed 4707fc93c36ae235115727296780dbc35101622bbd4Dan Egnor * with this method. You MUST verify the server's identity after connecting 4717fc93c36ae235115727296780dbc35101622bbd4Dan Egnor * the socket to avoid man-in-the-middle attacks.</p> 4727fc93c36ae235115727296780dbc35101622bbd4Dan Egnor */ 47360586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor @Override 47460586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor public Socket createSocket(InetAddress addr, int port, InetAddress localAddr, int localPort) 47560586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor throws IOException { 47660586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor OpenSSLSocketImpl s = (OpenSSLSocketImpl) getDelegate().createSocket( 47760586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor addr, port, localAddr, localPort); 478f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson s.setNpnProtocols(mNpnProtocols); 479100d7290264338c6536739abd59879aaaa812537Kenny Root s.setAlpnProtocols(mAlpnProtocols); 48060586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor s.setHandshakeTimeout(mHandshakeTimeoutMillis); 4814ef6c9b6a16c9b65699705aaa64977fc60dd3331Alex Klyubin s.setChannelIdPrivateKey(mChannelIdPrivateKey); 48260586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor return s; 4839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 4849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 4857fc93c36ae235115727296780dbc35101622bbd4Dan Egnor /** 4867fc93c36ae235115727296780dbc35101622bbd4Dan Egnor * {@inheritDoc} 4877fc93c36ae235115727296780dbc35101622bbd4Dan Egnor * 4887fc93c36ae235115727296780dbc35101622bbd4Dan Egnor * <p class="caution"><b>Warning:</b> Hostname verification is not performed 4897fc93c36ae235115727296780dbc35101622bbd4Dan Egnor * with this method. You MUST verify the server's identity after connecting 4907fc93c36ae235115727296780dbc35101622bbd4Dan Egnor * the socket to avoid man-in-the-middle attacks.</p> 4917fc93c36ae235115727296780dbc35101622bbd4Dan Egnor */ 49260586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor @Override 49360586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor public Socket createSocket(InetAddress addr, int port) throws IOException { 49460586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor OpenSSLSocketImpl s = (OpenSSLSocketImpl) getDelegate().createSocket(addr, port); 495f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson s.setNpnProtocols(mNpnProtocols); 496100d7290264338c6536739abd59879aaaa812537Kenny Root s.setAlpnProtocols(mAlpnProtocols); 49760586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor s.setHandshakeTimeout(mHandshakeTimeoutMillis); 4984ef6c9b6a16c9b65699705aaa64977fc60dd3331Alex Klyubin s.setChannelIdPrivateKey(mChannelIdPrivateKey); 49960586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor return s; 5009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 5019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 5027fc93c36ae235115727296780dbc35101622bbd4Dan Egnor /** 5037fc93c36ae235115727296780dbc35101622bbd4Dan Egnor * {@inheritDoc} 5047fc93c36ae235115727296780dbc35101622bbd4Dan Egnor * 505df27c0c26209fe04de332497beafcafc1fbaad2bAndrew Stadler * <p>This method verifies the peer's certificate hostname after connecting 506df27c0c26209fe04de332497beafcafc1fbaad2bAndrew Stadler * (unless created with {@link #getInsecure(int, SSLSessionCache)}). 5077fc93c36ae235115727296780dbc35101622bbd4Dan Egnor */ 50860586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor @Override 50960586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor public Socket createSocket(String host, int port, InetAddress localAddr, int localPort) 51060586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor throws IOException { 51160586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor OpenSSLSocketImpl s = (OpenSSLSocketImpl) getDelegate().createSocket( 51260586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor host, port, localAddr, localPort); 513f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson s.setNpnProtocols(mNpnProtocols); 514100d7290264338c6536739abd59879aaaa812537Kenny Root s.setAlpnProtocols(mAlpnProtocols); 51560586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor s.setHandshakeTimeout(mHandshakeTimeoutMillis); 5164ef6c9b6a16c9b65699705aaa64977fc60dd3331Alex Klyubin s.setChannelIdPrivateKey(mChannelIdPrivateKey); 517df27c0c26209fe04de332497beafcafc1fbaad2bAndrew Stadler if (mSecure) { 518df27c0c26209fe04de332497beafcafc1fbaad2bAndrew Stadler verifyHostname(s, host); 519df27c0c26209fe04de332497beafcafc1fbaad2bAndrew Stadler } 52060586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor return s; 52160586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor } 5229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 5237fc93c36ae235115727296780dbc35101622bbd4Dan Egnor /** 5247fc93c36ae235115727296780dbc35101622bbd4Dan Egnor * {@inheritDoc} 5257fc93c36ae235115727296780dbc35101622bbd4Dan Egnor * 526df27c0c26209fe04de332497beafcafc1fbaad2bAndrew Stadler * <p>This method verifies the peer's certificate hostname after connecting 527df27c0c26209fe04de332497beafcafc1fbaad2bAndrew Stadler * (unless created with {@link #getInsecure(int, SSLSessionCache)}). 5287fc93c36ae235115727296780dbc35101622bbd4Dan Egnor */ 52960586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor @Override 53060586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor public Socket createSocket(String host, int port) throws IOException { 53160586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor OpenSSLSocketImpl s = (OpenSSLSocketImpl) getDelegate().createSocket(host, port); 532f5fb5e80963abeabdf0ff10dcee068344235082eJesse Wilson s.setNpnProtocols(mNpnProtocols); 533100d7290264338c6536739abd59879aaaa812537Kenny Root s.setAlpnProtocols(mAlpnProtocols); 53460586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor s.setHandshakeTimeout(mHandshakeTimeoutMillis); 5354ef6c9b6a16c9b65699705aaa64977fc60dd3331Alex Klyubin s.setChannelIdPrivateKey(mChannelIdPrivateKey); 536df27c0c26209fe04de332497beafcafc1fbaad2bAndrew Stadler if (mSecure) { 537df27c0c26209fe04de332497beafcafc1fbaad2bAndrew Stadler verifyHostname(s, host); 538df27c0c26209fe04de332497beafcafc1fbaad2bAndrew Stadler } 53960586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor return s; 5409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 5419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 54260586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor @Override 5439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public String[] getDefaultCipherSuites() { 544019118af67c60448030540deca37d972c8839d38Alex Klyubin return getDelegate().getDefaultCipherSuites(); 5459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 5469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 54760586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor @Override 5489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public String[] getSupportedCipherSuites() { 54960586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor return getDelegate().getSupportedCipherSuites(); 5509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 5519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} 552