Skip to content

Commit c965110

Browse files
committed
pr suggestions part 1
1 parent ca76c18 commit c965110

File tree

6 files changed

+14
-175
lines changed

6 files changed

+14
-175
lines changed

MatrixSDK/Crypto/KeyBackup/MXKeyBackupPassword.h

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,6 @@ NS_ASSUME_NONNULL_BEGIN
3434
*/
3535
+ (nullable NSData *)generatePrivateKeyWithPassword:(NSString*)password salt:(NSString * _Nullable *_Nonnull)salt iterations:(NSUInteger*)iterations error:(NSError * _Nullable *)error;
3636

37-
/**
38-
Retrieve a private key from {password, salt, iterations}
39-
40-
@param password the password used to generated the private key.
41-
@param salt the salt.
42-
@param iterations number of key derivations
43-
@param error the output error
44-
@return a private key.
45-
*/
46-
+ (nullable NSData *)retrievePrivateKeyWithPassword:(NSString*)password salt:(NSString*)salt iterations:(NSUInteger)iterations error:(NSError * _Nullable *)error;
47-
4837
@end
4938

5039
NS_ASSUME_NONNULL_END

MatrixSDK/Crypto/KeyBackup/MXKeyBackupPassword.m

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,4 @@ + (NSData *)generatePrivateKeyWithPassword:(NSString *)password salt:(NSString *
3939
return nil;
4040
}
4141

42-
+ (NSData *)retrievePrivateKeyWithPassword:(NSString *)password salt:(NSString *)salt iterations:(NSUInteger)iterations error:(NSError *__autoreleasing _Nullable *)error
43-
{
44-
return nil;
45-
}
46-
4742
@end

MatrixSDK/Crypto/KeyBackup/MXRecoveryKey.m

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,20 @@ + (NSData *)decode:(NSString *)recoveryKey error:(NSError **)error
6969
}];
7070
return nil;
7171
}
72+
73+
if (result.length !=
74+
sizeof(kOlmRecoveryKeyPrefix) + [OLMPkDecryption privateKeyLength] + 1)
75+
{
76+
if (error)
77+
{
78+
*error = [NSError errorWithDomain:MXRecoveryKeyErrorDomain
79+
code:MXRecoveryKeyErrorLengthCode
80+
userInfo:@{
81+
NSLocalizedDescriptionKey: @"Incorrect length",
82+
}];
83+
}
84+
return nil;
85+
}
7286

7387
// Check the checksum
7488
UInt8 parity = 0;

MatrixSDK/Crypto/Recovery/MXRecoveryService.h

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -222,21 +222,6 @@ typedef NS_ENUM(NSInteger, MXRecoveryServiceErrorCode)
222222
*/
223223
- (nullable NSData*)privateKeyFromRecoveryKey:(NSString*)recoveryKey error:(NSError**)error;
224224

225-
/**
226-
Convert a passphrase into the private key.
227-
228-
This method is supposed to take time to avoid brut force attacks.
229-
230-
@param passphrase the passphrase
231-
232-
@param success A block object called when the operation succeeds.
233-
@param failure A block object called when the operation fails.
234-
*/
235-
- (void)privateKeyFromPassphrase:(NSString*)passphrase
236-
success:(void (^)(NSData *privateKey))success
237-
failure:(void (^)(NSError *error))failure;
238-
239-
240225
@end
241226

242227
NS_ASSUME_NONNULL_END

MatrixSDK/Crypto/Recovery/MXRecoveryService.m

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -769,60 +769,6 @@ - (nullable NSData*)privateKeyFromRecoveryKey:(NSString*)recoveryKey error:(NSEr
769769
return privateKey;
770770
}
771771

772-
- (void)privateKeyFromPassphrase:(NSString*)passphrase
773-
success:(void (^)(NSData *privateKey))success
774-
failure:(void (^)(NSError *error))failure
775-
{
776-
NSString *recoveryId = self.recoveryId;
777-
if (!recoveryId)
778-
{
779-
// No SSSS
780-
NSError *error = [NSError errorWithDomain:MXRecoveryServiceErrorDomain
781-
code:MXRecoveryServiceNoSSSSErrorCode
782-
userInfo:@{
783-
NSLocalizedDescriptionKey: @"MXRecoveryService: The account has no secret storage",
784-
}];
785-
failure(error);
786-
return;
787-
}
788-
789-
MXSecretStorageKeyContent *keyContent = [self.dependencies.secretStorage keyWithKeyId:self.recoveryId];
790-
if (!keyContent.passphrase)
791-
{
792-
// No passphrase
793-
NSError *error = [NSError errorWithDomain:MXRecoveryServiceErrorDomain
794-
code:MXRecoveryServiceNotProtectedByPassphraseErrorCode
795-
userInfo:@{
796-
NSLocalizedDescriptionKey: @"MXRecoveryService: Secret storage not protected by a passphrase",
797-
}];
798-
failure(error);
799-
return;
800-
}
801-
802-
803-
// Go to a queue for derivating the passphrase into a recovery key
804-
dispatch_async(self.dependencies.cryptoQueue, ^{
805-
806-
NSError *error;
807-
NSData *privateKey = [MXKeyBackupPassword retrievePrivateKeyWithPassword:passphrase
808-
salt:keyContent.passphrase.salt
809-
iterations:keyContent.passphrase.iterations
810-
error:&error];
811-
812-
813-
dispatch_async(dispatch_get_main_queue(), ^{
814-
if (privateKey)
815-
{
816-
success(privateKey);
817-
}
818-
else
819-
{
820-
failure(error);
821-
}
822-
});
823-
});
824-
}
825-
826772

827773
#pragma mark - Private methods -
828774

MatrixSDKTests/MXCryptoRecoveryServiceTests.m

Lines changed: 0 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -111,47 +111,6 @@ - (void)doTestWithAliceWithCrossSigningAndKeyBackup:(XCTestCase*)testCase
111111
}];
112112
}
113113

114-
// Test privateKeyFromRecoveryKey & privateKeyFromPassphrase
115-
//
116-
// - Have Alice with cross-signing bootstrapped
117-
// - Create a recovery with a passphrase
118-
// -> privateKeyFromRecoveryKey must return the same private key
119-
// -> privateKeyFromPassphrase must return the same private key
120-
- (void)testPrivateKeyTools
121-
{
122-
// - Have Alice with cross-signing bootstrapped
123-
[self doTestWithAliceWithCrossSigning:self readyToTest:^(MXSession *aliceSession, NSString *roomId, XCTestExpectation *expectation) {
124-
125-
MXRecoveryService *recoveryService = aliceSession.crypto.recoveryService;
126-
127-
// - Create a recovery with a passphrase
128-
NSString *passphrase = @"A passphrase";
129-
[recoveryService createRecoveryForSecrets:nil withPassphrase:passphrase createServicesBackups:NO success:^(MXSecretStorageKeyCreationInfo * _Nonnull keyCreationInfo) {
130-
131-
// -> privateKeyFromRecoveryKey must return the same private key
132-
NSError *error;
133-
NSData *privateKeyFromRecoveryKey = [recoveryService privateKeyFromRecoveryKey:keyCreationInfo.recoveryKey error:&error];
134-
XCTAssertNil(error);
135-
XCTAssertEqualObjects(privateKeyFromRecoveryKey, keyCreationInfo.privateKey);
136-
137-
// -> privateKeyFromPassphrase must return the same private key
138-
[recoveryService privateKeyFromPassphrase:passphrase success:^(NSData * _Nonnull privateKey) {
139-
140-
XCTAssertEqualObjects(privateKey, keyCreationInfo.privateKey);
141-
142-
[expectation fulfill];
143-
} failure:^(NSError * _Nonnull error) {
144-
XCTFail(@"The operation should not fail - NSError: %@", error);
145-
[expectation fulfill];
146-
}];
147-
148-
} failure:^(NSError * _Nonnull error) {
149-
XCTFail(@"The operation should not fail - NSError: %@", error);
150-
[expectation fulfill];
151-
}];
152-
}];
153-
}
154-
155114

156115
// Test bad recovery key string format
157116
//
@@ -179,55 +138,6 @@ - (void)testBadRecoveryKeyFormat
179138
}];
180139
}
181140

182-
// Test wrong private key
183-
//
184-
// - Have Alice with cross-signing bootstrapped
185-
// - Create a recovery with a passphrase
186-
// - Build a bad recovery key from a bad passphrase
187-
// - Try to recover with this bad key
188-
// -> It must error with expected NSError domain and code
189-
- (void)testWrongRecoveryKey
190-
{
191-
// - Have Alice with cross-signing bootstrapped
192-
[self doTestWithAliceWithCrossSigning:self readyToTest:^(MXSession *aliceSession, NSString *roomId, XCTestExpectation *expectation) {
193-
194-
MXRecoveryService *recoveryService = aliceSession.crypto.recoveryService;
195-
196-
// - Create a recovery with a passphrase
197-
[recoveryService createRecoveryForSecrets:nil withPassphrase:@"A passphrase" createServicesBackups:NO success:^(MXSecretStorageKeyCreationInfo * _Nonnull keyCreationInfo) {
198-
199-
// - Build a bad recovery key from a bad passphrase
200-
[recoveryService privateKeyFromPassphrase:@"A bad passphrase" success:^(NSData * _Nonnull badPrivateKey) {
201-
202-
// - Try to recover with this bad key
203-
[recoveryService recoverSecrets:nil withPrivateKey:badPrivateKey recoverServices:NO success:^(MXSecretRecoveryResult * _Nonnull recoveryResult) {
204-
205-
XCTFail(@"The operation should not succeed");
206-
[expectation fulfill];
207-
208-
} failure:^(NSError * _Nonnull error) {
209-
210-
// -> It must error with expected NSError domain and code
211-
XCTAssertNotNil(error);
212-
XCTAssertEqualObjects(error.domain, MXRecoveryServiceErrorDomain);
213-
XCTAssertEqual(error.code, MXRecoveryServiceBadRecoveryKeyErrorCode);
214-
215-
[expectation fulfill];
216-
}];
217-
218-
} failure:^(NSError * _Nonnull error) {
219-
XCTFail(@"The operation should not fail - NSError: %@", error);
220-
[expectation fulfill];
221-
}];
222-
223-
} failure:^(NSError * _Nonnull error) {
224-
XCTFail(@"The operation should not fail - NSError: %@", error);
225-
[expectation fulfill];
226-
}];
227-
}];
228-
}
229-
230-
231141
// Test createRecoveryForSecrets when there is already a key backup with the private key stored locally
232142
//
233143
// - Have Alice with cross-signing and key backup bootstrapped

0 commit comments

Comments
 (0)