Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / net / socket / ssl_server_socket_openssl.h
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
8 #include "base/memory/scoped_ptr.h"
9 #include "net/base/completion_callback.h"
10 #include "net/base/io_buffer.h"
11 #include "net/base/net_log.h"
12 #include "net/socket/ssl_server_socket.h"
13 #include "net/ssl/ssl_config_service.h"
14
15 // Avoid including misc OpenSSL headers, i.e.:
16 // <openssl/bio.h>
17 typedef struct bio_st BIO;
18 // <openssl/ssl.h>
19 typedef struct ssl_st SSL;
20
21 namespace net {
22
23 class SSLInfo;
24
25 class SSLServerSocketOpenSSL : public SSLServerSocket {
26  public:
27   // See comments on CreateSSLServerSocket for details of how these
28   // parameters are used.
29   SSLServerSocketOpenSSL(scoped_ptr<StreamSocket> socket,
30                          scoped_refptr<X509Certificate> certificate,
31                          crypto::RSAPrivateKey* key,
32                          const SSLConfig& ssl_config);
33   ~SSLServerSocketOpenSSL() override;
34
35   // SSLServerSocket interface.
36   int Handshake(const CompletionCallback& callback) override;
37
38   // SSLSocket interface.
39   int ExportKeyingMaterial(const base::StringPiece& label,
40                            bool has_context,
41                            const base::StringPiece& context,
42                            unsigned char* out,
43                            unsigned int outlen) override;
44   int GetTLSUniqueChannelBinding(std::string* out) override;
45
46   // Socket interface (via StreamSocket).
47   int Read(IOBuffer* buf,
48            int buf_len,
49            const CompletionCallback& callback) override;
50   int Write(IOBuffer* buf,
51             int buf_len,
52             const CompletionCallback& callback) override;
53   int SetReceiveBufferSize(int32 size) override;
54   int SetSendBufferSize(int32 size) override;
55
56   // StreamSocket implementation.
57   int Connect(const CompletionCallback& callback) override;
58   void Disconnect() override;
59   bool IsConnected() const override;
60   bool IsConnectedAndIdle() const override;
61   int GetPeerAddress(IPEndPoint* address) const override;
62   int GetLocalAddress(IPEndPoint* address) const override;
63   const BoundNetLog& NetLog() const override;
64   void SetSubresourceSpeculation() override;
65   void SetOmniboxSpeculation() override;
66   bool WasEverUsed() const override;
67   bool UsingTCPFastOpen() const override;
68   bool WasNpnNegotiated() const override;
69   NextProto GetNegotiatedProtocol() const override;
70   bool GetSSLInfo(SSLInfo* ssl_info) override;
71
72  private:
73   enum State {
74     STATE_NONE,
75     STATE_HANDSHAKE,
76   };
77
78   void OnSendComplete(int result);
79   void OnRecvComplete(int result);
80   void OnHandshakeIOComplete(int result);
81
82   int BufferSend();
83   void BufferSendComplete(int result);
84   void TransportWriteComplete(int result);
85   int BufferRecv();
86   void BufferRecvComplete(int result);
87   int TransportReadComplete(int result);
88   bool DoTransportIO();
89   int DoPayloadRead();
90   int DoPayloadWrite();
91
92   int DoHandshakeLoop(int last_io_result);
93   int DoReadLoop(int result);
94   int DoWriteLoop(int result);
95   int DoHandshake();
96   void DoHandshakeCallback(int result);
97   void DoReadCallback(int result);
98   void DoWriteCallback(int result);
99
100   int Init();
101
102   // Members used to send and receive buffer.
103   bool transport_send_busy_;
104   bool transport_recv_busy_;
105   bool transport_recv_eof_;
106
107   scoped_refptr<DrainableIOBuffer> send_buffer_;
108   scoped_refptr<IOBuffer> recv_buffer_;
109
110   BoundNetLog net_log_;
111
112   CompletionCallback user_handshake_callback_;
113   CompletionCallback user_read_callback_;
114   CompletionCallback user_write_callback_;
115
116   // Used by Read function.
117   scoped_refptr<IOBuffer> user_read_buf_;
118   int user_read_buf_len_;
119
120   // Used by Write function.
121   scoped_refptr<IOBuffer> user_write_buf_;
122   int user_write_buf_len_;
123
124   // Used by TransportWriteComplete() and TransportReadComplete() to signify an
125   // error writing to the transport socket. A value of OK indicates no error.
126   int transport_write_error_;
127
128   // OpenSSL stuff
129   SSL* ssl_;
130   BIO* transport_bio_;
131
132   // StreamSocket for sending and receiving data.
133   scoped_ptr<StreamSocket> transport_socket_;
134
135   // Options for the SSL socket.
136   SSLConfig ssl_config_;
137
138   // Certificate for the server.
139   scoped_refptr<X509Certificate> cert_;
140
141   // Private key used by the server.
142   scoped_ptr<crypto::RSAPrivateKey> key_;
143
144   State next_handshake_state_;
145   bool completed_handshake_;
146
147   DISALLOW_COPY_AND_ASSIGN(SSLServerSocketOpenSSL);
148 };
149
150 }  // namespace net
151
152 #endif  // NET_SOCKET_SSL_SERVER_SOCKET_OPENSSL_H_