Skip to content

Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.

Remove explicit keysize set and add RSA key validation sanity checks (#2469)

Leendert de Borst759f52198b2f
diff --git a/apps/server/AliasVault.Api/Controllers/V1/VaultController.cs b/apps/server/AliasVault.Api/Controllers/V1/VaultController.cs
index 774557c90..51f3bd0a7 100644
--- a/apps/server/AliasVault.Api/Controllers/V1/VaultController.cs
+++ b/apps/server/AliasVault.Api/Controllers/V1/VaultController.cs
@@ -159,6 +159,11 @@ public class VaultController(ILogger<VaultController> logger, IAliasServerDbCont
return BadRequest(ApiErrorCodeHelper.CreateValidationErrorResponse(ApiErrorCode.USERNAME_MISMATCH, 400));
}
+ if (!string.IsNullOrEmpty(model.EncryptionPublicKey) && !RsaPublicKeyValidator.IsValid(model.EncryptionPublicKey))
+ {
+ return BadRequest(ApiErrorCodeHelper.CreateValidationErrorResponse(ApiErrorCode.VAULT_ERROR, 400));
+ }
+
// Retrieve the current revision of the user's personal manifest, which contains the current encryption settings.
var currentManifest = await context.VaultManifests.FirstAsync(x => x.OwnerGroupId == user.PersonalGroupId);
diff --git a/apps/server/AliasVault.Api/Controllers/V2/AuthController.cs b/apps/server/AliasVault.Api/Controllers/V2/AuthController.cs
index 4d0cd2a36..d8a90d297 100644
--- a/apps/server/AliasVault.Api/Controllers/V2/AuthController.cs
+++ b/apps/server/AliasVault.Api/Controllers/V2/AuthController.cs
@@ -444,7 +444,7 @@ public class AuthController(IAliasServerDbContextFactory dbContextFactory, UserM
return BadRequest(ApiErrorCodeHelper.CreateValidationErrorResponse(ApiErrorCode.VAULT_KEY_NOT_FOUND, 400));
}
- if (!model.AccountKeysFitStorageLimits || !Signing.VerifyAccountPublicKey(model.SigningPublicKey, model.AccountPublicKey, model.AccountPublicKeySignature))
+ if (!model.AccountKeysFitStorageLimits || !RsaPublicKeyValidator.IsValid(model.AccountPublicKey) || !Signing.VerifyAccountPublicKey(model.SigningPublicKey, model.AccountPublicKey, model.AccountPublicKeySignature))
{
return BadRequest(ApiErrorCodeHelper.CreateValidationErrorResponse(ApiErrorCode.VAULT_ERROR, 400));
}
@@ -817,7 +817,7 @@ public class AuthController(IAliasServerDbContextFactory dbContextFactory, UserM
public async Task<IActionResult> InitiateMobileLogin([FromBody] MobileLoginInitiateRequest model, [FromServices] MobileLoginRateLimitService mobileLoginRateLimitService)
{
// Reject invalid public key structure.
- if (!MobileLoginPublicKeyValidator.IsValid(model.ClientPublicKey))
+ if (!RsaPublicKeyValidator.IsValid(model.ClientPublicKey))
{
return BadRequest(ApiErrorCodeHelper.CreateErrorResponse(ApiErrorCode.MOBILE_LOGIN_INVALID_PUBLIC_KEY, 400));
}
diff --git a/apps/server/AliasVault.Api/Controllers/V2/VaultController.cs b/apps/server/AliasVault.Api/Controllers/V2/VaultController.cs
index a64e36592..cfd00ece7 100644
--- a/apps/server/AliasVault.Api/Controllers/V2/VaultController.cs
+++ b/apps/server/AliasVault.Api/Controllers/V2/VaultController.cs
@@ -397,7 +397,7 @@ public class VaultController(
return BadRequest(ApiErrorCodeHelper.CreateValidationErrorResponse(ApiErrorCode.VAULT_KEY_NOT_FOUND, 400));
}
- if (!accountKeys.FitsStorageLimits || !Signing.VerifyAccountPublicKey(accountKeys.SigningPublicKey, accountKeys.AccountPublicKey, accountKeys.AccountPublicKeySignature))
+ if (!accountKeys.FitsStorageLimits || !RsaPublicKeyValidator.IsValid(accountKeys.AccountPublicKey) || !Signing.VerifyAccountPublicKey(accountKeys.SigningPublicKey, accountKeys.AccountPublicKey, accountKeys.AccountPublicKeySignature))
{
return BadRequest(ApiErrorCodeHelper.CreateValidationErrorResponse(ApiErrorCode.VAULT_ERROR, 400));
}
@@ -442,6 +442,12 @@ public class VaultController(
});
}
+ // The SMTP service encrypts every incoming mail with the primary delivery key, so a malformed one would lose that mail.
+ if (resolved.Any(r => !string.IsNullOrEmpty(r.Write.EncryptionPublicKey) && !RsaPublicKeyValidator.IsValid(r.Write.EncryptionPublicKey)))
+ {
+ return BadRequest(ApiErrorCodeHelper.CreateValidationErrorResponse(ApiErrorCode.VAULT_ERROR, 400));
+ }
+
// A new delivery key must be signed by the caller, so a stolen access token alone cannot redirect incoming mail.
if (!await DeliveryKeyChangesAreSignedAsync(context, user.Id, resolved, accountKeys))
{
diff --git a/apps/server/AliasVault.Api/Helpers/MobileLoginPublicKeyValidator.cs b/apps/server/AliasVault.Api/Helpers/RsaPublicKeyValidator.cs
similarity index 88%
rename from apps/server/AliasVault.Api/Helpers/MobileLoginPublicKeyValidator.cs
rename to apps/server/AliasVault.Api/Helpers/RsaPublicKeyValidator.cs
index 2d9908f3a..4e721b534 100644
--- a/apps/server/AliasVault.Api/Helpers/MobileLoginPublicKeyValidator.cs
+++ b/apps/server/AliasVault.Api/Helpers/RsaPublicKeyValidator.cs
@@ -1,5 +1,5 @@
-//-----------------------------------------------------------------------
-// <copyright file="MobileLoginPublicKeyValidator.cs" company="aliasvault">
+//-----------------------------------------------------------------------
+// <copyright file="RsaPublicKeyValidator.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>
@@ -11,11 +11,9 @@ using System.Buffers.Text;
using System.Text.Json;
/// <summary>
-/// Validates the client public key supplied when initiating a mobile login request. Clients send the
-/// RSA-OAEP public key as a JSON serialized JWK, so anything that is not a well-formed RSA public JWK
-/// of an accepted key size is rejected before it is persisted.
+/// Validates an RSA-OAEP public key (JSON serialized JWK) supplied by a client before it is persisted.
/// </summary>
-public static class MobileLoginPublicKeyValidator
+public static class RsaPublicKeyValidator
{
/// <summary>
/// Maximum accepted length for the serialized JWK. Current clients generate RSA-2048 keys, which are +/- 500 characters long.
@@ -55,7 +53,6 @@ public static class MobileLoginPublicKeyValidator
return false;
}
- // The mobile login handshake only uses RSA keys.
if (!TryGetNonEmptyString(root, "kty", out var keyType) || keyType != "RSA")
{
return false;
diff --git a/apps/server/Utilities/AliasVault.Cryptography/Encryption.cs b/apps/server/Utilities/AliasVault.Cryptography/Encryption.cs
index 4dd04d79d..50b6a23f7 100644
--- a/apps/server/Utilities/AliasVault.Cryptography/Encryption.cs
+++ b/apps/server/Utilities/AliasVault.Cryptography/Encryption.cs
@@ -36,7 +36,6 @@ public static class Encryption
using (var rsa = RSA.Create())
{
ImportPublicKey(rsa, publicKey);
- rsa.KeySize = 2048;
var rsaParams = RSAEncryptionPadding.OaepSHA256;
byte[] encryptedKey = rsa.Encrypt(symmetricKey, rsaParams);
@@ -55,7 +54,6 @@ public static class Encryption
using var rsa = RSA.Create();
ImportPrivateKey(rsa, privateKey);
- rsa.KeySize = 2048;
var rsaParams = RSAEncryptionPadding.OaepSHA256;
byte[] cipherBytes = Convert.FromBase64String(ciphertext);