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