summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoel Fischer <joeljfischer@gmail.com>2021-04-16 11:17:19 -0400
committerJoel Fischer <joeljfischer@gmail.com>2021-04-16 11:17:19 -0400
commit415b72807ba83dd757e86c16428396a3cca05eca (patch)
tree04443a3447b47427e391944b903ba98b70f6605b
parent89152d813cc2fd4f874e537d799c8c026acd912c (diff)
downloadsdl_ios-415b72807ba83dd757e86c16428396a3cca05eca.tar.gz
Move encryption check to the protocol layer to handle all cases
-rw-r--r--SmartDeviceLink/private/SDLLifecycleManager.m22
-rw-r--r--SmartDeviceLink/private/SDLProtocol.m15
2 files changed, 13 insertions, 24 deletions
diff --git a/SmartDeviceLink/private/SDLLifecycleManager.m b/SmartDeviceLink/private/SDLLifecycleManager.m
index c049a6be1..e0ed529b1 100644
--- a/SmartDeviceLink/private/SDLLifecycleManager.m
+++ b/SmartDeviceLink/private/SDLLifecycleManager.m
@@ -753,19 +753,6 @@ NSString *const BackgroundTaskTransportName = @"com.sdl.transport.backgroundTask
return;
}
-
- if (!request.isPayloadProtected && [self.encryptionLifecycleManager rpcRequiresEncryption:request]) {
- request.payloadProtected = YES;
- }
-
- if (request.isPayloadProtected && !self.encryptionLifecycleManager.isEncryptionReady) {
- SDLLogW(@"Encryption Manager not ready, request not sent (%@)", request);
- if (handler) {
- handler(request, nil, [NSError sdl_encryption_lifecycle_notReadyError]);
- }
-
- return;
- }
[SDLGlobals runSyncOnSerialSubQueue:self.lifecycleQueue block:^{
[self sdl_sendConnectionRequest:request withResponseHandler:handler];
@@ -798,15 +785,6 @@ NSString *const BackgroundTaskTransportName = @"com.sdl.transport.backgroundTask
return;
}
- if (request.isPayloadProtected && !self.encryptionLifecycleManager.isEncryptionReady) {
- SDLLogW(@"Encryption Manager not ready, request not sent (%@)", request);
- if (handler) {
- handler(request, nil, [NSError sdl_encryption_lifecycle_notReadyError]);
- }
-
- return;
- }
-
// Before we send a message, we have to check if we need to adapt the RPC. When adapting the RPC, there could be multiple RPCs that need to be sent.
NSError *error = nil;
NSArray<SDLRPCMessage *> *messages = [SDLLifecycleRPCAdapter adaptRPC:request direction:SDLRPCDirectionOutgoing];
diff --git a/SmartDeviceLink/private/SDLProtocol.m b/SmartDeviceLink/private/SDLProtocol.m
index 13b628bf7..a2f8cc021 100644
--- a/SmartDeviceLink/private/SDLProtocol.m
+++ b/SmartDeviceLink/private/SDLProtocol.m
@@ -291,12 +291,22 @@ NS_ASSUME_NONNULL_BEGIN
- (BOOL)sendRPC:(SDLRPCMessage *)message encrypted:(BOOL)encryption error:(NSError *__autoreleasing *)error {
NSParameterAssert(message != nil);
+
+ // Check that we can send the message over encryption and fail early if we cannot
+ if (message.isPayloadProtected && !self.encryptionLifecycleManager.isEncryptionReady) {
+ SDLLogE(@"Encryption Manager not ready, message not sent (%@)", message);
+ if (error != nil) {
+ *error = [NSError sdl_encryption_lifecycle_notReadyError];
+ }
+ return NO;
+ }
+
+ // Convert the message dictionary to JSON and return early if it fails
NSError *jsonError = nil;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:[message serializeAsDictionary:(Byte)[SDLGlobals sharedGlobals].protocolVersion.major] options:kNilOptions error:&jsonError];
-
if (jsonError != nil) {
+ SDLLogE(@"Error encoding JSON data: %@", jsonError);
*error = jsonError;
- SDLLogW(@"Error encoding JSON data: %@", jsonError);
return NO;
}
@@ -348,6 +358,7 @@ NS_ASSUME_NONNULL_BEGIN
messagePayload = rpcPayload.data;
}
+ // If the encryption failed, pass back the error and return false
if (!messagePayload) {
if (encryptError != nil) {
*error = encryptError;