fake_ssl_client_socket.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. // This StreamSocket implementation is to be used with servers that
  6. // accept connections on port 443 but don't really use SSL. For
  7. // example, the Google Talk servers do this to bypass proxies. (The
  8. // connection is upgraded to TLS as part of the XMPP negotiation, so
  9. // security is preserved.) A "fake" SSL handshake is done immediately
  10. // after connection to fool proxies into thinking that this is a real
  11. // SSL connection.
  12. //
  13. // NOTE: This StreamSocket implementation does *not* do a real SSL
  14. // handshake nor does it do any encryption!
  15. #ifndef COMPONENTS_WEBRTC_FAKE_SSL_CLIENT_SOCKET_H_
  16. #define COMPONENTS_WEBRTC_FAKE_SSL_CLIENT_SOCKET_H_
  17. #include <stdint.h>
  18. #include <cstddef>
  19. #include <memory>
  20. #include "base/compiler_specific.h"
  21. #include "base/memory/ref_counted.h"
  22. #include "base/strings/string_piece.h"
  23. #include "net/base/completion_once_callback.h"
  24. #include "net/base/net_errors.h"
  25. #include "net/socket/stream_socket.h"
  26. #include "net/traffic_annotation/network_traffic_annotation.h"
  27. namespace net {
  28. class DrainableIOBuffer;
  29. class SSLInfo;
  30. } // namespace net
  31. namespace webrtc {
  32. class FakeSSLClientSocket : public net::StreamSocket {
  33. public:
  34. explicit FakeSSLClientSocket(
  35. std::unique_ptr<net::StreamSocket> transport_socket);
  36. ~FakeSSLClientSocket() override;
  37. // Exposed for testing.
  38. static base::StringPiece GetSslClientHello();
  39. static base::StringPiece GetSslServerHello();
  40. // net::StreamSocket implementation.
  41. int Read(net::IOBuffer* buf,
  42. int buf_len,
  43. net::CompletionOnceCallback callback) override;
  44. int ReadIfReady(net::IOBuffer* buf,
  45. int buf_len,
  46. net::CompletionOnceCallback callback) override;
  47. int CancelReadIfReady() override;
  48. int Write(
  49. net::IOBuffer* buf,
  50. int buf_len,
  51. net::CompletionOnceCallback callback,
  52. const net::NetworkTrafficAnnotationTag& traffic_annotation) override;
  53. int SetReceiveBufferSize(int32_t size) override;
  54. int SetSendBufferSize(int32_t size) override;
  55. int Connect(net::CompletionOnceCallback callback) override;
  56. void Disconnect() override;
  57. bool IsConnected() const override;
  58. bool IsConnectedAndIdle() const override;
  59. int GetPeerAddress(net::IPEndPoint* address) const override;
  60. int GetLocalAddress(net::IPEndPoint* address) const override;
  61. const net::NetLogWithSource& NetLog() const override;
  62. bool WasEverUsed() const override;
  63. bool WasAlpnNegotiated() const override;
  64. net::NextProto GetNegotiatedProtocol() const override;
  65. bool GetSSLInfo(net::SSLInfo* ssl_info) override;
  66. int64_t GetTotalReceivedBytes() const override;
  67. void ApplySocketTag(const net::SocketTag& tag) override;
  68. private:
  69. enum HandshakeState {
  70. STATE_NONE,
  71. STATE_CONNECT,
  72. STATE_SEND_CLIENT_HELLO,
  73. STATE_VERIFY_SERVER_HELLO,
  74. };
  75. int DoHandshakeLoop();
  76. void RunUserConnectCallback(int status);
  77. void DoHandshakeLoopWithUserConnectCallback();
  78. int DoConnect();
  79. void OnConnectDone(int status);
  80. void ProcessConnectDone();
  81. int DoSendClientHello();
  82. void OnSendClientHelloDone(int status);
  83. void ProcessSendClientHelloDone(size_t written);
  84. int DoVerifyServerHello();
  85. void OnVerifyServerHelloDone(int status);
  86. net::Error ProcessVerifyServerHelloDone(size_t read);
  87. std::unique_ptr<net::StreamSocket> transport_socket_;
  88. // During the handshake process, holds a value from HandshakeState.
  89. // STATE_NONE otherwise.
  90. HandshakeState next_handshake_state_;
  91. // True iff we're connected and we've finished the handshake.
  92. bool handshake_completed_;
  93. // The callback passed to Connect().
  94. net::CompletionOnceCallback user_connect_callback_;
  95. scoped_refptr<net::DrainableIOBuffer> write_buf_;
  96. scoped_refptr<net::DrainableIOBuffer> read_buf_;
  97. };
  98. } // namespace webrtc
  99. #endif // COMPONENTS_WEBRTC_FAKE_SSL_CLIENT_SOCKET_H_