Skip to content

Commit f0f87bc

Browse files
committed
restored and corrected some code
1 parent 598ece7 commit f0f87bc

File tree

6 files changed

+328
-0
lines changed

6 files changed

+328
-0
lines changed

MatrixSDK.xcodeproj/project.pbxproj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,10 @@
663663
A0A2397A295202930001F722 /* MXAggregatedPollsUpdater.swift in Sources */ = {isa = PBXBuildFile; fileRef = A0A23978295202930001F722 /* MXAggregatedPollsUpdater.swift */; };
664664
A0B1217F295306F700E704C2 /* MXAggregatedPollsUpdaterTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A0B1217E295306F700E704C2 /* MXAggregatedPollsUpdaterTests.swift */; };
665665
A0B12180295306F700E704C2 /* MXAggregatedPollsUpdaterTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A0B1217E295306F700E704C2 /* MXAggregatedPollsUpdaterTests.swift */; };
666+
A71E55322CA6FEBD00B06B17 /* MXKeyBackupPassword.h in Headers */ = {isa = PBXBuildFile; fileRef = A71E55302CA6FEBC00B06B17 /* MXKeyBackupPassword.h */; settings = {ATTRIBUTES = (Public, ); }; };
667+
A71E55332CA6FEBD00B06B17 /* MXKeyBackupPassword.h in Headers */ = {isa = PBXBuildFile; fileRef = A71E55302CA6FEBC00B06B17 /* MXKeyBackupPassword.h */; settings = {ATTRIBUTES = (Public, ); }; };
668+
A71E55342CA6FEBD00B06B17 /* MXKeyBackupPassword.m in Sources */ = {isa = PBXBuildFile; fileRef = A71E55312CA6FEBC00B06B17 /* MXKeyBackupPassword.m */; };
669+
A71E55352CA6FEBD00B06B17 /* MXKeyBackupPassword.m in Sources */ = {isa = PBXBuildFile; fileRef = A71E55312CA6FEBC00B06B17 /* MXKeyBackupPassword.m */; };
666670
A759E23D2C98EE7D002429A8 /* MXCrypto.m in Sources */ = {isa = PBXBuildFile; fileRef = A759E23C2C98EE7D002429A8 /* MXCrypto.m */; };
667671
A759E23E2C98EE7D002429A8 /* MXCrypto.m in Sources */ = {isa = PBXBuildFile; fileRef = A759E23C2C98EE7D002429A8 /* MXCrypto.m */; };
668672
A75CAD692A9796DE00F06072 /* MXWellKnownAuthentication.m in Sources */ = {isa = PBXBuildFile; fileRef = A75CAD682A9796DE00F06072 /* MXWellKnownAuthentication.m */; };
@@ -2483,6 +2487,8 @@
24832487
9274AFE71EE580240009BEB6 /* MXCallKitAdapter.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MXCallKitAdapter.m; sourceTree = "<group>"; };
24842488
A0A23978295202930001F722 /* MXAggregatedPollsUpdater.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MXAggregatedPollsUpdater.swift; sourceTree = "<group>"; };
24852489
A0B1217E295306F700E704C2 /* MXAggregatedPollsUpdaterTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MXAggregatedPollsUpdaterTests.swift; sourceTree = "<group>"; };
2490+
A71E55302CA6FEBC00B06B17 /* MXKeyBackupPassword.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MXKeyBackupPassword.h; sourceTree = "<group>"; };
2491+
A71E55312CA6FEBC00B06B17 /* MXKeyBackupPassword.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MXKeyBackupPassword.m; sourceTree = "<group>"; };
24862492
A759E23C2C98EE7D002429A8 /* MXCrypto.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MXCrypto.m; sourceTree = "<group>"; };
24872493
A75CAD682A9796DE00F06072 /* MXWellKnownAuthentication.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MXWellKnownAuthentication.m; sourceTree = "<group>"; };
24882494
A75CAD6B2A97970500F06072 /* MXWellKnownAuthentication.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MXWellKnownAuthentication.h; sourceTree = "<group>"; };
@@ -3941,6 +3947,8 @@
39413947
32BBAE642178E99100D85F46 /* KeyBackup */ = {
39423948
isa = PBXGroup;
39433949
children = (
3950+
A71E55302CA6FEBC00B06B17 /* MXKeyBackupPassword.h */,
3951+
A71E55312CA6FEBC00B06B17 /* MXKeyBackupPassword.m */,
39443952
EDE70DC628DA22E200099736 /* Engine */,
39453953
32BBAE652178E99100D85F46 /* Data */,
39463954
32BBAE722179CF4000D85F46 /* MXKeyBackup.h */,
@@ -5741,6 +5749,7 @@
57415749
3A23A741256D322C00B9D00F /* MXAes.h in Headers */,
57425750
EC0B943D271DB68F00B4D440 /* MXVoidRoomSummaryStore.h in Headers */,
57435751
32BBAE6D2178E99100D85F46 /* MXKeyBackupVersion.h in Headers */,
5752+
A71E55322CA6FEBD00B06B17 /* MXKeyBackupPassword.h in Headers */,
57445753
323547D82226D5D600F15F94 /* MXWellKnownBaseConfig.h in Headers */,
57455754
32A151461DAF7C0C00400192 /* MXDeviceInfo.h in Headers */,
57465755
EC8A53A925B1BC77004E0802 /* MXCallRejectEventContent.h in Headers */,
@@ -6133,6 +6142,7 @@
61336142
EC60EDBD265CFE8600B39A4E /* MXRoomSyncAccountData.h in Headers */,
61346143
B14EF32B2397E90400758AF0 /* MXBackgroundModeHandler.h in Headers */,
61356144
B14EF32C2397E90400758AF0 /* MXRoomPredecessorInfo.h in Headers */,
6145+
A71E55332CA6FEBD00B06B17 /* MXKeyBackupPassword.h in Headers */,
61366146
B14EF32D2397E90400758AF0 /* MXCallStack.h in Headers */,
61376147
B14EF32E2397E90400758AF0 /* MXMediaLoader.h in Headers */,
61386148
ECDA763A27B6B74C000C48CF /* MXCapabilities.h in Headers */,
@@ -6647,6 +6657,7 @@
66476657
327E9ABD2284521C00A98BC1 /* MXEventUnsignedData.m in Sources */,
66486658
32AF9286240EA2430008A0FD /* MXSecretShareRequest.m in Sources */,
66496659
ED5C754428B3E80300D24E85 /* MXLogObjcWrapper.m in Sources */,
6660+
A71E55342CA6FEBD00B06B17 /* MXKeyBackupPassword.m in Sources */,
66506661
ED47CB6D28523995004FD755 /* MXCryptoV2.swift in Sources */,
66516662
ED463ECB29B0B75800957941 /* EventEncryptionAlgorithm+String.swift in Sources */,
66526663
ED37834929C9B6E700A449DA /* MXEventDecryptionDecoration.swift in Sources */,
@@ -7272,6 +7283,7 @@
72727283
B14EF2072397E90400758AF0 /* MXAutoDiscovery.m in Sources */,
72737284
ED5C754528B3E80300D24E85 /* MXLogObjcWrapper.m in Sources */,
72747285
ED47CB6E28523995004FD755 /* MXCryptoV2.swift in Sources */,
7286+
A71E55352CA6FEBD00B06B17 /* MXKeyBackupPassword.m in Sources */,
72757287
ED463ECC29B0B75800957941 /* EventEncryptionAlgorithm+String.swift in Sources */,
72767288
EDF154E2296C203E004D7FFE /* MXCryptoMachineStore.swift in Sources */,
72777289
ED37834A29C9B6E700A449DA /* MXEventDecryptionDecoration.swift in Sources */,
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
Copyright 2019 New Vector Ltd
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
#import <Foundation/Foundation.h>
18+
19+
NS_ASSUME_NONNULL_BEGIN
20+
21+
/**
22+
Utility to compute a backup private key from a password and vice-versa.
23+
*/
24+
@interface MXKeyBackupPassword : NSObject
25+
26+
/**
27+
Compute a private key from a password.
28+
29+
@param password the password to use.
30+
@param salt the salt used to generate the private key.
31+
@param iterations number of key derivations done on the generated private key.
32+
@param error the output error.
33+
@return a private key.
34+
*/
35+
+ (nullable NSData *)generatePrivateKeyWithPassword:(NSString*)password salt:(NSString * _Nullable *_Nonnull)salt iterations:(NSUInteger*)iterations error:(NSError * _Nullable *)error;
36+
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+
48+
@end
49+
50+
NS_ASSUME_NONNULL_END
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
Copyright 2019 New Vector Ltd
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
#import "MXKeyBackupPassword.h"
18+
19+
#import "MXTools.h"
20+
#import "MXCryptoConstants.h"
21+
22+
#import <Security/Security.h>
23+
#import <CommonCrypto/CommonDigest.h>
24+
#import <CommonCrypto/CommonCryptor.h>
25+
#import <CommonCrypto/CommonKeyDerivation.h>
26+
27+
#pragma mark - Constants
28+
29+
static NSUInteger const kSaltLength = 32;
30+
static NSUInteger const kDefaultIterations = 500000;
31+
32+
33+
@implementation MXKeyBackupPassword
34+
35+
+ (NSData *)generatePrivateKeyWithPassword:(NSString *)password salt:(NSString *__autoreleasing *)salt iterations:(NSUInteger *)iterations error:(NSError *__autoreleasing _Nullable *)error
36+
{
37+
*salt = [[MXTools generateSecret] substringWithRange:NSMakeRange(0, kSaltLength)];
38+
*iterations = kDefaultIterations;
39+
40+
NSData *privateKey = [self deriveKey:password salt:*salt iterations:kDefaultIterations error:error];
41+
42+
return privateKey;
43+
}
44+
45+
+ (NSData *)retrievePrivateKeyWithPassword:(NSString *)password salt:(NSString *)salt iterations:(NSUInteger)iterations error:(NSError *__autoreleasing _Nullable *)error
46+
{
47+
return [self deriveKey:password salt:salt iterations:iterations error:error];
48+
}
49+
50+
51+
#pragma mark - Private methods
52+
53+
/**
54+
Compute a private key by deriving a password and a salt strings.
55+
56+
@param password the password.
57+
@param salt the salt.
58+
@param iterations number of derivations.
59+
@param error the output error.
60+
@return a private key.
61+
*/
62+
+ (nullable NSData *)deriveKey:(NSString*)password salt:(NSString*)salt iterations:(NSUInteger)iterations error:(NSError *__autoreleasing _Nullable *)error
63+
{
64+
NSDate *startDate = [NSDate date];
65+
66+
NSData *passwordData = [password dataUsingEncoding:NSUTF8StringEncoding];
67+
NSData *saltData = [salt dataUsingEncoding:NSUTF8StringEncoding];
68+
69+
NSMutableData *derivedKey = [NSMutableData dataWithLength: 32];
70+
71+
int result = CCKeyDerivationPBKDF(kCCPBKDF2,
72+
passwordData.bytes,
73+
passwordData.length,
74+
saltData.bytes,
75+
saltData.length,
76+
kCCPRFHmacAlgSHA512,
77+
(uint)iterations,
78+
derivedKey.mutableBytes,
79+
derivedKey.length);
80+
81+
MXLogDebug(@"[MXKeyBackupPassword] deriveKey: %tu iterations took %.0fms", iterations, [[NSDate date] timeIntervalSinceDate:startDate] * 1000);
82+
83+
if (result != kCCSuccess)
84+
{
85+
derivedKey = nil;
86+
87+
if (*error)
88+
{
89+
*error = [NSError errorWithDomain:MXKeyBackupErrorDomain
90+
code:MXKeyBackupErrorCannotDeriveKeyCode
91+
userInfo:@{
92+
NSLocalizedDescriptionKey: [NSString stringWithFormat:@"CCKeyDerivationPBKDF fails: %@", @(result)]
93+
}];
94+
}
95+
}
96+
97+
return derivedKey;
98+
}
99+
100+
@end

MatrixSDK/Crypto/Recovery/MXRecoveryService.h

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,44 @@ typedef NS_ENUM(NSInteger, MXRecoveryServiceErrorCode)
120120

121121
#pragma mark - Backup to recovery
122122

123+
/**
124+
Create a recovery and store secrets there.
125+
126+
It will send keys from the local storage to the recovery on the homeserver.
127+
Those keys are sent encrypted thanks to SSSS that implements this recovery.
128+
129+
@param secrets secrets ids to store in the recovery. Nil for all self.supportedSecrets.
130+
@param privateKey a private key used to generate the recovery key to encrypt keys.
131+
@param createServicesBackups YES to create backups for associated services. Only keyBackup is supported.
132+
133+
@param success A block object called when the operation succeeds.
134+
@param failure A block object called when the operation fails.
135+
*/
136+
- (void)createRecoveryForSecrets:(nullable NSArray<NSString*>*)secrets
137+
withPrivateKey:(NSData*)privateKey
138+
createServicesBackups:(BOOL)createServicesBackups
139+
success:(void (^)(MXSecretStorageKeyCreationInfo *keyCreationInfo))success
140+
failure:(void (^)(NSError *error))failure;
141+
142+
/**
143+
Create a recovery and store secrets there.
144+
145+
It will send keys from the local storage to the recovery on the homeserver.
146+
Those keys are sent encrypted thanks to SSSS that implements this recovery.
147+
148+
@param secrets secrets ids to store in the recovery. Nil for all self.supportedSecrets.
149+
@param passphrase a passphrase used to generate the recovery key to encrypt keys. Nil will generate it.
150+
@param createServicesBackups YES to create backups for associated services. Only keyBackup is supported.
151+
152+
@param success A block object called when the operation succeeds.
153+
@param failure A block object called when the operation fails.
154+
*/
155+
- (void)createRecoveryForSecrets:(nullable NSArray<NSString*>*)secrets
156+
withPassphrase:(nullable NSString*)passphrase
157+
createServicesBackups:(BOOL)createServicesBackups
158+
success:(void (^)(MXSecretStorageKeyCreationInfo *keyCreationInfo))success
159+
failure:(void (^)(NSError *error))failure;
160+
123161
/**
124162
Update secrets to the existing recovery.
125163
@@ -184,6 +222,21 @@ typedef NS_ENUM(NSInteger, MXRecoveryServiceErrorCode)
184222
*/
185223
- (nullable NSData*)privateKeyFromRecoveryKey:(NSString*)recoveryKey error:(NSError**)error;
186224

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+
187240
@end
188241

189242
NS_ASSUME_NONNULL_END

0 commit comments

Comments
 (0)