Skip to content

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

3.3 KB
Raw Blame
// <auto-generated />
// This file is auto-generated from core/models/scripts/generate-key-vocabulary.cjs.
// Do not edit this file directly. Run 'core/models/build.sh' (or 'node core/models/scripts/generate-key-vocabulary.cjs') to regenerate.

#nullable enable

namespace AliasVault.Shared.Models.Enums;

/// <summary>
/// The algorithms a piece of vault key ciphertext can be encrypted with. The token travels next to every
/// ciphertext, so a reader always knows how to open it without inferring anything from context.
/// The token is the only persisted and transmitted identifier; the ordinals below are cosmetic.
/// </summary>
public enum VaultKeyAlgorithm
{
    /// <summary>
    /// AES-256-GCM: symmetric, used where the reader already holds the wrapping key.
    /// </summary>
    Aes256Gcm = 0,

    /// <summary>
    /// RSA-OAEP with SHA-256: asymmetric, used to encrypt a VEK to a recipient's public key.
    /// </summary>
    RsaOaepSha256 = 1,
}

/// <summary>
/// Token conversions for <see cref="VaultKeyAlgorithm"/>.
/// </summary>
public static class VaultKeyAlgorithms
{
    /// <summary>
    /// Returns the wire/storage token for a VaultKeyAlgorithm.
    /// </summary>
    /// <param name="type">The VaultKeyAlgorithm to convert.</param>
    /// <returns>The token.</returns>
    public static string ToToken(VaultKeyAlgorithm type) => type switch
    {
        VaultKeyAlgorithm.Aes256Gcm => "aes256-gcm",
        VaultKeyAlgorithm.RsaOaepSha256 => "rsa-oaep-sha256",
        _ => throw new ArgumentOutOfRangeException(nameof(type), type, "Unknown VaultKeyAlgorithm."),
    };

    /// <summary>
    /// Parses a token.
    /// </summary>
    /// <param name="token">The token to parse.</param>
    /// <param name="type">The parsed VaultKeyAlgorithm.</param>
    /// <returns>True when the token names a VaultKeyAlgorithm this build supports.</returns>
    public static bool TryParse(string? token, out VaultKeyAlgorithm type)
    {
        switch (token)
        {
            case "aes256-gcm":
                type = VaultKeyAlgorithm.Aes256Gcm;
                return true;
            case "rsa-oaep-sha256":
                type = VaultKeyAlgorithm.RsaOaepSha256;
                return true;
            default:
                type = default;
                return false;
        }
    }

    /// <summary>
    /// Parses a token and returns the VaultKeyAlgorithm.
    /// </summary>
    /// <param name="token">The token to parse.</param>
    /// <returns>The parsed VaultKeyAlgorithm.</returns>
    public static VaultKeyAlgorithm Parse(string? token)
    {
        if (!TryParse(token, out var type))
        {
            throw new ArgumentOutOfRangeException(nameof(token), token, "Unknown VaultKeyAlgorithm token.");
        }

        return type;
    }

    /// <summary>
    /// Whether the algorithm encrypts to a public key, which is what a grant requires.
    /// </summary>
    /// <param name="type">The VaultKeyAlgorithm to check.</param>
    /// <returns>The flag value for that VaultKeyAlgorithm.</returns>
    public static bool IsAsymmetric(VaultKeyAlgorithm type) => type switch
    {
        VaultKeyAlgorithm.Aes256Gcm => false,
        VaultKeyAlgorithm.RsaOaepSha256 => true,
        _ => throw new ArgumentOutOfRangeException(nameof(type), type, "Unknown VaultKeyAlgorithm."),
    };
}