Skip to content

Commit 50b6436

Browse files
authored
Add key ring size to keyProviderOptions. (#109)
* Add key ring size to keyProviderOptions. * add discard_frame_when_cryptor_not_ready to KeyProviderOptions. * update.
1 parent 3cdeeb0 commit 50b6436

File tree

6 files changed

+70
-15
lines changed

6 files changed

+70
-15
lines changed

api/crypto/frame_crypto_transformer.cc

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,9 @@ void FrameCryptorTransformer::encryptFrame(
386386
if (date_in.size() == 0 || !enabled_cryption) {
387387
RTC_LOG(LS_WARNING) << "FrameCryptorTransformer::encryptFrame() "
388388
"date_in.size() == 0 || enabled_cryption == false";
389+
if(key_provider_->options().discard_frame_when_cryptor_not_ready) {
390+
return;
391+
}
389392
sink_callback->OnTransformedFrame(std::move(frame));
390393
return;
391394
}
@@ -494,6 +497,10 @@ void FrameCryptorTransformer::decryptFrame(
494497
if (date_in.size() == 0 || !enabled_cryption) {
495498
RTC_LOG(LS_WARNING) << "FrameCryptorTransformer::decryptFrame() "
496499
"date_in.size() == 0 || enabled_cryption == false";
500+
if(key_provider_->options().discard_frame_when_cryptor_not_ready) {
501+
return;
502+
}
503+
497504
sink_callback->OnTransformedFrame(std::move(frame));
498505
return;
499506
}
@@ -551,11 +558,11 @@ void FrameCryptorTransformer::decryptFrame(
551558
? key_provider_->GetSharedKey(participant_id_)
552559
: key_provider_->GetKey(participant_id_);
553560

554-
if (key_index >= KEYRING_SIZE || key_handler == nullptr ||
561+
if (0 > key_index || key_index >= key_provider_->options().key_ring_size || key_handler == nullptr ||
555562
key_handler->GetKeySet(key_index) == nullptr) {
556563
RTC_LOG(LS_INFO) << "FrameCryptorTransformer::decryptFrame() no keys, or "
557564
"key_index["
558-
<< key_index_ << "] out of range for participant "
565+
<< key_index << "] out of range for participant "
559566
<< participant_id_;
560567
if (last_dec_error_ != FrameCryptionState::kMissingKey) {
561568
last_dec_error_ = FrameCryptionState::kMissingKey;

api/crypto/frame_crypto_transformer.h

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ int DerivePBKDF2KeyFromRawKey(const std::vector<uint8_t> raw_key,
3434

3535
namespace webrtc {
3636

37-
const size_t KEYRING_SIZE = 16;
37+
const size_t DEFAULT_KEYRING_SIZE = 16;
38+
const size_t MAX_KEYRING_SIZE = 255;
3839

3940
class ParticipantKeyHandler;
4041

@@ -44,14 +45,22 @@ struct KeyProviderOptions {
4445
std::vector<uint8_t> uncrypted_magic_bytes;
4546
int ratchet_window_size;
4647
int failure_tolerance;
48+
// key ring size should be between 1 and 255
49+
int key_ring_size;
50+
bool discard_frame_when_cryptor_not_ready;
4751
KeyProviderOptions()
48-
: shared_key(false), ratchet_window_size(0), failure_tolerance(-1) {}
52+
: shared_key(false),
53+
ratchet_window_size(0),
54+
failure_tolerance(-1),
55+
key_ring_size(DEFAULT_KEYRING_SIZE),
56+
discard_frame_when_cryptor_not_ready(false) {}
4957
KeyProviderOptions(KeyProviderOptions& copy)
5058
: shared_key(copy.shared_key),
5159
ratchet_salt(copy.ratchet_salt),
5260
uncrypted_magic_bytes(copy.uncrypted_magic_bytes),
5361
ratchet_window_size(copy.ratchet_window_size),
54-
failure_tolerance(copy.failure_tolerance) {}
62+
failure_tolerance(copy.failure_tolerance),
63+
key_ring_size(copy.key_ring_size) {}
5564
};
5665

5766
class KeyProvider : public rtc::RefCountInterface {
@@ -99,7 +108,14 @@ class ParticipantKeyHandler : public rtc::RefCountInterface {
99108
public:
100109
ParticipantKeyHandler(KeyProvider* key_provider)
101110
: key_provider_(key_provider) {
102-
crypto_key_ring_.resize(KEYRING_SIZE);
111+
int key_ring_size = key_provider_->options().key_ring_size;
112+
if(key_ring_size <= 0) {
113+
key_ring_size = DEFAULT_KEYRING_SIZE;
114+
} else if (key_ring_size > (int)MAX_KEYRING_SIZE) {
115+
// Keyring size needs to be between 1 and 256
116+
key_ring_size = MAX_KEYRING_SIZE;
117+
}
118+
crypto_key_ring_.resize(key_ring_size);
103119
}
104120

105121
virtual ~ParticipantKeyHandler() = default;

sdk/android/api/org/webrtc/FrameCryptorFactory.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818

1919
public class FrameCryptorFactory {
2020
public static FrameCryptorKeyProvider createFrameCryptorKeyProvider(
21-
boolean sharedKey, byte[] ratchetSalt, int ratchetWindowSize, byte[] uncryptedMagicBytes, int failureTolerance) {
22-
return nativeCreateFrameCryptorKeyProvider(sharedKey, ratchetSalt, ratchetWindowSize, uncryptedMagicBytes, failureTolerance);
21+
boolean sharedKey, byte[] ratchetSalt, int ratchetWindowSize, byte[] uncryptedMagicBytes, int failureTolerance, int keyRingSize, boolean discardFrameWhenCryptorNotReady) {
22+
return nativeCreateFrameCryptorKeyProvider(sharedKey, ratchetSalt, ratchetWindowSize, uncryptedMagicBytes, failureTolerance, keyRingSize, discardFrameWhenCryptorNotReady);
2323
}
2424

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

4242
private static native FrameCryptorKeyProvider nativeCreateFrameCryptorKeyProvider(
43-
boolean sharedKey, byte[] ratchetSalt, int ratchetWindowSize, byte[] uncryptedMagicBytes, int failureTolerance);
43+
boolean sharedKey, byte[] ratchetSalt, int ratchetWindowSize, byte[] uncryptedMagicBytes, int failureTolerance, int keyRingSize, boolean discardFrameWhenCryptorNotReady);
4444
}

sdk/android/src/jni/pc/frame_cryptor.cc

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,18 +179,21 @@ JNI_FrameCryptorFactory_CreateFrameCryptorKeyProvider(
179179
const base::android::JavaParamRef<jbyteArray>& j_ratchetSalt,
180180
jint j_ratchetWindowSize,
181181
const base::android::JavaParamRef<jbyteArray>& j_uncryptedMagicBytes,
182-
jint j_failureTolerance) {
182+
jint j_failureTolerance,
183+
jint j_keyRingSize,
184+
jboolean j_discardFrameWhenCryptorNotReady) {
183185
auto ratchetSalt = JavaToNativeByteArray(env, j_ratchetSalt);
184186
KeyProviderOptions options;
185187
options.ratchet_salt =
186188
std::vector<uint8_t>(ratchetSalt.begin(), ratchetSalt.end());
187189
options.ratchet_window_size = j_ratchetWindowSize;
188-
189190
auto uncryptedMagicBytes = JavaToNativeByteArray(env, j_uncryptedMagicBytes);
190191
options.uncrypted_magic_bytes =
191192
std::vector<uint8_t>(uncryptedMagicBytes.begin(), uncryptedMagicBytes.end());
192193
options.shared_key = j_shared;
193-
options.failure_tolerance = j_failureTolerance;
194+
options.failure_tolerance = j_failureTolerance;
195+
options.key_ring_size = j_keyRingSize;
196+
options.discard_frame_when_cryptor_not_ready = j_discardFrameWhenCryptorNotReady;
194197
return NativeToJavaFrameCryptorKeyProvider(
195198
env, rtc::make_ref_counted<webrtc::DefaultKeyProviderImpl>(options));
196199
}

sdk/objc/api/peerconnection/RTCFrameCryptorKeyProvider.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,16 @@ RTC_OBJC_EXPORT
4646
ratchetWindowSize:(int)windowSize
4747
sharedKeyMode:(BOOL)sharedKey
4848
uncryptedMagicBytes:(nullable NSData *)uncryptedMagicBytes
49-
failureTolerance:(int)failureTolerance;
49+
failureTolerance:(int)failureTolerance
50+
keyRingSize:(int)keyRingSize;
51+
52+
- (instancetype)initWithRatchetSalt:(NSData *)salt
53+
ratchetWindowSize:(int)windowSize
54+
sharedKeyMode:(BOOL)sharedKey
55+
uncryptedMagicBytes:(nullable NSData *)uncryptedMagicBytes
56+
failureTolerance:(int)failureTolerance
57+
keyRingSize:(int)keyRingSize
58+
discardFrameWhenCryptorNotReady:(BOOL)discardFrameWhenCryptorNotReady;
5059

5160
@end
5261

sdk/objc/api/peerconnection/RTCFrameCryptorKeyProvider.mm

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,41 @@ - (instancetype)initWithRatchetSalt:(NSData *)salt
3838
ratchetWindowSize:windowSize
3939
sharedKeyMode:sharedKey
4040
uncryptedMagicBytes:uncryptedMagicBytes
41-
failureTolerance:-1];
41+
failureTolerance:-1
42+
keyRingSize:webrtc::DEFAULT_KEYRING_SIZE];
4243
}
4344

4445
- (instancetype)initWithRatchetSalt:(NSData *)salt
4546
ratchetWindowSize:(int)windowSize
4647
sharedKeyMode:(BOOL)sharedKey
4748
uncryptedMagicBytes:(nullable NSData *)uncryptedMagicBytes
48-
failureTolerance:(int)failureTolerance {
49+
failureTolerance:(int)failureTolerance
50+
keyRingSize:(int)keyRingSize {
51+
return [self initWithRatchetSalt:salt
52+
ratchetWindowSize:windowSize
53+
sharedKeyMode:sharedKey
54+
uncryptedMagicBytes:uncryptedMagicBytes
55+
failureTolerance:-1
56+
keyRingSize:keyRingSize
57+
discardFrameWhenCryptorNotReady:false];
58+
}
59+
60+
- (instancetype)initWithRatchetSalt:(NSData *)salt
61+
ratchetWindowSize:(int)windowSize
62+
sharedKeyMode:(BOOL)sharedKey
63+
uncryptedMagicBytes:(nullable NSData *)uncryptedMagicBytes
64+
failureTolerance:(int)failureTolerance
65+
keyRingSize:(int)keyRingSize
66+
discardFrameWhenCryptorNotReady:(BOOL)discardFrameWhenCryptorNotReady {
4967
if (self = [super init]) {
5068
webrtc::KeyProviderOptions options;
5169
options.ratchet_salt = std::vector<uint8_t>((const uint8_t *)salt.bytes,
5270
((const uint8_t *)salt.bytes) + salt.length);
5371
options.ratchet_window_size = windowSize;
5472
options.shared_key = sharedKey;
5573
options.failure_tolerance = failureTolerance;
74+
options.key_ring_size = keyRingSize;
75+
options.discard_frame_when_cryptor_not_ready = discardFrameWhenCryptorNotReady;
5676
if(uncryptedMagicBytes != nil) {
5777
options.uncrypted_magic_bytes = std::vector<uint8_t>((const uint8_t *)uncryptedMagicBytes.bytes,
5878
((const uint8_t *)uncryptedMagicBytes.bytes) + uncryptedMagicBytes.length);

0 commit comments

Comments
 (0)