dtls_transport.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * Copyright 2011 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_DTLS_TRANSPORT_H_
  11. #define P2P_BASE_DTLS_TRANSPORT_H_
  12. #include <memory>
  13. #include <string>
  14. #include <vector>
  15. #include "api/crypto/crypto_options.h"
  16. #include "p2p/base/dtls_transport_internal.h"
  17. #include "p2p/base/ice_transport_internal.h"
  18. #include "rtc_base/buffer.h"
  19. #include "rtc_base/buffer_queue.h"
  20. #include "rtc_base/constructor_magic.h"
  21. #include "rtc_base/ssl_stream_adapter.h"
  22. #include "rtc_base/stream.h"
  23. #include "rtc_base/strings/string_builder.h"
  24. #include "rtc_base/thread_checker.h"
  25. namespace rtc {
  26. class PacketTransportInternal;
  27. }
  28. namespace cricket {
  29. // A bridge between a packet-oriented/transport-type interface on
  30. // the bottom and a StreamInterface on the top.
  31. class StreamInterfaceChannel : public rtc::StreamInterface {
  32. public:
  33. explicit StreamInterfaceChannel(IceTransportInternal* ice_transport);
  34. // Push in a packet; this gets pulled out from Read().
  35. bool OnPacketReceived(const char* data, size_t size);
  36. // Implementations of StreamInterface
  37. rtc::StreamState GetState() const override;
  38. void Close() override;
  39. rtc::StreamResult Read(void* buffer,
  40. size_t buffer_len,
  41. size_t* read,
  42. int* error) override;
  43. rtc::StreamResult Write(const void* data,
  44. size_t data_len,
  45. size_t* written,
  46. int* error) override;
  47. private:
  48. IceTransportInternal* ice_transport_; // owned by DtlsTransport
  49. rtc::StreamState state_;
  50. rtc::BufferQueue packets_;
  51. RTC_DISALLOW_COPY_AND_ASSIGN(StreamInterfaceChannel);
  52. };
  53. // This class provides a DTLS SSLStreamAdapter inside a TransportChannel-style
  54. // packet-based interface, wrapping an existing TransportChannel instance
  55. // (e.g a P2PTransportChannel)
  56. // Here's the way this works:
  57. //
  58. // DtlsTransport {
  59. // SSLStreamAdapter* dtls_ {
  60. // StreamInterfaceChannel downward_ {
  61. // IceTransportInternal* ice_transport_;
  62. // }
  63. // }
  64. // }
  65. //
  66. // - Data which comes into DtlsTransport from the underlying
  67. // ice_transport_ via OnReadPacket() is checked for whether it is DTLS
  68. // or not, and if it is, is passed to DtlsTransport::HandleDtlsPacket,
  69. // which pushes it into to downward_. dtls_ is listening for events on
  70. // downward_, so it immediately calls downward_->Read().
  71. //
  72. // - Data written to DtlsTransport is passed either to downward_ or directly
  73. // to ice_transport_, depending on whether DTLS is negotiated and whether
  74. // the flags include PF_SRTP_BYPASS
  75. //
  76. // - The SSLStreamAdapter writes to downward_->Write() which translates it
  77. // into packet writes on ice_transport_.
  78. //
  79. // This class is not thread safe; all methods must be called on the same thread
  80. // as the constructor.
  81. class DtlsTransport : public DtlsTransportInternal {
  82. public:
  83. // |ice_transport| is the ICE transport this DTLS transport is wrapping. It
  84. // must outlive this DTLS transport.
  85. //
  86. // |crypto_options| are the options used for the DTLS handshake. This affects
  87. // whether GCM crypto suites are negotiated.
  88. //
  89. // |event_log| is an optional RtcEventLog for logging state changes. It should
  90. // outlive the DtlsTransport.
  91. explicit DtlsTransport(IceTransportInternal* ice_transport,
  92. const webrtc::CryptoOptions& crypto_options,
  93. webrtc::RtcEventLog* event_log);
  94. ~DtlsTransport() override;
  95. const webrtc::CryptoOptions& crypto_options() const override;
  96. DtlsTransportState dtls_state() const override;
  97. const std::string& transport_name() const override;
  98. int component() const override;
  99. // DTLS is active if a local certificate was set. Otherwise this acts in a
  100. // "passthrough" mode, sending packets directly through the underlying ICE
  101. // transport.
  102. // TODO(deadbeef): Remove this weirdness, and handle it in the upper layers.
  103. bool IsDtlsActive() const override;
  104. // SetLocalCertificate is what makes DTLS active. It must be called before
  105. // SetRemoteFinterprint.
  106. // TODO(deadbeef): Once DtlsTransport no longer has the concept of being
  107. // "active" or not (acting as a passthrough if not active), just require this
  108. // certificate on construction or "Start".
  109. bool SetLocalCertificate(
  110. const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) override;
  111. rtc::scoped_refptr<rtc::RTCCertificate> GetLocalCertificate() const override;
  112. // SetRemoteFingerprint must be called after SetLocalCertificate, and any
  113. // other methods like SetDtlsRole. It's what triggers the actual DTLS setup.
  114. // TODO(deadbeef): Rename to "Start" like in ORTC?
  115. bool SetRemoteFingerprint(const std::string& digest_alg,
  116. const uint8_t* digest,
  117. size_t digest_len) override;
  118. // Called to send a packet (via DTLS, if turned on).
  119. int SendPacket(const char* data,
  120. size_t size,
  121. const rtc::PacketOptions& options,
  122. int flags) override;
  123. bool GetOption(rtc::Socket::Option opt, int* value) override;
  124. bool SetSslMaxProtocolVersion(rtc::SSLProtocolVersion version) override;
  125. // Find out which TLS version was negotiated
  126. bool GetSslVersionBytes(int* version) const override;
  127. // Find out which DTLS-SRTP cipher was negotiated
  128. bool GetSrtpCryptoSuite(int* cipher) override;
  129. bool GetDtlsRole(rtc::SSLRole* role) const override;
  130. bool SetDtlsRole(rtc::SSLRole role) override;
  131. // Find out which DTLS cipher was negotiated
  132. bool GetSslCipherSuite(int* cipher) override;
  133. // Once DTLS has been established, this method retrieves the certificate
  134. // chain in use by the remote peer, for use in external identity
  135. // verification.
  136. std::unique_ptr<rtc::SSLCertChain> GetRemoteSSLCertChain() const override;
  137. // Once DTLS has established (i.e., this ice_transport is writable), this
  138. // method extracts the keys negotiated during the DTLS handshake, for use in
  139. // external encryption. DTLS-SRTP uses this to extract the needed SRTP keys.
  140. // See the SSLStreamAdapter documentation for info on the specific parameters.
  141. bool ExportKeyingMaterial(const std::string& label,
  142. const uint8_t* context,
  143. size_t context_len,
  144. bool use_context,
  145. uint8_t* result,
  146. size_t result_len) override;
  147. IceTransportInternal* ice_transport() override;
  148. // For informational purposes. Tells if the DTLS handshake has finished.
  149. // This may be true even if writable() is false, if the remote fingerprint
  150. // has not yet been verified.
  151. bool IsDtlsConnected();
  152. bool receiving() const override;
  153. bool writable() const override;
  154. int GetError() override;
  155. absl::optional<rtc::NetworkRoute> network_route() const override;
  156. int SetOption(rtc::Socket::Option opt, int value) override;
  157. std::string ToString() const {
  158. const absl::string_view RECEIVING_ABBREV[2] = {"_", "R"};
  159. const absl::string_view WRITABLE_ABBREV[2] = {"_", "W"};
  160. rtc::StringBuilder sb;
  161. sb << "DtlsTransport[" << transport_name_ << "|" << component_ << "|"
  162. << RECEIVING_ABBREV[receiving()] << WRITABLE_ABBREV[writable()] << "]";
  163. return sb.Release();
  164. }
  165. private:
  166. void ConnectToIceTransport();
  167. void OnWritableState(rtc::PacketTransportInternal* transport);
  168. void OnReadPacket(rtc::PacketTransportInternal* transport,
  169. const char* data,
  170. size_t size,
  171. const int64_t& packet_time_us,
  172. int flags);
  173. void OnSentPacket(rtc::PacketTransportInternal* transport,
  174. const rtc::SentPacket& sent_packet);
  175. void OnReadyToSend(rtc::PacketTransportInternal* transport);
  176. void OnReceivingState(rtc::PacketTransportInternal* transport);
  177. void OnDtlsEvent(rtc::StreamInterface* stream_, int sig, int err);
  178. void OnNetworkRouteChanged(absl::optional<rtc::NetworkRoute> network_route);
  179. bool SetupDtls();
  180. void MaybeStartDtls();
  181. bool HandleDtlsPacket(const char* data, size_t size);
  182. void OnDtlsHandshakeError(rtc::SSLHandshakeError error);
  183. void ConfigureHandshakeTimeout();
  184. void set_receiving(bool receiving);
  185. void set_writable(bool writable);
  186. // Sets the DTLS state, signaling if necessary.
  187. void set_dtls_state(DtlsTransportState state);
  188. rtc::ThreadChecker thread_checker_;
  189. std::string transport_name_;
  190. int component_;
  191. DtlsTransportState dtls_state_ = DTLS_TRANSPORT_NEW;
  192. // Underlying ice_transport, not owned by this class.
  193. IceTransportInternal* ice_transport_;
  194. std::unique_ptr<rtc::SSLStreamAdapter> dtls_; // The DTLS stream
  195. StreamInterfaceChannel*
  196. downward_; // Wrapper for ice_transport_, owned by dtls_.
  197. std::vector<int> srtp_ciphers_; // SRTP ciphers to use with DTLS.
  198. bool dtls_active_ = false;
  199. rtc::scoped_refptr<rtc::RTCCertificate> local_certificate_;
  200. absl::optional<rtc::SSLRole> dtls_role_;
  201. rtc::SSLProtocolVersion ssl_max_version_;
  202. webrtc::CryptoOptions crypto_options_;
  203. rtc::Buffer remote_fingerprint_value_;
  204. std::string remote_fingerprint_algorithm_;
  205. // Cached DTLS ClientHello packet that was received before we started the
  206. // DTLS handshake. This could happen if the hello was received before the
  207. // ice transport became writable, or before a remote fingerprint was received.
  208. rtc::Buffer cached_client_hello_;
  209. bool receiving_ = false;
  210. bool writable_ = false;
  211. webrtc::RtcEventLog* const event_log_;
  212. RTC_DISALLOW_COPY_AND_ASSIGN(DtlsTransport);
  213. };
  214. } // namespace cricket
  215. #endif // P2P_BASE_DTLS_TRANSPORT_H_