private void MoveKeyfileToInternalFolder() { Func <Action> copyAndReturnPostExecute = () => { try { CompositeKey masterKey = App.Kp2a.GetDb().KpDatabase.MasterKey; var sourceIoc = ((KcpKeyFile)masterKey.GetUserKey(typeof(KcpKeyFile))).Ioc; var newIoc = ImportFileToInternalDirectory(sourceIoc); ((KcpKeyFile)masterKey.GetUserKey(typeof(KcpKeyFile))).ResetIoc(newIoc); var keyfileString = IOConnectionInfo.SerializeToString(newIoc); App.Kp2a.StoreOpenedFileAsRecent(App.Kp2a.GetDb().Ioc, keyfileString); return(() => { UpdateImportKeyfilePref(); var builder = new AlertDialog.Builder(Activity); builder .SetMessage(Resource.String.KeyfileMoved); builder.SetPositiveButton(Android.Resource.String.Ok, (sender, args) => { }); builder.Show(); }); } catch (Exception e) { return(() => { Toast.MakeText(Activity, App.Kp2a.GetResourceString(UiStringKey.ErrorOcurred) + " " + e.Message, ToastLength.Long).Show(); }); } }; new SimpleLoadingDialog(Activity, GetString(Resource.String.CopyingFile), false, copyAndReturnPostExecute ).Execute(); }
private static Kdb3ErrorCode SetDatabaseKey(Kdb3Manager mgr, CompositeKey pwKey) { Kdb3ErrorCode e; bool bPassword = pwKey.ContainsType(typeof(KcpPassword)); bool bKeyFile = pwKey.ContainsType(typeof(KcpKeyFile)); string strPassword = (bPassword ? (pwKey.GetUserKey( typeof(KcpPassword)) as KcpPassword).Password.ReadString() : string.Empty); string strKeyFile = (bKeyFile ? (pwKey.GetUserKey( typeof(KcpKeyFile)) as KcpKeyFile).Path : string.Empty); if (bPassword && bKeyFile) { e = mgr.SetMasterKey(strKeyFile, true, strPassword, IntPtr.Zero, false); } else if (bPassword && !bKeyFile) { e = mgr.SetMasterKey(strPassword, false, null, IntPtr.Zero, false); } else if (!bPassword && bKeyFile) { e = mgr.SetMasterKey(strKeyFile, true, null, IntPtr.Zero, false); } else { Debug.Assert(false); throw new Exception(KLRes.InvalidCompositeKey); } return(e); }
public override void Run() { StatusLogger.UpdateMessage(UiStringKey.exporting_database); try { var fileStorage = _app.GetFileStorage(_targetIoc); if (fileStorage is IOfflineSwitchable) { ((IOfflineSwitchable)fileStorage).IsOffline = false; } CompositeKey masterKey = App.Kp2a.CurrentDb.KpDatabase.MasterKey; var sourceIoc = ((KcpKeyFile)masterKey.GetUserKey(typeof(KcpKeyFile))).Ioc; IoUtil.Copy(_targetIoc, sourceIoc, App.Kp2a); if (fileStorage is IOfflineSwitchable) { ((IOfflineSwitchable)fileStorage).IsOffline = App.Kp2a.OfflineMode; } Finish(true); } catch (Exception ex) { Finish(false, ex.Message); } }
private static KdbErrorCode SetDatabaseKey(KdbManager mgr, CompositeKey pwKey) { string strPassword = null; if (pwKey.ContainsType(typeof(KcpPassword))) { KcpPassword p = (pwKey.GetUserKey(typeof(KcpPassword)) as KcpPassword); ProtectedString ps = ((p != null) ? p.Password : null); if (ps == null) { throw new Exception(KPRes.OptionReqOn + @" '" + KPRes.MasterPasswordRmbWhileOpen + @"'."); } strPassword = ps.ReadString(); } string strKeyFile = null; if (pwKey.ContainsType(typeof(KcpKeyFile))) { strKeyFile = (pwKey.GetUserKey(typeof(KcpKeyFile)) as KcpKeyFile).Path; } KdbErrorCode e; if (!string.IsNullOrEmpty(strKeyFile)) { e = mgr.SetMasterKey(strKeyFile, true, strPassword, IntPtr.Zero, false); } else if (strPassword != null) { e = mgr.SetMasterKey(strPassword, false, null, IntPtr.Zero, false); } else if (pwKey.ContainsType(typeof(KcpUserAccount))) { throw new Exception(KPRes.KdbWUA); } else { throw new Exception(KLRes.InvalidCompositeKey); } return(e); }
public void SetKeySources(IOConnectionInfo iocDb, CompositeKey cmpKey) { string strID = GetKeyAssocID(iocDb); int idx = GetKeyAssocIndex(strID); if((cmpKey == null) || !m_bRememberKeySources) { if(idx >= 0) m_vKeySources.RemoveAt(idx); return; } AceKeyAssoc a = new AceKeyAssoc(); a.DatabasePath = strID; IUserKey kcpFile = cmpKey.GetUserKey(typeof(KcpKeyFile)); if(kcpFile != null) { string strKeyFile = ((KcpKeyFile)kcpFile).Path; if(!string.IsNullOrEmpty(strKeyFile) && !StrUtil.IsDataUri(strKeyFile)) { if(!UrlUtil.IsAbsolutePath(strKeyFile)) strKeyFile = UrlUtil.MakeAbsolutePath(WinUtil.GetExecutable(), strKeyFile); a.KeyFilePath = strKeyFile; } } IUserKey kcpCustom = cmpKey.GetUserKey(typeof(KcpCustomKey)); if(kcpCustom != null) a.KeyProvider = ((KcpCustomKey)kcpCustom).Name; IUserKey kcpUser = cmpKey.GetUserKey(typeof(KcpUserAccount)); a.UserAccount = (kcpUser != null); bool bAtLeastOne = ((a.KeyFilePath.Length > 0) || (a.KeyProvider.Length > 0) || a.UserAccount); if(bAtLeastOne) { if(idx >= 0) m_vKeySources[idx] = a; else m_vKeySources.Add(a); } else if(idx >= 0) m_vKeySources.RemoveAt(idx); }
/// <summary> /// This function checks if the actual SyncSource (this) represents exactly the same /// SyncSource that would occur if we will use the given entry and db to create a new one. /// </summary> /// <param name="entry">The PwEntry from our db that should represent a SyncSource.</param> /// <param name="db">The db which contains the given entry.</param> /// <returns>True if (entry + db) will create the same SyncSource object like "this".</returns> public bool IsEqual(PwEntry entry, PwDatabase db) { if (!IsSimilar(entry, db)) { return(false); } IUserKey key1 = new KcpPassword(entry.Strings.ReadSafe(KeeShare.PasswordField)); IUserKey key2 = m_key.GetUserKey(typeof(KcpPassword)); // null is interpreted as equal return(key1 == key2 || key1.KeyData.Equals(key2.KeyData)); }
Database TryLoad(MemoryStream databaseStream) { //create a copy of the stream so we can try again if we get an exception which indicates we should change parameters //This is not optimal in terms of (short-time) memory usage but is hard to avoid because the Keepass library closes streams also in case of errors. //Alternatives would involve increased traffic (if file is on remote) and slower loading times, so this seems to be the best choice. MemoryStream workingCopy = new MemoryStream(); databaseStream.CopyTo(workingCopy); workingCopy.Seek(0, SeekOrigin.Begin); //reset stream if we need to reuse it later: databaseStream.Seek(0, SeekOrigin.Begin); //now let's go: try { Database newDb = _app.LoadDatabase(_ioc, workingCopy, _compositeKey, StatusLogger, _format); Kp2aLog.Log("LoadDB OK"); //make sure the stored access time for the actual file is more recent than that of its backup Thread.Sleep(10); SaveFileData(_ioc, _keyfileOrProvider); Finish(true, _format.SuccessMessage); return(newDb); } catch (OldFormatException) { _format = new KdbDatabaseFormat(_app); return(TryLoad(databaseStream)); } catch (InvalidCompositeKeyException) { KcpPassword passwordKey = (KcpPassword)_compositeKey.GetUserKey(typeof(KcpPassword)); if ((passwordKey != null) && (passwordKey.Password.ReadString() == "") && (_compositeKey.UserKeyCount > 1)) { //if we don't get a password, we don't know whether this means "empty password" or "no password" //retry without password: _compositeKey.RemoveUserKey(passwordKey); //retry: return(TryLoad(databaseStream)); } else { throw; } } }
private void UpdateImportKeyfilePref() { var prefs = PreferenceManager.GetDefaultSharedPreferences(Activity); var rememberKeyfile = prefs.GetBoolean(GetString(Resource.String.keyfile_key), Resources.GetBoolean(Resource.Boolean.keyfile_default)); Preference importKeyfile = FindPreference("import_keyfile_prefs"); Preference exportKeyfile = FindPreference("export_keyfile_prefs"); importKeyfile.Summary = ""; if (!rememberKeyfile) { importKeyfile.Summary = GetString(Resource.String.KeyfileMoveRequiresRememberKeyfile); importKeyfile.Enabled = false; exportKeyfile.Enabled = false; return; } CompositeKey masterKey = App.Kp2a.CurrentDb.KpDatabase.MasterKey; if (masterKey.ContainsType(typeof(KcpKeyFile))) { IOConnectionInfo iocKeyfile = ((KcpKeyFile)masterKey.GetUserKey(typeof(KcpKeyFile))).Ioc; if (iocKeyfile.IsLocalFile() && IoUtil.IsInInternalDirectory(iocKeyfile.Path, Activity)) { importKeyfile.Enabled = false; exportKeyfile.Enabled = true; exportKeyfile.PreferenceClick += (sender, args) => { ExportKeyfileFromInternalFolder(); }; importKeyfile.Summary = GetString(Resource.String.FileIsInInternalDirectory); } else { exportKeyfile.Enabled = false; importKeyfile.Enabled = true; importKeyfile.PreferenceClick += (sender, args) => { MoveKeyfileToInternalFolder(); }; } } else { exportKeyfile.Enabled = false; importKeyfile.Enabled = false; } }