Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.
Refactor VaultKeyAlgorithms.cs (#2159)
Leendert de Borstdf91bcbaf991
diff --git a/apps/server/Databases/AliasServerDb/VaultKeyAlgorithmDefinition.cs b/apps/server/Databases/AliasServerDb/VaultKeyAlgorithmDefinition.csnew file mode 100644index 000000000..f619c0574--- /dev/null+++ b/apps/server/Databases/AliasServerDb/VaultKeyAlgorithmDefinition.cs@@ -0,0 +1,29 @@+//-----------------------------------------------------------------------+// <copyright file="VaultKeyAlgorithmDefinition.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 AliasServerDb;++/// <summary>+/// The properties of a single <see cref="VaultKeyAlgorithm"/>.+/// </summary>+public sealed class VaultKeyAlgorithmDefinition+{+ /// <summary>+ /// Gets the algorithm this definition describes.+ /// </summary>+ public required VaultKeyAlgorithm Algorithm { get; init; }++ /// <summary>+ /// Gets the token that names the algorithm on the wire and in the database.+ /// </summary>+ public required string Token { get; init; }++ /// <summary>+ /// Gets a value indicating whether the algorithm encrypts to a public key.+ /// </summary>+ public required bool IsAsymmetric { get; init; }+}diff --git a/apps/server/Databases/AliasServerDb/VaultKeyAlgorithms.cs b/apps/server/Databases/AliasServerDb/VaultKeyAlgorithms.csindex a31fcf7d8..253277726 100644--- a/apps/server/Databases/AliasServerDb/VaultKeyAlgorithms.cs+++ b/apps/server/Databases/AliasServerDb/VaultKeyAlgorithms.cs@@ -1,4 +1,4 @@-//-----------------------------------------------------------------------+//-----------------------------------------------------------------------// <copyright file="VaultKeyAlgorithms.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.@@ -13,26 +13,60 @@ namespace AliasServerDb;public static class VaultKeyAlgorithms{/// <summary>- /// The token for <see cref="VaultKeyAlgorithm.Aes256Gcm"/>.+ /// Every algorithm this build supports./// </summary>- public const string Aes256GcmToken = "aes256-gcm";+ private static readonly VaultKeyAlgorithmDefinition[] Definitions =+ [+ new VaultKeyAlgorithmDefinition+ {+ Algorithm = VaultKeyAlgorithm.Aes256Gcm,+ Token = "aes256-gcm",+ IsAsymmetric = false,+ },+ new VaultKeyAlgorithmDefinition+ {+ Algorithm = VaultKeyAlgorithm.RsaOaepSha256,+ Token = "rsa-oaep-sha256",+ IsAsymmetric = true,+ },+ ];++ private static readonly Dictionary<VaultKeyAlgorithm, VaultKeyAlgorithmDefinition> ByAlgorithm = Definitions.ToDictionary(x => x.Algorithm);+ private static readonly Dictionary<string, VaultKeyAlgorithmDefinition> ByToken = Definitions.ToDictionary(x => x.Token, StringComparer.Ordinal);++ /// <summary>+ /// Initializes static members of the <see cref="VaultKeyAlgorithms"/> class.+ /// </summary>+ static VaultKeyAlgorithms()+ {+ var undefined = Enum.GetValues<VaultKeyAlgorithm>().Where(x => !ByAlgorithm.ContainsKey(x)).ToList();+ if (undefined.Count > 0)+ {+ throw new InvalidOperationException($"Vault key algorithm(s) without a definition: {string.Join(", ", undefined)}.");+ }+ }/// <summary>- /// The token for <see cref="VaultKeyAlgorithm.RsaOaepSha256"/>.+ /// Returns the definition of an algorithm./// </summary>- public const string RsaOaepSha256Token = "rsa-oaep-sha256";+ /// <param name="algorithm">The algorithm to look up.</param>+ /// <returns>The definition.</returns>+ public static VaultKeyAlgorithmDefinition GetDefinition(VaultKeyAlgorithm algorithm)+ {+ if (!ByAlgorithm.TryGetValue(algorithm, out var definition))+ {+ throw new ArgumentOutOfRangeException(nameof(algorithm), algorithm, "Unknown vault key algorithm.");+ }++ return definition;+ }/// <summary>/// Returns the token for an algorithm./// </summary>/// <param name="algorithm">The algorithm to convert.</param>/// <returns>The token.</returns>- public static string ToToken(VaultKeyAlgorithm algorithm) => algorithm switch- {- VaultKeyAlgorithm.Aes256Gcm => Aes256GcmToken,- VaultKeyAlgorithm.RsaOaepSha256 => RsaOaepSha256Token,- _ => throw new ArgumentOutOfRangeException(nameof(algorithm), algorithm, "Unknown vault key algorithm."),- };+ public static string ToToken(VaultKeyAlgorithm algorithm) => GetDefinition(algorithm).Token;/// <summary>/// Parses a token.@@ -42,18 +76,14 @@ public static class VaultKeyAlgorithms/// <returns>True when the token names an algorithm this build supports.</returns>public static bool TryParse(string? token, out VaultKeyAlgorithm algorithm){- switch (token)+ if (token is null || !ByToken.TryGetValue(token, out var definition)){- case Aes256GcmToken:- algorithm = VaultKeyAlgorithm.Aes256Gcm;- return true;- case RsaOaepSha256Token:- algorithm = VaultKeyAlgorithm.RsaOaepSha256;- return true;- default:- algorithm = default;- return false;+ algorithm = default;+ return false;}++ algorithm = definition.Algorithm;+ return true;}/// <summary>@@ -72,10 +102,9 @@ public static class VaultKeyAlgorithms}/// <summary>- /// Whether an algorithm encrypts to a public key, i.e. is valid on a <see cref="ManifestKeyType.GrantKey"/> grant. A- /// symmetric algorithm there would mean the sharer and the recipient share a secret, which they never do.+ /// Whether an algorithm encrypts to a public key, i.e. is valid on a <see cref="ManifestKeyType.GrantKey"/> grant./// </summary>/// <param name="algorithm">The algorithm to check.</param>/// <returns>True for asymmetric algorithms.</returns>- public static bool IsAsymmetric(VaultKeyAlgorithm algorithm) => algorithm == VaultKeyAlgorithm.RsaOaepSha256;+ public static bool IsAsymmetric(VaultKeyAlgorithm algorithm) => GetDefinition(algorithm).IsAsymmetric;}