blob: b5fab499de83a85802d3226e0ef1a2d5e8125201 [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>
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 Benjamin6f2da652019-06-26 23:36:3517#include "net/base/network_isolation_key.h"
Matt Menke7b5051072019-01-27 21:22:4918#include "net/base/privacy_mode.h"
dalykedd30d982019-12-16 15:31:1019#include "net/dns/public/resolve_error_info.h"
Matt Menke7b5051072019-01-27 21:22:4920#include "net/socket/connect_job.h"
21#include "net/socket/connection_attempts.h"
22#include "net/socket/ssl_client_socket.h"
Matt Menke39b7c5a2019-04-10 19:47:5123#include "net/ssl/ssl_cert_request_info.h"
Matt Menke7b5051072019-01-27 21:22:4924#include "net/ssl/ssl_config_service.h"
25
26namespace net {
27
Matt Menke7b5051072019-01-27 21:22:4928class HostPortPair;
Matt Menke7b5051072019-01-27 21:22:4929class HttpProxySocketParams;
Matt Menkea6f99ad2019-03-08 02:26:4330class SocketTag;
Matt Menke7b5051072019-01-27 21:22:4931class SOCKSSocketParams;
Matt Menke7b5051072019-01-27 21:22:4932class TransportSocketParams;
33
34class 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 Menke1bbe89a2019-03-25 18:43:5641 SSLSocketParams(scoped_refptr<TransportSocketParams> direct_params,
42 scoped_refptr<SOCKSSocketParams> socks_proxy_params,
43 scoped_refptr<HttpProxySocketParams> http_proxy_params,
Matt Menke7b5051072019-01-27 21:22:4944 const HostPortPair& host_and_port,
45 const SSLConfig& ssl_config,
David Benjamin6f2da652019-06-26 23:36:3546 PrivacyMode privacy_mode,
47 NetworkIsolationKey network_isolation_key);
Matt Menke7b5051072019-01-27 21:22:4948
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 Benjamin6f2da652019-06-26 23:36:3565 const NetworkIsolationKey& network_isolation_key() const {
66 return network_isolation_key_;
67 }
Matt Menke7b5051072019-01-27 21:22:4968
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 Benjamin6f2da652019-06-26 23:36:3579 const NetworkIsolationKey network_isolation_key_;
Matt Menke7b5051072019-01-27 21:22:4980
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 Menke9d5e2c92019-02-05 01:42:2386class NET_EXPORT_PRIVATE SSLConnectJob : public ConnectJob,
87 public ConnectJob::Delegate {
Matt Menke7b5051072019-01-27 21:22:4988 public:
89 // Note: the SSLConnectJob does not own |messenger| so it must outlive the
90 // job.
Matt Menkecb77b5402019-01-28 17:11:2391 SSLConnectJob(RequestPriority priority,
Matt Menkea6f99ad2019-03-08 02:26:4392 const SocketTag& socket_tag,
93 const CommonConnectJobParams* common_connect_job_params,
Matt Menke1bbe89a2019-03-25 18:43:5694 scoped_refptr<SSLSocketParams> params,
Matt Menke1a6c92d2019-02-23 00:25:3895 ConnectJob::Delegate* delegate,
96 const NetLogWithSource* net_log);
Matt Menke7b5051072019-01-27 21:22:4997 ~SSLConnectJob() override;
98
99 // ConnectJob methods.
100 LoadState GetLoadState() const override;
Matt Menke141b87f22019-01-30 02:43:03101 bool HasEstablishedConnection() const override;
Matt Menke7b5051072019-01-27 21:22:49102
Matt Menke9d5e2c92019-02-05 01:42:23103 // ConnectJob::Delegate methods.
104 void OnConnectJobComplete(int result, ConnectJob* job) override;
Matt Menkeb57663b32019-03-01 17:17:10105 void OnNeedsProxyAuth(const HttpResponseInfo& response,
106 HttpAuthController* auth_controller,
107 base::OnceClosure restart_with_auth_callback,
108 ConnectJob* job) override;
Matt Menke6030ed9f2019-04-11 20:25:55109 ConnectionAttempts GetConnectionAttempts() const override;
dalykedd30d982019-12-16 15:31:10110 ResolveErrorInfo GetResolveErrorInfo() const override;
Matt Menke6f84d1f12019-04-11 19:26:47111 bool IsSSLError() const override;
112 scoped_refptr<SSLCertRequestInfo> GetCertRequestInfo() override;
Matt Menke7b5051072019-01-27 21:22:49113
Matt Menke36eaf5c2019-04-02 16:15:52114 // 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 Menke7b5051072019-01-27 21:22:49118 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 Menke7b5051072019-01-27 21:22:49157
158 State next_state_;
159 CompletionRepeatingCallback callback_;
Matt Menke9d5e2c92019-02-05 01:42:23160 std::unique_ptr<ConnectJob> nested_connect_job_;
161 std::unique_ptr<StreamSocket> nested_socket_;
Matt Menke7b5051072019-01-27 21:22:49162 std::unique_ptr<SSLClientSocket> ssl_socket_;
163
Matt Menkec1ae1d52019-04-10 19:28:27164 // True once SSL negotiation has started.
165 bool ssl_negotiation_started_;
166
Matt Menke39b7c5a2019-04-10 19:47:51167 scoped_refptr<SSLCertRequestInfo> ssl_cert_request_info_;
Matt Menke7b5051072019-01-27 21:22:49168
Matt Menke7b5051072019-01-27 21:22:49169 ConnectionAttempts connection_attempts_;
dalykedd30d982019-12-16 15:31:10170 ResolveErrorInfo resolve_error_info_;
Matt Menke7b5051072019-01-27 21:22:49171 // 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_