blob: 575f429335ae0eaa9a2b1a68d9d6fb4dfbab1fd5 [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"
Matt Menke7b5051072019-01-27 21:22:4919#include "net/socket/connect_job.h"
20#include "net/socket/connection_attempts.h"
21#include "net/socket/ssl_client_socket.h"
Matt Menke39b7c5a2019-04-10 19:47:5122#include "net/ssl/ssl_cert_request_info.h"
Matt Menke7b5051072019-01-27 21:22:4923#include "net/ssl/ssl_config_service.h"
24
25namespace net {
26
Matt Menke7b5051072019-01-27 21:22:4927class HostPortPair;
Matt Menke7b5051072019-01-27 21:22:4928class HttpProxySocketParams;
Matt Menkea6f99ad2019-03-08 02:26:4329class SocketTag;
Matt Menke7b5051072019-01-27 21:22:4930class SOCKSSocketParams;
Matt Menke7b5051072019-01-27 21:22:4931class TransportSocketParams;
32
33class NET_EXPORT_PRIVATE SSLSocketParams
34 : public base::RefCounted<SSLSocketParams> {
35 public:
36 enum ConnectionType { DIRECT, SOCKS_PROXY, HTTP_PROXY };
37
38 // Exactly one of |direct_params|, |socks_proxy_params|, and
39 // |http_proxy_params| must be non-NULL.
Matt Menke1bbe89a2019-03-25 18:43:5640 SSLSocketParams(scoped_refptr<TransportSocketParams> direct_params,
41 scoped_refptr<SOCKSSocketParams> socks_proxy_params,
42 scoped_refptr<HttpProxySocketParams> http_proxy_params,
Matt Menke7b5051072019-01-27 21:22:4943 const HostPortPair& host_and_port,
44 const SSLConfig& ssl_config,
David Benjamin6f2da652019-06-26 23:36:3545 PrivacyMode privacy_mode,
46 NetworkIsolationKey network_isolation_key);
Matt Menke7b5051072019-01-27 21:22:4947
48 // Returns the type of the underlying connection.
49 ConnectionType GetConnectionType() const;
50
51 // Must be called only when GetConnectionType() returns DIRECT.
52 const scoped_refptr<TransportSocketParams>& GetDirectConnectionParams() const;
53
54 // Must be called only when GetConnectionType() returns SOCKS_PROXY.
55 const scoped_refptr<SOCKSSocketParams>& GetSocksProxyConnectionParams() const;
56
57 // Must be called only when GetConnectionType() returns HTTP_PROXY.
58 const scoped_refptr<HttpProxySocketParams>& GetHttpProxyConnectionParams()
59 const;
60
61 const HostPortPair& host_and_port() const { return host_and_port_; }
62 const SSLConfig& ssl_config() const { return ssl_config_; }
63 PrivacyMode privacy_mode() const { return privacy_mode_; }
David Benjamin6f2da652019-06-26 23:36:3564 const NetworkIsolationKey& network_isolation_key() const {
65 return network_isolation_key_;
66 }
Matt Menke7b5051072019-01-27 21:22:4967
68 private:
69 friend class base::RefCounted<SSLSocketParams>;
70 ~SSLSocketParams();
71
72 const scoped_refptr<TransportSocketParams> direct_params_;
73 const scoped_refptr<SOCKSSocketParams> socks_proxy_params_;
74 const scoped_refptr<HttpProxySocketParams> http_proxy_params_;
75 const HostPortPair host_and_port_;
76 const SSLConfig ssl_config_;
77 const PrivacyMode privacy_mode_;
David Benjamin6f2da652019-06-26 23:36:3578 const NetworkIsolationKey network_isolation_key_;
Matt Menke7b5051072019-01-27 21:22:4979
80 DISALLOW_COPY_AND_ASSIGN(SSLSocketParams);
81};
82
83// SSLConnectJob establishes a connection, through a proxy if needed, and then
84// handles the SSL handshake. It returns an SSLClientSocket on success.
Matt Menke9d5e2c92019-02-05 01:42:2385class NET_EXPORT_PRIVATE SSLConnectJob : public ConnectJob,
86 public ConnectJob::Delegate {
Matt Menke7b5051072019-01-27 21:22:4987 public:
88 // Note: the SSLConnectJob does not own |messenger| so it must outlive the
89 // job.
Matt Menkecb77b5402019-01-28 17:11:2390 SSLConnectJob(RequestPriority priority,
Matt Menkea6f99ad2019-03-08 02:26:4391 const SocketTag& socket_tag,
92 const CommonConnectJobParams* common_connect_job_params,
Matt Menke1bbe89a2019-03-25 18:43:5693 scoped_refptr<SSLSocketParams> params,
Matt Menke1a6c92d2019-02-23 00:25:3894 ConnectJob::Delegate* delegate,
95 const NetLogWithSource* net_log);
Matt Menke7b5051072019-01-27 21:22:4996 ~SSLConnectJob() override;
97
98 // ConnectJob methods.
99 LoadState GetLoadState() const override;
Matt Menke141b87f22019-01-30 02:43:03100 bool HasEstablishedConnection() const override;
Matt Menke7b5051072019-01-27 21:22:49101
Matt Menke9d5e2c92019-02-05 01:42:23102 // ConnectJob::Delegate methods.
103 void OnConnectJobComplete(int result, ConnectJob* job) override;
Matt Menkeb57663b32019-03-01 17:17:10104 void OnNeedsProxyAuth(const HttpResponseInfo& response,
105 HttpAuthController* auth_controller,
106 base::OnceClosure restart_with_auth_callback,
107 ConnectJob* job) override;
Matt Menke6030ed9f2019-04-11 20:25:55108 ConnectionAttempts GetConnectionAttempts() const override;
Matt Menke6f84d1f12019-04-11 19:26:47109 bool IsSSLError() const override;
110 scoped_refptr<SSLCertRequestInfo> GetCertRequestInfo() override;
Matt Menke7b5051072019-01-27 21:22:49111
Matt Menke36eaf5c2019-04-02 16:15:52112 // Returns the timeout for the SSL handshake. This is the same for all
113 // connections regardless of whether or not there is a proxy in use.
114 static base::TimeDelta HandshakeTimeoutForTesting();
115
Matt Menke7b5051072019-01-27 21:22:49116 private:
117 enum State {
118 STATE_TRANSPORT_CONNECT,
119 STATE_TRANSPORT_CONNECT_COMPLETE,
120 STATE_SOCKS_CONNECT,
121 STATE_SOCKS_CONNECT_COMPLETE,
122 STATE_TUNNEL_CONNECT,
123 STATE_TUNNEL_CONNECT_COMPLETE,
124 STATE_SSL_CONNECT,
125 STATE_SSL_CONNECT_COMPLETE,
126 STATE_NONE,
127 };
128
129 void OnIOComplete(int result);
130
131 // Runs the state transition loop.
132 int DoLoop(int result);
133
134 int DoTransportConnect();
135 int DoTransportConnectComplete(int result);
136 int DoSOCKSConnect();
137 int DoSOCKSConnectComplete(int result);
138 int DoTunnelConnect();
139 int DoTunnelConnectComplete(int result);
140 int DoSSLConnect();
141 int DoSSLConnectComplete(int result);
142
143 // Returns the initial state for the state machine based on the
144 // |connection_type|.
145 static State GetInitialState(SSLSocketParams::ConnectionType connection_type);
146
147 // Starts the SSL connection process. Returns OK on success and
148 // ERR_IO_PENDING if it cannot immediately service the request.
149 // Otherwise, it returns a net error code.
150 int ConnectInternal() override;
151
152 void ChangePriorityInternal(RequestPriority priority) override;
153
154 scoped_refptr<SSLSocketParams> params_;
Matt Menke7b5051072019-01-27 21:22:49155
156 State next_state_;
157 CompletionRepeatingCallback callback_;
Matt Menke9d5e2c92019-02-05 01:42:23158 std::unique_ptr<ConnectJob> nested_connect_job_;
159 std::unique_ptr<StreamSocket> nested_socket_;
Matt Menke7b5051072019-01-27 21:22:49160 std::unique_ptr<SSLClientSocket> ssl_socket_;
161
Matt Menkec1ae1d52019-04-10 19:28:27162 // True once SSL negotiation has started.
163 bool ssl_negotiation_started_;
164
Matt Menke39b7c5a2019-04-10 19:47:51165 scoped_refptr<SSLCertRequestInfo> ssl_cert_request_info_;
Matt Menke7b5051072019-01-27 21:22:49166
Matt Menke7b5051072019-01-27 21:22:49167 ConnectionAttempts connection_attempts_;
168 // The address of the server the connect job is connected to. Populated if
169 // and only if the connect job is connected *directly* to the server (not
170 // through an HTTPS CONNECT request or a SOCKS proxy).
171 IPEndPoint server_address_;
172
173 DISALLOW_COPY_AND_ASSIGN(SSLConnectJob);
174};
175
176} // namespace net
177
178#endif // NET_SOCKET_SSL_CONNECT_JOB_H_