srtp_session.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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_SESSION_H_
  11. #define PC_SRTP_SESSION_H_
  12. #include <vector>
  13. #include "api/scoped_refptr.h"
  14. #include "rtc_base/thread_checker.h"
  15. // Forward declaration to avoid pulling in libsrtp headers here
  16. struct srtp_event_data_t;
  17. struct srtp_ctx_t_;
  18. namespace cricket {
  19. // Prohibits webrtc from initializing libsrtp. This can be used if libsrtp is
  20. // initialized by another library or explicitly. Note that this must be called
  21. // before creating an SRTP session with WebRTC.
  22. void ProhibitLibsrtpInitialization();
  23. // Class that wraps a libSRTP session.
  24. class SrtpSession {
  25. public:
  26. SrtpSession();
  27. ~SrtpSession();
  28. // Configures the session for sending data using the specified
  29. // cipher-suite and key. Receiving must be done by a separate session.
  30. bool SetSend(int cs,
  31. const uint8_t* key,
  32. size_t len,
  33. const std::vector<int>& extension_ids);
  34. bool UpdateSend(int cs,
  35. const uint8_t* key,
  36. size_t len,
  37. const std::vector<int>& extension_ids);
  38. // Configures the session for receiving data using the specified
  39. // cipher-suite and key. Sending must be done by a separate session.
  40. bool SetRecv(int cs,
  41. const uint8_t* key,
  42. size_t len,
  43. const std::vector<int>& extension_ids);
  44. bool UpdateRecv(int cs,
  45. const uint8_t* key,
  46. size_t len,
  47. const std::vector<int>& extension_ids);
  48. // Encrypts/signs an individual RTP/RTCP packet, in-place.
  49. // If an HMAC is used, this will increase the packet size.
  50. bool ProtectRtp(void* data, int in_len, int max_len, int* out_len);
  51. // Overloaded version, outputs packet index.
  52. bool ProtectRtp(void* data,
  53. int in_len,
  54. int max_len,
  55. int* out_len,
  56. int64_t* index);
  57. bool ProtectRtcp(void* data, int in_len, int max_len, int* out_len);
  58. // Decrypts/verifies an invidiual RTP/RTCP packet.
  59. // If an HMAC is used, this will decrease the packet size.
  60. bool UnprotectRtp(void* data, int in_len, int* out_len);
  61. bool UnprotectRtcp(void* data, int in_len, int* out_len);
  62. // Helper method to get authentication params.
  63. bool GetRtpAuthParams(uint8_t** key, int* key_len, int* tag_len);
  64. int GetSrtpOverhead() const;
  65. // If external auth is enabled, SRTP will write a dummy auth tag that then
  66. // later must get replaced before the packet is sent out. Only supported for
  67. // non-GCM cipher suites and can be checked through "IsExternalAuthActive"
  68. // if it is actually used. This method is only valid before the RTP params
  69. // have been set.
  70. void EnableExternalAuth();
  71. bool IsExternalAuthEnabled() const;
  72. // A SRTP session supports external creation of the auth tag if a non-GCM
  73. // cipher is used. This method is only valid after the RTP params have
  74. // been set.
  75. bool IsExternalAuthActive() const;
  76. private:
  77. bool DoSetKey(int type,
  78. int cs,
  79. const uint8_t* key,
  80. size_t len,
  81. const std::vector<int>& extension_ids);
  82. bool SetKey(int type,
  83. int cs,
  84. const uint8_t* key,
  85. size_t len,
  86. const std::vector<int>& extension_ids);
  87. bool UpdateKey(int type,
  88. int cs,
  89. const uint8_t* key,
  90. size_t len,
  91. const std::vector<int>& extension_ids);
  92. // Returns send stream current packet index from srtp db.
  93. bool GetSendStreamPacketIndex(void* data, int in_len, int64_t* index);
  94. // These methods are responsible for initializing libsrtp (if the usage count
  95. // is incremented from 0 to 1) or deinitializing it (when decremented from 1
  96. // to 0).
  97. //
  98. // Returns true if successful (will always be successful if already inited).
  99. static bool IncrementLibsrtpUsageCountAndMaybeInit();
  100. static void DecrementLibsrtpUsageCountAndMaybeDeinit();
  101. void HandleEvent(const srtp_event_data_t* ev);
  102. static void HandleEventThunk(srtp_event_data_t* ev);
  103. rtc::ThreadChecker thread_checker_;
  104. srtp_ctx_t_* session_ = nullptr;
  105. int rtp_auth_tag_len_ = 0;
  106. int rtcp_auth_tag_len_ = 0;
  107. bool inited_ = false;
  108. static rtc::GlobalLock lock_;
  109. int last_send_seq_num_ = -1;
  110. bool external_auth_active_ = false;
  111. bool external_auth_enabled_ = false;
  112. int decryption_failure_count_ = 0;
  113. RTC_DISALLOW_COPY_AND_ASSIGN(SrtpSession);
  114. };
  115. } // namespace cricket
  116. #endif // PC_SRTP_SESSION_H_