OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #ifndef REMOTING_JINGLE_GLUE_SSL_SOCKET_ADAPTER_H_ | |
6 #define REMOTING_JINGLE_GLUE_SSL_SOCKET_ADAPTER_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_errors.h" | |
12 #include "net/base/net_log.h" | |
13 #include "net/socket/ssl_client_socket.h" | |
14 #include "net/socket/stream_socket.h" | |
15 #include "third_party/libjingle/source/talk/base/asyncsocket.h" | |
16 #include "third_party/libjingle/source/talk/base/ssladapter.h" | |
17 | |
18 namespace net { | |
19 class CertVerifier; | |
20 class TransportSecurityState; | |
21 } // namespace net | |
22 | |
23 namespace remoting { | |
24 | |
25 class SSLSocketAdapter; | |
26 | |
27 // TODO(sergeyu): Write unittests for this code! | |
28 | |
29 // This class provides a wrapper to libjingle's talk_base::AsyncSocket that | |
30 // implements Chromium's net::StreamSocket interface. It's used by | |
31 // SSLSocketAdapter to enable Chromium's SSL implementation to work over | |
32 // libjingle's socket class. | |
33 class TransportSocket : public net::StreamSocket, public sigslot::has_slots<> { | |
34 public: | |
35 TransportSocket(talk_base::AsyncSocket* socket, | |
36 SSLSocketAdapter *ssl_adapter); | |
37 virtual ~TransportSocket(); | |
38 | |
39 void set_addr(const talk_base::SocketAddress& addr) { | |
40 addr_ = addr; | |
41 } | |
42 | |
43 // net::StreamSocket implementation. | |
44 virtual int Connect(const net::CompletionCallback& callback) OVERRIDE; | |
45 virtual void Disconnect() OVERRIDE; | |
46 virtual bool IsConnected() const OVERRIDE; | |
47 virtual bool IsConnectedAndIdle() const OVERRIDE; | |
48 virtual int GetPeerAddress(net::IPEndPoint* address) const OVERRIDE; | |
49 virtual int GetLocalAddress(net::IPEndPoint* address) const OVERRIDE; | |
50 virtual const net::BoundNetLog& NetLog() const OVERRIDE; | |
51 virtual void SetSubresourceSpeculation() OVERRIDE; | |
52 virtual void SetOmniboxSpeculation() OVERRIDE; | |
53 virtual bool WasEverUsed() const OVERRIDE; | |
54 virtual bool UsingTCPFastOpen() const OVERRIDE; | |
55 virtual int64 NumBytesRead() const OVERRIDE; | |
56 virtual base::TimeDelta GetConnectTimeMicros() const OVERRIDE; | |
57 virtual bool WasNpnNegotiated() const OVERRIDE; | |
58 virtual net::NextProto GetNegotiatedProtocol() const OVERRIDE; | |
59 virtual bool GetSSLInfo(net::SSLInfo* ssl_info) OVERRIDE; | |
60 | |
61 // net::Socket implementation. | |
62 virtual int Read(net::IOBuffer* buf, int buf_len, | |
63 const net::CompletionCallback& callback) OVERRIDE; | |
64 virtual int Write(net::IOBuffer* buf, int buf_len, | |
65 const net::CompletionCallback& callback) OVERRIDE; | |
66 virtual bool SetReceiveBufferSize(int32 size) OVERRIDE; | |
67 virtual bool SetSendBufferSize(int32 size) OVERRIDE; | |
68 | |
69 private: | |
70 friend class SSLSocketAdapter; | |
71 | |
72 void OnReadEvent(talk_base::AsyncSocket* socket); | |
73 void OnWriteEvent(talk_base::AsyncSocket* socket); | |
74 | |
75 // Holds the user's completion callback when Write and Read are called. | |
76 net::CompletionCallback read_callback_; | |
77 net::CompletionCallback write_callback_; | |
78 | |
79 scoped_refptr<net::IOBuffer> read_buffer_; | |
80 int read_buffer_len_; | |
81 scoped_refptr<net::IOBuffer> write_buffer_; | |
82 int write_buffer_len_; | |
83 | |
84 net::BoundNetLog net_log_; | |
85 | |
86 talk_base::AsyncSocket *socket_; | |
87 talk_base::SocketAddress addr_; | |
88 | |
89 bool was_used_to_convey_data_; | |
90 | |
91 DISALLOW_COPY_AND_ASSIGN(TransportSocket); | |
92 }; | |
93 | |
94 // This provides a talk_base::AsyncSocketAdapter interface around Chromium's | |
95 // net::SSLClientSocket class. This allows remoting to use Chromium's SSL | |
96 // implementation instead of OpenSSL. | |
97 class SSLSocketAdapter : public talk_base::SSLAdapter { | |
98 public: | |
99 explicit SSLSocketAdapter(talk_base::AsyncSocket* socket); | |
100 virtual ~SSLSocketAdapter(); | |
101 | |
102 // StartSSL returns 0 if successful, or non-zero on failure. | |
103 // If StartSSL is called while the socket is closed or connecting, the SSL | |
104 // negotiation will begin as soon as the socket connects. | |
105 // | |
106 // restartable is not implemented, and must be set to false. | |
107 virtual int StartSSL(const char* hostname, bool restartable) OVERRIDE; | |
108 | |
109 // Create the default SSL adapter for this platform. | |
110 static SSLSocketAdapter* Create(AsyncSocket* socket); | |
111 | |
112 virtual int Send(const void* pv, size_t cb) OVERRIDE; | |
113 virtual int Recv(void* pv, size_t cb) OVERRIDE; | |
114 | |
115 private: | |
116 friend class TransportSocket; | |
117 | |
118 enum SSLState { | |
119 SSLSTATE_NONE, | |
120 SSLSTATE_WAIT, | |
121 SSLSTATE_CONNECTED, | |
122 SSLSTATE_ERROR, | |
123 }; | |
124 | |
125 void OnConnected(int result); | |
126 void OnRead(int result); | |
127 void OnWritten(int result); | |
128 | |
129 void DoWrite(); | |
130 | |
131 virtual void OnConnectEvent(talk_base::AsyncSocket* socket) OVERRIDE; | |
132 | |
133 int BeginSSL(); | |
134 | |
135 bool ignore_bad_cert_; | |
136 std::string hostname_; | |
137 TransportSocket* transport_socket_; | |
138 | |
139 // |cert_verifier_| must be defined before |ssl_socket_|, so that | |
140 // it's destroyed after |ssl_socket_|. | |
141 scoped_ptr<net::CertVerifier> cert_verifier_; | |
142 scoped_ptr<net::TransportSecurityState> transport_security_state_; | |
143 scoped_ptr<net::SSLClientSocket> ssl_socket_; | |
144 | |
145 SSLState ssl_state_; | |
146 | |
147 bool read_pending_; | |
148 scoped_refptr<net::GrowableIOBuffer> read_buffer_; | |
149 | |
150 bool write_pending_; | |
151 scoped_refptr<net::DrainableIOBuffer> write_buffer_; | |
152 | |
153 DISALLOW_COPY_AND_ASSIGN(SSLSocketAdapter); | |
154 }; | |
155 | |
156 } // namespace remoting | |
157 | |
158 #endif // REMOTING_JINGLE_GLUE_SSL_SOCKET_ADAPTER_H_ | |
OLD | NEW |