tls_socket_factory.h 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. #ifndef SERVICES_NETWORK_TLS_SOCKET_FACTORY_H_
  5. #define SERVICES_NETWORK_TLS_SOCKET_FACTORY_H_
  6. #include <memory>
  7. #include <vector>
  8. #include "base/component_export.h"
  9. #include "base/memory/raw_ptr.h"
  10. #include "mojo/public/cpp/bindings/pending_receiver.h"
  11. #include "mojo/public/cpp/bindings/pending_remote.h"
  12. #include "mojo/public/cpp/bindings/unique_receiver_set.h"
  13. #include "net/socket/ssl_client_socket.h"
  14. #include "net/traffic_annotation/network_traffic_annotation.h"
  15. #include "services/network/public/mojom/tcp_socket.mojom.h"
  16. #include "services/network/public/mojom/tls_socket.mojom-forward.h"
  17. #include "third_party/abseil-cpp/absl/types/optional.h"
  18. namespace net {
  19. class ClientSocketFactory;
  20. class SSLConfigService;
  21. class StreamSocket;
  22. class URLRequestContext;
  23. } // namespace net
  24. namespace network {
  25. // Helper class that handles TLS socket requests.
  26. class COMPONENT_EXPORT(NETWORK_SERVICE) TLSSocketFactory {
  27. public:
  28. class Delegate {
  29. public:
  30. virtual const net::StreamSocket* BorrowSocket() = 0;
  31. virtual std::unique_ptr<net::StreamSocket> TakeSocket() = 0;
  32. };
  33. // See documentation of UpgradeToTLS in tcp_socket.mojom for
  34. // the semantics of the results.
  35. using UpgradeToTLSCallback =
  36. base::OnceCallback<void(int32_t net_error,
  37. mojo::ScopedDataPipeConsumerHandle receive_stream,
  38. mojo::ScopedDataPipeProducerHandle send_stream,
  39. const absl::optional<net::SSLInfo>& ssl_info)>;
  40. // Constructs a TLSSocketFactory. If |net_log| is non-null, it is used to
  41. // log NetLog events when logging is enabled. |net_log| used to must outlive
  42. // |this|. Sockets will be created using, the earliest available from:
  43. // 1) A ClientSocketFactory set on |url_request_context|'s
  44. // HttpNetworkSession::Context
  45. // 2) The default ClientSocketFactory.
  46. explicit TLSSocketFactory(net::URLRequestContext* url_request_context);
  47. TLSSocketFactory(const TLSSocketFactory&) = delete;
  48. TLSSocketFactory& operator=(const TLSSocketFactory&) = delete;
  49. virtual ~TLSSocketFactory();
  50. // Upgrades an existing socket to TLS. The previous pipes and data pump
  51. // must already have been destroyed before the call to this method.
  52. void UpgradeToTLS(
  53. Delegate* socket_delegate,
  54. const net::HostPortPair& host_port_pair,
  55. mojom::TLSClientSocketOptionsPtr socket_options,
  56. const net::MutableNetworkTrafficAnnotationTag& traffic_annotation,
  57. mojo::PendingReceiver<mojom::TLSClientSocket> receiver,
  58. mojo::PendingRemote<mojom::SocketObserver> observer,
  59. UpgradeToTLSCallback callback);
  60. private:
  61. void CreateTLSClientSocket(
  62. const net::HostPortPair& host_port_pair,
  63. mojom::TLSClientSocketOptionsPtr socket_options,
  64. mojo::PendingReceiver<mojom::TLSClientSocket> receiver,
  65. std::unique_ptr<net::StreamSocket> underlying_socket,
  66. mojo::PendingRemote<mojom::SocketObserver> observer,
  67. const net::NetworkTrafficAnnotationTag& traffic_annotation,
  68. mojom::TCPConnectedSocket::UpgradeToTLSCallback callback);
  69. // The following are used when |unsafely_skip_cert_verification| is specified
  70. // in upgrade options.
  71. std::unique_ptr<net::SSLClientContext> no_verification_ssl_client_context_;
  72. std::unique_ptr<net::CertVerifier> no_verification_cert_verifier_;
  73. std::unique_ptr<net::TransportSecurityState>
  74. no_verification_transport_security_state_;
  75. std::unique_ptr<net::CTPolicyEnforcer> no_verification_ct_policy_enforcer_;
  76. net::SSLClientContext ssl_client_context_;
  77. raw_ptr<net::ClientSocketFactory> client_socket_factory_;
  78. const raw_ptr<net::SSLConfigService> ssl_config_service_;
  79. mojo::UniqueReceiverSet<mojom::TLSClientSocket> tls_socket_receivers_;
  80. };
  81. } // namespace network
  82. #endif // SERVICES_NETWORK_TLS_SOCKET_FACTORY_H_