[email protected] | 61f3ddf | 2012-02-08 02:45:39 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 2 | // 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] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 7 | |
[email protected] | ea4a1c6a | 2010-12-09 13:33:28 | [diff] [blame] | 8 | #include <string> |
| 9 | |
[email protected] | a5bd18f | 2012-05-11 15:51:48 | [diff] [blame] | 10 | #include "base/compiler_specific.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 11 | #include "base/memory/scoped_ptr.h" |
[email protected] | 170e76c | 2010-10-04 15:04:20 | [diff] [blame] | 12 | #include "net/base/cert_verify_result.h" |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 13 | #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 | |
| 19 | typedef struct bio_st BIO; |
[email protected] | 718c967 | 2010-12-02 10:04:10 | [diff] [blame] | 20 | typedef struct evp_pkey_st EVP_PKEY; |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 21 | typedef struct ssl_st SSL; |
[email protected] | 718c967 | 2010-12-02 10:04:10 | [diff] [blame] | 22 | typedef struct x509_st X509; |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 23 | |
| 24 | namespace net { |
| 25 | |
[email protected] | 170e76c | 2010-10-04 15:04:20 | [diff] [blame] | 26 | class CertVerifier; |
[email protected] | 822581d | 2010-12-16 17:27:15 | [diff] [blame] | 27 | class SingleRequestCertVerifier; |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 28 | class SSLCertRequestInfo; |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 29 | class SSLInfo; |
| 30 | |
| 31 | // An SSL client socket implemented with OpenSSL. |
| 32 | class 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] | 055d7f2 | 2010-11-15 12:03:12 | [diff] [blame] | 39 | const HostPortPair& host_and_port, |
[email protected] | 822581d | 2010-12-16 17:27:15 | [diff] [blame] | 40 | const SSLConfig& ssl_config, |
[email protected] | feb79bcd | 2011-07-21 16:55:17 | [diff] [blame] | 41 | const SSLClientSocketContext& context); |
[email protected] | a5bd18f | 2012-05-11 15:51:48 | [diff] [blame] | 42 | virtual ~SSLClientSocketOpenSSL(); |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 43 | |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 44 | const HostPortPair& host_and_port() const { return host_and_port_; } |
[email protected] | c3456bb | 2011-12-12 22:22:19 | [diff] [blame] | 45 | const std::string& ssl_session_cache_shard() const { |
| 46 | return ssl_session_cache_shard_; |
| 47 | } |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 48 | |
[email protected] | 718c967 | 2010-12-02 10:04:10 | [diff] [blame] | 49 | // 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] | ea4a1c6a | 2010-12-09 13:33:28 | [diff] [blame] | 53 | // 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] | dbf036f | 2011-12-06 23:33:24 | [diff] [blame] | 57 | // SSLClientSocket implementation. |
[email protected] | a5bd18f | 2012-05-11 15:51:48 | [diff] [blame] | 58 | virtual void GetSSLCertRequestInfo( |
| 59 | SSLCertRequestInfo* cert_request_info) OVERRIDE; |
[email protected] | 81ec7c1 | 2012-07-31 18:32:19 | [diff] [blame^] | 60 | virtual NextProtoStatus GetNextProto(std::string* proto, |
| 61 | std::string* server_protos) OVERRIDE; |
| 62 | virtual ServerBoundCertService* GetServerBoundCertService() const OVERRIDE; |
| 63 | |
| 64 | // SSLSocket implementation. |
[email protected] | b0ff3f8 | 2011-07-23 05:12:39 | [diff] [blame] | 65 | virtual int ExportKeyingMaterial(const base::StringPiece& label, |
[email protected] | 1bc6f5e | 2012-03-15 00:20:58 | [diff] [blame] | 66 | bool has_context, |
[email protected] | b0ff3f8 | 2011-07-23 05:12:39 | [diff] [blame] | 67 | const base::StringPiece& context, |
[email protected] | 1bc6f5e | 2012-03-15 00:20:58 | [diff] [blame] | 68 | unsigned char* out, |
[email protected] | a5bd18f | 2012-05-11 15:51:48 | [diff] [blame] | 69 | unsigned int outlen) OVERRIDE; |
[email protected] | 81ec7c1 | 2012-07-31 18:32:19 | [diff] [blame^] | 70 | virtual int GetTLSUniqueChannelBinding(std::string* out) OVERRIDE; |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 71 | |
[email protected] | dbf036f | 2011-12-06 23:33:24 | [diff] [blame] | 72 | // StreamSocket implementation. |
[email protected] | a5bd18f | 2012-05-11 15:51:48 | [diff] [blame] | 73 | 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] | a352869 | 2012-06-08 00:11:42 | [diff] [blame] | 77 | virtual int GetPeerAddress(IPEndPoint* address) const OVERRIDE; |
[email protected] | a5bd18f | 2012-05-11 15:51:48 | [diff] [blame] | 78 | 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] | 2d88e7d | 2012-07-19 17:55:17 | [diff] [blame] | 86 | virtual bool GetSSLInfo(SSLInfo* ssl_info) OVERRIDE; |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 87 | |
[email protected] | dbf036f | 2011-12-06 23:33:24 | [diff] [blame] | 88 | // Socket implementation. |
[email protected] | 3f55aa1 | 2011-12-07 02:03:33 | [diff] [blame] | 89 | virtual int Read(IOBuffer* buf, int buf_len, |
[email protected] | a5bd18f | 2012-05-11 15:51:48 | [diff] [blame] | 90 | const CompletionCallback& callback) OVERRIDE; |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 91 | virtual int Write(IOBuffer* buf, int buf_len, |
[email protected] | a5bd18f | 2012-05-11 15:51:48 | [diff] [blame] | 92 | const CompletionCallback& callback) OVERRIDE; |
| 93 | virtual bool SetReceiveBufferSize(int32 size) OVERRIDE; |
| 94 | virtual bool SetSendBufferSize(int32 size) OVERRIDE; |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 95 | |
| 96 | private: |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 97 | bool Init(); |
| 98 | void DoReadCallback(int result); |
| 99 | void DoWriteCallback(int result); |
| 100 | |
| 101 | bool DoTransportIO(); |
| 102 | int DoHandshake(); |
[email protected] | 170e76c | 2010-10-04 15:04:20 | [diff] [blame] | 103 | int DoVerifyCert(int result); |
| 104 | int DoVerifyCertComplete(int result); |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 105 | void DoConnectCallback(int result); |
[email protected] | 170e76c | 2010-10-04 15:04:20 | [diff] [blame] | 106 | X509Certificate* UpdateServerCert(); |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 107 | |
| 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] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 125 | bool transport_send_busy_; |
| 126 | scoped_refptr<DrainableIOBuffer> send_buffer_; |
| 127 | bool transport_recv_busy_; |
[email protected] | a85197e | 2012-05-22 19:07:28 | [diff] [blame] | 128 | bool transport_recv_eof_; |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 129 | scoped_refptr<IOBuffer> recv_buffer_; |
| 130 | |
[email protected] | dbf036f | 2011-12-06 23:33:24 | [diff] [blame] | 131 | CompletionCallback user_connect_callback_; |
[email protected] | 3f55aa1 | 2011-12-07 02:03:33 | [diff] [blame] | 132 | CompletionCallback user_read_callback_; |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 133 | CompletionCallback user_write_callback_; |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 134 | |
| 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] | 170e76c | 2010-10-04 15:04:20 | [diff] [blame] | 143 | // Set when handshake finishes. |
| 144 | scoped_refptr<X509Certificate> server_cert_; |
| 145 | CertVerifyResult server_cert_verify_result_; |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 146 | bool completed_handshake_; |
[email protected] | 170e76c | 2010-10-04 15:04:20 | [diff] [blame] | 147 | |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 148 | // 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] | 822581d | 2010-12-16 17:27:15 | [diff] [blame] | 153 | CertVerifier* const cert_verifier_; |
| 154 | scoped_ptr<SingleRequestCertVerifier> verifier_; |
[email protected] | 170e76c | 2010-10-04 15:04:20 | [diff] [blame] | 155 | |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 156 | // OpenSSL stuff |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 157 | SSL* ssl_; |
| 158 | BIO* transport_bio_; |
| 159 | |
| 160 | scoped_ptr<ClientSocketHandle> transport_; |
[email protected] | 055d7f2 | 2010-11-15 12:03:12 | [diff] [blame] | 161 | const HostPortPair host_and_port_; |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 162 | SSLConfig ssl_config_; |
[email protected] | c3456bb | 2011-12-12 22:22:19 | [diff] [blame] | 163 | // 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] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 167 | |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 168 | // Used for session cache diagnostics. |
| 169 | bool trying_cached_session_; |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 170 | |
| 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] | ea4a1c6a | 2010-12-09 13:33:28 | [diff] [blame] | 178 | NextProtoStatus npn_status_; |
| 179 | std::string npn_proto_; |
[email protected] | 55e973d | 2011-12-05 23:03:24 | [diff] [blame] | 180 | std::string server_protos_; |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 181 | BoundNetLog net_log_; |
| 182 | }; |
| 183 | |
| 184 | } // namespace net |
| 185 | |
[email protected] | 7e5dd49f | 2010-12-08 18:33:49 | [diff] [blame] | 186 | #endif // NET_SOCKET_SSL_CLIENT_SOCKET_OPENSSL_H_ |