mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2024-11-24 19:50:38 +08:00
避免对外传递 srtp 依赖
This commit is contained in:
parent
388bf71813
commit
0fc38bbadb
@ -20,15 +20,20 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
// #define MS_LOG_DEV_LEVEL 3
|
||||
|
||||
#include "SrtpSession.hpp"
|
||||
#include <cstring> // std::memset(), std::memcpy()
|
||||
#include "logger.h"
|
||||
#include "Util/util.h"
|
||||
|
||||
#include "Util/logger.h"
|
||||
#include "Util/util.h"
|
||||
#include "logger.h"
|
||||
|
||||
#include <srtp2/srtp.h>
|
||||
|
||||
#include <cstring> // std::memset(), std::memcpy()
|
||||
#include <vector>
|
||||
|
||||
using namespace toolkit;
|
||||
|
||||
namespace RTC
|
||||
{
|
||||
namespace RTC {
|
||||
|
||||
/* Static. */
|
||||
|
||||
static std::vector<const char *> errors = {
|
||||
@ -57,11 +62,24 @@ namespace RTC
|
||||
"error parsing data (srtp_err_status_parse_err)",
|
||||
"error encoding data (srtp_err_status_encode_err)",
|
||||
"error while using semaphores (srtp_err_status_semaphore_err)",
|
||||
"error while using pfkey (srtp_err_status_pfkey_err)"};
|
||||
// clang-format on
|
||||
"error while using pfkey (srtp_err_status_pfkey_err)"
|
||||
};
|
||||
|
||||
/* Static methods. */
|
||||
|
||||
class DepLibSRTP : public std::enable_shared_from_this<DepLibSRTP> {
|
||||
public:
|
||||
using Ptr = std::shared_ptr<DepLibSRTP>;
|
||||
~DepLibSRTP();
|
||||
|
||||
static bool IsError(srtp_err_status_t code);
|
||||
static const char *GetErrorString(srtp_err_status_t code);
|
||||
static DepLibSRTP &Instance();
|
||||
|
||||
private:
|
||||
DepLibSRTP();
|
||||
};
|
||||
|
||||
const char *DepLibSRTP::GetErrorString(srtp_err_status_t code) {
|
||||
// This throws out_of_range if the given index is not in the vector.
|
||||
return errors.at(code);
|
||||
@ -104,8 +122,7 @@ namespace RTC
|
||||
// Set libsrtp event handler.
|
||||
err = srtp_install_event_handler([](srtp_event_data_t *data) {
|
||||
MS_TRACE();
|
||||
switch (data->event)
|
||||
{
|
||||
switch (data->event) {
|
||||
case event_ssrc_collision:
|
||||
MS_WARN_TAG(srtp, "SSRC collision occurred");
|
||||
break;
|
||||
@ -124,8 +141,7 @@ namespace RTC
|
||||
}
|
||||
});
|
||||
|
||||
if (DepLibSRTP::IsError(err))
|
||||
{
|
||||
if (DepLibSRTP::IsError(err)) {
|
||||
MS_THROW_ERROR("srtp_install_event_handler() failed: %s", DepLibSRTP::GetErrorString(err));
|
||||
}
|
||||
}
|
||||
@ -139,8 +155,7 @@ namespace RTC
|
||||
|
||||
/* Instance methods. */
|
||||
|
||||
SrtpSession::SrtpSession(Type type, CryptoSuite cryptoSuite, uint8_t* key, size_t keyLen)
|
||||
{
|
||||
SrtpSession::SrtpSession(Type type, CryptoSuite cryptoSuite, uint8_t *key, size_t keyLen) {
|
||||
_env = DepLibSRTP::Instance().shared_from_this();
|
||||
MS_TRACE();
|
||||
|
||||
@ -149,18 +164,15 @@ namespace RTC
|
||||
// Set all policy fields to 0.
|
||||
std::memset(&policy, 0, sizeof(srtp_policy_t));
|
||||
|
||||
switch (cryptoSuite)
|
||||
{
|
||||
case CryptoSuite::AES_CM_128_HMAC_SHA1_80:
|
||||
{
|
||||
switch (cryptoSuite) {
|
||||
case CryptoSuite::AES_CM_128_HMAC_SHA1_80: {
|
||||
srtp_crypto_policy_set_aes_cm_128_hmac_sha1_80(&policy.rtp);
|
||||
srtp_crypto_policy_set_aes_cm_128_hmac_sha1_80(&policy.rtcp);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case CryptoSuite::AES_CM_128_HMAC_SHA1_32:
|
||||
{
|
||||
case CryptoSuite::AES_CM_128_HMAC_SHA1_32: {
|
||||
srtp_crypto_policy_set_aes_cm_128_hmac_sha1_32(&policy.rtp);
|
||||
// NOTE: Must be 80 for RTCP.
|
||||
srtp_crypto_policy_set_aes_cm_128_hmac_sha1_80(&policy.rtcp);
|
||||
@ -168,34 +180,28 @@ namespace RTC
|
||||
break;
|
||||
}
|
||||
|
||||
case CryptoSuite::AEAD_AES_256_GCM:
|
||||
{
|
||||
case CryptoSuite::AEAD_AES_256_GCM: {
|
||||
srtp_crypto_policy_set_aes_gcm_256_16_auth(&policy.rtp);
|
||||
srtp_crypto_policy_set_aes_gcm_256_16_auth(&policy.rtcp);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case CryptoSuite::AEAD_AES_128_GCM:
|
||||
{
|
||||
case CryptoSuite::AEAD_AES_128_GCM: {
|
||||
srtp_crypto_policy_set_aes_gcm_128_16_auth(&policy.rtp);
|
||||
srtp_crypto_policy_set_aes_gcm_128_16_auth(&policy.rtcp);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
{
|
||||
default: {
|
||||
MS_ABORT("unknown SRTP crypto suite");
|
||||
}
|
||||
}
|
||||
|
||||
MS_ASSERT(
|
||||
(int)keyLen == policy.rtp.cipher_key_len,
|
||||
"given keyLen does not match policy.rtp.cipher_keyLen");
|
||||
MS_ASSERT((int)keyLen == policy.rtp.cipher_key_len, "given keyLen does not match policy.rtp.cipher_keyLen");
|
||||
|
||||
switch (type)
|
||||
{
|
||||
switch (type) {
|
||||
case Type::INBOUND:
|
||||
policy.ssrc.type = ssrc_any_inbound;
|
||||
break;
|
||||
@ -219,12 +225,10 @@ namespace RTC
|
||||
MS_THROW_ERROR("srtp_create() failed: %s", DepLibSRTP::GetErrorString(err));
|
||||
}
|
||||
|
||||
SrtpSession::~SrtpSession()
|
||||
{
|
||||
SrtpSession::~SrtpSession() {
|
||||
MS_TRACE();
|
||||
|
||||
if (this->session != nullptr)
|
||||
{
|
||||
if (this->session != nullptr) {
|
||||
srtp_err_status_t err = srtp_dealloc(this->session);
|
||||
|
||||
if (DepLibSRTP::IsError(err))
|
||||
@ -232,14 +236,11 @@ namespace RTC
|
||||
}
|
||||
}
|
||||
|
||||
bool SrtpSession::EncryptRtp(uint8_t* data, int* len)
|
||||
{
|
||||
bool SrtpSession::EncryptRtp(uint8_t *data, int *len) {
|
||||
MS_TRACE();
|
||||
srtp_err_status_t err =
|
||||
srtp_protect(this->session, static_cast<void*>(data), reinterpret_cast<int*>(len));
|
||||
srtp_err_status_t err = srtp_protect(this->session, static_cast<void *>(data), reinterpret_cast<int *>(len));
|
||||
|
||||
if (DepLibSRTP::IsError(err))
|
||||
{
|
||||
if (DepLibSRTP::IsError(err)) {
|
||||
WarnL << "srtp_protect() failed:" << DepLibSRTP::GetErrorString(err);
|
||||
return false;
|
||||
}
|
||||
@ -247,15 +248,12 @@ namespace RTC
|
||||
return true;
|
||||
}
|
||||
|
||||
bool SrtpSession::DecryptSrtp(uint8_t* data, int* len)
|
||||
{
|
||||
bool SrtpSession::DecryptSrtp(uint8_t *data, int *len) {
|
||||
MS_TRACE();
|
||||
|
||||
srtp_err_status_t err =
|
||||
srtp_unprotect(this->session, static_cast<void*>(data), reinterpret_cast<int*>(len));
|
||||
srtp_err_status_t err = srtp_unprotect(this->session, static_cast<void *>(data), reinterpret_cast<int *>(len));
|
||||
|
||||
if (DepLibSRTP::IsError(err))
|
||||
{
|
||||
if (DepLibSRTP::IsError(err)) {
|
||||
WarnL << "srtp_unprotect() failed:" << DepLibSRTP::GetErrorString(err);
|
||||
return false;
|
||||
}
|
||||
@ -263,14 +261,11 @@ namespace RTC
|
||||
return true;
|
||||
}
|
||||
|
||||
bool SrtpSession::EncryptRtcp(uint8_t* data, int* len)
|
||||
{
|
||||
bool SrtpSession::EncryptRtcp(uint8_t *data, int *len) {
|
||||
MS_TRACE();
|
||||
srtp_err_status_t err = srtp_protect_rtcp(
|
||||
this->session, static_cast<void*>(data), reinterpret_cast<int*>(len));
|
||||
srtp_err_status_t err = srtp_protect_rtcp(this->session, static_cast<void *>(data), reinterpret_cast<int *>(len));
|
||||
|
||||
if (DepLibSRTP::IsError(err))
|
||||
{
|
||||
if (DepLibSRTP::IsError(err)) {
|
||||
WarnL << "srtp_protect_rtcp() failed:" << DepLibSRTP::GetErrorString(err);
|
||||
return false;
|
||||
}
|
||||
@ -278,19 +273,21 @@ namespace RTC
|
||||
return true;
|
||||
}
|
||||
|
||||
bool SrtpSession::DecryptSrtcp(uint8_t* data, int* len)
|
||||
{
|
||||
bool SrtpSession::DecryptSrtcp(uint8_t *data, int *len) {
|
||||
MS_TRACE();
|
||||
|
||||
srtp_err_status_t err =
|
||||
srtp_unprotect_rtcp(this->session, static_cast<void*>(data), reinterpret_cast<int*>(len));
|
||||
srtp_err_status_t err = srtp_unprotect_rtcp(this->session, static_cast<void *>(data), reinterpret_cast<int *>(len));
|
||||
|
||||
if (DepLibSRTP::IsError(err))
|
||||
{
|
||||
if (DepLibSRTP::IsError(err)) {
|
||||
WarnL << "srtp_unprotect_rtcp() failed:" << DepLibSRTP::GetErrorString(err);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void SrtpSession::RemoveStream(uint32_t ssrc) {
|
||||
srtp_remove_stream(this->session, uint32_t { htonl(ssrc) });
|
||||
}
|
||||
|
||||
} // namespace RTC
|
||||
|
@ -20,31 +20,18 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
#define MS_RTC_SRTP_SESSION_HPP
|
||||
|
||||
#include "Utils.hpp"
|
||||
#include <srtp2/srtp.h>
|
||||
#include <vector>
|
||||
|
||||
#include <memory>
|
||||
|
||||
namespace RTC
|
||||
{
|
||||
class DepLibSRTP : public std::enable_shared_from_this<DepLibSRTP>
|
||||
{
|
||||
typedef struct srtp_ctx_t_ *srtp_t;
|
||||
|
||||
namespace RTC {
|
||||
|
||||
class DepLibSRTP;
|
||||
|
||||
class SrtpSession {
|
||||
public:
|
||||
using Ptr = std::shared_ptr<DepLibSRTP>;
|
||||
~DepLibSRTP();
|
||||
|
||||
static bool IsError(srtp_err_status_t code);
|
||||
static const char *GetErrorString(srtp_err_status_t code);
|
||||
static DepLibSRTP &Instance();
|
||||
|
||||
private:
|
||||
DepLibSRTP();
|
||||
};
|
||||
|
||||
class SrtpSession
|
||||
{
|
||||
public:
|
||||
enum class CryptoSuite
|
||||
{
|
||||
enum class CryptoSuite {
|
||||
NONE = 0,
|
||||
AES_CM_128_HMAC_SHA1_80 = 1,
|
||||
AES_CM_128_HMAC_SHA1_32,
|
||||
@ -53,11 +40,7 @@ namespace RTC
|
||||
};
|
||||
|
||||
public:
|
||||
enum class Type
|
||||
{
|
||||
INBOUND = 1,
|
||||
OUTBOUND
|
||||
};
|
||||
enum class Type { INBOUND = 1, OUTBOUND };
|
||||
|
||||
public:
|
||||
SrtpSession(Type type, CryptoSuite cryptoSuite, uint8_t *key, size_t keyLen);
|
||||
@ -68,16 +51,14 @@ namespace RTC
|
||||
bool DecryptSrtp(uint8_t *data, int *len);
|
||||
bool EncryptRtcp(uint8_t *data, int *len);
|
||||
bool DecryptSrtcp(uint8_t *data, int *len);
|
||||
void RemoveStream(uint32_t ssrc)
|
||||
{
|
||||
srtp_remove_stream(this->session, uint32_t{ htonl(ssrc) });
|
||||
}
|
||||
void RemoveStream(uint32_t ssrc);
|
||||
|
||||
private:
|
||||
// Allocated by this.
|
||||
srtp_t session { nullptr };
|
||||
DepLibSRTP::Ptr _env;
|
||||
std::shared_ptr<DepLibSRTP> _env;
|
||||
};
|
||||
|
||||
} // namespace RTC
|
||||
|
||||
#endif
|
||||
|
@ -13,6 +13,9 @@
|
||||
#include "Rtcp/RtcpFCI.h"
|
||||
#include "RtpExt.h"
|
||||
#include "Rtsp/RtpReceiver.h"
|
||||
|
||||
#include <srtp2/srtp.h>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#define RTP_SSRC_OFFSET 1
|
||||
|
Loading…
Reference in New Issue
Block a user