blob: 017ff5854bc16e8c874e5955fd2afabdfb114170 [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
davidben1d489522015-07-01 18:48:468#include <openssl/base.h>
9#include <openssl/ssl.h>
Avi Drissman13fc8932015-12-20 04:40:4610#include <stddef.h>
tbansalf82cc8e2015-10-14 20:05:4911#include <stdint.h>
davidben1d489522015-07-01 18:48:4612
danakj655b66c2016-04-16 00:51:3813#include <memory>
[email protected]ea4a1c6a2010-12-09 13:33:2814#include <string>
davidben1d489522015-07-01 18:48:4615#include <vector>
[email protected]ea4a1c6a2010-12-09 13:33:2816
nharperb7441ef2016-01-25 23:54:1417#include "base/compiler_specific.h"
18#include "base/containers/mru_cache.h"
davidben2a811e4e2015-12-01 10:49:3419#include "base/memory/ref_counted.h"
[email protected]be90ba32013-05-13 20:05:2520#include "base/memory/weak_ptr.h"
davidben2a811e4e2015-12-01 10:49:3421#include "build/build_config.h"
[email protected]d518cd92010-09-29 12:27:4422#include "net/base/completion_callback.h"
23#include "net/base/io_buffer.h"
eroman7f9236a2015-05-11 21:23:4324#include "net/cert/cert_verifier.h"
[email protected]6e7845ae2013-03-29 21:48:1125#include "net/cert/cert_verify_result.h"
davidbeneb5f8ef32014-09-04 14:14:3226#include "net/cert/ct_verify_result.h"
[email protected]d518cd92010-09-29 12:27:4427#include "net/socket/client_socket_handle.h"
[email protected]536fd0b2013-03-14 17:41:5728#include "net/socket/ssl_client_socket.h"
[email protected]6b8a3c742014-07-25 00:25:3529#include "net/ssl/channel_id_service.h"
davidbenb8c23212014-10-28 00:12:1630#include "net/ssl/openssl_ssl_util.h"
davidbenc269cc4b2016-07-27 14:55:0331#include "net/ssl/scoped_openssl_types.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]d518cd92010-09-29 12:27:4434
davidben2a811e4e2015-12-01 10:49:3435namespace base {
36class FilePath;
37class SequencedTaskRunner;
38}
39
[email protected]d518cd92010-09-29 12:27:4440namespace net {
41
[email protected]170e76c2010-10-04 15:04:2042class CertVerifier;
davidbeneb5f8ef32014-09-04 14:14:3243class CTVerifier;
[email protected]d518cd92010-09-29 12:27:4444class SSLCertRequestInfo;
[email protected]d518cd92010-09-29 12:27:4445class SSLInfo;
46
nharperb7441ef2016-01-25 23:54:1447using SignedEkmMap = base::MRUCache<std::string, std::vector<uint8_t>>;
48
svaldeze83af292016-04-26 14:33:3749class SSLClientSocketImpl : public SSLClientSocket {
[email protected]d518cd92010-09-29 12:27:4450 public:
51 // Takes ownership of the transport_socket, which may already be connected.
52 // The given hostname will be compared with the name(s) in the server's
53 // certificate during the SSL handshake. ssl_config specifies the SSL
54 // settings.
svaldeze83af292016-04-26 14:33:3755 SSLClientSocketImpl(std::unique_ptr<ClientSocketHandle> transport_socket,
56 const HostPortPair& host_and_port,
57 const SSLConfig& ssl_config,
58 const SSLClientSocketContext& context);
59 ~SSLClientSocketImpl() override;
[email protected]d518cd92010-09-29 12:27:4460
[email protected]fbef13932010-11-23 12:38:5361 const HostPortPair& host_and_port() const { return host_and_port_; }
[email protected]c3456bb2011-12-12 22:22:1962 const std::string& ssl_session_cache_shard() const {
63 return ssl_session_cache_shard_;
64 }
[email protected]fbef13932010-11-23 12:38:5365
davidben2a811e4e2015-12-01 10:49:3466#if !defined(OS_NACL)
67 // Log SSL key material to |path| on |task_runner|. Must be called before any
68 // SSLClientSockets are created.
69 static void SetSSLKeyLogFile(
70 const base::FilePath& path,
71 const scoped_refptr<base::SequencedTaskRunner>& task_runner);
72#endif
zhongyi81f85c6d92015-10-16 19:34:1473
[email protected]dbf036f2011-12-06 23:33:2474 // SSLClientSocket implementation.
dchengb03027d2014-10-21 12:00:2075 void GetSSLCertRequestInfo(SSLCertRequestInfo* cert_request_info) override;
dchengb03027d2014-10-21 12:00:2076 ChannelIDService* GetChannelIDService() const override;
nharperb7441ef2016-01-25 23:54:1477 Error GetSignedEKMForTokenBinding(crypto::ECPrivateKey* key,
78 std::vector<uint8_t>* out) override;
nharperb36644f2016-02-22 23:14:4379 crypto::ECPrivateKey* GetChannelIDKey() const override;
[email protected]81ec7c12012-07-31 18:32:1980
81 // SSLSocket implementation.
dchengb03027d2014-10-21 12:00:2082 int ExportKeyingMaterial(const base::StringPiece& label,
83 bool has_context,
84 const base::StringPiece& context,
85 unsigned char* out,
86 unsigned int outlen) override;
[email protected]d518cd92010-09-29 12:27:4487
[email protected]dbf036f2011-12-06 23:33:2488 // StreamSocket implementation.
dchengb03027d2014-10-21 12:00:2089 int Connect(const CompletionCallback& callback) override;
90 void Disconnect() override;
91 bool IsConnected() const override;
92 bool IsConnectedAndIdle() const override;
93 int GetPeerAddress(IPEndPoint* address) const override;
94 int GetLocalAddress(IPEndPoint* address) const override;
95 const BoundNetLog& NetLog() const override;
96 void SetSubresourceSpeculation() override;
97 void SetOmniboxSpeculation() override;
98 bool WasEverUsed() const override;
bnc3cf2a592016-08-11 14:48:3699 bool WasNpnNegotiated() const override;
100 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;
[email protected]d518cd92010-09-29 12:27:44106
[email protected]dbf036f2011-12-06 23:33:24107 // Socket implementation.
dchengb03027d2014-10-21 12:00:20108 int Read(IOBuffer* buf,
109 int buf_len,
110 const CompletionCallback& callback) override;
111 int Write(IOBuffer* buf,
112 int buf_len,
113 const CompletionCallback& callback) override;
Avi Drissman13fc8932015-12-20 04:40:46114 int SetReceiveBufferSize(int32_t size) override;
115 int SetSendBufferSize(int32_t size) override;
[email protected]d518cd92010-09-29 12:27:44116
117 private:
[email protected]7f38da8a2014-03-17 16:44:26118 class PeerCertificateChain;
[email protected]821e3bb2013-11-08 01:06:01119 class SSLContext;
120 friend class SSLClientSocket;
121 friend class SSLContext;
122
[email protected]c8a80e92014-05-17 16:02:08123 int Init();
[email protected]d518cd92010-09-29 12:27:44124 void DoReadCallback(int result);
125 void DoWriteCallback(int result);
126
127 bool DoTransportIO();
128 int DoHandshake();
davidbenc4212c02015-05-12 22:30:18129 int DoHandshakeComplete(int result);
[email protected]faff9852014-06-21 06:13:46130 int DoChannelIDLookup();
131 int DoChannelIDLookupComplete(int result);
[email protected]170e76c2010-10-04 15:04:20132 int DoVerifyCert(int result);
133 int DoVerifyCertComplete(int result);
[email protected]d518cd92010-09-29 12:27:44134 void DoConnectCallback(int result);
davidben30798ed82014-09-19 19:28:20135 void UpdateServerCert();
[email protected]d518cd92010-09-29 12:27:44136
137 void OnHandshakeIOComplete(int result);
138 void OnSendComplete(int result);
139 void OnRecvComplete(int result);
140
141 int DoHandshakeLoop(int last_io_result);
davidben1b133ad2014-10-23 04:23:13142 int DoReadLoop();
143 int DoWriteLoop();
[email protected]d518cd92010-09-29 12:27:44144 int DoPayloadRead();
145 int DoPayloadWrite();
146
davidben1d489522015-07-01 18:48:46147 // Called when an asynchronous event completes which may have blocked the
148 // pending Read or Write calls, if any. Retries both state machines and, if
149 // complete, runs the respective callbacks.
150 void PumpReadWriteEvents();
151
[email protected]d518cd92010-09-29 12:27:44152 int BufferSend();
153 int BufferRecv();
154 void BufferSendComplete(int result);
155 void BufferRecvComplete(int result);
156 void TransportWriteComplete(int result);
[email protected]3e5c6922014-02-06 02:42:16157 int TransportReadComplete(int result);
rsleevi4a6ca8c2016-06-24 03:05:22158 int VerifyCT();
[email protected]d518cd92010-09-29 12:27:44159
[email protected]821e3bb2013-11-08 01:06:01160 // Callback from the SSL layer that indicates the remote server is requesting
161 // a certificate for this client.
[email protected]82c59022014-08-15 09:38:27162 int ClientCertRequestCallback(SSL* ssl);
[email protected]821e3bb2013-11-08 01:06:01163
[email protected]b051cdb62014-02-28 02:20:16164 // CertVerifyCallback is called to verify the server's certificates. We do
165 // verification after the handshake so this function only enforces that the
166 // certificates don't change during renegotiation.
svaldeze83af292016-04-26 14:33:37167 int CertVerifyCallback(X509_STORE_CTX* store_ctx);
[email protected]b051cdb62014-02-28 02:20:16168
[email protected]821e3bb2013-11-08 01:06:01169 // Callback from the SSL layer to check which NPN protocol we are supporting
svaldeze83af292016-04-26 14:33:37170 int SelectNextProtoCallback(unsigned char** out,
171 unsigned char* outlen,
172 const unsigned char* in,
173 unsigned int inlen);
[email protected]821e3bb2013-11-08 01:06:01174
[email protected]5aea79182014-07-14 20:43:41175 // Called during an operation on |transport_bio_|'s peer. Checks saved
176 // transport error state and, if appropriate, returns an error through
177 // OpenSSL's error system.
svaldeze83af292016-04-26 14:33:37178 long MaybeReplayTransportError(BIO* bio,
[email protected]5aea79182014-07-14 20:43:41179 int cmd,
svaldeze83af292016-04-26 14:33:37180 const char* argp,
181 int argi,
182 long argl,
[email protected]5aea79182014-07-14 20:43:41183 long retvalue);
184
185 // Callback from the SSL layer when an operation is performed on
186 // |transport_bio_|'s peer.
svaldeze83af292016-04-26 14:33:37187 static long BIOCallback(BIO* bio,
[email protected]5aea79182014-07-14 20:43:41188 int cmd,
svaldeze83af292016-04-26 14:33:37189 const char* argp,
190 int argi,
191 long argl,
[email protected]5aea79182014-07-14 20:43:41192 long retvalue);
193
davidbendafe4e52015-04-08 22:53:52194 // Called after the initial handshake completes and after the server
195 // certificate has been verified. The order of handshake completion and
196 // certificate verification depends on whether the connection was false
197 // started. After both have happened (thus calling this twice), the session is
198 // safe to cache and will be cached.
199 void MaybeCacheSession();
200
davidben44aeae62015-06-24 20:47:43201 // Called from the SSL layer whenever a new session is established.
202 int NewSessionCallback(SSL_SESSION* session);
davidbendafe4e52015-04-08 22:53:52203
estark723b5eeb2016-02-18 21:01:12204 // Adds the Certificate Transparency info from ct_verify_result_ to
205 // |ssl_info|.
davidbeneb5f8ef32014-09-04 14:14:32206 // SCTs are held in three separate vectors in ct_verify_result, each
207 // vetor representing a particular verification state, this method associates
208 // each of the SCTs with the corresponding SCTVerifyStatus as it adds it to
209 // the |ssl_info|.signed_certificate_timestamps list.
estark723b5eeb2016-02-18 21:01:12210 void AddCTInfoToSSLInfo(SSLInfo* ssl_info) const;
davidbeneb5f8ef32014-09-04 14:14:32211
rsleevif020edc2015-03-16 19:31:24212 // Returns a unique key string for the SSL session cache for
213 // this socket.
214 std::string GetSessionCacheKey() const;
215
davidben421116c2015-05-12 19:56:51216 // Returns true if renegotiations are allowed.
217 bool IsRenegotiationAllowed() const;
218
davidben1d489522015-07-01 18:48:46219 // Callbacks for operations with the private key.
220 int PrivateKeyTypeCallback();
davidben1d489522015-07-01 18:48:46221 size_t PrivateKeyMaxSignatureLenCallback();
davidben0bca07fd2016-07-18 15:12:03222 ssl_private_key_result_t PrivateKeySignDigestCallback(uint8_t* out,
223 size_t* out_len,
224 size_t max_out,
225 const EVP_MD* md,
226 const uint8_t* in,
227 size_t in_len);
228 ssl_private_key_result_t PrivateKeyCompleteCallback(uint8_t* out,
229 size_t* out_len,
230 size_t max_out);
davidben1d489522015-07-01 18:48:46231
davidben0bca07fd2016-07-18 15:12:03232 void OnPrivateKeyComplete(Error error, const std::vector<uint8_t>& signature);
davidben1d489522015-07-01 18:48:46233
nharper736ceda2015-11-07 00:16:59234 int TokenBindingAdd(const uint8_t** out,
235 size_t* out_len,
236 int* out_alert_value);
237 int TokenBindingParse(const uint8_t* contents,
238 size_t contents_len,
239 int* out_alert_value);
240
davidben281d13f02016-04-27 20:43:28241 void LogConnectEndEvent(int rv);
242
bncbd442c22016-09-14 20:49:16243 // Record whether ALPN was used, and if so, the negotiated protocol,
244 // in a UMA histogram.
245 void RecordNegotiatedProtocol() const;
bnc3cf2a592016-08-11 14:48:36246
247 // Records histograms for channel id support during full handshakes - resumed
248 // handshakes are ignored.
249 void RecordChannelIDSupport() const;
250
251 // Returns whether TLS channel ID is enabled.
252 bool IsChannelIDEnabled() const;
253
[email protected]d518cd92010-09-29 12:27:44254 bool transport_send_busy_;
[email protected]d518cd92010-09-29 12:27:44255 bool transport_recv_busy_;
[email protected]4b768562013-02-16 04:10:07256
svaldeze83af292016-04-26 14:33:37257 // Buffers which are shared by BoringSSL and SSLClientSocketImpl.
haavardm2d92e722014-12-19 13:45:44258 // GrowableIOBuffer is used to keep ownership and setting offset.
259 scoped_refptr<GrowableIOBuffer> send_buffer_;
260 scoped_refptr<GrowableIOBuffer> recv_buffer_;
[email protected]d518cd92010-09-29 12:27:44261
[email protected]dbf036f2011-12-06 23:33:24262 CompletionCallback user_connect_callback_;
[email protected]3f55aa12011-12-07 02:03:33263 CompletionCallback user_read_callback_;
[email protected]83039bb2011-12-09 18:43:55264 CompletionCallback user_write_callback_;
[email protected]d518cd92010-09-29 12:27:44265
266 // Used by Read function.
267 scoped_refptr<IOBuffer> user_read_buf_;
268 int user_read_buf_len_;
269
270 // Used by Write function.
271 scoped_refptr<IOBuffer> user_write_buf_;
272 int user_write_buf_len_;
273
[email protected]4b768562013-02-16 04:10:07274 // Used by DoPayloadRead() when attempting to fill the caller's buffer with
275 // as much data as possible without blocking.
276 // If DoPayloadRead() encounters an error after having read some data, stores
277 // the result to return on the *next* call to DoPayloadRead(). A value > 0
278 // indicates there is no pending result, otherwise 0 indicates EOF and < 0
279 // indicates an error.
280 int pending_read_error_;
281
davidbenb8c23212014-10-28 00:12:16282 // If there is a pending read result, the OpenSSL result code (output of
283 // SSL_get_error) associated with it.
284 int pending_read_ssl_error_;
285
286 // If there is a pending read result, the OpenSSLErrorInfo associated with it.
287 OpenSSLErrorInfo pending_read_error_info_;
288
[email protected]5aea79182014-07-14 20:43:41289 // Used by TransportReadComplete() to signify an error reading from the
290 // transport socket. A value of OK indicates the socket is still
291 // readable. EOFs are mapped to ERR_CONNECTION_CLOSED.
292 int transport_read_error_;
293
[email protected]3e5c6922014-02-06 02:42:16294 // Used by TransportWriteComplete() and TransportReadComplete() to signify an
295 // error writing to the transport socket. A value of OK indicates no error.
296 int transport_write_error_;
297
[email protected]64b5c892014-08-08 09:39:26298 // Set when Connect finishes.
danakj655b66c2016-04-16 00:51:38299 std::unique_ptr<PeerCertificateChain> server_cert_chain_;
[email protected]170e76c2010-10-04 15:04:20300 scoped_refptr<X509Certificate> server_cert_;
301 CertVerifyResult server_cert_verify_result_;
dadriand476e652016-07-26 21:33:24302 std::string ocsp_response_;
[email protected]64b5c892014-08-08 09:39:26303 bool completed_connect_;
[email protected]170e76c2010-10-04 15:04:20304
[email protected]0dc88b32014-03-26 20:12:28305 // Set when Read() or Write() successfully reads or writes data to or from the
306 // network.
307 bool was_ever_used_;
308
[email protected]515adc22013-01-09 16:01:23309 // List of DER-encoded X.509 DistinguishedName of certificate authorities
310 // allowed by the server.
311 std::vector<std::string> cert_authorities_;
[email protected]c0787702014-05-20 21:51:44312 // List of SSLClientCertType values for client certificates allowed by the
313 // server.
314 std::vector<SSLClientCertType> cert_key_types_;
[email protected]d518cd92010-09-29 12:27:44315
[email protected]822581d2010-12-16 17:27:15316 CertVerifier* const cert_verifier_;
danakj655b66c2016-04-16 00:51:38317 std::unique_ptr<CertVerifier::Request> cert_verifier_request_;
davidben09c3d072014-08-25 20:33:58318 base::TimeTicks start_cert_verification_time_;
[email protected]170e76c2010-10-04 15:04:20319
davidbeneb5f8ef32014-09-04 14:14:32320 // Certificate Transparency: Verifier and result holder.
321 ct::CTVerifyResult ct_verify_result_;
322 CTVerifier* cert_transparency_verifier_;
323
[email protected]ee0f2aa82013-10-25 11:59:26324 // The service for retrieving Channel ID keys. May be NULL.
[email protected]6b8a3c742014-07-25 00:25:35325 ChannelIDService* channel_id_service_;
nharper736ceda2015-11-07 00:16:59326 bool tb_was_negotiated_;
327 TokenBindingParam tb_negotiated_param_;
nharperb7441ef2016-01-25 23:54:14328 SignedEkmMap tb_signed_ekm_map_;
[email protected]ee0f2aa82013-10-25 11:59:26329
[email protected]d518cd92010-09-29 12:27:44330 // OpenSSL stuff
[email protected]d518cd92010-09-29 12:27:44331 SSL* ssl_;
332 BIO* transport_bio_;
333
danakj655b66c2016-04-16 00:51:38334 std::unique_ptr<ClientSocketHandle> transport_;
[email protected]055d7f22010-11-15 12:03:12335 const HostPortPair host_and_port_;
[email protected]d518cd92010-09-29 12:27:44336 SSLConfig ssl_config_;
[email protected]c3456bb2011-12-12 22:22:19337 // ssl_session_cache_shard_ is an opaque string that partitions the SSL
338 // session cache. i.e. sessions created with one value will not attempt to
339 // resume on the socket with a different value.
340 const std::string ssl_session_cache_shard_;
[email protected]d518cd92010-09-29 12:27:44341
[email protected]d518cd92010-09-29 12:27:44342 enum State {
343 STATE_NONE,
344 STATE_HANDSHAKE,
davidbenc4212c02015-05-12 22:30:18345 STATE_HANDSHAKE_COMPLETE,
[email protected]faff9852014-06-21 06:13:46346 STATE_CHANNEL_ID_LOOKUP,
347 STATE_CHANNEL_ID_LOOKUP_COMPLETE,
[email protected]d518cd92010-09-29 12:27:44348 STATE_VERIFY_CERT,
349 STATE_VERIFY_CERT_COMPLETE,
350 };
351 State next_handshake_state_;
svaldez4af14d22015-08-20 13:48:24352
353 // True if the socket has been disconnected.
354 bool disconnected_;
355
[email protected]ea4a1c6a2010-12-09 13:33:28356 NextProtoStatus npn_status_;
bnc3cf2a592016-08-11 14:48:36357 NextProto negotiated_protocol_;
358 // Protocol negotiation extension used.
359 SSLNegotiationExtension negotiation_extension_;
[email protected]6b8a3c742014-07-25 00:25:35360 // Written by the |channel_id_service_|.
danakj655b66c2016-04-16 00:51:38361 std::unique_ptr<crypto::ECPrivateKey> channel_id_key_;
davidben52053b382015-04-27 19:22:29362 // True if a channel ID was sent.
363 bool channel_id_sent_;
davidbenc269cc4b2016-07-27 14:55:03364 // If non-null, the newly-established to be inserted into the session cache
365 // once certificate verification is done.
366 ScopedSSL_SESSION pending_session_;
davidbendafe4e52015-04-08 22:53:52367 // True if the initial handshake's certificate has been verified.
368 bool certificate_verified_;
[email protected]6b8a3c742014-07-25 00:25:35369 // The request handle for |channel_id_service_|.
nharper75ade892015-06-10 19:05:35370 ChannelIDService::Request channel_id_request_;
[email protected]8bd4e7a2014-08-09 14:49:17371
davidben1d489522015-07-01 18:48:46372 int signature_result_;
373 std::vector<uint8_t> signature_;
374
[email protected]8bd4e7a2014-08-09 14:49:17375 TransportSecurityState* transport_security_state_;
376
estark6f9b3d82016-01-12 21:37:05377 CTPolicyEnforcer* const policy_enforcer_;
eranm6571b2b2014-12-03 15:53:23378
[email protected]8bd4e7a2014-08-09 14:49:17379 // pinning_failure_log contains a message produced by
380 // TransportSecurityState::CheckPublicKeyPins in the event of a
381 // pinning failure. It is a (somewhat) human-readable string.
382 std::string pinning_failure_log_;
383
dadriandf302c42016-06-10 18:48:59384 // True if PKP is bypassed due to a local trust anchor.
385 bool pkp_bypassed_;
386
[email protected]d518cd92010-09-29 12:27:44387 BoundNetLog net_log_;
svaldeze83af292016-04-26 14:33:37388 base::WeakPtrFactory<SSLClientSocketImpl> weak_factory_;
[email protected]d518cd92010-09-29 12:27:44389};
390
391} // namespace net
392
svaldeze83af292016-04-26 14:33:37393#endif // NET_SOCKET_SSL_CLIENT_SOCKET_IMPL_H_