Skip to content

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

Replace KeychainAccess with native iOS API for more control (#771)

Leendert de Borstd758de267419
diff --git a/apps/mobile-app/ios/AliasVault.xcodeproj/project.pbxproj b/apps/mobile-app/ios/AliasVault.xcodeproj/project.pbxproj
index 3072c0675..eec7f4e64 100644
--- a/apps/mobile-app/ios/AliasVault.xcodeproj/project.pbxproj
+++ b/apps/mobile-app/ios/AliasVault.xcodeproj/project.pbxproj
@@ -3,7 +3,7 @@
archiveVersion = 1;
classes = {
};
- objectVersion = 70;
+ objectVersion = 60;
objects = {
/* Begin PBXBuildFile section */
@@ -190,7 +190,7 @@
/* End PBXFileReference section */
/* Begin PBXFileSystemSynchronizedBuildFileExceptionSet section */
- CEE9098F2DA548C7008D568F /* PBXFileSystemSynchronizedBuildFileExceptionSet */ = {
+ CEE9098F2DA548C7008D568F /* Exceptions for "Autofill" folder in "Autofill" target */ = {
isa = PBXFileSystemSynchronizedBuildFileExceptionSet;
membershipExceptions = (
Info.plist,
@@ -200,11 +200,62 @@
/* End PBXFileSystemSynchronizedBuildFileExceptionSet section */
/* Begin PBXFileSystemSynchronizedRootGroup section */
- CEE480882DBE86DC00F4A367 /* VaultStoreKit */ = {isa = PBXFileSystemSynchronizedRootGroup; explicitFileTypes = {}; explicitFolders = (); path = VaultStoreKit; sourceTree = "<group>"; };
- CEE480972DBE86DD00F4A367 /* VaultStoreKitTests */ = {isa = PBXFileSystemSynchronizedRootGroup; explicitFileTypes = {}; explicitFolders = (); path = VaultStoreKitTests; sourceTree = "<group>"; };
- CEE4816B2DBE8AC800F4A367 /* VaultUI */ = {isa = PBXFileSystemSynchronizedRootGroup; explicitFileTypes = {}; explicitFolders = (); path = VaultUI; sourceTree = "<group>"; };
- CEE482AB2DBE8EFE00F4A367 /* VaultModels */ = {isa = PBXFileSystemSynchronizedRootGroup; explicitFileTypes = {}; explicitFolders = (); path = VaultModels; sourceTree = "<group>"; };
- CEE909812DA548C7008D568F /* Autofill */ = {isa = PBXFileSystemSynchronizedRootGroup; exceptions = (CEE9098F2DA548C7008D568F /* PBXFileSystemSynchronizedBuildFileExceptionSet */, ); explicitFileTypes = {}; explicitFolders = (); path = Autofill; sourceTree = "<group>"; };
+ CEE480882DBE86DC00F4A367 /* VaultStoreKit */ = {
+ isa = PBXFileSystemSynchronizedRootGroup;
+ exceptions = (
+ );
+ explicitFileTypes = {
+ };
+ explicitFolders = (
+ );
+ path = VaultStoreKit;
+ sourceTree = "<group>";
+ };
+ CEE480972DBE86DD00F4A367 /* VaultStoreKitTests */ = {
+ isa = PBXFileSystemSynchronizedRootGroup;
+ exceptions = (
+ );
+ explicitFileTypes = {
+ };
+ explicitFolders = (
+ );
+ path = VaultStoreKitTests;
+ sourceTree = "<group>";
+ };
+ CEE4816B2DBE8AC800F4A367 /* VaultUI */ = {
+ isa = PBXFileSystemSynchronizedRootGroup;
+ exceptions = (
+ );
+ explicitFileTypes = {
+ };
+ explicitFolders = (
+ );
+ path = VaultUI;
+ sourceTree = "<group>";
+ };
+ CEE482AB2DBE8EFE00F4A367 /* VaultModels */ = {
+ isa = PBXFileSystemSynchronizedRootGroup;
+ exceptions = (
+ );
+ explicitFileTypes = {
+ };
+ explicitFolders = (
+ );
+ path = VaultModels;
+ sourceTree = "<group>";
+ };
+ CEE909812DA548C7008D568F /* Autofill */ = {
+ isa = PBXFileSystemSynchronizedRootGroup;
+ exceptions = (
+ CEE9098F2DA548C7008D568F /* Exceptions for "Autofill" folder in "Autofill" target */,
+ );
+ explicitFileTypes = {
+ };
+ explicitFolders = (
+ );
+ path = Autofill;
+ sourceTree = "<group>";
+ };
/* End PBXFileSystemSynchronizedRootGroup section */
/* Begin PBXFrameworksBuildPhase section */
@@ -1159,7 +1210,10 @@
LIBRARY_SEARCH_PATHS = "$(SDKROOT)/usr/lib/swift\"$(inherited)\"";
MTL_ENABLE_DEBUG_INFO = YES;
ONLY_ACTIVE_ARCH = YES;
- OTHER_LDFLAGS = "$(inherited) ";
+ OTHER_LDFLAGS = (
+ "$(inherited)",
+ " ",
+ );
REACT_NATIVE_PATH = "${PODS_ROOT}/../../node_modules/react-native";
SDKROOT = iphoneos;
SWIFT_ACTIVE_COMPILATION_CONDITIONS = "$(inherited) DEBUG";
@@ -1214,7 +1268,10 @@
);
LIBRARY_SEARCH_PATHS = "$(SDKROOT)/usr/lib/swift\"$(inherited)\"";
MTL_ENABLE_DEBUG_INFO = NO;
- OTHER_LDFLAGS = "$(inherited) ";
+ OTHER_LDFLAGS = (
+ "$(inherited)",
+ " ",
+ );
REACT_NATIVE_PATH = "${PODS_ROOT}/../../node_modules/react-native";
SDKROOT = iphoneos;
USE_HERMES = true;
diff --git a/apps/mobile-app/ios/Podfile b/apps/mobile-app/ios/Podfile
index ceb6cdaee..639fa04ad 100644
--- a/apps/mobile-app/ios/Podfile
+++ b/apps/mobile-app/ios/Podfile
@@ -15,7 +15,6 @@ prepare_react_native_project!
target 'AliasVault' do
use_expo_modules!
- pod 'KeychainAccess', '~> 4.2.2'
pod 'SQLite.swift', '~> 0.14.0'
pod 'SwiftLint', :configurations => ['Debug']
pod 'Macaw', '~> 0.9.7'
@@ -78,18 +77,15 @@ target 'AliasVault' do
end
target 'Autofill' do
- pod 'KeychainAccess', '~> 4.2.2'
pod 'SQLite.swift', '~> 0.14.0'
pod 'Macaw', '~> 0.9.7'
end
target 'VaultStoreKit' do
- pod 'KeychainAccess', '~> 4.2.2'
pod 'SQLite.swift', '~> 0.14.0'
end
target 'VaultStoreKitTests' do
- pod 'KeychainAccess', '~> 4.2.2'
pod 'SQLite.swift', '~> 0.14.0'
end
diff --git a/apps/mobile-app/ios/Podfile.lock b/apps/mobile-app/ios/Podfile.lock
index 881a1f598..8c0d06b74 100644
--- a/apps/mobile-app/ios/Podfile.lock
+++ b/apps/mobile-app/ios/Podfile.lock
@@ -280,7 +280,6 @@ PODS:
- hermes-engine (0.76.9):
- hermes-engine/Pre-built (= 0.76.9)
- hermes-engine/Pre-built (0.76.9)
- - KeychainAccess (4.2.2)
- Macaw (0.9.10):
- SWXMLHash
- OpenSSL-Universal (3.3.3001)
@@ -2210,7 +2209,6 @@ DEPENDENCIES:
- fmt (from `../node_modules/react-native/third-party-podspecs/fmt.podspec`)
- glog (from `../node_modules/react-native/third-party-podspecs/glog.podspec`)
- hermes-engine (from `../node_modules/react-native/sdks/hermes-engine/hermes-engine.podspec`)
- - KeychainAccess (~> 4.2.2)
- Macaw (~> 0.9.7)
- RCT-Folly (from `../node_modules/react-native/third-party-podspecs/RCT-Folly.podspec`)
- RCT-Folly/Fabric (from `../node_modules/react-native/third-party-podspecs/RCT-Folly.podspec`)
@@ -2288,7 +2286,6 @@ DEPENDENCIES:
SPEC REPOS:
trunk:
- CatCrypto
- - KeychainAccess
- Macaw
- OpenSSL-Universal
- SocketRocket
@@ -2536,7 +2533,6 @@ SPEC CHECKSUMS:
fmt: 01b82d4ca6470831d1cc0852a1af644be019e8f6
glog: 08b301085f15bcbb6ff8632a8ebaf239aae04e6a
hermes-engine: 9e868dc7be781364296d6ee2f56d0c1a9ef0bb11
- KeychainAccess: c0c4f7f38f6fc7bbe58f5702e25f7bd2f65abf51
Macaw: 7af8ea57aa2cab35b4a52a45e6f85eea753ea9ae
OpenSSL-Universal: 6082b0bf950e5636fe0d78def171184e2b3899c2
RCT-Folly: ea9d9256ba7f9322ef911169a9f696e5857b9e17
@@ -2612,6 +2608,6 @@ SPEC CHECKSUMS:
SWXMLHash: dd733a457e9c4fe93b1538654057aefae4acb382
Yoga: feb4910aba9742cfedc059e2b2902e22ffe9954a
-PODFILE CHECKSUM: 2997b8dece543dad4f5eb5ad3a05ab9dcb62d4c1
+PODFILE CHECKSUM: e97319487cdfeb6c1e66b945d3e9c4bb0468c12d
COCOAPODS: 1.16.2
diff --git a/apps/mobile-app/ios/VaultStoreKit/VaultStore+Auth.swift b/apps/mobile-app/ios/VaultStoreKit/VaultStore+Auth.swift
index 450718108..d6776a67d 100644
--- a/apps/mobile-app/ios/VaultStoreKit/VaultStore+Auth.swift
+++ b/apps/mobile-app/ios/VaultStoreKit/VaultStore+Auth.swift
@@ -1,6 +1,6 @@
import Foundation
import LocalAuthentication
-import KeychainAccess
+import Security
import VaultModels
/// Extension for the VaultStore class to handle authentication methods
@@ -14,16 +14,26 @@ extension VaultStore {
if !self.enabledAuthMethods.contains(.faceID) {
print("Face ID is now disabled, removing key from keychain immediately")
do {
- try self.keychain
- .authenticationPrompt("Authenticate to remove your vault decryption key")
- .remove(VaultConstants.encryptionKeyKey)
+ try removeKeyFromKeychain()
print("Successfully removed encryption key from keychain")
} catch {
print("Failed to remove encryption key from keychain: \(error)")
throw error
}
} else {
- print("Face ID is now enabled, next time user logs in the key will be persisted in keychain")
+ print("Face ID is now enabled, persisting encryption key in memory to keychain")
+ do {
+ guard let keyData = self.encryptionKey else {
+ print("Encryption key is empty, skipping keychain persistence")
+ return
+ }
+
+ try storeKeyInKeychain(keyData)
+ print("Successfully stored encryption key in keychain")
+ } catch {
+ print("Failed to store encryption key in keychain: \(error)")
+ throw error
+ }
}
}
diff --git a/apps/mobile-app/ios/VaultStoreKit/VaultStore+Cache.swift b/apps/mobile-app/ios/VaultStoreKit/VaultStore+Cache.swift
index 53ab1c929..dda344b80 100644
--- a/apps/mobile-app/ios/VaultStoreKit/VaultStore+Cache.swift
+++ b/apps/mobile-app/ios/VaultStoreKit/VaultStore+Cache.swift
@@ -1,4 +1,5 @@
import Foundation
+import Security
/// Extension for the VaultStore class to handle cache management
extension VaultStore {
@@ -14,9 +15,7 @@ extension VaultStore {
print("Clearing vault - removing all stored data")
do {
- try self.keychain
- .authenticationPrompt("Authenticate to remove your vault decryption key")
- .remove(VaultConstants.encryptionKeyKey)
+ try removeKeyFromKeychain()
print("Successfully removed encryption key from keychain")
} catch {
print("Failed to remove encryption key from keychain: \(error)")
diff --git a/apps/mobile-app/ios/VaultStoreKit/VaultStore+Crypto.swift b/apps/mobile-app/ios/VaultStoreKit/VaultStore+Crypto.swift
index c2ff7cc9d..0f7b66c5a 100644
--- a/apps/mobile-app/ios/VaultStoreKit/VaultStore+Crypto.swift
+++ b/apps/mobile-app/ios/VaultStoreKit/VaultStore+Crypto.swift
@@ -1,7 +1,7 @@
import Foundation
import CryptoKit
import LocalAuthentication
-import KeychainAccess
+import Security
/// Extension for the VaultStore class to handle encryption/decryption
extension VaultStore {
@@ -21,9 +21,7 @@ extension VaultStore {
if self.enabledAuthMethods.contains(.faceID) {
print("Face ID is enabled, storing key in keychain")
do {
- try self.keychain
- .authenticationPrompt("Authenticate to save your vault decryption key in the iOS keychain")
- .set(keyData, key: VaultConstants.encryptionKeyKey)
+ try storeKeyInKeychain(keyData)
print("Encryption key saved successfully to keychain")
} catch {
print("Failed to save encryption key to keychain: \(error)")
@@ -73,27 +71,92 @@ extension VaultStore {
print("Attempting to get encryption key from keychain as Face ID is enabled as an option")
do {
- guard let keyData = try self.keychain
- .authenticationPrompt("Authenticate to unlock your vault")
- .getData(VaultConstants.encryptionKeyKey) else {
- throw NSError(domain: "VaultStore", code: 2, userInfo: [NSLocalizedDescriptionKey: "No encryption key found"])
- }
+ let keyData = try retrieveKeyFromKeychain(context: context)
self.encryptionKey = keyData
return keyData
- } catch let keychainError as KeychainAccess.Status {
- switch keychainError {
- case .itemNotFound:
- throw NSError(domain: "VaultStore", code: 2, userInfo: [NSLocalizedDescriptionKey: "No encryption key found"])
- case .authFailed:
- throw NSError(domain: "VaultStore", code: 8, userInfo: [NSLocalizedDescriptionKey: "Authentication failed"])
- default:
- throw NSError(domain: "VaultStore", code: 9, userInfo: [NSLocalizedDescriptionKey: "Keychain access error: \(keychainError.localizedDescription)"])
- }
} catch {
- throw NSError(domain: "VaultStore", code: 9, userInfo: [NSLocalizedDescriptionKey: "Unexpected error accessing keychain: \(error.localizedDescription)"])
+ throw NSError(domain: "VaultStore", code: 9, userInfo: [NSLocalizedDescriptionKey: "Failed to retrieve key from keychain: \(error.localizedDescription)"])
}
}
throw NSError(domain: "VaultStore", code: 3, userInfo: [NSLocalizedDescriptionKey: "No encryption key found in memory"])
}
+
+ /// Store the encryption key in the keychain
+ internal func storeKeyInKeychain(_ keyData: Data) throws {
+ guard let accessControl = SecAccessControlCreateWithFlags(
+ nil,
+ kSecAttrAccessibleWhenPasscodeSetThisDeviceOnly,
+ [.userPresence],
+ nil
+ ) else {
+ throw NSError(domain: "VaultStore", code: 11, userInfo: [NSLocalizedDescriptionKey: "Failed to create access control"])
+ }
+
+ let query: [String: Any] = [
+ kSecClass as String: kSecClassGenericPassword,
+ kSecAttrService as String: VaultConstants.keychainService,
+ kSecAttrAccount as String: VaultConstants.encryptionKeyKey,
+ kSecAttrAccessGroup as String: VaultConstants.keychainAccessGroup,
+ kSecValueData as String: keyData,
+ kSecAttrAccessControl as String: accessControl
+ ]
+
+ SecItemDelete(query as CFDictionary)
+
+ let status = SecItemAdd(query as CFDictionary, nil)
+ guard status == errSecSuccess else {
+ throw NSError(domain: "VaultStore", code: 10, userInfo: [NSLocalizedDescriptionKey: "Failed to store key in keychain: \(status)"])
+ }
+ }
+
+ /// Remove the encryption key from the keychain
+ internal func removeKeyFromKeychain() throws {
+ let query: [String: Any] = [
+ kSecClass as String: kSecClassGenericPassword,
+ kSecAttrService as String: VaultConstants.keychainService,
+ kSecAttrAccount as String: VaultConstants.encryptionKeyKey,
+ kSecAttrAccessGroup as String: VaultConstants.keychainAccessGroup
+ ]
+
+ let status = SecItemDelete(query as CFDictionary)
+ guard status == errSecSuccess || status == errSecItemNotFound else {
+ throw NSError(domain: "VaultStore", code: 11, userInfo: [NSLocalizedDescriptionKey: "Failed to remove key from keychain: \(status)"])
+ }
+ }
+
+ // MARK: - Private Keychain Methods
+
+ /// Retrieve the encryption key from the keychain
+ private func retrieveKeyFromKeychain(context: LAContext) throws -> Data {
+ // Ensure interaction is allowed so system can prompt for Face ID or passcode fallback
+ context.interactionNotAllowed = false
+ context.localizedReason = "Authenticate to unlock your vault"
+
+ let query: [String: Any] = [
+ kSecClass as String: kSecClassGenericPassword,
+ kSecAttrService as String: VaultConstants.keychainService,
+ kSecAttrAccount as String: VaultConstants.encryptionKeyKey,
+ kSecAttrAccessGroup as String: VaultConstants.keychainAccessGroup,
+ kSecReturnData as String: true,
+ kSecUseAuthenticationContext as String: context,
+ kSecMatchLimit as String: kSecMatchLimitOne
+ ]
+
+ var result: AnyObject?
+ let status = SecItemCopyMatching(query as CFDictionary, &result)
+
+ guard status == errSecSuccess,
+ let keyData = result as? Data else {
+ if status == errSecUserCanceled {
+ throw NSError(domain: "VaultStore", code: 8, userInfo: [NSLocalizedDescriptionKey: "Authentication canceled by user"])
+ } else if status == errSecAuthFailed {
+ throw NSError(domain: "VaultStore", code: 8, userInfo: [NSLocalizedDescriptionKey: "Authentication failed"])
+ } else {
+ throw NSError(domain: "VaultStore", code: 2, userInfo: [NSLocalizedDescriptionKey: "No encryption key found"])
+ }
+ }
+
+ return keyData
+ }
}
diff --git a/apps/mobile-app/ios/VaultStoreKit/VaultStore+Database.swift b/apps/mobile-app/ios/VaultStoreKit/VaultStore+Database.swift
index f020dd1e9..23680389d 100644
--- a/apps/mobile-app/ios/VaultStoreKit/VaultStore+Database.swift
+++ b/apps/mobile-app/ios/VaultStoreKit/VaultStore+Database.swift
@@ -30,22 +30,30 @@ extension VaultStore {
let encryptedDbData = Data(base64Encoded: encryptedDbBase64)!
- do {
- let decryptedDbBase64 = try decrypt(data: encryptedDbData)
- try setupDatabaseWithDecryptedData(decryptedDbBase64)
- } catch {
- print("First decryption attempt failed: \(error)")
-
- encryptionKey = nil
-
+ // Try to decrypt the database twice to handle cases where the first attempt fails
+ // and this first attempt was using the cached encryption key.
+ if encryptionKey != nil {
+ // There is a cached encryption key, so we try to decrypt the database with it.
+ // If this fails, we remove the cached key and try a second time triggering
+ // re-authentication.
do {
let decryptedDbBase64 = try decrypt(data: encryptedDbData)
try setupDatabaseWithDecryptedData(decryptedDbBase64)
+ return
} catch {
- print("Second decryption attempt failed: \(error)")
- throw NSError(domain: "VaultStore", code: 5, userInfo: [NSLocalizedDescriptionKey: "Failed to decrypt database after retry: \(error.localizedDescription)"])
+ print("First decryption attempt with cached key failed: \(error)")
+ encryptionKey = nil
}
}
+
+ // No cached key or first attempt failed — trigger authentication and try again
+ do {
+ let decryptedDbBase64 = try decrypt(data: encryptedDbData)
+ try setupDatabaseWithDecryptedData(decryptedDbBase64)
+ } catch {
+ print("Second decryption attempt after reauth failed: \(error)")
+ throw NSError(domain: "VaultStore", code: 5, userInfo: [NSLocalizedDescriptionKey: "Could not unlock vault"])
+ }
}
/// Remove the encrypted database from the local filesystem
diff --git a/apps/mobile-app/ios/VaultStoreKit/VaultStore.swift b/apps/mobile-app/ios/VaultStoreKit/VaultStore.swift
index 6ea324cf3..9bfa0b686 100644
--- a/apps/mobile-app/ios/VaultStoreKit/VaultStore.swift
+++ b/apps/mobile-app/ios/VaultStoreKit/VaultStore.swift
@@ -1,9 +1,9 @@
import Foundation
-import KeychainAccess
import SQLite
import LocalAuthentication
import CryptoKit
import CommonCrypto
+import Security
import VaultModels
/// This class is used to store and retrieve the encrypted AliasVault database and encryption key.
@@ -16,10 +16,6 @@ public class VaultStore {
/// require re-authentication every time the vault is accessed.
public static let shared = VaultStore()
- /// The keychain to access the vault's encryption key.
- internal let keychain = Keychain(service: VaultConstants.keychainService, accessGroup: VaultConstants.keychainAccessGroup)
- .accessibility(.whenPasscodeSetThisDeviceOnly, authenticationPolicy: .biometryAny)
-
/// The user defaults using the shared container which is accessible by both the React Native
/// app and the iOS Autofill extension.
internal let userDefaults = UserDefaults(suiteName: VaultConstants.userDefaultsSuite)!