blob: fd7a68a3ea29f6e1484ea4168a2923fbd64d87a0 [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"
eroman7f9236a2015-05-11 21:23:4315#include "net/cert/cert_verifier.h"
[email protected]6e7845ae2013-03-29 21:48:1116#include "net/cert/cert_verify_result.h"
davidbeneb5f8ef32014-09-04 14:14:3217#include "net/cert/ct_verify_result.h"
[email protected]d518cd92010-09-29 12:27:4418#include "net/socket/client_socket_handle.h"
[email protected]536fd0b2013-03-14 17:41:5719#include "net/socket/ssl_client_socket.h"
[email protected]6b8a3c742014-07-25 00:25:3520#include "net/ssl/channel_id_service.h"
davidbenb8c23212014-10-28 00:12:1621#include "net/ssl/openssl_ssl_util.h"
[email protected]c0787702014-05-20 21:51:4422#include "net/ssl/ssl_client_cert_type.h"
[email protected]536fd0b2013-03-14 17:41:5723#include "net/ssl/ssl_config_service.h"
davidbenf2eaaf92015-05-15 22:18:4224#include "net/ssl/ssl_failure_state.h"
[email protected]d518cd92010-09-29 12:27:4425
[email protected]03a07b2e2013-02-11 20:13:4526// Avoid including misc OpenSSL headers, i.e.:
27// <openssl/bio.h>
[email protected]d518cd92010-09-29 12:27:4428typedef struct bio_st BIO;
[email protected]03a07b2e2013-02-11 20:13:4529// <openssl/evp.h>
[email protected]718c9672010-12-02 10:04:1030typedef struct evp_pkey_st EVP_PKEY;
[email protected]03a07b2e2013-02-11 20:13:4531// <openssl/ssl.h>
[email protected]d518cd92010-09-29 12:27:4432typedef struct ssl_st SSL;
[email protected]03a07b2e2013-02-11 20:13:4533// <openssl/x509.h>
[email protected]718c9672010-12-02 10:04:1034typedef struct x509_st X509;
[email protected]b051cdb62014-02-28 02:20:1635// <openssl/ossl_type.h>
36typedef struct x509_store_ctx_st X509_STORE_CTX;
[email protected]d518cd92010-09-29 12:27:4437
38namespace net {
39
[email protected]170e76c2010-10-04 15:04:2040class CertVerifier;
davidbeneb5f8ef32014-09-04 14:14:3241class CTVerifier;
[email protected]d518cd92010-09-29 12:27:4442class SSLCertRequestInfo;
[email protected]d518cd92010-09-29 12:27:4443class SSLInfo;
44
45// An SSL client socket implemented with OpenSSL.
46class SSLClientSocketOpenSSL : public SSLClientSocket {
47 public:
48 // Takes ownership of the transport_socket, which may already be connected.
49 // The given hostname will be compared with the name(s) in the server's
50 // certificate during the SSL handshake. ssl_config specifies the SSL
51 // settings.
[email protected]18ccfdb2013-08-15 00:13:4452 SSLClientSocketOpenSSL(scoped_ptr<ClientSocketHandle> transport_socket,
[email protected]055d7f22010-11-15 12:03:1253 const HostPortPair& host_and_port,
[email protected]822581d2010-12-16 17:27:1554 const SSLConfig& ssl_config,
[email protected]feb79bcd2011-07-21 16:55:1755 const SSLClientSocketContext& context);
dchengb03027d2014-10-21 12:00:2056 ~SSLClientSocketOpenSSL() override;
[email protected]d518cd92010-09-29 12:27:4457
[email protected]fbef13932010-11-23 12:38:5358 const HostPortPair& host_and_port() const { return host_and_port_; }
[email protected]c3456bb2011-12-12 22:22:1959 const std::string& ssl_session_cache_shard() const {
60 return ssl_session_cache_shard_;
61 }
[email protected]fbef13932010-11-23 12:38:5362
[email protected]dbf036f2011-12-06 23:33:2463 // SSLClientSocket implementation.
dchengb03027d2014-10-21 12:00:2064 void GetSSLCertRequestInfo(SSLCertRequestInfo* cert_request_info) override;
davidben6974bf72015-04-27 17:52:4865 NextProtoStatus GetNextProto(std::string* proto) const override;
dchengb03027d2014-10-21 12:00:2066 ChannelIDService* GetChannelIDService() const override;
davidbenf2eaaf92015-05-15 22:18:4267 SSLFailureState GetSSLFailureState() const override;
[email protected]81ec7c12012-07-31 18:32:1968
69 // SSLSocket implementation.
dchengb03027d2014-10-21 12:00:2070 int ExportKeyingMaterial(const base::StringPiece& label,
71 bool has_context,
72 const base::StringPiece& context,
73 unsigned char* out,
74 unsigned int outlen) override;
75 int GetTLSUniqueChannelBinding(std::string* out) override;
[email protected]d518cd92010-09-29 12:27:4476
[email protected]dbf036f2011-12-06 23:33:2477 // StreamSocket implementation.
dchengb03027d2014-10-21 12:00:2078 int Connect(const CompletionCallback& callback) override;
79 void Disconnect() override;
80 bool IsConnected() const override;
81 bool IsConnectedAndIdle() const override;
82 int GetPeerAddress(IPEndPoint* address) const override;
83 int GetLocalAddress(IPEndPoint* address) const override;
84 const BoundNetLog& NetLog() const override;
85 void SetSubresourceSpeculation() override;
86 void SetOmniboxSpeculation() override;
87 bool WasEverUsed() const override;
88 bool UsingTCPFastOpen() const override;
89 bool GetSSLInfo(SSLInfo* ssl_info) override;
ttuttle23fdb7b2015-05-15 01:28:0390 void GetConnectionAttempts(ConnectionAttempts* out) const override;
91 void ClearConnectionAttempts() override {}
92 void AddConnectionAttempts(const ConnectionAttempts& attempts) override {}
[email protected]d518cd92010-09-29 12:27:4493
[email protected]dbf036f2011-12-06 23:33:2494 // Socket implementation.
dchengb03027d2014-10-21 12:00:2095 int Read(IOBuffer* buf,
96 int buf_len,
97 const CompletionCallback& callback) override;
98 int Write(IOBuffer* buf,
99 int buf_len,
100 const CompletionCallback& callback) override;
101 int SetReceiveBufferSize(int32 size) override;
102 int SetSendBufferSize(int32 size) override;
[email protected]d518cd92010-09-29 12:27:44103
[email protected]7f38da8a2014-03-17 16:44:26104 protected:
105 // SSLClientSocket implementation.
dchengb03027d2014-10-21 12:00:20106 scoped_refptr<X509Certificate> GetUnverifiedServerCertificateChain()
mostynbba063d6032014-10-09 11:01:13107 const override;
[email protected]7f38da8a2014-03-17 16:44:26108
[email protected]d518cd92010-09-29 12:27:44109 private:
[email protected]7f38da8a2014-03-17 16:44:26110 class PeerCertificateChain;
[email protected]821e3bb2013-11-08 01:06:01111 class SSLContext;
112 friend class SSLClientSocket;
113 friend class SSLContext;
114
[email protected]c8a80e92014-05-17 16:02:08115 int Init();
[email protected]d518cd92010-09-29 12:27:44116 void DoReadCallback(int result);
117 void DoWriteCallback(int result);
118
[email protected]d518cd92010-09-29 12:27:44119 bool DoTransportIO();
120 int DoHandshake();
davidbenc4212c02015-05-12 22:30:18121 int DoHandshakeComplete(int result);
[email protected]faff9852014-06-21 06:13:46122 int DoChannelIDLookup();
123 int DoChannelIDLookupComplete(int result);
[email protected]170e76c2010-10-04 15:04:20124 int DoVerifyCert(int result);
125 int DoVerifyCertComplete(int result);
[email protected]d518cd92010-09-29 12:27:44126 void DoConnectCallback(int result);
davidben30798ed82014-09-19 19:28:20127 void UpdateServerCert();
davidbeneb5f8ef32014-09-04 14:14:32128 void VerifyCT();
[email protected]d518cd92010-09-29 12:27:44129
130 void OnHandshakeIOComplete(int result);
131 void OnSendComplete(int result);
132 void OnRecvComplete(int result);
133
134 int DoHandshakeLoop(int last_io_result);
davidben1b133ad2014-10-23 04:23:13135 int DoReadLoop();
136 int DoWriteLoop();
[email protected]d518cd92010-09-29 12:27:44137 int DoPayloadRead();
138 int DoPayloadWrite();
139
140 int BufferSend();
141 int BufferRecv();
142 void BufferSendComplete(int result);
143 void BufferRecvComplete(int result);
144 void TransportWriteComplete(int result);
[email protected]3e5c6922014-02-06 02:42:16145 int TransportReadComplete(int result);
[email protected]d518cd92010-09-29 12:27:44146
[email protected]821e3bb2013-11-08 01:06:01147 // Callback from the SSL layer that indicates the remote server is requesting
148 // a certificate for this client.
[email protected]82c59022014-08-15 09:38:27149 int ClientCertRequestCallback(SSL* ssl);
[email protected]821e3bb2013-11-08 01:06:01150
[email protected]b051cdb62014-02-28 02:20:16151 // CertVerifyCallback is called to verify the server's certificates. We do
152 // verification after the handshake so this function only enforces that the
153 // certificates don't change during renegotiation.
154 int CertVerifyCallback(X509_STORE_CTX *store_ctx);
155
[email protected]821e3bb2013-11-08 01:06:01156 // Callback from the SSL layer to check which NPN protocol we are supporting
157 int SelectNextProtoCallback(unsigned char** out, unsigned char* outlen,
158 const unsigned char* in, unsigned int inlen);
159
[email protected]5aea79182014-07-14 20:43:41160 // Called during an operation on |transport_bio_|'s peer. Checks saved
161 // transport error state and, if appropriate, returns an error through
162 // OpenSSL's error system.
163 long MaybeReplayTransportError(BIO *bio,
164 int cmd,
165 const char *argp, int argi, long argl,
166 long retvalue);
167
168 // Callback from the SSL layer when an operation is performed on
169 // |transport_bio_|'s peer.
170 static long BIOCallback(BIO *bio,
171 int cmd,
172 const char *argp, int argi, long argl,
173 long retvalue);
174
davidbendafe4e52015-04-08 22:53:52175 // Called after the initial handshake completes and after the server
176 // certificate has been verified. The order of handshake completion and
177 // certificate verification depends on whether the connection was false
178 // started. After both have happened (thus calling this twice), the session is
179 // safe to cache and will be cached.
180 void MaybeCacheSession();
181
182 // Callback from the SSL layer when the internal state machine progresses. It
183 // is used to listen for when the handshake completes entirely; |Connect| may
184 // return early if false starting.
185 void InfoCallback(int type, int val);
186
davidbeneb5f8ef32014-09-04 14:14:32187 // Adds the SignedCertificateTimestamps from ct_verify_result_ to |ssl_info|.
188 // SCTs are held in three separate vectors in ct_verify_result, each
189 // vetor representing a particular verification state, this method associates
190 // each of the SCTs with the corresponding SCTVerifyStatus as it adds it to
191 // the |ssl_info|.signed_certificate_timestamps list.
192 void AddSCTInfoToSSLInfo(SSLInfo* ssl_info) const;
193
rsleevif020edc2015-03-16 19:31:24194 // Returns a unique key string for the SSL session cache for
195 // this socket.
196 std::string GetSessionCacheKey() const;
197
davidben421116c2015-05-12 19:56:51198 // Returns true if renegotiations are allowed.
199 bool IsRenegotiationAllowed() const;
200
[email protected]d518cd92010-09-29 12:27:44201 bool transport_send_busy_;
[email protected]d518cd92010-09-29 12:27:44202 bool transport_recv_busy_;
[email protected]4b768562013-02-16 04:10:07203
haavardm2d92e722014-12-19 13:45:44204 // Buffers which are shared by BoringSSL and SSLClientSocketOpenSSL.
205 // GrowableIOBuffer is used to keep ownership and setting offset.
206 scoped_refptr<GrowableIOBuffer> send_buffer_;
207 scoped_refptr<GrowableIOBuffer> recv_buffer_;
[email protected]d518cd92010-09-29 12:27:44208
[email protected]dbf036f2011-12-06 23:33:24209 CompletionCallback user_connect_callback_;
[email protected]3f55aa12011-12-07 02:03:33210 CompletionCallback user_read_callback_;
[email protected]83039bb2011-12-09 18:43:55211 CompletionCallback user_write_callback_;
[email protected]d518cd92010-09-29 12:27:44212
[email protected]d518cd92010-09-29 12:27:44213 // Used by Read function.
214 scoped_refptr<IOBuffer> user_read_buf_;
215 int user_read_buf_len_;
216
217 // Used by Write function.
218 scoped_refptr<IOBuffer> user_write_buf_;
219 int user_write_buf_len_;
220
[email protected]4b768562013-02-16 04:10:07221 // Used by DoPayloadRead() when attempting to fill the caller's buffer with
222 // as much data as possible without blocking.
223 // If DoPayloadRead() encounters an error after having read some data, stores
224 // the result to return on the *next* call to DoPayloadRead(). A value > 0
225 // indicates there is no pending result, otherwise 0 indicates EOF and < 0
226 // indicates an error.
227 int pending_read_error_;
228
davidbenb8c23212014-10-28 00:12:16229 // If there is a pending read result, the OpenSSL result code (output of
230 // SSL_get_error) associated with it.
231 int pending_read_ssl_error_;
232
233 // If there is a pending read result, the OpenSSLErrorInfo associated with it.
234 OpenSSLErrorInfo pending_read_error_info_;
235
[email protected]5aea79182014-07-14 20:43:41236 // Used by TransportReadComplete() to signify an error reading from the
237 // transport socket. A value of OK indicates the socket is still
238 // readable. EOFs are mapped to ERR_CONNECTION_CLOSED.
239 int transport_read_error_;
240
[email protected]3e5c6922014-02-06 02:42:16241 // Used by TransportWriteComplete() and TransportReadComplete() to signify an
242 // error writing to the transport socket. A value of OK indicates no error.
243 int transport_write_error_;
244
[email protected]64b5c892014-08-08 09:39:26245 // Set when Connect finishes.
[email protected]7f38da8a2014-03-17 16:44:26246 scoped_ptr<PeerCertificateChain> server_cert_chain_;
[email protected]170e76c2010-10-04 15:04:20247 scoped_refptr<X509Certificate> server_cert_;
248 CertVerifyResult server_cert_verify_result_;
[email protected]64b5c892014-08-08 09:39:26249 bool completed_connect_;
[email protected]170e76c2010-10-04 15:04:20250
[email protected]0dc88b32014-03-26 20:12:28251 // Set when Read() or Write() successfully reads or writes data to or from the
252 // network.
253 bool was_ever_used_;
254
[email protected]515adc22013-01-09 16:01:23255 // List of DER-encoded X.509 DistinguishedName of certificate authorities
256 // allowed by the server.
257 std::vector<std::string> cert_authorities_;
[email protected]c0787702014-05-20 21:51:44258 // List of SSLClientCertType values for client certificates allowed by the
259 // server.
260 std::vector<SSLClientCertType> cert_key_types_;
[email protected]d518cd92010-09-29 12:27:44261
[email protected]822581d2010-12-16 17:27:15262 CertVerifier* const cert_verifier_;
eroman7f9236a2015-05-11 21:23:43263 scoped_ptr<CertVerifier::Request> cert_verifier_request_;
davidben09c3d072014-08-25 20:33:58264 base::TimeTicks start_cert_verification_time_;
[email protected]170e76c2010-10-04 15:04:20265
davidbeneb5f8ef32014-09-04 14:14:32266 // Certificate Transparency: Verifier and result holder.
267 ct::CTVerifyResult ct_verify_result_;
268 CTVerifier* cert_transparency_verifier_;
269
[email protected]ee0f2aa82013-10-25 11:59:26270 // The service for retrieving Channel ID keys. May be NULL.
[email protected]6b8a3c742014-07-25 00:25:35271 ChannelIDService* channel_id_service_;
[email protected]ee0f2aa82013-10-25 11:59:26272
[email protected]d518cd92010-09-29 12:27:44273 // OpenSSL stuff
[email protected]d518cd92010-09-29 12:27:44274 SSL* ssl_;
275 BIO* transport_bio_;
276
277 scoped_ptr<ClientSocketHandle> transport_;
[email protected]055d7f22010-11-15 12:03:12278 const HostPortPair host_and_port_;
[email protected]d518cd92010-09-29 12:27:44279 SSLConfig ssl_config_;
[email protected]c3456bb2011-12-12 22:22:19280 // ssl_session_cache_shard_ is an opaque string that partitions the SSL
281 // session cache. i.e. sessions created with one value will not attempt to
282 // resume on the socket with a different value.
283 const std::string ssl_session_cache_shard_;
[email protected]d518cd92010-09-29 12:27:44284
[email protected]d518cd92010-09-29 12:27:44285 enum State {
286 STATE_NONE,
287 STATE_HANDSHAKE,
davidbenc4212c02015-05-12 22:30:18288 STATE_HANDSHAKE_COMPLETE,
[email protected]faff9852014-06-21 06:13:46289 STATE_CHANNEL_ID_LOOKUP,
290 STATE_CHANNEL_ID_LOOKUP_COMPLETE,
[email protected]d518cd92010-09-29 12:27:44291 STATE_VERIFY_CERT,
292 STATE_VERIFY_CERT_COMPLETE,
293 };
294 State next_handshake_state_;
[email protected]ea4a1c6a2010-12-09 13:33:28295 NextProtoStatus npn_status_;
296 std::string npn_proto_;
[email protected]6b8a3c742014-07-25 00:25:35297 // Written by the |channel_id_service_|.
nharper2e171cf2015-06-01 20:29:23298 scoped_ptr<crypto::ECPrivateKey> channel_id_key_;
davidben52053b382015-04-27 19:22:29299 // True if a channel ID was sent.
300 bool channel_id_sent_;
davidbendafe4e52015-04-08 22:53:52301 // True if the initial handshake has completed.
302 bool handshake_completed_;
303 // True if the initial handshake's certificate has been verified.
304 bool certificate_verified_;
[email protected]6b8a3c742014-07-25 00:25:35305 // The request handle for |channel_id_service_|.
nharper75ade892015-06-10 19:05:35306 ChannelIDService::Request channel_id_request_;
davidbenf2eaaf92015-05-15 22:18:42307 SSLFailureState ssl_failure_state_;
[email protected]8bd4e7a2014-08-09 14:49:17308
309 TransportSecurityState* transport_security_state_;
310
eranm6571b2b2014-12-03 15:53:23311 CertPolicyEnforcer* const policy_enforcer_;
312
[email protected]8bd4e7a2014-08-09 14:49:17313 // pinning_failure_log contains a message produced by
314 // TransportSecurityState::CheckPublicKeyPins in the event of a
315 // pinning failure. It is a (somewhat) human-readable string.
316 std::string pinning_failure_log_;
317
[email protected]d518cd92010-09-29 12:27:44318 BoundNetLog net_log_;
kulkarni.acd7b4462014-08-28 07:41:34319 base::WeakPtrFactory<SSLClientSocketOpenSSL> weak_factory_;
[email protected]d518cd92010-09-29 12:27:44320};
321
322} // namespace net
323
[email protected]7e5dd49f2010-12-08 18:33:49324#endif // NET_SOCKET_SSL_CLIENT_SOCKET_OPENSSL_H_