tls_connection_factory.cc 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // Copyright 2019 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. #include "components/openscreen_platform/tls_connection_factory.h"
  5. #include <openssl/pool.h>
  6. #include <utility>
  7. #include "base/notreached.h"
  8. #include "components/openscreen_platform/network_context.h"
  9. #include "components/openscreen_platform/network_util.h"
  10. #include "components/openscreen_platform/tls_client_connection.h"
  11. #include "net/base/host_port_pair.h"
  12. #include "net/base/net_errors.h"
  13. #include "net/ssl/ssl_info.h"
  14. #include "net/traffic_annotation/network_traffic_annotation.h"
  15. #include "services/network/public/mojom/network_context.mojom.h"
  16. #include "third_party/openscreen/src/platform/api/tls_connection.h"
  17. #include "third_party/openscreen/src/platform/base/tls_connect_options.h"
  18. #include "third_party/openscreen/src/platform/base/tls_credentials.h"
  19. #include "third_party/openscreen/src/platform/base/tls_listen_options.h"
  20. namespace openscreen {
  21. std::unique_ptr<TlsConnectionFactory> TlsConnectionFactory::CreateFactory(
  22. Client* client,
  23. TaskRunner* task_runner) {
  24. return std::make_unique<openscreen_platform::TlsConnectionFactory>(
  25. client, task_runner);
  26. }
  27. } // namespace openscreen
  28. namespace openscreen_platform {
  29. namespace {
  30. using openscreen::IPEndpoint;
  31. using openscreen::TlsConnectOptions;
  32. using openscreen::TlsCredentials;
  33. using openscreen::TlsListenOptions;
  34. constexpr net::NetworkTrafficAnnotationTag kTrafficAnnotation =
  35. net::DefineNetworkTrafficAnnotation("openscreen_tls_message", R"(
  36. semantics {
  37. sender: "Open Screen"
  38. description:
  39. "Open Screen TLS messages are used by the third_party Open Screen "
  40. "library, primarily for the libcast CastSocket implementation."
  41. trigger:
  42. "Any TLS encrypted message that needs to be sent or received by "
  43. "the Open Screen library."
  44. data:
  45. "Messages defined by the Open Screen Protocol specification."
  46. destination: OTHER
  47. destination_other:
  48. "The connection is made to an Open Screen endpoint on the LAN "
  49. "selected by the user, i.e. via a dialog."
  50. }
  51. policy {
  52. cookies_allowed: NO
  53. setting:
  54. "This request cannot be disabled, but it would not be sent if user "
  55. "does not connect to a Open Screen endpoint on the local network."
  56. policy_exception_justification: "Not implemented."
  57. })");
  58. } // namespace
  59. TlsConnectionFactory::~TlsConnectionFactory() = default;
  60. void TlsConnectionFactory::Connect(const IPEndpoint& remote_address,
  61. const TlsConnectOptions& options) {
  62. network::mojom::NetworkContext* network_context =
  63. openscreen_platform::GetNetworkContext();
  64. if (!network_context) {
  65. client_->OnError(this, openscreen::Error::Code::kItemNotFound);
  66. return;
  67. }
  68. TcpConnectRequest request(options, remote_address,
  69. mojo::Remote<network::mojom::TCPConnectedSocket>{});
  70. const net::AddressList address_list(
  71. openscreen_platform::ToNetEndPoint(remote_address));
  72. mojo::PendingReceiver<network::mojom::TCPConnectedSocket> receiver =
  73. request.tcp_socket.BindNewPipeAndPassReceiver();
  74. network_context->CreateTCPConnectedSocket(
  75. absl::nullopt /* local_addr */, address_list,
  76. nullptr /* tcp_connected_socket_options */,
  77. net::MutableNetworkTrafficAnnotationTag(kTrafficAnnotation),
  78. std::move(receiver), mojo::NullRemote(), /* observer */
  79. base::BindOnce(&TlsConnectionFactory::OnTcpConnect,
  80. weak_factory_.GetWeakPtr(), std::move(request)));
  81. }
  82. void TlsConnectionFactory::SetListenCredentials(
  83. const TlsCredentials& credentials) {
  84. NOTIMPLEMENTED();
  85. }
  86. void TlsConnectionFactory::Listen(const IPEndpoint& local_address,
  87. const TlsListenOptions& options) {
  88. NOTIMPLEMENTED();
  89. }
  90. TlsConnectionFactory::TlsConnectionFactory(
  91. openscreen::TlsConnectionFactory::Client* client,
  92. openscreen::TaskRunner* task_runner)
  93. : client_(client), task_runner_(task_runner) {}
  94. TlsConnectionFactory::TcpConnectRequest::TcpConnectRequest(
  95. openscreen::TlsConnectOptions options_in,
  96. openscreen::IPEndpoint remote_address_in,
  97. mojo::Remote<network::mojom::TCPConnectedSocket> tcp_socket_in)
  98. : options(std::move(options_in)),
  99. remote_address(std::move(remote_address_in)),
  100. tcp_socket(std::move(tcp_socket_in)) {}
  101. TlsConnectionFactory::TcpConnectRequest::TcpConnectRequest(
  102. TcpConnectRequest&&) = default;
  103. TlsConnectionFactory::TcpConnectRequest&
  104. TlsConnectionFactory::TcpConnectRequest::operator=(TcpConnectRequest&&) =
  105. default;
  106. TlsConnectionFactory::TcpConnectRequest::~TcpConnectRequest() = default;
  107. TlsConnectionFactory::TlsUpgradeRequest::TlsUpgradeRequest(
  108. openscreen::IPEndpoint local_address_in,
  109. openscreen::IPEndpoint remote_address_in,
  110. mojo::Remote<network::mojom::TCPConnectedSocket> tcp_socket_in,
  111. mojo::Remote<network::mojom::TLSClientSocket> tls_socket_in)
  112. : local_address(std::move(local_address_in)),
  113. remote_address(std::move(remote_address_in)),
  114. tcp_socket(std::move(tcp_socket_in)),
  115. tls_socket(std::move(tls_socket_in)) {}
  116. TlsConnectionFactory::TlsUpgradeRequest::TlsUpgradeRequest(
  117. TlsUpgradeRequest&&) = default;
  118. TlsConnectionFactory::TlsUpgradeRequest&
  119. TlsConnectionFactory::TlsUpgradeRequest::operator=(TlsUpgradeRequest&&) =
  120. default;
  121. TlsConnectionFactory::TlsUpgradeRequest::~TlsUpgradeRequest() = default;
  122. void TlsConnectionFactory::OnTcpConnect(
  123. TcpConnectRequest request,
  124. int32_t net_result,
  125. const absl::optional<net::IPEndPoint>& local_address,
  126. const absl::optional<net::IPEndPoint>& remote_address,
  127. mojo::ScopedDataPipeConsumerHandle receive_stream,
  128. mojo::ScopedDataPipeProducerHandle send_stream) {
  129. // We only care about net_result, since local_address doesn't matter,
  130. // remote_address should be 1 out of 1 addresses provided in the connect
  131. // call, and the streams must be closed before upgrading is allowed.
  132. if (net_result != net::OK) {
  133. client_->OnConnectionFailed(this, request.remote_address);
  134. return;
  135. }
  136. net::HostPortPair host_port_pair = net::HostPortPair::FromIPEndPoint(
  137. openscreen_platform::ToNetEndPoint(request.remote_address));
  138. network::mojom::TLSClientSocketOptionsPtr options =
  139. network::mojom::TLSClientSocketOptions::New();
  140. options->unsafely_skip_cert_verification =
  141. request.options.unsafely_skip_certificate_validation;
  142. openscreen::IPEndpoint local_endpoint{};
  143. if (local_address) {
  144. local_endpoint =
  145. openscreen_platform::ToOpenScreenEndPoint(local_address.value());
  146. }
  147. TlsUpgradeRequest upgrade_request(
  148. std::move(local_endpoint), std::move(request.remote_address),
  149. std::move(request.tcp_socket),
  150. mojo::Remote<network::mojom::TLSClientSocket>{});
  151. network::mojom::TCPConnectedSocket* tcp_socket =
  152. upgrade_request.tcp_socket.get();
  153. mojo::PendingReceiver<network::mojom::TLSClientSocket> tls_receiver =
  154. upgrade_request.tls_socket.BindNewPipeAndPassReceiver();
  155. tcp_socket->UpgradeToTLS(
  156. host_port_pair, std::move(options),
  157. net::MutableNetworkTrafficAnnotationTag(kTrafficAnnotation),
  158. std::move(tls_receiver), mojo::NullRemote() /* observer */,
  159. base::BindOnce(&TlsConnectionFactory::OnTlsUpgrade,
  160. weak_factory_.GetWeakPtr(), std::move(upgrade_request)));
  161. }
  162. void TlsConnectionFactory::OnTlsUpgrade(
  163. TlsUpgradeRequest request,
  164. int32_t net_result,
  165. mojo::ScopedDataPipeConsumerHandle receive_stream,
  166. mojo::ScopedDataPipeProducerHandle send_stream,
  167. const absl::optional<net::SSLInfo>& ssl_info) {
  168. if (net_result != net::OK) {
  169. client_->OnConnectionFailed(this, request.remote_address);
  170. return;
  171. }
  172. auto tls_connection = std::make_unique<TlsClientConnection>(
  173. task_runner_, request.local_address, request.remote_address,
  174. std::move(receive_stream), std::move(send_stream),
  175. std::move(request.tcp_socket), std::move(request.tls_socket));
  176. CRYPTO_BUFFER* der_buffer = ssl_info.value().unverified_cert->cert_buffer();
  177. const uint8_t* data = CRYPTO_BUFFER_data(der_buffer);
  178. std::vector<uint8_t> der_x509_certificate(
  179. data, data + CRYPTO_BUFFER_len(der_buffer));
  180. client_->OnConnected(this, std::move(der_x509_certificate),
  181. std::move(tls_connection));
  182. }
  183. } // namespace openscreen_platform