1 // Copyright (c) 2012 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.
5 #ifndef NET_SOCKET_SSL_SERVER_SOCKET_NSS_H_
6 #define NET_SOCKET_SSL_SERVER_SOCKET_NSS_H_
14 #include "base/memory/scoped_ptr.h"
15 #include "net/base/completion_callback.h"
16 #include "net/base/host_port_pair.h"
17 #include "net/base/net_log.h"
18 #include "net/base/nss_memio.h"
19 #include "net/base/ssl_config_service.h"
20 #include "net/socket/ssl_server_socket.h"
24 class SSLServerSocketNSS
: public SSLServerSocket
{
26 // See comments on CreateSSLServerSocket for details of how these
27 // parameters are used.
28 SSLServerSocketNSS(StreamSocket
* socket
,
29 scoped_refptr
<X509Certificate
> certificate
,
30 crypto::RSAPrivateKey
* key
,
31 const SSLConfig
& ssl_config
);
32 virtual ~SSLServerSocketNSS();
34 // SSLServerSocket interface.
35 virtual int Handshake(const CompletionCallback
& callback
) OVERRIDE
;
36 virtual int ExportKeyingMaterial(const base::StringPiece
& label
,
38 const base::StringPiece
& context
,
40 unsigned int outlen
) OVERRIDE
;
42 // Socket interface (via StreamSocket).
43 virtual int Read(IOBuffer
* buf
, int buf_len
,
44 const CompletionCallback
& callback
) OVERRIDE
;
45 virtual int Write(IOBuffer
* buf
, int buf_len
,
46 const CompletionCallback
& callback
) OVERRIDE
;
47 virtual bool SetReceiveBufferSize(int32 size
) OVERRIDE
;
48 virtual bool SetSendBufferSize(int32 size
) OVERRIDE
;
50 // StreamSocket implementation.
51 virtual int Connect(const CompletionCallback
& callback
) OVERRIDE
;
52 virtual void Disconnect() OVERRIDE
;
53 virtual bool IsConnected() const OVERRIDE
;
54 virtual bool IsConnectedAndIdle() const OVERRIDE
;
55 virtual int GetPeerAddress(IPEndPoint
* address
) const OVERRIDE
;
56 virtual int GetLocalAddress(IPEndPoint
* address
) const OVERRIDE
;
57 virtual const BoundNetLog
& NetLog() const OVERRIDE
;
58 virtual void SetSubresourceSpeculation() OVERRIDE
;
59 virtual void SetOmniboxSpeculation() OVERRIDE
;
60 virtual bool WasEverUsed() const OVERRIDE
;
61 virtual bool UsingTCPFastOpen() const OVERRIDE
;
62 virtual int64
NumBytesRead() const OVERRIDE
;
63 virtual base::TimeDelta
GetConnectTimeMicros() const OVERRIDE
;
64 virtual NextProto
GetNegotiatedProtocol() const OVERRIDE
;
72 int InitializeSSLOptions();
74 void OnSendComplete(int result
);
75 void OnRecvComplete(int result
);
76 void OnHandshakeIOComplete(int result
);
79 void BufferSendComplete(int result
);
81 void BufferRecvComplete(int result
);
86 int DoHandshakeLoop(int last_io_result
);
87 int DoReadLoop(int result
);
88 int DoWriteLoop(int result
);
90 void DoHandshakeCallback(int result
);
91 void DoReadCallback(int result
);
92 void DoWriteCallback(int result
);
94 static SECStatus
OwnAuthCertHandler(void* arg
,
98 static void HandshakeCallback(PRFileDesc
* socket
, void* arg
);
102 // Members used to send and receive buffer.
103 bool transport_send_busy_
;
104 bool transport_recv_busy_
;
106 scoped_refptr
<IOBuffer
> recv_buffer_
;
108 BoundNetLog net_log_
;
110 CompletionCallback user_handshake_callback_
;
111 CompletionCallback user_read_callback_
;
112 CompletionCallback user_write_callback_
;
114 // Used by Read function.
115 scoped_refptr
<IOBuffer
> user_read_buf_
;
116 int user_read_buf_len_
;
118 // Used by Write function.
119 scoped_refptr
<IOBuffer
> user_write_buf_
;
120 int user_write_buf_len_
;
122 // The NSS SSL state machine
125 // Buffers for the network end of the SSL state machine
126 memio_Private
* nss_bufs_
;
128 // StreamSocket for sending and receiving data.
129 scoped_ptr
<StreamSocket
> transport_socket_
;
131 // Options for the SSL socket.
132 SSLConfig ssl_config_
;
134 // Certificate for the server.
135 scoped_refptr
<X509Certificate
> cert_
;
137 // Private key used by the server.
138 scoped_ptr
<crypto::RSAPrivateKey
> key_
;
140 State next_handshake_state_
;
141 bool completed_handshake_
;
143 DISALLOW_COPY_AND_ASSIGN(SSLServerSocketNSS
);
148 #endif // NET_SOCKET_SSL_SERVER_SOCKET_NSS_H_