fake_dtls_transport.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*
  2. * Copyright 2017 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. #ifndef P2P_BASE_FAKE_DTLS_TRANSPORT_H_
  11. #define P2P_BASE_FAKE_DTLS_TRANSPORT_H_
  12. #include <memory>
  13. #include <string>
  14. #include <utility>
  15. #include <vector>
  16. #include "api/crypto/crypto_options.h"
  17. #include "p2p/base/dtls_transport_internal.h"
  18. #include "p2p/base/fake_ice_transport.h"
  19. #include "rtc_base/fake_ssl_identity.h"
  20. #include "rtc_base/rtc_certificate.h"
  21. namespace cricket {
  22. // Fake DTLS transport which is implemented by wrapping a fake ICE transport.
  23. // Doesn't interact directly with fake ICE transport for anything other than
  24. // sending packets.
  25. class FakeDtlsTransport : public DtlsTransportInternal {
  26. public:
  27. explicit FakeDtlsTransport(FakeIceTransport* ice_transport)
  28. : ice_transport_(ice_transport),
  29. transport_name_(ice_transport->transport_name()),
  30. component_(ice_transport->component()),
  31. dtls_fingerprint_("", nullptr) {
  32. RTC_DCHECK(ice_transport_);
  33. ice_transport_->SignalReadPacket.connect(
  34. this, &FakeDtlsTransport::OnIceTransportReadPacket);
  35. ice_transport_->SignalNetworkRouteChanged.connect(
  36. this, &FakeDtlsTransport::OnNetworkRouteChanged);
  37. }
  38. explicit FakeDtlsTransport(std::unique_ptr<FakeIceTransport> ice)
  39. : owned_ice_transport_(std::move(ice)),
  40. transport_name_(owned_ice_transport_->transport_name()),
  41. component_(owned_ice_transport_->component()),
  42. dtls_fingerprint_("", rtc::ArrayView<const uint8_t>()) {
  43. ice_transport_ = owned_ice_transport_.get();
  44. ice_transport_->SignalReadPacket.connect(
  45. this, &FakeDtlsTransport::OnIceTransportReadPacket);
  46. ice_transport_->SignalNetworkRouteChanged.connect(
  47. this, &FakeDtlsTransport::OnNetworkRouteChanged);
  48. }
  49. // If this constructor is called, a new fake ICE transport will be created,
  50. // and this FakeDtlsTransport will take the ownership.
  51. explicit FakeDtlsTransport(const std::string& name, int component)
  52. : FakeDtlsTransport(std::make_unique<FakeIceTransport>(name, component)) {
  53. }
  54. ~FakeDtlsTransport() override {
  55. if (dest_ && dest_->dest_ == this) {
  56. dest_->dest_ = nullptr;
  57. }
  58. }
  59. // Get inner fake ICE transport.
  60. FakeIceTransport* fake_ice_transport() { return ice_transport_; }
  61. // If async, will send packets by "Post"-ing to message queue instead of
  62. // synchronously "Send"-ing.
  63. void SetAsync(bool async) { ice_transport_->SetAsync(async); }
  64. void SetAsyncDelay(int delay_ms) { ice_transport_->SetAsyncDelay(delay_ms); }
  65. // SetWritable, SetReceiving and SetDestination are the main methods that can
  66. // be used for testing, to simulate connectivity or lack thereof.
  67. void SetWritable(bool writable) {
  68. ice_transport_->SetWritable(writable);
  69. set_writable(writable);
  70. }
  71. void SetReceiving(bool receiving) {
  72. ice_transport_->SetReceiving(receiving);
  73. set_receiving(receiving);
  74. }
  75. void SetDtlsState(DtlsTransportState state) {
  76. dtls_state_ = state;
  77. SignalDtlsState(this, dtls_state_);
  78. }
  79. // Simulates the two DTLS transports connecting to each other.
  80. // If |asymmetric| is true this method only affects this FakeDtlsTransport.
  81. // If false, it affects |dest| as well.
  82. void SetDestination(FakeDtlsTransport* dest, bool asymmetric = false) {
  83. if (dest == dest_) {
  84. return;
  85. }
  86. RTC_DCHECK(!dest || !dest_)
  87. << "Changing fake destination from one to another is not supported.";
  88. if (dest && !dest_) {
  89. // This simulates the DTLS handshake.
  90. dest_ = dest;
  91. if (local_cert_ && dest_->local_cert_) {
  92. do_dtls_ = true;
  93. RTC_LOG(LS_INFO) << "FakeDtlsTransport is doing DTLS";
  94. } else {
  95. do_dtls_ = false;
  96. RTC_LOG(LS_INFO) << "FakeDtlsTransport is not doing DTLS";
  97. }
  98. SetWritable(true);
  99. if (!asymmetric) {
  100. dest->SetDestination(this, true);
  101. }
  102. // If the |dtls_role_| is unset, set it to SSL_CLIENT by default.
  103. if (!dtls_role_) {
  104. dtls_role_ = std::move(rtc::SSL_CLIENT);
  105. }
  106. SetDtlsState(DTLS_TRANSPORT_CONNECTED);
  107. ice_transport_->SetDestination(
  108. static_cast<FakeIceTransport*>(dest->ice_transport()), asymmetric);
  109. } else {
  110. // Simulates loss of connectivity, by asymmetrically forgetting dest_.
  111. dest_ = nullptr;
  112. SetWritable(false);
  113. ice_transport_->SetDestination(nullptr, asymmetric);
  114. }
  115. }
  116. // Fake DtlsTransportInternal implementation.
  117. DtlsTransportState dtls_state() const override { return dtls_state_; }
  118. const std::string& transport_name() const override { return transport_name_; }
  119. int component() const override { return component_; }
  120. const rtc::SSLFingerprint& dtls_fingerprint() const {
  121. return dtls_fingerprint_;
  122. }
  123. bool SetRemoteFingerprint(const std::string& alg,
  124. const uint8_t* digest,
  125. size_t digest_len) override {
  126. dtls_fingerprint_ =
  127. rtc::SSLFingerprint(alg, rtc::MakeArrayView(digest, digest_len));
  128. return true;
  129. }
  130. bool SetSslMaxProtocolVersion(rtc::SSLProtocolVersion version) override {
  131. return true;
  132. }
  133. bool SetDtlsRole(rtc::SSLRole role) override {
  134. dtls_role_ = std::move(role);
  135. return true;
  136. }
  137. bool GetDtlsRole(rtc::SSLRole* role) const override {
  138. if (!dtls_role_) {
  139. return false;
  140. }
  141. *role = *dtls_role_;
  142. return true;
  143. }
  144. const webrtc::CryptoOptions& crypto_options() const override {
  145. return crypto_options_;
  146. }
  147. void SetCryptoOptions(const webrtc::CryptoOptions& crypto_options) {
  148. crypto_options_ = crypto_options;
  149. }
  150. bool SetLocalCertificate(
  151. const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) override {
  152. do_dtls_ = true;
  153. local_cert_ = certificate;
  154. return true;
  155. }
  156. void SetRemoteSSLCertificate(rtc::FakeSSLCertificate* cert) {
  157. remote_cert_ = cert;
  158. }
  159. bool IsDtlsActive() const override { return do_dtls_; }
  160. bool GetSslVersionBytes(int* version) const override {
  161. if (!do_dtls_) {
  162. return false;
  163. }
  164. *version = 0x0102;
  165. return true;
  166. }
  167. bool GetSrtpCryptoSuite(int* crypto_suite) override {
  168. if (!do_dtls_) {
  169. return false;
  170. }
  171. *crypto_suite = crypto_suite_;
  172. return true;
  173. }
  174. void SetSrtpCryptoSuite(int crypto_suite) { crypto_suite_ = crypto_suite; }
  175. bool GetSslCipherSuite(int* cipher_suite) override {
  176. if (ssl_cipher_suite_) {
  177. *cipher_suite = *ssl_cipher_suite_;
  178. return true;
  179. }
  180. return false;
  181. }
  182. void SetSslCipherSuite(absl::optional<int> cipher_suite) {
  183. ssl_cipher_suite_ = cipher_suite;
  184. }
  185. rtc::scoped_refptr<rtc::RTCCertificate> GetLocalCertificate() const override {
  186. return local_cert_;
  187. }
  188. std::unique_ptr<rtc::SSLCertChain> GetRemoteSSLCertChain() const override {
  189. if (!remote_cert_) {
  190. return nullptr;
  191. }
  192. return std::make_unique<rtc::SSLCertChain>(remote_cert_->Clone());
  193. }
  194. bool ExportKeyingMaterial(const std::string& label,
  195. const uint8_t* context,
  196. size_t context_len,
  197. bool use_context,
  198. uint8_t* result,
  199. size_t result_len) override {
  200. if (!do_dtls_) {
  201. return false;
  202. }
  203. memset(result, 0xff, result_len);
  204. return true;
  205. }
  206. void set_ssl_max_protocol_version(rtc::SSLProtocolVersion version) {
  207. ssl_max_version_ = version;
  208. }
  209. rtc::SSLProtocolVersion ssl_max_protocol_version() const {
  210. return ssl_max_version_;
  211. }
  212. IceTransportInternal* ice_transport() override { return ice_transport_; }
  213. // PacketTransportInternal implementation, which passes through to fake ICE
  214. // transport for sending actual packets.
  215. bool writable() const override { return writable_; }
  216. bool receiving() const override { return receiving_; }
  217. int SendPacket(const char* data,
  218. size_t len,
  219. const rtc::PacketOptions& options,
  220. int flags) override {
  221. // We expect only SRTP packets to be sent through this interface.
  222. if (flags != PF_SRTP_BYPASS && flags != 0) {
  223. return -1;
  224. }
  225. return ice_transport_->SendPacket(data, len, options, flags);
  226. }
  227. int SetOption(rtc::Socket::Option opt, int value) override {
  228. return ice_transport_->SetOption(opt, value);
  229. }
  230. bool GetOption(rtc::Socket::Option opt, int* value) override {
  231. return ice_transport_->GetOption(opt, value);
  232. }
  233. int GetError() override { return ice_transport_->GetError(); }
  234. absl::optional<rtc::NetworkRoute> network_route() const override {
  235. return ice_transport_->network_route();
  236. }
  237. private:
  238. void OnIceTransportReadPacket(PacketTransportInternal* ice_,
  239. const char* data,
  240. size_t len,
  241. const int64_t& packet_time_us,
  242. int flags) {
  243. SignalReadPacket(this, data, len, packet_time_us, flags);
  244. }
  245. void set_receiving(bool receiving) {
  246. if (receiving_ == receiving) {
  247. return;
  248. }
  249. receiving_ = receiving;
  250. SignalReceivingState(this);
  251. }
  252. void set_writable(bool writable) {
  253. if (writable_ == writable) {
  254. return;
  255. }
  256. writable_ = writable;
  257. if (writable_) {
  258. SignalReadyToSend(this);
  259. }
  260. SignalWritableState(this);
  261. }
  262. void OnNetworkRouteChanged(absl::optional<rtc::NetworkRoute> network_route) {
  263. SignalNetworkRouteChanged(network_route);
  264. }
  265. FakeIceTransport* ice_transport_;
  266. std::unique_ptr<FakeIceTransport> owned_ice_transport_;
  267. std::string transport_name_;
  268. int component_;
  269. FakeDtlsTransport* dest_ = nullptr;
  270. rtc::scoped_refptr<rtc::RTCCertificate> local_cert_;
  271. rtc::FakeSSLCertificate* remote_cert_ = nullptr;
  272. bool do_dtls_ = false;
  273. rtc::SSLProtocolVersion ssl_max_version_ = rtc::SSL_PROTOCOL_DTLS_12;
  274. rtc::SSLFingerprint dtls_fingerprint_;
  275. absl::optional<rtc::SSLRole> dtls_role_;
  276. int crypto_suite_ = rtc::SRTP_AES128_CM_SHA1_80;
  277. absl::optional<int> ssl_cipher_suite_;
  278. webrtc::CryptoOptions crypto_options_;
  279. DtlsTransportState dtls_state_ = DTLS_TRANSPORT_NEW;
  280. bool receiving_ = false;
  281. bool writable_ = false;
  282. };
  283. } // namespace cricket
  284. #endif // P2P_BASE_FAKE_DTLS_TRANSPORT_H_