Skip to content

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

Tweak PIN unlock failed attempt counter (#2469)

Leendert de Borstb25d01ba20dd
diff --git a/apps/browser-extension/src/utils/PinUnlockService.ts b/apps/browser-extension/src/utils/PinUnlockService.ts
index c0088c750..b28b9dedf 100644
--- a/apps/browser-extension/src/utils/PinUnlockService.ts
+++ b/apps/browser-extension/src/utils/PinUnlockService.ts
@@ -126,14 +126,18 @@ export function isValidPin(pin: string): boolean {
}
/**
- * Get failed attempts count
+ * Get failed attempts count.
*/
export async function getFailedAttempts(): Promise<number> {
try {
- const result = await storage.getItem(StorageKeys.PIN_FAILED_ATTEMPTS) as number | null;
- return result || 0;
+ const result = await storage.getItem(StorageKeys.PIN_FAILED_ATTEMPTS);
+ if (result === null || result === undefined) {
+ return 0;
+ }
+ return Number.isInteger(result) && (result as number) >= 0 ? result as number : MAX_PIN_ATTEMPTS;
} catch {
- return 0;
+ // Failure to retrieve the counter counts as the maximum for safety reasons.
+ return MAX_PIN_ATTEMPTS;
}
}
diff --git a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultPin.kt b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultPin.kt
index 2d6079fda..668b0361e 100644
--- a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultPin.kt
+++ b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultPin.kt
@@ -121,8 +121,9 @@ class VaultPin(
return try {
retrievePinFailedAttemptsFromKeystore()
} catch (e: Exception) {
- Log.e(TAG, "Failed to retrieve failed attempts, returning 0", e)
- 0
+ // Failure to retrieve the counter counts as the maximum for safety reasons.
+ Log.e(TAG, "Failed to retrieve failed attempts, treating PIN as locked", e)
+ MAX_PIN_ATTEMPTS
}
}
@@ -193,6 +194,11 @@ class VaultPin(
@Throws(PinUnlockException::class)
@Suppress("SwallowedException") // We intentionally swallow to avoid exposing crypto implementation details
fun unlockWithPin(pin: String): String {
+ if (getPinFailedAttempts() >= MAX_PIN_ATTEMPTS) {
+ removeAndDisablePin()
+ throw PinUnlockException.Locked
+ }
+
try {
// Retrieve encrypted key and salt from Keystore
val (encryptedKey, salt) = retrievePinDataFromKeystore()
@@ -416,12 +422,15 @@ class VaultPin(
// Get decryption key from Keystore
val secretKey = keyStore.getKey(KEYSTORE_ALIAS_DATA_ENCRYPTION, null) as? SecretKey
- ?: return 0
+ ?: return MAX_PIN_ATTEMPTS
// Decrypt the attempts data
val cipher = Cipher.getInstance("AES/GCM/NoPadding")
cipher.init(Cipher.DECRYPT_MODE, secretKey, GCMParameterSpec(GCM_TAG_LENGTH, iv))
val decryptedData = cipher.doFinal(encryptedData)
+ if (decryptedData.size != 4) {
+ return MAX_PIN_ATTEMPTS
+ }
// Convert ByteArray to Int
return ((decryptedData[0].toInt() and 0xFF) shl 24) or
@@ -429,8 +438,8 @@ class VaultPin(
((decryptedData[2].toInt() and 0xFF) shl 8) or
(decryptedData[3].toInt() and 0xFF)
} catch (e: Exception) {
- Log.e(TAG, "Failed to decode failed attempts", e)
- return 0
+ Log.e(TAG, "Failed to decode failed attempts, treating PIN as locked", e)
+ return MAX_PIN_ATTEMPTS
}
}
diff --git a/apps/mobile-app/ios/VaultStoreKit/VaultStore+Pin.swift b/apps/mobile-app/ios/VaultStoreKit/VaultStore+Pin.swift
index 9d0fb4f14..ab509adfd 100644
--- a/apps/mobile-app/ios/VaultStoreKit/VaultStore+Pin.swift
+++ b/apps/mobile-app/ios/VaultStoreKit/VaultStore+Pin.swift
@@ -32,9 +32,14 @@ extension VaultStore {
return length > 0 ? length : nil
}
- /// Get failed attempts count from secure storage
+ /// Get failed attempts count from secure storage.
public func getPinFailedAttempts() -> Int {
- return (try? retrievePinFailedAttemptsFromKeychain()) ?? 0
+ return try {
+ retrievePinFailedAttemptsFromKeychain()
+ } catch {
+ // Failure to retrieve the counter counts as the maximum for safety reasons.
+ return Self.maxPinAttempts
+ }
}
// MARK: - PIN Setup Methods
@@ -108,6 +113,11 @@ extension VaultStore {
/// Decrypt the key the PIN protects, counting a failure against the PIN attempts.
private func decryptPinProtectedKey(_ pin: String) throws -> Data {
+ if getPinFailedAttempts() >= Self.maxPinAttempts {
+ try? removeAndDisablePin()
+ throw PinUnlockError.locked
+ }
+
do {
// Retrieve encrypted key and salt from keychain
let (encryptedKey, salt) = try retrievePinDataFromKeychain()
@@ -301,12 +311,19 @@ extension VaultStore {
var result: AnyObject?
let status = SecItemCopyMatching(query as CFDictionary, &result)
- guard status == errSecSuccess, let data = result as? Data else {
- // Return 0 if not found
+ if status == errSecItemNotFound {
return 0
}
- return data.withUnsafeBytes { $0.load(as: Int.self) }
+ guard status == errSecSuccess, let data = result as? Data, data.count == MemoryLayout<Int>.size else {
+ throw NSError(domain: "VaultStore", code: 42, userInfo: [NSLocalizedDescriptionKey: "Failed to read PIN failed attempts: \(status)"])
+ }
+
+ let attempts = data.withUnsafeBytes { $0.loadUnaligned(as: Int.self) }
+ guard attempts >= 0 else {
+ throw NSError(domain: "VaultStore", code: 43, userInfo: [NSLocalizedDescriptionKey: "Invalid PIN failed attempts value"])
+ }
+ return attempts
}
/// Remove failed attempts counter from Keychain