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