Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix wrong size_t* to int* conversion in 64bit Big-Endian hosts #637

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions worker/include/RTC/SrtpSession.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ namespace RTC
~SrtpSession();

public:
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);
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);
void RemoveStream(uint32_t ssrc)
{
srtp_remove_stream(this->session, uint32_t{ htonl(ssrc) });
Expand Down
32 changes: 21 additions & 11 deletions worker/src/RTC/PipeTransport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -449,9 +449,9 @@ namespace RTC
}

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

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

auto len = static_cast<size_t>(intLen);

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

// Increase send transmission.
Expand All @@ -476,11 +478,13 @@ namespace RTC
return;

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

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

auto len = static_cast<size_t>(intLen);

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

// Increase send transmission.
Expand All @@ -495,11 +499,13 @@ namespace RTC
return;

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

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

auto len = static_cast<size_t>(intLen);

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

// Increase send transmission.
Expand Down Expand Up @@ -583,9 +589,11 @@ namespace RTC
return;

// Decrypt the SRTP packet.
if (HasSrtp() && !this->srtpRecvSession->DecryptSrtp(const_cast<uint8_t*>(data), &len))
auto intLen = static_cast<int>(len);

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

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

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

if (!packet)
{
Expand Down Expand Up @@ -641,7 +649,9 @@ namespace RTC
return;

// Decrypt the SRTCP packet.
if (HasSrtp() && !this->srtpRecvSession->DecryptSrtcp(const_cast<uint8_t*>(data), &len))
auto intLen = static_cast<int>(len);

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

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

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

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

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

auto len = static_cast<size_t>(intLen);

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

// Increase send transmission.
Expand All @@ -661,11 +663,13 @@ namespace RTC
return;

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

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

auto len = static_cast<size_t>(intLen);

if (this->rtcpMux)
this->tuple->Send(data, len);
else if (this->rtcpTuple)
Expand All @@ -683,11 +687,13 @@ namespace RTC
return;

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

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

auto len = static_cast<size_t>(intLen);

if (this->rtcpMux)
this->tuple->Send(data, len);
else if (this->rtcpTuple)
Expand Down Expand Up @@ -775,9 +781,11 @@ namespace RTC
return;

// Decrypt the SRTP packet.
if (HasSrtp() && !this->srtpRecvSession->DecryptSrtp(const_cast<uint8_t*>(data), &len))
auto intLen = static_cast<int>(len);

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

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

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

if (!packet)
{
Expand Down Expand Up @@ -871,7 +879,9 @@ namespace RTC
return;

// Decrypt the SRTCP packet.
if (HasSrtp() && !this->srtpRecvSession->DecryptSrtcp(const_cast<uint8_t*>(data), &len))
auto intLen = static_cast<int>(len);

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

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

if (!packet)
{
Expand Down
28 changes: 12 additions & 16 deletions worker/src/RTC/SrtpSession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,22 +146,21 @@ namespace RTC
}
}

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

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

return false;
}

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

srtp_err_status_t err =
srtp_protect(this->session, static_cast<void*>(EncryptBuffer), reinterpret_cast<int*>(len));
srtp_err_status_t err = srtp_protect(this->session, static_cast<void*>(EncryptBuffer), len);

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

bool SrtpSession::DecryptSrtp(uint8_t* data, size_t* 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), len);

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

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

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

return false;
}

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

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

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

bool SrtpSession::DecryptSrtcp(uint8_t* data, size_t* 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), len);

if (DepLibSRTP::IsError(err))
{
Expand Down
32 changes: 21 additions & 11 deletions worker/src/RTC/WebRtcTransport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -751,9 +751,9 @@ namespace RTC
}

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

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

auto len = static_cast<size_t>(intLen);

this->iceServer->GetSelectedTuple()->Send(data, len, cb);

// Increase send transmission.
Expand All @@ -778,7 +780,7 @@ namespace RTC
return;

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

// Ensure there is sending SRTP session.
if (!this->srtpSendSession)
Expand All @@ -788,9 +790,11 @@ namespace RTC
return;
}

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

auto len = static_cast<size_t>(intLen);

this->iceServer->GetSelectedTuple()->Send(data, len);

// Increase send transmission.
Expand All @@ -805,7 +809,7 @@ namespace RTC
return;

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

// Ensure there is sending SRTP session.
if (!this->srtpSendSession)
Expand All @@ -815,9 +819,11 @@ namespace RTC
return;
}

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

auto len = static_cast<size_t>(intLen);

this->iceServer->GetSelectedTuple()->Send(data, len);

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

// Decrypt the SRTP packet.
if (!this->srtpRecvSession->DecryptSrtp(const_cast<uint8_t*>(data), &len))
auto intLen = static_cast<int>(len);

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

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

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

if (!packet)
{
Expand Down Expand Up @@ -1053,10 +1061,12 @@ namespace RTC
}

// Decrypt the SRTCP packet.
if (!this->srtpRecvSession->DecryptSrtcp(const_cast<uint8_t*>(data), &len))
auto intLen = static_cast<int>(len);

if (!this->srtpRecvSession->DecryptSrtcp(const_cast<uint8_t*>(data), &intLen))
return;

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

if (!packet)
{
Expand Down