[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_ |
| 7 | #pragma once |
| 8 | |
[email protected] | ea4a1c6a | 2010-12-09 13:33:28 | [diff] [blame] | 9 | #include <string> |
| 10 | |
[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; |
| 29 | class SSLConfig; |
| 30 | class SSLInfo; |
| 31 | |
| 32 | // An SSL client socket implemented with OpenSSL. |
| 33 | class SSLClientSocketOpenSSL : public SSLClientSocket { |
| 34 | public: |
| 35 | // Takes ownership of the transport_socket, which may already be connected. |
| 36 | // The given hostname will be compared with the name(s) in the server's |
| 37 | // certificate during the SSL handshake. ssl_config specifies the SSL |
| 38 | // settings. |
| 39 | SSLClientSocketOpenSSL(ClientSocketHandle* transport_socket, |
[email protected] | 055d7f2 | 2010-11-15 12:03:12 | [diff] [blame] | 40 | const HostPortPair& host_and_port, |
[email protected] | 822581d | 2010-12-16 17:27:15 | [diff] [blame] | 41 | const SSLConfig& ssl_config, |
[email protected] | feb79bcd | 2011-07-21 16:55:17 | [diff] [blame] | 42 | const SSLClientSocketContext& context); |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 43 | ~SSLClientSocketOpenSSL(); |
| 44 | |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 45 | const HostPortPair& host_and_port() const { return host_and_port_; } |
[email protected] | c3456bb | 2011-12-12 22:22:19 | [diff] [blame] | 46 | const std::string& ssl_session_cache_shard() const { |
| 47 | return ssl_session_cache_shard_; |
| 48 | } |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 49 | |
[email protected] | 718c967 | 2010-12-02 10:04:10 | [diff] [blame] | 50 | // Callback from the SSL layer that indicates the remote server is requesting |
| 51 | // a certificate for this client. |
| 52 | int ClientCertRequestCallback(SSL* ssl, X509** x509, EVP_PKEY** pkey); |
| 53 | |
[email protected] | ea4a1c6a | 2010-12-09 13:33:28 | [diff] [blame] | 54 | // Callback from the SSL layer to check which NPN protocol we are supporting |
| 55 | int SelectNextProtoCallback(unsigned char** out, unsigned char* outlen, |
| 56 | const unsigned char* in, unsigned int inlen); |
| 57 | |
[email protected] | dbf036f | 2011-12-06 23:33:24 | [diff] [blame] | 58 | // SSLClientSocket implementation. |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 59 | virtual void GetSSLInfo(SSLInfo* ssl_info); |
| 60 | virtual void GetSSLCertRequestInfo(SSLCertRequestInfo* cert_request_info); |
[email protected] | b0ff3f8 | 2011-07-23 05:12:39 | [diff] [blame] | 61 | virtual int ExportKeyingMaterial(const base::StringPiece& label, |
| 62 | const base::StringPiece& context, |
| 63 | unsigned char *out, |
| 64 | unsigned int outlen); |
[email protected] | 55e973d | 2011-12-05 23:03:24 | [diff] [blame] | 65 | virtual NextProtoStatus GetNextProto(std::string* proto, |
| 66 | std::string* server_protos); |
[email protected] | 61f3ddf | 2012-02-08 02:45:39 | [diff] [blame^] | 67 | virtual OriginBoundCertService* GetOriginBoundCertService() const; |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 68 | |
[email protected] | dbf036f | 2011-12-06 23:33:24 | [diff] [blame] | 69 | // StreamSocket implementation. |
[email protected] | dbf036f | 2011-12-06 23:33:24 | [diff] [blame] | 70 | virtual int Connect(const CompletionCallback& callback); |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 71 | virtual void Disconnect(); |
| 72 | virtual bool IsConnected() const; |
| 73 | virtual bool IsConnectedAndIdle() const; |
[email protected] | 7e5dd49f | 2010-12-08 18:33:49 | [diff] [blame] | 74 | virtual int GetPeerAddress(AddressList* address) const; |
[email protected] | e7f74da | 2011-04-19 23:49:35 | [diff] [blame] | 75 | virtual int GetLocalAddress(IPEndPoint* address) const; |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 76 | virtual const BoundNetLog& NetLog() const; |
| 77 | virtual void SetSubresourceSpeculation(); |
| 78 | virtual void SetOmniboxSpeculation(); |
| 79 | virtual bool WasEverUsed() const; |
[email protected] | 7f7e9239 | 2010-10-26 18:29:29 | [diff] [blame] | 80 | virtual bool UsingTCPFastOpen() const; |
[email protected] | 1e71d307 | 2011-07-05 11:34:47 | [diff] [blame] | 81 | virtual int64 NumBytesRead() const; |
| 82 | virtual base::TimeDelta GetConnectTimeMicros() const; |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 83 | |
[email protected] | dbf036f | 2011-12-06 23:33:24 | [diff] [blame] | 84 | // Socket implementation. |
[email protected] | 3f55aa1 | 2011-12-07 02:03:33 | [diff] [blame] | 85 | virtual int Read(IOBuffer* buf, int buf_len, |
| 86 | const CompletionCallback& callback); |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 87 | virtual int Write(IOBuffer* buf, int buf_len, |
| 88 | const CompletionCallback& callback); |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 89 | virtual bool SetReceiveBufferSize(int32 size); |
| 90 | virtual bool SetSendBufferSize(int32 size); |
| 91 | |
| 92 | private: |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 93 | bool Init(); |
| 94 | void DoReadCallback(int result); |
| 95 | void DoWriteCallback(int result); |
| 96 | |
| 97 | bool DoTransportIO(); |
| 98 | int DoHandshake(); |
[email protected] | 170e76c | 2010-10-04 15:04:20 | [diff] [blame] | 99 | int DoVerifyCert(int result); |
| 100 | int DoVerifyCertComplete(int result); |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 101 | void DoConnectCallback(int result); |
[email protected] | 170e76c | 2010-10-04 15:04:20 | [diff] [blame] | 102 | X509Certificate* UpdateServerCert(); |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 103 | |
| 104 | void OnHandshakeIOComplete(int result); |
| 105 | void OnSendComplete(int result); |
| 106 | void OnRecvComplete(int result); |
| 107 | |
| 108 | int DoHandshakeLoop(int last_io_result); |
| 109 | int DoReadLoop(int result); |
| 110 | int DoWriteLoop(int result); |
| 111 | int DoPayloadRead(); |
| 112 | int DoPayloadWrite(); |
| 113 | |
| 114 | int BufferSend(); |
| 115 | int BufferRecv(); |
| 116 | void BufferSendComplete(int result); |
| 117 | void BufferRecvComplete(int result); |
| 118 | void TransportWriteComplete(int result); |
| 119 | void TransportReadComplete(int result); |
| 120 | |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 121 | bool transport_send_busy_; |
| 122 | scoped_refptr<DrainableIOBuffer> send_buffer_; |
| 123 | bool transport_recv_busy_; |
| 124 | scoped_refptr<IOBuffer> recv_buffer_; |
| 125 | |
[email protected] | dbf036f | 2011-12-06 23:33:24 | [diff] [blame] | 126 | CompletionCallback user_connect_callback_; |
[email protected] | 3f55aa1 | 2011-12-07 02:03:33 | [diff] [blame] | 127 | CompletionCallback user_read_callback_; |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 128 | CompletionCallback user_write_callback_; |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 129 | |
| 130 | // Used by Read function. |
| 131 | scoped_refptr<IOBuffer> user_read_buf_; |
| 132 | int user_read_buf_len_; |
| 133 | |
| 134 | // Used by Write function. |
| 135 | scoped_refptr<IOBuffer> user_write_buf_; |
| 136 | int user_write_buf_len_; |
| 137 | |
[email protected] | 170e76c | 2010-10-04 15:04:20 | [diff] [blame] | 138 | // Set when handshake finishes. |
| 139 | scoped_refptr<X509Certificate> server_cert_; |
| 140 | CertVerifyResult server_cert_verify_result_; |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 141 | bool completed_handshake_; |
[email protected] | 170e76c | 2010-10-04 15:04:20 | [diff] [blame] | 142 | |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 143 | // Stores client authentication information between ClientAuthHandler and |
| 144 | // GetSSLCertRequestInfo calls. |
| 145 | std::vector<scoped_refptr<X509Certificate> > client_certs_; |
| 146 | bool client_auth_cert_needed_; |
| 147 | |
[email protected] | 822581d | 2010-12-16 17:27:15 | [diff] [blame] | 148 | CertVerifier* const cert_verifier_; |
| 149 | scoped_ptr<SingleRequestCertVerifier> verifier_; |
[email protected] | 170e76c | 2010-10-04 15:04:20 | [diff] [blame] | 150 | |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 151 | // OpenSSL stuff |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 152 | SSL* ssl_; |
| 153 | BIO* transport_bio_; |
| 154 | |
| 155 | scoped_ptr<ClientSocketHandle> transport_; |
[email protected] | 055d7f2 | 2010-11-15 12:03:12 | [diff] [blame] | 156 | const HostPortPair host_and_port_; |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 157 | SSLConfig ssl_config_; |
[email protected] | c3456bb | 2011-12-12 22:22:19 | [diff] [blame] | 158 | // ssl_session_cache_shard_ is an opaque string that partitions the SSL |
| 159 | // session cache. i.e. sessions created with one value will not attempt to |
| 160 | // resume on the socket with a different value. |
| 161 | const std::string ssl_session_cache_shard_; |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 162 | |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 163 | // Used for session cache diagnostics. |
| 164 | bool trying_cached_session_; |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 165 | |
| 166 | enum State { |
| 167 | STATE_NONE, |
| 168 | STATE_HANDSHAKE, |
| 169 | STATE_VERIFY_CERT, |
| 170 | STATE_VERIFY_CERT_COMPLETE, |
| 171 | }; |
| 172 | State next_handshake_state_; |
[email protected] | ea4a1c6a | 2010-12-09 13:33:28 | [diff] [blame] | 173 | NextProtoStatus npn_status_; |
| 174 | std::string npn_proto_; |
[email protected] | 55e973d | 2011-12-05 23:03:24 | [diff] [blame] | 175 | std::string server_protos_; |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 176 | BoundNetLog net_log_; |
| 177 | }; |
| 178 | |
| 179 | } // namespace net |
| 180 | |
[email protected] | 7e5dd49f | 2010-12-08 18:33:49 | [diff] [blame] | 181 | #endif // NET_SOCKET_SSL_CLIENT_SOCKET_OPENSSL_H_ |