henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #if defined(_MSC_VER) && _MSC_VER < 1300 |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 12 | #pragma warning(disable : 4786) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 13 | #endif |
| 14 | |
Ali Tofigh | 7fa9057 | 2022-03-17 14:47:49 | [diff] [blame] | 15 | #include "rtc_base/socket_adapters.h" |
| 16 | |
andresp@webrtc.org | ff689be | 2015-02-12 11:54:26 | [diff] [blame] | 17 | #include <algorithm> |
| 18 | |
Niels Möller | aa3c1cc | 2018-11-02 09:54:56 | [diff] [blame] | 19 | #include "absl/strings/match.h" |
Ali Tofigh | 7fa9057 | 2022-03-17 14:47:49 | [diff] [blame] | 20 | #include "absl/strings/string_view.h" |
Yves Gerey | 988cc08 | 2018-10-23 10:03:01 | [diff] [blame] | 21 | #include "rtc_base/buffer.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 22 | #include "rtc_base/byte_buffer.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 23 | #include "rtc_base/checks.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 24 | #include "rtc_base/logging.h" |
Jonas Olsson | 366a50c | 2018-09-06 11:41:30 | [diff] [blame] | 25 | #include "rtc_base/strings/string_builder.h" |
Joachim Bauch | 5b32f23 | 2018-03-07 19:02:26 | [diff] [blame] | 26 | #include "rtc_base/zero_memory.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 27 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 28 | namespace rtc { |
| 29 | |
Niels Möller | d0b8879 | 2021-08-12 08:32:30 | [diff] [blame] | 30 | BufferedReadAdapter::BufferedReadAdapter(Socket* socket, size_t size) |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 31 | : AsyncSocketAdapter(socket), |
| 32 | buffer_size_(size), |
| 33 | data_len_(0), |
| 34 | buffering_(false) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 35 | buffer_ = new char[buffer_size_]; |
| 36 | } |
| 37 | |
| 38 | BufferedReadAdapter::~BufferedReadAdapter() { |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 39 | delete[] buffer_; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 40 | } |
| 41 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 42 | int BufferedReadAdapter::Send(const void* pv, size_t cb) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 43 | if (buffering_) { |
| 44 | // TODO: Spoof error better; Signal Writeable |
Niels Möller | 8729d78 | 2021-08-11 09:22:44 | [diff] [blame] | 45 | SetError(EWOULDBLOCK); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 46 | return -1; |
| 47 | } |
| 48 | return AsyncSocketAdapter::Send(pv, cb); |
| 49 | } |
| 50 | |
Stefan Holmer | 9131efd | 2016-05-23 16:19:26 | [diff] [blame] | 51 | int BufferedReadAdapter::Recv(void* pv, size_t cb, int64_t* timestamp) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 52 | if (buffering_) { |
Niels Möller | 8729d78 | 2021-08-11 09:22:44 | [diff] [blame] | 53 | SetError(EWOULDBLOCK); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 54 | return -1; |
| 55 | } |
| 56 | |
| 57 | size_t read = 0; |
| 58 | |
| 59 | if (data_len_) { |
andresp@webrtc.org | ff689be | 2015-02-12 11:54:26 | [diff] [blame] | 60 | read = std::min(cb, data_len_); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 61 | memcpy(pv, buffer_, read); |
| 62 | data_len_ -= read; |
| 63 | if (data_len_ > 0) { |
| 64 | memmove(buffer_, buffer_ + read, data_len_); |
| 65 | } |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 66 | pv = static_cast<char*>(pv) + read; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 67 | cb -= read; |
| 68 | } |
| 69 | |
| 70 | // FIX: If cb == 0, we won't generate another read event |
| 71 | |
Stefan Holmer | 9131efd | 2016-05-23 16:19:26 | [diff] [blame] | 72 | int res = AsyncSocketAdapter::Recv(pv, cb, timestamp); |
deadbeef | c5d0d95 | 2015-07-16 17:22:21 | [diff] [blame] | 73 | if (res >= 0) { |
| 74 | // Read from socket and possibly buffer; return combined length |
| 75 | return res + static_cast<int>(read); |
| 76 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 77 | |
deadbeef | c5d0d95 | 2015-07-16 17:22:21 | [diff] [blame] | 78 | if (read > 0) { |
| 79 | // Failed to read from socket, but still read something from buffer |
| 80 | return static_cast<int>(read); |
| 81 | } |
| 82 | |
| 83 | // Didn't read anything; return error from socket |
| 84 | return res; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | void BufferedReadAdapter::BufferInput(bool on) { |
| 88 | buffering_ = on; |
| 89 | } |
| 90 | |
Niels Möller | d0b8879 | 2021-08-12 08:32:30 | [diff] [blame] | 91 | void BufferedReadAdapter::OnReadEvent(Socket* socket) { |
Niels Möller | 8729d78 | 2021-08-11 09:22:44 | [diff] [blame] | 92 | RTC_DCHECK(socket == GetSocket()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 93 | |
| 94 | if (!buffering_) { |
| 95 | AsyncSocketAdapter::OnReadEvent(socket); |
| 96 | return; |
| 97 | } |
| 98 | |
| 99 | if (data_len_ >= buffer_size_) { |
Jonas Olsson | 45cc890 | 2018-02-13 09:37:07 | [diff] [blame] | 100 | RTC_LOG(LS_ERROR) << "Input buffer overflow"; |
Artem Titov | d325196 | 2021-11-15 15:57:07 | [diff] [blame] | 101 | RTC_DCHECK_NOTREACHED(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 102 | data_len_ = 0; |
| 103 | } |
| 104 | |
Niels Möller | 8729d78 | 2021-08-11 09:22:44 | [diff] [blame] | 105 | int len = AsyncSocketAdapter::Recv(buffer_ + data_len_, |
| 106 | buffer_size_ - data_len_, nullptr); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 107 | if (len < 0) { |
| 108 | // TODO: Do something better like forwarding the error to the user. |
Harald Alvestrand | 97597c0 | 2021-11-04 12:01:23 | [diff] [blame] | 109 | RTC_LOG_ERR(LS_INFO) << "Recv"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 110 | return; |
| 111 | } |
| 112 | |
| 113 | data_len_ += len; |
| 114 | |
| 115 | ProcessInput(buffer_, &data_len_); |
| 116 | } |
| 117 | |
| 118 | /////////////////////////////////////////////////////////////////////////////// |
| 119 | |
| 120 | // This is a SSL v2 CLIENT_HELLO message. |
| 121 | // TODO: Should this have a session id? The response doesn't have a |
| 122 | // certificate, so the hello should have a session id. |
Peter Boström | 0c4e06b | 2015-10-07 10:23:21 | [diff] [blame] | 123 | static const uint8_t kSslClientHello[] = { |
| 124 | 0x80, 0x46, // msg len |
| 125 | 0x01, // CLIENT_HELLO |
| 126 | 0x03, 0x01, // SSL 3.1 |
| 127 | 0x00, 0x2d, // ciphersuite len |
| 128 | 0x00, 0x00, // session id len |
| 129 | 0x00, 0x10, // challenge len |
| 130 | 0x01, 0x00, 0x80, 0x03, 0x00, 0x80, 0x07, 0x00, 0xc0, // ciphersuites |
| 131 | 0x06, 0x00, 0x40, 0x02, 0x00, 0x80, 0x04, 0x00, 0x80, // |
| 132 | 0x00, 0x00, 0x04, 0x00, 0xfe, 0xff, 0x00, 0x00, 0x0a, // |
| 133 | 0x00, 0xfe, 0xfe, 0x00, 0x00, 0x09, 0x00, 0x00, 0x64, // |
| 134 | 0x00, 0x00, 0x62, 0x00, 0x00, 0x03, 0x00, 0x00, 0x06, // |
| 135 | 0x1f, 0x17, 0x0c, 0xa6, 0x2f, 0x00, 0x78, 0xfc, // challenge |
| 136 | 0x46, 0x55, 0x2e, 0xb1, 0x83, 0x39, 0xf1, 0xea // |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 137 | }; |
| 138 | |
Niels Möller | 4415315 | 2018-12-17 13:04:05 | [diff] [blame] | 139 | // static |
| 140 | ArrayView<const uint8_t> AsyncSSLSocket::SslClientHello() { |
| 141 | // Implicit conversion directly from kSslClientHello to ArrayView fails when |
| 142 | // built with gcc. |
| 143 | return {kSslClientHello, sizeof(kSslClientHello)}; |
| 144 | } |
| 145 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 146 | // This is a TLSv1 SERVER_HELLO message. |
Peter Boström | 0c4e06b | 2015-10-07 10:23:21 | [diff] [blame] | 147 | static const uint8_t kSslServerHello[] = { |
| 148 | 0x16, // handshake message |
| 149 | 0x03, 0x01, // SSL 3.1 |
| 150 | 0x00, 0x4a, // message len |
| 151 | 0x02, // SERVER_HELLO |
| 152 | 0x00, 0x00, 0x46, // handshake len |
| 153 | 0x03, 0x01, // SSL 3.1 |
| 154 | 0x42, 0x85, 0x45, 0xa7, 0x27, 0xa9, 0x5d, 0xa0, // server random |
| 155 | 0xb3, 0xc5, 0xe7, 0x53, 0xda, 0x48, 0x2b, 0x3f, // |
| 156 | 0xc6, 0x5a, 0xca, 0x89, 0xc1, 0x58, 0x52, 0xa1, // |
| 157 | 0x78, 0x3c, 0x5b, 0x17, 0x46, 0x00, 0x85, 0x3f, // |
| 158 | 0x20, // session id len |
| 159 | 0x0e, 0xd3, 0x06, 0x72, 0x5b, 0x5b, 0x1b, 0x5f, // session id |
| 160 | 0x15, 0xac, 0x13, 0xf9, 0x88, 0x53, 0x9d, 0x9b, // |
| 161 | 0xe8, 0x3d, 0x7b, 0x0c, 0x30, 0x32, 0x6e, 0x38, // |
| 162 | 0x4d, 0xa2, 0x75, 0x57, 0x41, 0x6c, 0x34, 0x5c, // |
| 163 | 0x00, 0x04, // RSA/RC4-128/MD5 |
| 164 | 0x00 // null compression |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 165 | }; |
| 166 | |
Niels Möller | 4415315 | 2018-12-17 13:04:05 | [diff] [blame] | 167 | // static |
| 168 | ArrayView<const uint8_t> AsyncSSLSocket::SslServerHello() { |
| 169 | return {kSslServerHello, sizeof(kSslServerHello)}; |
| 170 | } |
| 171 | |
Niels Möller | d0b8879 | 2021-08-12 08:32:30 | [diff] [blame] | 172 | AsyncSSLSocket::AsyncSSLSocket(Socket* socket) |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 173 | : BufferedReadAdapter(socket, 1024) {} |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 174 | |
| 175 | int AsyncSSLSocket::Connect(const SocketAddress& addr) { |
| 176 | // Begin buffering before we connect, so that there isn't a race condition |
| 177 | // between potential senders and receiving the OnConnectEvent signal |
| 178 | BufferInput(true); |
| 179 | return BufferedReadAdapter::Connect(addr); |
| 180 | } |
| 181 | |
Niels Möller | d0b8879 | 2021-08-12 08:32:30 | [diff] [blame] | 182 | void AsyncSSLSocket::OnConnectEvent(Socket* socket) { |
Niels Möller | 8729d78 | 2021-08-11 09:22:44 | [diff] [blame] | 183 | RTC_DCHECK(socket == GetSocket()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 184 | // TODO: we could buffer output too... |
nisse | c16fa5e | 2017-02-07 15:18:43 | [diff] [blame] | 185 | const int res = DirectSend(kSslClientHello, sizeof(kSslClientHello)); |
Niels Möller | b0cb4d1 | 2021-08-20 09:04:04 | [diff] [blame] | 186 | if (res != sizeof(kSslClientHello)) { |
Niels Möller | 2eb465f | 2021-08-23 09:17:56 | [diff] [blame] | 187 | RTC_LOG(LS_ERROR) << "Sending fake SSL ClientHello message failed."; |
Niels Möller | b0cb4d1 | 2021-08-20 09:04:04 | [diff] [blame] | 188 | Close(); |
| 189 | SignalCloseEvent(this, 0); |
| 190 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | void AsyncSSLSocket::ProcessInput(char* data, size_t* len) { |
| 194 | if (*len < sizeof(kSslServerHello)) |
| 195 | return; |
| 196 | |
| 197 | if (memcmp(kSslServerHello, data, sizeof(kSslServerHello)) != 0) { |
Niels Möller | 2eb465f | 2021-08-23 09:17:56 | [diff] [blame] | 198 | RTC_LOG(LS_ERROR) << "Received non-matching fake SSL ServerHello message."; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 199 | Close(); |
| 200 | SignalCloseEvent(this, 0); // TODO: error code? |
| 201 | return; |
| 202 | } |
| 203 | |
| 204 | *len -= sizeof(kSslServerHello); |
| 205 | if (*len > 0) { |
| 206 | memmove(data, data + sizeof(kSslServerHello), *len); |
| 207 | } |
| 208 | |
| 209 | bool remainder = (*len > 0); |
| 210 | BufferInput(false); |
| 211 | SignalConnectEvent(this); |
| 212 | |
| 213 | // FIX: if SignalConnect causes the socket to be destroyed, we are in trouble |
| 214 | if (remainder) |
| 215 | SignalReadEvent(this); |
| 216 | } |
| 217 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 218 | } // namespace rtc |