提交 6938a76d authored 作者: Iñaki Baz Castillo's avatar Iñaki Baz Castillo

PipeTransport with SRTP may be done (testing needed!)

上级 010c66c6
#ifndef MS_RTC_PIPE_TRANSPORT_HPP
#define MS_RTC_PIPE_TRANSPORT_HPP
#include "RTC/SrtpSession.hpp"
#include "RTC/Transport.hpp"
#include "RTC/TransportTuple.hpp"
#include "RTC/UdpSocket.hpp"
......@@ -26,6 +27,7 @@ namespace RTC
void HandleRequest(Channel::Request* request) override;
private:
bool HasSrtp() const;
bool IsConnected() const override;
void SendRtpPacket(RTC::RtpPacket* packet, RTC::Transport::onSendCallback* cb = nullptr) override;
void SendRtcpPacket(RTC::RTCP::Packet* packet) override;
......@@ -45,6 +47,8 @@ namespace RTC
// Allocated by this.
RTC::UdpSocket* udpSocket{ nullptr };
RTC::TransportTuple* tuple{ nullptr };
RTC::SrtpSession* srtpRecvSession{ nullptr };
RTC::SrtpSession* srtpSendSession{ nullptr };
// Others.
ListenIp listenIp;
bool rtx{ false };
......
......@@ -5,12 +5,15 @@
#include "Logger.hpp"
#include "MediaSoupErrors.hpp"
#include "Utils.hpp"
#include <cstring> // std::memcpy()
namespace RTC
{
/* Static. */
// If SRTP is enabled we mandate AES_CM_128_HMAC_SHA1_80.
// AES-HMAC: http://tools.ietf.org/html/rfc3711
static RTC::SrtpSession::Profile SrtpProfile{ RTC::SrtpSession::Profile::AES_CM_128_HMAC_SHA1_80 };
static constexpr size_t SrtpMasterLength{ 30 };
/* Instance methods. */
......@@ -93,6 +96,12 @@ namespace RTC
delete this->tuple;
this->tuple = nullptr;
delete this->srtpSendSession;
this->srtpSendSession = nullptr;
delete this->srtpRecvSession;
this->srtpRecvSession = nullptr;
}
void PipeTransport::FillJson(json& jsonObject) const
......@@ -106,7 +115,7 @@ namespace RTC
jsonObject["rtx"] = this->rtx;
// Add srtpKey.
if (!this->srtpKey.empty())
if (HasSrtp())
jsonObject["srtpKey"] = this->srtpKey;
// Add tuple.
......@@ -203,13 +212,13 @@ namespace RTC
auto jsonSrtpKeyIt = request->data.find("srtpKey");
if (this->srtpKey.empty() && jsonSrtpKeyIt != request->data.end())
if (!HasSrtp() && jsonSrtpKeyIt != request->data.end())
{
MS_THROW_TYPE_ERROR("invalid srtpKey (SRTP not enabled locally)");
}
// clang-format off
else if (
!this->srtpKey.empty() &&
HasSrtp() &&
(jsonSrtpKeyIt == request->data.end() || !jsonSrtpKeyIt->is_string())
)
// clang-format on
......@@ -217,12 +226,47 @@ namespace RTC
MS_THROW_TYPE_ERROR("missing srtpKey (SRTP enabled locally)");
}
if (!this->srtpKey.empty())
if (HasSrtp())
{
auto remoteSrtpKey = jsonSrtpKeyIt->get<std::string>();
auto srtpKey = jsonSrtpKeyIt->get<std::string>();
if (remoteSrtpKey.size() != SrtpMasterLength)
if (srtpKey.size() != SrtpMasterLength)
MS_THROW_TYPE_ERROR("invalid srtpKey length");
auto* srtpLocalKey = new uint8_t[SrtpMasterLength];
auto* srtpRemoteKey = new uint8_t[SrtpMasterLength];
std::memcpy(srtpLocalKey, this->srtpKey.c_str(), SrtpMasterLength);
std::memcpy(srtpRemoteKey, srtpKey.c_str(), SrtpMasterLength);
try
{
this->srtpSendSession = new RTC::SrtpSession(
RTC::SrtpSession::Type::OUTBOUND, SrtpProfile, srtpLocalKey, SrtpMasterLength);
}
catch (const MediaSoupError& error)
{
delete[] srtpLocalKey;
delete[] srtpRemoteKey;
MS_THROW_ERROR("error creating SRTP sending session: %s", error.what());
}
try
{
this->srtpRecvSession = new RTC::SrtpSession(
RTC::SrtpSession::Type::INBOUND, SrtpProfile, srtpRemoteKey, SrtpMasterLength);
}
catch (const MediaSoupError& error)
{
delete[] srtpLocalKey;
delete[] srtpRemoteKey;
MS_THROW_ERROR("error creating SRTP receiving session: %s", error.what());
}
delete[] srtpLocalKey;
delete[] srtpRemoteKey;
}
int err;
......@@ -270,11 +314,14 @@ namespace RTC
}
catch (const MediaSoupError& error)
{
if (this->tuple != nullptr)
{
delete this->tuple;
this->tuple = nullptr;
}
delete this->srtpSendSession;
this->srtpSendSession = nullptr;
delete this->srtpRecvSession;
this->srtpRecvSession = nullptr;
delete this->tuple;
this->tuple = nullptr;
throw;
}
......@@ -301,6 +348,11 @@ namespace RTC
}
}
inline bool PipeTransport::HasSrtp() const
{
return !this->srtpKey.empty();
}
inline bool PipeTransport::IsConnected() const
{
return this->tuple != nullptr;
......@@ -325,6 +377,18 @@ namespace RTC
const uint8_t* data = packet->GetData();
size_t len = packet->GetSize();
if (HasSrtp() && !this->srtpSendSession->EncryptRtp(&data, &len))
{
if (cb)
{
(*cb)(false);
delete cb;
}
return;
}
this->tuple->Send(data, len, cb);
// Increase send transmission.
......@@ -341,6 +405,9 @@ namespace RTC
const uint8_t* data = packet->GetData();
size_t len = packet->GetSize();
if (HasSrtp() && !this->srtpSendSession->EncryptRtcp(&data, &len))
return;
this->tuple->Send(data, len);
// Increase send transmission.
......@@ -357,6 +424,9 @@ namespace RTC
const uint8_t* data = packet->GetData();
size_t len = packet->GetSize();
if (HasSrtp() && !this->srtpSendSession->EncryptRtcp(&data, &len))
return;
this->tuple->Send(data, len);
// Increase send transmission.
......@@ -419,6 +489,30 @@ namespace RTC
return;
}
// Decrypt the SRTP packet.
if (HasSrtp() && !this->srtpRecvSession->DecryptSrtp(const_cast<uint8_t*>(data), &len))
{
RTC::RtpPacket* packet = RTC::RtpPacket::Parse(data, len);
if (!packet)
{
MS_WARN_TAG(srtp, "DecryptSrtp() failed due to an invalid RTP packet");
}
else
{
MS_WARN_TAG(
srtp,
"DecryptSrtp() failed [ssrc:%" PRIu32 ", payloadType:%" PRIu8 ", seq:%" PRIu16 "]",
packet->GetSsrc(),
packet->GetPayloadType(),
packet->GetSequenceNumber());
delete packet;
}
return;
}
RTC::RtpPacket* packet = RTC::RtpPacket::Parse(data, len);
if (packet == nullptr)
......@@ -448,6 +542,10 @@ namespace RTC
return;
}
// Decrypt the SRTCP packet.
if (HasSrtp() && !this->srtpRecvSession->DecryptSrtcp(const_cast<uint8_t*>(data), &len))
return;
RTC::RTCP::Packet* packet = RTC::RTCP::Packet::Parse(data, len);
if (packet == nullptr)
......
......@@ -384,17 +384,11 @@ namespace RTC
}
catch (const MediaSoupError& error)
{
if (this->tuple != nullptr)
{
delete this->tuple;
this->tuple = nullptr;
}
delete this->tuple;
this->tuple = nullptr;
if (this->rtcpTuple != nullptr)
{
delete this->rtcpTuple;
this->rtcpTuple = nullptr;
}
delete this->rtcpTuple;
this->rtcpTuple = nullptr;
throw;
}
......
......@@ -247,11 +247,11 @@ namespace RTC
this->iceCandidates.clear();
delete this->srtpRecvSession;
this->srtpRecvSession = nullptr;
delete this->srtpSendSession;
this->srtpSendSession = nullptr;
delete this->srtpRecvSession;
this->srtpRecvSession = nullptr;
}
void WebRtcTransport::FillJson(json& jsonObject) const
......@@ -1205,16 +1205,11 @@ namespace RTC
MS_DEBUG_TAG(dtls, "DTLS connected");
// Close it if it was already set and update it.
if (this->srtpSendSession)
{
delete this->srtpSendSession;
this->srtpSendSession = nullptr;
}
if (this->srtpRecvSession)
{
delete this->srtpRecvSession;
this->srtpRecvSession = nullptr;
}
delete this->srtpSendSession;
this->srtpSendSession = nullptr;
delete this->srtpRecvSession;
this->srtpRecvSession = nullptr;
try
{
......@@ -1230,6 +1225,17 @@ namespace RTC
{
this->srtpRecvSession = new RTC::SrtpSession(
RTC::SrtpSession::Type::INBOUND, srtpProfile, srtpRemoteKey, srtpRemoteKeyLen);
// Notify the Node WebRtcTransport.
json data = json::object();
data["dtlsState"] = "connected";
data["dtlsRemoteCert"] = remoteCert;
Channel::Notifier::Emit(this->id, "dtlsstatechange", data);
// Tell the parent class.
RTC::Transport::Connected();
}
catch (const MediaSoupError& error)
{
......@@ -1238,17 +1244,6 @@ namespace RTC
delete this->srtpSendSession;
this->srtpSendSession = nullptr;
}
// Notify the Node WebRtcTransport.
json data = json::object();
data["dtlsState"] = "connected";
data["dtlsRemoteCert"] = remoteCert;
Channel::Notifier::Emit(this->id, "dtlsstatechange", data);
// Tell the parent class.
RTC::Transport::Connected();
}
inline void WebRtcTransport::OnDtlsTransportFailed(const RTC::DtlsTransport* /*dtlsTransport*/)
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论