blob: d32744d5150da1977d9a4ad088763b1eed52b387 [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
50 // Returns the type of the underlying connection.
51 ConnectionType GetConnectionType() const;
52
53 // Must be called only when GetConnectionType() returns DIRECT.
54 const scoped_refptr<TransportSocketParams>& GetDirectConnectionParams() const;
55
56 // Must be called only when GetConnectionType() returns SOCKS_PROXY.
57 const scoped_refptr<SOCKSSocketParams>& GetSocksProxyConnectionParams() const;
58
59 // Must be called only when GetConnectionType() returns HTTP_PROXY.
60 const scoped_refptr<HttpProxySocketParams>& GetHttpProxyConnectionParams()
61 const;
62
63 const HostPortPair& host_and_port() const { return host_and_port_; }
64 const SSLConfig& ssl_config() const { return ssl_config_; }
65 PrivacyMode privacy_mode() const { return privacy_mode_; }
David Benjamin6f2da652019-06-26 23:36:3566 const NetworkIsolationKey& network_isolation_key() const {
67 return network_isolation_key_;
68 }
Matt Menke7b5051072019-01-27 21:22:4969
70 private:
71 friend class base::RefCounted<SSLSocketParams>;
72 ~SSLSocketParams();
73
74 const scoped_refptr<TransportSocketParams> direct_params_;
75 const scoped_refptr<SOCKSSocketParams> socks_proxy_params_;
76 const scoped_refptr<HttpProxySocketParams> http_proxy_params_;
77 const HostPortPair host_and_port_;
78 const SSLConfig ssl_config_;
79 const PrivacyMode privacy_mode_;
David Benjamin6f2da652019-06-26 23:36:3580 const NetworkIsolationKey network_isolation_key_;
Matt Menke7b5051072019-01-27 21:22:4981
82 DISALLOW_COPY_AND_ASSIGN(SSLSocketParams);
83};
84
85// SSLConnectJob establishes a connection, through a proxy if needed, and then
86// handles the SSL handshake. It returns an SSLClientSocket on success.
Matt Menke9d5e2c92019-02-05 01:42:2387class NET_EXPORT_PRIVATE SSLConnectJob : public ConnectJob,
88 public ConnectJob::Delegate {
Matt Menke7b5051072019-01-27 21:22:4989 public:
Eric Ortha9b8be02021-06-29 23:09:0890 class NET_EXPORT_PRIVATE Factory {
91 public:
92 Factory() = default;
93 virtual ~Factory() = default;
94
95 virtual std::unique_ptr<SSLConnectJob> Create(
96 RequestPriority priority,
97 const SocketTag& socket_tag,
98 const CommonConnectJobParams* common_connect_job_params,
99 scoped_refptr<SSLSocketParams> params,
100 ConnectJob::Delegate* delegate,
101 const NetLogWithSource* net_log);
102 };
103
Matt Menke7b5051072019-01-27 21:22:49104 // Note: the SSLConnectJob does not own |messenger| so it must outlive the
105 // job.
Matt Menkecb77b5402019-01-28 17:11:23106 SSLConnectJob(RequestPriority priority,
Matt Menkea6f99ad2019-03-08 02:26:43107 const SocketTag& socket_tag,
108 const CommonConnectJobParams* common_connect_job_params,
Matt Menke1bbe89a2019-03-25 18:43:56109 scoped_refptr<SSLSocketParams> params,
Matt Menke1a6c92d2019-02-23 00:25:38110 ConnectJob::Delegate* delegate,
111 const NetLogWithSource* net_log);
Peter Boström293b1342021-09-22 17:31:43112
113 SSLConnectJob(const SSLConnectJob&) = delete;
114 SSLConnectJob& operator=(const SSLConnectJob&) = delete;
115
Matt Menke7b5051072019-01-27 21:22:49116 ~SSLConnectJob() override;
117
118 // ConnectJob methods.
119 LoadState GetLoadState() const override;
Matt Menke141b87f22019-01-30 02:43:03120 bool HasEstablishedConnection() const override;
Matt Menke7b5051072019-01-27 21:22:49121
Matt Menke9d5e2c92019-02-05 01:42:23122 // ConnectJob::Delegate methods.
123 void OnConnectJobComplete(int result, ConnectJob* job) override;
Matt Menkeb57663b32019-03-01 17:17:10124 void OnNeedsProxyAuth(const HttpResponseInfo& response,
125 HttpAuthController* auth_controller,
126 base::OnceClosure restart_with_auth_callback,
127 ConnectJob* job) override;
Matt Menke6030ed9f2019-04-11 20:25:55128 ConnectionAttempts GetConnectionAttempts() const override;
dalykedd30d982019-12-16 15:31:10129 ResolveErrorInfo GetResolveErrorInfo() const override;
Matt Menke6f84d1f12019-04-11 19:26:47130 bool IsSSLError() const override;
131 scoped_refptr<SSLCertRequestInfo> GetCertRequestInfo() override;
Matt Menke7b5051072019-01-27 21:22:49132
Matt Menke36eaf5c2019-04-02 16:15:52133 // Returns the timeout for the SSL handshake. This is the same for all
134 // connections regardless of whether or not there is a proxy in use.
135 static base::TimeDelta HandshakeTimeoutForTesting();
136
Matt Menke7b5051072019-01-27 21:22:49137 private:
138 enum State {
139 STATE_TRANSPORT_CONNECT,
140 STATE_TRANSPORT_CONNECT_COMPLETE,
141 STATE_SOCKS_CONNECT,
142 STATE_SOCKS_CONNECT_COMPLETE,
143 STATE_TUNNEL_CONNECT,
144 STATE_TUNNEL_CONNECT_COMPLETE,
145 STATE_SSL_CONNECT,
146 STATE_SSL_CONNECT_COMPLETE,
147 STATE_NONE,
148 };
149
150 void OnIOComplete(int result);
151
152 // Runs the state transition loop.
153 int DoLoop(int result);
154
155 int DoTransportConnect();
156 int DoTransportConnectComplete(int result);
157 int DoSOCKSConnect();
158 int DoSOCKSConnectComplete(int result);
159 int DoTunnelConnect();
160 int DoTunnelConnectComplete(int result);
161 int DoSSLConnect();
162 int DoSSLConnectComplete(int result);
163
164 // Returns the initial state for the state machine based on the
165 // |connection_type|.
166 static State GetInitialState(SSLSocketParams::ConnectionType connection_type);
167
168 // Starts the SSL connection process. Returns OK on success and
169 // ERR_IO_PENDING if it cannot immediately service the request.
170 // Otherwise, it returns a net error code.
171 int ConnectInternal() override;
172
David Benjamin07a07d652020-02-26 22:26:59173 void ResetStateForRestart();
174
Matt Menke7b5051072019-01-27 21:22:49175 void ChangePriorityInternal(RequestPriority priority) override;
176
177 scoped_refptr<SSLSocketParams> params_;
Matt Menke7b5051072019-01-27 21:22:49178
179 State next_state_;
180 CompletionRepeatingCallback callback_;
Matt Menke9d5e2c92019-02-05 01:42:23181 std::unique_ptr<ConnectJob> nested_connect_job_;
182 std::unique_ptr<StreamSocket> nested_socket_;
Matt Menke7b5051072019-01-27 21:22:49183 std::unique_ptr<SSLClientSocket> ssl_socket_;
184
Matt Menkec1ae1d52019-04-10 19:28:27185 // True once SSL negotiation has started.
186 bool ssl_negotiation_started_;
187
David Benjamin07a07d652020-02-26 22:26:59188 // True if legacy crypto should be disabled for the job's current connection
189 // attempt. On error, the connection will be retried with legacy crypto
190 // enabled.
191 bool disable_legacy_crypto_with_fallback_;
192
Matt Menke39b7c5a2019-04-10 19:47:51193 scoped_refptr<SSLCertRequestInfo> ssl_cert_request_info_;
Matt Menke7b5051072019-01-27 21:22:49194
Matt Menke7b5051072019-01-27 21:22:49195 ConnectionAttempts connection_attempts_;
dalykedd30d982019-12-16 15:31:10196 ResolveErrorInfo resolve_error_info_;
Matt Menke7b5051072019-01-27 21:22:49197 // The address of the server the connect job is connected to. Populated if
198 // and only if the connect job is connected *directly* to the server (not
199 // through an HTTPS CONNECT request or a SOCKS proxy).
200 IPEndPoint server_address_;
201
Cammie Smith Barnesaa2a8b52020-12-17 19:33:19202 // Any DNS aliases for the remote endpoint. The alias chain order is
203 // preserved in reverse, from canonical name (i.e. address record name)
204 // through to query name. Stored because `nested_connect_job_` has a
205 // limited lifetime and the aliases can no longer be retrieved from there by
206 // by the time that the aliases are needed to be passed in SetSocket.
207 std::vector<std::string> dns_aliases_;
Matt Menke7b5051072019-01-27 21:22:49208};
209
210} // namespace net
211
212#endif // NET_SOCKET_SSL_CONNECT_JOB_H_