blob: 9acb728f961b498a06236f0f2af1c8f9fba127af [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
5#ifndef NET_SOCKET_SSL_CLIENT_SOCKET_OPENSSL_H_
6#define NET_SOCKET_SSL_CLIENT_SOCKET_OPENSSL_H_
[email protected]d518cd92010-09-29 12:27:447
davidben1d489522015-07-01 18:48:468#include <openssl/base.h>
9#include <openssl/ssl.h>
tbansalf82cc8e2015-10-14 20:05:4910#include <stdint.h>
davidben1d489522015-07-01 18:48:4611
[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
[email protected]a5bd18f2012-05-11 15:51:4815#include "base/compiler_specific.h"
[email protected]3b63f8f42011-03-28 01:54:1516#include "base/memory/scoped_ptr.h"
[email protected]be90ba32013-05-13 20:05:2517#include "base/memory/weak_ptr.h"
[email protected]d518cd92010-09-29 12:27:4418#include "net/base/completion_callback.h"
19#include "net/base/io_buffer.h"
eroman7f9236a2015-05-11 21:23:4320#include "net/cert/cert_verifier.h"
[email protected]6e7845ae2013-03-29 21:48:1121#include "net/cert/cert_verify_result.h"
davidbeneb5f8ef32014-09-04 14:14:3222#include "net/cert/ct_verify_result.h"
[email protected]d518cd92010-09-29 12:27:4423#include "net/socket/client_socket_handle.h"
[email protected]536fd0b2013-03-14 17:41:5724#include "net/socket/ssl_client_socket.h"
[email protected]6b8a3c742014-07-25 00:25:3525#include "net/ssl/channel_id_service.h"
davidbenb8c23212014-10-28 00:12:1626#include "net/ssl/openssl_ssl_util.h"
[email protected]c0787702014-05-20 21:51:4427#include "net/ssl/ssl_client_cert_type.h"
[email protected]536fd0b2013-03-14 17:41:5728#include "net/ssl/ssl_config_service.h"
davidbenf2eaaf92015-05-15 22:18:4229#include "net/ssl/ssl_failure_state.h"
[email protected]d518cd92010-09-29 12:27:4430
[email protected]d518cd92010-09-29 12:27:4431namespace net {
32
[email protected]170e76c2010-10-04 15:04:2033class CertVerifier;
davidbeneb5f8ef32014-09-04 14:14:3234class CTVerifier;
[email protected]d518cd92010-09-29 12:27:4435class SSLCertRequestInfo;
[email protected]d518cd92010-09-29 12:27:4436class SSLInfo;
davidben1d489522015-07-01 18:48:4637class SSLPrivateKey;
[email protected]d518cd92010-09-29 12:27:4438
39// An SSL client socket implemented with OpenSSL.
40class SSLClientSocketOpenSSL : public SSLClientSocket {
41 public:
42 // Takes ownership of the transport_socket, which may already be connected.
43 // The given hostname will be compared with the name(s) in the server's
44 // certificate during the SSL handshake. ssl_config specifies the SSL
45 // settings.
[email protected]18ccfdb2013-08-15 00:13:4446 SSLClientSocketOpenSSL(scoped_ptr<ClientSocketHandle> transport_socket,
[email protected]055d7f22010-11-15 12:03:1247 const HostPortPair& host_and_port,
[email protected]822581d2010-12-16 17:27:1548 const SSLConfig& ssl_config,
[email protected]feb79bcd2011-07-21 16:55:1749 const SSLClientSocketContext& context);
dchengb03027d2014-10-21 12:00:2050 ~SSLClientSocketOpenSSL() override;
[email protected]d518cd92010-09-29 12:27:4451
[email protected]fbef13932010-11-23 12:38:5352 const HostPortPair& host_and_port() const { return host_and_port_; }
[email protected]c3456bb2011-12-12 22:22:1953 const std::string& ssl_session_cache_shard() const {
54 return ssl_session_cache_shard_;
55 }
[email protected]fbef13932010-11-23 12:38:5356
zhongyi81f85c6d92015-10-16 19:34:1457 // Export ssl key log files if env variable is not set.
58 static void SetSSLKeyLogFile(const std::string& ssl_keylog_file);
59
[email protected]dbf036f2011-12-06 23:33:2460 // SSLClientSocket implementation.
dchengb03027d2014-10-21 12:00:2061 void GetSSLCertRequestInfo(SSLCertRequestInfo* cert_request_info) override;
davidben6974bf72015-04-27 17:52:4862 NextProtoStatus GetNextProto(std::string* proto) const override;
dchengb03027d2014-10-21 12:00:2063 ChannelIDService* GetChannelIDService() const override;
davidbenf2eaaf92015-05-15 22:18:4264 SSLFailureState GetSSLFailureState() const override;
[email protected]81ec7c12012-07-31 18:32:1965
66 // SSLSocket implementation.
dchengb03027d2014-10-21 12:00:2067 int ExportKeyingMaterial(const base::StringPiece& label,
68 bool has_context,
69 const base::StringPiece& context,
70 unsigned char* out,
71 unsigned int outlen) override;
72 int GetTLSUniqueChannelBinding(std::string* out) override;
[email protected]d518cd92010-09-29 12:27:4473
[email protected]dbf036f2011-12-06 23:33:2474 // StreamSocket implementation.
dchengb03027d2014-10-21 12:00:2075 int Connect(const CompletionCallback& callback) override;
76 void Disconnect() override;
77 bool IsConnected() const override;
78 bool IsConnectedAndIdle() const override;
79 int GetPeerAddress(IPEndPoint* address) const override;
80 int GetLocalAddress(IPEndPoint* address) const override;
81 const BoundNetLog& NetLog() const override;
82 void SetSubresourceSpeculation() override;
83 void SetOmniboxSpeculation() override;
84 bool WasEverUsed() const override;
85 bool UsingTCPFastOpen() const override;
86 bool GetSSLInfo(SSLInfo* ssl_info) override;
ttuttle23fdb7b2015-05-15 01:28:0387 void GetConnectionAttempts(ConnectionAttempts* out) const override;
88 void ClearConnectionAttempts() override {}
89 void AddConnectionAttempts(const ConnectionAttempts& attempts) override {}
tbansalf82cc8e2015-10-14 20:05:4990 int64_t GetTotalReceivedBytes() const override;
[email protected]d518cd92010-09-29 12:27:4491
[email protected]dbf036f2011-12-06 23:33:2492 // Socket implementation.
dchengb03027d2014-10-21 12:00:2093 int Read(IOBuffer* buf,
94 int buf_len,
95 const CompletionCallback& callback) override;
96 int Write(IOBuffer* buf,
97 int buf_len,
98 const CompletionCallback& callback) override;
99 int SetReceiveBufferSize(int32 size) override;
100 int SetSendBufferSize(int32 size) override;
[email protected]d518cd92010-09-29 12:27:44101
102 private:
[email protected]7f38da8a2014-03-17 16:44:26103 class PeerCertificateChain;
[email protected]821e3bb2013-11-08 01:06:01104 class SSLContext;
105 friend class SSLClientSocket;
106 friend class SSLContext;
107
[email protected]c8a80e92014-05-17 16:02:08108 int Init();
[email protected]d518cd92010-09-29 12:27:44109 void DoReadCallback(int result);
110 void DoWriteCallback(int result);
111
112 bool DoTransportIO();
113 int DoHandshake();
davidbenc4212c02015-05-12 22:30:18114 int DoHandshakeComplete(int result);
[email protected]faff9852014-06-21 06:13:46115 int DoChannelIDLookup();
116 int DoChannelIDLookupComplete(int result);
[email protected]170e76c2010-10-04 15:04:20117 int DoVerifyCert(int result);
118 int DoVerifyCertComplete(int result);
[email protected]d518cd92010-09-29 12:27:44119 void DoConnectCallback(int result);
davidben30798ed82014-09-19 19:28:20120 void UpdateServerCert();
davidbeneb5f8ef32014-09-04 14:14:32121 void VerifyCT();
[email protected]d518cd92010-09-29 12:27:44122
123 void OnHandshakeIOComplete(int result);
124 void OnSendComplete(int result);
125 void OnRecvComplete(int result);
126
127 int DoHandshakeLoop(int last_io_result);
davidben1b133ad2014-10-23 04:23:13128 int DoReadLoop();
129 int DoWriteLoop();
[email protected]d518cd92010-09-29 12:27:44130 int DoPayloadRead();
131 int DoPayloadWrite();
132
davidben1d489522015-07-01 18:48:46133 // Called when an asynchronous event completes which may have blocked the
134 // pending Read or Write calls, if any. Retries both state machines and, if
135 // complete, runs the respective callbacks.
136 void PumpReadWriteEvents();
137
[email protected]d518cd92010-09-29 12:27:44138 int BufferSend();
139 int BufferRecv();
140 void BufferSendComplete(int result);
141 void BufferRecvComplete(int result);
142 void TransportWriteComplete(int result);
[email protected]3e5c6922014-02-06 02:42:16143 int TransportReadComplete(int result);
[email protected]d518cd92010-09-29 12:27:44144
[email protected]821e3bb2013-11-08 01:06:01145 // Callback from the SSL layer that indicates the remote server is requesting
146 // a certificate for this client.
[email protected]82c59022014-08-15 09:38:27147 int ClientCertRequestCallback(SSL* ssl);
[email protected]821e3bb2013-11-08 01:06:01148
[email protected]b051cdb62014-02-28 02:20:16149 // CertVerifyCallback is called to verify the server's certificates. We do
150 // verification after the handshake so this function only enforces that the
151 // certificates don't change during renegotiation.
152 int CertVerifyCallback(X509_STORE_CTX *store_ctx);
153
[email protected]821e3bb2013-11-08 01:06:01154 // Callback from the SSL layer to check which NPN protocol we are supporting
155 int SelectNextProtoCallback(unsigned char** out, unsigned char* outlen,
156 const unsigned char* in, unsigned int inlen);
157
[email protected]5aea79182014-07-14 20:43:41158 // Called during an operation on |transport_bio_|'s peer. Checks saved
159 // transport error state and, if appropriate, returns an error through
160 // OpenSSL's error system.
161 long MaybeReplayTransportError(BIO *bio,
162 int cmd,
163 const char *argp, int argi, long argl,
164 long retvalue);
165
166 // Callback from the SSL layer when an operation is performed on
167 // |transport_bio_|'s peer.
168 static long BIOCallback(BIO *bio,
169 int cmd,
170 const char *argp, int argi, long argl,
171 long retvalue);
172
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
davidbeneb5f8ef32014-09-04 14:14:32183 // Adds the SignedCertificateTimestamps from ct_verify_result_ to |ssl_info|.
184 // SCTs are held in three separate vectors in ct_verify_result, each
185 // vetor representing a particular verification state, this method associates
186 // each of the SCTs with the corresponding SCTVerifyStatus as it adds it to
187 // the |ssl_info|.signed_certificate_timestamps list.
188 void AddSCTInfoToSSLInfo(SSLInfo* ssl_info) const;
189
rsleevif020edc2015-03-16 19:31:24190 // Returns a unique key string for the SSL session cache for
191 // this socket.
192 std::string GetSessionCacheKey() const;
193
davidben421116c2015-05-12 19:56:51194 // Returns true if renegotiations are allowed.
195 bool IsRenegotiationAllowed() const;
196
davidben1d489522015-07-01 18:48:46197 // Callbacks for operations with the private key.
198 int PrivateKeyTypeCallback();
davidben1d489522015-07-01 18:48:46199 size_t PrivateKeyMaxSignatureLenCallback();
200 ssl_private_key_result_t PrivateKeySignCallback(uint8_t* out,
201 size_t* out_len,
202 size_t max_out,
203 const EVP_MD* md,
204 const uint8_t* in,
205 size_t in_len);
206 ssl_private_key_result_t PrivateKeySignCompleteCallback(uint8_t* out,
207 size_t* out_len,
208 size_t max_out);
209
210 void OnPrivateKeySignComplete(Error error,
211 const std::vector<uint8_t>& signature);
212
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
[email protected]d518cd92010-09-29 12:27:44220 bool transport_send_busy_;
[email protected]d518cd92010-09-29 12:27:44221 bool transport_recv_busy_;
[email protected]4b768562013-02-16 04:10:07222
haavardm2d92e722014-12-19 13:45:44223 // Buffers which are shared by BoringSSL and SSLClientSocketOpenSSL.
224 // GrowableIOBuffer is used to keep ownership and setting offset.
225 scoped_refptr<GrowableIOBuffer> send_buffer_;
226 scoped_refptr<GrowableIOBuffer> recv_buffer_;
[email protected]d518cd92010-09-29 12:27:44227
[email protected]dbf036f2011-12-06 23:33:24228 CompletionCallback user_connect_callback_;
[email protected]3f55aa12011-12-07 02:03:33229 CompletionCallback user_read_callback_;
[email protected]83039bb2011-12-09 18:43:55230 CompletionCallback user_write_callback_;
[email protected]d518cd92010-09-29 12:27:44231
232 // Used by Read function.
233 scoped_refptr<IOBuffer> user_read_buf_;
234 int user_read_buf_len_;
235
236 // Used by Write function.
237 scoped_refptr<IOBuffer> user_write_buf_;
238 int user_write_buf_len_;
239
[email protected]4b768562013-02-16 04:10:07240 // Used by DoPayloadRead() when attempting to fill the caller's buffer with
241 // as much data as possible without blocking.
242 // If DoPayloadRead() encounters an error after having read some data, stores
243 // the result to return on the *next* call to DoPayloadRead(). A value > 0
244 // indicates there is no pending result, otherwise 0 indicates EOF and < 0
245 // indicates an error.
246 int pending_read_error_;
247
davidbenb8c23212014-10-28 00:12:16248 // If there is a pending read result, the OpenSSL result code (output of
249 // SSL_get_error) associated with it.
250 int pending_read_ssl_error_;
251
252 // If there is a pending read result, the OpenSSLErrorInfo associated with it.
253 OpenSSLErrorInfo pending_read_error_info_;
254
[email protected]5aea79182014-07-14 20:43:41255 // Used by TransportReadComplete() to signify an error reading from the
256 // transport socket. A value of OK indicates the socket is still
257 // readable. EOFs are mapped to ERR_CONNECTION_CLOSED.
258 int transport_read_error_;
259
[email protected]3e5c6922014-02-06 02:42:16260 // Used by TransportWriteComplete() and TransportReadComplete() to signify an
261 // error writing to the transport socket. A value of OK indicates no error.
262 int transport_write_error_;
263
[email protected]64b5c892014-08-08 09:39:26264 // Set when Connect finishes.
[email protected]7f38da8a2014-03-17 16:44:26265 scoped_ptr<PeerCertificateChain> server_cert_chain_;
[email protected]170e76c2010-10-04 15:04:20266 scoped_refptr<X509Certificate> server_cert_;
267 CertVerifyResult server_cert_verify_result_;
[email protected]64b5c892014-08-08 09:39:26268 bool completed_connect_;
[email protected]170e76c2010-10-04 15:04:20269
[email protected]0dc88b32014-03-26 20:12:28270 // Set when Read() or Write() successfully reads or writes data to or from the
271 // network.
272 bool was_ever_used_;
273
[email protected]515adc22013-01-09 16:01:23274 // List of DER-encoded X.509 DistinguishedName of certificate authorities
275 // allowed by the server.
276 std::vector<std::string> cert_authorities_;
[email protected]c0787702014-05-20 21:51:44277 // List of SSLClientCertType values for client certificates allowed by the
278 // server.
279 std::vector<SSLClientCertType> cert_key_types_;
[email protected]d518cd92010-09-29 12:27:44280
[email protected]822581d2010-12-16 17:27:15281 CertVerifier* const cert_verifier_;
eroman7f9236a2015-05-11 21:23:43282 scoped_ptr<CertVerifier::Request> cert_verifier_request_;
davidben09c3d072014-08-25 20:33:58283 base::TimeTicks start_cert_verification_time_;
[email protected]170e76c2010-10-04 15:04:20284
davidbeneb5f8ef32014-09-04 14:14:32285 // Certificate Transparency: Verifier and result holder.
286 ct::CTVerifyResult ct_verify_result_;
287 CTVerifier* cert_transparency_verifier_;
288
[email protected]ee0f2aa82013-10-25 11:59:26289 // The service for retrieving Channel ID keys. May be NULL.
[email protected]6b8a3c742014-07-25 00:25:35290 ChannelIDService* channel_id_service_;
nharper736ceda2015-11-07 00:16:59291 bool tb_was_negotiated_;
292 TokenBindingParam tb_negotiated_param_;
[email protected]ee0f2aa82013-10-25 11:59:26293
[email protected]d518cd92010-09-29 12:27:44294 // OpenSSL stuff
[email protected]d518cd92010-09-29 12:27:44295 SSL* ssl_;
296 BIO* transport_bio_;
297
298 scoped_ptr<ClientSocketHandle> transport_;
[email protected]055d7f22010-11-15 12:03:12299 const HostPortPair host_and_port_;
[email protected]d518cd92010-09-29 12:27:44300 SSLConfig ssl_config_;
[email protected]c3456bb2011-12-12 22:22:19301 // ssl_session_cache_shard_ is an opaque string that partitions the SSL
302 // session cache. i.e. sessions created with one value will not attempt to
303 // resume on the socket with a different value.
304 const std::string ssl_session_cache_shard_;
[email protected]d518cd92010-09-29 12:27:44305
[email protected]d518cd92010-09-29 12:27:44306 enum State {
307 STATE_NONE,
308 STATE_HANDSHAKE,
davidbenc4212c02015-05-12 22:30:18309 STATE_HANDSHAKE_COMPLETE,
[email protected]faff9852014-06-21 06:13:46310 STATE_CHANNEL_ID_LOOKUP,
311 STATE_CHANNEL_ID_LOOKUP_COMPLETE,
[email protected]d518cd92010-09-29 12:27:44312 STATE_VERIFY_CERT,
313 STATE_VERIFY_CERT_COMPLETE,
314 };
315 State next_handshake_state_;
svaldez4af14d22015-08-20 13:48:24316
317 // True if the socket has been disconnected.
318 bool disconnected_;
319
[email protected]ea4a1c6a2010-12-09 13:33:28320 NextProtoStatus npn_status_;
321 std::string npn_proto_;
[email protected]6b8a3c742014-07-25 00:25:35322 // Written by the |channel_id_service_|.
nharper2e171cf2015-06-01 20:29:23323 scoped_ptr<crypto::ECPrivateKey> channel_id_key_;
davidben52053b382015-04-27 19:22:29324 // True if a channel ID was sent.
325 bool channel_id_sent_;
davidben44aeae62015-06-24 20:47:43326 // True if the current session was newly-established, but the certificate had
327 // not yet been verified externally, so it cannot be inserted into the cache
328 // until later.
329 bool session_pending_;
davidbendafe4e52015-04-08 22:53:52330 // True if the initial handshake's certificate has been verified.
331 bool certificate_verified_;
[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_;
davidbenf2eaaf92015-05-15 22:18:42334 SSLFailureState ssl_failure_state_;
[email protected]8bd4e7a2014-08-09 14:49:17335
davidben1d489522015-07-01 18:48:46336 scoped_ptr<SSLPrivateKey> private_key_;
337 int signature_result_;
338 std::vector<uint8_t> signature_;
339
[email protected]8bd4e7a2014-08-09 14:49:17340 TransportSecurityState* transport_security_state_;
341
eranm6571b2b2014-12-03 15:53:23342 CertPolicyEnforcer* const policy_enforcer_;
343
[email protected]8bd4e7a2014-08-09 14:49:17344 // pinning_failure_log contains a message produced by
345 // TransportSecurityState::CheckPublicKeyPins in the event of a
346 // pinning failure. It is a (somewhat) human-readable string.
347 std::string pinning_failure_log_;
348
[email protected]d518cd92010-09-29 12:27:44349 BoundNetLog net_log_;
kulkarni.acd7b4462014-08-28 07:41:34350 base::WeakPtrFactory<SSLClientSocketOpenSSL> weak_factory_;
[email protected]d518cd92010-09-29 12:27:44351};
352
353} // namespace net
354
[email protected]7e5dd49f2010-12-08 18:33:49355#endif // NET_SOCKET_SSL_CLIENT_SOCKET_OPENSSL_H_