| 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 "components/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 "components/cast_channel/cast_auth_util.h" |
| 26 #include "extensions/browser/api/cast_channel/cast_framer.h" | 26 #include "components/cast_channel/cast_framer.h" |
| 27 #include "extensions/browser/api/cast_channel/cast_message_util.h" | 27 #include "components/cast_channel/cast_message_util.h" |
| 28 #include "extensions/browser/api/cast_channel/cast_transport.h" | 28 #include "components/cast_channel/cast_transport.h" |
| 29 #include "extensions/browser/api/cast_channel/logger.h" | 29 #include "components/cast_channel/logger.h" |
| 30 #include "extensions/common/api/cast_channel/cast_channel.pb.h" | 30 #include "components/cast_channel/proto/cast_channel.pb.h" |
| 31 #include "net/base/address_list.h" | 31 #include "net/base/address_list.h" |
| 32 #include "net/base/host_port_pair.h" | 32 #include "net/base/host_port_pair.h" |
| 33 #include "net/base/net_errors.h" | 33 #include "net/base/net_errors.h" |
| 34 #include "net/cert/cert_verifier.h" | 34 #include "net/cert/cert_verifier.h" |
| 35 #include "net/cert/cert_verify_result.h" | 35 #include "net/cert/cert_verify_result.h" |
| 36 #include "net/cert/ct_policy_enforcer.h" | 36 #include "net/cert/ct_policy_enforcer.h" |
| 37 #include "net/cert/multi_log_ct_verifier.h" | 37 #include "net/cert/multi_log_ct_verifier.h" |
| 38 #include "net/cert/x509_certificate.h" | 38 #include "net/cert/x509_certificate.h" |
| 39 #include "net/http/transport_security_state.h" | 39 #include "net/http/transport_security_state.h" |
| 40 #include "net/log/net_log.h" | 40 #include "net/log/net_log.h" |
| 41 #include "net/log/net_log_source_type.h" | 41 #include "net/log/net_log_source_type.h" |
| 42 #include "net/socket/client_socket_factory.h" | 42 #include "net/socket/client_socket_factory.h" |
| 43 #include "net/socket/client_socket_handle.h" | 43 #include "net/socket/client_socket_handle.h" |
| 44 #include "net/socket/ssl_client_socket.h" | 44 #include "net/socket/ssl_client_socket.h" |
| 45 #include "net/socket/stream_socket.h" | 45 #include "net/socket/stream_socket.h" |
| 46 #include "net/socket/tcp_client_socket.h" | 46 #include "net/socket/tcp_client_socket.h" |
| 47 #include "net/ssl/ssl_config_service.h" | 47 #include "net/ssl/ssl_config_service.h" |
| 48 #include "net/ssl/ssl_info.h" | 48 #include "net/ssl/ssl_info.h" |
| 49 | 49 |
| 50 // Helper for logging data with remote host IP and authentication state. | 50 // Helper for logging data with remote host IP and authentication state. |
| 51 // Assumes |ip_endpoint_| of type net::IPEndPoint and |channel_auth_| of enum | 51 // Assumes |ip_endpoint_| of type net::IPEndPoint and |channel_auth_| of enum |
| 52 // type ChannelAuthType are available in the current scope. | 52 // type ChannelAuthType are available in the current scope. |
| 53 #define CONNECTION_INFO() \ | 53 #define CONNECTION_INFO() \ |
| 54 "[" << ip_endpoint_.ToString() \ | 54 "[" << ip_endpoint_.ToString() \ |
| 55 << ", auth=" << ::cast_channel::ChannelAuthTypeToString(channel_auth_) \ | 55 << ", auth=" << ::cast_channel::ChannelAuthTypeToString(channel_auth_) \ |
| 56 << "] " | 56 << "] " |
| 57 #define VLOG_WITH_CONNECTION(level) VLOG(level) << CONNECTION_INFO() | 57 #define VLOG_WITH_CONNECTION(level) VLOG(level) << CONNECTION_INFO() |
| 58 #define LOG_WITH_CONNECTION(level) LOG(level) << CONNECTION_INFO() | 58 #define LOG_WITH_CONNECTION(level) LOG(level) << CONNECTION_INFO() |
| 59 | 59 |
| 60 namespace extensions { | |
| 61 namespace api { | |
| 62 namespace cast_channel { | 60 namespace cast_channel { |
| 63 namespace { | 61 namespace { |
| 64 | 62 |
| 65 bool IsTerminalState(proto::ConnectionState state) { | 63 bool IsTerminalState(proto::ConnectionState state) { |
| 66 return state == proto::CONN_STATE_FINISHED || | 64 return state == proto::CONN_STATE_FINISHED || |
| 67 state == proto::CONN_STATE_ERROR || state == proto::CONN_STATE_TIMEOUT; | 65 state == proto::CONN_STATE_ERROR || state == proto::CONN_STATE_TIMEOUT; |
| 68 } | 66 } |
| 69 | 67 |
| 70 // Cert verifier which blindly accepts all certificates, regardless of validity. | 68 // Cert verifier which blindly accepts all certificates, regardless of validity. |
| 71 class FakeCertVerifier : public net::CertVerifier { | 69 class FakeCertVerifier : public net::CertVerifier { |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 200 // Note that |context| fields remain owned by CastSocketImpl. | 198 // Note that |context| fields remain owned by CastSocketImpl. |
| 201 net::SSLClientSocketContext context; | 199 net::SSLClientSocketContext context; |
| 202 context.cert_verifier = cert_verifier_.get(); | 200 context.cert_verifier = cert_verifier_.get(); |
| 203 context.transport_security_state = transport_security_state_.get(); | 201 context.transport_security_state = transport_security_state_.get(); |
| 204 context.cert_transparency_verifier = cert_transparency_verifier_.get(); | 202 context.cert_transparency_verifier = cert_transparency_verifier_.get(); |
| 205 context.ct_policy_enforcer = ct_policy_enforcer_.get(); | 203 context.ct_policy_enforcer = ct_policy_enforcer_.get(); |
| 206 | 204 |
| 207 std::unique_ptr<net::ClientSocketHandle> connection( | 205 std::unique_ptr<net::ClientSocketHandle> connection( |
| 208 new net::ClientSocketHandle); | 206 new net::ClientSocketHandle); |
| 209 connection->SetSocket(std::move(socket)); | 207 connection->SetSocket(std::move(socket)); |
| 210 net::HostPortPair host_and_port = net::HostPortPair::FromIPEndPoint( | 208 net::HostPortPair host_and_port = |
| 211 ip_endpoint_); | 209 net::HostPortPair::FromIPEndPoint(ip_endpoint_); |
| 212 | 210 |
| 213 return net::ClientSocketFactory::GetDefaultFactory()->CreateSSLClientSocket( | 211 return net::ClientSocketFactory::GetDefaultFactory()->CreateSSLClientSocket( |
| 214 std::move(connection), host_and_port, ssl_config, context); | 212 std::move(connection), host_and_port, ssl_config, context); |
| 215 } | 213 } |
| 216 | 214 |
| 217 scoped_refptr<net::X509Certificate> CastSocketImpl::ExtractPeerCert() { | 215 scoped_refptr<net::X509Certificate> CastSocketImpl::ExtractPeerCert() { |
| 218 net::SSLInfo ssl_info; | 216 net::SSLInfo ssl_info; |
| 219 if (!socket_->GetSSLInfo(&ssl_info) || !ssl_info.cert.get()) | 217 if (!socket_->GetSSLInfo(&ssl_info) || !ssl_info.cert.get()) |
| 220 return nullptr; | 218 return nullptr; |
| 221 | 219 |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 268 | 266 |
| 269 connect_callback_ = callback; | 267 connect_callback_ = callback; |
| 270 SetReadyState(ReadyState::CONNECTING); | 268 SetReadyState(ReadyState::CONNECTING); |
| 271 SetConnectState(proto::CONN_STATE_TCP_CONNECT); | 269 SetConnectState(proto::CONN_STATE_TCP_CONNECT); |
| 272 | 270 |
| 273 // Set up connection timeout. | 271 // Set up connection timeout. |
| 274 if (connect_timeout_.InMicroseconds() > 0) { | 272 if (connect_timeout_.InMicroseconds() > 0) { |
| 275 DCHECK(connect_timeout_callback_.IsCancelled()); | 273 DCHECK(connect_timeout_callback_.IsCancelled()); |
| 276 connect_timeout_callback_.Reset( | 274 connect_timeout_callback_.Reset( |
| 277 base::Bind(&CastSocketImpl::OnConnectTimeout, base::Unretained(this))); | 275 base::Bind(&CastSocketImpl::OnConnectTimeout, base::Unretained(this))); |
| 278 GetTimer()->Start(FROM_HERE, | 276 GetTimer()->Start(FROM_HERE, connect_timeout_, |
| 279 connect_timeout_, | |
| 280 connect_timeout_callback_.callback()); | 277 connect_timeout_callback_.callback()); |
| 281 } | 278 } |
| 282 | 279 |
| 283 DoConnectLoop(net::OK); | 280 DoConnectLoop(net::OK); |
| 284 } | 281 } |
| 285 | 282 |
| 286 CastTransport* CastSocketImpl::transport() const { | 283 CastTransport* CastSocketImpl::transport() const { |
| 287 return transport_.get(); | 284 return transport_.get(); |
| 288 } | 285 } |
| 289 | 286 |
| (...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 501 const CastMessage& message) { | 498 const CastMessage& message) { |
| 502 if (!IsAuthMessage(message)) { | 499 if (!IsAuthMessage(message)) { |
| 503 error_state_ = ChannelError::TRANSPORT_ERROR; | 500 error_state_ = ChannelError::TRANSPORT_ERROR; |
| 504 socket_->PostTaskToStartConnectLoop(net::ERR_INVALID_RESPONSE); | 501 socket_->PostTaskToStartConnectLoop(net::ERR_INVALID_RESPONSE); |
| 505 } else { | 502 } else { |
| 506 socket_->challenge_reply_.reset(new CastMessage(message)); | 503 socket_->challenge_reply_.reset(new CastMessage(message)); |
| 507 socket_->PostTaskToStartConnectLoop(net::OK); | 504 socket_->PostTaskToStartConnectLoop(net::OK); |
| 508 } | 505 } |
| 509 } | 506 } |
| 510 | 507 |
| 511 void CastSocketImpl::AuthTransportDelegate::Start() { | 508 void CastSocketImpl::AuthTransportDelegate::Start() {} |
| 512 } | |
| 513 | 509 |
| 514 int CastSocketImpl::DoAuthChallengeReplyComplete(int result) { | 510 int CastSocketImpl::DoAuthChallengeReplyComplete(int result) { |
| 515 VLOG_WITH_CONNECTION(1) << "DoAuthChallengeReplyComplete: " << result; | 511 VLOG_WITH_CONNECTION(1) << "DoAuthChallengeReplyComplete: " << result; |
| 516 | 512 |
| 517 if (auth_delegate_->error_state() != ChannelError::NONE) { | 513 if (auth_delegate_->error_state() != ChannelError::NONE) { |
| 518 SetErrorState(auth_delegate_->error_state()); | 514 SetErrorState(auth_delegate_->error_state()); |
| 519 SetConnectState(proto::CONN_STATE_ERROR); | 515 SetConnectState(proto::CONN_STATE_ERROR); |
| 520 return net::ERR_CONNECTION_FAILED; | 516 return net::ERR_CONNECTION_FAILED; |
| 521 } | 517 } |
| 522 auth_delegate_ = nullptr; | 518 auth_delegate_ = nullptr; |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 608 | 604 |
| 609 void CastSocketImpl::SetErrorState(ChannelError error_state) { | 605 void CastSocketImpl::SetErrorState(ChannelError error_state) { |
| 610 VLOG_WITH_CONNECTION(1) << "SetErrorState " | 606 VLOG_WITH_CONNECTION(1) << "SetErrorState " |
| 611 << ::cast_channel::ChannelErrorToString(error_state); | 607 << ::cast_channel::ChannelErrorToString(error_state); |
| 612 DCHECK_EQ(ChannelError::NONE, error_state_); | 608 DCHECK_EQ(ChannelError::NONE, error_state_); |
| 613 error_state_ = error_state; | 609 error_state_ = error_state; |
| 614 delegate_->OnError(error_state_); | 610 delegate_->OnError(error_state_); |
| 615 } | 611 } |
| 616 | 612 |
| 617 } // namespace cast_channel | 613 } // namespace cast_channel |
| 618 } // namespace api | |
| 619 } // namespace extensions | |
| 620 #undef VLOG_WITH_CONNECTION | 614 #undef VLOG_WITH_CONNECTION |
| OLD | NEW |