@@ -230,8 +230,10 @@ open class BrowserProfile: Profile {
230
230
fatalError ( " Could not create directory at root path: \( error) " )
231
231
}
232
232
} ( )
233
+ private var rustKeychainEnabled = false
233
234
fileprivate let name : String
234
- fileprivate let keychain : MZKeychainWrapper
235
+ fileprivate let keychain : RustKeychain
236
+ fileprivate let legacyKeychain : MZKeychainWrapper
235
237
var isShutdown = false
236
238
237
239
internal let files : FileAccessor
@@ -257,14 +259,17 @@ open class BrowserProfile: Profile {
257
259
init ( localName: String ,
258
260
fxaCommandsDelegate: FxACommandsDelegate ? = nil ,
259
261
creditCardAutofillEnabled: Bool = false ,
262
+ rustKeychainEnabled: Bool = false ,
260
263
clear: Bool = false ,
261
264
logger: Logger = DefaultLogger . shared) {
262
265
logger. log ( " Initing profile \( localName) on thread \( Thread . current) . " ,
263
266
level: . debug,
264
267
category: . setup)
265
268
self . name = localName
266
269
self . files = ProfileFileAccessor ( localName: localName)
267
- self . keychain = MZKeychainWrapper . sharedClientAppContainerKeychain
270
+ self . rustKeychainEnabled = rustKeychainEnabled
271
+ self . keychain = KeychainManager . shared
272
+ self . legacyKeychain = KeychainManager . legacyShared
268
273
self . logger = logger
269
274
self . fxaCommandsDelegate = fxaCommandsDelegate
270
275
@@ -301,7 +306,11 @@ open class BrowserProfile: Profile {
301
306
logger. log ( " New profile. Removing old Keychain/Prefs data. " ,
302
307
level: . info,
303
308
category: . setup)
304
- MZKeychainWrapper . wipeKeychain ( )
309
+ if rustKeychainEnabled {
310
+ RustKeychain . wipeKeychain ( )
311
+ } else {
312
+ MZKeychainWrapper . wipeKeychain ( )
313
+ }
305
314
prefs. clearAll ( )
306
315
}
307
316
@@ -311,7 +320,8 @@ open class BrowserProfile: Profile {
311
320
// Initiating the sync manager has to happen prior to the databases being opened,
312
321
// because opening them can trigger events to which the SyncManager listens.
313
322
self . syncManager = RustSyncManager ( profile: self ,
314
- creditCardAutofillEnabled: creditCardAutofillEnabled)
323
+ creditCardAutofillEnabled: creditCardAutofillEnabled,
324
+ rustKeychainEnabled: rustKeychainEnabled)
315
325
316
326
let notificationCenter = NotificationCenter . default
317
327
@@ -676,7 +686,7 @@ open class BrowserProfile: Profile {
676
686
fileURLWithPath: directory,
677
687
isDirectory: true
678
688
) . appendingPathComponent ( " loginsPerField.db " ) . path
679
- return RustLogins ( databasePath: databasePath)
689
+ return RustLogins ( databasePath: databasePath, rustKeychainEnabled : self . rustKeychainEnabled )
680
690
} ( )
681
691
682
692
lazy var remoteSettingsService : RemoteSettingsService ? = {
@@ -755,24 +765,39 @@ open class BrowserProfile: Profile {
755
765
prefs. removeObjectForKey ( PrefsKeys . KeyLastRemoteTabSyncTime)
756
766
757
767
// Save the keys that will be restored
758
- let rustAutofillKey = RustAutofillEncryptionKeys ( )
759
- let creditCardKey = keychain. string ( forKey: rustAutofillKey. ccKeychainKey)
760
768
let rustLoginsKeys = RustLoginEncryptionKeys ( )
761
- let perFieldKey = keychain. string ( forKey: rustLoginsKeys. loginPerFieldKeychainKey)
762
- // Remove all items, removal is not key-by-key specific (due to the risk of failing to delete something),
763
- // simply restore what is needed.
764
- keychain. removeAllKeys ( )
765
-
766
- if let perFieldKey = perFieldKey {
767
- keychain. set (
768
- perFieldKey,
769
- forKey: rustLoginsKeys. loginPerFieldKeychainKey,
770
- withAccessibility: . afterFirstUnlock
771
- )
769
+ let rustAutofillKey = RustAutofillEncryptionKeys ( )
770
+ var loginsKey : String ?
771
+ let creditCardKey = legacyKeychain. string ( forKey: rustAutofillKey. ccKeychainKey)
772
+
773
+ if rustKeychainEnabled {
774
+ ( loginsKey, _) = keychain. getLoginsKeyData ( )
775
+
776
+ // Remove all items, removal is not key-by-key specific (due to the risk of failing to delete something),
777
+ // simply restore what is needed.
778
+ keychain. removeAllKeys ( )
779
+
780
+ if let loginsKey = loginsKey {
781
+ keychain. setLoginsKey ( loginsKey)
782
+ }
783
+ } else {
784
+ loginsKey = legacyKeychain. string ( forKey: rustLoginsKeys. loginPerFieldKeychainKey)
785
+
786
+ // Remove all items, removal is not key-by-key specific (due to the risk of failing to delete something),
787
+ // simply restore what is needed.
788
+ legacyKeychain. removeAllKeys ( )
789
+
790
+ if let loginsKey = loginsKey {
791
+ legacyKeychain. set ( loginsKey,
792
+ forKey: rustLoginsKeys. loginPerFieldKeychainKey,
793
+ withAccessibility: . afterFirstUnlock)
794
+ }
772
795
}
773
796
774
797
if let creditCardKey = creditCardKey {
775
- keychain. set ( creditCardKey, forKey: rustAutofillKey. ccKeychainKey, withAccessibility: . afterFirstUnlock)
798
+ legacyKeychain. set ( creditCardKey,
799
+ forKey: rustAutofillKey. ccKeychainKey,
800
+ withAccessibility: . afterFirstUnlock)
776
801
}
777
802
778
803
// Tell any observers that our account has changed.
0 commit comments