Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.
Update server unit tests (#2468)
Leendert de Borstf71f2051b394
diff --git a/apps/server/Tests/AliasVault.IntegrationTests/AliasVault.IntegrationTests.csproj b/apps/server/Tests/AliasVault.IntegrationTests/AliasVault.IntegrationTests.csprojindex 3021eae7d..0a4c1c058 100644--- a/apps/server/Tests/AliasVault.IntegrationTests/AliasVault.IntegrationTests.csproj+++ b/apps/server/Tests/AliasVault.IntegrationTests/AliasVault.IntegrationTests.csproj@@ -54,4 +54,8 @@<ProjectReference Include="..\..\Utilities\AliasVault.Cryptography\AliasVault.Cryptography.csproj" /></ItemGroup>+ <ItemGroup>+ <Compile Include="..\Shared\TestDecryption.cs" Link="Shared\TestDecryption.cs" />+ </ItemGroup>+</Project>diff --git a/apps/server/Tests/AliasVault.IntegrationTests/SmtpServer/SmtpServerTests.cs b/apps/server/Tests/AliasVault.IntegrationTests/SmtpServer/SmtpServerTests.csindex 7e65c2b7f..2033cc826 100644--- a/apps/server/Tests/AliasVault.IntegrationTests/SmtpServer/SmtpServerTests.cs+++ b/apps/server/Tests/AliasVault.IntegrationTests/SmtpServer/SmtpServerTests.cs@@ -13,6 +13,7 @@ using System.Text;using AliasVault.Cryptography;using AliasVault.IntegrationTests.SmtpServer.Helpers;using AliasVault.SmtpService.Handlers;+using AliasVault.Tests.Shared;using MailKit.Net.Smtp;using MailKit.Security;using Microsoft.EntityFrameworkCore;@@ -248,7 +249,7 @@ public class SmtpServerTests});// Decrypt the email and then check all individual fields.- processedEmail = EmailEncryption.DecryptEmail(processedEmail, PrivateKey);+ processedEmail = TestDecryption.DecryptEmail(processedEmail, PrivateKey);Assert.Multiple(() =>{Assert.That(processedEmail, Is.Not.Null);@@ -436,7 +437,7 @@ public class SmtpServerTestsAssert.That(processedEmail.To, Is.EqualTo("[email protected]"));// Decrypt the email and then check all individual fields.- processedEmail = EmailEncryption.DecryptEmail(processedEmail, PrivateKey);+ processedEmail = TestDecryption.DecryptEmail(processedEmail, PrivateKey);Assert.Multiple(() =>{Assert.That(processedEmail, Is.Not.Null);@@ -476,7 +477,7 @@ public class SmtpServerTestsAssert.That(processedEmail.To, Is.EqualTo("[email protected]"));// Decrypt the email and then check all individual fields.- processedEmail = EmailEncryption.DecryptEmail(processedEmail, PrivateKey);+ processedEmail = TestDecryption.DecryptEmail(processedEmail, PrivateKey);Assert.Multiple(() =>{Assert.That(processedEmail, Is.Not.Null);@@ -528,7 +529,7 @@ public class SmtpServerTests});// Decrypt the email; the decrypted source bytes must differ from the stored (encrypted) bytes.- processedEmail = EmailEncryption.DecryptEmail(processedEmail, PrivateKey);+ processedEmail = TestDecryption.DecryptEmail(processedEmail, PrivateKey);var decryptedSourceBytes = processedEmail.MessageSourceBytes!;Assert.Multiple(() =>{@@ -590,7 +591,7 @@ public class SmtpServerTestsvar processedEmail = await _testHostBuilder.GetDbContext().Emails.Include(x => x.DecryptionKeys).FirstAsync();// Decrypt the email and verify the accented characters survived into the preview.- processedEmail = EmailEncryption.DecryptEmail(processedEmail, PrivateKey);+ processedEmail = TestDecryption.DecryptEmail(processedEmail, PrivateKey);Assert.Multiple(() =>{Assert.That(processedEmail, Is.Not.Null);@@ -745,7 +746,7 @@ public class SmtpServerTests// The whole point of detaching: the source a client downloads to read the mail no longer carries the payload.Assert.That(processedEmail.MessageSourceBytes!.Length, Is.LessThan(attachmentData.Length / 4), "The stored source should no longer carry the attachment payload.");- processedEmail = EmailEncryption.DecryptEmail(processedEmail, PrivateKey);+ processedEmail = TestDecryption.DecryptEmail(processedEmail, PrivateKey);var sourceBytes = Gunzip(processedEmail.MessageSourceBytes!);var messageSource = Encoding.UTF8.GetString(sourceBytes);Assert.Multiple(() =>@@ -806,7 +807,7 @@ public class SmtpServerTestsforeach (var email in emails){- var decrypted = EmailEncryption.DecryptEmail(email, PrivateKey);+ var decrypted = TestDecryption.DecryptEmail(email, PrivateKey);var detachedBody = Gunzip(decrypted.Parts.Single().Bytes);var spliced = SpliceDetachedPart(Gunzip(decrypted.MessageSourceBytes!), detachedBody);diff --git a/apps/server/Tests/AliasVault.UnitTests/AliasVault.UnitTests.csproj b/apps/server/Tests/AliasVault.UnitTests/AliasVault.UnitTests.csprojindex ddde129f1..d50e6e158 100644--- a/apps/server/Tests/AliasVault.UnitTests/AliasVault.UnitTests.csproj+++ b/apps/server/Tests/AliasVault.UnitTests/AliasVault.UnitTests.csproj@@ -56,4 +56,8 @@<ProjectReference Include="..\..\Utilities\AliasVault.FaviconExtractor\AliasVault.FaviconExtractor.csproj" /></ItemGroup>+ <ItemGroup>+ <Compile Include="..\Shared\TestDecryption.cs" Link="Shared\TestDecryption.cs" />+ </ItemGroup>+</Project>diff --git a/apps/server/Tests/AliasVault.UnitTests/Utilities/EmailEncryptionTests.cs b/apps/server/Tests/AliasVault.UnitTests/Utilities/EmailEncryptionTests.csindex bfdc12db5..13089dfeb 100644--- a/apps/server/Tests/AliasVault.UnitTests/Utilities/EmailEncryptionTests.cs+++ b/apps/server/Tests/AliasVault.UnitTests/Utilities/EmailEncryptionTests.cs@@ -12,6 +12,7 @@ using System.Text;using AliasServerDb;using AliasVault.Cryptography;using AliasVault.Shared.Models.Enums;+using AliasVault.Tests.Shared;/// <summary>/// Tests for the EmailEncryption class, covering the source-only storage format (gzipped MessageSourceBytes)@@ -42,7 +43,7 @@ public class EmailEncryptionTestsAssert.That(encrypted.DecryptionKeys[0].VaultManifestDeliveryKeyId, Is.EqualTo(deliveryKey.Id));});- var decrypted = EmailEncryption.DecryptEmail(encrypted, RsaEncryptionTests.PrivateKey);+ var decrypted = TestDecryption.DecryptEmail(encrypted, RsaEncryptionTests.PrivateKey);Assert.Multiple(() =>{Assert.That(decrypted.MessageSourceBytes![0], Is.EqualTo(0x1f), "Decrypted source should still be gzip-compressed (gzip magic byte 1).");@@ -77,7 +78,7 @@ public class EmailEncryptionTestsAssert.That(encrypted.MessageSourceBytes, Is.Null, "MessageSourceBytes should stay null for legacy-shaped emails.");});- var decrypted = EmailEncryption.DecryptEmail(encrypted, RsaEncryptionTests.PrivateKey);+ var decrypted = TestDecryption.DecryptEmail(encrypted, RsaEncryptionTests.PrivateKey);Assert.Multiple(() =>{Assert.That(decrypted.MessageSource, Is.EqualTo("Legacy raw source"));diff --git a/apps/server/Tests/AliasVault.UnitTests/Utilities/RsaEncryptionTests.cs b/apps/server/Tests/AliasVault.UnitTests/Utilities/RsaEncryptionTests.csindex e6e5949e4..8a75c7cb7 100644--- a/apps/server/Tests/AliasVault.UnitTests/Utilities/RsaEncryptionTests.cs+++ b/apps/server/Tests/AliasVault.UnitTests/Utilities/RsaEncryptionTests.cs@@ -9,6 +9,7 @@ namespace AliasVault.UnitTests.Utilities;using System.Text.Json;using AliasVault.Cryptography;+using AliasVault.Tests.Shared;/// <summary>/// Tests for the SrpArgonEncryption class.@@ -48,7 +49,7 @@ public class RsaEncryptionTests// Client-side part:// -----------------// 4. Decrypt symmetric key with private key.- var decryptedSymmetricKey = Encryption.DecryptSymmetricKeyWithRsa(encryptedSymmetricKey, PrivateKey);+ var decryptedSymmetricKey = TestDecryption.DecryptSymmetricKeyWithRsa(encryptedSymmetricKey, PrivateKey);// 5. Decrypt email body with symmetric key.var decryptedEmailBody = Encryption.SymmetricDecrypt(encryptedEmailBody, decryptedSymmetricKey);@@ -118,7 +119,7 @@ public class RsaEncryptionTestsvar encryptedKey = Encryption.EncryptSymmetricKeyWithRsa(symmetricKey, PublicKey);// Assuming you have a method to decrypt with the private key- var decryptedKey = Encryption.DecryptSymmetricKeyWithRsa(encryptedKey, PrivateKey);+ var decryptedKey = TestDecryption.DecryptSymmetricKeyWithRsa(encryptedKey, PrivateKey);Assert.That(decryptedKey, Is.EqualTo(symmetricKey), "Decrypted key should match the original symmetric key.");}@@ -132,9 +133,9 @@ public class RsaEncryptionTestsvar symmetricKey = Encryption.GenerateRandomSymmetricKey();var encryptedKey = Encryption.EncryptSymmetricKeyWithRsa(symmetricKey, PublicKey, Encryption.MobileLoginPayloadKeyLabel);- Assert.That(Encryption.DecryptSymmetricKeyWithRsa(encryptedKey, PrivateKey, Encryption.MobileLoginPayloadKeyLabel), Is.EqualTo(symmetricKey));- Assert.That(() => Encryption.DecryptSymmetricKeyWithRsa(encryptedKey, PrivateKey, "aliasvault/v1/other"), Throws.Exception);- Assert.That(() => Encryption.DecryptSymmetricKeyWithRsa(encryptedKey, PrivateKey), Throws.Exception);+ Assert.That(TestDecryption.DecryptSymmetricKeyWithRsa(encryptedKey, PrivateKey, Encryption.MobileLoginPayloadKeyLabel), Is.EqualTo(symmetricKey));+ Assert.That(() => TestDecryption.DecryptSymmetricKeyWithRsa(encryptedKey, PrivateKey, "aliasvault/v1/other"), Throws.Exception);+ Assert.That(() => TestDecryption.DecryptSymmetricKeyWithRsa(encryptedKey, PrivateKey), Throws.Exception);}/// <summary>@@ -146,7 +147,7 @@ public class RsaEncryptionTestsvar symmetricKey = Encryption.GenerateRandomSymmetricKey();var encryptedKey = Encryption.EncryptSymmetricKeyWithRsa(symmetricKey, PublicKey, string.Empty);- Assert.That(Encryption.DecryptSymmetricKeyWithRsa(encryptedKey, PrivateKey), Is.EqualTo(symmetricKey));+ Assert.That(TestDecryption.DecryptSymmetricKeyWithRsa(encryptedKey, PrivateKey), Is.EqualTo(symmetricKey));}/// <summary>diff --git a/apps/server/Tests/Shared/TestDecryption.cs b/apps/server/Tests/Shared/TestDecryption.csnew file mode 100644index 000000000..c85af6200--- /dev/null+++ b/apps/server/Tests/Shared/TestDecryption.cs@@ -0,0 +1,144 @@+//-----------------------------------------------------------------------+// <copyright file="TestDecryption.cs" company="aliasvault">+// Copyright (c) aliasvault. All rights reserved.+// Licensed under the AGPLv3 license. See LICENSE.md file in the project root for full license information.+// </copyright>+//-----------------------------------------------------------------------++namespace AliasVault.Tests.Shared;++using System.Buffers.Text;+using System.Security.Cryptography;+using System.Text.Json;+using AliasServerDb;+using AliasVault.Cryptography;+using Org.BouncyCastle.Crypto.Parameters;+using Org.BouncyCastle.Security;++/// <summary>+/// Client-side decryption counterparts of the server's encryption, for tests only; the server never holds a private key.+/// </summary>+public static class TestDecryption+{+ /// <summary>+ /// Decrypts a symmetric key encrypted with <see cref="Encryption.EncryptSymmetricKeyWithRsa(byte[], string)"/> (BCL RSA-OAEP-SHA256, no label).+ /// </summary>+ /// <param name="ciphertext">The encrypted symmetric key as base64.</param>+ /// <param name="privateKey">The RSA private key in JWK format.</param>+ /// <returns>The decrypted symmetric key.</returns>+ public static byte[] DecryptSymmetricKeyWithRsa(string ciphertext, string privateKey)+ {+ using var rsa = RSA.Create();++ ImportPrivateKey(rsa, privateKey);+ return rsa.Decrypt(Convert.FromBase64String(ciphertext), RSAEncryptionPadding.OaepSHA256);+ }++ /// <summary>+ /// Decrypts a symmetric key encrypted with <see cref="Encryption.EncryptSymmetricKeyWithRsa(byte[], string, string)"/> under an OAEP label.+ /// </summary>+ /// <param name="ciphertext">The encrypted symmetric key as base64.</param>+ /// <param name="privateKey">The RSA private key in JWK format.</param>+ /// <param name="label">The OAEP label the ciphertext was encrypted under.</param>+ /// <returns>The decrypted symmetric key.</returns>+ public static byte[] DecryptSymmetricKeyWithRsa(string ciphertext, string privateKey, string label)+ {+ var jwk = JsonSerializer.Deserialize<JsonElement>(privateKey);+ var key = new RsaPrivateCrtKeyParameters(Encryption.JwkInteger(jwk, "n"), Encryption.JwkInteger(jwk, "e"), Encryption.JwkInteger(jwk, "d"), Encryption.JwkInteger(jwk, "p"), Encryption.JwkInteger(jwk, "q"), Encryption.JwkInteger(jwk, "dp"), Encryption.JwkInteger(jwk, "dq"), Encryption.JwkInteger(jwk, "qi"));+ var oaep = Encryption.LabelledOaep(label);+ oaep.Init(false, new ParametersWithRandom(key, new SecureRandom()));+ var cipherBytes = Convert.FromBase64String(ciphertext);+ return oaep.ProcessBlock(cipherBytes, 0, cipherBytes.Length);+ }++ /// <summary>+ /// Decrypts an email encrypted by <see cref="EmailEncryption.EncryptEmail"/> with the private key of one of its delivery keys.+ /// </summary>+ /// <param name="email">The encrypted email; its fields are replaced with the decrypted values.</param>+ /// <param name="userPrivateKey">The RSA private key in JWK format.</param>+ /// <returns>The same email object with all encrypted fields decrypted.</returns>+ public static Email DecryptEmail(Email email, string userPrivateKey)+ {+ // With multiple decryption keys the matching one is found by trying each.+ byte[]? symmetricKey = null;+ foreach (var decryptionKey in email.DecryptionKeys)+ {+ try+ {+ symmetricKey = DecryptSymmetricKeyWithRsa(decryptionKey.EncryptedSymmetricKey, userPrivateKey);+ break;+ }+ catch (CryptographicException)+ {+ // The key belongs to another manifest; try the next one.+ }+ }++ if (symmetricKey is null)+ {+ throw new InvalidOperationException("The email carries no decryption key that the provided private key can open.");+ }++ if (email.MessageHtml is not null)+ {+ email.MessageHtml = Encryption.SymmetricDecrypt(email.MessageHtml, symmetricKey);+ }++ if (email.MessagePlain is not null)+ {+ email.MessagePlain = Encryption.SymmetricDecrypt(email.MessagePlain, symmetricKey);+ }++ if (email.MessagePreview is not null)+ {+ email.MessagePreview = Encryption.SymmetricDecrypt(email.MessagePreview, symmetricKey);+ }++ if (email.MessageSource is not null)+ {+ email.MessageSource = Encryption.SymmetricDecrypt(email.MessageSource, symmetricKey);+ }++ if (email.MessageSourceBytes is not null)+ {+ // The decrypted bytes are the gzip-compressed source; callers must gunzip to get the raw MIME.+ email.MessageSourceBytes = Encryption.SymmetricDecrypt(email.MessageSourceBytes, symmetricKey);+ }++ email.Subject = Encryption.SymmetricDecrypt(email.Subject, symmetricKey);+ email.From = Encryption.SymmetricDecrypt(email.From, symmetricKey);+ email.FromLocal = Encryption.SymmetricDecrypt(email.FromLocal, symmetricKey);+ email.FromDomain = Encryption.SymmetricDecrypt(email.FromDomain, symmetricKey);++ // Decrypt the attachment bodies that were detached from the source.+ foreach (var part in email.Parts)+ {+ part.Bytes = Encryption.SymmetricDecrypt(part.Bytes, symmetricKey);+ }++ return email;+ }++ /// <summary>+ /// Imports an RSA private key from JWK format.+ /// </summary>+ /// <param name="rsa">The RSA provider to import the key into.</param>+ /// <param name="jwk">The private key in JWK format.</param>+ private static void ImportPrivateKey(RSA rsa, string jwk)+ {+ var jwkObj = JsonSerializer.Deserialize<JsonElement>(jwk);+ byte[] Member(string name) => Base64Url.DecodeFromChars(jwkObj.GetProperty(name).GetString()!);++ rsa.ImportParameters(new RSAParameters+ {+ Modulus = Member("n"),+ Exponent = Member("e"),+ D = Member("d"),+ P = Member("p"),+ Q = Member("q"),+ DP = Member("dp"),+ DQ = Member("dq"),+ InverseQ = Member("qi"),+ });+ }+}diff --git a/apps/server/Utilities/AliasVault.Cryptography/AliasVault.Cryptography.csproj b/apps/server/Utilities/AliasVault.Cryptography/AliasVault.Cryptography.csprojindex 36e563f30..2ccf21579 100644--- a/apps/server/Utilities/AliasVault.Cryptography/AliasVault.Cryptography.csproj+++ b/apps/server/Utilities/AliasVault.Cryptography/AliasVault.Cryptography.csproj@@ -30,6 +30,11 @@</PackageReference></ItemGroup>+ <ItemGroup>+ <InternalsVisibleTo Include="AliasVault.UnitTests" />+ <InternalsVisibleTo Include="AliasVault.IntegrationTests" />+ </ItemGroup>+<ItemGroup><ProjectReference Include="..\..\Databases\AliasServerDb\AliasServerDb.csproj" /><ProjectReference Include="..\..\Shared\AliasVault.Shared.Server\AliasVault.Shared.Server.csproj" />