blob: 52f8241b438ef6f516afd485d47d032e65063da7 [file] [log] [blame]
[email protected]c8a80e92014-05-17 16:02:081// 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
tbansalf82cc8e2015-10-14 20:05:498#include <stdint.h>
9
Avi Drissman13fc8932015-12-20 04:40:4610#include "base/macros.h"
[email protected]c8a80e92014-05-17 16:02:0811#include "base/memory/scoped_ptr.h"
12#include "net/base/completion_callback.h"
13#include "net/base/io_buffer.h"
eroman87c53d62015-04-02 06:51:0714#include "net/log/net_log.h"
[email protected]c8a80e92014-05-17 16:02:0815#include "net/socket/ssl_server_socket.h"
svaldez6e7e82a22015-10-28 19:39:5316#include "net/ssl/ssl_server_config.h"
[email protected]c8a80e92014-05-17 16:02:0817
18// Avoid including misc OpenSSL headers, i.e.:
19// <openssl/bio.h>
20typedef struct bio_st BIO;
21// <openssl/ssl.h>
22typedef struct ssl_st SSL;
ryanchung987b2ff2016-02-19 00:17:1223typedef struct x509_store_ctx_st X509_STORE_CTX;
[email protected]c8a80e92014-05-17 16:02:0824
25namespace net {
26
27class SSLInfo;
28
29class 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,
maniscalcod54fe492016-01-05 00:36:4235 const crypto::RSAPrivateKey& key,
ryanchung987b2ff2016-02-19 00:17:1236 const SSLServerConfig& ssl_server_config);
dchengb03027d2014-10-21 12:00:2037 ~SSLServerSocketOpenSSL() override;
[email protected]c8a80e92014-05-17 16:02:0838
39 // SSLServerSocket interface.
dchengb03027d2014-10-21 12:00:2040 int Handshake(const CompletionCallback& callback) override;
[email protected]c8a80e92014-05-17 16:02:0841
42 // SSLSocket interface.
dchengb03027d2014-10-21 12:00:2043 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]c8a80e92014-05-17 16:02:0849
50 // Socket interface (via StreamSocket).
dchengb03027d2014-10-21 12:00:2051 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 Drissman13fc8932015-12-20 04:40:4657 int SetReceiveBufferSize(int32_t size) override;
58 int SetSendBufferSize(int32_t size) override;
[email protected]c8a80e92014-05-17 16:02:0859
60 // StreamSocket implementation.
dchengb03027d2014-10-21 12:00:2061 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;
ttuttle23fdb7b2015-05-15 01:28:0375 void GetConnectionAttempts(ConnectionAttempts* out) const override;
76 void ClearConnectionAttempts() override {}
77 void AddConnectionAttempts(const ConnectionAttempts& attempts) override {}
tbansalf82cc8e2015-10-14 20:05:4978 int64_t GetTotalReceivedBytes() const override;
[email protected]c8a80e92014-05-17 16:02:0879
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();
ryanchung987b2ff2016-02-19 00:17:12109 static int CertVerifyCallback(X509_STORE_CTX* store_ctx, void* arg);
[email protected]c8a80e92014-05-17 16:02:08110
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.
ryanchung987b2ff2016-02-19 00:17:12145 SSLServerConfig ssl_server_config_;
[email protected]c8a80e92014-05-17 16:02:08146
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
ryanchung987b2ff2016-02-19 00:17:12153 // Certificate for the client.
154 scoped_refptr<X509Certificate> client_cert_;
155
[email protected]c8a80e92014-05-17 16:02:08156 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_