Skip to content

Commit

Permalink
MXCrossSigning: Gossip the master key
Browse files Browse the repository at this point in the history
  • Loading branch information
manuroe committed Jun 25, 2020
1 parent fa348c9 commit a445438
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 3 deletions.
4 changes: 3 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ Improvements:
* MXCrossSigning: Add the bootstrapWithAuthParams method.
* MXRecoveryService: Create this service to manage keys we want to store in SSSS.
* MXRecoveryService: Add deleteRecovery.
* MXSecretStorage: Add options to remove secrets and SSSS. * MXWellKnown: Add JSONDictionary implementation to return original and extended data.
* MXSecretStorage: Add options to remove secrets and SSSS.
* MXWellKnown: Add JSONDictionary implementation to return original and extended data.
* MXCrossSigning: Gossip the master key (vector-im/riot-ios/issues/3346).

Bug fix:
* MXSecretShareManager: Fix crash in cancelRequestWithRequestId (vector-im/riot-ios/issues/3272).
Expand Down
1 change: 1 addition & 0 deletions MatrixSDK/Crypto/CrossSigning/MXCrossSigning.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ typedef NS_ENUM(NSInteger, MXCrossSigningErrorCode)
@property (nonatomic, readonly) MXCrossSigningState state;
@property (nonatomic, readonly) BOOL canTrustCrossSigning;
@property (nonatomic, readonly) BOOL canCrossSign;
@property (nonatomic, readonly) BOOL hasAllPrivateKeys;

/**
Check update for this device cross-signing state (self.state).
Expand Down
57 changes: 55 additions & 2 deletions MatrixSDK/Crypto/CrossSigning/MXCrossSigning.m
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ - (BOOL)canTrustCrossSigning
return (_state >= MXCrossSigningStateTrustCrossSigning);
}

- (BOOL)hasAllPrivateKeys
{
id<MXCryptoStore> cryptoStore = self.crypto.store;

return ([cryptoStore secretWithSecretId:MXSecretId.crossSigningMaster]
&& [cryptoStore secretWithSecretId:MXSecretId.crossSigningSelfSigning]
&& [cryptoStore secretWithSecretId:MXSecretId.crossSigningUserSigning]);
}

- (void)bootstrapWithPassword:(NSString*)password
success:(void (^)(void))success
failure:(void (^)(NSError *error))failure
Expand Down Expand Up @@ -75,6 +84,9 @@ - (void)bootstrapWithAuthParams:(NSDictionary*)authParams
{
MXCredentials *myCreds = _crypto.mxSession.matrixRestClient.credentials;


// TODO: Check if it exists before

// Create keys
NSDictionary<NSString*, NSData*> *privateKeys;
MXCrossSigningInfo *keys = [self createKeys:&privateKeys];
Expand Down Expand Up @@ -282,12 +294,51 @@ - (void)requestPrivateKeysToDeviceIds:(nullable NSArray<NSString*>*)deviceIds
{
NSLog(@"[MXCrossSigning] requestPrivateKeysToDeviceIds: %@", deviceIds);

// Make a secret share request for USK and SSK
// Make a secret share request for MSK, USK and SSK
dispatch_group_t successGroup = dispatch_group_create();
dispatch_group_t onPrivateKeysReceivedGroup = dispatch_group_create();

__block NSString *uskRequestId, *sskRequestId;
__block NSString *mskRequestId, *uskRequestId, *sskRequestId;


// MSK
dispatch_group_enter(successGroup);
dispatch_group_enter(onPrivateKeysReceivedGroup);
[self.crypto.secretShareManager requestSecret:MXSecretId.crossSigningMaster toDeviceIds:deviceIds success:^(NSString * _Nonnull requestId) {
mskRequestId = requestId;
dispatch_group_leave(successGroup);
} onSecretReceived:^BOOL(NSString * _Nonnull secret) {

BOOL isSecretValid = NO;
if (self.myUserCrossSigningKeys.masterKeys.keys)
{
isSecretValid = [self isSecretValid:secret forPublicKeys:self.myUserCrossSigningKeys.masterKeys.keys];
}
else
{
// Accept the secret anyway (It should not happen)
isSecretValid = YES;
}

NSLog(@"[MXCrossSigning] requestPrivateKeysToDeviceIds: Got MSK. isSecretValid: %@", @(isSecretValid));
if (isSecretValid)
{
[self.crypto.store storeSecret:secret withSecretId:MXSecretId.crossSigningMaster];
dispatch_group_leave(onPrivateKeysReceivedGroup);
}
return isSecretValid;
} failure:^(NSError * _Nonnull error) {
// Cancel the other request
if (mskRequestId)
{
[self.crypto.secretShareManager cancelRequestWithRequestId:mskRequestId success:^{} failure:^(NSError * _Nonnull error) {
}];
}
failure(error);
}];


// USK
dispatch_group_enter(successGroup);
dispatch_group_enter(onPrivateKeysReceivedGroup);
[self.crypto.secretShareManager requestSecret:MXSecretId.crossSigningUserSigning toDeviceIds:deviceIds success:^(NSString * _Nonnull requestId) {
Expand Down Expand Up @@ -323,6 +374,8 @@ - (void)requestPrivateKeysToDeviceIds:(nullable NSArray<NSString*>*)deviceIds
failure(error);
}];


// SSK
dispatch_group_enter(successGroup);
dispatch_group_enter(onPrivateKeysReceivedGroup);
[self.crypto.secretShareManager requestSecret:MXSecretId.crossSigningSelfSigning toDeviceIds:deviceIds success:^(NSString * _Nonnull requestId) {
Expand Down
4 changes: 4 additions & 0 deletions MatrixSDKTests/MXCrossSigningTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,7 @@ - (void)testRefreshState
// This simulates a self verification and trigger cross-signing behind the shell
// - The 2nd device requests cross-signing keys from the 1st one
// -> The 2nd device should be able to cross-sign now
// -> The 2nd device must have all cross-signing private keys
- (void)testPrivateKeysGossiping
{
// - Create Alice
Expand Down Expand Up @@ -498,6 +499,9 @@ - (void)testPrivateKeysGossiping

// -> The 2nd device should be able to cross-sign now
XCTAssertEqual(newAliceSession.crypto.crossSigning.state, MXCrossSigningStateCanCrossSign);

// -> The 2nd device must have all cross-signing private keys
XCTAssertTrue(newAliceSession.crypto.crossSigning.hasAllPrivateKeys);
[expectation fulfill];

} failure:^(NSError * _Nonnull error) {
Expand Down

0 comments on commit a445438

Please sign in to comment.