2014-05-13 18:00:26 +00:00
|
|
|
/*
|
|
|
|
|
* Copyright 2004 The WebRTC Project Authors. All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
|
|
|
|
* in the file PATENTS. All contributing project authors may
|
|
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#if defined(_MSC_VER) && _MSC_VER < 1300
|
2018-06-19 15:03:05 +02:00
|
|
|
#pragma warning(disable : 4786)
|
2014-05-13 18:00:26 +00:00
|
|
|
#endif
|
|
|
|
|
|
2022-03-17 15:47:49 +01:00
|
|
|
#include "rtc_base/socket_adapters.h"
|
|
|
|
|
|
2015-02-12 11:54:26 +00:00
|
|
|
#include <algorithm>
|
|
|
|
|
|
2018-11-02 10:54:56 +01:00
|
|
|
#include "absl/strings/match.h"
|
2022-03-17 15:47:49 +01:00
|
|
|
#include "absl/strings/string_view.h"
|
2018-10-23 12:03:01 +02:00
|
|
|
#include "rtc_base/buffer.h"
|
2019-01-11 09:11:00 -08:00
|
|
|
#include "rtc_base/byte_buffer.h"
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "rtc_base/checks.h"
|
|
|
|
|
#include "rtc_base/logging.h"
|
2018-09-06 13:41:30 +02:00
|
|
|
#include "rtc_base/strings/string_builder.h"
|
2018-03-07 20:02:26 +01:00
|
|
|
#include "rtc_base/zero_memory.h"
|
2014-05-13 18:00:26 +00:00
|
|
|
|
|
|
|
|
namespace rtc {
|
|
|
|
|
|
2021-08-12 10:32:30 +02:00
|
|
|
BufferedReadAdapter::BufferedReadAdapter(Socket* socket, size_t size)
|
2018-06-19 15:03:05 +02:00
|
|
|
: AsyncSocketAdapter(socket),
|
|
|
|
|
buffer_size_(size),
|
|
|
|
|
data_len_(0),
|
|
|
|
|
buffering_(false) {
|
2014-05-13 18:00:26 +00:00
|
|
|
buffer_ = new char[buffer_size_];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BufferedReadAdapter::~BufferedReadAdapter() {
|
2018-06-19 15:03:05 +02:00
|
|
|
delete[] buffer_;
|
2014-05-13 18:00:26 +00:00
|
|
|
}
|
|
|
|
|
|
2018-06-19 15:03:05 +02:00
|
|
|
int BufferedReadAdapter::Send(const void* pv, size_t cb) {
|
2014-05-13 18:00:26 +00:00
|
|
|
if (buffering_) {
|
|
|
|
|
// TODO: Spoof error better; Signal Writeable
|
2021-08-11 11:22:44 +02:00
|
|
|
SetError(EWOULDBLOCK);
|
2014-05-13 18:00:26 +00:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
return AsyncSocketAdapter::Send(pv, cb);
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-23 18:19:26 +02:00
|
|
|
int BufferedReadAdapter::Recv(void* pv, size_t cb, int64_t* timestamp) {
|
2014-05-13 18:00:26 +00:00
|
|
|
if (buffering_) {
|
2021-08-11 11:22:44 +02:00
|
|
|
SetError(EWOULDBLOCK);
|
2014-05-13 18:00:26 +00:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t read = 0;
|
|
|
|
|
|
|
|
|
|
if (data_len_) {
|
2015-02-12 11:54:26 +00:00
|
|
|
read = std::min(cb, data_len_);
|
2014-05-13 18:00:26 +00:00
|
|
|
memcpy(pv, buffer_, read);
|
|
|
|
|
data_len_ -= read;
|
|
|
|
|
if (data_len_ > 0) {
|
|
|
|
|
memmove(buffer_, buffer_ + read, data_len_);
|
|
|
|
|
}
|
2018-06-19 15:03:05 +02:00
|
|
|
pv = static_cast<char*>(pv) + read;
|
2014-05-13 18:00:26 +00:00
|
|
|
cb -= read;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// FIX: If cb == 0, we won't generate another read event
|
|
|
|
|
|
2016-05-23 18:19:26 +02:00
|
|
|
int res = AsyncSocketAdapter::Recv(pv, cb, timestamp);
|
2015-07-16 10:22:21 -07:00
|
|
|
if (res >= 0) {
|
|
|
|
|
// Read from socket and possibly buffer; return combined length
|
|
|
|
|
return res + static_cast<int>(read);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (read > 0) {
|
|
|
|
|
// Failed to read from socket, but still read something from buffer
|
|
|
|
|
return static_cast<int>(read);
|
|
|
|
|
}
|
2014-05-13 18:00:26 +00:00
|
|
|
|
2015-07-16 10:22:21 -07:00
|
|
|
// Didn't read anything; return error from socket
|
|
|
|
|
return res;
|
2014-05-13 18:00:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BufferedReadAdapter::BufferInput(bool on) {
|
|
|
|
|
buffering_ = on;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-12 10:32:30 +02:00
|
|
|
void BufferedReadAdapter::OnReadEvent(Socket* socket) {
|
2021-08-11 11:22:44 +02:00
|
|
|
RTC_DCHECK(socket == GetSocket());
|
2014-05-13 18:00:26 +00:00
|
|
|
|
|
|
|
|
if (!buffering_) {
|
|
|
|
|
AsyncSocketAdapter::OnReadEvent(socket);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (data_len_ >= buffer_size_) {
|
2018-02-13 10:37:07 +01:00
|
|
|
RTC_LOG(LS_ERROR) << "Input buffer overflow";
|
2021-11-15 16:57:07 +01:00
|
|
|
RTC_DCHECK_NOTREACHED();
|
2014-05-13 18:00:26 +00:00
|
|
|
data_len_ = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-11 11:22:44 +02:00
|
|
|
int len = AsyncSocketAdapter::Recv(buffer_ + data_len_,
|
|
|
|
|
buffer_size_ - data_len_, nullptr);
|
2014-05-13 18:00:26 +00:00
|
|
|
if (len < 0) {
|
|
|
|
|
// TODO: Do something better like forwarding the error to the user.
|
2021-11-04 12:01:23 +00:00
|
|
|
RTC_LOG_ERR(LS_INFO) << "Recv";
|
2014-05-13 18:00:26 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
data_len_ += len;
|
|
|
|
|
|
|
|
|
|
ProcessInput(buffer_, &data_len_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
// This is a SSL v2 CLIENT_HELLO message.
|
|
|
|
|
// TODO: Should this have a session id? The response doesn't have a
|
|
|
|
|
// certificate, so the hello should have a session id.
|
Use suffixed {uint,int}{8,16,32,64}_t types.
Removes the use of uint8, etc. in favor of uint8_t.
BUG=webrtc:5024
R=henrik.lundin@webrtc.org, henrikg@webrtc.org, perkj@webrtc.org, solenberg@webrtc.org, stefan@webrtc.org, tina.legrand@webrtc.org
Review URL: https://codereview.webrtc.org/1362503003 .
Cr-Commit-Position: refs/heads/master@{#10196}
2015-10-07 12:23:21 +02:00
|
|
|
static const uint8_t kSslClientHello[] = {
|
|
|
|
|
0x80, 0x46, // msg len
|
|
|
|
|
0x01, // CLIENT_HELLO
|
|
|
|
|
0x03, 0x01, // SSL 3.1
|
|
|
|
|
0x00, 0x2d, // ciphersuite len
|
|
|
|
|
0x00, 0x00, // session id len
|
|
|
|
|
0x00, 0x10, // challenge len
|
|
|
|
|
0x01, 0x00, 0x80, 0x03, 0x00, 0x80, 0x07, 0x00, 0xc0, // ciphersuites
|
|
|
|
|
0x06, 0x00, 0x40, 0x02, 0x00, 0x80, 0x04, 0x00, 0x80, //
|
|
|
|
|
0x00, 0x00, 0x04, 0x00, 0xfe, 0xff, 0x00, 0x00, 0x0a, //
|
|
|
|
|
0x00, 0xfe, 0xfe, 0x00, 0x00, 0x09, 0x00, 0x00, 0x64, //
|
|
|
|
|
0x00, 0x00, 0x62, 0x00, 0x00, 0x03, 0x00, 0x00, 0x06, //
|
|
|
|
|
0x1f, 0x17, 0x0c, 0xa6, 0x2f, 0x00, 0x78, 0xfc, // challenge
|
|
|
|
|
0x46, 0x55, 0x2e, 0xb1, 0x83, 0x39, 0xf1, 0xea //
|
2014-05-13 18:00:26 +00:00
|
|
|
};
|
|
|
|
|
|
2018-12-17 14:04:05 +01:00
|
|
|
// static
|
|
|
|
|
ArrayView<const uint8_t> AsyncSSLSocket::SslClientHello() {
|
|
|
|
|
// Implicit conversion directly from kSslClientHello to ArrayView fails when
|
|
|
|
|
// built with gcc.
|
|
|
|
|
return {kSslClientHello, sizeof(kSslClientHello)};
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-13 18:00:26 +00:00
|
|
|
// This is a TLSv1 SERVER_HELLO message.
|
Use suffixed {uint,int}{8,16,32,64}_t types.
Removes the use of uint8, etc. in favor of uint8_t.
BUG=webrtc:5024
R=henrik.lundin@webrtc.org, henrikg@webrtc.org, perkj@webrtc.org, solenberg@webrtc.org, stefan@webrtc.org, tina.legrand@webrtc.org
Review URL: https://codereview.webrtc.org/1362503003 .
Cr-Commit-Position: refs/heads/master@{#10196}
2015-10-07 12:23:21 +02:00
|
|
|
static const uint8_t kSslServerHello[] = {
|
|
|
|
|
0x16, // handshake message
|
|
|
|
|
0x03, 0x01, // SSL 3.1
|
|
|
|
|
0x00, 0x4a, // message len
|
|
|
|
|
0x02, // SERVER_HELLO
|
|
|
|
|
0x00, 0x00, 0x46, // handshake len
|
|
|
|
|
0x03, 0x01, // SSL 3.1
|
|
|
|
|
0x42, 0x85, 0x45, 0xa7, 0x27, 0xa9, 0x5d, 0xa0, // server random
|
|
|
|
|
0xb3, 0xc5, 0xe7, 0x53, 0xda, 0x48, 0x2b, 0x3f, //
|
|
|
|
|
0xc6, 0x5a, 0xca, 0x89, 0xc1, 0x58, 0x52, 0xa1, //
|
|
|
|
|
0x78, 0x3c, 0x5b, 0x17, 0x46, 0x00, 0x85, 0x3f, //
|
|
|
|
|
0x20, // session id len
|
|
|
|
|
0x0e, 0xd3, 0x06, 0x72, 0x5b, 0x5b, 0x1b, 0x5f, // session id
|
|
|
|
|
0x15, 0xac, 0x13, 0xf9, 0x88, 0x53, 0x9d, 0x9b, //
|
|
|
|
|
0xe8, 0x3d, 0x7b, 0x0c, 0x30, 0x32, 0x6e, 0x38, //
|
|
|
|
|
0x4d, 0xa2, 0x75, 0x57, 0x41, 0x6c, 0x34, 0x5c, //
|
|
|
|
|
0x00, 0x04, // RSA/RC4-128/MD5
|
|
|
|
|
0x00 // null compression
|
2014-05-13 18:00:26 +00:00
|
|
|
};
|
|
|
|
|
|
2018-12-17 14:04:05 +01:00
|
|
|
// static
|
|
|
|
|
ArrayView<const uint8_t> AsyncSSLSocket::SslServerHello() {
|
|
|
|
|
return {kSslServerHello, sizeof(kSslServerHello)};
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-12 10:32:30 +02:00
|
|
|
AsyncSSLSocket::AsyncSSLSocket(Socket* socket)
|
2018-06-19 15:03:05 +02:00
|
|
|
: BufferedReadAdapter(socket, 1024) {}
|
2014-05-13 18:00:26 +00:00
|
|
|
|
|
|
|
|
int AsyncSSLSocket::Connect(const SocketAddress& addr) {
|
|
|
|
|
// Begin buffering before we connect, so that there isn't a race condition
|
|
|
|
|
// between potential senders and receiving the OnConnectEvent signal
|
|
|
|
|
BufferInput(true);
|
|
|
|
|
return BufferedReadAdapter::Connect(addr);
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-12 10:32:30 +02:00
|
|
|
void AsyncSSLSocket::OnConnectEvent(Socket* socket) {
|
2021-08-11 11:22:44 +02:00
|
|
|
RTC_DCHECK(socket == GetSocket());
|
2014-05-13 18:00:26 +00:00
|
|
|
// TODO: we could buffer output too...
|
2017-02-07 07:18:43 -08:00
|
|
|
const int res = DirectSend(kSslClientHello, sizeof(kSslClientHello));
|
2021-08-20 11:04:04 +02:00
|
|
|
if (res != sizeof(kSslClientHello)) {
|
2021-08-23 11:17:56 +02:00
|
|
|
RTC_LOG(LS_ERROR) << "Sending fake SSL ClientHello message failed.";
|
2021-08-20 11:04:04 +02:00
|
|
|
Close();
|
|
|
|
|
SignalCloseEvent(this, 0);
|
|
|
|
|
}
|
2014-05-13 18:00:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AsyncSSLSocket::ProcessInput(char* data, size_t* len) {
|
|
|
|
|
if (*len < sizeof(kSslServerHello))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (memcmp(kSslServerHello, data, sizeof(kSslServerHello)) != 0) {
|
2021-08-23 11:17:56 +02:00
|
|
|
RTC_LOG(LS_ERROR) << "Received non-matching fake SSL ServerHello message.";
|
2014-05-13 18:00:26 +00:00
|
|
|
Close();
|
|
|
|
|
SignalCloseEvent(this, 0); // TODO: error code?
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
*len -= sizeof(kSslServerHello);
|
|
|
|
|
if (*len > 0) {
|
|
|
|
|
memmove(data, data + sizeof(kSslServerHello), *len);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool remainder = (*len > 0);
|
|
|
|
|
BufferInput(false);
|
|
|
|
|
SignalConnectEvent(this);
|
|
|
|
|
|
|
|
|
|
// FIX: if SignalConnect causes the socket to be destroyed, we are in trouble
|
|
|
|
|
if (remainder)
|
|
|
|
|
SignalReadEvent(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace rtc
|