blob: 8952fefb127dfab4a83affe18cc56afa262ce558 [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"
[email protected]d518cd92010-09-29 12:27:4416#include "net/socket/client_socket_handle.h"
[email protected]536fd0b2013-03-14 17:41:5717#include "net/socket/ssl_client_socket.h"
[email protected]ee0f2aa82013-10-25 11:59:2618#include "net/ssl/server_bound_cert_service.h"
[email protected]536fd0b2013-03-14 17:41:5719#include "net/ssl/ssl_config_service.h"
[email protected]d518cd92010-09-29 12:27:4420
[email protected]03a07b2e2013-02-11 20:13:4521// Avoid including misc OpenSSL headers, i.e.:
22// <openssl/bio.h>
[email protected]d518cd92010-09-29 12:27:4423typedef struct bio_st BIO;
[email protected]03a07b2e2013-02-11 20:13:4524// <openssl/evp.h>
[email protected]718c9672010-12-02 10:04:1025typedef struct evp_pkey_st EVP_PKEY;
[email protected]03a07b2e2013-02-11 20:13:4526// <openssl/ssl.h>
[email protected]d518cd92010-09-29 12:27:4427typedef struct ssl_st SSL;
[email protected]03a07b2e2013-02-11 20:13:4528// <openssl/x509.h>
[email protected]718c9672010-12-02 10:04:1029typedef struct x509_st X509;
[email protected]b051cdb62014-02-28 02:20:1630// <openssl/ossl_type.h>
31typedef struct x509_store_ctx_st X509_STORE_CTX;
[email protected]d518cd92010-09-29 12:27:4432
33namespace net {
34
[email protected]170e76c2010-10-04 15:04:2035class CertVerifier;
[email protected]822581d2010-12-16 17:27:1536class SingleRequestCertVerifier;
[email protected]d518cd92010-09-29 12:27:4437class SSLCertRequestInfo;
[email protected]d518cd92010-09-29 12:27:4438class SSLInfo;
39
40// An SSL client socket implemented with OpenSSL.
41class SSLClientSocketOpenSSL : public SSLClientSocket {
42 public:
43 // Takes ownership of the transport_socket, which may already be connected.
44 // The given hostname will be compared with the name(s) in the server's
45 // certificate during the SSL handshake. ssl_config specifies the SSL
46 // settings.
[email protected]18ccfdb2013-08-15 00:13:4447 SSLClientSocketOpenSSL(scoped_ptr<ClientSocketHandle> transport_socket,
[email protected]055d7f22010-11-15 12:03:1248 const HostPortPair& host_and_port,
[email protected]822581d2010-12-16 17:27:1549 const SSLConfig& ssl_config,
[email protected]feb79bcd2011-07-21 16:55:1750 const SSLClientSocketContext& context);
[email protected]a5bd18f2012-05-11 15:51:4851 virtual ~SSLClientSocketOpenSSL();
[email protected]d518cd92010-09-29 12:27:4452
[email protected]fbef13932010-11-23 12:38:5353 const HostPortPair& host_and_port() const { return host_and_port_; }
[email protected]c3456bb2011-12-12 22:22:1954 const std::string& ssl_session_cache_shard() const {
55 return ssl_session_cache_shard_;
56 }
[email protected]fbef13932010-11-23 12:38:5357
[email protected]dbf036f2011-12-06 23:33:2458 // SSLClientSocket implementation.
[email protected]a5bd18f2012-05-11 15:51:4859 virtual void GetSSLCertRequestInfo(
60 SSLCertRequestInfo* cert_request_info) OVERRIDE;
[email protected]81ec7c12012-07-31 18:32:1961 virtual NextProtoStatus GetNextProto(std::string* proto,
62 std::string* server_protos) OVERRIDE;
63 virtual ServerBoundCertService* GetServerBoundCertService() const OVERRIDE;
64
65 // SSLSocket implementation.
[email protected]b0ff3f82011-07-23 05:12:3966 virtual int ExportKeyingMaterial(const base::StringPiece& label,
[email protected]1bc6f5e2012-03-15 00:20:5867 bool has_context,
[email protected]b0ff3f82011-07-23 05:12:3968 const base::StringPiece& context,
[email protected]1bc6f5e2012-03-15 00:20:5869 unsigned char* out,
[email protected]a5bd18f2012-05-11 15:51:4870 unsigned int outlen) OVERRIDE;
[email protected]81ec7c12012-07-31 18:32:1971 virtual int GetTLSUniqueChannelBinding(std::string* out) OVERRIDE;
[email protected]d518cd92010-09-29 12:27:4472
[email protected]dbf036f2011-12-06 23:33:2473 // StreamSocket implementation.
[email protected]a5bd18f2012-05-11 15:51:4874 virtual int Connect(const CompletionCallback& callback) OVERRIDE;
75 virtual void Disconnect() OVERRIDE;
76 virtual bool IsConnected() const OVERRIDE;
77 virtual bool IsConnectedAndIdle() const OVERRIDE;
[email protected]a3528692012-06-08 00:11:4278 virtual int GetPeerAddress(IPEndPoint* address) const OVERRIDE;
[email protected]a5bd18f2012-05-11 15:51:4879 virtual int GetLocalAddress(IPEndPoint* address) const OVERRIDE;
80 virtual const BoundNetLog& NetLog() const OVERRIDE;
81 virtual void SetSubresourceSpeculation() OVERRIDE;
82 virtual void SetOmniboxSpeculation() OVERRIDE;
83 virtual bool WasEverUsed() const OVERRIDE;
84 virtual bool UsingTCPFastOpen() const OVERRIDE;
[email protected]2d88e7d2012-07-19 17:55:1785 virtual bool GetSSLInfo(SSLInfo* ssl_info) OVERRIDE;
[email protected]d518cd92010-09-29 12:27:4486
[email protected]dbf036f2011-12-06 23:33:2487 // Socket implementation.
[email protected]3f55aa12011-12-07 02:03:3388 virtual int Read(IOBuffer* buf, int buf_len,
[email protected]a5bd18f2012-05-11 15:51:4889 const CompletionCallback& callback) OVERRIDE;
[email protected]83039bb2011-12-09 18:43:5590 virtual int Write(IOBuffer* buf, int buf_len,
[email protected]a5bd18f2012-05-11 15:51:4891 const CompletionCallback& callback) OVERRIDE;
92 virtual bool SetReceiveBufferSize(int32 size) OVERRIDE;
93 virtual bool SetSendBufferSize(int32 size) OVERRIDE;
[email protected]d518cd92010-09-29 12:27:4494
[email protected]7f38da8a2014-03-17 16:44:2695 protected:
96 // SSLClientSocket implementation.
97 virtual scoped_refptr<X509Certificate> GetUnverifiedServerCertificateChain()
98 const OVERRIDE;
99
[email protected]d518cd92010-09-29 12:27:44100 private:
[email protected]7f38da8a2014-03-17 16:44:26101 class PeerCertificateChain;
[email protected]821e3bb2013-11-08 01:06:01102 class SSLContext;
103 friend class SSLClientSocket;
104 friend class SSLContext;
105
[email protected]d518cd92010-09-29 12:27:44106 bool Init();
107 void DoReadCallback(int result);
108 void DoWriteCallback(int result);
109
110 bool DoTransportIO();
111 int DoHandshake();
[email protected]170e76c2010-10-04 15:04:20112 int DoVerifyCert(int result);
113 int DoVerifyCertComplete(int result);
[email protected]d518cd92010-09-29 12:27:44114 void DoConnectCallback(int result);
[email protected]170e76c2010-10-04 15:04:20115 X509Certificate* UpdateServerCert();
[email protected]d518cd92010-09-29 12:27:44116
117 void OnHandshakeIOComplete(int result);
118 void OnSendComplete(int result);
119 void OnRecvComplete(int result);
120
121 int DoHandshakeLoop(int last_io_result);
122 int DoReadLoop(int result);
123 int DoWriteLoop(int result);
124 int DoPayloadRead();
125 int DoPayloadWrite();
126
127 int BufferSend();
128 int BufferRecv();
129 void BufferSendComplete(int result);
130 void BufferRecvComplete(int result);
131 void TransportWriteComplete(int result);
[email protected]3e5c6922014-02-06 02:42:16132 int TransportReadComplete(int result);
[email protected]d518cd92010-09-29 12:27:44133
[email protected]821e3bb2013-11-08 01:06:01134 // Callback from the SSL layer that indicates the remote server is requesting
135 // a certificate for this client.
136 int ClientCertRequestCallback(SSL* ssl, X509** x509, EVP_PKEY** pkey);
137
138 // Callback from the SSL layer that indicates the remote server supports TLS
139 // Channel IDs.
140 void ChannelIDRequestCallback(SSL* ssl, EVP_PKEY** pkey);
141
[email protected]b051cdb62014-02-28 02:20:16142 // CertVerifyCallback is called to verify the server's certificates. We do
143 // verification after the handshake so this function only enforces that the
144 // certificates don't change during renegotiation.
145 int CertVerifyCallback(X509_STORE_CTX *store_ctx);
146
[email protected]821e3bb2013-11-08 01:06:01147 // Callback from the SSL layer to check which NPN protocol we are supporting
148 int SelectNextProtoCallback(unsigned char** out, unsigned char* outlen,
149 const unsigned char* in, unsigned int inlen);
150
[email protected]d518cd92010-09-29 12:27:44151 bool transport_send_busy_;
[email protected]d518cd92010-09-29 12:27:44152 bool transport_recv_busy_;
[email protected]a85197e2012-05-22 19:07:28153 bool transport_recv_eof_;
[email protected]4b768562013-02-16 04:10:07154
155 scoped_refptr<DrainableIOBuffer> send_buffer_;
[email protected]d518cd92010-09-29 12:27:44156 scoped_refptr<IOBuffer> recv_buffer_;
157
[email protected]dbf036f2011-12-06 23:33:24158 CompletionCallback user_connect_callback_;
[email protected]3f55aa12011-12-07 02:03:33159 CompletionCallback user_read_callback_;
[email protected]83039bb2011-12-09 18:43:55160 CompletionCallback user_write_callback_;
[email protected]d518cd92010-09-29 12:27:44161
[email protected]be90ba32013-05-13 20:05:25162 base::WeakPtrFactory<SSLClientSocketOpenSSL> weak_factory_;
163
[email protected]d518cd92010-09-29 12:27:44164 // Used by Read function.
165 scoped_refptr<IOBuffer> user_read_buf_;
166 int user_read_buf_len_;
167
168 // Used by Write function.
169 scoped_refptr<IOBuffer> user_write_buf_;
170 int user_write_buf_len_;
171
[email protected]4b768562013-02-16 04:10:07172 // Used by DoPayloadRead() when attempting to fill the caller's buffer with
173 // as much data as possible without blocking.
174 // If DoPayloadRead() encounters an error after having read some data, stores
175 // the result to return on the *next* call to DoPayloadRead(). A value > 0
176 // indicates there is no pending result, otherwise 0 indicates EOF and < 0
177 // indicates an error.
178 int pending_read_error_;
179
[email protected]3e5c6922014-02-06 02:42:16180 // Used by TransportWriteComplete() and TransportReadComplete() to signify an
181 // error writing to the transport socket. A value of OK indicates no error.
182 int transport_write_error_;
183
[email protected]170e76c2010-10-04 15:04:20184 // Set when handshake finishes.
[email protected]7f38da8a2014-03-17 16:44:26185 scoped_ptr<PeerCertificateChain> server_cert_chain_;
[email protected]170e76c2010-10-04 15:04:20186 scoped_refptr<X509Certificate> server_cert_;
187 CertVerifyResult server_cert_verify_result_;
[email protected]fbef13932010-11-23 12:38:53188 bool completed_handshake_;
[email protected]170e76c2010-10-04 15:04:20189
[email protected]d518cd92010-09-29 12:27:44190 // Stores client authentication information between ClientAuthHandler and
191 // GetSSLCertRequestInfo calls.
[email protected]d518cd92010-09-29 12:27:44192 bool client_auth_cert_needed_;
[email protected]515adc22013-01-09 16:01:23193 // List of DER-encoded X.509 DistinguishedName of certificate authorities
194 // allowed by the server.
195 std::vector<std::string> cert_authorities_;
[email protected]d518cd92010-09-29 12:27:44196
[email protected]822581d2010-12-16 17:27:15197 CertVerifier* const cert_verifier_;
198 scoped_ptr<SingleRequestCertVerifier> verifier_;
[email protected]170e76c2010-10-04 15:04:20199
[email protected]ee0f2aa82013-10-25 11:59:26200 // The service for retrieving Channel ID keys. May be NULL.
201 ServerBoundCertService* server_bound_cert_service_;
202
[email protected]d518cd92010-09-29 12:27:44203 // OpenSSL stuff
[email protected]d518cd92010-09-29 12:27:44204 SSL* ssl_;
205 BIO* transport_bio_;
206
207 scoped_ptr<ClientSocketHandle> transport_;
[email protected]055d7f22010-11-15 12:03:12208 const HostPortPair host_and_port_;
[email protected]d518cd92010-09-29 12:27:44209 SSLConfig ssl_config_;
[email protected]c3456bb2011-12-12 22:22:19210 // ssl_session_cache_shard_ is an opaque string that partitions the SSL
211 // session cache. i.e. sessions created with one value will not attempt to
212 // resume on the socket with a different value.
213 const std::string ssl_session_cache_shard_;
[email protected]d518cd92010-09-29 12:27:44214
[email protected]fbef13932010-11-23 12:38:53215 // Used for session cache diagnostics.
216 bool trying_cached_session_;
[email protected]d518cd92010-09-29 12:27:44217
218 enum State {
219 STATE_NONE,
220 STATE_HANDSHAKE,
221 STATE_VERIFY_CERT,
222 STATE_VERIFY_CERT_COMPLETE,
223 };
224 State next_handshake_state_;
[email protected]ea4a1c6a2010-12-09 13:33:28225 NextProtoStatus npn_status_;
226 std::string npn_proto_;
[email protected]55e973d2011-12-05 23:03:24227 std::string server_protos_;
[email protected]ee0f2aa82013-10-25 11:59:26228 // Written by the |server_bound_cert_service_|.
229 std::string channel_id_private_key_;
230 std::string channel_id_cert_;
231 // The return value of the last call to |server_bound_cert_service_|.
232 int channel_id_request_return_value_;
233 // True if channel ID extension was negotiated.
234 bool channel_id_xtn_negotiated_;
235 // The request handle for |server_bound_cert_service_|.
236 ServerBoundCertService::RequestHandle channel_id_request_handle_;
[email protected]d518cd92010-09-29 12:27:44237 BoundNetLog net_log_;
238};
239
240} // namespace net
241
[email protected]7e5dd49f2010-12-08 18:33:49242#endif // NET_SOCKET_SSL_CLIENT_SOCKET_OPENSSL_H_