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

Restore createKeyWithKeyId #1885

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Changes from 1 commit
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
99 changes: 99 additions & 0 deletions MatrixSDK/Crypto/SecretStorage/MXSecretStorage.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#import "MXSession.h"
#import "MXTools.h"
#import "MXKeyBackupPassword.h"
#import "MXRecoveryKey.h"
#import "MXHkdfSha256.h"
#import "MXAesHmacSha2.h"
Expand Down Expand Up @@ -126,6 +127,104 @@
return operation;
}

- (MXHTTPOperation*)createKeyWithKeyId:(nullable NSString*)keyId
keyName:(nullable NSString*)keyName
passphrase:(nullable NSString*)passphrase
success:(void (^)(MXSecretStorageKeyCreationInfo *keyCreationInfo))success
failure:(void (^)(NSError *error))failure
{
MXLogDebug(@"[MXSecretStorage] createKeyWithKeyId: Creating new key with passphrase");
keyId = keyId ?: [[NSUUID UUID] UUIDString];

MXHTTPOperation *operation = [MXHTTPOperation new];

MXWeakify(self);
dispatch_async(processingQueue, ^{
MXStrongifyAndReturnIfNil(self);

NSError *error;

NSData *privateKey;
MXSecretStoragePassphrase *passphraseInfo;

if (passphrase)
{
// Generate a private key from the passphrase
NSString *salt;
NSUInteger iterations;
privateKey = [MXKeyBackupPassword generatePrivateKeyWithPassword:passphrase
salt:&salt
iterations:&iterations
error:&error];
if (!error)
{
passphraseInfo = [MXSecretStoragePassphrase new];
passphraseInfo.algorithm = @"m.pbkdf2";
passphraseInfo.salt = salt;
passphraseInfo.iterations = iterations;
}
}
else
{
OLMPkDecryption *decryption = [OLMPkDecryption new];

Check failure on line 169 in MatrixSDK/Crypto/SecretStorage/MXSecretStorage.m

View workflow job for this annotation

GitHub Actions / Unit Tests

use of undeclared identifier 'OLMPkDecryption'

Check failure on line 169 in MatrixSDK/Crypto/SecretStorage/MXSecretStorage.m

View workflow job for this annotation

GitHub Actions / Unit Tests

use of undeclared identifier 'decryption'

Check failure on line 169 in MatrixSDK/Crypto/SecretStorage/MXSecretStorage.m

View workflow job for this annotation

GitHub Actions / Unit Tests

use of undeclared identifier 'OLMPkDecryption'

Check failure on line 169 in MatrixSDK/Crypto/SecretStorage/MXSecretStorage.m

View workflow job for this annotation

GitHub Actions / Unit Tests

use of undeclared identifier 'OLMPkDecryption'

Check failure on line 169 in MatrixSDK/Crypto/SecretStorage/MXSecretStorage.m

View workflow job for this annotation

GitHub Actions / Unit Tests

use of undeclared identifier 'decryption'

Check failure on line 169 in MatrixSDK/Crypto/SecretStorage/MXSecretStorage.m

View workflow job for this annotation

GitHub Actions / Unit Tests

use of undeclared identifier 'OLMPkDecryption'

Check failure on line 169 in MatrixSDK/Crypto/SecretStorage/MXSecretStorage.m

View workflow job for this annotation

GitHub Actions / Integration Tests

use of undeclared identifier 'OLMPkDecryption'

Check failure on line 169 in MatrixSDK/Crypto/SecretStorage/MXSecretStorage.m

View workflow job for this annotation

GitHub Actions / Integration Tests

use of undeclared identifier 'decryption'

Check failure on line 169 in MatrixSDK/Crypto/SecretStorage/MXSecretStorage.m

View workflow job for this annotation

GitHub Actions / Integration Tests

use of undeclared identifier 'OLMPkDecryption'

Check failure on line 169 in MatrixSDK/Crypto/SecretStorage/MXSecretStorage.m

View workflow job for this annotation

GitHub Actions / Integration Tests

use of undeclared identifier 'OLMPkDecryption'

Check failure on line 169 in MatrixSDK/Crypto/SecretStorage/MXSecretStorage.m

View workflow job for this annotation

GitHub Actions / Integration Tests

use of undeclared identifier 'decryption'

Check failure on line 169 in MatrixSDK/Crypto/SecretStorage/MXSecretStorage.m

View workflow job for this annotation

GitHub Actions / Integration Tests

use of undeclared identifier 'OLMPkDecryption'
Copy link
Member Author

Choose a reason for hiding this comment

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

This uses OLMKit

Copy link
Member

@BillCarsonFr BillCarsonFr Oct 25, 2024

Choose a reason for hiding this comment

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

This uses OLMKit

I think it's just generating 32 random bytes, you can replace with SecRandomCopyBytes

Copy link
Member Author

Choose a reason for hiding this comment

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

@BillCarsonFr okay I think I have replaced it correctly, let me know if this is fine and we can merge it

[decryption generateKey:&error];

Check failure on line 170 in MatrixSDK/Crypto/SecretStorage/MXSecretStorage.m

View workflow job for this annotation

GitHub Actions / Unit Tests

use of undeclared identifier 'decryption'

Check failure on line 170 in MatrixSDK/Crypto/SecretStorage/MXSecretStorage.m

View workflow job for this annotation

GitHub Actions / Unit Tests

use of undeclared identifier 'decryption'

Check failure on line 170 in MatrixSDK/Crypto/SecretStorage/MXSecretStorage.m

View workflow job for this annotation

GitHub Actions / Integration Tests

use of undeclared identifier 'decryption'

Check failure on line 170 in MatrixSDK/Crypto/SecretStorage/MXSecretStorage.m

View workflow job for this annotation

GitHub Actions / Integration Tests

use of undeclared identifier 'decryption'
privateKey = decryption.privateKey;

Check failure on line 171 in MatrixSDK/Crypto/SecretStorage/MXSecretStorage.m

View workflow job for this annotation

GitHub Actions / Unit Tests

use of undeclared identifier 'decryption'

Check failure on line 171 in MatrixSDK/Crypto/SecretStorage/MXSecretStorage.m

View workflow job for this annotation

GitHub Actions / Unit Tests

use of undeclared identifier 'decryption'

Check failure on line 171 in MatrixSDK/Crypto/SecretStorage/MXSecretStorage.m

View workflow job for this annotation

GitHub Actions / Integration Tests

use of undeclared identifier 'decryption'

Check failure on line 171 in MatrixSDK/Crypto/SecretStorage/MXSecretStorage.m

View workflow job for this annotation

GitHub Actions / Integration Tests

use of undeclared identifier 'decryption'
}

if (error)
{
dispatch_async(dispatch_get_main_queue(), ^{
MXLogDebug(@"[MXSecretStorage] createKeyWithKeyId: Failed to create a new key - %@", error);
failure(error);
});
return;
}

// Build iv and mac
MXEncryptedSecretContent *encryptedZeroString = [self encryptedZeroStringWithPrivateKey:privateKey iv:nil error:&error];
if (error)
{
dispatch_async(dispatch_get_main_queue(), ^{
MXLogDebug(@"[MXSecretStorage] createKeyWithKeyId: Failed to create a new key - %@", error);
failure(error);
});
return;
}

MXSecretStorageKeyContent *ssssKeyContent = [MXSecretStorageKeyContent new];
ssssKeyContent.name = keyName;
ssssKeyContent.algorithm = MXSecretStorageKeyAlgorithm.aesHmacSha2;
ssssKeyContent.passphrase = passphraseInfo;
ssssKeyContent.iv = encryptedZeroString.iv;
ssssKeyContent.mac = encryptedZeroString.mac;

NSString *accountDataId = [self storageKeyIdForKey:keyId];
MXHTTPOperation *operation2 = [self setAccountData:ssssKeyContent.JSONDictionary forType:accountDataId success:^{

MXSecretStorageKeyCreationInfo *keyCreationInfo = [MXSecretStorageKeyCreationInfo new];
keyCreationInfo.keyId = keyId;
keyCreationInfo.content = ssssKeyContent;
keyCreationInfo.privateKey = privateKey;
keyCreationInfo.recoveryKey = [MXRecoveryKey encode:privateKey];

dispatch_async(dispatch_get_main_queue(), ^{
MXLogDebug(@"[MXSecretStorage] createKeyWithKeyId: Successfully created a new key");
success(keyCreationInfo);
});

} failure:^(NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
MXLogDebug(@"[MXSecretStorage] createKeyWithKeyId: Failed to create a new key - %@", error);
failure(error);
});
}];

[operation mutateTo:operation2];
});

return operation;
}

- (MXHTTPOperation*)deleteKeyWithKeyId:(nullable NSString*)keyId
success:(void (^)(void))success
failure:(void (^)(NSError *error))failure
Expand Down
Loading