blob: daf2bac7a921875a4f97f8aabb112f95ac12d3b1 [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
David Benjamindc5fd6a2022-03-24 23:00:418#include <stdint.h>
9
Matt Menke7b5051072019-01-27 21:22:4910#include <memory>
Eric Orthac661912022-01-10 21:44:1711#include <set>
Matt Menke7b5051072019-01-27 21:22:4912#include <string>
David Benjamindc5fd6a2022-03-24 23:00:4113#include <vector>
Matt Menke7b5051072019-01-27 21:22:4914
Matt Menke7b5051072019-01-27 21:22:4915#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 Benjamin6f2da652019-06-26 23:36:3520#include "net/base/network_isolation_key.h"
Matt Menke7b5051072019-01-27 21:22:4921#include "net/base/privacy_mode.h"
Yoichiro Hibara6a4e2822022-09-05 04:20:0222#include "net/dns/public/host_resolver_results.h"
dalykedd30d982019-12-16 15:31:1023#include "net/dns/public/resolve_error_info.h"
Matt Menke7b5051072019-01-27 21:22:4924#include "net/socket/connect_job.h"
25#include "net/socket/connection_attempts.h"
26#include "net/socket/ssl_client_socket.h"
Matt Menke39b7c5a2019-04-10 19:47:5127#include "net/ssl/ssl_cert_request_info.h"
Matt Menke7b5051072019-01-27 21:22:4928#include "net/ssl/ssl_config_service.h"
David Benjamindc5fd6a2022-03-24 23:00:4129#include "third_party/abseil-cpp/absl/types/optional.h"
Matt Menke7b5051072019-01-27 21:22:4930
31namespace net {
32
Matt Menke7b5051072019-01-27 21:22:4933class HostPortPair;
Matt Menke7b5051072019-01-27 21:22:4934class HttpProxySocketParams;
Matt Menkea6f99ad2019-03-08 02:26:4335class SocketTag;
Matt Menke7b5051072019-01-27 21:22:4936class SOCKSSocketParams;
Matt Menke7b5051072019-01-27 21:22:4937class TransportSocketParams;
38
39class 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 Menke1bbe89a2019-03-25 18:43:5646 SSLSocketParams(scoped_refptr<TransportSocketParams> direct_params,
47 scoped_refptr<SOCKSSocketParams> socks_proxy_params,
48 scoped_refptr<HttpProxySocketParams> http_proxy_params,
Matt Menke7b5051072019-01-27 21:22:4949 const HostPortPair& host_and_port,
50 const SSLConfig& ssl_config,
David Benjamin6f2da652019-06-26 23:36:3551 PrivacyMode privacy_mode,
52 NetworkIsolationKey network_isolation_key);
Matt Menke7b5051072019-01-27 21:22:4953
Peter Boström407869b2021-10-07 04:42:4854 SSLSocketParams(const SSLSocketParams&) = delete;
55 SSLSocketParams& operator=(const SSLSocketParams&) = delete;
56
Matt Menke7b5051072019-01-27 21:22:4957 // 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 Benjamin6f2da652019-06-26 23:36:3573 const NetworkIsolationKey& network_isolation_key() const {
74 return network_isolation_key_;
75 }
Matt Menke7b5051072019-01-27 21:22:4976
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 Benjamin6f2da652019-06-26 23:36:3587 const NetworkIsolationKey network_isolation_key_;
Matt Menke7b5051072019-01-27 21:22:4988};
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 Menke9d5e2c92019-02-05 01:42:2392class NET_EXPORT_PRIVATE SSLConnectJob : public ConnectJob,
93 public ConnectJob::Delegate {
Matt Menke7b5051072019-01-27 21:22:4994 public:
Eric Ortha9b8be02021-06-29 23:09:0895 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 Menke7b5051072019-01-27 21:22:49109 // Note: the SSLConnectJob does not own |messenger| so it must outlive the
110 // job.
Matt Menkecb77b5402019-01-28 17:11:23111 SSLConnectJob(RequestPriority priority,
Matt Menkea6f99ad2019-03-08 02:26:43112 const SocketTag& socket_tag,
113 const CommonConnectJobParams* common_connect_job_params,
Matt Menke1bbe89a2019-03-25 18:43:56114 scoped_refptr<SSLSocketParams> params,
Matt Menke1a6c92d2019-02-23 00:25:38115 ConnectJob::Delegate* delegate,
116 const NetLogWithSource* net_log);
Peter Boström293b1342021-09-22 17:31:43117
118 SSLConnectJob(const SSLConnectJob&) = delete;
119 SSLConnectJob& operator=(const SSLConnectJob&) = delete;
120
Matt Menke7b5051072019-01-27 21:22:49121 ~SSLConnectJob() override;
122
123 // ConnectJob methods.
124 LoadState GetLoadState() const override;
Matt Menke141b87f22019-01-30 02:43:03125 bool HasEstablishedConnection() const override;
Matt Menke7b5051072019-01-27 21:22:49126
Matt Menke9d5e2c92019-02-05 01:42:23127 // ConnectJob::Delegate methods.
128 void OnConnectJobComplete(int result, ConnectJob* job) override;
Matt Menkeb57663b32019-03-01 17:17:10129 void OnNeedsProxyAuth(const HttpResponseInfo& response,
130 HttpAuthController* auth_controller,
131 base::OnceClosure restart_with_auth_callback,
132 ConnectJob* job) override;
Matt Menke6030ed9f2019-04-11 20:25:55133 ConnectionAttempts GetConnectionAttempts() const override;
dalykedd30d982019-12-16 15:31:10134 ResolveErrorInfo GetResolveErrorInfo() const override;
Matt Menke6f84d1f12019-04-11 19:26:47135 bool IsSSLError() const override;
136 scoped_refptr<SSLCertRequestInfo> GetCertRequestInfo() override;
Matt Menke7b5051072019-01-27 21:22:49137
Matt Menke36eaf5c2019-04-02 16:15:52138 // 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 Menke7b5051072019-01-27 21:22:49142 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 Benjamin07a07d652020-02-26 22:26:59178 void ResetStateForRestart();
179
Matt Menke7b5051072019-01-27 21:22:49180 void ChangePriorityInternal(RequestPriority priority) override;
181
182 scoped_refptr<SSLSocketParams> params_;
Matt Menke7b5051072019-01-27 21:22:49183
184 State next_state_;
185 CompletionRepeatingCallback callback_;
Matt Menke9d5e2c92019-02-05 01:42:23186 std::unique_ptr<ConnectJob> nested_connect_job_;
187 std::unique_ptr<StreamSocket> nested_socket_;
Matt Menke7b5051072019-01-27 21:22:49188 std::unique_ptr<SSLClientSocket> ssl_socket_;
189
Matt Menkec1ae1d52019-04-10 19:28:27190 // True once SSL negotiation has started.
Tsuyoshi Horo2ec06e002022-06-09 01:38:59191 bool ssl_negotiation_started_ = false;
Matt Menkec1ae1d52019-04-10 19:28:27192
David Benjamin07a07d652020-02-26 22:26:59193 // 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 Horo2ec06e002022-06-09 01:38:59196 bool disable_legacy_crypto_with_fallback_ = true;
David Benjamin07a07d652020-02-26 22:26:59197
Matt Menke39b7c5a2019-04-10 19:47:51198 scoped_refptr<SSLCertRequestInfo> ssl_cert_request_info_;
Matt Menke7b5051072019-01-27 21:22:49199
Matt Menke7b5051072019-01-27 21:22:49200 ConnectionAttempts connection_attempts_;
dalykedd30d982019-12-16 15:31:10201 ResolveErrorInfo resolve_error_info_;
Matt Menke7b5051072019-01-27 21:22:49202 // 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 Orthac661912022-01-10 21:44:17207 // 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 Benjamindc5fd6a2022-03-24 23:00:41213
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 Menke7b5051072019-01-27 21:22:49221};
222
223} // namespace net
224
225#endif // NET_SOCKET_SSL_CONNECT_JOB_H_