blob: 8ed45c3cf340a96861907a1f343b3a6aaf1cf6c6 [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"
[email protected]c0787702014-05-20 21:51:4420#include "net/ssl/ssl_client_cert_type.h"
[email protected]536fd0b2013-03-14 17:41:5721#include "net/ssl/ssl_config_service.h"
[email protected]d518cd92010-09-29 12:27:4422
[email protected]03a07b2e2013-02-11 20:13:4523// Avoid including misc OpenSSL headers, i.e.:
24// <openssl/bio.h>
[email protected]d518cd92010-09-29 12:27:4425typedef struct bio_st BIO;
[email protected]03a07b2e2013-02-11 20:13:4526// <openssl/evp.h>
[email protected]718c9672010-12-02 10:04:1027typedef struct evp_pkey_st EVP_PKEY;
[email protected]03a07b2e2013-02-11 20:13:4528// <openssl/ssl.h>
[email protected]d518cd92010-09-29 12:27:4429typedef struct ssl_st SSL;
[email protected]03a07b2e2013-02-11 20:13:4530// <openssl/x509.h>
[email protected]718c9672010-12-02 10:04:1031typedef struct x509_st X509;
[email protected]b051cdb62014-02-28 02:20:1632// <openssl/ossl_type.h>
33typedef struct x509_store_ctx_st X509_STORE_CTX;
[email protected]d518cd92010-09-29 12:27:4434
35namespace net {
36
[email protected]170e76c2010-10-04 15:04:2037class CertVerifier;
davidbeneb5f8ef32014-09-04 14:14:3238class CTVerifier;
[email protected]822581d2010-12-16 17:27:1539class SingleRequestCertVerifier;
[email protected]d518cd92010-09-29 12:27:4440class SSLCertRequestInfo;
[email protected]d518cd92010-09-29 12:27:4441class SSLInfo;
42
43// An SSL client socket implemented with OpenSSL.
44class SSLClientSocketOpenSSL : public SSLClientSocket {
45 public:
46 // Takes ownership of the transport_socket, which may already be connected.
47 // The given hostname will be compared with the name(s) in the server's
48 // certificate during the SSL handshake. ssl_config specifies the SSL
49 // settings.
[email protected]18ccfdb2013-08-15 00:13:4450 SSLClientSocketOpenSSL(scoped_ptr<ClientSocketHandle> transport_socket,
[email protected]055d7f22010-11-15 12:03:1251 const HostPortPair& host_and_port,
[email protected]822581d2010-12-16 17:27:1552 const SSLConfig& ssl_config,
[email protected]feb79bcd2011-07-21 16:55:1753 const SSLClientSocketContext& context);
dchengb03027d2014-10-21 12:00:2054 ~SSLClientSocketOpenSSL() override;
[email protected]d518cd92010-09-29 12:27:4455
[email protected]fbef13932010-11-23 12:38:5356 const HostPortPair& host_and_port() const { return host_and_port_; }
[email protected]c3456bb2011-12-12 22:22:1957 const std::string& ssl_session_cache_shard() const {
58 return ssl_session_cache_shard_;
59 }
[email protected]fbef13932010-11-23 12:38:5360
[email protected]dbf036f2011-12-06 23:33:2461 // SSLClientSocket implementation.
dchengb03027d2014-10-21 12:00:2062 std::string GetSessionCacheKey() const override;
63 bool InSessionCache() const override;
64 void SetHandshakeCompletionCallback(const base::Closure& callback) override;
65 void GetSSLCertRequestInfo(SSLCertRequestInfo* cert_request_info) override;
66 NextProtoStatus GetNextProto(std::string* proto) override;
67 ChannelIDService* GetChannelIDService() 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;
[email protected]d518cd92010-09-29 12:27:4490
[email protected]dbf036f2011-12-06 23:33:2491 // Socket implementation.
dchengb03027d2014-10-21 12:00:2092 int Read(IOBuffer* buf,
93 int buf_len,
94 const CompletionCallback& callback) override;
95 int Write(IOBuffer* buf,
96 int buf_len,
97 const CompletionCallback& callback) override;
98 int SetReceiveBufferSize(int32 size) override;
99 int SetSendBufferSize(int32 size) override;
[email protected]d518cd92010-09-29 12:27:44100
[email protected]7f38da8a2014-03-17 16:44:26101 protected:
102 // SSLClientSocket implementation.
dchengb03027d2014-10-21 12:00:20103 scoped_refptr<X509Certificate> GetUnverifiedServerCertificateChain()
mostynbba063d6032014-10-09 11:01:13104 const override;
[email protected]7f38da8a2014-03-17 16:44:26105
[email protected]d518cd92010-09-29 12:27:44106 private:
[email protected]7f38da8a2014-03-17 16:44:26107 class PeerCertificateChain;
[email protected]821e3bb2013-11-08 01:06:01108 class SSLContext;
109 friend class SSLClientSocket;
110 friend class SSLContext;
111
[email protected]c8a80e92014-05-17 16:02:08112 int Init();
[email protected]d518cd92010-09-29 12:27:44113 void DoReadCallback(int result);
114 void DoWriteCallback(int result);
115
[email protected]8e458552014-08-05 00:02:15116 void OnHandshakeCompletion();
117
[email protected]d518cd92010-09-29 12:27:44118 bool DoTransportIO();
119 int DoHandshake();
[email protected]faff9852014-06-21 06:13:46120 int DoChannelIDLookup();
121 int DoChannelIDLookupComplete(int result);
[email protected]170e76c2010-10-04 15:04:20122 int DoVerifyCert(int result);
123 int DoVerifyCertComplete(int result);
[email protected]d518cd92010-09-29 12:27:44124 void DoConnectCallback(int result);
davidben30798ed82014-09-19 19:28:20125 void UpdateServerCert();
davidbeneb5f8ef32014-09-04 14:14:32126 void VerifyCT();
[email protected]d518cd92010-09-29 12:27:44127
128 void OnHandshakeIOComplete(int result);
129 void OnSendComplete(int result);
130 void OnRecvComplete(int result);
131
132 int DoHandshakeLoop(int last_io_result);
davidben1b133ad2014-10-23 04:23:13133 int DoReadLoop();
134 int DoWriteLoop();
[email protected]d518cd92010-09-29 12:27:44135 int DoPayloadRead();
136 int DoPayloadWrite();
137
138 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
[email protected]64b5c892014-08-08 09:39:26173 // Callback that is used to obtain information about the state of the SSL
174 // handshake.
175 static void InfoCallback(const SSL* ssl, int type, int val);
176
[email protected]e4738ba52014-08-07 10:07:22177 void CheckIfHandshakeFinished();
178
davidbeneb5f8ef32014-09-04 14:14:32179 // Adds the SignedCertificateTimestamps from ct_verify_result_ to |ssl_info|.
180 // SCTs are held in three separate vectors in ct_verify_result, each
181 // vetor representing a particular verification state, this method associates
182 // each of the SCTs with the corresponding SCTVerifyStatus as it adds it to
183 // the |ssl_info|.signed_certificate_timestamps list.
184 void AddSCTInfoToSSLInfo(SSLInfo* ssl_info) const;
185
[email protected]d518cd92010-09-29 12:27:44186 bool transport_send_busy_;
[email protected]d518cd92010-09-29 12:27:44187 bool transport_recv_busy_;
[email protected]4b768562013-02-16 04:10:07188
189 scoped_refptr<DrainableIOBuffer> send_buffer_;
[email protected]d518cd92010-09-29 12:27:44190 scoped_refptr<IOBuffer> recv_buffer_;
191
[email protected]dbf036f2011-12-06 23:33:24192 CompletionCallback user_connect_callback_;
[email protected]3f55aa12011-12-07 02:03:33193 CompletionCallback user_read_callback_;
[email protected]83039bb2011-12-09 18:43:55194 CompletionCallback user_write_callback_;
[email protected]d518cd92010-09-29 12:27:44195
[email protected]d518cd92010-09-29 12:27:44196 // Used by Read function.
197 scoped_refptr<IOBuffer> user_read_buf_;
198 int user_read_buf_len_;
199
200 // Used by Write function.
201 scoped_refptr<IOBuffer> user_write_buf_;
202 int user_write_buf_len_;
203
[email protected]4b768562013-02-16 04:10:07204 // Used by DoPayloadRead() when attempting to fill the caller's buffer with
205 // as much data as possible without blocking.
206 // If DoPayloadRead() encounters an error after having read some data, stores
207 // the result to return on the *next* call to DoPayloadRead(). A value > 0
208 // indicates there is no pending result, otherwise 0 indicates EOF and < 0
209 // indicates an error.
210 int pending_read_error_;
211
[email protected]5aea79182014-07-14 20:43:41212 // Used by TransportReadComplete() to signify an error reading from the
213 // transport socket. A value of OK indicates the socket is still
214 // readable. EOFs are mapped to ERR_CONNECTION_CLOSED.
215 int transport_read_error_;
216
[email protected]3e5c6922014-02-06 02:42:16217 // Used by TransportWriteComplete() and TransportReadComplete() to signify an
218 // error writing to the transport socket. A value of OK indicates no error.
219 int transport_write_error_;
220
[email protected]64b5c892014-08-08 09:39:26221 // Set when Connect finishes.
[email protected]7f38da8a2014-03-17 16:44:26222 scoped_ptr<PeerCertificateChain> server_cert_chain_;
[email protected]170e76c2010-10-04 15:04:20223 scoped_refptr<X509Certificate> server_cert_;
224 CertVerifyResult server_cert_verify_result_;
[email protected]64b5c892014-08-08 09:39:26225 bool completed_connect_;
[email protected]170e76c2010-10-04 15:04:20226
[email protected]0dc88b32014-03-26 20:12:28227 // Set when Read() or Write() successfully reads or writes data to or from the
228 // network.
229 bool was_ever_used_;
230
[email protected]d518cd92010-09-29 12:27:44231 // Stores client authentication information between ClientAuthHandler and
232 // GetSSLCertRequestInfo calls.
[email protected]d518cd92010-09-29 12:27:44233 bool client_auth_cert_needed_;
[email protected]515adc22013-01-09 16:01:23234 // List of DER-encoded X.509 DistinguishedName of certificate authorities
235 // allowed by the server.
236 std::vector<std::string> cert_authorities_;
[email protected]c0787702014-05-20 21:51:44237 // List of SSLClientCertType values for client certificates allowed by the
238 // server.
239 std::vector<SSLClientCertType> cert_key_types_;
[email protected]d518cd92010-09-29 12:27:44240
[email protected]822581d2010-12-16 17:27:15241 CertVerifier* const cert_verifier_;
242 scoped_ptr<SingleRequestCertVerifier> verifier_;
davidben09c3d072014-08-25 20:33:58243 base::TimeTicks start_cert_verification_time_;
[email protected]170e76c2010-10-04 15:04:20244
davidbeneb5f8ef32014-09-04 14:14:32245 // Certificate Transparency: Verifier and result holder.
246 ct::CTVerifyResult ct_verify_result_;
247 CTVerifier* cert_transparency_verifier_;
248
[email protected]ee0f2aa82013-10-25 11:59:26249 // The service for retrieving Channel ID keys. May be NULL.
[email protected]6b8a3c742014-07-25 00:25:35250 ChannelIDService* channel_id_service_;
[email protected]ee0f2aa82013-10-25 11:59:26251
[email protected]8e458552014-08-05 00:02:15252 // Callback that is invoked when the connection finishes.
253 //
254 // Note: this callback will be run in Disconnect(). It will not alter
255 // any member variables of the SSLClientSocketOpenSSL.
256 base::Closure handshake_completion_callback_;
257
[email protected]d518cd92010-09-29 12:27:44258 // OpenSSL stuff
[email protected]d518cd92010-09-29 12:27:44259 SSL* ssl_;
260 BIO* transport_bio_;
261
262 scoped_ptr<ClientSocketHandle> transport_;
[email protected]055d7f22010-11-15 12:03:12263 const HostPortPair host_and_port_;
[email protected]d518cd92010-09-29 12:27:44264 SSLConfig ssl_config_;
[email protected]c3456bb2011-12-12 22:22:19265 // ssl_session_cache_shard_ is an opaque string that partitions the SSL
266 // session cache. i.e. sessions created with one value will not attempt to
267 // resume on the socket with a different value.
268 const std::string ssl_session_cache_shard_;
[email protected]d518cd92010-09-29 12:27:44269
[email protected]fbef13932010-11-23 12:38:53270 // Used for session cache diagnostics.
271 bool trying_cached_session_;
[email protected]d518cd92010-09-29 12:27:44272
273 enum State {
274 STATE_NONE,
275 STATE_HANDSHAKE,
[email protected]faff9852014-06-21 06:13:46276 STATE_CHANNEL_ID_LOOKUP,
277 STATE_CHANNEL_ID_LOOKUP_COMPLETE,
[email protected]d518cd92010-09-29 12:27:44278 STATE_VERIFY_CERT,
279 STATE_VERIFY_CERT_COMPLETE,
280 };
281 State next_handshake_state_;
[email protected]ea4a1c6a2010-12-09 13:33:28282 NextProtoStatus npn_status_;
283 std::string npn_proto_;
[email protected]6b8a3c742014-07-25 00:25:35284 // Written by the |channel_id_service_|.
[email protected]ee0f2aa82013-10-25 11:59:26285 std::string channel_id_private_key_;
286 std::string channel_id_cert_;
[email protected]ee0f2aa82013-10-25 11:59:26287 // True if channel ID extension was negotiated.
288 bool channel_id_xtn_negotiated_;
[email protected]e4738ba52014-08-07 10:07:22289 // True if InfoCallback has been run with result = SSL_CB_HANDSHAKE_DONE.
[email protected]64b5c892014-08-08 09:39:26290 bool handshake_succeeded_;
[email protected]e4738ba52014-08-07 10:07:22291 // True if MarkSSLSessionAsGood has been called for this socket's
[email protected]64b5c892014-08-08 09:39:26292 // SSL session.
[email protected]e4738ba52014-08-07 10:07:22293 bool marked_session_as_good_;
[email protected]6b8a3c742014-07-25 00:25:35294 // The request handle for |channel_id_service_|.
295 ChannelIDService::RequestHandle channel_id_request_handle_;
[email protected]8bd4e7a2014-08-09 14:49:17296
297 TransportSecurityState* transport_security_state_;
298
299 // pinning_failure_log contains a message produced by
300 // TransportSecurityState::CheckPublicKeyPins in the event of a
301 // pinning failure. It is a (somewhat) human-readable string.
302 std::string pinning_failure_log_;
303
[email protected]d518cd92010-09-29 12:27:44304 BoundNetLog net_log_;
kulkarni.acd7b4462014-08-28 07:41:34305 base::WeakPtrFactory<SSLClientSocketOpenSSL> weak_factory_;
[email protected]d518cd92010-09-29 12:27:44306};
307
308} // namespace net
309
[email protected]7e5dd49f2010-12-08 18:33:49310#endif // NET_SOCKET_SSL_CLIENT_SOCKET_OPENSSL_H_