blob: 3dd870ea8eaec00ff14138a3dddf215d31f0ff1b [file] [log] [blame]
Tobias Thierer5be18512016-06-24 19:23:19 +01001/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
Tobias Thiererfd275412017-04-11 21:01:50 +010018package libcore.net.http;
19
20import com.squareup.okhttp.CertificatePinner;
21import com.squareup.okhttp.ConnectionSpec;
22import com.squareup.okhttp.ConnectionSpecs;
23import com.squareup.okhttp.OkHttpClient;
24import com.squareup.okhttp.OkUrlFactories;
25import com.squareup.okhttp.OkUrlFactory;
26import com.squareup.okhttp.Protocol;
Tobias Thierer5be18512016-06-24 19:23:19 +010027
28import java.net.Proxy;
Tobias Thiererd9a7a712016-10-17 17:51:32 +010029import java.util.Collections;
Tobias Thierer5be18512016-06-24 19:23:19 +010030import java.util.List;
31
32import javax.net.ssl.HttpsURLConnection;
33
34public final class HttpsHandler extends HttpHandler {
35
36 /**
Tobias Thiererd9a7a712016-10-17 17:51:32 +010037 * The connection spec to use when connecting to an https:// server. Note that Android does
38 * not set the cipher suites or TLS versions to use so the socket's defaults will be used
39 * instead. When the SSLSocketFactory is provided by the app or GMS core we will not
40 * override the enabled ciphers or TLS versions set on the sockets it produces with a
41 * list hardcoded at release time. This is deliberate.
Tobias Thierer5be18512016-06-24 19:23:19 +010042 */
Tobias Thiererfd275412017-04-11 21:01:50 +010043 private static final ConnectionSpec TLS_CONNECTION_SPEC = ConnectionSpecs.builder(true)
Tobias Thiererd9a7a712016-10-17 17:51:32 +010044 .allEnabledCipherSuites()
45 .allEnabledTlsVersions()
Tobias Thierer5be18512016-06-24 19:23:19 +010046 .supportsTlsExtensions(true)
47 .build();
48
Tobias Thiererd9a7a712016-10-17 17:51:32 +010049 private static final List<Protocol> HTTP_1_1_ONLY =
50 Collections.singletonList(Protocol.HTTP_1_1);
Tobias Thierer5be18512016-06-24 19:23:19 +010051
52 private final ConfigAwareConnectionPool configAwareConnectionPool =
53 ConfigAwareConnectionPool.getInstance();
54
55 @Override protected int getDefaultPort() {
56 return 443;
57 }
58
59 @Override
60 protected OkUrlFactory newOkUrlFactory(Proxy proxy) {
61 OkUrlFactory okUrlFactory = createHttpsOkUrlFactory(proxy);
62 // For HttpsURLConnections created through java.net.URL Android uses a connection pool that
63 // is aware when the default network changes so that pooled connections are not re-used when
64 // the default network changes.
65 okUrlFactory.client().setConnectionPool(configAwareConnectionPool.get());
66 return okUrlFactory;
67 }
68
69 /**
70 * Creates an OkHttpClient suitable for creating {@link HttpsURLConnection} instances on
71 * Android.
72 */
73 // Visible for android.net.Network.
74 public static OkUrlFactory createHttpsOkUrlFactory(Proxy proxy) {
75 // The HTTPS OkHttpClient is an HTTP OkHttpClient with extra configuration.
76 OkUrlFactory okUrlFactory = HttpHandler.createHttpOkUrlFactory(proxy);
77
78 // All HTTPS requests are allowed.
Tobias Thiererfd275412017-04-11 21:01:50 +010079 OkUrlFactories.setUrlFilter(okUrlFactory, null);
Tobias Thierer5be18512016-06-24 19:23:19 +010080
81 OkHttpClient okHttpClient = okUrlFactory.client();
82
83 // Only enable HTTP/1.1 (implies HTTP/1.0). Disable SPDY / HTTP/2.0.
84 okHttpClient.setProtocols(HTTP_1_1_ONLY);
85
Tobias Thiererd9a7a712016-10-17 17:51:32 +010086 okHttpClient.setConnectionSpecs(Collections.singletonList(TLS_CONNECTION_SPEC));
Tobias Thierer5be18512016-06-24 19:23:19 +010087
88 // Android support certificate pinning via NetworkSecurityConfig so there is no need to
89 // also expose OkHttp's mechanism. The OkHttpClient underlying https HttpsURLConnections
90 // in Android should therefore always use the default certificate pinner, whose set of
91 // {@code hostNamesToPin} is empty.
92 okHttpClient.setCertificatePinner(CertificatePinner.DEFAULT);
93
94 // OkHttp does not automatically honor the system-wide HostnameVerifier set with
95 // HttpsURLConnection.setDefaultHostnameVerifier().
96 okUrlFactory.client().setHostnameVerifier(HttpsURLConnection.getDefaultHostnameVerifier());
97 // OkHttp does not automatically honor the system-wide SSLSocketFactory set with
98 // HttpsURLConnection.setDefaultSSLSocketFactory().
99 // See https://github.com/square/okhttp/issues/184 for details.
100 okHttpClient.setSslSocketFactory(HttpsURLConnection.getDefaultSSLSocketFactory());
101
102 return okUrlFactory;
103 }
104}