blob: b96e456b6a1e8796e61d73162324c3f8c53c14a4 [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"
[email protected]c0787702014-05-20 21:51:4431#include "net/ssl/ssl_client_cert_type.h"
[email protected]536fd0b2013-03-14 17:41:5732#include "net/ssl/ssl_config_service.h"
davidbenf2eaaf92015-05-15 22:18:4233#include "net/ssl/ssl_failure_state.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;
davidben6974bf72015-04-27 17:52:4876 NextProtoStatus GetNextProto(std::string* proto) const override;
dchengb03027d2014-10-21 12:00:2077 ChannelIDService* GetChannelIDService() const override;
nharperb7441ef2016-01-25 23:54:1478 Error GetSignedEKMForTokenBinding(crypto::ECPrivateKey* key,
79 std::vector<uint8_t>* out) override;
nharperb36644f2016-02-22 23:14:4380 crypto::ECPrivateKey* GetChannelIDKey() const override;
davidbenf2eaaf92015-05-15 22:18:4281 SSLFailureState GetSSLFailureState() const override;
[email protected]81ec7c12012-07-31 18:32:1982
83 // SSLSocket implementation.
dchengb03027d2014-10-21 12:00:2084 int ExportKeyingMaterial(const base::StringPiece& label,
85 bool has_context,
86 const base::StringPiece& context,
87 unsigned char* out,
88 unsigned int outlen) override;
[email protected]d518cd92010-09-29 12:27:4489
[email protected]dbf036f2011-12-06 23:33:2490 // StreamSocket implementation.
dchengb03027d2014-10-21 12:00:2091 int Connect(const CompletionCallback& callback) override;
92 void Disconnect() override;
93 bool IsConnected() const override;
94 bool IsConnectedAndIdle() const override;
95 int GetPeerAddress(IPEndPoint* address) const override;
96 int GetLocalAddress(IPEndPoint* address) const override;
97 const BoundNetLog& NetLog() const override;
98 void SetSubresourceSpeculation() override;
99 void SetOmniboxSpeculation() override;
100 bool WasEverUsed() 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();
davidbeneb5f8ef32014-09-04 14:14:32136 void VerifyCT();
[email protected]d518cd92010-09-29 12:27:44137
138 void OnHandshakeIOComplete(int result);
139 void OnSendComplete(int result);
140 void OnRecvComplete(int result);
141
142 int DoHandshakeLoop(int last_io_result);
davidben1b133ad2014-10-23 04:23:13143 int DoReadLoop();
144 int DoWriteLoop();
[email protected]d518cd92010-09-29 12:27:44145 int DoPayloadRead();
146 int DoPayloadWrite();
147
davidben1d489522015-07-01 18:48:46148 // Called when an asynchronous event completes which may have blocked the
149 // pending Read or Write calls, if any. Retries both state machines and, if
150 // complete, runs the respective callbacks.
151 void PumpReadWriteEvents();
152
[email protected]d518cd92010-09-29 12:27:44153 int BufferSend();
154 int BufferRecv();
155 void BufferSendComplete(int result);
156 void BufferRecvComplete(int result);
157 void TransportWriteComplete(int result);
[email protected]3e5c6922014-02-06 02:42:16158 int TransportReadComplete(int result);
[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();
222 ssl_private_key_result_t PrivateKeySignCallback(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 PrivateKeySignCompleteCallback(uint8_t* out,
229 size_t* out_len,
230 size_t max_out);
231
232 void OnPrivateKeySignComplete(Error error,
233 const std::vector<uint8_t>& signature);
234
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
[email protected]d518cd92010-09-29 12:27:44242 bool transport_send_busy_;
[email protected]d518cd92010-09-29 12:27:44243 bool transport_recv_busy_;
[email protected]4b768562013-02-16 04:10:07244
svaldeze83af292016-04-26 14:33:37245 // Buffers which are shared by BoringSSL and SSLClientSocketImpl.
haavardm2d92e722014-12-19 13:45:44246 // GrowableIOBuffer is used to keep ownership and setting offset.
247 scoped_refptr<GrowableIOBuffer> send_buffer_;
248 scoped_refptr<GrowableIOBuffer> recv_buffer_;
[email protected]d518cd92010-09-29 12:27:44249
[email protected]dbf036f2011-12-06 23:33:24250 CompletionCallback user_connect_callback_;
[email protected]3f55aa12011-12-07 02:03:33251 CompletionCallback user_read_callback_;
[email protected]83039bb2011-12-09 18:43:55252 CompletionCallback user_write_callback_;
[email protected]d518cd92010-09-29 12:27:44253
254 // Used by Read function.
255 scoped_refptr<IOBuffer> user_read_buf_;
256 int user_read_buf_len_;
257
258 // Used by Write function.
259 scoped_refptr<IOBuffer> user_write_buf_;
260 int user_write_buf_len_;
261
[email protected]4b768562013-02-16 04:10:07262 // Used by DoPayloadRead() when attempting to fill the caller's buffer with
263 // as much data as possible without blocking.
264 // If DoPayloadRead() encounters an error after having read some data, stores
265 // the result to return on the *next* call to DoPayloadRead(). A value > 0
266 // indicates there is no pending result, otherwise 0 indicates EOF and < 0
267 // indicates an error.
268 int pending_read_error_;
269
davidbenb8c23212014-10-28 00:12:16270 // If there is a pending read result, the OpenSSL result code (output of
271 // SSL_get_error) associated with it.
272 int pending_read_ssl_error_;
273
274 // If there is a pending read result, the OpenSSLErrorInfo associated with it.
275 OpenSSLErrorInfo pending_read_error_info_;
276
[email protected]5aea79182014-07-14 20:43:41277 // Used by TransportReadComplete() to signify an error reading from the
278 // transport socket. A value of OK indicates the socket is still
279 // readable. EOFs are mapped to ERR_CONNECTION_CLOSED.
280 int transport_read_error_;
281
[email protected]3e5c6922014-02-06 02:42:16282 // Used by TransportWriteComplete() and TransportReadComplete() to signify an
283 // error writing to the transport socket. A value of OK indicates no error.
284 int transport_write_error_;
285
[email protected]64b5c892014-08-08 09:39:26286 // Set when Connect finishes.
danakj655b66c2016-04-16 00:51:38287 std::unique_ptr<PeerCertificateChain> server_cert_chain_;
[email protected]170e76c2010-10-04 15:04:20288 scoped_refptr<X509Certificate> server_cert_;
289 CertVerifyResult server_cert_verify_result_;
[email protected]64b5c892014-08-08 09:39:26290 bool completed_connect_;
[email protected]170e76c2010-10-04 15:04:20291
[email protected]0dc88b32014-03-26 20:12:28292 // Set when Read() or Write() successfully reads or writes data to or from the
293 // network.
294 bool was_ever_used_;
295
[email protected]515adc22013-01-09 16:01:23296 // List of DER-encoded X.509 DistinguishedName of certificate authorities
297 // allowed by the server.
298 std::vector<std::string> cert_authorities_;
[email protected]c0787702014-05-20 21:51:44299 // List of SSLClientCertType values for client certificates allowed by the
300 // server.
301 std::vector<SSLClientCertType> cert_key_types_;
[email protected]d518cd92010-09-29 12:27:44302
[email protected]822581d2010-12-16 17:27:15303 CertVerifier* const cert_verifier_;
danakj655b66c2016-04-16 00:51:38304 std::unique_ptr<CertVerifier::Request> cert_verifier_request_;
davidben09c3d072014-08-25 20:33:58305 base::TimeTicks start_cert_verification_time_;
[email protected]170e76c2010-10-04 15:04:20306
davidbeneb5f8ef32014-09-04 14:14:32307 // Certificate Transparency: Verifier and result holder.
308 ct::CTVerifyResult ct_verify_result_;
309 CTVerifier* cert_transparency_verifier_;
310
[email protected]ee0f2aa82013-10-25 11:59:26311 // The service for retrieving Channel ID keys. May be NULL.
[email protected]6b8a3c742014-07-25 00:25:35312 ChannelIDService* channel_id_service_;
nharper736ceda2015-11-07 00:16:59313 bool tb_was_negotiated_;
314 TokenBindingParam tb_negotiated_param_;
nharperb7441ef2016-01-25 23:54:14315 SignedEkmMap tb_signed_ekm_map_;
[email protected]ee0f2aa82013-10-25 11:59:26316
[email protected]d518cd92010-09-29 12:27:44317 // OpenSSL stuff
[email protected]d518cd92010-09-29 12:27:44318 SSL* ssl_;
319 BIO* transport_bio_;
320
danakj655b66c2016-04-16 00:51:38321 std::unique_ptr<ClientSocketHandle> transport_;
[email protected]055d7f22010-11-15 12:03:12322 const HostPortPair host_and_port_;
[email protected]d518cd92010-09-29 12:27:44323 SSLConfig ssl_config_;
[email protected]c3456bb2011-12-12 22:22:19324 // ssl_session_cache_shard_ is an opaque string that partitions the SSL
325 // session cache. i.e. sessions created with one value will not attempt to
326 // resume on the socket with a different value.
327 const std::string ssl_session_cache_shard_;
[email protected]d518cd92010-09-29 12:27:44328
[email protected]d518cd92010-09-29 12:27:44329 enum State {
330 STATE_NONE,
331 STATE_HANDSHAKE,
davidbenc4212c02015-05-12 22:30:18332 STATE_HANDSHAKE_COMPLETE,
[email protected]faff9852014-06-21 06:13:46333 STATE_CHANNEL_ID_LOOKUP,
334 STATE_CHANNEL_ID_LOOKUP_COMPLETE,
[email protected]d518cd92010-09-29 12:27:44335 STATE_VERIFY_CERT,
336 STATE_VERIFY_CERT_COMPLETE,
337 };
338 State next_handshake_state_;
svaldez4af14d22015-08-20 13:48:24339
340 // True if the socket has been disconnected.
341 bool disconnected_;
342
[email protected]ea4a1c6a2010-12-09 13:33:28343 NextProtoStatus npn_status_;
344 std::string npn_proto_;
[email protected]6b8a3c742014-07-25 00:25:35345 // Written by the |channel_id_service_|.
danakj655b66c2016-04-16 00:51:38346 std::unique_ptr<crypto::ECPrivateKey> channel_id_key_;
davidben52053b382015-04-27 19:22:29347 // True if a channel ID was sent.
348 bool channel_id_sent_;
davidben44aeae62015-06-24 20:47:43349 // True if the current session was newly-established, but the certificate had
350 // not yet been verified externally, so it cannot be inserted into the cache
351 // until later.
352 bool session_pending_;
davidbendafe4e52015-04-08 22:53:52353 // True if the initial handshake's certificate has been verified.
354 bool certificate_verified_;
[email protected]6b8a3c742014-07-25 00:25:35355 // The request handle for |channel_id_service_|.
nharper75ade892015-06-10 19:05:35356 ChannelIDService::Request channel_id_request_;
davidbenf2eaaf92015-05-15 22:18:42357 SSLFailureState ssl_failure_state_;
[email protected]8bd4e7a2014-08-09 14:49:17358
davidben1d489522015-07-01 18:48:46359 int signature_result_;
360 std::vector<uint8_t> signature_;
361
[email protected]8bd4e7a2014-08-09 14:49:17362 TransportSecurityState* transport_security_state_;
363
estark6f9b3d82016-01-12 21:37:05364 CTPolicyEnforcer* const policy_enforcer_;
eranm6571b2b2014-12-03 15:53:23365
[email protected]8bd4e7a2014-08-09 14:49:17366 // pinning_failure_log contains a message produced by
367 // TransportSecurityState::CheckPublicKeyPins in the event of a
368 // pinning failure. It is a (somewhat) human-readable string.
369 std::string pinning_failure_log_;
370
[email protected]d518cd92010-09-29 12:27:44371 BoundNetLog net_log_;
svaldeze83af292016-04-26 14:33:37372 base::WeakPtrFactory<SSLClientSocketImpl> weak_factory_;
[email protected]d518cd92010-09-29 12:27:44373};
374
375} // namespace net
376
svaldeze83af292016-04-26 14:33:37377#endif // NET_SOCKET_SSL_CLIENT_SOCKET_IMPL_H_