blob: 29833b9de1ef500343f5dd58f7ad92db24a2a802 [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
[email protected]ea4a1c6a2010-12-09 13:33:288#include <string>
9
[email protected]a5bd18f2012-05-11 15:51:4810#include "base/compiler_specific.h"
[email protected]3b63f8f42011-03-28 01:54:1511#include "base/memory/scoped_ptr.h"
[email protected]be90ba32013-05-13 20:05:2512#include "base/memory/weak_ptr.h"
[email protected]d518cd92010-09-29 12:27:4413#include "net/base/completion_callback.h"
14#include "net/base/io_buffer.h"
[email protected]6e7845ae2013-03-29 21:48:1115#include "net/cert/cert_verify_result.h"
davidbeneb5f8ef32014-09-04 14:14:3216#include "net/cert/ct_verify_result.h"
[email protected]d518cd92010-09-29 12:27:4417#include "net/socket/client_socket_handle.h"
[email protected]536fd0b2013-03-14 17:41:5718#include "net/socket/ssl_client_socket.h"
[email protected]6b8a3c742014-07-25 00:25:3519#include "net/ssl/channel_id_service.h"
davidbenb8c23212014-10-28 00:12:1620#include "net/ssl/openssl_ssl_util.h"
[email protected]c0787702014-05-20 21:51:4421#include "net/ssl/ssl_client_cert_type.h"
[email protected]536fd0b2013-03-14 17:41:5722#include "net/ssl/ssl_config_service.h"
[email protected]d518cd92010-09-29 12:27:4423
[email protected]03a07b2e2013-02-11 20:13:4524// Avoid including misc OpenSSL headers, i.e.:
25// <openssl/bio.h>
[email protected]d518cd92010-09-29 12:27:4426typedef struct bio_st BIO;
[email protected]03a07b2e2013-02-11 20:13:4527// <openssl/evp.h>
[email protected]718c9672010-12-02 10:04:1028typedef struct evp_pkey_st EVP_PKEY;
[email protected]03a07b2e2013-02-11 20:13:4529// <openssl/ssl.h>
[email protected]d518cd92010-09-29 12:27:4430typedef struct ssl_st SSL;
[email protected]03a07b2e2013-02-11 20:13:4531// <openssl/x509.h>
[email protected]718c9672010-12-02 10:04:1032typedef struct x509_st X509;
[email protected]b051cdb62014-02-28 02:20:1633// <openssl/ossl_type.h>
34typedef struct x509_store_ctx_st X509_STORE_CTX;
[email protected]d518cd92010-09-29 12:27:4435
36namespace net {
37
[email protected]170e76c2010-10-04 15:04:2038class CertVerifier;
davidbeneb5f8ef32014-09-04 14:14:3239class CTVerifier;
[email protected]822581d2010-12-16 17:27:1540class SingleRequestCertVerifier;
[email protected]d518cd92010-09-29 12:27:4441class SSLCertRequestInfo;
[email protected]d518cd92010-09-29 12:27:4442class SSLInfo;
43
44// An SSL client socket implemented with OpenSSL.
45class SSLClientSocketOpenSSL : public SSLClientSocket {
46 public:
47 // Takes ownership of the transport_socket, which may already be connected.
48 // The given hostname will be compared with the name(s) in the server's
49 // certificate during the SSL handshake. ssl_config specifies the SSL
50 // settings.
[email protected]18ccfdb2013-08-15 00:13:4451 SSLClientSocketOpenSSL(scoped_ptr<ClientSocketHandle> transport_socket,
[email protected]055d7f22010-11-15 12:03:1252 const HostPortPair& host_and_port,
[email protected]822581d2010-12-16 17:27:1553 const SSLConfig& ssl_config,
[email protected]feb79bcd2011-07-21 16:55:1754 const SSLClientSocketContext& context);
dchengb03027d2014-10-21 12:00:2055 ~SSLClientSocketOpenSSL() override;
[email protected]d518cd92010-09-29 12:27:4456
[email protected]fbef13932010-11-23 12:38:5357 const HostPortPair& host_and_port() const { return host_and_port_; }
[email protected]c3456bb2011-12-12 22:22:1958 const std::string& ssl_session_cache_shard() const {
59 return ssl_session_cache_shard_;
60 }
[email protected]fbef13932010-11-23 12:38:5361
[email protected]dbf036f2011-12-06 23:33:2462 // SSLClientSocket implementation.
dchengb03027d2014-10-21 12:00:2063 void GetSSLCertRequestInfo(SSLCertRequestInfo* cert_request_info) override;
davidben6974bf72015-04-27 17:52:4864 NextProtoStatus GetNextProto(std::string* proto) const override;
dchengb03027d2014-10-21 12:00:2065 ChannelIDService* GetChannelIDService() const override;
[email protected]81ec7c12012-07-31 18:32:1966
67 // SSLSocket implementation.
dchengb03027d2014-10-21 12:00:2068 int ExportKeyingMaterial(const base::StringPiece& label,
69 bool has_context,
70 const base::StringPiece& context,
71 unsigned char* out,
72 unsigned int outlen) override;
73 int GetTLSUniqueChannelBinding(std::string* out) override;
[email protected]d518cd92010-09-29 12:27:4474
[email protected]dbf036f2011-12-06 23:33:2475 // StreamSocket implementation.
dchengb03027d2014-10-21 12:00:2076 int Connect(const CompletionCallback& callback) override;
77 void Disconnect() override;
78 bool IsConnected() const override;
79 bool IsConnectedAndIdle() const override;
80 int GetPeerAddress(IPEndPoint* address) const override;
81 int GetLocalAddress(IPEndPoint* address) const override;
82 const BoundNetLog& NetLog() const override;
83 void SetSubresourceSpeculation() override;
84 void SetOmniboxSpeculation() override;
85 bool WasEverUsed() const override;
86 bool UsingTCPFastOpen() const override;
87 bool GetSSLInfo(SSLInfo* ssl_info) override;
[email protected]d518cd92010-09-29 12:27:4488
[email protected]dbf036f2011-12-06 23:33:2489 // Socket implementation.
dchengb03027d2014-10-21 12:00:2090 int Read(IOBuffer* buf,
91 int buf_len,
92 const CompletionCallback& callback) override;
93 int Write(IOBuffer* buf,
94 int buf_len,
95 const CompletionCallback& callback) override;
96 int SetReceiveBufferSize(int32 size) override;
97 int SetSendBufferSize(int32 size) override;
[email protected]d518cd92010-09-29 12:27:4498
[email protected]7f38da8a2014-03-17 16:44:2699 protected:
100 // SSLClientSocket implementation.
dchengb03027d2014-10-21 12:00:20101 scoped_refptr<X509Certificate> GetUnverifiedServerCertificateChain()
mostynbba063d6032014-10-09 11:01:13102 const override;
[email protected]7f38da8a2014-03-17 16:44:26103
[email protected]d518cd92010-09-29 12:27:44104 private:
[email protected]7f38da8a2014-03-17 16:44:26105 class PeerCertificateChain;
[email protected]821e3bb2013-11-08 01:06:01106 class SSLContext;
107 friend class SSLClientSocket;
108 friend class SSLContext;
109
[email protected]c8a80e92014-05-17 16:02:08110 int Init();
[email protected]d518cd92010-09-29 12:27:44111 void DoReadCallback(int result);
112 void DoWriteCallback(int result);
113
[email protected]d518cd92010-09-29 12:27:44114 bool DoTransportIO();
115 int DoHandshake();
[email protected]faff9852014-06-21 06:13:46116 int DoChannelIDLookup();
117 int DoChannelIDLookupComplete(int result);
[email protected]170e76c2010-10-04 15:04:20118 int DoVerifyCert(int result);
119 int DoVerifyCertComplete(int result);
[email protected]d518cd92010-09-29 12:27:44120 void DoConnectCallback(int result);
davidben30798ed82014-09-19 19:28:20121 void UpdateServerCert();
davidbeneb5f8ef32014-09-04 14:14:32122 void VerifyCT();
[email protected]d518cd92010-09-29 12:27:44123
124 void OnHandshakeIOComplete(int result);
125 void OnSendComplete(int result);
126 void OnRecvComplete(int result);
127
128 int DoHandshakeLoop(int last_io_result);
davidben1b133ad2014-10-23 04:23:13129 int DoReadLoop();
130 int DoWriteLoop();
[email protected]d518cd92010-09-29 12:27:44131 int DoPayloadRead();
132 int DoPayloadWrite();
133
134 int BufferSend();
135 int BufferRecv();
136 void BufferSendComplete(int result);
137 void BufferRecvComplete(int result);
138 void TransportWriteComplete(int result);
[email protected]3e5c6922014-02-06 02:42:16139 int TransportReadComplete(int result);
[email protected]d518cd92010-09-29 12:27:44140
[email protected]821e3bb2013-11-08 01:06:01141 // Callback from the SSL layer that indicates the remote server is requesting
142 // a certificate for this client.
[email protected]82c59022014-08-15 09:38:27143 int ClientCertRequestCallback(SSL* ssl);
[email protected]821e3bb2013-11-08 01:06:01144
[email protected]b051cdb62014-02-28 02:20:16145 // CertVerifyCallback is called to verify the server's certificates. We do
146 // verification after the handshake so this function only enforces that the
147 // certificates don't change during renegotiation.
148 int CertVerifyCallback(X509_STORE_CTX *store_ctx);
149
[email protected]821e3bb2013-11-08 01:06:01150 // Callback from the SSL layer to check which NPN protocol we are supporting
151 int SelectNextProtoCallback(unsigned char** out, unsigned char* outlen,
152 const unsigned char* in, unsigned int inlen);
153
[email protected]5aea79182014-07-14 20:43:41154 // Called during an operation on |transport_bio_|'s peer. Checks saved
155 // transport error state and, if appropriate, returns an error through
156 // OpenSSL's error system.
157 long MaybeReplayTransportError(BIO *bio,
158 int cmd,
159 const char *argp, int argi, long argl,
160 long retvalue);
161
162 // Callback from the SSL layer when an operation is performed on
163 // |transport_bio_|'s peer.
164 static long BIOCallback(BIO *bio,
165 int cmd,
166 const char *argp, int argi, long argl,
167 long retvalue);
168
davidbendafe4e52015-04-08 22:53:52169 // Called after the initial handshake completes and after the server
170 // certificate has been verified. The order of handshake completion and
171 // certificate verification depends on whether the connection was false
172 // started. After both have happened (thus calling this twice), the session is
173 // safe to cache and will be cached.
174 void MaybeCacheSession();
175
176 // Callback from the SSL layer when the internal state machine progresses. It
177 // is used to listen for when the handshake completes entirely; |Connect| may
178 // return early if false starting.
179 void InfoCallback(int type, int val);
180
davidbeneb5f8ef32014-09-04 14:14:32181 // Adds the SignedCertificateTimestamps from ct_verify_result_ to |ssl_info|.
182 // SCTs are held in three separate vectors in ct_verify_result, each
183 // vetor representing a particular verification state, this method associates
184 // each of the SCTs with the corresponding SCTVerifyStatus as it adds it to
185 // the |ssl_info|.signed_certificate_timestamps list.
186 void AddSCTInfoToSSLInfo(SSLInfo* ssl_info) const;
187
rsleevif020edc2015-03-16 19:31:24188 // Returns a unique key string for the SSL session cache for
189 // this socket.
190 std::string GetSessionCacheKey() const;
191
[email protected]d518cd92010-09-29 12:27:44192 bool transport_send_busy_;
[email protected]d518cd92010-09-29 12:27:44193 bool transport_recv_busy_;
[email protected]4b768562013-02-16 04:10:07194
haavardm2d92e722014-12-19 13:45:44195 // Buffers which are shared by BoringSSL and SSLClientSocketOpenSSL.
196 // GrowableIOBuffer is used to keep ownership and setting offset.
197 scoped_refptr<GrowableIOBuffer> send_buffer_;
198 scoped_refptr<GrowableIOBuffer> recv_buffer_;
[email protected]d518cd92010-09-29 12:27:44199
[email protected]dbf036f2011-12-06 23:33:24200 CompletionCallback user_connect_callback_;
[email protected]3f55aa12011-12-07 02:03:33201 CompletionCallback user_read_callback_;
[email protected]83039bb2011-12-09 18:43:55202 CompletionCallback user_write_callback_;
[email protected]d518cd92010-09-29 12:27:44203
[email protected]d518cd92010-09-29 12:27:44204 // Used by Read function.
205 scoped_refptr<IOBuffer> user_read_buf_;
206 int user_read_buf_len_;
207
208 // Used by Write function.
209 scoped_refptr<IOBuffer> user_write_buf_;
210 int user_write_buf_len_;
211
[email protected]4b768562013-02-16 04:10:07212 // Used by DoPayloadRead() when attempting to fill the caller's buffer with
213 // as much data as possible without blocking.
214 // If DoPayloadRead() encounters an error after having read some data, stores
215 // the result to return on the *next* call to DoPayloadRead(). A value > 0
216 // indicates there is no pending result, otherwise 0 indicates EOF and < 0
217 // indicates an error.
218 int pending_read_error_;
219
davidbenb8c23212014-10-28 00:12:16220 // If there is a pending read result, the OpenSSL result code (output of
221 // SSL_get_error) associated with it.
222 int pending_read_ssl_error_;
223
224 // If there is a pending read result, the OpenSSLErrorInfo associated with it.
225 OpenSSLErrorInfo pending_read_error_info_;
226
[email protected]5aea79182014-07-14 20:43:41227 // Used by TransportReadComplete() to signify an error reading from the
228 // transport socket. A value of OK indicates the socket is still
229 // readable. EOFs are mapped to ERR_CONNECTION_CLOSED.
230 int transport_read_error_;
231
[email protected]3e5c6922014-02-06 02:42:16232 // Used by TransportWriteComplete() and TransportReadComplete() to signify an
233 // error writing to the transport socket. A value of OK indicates no error.
234 int transport_write_error_;
235
[email protected]64b5c892014-08-08 09:39:26236 // Set when Connect finishes.
[email protected]7f38da8a2014-03-17 16:44:26237 scoped_ptr<PeerCertificateChain> server_cert_chain_;
[email protected]170e76c2010-10-04 15:04:20238 scoped_refptr<X509Certificate> server_cert_;
239 CertVerifyResult server_cert_verify_result_;
[email protected]64b5c892014-08-08 09:39:26240 bool completed_connect_;
[email protected]170e76c2010-10-04 15:04:20241
[email protected]0dc88b32014-03-26 20:12:28242 // Set when Read() or Write() successfully reads or writes data to or from the
243 // network.
244 bool was_ever_used_;
245
[email protected]d518cd92010-09-29 12:27:44246 // Stores client authentication information between ClientAuthHandler and
247 // GetSSLCertRequestInfo calls.
[email protected]d518cd92010-09-29 12:27:44248 bool client_auth_cert_needed_;
[email protected]515adc22013-01-09 16:01:23249 // List of DER-encoded X.509 DistinguishedName of certificate authorities
250 // allowed by the server.
251 std::vector<std::string> cert_authorities_;
[email protected]c0787702014-05-20 21:51:44252 // List of SSLClientCertType values for client certificates allowed by the
253 // server.
254 std::vector<SSLClientCertType> cert_key_types_;
[email protected]d518cd92010-09-29 12:27:44255
[email protected]822581d2010-12-16 17:27:15256 CertVerifier* const cert_verifier_;
257 scoped_ptr<SingleRequestCertVerifier> verifier_;
davidben09c3d072014-08-25 20:33:58258 base::TimeTicks start_cert_verification_time_;
[email protected]170e76c2010-10-04 15:04:20259
davidbeneb5f8ef32014-09-04 14:14:32260 // Certificate Transparency: Verifier and result holder.
261 ct::CTVerifyResult ct_verify_result_;
262 CTVerifier* cert_transparency_verifier_;
263
[email protected]ee0f2aa82013-10-25 11:59:26264 // The service for retrieving Channel ID keys. May be NULL.
[email protected]6b8a3c742014-07-25 00:25:35265 ChannelIDService* channel_id_service_;
[email protected]ee0f2aa82013-10-25 11:59:26266
[email protected]d518cd92010-09-29 12:27:44267 // OpenSSL stuff
[email protected]d518cd92010-09-29 12:27:44268 SSL* ssl_;
269 BIO* transport_bio_;
270
271 scoped_ptr<ClientSocketHandle> transport_;
[email protected]055d7f22010-11-15 12:03:12272 const HostPortPair host_and_port_;
[email protected]d518cd92010-09-29 12:27:44273 SSLConfig ssl_config_;
[email protected]c3456bb2011-12-12 22:22:19274 // ssl_session_cache_shard_ is an opaque string that partitions the SSL
275 // session cache. i.e. sessions created with one value will not attempt to
276 // resume on the socket with a different value.
277 const std::string ssl_session_cache_shard_;
[email protected]d518cd92010-09-29 12:27:44278
[email protected]d518cd92010-09-29 12:27:44279 enum State {
280 STATE_NONE,
281 STATE_HANDSHAKE,
[email protected]faff9852014-06-21 06:13:46282 STATE_CHANNEL_ID_LOOKUP,
283 STATE_CHANNEL_ID_LOOKUP_COMPLETE,
[email protected]d518cd92010-09-29 12:27:44284 STATE_VERIFY_CERT,
285 STATE_VERIFY_CERT_COMPLETE,
286 };
287 State next_handshake_state_;
[email protected]ea4a1c6a2010-12-09 13:33:28288 NextProtoStatus npn_status_;
289 std::string npn_proto_;
[email protected]6b8a3c742014-07-25 00:25:35290 // Written by the |channel_id_service_|.
[email protected]ee0f2aa82013-10-25 11:59:26291 std::string channel_id_private_key_;
292 std::string channel_id_cert_;
davidben52053b382015-04-27 19:22:29293 // True if a channel ID was sent.
294 bool channel_id_sent_;
davidbendafe4e52015-04-08 22:53:52295 // True if the initial handshake has completed.
296 bool handshake_completed_;
297 // True if the initial handshake's certificate has been verified.
298 bool certificate_verified_;
[email protected]6b8a3c742014-07-25 00:25:35299 // The request handle for |channel_id_service_|.
300 ChannelIDService::RequestHandle channel_id_request_handle_;
[email protected]8bd4e7a2014-08-09 14:49:17301
302 TransportSecurityState* transport_security_state_;
303
eranm6571b2b2014-12-03 15:53:23304 CertPolicyEnforcer* const policy_enforcer_;
305
[email protected]8bd4e7a2014-08-09 14:49:17306 // pinning_failure_log contains a message produced by
307 // TransportSecurityState::CheckPublicKeyPins in the event of a
308 // pinning failure. It is a (somewhat) human-readable string.
309 std::string pinning_failure_log_;
310
[email protected]d518cd92010-09-29 12:27:44311 BoundNetLog net_log_;
kulkarni.acd7b4462014-08-28 07:41:34312 base::WeakPtrFactory<SSLClientSocketOpenSSL> weak_factory_;
[email protected]d518cd92010-09-29 12:27:44313};
314
315} // namespace net
316
[email protected]7e5dd49f2010-12-08 18:33:49317#endif // NET_SOCKET_SSL_CLIENT_SOCKET_OPENSSL_H_