Skip to content

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

Add mobile app weaker derivation settings check

Leendert de Borst77a3bd47b1ee
diff --git a/apps/mobile-app/app/login.tsx b/apps/mobile-app/app/login.tsx
index e4424da37..fc13bd0dd 100644
--- a/apps/mobile-app/app/login.tsx
+++ b/apps/mobile-app/app/login.tsx
@@ -1,4 +1,5 @@
import { describeAuthError, formatErrorMessage } from '@aliasvault/client/auth/AuthErrorMessage';
+import { assertNotWeakerDerivation } from '@aliasvault/client/auth/DerivationSettings';
import { KeyDerivationUpgradeService } from '@aliasvault/client/auth/KeyDerivationUpgradeService';
import { DEFAULT_ENCRYPTION, SrpAuthService, type PreparedCredentials } from '@aliasvault/client/auth/SrpAuthService';
import { SrpLoginService } from '@aliasvault/client/auth/SrpLoginService';
@@ -405,6 +406,11 @@ export default function LoginScreen() : React.ReactNode {
try {
const initiateLoginResponse = await srpUtil.initiateLogin(SrpAuthService.normalizeUsername(credentials.username));
+ const storedParams = await NativeVaultManager.getUnlockKeyDerivationParams();
+ if (storedParams) {
+ assertNotWeakerDerivation(initiateLoginResponse.encryptionSettings, (JSON.parse(storedParams) as UnlockKeyDerivationParams).encryptionSettings);
+ }
+
const unlockKeyBase64 = await NativeVaultManager.deriveKeyFromPassword(credentials.password, initiateLoginResponse.salt, initiateLoginResponse.encryptionType, initiateLoginResponse.encryptionSettings);
const passwordCredentials = await SrpAuthService.loginCredentials(unlockKeyBase64, initiateLoginResponse, credentials.username);
diff --git a/core/client/src/auth/DerivationSettings.ts b/core/client/src/auth/DerivationSettings.ts
index 2b46259c6..9ca937ab4 100644
--- a/core/client/src/auth/DerivationSettings.ts
+++ b/core/client/src/auth/DerivationSettings.ts
@@ -19,6 +19,18 @@ export function isWeakerDerivation(served: string, reference: string): boolean {
return next.memory < known.memory || next.iterations < known.iterations;
}
+/**
+ * Throws when `served` is weaker than `reference`, so a server cannot request a cheaper-to-guess login proof than what device used previously (downgrade attack).
+ * @param served - the encryption settings JSON the server sent
+ * @param reference - the encryption settings JSON this device already derived the unlock key with
+ * @throws Error with {@link AppErrorCode.UNLOCK_SETTINGS_WEAKENED} when the served settings are weaker.
+ */
+export function assertNotWeakerDerivation(served: string, reference: string): void {
+ if (isWeakerDerivation(served, reference)) {
+ throw new Error(formatErrorWithCode('The server sent weaker key derivation settings than this device holds', AppErrorCode.UNLOCK_SETTINGS_WEAKENED));
+ }
+}
+
/**
* The memory and iteration cost of an encryption settings JSON, or null when it has no numeric values for them.
*/
@@ -47,8 +59,6 @@ export async function unlockKeyForChallenge(challenge: UnlockKeyDerivationParams
if (challenge.encryptionSettings === stored.encryptionSettings) {
return storedUnlockKeyBase64;
}
- if (isWeakerDerivation(challenge.encryptionSettings, stored.encryptionSettings)) {
- throw new Error(formatErrorWithCode('The server sent weaker key derivation settings than this device holds', AppErrorCode.UNLOCK_SETTINGS_WEAKENED));
- }
+ assertNotWeakerDerivation(challenge.encryptionSettings, stored.encryptionSettings);
return derive(challenge.salt, challenge.encryptionType, challenge.encryptionSettings);
}
diff --git a/core/client/src/auth/SrpAuthService.ts b/core/client/src/auth/SrpAuthService.ts
index 24c2dda98..0f6f93214 100644
--- a/core/client/src/auth/SrpAuthService.ts
+++ b/core/client/src/auth/SrpAuthService.ts
@@ -1,10 +1,9 @@
-import { AppErrorCode, formatErrorWithCode } from '../api/errors/AppErrorCodes';
import { StorageKeys } from '../constants/StorageKeys';
import { getPlatform } from '../platform/ClientPlatform';
import { argon2DeriveKey, createAccountKeyHierarchy, rustCore } from '../rust/RustCore';
import { bytesToBase64 } from '../utilities/Base64';
-import { isWeakerDerivation } from './DerivationSettings';
+import { assertNotWeakerDerivation } from './DerivationSettings';
import type { AccountKeyBlobs, AccountKeyHierarchy, SrpEphemeral, SrpSession } from '../rust/RustCoreTypes';
import type { UnlockKeyDerivationParams } from '@aliasvault/models/metadata';
@@ -268,8 +267,8 @@ export class SrpAuthService {
*/
public static async deriveUnlockKey(password: string, salt: string, encryptionSettings: string): Promise<string> {
const stored = await getPlatform().storage.get<UnlockKeyDerivationParams>(StorageKeys.UNLOCK_KEY_DERIVATION_PARAMS);
- if (stored && isWeakerDerivation(encryptionSettings, stored.encryptionSettings)) {
- throw new Error(formatErrorWithCode('The server sent weaker key derivation settings than this device holds', AppErrorCode.UNLOCK_SETTINGS_WEAKENED));
+ if (stored) {
+ assertNotWeakerDerivation(encryptionSettings, stored.encryptionSettings);
}
return bytesToBase64(await argon2DeriveKey(password, salt, encryptionSettings));
}