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" |
| 17 | #include "net/base/privacy_mode.h" |
| 18 | #include "net/http/http_response_info.h" |
| 19 | #include "net/socket/connect_job.h" |
| 20 | #include "net/socket/connection_attempts.h" |
| 21 | #include "net/socket/ssl_client_socket.h" |
| 22 | #include "net/ssl/ssl_config_service.h" |
| 23 | |
| 24 | namespace net { |
| 25 | |
Matt Menke | 7b505107 | 2019-01-27 21:22:49 | [diff] [blame] | 26 | class HostPortPair; |
Matt Menke | 7b505107 | 2019-01-27 21:22:49 | [diff] [blame] | 27 | class HttpProxySocketParams; |
Matt Menke | 7b505107 | 2019-01-27 21:22:49 | [diff] [blame] | 28 | class SOCKSSocketParams; |
Matt Menke | 0754b5d0 | 2019-02-10 21:46:43 | [diff] [blame] | 29 | class TransportClientSocketPool; |
Matt Menke | 7b505107 | 2019-01-27 21:22:49 | [diff] [blame] | 30 | class TransportSocketParams; |
| 31 | |
| 32 | class NET_EXPORT_PRIVATE SSLSocketParams |
| 33 | : public base::RefCounted<SSLSocketParams> { |
| 34 | public: |
| 35 | enum ConnectionType { DIRECT, SOCKS_PROXY, HTTP_PROXY }; |
| 36 | |
| 37 | // Exactly one of |direct_params|, |socks_proxy_params|, and |
| 38 | // |http_proxy_params| must be non-NULL. |
| 39 | SSLSocketParams(const scoped_refptr<TransportSocketParams>& direct_params, |
| 40 | const scoped_refptr<SOCKSSocketParams>& socks_proxy_params, |
| 41 | const scoped_refptr<HttpProxySocketParams>& http_proxy_params, |
| 42 | const HostPortPair& host_and_port, |
| 43 | const SSLConfig& ssl_config, |
| 44 | PrivacyMode privacy_mode); |
| 45 | |
| 46 | // Returns the type of the underlying connection. |
| 47 | ConnectionType GetConnectionType() const; |
| 48 | |
| 49 | // Must be called only when GetConnectionType() returns DIRECT. |
| 50 | const scoped_refptr<TransportSocketParams>& GetDirectConnectionParams() const; |
| 51 | |
| 52 | // Must be called only when GetConnectionType() returns SOCKS_PROXY. |
| 53 | const scoped_refptr<SOCKSSocketParams>& GetSocksProxyConnectionParams() const; |
| 54 | |
| 55 | // Must be called only when GetConnectionType() returns HTTP_PROXY. |
| 56 | const scoped_refptr<HttpProxySocketParams>& GetHttpProxyConnectionParams() |
| 57 | const; |
| 58 | |
| 59 | const HostPortPair& host_and_port() const { return host_and_port_; } |
| 60 | const SSLConfig& ssl_config() const { return ssl_config_; } |
| 61 | PrivacyMode privacy_mode() const { return privacy_mode_; } |
| 62 | |
| 63 | private: |
| 64 | friend class base::RefCounted<SSLSocketParams>; |
| 65 | ~SSLSocketParams(); |
| 66 | |
| 67 | const scoped_refptr<TransportSocketParams> direct_params_; |
| 68 | const scoped_refptr<SOCKSSocketParams> socks_proxy_params_; |
| 69 | const scoped_refptr<HttpProxySocketParams> http_proxy_params_; |
| 70 | const HostPortPair host_and_port_; |
| 71 | const SSLConfig ssl_config_; |
| 72 | const PrivacyMode privacy_mode_; |
| 73 | |
| 74 | DISALLOW_COPY_AND_ASSIGN(SSLSocketParams); |
| 75 | }; |
| 76 | |
| 77 | // SSLConnectJob establishes a connection, through a proxy if needed, and then |
| 78 | // handles the SSL handshake. It returns an SSLClientSocket on success. |
Matt Menke | 9d5e2c9 | 2019-02-05 01:42:23 | [diff] [blame] | 79 | class NET_EXPORT_PRIVATE SSLConnectJob : public ConnectJob, |
| 80 | public ConnectJob::Delegate { |
Matt Menke | 7b505107 | 2019-01-27 21:22:49 | [diff] [blame] | 81 | public: |
| 82 | // Note: the SSLConnectJob does not own |messenger| so it must outlive the |
| 83 | // job. |
Matt Menke | cb77b540 | 2019-01-28 17:11:23 | [diff] [blame] | 84 | SSLConnectJob(RequestPriority priority, |
| 85 | const CommonConnectJobParams& common_connect_job_params, |
Matt Menke | 7b505107 | 2019-01-27 21:22:49 | [diff] [blame] | 86 | const scoped_refptr<SSLSocketParams>& params, |
Matt Menke | 0754b5d0 | 2019-02-10 21:46:43 | [diff] [blame] | 87 | TransportClientSocketPool* http_proxy_pool, |
Matt Menke | 1a6c92d | 2019-02-23 00:25:38 | [diff] [blame^] | 88 | ConnectJob::Delegate* delegate, |
| 89 | const NetLogWithSource* net_log); |
Matt Menke | 7b505107 | 2019-01-27 21:22:49 | [diff] [blame] | 90 | ~SSLConnectJob() override; |
| 91 | |
| 92 | // ConnectJob methods. |
| 93 | LoadState GetLoadState() const override; |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 94 | bool HasEstablishedConnection() const override; |
Matt Menke | 7b505107 | 2019-01-27 21:22:49 | [diff] [blame] | 95 | |
Matt Menke | 9d5e2c9 | 2019-02-05 01:42:23 | [diff] [blame] | 96 | // ConnectJob::Delegate methods. |
| 97 | void OnConnectJobComplete(int result, ConnectJob* job) override; |
| 98 | |
Matt Menke | 7b505107 | 2019-01-27 21:22:49 | [diff] [blame] | 99 | void GetAdditionalErrorState(ClientSocketHandle* handle) override; |
| 100 | |
| 101 | // Returns the connection timeout that will be used by a HttpProxyConnectJob |
| 102 | // created with the specified parameters, given current network conditions. |
| 103 | static base::TimeDelta ConnectionTimeout( |
| 104 | const SSLSocketParams& params, |
| 105 | const NetworkQualityEstimator* network_quality_estimator); |
| 106 | |
| 107 | private: |
| 108 | enum State { |
| 109 | STATE_TRANSPORT_CONNECT, |
| 110 | STATE_TRANSPORT_CONNECT_COMPLETE, |
| 111 | STATE_SOCKS_CONNECT, |
| 112 | STATE_SOCKS_CONNECT_COMPLETE, |
| 113 | STATE_TUNNEL_CONNECT, |
| 114 | STATE_TUNNEL_CONNECT_COMPLETE, |
| 115 | STATE_SSL_CONNECT, |
| 116 | STATE_SSL_CONNECT_COMPLETE, |
| 117 | STATE_NONE, |
| 118 | }; |
| 119 | |
| 120 | void OnIOComplete(int result); |
| 121 | |
| 122 | // Runs the state transition loop. |
| 123 | int DoLoop(int result); |
| 124 | |
| 125 | int DoTransportConnect(); |
| 126 | int DoTransportConnectComplete(int result); |
| 127 | int DoSOCKSConnect(); |
| 128 | int DoSOCKSConnectComplete(int result); |
| 129 | int DoTunnelConnect(); |
| 130 | int DoTunnelConnectComplete(int result); |
| 131 | int DoSSLConnect(); |
| 132 | int DoSSLConnectComplete(int result); |
| 133 | |
| 134 | // Returns the initial state for the state machine based on the |
| 135 | // |connection_type|. |
| 136 | static State GetInitialState(SSLSocketParams::ConnectionType connection_type); |
| 137 | |
| 138 | // Starts the SSL connection process. Returns OK on success and |
| 139 | // ERR_IO_PENDING if it cannot immediately service the request. |
| 140 | // Otherwise, it returns a net error code. |
| 141 | int ConnectInternal() override; |
| 142 | |
| 143 | void ChangePriorityInternal(RequestPriority priority) override; |
| 144 | |
| 145 | scoped_refptr<SSLSocketParams> params_; |
Matt Menke | 0754b5d0 | 2019-02-10 21:46:43 | [diff] [blame] | 146 | TransportClientSocketPool* const http_proxy_pool_; |
Matt Menke | 7b505107 | 2019-01-27 21:22:49 | [diff] [blame] | 147 | |
| 148 | State next_state_; |
| 149 | CompletionRepeatingCallback callback_; |
Matt Menke | 9d5e2c9 | 2019-02-05 01:42:23 | [diff] [blame] | 150 | std::unique_ptr<ConnectJob> nested_connect_job_; |
| 151 | std::unique_ptr<StreamSocket> nested_socket_; |
Matt Menke | 7b505107 | 2019-01-27 21:22:49 | [diff] [blame] | 152 | std::unique_ptr<ClientSocketHandle> transport_socket_handle_; |
| 153 | std::unique_ptr<SSLClientSocket> ssl_socket_; |
| 154 | |
| 155 | HttpResponseInfo error_response_info_; |
| 156 | |
| 157 | ConnectionAttempts connection_attempts_; |
| 158 | // The address of the server the connect job is connected to. Populated if |
| 159 | // and only if the connect job is connected *directly* to the server (not |
| 160 | // through an HTTPS CONNECT request or a SOCKS proxy). |
| 161 | IPEndPoint server_address_; |
| 162 | |
| 163 | DISALLOW_COPY_AND_ASSIGN(SSLConnectJob); |
| 164 | }; |
| 165 | |
| 166 | } // namespace net |
| 167 | |
| 168 | #endif // NET_SOCKET_SSL_CONNECT_JOB_H_ |