| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "extensions/browser/api/cast_channel/cast_socket.h" | 5 #include "extensions/browser/api/cast_channel/cast_socket.h" |
| 6 | 6 |
| 7 #include <stdlib.h> | 7 #include <stdlib.h> |
| 8 #include <string.h> | 8 #include <string.h> |
| 9 | 9 |
| 10 #include <utility> | 10 #include <utility> |
| 11 | 11 |
| 12 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/callback_helpers.h" | 13 #include "base/callback_helpers.h" |
| 14 #include "base/format_macros.h" | 14 #include "base/format_macros.h" |
| 15 #include "base/lazy_instance.h" | 15 #include "base/lazy_instance.h" |
| 16 #include "base/location.h" | 16 #include "base/location.h" |
| 17 #include "base/memory/ptr_util.h" | 17 #include "base/memory/ptr_util.h" |
| 18 #include "base/numerics/safe_conversions.h" | 18 #include "base/numerics/safe_conversions.h" |
| 19 #include "base/single_thread_task_runner.h" | 19 #include "base/single_thread_task_runner.h" |
| 20 #include "base/strings/string_number_conversions.h" | 20 #include "base/strings/string_number_conversions.h" |
| 21 #include "base/strings/stringprintf.h" | 21 #include "base/strings/stringprintf.h" |
| 22 #include "base/sys_byteorder.h" | 22 #include "base/sys_byteorder.h" |
| 23 #include "base/threading/thread_task_runner_handle.h" | 23 #include "base/threading/thread_task_runner_handle.h" |
| 24 #include "base/time/time.h" | 24 #include "base/time/time.h" |
| 25 #include "extensions/browser/api/cast_channel/cast_auth_util.h" | 25 #include "extensions/browser/api/cast_channel/cast_auth_util.h" |
| 26 #include "extensions/browser/api/cast_channel/cast_framer.h" | 26 #include "extensions/browser/api/cast_channel/cast_framer.h" |
| 27 #include "extensions/browser/api/cast_channel/cast_message_util.h" | 27 #include "extensions/browser/api/cast_channel/cast_message_util.h" |
| 28 #include "extensions/browser/api/cast_channel/cast_transport.h" | 28 #include "extensions/browser/api/cast_channel/cast_transport.h" |
| 29 #include "extensions/browser/api/cast_channel/logger.h" | 29 #include "extensions/browser/api/cast_channel/logger.h" |
| 30 #include "extensions/browser/api/cast_channel/logger_util.h" | |
| 31 #include "extensions/common/api/cast_channel/cast_channel.pb.h" | 30 #include "extensions/common/api/cast_channel/cast_channel.pb.h" |
| 32 #include "net/base/address_list.h" | 31 #include "net/base/address_list.h" |
| 33 #include "net/base/host_port_pair.h" | 32 #include "net/base/host_port_pair.h" |
| 34 #include "net/base/net_errors.h" | 33 #include "net/base/net_errors.h" |
| 35 #include "net/cert/cert_verifier.h" | 34 #include "net/cert/cert_verifier.h" |
| 36 #include "net/cert/cert_verify_result.h" | 35 #include "net/cert/cert_verify_result.h" |
| 37 #include "net/cert/ct_policy_enforcer.h" | 36 #include "net/cert/ct_policy_enforcer.h" |
| 38 #include "net/cert/multi_log_ct_verifier.h" | 37 #include "net/cert/multi_log_ct_verifier.h" |
| 39 #include "net/cert/x509_certificate.h" | 38 #include "net/cert/x509_certificate.h" |
| 40 #include "net/http/transport_security_state.h" | 39 #include "net/http/transport_security_state.h" |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 203 connection->SetSocket(std::move(socket)); | 202 connection->SetSocket(std::move(socket)); |
| 204 net::HostPortPair host_and_port = net::HostPortPair::FromIPEndPoint( | 203 net::HostPortPair host_and_port = net::HostPortPair::FromIPEndPoint( |
| 205 ip_endpoint_); | 204 ip_endpoint_); |
| 206 | 205 |
| 207 return net::ClientSocketFactory::GetDefaultFactory()->CreateSSLClientSocket( | 206 return net::ClientSocketFactory::GetDefaultFactory()->CreateSSLClientSocket( |
| 208 std::move(connection), host_and_port, ssl_config, context); | 207 std::move(connection), host_and_port, ssl_config, context); |
| 209 } | 208 } |
| 210 | 209 |
| 211 scoped_refptr<net::X509Certificate> CastSocketImpl::ExtractPeerCert() { | 210 scoped_refptr<net::X509Certificate> CastSocketImpl::ExtractPeerCert() { |
| 212 net::SSLInfo ssl_info; | 211 net::SSLInfo ssl_info; |
| 213 if (!socket_->GetSSLInfo(&ssl_info) || !ssl_info.cert.get()) { | 212 if (!socket_->GetSSLInfo(&ssl_info) || !ssl_info.cert.get()) |
| 214 return nullptr; | 213 return nullptr; |
| 215 } | |
| 216 | |
| 217 logger_->LogSocketEvent(channel_id_, proto::SSL_INFO_OBTAINED); | |
| 218 | 214 |
| 219 return ssl_info.cert; | 215 return ssl_info.cert; |
| 220 } | 216 } |
| 221 | 217 |
| 222 bool CastSocketImpl::VerifyChannelPolicy(const AuthResult& result) { | 218 bool CastSocketImpl::VerifyChannelPolicy(const AuthResult& result) { |
| 223 audio_only_ = (result.channel_policies & AuthResult::POLICY_AUDIO_ONLY) != 0; | 219 audio_only_ = (result.channel_policies & AuthResult::POLICY_AUDIO_ONLY) != 0; |
| 224 if (audio_only_ && | 220 if (audio_only_ && |
| 225 (device_capabilities_ & CastDeviceCapability::VIDEO_OUT) != 0) { | 221 (device_capabilities_ & CastDeviceCapability::VIDEO_OUT) != 0) { |
| 226 LOG_WITH_CONNECTION(ERROR) | 222 LOG_WITH_CONNECTION(ERROR) |
| 227 << "Audio only channel policy enforced for video out capable device"; | 223 << "Audio only channel policy enforced for video out capable device"; |
| 228 logger_->LogSocketEventWithDetails( | |
| 229 channel_id_, proto::CHANNEL_POLICY_ENFORCED, std::string()); | |
| 230 return false; | 224 return false; |
| 231 } | 225 } |
| 232 return true; | 226 return true; |
| 233 } | 227 } |
| 234 | 228 |
| 235 bool CastSocketImpl::VerifyChallengeReply() { | 229 bool CastSocketImpl::VerifyChallengeReply() { |
| 236 DCHECK(peer_cert_); | 230 DCHECK(peer_cert_); |
| 237 AuthResult result = | 231 AuthResult result = |
| 238 AuthenticateChallengeReply(*challenge_reply_, *peer_cert_); | 232 AuthenticateChallengeReply(*challenge_reply_, *peer_cert_); |
| 239 logger_->LogSocketChallengeReplyEvent(channel_id_, result); | 233 logger_->LogSocketChallengeReplyEvent(channel_id_, result); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 253 | 247 |
| 254 void CastSocketImpl::Connect(std::unique_ptr<CastTransport::Delegate> delegate, | 248 void CastSocketImpl::Connect(std::unique_ptr<CastTransport::Delegate> delegate, |
| 255 base::Callback<void(ChannelError)> callback) { | 249 base::Callback<void(ChannelError)> callback) { |
| 256 DCHECK(CalledOnValidThread()); | 250 DCHECK(CalledOnValidThread()); |
| 257 VLOG_WITH_CONNECTION(1) << "Connect readyState = " << ready_state_; | 251 VLOG_WITH_CONNECTION(1) << "Connect readyState = " << ready_state_; |
| 258 DCHECK_EQ(proto::CONN_STATE_START_CONNECT, connect_state_); | 252 DCHECK_EQ(proto::CONN_STATE_START_CONNECT, connect_state_); |
| 259 | 253 |
| 260 delegate_ = std::move(delegate); | 254 delegate_ = std::move(delegate); |
| 261 | 255 |
| 262 if (ready_state_ != READY_STATE_NONE) { | 256 if (ready_state_ != READY_STATE_NONE) { |
| 263 logger_->LogSocketEventWithDetails( | |
| 264 channel_id_, proto::CONNECT_FAILED, "ReadyState not NONE"); | |
| 265 callback.Run(CHANNEL_ERROR_CONNECT_ERROR); | 257 callback.Run(CHANNEL_ERROR_CONNECT_ERROR); |
| 266 return; | 258 return; |
| 267 } | 259 } |
| 268 | 260 |
| 269 connect_callback_ = callback; | 261 connect_callback_ = callback; |
| 270 SetReadyState(READY_STATE_CONNECTING); | 262 SetReadyState(READY_STATE_CONNECTING); |
| 271 SetConnectState(proto::CONN_STATE_TCP_CONNECT); | 263 SetConnectState(proto::CONN_STATE_TCP_CONNECT); |
| 272 | 264 |
| 273 // Set up connection timeout. | 265 // Set up connection timeout. |
| 274 if (connect_timeout_.InMicroseconds() > 0) { | 266 if (connect_timeout_.InMicroseconds() > 0) { |
| 275 DCHECK(connect_timeout_callback_.IsCancelled()); | 267 DCHECK(connect_timeout_callback_.IsCancelled()); |
| 276 connect_timeout_callback_.Reset( | 268 connect_timeout_callback_.Reset( |
| 277 base::Bind(&CastSocketImpl::OnConnectTimeout, base::Unretained(this))); | 269 base::Bind(&CastSocketImpl::OnConnectTimeout, base::Unretained(this))); |
| 278 GetTimer()->Start(FROM_HERE, | 270 GetTimer()->Start(FROM_HERE, |
| 279 connect_timeout_, | 271 connect_timeout_, |
| 280 connect_timeout_callback_.callback()); | 272 connect_timeout_callback_.callback()); |
| 281 } | 273 } |
| 282 | 274 |
| 283 DoConnectLoop(net::OK); | 275 DoConnectLoop(net::OK); |
| 284 } | 276 } |
| 285 | 277 |
| 286 CastTransport* CastSocketImpl::transport() const { | 278 CastTransport* CastSocketImpl::transport() const { |
| 287 return transport_.get(); | 279 return transport_.get(); |
| 288 } | 280 } |
| 289 | 281 |
| 290 void CastSocketImpl::OnConnectTimeout() { | 282 void CastSocketImpl::OnConnectTimeout() { |
| 291 DCHECK(CalledOnValidThread()); | 283 DCHECK(CalledOnValidThread()); |
| 292 // Stop all pending connection setup tasks and report back to the client. | 284 // Stop all pending connection setup tasks and report back to the client. |
| 293 is_canceled_ = true; | 285 is_canceled_ = true; |
| 294 logger_->LogSocketEvent(channel_id_, proto::CONNECT_TIMED_OUT); | |
| 295 VLOG_WITH_CONNECTION(1) << "Timeout while establishing a connection."; | 286 VLOG_WITH_CONNECTION(1) << "Timeout while establishing a connection."; |
| 296 SetErrorState(CHANNEL_ERROR_CONNECT_TIMEOUT); | 287 SetErrorState(CHANNEL_ERROR_CONNECT_TIMEOUT); |
| 297 DoConnectCallback(); | 288 DoConnectCallback(); |
| 298 } | 289 } |
| 299 | 290 |
| 300 void CastSocketImpl::PostTaskToStartConnectLoop(int result) { | 291 void CastSocketImpl::PostTaskToStartConnectLoop(int result) { |
| 301 DCHECK(CalledOnValidThread()); | 292 DCHECK(CalledOnValidThread()); |
| 302 DCHECK(connect_loop_callback_.IsCancelled()); | 293 DCHECK(connect_loop_callback_.IsCancelled()); |
| 303 connect_loop_callback_.Reset(base::Bind(&CastSocketImpl::DoConnectLoop, | 294 connect_loop_callback_.Reset(base::Bind(&CastSocketImpl::DoConnectLoop, |
| 304 base::Unretained(this), result)); | 295 base::Unretained(this), result)); |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 355 SetErrorState(CHANNEL_ERROR_UNKNOWN); | 346 SetErrorState(CHANNEL_ERROR_UNKNOWN); |
| 356 DoConnectCallback(); | 347 DoConnectCallback(); |
| 357 return; | 348 return; |
| 358 } | 349 } |
| 359 } while (rv != net::ERR_IO_PENDING && !IsTerminalState(connect_state_)); | 350 } while (rv != net::ERR_IO_PENDING && !IsTerminalState(connect_state_)); |
| 360 // Exit the state machine if an asynchronous network operation is pending | 351 // Exit the state machine if an asynchronous network operation is pending |
| 361 // or if the state machine is in the terminal "finished" state. | 352 // or if the state machine is in the terminal "finished" state. |
| 362 | 353 |
| 363 if (IsTerminalState(connect_state_)) { | 354 if (IsTerminalState(connect_state_)) { |
| 364 DCHECK_NE(rv, net::ERR_IO_PENDING); | 355 DCHECK_NE(rv, net::ERR_IO_PENDING); |
| 365 logger_->LogSocketConnectState(channel_id_, connect_state_); | |
| 366 GetTimer()->Stop(); | 356 GetTimer()->Stop(); |
| 367 DoConnectCallback(); | 357 DoConnectCallback(); |
| 368 } else { | 358 } else { |
| 369 DCHECK_EQ(rv, net::ERR_IO_PENDING); | 359 DCHECK_EQ(rv, net::ERR_IO_PENDING); |
| 370 } | 360 } |
| 371 } | 361 } |
| 372 | 362 |
| 373 int CastSocketImpl::DoTcpConnect() { | 363 int CastSocketImpl::DoTcpConnect() { |
| 374 DCHECK(connect_loop_callback_.IsCancelled()); | 364 DCHECK(connect_loop_callback_.IsCancelled()); |
| 375 VLOG_WITH_CONNECTION(1) << "DoTcpConnect"; | 365 VLOG_WITH_CONNECTION(1) << "DoTcpConnect"; |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 492 | 482 |
| 493 void CastSocketImpl::AuthTransportDelegate::OnError(ChannelError error_state) { | 483 void CastSocketImpl::AuthTransportDelegate::OnError(ChannelError error_state) { |
| 494 error_state_ = error_state; | 484 error_state_ = error_state; |
| 495 socket_->PostTaskToStartConnectLoop(net::ERR_CONNECTION_FAILED); | 485 socket_->PostTaskToStartConnectLoop(net::ERR_CONNECTION_FAILED); |
| 496 } | 486 } |
| 497 | 487 |
| 498 void CastSocketImpl::AuthTransportDelegate::OnMessage( | 488 void CastSocketImpl::AuthTransportDelegate::OnMessage( |
| 499 const CastMessage& message) { | 489 const CastMessage& message) { |
| 500 if (!IsAuthMessage(message)) { | 490 if (!IsAuthMessage(message)) { |
| 501 error_state_ = CHANNEL_ERROR_TRANSPORT_ERROR; | 491 error_state_ = CHANNEL_ERROR_TRANSPORT_ERROR; |
| 502 socket_->logger_->LogSocketEvent(socket_->channel_id_, | |
| 503 proto::AUTH_CHALLENGE_REPLY_INVALID); | |
| 504 socket_->PostTaskToStartConnectLoop(net::ERR_INVALID_RESPONSE); | 492 socket_->PostTaskToStartConnectLoop(net::ERR_INVALID_RESPONSE); |
| 505 } else { | 493 } else { |
| 506 socket_->challenge_reply_.reset(new CastMessage(message)); | 494 socket_->challenge_reply_.reset(new CastMessage(message)); |
| 507 socket_->logger_->LogSocketEvent(socket_->channel_id_, | |
| 508 proto::RECEIVED_CHALLENGE_REPLY); | |
| 509 socket_->PostTaskToStartConnectLoop(net::OK); | 495 socket_->PostTaskToStartConnectLoop(net::OK); |
| 510 } | 496 } |
| 511 } | 497 } |
| 512 | 498 |
| 513 void CastSocketImpl::AuthTransportDelegate::Start() { | 499 void CastSocketImpl::AuthTransportDelegate::Start() { |
| 514 } | 500 } |
| 515 | 501 |
| 516 int CastSocketImpl::DoAuthChallengeReplyComplete(int result) { | 502 int CastSocketImpl::DoAuthChallengeReplyComplete(int result) { |
| 517 VLOG_WITH_CONNECTION(1) << "DoAuthChallengeReplyComplete: " << result; | 503 VLOG_WITH_CONNECTION(1) << "DoAuthChallengeReplyComplete: " << result; |
| 518 | 504 |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 579 if (GetTimer()) { | 565 if (GetTimer()) { |
| 580 GetTimer()->Stop(); | 566 GetTimer()->Stop(); |
| 581 } | 567 } |
| 582 | 568 |
| 583 // Cancel callbacks that we queued ourselves to re-enter the connect or read | 569 // Cancel callbacks that we queued ourselves to re-enter the connect or read |
| 584 // loops. | 570 // loops. |
| 585 connect_loop_callback_.Cancel(); | 571 connect_loop_callback_.Cancel(); |
| 586 send_auth_challenge_callback_.Cancel(); | 572 send_auth_challenge_callback_.Cancel(); |
| 587 connect_timeout_callback_.Cancel(); | 573 connect_timeout_callback_.Cancel(); |
| 588 SetReadyState(READY_STATE_CLOSED); | 574 SetReadyState(READY_STATE_CLOSED); |
| 589 logger_->LogSocketEvent(channel_id_, proto::SOCKET_CLOSED); | |
| 590 } | 575 } |
| 591 | 576 |
| 592 bool CastSocketImpl::CalledOnValidThread() const { | 577 bool CastSocketImpl::CalledOnValidThread() const { |
| 593 return thread_checker_.CalledOnValidThread(); | 578 return thread_checker_.CalledOnValidThread(); |
| 594 } | 579 } |
| 595 | 580 |
| 596 base::Timer* CastSocketImpl::GetTimer() { | 581 base::Timer* CastSocketImpl::GetTimer() { |
| 597 return connect_timeout_timer_.get(); | 582 return connect_timeout_timer_.get(); |
| 598 } | 583 } |
| 599 | 584 |
| 600 void CastSocketImpl::SetConnectState(proto::ConnectionState connect_state) { | 585 void CastSocketImpl::SetConnectState(proto::ConnectionState connect_state) { |
| 601 if (connect_state_ != connect_state) { | 586 if (connect_state_ != connect_state) { |
| 602 connect_state_ = connect_state; | 587 connect_state_ = connect_state; |
| 603 logger_->LogSocketConnectState(channel_id_, connect_state_); | |
| 604 } | 588 } |
| 605 } | 589 } |
| 606 | 590 |
| 607 void CastSocketImpl::SetReadyState(ReadyState ready_state) { | 591 void CastSocketImpl::SetReadyState(ReadyState ready_state) { |
| 608 if (ready_state_ != ready_state) { | 592 if (ready_state_ != ready_state) |
| 609 ready_state_ = ready_state; | 593 ready_state_ = ready_state; |
| 610 logger_->LogSocketReadyState(channel_id_, ReadyStateToProto(ready_state_)); | |
| 611 } | |
| 612 } | 594 } |
| 613 | 595 |
| 614 void CastSocketImpl::SetErrorState(ChannelError error_state) { | 596 void CastSocketImpl::SetErrorState(ChannelError error_state) { |
| 615 VLOG_WITH_CONNECTION(1) << "SetErrorState " << error_state; | 597 VLOG_WITH_CONNECTION(1) << "SetErrorState " << error_state; |
| 616 DCHECK_EQ(CHANNEL_ERROR_NONE, error_state_); | 598 DCHECK_EQ(CHANNEL_ERROR_NONE, error_state_); |
| 617 error_state_ = error_state; | 599 error_state_ = error_state; |
| 618 logger_->LogSocketErrorState(channel_id_, ErrorStateToProto(error_state_)); | |
| 619 delegate_->OnError(error_state_); | 600 delegate_->OnError(error_state_); |
| 620 } | 601 } |
| 621 } // namespace cast_channel | 602 } // namespace cast_channel |
| 622 } // namespace api | 603 } // namespace api |
| 623 } // namespace extensions | 604 } // namespace extensions |
| 624 #undef VLOG_WITH_CONNECTION | 605 #undef VLOG_WITH_CONNECTION |
| OLD | NEW |