You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

131 lines
4.6 KiB

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