blob: 26ab586fbdc57f1933fd7b1ac294f561a15b77f5 [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"
17#include "net/base/privacy_mode.h"
18#include "net/http/http_response_info.h"
19#include "net/socket/connect_job.h"
20#include "net/socket/connection_attempts.h"
21#include "net/socket/ssl_client_socket.h"
22#include "net/ssl/ssl_config_service.h"
23
24namespace net {
25
Matt Menkeaade5812019-03-02 13:38:0026class ClientSocketHandle;
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,
45 PrivacyMode privacy_mode);
46
47 // Returns the type of the underlying connection.
48 ConnectionType GetConnectionType() const;
49
50 // Must be called only when GetConnectionType() returns DIRECT.
51 const scoped_refptr<TransportSocketParams>& GetDirectConnectionParams() const;
52
53 // Must be called only when GetConnectionType() returns SOCKS_PROXY.
54 const scoped_refptr<SOCKSSocketParams>& GetSocksProxyConnectionParams() const;
55
56 // Must be called only when GetConnectionType() returns HTTP_PROXY.
57 const scoped_refptr<HttpProxySocketParams>& GetHttpProxyConnectionParams()
58 const;
59
60 const HostPortPair& host_and_port() const { return host_and_port_; }
61 const SSLConfig& ssl_config() const { return ssl_config_; }
62 PrivacyMode privacy_mode() const { return privacy_mode_; }
63
64 private:
65 friend class base::RefCounted<SSLSocketParams>;
66 ~SSLSocketParams();
67
68 const scoped_refptr<TransportSocketParams> direct_params_;
69 const scoped_refptr<SOCKSSocketParams> socks_proxy_params_;
70 const scoped_refptr<HttpProxySocketParams> http_proxy_params_;
71 const HostPortPair host_and_port_;
72 const SSLConfig ssl_config_;
73 const PrivacyMode privacy_mode_;
74
75 DISALLOW_COPY_AND_ASSIGN(SSLSocketParams);
76};
77
78// SSLConnectJob establishes a connection, through a proxy if needed, and then
79// handles the SSL handshake. It returns an SSLClientSocket on success.
Matt Menke9d5e2c92019-02-05 01:42:2380class NET_EXPORT_PRIVATE SSLConnectJob : public ConnectJob,
81 public ConnectJob::Delegate {
Matt Menke7b5051072019-01-27 21:22:4982 public:
83 // Note: the SSLConnectJob does not own |messenger| so it must outlive the
84 // job.
Matt Menkecb77b5402019-01-28 17:11:2385 SSLConnectJob(RequestPriority priority,
Matt Menkea6f99ad2019-03-08 02:26:4386 const SocketTag& socket_tag,
87 const CommonConnectJobParams* common_connect_job_params,
Matt Menke1bbe89a2019-03-25 18:43:5688 scoped_refptr<SSLSocketParams> params,
Matt Menke1a6c92d2019-02-23 00:25:3889 ConnectJob::Delegate* delegate,
90 const NetLogWithSource* net_log);
Matt Menke7b5051072019-01-27 21:22:4991 ~SSLConnectJob() override;
92
93 // ConnectJob methods.
94 LoadState GetLoadState() const override;
Matt Menke141b87f22019-01-30 02:43:0395 bool HasEstablishedConnection() const override;
Matt Menke7b5051072019-01-27 21:22:4996
Matt Menke9d5e2c92019-02-05 01:42:2397 // ConnectJob::Delegate methods.
98 void OnConnectJobComplete(int result, ConnectJob* job) override;
Matt Menkeb57663b32019-03-01 17:17:1099 void OnNeedsProxyAuth(const HttpResponseInfo& response,
100 HttpAuthController* auth_controller,
101 base::OnceClosure restart_with_auth_callback,
102 ConnectJob* job) override;
Matt Menke7b5051072019-01-27 21:22:49103 void GetAdditionalErrorState(ClientSocketHandle* handle) override;
Matt Menkec1ae1d52019-04-10 19:28:27104 std::unique_ptr<StreamSocket> PassProxySocketOnFailure() override;
Matt Menke7b5051072019-01-27 21:22:49105
Matt Menke36eaf5c2019-04-02 16:15:52106 // Returns the timeout for the SSL handshake. This is the same for all
107 // connections regardless of whether or not there is a proxy in use.
108 static base::TimeDelta HandshakeTimeoutForTesting();
109
Matt Menke7b5051072019-01-27 21:22:49110 private:
111 enum State {
112 STATE_TRANSPORT_CONNECT,
113 STATE_TRANSPORT_CONNECT_COMPLETE,
114 STATE_SOCKS_CONNECT,
115 STATE_SOCKS_CONNECT_COMPLETE,
116 STATE_TUNNEL_CONNECT,
117 STATE_TUNNEL_CONNECT_COMPLETE,
118 STATE_SSL_CONNECT,
119 STATE_SSL_CONNECT_COMPLETE,
120 STATE_NONE,
121 };
122
123 void OnIOComplete(int result);
124
125 // Runs the state transition loop.
126 int DoLoop(int result);
127
128 int DoTransportConnect();
129 int DoTransportConnectComplete(int result);
130 int DoSOCKSConnect();
131 int DoSOCKSConnectComplete(int result);
132 int DoTunnelConnect();
133 int DoTunnelConnectComplete(int result);
134 int DoSSLConnect();
135 int DoSSLConnectComplete(int result);
136
137 // Returns the initial state for the state machine based on the
138 // |connection_type|.
139 static State GetInitialState(SSLSocketParams::ConnectionType connection_type);
140
141 // Starts the SSL connection process. Returns OK on success and
142 // ERR_IO_PENDING if it cannot immediately service the request.
143 // Otherwise, it returns a net error code.
144 int ConnectInternal() override;
145
146 void ChangePriorityInternal(RequestPriority priority) override;
147
148 scoped_refptr<SSLSocketParams> params_;
Matt Menke7b5051072019-01-27 21:22:49149
150 State next_state_;
151 CompletionRepeatingCallback callback_;
Matt Menke9d5e2c92019-02-05 01:42:23152 std::unique_ptr<ConnectJob> nested_connect_job_;
153 std::unique_ptr<StreamSocket> nested_socket_;
Matt Menke7b5051072019-01-27 21:22:49154 std::unique_ptr<SSLClientSocket> ssl_socket_;
155
Matt Menkec1ae1d52019-04-10 19:28:27156 // True once SSL negotiation has started.
157 bool ssl_negotiation_started_;
158
Matt Menke7b5051072019-01-27 21:22:49159 HttpResponseInfo error_response_info_;
160
161 ConnectionAttempts connection_attempts_;
162 // The address of the server the connect job is connected to. Populated if
163 // and only if the connect job is connected *directly* to the server (not
164 // through an HTTPS CONNECT request or a SOCKS proxy).
165 IPEndPoint server_address_;
166
167 DISALLOW_COPY_AND_ASSIGN(SSLConnectJob);
168};
169
170} // namespace net
171
172#endif // NET_SOCKET_SSL_CONNECT_JOB_H_