[email protected] | e34400c3 | 2012-01-24 02:49:33 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "net/socket/ssl_client_socket_pool.h" |
| 6 | |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 7 | #include "base/bind.h" |
| 8 | #include "base/bind_helpers.h" |
[email protected] | c63248d4 | 2011-02-18 17:54:39 | [diff] [blame] | 9 | #include "base/metrics/field_trial.h" |
[email protected] | 835d7c8 | 2010-10-14 04:38:38 | [diff] [blame] | 10 | #include "base/metrics/histogram.h" |
[email protected] | 92a3124 | 2013-06-28 18:08:08 | [diff] [blame] | 11 | #include "base/metrics/sparse_histogram.h" |
[email protected] | 8e45855 | 2014-08-05 00:02:15 | [diff] [blame] | 12 | #include "base/stl_util.h" |
[email protected] | ba00b49 | 2010-09-08 14:53:38 | [diff] [blame] | 13 | #include "base/values.h" |
[email protected] | 4f4de7e6 | 2010-11-12 19:55:27 | [diff] [blame] | 14 | #include "net/base/host_port_pair.h" |
[email protected] | 536fd0b | 2013-03-14 17:41:57 | [diff] [blame] | 15 | #include "net/base/net_errors.h" |
[email protected] | 33b511c | 2010-08-11 00:04:43 | [diff] [blame] | 16 | #include "net/http/http_proxy_client_socket.h" |
| 17 | #include "net/http/http_proxy_client_socket_pool.h" |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 18 | #include "net/socket/client_socket_factory.h" |
| 19 | #include "net/socket/client_socket_handle.h" |
[email protected] | 33b511c | 2010-08-11 00:04:43 | [diff] [blame] | 20 | #include "net/socket/socks_client_socket_pool.h" |
| 21 | #include "net/socket/ssl_client_socket.h" |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 22 | #include "net/socket/transport_client_socket_pool.h" |
[email protected] | 536fd0b | 2013-03-14 17:41:57 | [diff] [blame] | 23 | #include "net/ssl/ssl_cert_request_info.h" |
[email protected] | 92a3124 | 2013-06-28 18:08:08 | [diff] [blame] | 24 | #include "net/ssl/ssl_connection_status_flags.h" |
| 25 | #include "net/ssl/ssl_info.h" |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 26 | |
| 27 | namespace net { |
| 28 | |
| 29 | SSLSocketParams::SSLSocketParams( |
[email protected] | ea79ba9 | 2013-08-15 21:56:20 | [diff] [blame] | 30 | const scoped_refptr<TransportSocketParams>& direct_params, |
| 31 | const scoped_refptr<SOCKSSocketParams>& socks_proxy_params, |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 32 | const scoped_refptr<HttpProxySocketParams>& http_proxy_params, |
[email protected] | 4f4de7e6 | 2010-11-12 19:55:27 | [diff] [blame] | 33 | const HostPortPair& host_and_port, |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 34 | const SSLConfig& ssl_config, |
[email protected] | 5e5021a | 2013-07-17 05:23:36 | [diff] [blame] | 35 | PrivacyMode privacy_mode, |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 36 | int load_flags, |
[email protected] | 9e9e842e | 2010-07-23 23:09:15 | [diff] [blame] | 37 | bool force_spdy_over_ssl, |
| 38 | bool want_spdy_over_npn) |
[email protected] | ea79ba9 | 2013-08-15 21:56:20 | [diff] [blame] | 39 | : direct_params_(direct_params), |
| 40 | socks_proxy_params_(socks_proxy_params), |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 41 | http_proxy_params_(http_proxy_params), |
[email protected] | 4f4de7e6 | 2010-11-12 19:55:27 | [diff] [blame] | 42 | host_and_port_(host_and_port), |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 43 | ssl_config_(ssl_config), |
[email protected] | 5e5021a | 2013-07-17 05:23:36 | [diff] [blame] | 44 | privacy_mode_(privacy_mode), |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 45 | load_flags_(load_flags), |
[email protected] | 9e9e842e | 2010-07-23 23:09:15 | [diff] [blame] | 46 | force_spdy_over_ssl_(force_spdy_over_ssl), |
[email protected] | 0e14e87d | 2011-06-21 21:24:19 | [diff] [blame] | 47 | want_spdy_over_npn_(want_spdy_over_npn), |
| 48 | ignore_limits_(false) { |
dcheng | 707f860 | 2014-09-04 00:13:52 | [diff] [blame] | 49 | if (direct_params_.get()) { |
| 50 | DCHECK(!socks_proxy_params_.get()); |
| 51 | DCHECK(!http_proxy_params_.get()); |
[email protected] | ea79ba9 | 2013-08-15 21:56:20 | [diff] [blame] | 52 | ignore_limits_ = direct_params_->ignore_limits(); |
dcheng | 707f860 | 2014-09-04 00:13:52 | [diff] [blame] | 53 | } else if (socks_proxy_params_.get()) { |
| 54 | DCHECK(!http_proxy_params_.get()); |
[email protected] | ea79ba9 | 2013-08-15 21:56:20 | [diff] [blame] | 55 | ignore_limits_ = socks_proxy_params_->ignore_limits(); |
| 56 | } else { |
dcheng | 707f860 | 2014-09-04 00:13:52 | [diff] [blame] | 57 | DCHECK(http_proxy_params_.get()); |
[email protected] | ea79ba9 | 2013-08-15 21:56:20 | [diff] [blame] | 58 | ignore_limits_ = http_proxy_params_->ignore_limits(); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 59 | } |
| 60 | } |
| 61 | |
| 62 | SSLSocketParams::~SSLSocketParams() {} |
| 63 | |
[email protected] | ea79ba9 | 2013-08-15 21:56:20 | [diff] [blame] | 64 | SSLSocketParams::ConnectionType SSLSocketParams::GetConnectionType() const { |
dcheng | 707f860 | 2014-09-04 00:13:52 | [diff] [blame] | 65 | if (direct_params_.get()) { |
| 66 | DCHECK(!socks_proxy_params_.get()); |
| 67 | DCHECK(!http_proxy_params_.get()); |
[email protected] | ea79ba9 | 2013-08-15 21:56:20 | [diff] [blame] | 68 | return DIRECT; |
| 69 | } |
| 70 | |
dcheng | 707f860 | 2014-09-04 00:13:52 | [diff] [blame] | 71 | if (socks_proxy_params_.get()) { |
| 72 | DCHECK(!http_proxy_params_.get()); |
[email protected] | ea79ba9 | 2013-08-15 21:56:20 | [diff] [blame] | 73 | return SOCKS_PROXY; |
| 74 | } |
| 75 | |
dcheng | 707f860 | 2014-09-04 00:13:52 | [diff] [blame] | 76 | DCHECK(http_proxy_params_.get()); |
[email protected] | ea79ba9 | 2013-08-15 21:56:20 | [diff] [blame] | 77 | return HTTP_PROXY; |
| 78 | } |
| 79 | |
| 80 | const scoped_refptr<TransportSocketParams>& |
| 81 | SSLSocketParams::GetDirectConnectionParams() const { |
| 82 | DCHECK_EQ(GetConnectionType(), DIRECT); |
| 83 | return direct_params_; |
| 84 | } |
| 85 | |
| 86 | const scoped_refptr<SOCKSSocketParams>& |
| 87 | SSLSocketParams::GetSocksProxyConnectionParams() const { |
| 88 | DCHECK_EQ(GetConnectionType(), SOCKS_PROXY); |
| 89 | return socks_proxy_params_; |
| 90 | } |
| 91 | |
| 92 | const scoped_refptr<HttpProxySocketParams>& |
| 93 | SSLSocketParams::GetHttpProxyConnectionParams() const { |
| 94 | DCHECK_EQ(GetConnectionType(), HTTP_PROXY); |
| 95 | return http_proxy_params_; |
| 96 | } |
| 97 | |
[email protected] | 8e45855 | 2014-08-05 00:02:15 | [diff] [blame] | 98 | SSLConnectJobMessenger::SocketAndCallback::SocketAndCallback( |
| 99 | SSLClientSocket* ssl_socket, |
| 100 | const base::Closure& job_resumption_callback) |
| 101 | : socket(ssl_socket), callback(job_resumption_callback) { |
| 102 | } |
| 103 | |
| 104 | SSLConnectJobMessenger::SocketAndCallback::~SocketAndCallback() { |
| 105 | } |
| 106 | |
[email protected] | cffd7f9 | 2014-08-21 21:30:50 | [diff] [blame] | 107 | SSLConnectJobMessenger::SSLConnectJobMessenger( |
| 108 | const base::Closure& messenger_finished_callback) |
| 109 | : messenger_finished_callback_(messenger_finished_callback), |
| 110 | weak_factory_(this) { |
[email protected] | 8e45855 | 2014-08-05 00:02:15 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | SSLConnectJobMessenger::~SSLConnectJobMessenger() { |
| 114 | } |
| 115 | |
| 116 | void SSLConnectJobMessenger::RemovePendingSocket(SSLClientSocket* ssl_socket) { |
| 117 | // Sockets do not need to be removed from connecting_sockets_ because |
| 118 | // OnSSLHandshakeCompleted will do this. |
| 119 | for (SSLPendingSocketsAndCallbacks::iterator it = |
| 120 | pending_sockets_and_callbacks_.begin(); |
| 121 | it != pending_sockets_and_callbacks_.end(); |
| 122 | ++it) { |
| 123 | if (it->socket == ssl_socket) { |
| 124 | pending_sockets_and_callbacks_.erase(it); |
| 125 | return; |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | bool SSLConnectJobMessenger::CanProceed(SSLClientSocket* ssl_socket) { |
[email protected] | cffd7f9 | 2014-08-21 21:30:50 | [diff] [blame] | 131 | // If there are no connecting sockets, allow the connection to proceed. |
| 132 | return connecting_sockets_.empty(); |
[email protected] | 8e45855 | 2014-08-05 00:02:15 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | void SSLConnectJobMessenger::MonitorConnectionResult( |
| 136 | SSLClientSocket* ssl_socket) { |
| 137 | connecting_sockets_.push_back(ssl_socket); |
| 138 | ssl_socket->SetHandshakeCompletionCallback( |
| 139 | base::Bind(&SSLConnectJobMessenger::OnSSLHandshakeCompleted, |
| 140 | weak_factory_.GetWeakPtr())); |
| 141 | } |
| 142 | |
| 143 | void SSLConnectJobMessenger::AddPendingSocket(SSLClientSocket* ssl_socket, |
| 144 | const base::Closure& callback) { |
| 145 | DCHECK(!connecting_sockets_.empty()); |
| 146 | pending_sockets_and_callbacks_.push_back( |
| 147 | SocketAndCallback(ssl_socket, callback)); |
| 148 | } |
| 149 | |
| 150 | void SSLConnectJobMessenger::OnSSLHandshakeCompleted() { |
| 151 | connecting_sockets_.clear(); |
| 152 | SSLPendingSocketsAndCallbacks temp_list; |
| 153 | temp_list.swap(pending_sockets_and_callbacks_); |
[email protected] | cffd7f9 | 2014-08-21 21:30:50 | [diff] [blame] | 154 | base::Closure messenger_finished_callback = messenger_finished_callback_; |
| 155 | messenger_finished_callback.Run(); |
[email protected] | 8e45855 | 2014-08-05 00:02:15 | [diff] [blame] | 156 | RunAllCallbacks(temp_list); |
| 157 | } |
| 158 | |
| 159 | void SSLConnectJobMessenger::RunAllCallbacks( |
| 160 | const SSLPendingSocketsAndCallbacks& pending_sockets_and_callbacks) { |
| 161 | for (std::vector<SocketAndCallback>::const_iterator it = |
| 162 | pending_sockets_and_callbacks.begin(); |
| 163 | it != pending_sockets_and_callbacks.end(); |
| 164 | ++it) { |
| 165 | it->callback.Run(); |
| 166 | } |
| 167 | } |
| 168 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 169 | // Timeout for the SSL handshake portion of the connect. |
| 170 | static const int kSSLHandshakeTimeoutInSeconds = 30; |
| 171 | |
[email protected] | feb79bcd | 2011-07-21 16:55:17 | [diff] [blame] | 172 | SSLConnectJob::SSLConnectJob(const std::string& group_name, |
[email protected] | 3f6007ab | 2013-08-22 19:45:39 | [diff] [blame] | 173 | RequestPriority priority, |
[email protected] | feb79bcd | 2011-07-21 16:55:17 | [diff] [blame] | 174 | const scoped_refptr<SSLSocketParams>& params, |
| 175 | const base::TimeDelta& timeout_duration, |
| 176 | TransportClientSocketPool* transport_pool, |
| 177 | SOCKSClientSocketPool* socks_pool, |
| 178 | HttpProxyClientSocketPool* http_proxy_pool, |
| 179 | ClientSocketFactory* client_socket_factory, |
[email protected] | feb79bcd | 2011-07-21 16:55:17 | [diff] [blame] | 180 | const SSLClientSocketContext& context, |
[email protected] | cffd7f9 | 2014-08-21 21:30:50 | [diff] [blame] | 181 | const GetMessengerCallback& get_messenger_callback, |
[email protected] | feb79bcd | 2011-07-21 16:55:17 | [diff] [blame] | 182 | Delegate* delegate, |
| 183 | NetLog* net_log) |
[email protected] | 5e5021a | 2013-07-17 05:23:36 | [diff] [blame] | 184 | : ConnectJob(group_name, |
| 185 | timeout_duration, |
[email protected] | 3f6007ab | 2013-08-22 19:45:39 | [diff] [blame] | 186 | priority, |
[email protected] | 5e5021a | 2013-07-17 05:23:36 | [diff] [blame] | 187 | delegate, |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 188 | BoundNetLog::Make(net_log, NetLog::SOURCE_CONNECT_JOB)), |
| 189 | params_(params), |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 190 | transport_pool_(transport_pool), |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 191 | socks_pool_(socks_pool), |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 192 | http_proxy_pool_(http_proxy_pool), |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 193 | client_socket_factory_(client_socket_factory), |
[email protected] | 5e5021a | 2013-07-17 05:23:36 | [diff] [blame] | 194 | context_(context.cert_verifier, |
[email protected] | 6b8a3c74 | 2014-07-25 00:25:35 | [diff] [blame] | 195 | context.channel_id_service, |
[email protected] | 5e5021a | 2013-07-17 05:23:36 | [diff] [blame] | 196 | context.transport_security_state, |
[email protected] | 284303b6 | 2013-11-28 15:11:54 | [diff] [blame] | 197 | context.cert_transparency_verifier, |
eranm | 6571b2b | 2014-12-03 15:53:23 | [diff] [blame] | 198 | context.cert_policy_enforcer, |
[email protected] | 314b0399 | 2014-04-01 01:28:53 | [diff] [blame] | 199 | (params->privacy_mode() == PRIVACY_MODE_ENABLED |
[email protected] | 5e5021a | 2013-07-17 05:23:36 | [diff] [blame] | 200 | ? "pm/" + context.ssl_session_cache_shard |
| 201 | : context.ssl_session_cache_shard)), |
[email protected] | 8e45855 | 2014-08-05 00:02:15 | [diff] [blame] | 202 | io_callback_( |
| 203 | base::Bind(&SSLConnectJob::OnIOComplete, base::Unretained(this))), |
[email protected] | cffd7f9 | 2014-08-21 21:30:50 | [diff] [blame] | 204 | messenger_(NULL), |
| 205 | get_messenger_callback_(get_messenger_callback), |
[email protected] | 8e45855 | 2014-08-05 00:02:15 | [diff] [blame] | 206 | weak_factory_(this) { |
| 207 | } |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 208 | |
[email protected] | 8e45855 | 2014-08-05 00:02:15 | [diff] [blame] | 209 | SSLConnectJob::~SSLConnectJob() { |
| 210 | if (ssl_socket_.get() && messenger_) |
| 211 | messenger_->RemovePendingSocket(ssl_socket_.get()); |
| 212 | } |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 213 | |
| 214 | LoadState SSLConnectJob::GetLoadState() const { |
| 215 | switch (next_state_) { |
[email protected] | 135e226 | 2010-07-17 00:32:04 | [diff] [blame] | 216 | case STATE_TUNNEL_CONNECT_COMPLETE: |
| 217 | if (transport_socket_handle_->socket()) |
| 218 | return LOAD_STATE_ESTABLISHING_PROXY_TUNNEL; |
| 219 | // else, fall through. |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 220 | case STATE_TRANSPORT_CONNECT: |
| 221 | case STATE_TRANSPORT_CONNECT_COMPLETE: |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 222 | case STATE_SOCKS_CONNECT: |
| 223 | case STATE_SOCKS_CONNECT_COMPLETE: |
| 224 | case STATE_TUNNEL_CONNECT: |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 225 | return transport_socket_handle_->GetLoadState(); |
[email protected] | 8e45855 | 2014-08-05 00:02:15 | [diff] [blame] | 226 | case STATE_CREATE_SSL_SOCKET: |
| 227 | case STATE_CHECK_FOR_RESUME: |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 228 | case STATE_SSL_CONNECT: |
| 229 | case STATE_SSL_CONNECT_COMPLETE: |
| 230 | return LOAD_STATE_SSL_HANDSHAKE; |
| 231 | default: |
| 232 | NOTREACHED(); |
| 233 | return LOAD_STATE_IDLE; |
| 234 | } |
| 235 | } |
| 236 | |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 237 | void SSLConnectJob::GetAdditionalErrorState(ClientSocketHandle* handle) { |
[email protected] | ad74a59 | 2011-01-21 18:40:55 | [diff] [blame] | 238 | // Headers in |error_response_info_| indicate a proxy tunnel setup |
| 239 | // problem. See DoTunnelConnectComplete. |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 240 | if (error_response_info_.headers.get()) { |
[email protected] | ad74a59 | 2011-01-21 18:40:55 | [diff] [blame] | 241 | handle->set_pending_http_proxy_connection( |
| 242 | transport_socket_handle_.release()); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 243 | } |
[email protected] | ad74a59 | 2011-01-21 18:40:55 | [diff] [blame] | 244 | handle->set_ssl_error_response_info(error_response_info_); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 245 | if (!connect_timing_.ssl_start.is_null()) |
[email protected] | ad74a59 | 2011-01-21 18:40:55 | [diff] [blame] | 246 | handle->set_is_ssl_error(true); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | void SSLConnectJob::OnIOComplete(int result) { |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 250 | int rv = DoLoop(result); |
| 251 | if (rv != ERR_IO_PENDING) |
| 252 | NotifyDelegateOfCompletion(rv); // Deletes |this|. |
| 253 | } |
| 254 | |
| 255 | int SSLConnectJob::DoLoop(int result) { |
| 256 | DCHECK_NE(next_state_, STATE_NONE); |
| 257 | |
| 258 | int rv = result; |
| 259 | do { |
| 260 | State state = next_state_; |
| 261 | next_state_ = STATE_NONE; |
| 262 | switch (state) { |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 263 | case STATE_TRANSPORT_CONNECT: |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 264 | DCHECK_EQ(OK, rv); |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 265 | rv = DoTransportConnect(); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 266 | break; |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 267 | case STATE_TRANSPORT_CONNECT_COMPLETE: |
| 268 | rv = DoTransportConnectComplete(rv); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 269 | break; |
| 270 | case STATE_SOCKS_CONNECT: |
| 271 | DCHECK_EQ(OK, rv); |
| 272 | rv = DoSOCKSConnect(); |
| 273 | break; |
| 274 | case STATE_SOCKS_CONNECT_COMPLETE: |
| 275 | rv = DoSOCKSConnectComplete(rv); |
| 276 | break; |
| 277 | case STATE_TUNNEL_CONNECT: |
| 278 | DCHECK_EQ(OK, rv); |
| 279 | rv = DoTunnelConnect(); |
| 280 | break; |
| 281 | case STATE_TUNNEL_CONNECT_COMPLETE: |
| 282 | rv = DoTunnelConnectComplete(rv); |
| 283 | break; |
[email protected] | 8e45855 | 2014-08-05 00:02:15 | [diff] [blame] | 284 | case STATE_CREATE_SSL_SOCKET: |
| 285 | rv = DoCreateSSLSocket(); |
| 286 | break; |
| 287 | case STATE_CHECK_FOR_RESUME: |
| 288 | rv = DoCheckForResume(); |
| 289 | break; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 290 | case STATE_SSL_CONNECT: |
| 291 | DCHECK_EQ(OK, rv); |
| 292 | rv = DoSSLConnect(); |
| 293 | break; |
| 294 | case STATE_SSL_CONNECT_COMPLETE: |
| 295 | rv = DoSSLConnectComplete(rv); |
| 296 | break; |
| 297 | default: |
| 298 | NOTREACHED() << "bad state"; |
| 299 | rv = ERR_FAILED; |
| 300 | break; |
| 301 | } |
| 302 | } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE); |
| 303 | |
| 304 | return rv; |
| 305 | } |
| 306 | |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 307 | int SSLConnectJob::DoTransportConnect() { |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 308 | DCHECK(transport_pool_); |
[email protected] | 899c3e9 | 2010-08-28 15:53:50 | [diff] [blame] | 309 | |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 310 | next_state_ = STATE_TRANSPORT_CONNECT_COMPLETE; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 311 | transport_socket_handle_.reset(new ClientSocketHandle()); |
[email protected] | ea79ba9 | 2013-08-15 21:56:20 | [diff] [blame] | 312 | scoped_refptr<TransportSocketParams> direct_params = |
| 313 | params_->GetDirectConnectionParams(); |
[email protected] | 5109c195 | 2013-08-20 18:44:10 | [diff] [blame] | 314 | return transport_socket_handle_->Init(group_name(), |
| 315 | direct_params, |
[email protected] | 3f6007ab | 2013-08-22 19:45:39 | [diff] [blame] | 316 | priority(), |
[email protected] | 8e45855 | 2014-08-05 00:02:15 | [diff] [blame] | 317 | io_callback_, |
[email protected] | 5109c195 | 2013-08-20 18:44:10 | [diff] [blame] | 318 | transport_pool_, |
| 319 | net_log()); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 320 | } |
| 321 | |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 322 | int SSLConnectJob::DoTransportConnectComplete(int result) { |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 323 | if (result == OK) |
[email protected] | 8e45855 | 2014-08-05 00:02:15 | [diff] [blame] | 324 | next_state_ = STATE_CREATE_SSL_SOCKET; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 325 | |
| 326 | return result; |
| 327 | } |
| 328 | |
| 329 | int SSLConnectJob::DoSOCKSConnect() { |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 330 | DCHECK(socks_pool_); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 331 | next_state_ = STATE_SOCKS_CONNECT_COMPLETE; |
| 332 | transport_socket_handle_.reset(new ClientSocketHandle()); |
[email protected] | ea79ba9 | 2013-08-15 21:56:20 | [diff] [blame] | 333 | scoped_refptr<SOCKSSocketParams> socks_proxy_params = |
| 334 | params_->GetSocksProxyConnectionParams(); |
[email protected] | 5109c195 | 2013-08-20 18:44:10 | [diff] [blame] | 335 | return transport_socket_handle_->Init(group_name(), |
| 336 | socks_proxy_params, |
[email protected] | 3f6007ab | 2013-08-22 19:45:39 | [diff] [blame] | 337 | priority(), |
[email protected] | 8e45855 | 2014-08-05 00:02:15 | [diff] [blame] | 338 | io_callback_, |
[email protected] | 5109c195 | 2013-08-20 18:44:10 | [diff] [blame] | 339 | socks_pool_, |
| 340 | net_log()); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | int SSLConnectJob::DoSOCKSConnectComplete(int result) { |
| 344 | if (result == OK) |
[email protected] | 8e45855 | 2014-08-05 00:02:15 | [diff] [blame] | 345 | next_state_ = STATE_CREATE_SSL_SOCKET; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 346 | |
| 347 | return result; |
| 348 | } |
| 349 | |
| 350 | int SSLConnectJob::DoTunnelConnect() { |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 351 | DCHECK(http_proxy_pool_); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 352 | next_state_ = STATE_TUNNEL_CONNECT_COMPLETE; |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 353 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 354 | transport_socket_handle_.reset(new ClientSocketHandle()); |
| 355 | scoped_refptr<HttpProxySocketParams> http_proxy_params = |
[email protected] | ea79ba9 | 2013-08-15 21:56:20 | [diff] [blame] | 356 | params_->GetHttpProxyConnectionParams(); |
[email protected] | 5109c195 | 2013-08-20 18:44:10 | [diff] [blame] | 357 | return transport_socket_handle_->Init(group_name(), |
| 358 | http_proxy_params, |
[email protected] | 3f6007ab | 2013-08-22 19:45:39 | [diff] [blame] | 359 | priority(), |
[email protected] | 8e45855 | 2014-08-05 00:02:15 | [diff] [blame] | 360 | io_callback_, |
[email protected] | 5109c195 | 2013-08-20 18:44:10 | [diff] [blame] | 361 | http_proxy_pool_, |
| 362 | net_log()); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | int SSLConnectJob::DoTunnelConnectComplete(int result) { |
[email protected] | 4f4de7e6 | 2010-11-12 19:55:27 | [diff] [blame] | 366 | // Extract the information needed to prompt for appropriate proxy |
| 367 | // authentication so that when ClientSocketPoolBaseHelper calls |
| 368 | // |GetAdditionalErrorState|, we can easily set the state. |
| 369 | if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) { |
| 370 | error_response_info_ = transport_socket_handle_->ssl_error_response_info(); |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 371 | } else if (result == ERR_PROXY_AUTH_REQUESTED || |
| 372 | result == ERR_HTTPS_PROXY_TUNNEL_RESPONSE) { |
[email protected] | 3268023f | 2011-05-05 00:08:10 | [diff] [blame] | 373 | StreamSocket* socket = transport_socket_handle_->socket(); |
pcc | 684f44b | 2015-01-30 18:59:23 | [diff] [blame] | 374 | ProxyClientSocket* tunnel_socket = static_cast<ProxyClientSocket*>(socket); |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 375 | error_response_info_ = *tunnel_socket->GetConnectResponseInfo(); |
[email protected] | 4f4de7e6 | 2010-11-12 19:55:27 | [diff] [blame] | 376 | } |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 377 | if (result < 0) |
| 378 | return result; |
[email protected] | 8e45855 | 2014-08-05 00:02:15 | [diff] [blame] | 379 | next_state_ = STATE_CREATE_SSL_SOCKET; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 380 | return result; |
| 381 | } |
| 382 | |
[email protected] | 8e45855 | 2014-08-05 00:02:15 | [diff] [blame] | 383 | int SSLConnectJob::DoCreateSSLSocket() { |
pkasting | 93ba27f6 | 2015-02-28 02:53:46 | [diff] [blame^] | 384 | // TODO(pkasting): Remove ScopedTracker below once crbug.com/462815 is fixed. |
pkasting | 58e029b | 2015-02-21 05:17:28 | [diff] [blame] | 385 | tracked_objects::ScopedTracker tracking_profile( |
| 386 | FROM_HERE_WITH_EXPLICIT_FUNCTION( |
pkasting | 93ba27f6 | 2015-02-28 02:53:46 | [diff] [blame^] | 387 | "462815 SSLConnectJob::DoCreateSSLSocket")); |
[email protected] | 8e45855 | 2014-08-05 00:02:15 | [diff] [blame] | 388 | next_state_ = STATE_CHECK_FOR_RESUME; |
| 389 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 390 | // Reset the timeout to just the time allowed for the SSL handshake. |
| 391 | ResetTimer(base::TimeDelta::FromSeconds(kSSLHandshakeTimeoutInSeconds)); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 392 | |
| 393 | // If the handle has a fresh socket, get its connect start and DNS times. |
| 394 | // This should always be the case. |
| 395 | const LoadTimingInfo::ConnectTiming& socket_connect_timing = |
| 396 | transport_socket_handle_->connect_timing(); |
| 397 | if (!transport_socket_handle_->is_reused() && |
| 398 | !socket_connect_timing.connect_start.is_null()) { |
| 399 | // Overwriting |connect_start| serves two purposes - it adjusts timing so |
| 400 | // |connect_start| doesn't include dns times, and it adjusts the time so |
| 401 | // as not to include time spent waiting for an idle socket. |
| 402 | connect_timing_.connect_start = socket_connect_timing.connect_start; |
| 403 | connect_timing_.dns_start = socket_connect_timing.dns_start; |
| 404 | connect_timing_.dns_end = socket_connect_timing.dns_end; |
| 405 | } |
| 406 | |
[email protected] | 18ccfdb | 2013-08-15 00:13:44 | [diff] [blame] | 407 | ssl_socket_ = client_socket_factory_->CreateSSLClientSocket( |
| 408 | transport_socket_handle_.Pass(), |
[email protected] | 5e5021a | 2013-07-17 05:23:36 | [diff] [blame] | 409 | params_->host_and_port(), |
| 410 | params_->ssl_config(), |
[email protected] | 18ccfdb | 2013-08-15 00:13:44 | [diff] [blame] | 411 | context_); |
[email protected] | cffd7f9 | 2014-08-21 21:30:50 | [diff] [blame] | 412 | |
| 413 | if (!ssl_socket_->InSessionCache()) |
| 414 | messenger_ = get_messenger_callback_.Run(ssl_socket_->GetSessionCacheKey()); |
| 415 | |
[email protected] | 8e45855 | 2014-08-05 00:02:15 | [diff] [blame] | 416 | return OK; |
| 417 | } |
| 418 | |
| 419 | int SSLConnectJob::DoCheckForResume() { |
[email protected] | 8e45855 | 2014-08-05 00:02:15 | [diff] [blame] | 420 | next_state_ = STATE_SSL_CONNECT; |
[email protected] | cffd7f9 | 2014-08-21 21:30:50 | [diff] [blame] | 421 | |
[email protected] | 8e45855 | 2014-08-05 00:02:15 | [diff] [blame] | 422 | if (!messenger_) |
| 423 | return OK; |
| 424 | |
[email protected] | 8e45855 | 2014-08-05 00:02:15 | [diff] [blame] | 425 | if (messenger_->CanProceed(ssl_socket_.get())) { |
[email protected] | cffd7f9 | 2014-08-21 21:30:50 | [diff] [blame] | 426 | messenger_->MonitorConnectionResult(ssl_socket_.get()); |
| 427 | // The SSLConnectJob no longer needs access to the messenger after this |
| 428 | // point. |
| 429 | messenger_ = NULL; |
[email protected] | 8e45855 | 2014-08-05 00:02:15 | [diff] [blame] | 430 | return OK; |
| 431 | } |
[email protected] | cffd7f9 | 2014-08-21 21:30:50 | [diff] [blame] | 432 | |
[email protected] | 8e45855 | 2014-08-05 00:02:15 | [diff] [blame] | 433 | messenger_->AddPendingSocket(ssl_socket_.get(), |
| 434 | base::Bind(&SSLConnectJob::ResumeSSLConnection, |
| 435 | weak_factory_.GetWeakPtr())); |
[email protected] | cffd7f9 | 2014-08-21 21:30:50 | [diff] [blame] | 436 | |
[email protected] | 8e45855 | 2014-08-05 00:02:15 | [diff] [blame] | 437 | return ERR_IO_PENDING; |
| 438 | } |
| 439 | |
| 440 | int SSLConnectJob::DoSSLConnect() { |
pkasting | 93ba27f6 | 2015-02-28 02:53:46 | [diff] [blame^] | 441 | // TODO(pkasting): Remove ScopedTracker below once crbug.com/462813 is fixed. |
pkasting | 58e029b | 2015-02-21 05:17:28 | [diff] [blame] | 442 | tracked_objects::ScopedTracker tracking_profile( |
pkasting | 93ba27f6 | 2015-02-28 02:53:46 | [diff] [blame^] | 443 | FROM_HERE_WITH_EXPLICIT_FUNCTION("462813 SSLConnectJob::DoSSLConnect")); |
[email protected] | 8e45855 | 2014-08-05 00:02:15 | [diff] [blame] | 444 | next_state_ = STATE_SSL_CONNECT_COMPLETE; |
| 445 | |
| 446 | connect_timing_.ssl_start = base::TimeTicks::Now(); |
| 447 | |
| 448 | return ssl_socket_->Connect(io_callback_); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | int SSLConnectJob::DoSSLConnectComplete(int result) { |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 452 | connect_timing_.ssl_end = base::TimeTicks::Now(); |
| 453 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 454 | SSLClientSocket::NextProtoStatus status = |
| 455 | SSLClientSocket::kNextProtoUnsupported; |
| 456 | std::string proto; |
| 457 | // GetNextProto will fail and and trigger a NOTREACHED if we pass in a socket |
| 458 | // that hasn't had SSL_ImportFD called on it. If we get a certificate error |
| 459 | // here, then we know that we called SSL_ImportFD. |
bnc | 0d28ea5 | 2014-10-13 15:15:38 | [diff] [blame] | 460 | if (result == OK || IsCertificateError(result)) { |
[email protected] | abc44b75 | 2014-07-30 03:52:15 | [diff] [blame] | 461 | status = ssl_socket_->GetNextProto(&proto); |
bnc | 0d28ea5 | 2014-10-13 15:15:38 | [diff] [blame] | 462 | ssl_socket_->RecordNegotiationExtension(); |
| 463 | } |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 464 | |
[email protected] | 9e9e842e | 2010-07-23 23:09:15 | [diff] [blame] | 465 | // If we want spdy over npn, make sure it succeeded. |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 466 | if (status == SSLClientSocket::kNextProtoNegotiated) { |
[email protected] | d7c9f42 | 2010-08-27 22:54:53 | [diff] [blame] | 467 | ssl_socket_->set_was_npn_negotiated(true); |
[email protected] | 8e3c78cb | 2012-03-31 03:58:46 | [diff] [blame] | 468 | NextProto protocol_negotiated = |
[email protected] | bace48c | 2010-08-03 20:52:02 | [diff] [blame] | 469 | SSLClientSocket::NextProtoFromString(proto); |
[email protected] | c30bcce | 2011-12-20 17:50:51 | [diff] [blame] | 470 | ssl_socket_->set_protocol_negotiated(protocol_negotiated); |
[email protected] | 63bf966 | 2013-03-05 20:46:01 | [diff] [blame] | 471 | // If we negotiated a SPDY version, it must have been present in |
| 472 | // SSLConfig::next_protos. |
| 473 | // TODO(mbelshe): Verify this. |
| 474 | if (protocol_negotiated >= kProtoSPDYMinimumVersion && |
| 475 | protocol_negotiated <= kProtoSPDYMaximumVersion) { |
[email protected] | d7c9f42 | 2010-08-27 22:54:53 | [diff] [blame] | 476 | ssl_socket_->set_was_spdy_negotiated(true); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 477 | } |
| 478 | } |
[email protected] | d7c9f42 | 2010-08-27 22:54:53 | [diff] [blame] | 479 | if (params_->want_spdy_over_npn() && !ssl_socket_->was_spdy_negotiated()) |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 480 | return ERR_NPN_NEGOTIATION_FAILED; |
| 481 | |
[email protected] | 9e9e842e | 2010-07-23 23:09:15 | [diff] [blame] | 482 | // Spdy might be turned on by default, or it might be over npn. |
| 483 | bool using_spdy = params_->force_spdy_over_ssl() || |
| 484 | params_->want_spdy_over_npn(); |
| 485 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 486 | if (result == OK || |
| 487 | ssl_socket_->IgnoreCertError(result, params_->load_flags())) { |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 488 | DCHECK(!connect_timing_.ssl_start.is_null()); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 489 | base::TimeDelta connect_duration = |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 490 | connect_timing_.ssl_end - connect_timing_.ssl_start; |
[email protected] | 835d7c8 | 2010-10-14 04:38:38 | [diff] [blame] | 491 | if (using_spdy) { |
[email protected] | 84727696 | 2013-03-08 23:53:17 | [diff] [blame] | 492 | UMA_HISTOGRAM_CUSTOM_TIMES("Net.SpdyConnectionLatency_2", |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 493 | connect_duration, |
| 494 | base::TimeDelta::FromMilliseconds(1), |
[email protected] | 84727696 | 2013-03-08 23:53:17 | [diff] [blame] | 495 | base::TimeDelta::FromMinutes(1), |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 496 | 100); |
[email protected] | f906bfe | 2011-06-09 16:35:24 | [diff] [blame] | 497 | } |
[email protected] | f906bfe | 2011-06-09 16:35:24 | [diff] [blame] | 498 | |
[email protected] | 84727696 | 2013-03-08 23:53:17 | [diff] [blame] | 499 | UMA_HISTOGRAM_CUSTOM_TIMES("Net.SSL_Connection_Latency_2", |
[email protected] | f906bfe | 2011-06-09 16:35:24 | [diff] [blame] | 500 | connect_duration, |
| 501 | base::TimeDelta::FromMilliseconds(1), |
[email protected] | 84727696 | 2013-03-08 23:53:17 | [diff] [blame] | 502 | base::TimeDelta::FromMinutes(1), |
[email protected] | f906bfe | 2011-06-09 16:35:24 | [diff] [blame] | 503 | 100); |
| 504 | |
[email protected] | b076d6c | 2011-06-29 14:47:51 | [diff] [blame] | 505 | SSLInfo ssl_info; |
davidben | d3f1515 | 2015-02-20 23:43:09 | [diff] [blame] | 506 | bool has_ssl_info = ssl_socket_->GetSSLInfo(&ssl_info); |
| 507 | DCHECK(has_ssl_info); |
[email protected] | b076d6c | 2011-06-29 14:47:51 | [diff] [blame] | 508 | |
davidben | 9ff250e0 | 2015-02-19 23:06:51 | [diff] [blame] | 509 | UMA_HISTOGRAM_ENUMERATION("Net.SSLVersion", SSLConnectionStatusToVersion( |
| 510 | ssl_info.connection_status), |
| 511 | SSL_CONNECTION_VERSION_MAX); |
| 512 | |
[email protected] | 92a3124 | 2013-06-28 18:08:08 | [diff] [blame] | 513 | UMA_HISTOGRAM_SPARSE_SLOWLY("Net.SSL_CipherSuite", |
| 514 | SSLConnectionStatusToCipherSuite( |
| 515 | ssl_info.connection_status)); |
| 516 | |
davidben | 09c3d07 | 2014-08-25 20:33:58 | [diff] [blame] | 517 | UMA_HISTOGRAM_BOOLEAN( |
| 518 | "Net.RenegotiationExtensionSupported", |
| 519 | (ssl_info.connection_status & |
| 520 | SSL_CONNECTION_NO_RENEGOTIATION_EXTENSION) == 0); |
| 521 | |
[email protected] | b076d6c | 2011-06-29 14:47:51 | [diff] [blame] | 522 | if (ssl_info.handshake_type == SSLInfo::HANDSHAKE_RESUME) { |
| 523 | UMA_HISTOGRAM_CUSTOM_TIMES("Net.SSL_Connection_Latency_Resume_Handshake", |
| 524 | connect_duration, |
| 525 | base::TimeDelta::FromMilliseconds(1), |
| 526 | base::TimeDelta::FromMinutes(1), |
| 527 | 100); |
| 528 | } else if (ssl_info.handshake_type == SSLInfo::HANDSHAKE_FULL) { |
| 529 | UMA_HISTOGRAM_CUSTOM_TIMES("Net.SSL_Connection_Latency_Full_Handshake", |
| 530 | connect_duration, |
| 531 | base::TimeDelta::FromMilliseconds(1), |
| 532 | base::TimeDelta::FromMinutes(1), |
| 533 | 100); |
| 534 | } |
| 535 | |
[email protected] | f906bfe | 2011-06-09 16:35:24 | [diff] [blame] | 536 | const std::string& host = params_->host_and_port().host(); |
[email protected] | 8e45855 | 2014-08-05 00:02:15 | [diff] [blame] | 537 | bool is_google = |
| 538 | host == "google.com" || |
| 539 | (host.size() > 11 && host.rfind(".google.com") == host.size() - 11); |
[email protected] | f906bfe | 2011-06-09 16:35:24 | [diff] [blame] | 540 | if (is_google) { |
[email protected] | 84727696 | 2013-03-08 23:53:17 | [diff] [blame] | 541 | UMA_HISTOGRAM_CUSTOM_TIMES("Net.SSL_Connection_Latency_Google2", |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 542 | connect_duration, |
| 543 | base::TimeDelta::FromMilliseconds(1), |
[email protected] | 84727696 | 2013-03-08 23:53:17 | [diff] [blame] | 544 | base::TimeDelta::FromMinutes(1), |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 545 | 100); |
[email protected] | b076d6c | 2011-06-29 14:47:51 | [diff] [blame] | 546 | if (ssl_info.handshake_type == SSLInfo::HANDSHAKE_RESUME) { |
| 547 | UMA_HISTOGRAM_CUSTOM_TIMES("Net.SSL_Connection_Latency_Google_" |
| 548 | "Resume_Handshake", |
| 549 | connect_duration, |
| 550 | base::TimeDelta::FromMilliseconds(1), |
| 551 | base::TimeDelta::FromMinutes(1), |
| 552 | 100); |
| 553 | } else if (ssl_info.handshake_type == SSLInfo::HANDSHAKE_FULL) { |
| 554 | UMA_HISTOGRAM_CUSTOM_TIMES("Net.SSL_Connection_Latency_Google_" |
| 555 | "Full_Handshake", |
| 556 | connect_duration, |
| 557 | base::TimeDelta::FromMilliseconds(1), |
| 558 | base::TimeDelta::FromMinutes(1), |
| 559 | 100); |
| 560 | } |
[email protected] | 835d7c8 | 2010-10-14 04:38:38 | [diff] [blame] | 561 | } |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 562 | } |
[email protected] | 8b49869 | 2010-07-16 17:11:43 | [diff] [blame] | 563 | |
jeremyim | 8d44fadd | 2015-02-10 19:18:15 | [diff] [blame] | 564 | UMA_HISTOGRAM_SPARSE_SLOWLY("Net.SSL_Connection_Error", std::abs(result)); |
| 565 | if (params_->ssl_config().fastradio_padding_eligible) { |
| 566 | UMA_HISTOGRAM_SPARSE_SLOWLY("Net.SSL_Connection_Error_FastRadioPadding", |
| 567 | std::abs(result)); |
| 568 | } |
| 569 | |
[email protected] | 8b49869 | 2010-07-16 17:11:43 | [diff] [blame] | 570 | if (result == OK || IsCertificateError(result)) { |
dcheng | e3d1ddc | 2014-10-15 19:30:51 | [diff] [blame] | 571 | SetSocket(ssl_socket_.Pass()); |
[email protected] | 8b49869 | 2010-07-16 17:11:43 | [diff] [blame] | 572 | } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) { |
| 573 | error_response_info_.cert_request_info = new SSLCertRequestInfo; |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 574 | ssl_socket_->GetSSLCertRequestInfo( |
| 575 | error_response_info_.cert_request_info.get()); |
[email protected] | 8b49869 | 2010-07-16 17:11:43 | [diff] [blame] | 576 | } |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 577 | |
| 578 | return result; |
| 579 | } |
| 580 | |
[email protected] | 8e45855 | 2014-08-05 00:02:15 | [diff] [blame] | 581 | void SSLConnectJob::ResumeSSLConnection() { |
| 582 | DCHECK_EQ(next_state_, STATE_SSL_CONNECT); |
[email protected] | cffd7f9 | 2014-08-21 21:30:50 | [diff] [blame] | 583 | messenger_ = NULL; |
[email protected] | 8e45855 | 2014-08-05 00:02:15 | [diff] [blame] | 584 | OnIOComplete(OK); |
| 585 | } |
| 586 | |
[email protected] | ea79ba9 | 2013-08-15 21:56:20 | [diff] [blame] | 587 | SSLConnectJob::State SSLConnectJob::GetInitialState( |
| 588 | SSLSocketParams::ConnectionType connection_type) { |
| 589 | switch (connection_type) { |
| 590 | case SSLSocketParams::DIRECT: |
| 591 | return STATE_TRANSPORT_CONNECT; |
| 592 | case SSLSocketParams::HTTP_PROXY: |
| 593 | return STATE_TUNNEL_CONNECT; |
| 594 | case SSLSocketParams::SOCKS_PROXY: |
| 595 | return STATE_SOCKS_CONNECT; |
[email protected] | ad74a59 | 2011-01-21 18:40:55 | [diff] [blame] | 596 | } |
[email protected] | ea79ba9 | 2013-08-15 21:56:20 | [diff] [blame] | 597 | NOTREACHED(); |
| 598 | return STATE_NONE; |
| 599 | } |
| 600 | |
| 601 | int SSLConnectJob::ConnectInternal() { |
| 602 | next_state_ = GetInitialState(params_->GetConnectionType()); |
[email protected] | ad74a59 | 2011-01-21 18:40:55 | [diff] [blame] | 603 | return DoLoop(OK); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | SSLClientSocketPool::SSLConnectJobFactory::SSLConnectJobFactory( |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 607 | TransportClientSocketPool* transport_pool, |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 608 | SOCKSClientSocketPool* socks_pool, |
| 609 | HttpProxyClientSocketPool* http_proxy_pool, |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 610 | ClientSocketFactory* client_socket_factory, |
[email protected] | feb79bcd | 2011-07-21 16:55:17 | [diff] [blame] | 611 | const SSLClientSocketContext& context, |
[email protected] | cffd7f9 | 2014-08-21 21:30:50 | [diff] [blame] | 612 | const SSLConnectJob::GetMessengerCallback& get_messenger_callback, |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 613 | NetLog* net_log) |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 614 | : transport_pool_(transport_pool), |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 615 | socks_pool_(socks_pool), |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 616 | http_proxy_pool_(http_proxy_pool), |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 617 | client_socket_factory_(client_socket_factory), |
[email protected] | feb79bcd | 2011-07-21 16:55:17 | [diff] [blame] | 618 | context_(context), |
[email protected] | cffd7f9 | 2014-08-21 21:30:50 | [diff] [blame] | 619 | get_messenger_callback_(get_messenger_callback), |
| 620 | net_log_(net_log) { |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 621 | base::TimeDelta max_transport_timeout = base::TimeDelta(); |
| 622 | base::TimeDelta pool_timeout; |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 623 | if (transport_pool_) |
| 624 | max_transport_timeout = transport_pool_->ConnectionTimeout(); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 625 | if (socks_pool_) { |
| 626 | pool_timeout = socks_pool_->ConnectionTimeout(); |
| 627 | if (pool_timeout > max_transport_timeout) |
| 628 | max_transport_timeout = pool_timeout; |
| 629 | } |
| 630 | if (http_proxy_pool_) { |
| 631 | pool_timeout = http_proxy_pool_->ConnectionTimeout(); |
| 632 | if (pool_timeout > max_transport_timeout) |
| 633 | max_transport_timeout = pool_timeout; |
| 634 | } |
| 635 | timeout_ = max_transport_timeout + |
| 636 | base::TimeDelta::FromSeconds(kSSLHandshakeTimeoutInSeconds); |
| 637 | } |
| 638 | |
[email protected] | 8e45855 | 2014-08-05 00:02:15 | [diff] [blame] | 639 | SSLClientSocketPool::SSLConnectJobFactory::~SSLConnectJobFactory() { |
[email protected] | 8e45855 | 2014-08-05 00:02:15 | [diff] [blame] | 640 | } |
| 641 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 642 | SSLClientSocketPool::SSLClientSocketPool( |
| 643 | int max_sockets, |
| 644 | int max_sockets_per_group, |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 645 | ClientSocketPoolHistograms* histograms, |
[email protected] | 822581d | 2010-12-16 17:27:15 | [diff] [blame] | 646 | CertVerifier* cert_verifier, |
[email protected] | 6b8a3c74 | 2014-07-25 00:25:35 | [diff] [blame] | 647 | ChannelIDService* channel_id_service, |
[email protected] | a2a4197 | 2011-12-07 17:47:27 | [diff] [blame] | 648 | TransportSecurityState* transport_security_state, |
[email protected] | 284303b6 | 2013-11-28 15:11:54 | [diff] [blame] | 649 | CTVerifier* cert_transparency_verifier, |
eranm | 6571b2b | 2014-12-03 15:53:23 | [diff] [blame] | 650 | CertPolicyEnforcer* cert_policy_enforcer, |
[email protected] | c3456bb | 2011-12-12 22:22:19 | [diff] [blame] | 651 | const std::string& ssl_session_cache_shard, |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 652 | ClientSocketFactory* client_socket_factory, |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 653 | TransportClientSocketPool* transport_pool, |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 654 | SOCKSClientSocketPool* socks_pool, |
| 655 | HttpProxyClientSocketPool* http_proxy_pool, |
[email protected] | 7abf7d2 | 2010-09-04 01:41:59 | [diff] [blame] | 656 | SSLConfigService* ssl_config_service, |
[email protected] | 8e45855 | 2014-08-05 00:02:15 | [diff] [blame] | 657 | bool enable_ssl_connect_job_waiting, |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 658 | NetLog* net_log) |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 659 | : transport_pool_(transport_pool), |
[email protected] | ba00b49 | 2010-09-08 14:53:38 | [diff] [blame] | 660 | socks_pool_(socks_pool), |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 661 | http_proxy_pool_(http_proxy_pool), |
[email protected] | 8e45855 | 2014-08-05 00:02:15 | [diff] [blame] | 662 | base_(this, |
| 663 | max_sockets, |
| 664 | max_sockets_per_group, |
| 665 | histograms, |
[email protected] | 82b8c96 | 2011-10-12 09:17:30 | [diff] [blame] | 666 | ClientSocketPool::unused_idle_socket_timeout(), |
| 667 | ClientSocketPool::used_idle_socket_timeout(), |
[email protected] | 8e45855 | 2014-08-05 00:02:15 | [diff] [blame] | 668 | new SSLConnectJobFactory( |
| 669 | transport_pool, |
| 670 | socks_pool, |
| 671 | http_proxy_pool, |
| 672 | client_socket_factory, |
[email protected] | 8e45855 | 2014-08-05 00:02:15 | [diff] [blame] | 673 | SSLClientSocketContext(cert_verifier, |
| 674 | channel_id_service, |
| 675 | transport_security_state, |
| 676 | cert_transparency_verifier, |
eranm | 6571b2b | 2014-12-03 15:53:23 | [diff] [blame] | 677 | cert_policy_enforcer, |
[email protected] | 8e45855 | 2014-08-05 00:02:15 | [diff] [blame] | 678 | ssl_session_cache_shard), |
[email protected] | cffd7f9 | 2014-08-21 21:30:50 | [diff] [blame] | 679 | base::Bind( |
| 680 | &SSLClientSocketPool::GetOrCreateSSLConnectJobMessenger, |
| 681 | base::Unretained(this)), |
[email protected] | 8e45855 | 2014-08-05 00:02:15 | [diff] [blame] | 682 | net_log)), |
[email protected] | cffd7f9 | 2014-08-21 21:30:50 | [diff] [blame] | 683 | ssl_config_service_(ssl_config_service), |
| 684 | enable_ssl_connect_job_waiting_(enable_ssl_connect_job_waiting) { |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 685 | if (ssl_config_service_.get()) |
[email protected] | 7abf7d2 | 2010-09-04 01:41:59 | [diff] [blame] | 686 | ssl_config_service_->AddObserver(this); |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 687 | if (transport_pool_) |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 688 | base_.AddLowerLayeredPool(transport_pool_); |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 689 | if (socks_pool_) |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 690 | base_.AddLowerLayeredPool(socks_pool_); |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 691 | if (http_proxy_pool_) |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 692 | base_.AddLowerLayeredPool(http_proxy_pool_); |
[email protected] | 7abf7d2 | 2010-09-04 01:41:59 | [diff] [blame] | 693 | } |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 694 | |
[email protected] | 7abf7d2 | 2010-09-04 01:41:59 | [diff] [blame] | 695 | SSLClientSocketPool::~SSLClientSocketPool() { |
[email protected] | cffd7f9 | 2014-08-21 21:30:50 | [diff] [blame] | 696 | STLDeleteContainerPairSecondPointers(messenger_map_.begin(), |
| 697 | messenger_map_.end()); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 698 | if (ssl_config_service_.get()) |
[email protected] | 7abf7d2 | 2010-09-04 01:41:59 | [diff] [blame] | 699 | ssl_config_service_->RemoveObserver(this); |
| 700 | } |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 701 | |
[email protected] | cffd7f9 | 2014-08-21 21:30:50 | [diff] [blame] | 702 | scoped_ptr<ConnectJob> SSLClientSocketPool::SSLConnectJobFactory::NewConnectJob( |
[email protected] | ad74a59 | 2011-01-21 18:40:55 | [diff] [blame] | 703 | const std::string& group_name, |
| 704 | const PoolBase::Request& request, |
| 705 | ConnectJob::Delegate* delegate) const { |
[email protected] | 8e45855 | 2014-08-05 00:02:15 | [diff] [blame] | 706 | return scoped_ptr<ConnectJob>(new SSLConnectJob(group_name, |
| 707 | request.priority(), |
| 708 | request.params(), |
| 709 | ConnectionTimeout(), |
| 710 | transport_pool_, |
| 711 | socks_pool_, |
| 712 | http_proxy_pool_, |
| 713 | client_socket_factory_, |
[email protected] | 8e45855 | 2014-08-05 00:02:15 | [diff] [blame] | 714 | context_, |
[email protected] | cffd7f9 | 2014-08-21 21:30:50 | [diff] [blame] | 715 | get_messenger_callback_, |
[email protected] | 8e45855 | 2014-08-05 00:02:15 | [diff] [blame] | 716 | delegate, |
| 717 | net_log_)); |
[email protected] | ad74a59 | 2011-01-21 18:40:55 | [diff] [blame] | 718 | } |
| 719 | |
[email protected] | cffd7f9 | 2014-08-21 21:30:50 | [diff] [blame] | 720 | base::TimeDelta SSLClientSocketPool::SSLConnectJobFactory::ConnectionTimeout() |
| 721 | const { |
[email protected] | 2a848e0e | 2012-08-09 22:27:31 | [diff] [blame] | 722 | return timeout_; |
| 723 | } |
| 724 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 725 | int SSLClientSocketPool::RequestSocket(const std::string& group_name, |
| 726 | const void* socket_params, |
| 727 | RequestPriority priority, |
| 728 | ClientSocketHandle* handle, |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 729 | const CompletionCallback& callback, |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 730 | const BoundNetLog& net_log) { |
| 731 | const scoped_refptr<SSLSocketParams>* casted_socket_params = |
| 732 | static_cast<const scoped_refptr<SSLSocketParams>*>(socket_params); |
| 733 | |
| 734 | return base_.RequestSocket(group_name, *casted_socket_params, priority, |
| 735 | handle, callback, net_log); |
| 736 | } |
| 737 | |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 738 | void SSLClientSocketPool::RequestSockets( |
| 739 | const std::string& group_name, |
| 740 | const void* params, |
| 741 | int num_sockets, |
| 742 | const BoundNetLog& net_log) { |
| 743 | const scoped_refptr<SSLSocketParams>* casted_params = |
| 744 | static_cast<const scoped_refptr<SSLSocketParams>*>(params); |
| 745 | |
| 746 | base_.RequestSockets(group_name, *casted_params, num_sockets, net_log); |
| 747 | } |
| 748 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 749 | void SSLClientSocketPool::CancelRequest(const std::string& group_name, |
[email protected] | 05ea9ff | 2010-07-15 19:08:21 | [diff] [blame] | 750 | ClientSocketHandle* handle) { |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 751 | base_.CancelRequest(group_name, handle); |
| 752 | } |
| 753 | |
| 754 | void SSLClientSocketPool::ReleaseSocket(const std::string& group_name, |
[email protected] | 18ccfdb | 2013-08-15 00:13:44 | [diff] [blame] | 755 | scoped_ptr<StreamSocket> socket, |
| 756 | int id) { |
| 757 | base_.ReleaseSocket(group_name, socket.Pass(), id); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 758 | } |
| 759 | |
[email protected] | 7af985a | 2012-12-14 22:40:42 | [diff] [blame] | 760 | void SSLClientSocketPool::FlushWithError(int error) { |
| 761 | base_.FlushWithError(error); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 762 | } |
| 763 | |
| 764 | void SSLClientSocketPool::CloseIdleSockets() { |
| 765 | base_.CloseIdleSockets(); |
| 766 | } |
| 767 | |
[email protected] | ddb1e5a | 2010-12-13 20:10:45 | [diff] [blame] | 768 | int SSLClientSocketPool::IdleSocketCount() const { |
| 769 | return base_.idle_socket_count(); |
| 770 | } |
| 771 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 772 | int SSLClientSocketPool::IdleSocketCountInGroup( |
| 773 | const std::string& group_name) const { |
| 774 | return base_.IdleSocketCountInGroup(group_name); |
| 775 | } |
| 776 | |
| 777 | LoadState SSLClientSocketPool::GetLoadState( |
| 778 | const std::string& group_name, const ClientSocketHandle* handle) const { |
| 779 | return base_.GetLoadState(group_name, handle); |
| 780 | } |
| 781 | |
[email protected] | ea5ef4c | 2013-06-13 22:50:27 | [diff] [blame] | 782 | base::DictionaryValue* SSLClientSocketPool::GetInfoAsValue( |
[email protected] | ba00b49 | 2010-09-08 14:53:38 | [diff] [blame] | 783 | const std::string& name, |
| 784 | const std::string& type, |
| 785 | bool include_nested_pools) const { |
[email protected] | ea5ef4c | 2013-06-13 22:50:27 | [diff] [blame] | 786 | base::DictionaryValue* dict = base_.GetInfoAsValue(name, type); |
[email protected] | ba00b49 | 2010-09-08 14:53:38 | [diff] [blame] | 787 | if (include_nested_pools) { |
[email protected] | ea5ef4c | 2013-06-13 22:50:27 | [diff] [blame] | 788 | base::ListValue* list = new base::ListValue(); |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 789 | if (transport_pool_) { |
| 790 | list->Append(transport_pool_->GetInfoAsValue("transport_socket_pool", |
| 791 | "transport_socket_pool", |
| 792 | false)); |
[email protected] | ba00b49 | 2010-09-08 14:53:38 | [diff] [blame] | 793 | } |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 794 | if (socks_pool_) { |
[email protected] | ba00b49 | 2010-09-08 14:53:38 | [diff] [blame] | 795 | list->Append(socks_pool_->GetInfoAsValue("socks_pool", |
| 796 | "socks_pool", |
| 797 | true)); |
| 798 | } |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 799 | if (http_proxy_pool_) { |
| 800 | list->Append(http_proxy_pool_->GetInfoAsValue("http_proxy_pool", |
| 801 | "http_proxy_pool", |
| 802 | true)); |
| 803 | } |
[email protected] | ba00b49 | 2010-09-08 14:53:38 | [diff] [blame] | 804 | dict->Set("nested_pools", list); |
| 805 | } |
| 806 | return dict; |
| 807 | } |
| 808 | |
[email protected] | ddb1e5a | 2010-12-13 20:10:45 | [diff] [blame] | 809 | base::TimeDelta SSLClientSocketPool::ConnectionTimeout() const { |
| 810 | return base_.ConnectionTimeout(); |
| 811 | } |
| 812 | |
| 813 | ClientSocketPoolHistograms* SSLClientSocketPool::histograms() const { |
| 814 | return base_.histograms(); |
| 815 | } |
| 816 | |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 817 | bool SSLClientSocketPool::IsStalled() const { |
| 818 | return base_.IsStalled(); |
| 819 | } |
| 820 | |
| 821 | void SSLClientSocketPool::AddHigherLayeredPool(HigherLayeredPool* higher_pool) { |
| 822 | base_.AddHigherLayeredPool(higher_pool); |
| 823 | } |
| 824 | |
| 825 | void SSLClientSocketPool::RemoveHigherLayeredPool( |
| 826 | HigherLayeredPool* higher_pool) { |
| 827 | base_.RemoveHigherLayeredPool(higher_pool); |
[email protected] | ad74a59 | 2011-01-21 18:40:55 | [diff] [blame] | 828 | } |
| 829 | |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 830 | bool SSLClientSocketPool::CloseOneIdleConnection() { |
| 831 | if (base_.CloseOneIdleSocket()) |
| 832 | return true; |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 833 | return base_.CloseOneIdleConnectionInHigherLayeredPool(); |
| 834 | } |
| 835 | |
[email protected] | cffd7f9 | 2014-08-21 21:30:50 | [diff] [blame] | 836 | SSLConnectJobMessenger* SSLClientSocketPool::GetOrCreateSSLConnectJobMessenger( |
| 837 | const std::string& cache_key) { |
| 838 | if (!enable_ssl_connect_job_waiting_) |
| 839 | return NULL; |
| 840 | MessengerMap::const_iterator it = messenger_map_.find(cache_key); |
| 841 | if (it == messenger_map_.end()) { |
| 842 | std::pair<MessengerMap::iterator, bool> iter = |
| 843 | messenger_map_.insert(MessengerMap::value_type( |
| 844 | cache_key, |
| 845 | new SSLConnectJobMessenger( |
| 846 | base::Bind(&SSLClientSocketPool::DeleteSSLConnectJobMessenger, |
| 847 | base::Unretained(this), |
| 848 | cache_key)))); |
| 849 | it = iter.first; |
| 850 | } |
| 851 | return it->second; |
| 852 | } |
| 853 | |
| 854 | void SSLClientSocketPool::DeleteSSLConnectJobMessenger( |
| 855 | const std::string& cache_key) { |
| 856 | MessengerMap::iterator it = messenger_map_.find(cache_key); |
| 857 | CHECK(it != messenger_map_.end()); |
| 858 | delete it->second; |
| 859 | messenger_map_.erase(it); |
| 860 | } |
| 861 | |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 862 | void SSLClientSocketPool::OnSSLConfigChanged() { |
| 863 | FlushWithError(ERR_NETWORK_CHANGED); |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 864 | } |
| 865 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 866 | } // namespace net |