diff --git a/src/Core/Models/Domain/Attachment.cs b/src/Core/Models/Domain/Attachment.cs index d4669e988..b12395a58 100644 --- a/src/Core/Models/Domain/Attachment.cs +++ b/src/Core/Models/Domain/Attachment.cs @@ -34,7 +34,6 @@ namespace Bit.Core.Models.Domain public string SizeName { get; set; } public EncString Key { get; set; } public EncString FileName { get; set; } - public EncString CipherKey { get; set; } public async Task DecryptAsync(string orgId, SymmetricCryptoKey key = null) { diff --git a/src/Core/Models/Domain/Cipher.cs b/src/Core/Models/Domain/Cipher.cs index 91c5e63ec..a7a2d6943 100644 --- a/src/Core/Models/Domain/Cipher.cs +++ b/src/Core/Models/Domain/Cipher.cs @@ -134,24 +134,14 @@ namespace Bit.Core.Models.Domain { model.Attachments = new List(); var tasks = new List(); - async Task decryptAndAddAttachmentAsync(Attachment attachment, SymmetricCryptoKey decKey) + async Task decryptAndAddAttachmentAsync(Attachment attachment) { - var decAttachment = await attachment.DecryptAsync(OrganizationId, model.Key ?? decKey); + var decAttachment = await attachment.DecryptAsync(OrganizationId, model.Key); model.Attachments.Add(decAttachment); } - var cryptoService = ServiceContainer.Resolve(); foreach (var attachment in Attachments) { - SymmetricCryptoKey decKey = null; - //If the cipher.key is null but the attachment.cipherKey has a value we will use it to decrypt the attachment - if (Key == null && attachment.CipherKey != null) - { - var orgKey = await cryptoService.GetOrgKeyAsync(OrganizationId); - - var key = await cryptoService.DecryptToBytesAsync(attachment.CipherKey, orgKey); - decKey = new CipherKey(key); - } - tasks.Add(decryptAndAddAttachmentAsync(attachment, decKey)); + tasks.Add(decryptAndAddAttachmentAsync(attachment)); } await Task.WhenAll(tasks); } diff --git a/src/Core/Models/View/AttachmentView.cs b/src/Core/Models/View/AttachmentView.cs index 1e712ed3c..879ec583c 100644 --- a/src/Core/Models/View/AttachmentView.cs +++ b/src/Core/Models/View/AttachmentView.cs @@ -20,7 +20,6 @@ namespace Bit.Core.Models.View public string SizeName { get; set; } public string FileName { get; set; } public SymmetricCryptoKey Key { get; set; } - public CipherKey CipherKey { get; set; } public long FileSize { diff --git a/src/Core/Services/CipherService.cs b/src/Core/Services/CipherService.cs index 5cdcbe0c2..f2860600a 100644 --- a/src/Core/Services/CipherService.cs +++ b/src/Core/Services/CipherService.cs @@ -579,34 +579,25 @@ namespace Bit.Core.Services if(cipherView.Key == null) { cipher = await EncryptAsync(cipherView); - var putCipherRequest = new CipherRequest(cipher); - var putCipherResponse = await _apiService.PutCipherAsync(cipherView.Id, putCipherRequest); - var cipherData = new CipherData(putCipherResponse, await _stateService.GetActiveUserIdAsync()); - await UpsertAsync(cipherData); + await UpdateAndUpsertAsync(async () => await _apiService.PutCipherAsync(cipherView.Id, new CipherRequest(cipher))); cipher = await GetAsync(cipherView.Id); cipherView = await cipher.DecryptAsync(); } - if (cipherView.Attachments != null) { foreach (var attachment in cipherView.Attachments) { if (attachment.Key == null) { - attachmentTasks.Add(ShareAttachmentWithServerAsync(attachment, cipherView.Id, organizationId, cipherView.Key, cipher?.Key)); - attachment.CipherKey = cipherView.Key; + attachmentTasks.Add(ShareAttachmentWithServerAsync(attachment, cipherView.Id, organizationId)); } } } await Task.WhenAll(attachmentTasks); cipherView.OrganizationId = organizationId; cipherView.CollectionIds = collectionIds; - var encCipher = await EncryptAsync(cipherView); - var request = new CipherShareRequest(encCipher); - var response = await _apiService.PutShareCipherAsync(cipherView.Id, request); - var userId = await _stateService.GetActiveUserIdAsync(); - var data = new CipherData(response, userId, collectionIds); - await UpsertAsync(data); + cipher = await EncryptAsync(cipherView); + await UpdateAndUpsertAsync(async () => await _apiService.PutShareCipherAsync(cipherView.Id, new CipherShareRequest(cipher)), collectionIds); } public async Task SaveAttachmentRawWithServerAsync(Cipher cipher, CipherView cipherView, string filename, byte[] data) @@ -869,13 +860,12 @@ namespace Bit.Core.Services // Helpers - private async Task> MakeAttachmentKeyAsync(string organizationId, Cipher cipher = null, CipherView cipherView = null, SymmetricCryptoKey cipherKey = null) + private async Task> MakeAttachmentKeyAsync(string organizationId, Cipher cipher = null, CipherView cipherView = null) { var orgKey = await _cryptoService.GetOrgKeyAsync(organizationId); - //We give priority to the use of cipher.key if it exists - SymmetricCryptoKey encryptionKey = cipherKey ?? orgKey; - if (cipher != null && cipherView != null && cipher.Key == null) + SymmetricCryptoKey encryptionKey = orgKey; + if (cipher != null && cipherView != null) { encryptionKey = await UpdateCipherAndGetCipherKeyAsync(cipher, cipherView, orgKey, false); } @@ -887,7 +877,7 @@ namespace Bit.Core.Services } private async Task ShareAttachmentWithServerAsync(AttachmentView attachmentView, string cipherId, - string organizationId, SymmetricCryptoKey cipherKey = null, EncString cipherKeyProtected = null) + string organizationId) { var attachmentResponse = await _httpClient.GetAsync(attachmentView.Url); if (!attachmentResponse.IsSuccessStatusCode) @@ -898,7 +888,7 @@ namespace Bit.Core.Services var bytes = await attachmentResponse.Content.ReadAsByteArrayAsync(); var decBytes = await _cryptoService.DecryptFromBytesAsync(bytes, null); - var (attachmentKey, protectedAttachmentKey, encKey) = await MakeAttachmentKeyAsync(organizationId, cipherKey:cipherKey); + var (attachmentKey, protectedAttachmentKey, encKey) = await MakeAttachmentKeyAsync(organizationId); var encFileName = await _cryptoService.EncryptAsync(attachmentView.FileName, encKey); var encFileData = await _cryptoService.EncryptToBytesAsync(decBytes, attachmentKey); @@ -907,10 +897,6 @@ namespace Bit.Core.Services var fd = new MultipartFormDataContent(boundary); fd.Add(new StringContent(protectedAttachmentKey.EncryptedString), "key"); fd.Add(new StreamContent(new MemoryStream(encFileData.Buffer)), "data", encFileName.EncryptedString); - if(cipherKey != null && cipherKeyProtected != null) - { - fd.Add(new StringContent(cipherKeyProtected.EncryptedString), "cipherKey"); - } await _apiService.PostShareCipherAttachmentAsync(cipherId, attachmentView.Id, fd, organizationId); } @@ -1374,6 +1360,13 @@ namespace Bit.Core.Services } } + private async Task UpdateAndUpsertAsync(Func> func, HashSet collectionIds = null) + { + var response = await func(); + var data = new CipherData(response, await _stateService.GetActiveUserIdAsync(), collectionIds); + await UpsertAsync(data); + } + private class CipherLocaleComparer : IComparer { private readonly II18nService _i18nService;