blob: 150a479fa4a807632433ede1e89d47255cfe93be [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"
[email protected]d518cd92010-09-29 12:27:4420#include "net/base/completion_callback.h"
21#include "net/base/io_buffer.h"
eroman7f9236a2015-05-11 21:23:4322#include "net/cert/cert_verifier.h"
[email protected]6e7845ae2013-03-29 21:48:1123#include "net/cert/cert_verify_result.h"
davidbeneb5f8ef32014-09-04 14:14:3224#include "net/cert/ct_verify_result.h"
mikecironef22f9812016-10-04 03:40:1925#include "net/log/net_log_with_source.h"
[email protected]d518cd92010-09-29 12:27:4426#include "net/socket/client_socket_handle.h"
bnc3472afd2016-11-17 15:27:2127#include "net/socket/next_proto.h"
davidben3418e81f2016-10-19 00:09:4528#include "net/socket/socket_bio_adapter.h"
[email protected]536fd0b2013-03-14 17:41:5729#include "net/socket/ssl_client_socket.h"
[email protected]6b8a3c742014-07-25 00:25:3530#include "net/ssl/channel_id_service.h"
davidbenb8c23212014-10-28 00:12:1631#include "net/ssl/openssl_ssl_util.h"
[email protected]c0787702014-05-20 21:51:4432#include "net/ssl/ssl_client_cert_type.h"
[email protected]536fd0b2013-03-14 17:41:5733#include "net/ssl/ssl_config_service.h"
[email protected]a2b2cfc2017-12-06 09:06:0834#include "net/traffic_annotation/network_traffic_annotation.h"
tfarinae8cb8aa2016-10-21 02:44:0135#include "third_party/boringssl/src/include/openssl/base.h"
36#include "third_party/boringssl/src/include/openssl/ssl.h"
[email protected]d518cd92010-09-29 12:27:4437
davidben2a811e4e2015-12-01 10:49:3438namespace base {
xunjieli9f8c5fb52016-12-07 22:59:3339namespace trace_event {
40class ProcessMemoryDump;
41}
davidben2a811e4e2015-12-01 10:49:3442}
43
davidbenfe132d92016-09-27 18:07:2144namespace crypto {
45class OpenSSLErrStackTracer;
46}
47
[email protected]d518cd92010-09-29 12:27:4448namespace net {
49
[email protected]170e76c2010-10-04 15:04:2050class CertVerifier;
davidbeneb5f8ef32014-09-04 14:14:3251class CTVerifier;
[email protected]d518cd92010-09-29 12:27:4452class SSLCertRequestInfo;
[email protected]d518cd92010-09-29 12:27:4453class SSLInfo;
David Benjaminbd37c172018-07-11 17:24:5754class SSLKeyLogger;
[email protected]d518cd92010-09-29 12:27:4455
nharper78e6d2b2016-09-21 05:42:3556using TokenBindingSignatureMap =
57 base::MRUCache<std::pair<TokenBindingType, std::string>,
58 std::vector<uint8_t>>;
nharperb7441ef2016-01-25 23:54:1459
davidben3418e81f2016-10-19 00:09:4560class SSLClientSocketImpl : public SSLClientSocket,
61 public SocketBIOAdapter::Delegate {
[email protected]d518cd92010-09-29 12:27:4462 public:
63 // Takes ownership of the transport_socket, which may already be connected.
64 // The given hostname will be compared with the name(s) in the server's
65 // certificate during the SSL handshake. ssl_config specifies the SSL
66 // settings.
svaldeze83af292016-04-26 14:33:3767 SSLClientSocketImpl(std::unique_ptr<ClientSocketHandle> transport_socket,
68 const HostPortPair& host_and_port,
69 const SSLConfig& ssl_config,
70 const SSLClientSocketContext& context);
71 ~SSLClientSocketImpl() override;
[email protected]d518cd92010-09-29 12:27:4472
[email protected]fbef13932010-11-23 12:38:5373 const HostPortPair& host_and_port() const { return host_and_port_; }
[email protected]c3456bb2011-12-12 22:22:1974 const std::string& ssl_session_cache_shard() const {
75 return ssl_session_cache_shard_;
76 }
[email protected]fbef13932010-11-23 12:38:5377
David Benjaminbd37c172018-07-11 17:24:5778 // Log SSL key material to |logger|. Must be called before any
79 // SSLClientSockets are created.
80 static void SetSSLKeyLogger(std::unique_ptr<SSLKeyLogger> logger);
zhongyi81f85c6d92015-10-16 19:34:1481
[email protected]81ec7c12012-07-31 18:32:1982 // SSLSocket implementation.
dchengb03027d2014-10-21 12:00:2083 int ExportKeyingMaterial(const base::StringPiece& label,
84 bool has_context,
85 const base::StringPiece& context,
86 unsigned char* out,
87 unsigned int outlen) override;
[email protected]d518cd92010-09-29 12:27:4488
[email protected]dbf036f2011-12-06 23:33:2489 // StreamSocket implementation.
Brad Lassey3a814172018-04-26 03:30:2190 int Connect(CompletionOnceCallback callback) override;
dchengb03027d2014-10-21 12:00:2091 void Disconnect() override;
Steven Valdez6af02df2018-07-15 21:52:3392 int ConfirmHandshake(CompletionOnceCallback callback) override;
dchengb03027d2014-10-21 12:00:2093 bool IsConnected() const override;
94 bool IsConnectedAndIdle() const override;
95 int GetPeerAddress(IPEndPoint* address) const override;
96 int GetLocalAddress(IPEndPoint* address) const override;
tfarina428341112016-09-22 13:38:2097 const NetLogWithSource& NetLog() const override;
dchengb03027d2014-10-21 12:00:2098 bool WasEverUsed() const override;
tfarina2846404c2016-12-25 14:31:3799 bool WasAlpnNegotiated() const override;
bnc3cf2a592016-08-11 14:48:36100 NextProto GetNegotiatedProtocol() const override;
dchengb03027d2014-10-21 12:00:20101 bool GetSSLInfo(SSLInfo* ssl_info) override;
ttuttle23fdb7b2015-05-15 01:28:03102 void GetConnectionAttempts(ConnectionAttempts* out) const override;
103 void ClearConnectionAttempts() override {}
104 void AddConnectionAttempts(const ConnectionAttempts& attempts) override {}
tbansalf82cc8e2015-10-14 20:05:49105 int64_t GetTotalReceivedBytes() const override;
xunjieli998d2472017-01-12 01:12:28106 void DumpMemoryStats(SocketMemoryStats* stats) const override;
Bence Békydae8af5f2018-04-13 08:53:17107 void GetSSLCertRequestInfo(
108 SSLCertRequestInfo* cert_request_info) const override;
109 ChannelIDService* GetChannelIDService() const override;
110 Error GetTokenBindingSignature(crypto::ECPrivateKey* key,
111 TokenBindingType tb_type,
112 std::vector<uint8_t>* out) override;
113 crypto::ECPrivateKey* GetChannelIDKey() const override;
114
Paul Jensen0f49dec2017-12-12 23:39:58115 void ApplySocketTag(const SocketTag& tag) override;
xunjieli9f8c5fb52016-12-07 22:59:33116
117 // Dumps memory allocation stats. |pmd| is the browser process memory dump.
118 static void DumpSSLClientSessionMemoryStats(
119 base::trace_event::ProcessMemoryDump* pmd);
[email protected]d518cd92010-09-29 12:27:44120
[email protected]dbf036f2011-12-06 23:33:24121 // Socket implementation.
dchengb03027d2014-10-21 12:00:20122 int Read(IOBuffer* buf,
123 int buf_len,
Brad Lassey3a814172018-04-26 03:30:21124 CompletionOnceCallback callback) override;
xunjieli321a96f32017-03-07 19:42:17125 int ReadIfReady(IOBuffer* buf,
126 int buf_len,
Brad Lassey3a814172018-04-26 03:30:21127 CompletionOnceCallback callback) override;
dchengb03027d2014-10-21 12:00:20128 int Write(IOBuffer* buf,
129 int buf_len,
Brad Lassey3a814172018-04-26 03:30:21130 CompletionOnceCallback callback,
[email protected]578968d42017-12-13 15:39:32131 const NetworkTrafficAnnotationTag& traffic_annotation) override;
Avi Drissman13fc8932015-12-20 04:40:46132 int SetReceiveBufferSize(int32_t size) override;
133 int SetSendBufferSize(int32_t size) override;
[email protected]d518cd92010-09-29 12:27:44134
davidben3418e81f2016-10-19 00:09:45135 // SocketBIOAdapter implementation:
136 void OnReadReady() override;
137 void OnWriteReady() override;
138
[email protected]d518cd92010-09-29 12:27:44139 private:
[email protected]7f38da8a2014-03-17 16:44:26140 class PeerCertificateChain;
[email protected]821e3bb2013-11-08 01:06:01141 class SSLContext;
142 friend class SSLClientSocket;
143 friend class SSLContext;
144
[email protected]c8a80e92014-05-17 16:02:08145 int Init();
[email protected]d518cd92010-09-29 12:27:44146 void DoReadCallback(int result);
147 void DoWriteCallback(int result);
148
[email protected]d518cd92010-09-29 12:27:44149 int DoHandshake();
davidbenc4212c02015-05-12 22:30:18150 int DoHandshakeComplete(int result);
[email protected]faff9852014-06-21 06:13:46151 int DoChannelIDLookup();
152 int DoChannelIDLookupComplete(int result);
[email protected]170e76c2010-10-04 15:04:20153 int DoVerifyCert(int result);
154 int DoVerifyCertComplete(int result);
[email protected]d518cd92010-09-29 12:27:44155 void DoConnectCallback(int result);
[email protected]d518cd92010-09-29 12:27:44156
157 void OnHandshakeIOComplete(int result);
[email protected]d518cd92010-09-29 12:27:44158
159 int DoHandshakeLoop(int last_io_result);
xunjieli321a96f32017-03-07 19:42:17160 int DoPayloadRead(IOBuffer* buf, int buf_len);
[email protected]d518cd92010-09-29 12:27:44161 int DoPayloadWrite();
162
davidben1d489522015-07-01 18:48:46163 // Called when an asynchronous event completes which may have blocked the
davidben3418e81f2016-10-19 00:09:45164 // pending Connect, Read or Write calls, if any. Retries all state machines
165 // and, if complete, runs the respective callbacks.
166 void RetryAllOperations();
davidben1d489522015-07-01 18:48:46167
rsleevi4a6ca8c2016-06-24 03:05:22168 int VerifyCT();
[email protected]d518cd92010-09-29 12:27:44169
[email protected]821e3bb2013-11-08 01:06:01170 // Callback from the SSL layer that indicates the remote server is requesting
171 // a certificate for this client.
[email protected]82c59022014-08-15 09:38:27172 int ClientCertRequestCallback(SSL* ssl);
[email protected]821e3bb2013-11-08 01:06:01173
davidbendafe4e52015-04-08 22:53:52174 // Called after the initial handshake completes and after the server
175 // certificate has been verified. The order of handshake completion and
176 // certificate verification depends on whether the connection was false
177 // started. After both have happened (thus calling this twice), the session is
178 // safe to cache and will be cached.
179 void MaybeCacheSession();
180
davidben44aeae62015-06-24 20:47:43181 // Called from the SSL layer whenever a new session is established.
182 int NewSessionCallback(SSL_SESSION* session);
davidbendafe4e52015-04-08 22:53:52183
estark723b5eeb2016-02-18 21:01:12184 // Adds the Certificate Transparency info from ct_verify_result_ to
185 // |ssl_info|.
davidbeneb5f8ef32014-09-04 14:14:32186 // SCTs are held in three separate vectors in ct_verify_result, each
187 // vetor representing a particular verification state, this method associates
188 // each of the SCTs with the corresponding SCTVerifyStatus as it adds it to
189 // the |ssl_info|.signed_certificate_timestamps list.
estark723b5eeb2016-02-18 21:01:12190 void AddCTInfoToSSLInfo(SSLInfo* ssl_info) const;
davidbeneb5f8ef32014-09-04 14:14:32191
David Benjaminb3840f42017-08-03 15:50:16192 // Returns a unique key string for the SSL session cache for this socket. This
193 // must not be called if |ssl_session_cache_shard_| is empty.
rsleevif020edc2015-03-16 19:31:24194 std::string GetSessionCacheKey() const;
195
davidben421116c2015-05-12 19:56:51196 // Returns true if renegotiations are allowed.
197 bool IsRenegotiationAllowed() const;
198
davidben1d489522015-07-01 18:48:46199 // Callbacks for operations with the private key.
David Benjaminb9bafbe2017-11-07 21:41:38200 ssl_private_key_result_t PrivateKeySignCallback(uint8_t* out,
201 size_t* out_len,
202 size_t max_out,
203 uint16_t algorithm,
204 const uint8_t* in,
205 size_t in_len);
davidben0bca07fd2016-07-18 15:12:03206 ssl_private_key_result_t PrivateKeyCompleteCallback(uint8_t* out,
207 size_t* out_len,
208 size_t max_out);
davidben1d489522015-07-01 18:48:46209
davidben0bca07fd2016-07-18 15:12:03210 void OnPrivateKeyComplete(Error error, const std::vector<uint8_t>& signature);
davidben1d489522015-07-01 18:48:46211
David Benjamin7a8e4dfa2018-06-12 23:07:21212 // Called from the BoringSSL info callback. (See |SSL_CTX_set_info_callback|.)
213 void InfoCallback(int type, int value);
214
davidbencef9e212017-04-19 15:00:10215 // Called whenever BoringSSL processes a protocol message.
216 void MessageCallback(int is_write,
217 int content_type,
218 const void* buf,
219 size_t len);
220
nharper736ceda2015-11-07 00:16:59221 int TokenBindingAdd(const uint8_t** out,
222 size_t* out_len,
223 int* out_alert_value);
224 int TokenBindingParse(const uint8_t* contents,
225 size_t contents_len,
226 int* out_alert_value);
227
davidben281d13f02016-04-27 20:43:28228 void LogConnectEndEvent(int rv);
229
bncbd442c22016-09-14 20:49:16230 // Record whether ALPN was used, and if so, the negotiated protocol,
231 // in a UMA histogram.
232 void RecordNegotiatedProtocol() const;
bnc3cf2a592016-08-11 14:48:36233
bnc3cf2a592016-08-11 14:48:36234 // Returns whether TLS channel ID is enabled.
235 bool IsChannelIDEnabled() const;
236
davidbenfe132d92016-09-27 18:07:21237 // Returns the net error corresponding to the most recent OpenSSL
238 // error. ssl_error is the output of SSL_get_error.
239 int MapLastOpenSSLError(int ssl_error,
240 const crypto::OpenSSLErrStackTracer& tracer,
241 OpenSSLErrorInfo* info);
242
Brad Lassey3a814172018-04-26 03:30:21243 CompletionOnceCallback user_connect_callback_;
244 CompletionOnceCallback user_read_callback_;
245 CompletionOnceCallback user_write_callback_;
[email protected]d518cd92010-09-29 12:27:44246
247 // Used by Read function.
248 scoped_refptr<IOBuffer> user_read_buf_;
249 int user_read_buf_len_;
250
251 // Used by Write function.
252 scoped_refptr<IOBuffer> user_write_buf_;
253 int user_write_buf_len_;
254
[email protected]4b768562013-02-16 04:10:07255 // Used by DoPayloadRead() when attempting to fill the caller's buffer with
256 // as much data as possible without blocking.
257 // If DoPayloadRead() encounters an error after having read some data, stores
258 // the result to return on the *next* call to DoPayloadRead(). A value > 0
259 // indicates there is no pending result, otherwise 0 indicates EOF and < 0
260 // indicates an error.
261 int pending_read_error_;
262
davidbenb8c23212014-10-28 00:12:16263 // If there is a pending read result, the OpenSSL result code (output of
264 // SSL_get_error) associated with it.
265 int pending_read_ssl_error_;
266
267 // If there is a pending read result, the OpenSSLErrorInfo associated with it.
268 OpenSSLErrorInfo pending_read_error_info_;
269
[email protected]64b5c892014-08-08 09:39:26270 // Set when Connect finishes.
[email protected]170e76c2010-10-04 15:04:20271 scoped_refptr<X509Certificate> server_cert_;
272 CertVerifyResult server_cert_verify_result_;
[email protected]64b5c892014-08-08 09:39:26273 bool completed_connect_;
[email protected]170e76c2010-10-04 15:04:20274
[email protected]0dc88b32014-03-26 20:12:28275 // Set when Read() or Write() successfully reads or writes data to or from the
276 // network.
277 bool was_ever_used_;
278
[email protected]822581d2010-12-16 17:27:15279 CertVerifier* const cert_verifier_;
danakj655b66c2016-04-16 00:51:38280 std::unique_ptr<CertVerifier::Request> cert_verifier_request_;
davidben09c3d072014-08-25 20:33:58281 base::TimeTicks start_cert_verification_time_;
[email protected]170e76c2010-10-04 15:04:20282
davidbeneb5f8ef32014-09-04 14:14:32283 // Certificate Transparency: Verifier and result holder.
284 ct::CTVerifyResult ct_verify_result_;
285 CTVerifier* cert_transparency_verifier_;
286
[email protected]ee0f2aa82013-10-25 11:59:26287 // The service for retrieving Channel ID keys. May be NULL.
[email protected]6b8a3c742014-07-25 00:25:35288 ChannelIDService* channel_id_service_;
nharper78e6d2b2016-09-21 05:42:35289 TokenBindingSignatureMap tb_signature_map_;
[email protected]ee0f2aa82013-10-25 11:59:26290
[email protected]d518cd92010-09-29 12:27:44291 // OpenSSL stuff
davidbend80c12c2016-10-11 00:13:49292 bssl::UniquePtr<SSL> ssl_;
[email protected]d518cd92010-09-29 12:27:44293
danakj655b66c2016-04-16 00:51:38294 std::unique_ptr<ClientSocketHandle> transport_;
davidben3418e81f2016-10-19 00:09:45295 std::unique_ptr<SocketBIOAdapter> transport_adapter_;
[email protected]055d7f22010-11-15 12:03:12296 const HostPortPair host_and_port_;
[email protected]d518cd92010-09-29 12:27:44297 SSLConfig ssl_config_;
[email protected]c3456bb2011-12-12 22:22:19298 // ssl_session_cache_shard_ is an opaque string that partitions the SSL
299 // session cache. i.e. sessions created with one value will not attempt to
300 // resume on the socket with a different value.
301 const std::string ssl_session_cache_shard_;
[email protected]d518cd92010-09-29 12:27:44302
[email protected]d518cd92010-09-29 12:27:44303 enum State {
304 STATE_NONE,
305 STATE_HANDSHAKE,
davidbenc4212c02015-05-12 22:30:18306 STATE_HANDSHAKE_COMPLETE,
[email protected]faff9852014-06-21 06:13:46307 STATE_CHANNEL_ID_LOOKUP,
308 STATE_CHANNEL_ID_LOOKUP_COMPLETE,
[email protected]d518cd92010-09-29 12:27:44309 STATE_VERIFY_CERT,
310 STATE_VERIFY_CERT_COMPLETE,
311 };
312 State next_handshake_state_;
svaldez4af14d22015-08-20 13:48:24313
Steven Valdez6af02df2018-07-15 21:52:33314 // True if we are currently confirming the handshake.
315 bool in_confirm_handshake_;
316
svaldez4af14d22015-08-20 13:48:24317 // True if the socket has been disconnected.
318 bool disconnected_;
319
bnc3cf2a592016-08-11 14:48:36320 NextProto negotiated_protocol_;
[email protected]6b8a3c742014-07-25 00:25:35321 // Written by the |channel_id_service_|.
danakj655b66c2016-04-16 00:51:38322 std::unique_ptr<crypto::ECPrivateKey> channel_id_key_;
davidben52053b382015-04-27 19:22:29323 // True if a channel ID was sent.
324 bool channel_id_sent_;
davidbenc269cc4b2016-07-27 14:55:03325 // If non-null, the newly-established to be inserted into the session cache
326 // once certificate verification is done.
davidbend80c12c2016-10-11 00:13:49327 bssl::UniquePtr<SSL_SESSION> pending_session_;
davidbendafe4e52015-04-08 22:53:52328 // True if the initial handshake's certificate has been verified.
329 bool certificate_verified_;
davidbenfe132d92016-09-27 18:07:21330 // Set to true if a CertificateRequest was received.
331 bool certificate_requested_;
[email protected]6b8a3c742014-07-25 00:25:35332 // The request handle for |channel_id_service_|.
nharper75ade892015-06-10 19:05:35333 ChannelIDService::Request channel_id_request_;
[email protected]8bd4e7a2014-08-09 14:49:17334
davidben1d489522015-07-01 18:48:46335 int signature_result_;
336 std::vector<uint8_t> signature_;
337
[email protected]8bd4e7a2014-08-09 14:49:17338 TransportSecurityState* transport_security_state_;
339
estark6f9b3d82016-01-12 21:37:05340 CTPolicyEnforcer* const policy_enforcer_;
eranm6571b2b2014-12-03 15:53:23341
[email protected]8bd4e7a2014-08-09 14:49:17342 // pinning_failure_log contains a message produced by
343 // TransportSecurityState::CheckPublicKeyPins in the event of a
344 // pinning failure. It is a (somewhat) human-readable string.
345 std::string pinning_failure_log_;
346
dadriandf302c42016-06-10 18:48:59347 // True if PKP is bypassed due to a local trust anchor.
348 bool pkp_bypassed_;
349
Carlos IL81133382017-12-06 17:18:45350 // True if there was a certificate error which should be treated as fatal,
351 // and false otherwise.
352 bool is_fatal_cert_error_;
353
tfarina428341112016-09-22 13:38:20354 NetLogWithSource net_log_;
svaldeze83af292016-04-26 14:33:37355 base::WeakPtrFactory<SSLClientSocketImpl> weak_factory_;
David Benjamine34d74242017-06-29 20:35:16356
357 DISALLOW_COPY_AND_ASSIGN(SSLClientSocketImpl);
[email protected]d518cd92010-09-29 12:27:44358};
359
360} // namespace net
361
svaldeze83af292016-04-26 14:33:37362#endif // NET_SOCKET_SSL_CLIENT_SOCKET_IMPL_H_