blob: 4563f2392239400ec16fb3fa715362b1249c4fe4 [file] [log] [blame]
Matt Menke7b5051072019-01-27 21:22:491// 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 Barnesaa2a8b52020-12-17 19:33:1910#include <vector>
Matt Menke7b5051072019-01-27 21:22:4911
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 Benjamin6f2da652019-06-26 23:36:3518#include "net/base/network_isolation_key.h"
Matt Menke7b5051072019-01-27 21:22:4919#include "net/base/privacy_mode.h"
dalykedd30d982019-12-16 15:31:1020#include "net/dns/public/resolve_error_info.h"
Matt Menke7b5051072019-01-27 21:22:4921#include "net/socket/connect_job.h"
22#include "net/socket/connection_attempts.h"
23#include "net/socket/ssl_client_socket.h"
Matt Menke39b7c5a2019-04-10 19:47:5124#include "net/ssl/ssl_cert_request_info.h"
Matt Menke7b5051072019-01-27 21:22:4925#include "net/ssl/ssl_config_service.h"
26
27namespace net {
28
Matt Menke7b5051072019-01-27 21:22:4929class HostPortPair;
Matt Menke7b5051072019-01-27 21:22:4930class HttpProxySocketParams;
Matt Menkea6f99ad2019-03-08 02:26:4331class SocketTag;
Matt Menke7b5051072019-01-27 21:22:4932class SOCKSSocketParams;
Matt Menke7b5051072019-01-27 21:22:4933class TransportSocketParams;
34
35class 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 Menke1bbe89a2019-03-25 18:43:5642 SSLSocketParams(scoped_refptr<TransportSocketParams> direct_params,
43 scoped_refptr<SOCKSSocketParams> socks_proxy_params,
44 scoped_refptr<HttpProxySocketParams> http_proxy_params,
Matt Menke7b5051072019-01-27 21:22:4945 const HostPortPair& host_and_port,
46 const SSLConfig& ssl_config,
David Benjamin6f2da652019-06-26 23:36:3547 PrivacyMode privacy_mode,
48 NetworkIsolationKey network_isolation_key);
Matt Menke7b5051072019-01-27 21:22:4949
Peter Boström407869b2021-10-07 04:42:4850 SSLSocketParams(const SSLSocketParams&) = delete;
51 SSLSocketParams& operator=(const SSLSocketParams&) = delete;
52
Matt Menke7b5051072019-01-27 21:22:4953 // 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 Benjamin6f2da652019-06-26 23:36:3569 const NetworkIsolationKey& network_isolation_key() const {
70 return network_isolation_key_;
71 }
Matt Menke7b5051072019-01-27 21:22:4972
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 Benjamin6f2da652019-06-26 23:36:3583 const NetworkIsolationKey network_isolation_key_;
Matt Menke7b5051072019-01-27 21:22:4984};
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 Menke9d5e2c92019-02-05 01:42:2388class NET_EXPORT_PRIVATE SSLConnectJob : public ConnectJob,
89 public ConnectJob::Delegate {
Matt Menke7b5051072019-01-27 21:22:4990 public:
Eric Ortha9b8be02021-06-29 23:09:0891 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 Menke7b5051072019-01-27 21:22:49105 // Note: the SSLConnectJob does not own |messenger| so it must outlive the
106 // job.
Matt Menkecb77b5402019-01-28 17:11:23107 SSLConnectJob(RequestPriority priority,
Matt Menkea6f99ad2019-03-08 02:26:43108 const SocketTag& socket_tag,
109 const CommonConnectJobParams* common_connect_job_params,
Matt Menke1bbe89a2019-03-25 18:43:56110 scoped_refptr<SSLSocketParams> params,
Matt Menke1a6c92d2019-02-23 00:25:38111 ConnectJob::Delegate* delegate,
112 const NetLogWithSource* net_log);
Peter Boström293b1342021-09-22 17:31:43113
114 SSLConnectJob(const SSLConnectJob&) = delete;
115 SSLConnectJob& operator=(const SSLConnectJob&) = delete;
116
Matt Menke7b5051072019-01-27 21:22:49117 ~SSLConnectJob() override;
118
119 // ConnectJob methods.
120 LoadState GetLoadState() const override;
Matt Menke141b87f22019-01-30 02:43:03121 bool HasEstablishedConnection() const override;
Matt Menke7b5051072019-01-27 21:22:49122
Matt Menke9d5e2c92019-02-05 01:42:23123 // ConnectJob::Delegate methods.
124 void OnConnectJobComplete(int result, ConnectJob* job) override;
Matt Menkeb57663b32019-03-01 17:17:10125 void OnNeedsProxyAuth(const HttpResponseInfo& response,
126 HttpAuthController* auth_controller,
127 base::OnceClosure restart_with_auth_callback,
128 ConnectJob* job) override;
Matt Menke6030ed9f2019-04-11 20:25:55129 ConnectionAttempts GetConnectionAttempts() const override;
dalykedd30d982019-12-16 15:31:10130 ResolveErrorInfo GetResolveErrorInfo() const override;
Matt Menke6f84d1f12019-04-11 19:26:47131 bool IsSSLError() const override;
132 scoped_refptr<SSLCertRequestInfo> GetCertRequestInfo() override;
Matt Menke7b5051072019-01-27 21:22:49133
Matt Menke36eaf5c2019-04-02 16:15:52134 // 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 Menke7b5051072019-01-27 21:22:49138 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 Benjamin07a07d652020-02-26 22:26:59174 void ResetStateForRestart();
175
Matt Menke7b5051072019-01-27 21:22:49176 void ChangePriorityInternal(RequestPriority priority) override;
177
178 scoped_refptr<SSLSocketParams> params_;
Matt Menke7b5051072019-01-27 21:22:49179
180 State next_state_;
181 CompletionRepeatingCallback callback_;
Matt Menke9d5e2c92019-02-05 01:42:23182 std::unique_ptr<ConnectJob> nested_connect_job_;
183 std::unique_ptr<StreamSocket> nested_socket_;
Matt Menke7b5051072019-01-27 21:22:49184 std::unique_ptr<SSLClientSocket> ssl_socket_;
185
Matt Menkec1ae1d52019-04-10 19:28:27186 // True once SSL negotiation has started.
187 bool ssl_negotiation_started_;
188
David Benjamin07a07d652020-02-26 22:26:59189 // 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 Menke39b7c5a2019-04-10 19:47:51194 scoped_refptr<SSLCertRequestInfo> ssl_cert_request_info_;
Matt Menke7b5051072019-01-27 21:22:49195
Matt Menke7b5051072019-01-27 21:22:49196 ConnectionAttempts connection_attempts_;
dalykedd30d982019-12-16 15:31:10197 ResolveErrorInfo resolve_error_info_;
Matt Menke7b5051072019-01-27 21:22:49198 // 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 Barnesaa2a8b52020-12-17 19:33:19203 // 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 Menke7b5051072019-01-27 21:22:49209};
210
211} // namespace net
212
213#endif // NET_SOCKET_SSL_CONNECT_JOB_H_