The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.net; |
| 18 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 19 | import android.os.SystemProperties; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | import android.util.Log; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | import java.io.IOException; |
| 22 | import java.net.InetAddress; |
| 23 | import java.net.Socket; |
Brian Carlstrom | 7ab7a8b | 2012-09-21 16:33:50 -0700 | [diff] [blame] | 24 | import java.net.SocketException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | import java.security.KeyManagementException; |
Alex Klyubin | ac5eb03 | 2013-03-12 10:30:59 -0700 | [diff] [blame] | 26 | import java.security.PrivateKey; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | import java.security.cert.X509Certificate; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | import javax.net.SocketFactory; |
Dan Egnor | 7fc93c3 | 2010-06-29 17:51:28 -0700 | [diff] [blame] | 29 | import javax.net.ssl.HostnameVerifier; |
| 30 | import javax.net.ssl.HttpsURLConnection; |
Ben Komalo | 1b52806 | 2011-05-03 09:40:15 -0700 | [diff] [blame] | 31 | import javax.net.ssl.KeyManager; |
Dan Egnor | 7fc93c3 | 2010-06-29 17:51:28 -0700 | [diff] [blame] | 32 | import javax.net.ssl.SSLException; |
| 33 | import javax.net.ssl.SSLPeerUnverifiedException; |
| 34 | import javax.net.ssl.SSLSession; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | import javax.net.ssl.SSLSocket; |
| 36 | import javax.net.ssl.SSLSocketFactory; |
| 37 | import javax.net.ssl.TrustManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | import javax.net.ssl.X509TrustManager; |
Brian Carlstrom | 3c7c351 | 2010-08-04 15:44:39 -0700 | [diff] [blame] | 39 | import org.apache.harmony.xnet.provider.jsse.OpenSSLContextImpl; |
Dan Egnor | 60586f2 | 2010-02-08 21:56:38 -0800 | [diff] [blame] | 40 | import org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | import org.apache.harmony.xnet.provider.jsse.SSLClientSessionCache; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 42 | |
| 43 | /** |
Dan Egnor | 60586f2 | 2010-02-08 21:56:38 -0800 | [diff] [blame] | 44 | * SSLSocketFactory implementation with several extra features: |
Dan Egnor | 7fc93c3 | 2010-06-29 17:51:28 -0700 | [diff] [blame] | 45 | * |
Dan Egnor | 60586f2 | 2010-02-08 21:56:38 -0800 | [diff] [blame] | 46 | * <ul> |
| 47 | * <li>Timeout specification for SSL handshake operations |
Dan Egnor | 7fc93c3 | 2010-06-29 17:51:28 -0700 | [diff] [blame] | 48 | * <li>Hostname verification in most cases (see WARNINGs below) |
Dan Egnor | 60586f2 | 2010-02-08 21:56:38 -0800 | [diff] [blame] | 49 | * <li>Optional SSL session caching with {@link SSLSessionCache} |
Dan Egnor | 9d4b575 | 2010-02-13 20:34:51 -0800 | [diff] [blame] | 50 | * <li>Optionally bypass all SSL certificate checks |
Dan Egnor | 60586f2 | 2010-02-08 21:56:38 -0800 | [diff] [blame] | 51 | * </ul> |
Dan Egnor | 7fc93c3 | 2010-06-29 17:51:28 -0700 | [diff] [blame] | 52 | * |
| 53 | * The handshake timeout does not apply to actual TCP socket connection. |
| 54 | * If you want a connection timeout as well, use {@link #createSocket()} |
| 55 | * and {@link Socket#connect(SocketAddress, int)}, after which you |
| 56 | * must verify the identity of the server you are connected to. |
| 57 | * |
| 58 | * <p class="caution"><b>Most {@link SSLSocketFactory} implementations do not |
| 59 | * verify the server's identity, allowing man-in-the-middle attacks.</b> |
| 60 | * This implementation does check the server's certificate hostname, but only |
| 61 | * for createSocket variants that specify a hostname. When using methods that |
| 62 | * use {@link InetAddress} or which return an unconnected socket, you MUST |
| 63 | * verify the server's identity yourself to ensure a secure connection.</p> |
| 64 | * |
| 65 | * <p>One way to verify the server's identity is to use |
| 66 | * {@link HttpsURLConnection#getDefaultHostnameVerifier()} to get a |
| 67 | * {@link HostnameVerifier} to verify the certificate hostname. |
| 68 | * |
| 69 | * <p>On development devices, "setprop socket.relaxsslcheck yes" bypasses all |
| 70 | * SSL certificate and hostname checks for testing purposes. This setting |
| 71 | * requires root access. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | */ |
| 73 | public class SSLCertificateSocketFactory extends SSLSocketFactory { |
Dan Egnor | 60586f2 | 2010-02-08 21:56:38 -0800 | [diff] [blame] | 74 | private static final String TAG = "SSLCertificateSocketFactory"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 75 | |
Dan Egnor | 60586f2 | 2010-02-08 21:56:38 -0800 | [diff] [blame] | 76 | private static final TrustManager[] INSECURE_TRUST_MANAGER = new TrustManager[] { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | new X509TrustManager() { |
Dan Egnor | 60586f2 | 2010-02-08 21:56:38 -0800 | [diff] [blame] | 78 | public X509Certificate[] getAcceptedIssuers() { return null; } |
| 79 | public void checkClientTrusted(X509Certificate[] certs, String authType) { } |
| 80 | public void checkServerTrusted(X509Certificate[] certs, String authType) { } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 81 | } |
| 82 | }; |
| 83 | |
Dan Egnor | 7fc93c3 | 2010-06-29 17:51:28 -0700 | [diff] [blame] | 84 | private static final HostnameVerifier HOSTNAME_VERIFIER = |
| 85 | HttpsURLConnection.getDefaultHostnameVerifier(); |
| 86 | |
Dan Egnor | 60586f2 | 2010-02-08 21:56:38 -0800 | [diff] [blame] | 87 | private SSLSocketFactory mInsecureFactory = null; |
| 88 | private SSLSocketFactory mSecureFactory = null; |
Ben Komalo | 1b52806 | 2011-05-03 09:40:15 -0700 | [diff] [blame] | 89 | private TrustManager[] mTrustManagers = null; |
| 90 | private KeyManager[] mKeyManagers = null; |
Jesse Wilson | f5fb5e8 | 2012-03-23 15:55:03 -0400 | [diff] [blame] | 91 | private byte[] mNpnProtocols = null; |
Alex Klyubin | ac5eb03 | 2013-03-12 10:30:59 -0700 | [diff] [blame] | 92 | private PrivateKey mChannelIdPrivateKey = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 93 | |
Dan Egnor | 60586f2 | 2010-02-08 21:56:38 -0800 | [diff] [blame] | 94 | private final int mHandshakeTimeoutMillis; |
| 95 | private final SSLClientSessionCache mSessionCache; |
Dan Egnor | 9d4b575 | 2010-02-13 20:34:51 -0800 | [diff] [blame] | 96 | private final boolean mSecure; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 97 | |
Dan Egnor | 60586f2 | 2010-02-08 21:56:38 -0800 | [diff] [blame] | 98 | /** @deprecated Use {@link #getDefault(int)} instead. */ |
Jesse Wilson | 28c7425 | 2010-11-04 14:09:18 -0700 | [diff] [blame] | 99 | @Deprecated |
Dan Egnor | 60586f2 | 2010-02-08 21:56:38 -0800 | [diff] [blame] | 100 | public SSLCertificateSocketFactory(int handshakeTimeoutMillis) { |
Dan Egnor | 9d4b575 | 2010-02-13 20:34:51 -0800 | [diff] [blame] | 101 | this(handshakeTimeoutMillis, null, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 102 | } |
| 103 | |
Dan Egnor | 9d4b575 | 2010-02-13 20:34:51 -0800 | [diff] [blame] | 104 | private SSLCertificateSocketFactory( |
Brian Carlstrom | 992f238 | 2012-09-26 14:33:47 -0700 | [diff] [blame] | 105 | int handshakeTimeoutMillis, SSLSessionCache cache, boolean secure) { |
Dan Egnor | 60586f2 | 2010-02-08 21:56:38 -0800 | [diff] [blame] | 106 | mHandshakeTimeoutMillis = handshakeTimeoutMillis; |
| 107 | mSessionCache = cache == null ? null : cache.mSessionCache; |
Dan Egnor | 9d4b575 | 2010-02-13 20:34:51 -0800 | [diff] [blame] | 108 | mSecure = secure; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | /** |
Dan Egnor | 9d4b575 | 2010-02-13 20:34:51 -0800 | [diff] [blame] | 112 | * Returns a new socket factory instance with an optional handshake timeout. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 113 | * |
Dan Egnor | 60586f2 | 2010-02-08 21:56:38 -0800 | [diff] [blame] | 114 | * @param handshakeTimeoutMillis to use for SSL connection handshake, or 0 |
| 115 | * for none. The socket timeout is reset to 0 after the handshake. |
Dan Egnor | 7fc93c3 | 2010-06-29 17:51:28 -0700 | [diff] [blame] | 116 | * @return a new SSLSocketFactory with the specified parameters |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 117 | */ |
Dan Egnor | 60586f2 | 2010-02-08 21:56:38 -0800 | [diff] [blame] | 118 | public static SocketFactory getDefault(int handshakeTimeoutMillis) { |
Dan Egnor | 9d4b575 | 2010-02-13 20:34:51 -0800 | [diff] [blame] | 119 | return new SSLCertificateSocketFactory(handshakeTimeoutMillis, null, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | /** |
Dan Egnor | 9d4b575 | 2010-02-13 20:34:51 -0800 | [diff] [blame] | 123 | * Returns a new socket factory instance with an optional handshake timeout |
| 124 | * and SSL session cache. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 125 | * |
Dan Egnor | 60586f2 | 2010-02-08 21:56:38 -0800 | [diff] [blame] | 126 | * @param handshakeTimeoutMillis to use for SSL connection handshake, or 0 |
| 127 | * for none. The socket timeout is reset to 0 after the handshake. |
Jesse Wilson | ff55699 | 2011-03-23 16:43:39 -0700 | [diff] [blame] | 128 | * @param cache The {@link SSLSessionCache} to use, or null for no cache. |
Dan Egnor | 7fc93c3 | 2010-06-29 17:51:28 -0700 | [diff] [blame] | 129 | * @return a new SSLSocketFactory with the specified parameters |
Dan Egnor | 60586f2 | 2010-02-08 21:56:38 -0800 | [diff] [blame] | 130 | */ |
Dan Egnor | 9d4b575 | 2010-02-13 20:34:51 -0800 | [diff] [blame] | 131 | public static SSLSocketFactory getDefault(int handshakeTimeoutMillis, SSLSessionCache cache) { |
| 132 | return new SSLCertificateSocketFactory(handshakeTimeoutMillis, cache, true); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Returns a new instance of a socket factory with all SSL security checks |
| 137 | * disabled, using an optional handshake timeout and SSL session cache. |
Dan Egnor | 7fc93c3 | 2010-06-29 17:51:28 -0700 | [diff] [blame] | 138 | * |
| 139 | * <p class="caution"><b>Warning:</b> Sockets created using this factory |
| 140 | * are vulnerable to man-in-the-middle attacks!</p> |
Dan Egnor | 9d4b575 | 2010-02-13 20:34:51 -0800 | [diff] [blame] | 141 | * |
| 142 | * @param handshakeTimeoutMillis to use for SSL connection handshake, or 0 |
| 143 | * for none. The socket timeout is reset to 0 after the handshake. |
Jesse Wilson | ff55699 | 2011-03-23 16:43:39 -0700 | [diff] [blame] | 144 | * @param cache The {@link SSLSessionCache} to use, or null for no cache. |
Dan Egnor | 7fc93c3 | 2010-06-29 17:51:28 -0700 | [diff] [blame] | 145 | * @return an insecure SSLSocketFactory with the specified parameters |
Dan Egnor | 9d4b575 | 2010-02-13 20:34:51 -0800 | [diff] [blame] | 146 | */ |
| 147 | public static SSLSocketFactory getInsecure(int handshakeTimeoutMillis, SSLSessionCache cache) { |
| 148 | return new SSLCertificateSocketFactory(handshakeTimeoutMillis, cache, false); |
Dan Egnor | 60586f2 | 2010-02-08 21:56:38 -0800 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Returns a socket factory (also named SSLSocketFactory, but in a different |
| 153 | * namespace) for use with the Apache HTTP stack. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 154 | * |
Dan Egnor | 60586f2 | 2010-02-08 21:56:38 -0800 | [diff] [blame] | 155 | * @param handshakeTimeoutMillis to use for SSL connection handshake, or 0 |
| 156 | * for none. The socket timeout is reset to 0 after the handshake. |
Jesse Wilson | ff55699 | 2011-03-23 16:43:39 -0700 | [diff] [blame] | 157 | * @param cache The {@link SSLSessionCache} to use, or null for no cache. |
Dan Egnor | 60586f2 | 2010-02-08 21:56:38 -0800 | [diff] [blame] | 158 | * @return a new SocketFactory with the specified parameters |
| 159 | */ |
| 160 | public static org.apache.http.conn.ssl.SSLSocketFactory getHttpSocketFactory( |
Jesse Wilson | ff55699 | 2011-03-23 16:43:39 -0700 | [diff] [blame] | 161 | int handshakeTimeoutMillis, SSLSessionCache cache) { |
Dan Egnor | 60586f2 | 2010-02-08 21:56:38 -0800 | [diff] [blame] | 162 | return new org.apache.http.conn.ssl.SSLSocketFactory( |
Dan Egnor | 9d4b575 | 2010-02-13 20:34:51 -0800 | [diff] [blame] | 163 | new SSLCertificateSocketFactory(handshakeTimeoutMillis, cache, true)); |
Dan Egnor | 60586f2 | 2010-02-08 21:56:38 -0800 | [diff] [blame] | 164 | } |
| 165 | |
Dan Egnor | 7fc93c3 | 2010-06-29 17:51:28 -0700 | [diff] [blame] | 166 | /** |
| 167 | * Verify the hostname of the certificate used by the other end of a |
| 168 | * connected socket. You MUST call this if you did not supply a hostname |
| 169 | * to {@link #createSocket()}. It is harmless to call this method |
| 170 | * redundantly if the hostname has already been verified. |
| 171 | * |
| 172 | * <p>Wildcard certificates are allowed to verify any matching hostname, |
| 173 | * so "foo.bar.example.com" is verified if the peer has a certificate |
| 174 | * for "*.example.com". |
| 175 | * |
| 176 | * @param socket An SSL socket which has been connected to a server |
| 177 | * @param hostname The expected hostname of the remote server |
| 178 | * @throws IOException if something goes wrong handshaking with the server |
| 179 | * @throws SSLPeerUnverifiedException if the server cannot prove its identity |
| 180 | * |
| 181 | * @hide |
| 182 | */ |
| 183 | public static void verifyHostname(Socket socket, String hostname) throws IOException { |
| 184 | if (!(socket instanceof SSLSocket)) { |
| 185 | throw new IllegalArgumentException("Attempt to verify non-SSL socket"); |
| 186 | } |
| 187 | |
| 188 | if (!isSslCheckRelaxed()) { |
| 189 | // The code at the start of OpenSSLSocketImpl.startHandshake() |
| 190 | // ensures that the call is idempotent, so we can safely call it. |
| 191 | SSLSocket ssl = (SSLSocket) socket; |
| 192 | ssl.startHandshake(); |
| 193 | |
| 194 | SSLSession session = ssl.getSession(); |
| 195 | if (session == null) { |
| 196 | throw new SSLException("Cannot verify SSL socket without session"); |
| 197 | } |
| 198 | if (!HOSTNAME_VERIFIER.verify(hostname, session)) { |
| 199 | throw new SSLPeerUnverifiedException("Cannot verify hostname: " + hostname); |
| 200 | } |
| 201 | } |
| 202 | } |
| 203 | |
Ben Komalo | 1b52806 | 2011-05-03 09:40:15 -0700 | [diff] [blame] | 204 | private SSLSocketFactory makeSocketFactory( |
| 205 | KeyManager[] keyManagers, TrustManager[] trustManagers) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 206 | try { |
Brian Carlstrom | 3c7c351 | 2010-08-04 15:44:39 -0700 | [diff] [blame] | 207 | OpenSSLContextImpl sslContext = new OpenSSLContextImpl(); |
Ben Komalo | 1b52806 | 2011-05-03 09:40:15 -0700 | [diff] [blame] | 208 | sslContext.engineInit(keyManagers, trustManagers, null); |
Brian Carlstrom | 2c42c8f | 2010-09-14 00:11:14 -0700 | [diff] [blame] | 209 | sslContext.engineGetClientSessionContext().setPersistentCache(mSessionCache); |
Dan Egnor | 60586f2 | 2010-02-08 21:56:38 -0800 | [diff] [blame] | 210 | return sslContext.engineGetSocketFactory(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 211 | } catch (KeyManagementException e) { |
Dan Egnor | 60586f2 | 2010-02-08 21:56:38 -0800 | [diff] [blame] | 212 | Log.wtf(TAG, e); |
| 213 | return (SSLSocketFactory) SSLSocketFactory.getDefault(); // Fallback |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 214 | } |
| 215 | } |
| 216 | |
Dan Egnor | 7fc93c3 | 2010-06-29 17:51:28 -0700 | [diff] [blame] | 217 | private static boolean isSslCheckRelaxed() { |
| 218 | return "1".equals(SystemProperties.get("ro.debuggable")) && |
| 219 | "yes".equals(SystemProperties.get("socket.relaxsslcheck")); |
| 220 | } |
| 221 | |
Dan Egnor | 60586f2 | 2010-02-08 21:56:38 -0800 | [diff] [blame] | 222 | private synchronized SSLSocketFactory getDelegate() { |
Dan Egnor | 9d4b575 | 2010-02-13 20:34:51 -0800 | [diff] [blame] | 223 | // Relax the SSL check if instructed (for this factory, or systemwide) |
Dan Egnor | 7fc93c3 | 2010-06-29 17:51:28 -0700 | [diff] [blame] | 224 | if (!mSecure || isSslCheckRelaxed()) { |
Dan Egnor | 60586f2 | 2010-02-08 21:56:38 -0800 | [diff] [blame] | 225 | if (mInsecureFactory == null) { |
Dan Egnor | 9d4b575 | 2010-02-13 20:34:51 -0800 | [diff] [blame] | 226 | if (mSecure) { |
| 227 | Log.w(TAG, "*** BYPASSING SSL SECURITY CHECKS (socket.relaxsslcheck=yes) ***"); |
| 228 | } else { |
| 229 | Log.w(TAG, "Bypassing SSL security checks at caller's request"); |
| 230 | } |
Ben Komalo | 1b52806 | 2011-05-03 09:40:15 -0700 | [diff] [blame] | 231 | mInsecureFactory = makeSocketFactory(mKeyManagers, INSECURE_TRUST_MANAGER); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 232 | } |
Dan Egnor | 60586f2 | 2010-02-08 21:56:38 -0800 | [diff] [blame] | 233 | return mInsecureFactory; |
| 234 | } else { |
| 235 | if (mSecureFactory == null) { |
Ben Komalo | 1b52806 | 2011-05-03 09:40:15 -0700 | [diff] [blame] | 236 | mSecureFactory = makeSocketFactory(mKeyManagers, mTrustManagers); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 237 | } |
Dan Egnor | 60586f2 | 2010-02-08 21:56:38 -0800 | [diff] [blame] | 238 | return mSecureFactory; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 239 | } |
| 240 | } |
| 241 | |
Dan Egnor | 7fc93c3 | 2010-06-29 17:51:28 -0700 | [diff] [blame] | 242 | /** |
Ben Komalo | 1b52806 | 2011-05-03 09:40:15 -0700 | [diff] [blame] | 243 | * Sets the {@link TrustManager}s to be used for connections made by this factory. |
Ben Komalo | 1b52806 | 2011-05-03 09:40:15 -0700 | [diff] [blame] | 244 | */ |
| 245 | public void setTrustManagers(TrustManager[] trustManager) { |
| 246 | mTrustManagers = trustManager; |
| 247 | |
| 248 | // Clear out all cached secure factories since configurations have changed. |
| 249 | mSecureFactory = null; |
| 250 | // Note - insecure factories only ever use the INSECURE_TRUST_MANAGER so they need not |
| 251 | // be cleared out here. |
| 252 | } |
| 253 | |
| 254 | /** |
Jesse Wilson | f5fb5e8 | 2012-03-23 15:55:03 -0400 | [diff] [blame] | 255 | * Sets the <a href="http://technotes.googlecode.com/git/nextprotoneg.html">Next |
| 256 | * Protocol Negotiation (NPN)</a> protocols that this peer is interested in. |
| 257 | * |
| 258 | * <p>For servers this is the sequence of protocols to advertise as |
| 259 | * supported, in order of preference. This list is sent unencrypted to |
| 260 | * all clients that support NPN. |
| 261 | * |
| 262 | * <p>For clients this is a list of supported protocols to match against the |
| 263 | * server's list. If there is no protocol supported by both client and |
| 264 | * server then the first protocol in the client's list will be selected. |
| 265 | * The order of the client's protocols is otherwise insignificant. |
| 266 | * |
Jesse Wilson | 2108ead | 2012-05-17 13:46:17 -0400 | [diff] [blame] | 267 | * @param npnProtocols a non-empty list of protocol byte arrays. All arrays |
| 268 | * must be non-empty and of length less than 256. |
Jesse Wilson | f5fb5e8 | 2012-03-23 15:55:03 -0400 | [diff] [blame] | 269 | */ |
| 270 | public void setNpnProtocols(byte[][] npnProtocols) { |
| 271 | this.mNpnProtocols = toNpnProtocolsList(npnProtocols); |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * Returns an array containing the concatenation of length-prefixed byte |
| 276 | * strings. |
| 277 | */ |
| 278 | static byte[] toNpnProtocolsList(byte[]... npnProtocols) { |
Jesse Wilson | 2108ead | 2012-05-17 13:46:17 -0400 | [diff] [blame] | 279 | if (npnProtocols.length == 0) { |
| 280 | throw new IllegalArgumentException("npnProtocols.length == 0"); |
| 281 | } |
Jesse Wilson | f5fb5e8 | 2012-03-23 15:55:03 -0400 | [diff] [blame] | 282 | int totalLength = 0; |
| 283 | for (byte[] s : npnProtocols) { |
| 284 | if (s.length == 0 || s.length > 255) { |
| 285 | throw new IllegalArgumentException("s.length == 0 || s.length > 255: " + s.length); |
| 286 | } |
| 287 | totalLength += 1 + s.length; |
| 288 | } |
| 289 | byte[] result = new byte[totalLength]; |
| 290 | int pos = 0; |
| 291 | for (byte[] s : npnProtocols) { |
| 292 | result[pos++] = (byte) s.length; |
| 293 | for (byte b : s) { |
| 294 | result[pos++] = b; |
| 295 | } |
| 296 | } |
| 297 | return result; |
| 298 | } |
| 299 | |
| 300 | /** |
| 301 | * Returns the <a href="http://technotes.googlecode.com/git/nextprotoneg.html">Next |
| 302 | * Protocol Negotiation (NPN)</a> protocol selected by client and server, or |
| 303 | * null if no protocol was negotiated. |
| 304 | * |
| 305 | * @param socket a socket created by this factory. |
Narayan Kamath | b4db962 | 2012-09-17 17:59:00 +0100 | [diff] [blame] | 306 | * @throws IllegalArgumentException if the socket was not created by this factory. |
Jesse Wilson | f5fb5e8 | 2012-03-23 15:55:03 -0400 | [diff] [blame] | 307 | */ |
| 308 | public byte[] getNpnSelectedProtocol(Socket socket) { |
Narayan Kamath | b4db962 | 2012-09-17 17:59:00 +0100 | [diff] [blame] | 309 | return castToOpenSSLSocket(socket).getNpnSelectedProtocol(); |
Jesse Wilson | f5fb5e8 | 2012-03-23 15:55:03 -0400 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | /** |
Ben Komalo | 1b52806 | 2011-05-03 09:40:15 -0700 | [diff] [blame] | 313 | * Sets the {@link KeyManager}s to be used for connections made by this factory. |
Ben Komalo | 1b52806 | 2011-05-03 09:40:15 -0700 | [diff] [blame] | 314 | */ |
| 315 | public void setKeyManagers(KeyManager[] keyManagers) { |
| 316 | mKeyManagers = keyManagers; |
| 317 | |
| 318 | // Clear out any existing cached factories since configurations have changed. |
| 319 | mSecureFactory = null; |
| 320 | mInsecureFactory = null; |
| 321 | } |
| 322 | |
Narayan Kamath | b4db962 | 2012-09-17 17:59:00 +0100 | [diff] [blame] | 323 | /** |
Alex Klyubin | ac5eb03 | 2013-03-12 10:30:59 -0700 | [diff] [blame] | 324 | * Sets the private key to be used for TLS Channel ID by connections made by this |
Alex Klyubin | 4ef6c9b | 2013-01-18 12:50:39 -0800 | [diff] [blame] | 325 | * factory. |
| 326 | * |
| 327 | * @param privateKey private key (enables TLS Channel ID) or {@code null} for no key (disables |
| 328 | * TLS Channel ID). The private key has to be an Elliptic Curve (EC) key based on the |
| 329 | * NIST P-256 curve (aka SECG secp256r1 or ANSI X9.62 prime256v1). |
| 330 | * |
| 331 | * @hide |
| 332 | */ |
Alex Klyubin | ac5eb03 | 2013-03-12 10:30:59 -0700 | [diff] [blame] | 333 | public void setChannelIdPrivateKey(PrivateKey privateKey) { |
Alex Klyubin | 4ef6c9b | 2013-01-18 12:50:39 -0800 | [diff] [blame] | 334 | mChannelIdPrivateKey = privateKey; |
| 335 | } |
| 336 | |
| 337 | /** |
Narayan Kamath | b4db962 | 2012-09-17 17:59:00 +0100 | [diff] [blame] | 338 | * Enables <a href="http://tools.ietf.org/html/rfc5077#section-3.2">session ticket</a> |
| 339 | * support on the given socket. |
| 340 | * |
| 341 | * @param socket a socket created by this factory |
| 342 | * @param useSessionTickets {@code true} to enable session ticket support on this socket. |
| 343 | * @throws IllegalArgumentException if the socket was not created by this factory. |
| 344 | */ |
| 345 | public void setUseSessionTickets(Socket socket, boolean useSessionTickets) { |
| 346 | castToOpenSSLSocket(socket).setUseSessionTickets(useSessionTickets); |
| 347 | } |
| 348 | |
| 349 | /** |
| 350 | * Turns on <a href="http://tools.ietf.org/html/rfc6066#section-3">Server |
| 351 | * Name Indication (SNI)</a> on a given socket. |
| 352 | * |
| 353 | * @param socket a socket created by this factory. |
| 354 | * @param hostName the desired SNI hostname, null to disable. |
| 355 | * @throws IllegalArgumentException if the socket was not created by this factory. |
| 356 | */ |
| 357 | public void setHostname(Socket socket, String hostName) { |
| 358 | castToOpenSSLSocket(socket).setHostname(hostName); |
| 359 | } |
| 360 | |
Brian Carlstrom | 7ab7a8b | 2012-09-21 16:33:50 -0700 | [diff] [blame] | 361 | /** |
| 362 | * Sets this socket's SO_SNDTIMEO write timeout in milliseconds. |
| 363 | * Use 0 for no timeout. |
| 364 | * To take effect, this option must be set before the blocking method was called. |
| 365 | * |
| 366 | * @param socket a socket created by this factory. |
Brian Carlstrom | 992f238 | 2012-09-26 14:33:47 -0700 | [diff] [blame] | 367 | * @param timeout the desired write timeout in milliseconds. |
Brian Carlstrom | 7ab7a8b | 2012-09-21 16:33:50 -0700 | [diff] [blame] | 368 | * @throws IllegalArgumentException if the socket was not created by this factory. |
Brian Carlstrom | 992f238 | 2012-09-26 14:33:47 -0700 | [diff] [blame] | 369 | * |
| 370 | * @hide |
Brian Carlstrom | 7ab7a8b | 2012-09-21 16:33:50 -0700 | [diff] [blame] | 371 | */ |
| 372 | public void setSoWriteTimeout(Socket socket, int writeTimeoutMilliseconds) |
| 373 | throws SocketException { |
| 374 | castToOpenSSLSocket(socket).setSoWriteTimeout(writeTimeoutMilliseconds); |
| 375 | } |
| 376 | |
Narayan Kamath | b4db962 | 2012-09-17 17:59:00 +0100 | [diff] [blame] | 377 | private static OpenSSLSocketImpl castToOpenSSLSocket(Socket socket) { |
| 378 | if (!(socket instanceof OpenSSLSocketImpl)) { |
| 379 | throw new IllegalArgumentException("Socket not created by this factory: " |
| 380 | + socket); |
| 381 | } |
| 382 | |
| 383 | return (OpenSSLSocketImpl) socket; |
| 384 | } |
Ben Komalo | 1b52806 | 2011-05-03 09:40:15 -0700 | [diff] [blame] | 385 | |
| 386 | /** |
Dan Egnor | 7fc93c3 | 2010-06-29 17:51:28 -0700 | [diff] [blame] | 387 | * {@inheritDoc} |
| 388 | * |
Andrew Stadler | df27c0c | 2010-07-12 15:31:40 -0700 | [diff] [blame] | 389 | * <p>This method verifies the peer's certificate hostname after connecting |
| 390 | * (unless created with {@link #getInsecure(int, SSLSessionCache)}). |
Dan Egnor | 7fc93c3 | 2010-06-29 17:51:28 -0700 | [diff] [blame] | 391 | */ |
Dan Egnor | 60586f2 | 2010-02-08 21:56:38 -0800 | [diff] [blame] | 392 | @Override |
| 393 | public Socket createSocket(Socket k, String host, int port, boolean close) throws IOException { |
| 394 | OpenSSLSocketImpl s = (OpenSSLSocketImpl) getDelegate().createSocket(k, host, port, close); |
Jesse Wilson | f5fb5e8 | 2012-03-23 15:55:03 -0400 | [diff] [blame] | 395 | s.setNpnProtocols(mNpnProtocols); |
Dan Egnor | 60586f2 | 2010-02-08 21:56:38 -0800 | [diff] [blame] | 396 | s.setHandshakeTimeout(mHandshakeTimeoutMillis); |
Alex Klyubin | 4ef6c9b | 2013-01-18 12:50:39 -0800 | [diff] [blame] | 397 | s.setChannelIdPrivateKey(mChannelIdPrivateKey); |
Andrew Stadler | df27c0c | 2010-07-12 15:31:40 -0700 | [diff] [blame] | 398 | if (mSecure) { |
| 399 | verifyHostname(s, host); |
| 400 | } |
Dan Egnor | 60586f2 | 2010-02-08 21:56:38 -0800 | [diff] [blame] | 401 | return s; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 402 | } |
| 403 | |
Dan Egnor | 7fc93c3 | 2010-06-29 17:51:28 -0700 | [diff] [blame] | 404 | /** |
| 405 | * Creates a new socket which is not connected to any remote host. |
| 406 | * You must use {@link Socket#connect} to connect the socket. |
| 407 | * |
| 408 | * <p class="caution"><b>Warning:</b> Hostname verification is not performed |
| 409 | * with this method. You MUST verify the server's identity after connecting |
| 410 | * the socket to avoid man-in-the-middle attacks.</p> |
| 411 | */ |
Dan Egnor | 60586f2 | 2010-02-08 21:56:38 -0800 | [diff] [blame] | 412 | @Override |
| 413 | public Socket createSocket() throws IOException { |
| 414 | OpenSSLSocketImpl s = (OpenSSLSocketImpl) getDelegate().createSocket(); |
Jesse Wilson | f5fb5e8 | 2012-03-23 15:55:03 -0400 | [diff] [blame] | 415 | s.setNpnProtocols(mNpnProtocols); |
Dan Egnor | 60586f2 | 2010-02-08 21:56:38 -0800 | [diff] [blame] | 416 | s.setHandshakeTimeout(mHandshakeTimeoutMillis); |
Alex Klyubin | 4ef6c9b | 2013-01-18 12:50:39 -0800 | [diff] [blame] | 417 | s.setChannelIdPrivateKey(mChannelIdPrivateKey); |
Dan Egnor | 60586f2 | 2010-02-08 21:56:38 -0800 | [diff] [blame] | 418 | return s; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 419 | } |
| 420 | |
Dan Egnor | 7fc93c3 | 2010-06-29 17:51:28 -0700 | [diff] [blame] | 421 | /** |
| 422 | * {@inheritDoc} |
| 423 | * |
| 424 | * <p class="caution"><b>Warning:</b> Hostname verification is not performed |
| 425 | * with this method. You MUST verify the server's identity after connecting |
| 426 | * the socket to avoid man-in-the-middle attacks.</p> |
| 427 | */ |
Dan Egnor | 60586f2 | 2010-02-08 21:56:38 -0800 | [diff] [blame] | 428 | @Override |
| 429 | public Socket createSocket(InetAddress addr, int port, InetAddress localAddr, int localPort) |
| 430 | throws IOException { |
| 431 | OpenSSLSocketImpl s = (OpenSSLSocketImpl) getDelegate().createSocket( |
| 432 | addr, port, localAddr, localPort); |
Jesse Wilson | f5fb5e8 | 2012-03-23 15:55:03 -0400 | [diff] [blame] | 433 | s.setNpnProtocols(mNpnProtocols); |
Dan Egnor | 60586f2 | 2010-02-08 21:56:38 -0800 | [diff] [blame] | 434 | s.setHandshakeTimeout(mHandshakeTimeoutMillis); |
Alex Klyubin | 4ef6c9b | 2013-01-18 12:50:39 -0800 | [diff] [blame] | 435 | s.setChannelIdPrivateKey(mChannelIdPrivateKey); |
Dan Egnor | 60586f2 | 2010-02-08 21:56:38 -0800 | [diff] [blame] | 436 | return s; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 437 | } |
| 438 | |
Dan Egnor | 7fc93c3 | 2010-06-29 17:51:28 -0700 | [diff] [blame] | 439 | /** |
| 440 | * {@inheritDoc} |
| 441 | * |
| 442 | * <p class="caution"><b>Warning:</b> Hostname verification is not performed |
| 443 | * with this method. You MUST verify the server's identity after connecting |
| 444 | * the socket to avoid man-in-the-middle attacks.</p> |
| 445 | */ |
Dan Egnor | 60586f2 | 2010-02-08 21:56:38 -0800 | [diff] [blame] | 446 | @Override |
| 447 | public Socket createSocket(InetAddress addr, int port) throws IOException { |
| 448 | OpenSSLSocketImpl s = (OpenSSLSocketImpl) getDelegate().createSocket(addr, port); |
Jesse Wilson | f5fb5e8 | 2012-03-23 15:55:03 -0400 | [diff] [blame] | 449 | s.setNpnProtocols(mNpnProtocols); |
Dan Egnor | 60586f2 | 2010-02-08 21:56:38 -0800 | [diff] [blame] | 450 | s.setHandshakeTimeout(mHandshakeTimeoutMillis); |
Alex Klyubin | 4ef6c9b | 2013-01-18 12:50:39 -0800 | [diff] [blame] | 451 | s.setChannelIdPrivateKey(mChannelIdPrivateKey); |
Dan Egnor | 60586f2 | 2010-02-08 21:56:38 -0800 | [diff] [blame] | 452 | return s; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 453 | } |
| 454 | |
Dan Egnor | 7fc93c3 | 2010-06-29 17:51:28 -0700 | [diff] [blame] | 455 | /** |
| 456 | * {@inheritDoc} |
| 457 | * |
Andrew Stadler | df27c0c | 2010-07-12 15:31:40 -0700 | [diff] [blame] | 458 | * <p>This method verifies the peer's certificate hostname after connecting |
| 459 | * (unless created with {@link #getInsecure(int, SSLSessionCache)}). |
Dan Egnor | 7fc93c3 | 2010-06-29 17:51:28 -0700 | [diff] [blame] | 460 | */ |
Dan Egnor | 60586f2 | 2010-02-08 21:56:38 -0800 | [diff] [blame] | 461 | @Override |
| 462 | public Socket createSocket(String host, int port, InetAddress localAddr, int localPort) |
| 463 | throws IOException { |
| 464 | OpenSSLSocketImpl s = (OpenSSLSocketImpl) getDelegate().createSocket( |
| 465 | host, port, localAddr, localPort); |
Jesse Wilson | f5fb5e8 | 2012-03-23 15:55:03 -0400 | [diff] [blame] | 466 | s.setNpnProtocols(mNpnProtocols); |
Dan Egnor | 60586f2 | 2010-02-08 21:56:38 -0800 | [diff] [blame] | 467 | s.setHandshakeTimeout(mHandshakeTimeoutMillis); |
Alex Klyubin | 4ef6c9b | 2013-01-18 12:50:39 -0800 | [diff] [blame] | 468 | s.setChannelIdPrivateKey(mChannelIdPrivateKey); |
Andrew Stadler | df27c0c | 2010-07-12 15:31:40 -0700 | [diff] [blame] | 469 | if (mSecure) { |
| 470 | verifyHostname(s, host); |
| 471 | } |
Dan Egnor | 60586f2 | 2010-02-08 21:56:38 -0800 | [diff] [blame] | 472 | return s; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 473 | } |
| 474 | |
Dan Egnor | 7fc93c3 | 2010-06-29 17:51:28 -0700 | [diff] [blame] | 475 | /** |
| 476 | * {@inheritDoc} |
| 477 | * |
Andrew Stadler | df27c0c | 2010-07-12 15:31:40 -0700 | [diff] [blame] | 478 | * <p>This method verifies the peer's certificate hostname after connecting |
| 479 | * (unless created with {@link #getInsecure(int, SSLSessionCache)}). |
Dan Egnor | 7fc93c3 | 2010-06-29 17:51:28 -0700 | [diff] [blame] | 480 | */ |
Dan Egnor | 60586f2 | 2010-02-08 21:56:38 -0800 | [diff] [blame] | 481 | @Override |
| 482 | public Socket createSocket(String host, int port) throws IOException { |
| 483 | OpenSSLSocketImpl s = (OpenSSLSocketImpl) getDelegate().createSocket(host, port); |
Jesse Wilson | f5fb5e8 | 2012-03-23 15:55:03 -0400 | [diff] [blame] | 484 | s.setNpnProtocols(mNpnProtocols); |
Dan Egnor | 60586f2 | 2010-02-08 21:56:38 -0800 | [diff] [blame] | 485 | s.setHandshakeTimeout(mHandshakeTimeoutMillis); |
Alex Klyubin | 4ef6c9b | 2013-01-18 12:50:39 -0800 | [diff] [blame] | 486 | s.setChannelIdPrivateKey(mChannelIdPrivateKey); |
Andrew Stadler | df27c0c | 2010-07-12 15:31:40 -0700 | [diff] [blame] | 487 | if (mSecure) { |
| 488 | verifyHostname(s, host); |
| 489 | } |
Dan Egnor | 60586f2 | 2010-02-08 21:56:38 -0800 | [diff] [blame] | 490 | return s; |
| 491 | } |
| 492 | |
| 493 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 494 | public String[] getDefaultCipherSuites() { |
Dan Egnor | 60586f2 | 2010-02-08 21:56:38 -0800 | [diff] [blame] | 495 | return getDelegate().getSupportedCipherSuites(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 496 | } |
| 497 | |
Dan Egnor | 60586f2 | 2010-02-08 21:56:38 -0800 | [diff] [blame] | 498 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 499 | public String[] getSupportedCipherSuites() { |
Dan Egnor | 60586f2 | 2010-02-08 21:56:38 -0800 | [diff] [blame] | 500 | return getDelegate().getSupportedCipherSuites(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 501 | } |
| 502 | } |