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