Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.
Add SRP verify session logic to login handshake (#2439)
Leendert de Borst4e61d2b80a5b
diff --git a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/nativevaultmanager/RustCoreDispatcher.kt b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/nativevaultmanager/RustCoreDispatcher.ktindex 0b80f3b54..56612e989 100644--- a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/nativevaultmanager/RustCoreDispatcher.kt+++ b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/nativevaultmanager/RustCoreDispatcher.kt@@ -26,6 +26,7 @@ import uniffi.aliasvault_core.srpDeriveSessionimport uniffi.aliasvault_core.srpDeriveVerifierimport uniffi.aliasvault_core.srpGenerateEphemeralimport uniffi.aliasvault_core.srpGenerateSalt+import uniffi.aliasvault_core.srpVerifySessionimport uniffi.aliasvault_core.vaultCodecCanonicalizeFromSqliteimport uniffi.aliasvault_core.vaultCodecGenerateManifestSaltimport uniffi.aliasvault_core.vaultCodecLogoContentHash@@ -84,6 +85,7 @@ object RustCoreDispatcher {val session = srpDeriveSession(args.string(0), args.string(1), args.string(2), args.string(3), args.string(4))JSONObject().put("proof", session.proof).put("key", session.key).toString()}+ "srpVerifySession" -> srpVerifySession(args.string(0), args.string(1), args.string(2), args.string(3)).toString()"getSyncableTableNames" -> json(getSyncableTableNames())"pruneVaultJson" -> pruneVaultJson(args.string(0))diff --git a/apps/mobile-app/ios/NativeVaultManager/VaultManager.swift b/apps/mobile-app/ios/NativeVaultManager/VaultManager.swiftindex f3716a68a..47ddb40c6 100644--- a/apps/mobile-app/ios/NativeVaultManager/VaultManager.swift+++ b/apps/mobile-app/ios/NativeVaultManager/VaultManager.swift@@ -1604,6 +1604,8 @@ private enum RustCoreDispatcher {case "srpDeriveSession":let session = try RustCoreFramework.srpDeriveSession(clientSecret: try args.string(0), serverPublic: try args.string(1), salt: try args.string(2), identity: try args.string(3), privateKey: try args.string(4))return try json(["proof": session.proof, "key": session.key])+ case "srpVerifySession":+ return try json(try RustCoreFramework.srpVerifySession(clientPublic: try args.string(0), clientProof: try args.string(1), sessionKey: try args.string(2), serverProof: try args.string(3)))case "getSyncableTableNames": return try json(RustCoreFramework.getSyncableTableNames())case "pruneVaultJson": return try RustCoreFramework.pruneVaultJson(inputJson: try args.string(0))diff --git a/apps/mobile-app/platform/NativeRustCore.ts b/apps/mobile-app/platform/NativeRustCore.tsindex 2c495fe36..283ac0b01 100644--- a/apps/mobile-app/platform/NativeRustCore.ts+++ b/apps/mobile-app/platform/NativeRustCore.ts@@ -61,6 +61,7 @@ export const nativeRustCore: IRustCore = {srpDeriveVerifier: (privateKey) => call('srpDeriveVerifier', privateKey),srpGenerateEphemeral: () => call('srpGenerateEphemeral'),srpDeriveSession: (clientSecret, serverPublic, salt, identity, privateKey) => call('srpDeriveSession', clientSecret, serverPublic, salt, identity, privateKey),+ srpVerifySession: (clientPublic, clientProof, sessionKey, serverProof) => call('srpVerifySession', clientPublic, clientProof, sessionKey, serverProof),getSyncableTableNames: () => call('getSyncableTableNames'),diff --git a/core/client/src/auth/SrpAuthService.ts b/core/client/src/auth/SrpAuthService.tsindex 9d200e4ee..c03a561fe 100644--- a/core/client/src/auth/SrpAuthService.ts+++ b/core/client/src/auth/SrpAuthService.ts@@ -35,6 +35,15 @@ export type SrpClientProof = {clientSessionProof: string;};+/**+ * A client proof plus the session key it was derived with. The key stays client-side and is used to check the+ * server's proof, so it is kept apart from the proof that goes into the request body.+ */+export type SrpClientSession = {+ proof: SrpClientProof;+ sessionKey: string;+};+/*** Login credentials prepared from password derivation.*/@@ -187,11 +196,38 @@ export class SrpAuthService {passwordHashString: string,serverEphemeral: string): Promise<SrpClientProof> {+ return (await SrpAuthService.deriveClientSession(salt, srpIdentity, passwordHashString, serverEphemeral)).proof;+ }++ /**+ * Derives the client's SRP proof and the session key, for exchanges where the server's proof is checked.+ *+ * @param salt - The SRP salt from the initiate response+ * @param srpIdentity - The SRP identity from the initiate response+ * @param passwordHashString - The password hash as uppercase hex string+ * @param serverEphemeral - The server's public ephemeral from the initiate response+ * @returns The proof to submit and the session key to verify the server's proof with+ */+ public static async deriveClientSession(salt: string, srpIdentity: string, passwordHashString: string, serverEphemeral: string): Promise<SrpClientSession> {const clientEphemeral = await SrpAuthService.generateEphemeral();const privateKey = await SrpAuthService.derivePrivateKey(salt, srpIdentity, passwordHashString);const session = await SrpAuthService.deriveSession(clientEphemeral.secret, serverEphemeral, salt, srpIdentity, privateKey);- return { clientPublicEphemeral: clientEphemeral.public, clientSessionProof: session.proof };+ return { proof: { clientPublicEphemeral: clientEphemeral.public, clientSessionProof: session.proof }, sessionKey: session.key };+ }++ /**+ * Checks the server's session proof (M2), which confirms the server holds the verifier for this password.+ *+ * @param session - The client session the request was sent with+ * @param serverSessionProof - The server's proof from the validate response+ * @throws {Error} when the proof does not match+ */+ public static async verifyServerProof(session: SrpClientSession, serverSessionProof: string): Promise<void> {+ const valid = await rustCore().srpVerifySession(session.proof.clientPublicEphemeral, session.proof.clientSessionProof, session.sessionKey, serverSessionProof);+ if (!valid) {+ throw new Error('Server session proof verification failed.');+ }}/**@@ -200,20 +236,20 @@ export class SrpAuthService {* @param loginResponse - The login initiate response holding salt, SRP identity and server ephemeral* @param username - The username typed by the user, used as SRP identity fallback on older servers* @param passwordHashString - The password hash as uppercase hex string- * @returns The client public ephemeral and session proof to submit to the validate endpoint+ * @returns The proof to submit to the validate endpoint and the session key to verify the server's proof with*/- public static async deriveLoginProof(+ public static async deriveLoginSession(loginResponse: LoginResponse,username: string,passwordHashString: string- ): Promise<SrpClientProof> {+ ): Promise<SrpClientSession> {/** Use srpIdentity from server response if available, otherwise fall back to normalized username.* @todo Remove fallback after 0.26.0+ has been released.*/const srpIdentity = loginResponse.srpIdentity ?? SrpAuthService.normalizeUsername(username);- return SrpAuthService.deriveClientProof(loginResponse.salt, srpIdentity, passwordHashString, loginResponse.serverEphemeral);+ return SrpAuthService.deriveClientSession(loginResponse.salt, srpIdentity, passwordHashString, loginResponse.serverEphemeral);}/**diff --git a/core/client/src/auth/SrpLoginService.ts b/core/client/src/auth/SrpLoginService.tsindex b0b687821..cba47e8ab 100644--- a/core/client/src/auth/SrpLoginService.ts+++ b/core/client/src/auth/SrpLoginService.ts@@ -1,7 +1,7 @@import { ApiRequestError } from '../api/errors/ApiRequestError';import { throwIfServerPredatesV2Api } from '../sync/LegacyStorageModelMigration';-import { SrpAuthService } from './SrpAuthService';+import { SrpAuthService, type SrpClientSession } from './SrpAuthService';import type { WebApiService } from '../api/WebApiService';import type { AccountKeyHierarchy } from '../crypto/AccountKeys';@@ -76,9 +76,9 @@ export class SrpLoginService {*/public async validateLogin(username: string, passwordHashString: string, rememberMe: boolean, loginResponse: LoginResponse): Promise<ValidateLoginResponse> {const normalizedUsername = SrpAuthService.normalizeUsername(username);- const proof = await SrpAuthService.deriveLoginProof(loginResponse, normalizedUsername, passwordHashString);- const model: ValidateLoginRequest = { username: normalizedUsername, rememberMe, ...proof };- return this.parseAuthResponse<ValidateLoginResponse>(await this.post('Auth/validate', model));+ const session = await SrpAuthService.deriveLoginSession(loginResponse, normalizedUsername, passwordHashString);+ const model: ValidateLoginRequest = { username: normalizedUsername, rememberMe, ...session.proof };+ return this.verifiedLoginResponse(session, await this.post('Auth/validate', model));}/**@@ -92,9 +92,9 @@ export class SrpLoginService {*/public async validateLogin2Fa(username: string, passwordHashString: string, rememberMe: boolean, loginResponse: LoginResponse, code2Fa: number): Promise<ValidateLoginResponse> {const normalizedUsername = SrpAuthService.normalizeUsername(username);- const proof = await SrpAuthService.deriveLoginProof(loginResponse, normalizedUsername, passwordHashString);- const model: ValidateLoginRequest2Fa = { username: normalizedUsername, rememberMe, ...proof, code2Fa };- return this.parseAuthResponse<ValidateLoginResponse>(await this.post('Auth/validate-2fa', model));+ const session = await SrpAuthService.deriveLoginSession(loginResponse, normalizedUsername, passwordHashString);+ const model: ValidateLoginRequest2Fa = { username: normalizedUsername, rememberMe, ...session.proof, code2Fa };+ return this.verifiedLoginResponse(session, await this.post('Auth/validate-2fa', model));}/**@@ -108,9 +108,23 @@ export class SrpLoginService {*/public async validateLoginRecoveryCode(username: string, passwordHashString: string, rememberMe: boolean, loginResponse: LoginResponse, recoveryCode: string): Promise<ValidateLoginResponse> {const normalizedUsername = SrpAuthService.normalizeUsername(username);- const proof = await SrpAuthService.deriveLoginProof(loginResponse, normalizedUsername, passwordHashString);- const model: ValidateLoginRequestRecoveryCode = { username: normalizedUsername, rememberMe, ...proof, recoveryCode };- return this.parseAuthResponse<ValidateLoginResponse>(await this.post('Auth/validate-recovery-code', model));+ const session = await SrpAuthService.deriveLoginSession(loginResponse, normalizedUsername, passwordHashString);+ const model: ValidateLoginRequestRecoveryCode = { username: normalizedUsername, rememberMe, ...session.proof, recoveryCode };+ return this.verifiedLoginResponse(session, await this.post('Auth/validate-recovery-code', model));+ }++ /**+ * Parse a validate response and, once it carries tokens, check the server's session proof.+ * @param session - The client session the request was sent with+ * @param response - The raw response+ * @returns The parsed body+ */+ private async verifiedLoginResponse(session: SrpClientSession, response: Response): Promise<ValidateLoginResponse> {+ const result = await this.parseAuthResponse<ValidateLoginResponse>(response);+ if (!result.requiresTwoFactor) {+ await SrpAuthService.verifyServerProof(session, result.serverSessionProof);+ }+ return result;}/**diff --git a/core/client/src/rust/RustCoreBinding.ts b/core/client/src/rust/RustCoreBinding.tsindex 00b245bd6..3bf01d109 100644--- a/core/client/src/rust/RustCoreBinding.ts+++ b/core/client/src/rust/RustCoreBinding.ts@@ -45,6 +45,7 @@ export interface IRustCore {srpDeriveVerifier(privateKey: string): Promise<string>;srpGenerateEphemeral(): Promise<SrpEphemeral>;srpDeriveSession(clientSecret: string, serverPublic: string, salt: string, identity: string, privateKey: string): Promise<SrpSession>;+ srpVerifySession(clientPublic: string, clientProof: string, sessionKey: string, serverProof: string): Promise<boolean>;getSyncableTableNames(): Promise<string[]>;diff --git a/core/client/src/rust/WasmRustCore.ts b/core/client/src/rust/WasmRustCore.tsindex 36f964a55..1ab1f6848 100644--- a/core/client/src/rust/WasmRustCore.ts+++ b/core/client/src/rust/WasmRustCore.ts@@ -72,6 +72,8 @@ export function createWasmRustCore(loadWasm: WasmLoader): IRustCore {srpGenerateEphemeral: (): Promise<SrpEphemeral> => ready(() => core.srpGenerateEphemeral() as SrpEphemeral),srpDeriveSession: (clientSecret, serverPublic, salt, identity, privateKey): Promise<SrpSession> =>ready(() => core.srpDeriveSession(clientSecret, serverPublic, salt, identity, privateKey) as SrpSession),+ srpVerifySession: (clientPublic, clientProof, sessionKey, serverProof): Promise<boolean> =>+ ready(() => core.srpVerifySession(clientPublic, clientProof, sessionKey, serverProof)),getSyncableTableNames: (): Promise<string[]> => ready(() => core.getSyncableTableNames()),diff --git a/core/rust/src/bindings/uniffi_api.rs b/core/rust/src/bindings/uniffi_api.rsindex d732a5d46..34b4773ec 100644--- a/core/rust/src/bindings/uniffi_api.rs+++ b/core/rust/src/bindings/uniffi_api.rs@@ -239,6 +239,12 @@ pub fn srp_derive_session(client_secret: String, server_public: String, salt: Stcrate::crypto::srp::srp_derive_session(&client_secret, &server_public, &salt, &identity, &private_key)}+/// Whether the server's proof (M2) matches, which confirms it derived the same session key; hex inputs.+#[uniffi::export]+pub fn srp_verify_session(client_public: String, client_proof: String, session_key: String, server_proof: String) -> Result<bool, SrpError> {+ crate::crypto::srp::srp_verify_session(&client_public, &client_proof, &session_key, &server_proof)+}+// ═══════════════════════════════════════════════════════════════════════════════// Crypto// ═══════════════════════════════════════════════════════════════════════════════