fake_ssl_client_socket.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. #include "components/webrtc/fake_ssl_client_socket.h"
  5. #include <stddef.h>
  6. #include <stdint.h>
  7. #include <cstdlib>
  8. #include <cstring>
  9. #include <utility>
  10. #include "base/bind.h"
  11. #include "base/compiler_specific.h"
  12. #include "base/logging.h"
  13. #include "net/base/io_buffer.h"
  14. #include "net/base/net_errors.h"
  15. namespace webrtc {
  16. namespace {
  17. // The constants below were taken from libjingle's socketadapters.cc.
  18. // Basically, we do a "fake" SSL handshake to fool proxies into
  19. // thinking this is a real SSL connection.
  20. // This is a SSL v2 CLIENT_HELLO message.
  21. // TODO(juberti): Should this have a session id? The response doesn't have a
  22. // certificate, so the hello should have a session id.
  23. static const uint8_t kSslClientHello[] = {
  24. 0x80, 0x46, // msg len
  25. 0x01, // CLIENT_HELLO
  26. 0x03, 0x01, // SSL 3.1
  27. 0x00, 0x2d, // ciphersuite len
  28. 0x00, 0x00, // session id len
  29. 0x00, 0x10, // challenge len
  30. 0x01, 0x00, 0x80, 0x03, 0x00, 0x80, 0x07, 0x00, 0xc0, // ciphersuites
  31. 0x06, 0x00, 0x40, 0x02, 0x00, 0x80, 0x04, 0x00, 0x80, //
  32. 0x00, 0x00, 0x04, 0x00, 0xfe, 0xff, 0x00, 0x00, 0x0a, //
  33. 0x00, 0xfe, 0xfe, 0x00, 0x00, 0x09, 0x00, 0x00, 0x64, //
  34. 0x00, 0x00, 0x62, 0x00, 0x00, 0x03, 0x00, 0x00, 0x06, //
  35. 0x1f, 0x17, 0x0c, 0xa6, 0x2f, 0x00, 0x78, 0xfc, // challenge
  36. 0x46, 0x55, 0x2e, 0xb1, 0x83, 0x39, 0xf1, 0xea //
  37. };
  38. // This is a TLSv1 SERVER_HELLO message.
  39. static const uint8_t kSslServerHello[] = {
  40. 0x16, // handshake message
  41. 0x03, 0x01, // SSL 3.1
  42. 0x00, 0x4a, // message len
  43. 0x02, // SERVER_HELLO
  44. 0x00, 0x00, 0x46, // handshake len
  45. 0x03, 0x01, // SSL 3.1
  46. 0x42, 0x85, 0x45, 0xa7, 0x27, 0xa9, 0x5d, 0xa0, // server random
  47. 0xb3, 0xc5, 0xe7, 0x53, 0xda, 0x48, 0x2b, 0x3f, //
  48. 0xc6, 0x5a, 0xca, 0x89, 0xc1, 0x58, 0x52, 0xa1, //
  49. 0x78, 0x3c, 0x5b, 0x17, 0x46, 0x00, 0x85, 0x3f, //
  50. 0x20, // session id len
  51. 0x0e, 0xd3, 0x06, 0x72, 0x5b, 0x5b, 0x1b, 0x5f, // session id
  52. 0x15, 0xac, 0x13, 0xf9, 0x88, 0x53, 0x9d, 0x9b, //
  53. 0xe8, 0x3d, 0x7b, 0x0c, 0x30, 0x32, 0x6e, 0x38, //
  54. 0x4d, 0xa2, 0x75, 0x57, 0x41, 0x6c, 0x34, 0x5c, //
  55. 0x00, 0x04, // RSA/RC4-128/MD5
  56. 0x00 // null compression
  57. };
  58. // TODO(crbug/1183244): This annotation is not test specific but is for test. We
  59. // should fix it.
  60. constexpr net::NetworkTrafficAnnotationTag kTrafficAnnotation =
  61. net::DefineNetworkTrafficAnnotation(
  62. "test",
  63. "Traffic annotation for FakeSSLClientSocket in jingle");
  64. scoped_refptr<net::DrainableIOBuffer> NewDrainableIOBufferWithSize(int size) {
  65. return base::MakeRefCounted<net::DrainableIOBuffer>(
  66. base::MakeRefCounted<net::IOBuffer>(size), size);
  67. }
  68. } // namespace
  69. base::StringPiece FakeSSLClientSocket::GetSslClientHello() {
  70. return base::StringPiece(reinterpret_cast<const char*>(kSslClientHello),
  71. std::size(kSslClientHello));
  72. }
  73. base::StringPiece FakeSSLClientSocket::GetSslServerHello() {
  74. return base::StringPiece(reinterpret_cast<const char*>(kSslServerHello),
  75. std::size(kSslServerHello));
  76. }
  77. FakeSSLClientSocket::FakeSSLClientSocket(
  78. std::unique_ptr<net::StreamSocket> transport_socket)
  79. : transport_socket_(std::move(transport_socket)),
  80. next_handshake_state_(STATE_NONE),
  81. handshake_completed_(false),
  82. write_buf_(NewDrainableIOBufferWithSize(std::size(kSslClientHello))),
  83. read_buf_(NewDrainableIOBufferWithSize(std::size(kSslServerHello))) {
  84. CHECK(transport_socket_.get());
  85. std::memcpy(write_buf_->data(), kSslClientHello, std::size(kSslClientHello));
  86. }
  87. FakeSSLClientSocket::~FakeSSLClientSocket() {}
  88. int FakeSSLClientSocket::Read(net::IOBuffer* buf,
  89. int buf_len,
  90. net::CompletionOnceCallback callback) {
  91. DCHECK_EQ(next_handshake_state_, STATE_NONE);
  92. DCHECK(handshake_completed_);
  93. return transport_socket_->Read(buf, buf_len, std::move(callback));
  94. }
  95. int FakeSSLClientSocket::ReadIfReady(net::IOBuffer* buf,
  96. int buf_len,
  97. net::CompletionOnceCallback callback) {
  98. DCHECK_EQ(next_handshake_state_, STATE_NONE);
  99. DCHECK(handshake_completed_);
  100. return transport_socket_->ReadIfReady(buf, buf_len, std::move(callback));
  101. }
  102. int FakeSSLClientSocket::CancelReadIfReady() {
  103. DCHECK_EQ(next_handshake_state_, STATE_NONE);
  104. DCHECK(handshake_completed_);
  105. return transport_socket_->CancelReadIfReady();
  106. }
  107. int FakeSSLClientSocket::Write(
  108. net::IOBuffer* buf,
  109. int buf_len,
  110. net::CompletionOnceCallback callback,
  111. const net::NetworkTrafficAnnotationTag& traffic_annotation) {
  112. DCHECK_EQ(next_handshake_state_, STATE_NONE);
  113. DCHECK(handshake_completed_);
  114. return transport_socket_->Write(buf, buf_len, std::move(callback),
  115. traffic_annotation);
  116. }
  117. int FakeSSLClientSocket::SetReceiveBufferSize(int32_t size) {
  118. return transport_socket_->SetReceiveBufferSize(size);
  119. }
  120. int FakeSSLClientSocket::SetSendBufferSize(int32_t size) {
  121. return transport_socket_->SetSendBufferSize(size);
  122. }
  123. int FakeSSLClientSocket::Connect(net::CompletionOnceCallback callback) {
  124. // We don't support synchronous operation, even if
  125. // |transport_socket_| does.
  126. DCHECK(!callback.is_null());
  127. DCHECK_EQ(next_handshake_state_, STATE_NONE);
  128. DCHECK(!handshake_completed_);
  129. DCHECK(user_connect_callback_.is_null());
  130. DCHECK_EQ(write_buf_->BytesConsumed(), 0);
  131. DCHECK_EQ(read_buf_->BytesConsumed(), 0);
  132. next_handshake_state_ = STATE_CONNECT;
  133. int status = DoHandshakeLoop();
  134. if (status == net::ERR_IO_PENDING)
  135. user_connect_callback_ = std::move(callback);
  136. return status;
  137. }
  138. int FakeSSLClientSocket::DoHandshakeLoop() {
  139. DCHECK_NE(next_handshake_state_, STATE_NONE);
  140. int status = net::OK;
  141. do {
  142. HandshakeState state = next_handshake_state_;
  143. next_handshake_state_ = STATE_NONE;
  144. switch (state) {
  145. case STATE_CONNECT:
  146. status = DoConnect();
  147. break;
  148. case STATE_SEND_CLIENT_HELLO:
  149. status = DoSendClientHello();
  150. break;
  151. case STATE_VERIFY_SERVER_HELLO:
  152. status = DoVerifyServerHello();
  153. break;
  154. default:
  155. status = net::ERR_UNEXPECTED;
  156. LOG(DFATAL) << "unexpected state: " << state;
  157. break;
  158. }
  159. } while ((status != net::ERR_IO_PENDING) &&
  160. (next_handshake_state_ != STATE_NONE));
  161. return status;
  162. }
  163. void FakeSSLClientSocket::RunUserConnectCallback(int status) {
  164. DCHECK_LE(status, net::OK);
  165. next_handshake_state_ = STATE_NONE;
  166. std::move(user_connect_callback_).Run(status);
  167. }
  168. void FakeSSLClientSocket::DoHandshakeLoopWithUserConnectCallback() {
  169. int status = DoHandshakeLoop();
  170. if (status != net::ERR_IO_PENDING) {
  171. RunUserConnectCallback(status);
  172. }
  173. }
  174. int FakeSSLClientSocket::DoConnect() {
  175. int status = transport_socket_->Connect(base::BindOnce(
  176. &FakeSSLClientSocket::OnConnectDone, base::Unretained(this)));
  177. if (status != net::OK) {
  178. return status;
  179. }
  180. ProcessConnectDone();
  181. return net::OK;
  182. }
  183. void FakeSSLClientSocket::OnConnectDone(int status) {
  184. DCHECK_NE(status, net::ERR_IO_PENDING);
  185. DCHECK_LE(status, net::OK);
  186. DCHECK(!user_connect_callback_.is_null());
  187. if (status != net::OK) {
  188. RunUserConnectCallback(status);
  189. return;
  190. }
  191. ProcessConnectDone();
  192. DoHandshakeLoopWithUserConnectCallback();
  193. }
  194. void FakeSSLClientSocket::ProcessConnectDone() {
  195. DCHECK_EQ(write_buf_->BytesConsumed(), 0);
  196. DCHECK_EQ(read_buf_->BytesConsumed(), 0);
  197. next_handshake_state_ = STATE_SEND_CLIENT_HELLO;
  198. }
  199. int FakeSSLClientSocket::DoSendClientHello() {
  200. int status = transport_socket_->Write(
  201. write_buf_.get(), write_buf_->BytesRemaining(),
  202. base::BindOnce(&FakeSSLClientSocket::OnSendClientHelloDone,
  203. base::Unretained(this)),
  204. kTrafficAnnotation);
  205. if (status < net::OK) {
  206. return status;
  207. }
  208. ProcessSendClientHelloDone(static_cast<size_t>(status));
  209. return net::OK;
  210. }
  211. void FakeSSLClientSocket::OnSendClientHelloDone(int status) {
  212. DCHECK_NE(status, net::ERR_IO_PENDING);
  213. DCHECK(!user_connect_callback_.is_null());
  214. if (status < net::OK) {
  215. RunUserConnectCallback(status);
  216. return;
  217. }
  218. ProcessSendClientHelloDone(static_cast<size_t>(status));
  219. DoHandshakeLoopWithUserConnectCallback();
  220. }
  221. void FakeSSLClientSocket::ProcessSendClientHelloDone(size_t written) {
  222. DCHECK_LE(written, static_cast<size_t>(write_buf_->BytesRemaining()));
  223. DCHECK_EQ(read_buf_->BytesConsumed(), 0);
  224. if (written < static_cast<size_t>(write_buf_->BytesRemaining())) {
  225. next_handshake_state_ = STATE_SEND_CLIENT_HELLO;
  226. write_buf_->DidConsume(written);
  227. } else {
  228. next_handshake_state_ = STATE_VERIFY_SERVER_HELLO;
  229. }
  230. }
  231. int FakeSSLClientSocket::DoVerifyServerHello() {
  232. int status = transport_socket_->Read(
  233. read_buf_.get(), read_buf_->BytesRemaining(),
  234. base::BindOnce(&FakeSSLClientSocket::OnVerifyServerHelloDone,
  235. base::Unretained(this)));
  236. if (status < net::OK) {
  237. return status;
  238. }
  239. size_t read = static_cast<size_t>(status);
  240. return ProcessVerifyServerHelloDone(read);
  241. }
  242. void FakeSSLClientSocket::OnVerifyServerHelloDone(int status) {
  243. DCHECK_NE(status, net::ERR_IO_PENDING);
  244. DCHECK(!user_connect_callback_.is_null());
  245. if (status < net::OK) {
  246. RunUserConnectCallback(status);
  247. return;
  248. }
  249. size_t read = static_cast<size_t>(status);
  250. status = ProcessVerifyServerHelloDone(read);
  251. if (status < net::OK) {
  252. RunUserConnectCallback(status);
  253. return;
  254. }
  255. if (handshake_completed_) {
  256. RunUserConnectCallback(net::OK);
  257. } else {
  258. DoHandshakeLoopWithUserConnectCallback();
  259. }
  260. }
  261. net::Error FakeSSLClientSocket::ProcessVerifyServerHelloDone(size_t read) {
  262. DCHECK_LE(read, static_cast<size_t>(read_buf_->BytesRemaining()));
  263. if (read == 0U) {
  264. return net::ERR_UNEXPECTED;
  265. }
  266. const uint8_t* expected_data_start =
  267. &kSslServerHello[std::size(kSslServerHello) -
  268. read_buf_->BytesRemaining()];
  269. if (std::memcmp(expected_data_start, read_buf_->data(), read) != 0) {
  270. return net::ERR_UNEXPECTED;
  271. }
  272. if (read < static_cast<size_t>(read_buf_->BytesRemaining())) {
  273. next_handshake_state_ = STATE_VERIFY_SERVER_HELLO;
  274. read_buf_->DidConsume(read);
  275. } else {
  276. next_handshake_state_ = STATE_NONE;
  277. handshake_completed_ = true;
  278. }
  279. return net::OK;
  280. }
  281. void FakeSSLClientSocket::Disconnect() {
  282. transport_socket_->Disconnect();
  283. next_handshake_state_ = STATE_NONE;
  284. handshake_completed_ = false;
  285. user_connect_callback_.Reset();
  286. write_buf_->SetOffset(0);
  287. read_buf_->SetOffset(0);
  288. }
  289. bool FakeSSLClientSocket::IsConnected() const {
  290. return handshake_completed_ && transport_socket_->IsConnected();
  291. }
  292. bool FakeSSLClientSocket::IsConnectedAndIdle() const {
  293. return handshake_completed_ && transport_socket_->IsConnectedAndIdle();
  294. }
  295. int FakeSSLClientSocket::GetPeerAddress(net::IPEndPoint* address) const {
  296. return transport_socket_->GetPeerAddress(address);
  297. }
  298. int FakeSSLClientSocket::GetLocalAddress(net::IPEndPoint* address) const {
  299. return transport_socket_->GetLocalAddress(address);
  300. }
  301. const net::NetLogWithSource& FakeSSLClientSocket::NetLog() const {
  302. return transport_socket_->NetLog();
  303. }
  304. bool FakeSSLClientSocket::WasEverUsed() const {
  305. return transport_socket_->WasEverUsed();
  306. }
  307. bool FakeSSLClientSocket::WasAlpnNegotiated() const {
  308. return transport_socket_->WasAlpnNegotiated();
  309. }
  310. net::NextProto FakeSSLClientSocket::GetNegotiatedProtocol() const {
  311. return transport_socket_->GetNegotiatedProtocol();
  312. }
  313. bool FakeSSLClientSocket::GetSSLInfo(net::SSLInfo* ssl_info) {
  314. return transport_socket_->GetSSLInfo(ssl_info);
  315. }
  316. int64_t FakeSSLClientSocket::GetTotalReceivedBytes() const {
  317. NOTIMPLEMENTED();
  318. return 0;
  319. }
  320. void FakeSSLClientSocket::ApplySocketTag(const net::SocketTag& tag) {
  321. NOTIMPLEMENTED();
  322. }
  323. } // namespace webrtc