#define MS_CLASS "RTC::StunPacket"
#include "RTC/StunPacket.hpp"
#include "Logger.hpp"
#include "Utils.hpp"
#include <cstdio>
#include <cstring>
namespace RTC
{
const uint8_t StunPacket::MagicCookie[] = { 0x21, 0x12, 0xA4, 0x42 };
StunPacket* StunPacket::Parse(const uint8_t* data, size_t len)
{
MS_TRACE();
if (!StunPacket::IsStun(data, len))
{
return nullptr;
}
const uint16_t msgType = Utils::Byte::Get2Bytes(data, 0);
const uint16_t msgLength = Utils::Byte::Get2Bytes(data, 2);
if ((static_cast<size_t>(msgLength) != len - 20) || ((msgLength & 0x03) != 0))
{
MS_WARN_TAG(
ice,
"length field + 20 does not match total size (or it is not multiple of 4 bytes), "
"packet discarded");
return nullptr;
}
const uint16_t msgMethod =
(msgType & 0x000f) | ((msgType & 0x00e0) >> 1) | ((msgType & 0x3E00) >> 2);
const uint16_t msgClass = ((data[0] & 0x01) << 1) | ((data[1] & 0x10) >> 4);
auto* packet = new StunPacket(
static_cast<Class>(msgClass), static_cast<Method>(msgMethod), data + 8, data, len);
size_t pos{ 20 };
bool hasMessageIntegrity{ false };
bool hasFingerprint{ false };
size_t fingerprintAttrPos;
uint32_t fingerprint;
while (pos + 4 <= len)
{
auto attrType = static_cast<Attribute>(Utils::Byte::Get2Bytes(data, pos));
const uint16_t attrLength = Utils::Byte::Get2Bytes(data, pos + 2);
if ((pos + 4 + attrLength) > len)
{
MS_WARN_TAG(ice, "the attribute length exceeds the remaining size, packet discarded");
delete packet;
return nullptr;
}
if (hasFingerprint)
{
MS_WARN_TAG(ice, "attribute after FINGERPRINT is not allowed, packet discarded");
delete packet;
return nullptr;
}
if (hasMessageIntegrity && attrType != Attribute::FINGERPRINT)
{
MS_WARN_TAG(
ice,
"attribute after MESSAGE-INTEGRITY other than FINGERPRINT is not allowed, "
"packet discarded");
delete packet;
return nullptr;
}
const uint8_t* attrValuePos = data + pos + 4;
switch (attrType)
{
case Attribute::USERNAME:
{
packet->SetUsername(
reinterpret_cast<const char*>(attrValuePos), static_cast<size_t>(attrLength));
break;
}
case Attribute::PRIORITY:
{
if (attrLength != 4)
{
MS_WARN_TAG(ice, "attribute PRIORITY must be 4 bytes length, packet discarded");
delete packet;
return nullptr;
}
packet->SetPriority(Utils::Byte::Get4Bytes(attrValuePos, 0));
break;
}
case Attribute::ICE_CONTROLLING:
{
if (attrLength != 8)
{
MS_WARN_TAG(ice, "attribute ICE-CONTROLLING must be 8 bytes length, packet discarded");
delete packet;
return nullptr;
}
packet->SetIceControlling(Utils::Byte::Get8Bytes(attrValuePos, 0));
break;
}
case Attribute::ICE_CONTROLLED:
{
if (attrLength != 8)
{
MS_WARN_TAG(ice, "attribute ICE-CONTROLLED must be 8 bytes length, packet discarded");
delete packet;
return nullptr;
}
packet->SetIceControlled(Utils::Byte::Get8Bytes(attrValuePos, 0));
break;
}
case Attribute::USE_CANDIDATE:
{
if (attrLength != 0)
{
MS_WARN_TAG(ice, "attribute USE-CANDIDATE must be 0 bytes length, packet discarded");
delete packet;
return nullptr;
}
packet->SetUseCandidate();
break;
}
case Attribute::NOMINATION:
{
if (attrLength != 4)
{
MS_WARN_TAG(ice, "attribute NOMINATION must be 4 bytes length, packet discarded");
delete packet;
return nullptr;
}
packet->SetHasNomination();
packet->SetNomination(Utils::Byte::Get4Bytes(attrValuePos, 0));
break;
}
case Attribute::MESSAGE_INTEGRITY:
{
if (attrLength != 20)
{
MS_WARN_TAG(ice, "attribute MESSAGE-INTEGRITY must be 20 bytes length, packet discarded");
delete packet;
return nullptr;
}
hasMessageIntegrity = true;
packet->SetMessageIntegrity(attrValuePos);
break;
}
case Attribute::FINGERPRINT:
{
if (attrLength != 4)
{
MS_WARN_TAG(ice, "attribute FINGERPRINT must be 4 bytes length, packet discarded");
delete packet;
return nullptr;
}
hasFingerprint = true;
fingerprintAttrPos = pos;
fingerprint = Utils::Byte::Get4Bytes(attrValuePos, 0);
packet->SetFingerprint();
break;
}
case Attribute::ERROR_CODE:
{
if (attrLength < 4)
{
MS_WARN_TAG(ice, "attribute ERROR-CODE must be >= 4bytes length, packet discarded");
delete packet;
return nullptr;
}
const uint8_t errorClass = Utils::Byte::Get1Byte(attrValuePos, 2);
const uint8_t errorNumber = Utils::Byte::Get1Byte(attrValuePos, 3);
auto errorCode = static_cast<uint16_t>(errorClass * 100 + errorNumber);
packet->SetErrorCode(errorCode);
break;
}
case Attribute::SOFTWARE:
{
if (attrLength >= 763)
{
MS_WARN_TAG(
ice, "attribute SOFTWARE must be less than 763 bytes length, packet discarded");
delete packet;
return nullptr;
}
packet->SetSoftware(
reinterpret_cast<const char*>(attrValuePos), static_cast<size_t>(attrLength));
break;
}
default:;
}
pos = Utils::Byte::PadTo4Bytes(pos + 4 + attrLength);
}
if (pos != len)
{
MS_WARN_TAG(ice, "computed packet size does not match total size, packet discarded");
delete packet;
return nullptr;
}
if (hasFingerprint)
{
const uint32_t computedFingerprint =
Utils::Crypto::GetCRC32(data, fingerprintAttrPos) ^ 0x5354554e;
if (fingerprint != computedFingerprint)
{
MS_WARN_TAG(
ice,
"computed FINGERPRINT value does not match the value in the packet, "
"packet discarded");
delete packet;
return nullptr;
}
}
return packet;
}
StunPacket::StunPacket(
Class klass, Method method, const uint8_t* transactionId, const uint8_t* data, size_t size)
: klass(klass), method(method), transactionId(transactionId), data(const_cast<uint8_t*>(data)),
size(size)
{
MS_TRACE();
}
StunPacket::~StunPacket()
{
MS_TRACE();
}
void StunPacket::Dump() const
{
MS_TRACE();
MS_DUMP("<StunPacket>");
std::string klass;
switch (this->klass)
{
case Class::REQUEST:
klass = "request";
break;
case Class::INDICATION:
klass = "indication";
break;
case Class::SUCCESS_RESPONSE:
klass = "success response";
break;
case Class::ERROR_RESPONSE:
klass = "error response";
break;
}
if (this->method == Method::BINDING)
{
MS_DUMP(" Binding %s", klass.c_str());
}
else
{
MS_DUMP(" %s with unknown method %#.3x", klass.c_str(), static_cast<uint16_t>(this->method));
}
MS_DUMP(" size: %zu bytes", this->size);
auto transactionId1 = Utils::Byte::Get4Bytes(this->transactionId, 0);
auto transactionId2 = Utils::Byte::Get8Bytes(this->transactionId, 4);
MS_DUMP(" transactionId (first 4 bytes): %" PRIu32, transactionId1);
MS_DUMP(" transactionId (last 8 bytes): %" PRIu64, transactionId2);
if (this->errorCode != 0u)
{
MS_DUMP(" errorCode: %" PRIu16, this->errorCode);
}
if (!this->username.empty())
{
MS_DUMP(" username: %s", this->username.c_str());
}
if (this->priority != 0u)
{
MS_DUMP(" priority: %" PRIu32, this->priority);
}
if (this->iceControlling != 0u)
{
MS_DUMP(" iceControlling: %" PRIu64, this->iceControlling);
}
if (this->iceControlled != 0u)
{
MS_DUMP(" iceControlled: %" PRIu64, this->iceControlled);
}
if (this->hasUseCandidate)
{
MS_DUMP(" useCandidate: yes");
}
if (!this->software.empty())
{
MS_DUMP(" software: %s", this->software.c_str());
}
if (this->xorMappedAddress != nullptr)
{
int family;
uint16_t port;
std::string ip;
Utils::IP::GetAddressInfo(this->xorMappedAddress, family, ip, port);
MS_DUMP(" xorMappedAddress: %s : %" PRIu16, ip.c_str(), port);
}
if (this->messageIntegrity != nullptr)
{
char messageIntegrity[41];
for (int i{ 0 }; i < 20; ++i)
{
std::snprintf(messageIntegrity + (i * 2), 3, "%.2x", this->messageIntegrity[i]);
}
MS_DUMP(" messageIntegrity: %s", messageIntegrity);
}
if (this->hasFingerprint)
{
MS_DUMP(" fingerprint: yes");
}
MS_DUMP("</StunPacket>");
}
void StunPacket::SetPassword(const std::string& password)
{
if (this->klass == Class::ERROR_RESPONSE)
{
MS_ERROR("cannot set password for error responses");
return;
}
this->password = password;
}
StunPacket::Authentication StunPacket::CheckAuthentication(
const std::string& usernameFragment1, const std::string& password)
{
MS_TRACE();
switch (this->klass)
{
case Class::REQUEST:
case Class::INDICATION:
{
if (usernameFragment1.empty())
{
MS_WARN_TAG(ice, "usernameFragment1 not given, cannot authenticate request or indication");
return Authentication::BAD_MESSAGE;
}
if (this->username.empty())
{
MS_WARN_TAG(ice, "missing USERNAME attribute, cannot authenticate request or indication");
return Authentication::BAD_MESSAGE;
}
if (!this->messageIntegrity)
{
MS_WARN_TAG(
ice, "missing MESSAGE-INTEGRITY attribute, cannot authenticate request or indication");
return Authentication::BAD_MESSAGE;
}
const size_t usernameFragment1Len = usernameFragment1.length();
if (
this->username.length() <= usernameFragment1Len ||
this->username.at(usernameFragment1Len) != ':' ||
this->username.compare(0, usernameFragment1Len, usernameFragment1) != 0)
{
return Authentication::UNAUTHORIZED;
}
break;
}
case Class::SUCCESS_RESPONSE:
case Class::ERROR_RESPONSE:
{
if (!this->messageIntegrity)
{
MS_WARN_TAG(ice, "missing MESSAGE-INTEGRITY attribute, cannot authenticate response");
return Authentication::BAD_MESSAGE;
}
break;
}
default:
{
MS_WARN_TAG(
ice,
"unknown STUN class %" PRIu16 ", cannot authenticate",
static_cast<uint16_t>(this->klass));
return Authentication::BAD_MESSAGE;
}
}
if (this->hasFingerprint)
{
Utils::Byte::Set2Bytes(this->data, 2, static_cast<uint16_t>(this->size - 20 - 8));
}
const uint8_t* computedMessageIntegrity =
Utils::Crypto::GetHmacSha1(password, this->data, (this->messageIntegrity - 4) - this->data);
Authentication result;
if (std::memcmp(this->messageIntegrity, computedMessageIntegrity, 20) == 0)
{
result = Authentication::OK;
}
else
{
result = Authentication::UNAUTHORIZED;
}
if (this->hasFingerprint)
{
Utils::Byte::Set2Bytes(this->data, 2, static_cast<uint16_t>(this->size - 20));
}
return result;
}
StunPacket* StunPacket::CreateSuccessResponse()
{
MS_TRACE();
MS_ASSERT(
this->klass == Class::REQUEST,
"attempt to create a success response for a non request STUN packet");
return new StunPacket(Class::SUCCESS_RESPONSE, this->method, this->transactionId, nullptr, 0);
}
StunPacket* StunPacket::CreateErrorResponse(uint16_t errorCode)
{
MS_TRACE();
MS_ASSERT(
this->klass == Class::REQUEST,
"attempt to create an error response for a non request STUN packet");
auto* response =
new StunPacket(Class::ERROR_RESPONSE, this->method, this->transactionId, nullptr, 0);
response->SetErrorCode(errorCode);
return response;
}
void StunPacket::Serialize(uint8_t* buffer)
{
MS_TRACE();
uint16_t usernamePaddedLen{ 0 };
uint16_t xorMappedAddressPaddedLen{ 0 };
bool addXorMappedAddress =
((this->xorMappedAddress != nullptr) && this->method == StunPacket::Method::BINDING &&
this->klass == Class::SUCCESS_RESPONSE);
const bool addErrorCode = ((this->errorCode != 0u) && this->klass == Class::ERROR_RESPONSE);
const bool addMessageIntegrity =
(this->klass != Class::ERROR_RESPONSE && !this->password.empty());
const bool addFingerprint{ true };
this->data = buffer;
this->size = 20;
if (!this->username.empty())
{
usernamePaddedLen = Utils::Byte::PadTo4Bytes(this->username.length());
this->size += 4 + usernamePaddedLen;
}
if (this->priority != 0u)
{
this->size += 4 + 4;
}
if (this->iceControlling != 0u)
{
this->size += 4 + 8;
}
if (this->iceControlled != 0u)
{
this->size += 4 + 8;
}
if (this->hasUseCandidate)
{
this->size += 4;
}
if (addXorMappedAddress)
{
switch (this->xorMappedAddress->sa_family)
{
case AF_INET:
{
xorMappedAddressPaddedLen = 8;
this->size += 4 + 8;
break;
}
case AF_INET6:
{
xorMappedAddressPaddedLen = 20;
this->size += 4 + 20;
break;
}
default:
{
MS_ERROR("invalid inet family in XOR-MAPPED-ADDRESS attribute");
addXorMappedAddress = false;
}
}
}
if (addErrorCode)
{
this->size += 4 + 4;
}
if (addMessageIntegrity)
{
this->size += 4 + 20;
}
if (addFingerprint)
{
this->size += 4 + 4;
}
uint16_t typeField = (static_cast<uint16_t>(this->method) & 0x0f80) << 2;
typeField |= (static_cast<uint16_t>(this->method) & 0x0070) << 1;
typeField |= (static_cast<uint16_t>(this->method) & 0x000f);
typeField |= (static_cast<uint16_t>(this->klass) & 0x02) << 7;
typeField |= (static_cast<uint16_t>(this->klass) & 0x01) << 4;
Utils::Byte::Set2Bytes(buffer, 0, typeField);
Utils::Byte::Set2Bytes(buffer, 2, static_cast<uint16_t>(this->size) - 20);
std::memcpy(buffer + 4, StunPacket::MagicCookie, 4);
std::memcpy(buffer + 8, this->transactionId, 12);
this->transactionId = buffer + 8;
size_t pos{ 20 };
if (usernamePaddedLen != 0u)
{
Utils::Byte::Set2Bytes(buffer, pos, static_cast<uint16_t>(Attribute::USERNAME));
Utils::Byte::Set2Bytes(buffer, pos + 2, static_cast<uint16_t>(this->username.length()));
std::memcpy(buffer + pos + 4, this->username.c_str(), this->username.length());
pos += 4 + usernamePaddedLen;
}
if (this->priority != 0u)
{
Utils::Byte::Set2Bytes(buffer, pos, static_cast<uint16_t>(Attribute::PRIORITY));
Utils::Byte::Set2Bytes(buffer, pos + 2, 4);
Utils::Byte::Set4Bytes(buffer, pos + 4, this->priority);
pos += 4 + 4;
}
if (this->iceControlling != 0u)
{
Utils::Byte::Set2Bytes(buffer, pos, static_cast<uint16_t>(Attribute::ICE_CONTROLLING));
Utils::Byte::Set2Bytes(buffer, pos + 2, 8);
Utils::Byte::Set8Bytes(buffer, pos + 4, this->iceControlling);
pos += 4 + 8;
}
if (this->iceControlled != 0u)
{
Utils::Byte::Set2Bytes(buffer, pos, static_cast<uint16_t>(Attribute::ICE_CONTROLLED));
Utils::Byte::Set2Bytes(buffer, pos + 2, 8);
Utils::Byte::Set8Bytes(buffer, pos + 4, this->iceControlled);
pos += 4 + 8;
}
if (this->hasUseCandidate)
{
Utils::Byte::Set2Bytes(buffer, pos, static_cast<uint16_t>(Attribute::USE_CANDIDATE));
Utils::Byte::Set2Bytes(buffer, pos + 2, 0);
pos += 4;
}
if (addXorMappedAddress)
{
Utils::Byte::Set2Bytes(buffer, pos, static_cast<uint16_t>(Attribute::XOR_MAPPED_ADDRESS));
Utils::Byte::Set2Bytes(buffer, pos + 2, xorMappedAddressPaddedLen);
uint8_t* attrValue = buffer + pos + 4;
switch (this->xorMappedAddress->sa_family)
{
case AF_INET:
{
attrValue[0] = 0;
attrValue[1] = 0x01;
std::memcpy(
attrValue + 2,
&(reinterpret_cast<const sockaddr_in*>(this->xorMappedAddress))->sin_port,
2);
attrValue[2] ^= StunPacket::MagicCookie[0];
attrValue[3] ^= StunPacket::MagicCookie[1];
std::memcpy(
attrValue + 4,
&(reinterpret_cast<const sockaddr_in*>(this->xorMappedAddress))->sin_addr.s_addr,
4);
attrValue[4] ^= StunPacket::MagicCookie[0];
attrValue[5] ^= StunPacket::MagicCookie[1];
attrValue[6] ^= StunPacket::MagicCookie[2];
attrValue[7] ^= StunPacket::MagicCookie[3];
pos += 4 + 8;
break;
}
case AF_INET6:
{
attrValue[0] = 0;
attrValue[1] = 0x02;
std::memcpy(
attrValue + 2,
&(reinterpret_cast<const sockaddr_in6*>(this->xorMappedAddress))->sin6_port,
2);
attrValue[2] ^= StunPacket::MagicCookie[0];
attrValue[3] ^= StunPacket::MagicCookie[1];
std::memcpy(
attrValue + 4,
&(reinterpret_cast<const sockaddr_in6*>(this->xorMappedAddress))->sin6_addr.s6_addr,
16);
attrValue[4] ^= StunPacket::MagicCookie[0];
attrValue[5] ^= StunPacket::MagicCookie[1];
attrValue[6] ^= StunPacket::MagicCookie[2];
attrValue[7] ^= StunPacket::MagicCookie[3];
attrValue[8] ^= this->transactionId[0];
attrValue[9] ^= this->transactionId[1];
attrValue[10] ^= this->transactionId[2];
attrValue[11] ^= this->transactionId[3];
attrValue[12] ^= this->transactionId[4];
attrValue[13] ^= this->transactionId[5];
attrValue[14] ^= this->transactionId[6];
attrValue[15] ^= this->transactionId[7];
attrValue[16] ^= this->transactionId[8];
attrValue[17] ^= this->transactionId[9];
attrValue[18] ^= this->transactionId[10];
attrValue[19] ^= this->transactionId[11];
pos += 4 + 20;
break;
}
}
}
if (addErrorCode)
{
Utils::Byte::Set2Bytes(buffer, pos, static_cast<uint16_t>(Attribute::ERROR_CODE));
Utils::Byte::Set2Bytes(buffer, pos + 2, 4);
auto codeClass = static_cast<uint8_t>(this->errorCode / 100);
const uint8_t codeNumber = static_cast<uint8_t>(this->errorCode) - (codeClass * 100);
Utils::Byte::Set2Bytes(buffer, pos + 4, 0);
Utils::Byte::Set1Byte(buffer, pos + 6, codeClass);
Utils::Byte::Set1Byte(buffer, pos + 7, codeNumber);
pos += 4 + 4;
}
if (addMessageIntegrity)
{
if (addFingerprint)
{
Utils::Byte::Set2Bytes(buffer, 2, static_cast<uint16_t>(this->size - 20 - 8));
}
const uint8_t* computedMessageIntegrity =
Utils::Crypto::GetHmacSha1(this->password, buffer, pos);
Utils::Byte::Set2Bytes(buffer, pos, static_cast<uint16_t>(Attribute::MESSAGE_INTEGRITY));
Utils::Byte::Set2Bytes(buffer, pos + 2, 20);
std::memcpy(buffer + pos + 4, computedMessageIntegrity, 20);
this->messageIntegrity = buffer + pos + 4;
pos += 4 + 20;
if (addFingerprint)
{
Utils::Byte::Set2Bytes(buffer, 2, static_cast<uint16_t>(this->size - 20));
}
}
else
{
this->messageIntegrity = nullptr;
}
if (addFingerprint)
{
const uint32_t computedFingerprint = Utils::Crypto::GetCRC32(buffer, pos) ^ 0x5354554e;
Utils::Byte::Set2Bytes(buffer, pos, static_cast<uint16_t>(Attribute::FINGERPRINT));
Utils::Byte::Set2Bytes(buffer, pos + 2, 4);
Utils::Byte::Set4Bytes(buffer, pos + 4, computedFingerprint);
pos += 4 + 4;
this->hasFingerprint = true;
}
else
{
this->hasFingerprint = false;
}
MS_ASSERT(pos == this->size, "pos != this->size");
}
}