Skip to content

Commit

Permalink
add failure tolerance for framecryptor.
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudwebrtc committed Sep 9, 2023
1 parent 31774d3 commit 6afb155
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
6 changes: 3 additions & 3 deletions api/crypto/frame_crypto_transformer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ int AesGcmEncryptDecrypt(EncryptOrDecrypt mode,
}

if (!ok) {
RTC_LOG(LS_ERROR) << "Failed to perform AES-GCM operation.";
RTC_LOG(LS_WARNING) << "Failed to perform AES-GCM operation.";
return OperationError;
}

Expand Down Expand Up @@ -593,7 +593,7 @@ void FrameCryptorTransformer::decryptFrame(
decryption_success = true;
// success, so we set the new key
key_handler->SetKeyFromMaterial(new_material, key_index);
key_handler->SetHasValidKey(true);
key_handler->SetHasValidKey();
if (last_dec_error_ != FrameCryptionState::kKeyRatcheted) {
last_dec_error_ = FrameCryptionState::kKeyRatcheted;
if (observer_)
Expand Down Expand Up @@ -622,7 +622,7 @@ void FrameCryptorTransformer::decryptFrame(
if (!decryption_success) {
if (last_dec_error_ != FrameCryptionState::kDecryptionFailed) {
last_dec_error_ = FrameCryptionState::kDecryptionFailed;
key_handler->SetHasValidKey(false);
key_handler->DecryptionFailure();
if (observer_)
observer_->OnFrameCryptionStateChanged(participant_id_,
last_dec_error_);
Expand Down
28 changes: 22 additions & 6 deletions api/crypto/frame_crypto_transformer.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,14 @@ struct KeyProviderOptions {
std::vector<uint8_t> ratchet_salt;
std::vector<uint8_t> uncrypted_magic_bytes;
int ratchet_window_size;
KeyProviderOptions() : shared_key(false), ratchet_window_size(0) {}
int failure_tolerance;
KeyProviderOptions() : shared_key(false), ratchet_window_size(0), failure_tolerance(-1) {}
KeyProviderOptions(KeyProviderOptions& copy)
: shared_key(copy.shared_key),
ratchet_salt(copy.ratchet_salt),
uncrypted_magic_bytes(copy.uncrypted_magic_bytes),
ratchet_window_size(copy.ratchet_window_size) {}
ratchet_window_size(copy.ratchet_window_size),
failure_tolerance(copy.failure_tolerance) {}
};

class KeyProvider : public rtc::RefCountInterface {
Expand Down Expand Up @@ -116,7 +118,7 @@ class ParticipantKeyHandler {
}
SetKeyFromMaterial(new_material,
key_index != -1 ? key_index : current_key_index_);
SetHasValidKey(true);
SetHasValidKey();
return new_material;
}

Expand All @@ -127,7 +129,7 @@ class ParticipantKeyHandler {

virtual void SetKey(std::vector<uint8_t> password, int key_index) {
SetKeyFromMaterial(password, key_index);
SetHasValidKey(true);
SetHasValidKey();
}

std::vector<uint8_t> RatchetKeyMaterial(
Expand Down Expand Up @@ -156,9 +158,10 @@ class ParticipantKeyHandler {
return has_valid_key_;
}

void SetHasValidKey(bool has_valid_key) {
void SetHasValidKey() {
webrtc::MutexLock lock(&mutex_);
has_valid_key_ = has_valid_key;
decryption_failure_count_ = 0;
has_valid_key_ = true;
}

void SetKeyFromMaterial(std::vector<uint8_t> password, int key_index) {
Expand All @@ -170,8 +173,21 @@ class ParticipantKeyHandler {
DeriveKeys(password, key_provider_->options().ratchet_salt, 128);
}

void DecryptionFailure() {
webrtc::MutexLock lock(&mutex_);
if (key_provider_->options().failure_tolerance < 0) {
return;
}
decryption_failure_count_ += 1;

if (decryption_failure_count_ > key_provider_->options().failure_tolerance) {
has_valid_key_ = false;
}
}

private:
bool has_valid_key_ = false;
int decryption_failure_count_ = 0;
mutable webrtc::Mutex mutex_;
int current_key_index_ = 0;
KeyProvider* key_provider_;
Expand Down

0 comments on commit 6afb155

Please sign in to comment.