Skip to content

Commit

Permalink
Merge pull request #886 from paullouisageneau/cleanup
Browse files Browse the repository at this point in the history
Cleanup
  • Loading branch information
paullouisageneau authored May 13, 2023
2 parents 93f6ec6 + 50e1156 commit 506961b
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 115 deletions.
4 changes: 0 additions & 4 deletions include/rtc/rtcpsrreporter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ class RTC_CPP_EXPORT RtcpSrReporter final : public MediaHandlerElement {
uint32_t lastReportedTimestamp() const;
void setNeedsToReport();

// deprecated, do not call
[[deprecated]] void startRecording();
[[deprecated]] uint32_t previousReportedTimestamp() const { return lastReportedTimestamp(); }

private:
uint32_t mPacketCount = 0;
uint32_t mPayloadOctets = 0;
Expand Down
9 changes: 0 additions & 9 deletions include/rtc/rtppacketizationconfig.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,6 @@ class RTC_CPP_EXPORT RtpPacketizationConfig {
/// Convert seconds to timestamp
/// @param seconds Number of seconds
uint32_t secondsToTimestamp(double seconds);

// deprecated, do not use
double startTime = 0.;
enum class EpochStart : uint64_t {
T1970 = 2208988800, // number of seconds between 1970 and 1900
T1900 = 0
};
[[deprecated]] void setStartTime(double startTime, EpochStart epochStart,
optional<uint32_t> startTimestamp = std::nullopt);
};

} // namespace rtc
Expand Down
108 changes: 58 additions & 50 deletions src/impl/dtlssrtptransport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,28 +218,18 @@ bool DtlsSrtpTransport::demuxMessage(message_ptr message) {
}
}

EncryptionParams DtlsSrtpTransport::getEncryptionParams(string_view suite) {
if (suite == "SRTP_AES128_CM_SHA1_80")
return {SRTP_AES_128_KEY_LEN, SRTP_SALT_LEN, srtp_profile_aes128_cm_sha1_80};
if (suite == "SRTP_AES128_CM_SHA1_32")
return {SRTP_AES_128_KEY_LEN, SRTP_SALT_LEN, srtp_profile_aes128_cm_sha1_32};
if (suite == "SRTP_AEAD_AES_128_GCM")
return {SRTP_AES_128_KEY_LEN, SRTP_AEAD_SALT_LEN, srtp_profile_aead_aes_128_gcm};
if (suite == "SRTP_AEAD_AES_256_GCM")
return {SRTP_AES_256_KEY_LEN, SRTP_AEAD_SALT_LEN, srtp_profile_aead_aes_256_gcm};
throw std::logic_error("Unexpected SRTP suite name: " + std::string(suite));
}

void DtlsSrtpTransport::postHandshake() {
if (mInitDone)
return;
const unsigned char *clientKey, *clientSalt, *serverKey, *serverSalt;

#if USE_GNUTLS
PLOG_INFO << "Deriving SRTP keying material (GnuTLS)";
unsigned int keySize = SRTP_AES_128_KEY_LEN;
unsigned int saltSize = SRTP_SALT_LEN;
auto srtpProfile = srtp_profile_aes128_cm_sha1_80;
auto keySizeWithSalt = SRTP_AES_ICM_128_KEY_LEN_WSALT;

const srtp_profile_t srtpProfile = srtp_profile_aes128_cm_sha1_80;
const size_t keySize = SRTP_AES_128_KEY_LEN;
const size_t saltSize = SRTP_SALT_LEN;
const size_t keySizeWithSalt = SRTP_AES_ICM_128_KEY_LEN_WSALT;

const size_t materialLen = keySizeWithSalt * 2;
std::vector<unsigned char> material(materialLen);
gnutls_datum_t clientKeyDatum, clientSaltDatum, serverKeyDatum, serverSaltDatum;
Expand All @@ -258,59 +248,61 @@ void DtlsSrtpTransport::postHandshake() {
if (serverSaltDatum.size != saltSize)
throw std::logic_error("Unexpected SRTP salt size: " + to_string(serverSaltDatum.size));

clientKey = reinterpret_cast<const unsigned char *>(clientKeyDatum.data);
clientSalt = reinterpret_cast<const unsigned char *>(clientSaltDatum.data);
const unsigned char *clientKey = reinterpret_cast<const unsigned char *>(clientKeyDatum.data);
const unsigned char *clientSalt = reinterpret_cast<const unsigned char *>(clientSaltDatum.data);
const unsigned char *serverKey = reinterpret_cast<const unsigned char *>(serverKeyDatum.data);
const unsigned char *serverSalt = reinterpret_cast<const unsigned char *>(serverSaltDatum.data);

serverKey = reinterpret_cast<const unsigned char *>(serverKeyDatum.data);
serverSalt = reinterpret_cast<const unsigned char *>(serverSaltDatum.data);
#elif USE_MBEDTLS
PLOG_INFO << "Deriving SRTP keying material (Mbed TLS)";
unsigned int keySize = SRTP_AES_128_KEY_LEN;
unsigned int saltSize = SRTP_SALT_LEN;
auto srtpProfile = srtp_profile_aes128_cm_sha1_80;
auto keySizeWithSalt = SRTP_AES_ICM_128_KEY_LEN_WSALT;
mbedtls_dtls_srtp_info srtpInfo;

mbedtls_dtls_srtp_info srtpInfo;
mbedtls_ssl_get_dtls_srtp_negotiation_result(&mSsl, &srtpInfo);
if (srtpInfo.private_chosen_dtls_srtp_profile != MBEDTLS_TLS_SRTP_AES128_CM_HMAC_SHA1_80) {
if (srtpInfo.private_chosen_dtls_srtp_profile != MBEDTLS_TLS_SRTP_AES128_CM_HMAC_SHA1_80)
throw std::runtime_error("Failed to get SRTP profile");
}

const size_t materialLen = keySizeWithSalt * 2;
std::vector<unsigned char> material(materialLen);
const srtp_profile_t srtpProfile = srtp_profile_aes128_cm_sha1_80;
const size_t keySize = SRTP_AES_128_KEY_LEN;
const size_t saltSize = SRTP_SALT_LEN;
const size_t keySizeWithSalt = SRTP_AES_ICM_128_KEY_LEN_WSALT;

if (mTlsProfile == MBEDTLS_SSL_TLS_PRF_NONE)
throw std::logic_error("TLS PRF type is not set");

// The extractor provides the client write master key, the server write master key, the client
// write master salt and the server write master salt in that order.
const string label = "EXTRACTOR-dtls_srtp";

if (mTlsProfile == MBEDTLS_SSL_TLS_PRF_NONE) {
throw std::logic_error("Failed to get SRTP profile");
}
const size_t materialLen = keySizeWithSalt * 2;
std::vector<unsigned char> material(materialLen);

if (mbedtls_ssl_tls_prf(mTlsProfile, reinterpret_cast<const unsigned char *>(mMasterSecret), 48,
label.c_str(), reinterpret_cast<const unsigned char *>(mRandBytes), 64,
material.data(), materialLen) != 0) {
material.data(), materialLen) != 0)
throw std::runtime_error("Failed to derive SRTP keys");
}

// Order is client key, server key, client salt, and server salt
clientKey = material.data();
serverKey = clientKey + keySize;
clientSalt = serverKey + keySize;
serverSalt = clientSalt + saltSize;
#else
const unsigned char *clientKey = material.data();
const unsigned char *serverKey = clientKey + keySize;
const unsigned char *clientSalt = serverKey + keySize;
const unsigned char *serverSalt = clientSalt + saltSize;

#else // OpenSSL
PLOG_INFO << "Deriving SRTP keying material (OpenSSL)";
auto profile = SSL_get_selected_srtp_profile(mSsl);
if (!profile)
throw std::runtime_error("Failed to get SRTP profile: " +
openssl::error_string(ERR_get_error()));
PLOG_DEBUG << "srtp profile used is: " << profile->name;
auto [keySize, saltSize, srtpProfile] = getEncryptionParams(profile->name);
auto keySizeWithSalt = keySize + saltSize;
const size_t materialLen = keySizeWithSalt * 2;
std::vector<unsigned char> material(materialLen);

PLOG_DEBUG << "SRTP profile is: " << profile->name;

const auto [srtpProfile, keySize, saltSize] = getProfileParamsFromName(profile->name);
const size_t keySizeWithSalt = keySize + saltSize;

// The extractor provides the client write master key, the server write master key, the client
// write master salt and the server write master salt in that order.
const string label = "EXTRACTOR-dtls_srtp";
const size_t materialLen = keySizeWithSalt * 2;
std::vector<unsigned char> material(materialLen);

// returns 1 on success, 0 or -1 on failure (OpenSSL API is a complete mess...)
if (SSL_export_keying_material(mSsl, material.data(), materialLen, label.c_str(), label.size(),
Expand All @@ -319,11 +311,12 @@ void DtlsSrtpTransport::postHandshake() {
openssl::error_string(ERR_get_error()));

// Order is client key, server key, client salt, and server salt
clientKey = material.data();
serverKey = clientKey + keySize;
clientSalt = serverKey + keySize;
serverSalt = clientSalt + saltSize;
const unsigned char *clientKey = material.data();
const unsigned char *serverKey = clientKey + keySize;
const unsigned char *clientSalt = serverKey + keySize;
const unsigned char *serverSalt = clientSalt + saltSize;
#endif

mClientSessionKey.resize(keySizeWithSalt);
mServerSessionKey.resize(keySizeWithSalt);
std::memcpy(mClientSessionKey.data(), clientKey, keySize);
Expand Down Expand Up @@ -362,6 +355,21 @@ void DtlsSrtpTransport::postHandshake() {
mInitDone = true;
}

#if !USE_GNUTLS && !USE_MBEDTLS
ProfileParams DtlsSrtpTransport::getProfileParamsFromName(string_view name) {
if (name == "SRTP_AES128_CM_SHA1_80")
return {srtp_profile_aes128_cm_sha1_80, SRTP_AES_128_KEY_LEN, SRTP_SALT_LEN};
if (name == "SRTP_AES128_CM_SHA1_32")
return {srtp_profile_aes128_cm_sha1_32, SRTP_AES_128_KEY_LEN, SRTP_SALT_LEN};
if (name == "SRTP_AEAD_AES_128_GCM")
return {srtp_profile_aead_aes_128_gcm, SRTP_AES_128_KEY_LEN, SRTP_AEAD_SALT_LEN};
if (name == "SRTP_AEAD_AES_256_GCM")
return {srtp_profile_aead_aes_256_gcm, SRTP_AES_256_KEY_LEN, SRTP_AEAD_SALT_LEN};

throw std::logic_error("Unknown SRTP profile name: " + std::string(name));
}
#endif

} // namespace rtc::impl

#endif
14 changes: 8 additions & 6 deletions src/impl/dtlssrtptransport.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@

namespace rtc::impl {

struct EncryptionParams {
unsigned int keySize;
unsigned int saltSize;
struct ProfileParams {
srtp_profile_t srtpProfile;
size_t keySize;
size_t saltSize;
};

class DtlsSrtpTransport final : public DtlsTransport {
Expand All @@ -46,11 +46,13 @@ class DtlsSrtpTransport final : public DtlsTransport {
void recvMedia(message_ptr message);
bool demuxMessage(message_ptr message) override;
void postHandshake() override;
EncryptionParams getEncryptionParams(string_view suite);
message_callback mSrtpRecvCallback;

srtp_t mSrtpIn, mSrtpOut;
#if !USE_GNUTLS && !USE_MBEDTLS
ProfileParams getProfileParamsFromName(string_view name);
#endif

message_callback mSrtpRecvCallback;
srtp_t mSrtpIn, mSrtpOut;
std::atomic<bool> mInitDone = false;
std::vector<unsigned char> mClientSessionKey;
std::vector<unsigned char> mServerSessionKey;
Expand Down
27 changes: 11 additions & 16 deletions src/impl/httpproxytransport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@ HttpProxyTransport::HttpProxyTransport(shared_ptr<TcpTransport> lower, std::stri
std::string service, state_callback stateCallback)
: Transport(lower, std::move(stateCallback)), mHostname(std::move(hostname)),
mService(std::move(service)) {
PLOG_DEBUG << "Initializing HTTP proxy transport";
if (!lower->isActive())
throw std::logic_error("Http proxy creation failed, expects lower transport to be active");

PLOG_DEBUG << "Initializing http Proxy transport";
throw std::logic_error("HTTP proxy transport expects the lower transport to be active");
}

HttpProxyTransport::~HttpProxyTransport() { unregisterIncoming(); }
Expand All @@ -40,10 +39,8 @@ void HttpProxyTransport::start() {
void HttpProxyTransport::stop() { unregisterIncoming(); }

bool HttpProxyTransport::send(message_ptr message) {
std::lock_guard lock(mSendMutex);

if (state() != State::Connected)
throw std::runtime_error("Http proxy connection is not open");
throw std::runtime_error("HTTP proxy connection is not open");

PLOG_VERBOSE << "Send size=" << message->size();
return outgoing(message);
Expand All @@ -63,7 +60,7 @@ void HttpProxyTransport::incoming(message_ptr message) {
if (state() == State::Connecting) {
mBuffer.insert(mBuffer.end(), message->begin(), message->end());
if (size_t len = parseHttpResponse(mBuffer.data(), mBuffer.size())) {
PLOG_INFO << "Http proxy connection open";
PLOG_INFO << "HTTP proxy connection open";
changeState(State::Connected);
mBuffer.erase(mBuffer.begin(), mBuffer.begin() + len);

Expand All @@ -83,27 +80,25 @@ void HttpProxyTransport::incoming(message_ptr message) {
}

if (state() == State::Connected) {
PLOG_INFO << "Http Proxy disconnected";
PLOG_INFO << "HTTP proxy disconnected";
changeState(State::Disconnected);
recv(nullptr);
} else {
PLOG_ERROR << "Http Proxy failed";
PLOG_ERROR << "HTTP proxy connection failed";
changeState(State::Failed);
}
}

bool HttpProxyTransport::sendHttpRequest() {
PLOG_DEBUG << "Sending proxy http request";
PLOG_DEBUG << "Sending HTTP request to proxy";

const string request = generateHttpRequest();
auto data = reinterpret_cast<const byte *>(request.data());
return outgoing(make_message(data, data + request.size()));
}

std::string HttpProxyTransport::generateHttpRequest() {
std::string out =
"CONNECT " + mHostname + ":" + mService + " HTTP/1.1\r\nHost: " + mHostname + "\r\n\r\n";
return out;
string HttpProxyTransport::generateHttpRequest() {
return "CONNECT " + mHostname + ":" + mService + " HTTP/1.1\r\nHost: " + mHostname + "\r\n\r\n";
}

size_t HttpProxyTransport::parseHttpResponse(std::byte *buffer, size_t size) {
Expand All @@ -113,7 +108,7 @@ size_t HttpProxyTransport::parseHttpResponse(std::byte *buffer, size_t size) {
return 0;

if (lines.empty())
throw std::runtime_error("Invalid http request for proxy");
throw std::runtime_error("Invalid response from HTTP proxy");

std::istringstream status(std::move(lines.front()));
lines.pop_front();
Expand All @@ -123,7 +118,7 @@ size_t HttpProxyTransport::parseHttpResponse(std::byte *buffer, size_t size) {
status >> protocol >> code;

if (code != 200)
throw std::runtime_error("Unexpected response code " + to_string(code) + " for proxy");
throw std::runtime_error("Unexpected response code " + to_string(code) + " from HTTP proxy");

return length;
}
Expand Down
7 changes: 3 additions & 4 deletions src/impl/httpproxytransport.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,12 @@ class HttpProxyTransport final : public Transport,
private:
void incoming(message_ptr message) override;
bool sendHttpRequest();
std::string generateHttpRequest();
string generateHttpRequest();
size_t parseHttpResponse(std::byte *buffer, size_t size);

std::string mHostname;
std::string mService;
string mHostname;
string mService;
binary mBuffer;
std::mutex mSendMutex;
};

} // namespace rtc::impl
Expand Down
18 changes: 4 additions & 14 deletions src/impl/websocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,23 +146,13 @@ bool WebSocket::isClosed() const { return state == State::Closed; }
size_t WebSocket::maxMessageSize() const { return DEFAULT_MAX_MESSAGE_SIZE; }

optional<message_variant> WebSocket::receive() {
while (auto next = mRecvQueue.pop()) {
message_ptr message = *next;
if (message->type != Message::Control)
return to_variant(std::move(*message));
}
return nullopt;
auto next = mRecvQueue.pop();
return next ? std::make_optional(to_variant(std::move(**next))) : nullopt;
}

optional<message_variant> WebSocket::peek() {
while (auto next = mRecvQueue.peek()) {
message_ptr message = *next;
if (message->type != Message::Control)
return to_variant(std::move(*message));

mRecvQueue.pop();
}
return nullopt;
auto next = mRecvQueue.peek();
return next ? std::make_optional(to_variant(std::move(**next))) : nullopt;
}

size_t WebSocket::availableAmount() const { return mRecvQueue.amount(); }
Expand Down
4 changes: 0 additions & 4 deletions src/rtcpsrreporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,6 @@ void RtcpSrReporter::setNeedsToReport() {
mNeedsToReport = true;
}

void RtcpSrReporter::startRecording() {
// Dummy
}

uint32_t RtcpSrReporter::lastReportedTimestamp() const {
return mLastReportedTimestamp;
}
Expand Down
8 changes: 0 additions & 8 deletions src/rtppacketizationconfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,6 @@ uint32_t RtpPacketizationConfig::secondsToTimestamp(double seconds) {
return RtpPacketizationConfig::getTimestampFromSeconds(seconds, clockRate);
}

void RtpPacketizationConfig::setStartTime(double startTime, EpochStart epochStart,
optional<uint32_t> startTimestamp) {
// Deprecated dummy function
this->startTime = startTime + double(static_cast<uint64_t>(epochStart));
if (startTimestamp.has_value())
this->timestamp = this->startTimestamp = startTimestamp.value();
}

} // namespace rtc

#endif /* RTC_ENABLE_MEDIA */

0 comments on commit 506961b

Please sign in to comment.