mirror of
https://github.com/bitwarden/mobile
synced 2026-01-10 12:33:28 +00:00
download, decrypt and open attachment files
This commit is contained in:
@@ -297,18 +297,61 @@ namespace Bit.App.Services
|
||||
// Old encrypt-then-mac scheme, swap out the key
|
||||
if(_legacyEtmKey == null)
|
||||
{
|
||||
_legacyEtmKey = new SymmetricCryptoKey(key.Key, Enums.EncryptionType.AesCbc128_HmacSha256_B64);
|
||||
_legacyEtmKey = new SymmetricCryptoKey(key.Key, EncryptionType.AesCbc128_HmacSha256_B64);
|
||||
}
|
||||
|
||||
key = _legacyEtmKey;
|
||||
}
|
||||
|
||||
if(encyptedValue.EncryptionType != key.EncryptionType)
|
||||
return Crypto.AesCbcDecrypt(encyptedValue, key);
|
||||
}
|
||||
|
||||
public byte[] DecryptToBytes(byte[] encyptedValue, SymmetricCryptoKey key = null)
|
||||
{
|
||||
if(key == null)
|
||||
{
|
||||
throw new ArgumentException("encType unavailable.");
|
||||
key = EncKey ?? Key;
|
||||
}
|
||||
|
||||
return Crypto.AesCbcDecrypt(encyptedValue, key);
|
||||
if(key == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(key));
|
||||
}
|
||||
|
||||
if(encyptedValue == null || encyptedValue.Length == 0)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(encyptedValue));
|
||||
}
|
||||
|
||||
byte[] ct, iv, mac = null;
|
||||
var encType = (EncryptionType)encyptedValue[0];
|
||||
switch(encType)
|
||||
{
|
||||
case EncryptionType.AesCbc128_HmacSha256_B64:
|
||||
case EncryptionType.AesCbc256_HmacSha256_B64:
|
||||
if(encyptedValue.Length <= 49)
|
||||
{
|
||||
throw new InvalidOperationException("Invalid value length.");
|
||||
}
|
||||
|
||||
iv = new ArraySegment<byte>(encyptedValue, 1, 16).ToArray();
|
||||
mac = new ArraySegment<byte>(encyptedValue, 17, 32).ToArray();
|
||||
ct = new ArraySegment<byte>(encyptedValue, 49, encyptedValue.Length - 49).ToArray();
|
||||
break;
|
||||
case EncryptionType.AesCbc256_B64:
|
||||
if(encyptedValue.Length <= 17)
|
||||
{
|
||||
throw new InvalidOperationException("Invalid value length.");
|
||||
}
|
||||
|
||||
iv = new ArraySegment<byte>(encyptedValue, 1, 16).ToArray();
|
||||
ct = new ArraySegment<byte>(encyptedValue, 17, encyptedValue.Length - 17).ToArray();
|
||||
break;
|
||||
default:
|
||||
throw new InvalidOperationException("Invalid encryption type.");
|
||||
}
|
||||
|
||||
return Crypto.AesCbcDecrypt(encType, ct, iv, mac, key);
|
||||
}
|
||||
|
||||
public byte[] RsaDecryptToBytes(CipherString encyptedValue, byte[] privateKey)
|
||||
|
||||
@@ -7,6 +7,7 @@ using Bit.App.Models;
|
||||
using Bit.App.Models.Api;
|
||||
using Bit.App.Models.Data;
|
||||
using Xamarin.Forms;
|
||||
using System.Net.Http;
|
||||
|
||||
namespace Bit.App.Services
|
||||
{
|
||||
@@ -17,19 +18,22 @@ namespace Bit.App.Services
|
||||
private readonly IAuthService _authService;
|
||||
private readonly ILoginApiRepository _loginApiRepository;
|
||||
private readonly ISettingsService _settingsService;
|
||||
private readonly ICryptoService _cryptoService;
|
||||
|
||||
public LoginService(
|
||||
ILoginRepository loginRepository,
|
||||
IAttachmentRepository attachmentRepository,
|
||||
IAuthService authService,
|
||||
ILoginApiRepository loginApiRepository,
|
||||
ISettingsService settingsService)
|
||||
ISettingsService settingsService,
|
||||
ICryptoService cryptoService)
|
||||
{
|
||||
_loginRepository = loginRepository;
|
||||
_attachmentRepository = attachmentRepository;
|
||||
_authService = authService;
|
||||
_loginApiRepository = loginApiRepository;
|
||||
_settingsService = settingsService;
|
||||
_cryptoService = cryptoService;
|
||||
}
|
||||
|
||||
public async Task<Login> GetByIdAsync(string id)
|
||||
@@ -217,6 +221,33 @@ namespace Bit.App.Services
|
||||
return response;
|
||||
}
|
||||
|
||||
public async Task<byte[]> DownloadAndDecryptAttachmentAsync(SymmetricCryptoKey key, string url)
|
||||
{
|
||||
using(var client = new HttpClient())
|
||||
{
|
||||
try
|
||||
{
|
||||
var response = await client.GetAsync(new Uri(url)).ConfigureAwait(false);
|
||||
if(!response.IsSuccessStatusCode)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var data = await response.Content.ReadAsByteArrayAsync();
|
||||
if(data == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return _cryptoService.DecryptToBytes(data, key);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private string WebUriFromAndroidAppUri(string androidAppUriString)
|
||||
{
|
||||
if(!UriIsAndroidApp(androidAppUriString))
|
||||
|
||||
Reference in New Issue
Block a user