Skip to content

Commit

Permalink
element-hq/element-ios/issues/4406 - Implemented Olm fallback key sup…
Browse files Browse the repository at this point in the history
…port.
  • Loading branch information
stefanceriu committed Aug 30, 2021
1 parent 71fa179 commit bd1c541
Show file tree
Hide file tree
Showing 13 changed files with 504 additions and 52 deletions.
5 changes: 3 additions & 2 deletions MatrixSDK/Contrib/Swift/MXRestClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1727,13 +1727,14 @@ public extension MXRestClient {
- parameters:
- deviceKeys: the device keys to send.
- oneTimeKeys: the one-time keys to send.
- fallbackKeys: the fallback keys to send
- completion: A block object called when the operation completes.
- response: Provides information about the keys on success.

- returns: a `MXHTTPOperation` instance.
*/
@nonobjc @discardableResult func uploadKeys(_ deviceKeys: [String: Any], oneTimeKeys: [String: Any], forDevice deviceId: String? = nil, completion: @escaping (_ response: MXResponse<MXKeysUploadResponse>) -> Void) -> MXHTTPOperation {
return __uploadKeys(deviceKeys, oneTimeKeys: oneTimeKeys, success: currySuccess(completion), failure: curryFailure(completion))
@nonobjc @discardableResult func uploadKeys(_ deviceKeys: [String: Any]?, oneTimeKeys: [String: Any]?, fallbackKeys: [String: Any]?, forDevice deviceId: String? = nil, completion: @escaping (_ response: MXResponse<MXKeysUploadResponse>) -> Void) -> MXHTTPOperation {
return __uploadKeys(deviceKeys, oneTimeKeys: oneTimeKeys, fallbackKeys: fallbackKeys, success: currySuccess(completion), failure: curryFailure(completion))
}

/**
Expand Down
7 changes: 7 additions & 0 deletions MatrixSDK/Crypto/MXCrypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,13 @@ extern NSString *const MXDeviceListDidUpdateUsersDevicesNotification;
*/
- (void)handleDeviceOneTimeKeysCount:(NSDictionary<NSString *, NSNumber*>*)deviceOneTimeKeysCount;

/**
Handle the unused fallback keys returned in the /sync response.
@param deviceUnusedFallbackKeys the algorithms for which there are unused fallback keys
*/
- (void)handleDeviceUnusedFallbackKeys:(NSArray<NSString *> *)deviceUnusedFallbackKeys;

/**
Handle a room key event.
Expand Down
142 changes: 100 additions & 42 deletions MatrixSDK/Crypto/MXCrypto.m

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions MatrixSDK/Crypto/MXOlmDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,20 @@
*/
- (void)generateOneTimeKeys:(NSUInteger)numKeys;

/**
The current fallback key for this account.
@return a dictionary with one key which is "curve25519".
Its value is a dictionary where keys are keys ids
and values, the Curve25519 keys.
*/
@property (nonatomic, readonly) NSDictionary *fallbackKey;

/**
Generate a new fallback key
*/
- (void)generateFallbackKey;

/**
Generate a new outbound session.
Expand Down
12 changes: 12 additions & 0 deletions MatrixSDK/Crypto/MXOlmDevice.m
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,18 @@ - (void)generateOneTimeKeys:(NSUInteger)numKeys
}];
}

- (NSDictionary *)fallbackKey
{
return store.account.fallbackKey;
}

- (void)generateFallbackKey
{
[store performAccountOperationWithBlock:^(OLMAccount *olmAccount) {
[olmAccount generateFallbackKey];
}];
}

- (NSString *)createOutboundSession:(NSString *)theirIdentityKey theirOneTimeKey:(NSString *)theirOneTimeKey
{
NSError *error;
Expand Down
5 changes: 5 additions & 0 deletions MatrixSDK/JSONModels/Sync/MXSyncResponse.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ NS_ASSUME_NONNULL_BEGIN
*/
@property (nonatomic, nullable) NSDictionary<NSString *, NSNumber*> *deviceOneTimeKeysCount;

/**
List of algorithms for which the server has unused fallback keys
*/
@property (nonatomic, nullable) NSArray<NSString*> *unusedFallbackKeys;

/**
List of rooms.
*/
Expand Down
7 changes: 7 additions & 0 deletions MatrixSDK/JSONModels/Sync/MXSyncResponse.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#import "MXRoomsSyncResponse.h"
#import "MXGroupsSyncResponse.h"

static NSString * const kMXDeviceUnusedFallbackKeyTypesKey = @"org.matrix.msc2732.device_unused_fallback_key_types";

@implementation MXSyncResponse

+ (id)modelFromJSON:(NSDictionary *)JSONDictionary
Expand All @@ -35,6 +37,7 @@ + (id)modelFromJSON:(NSDictionary *)JSONDictionary
MXJSONModelSetMXJSONModel(syncResponse.toDevice, MXToDeviceSyncResponse, JSONDictionary[@"to_device"]);
MXJSONModelSetMXJSONModel(syncResponse.deviceLists, MXDeviceListResponse, JSONDictionary[@"device_lists"]);
MXJSONModelSetDictionary(syncResponse.deviceOneTimeKeysCount, JSONDictionary[@"device_one_time_keys_count"])
MXJSONModelSetArray(syncResponse.unusedFallbackKeys, JSONDictionary[kMXDeviceUnusedFallbackKeyTypesKey])
MXJSONModelSetMXJSONModel(syncResponse.rooms, MXRoomsSyncResponse, JSONDictionary[@"rooms"]);
MXJSONModelSetMXJSONModel(syncResponse.groups, MXGroupsSyncResponse, JSONDictionary[@"groups"]);
}
Expand Down Expand Up @@ -67,6 +70,10 @@ - (NSDictionary *)JSONDictionary
{
JSONDictionary[@"device_one_time_keys_count"] = self.deviceOneTimeKeysCount;
}
if (self.unusedFallbackKeys)
{
JSONDictionary[kMXDeviceUnusedFallbackKeyTypesKey] = self.unusedFallbackKeys;
}
if (self.rooms)
{
JSONDictionary[@"rooms"] = self.rooms.JSONDictionary;
Expand Down
5 changes: 4 additions & 1 deletion MatrixSDK/MXRestClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -2137,13 +2137,16 @@ Get the maximum size a media upload can be in bytes.
@param deviceKeys the device keys to send.
@param oneTimeKeys the one-time keys to send.
@param fallbackKeys the fallback keys to send.
@param success A block object called when the operation succeeds.
@param failure A block object called when the operation fails.
@return a MXHTTPOperation instance.
*/
- (MXHTTPOperation*)uploadKeys:(NSDictionary*)deviceKeys oneTimeKeys:(NSDictionary*)oneTimeKeys
- (MXHTTPOperation*)uploadKeys:(NSDictionary*)deviceKeys
oneTimeKeys:(NSDictionary*)oneTimeKeys
fallbackKeys:(NSDictionary *)fallbackKeys
success:(void (^)(MXKeysUploadResponse *keysUploadResponse))success
failure:(void (^)(NSError *error))failure NS_REFINED_FOR_SWIFT;

Expand Down
14 changes: 11 additions & 3 deletions MatrixSDK/MXRestClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -4046,14 +4046,18 @@ - (MXHTTPOperation*)searchUsers:(NSString*)pattern


#pragma mark - Crypto
- (MXHTTPOperation*)uploadKeys:(NSDictionary*)deviceKeys oneTimeKeys:(NSDictionary*)oneTimeKeys
- (MXHTTPOperation*)uploadKeys:(NSDictionary*)deviceKeys
oneTimeKeys:(NSDictionary*)oneTimeKeys
fallbackKeys:(NSDictionary *)fallbackKeys
success:(void (^)(MXKeysUploadResponse *keysUploadResponse))success
failure:(void (^)(NSError *error))failure
{
return [self uploadKeys:deviceKeys oneTimeKeys:oneTimeKeys forDeviceWithId:nil success:success failure:failure];
return [self uploadKeys:deviceKeys oneTimeKeys:oneTimeKeys fallbackKeys:fallbackKeys forDeviceWithId:nil success:success failure:failure];
}

- (MXHTTPOperation*)uploadKeys:(NSDictionary*)deviceKeys oneTimeKeys:(NSDictionary*)oneTimeKeys
- (MXHTTPOperation*)uploadKeys:(NSDictionary*)deviceKeys
oneTimeKeys:(NSDictionary*)oneTimeKeys
fallbackKeys:(NSDictionary *)fallbackKeys
forDeviceWithId:(NSString*)deviceId
success:(void (^)(MXKeysUploadResponse *keysUploadResponse))success
failure:(void (^)(NSError *error))failure
Expand All @@ -4069,6 +4073,10 @@ - (MXHTTPOperation*)uploadKeys:(NSDictionary*)deviceKeys oneTimeKeys:(NSDictiona
{
parameters[@"one_time_keys"] = oneTimeKeys;
}
if (fallbackKeys)
{
parameters[@"org.matrix.msc2732.fallback_keys"] = fallbackKeys;
}

MXWeakify(self);
return [httpClient requestWithMethod:@"POST"
Expand Down
2 changes: 2 additions & 0 deletions MatrixSDK/MXSession.m
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,8 @@ - (void)handleSyncResponse:(MXSyncResponse *)syncResponse
[self.crypto handleDeviceOneTimeKeysCount:syncResponse.deviceOneTimeKeysCount];
}

[self.crypto handleDeviceUnusedFallbackKeys:syncResponse.unusedFallbackKeys];

// Tell the crypto module to do its processing
[self.crypto onSyncCompleted:self.store.eventStreamToken
nextSyncToken:syncResponse.nextBatch
Expand Down
Loading

0 comments on commit bd1c541

Please sign in to comment.