srtp_transport.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 PC_SRTP_TRANSPORT_H_
  11. #define PC_SRTP_TRANSPORT_H_
  12. #include <stddef.h>
  13. #include <cstdint>
  14. #include <memory>
  15. #include <string>
  16. #include <vector>
  17. #include "absl/types/optional.h"
  18. #include "api/crypto_params.h"
  19. #include "api/rtc_error.h"
  20. #include "p2p/base/packet_transport_internal.h"
  21. #include "pc/rtp_transport.h"
  22. #include "pc/srtp_session.h"
  23. #include "rtc_base/async_packet_socket.h"
  24. #include "rtc_base/buffer.h"
  25. #include "rtc_base/copy_on_write_buffer.h"
  26. #include "rtc_base/network_route.h"
  27. namespace webrtc {
  28. // This subclass of the RtpTransport is used for SRTP which is reponsible for
  29. // protecting/unprotecting the packets. It provides interfaces to set the crypto
  30. // parameters for the SrtpSession underneath.
  31. class SrtpTransport : public RtpTransport {
  32. public:
  33. explicit SrtpTransport(bool rtcp_mux_enabled);
  34. virtual ~SrtpTransport() = default;
  35. // SrtpTransportInterface specific implementation.
  36. virtual RTCError SetSrtpSendKey(const cricket::CryptoParams& params);
  37. virtual RTCError SetSrtpReceiveKey(const cricket::CryptoParams& params);
  38. bool SendRtpPacket(rtc::CopyOnWriteBuffer* packet,
  39. const rtc::PacketOptions& options,
  40. int flags) override;
  41. bool SendRtcpPacket(rtc::CopyOnWriteBuffer* packet,
  42. const rtc::PacketOptions& options,
  43. int flags) override;
  44. // The transport becomes active if the send_session_ and recv_session_ are
  45. // created.
  46. bool IsSrtpActive() const override;
  47. bool IsWritable(bool rtcp) const override;
  48. // Create new send/recv sessions and set the negotiated crypto keys for RTP
  49. // packet encryption. The keys can either come from SDES negotiation or DTLS
  50. // handshake.
  51. bool SetRtpParams(int send_cs,
  52. const uint8_t* send_key,
  53. int send_key_len,
  54. const std::vector<int>& send_extension_ids,
  55. int recv_cs,
  56. const uint8_t* recv_key,
  57. int recv_key_len,
  58. const std::vector<int>& recv_extension_ids);
  59. // Create new send/recv sessions and set the negotiated crypto keys for RTCP
  60. // packet encryption. The keys can either come from SDES negotiation or DTLS
  61. // handshake.
  62. bool SetRtcpParams(int send_cs,
  63. const uint8_t* send_key,
  64. int send_key_len,
  65. const std::vector<int>& send_extension_ids,
  66. int recv_cs,
  67. const uint8_t* recv_key,
  68. int recv_key_len,
  69. const std::vector<int>& recv_extension_ids);
  70. void ResetParams();
  71. // If external auth is enabled, SRTP will write a dummy auth tag that then
  72. // later must get replaced before the packet is sent out. Only supported for
  73. // non-GCM cipher suites and can be checked through "IsExternalAuthActive"
  74. // if it is actually used. This method is only valid before the RTP params
  75. // have been set.
  76. void EnableExternalAuth();
  77. bool IsExternalAuthEnabled() const;
  78. // A SrtpTransport supports external creation of the auth tag if a non-GCM
  79. // cipher is used. This method is only valid after the RTP params have
  80. // been set.
  81. bool IsExternalAuthActive() const;
  82. // Returns srtp overhead for rtp packets.
  83. bool GetSrtpOverhead(int* srtp_overhead) const;
  84. // Returns rtp auth params from srtp context.
  85. bool GetRtpAuthParams(uint8_t** key, int* key_len, int* tag_len);
  86. // Cache RTP Absoulute SendTime extension header ID. This is only used when
  87. // external authentication is enabled.
  88. void CacheRtpAbsSendTimeHeaderExtension(int rtp_abs_sendtime_extn_id) {
  89. rtp_abs_sendtime_extn_id_ = rtp_abs_sendtime_extn_id;
  90. }
  91. protected:
  92. // If the writable state changed, fire the SignalWritableState.
  93. void MaybeUpdateWritableState();
  94. private:
  95. void ConnectToRtpTransport();
  96. void CreateSrtpSessions();
  97. void OnRtpPacketReceived(rtc::CopyOnWriteBuffer packet,
  98. int64_t packet_time_us) override;
  99. void OnRtcpPacketReceived(rtc::CopyOnWriteBuffer packet,
  100. int64_t packet_time_us) override;
  101. void OnNetworkRouteChanged(
  102. absl::optional<rtc::NetworkRoute> network_route) override;
  103. // Override the RtpTransport::OnWritableState.
  104. void OnWritableState(rtc::PacketTransportInternal* packet_transport) override;
  105. bool ProtectRtp(void* data, int in_len, int max_len, int* out_len);
  106. // Overloaded version, outputs packet index.
  107. bool ProtectRtp(void* data,
  108. int in_len,
  109. int max_len,
  110. int* out_len,
  111. int64_t* index);
  112. bool ProtectRtcp(void* data, int in_len, int max_len, int* out_len);
  113. // Decrypts/verifies an invidiual RTP/RTCP packet.
  114. // If an HMAC is used, this will decrease the packet size.
  115. bool UnprotectRtp(void* data, int in_len, int* out_len);
  116. bool UnprotectRtcp(void* data, int in_len, int* out_len);
  117. bool MaybeSetKeyParams();
  118. bool ParseKeyParams(const std::string& key_params, uint8_t* key, size_t len);
  119. const std::string content_name_;
  120. std::unique_ptr<cricket::SrtpSession> send_session_;
  121. std::unique_ptr<cricket::SrtpSession> recv_session_;
  122. std::unique_ptr<cricket::SrtpSession> send_rtcp_session_;
  123. std::unique_ptr<cricket::SrtpSession> recv_rtcp_session_;
  124. absl::optional<cricket::CryptoParams> send_params_;
  125. absl::optional<cricket::CryptoParams> recv_params_;
  126. absl::optional<int> send_cipher_suite_;
  127. absl::optional<int> recv_cipher_suite_;
  128. rtc::ZeroOnFreeBuffer<uint8_t> send_key_;
  129. rtc::ZeroOnFreeBuffer<uint8_t> recv_key_;
  130. bool writable_ = false;
  131. bool external_auth_enabled_ = false;
  132. int rtp_abs_sendtime_extn_id_ = -1;
  133. int decryption_failure_count_ = 0;
  134. };
  135. } // namespace webrtc
  136. #endif // PC_SRTP_TRANSPORT_H_