blob: 8c2482adff2094f5ffe84556a7b95102a1a5e7e2 [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
davidbenfe132d92016-09-27 18:07:2140namespace crypto {
41class OpenSSLErrStackTracer;
42}
43
[email protected]d518cd92010-09-29 12:27:4444namespace net {
45
[email protected]170e76c2010-10-04 15:04:2046class CertVerifier;
davidbeneb5f8ef32014-09-04 14:14:3247class CTVerifier;
[email protected]d518cd92010-09-29 12:27:4448class SSLCertRequestInfo;
[email protected]d518cd92010-09-29 12:27:4449class SSLInfo;
50
nharper78e6d2b2016-09-21 05:42:3551using TokenBindingSignatureMap =
52 base::MRUCache<std::pair<TokenBindingType, std::string>,
53 std::vector<uint8_t>>;
nharperb7441ef2016-01-25 23:54:1454
svaldeze83af292016-04-26 14:33:3755class SSLClientSocketImpl : public SSLClientSocket {
[email protected]d518cd92010-09-29 12:27:4456 public:
57 // Takes ownership of the transport_socket, which may already be connected.
58 // The given hostname will be compared with the name(s) in the server's
59 // certificate during the SSL handshake. ssl_config specifies the SSL
60 // settings.
svaldeze83af292016-04-26 14:33:3761 SSLClientSocketImpl(std::unique_ptr<ClientSocketHandle> transport_socket,
62 const HostPortPair& host_and_port,
63 const SSLConfig& ssl_config,
64 const SSLClientSocketContext& context);
65 ~SSLClientSocketImpl() override;
[email protected]d518cd92010-09-29 12:27:4466
[email protected]fbef13932010-11-23 12:38:5367 const HostPortPair& host_and_port() const { return host_and_port_; }
[email protected]c3456bb2011-12-12 22:22:1968 const std::string& ssl_session_cache_shard() const {
69 return ssl_session_cache_shard_;
70 }
[email protected]fbef13932010-11-23 12:38:5371
davidben2a811e4e2015-12-01 10:49:3472#if !defined(OS_NACL)
73 // Log SSL key material to |path| on |task_runner|. Must be called before any
74 // SSLClientSockets are created.
75 static void SetSSLKeyLogFile(
76 const base::FilePath& path,
77 const scoped_refptr<base::SequencedTaskRunner>& task_runner);
78#endif
zhongyi81f85c6d92015-10-16 19:34:1479
[email protected]dbf036f2011-12-06 23:33:2480 // SSLClientSocket implementation.
dchengb03027d2014-10-21 12:00:2081 void GetSSLCertRequestInfo(SSLCertRequestInfo* cert_request_info) override;
dchengb03027d2014-10-21 12:00:2082 ChannelIDService* GetChannelIDService() const override;
nharper78e6d2b2016-09-21 05:42:3583 Error GetTokenBindingSignature(crypto::ECPrivateKey* key,
84 TokenBindingType tb_type,
85 std::vector<uint8_t>* out) override;
nharperb36644f2016-02-22 23:14:4386 crypto::ECPrivateKey* GetChannelIDKey() const override;
[email protected]81ec7c12012-07-31 18:32:1987
88 // SSLSocket implementation.
dchengb03027d2014-10-21 12:00:2089 int ExportKeyingMaterial(const base::StringPiece& label,
90 bool has_context,
91 const base::StringPiece& context,
92 unsigned char* out,
93 unsigned int outlen) override;
[email protected]d518cd92010-09-29 12:27:4494
[email protected]dbf036f2011-12-06 23:33:2495 // StreamSocket implementation.
dchengb03027d2014-10-21 12:00:2096 int Connect(const CompletionCallback& callback) override;
97 void Disconnect() override;
98 bool IsConnected() const override;
99 bool IsConnectedAndIdle() const override;
100 int GetPeerAddress(IPEndPoint* address) const override;
101 int GetLocalAddress(IPEndPoint* address) const override;
tfarina428341112016-09-22 13:38:20102 const NetLogWithSource& NetLog() const override;
dchengb03027d2014-10-21 12:00:20103 void SetSubresourceSpeculation() override;
104 void SetOmniboxSpeculation() override;
105 bool WasEverUsed() const override;
bnc3cf2a592016-08-11 14:48:36106 bool WasNpnNegotiated() const override;
107 NextProto GetNegotiatedProtocol() const override;
dchengb03027d2014-10-21 12:00:20108 bool GetSSLInfo(SSLInfo* ssl_info) override;
ttuttle23fdb7b2015-05-15 01:28:03109 void GetConnectionAttempts(ConnectionAttempts* out) const override;
110 void ClearConnectionAttempts() override {}
111 void AddConnectionAttempts(const ConnectionAttempts& attempts) override {}
tbansalf82cc8e2015-10-14 20:05:49112 int64_t GetTotalReceivedBytes() const override;
[email protected]d518cd92010-09-29 12:27:44113
[email protected]dbf036f2011-12-06 23:33:24114 // Socket implementation.
dchengb03027d2014-10-21 12:00:20115 int Read(IOBuffer* buf,
116 int buf_len,
117 const CompletionCallback& callback) override;
118 int Write(IOBuffer* buf,
119 int buf_len,
120 const CompletionCallback& callback) override;
Avi Drissman13fc8932015-12-20 04:40:46121 int SetReceiveBufferSize(int32_t size) override;
122 int SetSendBufferSize(int32_t size) override;
[email protected]d518cd92010-09-29 12:27:44123
124 private:
[email protected]7f38da8a2014-03-17 16:44:26125 class PeerCertificateChain;
[email protected]821e3bb2013-11-08 01:06:01126 class SSLContext;
127 friend class SSLClientSocket;
128 friend class SSLContext;
129
[email protected]c8a80e92014-05-17 16:02:08130 int Init();
[email protected]d518cd92010-09-29 12:27:44131 void DoReadCallback(int result);
132 void DoWriteCallback(int result);
133
134 bool DoTransportIO();
135 int DoHandshake();
davidbenc4212c02015-05-12 22:30:18136 int DoHandshakeComplete(int result);
[email protected]faff9852014-06-21 06:13:46137 int DoChannelIDLookup();
138 int DoChannelIDLookupComplete(int result);
[email protected]170e76c2010-10-04 15:04:20139 int DoVerifyCert(int result);
140 int DoVerifyCertComplete(int result);
[email protected]d518cd92010-09-29 12:27:44141 void DoConnectCallback(int result);
davidben30798ed82014-09-19 19:28:20142 void UpdateServerCert();
[email protected]d518cd92010-09-29 12:27:44143
144 void OnHandshakeIOComplete(int result);
145 void OnSendComplete(int result);
146 void OnRecvComplete(int result);
147
148 int DoHandshakeLoop(int last_io_result);
davidben1b133ad2014-10-23 04:23:13149 int DoReadLoop();
150 int DoWriteLoop();
[email protected]d518cd92010-09-29 12:27:44151 int DoPayloadRead();
152 int DoPayloadWrite();
153
davidben1d489522015-07-01 18:48:46154 // Called when an asynchronous event completes which may have blocked the
155 // pending Read or Write calls, if any. Retries both state machines and, if
156 // complete, runs the respective callbacks.
157 void PumpReadWriteEvents();
158
[email protected]d518cd92010-09-29 12:27:44159 int BufferSend();
160 int BufferRecv();
161 void BufferSendComplete(int result);
162 void BufferRecvComplete(int result);
163 void TransportWriteComplete(int result);
[email protected]3e5c6922014-02-06 02:42:16164 int TransportReadComplete(int result);
rsleevi4a6ca8c2016-06-24 03:05:22165 int VerifyCT();
[email protected]d518cd92010-09-29 12:27:44166
[email protected]821e3bb2013-11-08 01:06:01167 // Callback from the SSL layer that indicates the remote server is requesting
168 // a certificate for this client.
[email protected]82c59022014-08-15 09:38:27169 int ClientCertRequestCallback(SSL* ssl);
[email protected]821e3bb2013-11-08 01:06:01170
[email protected]b051cdb62014-02-28 02:20:16171 // CertVerifyCallback is called to verify the server's certificates. We do
172 // verification after the handshake so this function only enforces that the
173 // certificates don't change during renegotiation.
svaldeze83af292016-04-26 14:33:37174 int CertVerifyCallback(X509_STORE_CTX* store_ctx);
[email protected]b051cdb62014-02-28 02:20:16175
[email protected]5aea79182014-07-14 20:43:41176 // Called during an operation on |transport_bio_|'s peer. Checks saved
177 // transport error state and, if appropriate, returns an error through
178 // OpenSSL's error system.
svaldeze83af292016-04-26 14:33:37179 long MaybeReplayTransportError(BIO* bio,
[email protected]5aea79182014-07-14 20:43:41180 int cmd,
svaldeze83af292016-04-26 14:33:37181 const char* argp,
182 int argi,
183 long argl,
[email protected]5aea79182014-07-14 20:43:41184 long retvalue);
185
186 // Callback from the SSL layer when an operation is performed on
187 // |transport_bio_|'s peer.
svaldeze83af292016-04-26 14:33:37188 static long BIOCallback(BIO* bio,
[email protected]5aea79182014-07-14 20:43:41189 int cmd,
svaldeze83af292016-04-26 14:33:37190 const char* argp,
191 int argi,
192 long argl,
[email protected]5aea79182014-07-14 20:43:41193 long retvalue);
194
davidbendafe4e52015-04-08 22:53:52195 // Called after the initial handshake completes and after the server
196 // certificate has been verified. The order of handshake completion and
197 // certificate verification depends on whether the connection was false
198 // started. After both have happened (thus calling this twice), the session is
199 // safe to cache and will be cached.
200 void MaybeCacheSession();
201
davidben44aeae62015-06-24 20:47:43202 // Called from the SSL layer whenever a new session is established.
203 int NewSessionCallback(SSL_SESSION* session);
davidbendafe4e52015-04-08 22:53:52204
estark723b5eeb2016-02-18 21:01:12205 // Adds the Certificate Transparency info from ct_verify_result_ to
206 // |ssl_info|.
davidbeneb5f8ef32014-09-04 14:14:32207 // SCTs are held in three separate vectors in ct_verify_result, each
208 // vetor representing a particular verification state, this method associates
209 // each of the SCTs with the corresponding SCTVerifyStatus as it adds it to
210 // the |ssl_info|.signed_certificate_timestamps list.
estark723b5eeb2016-02-18 21:01:12211 void AddCTInfoToSSLInfo(SSLInfo* ssl_info) const;
davidbeneb5f8ef32014-09-04 14:14:32212
rsleevif020edc2015-03-16 19:31:24213 // Returns a unique key string for the SSL session cache for
214 // this socket.
215 std::string GetSessionCacheKey() const;
216
davidben421116c2015-05-12 19:56:51217 // Returns true if renegotiations are allowed.
218 bool IsRenegotiationAllowed() const;
219
davidben1d489522015-07-01 18:48:46220 // Callbacks for operations with the private key.
221 int PrivateKeyTypeCallback();
davidben1d489522015-07-01 18:48:46222 size_t PrivateKeyMaxSignatureLenCallback();
davidben0bca07fd2016-07-18 15:12:03223 ssl_private_key_result_t PrivateKeySignDigestCallback(uint8_t* out,
224 size_t* out_len,
225 size_t max_out,
226 const EVP_MD* md,
227 const uint8_t* in,
228 size_t in_len);
229 ssl_private_key_result_t PrivateKeyCompleteCallback(uint8_t* out,
230 size_t* out_len,
231 size_t max_out);
davidben1d489522015-07-01 18:48:46232
davidben0bca07fd2016-07-18 15:12:03233 void OnPrivateKeyComplete(Error error, const std::vector<uint8_t>& signature);
davidben1d489522015-07-01 18:48:46234
nharper736ceda2015-11-07 00:16:59235 int TokenBindingAdd(const uint8_t** out,
236 size_t* out_len,
237 int* out_alert_value);
238 int TokenBindingParse(const uint8_t* contents,
239 size_t contents_len,
240 int* out_alert_value);
241
davidben281d13f02016-04-27 20:43:28242 void LogConnectEndEvent(int rv);
243
bncbd442c22016-09-14 20:49:16244 // Record whether ALPN was used, and if so, the negotiated protocol,
245 // in a UMA histogram.
246 void RecordNegotiatedProtocol() const;
bnc3cf2a592016-08-11 14:48:36247
248 // Records histograms for channel id support during full handshakes - resumed
249 // handshakes are ignored.
250 void RecordChannelIDSupport() const;
251
252 // Returns whether TLS channel ID is enabled.
253 bool IsChannelIDEnabled() const;
254
davidbenfe132d92016-09-27 18:07:21255 // Returns the net error corresponding to the most recent OpenSSL
256 // error. ssl_error is the output of SSL_get_error.
257 int MapLastOpenSSLError(int ssl_error,
258 const crypto::OpenSSLErrStackTracer& tracer,
259 OpenSSLErrorInfo* info);
260
[email protected]d518cd92010-09-29 12:27:44261 bool transport_send_busy_;
[email protected]d518cd92010-09-29 12:27:44262 bool transport_recv_busy_;
[email protected]4b768562013-02-16 04:10:07263
svaldeze83af292016-04-26 14:33:37264 // Buffers which are shared by BoringSSL and SSLClientSocketImpl.
haavardm2d92e722014-12-19 13:45:44265 // GrowableIOBuffer is used to keep ownership and setting offset.
266 scoped_refptr<GrowableIOBuffer> send_buffer_;
267 scoped_refptr<GrowableIOBuffer> recv_buffer_;
[email protected]d518cd92010-09-29 12:27:44268
[email protected]dbf036f2011-12-06 23:33:24269 CompletionCallback user_connect_callback_;
[email protected]3f55aa12011-12-07 02:03:33270 CompletionCallback user_read_callback_;
[email protected]83039bb2011-12-09 18:43:55271 CompletionCallback user_write_callback_;
[email protected]d518cd92010-09-29 12:27:44272
273 // Used by Read function.
274 scoped_refptr<IOBuffer> user_read_buf_;
275 int user_read_buf_len_;
276
277 // Used by Write function.
278 scoped_refptr<IOBuffer> user_write_buf_;
279 int user_write_buf_len_;
280
[email protected]4b768562013-02-16 04:10:07281 // Used by DoPayloadRead() when attempting to fill the caller's buffer with
282 // as much data as possible without blocking.
283 // If DoPayloadRead() encounters an error after having read some data, stores
284 // the result to return on the *next* call to DoPayloadRead(). A value > 0
285 // indicates there is no pending result, otherwise 0 indicates EOF and < 0
286 // indicates an error.
287 int pending_read_error_;
288
davidbenb8c23212014-10-28 00:12:16289 // If there is a pending read result, the OpenSSL result code (output of
290 // SSL_get_error) associated with it.
291 int pending_read_ssl_error_;
292
293 // If there is a pending read result, the OpenSSLErrorInfo associated with it.
294 OpenSSLErrorInfo pending_read_error_info_;
295
[email protected]5aea79182014-07-14 20:43:41296 // Used by TransportReadComplete() to signify an error reading from the
297 // transport socket. A value of OK indicates the socket is still
298 // readable. EOFs are mapped to ERR_CONNECTION_CLOSED.
299 int transport_read_error_;
300
[email protected]3e5c6922014-02-06 02:42:16301 // Used by TransportWriteComplete() and TransportReadComplete() to signify an
302 // error writing to the transport socket. A value of OK indicates no error.
303 int transport_write_error_;
304
[email protected]64b5c892014-08-08 09:39:26305 // Set when Connect finishes.
danakj655b66c2016-04-16 00:51:38306 std::unique_ptr<PeerCertificateChain> server_cert_chain_;
[email protected]170e76c2010-10-04 15:04:20307 scoped_refptr<X509Certificate> server_cert_;
308 CertVerifyResult server_cert_verify_result_;
dadriand476e652016-07-26 21:33:24309 std::string ocsp_response_;
[email protected]64b5c892014-08-08 09:39:26310 bool completed_connect_;
[email protected]170e76c2010-10-04 15:04:20311
[email protected]0dc88b32014-03-26 20:12:28312 // Set when Read() or Write() successfully reads or writes data to or from the
313 // network.
314 bool was_ever_used_;
315
[email protected]515adc22013-01-09 16:01:23316 // List of DER-encoded X.509 DistinguishedName of certificate authorities
317 // allowed by the server.
318 std::vector<std::string> cert_authorities_;
[email protected]c0787702014-05-20 21:51:44319 // List of SSLClientCertType values for client certificates allowed by the
320 // server.
321 std::vector<SSLClientCertType> cert_key_types_;
[email protected]d518cd92010-09-29 12:27:44322
[email protected]822581d2010-12-16 17:27:15323 CertVerifier* const cert_verifier_;
danakj655b66c2016-04-16 00:51:38324 std::unique_ptr<CertVerifier::Request> cert_verifier_request_;
davidben09c3d072014-08-25 20:33:58325 base::TimeTicks start_cert_verification_time_;
[email protected]170e76c2010-10-04 15:04:20326
davidbeneb5f8ef32014-09-04 14:14:32327 // Certificate Transparency: Verifier and result holder.
328 ct::CTVerifyResult ct_verify_result_;
329 CTVerifier* cert_transparency_verifier_;
330
[email protected]ee0f2aa82013-10-25 11:59:26331 // The service for retrieving Channel ID keys. May be NULL.
[email protected]6b8a3c742014-07-25 00:25:35332 ChannelIDService* channel_id_service_;
nharper736ceda2015-11-07 00:16:59333 bool tb_was_negotiated_;
334 TokenBindingParam tb_negotiated_param_;
nharper78e6d2b2016-09-21 05:42:35335 TokenBindingSignatureMap tb_signature_map_;
[email protected]ee0f2aa82013-10-25 11:59:26336
[email protected]d518cd92010-09-29 12:27:44337 // OpenSSL stuff
[email protected]d518cd92010-09-29 12:27:44338 SSL* ssl_;
339 BIO* transport_bio_;
340
danakj655b66c2016-04-16 00:51:38341 std::unique_ptr<ClientSocketHandle> transport_;
[email protected]055d7f22010-11-15 12:03:12342 const HostPortPair host_and_port_;
[email protected]d518cd92010-09-29 12:27:44343 SSLConfig ssl_config_;
[email protected]c3456bb2011-12-12 22:22:19344 // ssl_session_cache_shard_ is an opaque string that partitions the SSL
345 // session cache. i.e. sessions created with one value will not attempt to
346 // resume on the socket with a different value.
347 const std::string ssl_session_cache_shard_;
[email protected]d518cd92010-09-29 12:27:44348
[email protected]d518cd92010-09-29 12:27:44349 enum State {
350 STATE_NONE,
351 STATE_HANDSHAKE,
davidbenc4212c02015-05-12 22:30:18352 STATE_HANDSHAKE_COMPLETE,
[email protected]faff9852014-06-21 06:13:46353 STATE_CHANNEL_ID_LOOKUP,
354 STATE_CHANNEL_ID_LOOKUP_COMPLETE,
[email protected]d518cd92010-09-29 12:27:44355 STATE_VERIFY_CERT,
356 STATE_VERIFY_CERT_COMPLETE,
357 };
358 State next_handshake_state_;
svaldez4af14d22015-08-20 13:48:24359
360 // True if the socket has been disconnected.
361 bool disconnected_;
362
bnc3cf2a592016-08-11 14:48:36363 NextProto negotiated_protocol_;
[email protected]6b8a3c742014-07-25 00:25:35364 // Written by the |channel_id_service_|.
danakj655b66c2016-04-16 00:51:38365 std::unique_ptr<crypto::ECPrivateKey> channel_id_key_;
davidben52053b382015-04-27 19:22:29366 // True if a channel ID was sent.
367 bool channel_id_sent_;
davidbenc269cc4b2016-07-27 14:55:03368 // If non-null, the newly-established to be inserted into the session cache
369 // once certificate verification is done.
370 ScopedSSL_SESSION pending_session_;
davidbendafe4e52015-04-08 22:53:52371 // True if the initial handshake's certificate has been verified.
372 bool certificate_verified_;
davidbenfe132d92016-09-27 18:07:21373 // Set to true if a CertificateRequest was received.
374 bool certificate_requested_;
[email protected]6b8a3c742014-07-25 00:25:35375 // The request handle for |channel_id_service_|.
nharper75ade892015-06-10 19:05:35376 ChannelIDService::Request channel_id_request_;
[email protected]8bd4e7a2014-08-09 14:49:17377
davidben1d489522015-07-01 18:48:46378 int signature_result_;
379 std::vector<uint8_t> signature_;
380
[email protected]8bd4e7a2014-08-09 14:49:17381 TransportSecurityState* transport_security_state_;
382
estark6f9b3d82016-01-12 21:37:05383 CTPolicyEnforcer* const policy_enforcer_;
eranm6571b2b2014-12-03 15:53:23384
[email protected]8bd4e7a2014-08-09 14:49:17385 // pinning_failure_log contains a message produced by
386 // TransportSecurityState::CheckPublicKeyPins in the event of a
387 // pinning failure. It is a (somewhat) human-readable string.
388 std::string pinning_failure_log_;
389
dadriandf302c42016-06-10 18:48:59390 // True if PKP is bypassed due to a local trust anchor.
391 bool pkp_bypassed_;
392
tfarina428341112016-09-22 13:38:20393 NetLogWithSource net_log_;
svaldeze83af292016-04-26 14:33:37394 base::WeakPtrFactory<SSLClientSocketImpl> weak_factory_;
[email protected]d518cd92010-09-29 12:27:44395};
396
397} // namespace net
398
svaldeze83af292016-04-26 14:33:37399#endif // NET_SOCKET_SSL_CLIENT_SOCKET_IMPL_H_