1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "remoting/jingle_glue/ssl_socket_adapter.h"
#include "base/base64.h"
#include "base/compiler_specific.h"
#include "base/message_loop.h"
#include "jingle/glue/utils.h"
#include "net/base/address_list.h"
#include "net/base/cert_verifier.h"
#include "net/base/host_port_pair.h"
#include "net/base/net_errors.h"
#include "net/base/ssl_config_service.h"
#include "net/base/sys_addrinfo.h"
#include "net/socket/client_socket_factory.h"
#include "net/url_request/url_request_context.h"
namespace remoting {
SSLSocketAdapter* SSLSocketAdapter::Create(AsyncSocket* socket) {
return new SSLSocketAdapter(socket);
}
SSLSocketAdapter::SSLSocketAdapter(AsyncSocket* socket)
: SSLAdapter(socket),
ignore_bad_cert_(false),
cert_verifier_(new net::CertVerifier()),
ALLOW_THIS_IN_INITIALIZER_LIST(
connected_callback_(this, &SSLSocketAdapter::OnConnected)),
ALLOW_THIS_IN_INITIALIZER_LIST(
read_callback_(this, &SSLSocketAdapter::OnRead)),
ALLOW_THIS_IN_INITIALIZER_LIST(
write_callback_(this, &SSLSocketAdapter::OnWrite)),
ssl_state_(SSLSTATE_NONE),
read_state_(IOSTATE_NONE),
write_state_(IOSTATE_NONE) {
transport_socket_ = new TransportSocket(socket, this);
}
SSLSocketAdapter::~SSLSocketAdapter() {
}
int SSLSocketAdapter::StartSSL(const char* hostname, bool restartable) {
DCHECK(!restartable);
hostname_ = hostname;
if (socket_->GetState() != Socket::CS_CONNECTED) {
ssl_state_ = SSLSTATE_WAIT;
return 0;
} else {
return BeginSSL();
}
}
int SSLSocketAdapter::BeginSSL() {
if (!MessageLoop::current()) {
// Certificate verification is done via the Chrome message loop.
// Without this check, if we don't have a chrome message loop the
// SSL connection just hangs silently.
LOG(DFATAL) << "Chrome message loop (needed by SSL certificate "
<< "verification) does not exist";
return net::ERR_UNEXPECTED;
}
// SSLConfigService is not thread-safe, and the default values for SSLConfig
// are correct for us, so we don't use the config service to initialize this
// object.
net::SSLConfig ssl_config;
net::SSLClientSocketContext context;
context.cert_verifier = cert_verifier_.get();
transport_socket_->set_addr(talk_base::SocketAddress(hostname_, 0));
ssl_socket_.reset(
net::ClientSocketFactory::GetDefaultFactory()->CreateSSLClientSocket(
transport_socket_, net::HostPortPair(hostname_, 443), ssl_config,
NULL /* ssl_host_info */, context));
int result = ssl_socket_->Connect(&connected_callback_);
if (result == net::ERR_IO_PENDING || result == net::OK) {
return 0;
} else {
LOG(ERROR) << "Could not start SSL: " << net::ErrorToString(result);
return result;
}
}
int SSLSocketAdapter::Send(const void* buf, size_t len) {
if (ssl_state_ != SSLSTATE_CONNECTED) {
return AsyncSocketAdapter::Send(buf, len);
} else {
scoped_refptr<net::IOBuffer> transport_buf(new net::IOBuffer(len));
memcpy(transport_buf->data(), buf, len);
int result = ssl_socket_->Write(transport_buf, len, NULL);
if (result == net::ERR_IO_PENDING) {
SetError(EWOULDBLOCK);
}
transport_buf = NULL;
return result;
}
}
int SSLSocketAdapter::Recv(void* buf, size_t len) {
switch (ssl_state_) {
case SSLSTATE_NONE:
return AsyncSocketAdapter::Recv(buf, len);
case SSLSTATE_WAIT:
SetError(EWOULDBLOCK);
return -1;
case SSLSTATE_CONNECTED:
switch (read_state_) {
case IOSTATE_NONE: {
transport_buf_ = new net::IOBuffer(len);
int result = ssl_socket_->Read(transport_buf_, len, &read_callback_);
if (result >= 0) {
memcpy(buf, transport_buf_->data(), len);
}
if (result == net::ERR_IO_PENDING) {
read_state_ = IOSTATE_PENDING;
SetError(EWOULDBLOCK);
} else {
if (result < 0) {
SetError(result);
VLOG(1) << "Socket error " << result;
}
transport_buf_ = NULL;
}
return result;
}
case IOSTATE_PENDING:
SetError(EWOULDBLOCK);
return -1;
case IOSTATE_COMPLETE:
memcpy(buf, transport_buf_->data(), len);
transport_buf_ = NULL;
read_state_ = IOSTATE_NONE;
return data_transferred_;
}
}
NOTREACHED();
return -1;
}
void SSLSocketAdapter::OnConnected(int result) {
if (result == net::OK) {
ssl_state_ = SSLSTATE_CONNECTED;
OnConnectEvent(this);
} else {
LOG(WARNING) << "OnConnected failed with error " << result;
}
}
void SSLSocketAdapter::OnRead(int result) {
DCHECK(read_state_ == IOSTATE_PENDING);
read_state_ = IOSTATE_COMPLETE;
data_transferred_ = result;
AsyncSocketAdapter::OnReadEvent(this);
}
void SSLSocketAdapter::OnWrite(int result) {
DCHECK(write_state_ == IOSTATE_PENDING);
write_state_ = IOSTATE_COMPLETE;
data_transferred_ = result;
AsyncSocketAdapter::OnWriteEvent(this);
}
void SSLSocketAdapter::OnConnectEvent(talk_base::AsyncSocket* socket) {
if (ssl_state_ != SSLSTATE_WAIT) {
AsyncSocketAdapter::OnConnectEvent(socket);
} else {
ssl_state_ = SSLSTATE_NONE;
int result = BeginSSL();
if (0 != result) {
// TODO(zork): Handle this case gracefully.
LOG(WARNING) << "BeginSSL() failed with " << result;
}
}
}
TransportSocket::TransportSocket(talk_base::AsyncSocket* socket,
SSLSocketAdapter *ssl_adapter)
: read_callback_(NULL),
write_callback_(NULL),
read_buffer_len_(0),
write_buffer_len_(0),
socket_(socket),
was_used_to_convey_data_(false) {
socket_->SignalReadEvent.connect(this, &TransportSocket::OnReadEvent);
socket_->SignalWriteEvent.connect(this, &TransportSocket::OnWriteEvent);
}
TransportSocket::~TransportSocket() {
}
int TransportSocket::Connect(net::CompletionCallback* callback) {
// Connect is never called by SSLClientSocket, instead SSLSocketAdapter
// calls Connect() on socket_ directly.
NOTREACHED();
return false;
}
void TransportSocket::Disconnect() {
socket_->Close();
}
bool TransportSocket::IsConnected() const {
return (socket_->GetState() == talk_base::Socket::CS_CONNECTED);
}
bool TransportSocket::IsConnectedAndIdle() const {
// Not implemented.
NOTREACHED();
return false;
}
int TransportSocket::GetPeerAddress(net::AddressList* address) const {
talk_base::SocketAddress socket_address = socket_->GetRemoteAddress();
// libjingle supports only IPv4 addresses.
sockaddr_in ipv4addr;
socket_address.ToSockAddr(&ipv4addr);
struct addrinfo ai;
memset(&ai, 0, sizeof(ai));
ai.ai_family = ipv4addr.sin_family;
ai.ai_socktype = SOCK_STREAM;
ai.ai_protocol = IPPROTO_TCP;
ai.ai_addr = reinterpret_cast<struct sockaddr*>(&ipv4addr);
ai.ai_addrlen = sizeof(ipv4addr);
*address = net::AddressList::CreateByCopyingFirstAddress(&ai);
return net::OK;
}
int TransportSocket::GetLocalAddress(net::IPEndPoint* address) const {
talk_base::SocketAddress socket_address = socket_->GetLocalAddress();
if (jingle_glue::SocketAddressToIPEndPoint(socket_address, address)) {
return net::OK;
} else {
return net::ERR_FAILED;
}
}
const net::BoundNetLog& TransportSocket::NetLog() const {
return net_log_;
}
void TransportSocket::SetSubresourceSpeculation() {
NOTREACHED();
}
void TransportSocket::SetOmniboxSpeculation() {
NOTREACHED();
}
bool TransportSocket::WasEverUsed() const {
// We don't use this in ClientSocketPools, so this should never be used.
NOTREACHED();
return was_used_to_convey_data_;
}
bool TransportSocket::UsingTCPFastOpen() const {
return false;
}
int64 TransportSocket::NumBytesRead() const {
NOTREACHED();
return -1;
}
base::TimeDelta TransportSocket::GetConnectTimeMicros() const {
NOTREACHED();
return base::TimeDelta::FromMicroseconds(-1);
}
int TransportSocket::Read(net::IOBuffer* buf, int buf_len,
net::CompletionCallback* callback) {
DCHECK(buf);
DCHECK(!read_callback_);
DCHECK(!read_buffer_.get());
int result = socket_->Recv(buf->data(), buf_len);
if (result < 0) {
result = net::MapSystemError(socket_->GetError());
if (result == net::ERR_IO_PENDING) {
read_callback_ = callback;
read_buffer_ = buf;
read_buffer_len_ = buf_len;
}
}
if (result != net::ERR_IO_PENDING)
was_used_to_convey_data_ = true;
return result;
}
int TransportSocket::Write(net::IOBuffer* buf, int buf_len,
net::CompletionCallback* callback) {
DCHECK(buf);
DCHECK(!write_callback_);
DCHECK(!write_buffer_.get());
int result = socket_->Send(buf->data(), buf_len);
if (result < 0) {
result = net::MapSystemError(socket_->GetError());
if (result == net::ERR_IO_PENDING) {
write_callback_ = callback;
write_buffer_ = buf;
write_buffer_len_ = buf_len;
}
}
if (result != net::ERR_IO_PENDING)
was_used_to_convey_data_ = true;
return result;
}
bool TransportSocket::SetReceiveBufferSize(int32 size) {
// Not implemented.
return false;
}
bool TransportSocket::SetSendBufferSize(int32 size) {
// Not implemented.
return false;
}
void TransportSocket::OnReadEvent(talk_base::AsyncSocket* socket) {
if (read_callback_) {
DCHECK(read_buffer_.get());
net::CompletionCallback* callback = read_callback_;
scoped_refptr<net::IOBuffer> buffer = read_buffer_;
int buffer_len = read_buffer_len_;
read_callback_ = NULL;
read_buffer_ = NULL;
read_buffer_len_ = 0;
int result = socket_->Recv(buffer->data(), buffer_len);
if (result < 0) {
result = net::MapSystemError(socket_->GetError());
if (result == net::ERR_IO_PENDING) {
read_callback_ = callback;
read_buffer_ = buffer;
read_buffer_len_ = buffer_len;
return;
}
}
was_used_to_convey_data_ = true;
callback->RunWithParams(Tuple1<int>(result));
}
}
void TransportSocket::OnWriteEvent(talk_base::AsyncSocket* socket) {
if (write_callback_) {
DCHECK(write_buffer_.get());
net::CompletionCallback* callback = write_callback_;
scoped_refptr<net::IOBuffer> buffer = write_buffer_;
int buffer_len = write_buffer_len_;
write_callback_ = NULL;
write_buffer_ = NULL;
write_buffer_len_ = 0;
int result = socket_->Send(buffer->data(), buffer_len);
if (result < 0) {
result = net::MapSystemError(socket_->GetError());
if (result == net::ERR_IO_PENDING) {
write_callback_ = callback;
write_buffer_ = buffer;
write_buffer_len_ = buffer_len;
return;
}
}
was_used_to_convey_data_ = true;
callback->RunWithParams(Tuple1<int>(result));
}
}
} // namespace remoting
|