[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 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 | |
| 9 | #include "base/scoped_ptr.h" |
[email protected] | 170e76c | 2010-10-04 15:04:20 | [diff] [blame] | 10 | #include "net/base/cert_verify_result.h" |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 11 | #include "net/base/completion_callback.h" |
| 12 | #include "net/base/io_buffer.h" |
| 13 | #include "net/base/ssl_config_service.h" |
| 14 | #include "net/socket/ssl_client_socket.h" |
| 15 | #include "net/socket/client_socket_handle.h" |
| 16 | |
| 17 | typedef struct bio_st BIO; |
[email protected] | 718c967 | 2010-12-02 10:04:10 | [diff] [blame] | 18 | typedef struct evp_pkey_st EVP_PKEY; |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 19 | typedef struct ssl_st SSL; |
[email protected] | 718c967 | 2010-12-02 10:04:10 | [diff] [blame] | 20 | typedef struct x509_st X509; |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 21 | |
| 22 | namespace net { |
| 23 | |
[email protected] | 170e76c | 2010-10-04 15:04:20 | [diff] [blame] | 24 | class CertVerifier; |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 25 | class SSLCertRequestInfo; |
| 26 | class SSLConfig; |
| 27 | class SSLInfo; |
| 28 | |
| 29 | // An SSL client socket implemented with OpenSSL. |
| 30 | class SSLClientSocketOpenSSL : public SSLClientSocket { |
| 31 | public: |
| 32 | // Takes ownership of the transport_socket, which may already be connected. |
| 33 | // The given hostname will be compared with the name(s) in the server's |
| 34 | // certificate during the SSL handshake. ssl_config specifies the SSL |
| 35 | // settings. |
| 36 | SSLClientSocketOpenSSL(ClientSocketHandle* transport_socket, |
[email protected] | 055d7f2 | 2010-11-15 12:03:12 | [diff] [blame] | 37 | const HostPortPair& host_and_port, |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 38 | const SSLConfig& ssl_config); |
| 39 | ~SSLClientSocketOpenSSL(); |
| 40 | |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 41 | const HostPortPair& host_and_port() const { return host_and_port_; } |
| 42 | |
[email protected] | 718c967 | 2010-12-02 10:04:10 | [diff] [blame] | 43 | // Callback from the SSL layer that indicates the remote server is requesting |
| 44 | // a certificate for this client. |
| 45 | int ClientCertRequestCallback(SSL* ssl, X509** x509, EVP_PKEY** pkey); |
| 46 | |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 47 | // SSLClientSocket methods: |
| 48 | virtual void GetSSLInfo(SSLInfo* ssl_info); |
| 49 | virtual void GetSSLCertRequestInfo(SSLCertRequestInfo* cert_request_info); |
| 50 | virtual NextProtoStatus GetNextProto(std::string* proto); |
| 51 | |
| 52 | // ClientSocket methods: |
| 53 | virtual int Connect(CompletionCallback* callback); |
| 54 | virtual void Disconnect(); |
| 55 | virtual bool IsConnected() const; |
| 56 | virtual bool IsConnectedAndIdle() const; |
[email protected] | 7e5dd49f | 2010-12-08 18:33:49 | [diff] [blame^] | 57 | virtual int GetPeerAddress(AddressList* address) const; |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 58 | virtual const BoundNetLog& NetLog() const; |
| 59 | virtual void SetSubresourceSpeculation(); |
| 60 | virtual void SetOmniboxSpeculation(); |
| 61 | virtual bool WasEverUsed() const; |
[email protected] | 7f7e9239 | 2010-10-26 18:29:29 | [diff] [blame] | 62 | virtual bool UsingTCPFastOpen() const; |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 63 | |
| 64 | // Socket methods: |
| 65 | virtual int Read(IOBuffer* buf, int buf_len, CompletionCallback* callback); |
| 66 | virtual int Write(IOBuffer* buf, int buf_len, CompletionCallback* callback); |
| 67 | virtual bool SetReceiveBufferSize(int32 size); |
| 68 | virtual bool SetSendBufferSize(int32 size); |
| 69 | |
| 70 | private: |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 71 | bool Init(); |
| 72 | void DoReadCallback(int result); |
| 73 | void DoWriteCallback(int result); |
| 74 | |
| 75 | bool DoTransportIO(); |
| 76 | int DoHandshake(); |
[email protected] | 170e76c | 2010-10-04 15:04:20 | [diff] [blame] | 77 | int DoVerifyCert(int result); |
| 78 | int DoVerifyCertComplete(int result); |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 79 | void DoConnectCallback(int result); |
[email protected] | 170e76c | 2010-10-04 15:04:20 | [diff] [blame] | 80 | X509Certificate* UpdateServerCert(); |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 81 | |
| 82 | void OnHandshakeIOComplete(int result); |
| 83 | void OnSendComplete(int result); |
| 84 | void OnRecvComplete(int result); |
| 85 | |
| 86 | int DoHandshakeLoop(int last_io_result); |
| 87 | int DoReadLoop(int result); |
| 88 | int DoWriteLoop(int result); |
| 89 | int DoPayloadRead(); |
| 90 | int DoPayloadWrite(); |
| 91 | |
| 92 | int BufferSend(); |
| 93 | int BufferRecv(); |
| 94 | void BufferSendComplete(int result); |
| 95 | void BufferRecvComplete(int result); |
| 96 | void TransportWriteComplete(int result); |
| 97 | void TransportReadComplete(int result); |
| 98 | |
| 99 | CompletionCallbackImpl<SSLClientSocketOpenSSL> buffer_send_callback_; |
| 100 | CompletionCallbackImpl<SSLClientSocketOpenSSL> buffer_recv_callback_; |
| 101 | bool transport_send_busy_; |
| 102 | scoped_refptr<DrainableIOBuffer> send_buffer_; |
| 103 | bool transport_recv_busy_; |
| 104 | scoped_refptr<IOBuffer> recv_buffer_; |
| 105 | |
| 106 | CompletionCallback* user_connect_callback_; |
| 107 | CompletionCallback* user_read_callback_; |
| 108 | CompletionCallback* user_write_callback_; |
| 109 | |
| 110 | // Used by Read function. |
| 111 | scoped_refptr<IOBuffer> user_read_buf_; |
| 112 | int user_read_buf_len_; |
| 113 | |
| 114 | // Used by Write function. |
| 115 | scoped_refptr<IOBuffer> user_write_buf_; |
| 116 | int user_write_buf_len_; |
| 117 | |
[email protected] | 170e76c | 2010-10-04 15:04:20 | [diff] [blame] | 118 | // Set when handshake finishes. |
| 119 | scoped_refptr<X509Certificate> server_cert_; |
| 120 | CertVerifyResult server_cert_verify_result_; |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 121 | bool completed_handshake_; |
[email protected] | 170e76c | 2010-10-04 15:04:20 | [diff] [blame] | 122 | |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 123 | // Stores client authentication information between ClientAuthHandler and |
| 124 | // GetSSLCertRequestInfo calls. |
| 125 | std::vector<scoped_refptr<X509Certificate> > client_certs_; |
| 126 | bool client_auth_cert_needed_; |
| 127 | |
[email protected] | 170e76c | 2010-10-04 15:04:20 | [diff] [blame] | 128 | scoped_ptr<CertVerifier> verifier_; |
| 129 | CompletionCallbackImpl<SSLClientSocketOpenSSL> handshake_io_callback_; |
| 130 | |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 131 | // OpenSSL stuff |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 132 | SSL* ssl_; |
| 133 | BIO* transport_bio_; |
| 134 | |
| 135 | scoped_ptr<ClientSocketHandle> transport_; |
[email protected] | 055d7f2 | 2010-11-15 12:03:12 | [diff] [blame] | 136 | const HostPortPair host_and_port_; |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 137 | SSLConfig ssl_config_; |
| 138 | |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 139 | // Used for session cache diagnostics. |
| 140 | bool trying_cached_session_; |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 141 | |
| 142 | enum State { |
| 143 | STATE_NONE, |
| 144 | STATE_HANDSHAKE, |
| 145 | STATE_VERIFY_CERT, |
| 146 | STATE_VERIFY_CERT_COMPLETE, |
| 147 | }; |
| 148 | State next_handshake_state_; |
| 149 | BoundNetLog net_log_; |
| 150 | }; |
| 151 | |
| 152 | } // namespace net |
| 153 | |
[email protected] | 7e5dd49f | 2010-12-08 18:33:49 | [diff] [blame^] | 154 | #endif // NET_SOCKET_SSL_CLIENT_SOCKET_OPENSSL_H_ |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 155 | |