[email protected] | c8a80e9 | 2014-05-17 16:02:08 | [diff] [blame] | 1 | // Copyright 2014 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_SERVER_SOCKET_OPENSSL_H_ |
| 6 | #define NET_SOCKET_SSL_SERVER_SOCKET_OPENSSL_H_ |
| 7 | |
tbansal | f82cc8e | 2015-10-14 20:05:49 | [diff] [blame] | 8 | #include <stdint.h> |
| 9 | |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 10 | #include "base/macros.h" |
[email protected] | c8a80e9 | 2014-05-17 16:02:08 | [diff] [blame] | 11 | #include "base/memory/scoped_ptr.h" |
| 12 | #include "net/base/completion_callback.h" |
| 13 | #include "net/base/io_buffer.h" |
eroman | 87c53d6 | 2015-04-02 06:51:07 | [diff] [blame] | 14 | #include "net/log/net_log.h" |
[email protected] | c8a80e9 | 2014-05-17 16:02:08 | [diff] [blame] | 15 | #include "net/socket/ssl_server_socket.h" |
svaldez | 6e7e82a2 | 2015-10-28 19:39:53 | [diff] [blame] | 16 | #include "net/ssl/ssl_server_config.h" |
[email protected] | c8a80e9 | 2014-05-17 16:02:08 | [diff] [blame] | 17 | |
| 18 | // Avoid including misc OpenSSL headers, i.e.: |
| 19 | // <openssl/bio.h> |
| 20 | typedef struct bio_st BIO; |
| 21 | // <openssl/ssl.h> |
| 22 | typedef struct ssl_st SSL; |
ryanchung | 987b2ff | 2016-02-19 00:17:12 | [diff] [blame^] | 23 | typedef struct x509_store_ctx_st X509_STORE_CTX; |
[email protected] | c8a80e9 | 2014-05-17 16:02:08 | [diff] [blame] | 24 | |
| 25 | namespace net { |
| 26 | |
| 27 | class SSLInfo; |
| 28 | |
| 29 | class SSLServerSocketOpenSSL : public SSLServerSocket { |
| 30 | public: |
| 31 | // See comments on CreateSSLServerSocket for details of how these |
| 32 | // parameters are used. |
| 33 | SSLServerSocketOpenSSL(scoped_ptr<StreamSocket> socket, |
| 34 | scoped_refptr<X509Certificate> certificate, |
maniscalco | d54fe49 | 2016-01-05 00:36:42 | [diff] [blame] | 35 | const crypto::RSAPrivateKey& key, |
ryanchung | 987b2ff | 2016-02-19 00:17:12 | [diff] [blame^] | 36 | const SSLServerConfig& ssl_server_config); |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 37 | ~SSLServerSocketOpenSSL() override; |
[email protected] | c8a80e9 | 2014-05-17 16:02:08 | [diff] [blame] | 38 | |
| 39 | // SSLServerSocket interface. |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 40 | int Handshake(const CompletionCallback& callback) override; |
[email protected] | c8a80e9 | 2014-05-17 16:02:08 | [diff] [blame] | 41 | |
| 42 | // SSLSocket interface. |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 43 | int ExportKeyingMaterial(const base::StringPiece& label, |
| 44 | bool has_context, |
| 45 | const base::StringPiece& context, |
| 46 | unsigned char* out, |
| 47 | unsigned int outlen) override; |
| 48 | int GetTLSUniqueChannelBinding(std::string* out) override; |
[email protected] | c8a80e9 | 2014-05-17 16:02:08 | [diff] [blame] | 49 | |
| 50 | // Socket interface (via StreamSocket). |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 51 | int Read(IOBuffer* buf, |
| 52 | int buf_len, |
| 53 | const CompletionCallback& callback) override; |
| 54 | int Write(IOBuffer* buf, |
| 55 | int buf_len, |
| 56 | const CompletionCallback& callback) override; |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 57 | int SetReceiveBufferSize(int32_t size) override; |
| 58 | int SetSendBufferSize(int32_t size) override; |
[email protected] | c8a80e9 | 2014-05-17 16:02:08 | [diff] [blame] | 59 | |
| 60 | // StreamSocket implementation. |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 61 | int Connect(const CompletionCallback& callback) override; |
| 62 | void Disconnect() override; |
| 63 | bool IsConnected() const override; |
| 64 | bool IsConnectedAndIdle() const override; |
| 65 | int GetPeerAddress(IPEndPoint* address) const override; |
| 66 | int GetLocalAddress(IPEndPoint* address) const override; |
| 67 | const BoundNetLog& NetLog() const override; |
| 68 | void SetSubresourceSpeculation() override; |
| 69 | void SetOmniboxSpeculation() override; |
| 70 | bool WasEverUsed() const override; |
| 71 | bool UsingTCPFastOpen() const override; |
| 72 | bool WasNpnNegotiated() const override; |
| 73 | NextProto GetNegotiatedProtocol() const override; |
| 74 | bool GetSSLInfo(SSLInfo* ssl_info) override; |
ttuttle | 23fdb7b | 2015-05-15 01:28:03 | [diff] [blame] | 75 | void GetConnectionAttempts(ConnectionAttempts* out) const override; |
| 76 | void ClearConnectionAttempts() override {} |
| 77 | void AddConnectionAttempts(const ConnectionAttempts& attempts) override {} |
tbansal | f82cc8e | 2015-10-14 20:05:49 | [diff] [blame] | 78 | int64_t GetTotalReceivedBytes() const override; |
[email protected] | c8a80e9 | 2014-05-17 16:02:08 | [diff] [blame] | 79 | |
| 80 | private: |
| 81 | enum State { |
| 82 | STATE_NONE, |
| 83 | STATE_HANDSHAKE, |
| 84 | }; |
| 85 | |
| 86 | void OnSendComplete(int result); |
| 87 | void OnRecvComplete(int result); |
| 88 | void OnHandshakeIOComplete(int result); |
| 89 | |
| 90 | int BufferSend(); |
| 91 | void BufferSendComplete(int result); |
| 92 | void TransportWriteComplete(int result); |
| 93 | int BufferRecv(); |
| 94 | void BufferRecvComplete(int result); |
| 95 | int TransportReadComplete(int result); |
| 96 | bool DoTransportIO(); |
| 97 | int DoPayloadRead(); |
| 98 | int DoPayloadWrite(); |
| 99 | |
| 100 | int DoHandshakeLoop(int last_io_result); |
| 101 | int DoReadLoop(int result); |
| 102 | int DoWriteLoop(int result); |
| 103 | int DoHandshake(); |
| 104 | void DoHandshakeCallback(int result); |
| 105 | void DoReadCallback(int result); |
| 106 | void DoWriteCallback(int result); |
| 107 | |
| 108 | int Init(); |
ryanchung | 987b2ff | 2016-02-19 00:17:12 | [diff] [blame^] | 109 | static int CertVerifyCallback(X509_STORE_CTX* store_ctx, void* arg); |
[email protected] | c8a80e9 | 2014-05-17 16:02:08 | [diff] [blame] | 110 | |
| 111 | // Members used to send and receive buffer. |
| 112 | bool transport_send_busy_; |
| 113 | bool transport_recv_busy_; |
| 114 | bool transport_recv_eof_; |
| 115 | |
| 116 | scoped_refptr<DrainableIOBuffer> send_buffer_; |
| 117 | scoped_refptr<IOBuffer> recv_buffer_; |
| 118 | |
| 119 | BoundNetLog net_log_; |
| 120 | |
| 121 | CompletionCallback user_handshake_callback_; |
| 122 | CompletionCallback user_read_callback_; |
| 123 | CompletionCallback user_write_callback_; |
| 124 | |
| 125 | // Used by Read function. |
| 126 | scoped_refptr<IOBuffer> user_read_buf_; |
| 127 | int user_read_buf_len_; |
| 128 | |
| 129 | // Used by Write function. |
| 130 | scoped_refptr<IOBuffer> user_write_buf_; |
| 131 | int user_write_buf_len_; |
| 132 | |
| 133 | // Used by TransportWriteComplete() and TransportReadComplete() to signify an |
| 134 | // error writing to the transport socket. A value of OK indicates no error. |
| 135 | int transport_write_error_; |
| 136 | |
| 137 | // OpenSSL stuff |
| 138 | SSL* ssl_; |
| 139 | BIO* transport_bio_; |
| 140 | |
| 141 | // StreamSocket for sending and receiving data. |
| 142 | scoped_ptr<StreamSocket> transport_socket_; |
| 143 | |
| 144 | // Options for the SSL socket. |
ryanchung | 987b2ff | 2016-02-19 00:17:12 | [diff] [blame^] | 145 | SSLServerConfig ssl_server_config_; |
[email protected] | c8a80e9 | 2014-05-17 16:02:08 | [diff] [blame] | 146 | |
| 147 | // Certificate for the server. |
| 148 | scoped_refptr<X509Certificate> cert_; |
| 149 | |
| 150 | // Private key used by the server. |
| 151 | scoped_ptr<crypto::RSAPrivateKey> key_; |
| 152 | |
ryanchung | 987b2ff | 2016-02-19 00:17:12 | [diff] [blame^] | 153 | // Certificate for the client. |
| 154 | scoped_refptr<X509Certificate> client_cert_; |
| 155 | |
[email protected] | c8a80e9 | 2014-05-17 16:02:08 | [diff] [blame] | 156 | State next_handshake_state_; |
| 157 | bool completed_handshake_; |
| 158 | |
| 159 | DISALLOW_COPY_AND_ASSIGN(SSLServerSocketOpenSSL); |
| 160 | }; |
| 161 | |
| 162 | } // namespace net |
| 163 | |
| 164 | #endif // NET_SOCKET_SSL_SERVER_SOCKET_OPENSSL_H_ |