Skip to content

Commit

Permalink
chore: pr comments
Browse files Browse the repository at this point in the history
  • Loading branch information
achingbrain committed Jun 11, 2024
1 parent 9d03db6 commit 1923c37
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 35 deletions.
7 changes: 1 addition & 6 deletions include/rtc/peerconnection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,6 @@ struct RTC_CPP_EXPORT DataChannelInit {
string protocol = "";
};

struct RTC_CPP_EXPORT RemoteFingerprint {
string value;
CertificateFingerprint::Algorithm algorithm;
};

class RTC_CPP_EXPORT PeerConnection final : CheshireCat<impl::PeerConnection> {
public:
enum class State : int {
Expand Down Expand Up @@ -118,7 +113,7 @@ class RTC_CPP_EXPORT PeerConnection final : CheshireCat<impl::PeerConnection> {
void onSignalingStateChange(std::function<void(SignalingState state)> callback);

void resetCallbacks();
std::vector<struct RemoteFingerprint> remoteFingerprints();
CertificateFingerprint remoteFingerprint();

// Stats
void clearStats();
Expand Down
40 changes: 17 additions & 23 deletions src/impl/peerconnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,12 +234,14 @@ shared_ptr<DtlsTransport> PeerConnection::initDtlsTransport() {
fingerprintAlgorithm = remote->fingerprint()->algorithm;
}

mRemoteFingerprintAlgorithm = fingerprintAlgorithm;

auto lower = std::atomic_load(&mIceTransport);
if (!lower)
throw std::logic_error("No underlying ICE transport for DTLS transport");

auto certificate = mCertificate.get();
auto verifierCallback = weak_bind(&PeerConnection::checkFingerprint, this, _1, fingerprintAlgorithm);
auto verifierCallback = weak_bind(&PeerConnection::checkFingerprint, this, _1);
auto dtlsStateChangeCallback =
[this, weak_this = weak_from_this()](DtlsTransport::State transportState) {
auto shared_this = weak_this.lock();
Expand Down Expand Up @@ -439,36 +441,28 @@ void PeerConnection::rollbackLocalDescription() {
}
}

bool PeerConnection::checkFingerprint(const std::string &fingerprint, const CertificateFingerprint::Algorithm &algorithm) {
bool PeerConnection::checkFingerprint(const std::string &fingerprint) {
std::lock_guard lock(mRemoteDescriptionMutex);
if (!mRemoteDescription || !mRemoteDescription->fingerprint())
return false;

if (config.disableFingerprintVerification) {
PLOG_VERBOSE << "Skipping fingerprint validation";
mRemoteFingerprint = fingerprint;
return true;
}

auto expectedFingerprint = mRemoteDescription->fingerprint()->value;
if (config.disableFingerprintVerification || expectedFingerprint == fingerprint) {
if (expectedFingerprint == fingerprint) {
PLOG_VERBOSE << "Valid fingerprint \"" << fingerprint << "\"";
storeRemoteFingerprint(fingerprint, algorithm);
mRemoteFingerprint = fingerprint;
return true;
}

PLOG_ERROR << "Invalid fingerprint \"" << fingerprint << "\", expected \"" << expectedFingerprint << "\"";
return false;
}

void PeerConnection::storeRemoteFingerprint(const std::string &value, const CertificateFingerprint::Algorithm &algorithm) {
auto iter = std::find_if(rFingerprints.begin(), rFingerprints.end(), [&](const RemoteFingerprint& existing){return existing.value == value;});
bool seenPreviously = iter != rFingerprints.end();

if (seenPreviously) {
return;
}

rFingerprints.push_back({
value,
algorithm
});
}

void PeerConnection::forwardMessage(message_ptr message) {
if (!message) {
remoteCloseDataChannels();
Expand Down Expand Up @@ -1313,11 +1307,11 @@ void PeerConnection::resetCallbacks() {
trackCallback = nullptr;
}

std::vector<struct RemoteFingerprint> PeerConnection::remoteFingerprints() {
std::vector<struct RemoteFingerprint> ret;
ret = rFingerprints;

return ret;
CertificateFingerprint PeerConnection::remoteFingerprint() {
if (mRemoteFingerprint)
return {CertificateFingerprint{mRemoteFingerprintAlgorithm, *mRemoteFingerprint}};
else
return {};
}

void PeerConnection::updateTrackSsrcCache(const Description &description) {
Expand Down
8 changes: 4 additions & 4 deletions src/impl/peerconnection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ struct PeerConnection : std::enable_shared_from_this<PeerConnection> {

void endLocalCandidates();
void rollbackLocalDescription();
bool checkFingerprint(const std::string &fingerprint, const CertificateFingerprint::Algorithm &algorithm);
bool checkFingerprint(const std::string &fingerprint);
void forwardMessage(message_ptr message);
void forwardMedia(message_ptr message);
void forwardBufferedAmount(uint16_t stream, size_t amount);
Expand Down Expand Up @@ -98,7 +98,7 @@ struct PeerConnection : std::enable_shared_from_this<PeerConnection> {
bool changeSignalingState(SignalingState newState);

void resetCallbacks();
std::vector<struct RemoteFingerprint> remoteFingerprints();
CertificateFingerprint remoteFingerprint();

// Helper method for asynchronous callback invocation
template <typename... Args> void trigger(synchronized_callback<Args...> *cb, Args... args) {
Expand Down Expand Up @@ -130,7 +130,6 @@ struct PeerConnection : std::enable_shared_from_this<PeerConnection> {
private:
void dispatchMedia(message_ptr message);
void updateTrackSsrcCache(const Description &description);
void storeRemoteFingerprint(const std::string &fingerprint, const CertificateFingerprint::Algorithm &algorithm);

const init_token mInitToken = Init::Instance().token();
future_certificate_ptr mCertificate;
Expand Down Expand Up @@ -160,7 +159,8 @@ struct PeerConnection : std::enable_shared_from_this<PeerConnection> {
Queue<shared_ptr<DataChannel>> mPendingDataChannels;
Queue<shared_ptr<Track>> mPendingTracks;

std::vector<struct RemoteFingerprint> rFingerprints;
CertificateFingerprint::Algorithm mRemoteFingerprintAlgorithm = CertificateFingerprint::Algorithm::Sha256;
optional<string> mRemoteFingerprint;
};

} // namespace rtc::impl
Expand Down
4 changes: 2 additions & 2 deletions src/peerconnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -367,8 +367,8 @@ optional<std::chrono::milliseconds> PeerConnection::rtt() {
return sctpTransport ? sctpTransport->rtt() : nullopt;
}

std::vector<struct RemoteFingerprint> PeerConnection::remoteFingerprints() {
return impl()->remoteFingerprints();
CertificateFingerprint PeerConnection::remoteFingerprint() {
return impl()->remoteFingerprint();
}

std::ostream &operator<<(std::ostream &out, PeerConnection::State state) {
Expand Down

0 comments on commit 1923c37

Please sign in to comment.