Skip to content

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

Implement SPR for login flow (#43)

Leendert de Borst9f7ba2eb2052
diff --git a/README.md b/README.md
index 076c46642..c08f10915 100644
--- a/README.md
+++ b/README.md
@@ -63,8 +63,10 @@ The following technologies, frameworks and libraries are used in this project:
- [ASP.NET Core](https://dotnet.microsoft.com/apps/aspnet) - An open-source framework for building modern, cloud-based, internet-connected applications.
- [Entity Framework Core](https://docs.microsoft.com/en-us/ef/core/) - A lightweight, extensible, open-source and cross-platform version of the popular Entity Framework data access technology.
- [Blazor WASM](https://dotnet.microsoft.com/apps/aspnet/web-apps/blazor) - A framework for building interactive web UIs using C# instead of JavaScript. It's a single-page app framework that runs in the browser via WebAssembly.
+- [Playwright](https://playwright.dev/) - A Node.js library to automate Chromium, Firefox and WebKit with a single API. Used for end-to-end testing.
- [Docker](https://www.docker.com/) - A platform for building, sharing, and running containerized applications.
- [SQLite](https://www.sqlite.org/index.html) - A C-language library that implements a small, fast, self-contained, high-reliability, full-featured, SQL database engine.
- [Tailwind CSS](https://tailwindcss.com/) - A utility-first CSS framework for rapidly building custom designs.
- [Flowbite](https://flowbite.com/) - A free and open-source UI component library based on Tailwind CSS.
- [Konscious.Security.Cryptography](https://github.com/kmaragon/Konscious.Security.Cryptography) - A .NET library that implements Argon2id, a memory-hard password hashing algorithm.
+- [SRP.net](https://github.com/secure-remote-password/srp.net) - SRP6a Secure Remote Password protocol for secure password authentication.
diff --git a/src/AliasVault.Api/Controllers/AuthController.cs b/src/AliasVault.Api/Controllers/AuthController.cs
index 86e6d3999..0d4309e2e 100644
--- a/src/AliasVault.Api/Controllers/AuthController.cs
+++ b/src/AliasVault.Api/Controllers/AuthController.cs
@@ -3,9 +3,7 @@
// Copyright (c) lanedirt. All rights reserved.
// Licensed under the MIT license. See LICENSE.md file in the project root for full license information.
// </copyright>
-//-----------------------------------------------------------------------s
-
-using Cryptography.Models;
+//-----------------------------------------------------------------------
namespace AliasVault.Api.Controllers;
@@ -16,9 +14,12 @@ using System.Text;
using AliasDb;
using AliasVault.Shared.Models;
using AliasVault.Shared.Models.WebApi;
+using AliasVault.Shared.Models.WebApi.Auth;
using Asp.Versioning;
+using Cryptography.Models;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
+using Microsoft.Extensions.Caching.Memory;
using Microsoft.IdentityModel.Tokens;
/// <summary>
@@ -28,10 +29,11 @@ using Microsoft.IdentityModel.Tokens;
/// <param name="userManager">UserManager instance.</param>
/// <param name="signInManager">SignInManager instance.</param>
/// <param name="configuration">IConfiguration instance.</param>
+/// <param name="cache">IMemoryCache instance for persisting SRP values during multi-step login process.</param>
[Route("api/v{version:apiVersion}/[controller]")]
[ApiController]
[ApiVersion("1")]
-public class AuthController(AliasDbContext context, UserManager<AliasVaultUser> userManager, SignInManager<AliasVaultUser> signInManager, IConfiguration configuration) : ControllerBase
+public class AuthController(AliasDbContext context, UserManager<AliasVaultUser> userManager, SignInManager<AliasVaultUser> signInManager, IConfiguration configuration, IMemoryCache cache) : ControllerBase
{
/// <summary>
/// Login endpoint used to process login attempt using credentials.
@@ -39,16 +41,63 @@ public class AuthController(AliasDbContext context, UserManager<AliasVaultUser>
/// <param name="model">Login model.</param>
/// <returns>IActionResult.</returns>
[HttpPost("login")]
- public async Task<IActionResult> Login([FromBody] LoginModel model)
+ public async Task<IActionResult> Login([FromBody] LoginRequest model)
{
var user = await userManager.FindByEmailAsync(model.Email);
- if (user != null && await userManager.CheckPasswordAsync(user, model.Password))
+ if (user == null)
{
- var tokenModel = await GenerateNewTokensForUser(user);
- return Ok(tokenModel);
+ return BadRequest(ServerValidationErrorResponse.Create(new [] { "Invalid email or password. Please try again." }, 400));
}
- return BadRequest(ServerValidationErrorResponse.Create("Invalid username or password. Please try again.", 400));
+ // Server creates ephemeral and sends to client
+ var ephemeral = Cryptography.Srp.GenerateEphemeralServer(user.Verifier);
+
+ // Store the server ephemeral in memory cache for Validate() endpoint to use.
+ cache.Set(model.Email, ephemeral.Secret, TimeSpan.FromMinutes(5));
+ cache.Set(model.Email, ephemeral.Secret, TimeSpan.FromMinutes(5));
+
+ return Ok(new LoginResponse(user.Salt, ephemeral.Public));
+ }
+
+ /// <summary>
+ /// Validate endpoint used to validate the client's proof and generate the server's proof.
+ /// </summary>
+ /// <param name="model">ValidateLoginRequest model.</param>
+ /// <returns>IActionResult.</returns>
+ [HttpPost("validate")]
+ public async Task<IActionResult> Validate([FromBody] ValidateLoginRequest model)
+ {
+ var user = await userManager.FindByEmailAsync(model.Email);
+ if (user == null)
+ {
+ return BadRequest(ServerValidationErrorResponse.Create(new [] { "Invalid email or password. Please try again." }, 400));
+ }
+
+ if (!cache.TryGetValue(model.Email, out var serverSecretEphemeral) || !(serverSecretEphemeral is string))
+ {
+ return BadRequest(ServerValidationErrorResponse.Create(new [] { "Invalid email or password. Please try again." }, 400));
+ }
+
+ try
+ {
+ var serverSession = Cryptography.Srp.DeriveSessionServer(
+ serverSecretEphemeral.ToString() ?? string.Empty,
+ model.ClientPublicEphemeral,
+ user.Salt,
+ model.Email,
+ user.Verifier,
+ model.ClientSessionProof);
+
+ // If above does not throw an exception., then the client's proof is valid, and we can issue the JWT token.
+ var tokenModel = await GenerateNewTokensForUser(user);
+
+ // Return server proof for optional client check and token.
+ return Ok(new ValidateLoginResponse(serverSession.Proof, tokenModel));
+ }
+ catch
+ {
+ return BadRequest(ServerValidationErrorResponse.Create(new [] { "Invalid email or password. Please try again." }, 400));
+ }
}
/// <summary>
diff --git a/src/AliasVault.Api/Controllers/AuthenticatedRequestController.cs b/src/AliasVault.Api/Controllers/AuthenticatedRequestController.cs
index 5d57895a5..3ae720c22 100644
--- a/src/AliasVault.Api/Controllers/AuthenticatedRequestController.cs
+++ b/src/AliasVault.Api/Controllers/AuthenticatedRequestController.cs
@@ -5,11 +5,10 @@
// </copyright>
//-----------------------------------------------------------------------
-using AliasDb;
-
namespace AliasVault.Api.Controllers;
using System.Security.Claims;
+using AliasDb;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
diff --git a/src/AliasVault.Api/Controllers/IdentityController.cs b/src/AliasVault.Api/Controllers/IdentityController.cs
index 2e090c031..da11de04c 100644
--- a/src/AliasVault.Api/Controllers/IdentityController.cs
+++ b/src/AliasVault.Api/Controllers/IdentityController.cs
@@ -5,10 +5,9 @@
// </copyright>
//-----------------------------------------------------------------------
-using AliasDb;
-
namespace AliasVault.Api.Controllers;
+using AliasDb;
using AliasGenerators.Identity;
using AliasGenerators.Identity.Implementations;
using Asp.Versioning;
diff --git a/src/AliasVault.Api/Program.cs b/src/AliasVault.Api/Program.cs
index e434e013d..d74024aef 100644
--- a/src/AliasVault.Api/Program.cs
+++ b/src/AliasVault.Api/Program.cs
@@ -105,6 +105,8 @@ builder.Services.AddCors(options =>
});
builder.Services.AddControllers();
+builder.Services.AddMemoryCache();
+
builder.Services.AddApiVersioning(options =>
{
options.DefaultApiVersion = new ApiVersion(1, 0);
diff --git a/src/AliasVault.Shared/Models/WebApi/Auth/LoginRequest.cs b/src/AliasVault.Shared/Models/WebApi/Auth/LoginRequest.cs
new file mode 100644
index 000000000..1243a8167
--- /dev/null
+++ b/src/AliasVault.Shared/Models/WebApi/Auth/LoginRequest.cs
@@ -0,0 +1,28 @@
+//-----------------------------------------------------------------------
+// <copyright file="LoginRequest.cs" company="lanedirt">
+// Copyright (c) lanedirt. All rights reserved.
+// Licensed under the MIT license. See LICENSE.md file in the project root for full license information.
+// </copyright>
+//-----------------------------------------------------------------------
+
+namespace AliasVault.Shared.Models.WebApi.Auth;
+
+/// <summary>
+/// Represents a login request.
+/// </summary>
+public class LoginRequest
+{
+ /// <summary>
+ /// Initializes a new instance of the <see cref="LoginRequest"/> class.
+ /// </summary>
+ /// <param name="email">Email.</param>
+ public LoginRequest(string email)
+ {
+ Email = email;
+ }
+
+ /// <summary>
+ /// Gets or sets the email address.
+ /// </summary>
+ public string Email { get; set; }
+}
diff --git a/src/AliasVault.Shared/Models/WebApi/Auth/LoginResponse.cs b/src/AliasVault.Shared/Models/WebApi/Auth/LoginResponse.cs
new file mode 100644
index 000000000..94d767fe0
--- /dev/null
+++ b/src/AliasVault.Shared/Models/WebApi/Auth/LoginResponse.cs
@@ -0,0 +1,39 @@
+//-----------------------------------------------------------------------
+// <copyright file="LoginResponse.cs" company="lanedirt">
+// Copyright (c) lanedirt. All rights reserved.
+// Licensed under the MIT license. See LICENSE.md file in the project root for full license information.
+// </copyright>
+//-----------------------------------------------------------------------
+
+using System.Text.Json.Serialization;
+
+namespace AliasVault.Shared.Models.WebApi.Auth;
+
+/// <summary>
+/// Represents a login response.
+/// </summary>
+public class LoginResponse
+{
+ /// <summary>
+ /// Initializes a new instance of the <see cref="LoginResponse"/> class.
+ /// </summary>
+ /// <param name="salt">Salt.</param>
+ /// <param name="serverEphemeral">Server ephemeral.</param>
+ public LoginResponse(string salt, string serverEphemeral)
+ {
+ Salt = salt;
+ ServerEphemeral = serverEphemeral;
+ }
+
+ /// <summary>
+ /// Gets or sets the salt.
+ /// </summary>
+ [JsonPropertyName("salt")]
+ public string Salt { get; set; }
+
+ /// <summary>
+ /// Gets or sets the server's public ephemeral value.
+ /// </summary>
+ [JsonPropertyName("serverEphemeral")]
+ public string ServerEphemeral { get; set; }
+}
diff --git a/src/AliasVault.Shared/Models/WebApi/Auth/ValidateLoginRequest.cs b/src/AliasVault.Shared/Models/WebApi/Auth/ValidateLoginRequest.cs
new file mode 100644
index 000000000..ea5d391dd
--- /dev/null
+++ b/src/AliasVault.Shared/Models/WebApi/Auth/ValidateLoginRequest.cs
@@ -0,0 +1,43 @@
+//-----------------------------------------------------------------------
+// <copyright file="ValidateLoginRequest.cs" company="lanedirt">
+// Copyright (c) lanedirt. All rights reserved.
+// Licensed under the MIT license. See LICENSE.md file in the project root for full license information.
+// </copyright>
+//-----------------------------------------------------------------------
+
+namespace AliasVault.Shared.Models.WebApi.Auth
+{
+ /// <summary>
+ /// Represents a request to validate a login.
+ /// </summary>
+ public class ValidateLoginRequest
+ {
+ /// <summary>
+ /// Initializes a new instance of the <see cref="ValidateLoginRequest"/> class.
+ /// </summary>
+ /// <param name="email">Email.</param>
+ /// <param name="clientPublicEphemeral">Client public ephemeral.</param>
+ /// <param name="clientSessionProof">Client session proof.</param>
+ public ValidateLoginRequest(string email, string clientPublicEphemeral, string clientSessionProof)
+ {
+ Email = email;
+ ClientPublicEphemeral = clientPublicEphemeral;
+ ClientSessionProof = clientSessionProof;
+ }
+
+ /// <summary>
+ /// Gets or sets the email.
+ /// </summary>
+ public string Email { get; set; }
+
+ /// <summary>
+ /// Gets or sets the client's public ephemeral value.
+ /// </summary>
+ public string ClientPublicEphemeral { get; set; }
+
+ /// <summary>
+ /// Gets or sets the client's session proof.
+ /// </summary>
+ public string ClientSessionProof { get; set; }
+ }
+}
diff --git a/src/AliasVault.Shared/Models/WebApi/Auth/ValidateLoginResponse.cs b/src/AliasVault.Shared/Models/WebApi/Auth/ValidateLoginResponse.cs
new file mode 100644
index 000000000..dd507bcc7
--- /dev/null
+++ b/src/AliasVault.Shared/Models/WebApi/Auth/ValidateLoginResponse.cs
@@ -0,0 +1,40 @@
+//-----------------------------------------------------------------------
+// <copyright file="ValidateLoginResponse.cs" company="lanedirt">
+// Copyright (c) lanedirt. All rights reserved.
+// Licensed under the MIT license. See LICENSE.md file in the project root for full license information.
+// </copyright>
+//-----------------------------------------------------------------------
+
+using System.Text.Json.Serialization;
+
+namespace AliasVault.Shared.Models.WebApi.Auth
+{
+ /// <summary>
+ /// Represents a request to validate a login.
+ /// </summary>
+ public class ValidateLoginResponse
+ {
+ /// <summary>
+ /// Initializes a new instance of the <see cref="ValidateLoginResponse"/> class.
+ /// </summary>
+ /// <param name="serverSessionProof">Client session proof.</param>
+ /// <param name="token">Token model.</param>
+ public ValidateLoginResponse(string serverSessionProof, TokenModel token)
+ {
+ ServerSessionProof = serverSessionProof;
+ Token = token;
+ }
+
+ /// <summary>
+ /// Gets or sets the server's session proof.
+ /// </summary>
+ [JsonPropertyName("serverSessionProof")]
+ public string ServerSessionProof { get; set; }
+
+ /// <summary>
+ /// Gets or sets the JWT and refresh token.
+ /// </summary>
+ [JsonPropertyName("token")]
+ public TokenModel Token { get; set; }
+ }
+}
diff --git a/src/AliasVault.WebApp/Auth/Pages/Login.razor b/src/AliasVault.WebApp/Auth/Pages/Login.razor
index e7af1aa1e..b27a992df 100644
--- a/src/AliasVault.WebApp/Auth/Pages/Login.razor
+++ b/src/AliasVault.WebApp/Auth/Pages/Login.razor
@@ -7,7 +7,7 @@
@inject AuthService AuthService
@using System.Text.Json
@using AliasVault.Shared.Models
-@using AliasVault.Shared.Models.WebApi
+@using AliasVault.Shared.Models.WebApi.Auth
@using AliasVault.WebApp.Auth.Components
@using AliasVault.WebApp.Auth.Services
@@ -68,17 +68,10 @@
_loadingIndicator.Show();
_serverValidationErrors.Clear();
- // Create argon2id hash of the password locally
- var hash = await Cryptography.Cryptography.DeriveKeyFromPasswordAsync(_loginModel.Password, "aliasvault");
-
- //var hash = "hello";
- Console.WriteLine("Hash: " + hash);
- //_loadingIndicator.Hide();
- return;
-
try
{
- var result = await Http.PostAsJsonAsync("api/v1/Auth/login", _loginModel);
+ // Send request to server with email to get server ephemeral public key.
+ var result = await Http.PostAsJsonAsync("api/v1/Auth/login", new LoginRequest(_loginModel.Email));
var responseContent = await result.Content.ReadAsStringAsync();
if (!result.IsSuccessStatusCode)
@@ -88,28 +81,60 @@
return;
}
- var tokenObject = JsonSerializer.Deserialize<TokenModel>(responseContent);
-
- if (tokenObject != null)
+ var loginResponse = JsonSerializer.Deserialize<LoginResponse>(responseContent);
+ if (loginResponse == null)
{
- // Store the token as a plain string in local storage
- await AuthService.StoreAccessTokenAsync(tokenObject.Token);
- await AuthService.StoreRefreshTokenAsync(tokenObject.RefreshToken);
- await AuthStateProvider.GetAuthenticationStateAsync();
+ _serverValidationErrors.AddError("An error occurred while processing the login request.");
+ return;
}
- else
+
+ // 3. Client derives shared session key.
+ var clientEphemeral = Cryptography.Srp.GenerateEphemeralClient();
+ var privateKey = await Cryptography.Srp.DerivePrivateKey(loginResponse.Salt, _loginModel.Email, _loginModel.Password);
+ var clientSession = Cryptography.Srp.DeriveSessionClient(
+ privateKey,
+ clientEphemeral.Secret,
+ loginResponse.ServerEphemeral,
+ loginResponse.Salt,
+ _loginModel.Email);
+
+ // 4. Client sends proof of session key to server.
+ result = await Http.PostAsJsonAsync("api/v1/Auth/validate", new ValidateLoginRequest(_loginModel.Email, clientEphemeral.Public, clientSession.Proof));
+ responseContent = await result.Content.ReadAsStringAsync();
+
+ if (!result.IsSuccessStatusCode)
{
- // Handle the case where the token is not present in the response
- Console.WriteLine("Token not found in the response.");
+ _serverValidationErrors.ParseResponse(responseContent);
+ StateHasChanged();
return;
}
- await AuthStateProvider.GetAuthenticationStateAsync();
-
- if (result.IsSuccessStatusCode)
+ var validateLoginResponse = JsonSerializer.Deserialize<ValidateLoginResponse>(responseContent);
+ if (validateLoginResponse == null)
{
- NavigationManager.NavigateTo("/");
+ _serverValidationErrors.AddError("An error occurred while processing the login request.");
+ return;
}
+
+ // 5. Client verifies proof.
+ Cryptography.Srp.VerifySession(clientEphemeral.Public, clientSession, validateLoginResponse.ServerSessionProof);
+
+ // Store the tokens in local storage.
+ await AuthService.StoreAccessTokenAsync(validateLoginResponse.Token.Token);
+ await AuthService.StoreRefreshTokenAsync(validateLoginResponse.Token.RefreshToken);
+
+ // Redirect to home page.
+ await AuthStateProvider.GetAuthenticationStateAsync();
+ NavigationManager.NavigateTo("/");
+ }
+ catch (Exception ex)
+ {
+ // If in debug mode, throw the exception.
+ #if DEBUG
+ _serverValidationErrors.AddError(ex.ToString());
+ #else
+ _serverValidationErrors.AddError("An error occurred while processing the login request. Try again (later).");
+ #endif
}
finally
{
diff --git a/src/AliasVault.WebApp/Components/ServerValidationErrors.razor b/src/AliasVault.WebApp/Components/ServerValidationErrors.razor
index c4061842e..61c12031d 100644
--- a/src/AliasVault.WebApp/Components/ServerValidationErrors.razor
+++ b/src/AliasVault.WebApp/Components/ServerValidationErrors.razor
@@ -29,6 +29,15 @@
StateHasChanged();
}
+ /// <summary>
+ /// Adds a server validation error.
+ /// </summary>
+ public void AddError(string error)
+ {
+ _errors.Add(error);
+ StateHasChanged();
+ }
+
/// <summary>
/// Clears the server validation errors.
/// </summary>
diff --git a/src/Tests/AliasVault.UnitTests/CryptographyTests.cs b/src/Tests/AliasVault.UnitTests/EncryptionTests.cs
similarity index 85%
rename from src/Tests/AliasVault.UnitTests/CryptographyTests.cs
rename to src/Tests/AliasVault.UnitTests/EncryptionTests.cs
index 7abf9eb66..de0d7f238 100644
--- a/src/Tests/AliasVault.UnitTests/CryptographyTests.cs
+++ b/src/Tests/AliasVault.UnitTests/EncryptionTests.cs
@@ -1,5 +1,5 @@
//-----------------------------------------------------------------------
-// <copyright file="CryptographyTests.cs" company="lanedirt">
+// <copyright file="EncryptionTests.cs" company="lanedirt">
// Copyright (c) lanedirt. All rights reserved.
// Licensed under the MIT license. See LICENSE.md file in the project root for full license information.
// </copyright>
@@ -10,9 +10,9 @@ namespace AliasVault.Tests;
using System.Security.Cryptography;
/// <summary>
-/// Tests for the Cryptography class.
+/// Tests for the Encryption class.
/// </summary>
-public class CryptographyTests
+public class EncryptionTests
{
/// <summary>
/// Common setup for all tests.
@@ -34,11 +34,11 @@ public class CryptographyTests
string plaintext = "Hello, World!";
// Derive a key from the password using Argon2id
- byte[] key = Cryptography.Cryptography.DeriveKeyFromPassword(password, salt);
+ byte[] key = Cryptography.Encryption.DeriveKeyFromPassword(password, salt);
Console.WriteLine($"Derived key: {key.Length} bytes (hex: {BitConverter.ToString(key).Replace("-", string.Empty)})");
// Encrypt the plaintext
- string encrypted = Cryptography.Cryptography.Encrypt(plaintext, key);
+ string encrypted = Cryptography.Encryption.Encrypt(plaintext, key);
Console.WriteLine($"Encrypted: {encrypted}");
Assert.That(encrypted, Is.Not.Null);
@@ -46,7 +46,7 @@ public class CryptographyTests
Assert.That(encrypted, Is.Not.EqualTo(plaintext));
// Decrypt the ciphertext
- string decrypted = Cryptography.Cryptography.Decrypt(encrypted, key);
+ string decrypted = Cryptography.Encryption.Decrypt(encrypted, key);
Console.WriteLine($"Decrypted: {decrypted}");
Assert.That(decrypted, Is.EqualTo(plaintext));
}
@@ -63,15 +63,15 @@ public class CryptographyTests
string plaintext = "Hello, World!";
// Derive a key from the password using Argon2id
- byte[] key = Cryptography.Cryptography.DeriveKeyFromPassword(password, salt);
+ byte[] key = Cryptography.Encryption.DeriveKeyFromPassword(password, salt);
// Encrypt the plaintext
- string encrypted = Cryptography.Cryptography.Encrypt(plaintext, key);
+ string encrypted = Cryptography.Encryption.Encrypt(plaintext, key);
// Decrypt the ciphertext using a different key
- byte[] key2 = Cryptography.Cryptography.DeriveKeyFromPassword("your-password2", salt);
+ byte[] key2 = Cryptography.Encryption.DeriveKeyFromPassword("your-password2", salt);
- Assert.Throws<CryptographicException>(() => Cryptography.Cryptography.Decrypt(encrypted, key2));
+ Assert.Throws<CryptographicException>(() => Cryptography.Encryption.Decrypt(encrypted, key2));
}
/// <summary>
diff --git a/src/Utilities/Cryptography/Cryptography.cs b/src/Utilities/Cryptography/Encryption.cs
similarity index 97%
rename from src/Utilities/Cryptography/Cryptography.cs
rename to src/Utilities/Cryptography/Encryption.cs
index ef0defcca..f0c0b3bd0 100644
--- a/src/Utilities/Cryptography/Cryptography.cs
+++ b/src/Utilities/Cryptography/Encryption.cs
@@ -1,5 +1,5 @@
//-----------------------------------------------------------------------
-// <copyright file="Cryptography.cs" company="lanedirt">
+// <copyright file="Encryption.cs" company="lanedirt">
// Copyright (c) lanedirt. All rights reserved.
// Licensed under the MIT license. See LICENSE.md file in the project root for full license information.
// </copyright>
@@ -12,9 +12,9 @@ using System.Text;
using Konscious.Security.Cryptography;
/// <summary>
-/// Cryptography class.
+/// Encryption class.
/// </summary>
-public static class Cryptography
+public static class Encryption
{
/// <summary>
/// Derive a key used for encryption/decryption based on a user password and system salt.
diff --git a/src/Utilities/Cryptography/Srp.cs b/src/Utilities/Cryptography/Srp.cs
index 903462e62..e84667180 100644
--- a/src/Utilities/Cryptography/Srp.cs
+++ b/src/Utilities/Cryptography/Srp.cs
@@ -5,11 +5,10 @@
// </copyright>
//-----------------------------------------------------------------------
-using Cryptography.Models;
-
namespace Cryptography;
using System.Security.Cryptography;
+using Cryptography.Models;
using SecureRemotePassword;
/// <summary>
@@ -27,15 +26,11 @@ public class Srp
public static async Task<SrpSignup> SignupPrepareAsync(string email, string plaintextPassword)
{
// Derive a key from the password using Argon2id
- byte[] passwordHash = await Cryptography.DeriveKeyFromPasswordAsync(plaintextPassword);
-
- // Convert to string
- string passwordHashString = BitConverter.ToString(passwordHash).Replace("-", string.Empty);
// Signup: client generates a salt and verifier.
var client = new SrpClient();
var salt = client.GenerateSalt();
- var privateKey = client.DerivePrivateKey(salt, email, passwordHashString);
+ var privateKey = await DerivePrivateKey(salt, email, plaintextPassword);
var verifier = client.DeriveVerifier(privateKey);
return new SrpSignup(email, salt, privateKey, verifier);
@@ -46,12 +41,14 @@ public class Srp
/// </summary>
/// <param name="salt">Salt.</param>
/// <param name="email">Email.</param>
- /// <param name="passwordHash">PasswordHash.</param>
+ /// <param name="plaintextPassword">Plain text password.</param>
/// <returns>Private key as string.</returns>
- public static string DerivePrivateKey(string salt, string email, string passwordHash)
+ public static async Task<string> DerivePrivateKey(string salt, string email, string plaintextPassword)
{
var client = new SrpClient();
- return client.DerivePrivateKey(salt, email, passwordHash);
+ byte[] passwordHash = await Encryption.DeriveKeyFromPasswordAsync(plaintextPassword);
+ var passwordHashString = BitConverter.ToString(passwordHash).Replace("-", string.Empty);
+ return client.DerivePrivateKey(salt, email, passwordHashString);
}
/// <summary>
@@ -79,17 +76,17 @@ public class Srp
/// Derive a shared session key on the client side.
/// </summary>
/// <param name="privateKey">Password hash.</param>
- /// <param name="clientEphemeralSecret">Client ephemeral secret.</param>
- /// <param name="serverPublicEphemeral">Server public ephemeral.</param>
+ /// <param name="clientSecretEphemeral">Client ephemeral secret.</param>
+ /// <param name="serverEphemeralPublic">Server public ephemeral.</param>
/// <param name="salt">Salt.</param>
/// <param name="email">Email.</param>
/// <returns>session.</returns>
- public static SrpSession DeriveSessionClient(string privateKey, string clientEphemeralSecret, string serverPublicEphemeral, string salt, string email)
+ public static SrpSession DeriveSessionClient(string privateKey, string clientSecretEphemeral, string serverEphemeralPublic, string salt, string email)
{
var client = new SrpClient();
return client.DeriveSession(
- clientEphemeralSecret,
- serverPublicEphemeral,
+ clientSecretEphemeral,
+ serverEphemeralPublic,
salt,
email,
privateKey);