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

feat: add remoteFingerprints method to PeerConnection #1206

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
1 change: 1 addition & 0 deletions include/rtc/peerconnection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ class RTC_CPP_EXPORT PeerConnection final : CheshireCat<impl::PeerConnection> {
void onSignalingStateChange(std::function<void(SignalingState state)> callback);

void resetCallbacks();
CertificateFingerprint remoteFingerprint();

// Stats
void clearStats();
Expand Down
19 changes: 16 additions & 3 deletions src/impl/peerconnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,8 @@ 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");
Expand Down Expand Up @@ -439,17 +441,21 @@ void PeerConnection::rollbackLocalDescription() {
}
}

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

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

auto expectedFingerprint = mRemoteDescription->fingerprint()->value;
if (expectedFingerprint == fingerprint) {
if (expectedFingerprint == fingerprint) {
PLOG_VERBOSE << "Valid fingerprint \"" << fingerprint << "\"";
mRemoteFingerprint = fingerprint;
return true;
}

Expand Down Expand Up @@ -1301,6 +1307,13 @@ void PeerConnection::resetCallbacks() {
trackCallback = nullptr;
}

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

void PeerConnection::updateTrackSsrcCache(const Description &description) {
std::unique_lock lock(mTracksMutex); // for safely writing to mTracksBySsrc

Expand Down
6 changes: 5 additions & 1 deletion 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;
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,6 +98,7 @@ struct PeerConnection : std::enable_shared_from_this<PeerConnection> {
bool changeSignalingState(SignalingState newState);

void resetCallbacks();
CertificateFingerprint remoteFingerprint();

// Helper method for asynchronous callback invocation
template <typename... Args> void trigger(synchronized_callback<Args...> *cb, Args... args) {
Expand Down Expand Up @@ -157,6 +158,9 @@ struct PeerConnection : std::enable_shared_from_this<PeerConnection> {

Queue<shared_ptr<DataChannel>> mPendingDataChannels;
Queue<shared_ptr<Track>> mPendingTracks;

CertificateFingerprint::Algorithm mRemoteFingerprintAlgorithm = CertificateFingerprint::Algorithm::Sha256;
optional<string> mRemoteFingerprint;
};

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

CertificateFingerprint PeerConnection::remoteFingerprint() {
return impl()->remoteFingerprint();
}

std::ostream &operator<<(std::ostream &out, PeerConnection::State state) {
using State = PeerConnection::State;
const char *str;
Expand Down
Loading