blob: de677ba37138ce3742f0ff0edfcea5280d541c68 [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"
davidben2a811e4e2015-12-01 10:49:3417#include "base/memory/ref_counted.h"
[email protected]be90ba32013-05-13 20:05:2518#include "base/memory/weak_ptr.h"
davidben2a811e4e2015-12-01 10:49:3419#include "build/build_config.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"
tfarinae8cb8aa2016-10-21 02:44:0134#include "third_party/boringssl/src/include/openssl/base.h"
35#include "third_party/boringssl/src/include/openssl/ssl.h"
[email protected]d518cd92010-09-29 12:27:4436
davidben2a811e4e2015-12-01 10:49:3437namespace base {
38class FilePath;
39class SequencedTaskRunner;
40}
41
davidbenfe132d92016-09-27 18:07:2142namespace crypto {
43class OpenSSLErrStackTracer;
44}
45
[email protected]d518cd92010-09-29 12:27:4446namespace net {
47
[email protected]170e76c2010-10-04 15:04:2048class CertVerifier;
davidbeneb5f8ef32014-09-04 14:14:3249class CTVerifier;
davidben3418e81f2016-10-19 00:09:4550class SocketBIOAdapter;
[email protected]d518cd92010-09-29 12:27:4451class SSLCertRequestInfo;
[email protected]d518cd92010-09-29 12:27:4452class SSLInfo;
53
nharper78e6d2b2016-09-21 05:42:3554using TokenBindingSignatureMap =
55 base::MRUCache<std::pair<TokenBindingType, std::string>,
56 std::vector<uint8_t>>;
nharperb7441ef2016-01-25 23:54:1457
davidben3418e81f2016-10-19 00:09:4558class SSLClientSocketImpl : public SSLClientSocket,
59 public SocketBIOAdapter::Delegate {
[email protected]d518cd92010-09-29 12:27:4460 public:
61 // Takes ownership of the transport_socket, which may already be connected.
62 // The given hostname will be compared with the name(s) in the server's
63 // certificate during the SSL handshake. ssl_config specifies the SSL
64 // settings.
svaldeze83af292016-04-26 14:33:3765 SSLClientSocketImpl(std::unique_ptr<ClientSocketHandle> transport_socket,
66 const HostPortPair& host_and_port,
67 const SSLConfig& ssl_config,
68 const SSLClientSocketContext& context);
69 ~SSLClientSocketImpl() override;
[email protected]d518cd92010-09-29 12:27:4470
[email protected]fbef13932010-11-23 12:38:5371 const HostPortPair& host_and_port() const { return host_and_port_; }
[email protected]c3456bb2011-12-12 22:22:1972 const std::string& ssl_session_cache_shard() const {
73 return ssl_session_cache_shard_;
74 }
[email protected]fbef13932010-11-23 12:38:5375
davidben2a811e4e2015-12-01 10:49:3476#if !defined(OS_NACL)
77 // Log SSL key material to |path| on |task_runner|. Must be called before any
78 // SSLClientSockets are created.
79 static void SetSSLKeyLogFile(
80 const base::FilePath& path,
81 const scoped_refptr<base::SequencedTaskRunner>& task_runner);
82#endif
zhongyi81f85c6d92015-10-16 19:34:1483
[email protected]dbf036f2011-12-06 23:33:2484 // SSLClientSocket implementation.
dchengb03027d2014-10-21 12:00:2085 void GetSSLCertRequestInfo(SSLCertRequestInfo* cert_request_info) override;
dchengb03027d2014-10-21 12:00:2086 ChannelIDService* GetChannelIDService() const override;
nharper78e6d2b2016-09-21 05:42:3587 Error GetTokenBindingSignature(crypto::ECPrivateKey* key,
88 TokenBindingType tb_type,
89 std::vector<uint8_t>* out) override;
nharperb36644f2016-02-22 23:14:4390 crypto::ECPrivateKey* GetChannelIDKey() const override;
[email protected]81ec7c12012-07-31 18:32:1991
92 // SSLSocket implementation.
dchengb03027d2014-10-21 12:00:2093 int ExportKeyingMaterial(const base::StringPiece& label,
94 bool has_context,
95 const base::StringPiece& context,
96 unsigned char* out,
97 unsigned int outlen) override;
[email protected]d518cd92010-09-29 12:27:4498
[email protected]dbf036f2011-12-06 23:33:2499 // StreamSocket implementation.
dchengb03027d2014-10-21 12:00:20100 int Connect(const CompletionCallback& callback) override;
101 void Disconnect() override;
102 bool IsConnected() const override;
103 bool IsConnectedAndIdle() const override;
104 int GetPeerAddress(IPEndPoint* address) const override;
105 int GetLocalAddress(IPEndPoint* address) const override;
tfarina428341112016-09-22 13:38:20106 const NetLogWithSource& NetLog() const override;
dchengb03027d2014-10-21 12:00:20107 void SetSubresourceSpeculation() override;
108 void SetOmniboxSpeculation() override;
109 bool WasEverUsed() const override;
bnc3cf2a592016-08-11 14:48:36110 bool WasNpnNegotiated() const override;
111 NextProto GetNegotiatedProtocol() const override;
dchengb03027d2014-10-21 12:00:20112 bool GetSSLInfo(SSLInfo* ssl_info) override;
ttuttle23fdb7b2015-05-15 01:28:03113 void GetConnectionAttempts(ConnectionAttempts* out) const override;
114 void ClearConnectionAttempts() override {}
115 void AddConnectionAttempts(const ConnectionAttempts& attempts) override {}
tbansalf82cc8e2015-10-14 20:05:49116 int64_t GetTotalReceivedBytes() const override;
[email protected]d518cd92010-09-29 12:27:44117
[email protected]dbf036f2011-12-06 23:33:24118 // Socket implementation.
dchengb03027d2014-10-21 12:00:20119 int Read(IOBuffer* buf,
120 int buf_len,
121 const CompletionCallback& callback) override;
122 int Write(IOBuffer* buf,
123 int buf_len,
124 const CompletionCallback& callback) override;
Avi Drissman13fc8932015-12-20 04:40:46125 int SetReceiveBufferSize(int32_t size) override;
126 int SetSendBufferSize(int32_t size) override;
[email protected]d518cd92010-09-29 12:27:44127
davidben3418e81f2016-10-19 00:09:45128 // SocketBIOAdapter implementation:
129 void OnReadReady() override;
130 void OnWriteReady() override;
131
[email protected]d518cd92010-09-29 12:27:44132 private:
[email protected]7f38da8a2014-03-17 16:44:26133 class PeerCertificateChain;
[email protected]821e3bb2013-11-08 01:06:01134 class SSLContext;
135 friend class SSLClientSocket;
136 friend class SSLContext;
137
[email protected]c8a80e92014-05-17 16:02:08138 int Init();
[email protected]d518cd92010-09-29 12:27:44139 void DoReadCallback(int result);
140 void DoWriteCallback(int result);
141
[email protected]d518cd92010-09-29 12:27:44142 int DoHandshake();
davidbenc4212c02015-05-12 22:30:18143 int DoHandshakeComplete(int result);
[email protected]faff9852014-06-21 06:13:46144 int DoChannelIDLookup();
145 int DoChannelIDLookupComplete(int result);
[email protected]170e76c2010-10-04 15:04:20146 int DoVerifyCert(int result);
147 int DoVerifyCertComplete(int result);
[email protected]d518cd92010-09-29 12:27:44148 void DoConnectCallback(int result);
davidben30798ed82014-09-19 19:28:20149 void UpdateServerCert();
[email protected]d518cd92010-09-29 12:27:44150
151 void OnHandshakeIOComplete(int result);
[email protected]d518cd92010-09-29 12:27:44152
153 int DoHandshakeLoop(int last_io_result);
[email protected]d518cd92010-09-29 12:27:44154 int DoPayloadRead();
155 int DoPayloadWrite();
156
davidben1d489522015-07-01 18:48:46157 // Called when an asynchronous event completes which may have blocked the
davidben3418e81f2016-10-19 00:09:45158 // pending Connect, Read or Write calls, if any. Retries all state machines
159 // and, if complete, runs the respective callbacks.
160 void RetryAllOperations();
davidben1d489522015-07-01 18:48:46161
rsleevi4a6ca8c2016-06-24 03:05:22162 int VerifyCT();
[email protected]d518cd92010-09-29 12:27:44163
[email protected]821e3bb2013-11-08 01:06:01164 // Callback from the SSL layer that indicates the remote server is requesting
165 // a certificate for this client.
[email protected]82c59022014-08-15 09:38:27166 int ClientCertRequestCallback(SSL* ssl);
[email protected]821e3bb2013-11-08 01:06:01167
[email protected]b051cdb62014-02-28 02:20:16168 // CertVerifyCallback is called to verify the server's certificates. We do
169 // verification after the handshake so this function only enforces that the
170 // certificates don't change during renegotiation.
svaldeze83af292016-04-26 14:33:37171 int CertVerifyCallback(X509_STORE_CTX* store_ctx);
[email protected]b051cdb62014-02-28 02:20:16172
davidbendafe4e52015-04-08 22:53:52173 // Called after the initial handshake completes and after the server
174 // certificate has been verified. The order of handshake completion and
175 // certificate verification depends on whether the connection was false
176 // started. After both have happened (thus calling this twice), the session is
177 // safe to cache and will be cached.
178 void MaybeCacheSession();
179
davidben44aeae62015-06-24 20:47:43180 // Called from the SSL layer whenever a new session is established.
181 int NewSessionCallback(SSL_SESSION* session);
davidbendafe4e52015-04-08 22:53:52182
estark723b5eeb2016-02-18 21:01:12183 // Adds the Certificate Transparency info from ct_verify_result_ to
184 // |ssl_info|.
davidbeneb5f8ef32014-09-04 14:14:32185 // SCTs are held in three separate vectors in ct_verify_result, each
186 // vetor representing a particular verification state, this method associates
187 // each of the SCTs with the corresponding SCTVerifyStatus as it adds it to
188 // the |ssl_info|.signed_certificate_timestamps list.
estark723b5eeb2016-02-18 21:01:12189 void AddCTInfoToSSLInfo(SSLInfo* ssl_info) const;
davidbeneb5f8ef32014-09-04 14:14:32190
rsleevif020edc2015-03-16 19:31:24191 // Returns a unique key string for the SSL session cache for
192 // this socket.
193 std::string GetSessionCacheKey() const;
194
davidben421116c2015-05-12 19:56:51195 // Returns true if renegotiations are allowed.
196 bool IsRenegotiationAllowed() const;
197
davidben1d489522015-07-01 18:48:46198 // Callbacks for operations with the private key.
199 int PrivateKeyTypeCallback();
davidben1d489522015-07-01 18:48:46200 size_t PrivateKeyMaxSignatureLenCallback();
davidben0bca07fd2016-07-18 15:12:03201 ssl_private_key_result_t PrivateKeySignDigestCallback(uint8_t* out,
202 size_t* out_len,
203 size_t max_out,
204 const EVP_MD* md,
205 const uint8_t* in,
206 size_t in_len);
207 ssl_private_key_result_t PrivateKeyCompleteCallback(uint8_t* out,
208 size_t* out_len,
209 size_t max_out);
davidben1d489522015-07-01 18:48:46210
davidben0bca07fd2016-07-18 15:12:03211 void OnPrivateKeyComplete(Error error, const std::vector<uint8_t>& signature);
davidben1d489522015-07-01 18:48:46212
nharper736ceda2015-11-07 00:16:59213 int TokenBindingAdd(const uint8_t** out,
214 size_t* out_len,
215 int* out_alert_value);
216 int TokenBindingParse(const uint8_t* contents,
217 size_t contents_len,
218 int* out_alert_value);
219
davidben281d13f02016-04-27 20:43:28220 void LogConnectEndEvent(int rv);
221
bncbd442c22016-09-14 20:49:16222 // Record whether ALPN was used, and if so, the negotiated protocol,
223 // in a UMA histogram.
224 void RecordNegotiatedProtocol() const;
bnc3cf2a592016-08-11 14:48:36225
226 // Records histograms for channel id support during full handshakes - resumed
227 // handshakes are ignored.
228 void RecordChannelIDSupport() const;
229
230 // Returns whether TLS channel ID is enabled.
231 bool IsChannelIDEnabled() const;
232
davidbenfe132d92016-09-27 18:07:21233 // Returns the net error corresponding to the most recent OpenSSL
234 // error. ssl_error is the output of SSL_get_error.
235 int MapLastOpenSSLError(int ssl_error,
236 const crypto::OpenSSLErrStackTracer& tracer,
237 OpenSSLErrorInfo* info);
238
[email protected]dbf036f2011-12-06 23:33:24239 CompletionCallback user_connect_callback_;
[email protected]3f55aa12011-12-07 02:03:33240 CompletionCallback user_read_callback_;
[email protected]83039bb2011-12-09 18:43:55241 CompletionCallback user_write_callback_;
[email protected]d518cd92010-09-29 12:27:44242
243 // Used by Read function.
244 scoped_refptr<IOBuffer> user_read_buf_;
245 int user_read_buf_len_;
246
247 // Used by Write function.
248 scoped_refptr<IOBuffer> user_write_buf_;
249 int user_write_buf_len_;
250
[email protected]4b768562013-02-16 04:10:07251 // Used by DoPayloadRead() when attempting to fill the caller's buffer with
252 // as much data as possible without blocking.
253 // If DoPayloadRead() encounters an error after having read some data, stores
254 // the result to return on the *next* call to DoPayloadRead(). A value > 0
255 // indicates there is no pending result, otherwise 0 indicates EOF and < 0
256 // indicates an error.
257 int pending_read_error_;
258
davidbenb8c23212014-10-28 00:12:16259 // If there is a pending read result, the OpenSSL result code (output of
260 // SSL_get_error) associated with it.
261 int pending_read_ssl_error_;
262
263 // If there is a pending read result, the OpenSSLErrorInfo associated with it.
264 OpenSSLErrorInfo pending_read_error_info_;
265
[email protected]64b5c892014-08-08 09:39:26266 // Set when Connect finishes.
danakj655b66c2016-04-16 00:51:38267 std::unique_ptr<PeerCertificateChain> server_cert_chain_;
[email protected]170e76c2010-10-04 15:04:20268 scoped_refptr<X509Certificate> server_cert_;
269 CertVerifyResult server_cert_verify_result_;
dadriand476e652016-07-26 21:33:24270 std::string ocsp_response_;
[email protected]64b5c892014-08-08 09:39:26271 bool completed_connect_;
[email protected]170e76c2010-10-04 15:04:20272
[email protected]0dc88b32014-03-26 20:12:28273 // Set when Read() or Write() successfully reads or writes data to or from the
274 // network.
275 bool was_ever_used_;
276
[email protected]515adc22013-01-09 16:01:23277 // List of DER-encoded X.509 DistinguishedName of certificate authorities
278 // allowed by the server.
279 std::vector<std::string> cert_authorities_;
[email protected]c0787702014-05-20 21:51:44280 // List of SSLClientCertType values for client certificates allowed by the
281 // server.
282 std::vector<SSLClientCertType> cert_key_types_;
[email protected]d518cd92010-09-29 12:27:44283
[email protected]822581d2010-12-16 17:27:15284 CertVerifier* const cert_verifier_;
danakj655b66c2016-04-16 00:51:38285 std::unique_ptr<CertVerifier::Request> cert_verifier_request_;
davidben09c3d072014-08-25 20:33:58286 base::TimeTicks start_cert_verification_time_;
[email protected]170e76c2010-10-04 15:04:20287
davidbeneb5f8ef32014-09-04 14:14:32288 // Certificate Transparency: Verifier and result holder.
289 ct::CTVerifyResult ct_verify_result_;
290 CTVerifier* cert_transparency_verifier_;
291
[email protected]ee0f2aa82013-10-25 11:59:26292 // The service for retrieving Channel ID keys. May be NULL.
[email protected]6b8a3c742014-07-25 00:25:35293 ChannelIDService* channel_id_service_;
nharper736ceda2015-11-07 00:16:59294 bool tb_was_negotiated_;
295 TokenBindingParam tb_negotiated_param_;
nharper78e6d2b2016-09-21 05:42:35296 TokenBindingSignatureMap tb_signature_map_;
[email protected]ee0f2aa82013-10-25 11:59:26297
[email protected]d518cd92010-09-29 12:27:44298 // OpenSSL stuff
davidbend80c12c2016-10-11 00:13:49299 bssl::UniquePtr<SSL> ssl_;
[email protected]d518cd92010-09-29 12:27:44300
danakj655b66c2016-04-16 00:51:38301 std::unique_ptr<ClientSocketHandle> transport_;
davidben3418e81f2016-10-19 00:09:45302 std::unique_ptr<SocketBIOAdapter> transport_adapter_;
[email protected]055d7f22010-11-15 12:03:12303 const HostPortPair host_and_port_;
[email protected]d518cd92010-09-29 12:27:44304 SSLConfig ssl_config_;
[email protected]c3456bb2011-12-12 22:22:19305 // ssl_session_cache_shard_ is an opaque string that partitions the SSL
306 // session cache. i.e. sessions created with one value will not attempt to
307 // resume on the socket with a different value.
308 const std::string ssl_session_cache_shard_;
[email protected]d518cd92010-09-29 12:27:44309
[email protected]d518cd92010-09-29 12:27:44310 enum State {
311 STATE_NONE,
312 STATE_HANDSHAKE,
davidbenc4212c02015-05-12 22:30:18313 STATE_HANDSHAKE_COMPLETE,
[email protected]faff9852014-06-21 06:13:46314 STATE_CHANNEL_ID_LOOKUP,
315 STATE_CHANNEL_ID_LOOKUP_COMPLETE,
[email protected]d518cd92010-09-29 12:27:44316 STATE_VERIFY_CERT,
317 STATE_VERIFY_CERT_COMPLETE,
318 };
319 State next_handshake_state_;
svaldez4af14d22015-08-20 13:48:24320
321 // True if the socket has been disconnected.
322 bool disconnected_;
323
bnc3cf2a592016-08-11 14:48:36324 NextProto negotiated_protocol_;
[email protected]6b8a3c742014-07-25 00:25:35325 // Written by the |channel_id_service_|.
danakj655b66c2016-04-16 00:51:38326 std::unique_ptr<crypto::ECPrivateKey> channel_id_key_;
davidben52053b382015-04-27 19:22:29327 // True if a channel ID was sent.
328 bool channel_id_sent_;
davidbenc269cc4b2016-07-27 14:55:03329 // If non-null, the newly-established to be inserted into the session cache
330 // once certificate verification is done.
davidbend80c12c2016-10-11 00:13:49331 bssl::UniquePtr<SSL_SESSION> pending_session_;
davidbendafe4e52015-04-08 22:53:52332 // True if the initial handshake's certificate has been verified.
333 bool certificate_verified_;
davidbenfe132d92016-09-27 18:07:21334 // Set to true if a CertificateRequest was received.
335 bool certificate_requested_;
[email protected]6b8a3c742014-07-25 00:25:35336 // The request handle for |channel_id_service_|.
nharper75ade892015-06-10 19:05:35337 ChannelIDService::Request channel_id_request_;
[email protected]8bd4e7a2014-08-09 14:49:17338
davidben1d489522015-07-01 18:48:46339 int signature_result_;
340 std::vector<uint8_t> signature_;
341
[email protected]8bd4e7a2014-08-09 14:49:17342 TransportSecurityState* transport_security_state_;
343
estark6f9b3d82016-01-12 21:37:05344 CTPolicyEnforcer* const policy_enforcer_;
eranm6571b2b2014-12-03 15:53:23345
[email protected]8bd4e7a2014-08-09 14:49:17346 // pinning_failure_log contains a message produced by
347 // TransportSecurityState::CheckPublicKeyPins in the event of a
348 // pinning failure. It is a (somewhat) human-readable string.
349 std::string pinning_failure_log_;
350
dadriandf302c42016-06-10 18:48:59351 // True if PKP is bypassed due to a local trust anchor.
352 bool pkp_bypassed_;
353
tfarina428341112016-09-22 13:38:20354 NetLogWithSource net_log_;
svaldeze83af292016-04-26 14:33:37355 base::WeakPtrFactory<SSLClientSocketImpl> weak_factory_;
[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_