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

Add key ring size to keyProviderOptions. #109

Merged
merged 3 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Add key ring size to keyProviderOptions.
  • Loading branch information
cloudwebrtc committed Mar 22, 2024
commit ddb1f6a4a6531fecf65c6f15314e96a6c841ff60
4 changes: 2 additions & 2 deletions api/crypto/frame_crypto_transformer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -551,11 +551,11 @@ void FrameCryptorTransformer::decryptFrame(
? key_provider_->GetSharedKey(participant_id_)
: key_provider_->GetKey(participant_id_);

if (key_index >= KEYRING_SIZE || key_handler == nullptr ||
if (0 > key_index || key_index >= key_provider_->options().key_ring_size || key_handler == nullptr ||
key_handler->GetKeySet(key_index) == nullptr) {
RTC_LOG(LS_INFO) << "FrameCryptorTransformer::decryptFrame() no keys, or "
"key_index["
<< key_index_ << "] out of range for participant "
<< key_index << "] out of range for participant "
<< participant_id_;
if (last_dec_error_ != FrameCryptionState::kMissingKey) {
last_dec_error_ = FrameCryptionState::kMissingKey;
Expand Down
18 changes: 15 additions & 3 deletions api/crypto/frame_crypto_transformer.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,20 @@ struct KeyProviderOptions {
std::vector<uint8_t> uncrypted_magic_bytes;
int ratchet_window_size;
int failure_tolerance;
// key ring size should be between 1 and 255
int key_ring_size;
KeyProviderOptions()
: shared_key(false), ratchet_window_size(0), failure_tolerance(-1) {}
: shared_key(false),
ratchet_window_size(0),
failure_tolerance(-1),
key_ring_size(KEYRING_SIZE) {}
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),
failure_tolerance(copy.failure_tolerance) {}
failure_tolerance(copy.failure_tolerance),
key_ring_size(copy.key_ring_size) {}
};

class KeyProvider : public rtc::RefCountInterface {
Expand Down Expand Up @@ -99,7 +105,13 @@ class ParticipantKeyHandler : public rtc::RefCountInterface {
public:
ParticipantKeyHandler(KeyProvider* key_provider)
: key_provider_(key_provider) {
crypto_key_ring_.resize(KEYRING_SIZE);
int key_ring_size = key_provider_->options().key_ring_size;
if(key_ring_size <= 0) {
key_ring_size = KEYRING_SIZE;
} else if (key_ring_size >= 255) {
key_ring_size = 255;
}
crypto_key_ring_.resize(key_ring_size);
}

virtual ~ParticipantKeyHandler() = default;
Expand Down
6 changes: 3 additions & 3 deletions sdk/android/api/org/webrtc/FrameCryptorFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

public class FrameCryptorFactory {
public static FrameCryptorKeyProvider createFrameCryptorKeyProvider(
boolean sharedKey, byte[] ratchetSalt, int ratchetWindowSize, byte[] uncryptedMagicBytes, int failureTolerance) {
return nativeCreateFrameCryptorKeyProvider(sharedKey, ratchetSalt, ratchetWindowSize, uncryptedMagicBytes, failureTolerance);
boolean sharedKey, byte[] ratchetSalt, int ratchetWindowSize, byte[] uncryptedMagicBytes, int failureTolerance, int keyRingSize) {
return nativeCreateFrameCryptorKeyProvider(sharedKey, ratchetSalt, ratchetWindowSize, uncryptedMagicBytes, failureTolerance, keyRingSize);
}

public static FrameCryptor createFrameCryptorForRtpSender(PeerConnectionFactory factory, RtpSender rtpSender,
Expand All @@ -40,5 +40,5 @@ private static native FrameCryptor nativeCreateFrameCryptorForRtpReceiver(long f
long rtpReceiver, String participantId, int algorithm, long nativeFrameCryptorKeyProvider);

private static native FrameCryptorKeyProvider nativeCreateFrameCryptorKeyProvider(
boolean sharedKey, byte[] ratchetSalt, int ratchetWindowSize, byte[] uncryptedMagicBytes, int failureTolerance);
boolean sharedKey, byte[] ratchetSalt, int ratchetWindowSize, byte[] uncryptedMagicBytes, int failureTolerance, int keyRingSize);
}
7 changes: 4 additions & 3 deletions sdk/android/src/jni/pc/frame_cryptor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -179,18 +179,19 @@ JNI_FrameCryptorFactory_CreateFrameCryptorKeyProvider(
const base::android::JavaParamRef<jbyteArray>& j_ratchetSalt,
jint j_ratchetWindowSize,
const base::android::JavaParamRef<jbyteArray>& j_uncryptedMagicBytes,
jint j_failureTolerance) {
jint j_failureTolerance,
jint j_keyRingSize) {
auto ratchetSalt = JavaToNativeByteArray(env, j_ratchetSalt);
KeyProviderOptions options;
options.ratchet_salt =
std::vector<uint8_t>(ratchetSalt.begin(), ratchetSalt.end());
options.ratchet_window_size = j_ratchetWindowSize;

auto uncryptedMagicBytes = JavaToNativeByteArray(env, j_uncryptedMagicBytes);
options.uncrypted_magic_bytes =
std::vector<uint8_t>(uncryptedMagicBytes.begin(), uncryptedMagicBytes.end());
options.shared_key = j_shared;
options.failure_tolerance = j_failureTolerance;
options.failure_tolerance = j_failureTolerance;
options.key_ring_size = j_keyRingSize;
return NativeToJavaFrameCryptorKeyProvider(
env, rtc::make_ref_counted<webrtc::DefaultKeyProviderImpl>(options));
}
Expand Down
3 changes: 2 additions & 1 deletion sdk/objc/api/peerconnection/RTCFrameCryptorKeyProvider.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ RTC_OBJC_EXPORT
ratchetWindowSize:(int)windowSize
sharedKeyMode:(BOOL)sharedKey
uncryptedMagicBytes:(nullable NSData *)uncryptedMagicBytes
failureTolerance:(int)failureTolerance;
failureTolerance:(int)failureTolerance
keyRingSize:(int)keyRingSize;

@end

Expand Down
4 changes: 3 additions & 1 deletion sdk/objc/api/peerconnection/RTCFrameCryptorKeyProvider.mm
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,16 @@ - (instancetype)initWithRatchetSalt:(NSData *)salt
ratchetWindowSize:(int)windowSize
sharedKeyMode:(BOOL)sharedKey
uncryptedMagicBytes:(nullable NSData *)uncryptedMagicBytes
failureTolerance:(int)failureTolerance {
failureTolerance:(int)failureTolerance
keyRingSize:(int)keyRingSize {
if (self = [super init]) {
webrtc::KeyProviderOptions options;
options.ratchet_salt = std::vector<uint8_t>((const uint8_t *)salt.bytes,
((const uint8_t *)salt.bytes) + salt.length);
options.ratchet_window_size = windowSize;
options.shared_key = sharedKey;
options.failure_tolerance = failureTolerance;
options.key_ring_size = keyRingSize;
if(uncryptedMagicBytes != nil) {
options.uncrypted_magic_bytes = std::vector<uint8_t>((const uint8_t *)uncryptedMagicBytes.bytes,
((const uint8_t *)uncryptedMagicBytes.bytes) + uncryptedMagicBytes.length);
Expand Down