Helen Li | a6d3b2c | 2018-05-08 16:09:07 | [diff] [blame] | 1 | // Copyright 2018 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 "services/network/tls_client_socket.h" |
| 6 | |
| 7 | #include <utility> |
| 8 | |
Sebastien Marchand | 6d0558fd | 2019-01-25 16:49:37 | [diff] [blame] | 9 | #include "base/bind.h" |
Helen Li | a6d3b2c | 2018-05-08 16:09:07 | [diff] [blame] | 10 | #include "base/logging.h" |
| 11 | #include "base/memory/ptr_util.h" |
| 12 | #include "net/base/net_errors.h" |
| 13 | #include "net/socket/client_socket_factory.h" |
Helen Li | a6d3b2c | 2018-05-08 16:09:07 | [diff] [blame] | 14 | #include "net/socket/ssl_client_socket.h" |
Matt Menke | 841fc41 | 2019-03-05 23:20:12 | [diff] [blame] | 15 | #include "net/socket/stream_socket.h" |
Helen Li | a6d3b2c | 2018-05-08 16:09:07 | [diff] [blame] | 16 | #include "net/ssl/ssl_config.h" |
| 17 | #include "net/ssl/ssl_config_service.h" |
| 18 | |
| 19 | namespace network { |
| 20 | |
| 21 | TLSClientSocket::TLSClientSocket( |
| 22 | mojom::TLSClientSocketRequest request, |
| 23 | mojom::SocketObserverPtr observer, |
| 24 | const net::NetworkTrafficAnnotationTag& traffic_annotation) |
| 25 | : observer_(std::move(observer)), traffic_annotation_(traffic_annotation) {} |
| 26 | |
| 27 | TLSClientSocket::~TLSClientSocket() {} |
| 28 | |
| 29 | void TLSClientSocket::Connect( |
| 30 | const net::HostPortPair& host_port_pair, |
| 31 | const net::SSLConfig& ssl_config, |
Matt Menke | 841fc41 | 2019-03-05 23:20:12 | [diff] [blame] | 32 | std::unique_ptr<net::StreamSocket> tcp_socket, |
Helen Li | a6d3b2c | 2018-05-08 16:09:07 | [diff] [blame] | 33 | const net::SSLClientSocketContext& ssl_client_socket_context, |
| 34 | net::ClientSocketFactory* socket_factory, |
John Abd-El-Malek | ebbb0df2 | 2018-08-23 19:11:32 | [diff] [blame] | 35 | mojom::TCPConnectedSocket::UpgradeToTLSCallback callback, |
| 36 | bool send_ssl_info) { |
Helen Li | a6d3b2c | 2018-05-08 16:09:07 | [diff] [blame] | 37 | connect_callback_ = std::move(callback); |
John Abd-El-Malek | ebbb0df2 | 2018-08-23 19:11:32 | [diff] [blame] | 38 | send_ssl_info_ = send_ssl_info; |
Helen Li | a6d3b2c | 2018-05-08 16:09:07 | [diff] [blame] | 39 | socket_ = socket_factory->CreateSSLClientSocket(std::move(tcp_socket), |
| 40 | host_port_pair, ssl_config, |
| 41 | ssl_client_socket_context); |
| 42 | int result = socket_->Connect(base::BindRepeating( |
| 43 | &TLSClientSocket::OnTLSConnectCompleted, base::Unretained(this))); |
| 44 | if (result != net::ERR_IO_PENDING) |
| 45 | OnTLSConnectCompleted(result); |
| 46 | } |
| 47 | |
| 48 | void TLSClientSocket::OnTLSConnectCompleted(int result) { |
| 49 | DCHECK(!connect_callback_.is_null()); |
| 50 | |
| 51 | if (result != net::OK) { |
| 52 | socket_ = nullptr; |
| 53 | std::move(connect_callback_) |
| 54 | .Run(result, mojo::ScopedDataPipeConsumerHandle(), |
John Abd-El-Malek | ebbb0df2 | 2018-08-23 19:11:32 | [diff] [blame] | 55 | mojo::ScopedDataPipeProducerHandle(), base::nullopt); |
Helen Li | a6d3b2c | 2018-05-08 16:09:07 | [diff] [blame] | 56 | return; |
| 57 | } |
| 58 | mojo::DataPipe send_pipe; |
| 59 | mojo::DataPipe receive_pipe; |
| 60 | socket_data_pump_ = std::make_unique<SocketDataPump>( |
| 61 | socket_.get(), this /*delegate*/, std::move(receive_pipe.producer_handle), |
| 62 | std::move(send_pipe.consumer_handle), traffic_annotation_); |
John Abd-El-Malek | ebbb0df2 | 2018-08-23 19:11:32 | [diff] [blame] | 63 | base::Optional<net::SSLInfo> ssl_info; |
| 64 | if (send_ssl_info_) { |
| 65 | net::SSLInfo local; |
| 66 | socket_->GetSSLInfo(&local); |
| 67 | ssl_info = std::move(local); |
| 68 | } |
Helen Li | a6d3b2c | 2018-05-08 16:09:07 | [diff] [blame] | 69 | std::move(connect_callback_) |
| 70 | .Run(net::OK, std::move(receive_pipe.consumer_handle), |
John Abd-El-Malek | ebbb0df2 | 2018-08-23 19:11:32 | [diff] [blame] | 71 | std::move(send_pipe.producer_handle), std::move(ssl_info)); |
Helen Li | a6d3b2c | 2018-05-08 16:09:07 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | void TLSClientSocket::OnNetworkReadError(int net_error) { |
| 75 | if (observer_) |
| 76 | observer_->OnReadError(net_error); |
| 77 | } |
| 78 | |
| 79 | void TLSClientSocket::OnNetworkWriteError(int net_error) { |
| 80 | if (observer_) |
| 81 | observer_->OnWriteError(net_error); |
| 82 | } |
| 83 | |
| 84 | void TLSClientSocket::OnShutdown() { |
| 85 | // Do nothing. |
| 86 | } |
| 87 | |
| 88 | } // namespace network |