|
18 | 18 |
|
19 | 19 | #import "MXSession.h"
|
20 | 20 | #import "MXTools.h"
|
| 21 | +#import "MXKeyBackupPassword.h" |
21 | 22 | #import "MXRecoveryKey.h"
|
22 | 23 | #import "MXHkdfSha256.h"
|
23 | 24 | #import "MXAesHmacSha2.h"
|
24 | 25 | #import "MXBase64Tools.h"
|
25 | 26 | #import "MXEncryptedSecretContent.h"
|
26 | 27 |
|
| 28 | +#import <Security/Security.h> |
27 | 29 |
|
28 | 30 | #pragma mark - Constants
|
29 | 31 |
|
@@ -126,6 +128,112 @@ - (MXHTTPOperation*)createKeyWithKeyId:(nullable NSString*)keyId
|
126 | 128 | return operation;
|
127 | 129 | }
|
128 | 130 |
|
| 131 | +- (MXHTTPOperation*)createKeyWithKeyId:(nullable NSString*)keyId |
| 132 | + keyName:(nullable NSString*)keyName |
| 133 | + passphrase:(nullable NSString*)passphrase |
| 134 | + success:(void (^)(MXSecretStorageKeyCreationInfo *keyCreationInfo))success |
| 135 | + failure:(void (^)(NSError *error))failure |
| 136 | +{ |
| 137 | + MXLogDebug(@"[MXSecretStorage] createKeyWithKeyId: Creating new key with passphrase"); |
| 138 | + keyId = keyId ?: [[NSUUID UUID] UUIDString]; |
| 139 | + |
| 140 | + MXHTTPOperation *operation = [MXHTTPOperation new]; |
| 141 | + |
| 142 | + MXWeakify(self); |
| 143 | + dispatch_async(processingQueue, ^{ |
| 144 | + MXStrongifyAndReturnIfNil(self); |
| 145 | + |
| 146 | + NSError *error; |
| 147 | + |
| 148 | + NSData *privateKey; |
| 149 | + MXSecretStoragePassphrase *passphraseInfo; |
| 150 | + |
| 151 | + if (passphrase) |
| 152 | + { |
| 153 | + // Generate a private key from the passphrase |
| 154 | + NSString *salt; |
| 155 | + NSUInteger iterations; |
| 156 | + privateKey = [MXKeyBackupPassword generatePrivateKeyWithPassword:passphrase |
| 157 | + salt:&salt |
| 158 | + iterations:&iterations |
| 159 | + error:&error]; |
| 160 | + if (!error) |
| 161 | + { |
| 162 | + passphraseInfo = [MXSecretStoragePassphrase new]; |
| 163 | + passphraseInfo.algorithm = @"m.pbkdf2"; |
| 164 | + passphraseInfo.salt = salt; |
| 165 | + passphraseInfo.iterations = iterations; |
| 166 | + } |
| 167 | + } |
| 168 | + else |
| 169 | + { |
| 170 | + uint8_t randomBytes[32]; |
| 171 | + OSStatus status = SecRandomCopyBytes(kSecRandomDefault, sizeof(randomBytes), randomBytes); |
| 172 | + |
| 173 | + if (status == errSecSuccess) |
| 174 | + { |
| 175 | + privateKey = [NSData dataWithBytes:randomBytes length:sizeof(randomBytes)]; |
| 176 | + } |
| 177 | + else |
| 178 | + { |
| 179 | + MXLogDebug(@"Failed to generate random bytes with error: %d", (int)status); |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + if (error) |
| 184 | + { |
| 185 | + dispatch_async(dispatch_get_main_queue(), ^{ |
| 186 | + MXLogDebug(@"[MXSecretStorage] createKeyWithKeyId: Failed to create a new key - %@", error); |
| 187 | + failure(error); |
| 188 | + }); |
| 189 | + return; |
| 190 | + } |
| 191 | + |
| 192 | + // Build iv and mac |
| 193 | + MXEncryptedSecretContent *encryptedZeroString = [self encryptedZeroStringWithPrivateKey:privateKey iv:nil error:&error]; |
| 194 | + if (error) |
| 195 | + { |
| 196 | + dispatch_async(dispatch_get_main_queue(), ^{ |
| 197 | + MXLogDebug(@"[MXSecretStorage] createKeyWithKeyId: Failed to create a new key - %@", error); |
| 198 | + failure(error); |
| 199 | + }); |
| 200 | + return; |
| 201 | + } |
| 202 | + |
| 203 | + MXSecretStorageKeyContent *ssssKeyContent = [MXSecretStorageKeyContent new]; |
| 204 | + ssssKeyContent.name = keyName; |
| 205 | + ssssKeyContent.algorithm = MXSecretStorageKeyAlgorithm.aesHmacSha2; |
| 206 | + ssssKeyContent.passphrase = passphraseInfo; |
| 207 | + ssssKeyContent.iv = encryptedZeroString.iv; |
| 208 | + ssssKeyContent.mac = encryptedZeroString.mac; |
| 209 | + |
| 210 | + NSString *accountDataId = [self storageKeyIdForKey:keyId]; |
| 211 | + MXHTTPOperation *operation2 = [self setAccountData:ssssKeyContent.JSONDictionary forType:accountDataId success:^{ |
| 212 | + |
| 213 | + MXSecretStorageKeyCreationInfo *keyCreationInfo = [MXSecretStorageKeyCreationInfo new]; |
| 214 | + keyCreationInfo.keyId = keyId; |
| 215 | + keyCreationInfo.content = ssssKeyContent; |
| 216 | + keyCreationInfo.privateKey = privateKey; |
| 217 | + keyCreationInfo.recoveryKey = [MXRecoveryKey encode:privateKey]; |
| 218 | + |
| 219 | + dispatch_async(dispatch_get_main_queue(), ^{ |
| 220 | + MXLogDebug(@"[MXSecretStorage] createKeyWithKeyId: Successfully created a new key"); |
| 221 | + success(keyCreationInfo); |
| 222 | + }); |
| 223 | + |
| 224 | + } failure:^(NSError *error) { |
| 225 | + dispatch_async(dispatch_get_main_queue(), ^{ |
| 226 | + MXLogDebug(@"[MXSecretStorage] createKeyWithKeyId: Failed to create a new key - %@", error); |
| 227 | + failure(error); |
| 228 | + }); |
| 229 | + }]; |
| 230 | + |
| 231 | + [operation mutateTo:operation2]; |
| 232 | + }); |
| 233 | + |
| 234 | + return operation; |
| 235 | +} |
| 236 | + |
129 | 237 | - (MXHTTPOperation*)deleteKeyWithKeyId:(nullable NSString*)keyId
|
130 | 238 | success:(void (^)(void))success
|
131 | 239 | failure:(void (^)(NSError *error))failure
|
|
0 commit comments