Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accept key backups as usable if they're signed with the master cross-signing key #1492

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 18 additions & 12 deletions MatrixSDK/Crypto/KeyBackup/MXKeyBackup.m
Original file line number Diff line number Diff line change
Expand Up @@ -1139,18 +1139,18 @@ - (MXKeyBackupVersionTrust *)trustForKeyBackupVersionFromCryptoQueue:(MXKeyBacku
for (NSString *keyId in mySigs)
{
// XXX: is this how we're supposed to get the device id?
NSString *deviceId;
NSString *deviceIdOrCrossSigningKey;
NSArray<NSString *> *components = [keyId componentsSeparatedByString:@":"];
if (components.count == 2)
{
deviceId = components[1];
deviceIdOrCrossSigningKey = components[1];
}

if (deviceId)
if (deviceIdOrCrossSigningKey)
{
BOOL valid = NO;

MXDeviceInfo *device = [self->crypto.deviceList storedDevice:myUserId deviceId:deviceId];
MXDeviceInfo *device = [self->crypto.deviceList storedDevice:myUserId deviceId:deviceIdOrCrossSigningKey];
if (device)
{
NSError *error;
Expand All @@ -1162,27 +1162,33 @@ - (MXKeyBackupVersionTrust *)trustForKeyBackupVersionFromCryptoQueue:(MXKeyBacku
}

MXKeyBackupVersionTrustSignature *signature = [MXKeyBackupVersionTrustSignature new];
signature.deviceId = deviceId;
signature.deviceId = device.deviceId;
signature.device = device;
signature.valid = valid;
[signatures addObject:signature];
}
else // Try interpreting it as the MSK public key
else if ([deviceIdOrCrossSigningKey isEqualToString:crypto.crossSigning.myUserCrossSigningKeys.masterKeys.keys])
{
NSError *error;
BOOL valid = [crypto.crossSigning.crossSigningTools pkVerifyObject:authData.JSONDictionary userId:myUserId publicKey:deviceId error:&error];
BOOL valid = [crypto.crossSigning.crossSigningTools pkVerifyObject:authData.JSONDictionary userId:myUserId publicKey:deviceIdOrCrossSigningKey error:&error];

if (!valid)
{
MXLogDebug(@"[MXKeyBackup] trustForKeyBackupVersion: Signature with unknown key %@", deviceId);
MXLogDebug(@"[MXKeyBackup] trustForKeyBackupVersion: Signature with cross-signing master key is invalid");
}
else
{
MXKeyBackupVersionTrustSignature *signature = [MXKeyBackupVersionTrustSignature new];
signature.keys = deviceId;
signature.valid = valid;
[signatures addObject:signature];
keyBackupVersionTrust.usable = YES;
Copy link
Contributor

@Anderas Anderas Jun 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The existing parent else branch does not ensure that the deviceId is indeed a master key (it only mentions it in a comment), as opposed to other type of key. Could you please change the else // Try interpreting it as the MSK public key into something like else if ([deviceId isEqualToString:masterKey])? The master key can be obtained for instance as crypto.crossSigning.myUserCrossSigningKeys.masterKeys.keys. See also Android implementation for reference.

If the comparison check fails, we can log that as another issue.

Additionally it may be worth renaming the deviceId local variable into deviceIdOrCrossSigningKey, otherwise the name can be misleading

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only a month and a half later I've finally addressed this feedback, thanks @Anderas !

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for making the change. I see that we will now be adding a signature object even if the master key verification did not succeed, but it will be set as invalid (plus this copies js / android behaviour) so it sounds good to me

}

MXKeyBackupVersionTrustSignature *signature = [MXKeyBackupVersionTrustSignature new];
signature.keys = deviceIdOrCrossSigningKey;
signature.valid = valid;
[signatures addObject:signature];
}
else
{
MXLogDebug(@"[MXKeyBackup] trustForKeyBackupVersion: Signature with unknown key %@", deviceIdOrCrossSigningKey);
}
}
}
Expand Down
1 change: 1 addition & 0 deletions changelog.d/pr-1492.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Accept key backups as usable if they're signed with the master cross-signing key. Contributed by Brad @ Beeper