Skip to content

Commit be5a4d9

Browse files
committed
feat: add remoteFingerprints method to PeerConnection
Returns a vector that contains the certificate fingerprints used by the connection to the remote peer. Closes #1203 Refs #1166
1 parent 541d646 commit be5a4d9

File tree

4 files changed

+40
-7
lines changed

4 files changed

+40
-7
lines changed

include/rtc/peerconnection.hpp

+6
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ struct RTC_CPP_EXPORT DataChannelInit {
3535
string protocol = "";
3636
};
3737

38+
struct RTC_CPP_EXPORT RemoteFingerprint {
39+
string value;
40+
CertificateFingerprint::Algorithm algorithm;
41+
};
42+
3843
class RTC_CPP_EXPORT PeerConnection final : CheshireCat<impl::PeerConnection> {
3944
public:
4045
enum class State : int {
@@ -113,6 +118,7 @@ class RTC_CPP_EXPORT PeerConnection final : CheshireCat<impl::PeerConnection> {
113118
void onSignalingStateChange(std::function<void(SignalingState state)> callback);
114119

115120
void resetCallbacks();
121+
std::vector<struct RemoteFingerprint> remoteFingerprints();
116122

117123
// Stats
118124
void clearStats();

src/impl/peerconnection.cpp

+25-6
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ shared_ptr<DtlsTransport> PeerConnection::initDtlsTransport() {
239239
throw std::logic_error("No underlying ICE transport for DTLS transport");
240240

241241
auto certificate = mCertificate.get();
242-
auto verifierCallback = weak_bind(&PeerConnection::checkFingerprint, this, _1);
242+
auto verifierCallback = weak_bind(&PeerConnection::checkFingerprint, this, _1, fingerprintAlgorithm);
243243
auto dtlsStateChangeCallback =
244244
[this, weak_this = weak_from_this()](DtlsTransport::State transportState) {
245245
auto shared_this = weak_this.lock();
@@ -439,24 +439,36 @@ void PeerConnection::rollbackLocalDescription() {
439439
}
440440
}
441441

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

447-
if (config.disableFingerprintVerification)
448-
return true;
449-
450447
auto expectedFingerprint = mRemoteDescription->fingerprint()->value;
451-
if (expectedFingerprint == fingerprint) {
448+
if (config.disableFingerprintVerification || expectedFingerprint == fingerprint) {
452449
PLOG_VERBOSE << "Valid fingerprint \"" << fingerprint << "\"";
450+
storeRemoteFingerprint(fingerprint, algorithm);
453451
return true;
454452
}
455453

456454
PLOG_ERROR << "Invalid fingerprint \"" << fingerprint << "\", expected \"" << expectedFingerprint << "\"";
457455
return false;
458456
}
459457

458+
void PeerConnection::storeRemoteFingerprint(const std::string &fingerprint, const CertificateFingerprint::Algorithm &algorithm) {
459+
auto iter = std::find_if(rFingerprints.begin(), rFingerprints.end(), [&](const RemoteFingerprint& existing){return existing.value == fingerprint;});
460+
bool seenPreviously = iter != rFingerprints.end();
461+
462+
if (seenPreviously) {
463+
return;
464+
}
465+
466+
rFingerprints.push_back({
467+
.value = fingerprint,
468+
algorithm
469+
});
470+
}
471+
460472
void PeerConnection::forwardMessage(message_ptr message) {
461473
if (!message) {
462474
remoteCloseDataChannels();
@@ -1301,6 +1313,13 @@ void PeerConnection::resetCallbacks() {
13011313
trackCallback = nullptr;
13021314
}
13031315

1316+
std::vector<struct RemoteFingerprint> PeerConnection::remoteFingerprints() {
1317+
std::vector<struct RemoteFingerprint> ret;
1318+
ret = rFingerprints;
1319+
1320+
return ret;
1321+
}
1322+
13041323
void PeerConnection::updateTrackSsrcCache(const Description &description) {
13051324
std::unique_lock lock(mTracksMutex); // for safely writing to mTracksBySsrc
13061325

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, const CertificateFingerprint::Algorithm &algorithm);
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+
std::vector<struct RemoteFingerprint> remoteFingerprints();
101102

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

133135
const init_token mInitToken = Init::Instance().token();
134136
future_certificate_ptr mCertificate;
@@ -157,6 +159,8 @@ struct PeerConnection : std::enable_shared_from_this<PeerConnection> {
157159

158160
Queue<shared_ptr<DataChannel>> mPendingDataChannels;
159161
Queue<shared_ptr<Track>> mPendingTracks;
162+
163+
std::vector<struct RemoteFingerprint> rFingerprints;
160164
};
161165

162166
} // namespace rtc::impl

src/peerconnection.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,10 @@ optional<std::chrono::milliseconds> PeerConnection::rtt() {
367367
return sctpTransport ? sctpTransport->rtt() : nullopt;
368368
}
369369

370+
std::vector<struct RemoteFingerprint> PeerConnection::remoteFingerprints() {
371+
return impl()->remoteFingerprints();
372+
}
373+
370374
std::ostream &operator<<(std::ostream &out, PeerConnection::State state) {
371375
using State = PeerConnection::State;
372376
const char *str;

0 commit comments

Comments
 (0)