srtp_session.h 5.0 KB

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