Skip to content

Commit

Permalink
MXCrypto: Reset OTKs in case some IDs are already used.
Browse files Browse the repository at this point in the history
This is a mitigation for element-hq/element-ios#3721. But it does not fix the root issue.
  • Loading branch information
manuroe committed Oct 8, 2020
1 parent 1ed5cac commit 6f2f70d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
2 changes: 1 addition & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Changes to be released in next version
* MXCrypto: Add hasKeysToDecryptEvent method.

🐛 Bugfix
*
* MXCrypto: Reset OTKs in case some IDs are already used (https://github.com/vector-im/element-ios/issues/3721).

⚠️ API Changes
*
Expand Down
23 changes: 19 additions & 4 deletions MatrixSDK/Crypto/MXCrypto.m
Original file line number Diff line number Diff line change
Expand Up @@ -2641,7 +2641,7 @@ - (void)maybeUploadOneTimeKeys:(void (^)(void))success failure:(void (^)(NSError
NSLog(@"[MXCrypto] maybeUploadOneTimeKeys: there are %tu one-time keys on the homeserver", oneTimeKeyCount);

MXWeakify(self);
uploadOneTimeKeysOperation = [self generateAndUploadOneTimeKeys:oneTimeKeyCount success:^{
uploadOneTimeKeysOperation = [self generateAndUploadOneTimeKeys:oneTimeKeyCount retry:YES success:^{
MXStrongifyAndReturnIfNil(self);

self->uploadOneTimeKeysOperation = nil;
Expand Down Expand Up @@ -2695,7 +2695,7 @@ - (void)maybeUploadOneTimeKeys:(void (^)(void))success failure:(void (^)(NSError
NSLog(@"[MXCrypto] maybeUploadOneTimeKeys: %@ one-time keys on the homeserver", @(keyCount));

MXWeakify(self);
MXHTTPOperation *operation2 = [self generateAndUploadOneTimeKeys:keyCount success:^{
MXHTTPOperation *operation2 = [self generateAndUploadOneTimeKeys:keyCount retry:YES success:^{
MXStrongifyAndReturnIfNil(self);

self->uploadOneTimeKeysOperation = nil;
Expand Down Expand Up @@ -2743,7 +2743,7 @@ - (void)maybeUploadOneTimeKeys:(void (^)(void))success failure:(void (^)(NSError
}
}

- (MXHTTPOperation *)generateAndUploadOneTimeKeys:(NSUInteger)keyCount success:(void (^)(void))success failure:(void (^)(NSError *))failure
- (MXHTTPOperation *)generateAndUploadOneTimeKeys:(NSUInteger)keyCount retry:(BOOL)retry success:(void (^)(void))success failure:(void (^)(NSError *))failure
{
MXHTTPOperation *operation;

Expand All @@ -2753,7 +2753,22 @@ - (MXHTTPOperation *)generateAndUploadOneTimeKeys:(NSUInteger)keyCount success:(
success();
} failure:^(NSError *error) {
NSLog(@"[MXCrypto] generateAndUploadOneTimeKeys: Failed to publish one-time keys. Error: %@", error);
failure(error);

if ([MXError isMXError:error] && retry)
{
// The homeserver explicitly rejected the request.
// Reset local OTKs we tried to push and retry
// There is no matrix specific error but we really want to detect the error described at
// https://github.com/vector-im/element-ios/issues/3721
NSLog(@"[MXCrypto] uploadOneTimeKeys: Reset local OTKs because the server does not like them");
[self.olmDevice markOneTimeKeysAsPublished];

[self generateAndUploadOneTimeKeys:keyCount retry:NO success:success failure:failure];
}
else
{
failure(error);
}
}];
}

Expand Down

0 comments on commit 6f2f70d

Please sign in to comment.