Skip to content

Commit

Permalink
add discard_frame_when_cryptor_not_ready to KeyProviderOptions.
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudwebrtc committed Apr 4, 2024
1 parent ddb1f6a commit 1c5389f
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 12 deletions.
8 changes: 6 additions & 2 deletions api/crypto/frame_crypto_transformer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,9 @@ void FrameCryptorTransformer::encryptFrame(
if (date_in.size() == 0 || !enabled_cryption) {
RTC_LOG(LS_WARNING) << "FrameCryptorTransformer::encryptFrame() "
"date_in.size() == 0 || enabled_cryption == false";
sink_callback->OnTransformedFrame(std::move(frame));
if(!key_provider_->options().discard_frame_when_cryptor_not_ready) {
sink_callback->OnTransformedFrame(std::move(frame));
}
return;
}

Expand Down Expand Up @@ -494,7 +496,9 @@ void FrameCryptorTransformer::decryptFrame(
if (date_in.size() == 0 || !enabled_cryption) {
RTC_LOG(LS_WARNING) << "FrameCryptorTransformer::decryptFrame() "
"date_in.size() == 0 || enabled_cryption == false";
sink_callback->OnTransformedFrame(std::move(frame));
if(!key_provider_->options().discard_frame_when_cryptor_not_ready) {
sink_callback->OnTransformedFrame(std::move(frame));
}
return;
}

Expand Down
14 changes: 9 additions & 5 deletions api/crypto/frame_crypto_transformer.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ int DerivePBKDF2KeyFromRawKey(const std::vector<uint8_t> raw_key,

namespace webrtc {

const size_t KEYRING_SIZE = 16;
const size_t DEFAULT_KEYRING_SIZE = 16;
const size_t MAX_KEYRING_SIZE = 255;

class ParticipantKeyHandler;

Expand All @@ -46,11 +47,13 @@ struct KeyProviderOptions {
int failure_tolerance;
// key ring size should be between 1 and 255
int key_ring_size;
bool discard_frame_when_cryptor_not_ready;
KeyProviderOptions()
: shared_key(false),
ratchet_window_size(0),
failure_tolerance(-1),
key_ring_size(KEYRING_SIZE) {}
key_ring_size(DEFAULT_KEYRING_SIZE),
discard_frame_when_cryptor_not_ready(false) {}
KeyProviderOptions(KeyProviderOptions& copy)
: shared_key(copy.shared_key),
ratchet_salt(copy.ratchet_salt),
Expand Down Expand Up @@ -107,9 +110,10 @@ class ParticipantKeyHandler : public rtc::RefCountInterface {
: key_provider_(key_provider) {
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;
key_ring_size = DEFAULT_KEYRING_SIZE;
} else if (key_ring_size > (int)MAX_KEYRING_SIZE) {
// Keyring size needs to be between 1 and 256
key_ring_size = MAX_KEYRING_SIZE;
}
crypto_key_ring_.resize(key_ring_size);
}
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, int keyRingSize) {
return nativeCreateFrameCryptorKeyProvider(sharedKey, ratchetSalt, ratchetWindowSize, uncryptedMagicBytes, failureTolerance, keyRingSize);
boolean sharedKey, byte[] ratchetSalt, int ratchetWindowSize, byte[] uncryptedMagicBytes, int failureTolerance, int keyRingSize, boolean discardFrameWhenCryptorNotReady) {
return nativeCreateFrameCryptorKeyProvider(sharedKey, ratchetSalt, ratchetWindowSize, uncryptedMagicBytes, failureTolerance, keyRingSize, discardFrameWhenCryptorNotReady);
}

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, int keyRingSize);
boolean sharedKey, byte[] ratchetSalt, int ratchetWindowSize, byte[] uncryptedMagicBytes, int failureTolerance, int keyRingSize, boolean discardFrameWhenCryptorNotReady);
}
4 changes: 3 additions & 1 deletion sdk/android/src/jni/pc/frame_cryptor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,8 @@ JNI_FrameCryptorFactory_CreateFrameCryptorKeyProvider(
jint j_ratchetWindowSize,
const base::android::JavaParamRef<jbyteArray>& j_uncryptedMagicBytes,
jint j_failureTolerance,
jint j_keyRingSize) {
jint j_keyRingSize,
jboolean j_discardFrameWhenCryptorNotReady) {
auto ratchetSalt = JavaToNativeByteArray(env, j_ratchetSalt);
KeyProviderOptions options;
options.ratchet_salt =
Expand All @@ -192,6 +193,7 @@ JNI_FrameCryptorFactory_CreateFrameCryptorKeyProvider(
options.shared_key = j_shared;
options.failure_tolerance = j_failureTolerance;
options.key_ring_size = j_keyRingSize;
options.discard_frame_when_cryptor_not_ready = j_discardFrameWhenCryptorNotReady;
return NativeToJavaFrameCryptorKeyProvider(
env, rtc::make_ref_counted<webrtc::DefaultKeyProviderImpl>(options));
}
Expand Down
8 changes: 8 additions & 0 deletions sdk/objc/api/peerconnection/RTCFrameCryptorKeyProvider.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ RTC_OBJC_EXPORT
failureTolerance:(int)failureTolerance
keyRingSize:(int)keyRingSize;

- (instancetype)initWithRatchetSalt:(NSData *)salt
ratchetWindowSize:(int)windowSize
sharedKeyMode:(BOOL)sharedKey
uncryptedMagicBytes:(nullable NSData *)uncryptedMagicBytes
failureTolerance:(int)failureTolerance
keyRingSize:(int)keyRingSize
discardFrameWhenCryptorNotReady:(BOOL)discardFrameWhenCryptorNotReady;

@end

NS_ASSUME_NONNULL_END
20 changes: 19 additions & 1 deletion sdk/objc/api/peerconnection/RTCFrameCryptorKeyProvider.mm
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ - (instancetype)initWithRatchetSalt:(NSData *)salt
ratchetWindowSize:windowSize
sharedKeyMode:sharedKey
uncryptedMagicBytes:uncryptedMagicBytes
failureTolerance:-1];
failureTolerance:-1
keyRingSize:webrtc::DEFAULT_KEYRING_SIZE];
}

- (instancetype)initWithRatchetSalt:(NSData *)salt
Expand All @@ -47,6 +48,22 @@ - (instancetype)initWithRatchetSalt:(NSData *)salt
uncryptedMagicBytes:(nullable NSData *)uncryptedMagicBytes
failureTolerance:(int)failureTolerance
keyRingSize:(int)keyRingSize {
return [self initWithRatchetSalt:salt
ratchetWindowSize:windowSize
sharedKeyMode:sharedKey
uncryptedMagicBytes:uncryptedMagicBytes
failureTolerance:-1
keyRingSize:keyRingSize
discardFrameWhenCryptorNotReady:false];
}

- (instancetype)initWithRatchetSalt:(NSData *)salt
ratchetWindowSize:(int)windowSize
sharedKeyMode:(BOOL)sharedKey
uncryptedMagicBytes:(nullable NSData *)uncryptedMagicBytes
failureTolerance:(int)failureTolerance
keyRingSize:(int)keyRingSize
discardFrameWhenCryptorNotReady:(BOOL)discardFrameWhenCryptorNotReady {
if (self = [super init]) {
webrtc::KeyProviderOptions options;
options.ratchet_salt = std::vector<uint8_t>((const uint8_t *)salt.bytes,
Expand All @@ -55,6 +72,7 @@ - (instancetype)initWithRatchetSalt:(NSData *)salt
options.shared_key = sharedKey;
options.failure_tolerance = failureTolerance;
options.key_ring_size = keyRingSize;
options.discard_frame_when_cryptor_not_ready = discardFrameWhenCryptorNotReady;
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

0 comments on commit 1c5389f

Please sign in to comment.