blob: dccdb3217c6ccefad69d7aa56d452b7adf721d0a [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]170e76c2010-10-04 15:04:2012#include "net/base/cert_verify_result.h"
[email protected]d518cd92010-09-29 12:27:4413#include "net/base/completion_callback.h"
14#include "net/base/io_buffer.h"
15#include "net/base/ssl_config_service.h"
16#include "net/socket/ssl_client_socket.h"
17#include "net/socket/client_socket_handle.h"
18
19typedef struct bio_st BIO;
[email protected]718c9672010-12-02 10:04:1020typedef struct evp_pkey_st EVP_PKEY;
[email protected]d518cd92010-09-29 12:27:4421typedef struct ssl_st SSL;
[email protected]718c9672010-12-02 10:04:1022typedef struct x509_st X509;
[email protected]d518cd92010-09-29 12:27:4423
24namespace net {
25
[email protected]170e76c2010-10-04 15:04:2026class CertVerifier;
[email protected]822581d2010-12-16 17:27:1527class SingleRequestCertVerifier;
[email protected]d518cd92010-09-29 12:27:4428class SSLCertRequestInfo;
[email protected]d518cd92010-09-29 12:27:4429class SSLInfo;
30
31// An SSL client socket implemented with OpenSSL.
32class SSLClientSocketOpenSSL : public SSLClientSocket {
33 public:
34 // Takes ownership of the transport_socket, which may already be connected.
35 // The given hostname will be compared with the name(s) in the server's
36 // certificate during the SSL handshake. ssl_config specifies the SSL
37 // settings.
38 SSLClientSocketOpenSSL(ClientSocketHandle* transport_socket,
[email protected]055d7f22010-11-15 12:03:1239 const HostPortPair& host_and_port,
[email protected]822581d2010-12-16 17:27:1540 const SSLConfig& ssl_config,
[email protected]feb79bcd2011-07-21 16:55:1741 const SSLClientSocketContext& context);
[email protected]a5bd18f2012-05-11 15:51:4842 virtual ~SSLClientSocketOpenSSL();
[email protected]d518cd92010-09-29 12:27:4443
[email protected]fbef13932010-11-23 12:38:5344 const HostPortPair& host_and_port() const { return host_and_port_; }
[email protected]c3456bb2011-12-12 22:22:1945 const std::string& ssl_session_cache_shard() const {
46 return ssl_session_cache_shard_;
47 }
[email protected]fbef13932010-11-23 12:38:5348
[email protected]718c9672010-12-02 10:04:1049 // Callback from the SSL layer that indicates the remote server is requesting
50 // a certificate for this client.
51 int ClientCertRequestCallback(SSL* ssl, X509** x509, EVP_PKEY** pkey);
52
[email protected]ea4a1c6a2010-12-09 13:33:2853 // Callback from the SSL layer to check which NPN protocol we are supporting
54 int SelectNextProtoCallback(unsigned char** out, unsigned char* outlen,
55 const unsigned char* in, unsigned int inlen);
56
[email protected]dbf036f2011-12-06 23:33:2457 // SSLClientSocket implementation.
[email protected]a5bd18f2012-05-11 15:51:4858 virtual void GetSSLCertRequestInfo(
59 SSLCertRequestInfo* cert_request_info) OVERRIDE;
[email protected]81ec7c12012-07-31 18:32:1960 virtual NextProtoStatus GetNextProto(std::string* proto,
61 std::string* server_protos) OVERRIDE;
62 virtual ServerBoundCertService* GetServerBoundCertService() const OVERRIDE;
63
64 // SSLSocket implementation.
[email protected]b0ff3f82011-07-23 05:12:3965 virtual int ExportKeyingMaterial(const base::StringPiece& label,
[email protected]1bc6f5e2012-03-15 00:20:5866 bool has_context,
[email protected]b0ff3f82011-07-23 05:12:3967 const base::StringPiece& context,
[email protected]1bc6f5e2012-03-15 00:20:5868 unsigned char* out,
[email protected]a5bd18f2012-05-11 15:51:4869 unsigned int outlen) OVERRIDE;
[email protected]81ec7c12012-07-31 18:32:1970 virtual int GetTLSUniqueChannelBinding(std::string* out) OVERRIDE;
[email protected]d518cd92010-09-29 12:27:4471
[email protected]dbf036f2011-12-06 23:33:2472 // StreamSocket implementation.
[email protected]a5bd18f2012-05-11 15:51:4873 virtual int Connect(const CompletionCallback& callback) OVERRIDE;
74 virtual void Disconnect() OVERRIDE;
75 virtual bool IsConnected() const OVERRIDE;
76 virtual bool IsConnectedAndIdle() const OVERRIDE;
[email protected]a3528692012-06-08 00:11:4277 virtual int GetPeerAddress(IPEndPoint* address) const OVERRIDE;
[email protected]a5bd18f2012-05-11 15:51:4878 virtual int GetLocalAddress(IPEndPoint* address) const OVERRIDE;
79 virtual const BoundNetLog& NetLog() const OVERRIDE;
80 virtual void SetSubresourceSpeculation() OVERRIDE;
81 virtual void SetOmniboxSpeculation() OVERRIDE;
82 virtual bool WasEverUsed() const OVERRIDE;
83 virtual bool UsingTCPFastOpen() const OVERRIDE;
84 virtual int64 NumBytesRead() const OVERRIDE;
85 virtual base::TimeDelta GetConnectTimeMicros() const OVERRIDE;
[email protected]2d88e7d2012-07-19 17:55:1786 virtual bool GetSSLInfo(SSLInfo* ssl_info) OVERRIDE;
[email protected]d518cd92010-09-29 12:27:4487
[email protected]dbf036f2011-12-06 23:33:2488 // Socket implementation.
[email protected]3f55aa12011-12-07 02:03:3389 virtual int Read(IOBuffer* buf, int buf_len,
[email protected]a5bd18f2012-05-11 15:51:4890 const CompletionCallback& callback) OVERRIDE;
[email protected]83039bb2011-12-09 18:43:5591 virtual int Write(IOBuffer* buf, int buf_len,
[email protected]a5bd18f2012-05-11 15:51:4892 const CompletionCallback& callback) OVERRIDE;
93 virtual bool SetReceiveBufferSize(int32 size) OVERRIDE;
94 virtual bool SetSendBufferSize(int32 size) OVERRIDE;
[email protected]d518cd92010-09-29 12:27:4495
96 private:
[email protected]d518cd92010-09-29 12:27:4497 bool Init();
98 void DoReadCallback(int result);
99 void DoWriteCallback(int result);
100
101 bool DoTransportIO();
102 int DoHandshake();
[email protected]170e76c2010-10-04 15:04:20103 int DoVerifyCert(int result);
104 int DoVerifyCertComplete(int result);
[email protected]d518cd92010-09-29 12:27:44105 void DoConnectCallback(int result);
[email protected]170e76c2010-10-04 15:04:20106 X509Certificate* UpdateServerCert();
[email protected]d518cd92010-09-29 12:27:44107
108 void OnHandshakeIOComplete(int result);
109 void OnSendComplete(int result);
110 void OnRecvComplete(int result);
111
112 int DoHandshakeLoop(int last_io_result);
113 int DoReadLoop(int result);
114 int DoWriteLoop(int result);
115 int DoPayloadRead();
116 int DoPayloadWrite();
117
118 int BufferSend();
119 int BufferRecv();
120 void BufferSendComplete(int result);
121 void BufferRecvComplete(int result);
122 void TransportWriteComplete(int result);
123 void TransportReadComplete(int result);
124
[email protected]d518cd92010-09-29 12:27:44125 bool transport_send_busy_;
126 scoped_refptr<DrainableIOBuffer> send_buffer_;
127 bool transport_recv_busy_;
[email protected]a85197e2012-05-22 19:07:28128 bool transport_recv_eof_;
[email protected]d518cd92010-09-29 12:27:44129 scoped_refptr<IOBuffer> recv_buffer_;
130
[email protected]dbf036f2011-12-06 23:33:24131 CompletionCallback user_connect_callback_;
[email protected]3f55aa12011-12-07 02:03:33132 CompletionCallback user_read_callback_;
[email protected]83039bb2011-12-09 18:43:55133 CompletionCallback user_write_callback_;
[email protected]d518cd92010-09-29 12:27:44134
135 // Used by Read function.
136 scoped_refptr<IOBuffer> user_read_buf_;
137 int user_read_buf_len_;
138
139 // Used by Write function.
140 scoped_refptr<IOBuffer> user_write_buf_;
141 int user_write_buf_len_;
142
[email protected]170e76c2010-10-04 15:04:20143 // Set when handshake finishes.
144 scoped_refptr<X509Certificate> server_cert_;
145 CertVerifyResult server_cert_verify_result_;
[email protected]fbef13932010-11-23 12:38:53146 bool completed_handshake_;
[email protected]170e76c2010-10-04 15:04:20147
[email protected]d518cd92010-09-29 12:27:44148 // Stores client authentication information between ClientAuthHandler and
149 // GetSSLCertRequestInfo calls.
150 std::vector<scoped_refptr<X509Certificate> > client_certs_;
151 bool client_auth_cert_needed_;
152
[email protected]822581d2010-12-16 17:27:15153 CertVerifier* const cert_verifier_;
154 scoped_ptr<SingleRequestCertVerifier> verifier_;
[email protected]170e76c2010-10-04 15:04:20155
[email protected]d518cd92010-09-29 12:27:44156 // OpenSSL stuff
[email protected]d518cd92010-09-29 12:27:44157 SSL* ssl_;
158 BIO* transport_bio_;
159
160 scoped_ptr<ClientSocketHandle> transport_;
[email protected]055d7f22010-11-15 12:03:12161 const HostPortPair host_and_port_;
[email protected]d518cd92010-09-29 12:27:44162 SSLConfig ssl_config_;
[email protected]c3456bb2011-12-12 22:22:19163 // ssl_session_cache_shard_ is an opaque string that partitions the SSL
164 // session cache. i.e. sessions created with one value will not attempt to
165 // resume on the socket with a different value.
166 const std::string ssl_session_cache_shard_;
[email protected]d518cd92010-09-29 12:27:44167
[email protected]fbef13932010-11-23 12:38:53168 // Used for session cache diagnostics.
169 bool trying_cached_session_;
[email protected]d518cd92010-09-29 12:27:44170
171 enum State {
172 STATE_NONE,
173 STATE_HANDSHAKE,
174 STATE_VERIFY_CERT,
175 STATE_VERIFY_CERT_COMPLETE,
176 };
177 State next_handshake_state_;
[email protected]ea4a1c6a2010-12-09 13:33:28178 NextProtoStatus npn_status_;
179 std::string npn_proto_;
[email protected]55e973d2011-12-05 23:03:24180 std::string server_protos_;
[email protected]d518cd92010-09-29 12:27:44181 BoundNetLog net_log_;
182};
183
184} // namespace net
185
[email protected]7e5dd49f2010-12-08 18:33:49186#endif // NET_SOCKET_SSL_CLIENT_SOCKET_OPENSSL_H_