Matt Menke | 7b505107 | 2019-01-27 21:22:49 | [diff] [blame] | 1 | // Copyright (c) 2012 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 | #ifndef NET_SOCKET_SSL_CONNECT_JOB_H_ |
| 6 | #define NET_SOCKET_SSL_CONNECT_JOB_H_ |
| 7 | |
David Benjamin | dc5fd6a | 2022-03-24 23:00:41 | [diff] [blame] | 8 | #include <stdint.h> |
| 9 | |
Matt Menke | 7b505107 | 2019-01-27 21:22:49 | [diff] [blame] | 10 | #include <memory> |
Eric Orth | ac66191 | 2022-01-10 21:44:17 | [diff] [blame] | 11 | #include <set> |
Matt Menke | 7b505107 | 2019-01-27 21:22:49 | [diff] [blame] | 12 | #include <string> |
David Benjamin | dc5fd6a | 2022-03-24 23:00:41 | [diff] [blame] | 13 | #include <vector> |
Matt Menke | 7b505107 | 2019-01-27 21:22:49 | [diff] [blame] | 14 | |
Matt Menke | 7b505107 | 2019-01-27 21:22:49 | [diff] [blame] | 15 | #include "base/memory/ref_counted.h" |
| 16 | #include "base/time/time.h" |
| 17 | #include "net/base/completion_once_callback.h" |
| 18 | #include "net/base/completion_repeating_callback.h" |
| 19 | #include "net/base/net_export.h" |
David Benjamin | 6f2da65 | 2019-06-26 23:36:35 | [diff] [blame] | 20 | #include "net/base/network_isolation_key.h" |
Matt Menke | 7b505107 | 2019-01-27 21:22:49 | [diff] [blame] | 21 | #include "net/base/privacy_mode.h" |
Yoichiro Hibara | 6a4e282 | 2022-09-05 04:20:02 | [diff] [blame] | 22 | #include "net/dns/public/host_resolver_results.h" |
dalyk | edd30d98 | 2019-12-16 15:31:10 | [diff] [blame] | 23 | #include "net/dns/public/resolve_error_info.h" |
Matt Menke | 7b505107 | 2019-01-27 21:22:49 | [diff] [blame] | 24 | #include "net/socket/connect_job.h" |
| 25 | #include "net/socket/connection_attempts.h" |
| 26 | #include "net/socket/ssl_client_socket.h" |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 27 | #include "net/ssl/ssl_cert_request_info.h" |
Matt Menke | 7b505107 | 2019-01-27 21:22:49 | [diff] [blame] | 28 | #include "net/ssl/ssl_config_service.h" |
David Benjamin | dc5fd6a | 2022-03-24 23:00:41 | [diff] [blame] | 29 | #include "third_party/abseil-cpp/absl/types/optional.h" |
Matt Menke | 7b505107 | 2019-01-27 21:22:49 | [diff] [blame] | 30 | |
| 31 | namespace net { |
| 32 | |
Matt Menke | 7b505107 | 2019-01-27 21:22:49 | [diff] [blame] | 33 | class HostPortPair; |
Matt Menke | 7b505107 | 2019-01-27 21:22:49 | [diff] [blame] | 34 | class HttpProxySocketParams; |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 35 | class SocketTag; |
Matt Menke | 7b505107 | 2019-01-27 21:22:49 | [diff] [blame] | 36 | class SOCKSSocketParams; |
Matt Menke | 7b505107 | 2019-01-27 21:22:49 | [diff] [blame] | 37 | class TransportSocketParams; |
| 38 | |
| 39 | class NET_EXPORT_PRIVATE SSLSocketParams |
| 40 | : public base::RefCounted<SSLSocketParams> { |
| 41 | public: |
| 42 | enum ConnectionType { DIRECT, SOCKS_PROXY, HTTP_PROXY }; |
| 43 | |
| 44 | // Exactly one of |direct_params|, |socks_proxy_params|, and |
| 45 | // |http_proxy_params| must be non-NULL. |
Matt Menke | 1bbe89a | 2019-03-25 18:43:56 | [diff] [blame] | 46 | SSLSocketParams(scoped_refptr<TransportSocketParams> direct_params, |
| 47 | scoped_refptr<SOCKSSocketParams> socks_proxy_params, |
| 48 | scoped_refptr<HttpProxySocketParams> http_proxy_params, |
Matt Menke | 7b505107 | 2019-01-27 21:22:49 | [diff] [blame] | 49 | const HostPortPair& host_and_port, |
| 50 | const SSLConfig& ssl_config, |
David Benjamin | 6f2da65 | 2019-06-26 23:36:35 | [diff] [blame] | 51 | PrivacyMode privacy_mode, |
| 52 | NetworkIsolationKey network_isolation_key); |
Matt Menke | 7b505107 | 2019-01-27 21:22:49 | [diff] [blame] | 53 | |
Peter Boström | 407869b | 2021-10-07 04:42:48 | [diff] [blame] | 54 | SSLSocketParams(const SSLSocketParams&) = delete; |
| 55 | SSLSocketParams& operator=(const SSLSocketParams&) = delete; |
| 56 | |
Matt Menke | 7b505107 | 2019-01-27 21:22:49 | [diff] [blame] | 57 | // Returns the type of the underlying connection. |
| 58 | ConnectionType GetConnectionType() const; |
| 59 | |
| 60 | // Must be called only when GetConnectionType() returns DIRECT. |
| 61 | const scoped_refptr<TransportSocketParams>& GetDirectConnectionParams() const; |
| 62 | |
| 63 | // Must be called only when GetConnectionType() returns SOCKS_PROXY. |
| 64 | const scoped_refptr<SOCKSSocketParams>& GetSocksProxyConnectionParams() const; |
| 65 | |
| 66 | // Must be called only when GetConnectionType() returns HTTP_PROXY. |
| 67 | const scoped_refptr<HttpProxySocketParams>& GetHttpProxyConnectionParams() |
| 68 | const; |
| 69 | |
| 70 | const HostPortPair& host_and_port() const { return host_and_port_; } |
| 71 | const SSLConfig& ssl_config() const { return ssl_config_; } |
| 72 | PrivacyMode privacy_mode() const { return privacy_mode_; } |
David Benjamin | 6f2da65 | 2019-06-26 23:36:35 | [diff] [blame] | 73 | const NetworkIsolationKey& network_isolation_key() const { |
| 74 | return network_isolation_key_; |
| 75 | } |
Matt Menke | 7b505107 | 2019-01-27 21:22:49 | [diff] [blame] | 76 | |
| 77 | private: |
| 78 | friend class base::RefCounted<SSLSocketParams>; |
| 79 | ~SSLSocketParams(); |
| 80 | |
| 81 | const scoped_refptr<TransportSocketParams> direct_params_; |
| 82 | const scoped_refptr<SOCKSSocketParams> socks_proxy_params_; |
| 83 | const scoped_refptr<HttpProxySocketParams> http_proxy_params_; |
| 84 | const HostPortPair host_and_port_; |
| 85 | const SSLConfig ssl_config_; |
| 86 | const PrivacyMode privacy_mode_; |
David Benjamin | 6f2da65 | 2019-06-26 23:36:35 | [diff] [blame] | 87 | const NetworkIsolationKey network_isolation_key_; |
Matt Menke | 7b505107 | 2019-01-27 21:22:49 | [diff] [blame] | 88 | }; |
| 89 | |
| 90 | // SSLConnectJob establishes a connection, through a proxy if needed, and then |
| 91 | // handles the SSL handshake. It returns an SSLClientSocket on success. |
Matt Menke | 9d5e2c9 | 2019-02-05 01:42:23 | [diff] [blame] | 92 | class NET_EXPORT_PRIVATE SSLConnectJob : public ConnectJob, |
| 93 | public ConnectJob::Delegate { |
Matt Menke | 7b505107 | 2019-01-27 21:22:49 | [diff] [blame] | 94 | public: |
Eric Orth | a9b8be0 | 2021-06-29 23:09:08 | [diff] [blame] | 95 | class NET_EXPORT_PRIVATE Factory { |
| 96 | public: |
| 97 | Factory() = default; |
| 98 | virtual ~Factory() = default; |
| 99 | |
| 100 | virtual std::unique_ptr<SSLConnectJob> Create( |
| 101 | RequestPriority priority, |
| 102 | const SocketTag& socket_tag, |
| 103 | const CommonConnectJobParams* common_connect_job_params, |
| 104 | scoped_refptr<SSLSocketParams> params, |
| 105 | ConnectJob::Delegate* delegate, |
| 106 | const NetLogWithSource* net_log); |
| 107 | }; |
| 108 | |
Matt Menke | 7b505107 | 2019-01-27 21:22:49 | [diff] [blame] | 109 | // Note: the SSLConnectJob does not own |messenger| so it must outlive the |
| 110 | // job. |
Matt Menke | cb77b540 | 2019-01-28 17:11:23 | [diff] [blame] | 111 | SSLConnectJob(RequestPriority priority, |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 112 | const SocketTag& socket_tag, |
| 113 | const CommonConnectJobParams* common_connect_job_params, |
Matt Menke | 1bbe89a | 2019-03-25 18:43:56 | [diff] [blame] | 114 | scoped_refptr<SSLSocketParams> params, |
Matt Menke | 1a6c92d | 2019-02-23 00:25:38 | [diff] [blame] | 115 | ConnectJob::Delegate* delegate, |
| 116 | const NetLogWithSource* net_log); |
Peter Boström | 293b134 | 2021-09-22 17:31:43 | [diff] [blame] | 117 | |
| 118 | SSLConnectJob(const SSLConnectJob&) = delete; |
| 119 | SSLConnectJob& operator=(const SSLConnectJob&) = delete; |
| 120 | |
Matt Menke | 7b505107 | 2019-01-27 21:22:49 | [diff] [blame] | 121 | ~SSLConnectJob() override; |
| 122 | |
| 123 | // ConnectJob methods. |
| 124 | LoadState GetLoadState() const override; |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 125 | bool HasEstablishedConnection() const override; |
Matt Menke | 7b505107 | 2019-01-27 21:22:49 | [diff] [blame] | 126 | |
Matt Menke | 9d5e2c9 | 2019-02-05 01:42:23 | [diff] [blame] | 127 | // ConnectJob::Delegate methods. |
| 128 | void OnConnectJobComplete(int result, ConnectJob* job) override; |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 129 | void OnNeedsProxyAuth(const HttpResponseInfo& response, |
| 130 | HttpAuthController* auth_controller, |
| 131 | base::OnceClosure restart_with_auth_callback, |
| 132 | ConnectJob* job) override; |
Matt Menke | 6030ed9f | 2019-04-11 20:25:55 | [diff] [blame] | 133 | ConnectionAttempts GetConnectionAttempts() const override; |
dalyk | edd30d98 | 2019-12-16 15:31:10 | [diff] [blame] | 134 | ResolveErrorInfo GetResolveErrorInfo() const override; |
Matt Menke | 6f84d1f1 | 2019-04-11 19:26:47 | [diff] [blame] | 135 | bool IsSSLError() const override; |
| 136 | scoped_refptr<SSLCertRequestInfo> GetCertRequestInfo() override; |
Matt Menke | 7b505107 | 2019-01-27 21:22:49 | [diff] [blame] | 137 | |
Matt Menke | 36eaf5c | 2019-04-02 16:15:52 | [diff] [blame] | 138 | // Returns the timeout for the SSL handshake. This is the same for all |
| 139 | // connections regardless of whether or not there is a proxy in use. |
| 140 | static base::TimeDelta HandshakeTimeoutForTesting(); |
| 141 | |
Matt Menke | 7b505107 | 2019-01-27 21:22:49 | [diff] [blame] | 142 | private: |
| 143 | enum State { |
| 144 | STATE_TRANSPORT_CONNECT, |
| 145 | STATE_TRANSPORT_CONNECT_COMPLETE, |
| 146 | STATE_SOCKS_CONNECT, |
| 147 | STATE_SOCKS_CONNECT_COMPLETE, |
| 148 | STATE_TUNNEL_CONNECT, |
| 149 | STATE_TUNNEL_CONNECT_COMPLETE, |
| 150 | STATE_SSL_CONNECT, |
| 151 | STATE_SSL_CONNECT_COMPLETE, |
| 152 | STATE_NONE, |
| 153 | }; |
| 154 | |
| 155 | void OnIOComplete(int result); |
| 156 | |
| 157 | // Runs the state transition loop. |
| 158 | int DoLoop(int result); |
| 159 | |
| 160 | int DoTransportConnect(); |
| 161 | int DoTransportConnectComplete(int result); |
| 162 | int DoSOCKSConnect(); |
| 163 | int DoSOCKSConnectComplete(int result); |
| 164 | int DoTunnelConnect(); |
| 165 | int DoTunnelConnectComplete(int result); |
| 166 | int DoSSLConnect(); |
| 167 | int DoSSLConnectComplete(int result); |
| 168 | |
| 169 | // Returns the initial state for the state machine based on the |
| 170 | // |connection_type|. |
| 171 | static State GetInitialState(SSLSocketParams::ConnectionType connection_type); |
| 172 | |
| 173 | // Starts the SSL connection process. Returns OK on success and |
| 174 | // ERR_IO_PENDING if it cannot immediately service the request. |
| 175 | // Otherwise, it returns a net error code. |
| 176 | int ConnectInternal() override; |
| 177 | |
David Benjamin | 07a07d65 | 2020-02-26 22:26:59 | [diff] [blame] | 178 | void ResetStateForRestart(); |
| 179 | |
Matt Menke | 7b505107 | 2019-01-27 21:22:49 | [diff] [blame] | 180 | void ChangePriorityInternal(RequestPriority priority) override; |
| 181 | |
| 182 | scoped_refptr<SSLSocketParams> params_; |
Matt Menke | 7b505107 | 2019-01-27 21:22:49 | [diff] [blame] | 183 | |
| 184 | State next_state_; |
| 185 | CompletionRepeatingCallback callback_; |
Matt Menke | 9d5e2c9 | 2019-02-05 01:42:23 | [diff] [blame] | 186 | std::unique_ptr<ConnectJob> nested_connect_job_; |
| 187 | std::unique_ptr<StreamSocket> nested_socket_; |
Matt Menke | 7b505107 | 2019-01-27 21:22:49 | [diff] [blame] | 188 | std::unique_ptr<SSLClientSocket> ssl_socket_; |
| 189 | |
Matt Menke | c1ae1d5 | 2019-04-10 19:28:27 | [diff] [blame] | 190 | // True once SSL negotiation has started. |
Tsuyoshi Horo | 2ec06e00 | 2022-06-09 01:38:59 | [diff] [blame] | 191 | bool ssl_negotiation_started_ = false; |
Matt Menke | c1ae1d5 | 2019-04-10 19:28:27 | [diff] [blame] | 192 | |
David Benjamin | 07a07d65 | 2020-02-26 22:26:59 | [diff] [blame] | 193 | // True if legacy crypto should be disabled for the job's current connection |
| 194 | // attempt. On error, the connection will be retried with legacy crypto |
| 195 | // enabled. |
Tsuyoshi Horo | 2ec06e00 | 2022-06-09 01:38:59 | [diff] [blame] | 196 | bool disable_legacy_crypto_with_fallback_ = true; |
David Benjamin | 07a07d65 | 2020-02-26 22:26:59 | [diff] [blame] | 197 | |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 198 | scoped_refptr<SSLCertRequestInfo> ssl_cert_request_info_; |
Matt Menke | 7b505107 | 2019-01-27 21:22:49 | [diff] [blame] | 199 | |
Matt Menke | 7b505107 | 2019-01-27 21:22:49 | [diff] [blame] | 200 | ConnectionAttempts connection_attempts_; |
dalyk | edd30d98 | 2019-12-16 15:31:10 | [diff] [blame] | 201 | ResolveErrorInfo resolve_error_info_; |
Matt Menke | 7b505107 | 2019-01-27 21:22:49 | [diff] [blame] | 202 | // The address of the server the connect job is connected to. Populated if |
| 203 | // and only if the connect job is connected *directly* to the server (not |
| 204 | // through an HTTPS CONNECT request or a SOCKS proxy). |
| 205 | IPEndPoint server_address_; |
| 206 | |
Eric Orth | ac66191 | 2022-01-10 21:44:17 | [diff] [blame] | 207 | // Any DNS aliases for the remote endpoint. Includes all known aliases, e.g. |
| 208 | // from A, AAAA, or HTTPS, not just from the address used for the connection, |
| 209 | // in no particular order. Stored because `nested_connect_job_` has a limited |
| 210 | // lifetime and the aliases can no longer be retrieved from there by by the |
| 211 | // time that the aliases are needed to be passed in SetSocket. |
| 212 | std::set<std::string> dns_aliases_; |
David Benjamin | dc5fd6a | 2022-03-24 23:00:41 | [diff] [blame] | 213 | |
| 214 | // The endpoint result used by `nested_connect_job_`. Stored because |
| 215 | // `nested_connect_job_` has a limited lifetime. |
| 216 | absl::optional<HostResolverEndpointResult> endpoint_result_; |
| 217 | |
| 218 | // If not `absl::nullopt`, the ECH retry configs to use in the ECH recovery |
| 219 | // flow. `endpoint_result_` will then contain the endpoint to reconnect to. |
| 220 | absl::optional<std::vector<uint8_t>> ech_retry_configs_; |
Matt Menke | 7b505107 | 2019-01-27 21:22:49 | [diff] [blame] | 221 | }; |
| 222 | |
| 223 | } // namespace net |
| 224 | |
| 225 | #endif // NET_SOCKET_SSL_CONNECT_JOB_H_ |