Skip to content

Commit

Permalink
C++: Update libsrtp (#1314)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmillan authored Jan 16, 2024
1 parent d17a937 commit e226f2f
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 85 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

### NEXT

- C++: Update libsrtp to v3.0-alpha.

### 3.13.16

- Node: Add new `worker.on('subprocessclose')` event ([PR #1307](https://github.com/versatica/mediasoup/pull/1307)).
Expand Down
10 changes: 5 additions & 5 deletions worker/include/RTC/SrtpSession.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ namespace RTC
~SrtpSession();

public:
bool EncryptRtp(const uint8_t** data, int* len);
bool DecryptSrtp(uint8_t* data, int* len);
bool EncryptRtcp(const uint8_t** data, int* len);
bool DecryptSrtcp(uint8_t* data, int* len);
bool EncryptRtp(const uint8_t** data, size_t* len);
bool DecryptSrtp(uint8_t* data, size_t* len);
bool EncryptRtcp(const uint8_t** data, size_t* len);
bool DecryptSrtcp(uint8_t* data, size_t* len);
void RemoveStream(uint32_t ssrc)
{
srtp_remove_stream(this->session, uint32_t{ htonl(ssrc) });
srtp_stream_remove(this->session, uint32_t{ htonl(ssrc) });
}

private:
Expand Down
32 changes: 11 additions & 21 deletions worker/src/RTC/PipeTransport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -484,9 +484,9 @@ namespace RTC
}

const uint8_t* data = packet->GetData();
auto intLen = static_cast<int>(packet->GetSize());
auto len = packet->GetSize();

if (HasSrtp() && !this->srtpSendSession->EncryptRtp(&data, &intLen))
if (HasSrtp() && !this->srtpSendSession->EncryptRtp(&data, &len))
{
if (cb)
{
Expand All @@ -497,8 +497,6 @@ namespace RTC
return;
}

auto len = static_cast<size_t>(intLen);

this->tuple->Send(data, len, cb);

// Increase send transmission.
Expand All @@ -515,15 +513,13 @@ namespace RTC
}

const uint8_t* data = packet->GetData();
auto intLen = static_cast<int>(packet->GetSize());
auto len = packet->GetSize();

if (HasSrtp() && !this->srtpSendSession->EncryptRtcp(&data, &intLen))
if (HasSrtp() && !this->srtpSendSession->EncryptRtcp(&data, &len))
{
return;
}

auto len = static_cast<size_t>(intLen);

this->tuple->Send(data, len);

// Increase send transmission.
Expand All @@ -542,15 +538,13 @@ namespace RTC
packet->Serialize(RTC::RTCP::Buffer);

const uint8_t* data = packet->GetData();
auto intLen = static_cast<int>(packet->GetSize());
auto len = packet->GetSize();

if (HasSrtp() && !this->srtpSendSession->EncryptRtcp(&data, &intLen))
if (HasSrtp() && !this->srtpSendSession->EncryptRtcp(&data, &len))
{
return;
}

auto len = static_cast<size_t>(intLen);

this->tuple->Send(data, len);

// Increase send transmission.
Expand Down Expand Up @@ -638,11 +632,9 @@ namespace RTC
}

// Decrypt the SRTP packet.
auto intLen = static_cast<int>(len);

if (HasSrtp() && !this->srtpRecvSession->DecryptSrtp(const_cast<uint8_t*>(data), &intLen))
if (HasSrtp() && !this->srtpRecvSession->DecryptSrtp(const_cast<uint8_t*>(data), &len))
{
RTC::RtpPacket* packet = RTC::RtpPacket::Parse(data, static_cast<size_t>(intLen));
RTC::RtpPacket* packet = RTC::RtpPacket::Parse(data, len);

if (!packet)
{
Expand All @@ -663,7 +655,7 @@ namespace RTC
return;
}

RTC::RtpPacket* packet = RTC::RtpPacket::Parse(data, static_cast<size_t>(intLen));
RTC::RtpPacket* packet = RTC::RtpPacket::Parse(data, len);

if (!packet)
{
Expand Down Expand Up @@ -700,9 +692,7 @@ namespace RTC
}

// Decrypt the SRTCP packet.
auto intLen = static_cast<int>(len);

if (HasSrtp() && !this->srtpRecvSession->DecryptSrtcp(const_cast<uint8_t*>(data), &intLen))
if (HasSrtp() && !this->srtpRecvSession->DecryptSrtcp(const_cast<uint8_t*>(data), &len))
{
return;
}
Expand All @@ -715,7 +705,7 @@ namespace RTC
return;
}

RTC::RTCP::Packet* packet = RTC::RTCP::Packet::Parse(data, static_cast<size_t>(intLen));
RTC::RTCP::Packet* packet = RTC::RTCP::Packet::Parse(data, len);

if (!packet)
{
Expand Down
32 changes: 11 additions & 21 deletions worker/src/RTC/PlainTransport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -764,9 +764,9 @@ namespace RTC
}

const uint8_t* data = packet->GetData();
auto intLen = static_cast<int>(packet->GetSize());
auto len = packet->GetSize();

if (HasSrtp() && !this->srtpSendSession->EncryptRtp(&data, &intLen))
if (HasSrtp() && !this->srtpSendSession->EncryptRtp(&data, &len))
{
if (cb)
{
Expand All @@ -777,8 +777,6 @@ namespace RTC
return;
}

auto len = static_cast<size_t>(intLen);

this->tuple->Send(data, len, cb);

// Increase send transmission.
Expand All @@ -795,15 +793,13 @@ namespace RTC
}

const uint8_t* data = packet->GetData();
auto intLen = static_cast<int>(packet->GetSize());
auto len = packet->GetSize();

if (HasSrtp() && !this->srtpSendSession->EncryptRtcp(&data, &intLen))
if (HasSrtp() && !this->srtpSendSession->EncryptRtcp(&data, &len))
{
return;
}

auto len = static_cast<size_t>(intLen);

if (this->rtcpMux)
{
this->tuple->Send(data, len);
Expand All @@ -829,15 +825,13 @@ namespace RTC
packet->Serialize(RTC::RTCP::Buffer);

const uint8_t* data = packet->GetData();
auto intLen = static_cast<int>(packet->GetSize());
auto len = packet->GetSize();

if (HasSrtp() && !this->srtpSendSession->EncryptRtcp(&data, &intLen))
if (HasSrtp() && !this->srtpSendSession->EncryptRtcp(&data, &len))
{
return;
}

auto len = static_cast<size_t>(intLen);

if (this->rtcpMux)
{
this->tuple->Send(data, len);
Expand Down Expand Up @@ -933,11 +927,9 @@ namespace RTC
}

// Decrypt the SRTP packet.
auto intLen = static_cast<int>(len);

if (HasSrtp() && !this->srtpRecvSession->DecryptSrtp(const_cast<uint8_t*>(data), &intLen))
if (HasSrtp() && !this->srtpRecvSession->DecryptSrtp(const_cast<uint8_t*>(data), &len))
{
RTC::RtpPacket* packet = RTC::RtpPacket::Parse(data, static_cast<size_t>(intLen));
RTC::RtpPacket* packet = RTC::RtpPacket::Parse(data, len);

if (!packet)
{
Expand All @@ -958,7 +950,7 @@ namespace RTC
return;
}

RTC::RtpPacket* packet = RTC::RtpPacket::Parse(data, static_cast<size_t>(intLen));
RTC::RtpPacket* packet = RTC::RtpPacket::Parse(data, len);

if (!packet)
{
Expand Down Expand Up @@ -1031,9 +1023,7 @@ namespace RTC
}

// Decrypt the SRTCP packet.
auto intLen = static_cast<int>(len);

if (HasSrtp() && !this->srtpRecvSession->DecryptSrtcp(const_cast<uint8_t*>(data), &intLen))
if (HasSrtp() && !this->srtpRecvSession->DecryptSrtcp(const_cast<uint8_t*>(data), &len))
{
return;
}
Expand Down Expand Up @@ -1107,7 +1097,7 @@ namespace RTC
return;
}

RTC::RTCP::Packet* packet = RTC::RTCP::Packet::Parse(data, static_cast<size_t>(intLen));
RTC::RTCP::Packet* packet = RTC::RTCP::Packet::Parse(data, len);

if (!packet)
{
Expand Down
25 changes: 12 additions & 13 deletions worker/src/RTC/SrtpSession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,14 +189,14 @@ namespace RTC
}
}

bool SrtpSession::EncryptRtp(const uint8_t** data, int* len)
bool SrtpSession::EncryptRtp(const uint8_t** data, size_t* len)
{
MS_TRACE();

// Ensure that the resulting SRTP packet fits into the encrypt buffer.
if (static_cast<size_t>(*len) + SRTP_MAX_TRAILER_LEN > EncryptBufferSize)
if (*len + SRTP_MAX_TRAILER_LEN > EncryptBufferSize)
{
MS_WARN_TAG(srtp, "cannot encrypt RTP packet, size too big (%i bytes)", *len);
MS_WARN_TAG(srtp, "cannot encrypt RTP packet, size too big (%zu bytes)", *len);

return false;
}
Expand Down Expand Up @@ -224,7 +224,7 @@ namespace RTC

std::memcpy(encryptBuffer, *data, *len);

const srtp_err_status_t err = srtp_protect(this->session, static_cast<void*>(encryptBuffer), len);
const srtp_err_status_t err = srtp_protect(this->session, encryptBuffer, len);

if (DepLibSRTP::IsError(err))
{
Expand All @@ -239,11 +239,11 @@ namespace RTC
return true;
}

bool SrtpSession::DecryptSrtp(uint8_t* data, int* len)
bool SrtpSession::DecryptSrtp(uint8_t* data, size_t* len)
{
MS_TRACE();

const srtp_err_status_t err = srtp_unprotect(this->session, static_cast<void*>(data), len);
const srtp_err_status_t err = srtp_unprotect(this->session, data, len);

if (DepLibSRTP::IsError(err))
{
Expand All @@ -255,22 +255,21 @@ namespace RTC
return true;
}

bool SrtpSession::EncryptRtcp(const uint8_t** data, int* len)
bool SrtpSession::EncryptRtcp(const uint8_t** data, size_t* len)
{
MS_TRACE();

// Ensure that the resulting SRTCP packet fits into the encrypt buffer.
if (static_cast<size_t>(*len) + SRTP_MAX_TRAILER_LEN > EncryptBufferSize)
if (*len + SRTP_MAX_TRAILER_LEN > EncryptBufferSize)
{
MS_WARN_TAG(srtp, "cannot encrypt RTCP packet, size too big (%i bytes)", *len);
MS_WARN_TAG(srtp, "cannot encrypt RTCP packet, size too big (%zu bytes)", *len);

return false;
}

std::memcpy(EncryptBuffer, *data, *len);

const srtp_err_status_t err =
srtp_protect_rtcp(this->session, static_cast<void*>(EncryptBuffer), len);
const srtp_err_status_t err = srtp_protect_rtcp(this->session, EncryptBuffer, len);

if (DepLibSRTP::IsError(err))
{
Expand All @@ -285,11 +284,11 @@ namespace RTC
return true;
}

bool SrtpSession::DecryptSrtcp(uint8_t* data, int* len)
bool SrtpSession::DecryptSrtcp(uint8_t* data, size_t* len)
{
MS_TRACE();

const srtp_err_status_t err = srtp_unprotect_rtcp(this->session, static_cast<void*>(data), len);
const srtp_err_status_t err = srtp_unprotect_rtcp(this->session, data, len);

if (DepLibSRTP::IsError(err))
{
Expand Down
Loading

0 comments on commit e226f2f

Please sign in to comment.