Skip to content

Commit b756b5a

Browse files
Merge pull request #1206 from achingbrain/feat/add-remotefingerprints-method
feat: add remoteFingerprints method to PeerConnection
2 parents 4261b4f + 1923c37 commit b756b5a

File tree

4 files changed

+26
-4
lines changed

4 files changed

+26
-4
lines changed

include/rtc/peerconnection.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ class RTC_CPP_EXPORT PeerConnection final : CheshireCat<impl::PeerConnection> {
118118
void onSignalingStateChange(std::function<void(SignalingState state)> callback);
119119

120120
void resetCallbacks();
121+
CertificateFingerprint remoteFingerprint();
121122

122123
// Stats
123124
void clearStats();

src/impl/peerconnection.cpp

+16-3
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,8 @@ shared_ptr<DtlsTransport> PeerConnection::initDtlsTransport() {
234234
fingerprintAlgorithm = remote->fingerprint()->algorithm;
235235
}
236236

237+
mRemoteFingerprintAlgorithm = fingerprintAlgorithm;
238+
237239
auto lower = std::atomic_load(&mIceTransport);
238240
if (!lower)
239241
throw std::logic_error("No underlying ICE transport for DTLS transport");
@@ -439,17 +441,21 @@ void PeerConnection::rollbackLocalDescription() {
439441
}
440442
}
441443

442-
bool PeerConnection::checkFingerprint(const std::string &fingerprint) const {
444+
bool PeerConnection::checkFingerprint(const std::string &fingerprint) {
443445
std::lock_guard lock(mRemoteDescriptionMutex);
444446
if (!mRemoteDescription || !mRemoteDescription->fingerprint())
445447
return false;
446448

447-
if (config.disableFingerprintVerification)
449+
if (config.disableFingerprintVerification) {
450+
PLOG_VERBOSE << "Skipping fingerprint validation";
451+
mRemoteFingerprint = fingerprint;
448452
return true;
453+
}
449454

450455
auto expectedFingerprint = mRemoteDescription->fingerprint()->value;
451-
if (expectedFingerprint == fingerprint) {
456+
if (expectedFingerprint == fingerprint) {
452457
PLOG_VERBOSE << "Valid fingerprint \"" << fingerprint << "\"";
458+
mRemoteFingerprint = fingerprint;
453459
return true;
454460
}
455461

@@ -1301,6 +1307,13 @@ void PeerConnection::resetCallbacks() {
13011307
trackCallback = nullptr;
13021308
}
13031309

1310+
CertificateFingerprint PeerConnection::remoteFingerprint() {
1311+
if (mRemoteFingerprint)
1312+
return {CertificateFingerprint{mRemoteFingerprintAlgorithm, *mRemoteFingerprint}};
1313+
else
1314+
return {};
1315+
}
1316+
13041317
void PeerConnection::updateTrackSsrcCache(const Description &description) {
13051318
std::unique_lock lock(mTracksMutex); // for safely writing to mTracksBySsrc
13061319

src/impl/peerconnection.hpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ struct PeerConnection : std::enable_shared_from_this<PeerConnection> {
5353

5454
void endLocalCandidates();
5555
void rollbackLocalDescription();
56-
bool checkFingerprint(const std::string &fingerprint) const;
56+
bool checkFingerprint(const std::string &fingerprint);
5757
void forwardMessage(message_ptr message);
5858
void forwardMedia(message_ptr message);
5959
void forwardBufferedAmount(uint16_t stream, size_t amount);
@@ -98,6 +98,7 @@ struct PeerConnection : std::enable_shared_from_this<PeerConnection> {
9898
bool changeSignalingState(SignalingState newState);
9999

100100
void resetCallbacks();
101+
CertificateFingerprint remoteFingerprint();
101102

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

158159
Queue<shared_ptr<DataChannel>> mPendingDataChannels;
159160
Queue<shared_ptr<Track>> mPendingTracks;
161+
162+
CertificateFingerprint::Algorithm mRemoteFingerprintAlgorithm = CertificateFingerprint::Algorithm::Sha256;
163+
optional<string> mRemoteFingerprint;
160164
};
161165

162166
} // namespace rtc::impl

src/peerconnection.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,10 @@ optional<std::chrono::milliseconds> PeerConnection::rtt() {
372372
return sctpTransport ? sctpTransport->rtt() : nullopt;
373373
}
374374

375+
CertificateFingerprint PeerConnection::remoteFingerprint() {
376+
return impl()->remoteFingerprint();
377+
}
378+
375379
std::ostream &operator<<(std::ostream &out, PeerConnection::State state) {
376380
using State = PeerConnection::State;
377381
const char *str;

0 commit comments

Comments
 (0)