blob: a6285fdccecec13b43d8762d610143a10e4372b7 [file] [log] [blame]
[email protected]61f3ddf2012-02-08 02:45:391// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]d518cd92010-09-29 12:27:442// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
svaldeze83af292016-04-26 14:33:375#ifndef NET_SOCKET_SSL_CLIENT_SOCKET_IMPL_H_
6#define NET_SOCKET_SSL_CLIENT_SOCKET_IMPL_H_
[email protected]d518cd92010-09-29 12:27:447
Avi Drissman13fc8932015-12-20 04:40:468#include <stddef.h>
tbansalf82cc8e2015-10-14 20:05:499#include <stdint.h>
davidben1d489522015-07-01 18:48:4610
danakj655b66c2016-04-16 00:51:3811#include <memory>
[email protected]ea4a1c6a2010-12-09 13:33:2812#include <string>
davidben1d489522015-07-01 18:48:4613#include <vector>
[email protected]ea4a1c6a2010-12-09 13:33:2814
nharperb7441ef2016-01-25 23:54:1415#include "base/compiler_specific.h"
16#include "base/containers/mru_cache.h"
David Benjamine34d74242017-06-29 20:35:1617#include "base/macros.h"
davidben2a811e4e2015-12-01 10:49:3418#include "base/memory/ref_counted.h"
[email protected]be90ba32013-05-13 20:05:2519#include "base/memory/weak_ptr.h"
davidben2a811e4e2015-12-01 10:49:3420#include "build/build_config.h"
[email protected]d518cd92010-09-29 12:27:4421#include "net/base/completion_callback.h"
22#include "net/base/io_buffer.h"
eroman7f9236a2015-05-11 21:23:4323#include "net/cert/cert_verifier.h"
[email protected]6e7845ae2013-03-29 21:48:1124#include "net/cert/cert_verify_result.h"
davidbeneb5f8ef32014-09-04 14:14:3225#include "net/cert/ct_verify_result.h"
mikecironef22f9812016-10-04 03:40:1926#include "net/log/net_log_with_source.h"
[email protected]d518cd92010-09-29 12:27:4427#include "net/socket/client_socket_handle.h"
bnc3472afd2016-11-17 15:27:2128#include "net/socket/next_proto.h"
davidben3418e81f2016-10-19 00:09:4529#include "net/socket/socket_bio_adapter.h"
[email protected]536fd0b2013-03-14 17:41:5730#include "net/socket/ssl_client_socket.h"
[email protected]6b8a3c742014-07-25 00:25:3531#include "net/ssl/channel_id_service.h"
davidbenb8c23212014-10-28 00:12:1632#include "net/ssl/openssl_ssl_util.h"
[email protected]c0787702014-05-20 21:51:4433#include "net/ssl/ssl_client_cert_type.h"
[email protected]536fd0b2013-03-14 17:41:5734#include "net/ssl/ssl_config_service.h"
[email protected]a2b2cfc2017-12-06 09:06:0835#include "net/traffic_annotation/network_traffic_annotation.h"
tfarinae8cb8aa2016-10-21 02:44:0136#include "third_party/boringssl/src/include/openssl/base.h"
37#include "third_party/boringssl/src/include/openssl/ssl.h"
[email protected]d518cd92010-09-29 12:27:4438
davidben2a811e4e2015-12-01 10:49:3439namespace base {
40class FilePath;
xunjieli9f8c5fb52016-12-07 22:59:3341namespace trace_event {
42class ProcessMemoryDump;
43}
davidben2a811e4e2015-12-01 10:49:3444}
45
davidbenfe132d92016-09-27 18:07:2146namespace crypto {
47class OpenSSLErrStackTracer;
48}
49
[email protected]d518cd92010-09-29 12:27:4450namespace net {
51
[email protected]170e76c2010-10-04 15:04:2052class CertVerifier;
davidbeneb5f8ef32014-09-04 14:14:3253class CTVerifier;
[email protected]d518cd92010-09-29 12:27:4454class SSLCertRequestInfo;
[email protected]d518cd92010-09-29 12:27:4455class SSLInfo;
56
nharper78e6d2b2016-09-21 05:42:3557using TokenBindingSignatureMap =
58 base::MRUCache<std::pair<TokenBindingType, std::string>,
59 std::vector<uint8_t>>;
nharperb7441ef2016-01-25 23:54:1460
davidben3418e81f2016-10-19 00:09:4561class SSLClientSocketImpl : public SSLClientSocket,
62 public SocketBIOAdapter::Delegate {
[email protected]d518cd92010-09-29 12:27:4463 public:
64 // Takes ownership of the transport_socket, which may already be connected.
65 // The given hostname will be compared with the name(s) in the server's
66 // certificate during the SSL handshake. ssl_config specifies the SSL
67 // settings.
svaldeze83af292016-04-26 14:33:3768 SSLClientSocketImpl(std::unique_ptr<ClientSocketHandle> transport_socket,
69 const HostPortPair& host_and_port,
70 const SSLConfig& ssl_config,
71 const SSLClientSocketContext& context);
72 ~SSLClientSocketImpl() override;
[email protected]d518cd92010-09-29 12:27:4473
[email protected]fbef13932010-11-23 12:38:5374 const HostPortPair& host_and_port() const { return host_and_port_; }
[email protected]c3456bb2011-12-12 22:22:1975 const std::string& ssl_session_cache_shard() const {
76 return ssl_session_cache_shard_;
77 }
[email protected]fbef13932010-11-23 12:38:5378
davidben2a811e4e2015-12-01 10:49:3479#if !defined(OS_NACL)
David Benjamindc2f4b02017-07-27 23:59:0280 // Log SSL key material to |path|. Must be called before any SSLClientSockets
81 // are created.
82 static void SetSSLKeyLogFile(const base::FilePath& path);
davidben2a811e4e2015-12-01 10:49:3483#endif
zhongyi81f85c6d92015-10-16 19:34:1484
[email protected]81ec7c12012-07-31 18:32:1985 // SSLSocket implementation.
dchengb03027d2014-10-21 12:00:2086 int ExportKeyingMaterial(const base::StringPiece& label,
87 bool has_context,
88 const base::StringPiece& context,
89 unsigned char* out,
90 unsigned int outlen) override;
[email protected]d518cd92010-09-29 12:27:4491
[email protected]dbf036f2011-12-06 23:33:2492 // StreamSocket implementation.
Brad Lassey3a814172018-04-26 03:30:2193 int Connect(CompletionOnceCallback callback) override;
dchengb03027d2014-10-21 12:00:2094 void Disconnect() override;
95 bool IsConnected() const override;
96 bool IsConnectedAndIdle() const override;
97 int GetPeerAddress(IPEndPoint* address) const override;
98 int GetLocalAddress(IPEndPoint* address) const override;
tfarina428341112016-09-22 13:38:2099 const NetLogWithSource& NetLog() const override;
dchengb03027d2014-10-21 12:00:20100 void SetSubresourceSpeculation() override;
101 void SetOmniboxSpeculation() override;
102 bool WasEverUsed() const override;
tfarina2846404c2016-12-25 14:31:37103 bool WasAlpnNegotiated() const override;
bnc3cf2a592016-08-11 14:48:36104 NextProto GetNegotiatedProtocol() const override;
dchengb03027d2014-10-21 12:00:20105 bool GetSSLInfo(SSLInfo* ssl_info) override;
ttuttle23fdb7b2015-05-15 01:28:03106 void GetConnectionAttempts(ConnectionAttempts* out) const override;
107 void ClearConnectionAttempts() override {}
108 void AddConnectionAttempts(const ConnectionAttempts& attempts) override {}
tbansalf82cc8e2015-10-14 20:05:49109 int64_t GetTotalReceivedBytes() const override;
xunjieli998d2472017-01-12 01:12:28110 void DumpMemoryStats(SocketMemoryStats* stats) const override;
Bence Békydae8af5f2018-04-13 08:53:17111 void GetSSLCertRequestInfo(
112 SSLCertRequestInfo* cert_request_info) const override;
113 ChannelIDService* GetChannelIDService() const override;
114 Error GetTokenBindingSignature(crypto::ECPrivateKey* key,
115 TokenBindingType tb_type,
116 std::vector<uint8_t>* out) override;
117 crypto::ECPrivateKey* GetChannelIDKey() const override;
118
Paul Jensen0f49dec2017-12-12 23:39:58119 void ApplySocketTag(const SocketTag& tag) override;
xunjieli9f8c5fb52016-12-07 22:59:33120
121 // Dumps memory allocation stats. |pmd| is the browser process memory dump.
122 static void DumpSSLClientSessionMemoryStats(
123 base::trace_event::ProcessMemoryDump* pmd);
[email protected]d518cd92010-09-29 12:27:44124
[email protected]dbf036f2011-12-06 23:33:24125 // Socket implementation.
dchengb03027d2014-10-21 12:00:20126 int Read(IOBuffer* buf,
127 int buf_len,
Brad Lassey3a814172018-04-26 03:30:21128 CompletionOnceCallback callback) override;
xunjieli321a96f32017-03-07 19:42:17129 int ReadIfReady(IOBuffer* buf,
130 int buf_len,
Brad Lassey3a814172018-04-26 03:30:21131 CompletionOnceCallback callback) override;
dchengb03027d2014-10-21 12:00:20132 int Write(IOBuffer* buf,
133 int buf_len,
Brad Lassey3a814172018-04-26 03:30:21134 CompletionOnceCallback callback,
[email protected]578968d42017-12-13 15:39:32135 const NetworkTrafficAnnotationTag& traffic_annotation) override;
Avi Drissman13fc8932015-12-20 04:40:46136 int SetReceiveBufferSize(int32_t size) override;
137 int SetSendBufferSize(int32_t size) override;
[email protected]d518cd92010-09-29 12:27:44138
davidben3418e81f2016-10-19 00:09:45139 // SocketBIOAdapter implementation:
140 void OnReadReady() override;
141 void OnWriteReady() override;
142
[email protected]d518cd92010-09-29 12:27:44143 private:
[email protected]7f38da8a2014-03-17 16:44:26144 class PeerCertificateChain;
[email protected]821e3bb2013-11-08 01:06:01145 class SSLContext;
146 friend class SSLClientSocket;
147 friend class SSLContext;
148
[email protected]c8a80e92014-05-17 16:02:08149 int Init();
[email protected]d518cd92010-09-29 12:27:44150 void DoReadCallback(int result);
151 void DoWriteCallback(int result);
152
[email protected]d518cd92010-09-29 12:27:44153 int DoHandshake();
davidbenc4212c02015-05-12 22:30:18154 int DoHandshakeComplete(int result);
[email protected]faff9852014-06-21 06:13:46155 int DoChannelIDLookup();
156 int DoChannelIDLookupComplete(int result);
[email protected]170e76c2010-10-04 15:04:20157 int DoVerifyCert(int result);
158 int DoVerifyCertComplete(int result);
[email protected]d518cd92010-09-29 12:27:44159 void DoConnectCallback(int result);
[email protected]d518cd92010-09-29 12:27:44160
161 void OnHandshakeIOComplete(int result);
[email protected]d518cd92010-09-29 12:27:44162
163 int DoHandshakeLoop(int last_io_result);
xunjieli321a96f32017-03-07 19:42:17164 int DoPayloadRead(IOBuffer* buf, int buf_len);
[email protected]d518cd92010-09-29 12:27:44165 int DoPayloadWrite();
166
davidben1d489522015-07-01 18:48:46167 // Called when an asynchronous event completes which may have blocked the
davidben3418e81f2016-10-19 00:09:45168 // pending Connect, Read or Write calls, if any. Retries all state machines
169 // and, if complete, runs the respective callbacks.
170 void RetryAllOperations();
davidben1d489522015-07-01 18:48:46171
rsleevi4a6ca8c2016-06-24 03:05:22172 int VerifyCT();
[email protected]d518cd92010-09-29 12:27:44173
[email protected]821e3bb2013-11-08 01:06:01174 // Callback from the SSL layer that indicates the remote server is requesting
175 // a certificate for this client.
[email protected]82c59022014-08-15 09:38:27176 int ClientCertRequestCallback(SSL* ssl);
[email protected]821e3bb2013-11-08 01:06:01177
davidbendafe4e52015-04-08 22:53:52178 // Called after the initial handshake completes and after the server
179 // certificate has been verified. The order of handshake completion and
180 // certificate verification depends on whether the connection was false
181 // started. After both have happened (thus calling this twice), the session is
182 // safe to cache and will be cached.
183 void MaybeCacheSession();
184
davidben44aeae62015-06-24 20:47:43185 // Called from the SSL layer whenever a new session is established.
186 int NewSessionCallback(SSL_SESSION* session);
davidbendafe4e52015-04-08 22:53:52187
estark723b5eeb2016-02-18 21:01:12188 // Adds the Certificate Transparency info from ct_verify_result_ to
189 // |ssl_info|.
davidbeneb5f8ef32014-09-04 14:14:32190 // SCTs are held in three separate vectors in ct_verify_result, each
191 // vetor representing a particular verification state, this method associates
192 // each of the SCTs with the corresponding SCTVerifyStatus as it adds it to
193 // the |ssl_info|.signed_certificate_timestamps list.
estark723b5eeb2016-02-18 21:01:12194 void AddCTInfoToSSLInfo(SSLInfo* ssl_info) const;
davidbeneb5f8ef32014-09-04 14:14:32195
David Benjaminb3840f42017-08-03 15:50:16196 // Returns a unique key string for the SSL session cache for this socket. This
197 // must not be called if |ssl_session_cache_shard_| is empty.
rsleevif020edc2015-03-16 19:31:24198 std::string GetSessionCacheKey() const;
199
davidben421116c2015-05-12 19:56:51200 // Returns true if renegotiations are allowed.
201 bool IsRenegotiationAllowed() const;
202
davidben1d489522015-07-01 18:48:46203 // Callbacks for operations with the private key.
David Benjaminb9bafbe2017-11-07 21:41:38204 ssl_private_key_result_t PrivateKeySignCallback(uint8_t* out,
205 size_t* out_len,
206 size_t max_out,
207 uint16_t algorithm,
208 const uint8_t* in,
209 size_t in_len);
davidben0bca07fd2016-07-18 15:12:03210 ssl_private_key_result_t PrivateKeyCompleteCallback(uint8_t* out,
211 size_t* out_len,
212 size_t max_out);
davidben1d489522015-07-01 18:48:46213
davidben0bca07fd2016-07-18 15:12:03214 void OnPrivateKeyComplete(Error error, const std::vector<uint8_t>& signature);
davidben1d489522015-07-01 18:48:46215
davidbencef9e212017-04-19 15:00:10216 // Called whenever BoringSSL processes a protocol message.
217 void MessageCallback(int is_write,
218 int content_type,
219 const void* buf,
220 size_t len);
221
nharper736ceda2015-11-07 00:16:59222 int TokenBindingAdd(const uint8_t** out,
223 size_t* out_len,
224 int* out_alert_value);
225 int TokenBindingParse(const uint8_t* contents,
226 size_t contents_len,
227 int* out_alert_value);
228
davidben281d13f02016-04-27 20:43:28229 void LogConnectEndEvent(int rv);
230
bncbd442c22016-09-14 20:49:16231 // Record whether ALPN was used, and if so, the negotiated protocol,
232 // in a UMA histogram.
233 void RecordNegotiatedProtocol() const;
bnc3cf2a592016-08-11 14:48:36234
bnc3cf2a592016-08-11 14:48:36235 // Returns whether TLS channel ID is enabled.
236 bool IsChannelIDEnabled() const;
237
davidbenfe132d92016-09-27 18:07:21238 // Returns the net error corresponding to the most recent OpenSSL
239 // error. ssl_error is the output of SSL_get_error.
240 int MapLastOpenSSLError(int ssl_error,
241 const crypto::OpenSSLErrStackTracer& tracer,
242 OpenSSLErrorInfo* info);
243
Brad Lassey3a814172018-04-26 03:30:21244 CompletionOnceCallback user_connect_callback_;
245 CompletionOnceCallback user_read_callback_;
246 CompletionOnceCallback user_write_callback_;
[email protected]d518cd92010-09-29 12:27:44247
248 // Used by Read function.
249 scoped_refptr<IOBuffer> user_read_buf_;
250 int user_read_buf_len_;
251
252 // Used by Write function.
253 scoped_refptr<IOBuffer> user_write_buf_;
254 int user_write_buf_len_;
255
[email protected]4b768562013-02-16 04:10:07256 // Used by DoPayloadRead() when attempting to fill the caller's buffer with
257 // as much data as possible without blocking.
258 // If DoPayloadRead() encounters an error after having read some data, stores
259 // the result to return on the *next* call to DoPayloadRead(). A value > 0
260 // indicates there is no pending result, otherwise 0 indicates EOF and < 0
261 // indicates an error.
262 int pending_read_error_;
263
davidbenb8c23212014-10-28 00:12:16264 // If there is a pending read result, the OpenSSL result code (output of
265 // SSL_get_error) associated with it.
266 int pending_read_ssl_error_;
267
268 // If there is a pending read result, the OpenSSLErrorInfo associated with it.
269 OpenSSLErrorInfo pending_read_error_info_;
270
[email protected]64b5c892014-08-08 09:39:26271 // Set when Connect finishes.
[email protected]170e76c2010-10-04 15:04:20272 scoped_refptr<X509Certificate> server_cert_;
273 CertVerifyResult server_cert_verify_result_;
[email protected]64b5c892014-08-08 09:39:26274 bool completed_connect_;
[email protected]170e76c2010-10-04 15:04:20275
[email protected]0dc88b32014-03-26 20:12:28276 // Set when Read() or Write() successfully reads or writes data to or from the
277 // network.
278 bool was_ever_used_;
279
[email protected]822581d2010-12-16 17:27:15280 CertVerifier* const cert_verifier_;
danakj655b66c2016-04-16 00:51:38281 std::unique_ptr<CertVerifier::Request> cert_verifier_request_;
davidben09c3d072014-08-25 20:33:58282 base::TimeTicks start_cert_verification_time_;
[email protected]170e76c2010-10-04 15:04:20283
davidbeneb5f8ef32014-09-04 14:14:32284 // Certificate Transparency: Verifier and result holder.
285 ct::CTVerifyResult ct_verify_result_;
286 CTVerifier* cert_transparency_verifier_;
287
[email protected]ee0f2aa82013-10-25 11:59:26288 // The service for retrieving Channel ID keys. May be NULL.
[email protected]6b8a3c742014-07-25 00:25:35289 ChannelIDService* channel_id_service_;
nharper78e6d2b2016-09-21 05:42:35290 TokenBindingSignatureMap tb_signature_map_;
[email protected]ee0f2aa82013-10-25 11:59:26291
[email protected]d518cd92010-09-29 12:27:44292 // OpenSSL stuff
davidbend80c12c2016-10-11 00:13:49293 bssl::UniquePtr<SSL> ssl_;
[email protected]d518cd92010-09-29 12:27:44294
danakj655b66c2016-04-16 00:51:38295 std::unique_ptr<ClientSocketHandle> transport_;
davidben3418e81f2016-10-19 00:09:45296 std::unique_ptr<SocketBIOAdapter> transport_adapter_;
[email protected]055d7f22010-11-15 12:03:12297 const HostPortPair host_and_port_;
[email protected]d518cd92010-09-29 12:27:44298 SSLConfig ssl_config_;
[email protected]c3456bb2011-12-12 22:22:19299 // ssl_session_cache_shard_ is an opaque string that partitions the SSL
300 // session cache. i.e. sessions created with one value will not attempt to
301 // resume on the socket with a different value.
302 const std::string ssl_session_cache_shard_;
[email protected]d518cd92010-09-29 12:27:44303
[email protected]d518cd92010-09-29 12:27:44304 enum State {
305 STATE_NONE,
306 STATE_HANDSHAKE,
davidbenc4212c02015-05-12 22:30:18307 STATE_HANDSHAKE_COMPLETE,
[email protected]faff9852014-06-21 06:13:46308 STATE_CHANNEL_ID_LOOKUP,
309 STATE_CHANNEL_ID_LOOKUP_COMPLETE,
[email protected]d518cd92010-09-29 12:27:44310 STATE_VERIFY_CERT,
311 STATE_VERIFY_CERT_COMPLETE,
312 };
313 State next_handshake_state_;
svaldez4af14d22015-08-20 13:48:24314
315 // True if the socket has been disconnected.
316 bool disconnected_;
317
bnc3cf2a592016-08-11 14:48:36318 NextProto negotiated_protocol_;
[email protected]6b8a3c742014-07-25 00:25:35319 // Written by the |channel_id_service_|.
danakj655b66c2016-04-16 00:51:38320 std::unique_ptr<crypto::ECPrivateKey> channel_id_key_;
davidben52053b382015-04-27 19:22:29321 // True if a channel ID was sent.
322 bool channel_id_sent_;
davidbenc269cc4b2016-07-27 14:55:03323 // If non-null, the newly-established to be inserted into the session cache
324 // once certificate verification is done.
davidbend80c12c2016-10-11 00:13:49325 bssl::UniquePtr<SSL_SESSION> pending_session_;
davidbendafe4e52015-04-08 22:53:52326 // True if the initial handshake's certificate has been verified.
327 bool certificate_verified_;
davidbenfe132d92016-09-27 18:07:21328 // Set to true if a CertificateRequest was received.
329 bool certificate_requested_;
[email protected]6b8a3c742014-07-25 00:25:35330 // The request handle for |channel_id_service_|.
nharper75ade892015-06-10 19:05:35331 ChannelIDService::Request channel_id_request_;
[email protected]8bd4e7a2014-08-09 14:49:17332
davidben1d489522015-07-01 18:48:46333 int signature_result_;
334 std::vector<uint8_t> signature_;
335
[email protected]8bd4e7a2014-08-09 14:49:17336 TransportSecurityState* transport_security_state_;
337
estark6f9b3d82016-01-12 21:37:05338 CTPolicyEnforcer* const policy_enforcer_;
eranm6571b2b2014-12-03 15:53:23339
[email protected]8bd4e7a2014-08-09 14:49:17340 // pinning_failure_log contains a message produced by
341 // TransportSecurityState::CheckPublicKeyPins in the event of a
342 // pinning failure. It is a (somewhat) human-readable string.
343 std::string pinning_failure_log_;
344
dadriandf302c42016-06-10 18:48:59345 // True if PKP is bypassed due to a local trust anchor.
346 bool pkp_bypassed_;
347
Carlos IL81133382017-12-06 17:18:45348 // True if there was a certificate error which should be treated as fatal,
349 // and false otherwise.
350 bool is_fatal_cert_error_;
351
tfarina428341112016-09-22 13:38:20352 NetLogWithSource net_log_;
svaldeze83af292016-04-26 14:33:37353 base::WeakPtrFactory<SSLClientSocketImpl> weak_factory_;
David Benjamine34d74242017-06-29 20:35:16354
355 DISALLOW_COPY_AND_ASSIGN(SSLClientSocketImpl);
[email protected]d518cd92010-09-29 12:27:44356};
357
358} // namespace net
359
svaldeze83af292016-04-26 14:33:37360#endif // NET_SOCKET_SSL_CLIENT_SOCKET_IMPL_H_