public bool OTPDB_ChangePassword(bool change) { List <string> lMsg = new List <string>(); try { if (!Valid || !OTPDB_Exists || !OTPDB_Opened) { return(false); } if (change && !AppPolicy.Try(AppPolicyId.ChangeMasterKey)) { return(false); } if (change && !AppPolicy.Current.ChangeMasterKeyNoKey && !ReAskKey()) { lMsg.Add("Invalid masterkey entered"); return(false); } if (!change) { Tools.ShowInfo(PluginTranslate.OTP_CreateDB_PWHint); } bool bCancel; KeySources_Clear(); AceKeyAssoc aka_old = KeySources_Load(); OTPDB.MasterKey = OTPDB_RequestPassword(true, out bCancel); KeySources_Clear(); if (bCancel) { lMsg.Add("Password change cancelled by user"); return(false); } else { lMsg.Add("Password change done"); } //Store key sources to reread them in the proper format //SetKeySources won't do anything if KeePass config does not allow key sources to be remembered Program.Config.Defaults.SetKeySources(EmptyIOC, OTPDB.MasterKey); AceKeyAssoc aka_new = Program.Config.Defaults.GetKeySources(EmptyIOC); KeySources_Clear(); FlagChanged(false); FlagChanged(true); if (KeySources_Equal(aka_old, aka_new)) { return(true); } KeySources_Save(aka_new); return(true); } finally { PluginDebug.AddInfo("OTP DB - change password", 0, lMsg.ToArray()); } }
private PwDatabase OTPDB_Load() { List <string> lMsg = new List <string>(); try { OTPDB_Init(true); KeySources_Clear(); byte[] bOTPDB = GetOTPDBData(); // ConvertFromCustomData(DB.CustomData.Get(DBNAME)); if (bOTPDB == null) { return(null); } AceKeyAssoc aka_old = null; for (int i = 0; i < Program.Config.Security.MasterKeyTries; i++) { bool bCancel = false; if (Program.Config.Defaults.RememberKeySources && (i == 0)) { aka_old = KeySources_Load(); } CompositeKey ck = OTPDB_RequestPassword(false, out bCancel); if (bCancel) { lMsg.Add("Masterkey input cancelled by user"); break; } OTPDB.MasterKey = ck; try { OTPDB_Synchronize(OTPDB, m_FFP, bOTPDB, PluginTranslate.OTPDB_Opening); lMsg.Add("Masterkey valid, attempt: " + (i + 1).ToString()); Program.Config.Defaults.SetKeySources(EmptyIOC, OTPDB.MasterKey); AceKeyAssoc aka_new = Program.Config.Defaults.GetKeySources(EmptyIOC); KeySources_Clear(); if (!KeySources_Equal(aka_old, aka_new)) { KeySources_Save(aka_new); } KeySources_Clear(); OTPDAO.InitEntries(DB); UpdateDBHeader(); if (OTPDB_Opened && CheckAndMigrate(DB)) { FlagChanged(false); } return(OTPDB); } catch { lMsg.Add("Masterkey invalid, attempt: " + (i + 1).ToString()); } } KeySources_Clear(); OTPDB_Init(false); return(OTPDB); } finally { PluginDebug.AddInfo("OTP DB - Load", 0, lMsg.ToArray()); } }
private void KeySources_Save(AceKeyAssoc aka) { try { if (!Program.Config.Defaults.RememberKeySources) { DB.CustomData.Remove(Config.DBKeySources); return; } using (StringWriter writer = new StringWriter()) { XmlSerializer xsKeySources = new XmlSerializer(typeof(AceKeyAssoc)); xsKeySources.Serialize(writer, aka); writer.Flush(); DB.CustomData.Set(Config.DBKeySources, writer.ToString()); FlagChanged(false); } } catch { } }
private AceKeyAssoc KeySources_Load() { if (!Program.Config.Defaults.RememberKeySources) { return(null); } AceKeyAssoc aka = null; if (!DB.CustomData.Exists(Config.DBKeySources)) { return(aka); } string k = DB.CustomData.Get(Config.DBKeySources); try { XmlSerializer xsKeySources = new XmlSerializer(typeof(AceKeyAssoc)); aka = (AceKeyAssoc)xsKeySources.Deserialize(new StringReader(k)); Program.Config.Defaults.KeySources.RemoveAll(x => x.DatabasePath == aka.DatabasePath); Program.Config.Defaults.KeySources.Add(aka); } catch (Exception) { } return(aka); }
private bool KeySources_Equal(AceKeyAssoc aka_old, AceKeyAssoc aka_new) { if ((aka_old == null) || (aka_new == null)) { return(false); } if (aka_old.Password != aka_new.Password) { return(false); } if (aka_old.KeyFilePath != aka_new.KeyFilePath) { return(false); } if (aka_old.KeyProvider != aka_new.KeyProvider) { return(false); } if (aka_old.UserAccount != aka_new.UserAccount) { return(false); } return(true); }
private void OnFormLoad(object sender, EventArgs e) { m_bInitializing = true; GlobalWindowManager.AddWindow(this); // if(m_bRedirectActivation) Program.MainForm.RedirectActivationPush(this); string strBannerTitle = (!string.IsNullOrEmpty(m_strCustomTitle) ? m_strCustomTitle : KPRes.EnterCompositeKey); string strBannerDesc = WinUtil.CompactPath(m_ioInfo.Path, 45); BannerFactory.CreateBannerEx(this, m_bannerImage, Properties.Resources.B48x48_KGPG_Key2, strBannerTitle, strBannerDesc); this.Icon = AppIcons.Default; FontUtil.SetDefaultFont(m_cbPassword); FontUtil.AssignDefaultBold(m_cbPassword); FontUtil.AssignDefaultBold(m_cbKeyFile); FontUtil.AssignDefaultBold(m_cbUserAccount); UIUtil.ConfigureToolTip(m_ttRect); // m_ttRect.SetToolTip(m_cbHidePassword, KPRes.TogglePasswordAsterisks); m_ttRect.SetToolTip(m_btnOpenKeyFile, KPRes.KeyFileSelect); PwInputControlGroup.ConfigureHideButton(m_cbHidePassword, m_ttRect); string strStart = (!string.IsNullOrEmpty(m_strCustomTitle) ? m_strCustomTitle : KPRes.OpenDatabase); string strNameEx = UrlUtil.GetFileName(m_ioInfo.Path); if (!string.IsNullOrEmpty(strNameEx)) { this.Text = strStart + " - " + strNameEx; } else { this.Text = strStart; } // Must be set manually due to possible object override m_tbPassword.TextChanged += this.ProcessTextChangedPassword; // m_cmbKeyFile.OrderedImageList = m_lKeyFileImages; AddKeyFileSuggPriv(KPRes.NoKeyFileSpecifiedMeta, true); // Do not directly compare with Program.CommandLineArgs.FileName, // because this may be a relative path instead of an absolute one string strCmdLineFile = Program.CommandLineArgs.FileName; if ((strCmdLineFile != null) && (Program.MainForm != null)) { strCmdLineFile = Program.MainForm.IocFromCommandLine().Path; } if ((strCmdLineFile != null) && strCmdLineFile.Equals(m_ioInfo.Path, StrUtil.CaseIgnoreCmp)) { string str; str = Program.CommandLineArgs[AppDefs.CommandLineOptions.Password]; if (str != null) { m_cbPassword.Checked = true; m_tbPassword.Text = str; } str = Program.CommandLineArgs[AppDefs.CommandLineOptions.PasswordEncrypted]; if (str != null) { m_cbPassword.Checked = true; m_tbPassword.Text = StrUtil.DecryptString(str); } str = Program.CommandLineArgs[AppDefs.CommandLineOptions.PasswordStdIn]; if (str != null) { ProtectedString ps = KeyUtil.ReadPasswordStdIn(true); if (ps != null) { m_cbPassword.Checked = true; m_tbPassword.TextEx = ps; } } str = Program.CommandLineArgs[AppDefs.CommandLineOptions.KeyFile]; if (str != null) { m_cbKeyFile.Checked = true; AddKeyFileSuggPriv(str, true); } str = Program.CommandLineArgs[AppDefs.CommandLineOptions.PreSelect]; if (str != null) { m_cbKeyFile.Checked = true; AddKeyFileSuggPriv(str, true); } } m_cbHidePassword.Checked = true; OnCheckedHidePassword(sender, e); Debug.Assert(m_cmbKeyFile.Text.Length != 0); m_btnExit.Enabled = m_bCanExit; m_btnExit.Visible = m_bCanExit; ulong uKpf = Program.Config.UI.KeyPromptFlags; UIUtil.ApplyKeyUIFlags(uKpf, m_cbPassword, m_cbKeyFile, m_cbUserAccount, m_cbHidePassword); if ((uKpf & (ulong)AceKeyUIFlags.DisableKeyFile) != 0) { UIUtil.SetEnabled(m_cmbKeyFile, m_cbKeyFile.Checked); UIUtil.SetEnabled(m_btnOpenKeyFile, m_cbKeyFile.Checked); } if (((uKpf & (ulong)AceKeyUIFlags.CheckPassword) != 0) || ((uKpf & (ulong)AceKeyUIFlags.UncheckPassword) != 0)) { m_bPwStatePreset = true; } if (((uKpf & (ulong)AceKeyUIFlags.CheckUserAccount) != 0) || ((uKpf & (ulong)AceKeyUIFlags.UncheckUserAccount) != 0)) { m_bUaStatePreset = true; } CustomizeForScreenReader(); EnableUserControls(); m_bInitializing = false; // E.g. command line options have higher priority m_bCanModKeyFile = (m_cmbKeyFile.SelectedIndex == 0); m_aKeyAssoc = Program.Config.Defaults.GetKeySources(m_ioInfo); if (m_aKeyAssoc != null) { if (m_aKeyAssoc.Password && !m_bPwStatePreset) { m_cbPassword.Checked = true; } if (m_aKeyAssoc.KeyFilePath.Length > 0) { AddKeyFileSuggPriv(m_aKeyAssoc.KeyFilePath, null); } if (m_aKeyAssoc.UserAccount && !m_bUaStatePreset) { m_cbUserAccount.Checked = true; } } foreach (KeyProvider prov in Program.KeyProviderPool) { AddKeyFileSuggPriv(prov.Name, null); } // Local, but thread will continue to run anyway Thread th = new Thread(new ThreadStart(this.AsyncFormLoad)); th.Start(); // ThreadPool.QueueUserWorkItem(new WaitCallback(this.AsyncFormLoad)); this.BringToFront(); this.Activate(); // UIUtil.SetFocus(m_tbPassword, this); // See OnFormShown }
private void OnFormLoad(object sender, EventArgs e) { ++m_uUIAutoBlocked; // The password text box should not be focused by default // in order to avoid a Caps Lock warning tooltip bug; // https://sourceforge.net/p/keepass/bugs/1807/ Debug.Assert((m_tbPassword.TabIndex >= 2) && !m_tbPassword.Focused); GlobalWindowManager.AddWindow(this); // if(m_bRedirectActivation) Program.MainForm.RedirectActivationPush(this); string strBannerTitle = (!string.IsNullOrEmpty(m_strCustomTitle) ? m_strCustomTitle : KPRes.EnterCompositeKey); string strBannerDesc = m_ioInfo.GetDisplayName(); // Compacted by banner BannerFactory.CreateBannerEx(this, m_bannerImage, Properties.Resources.B48x48_KGPG_Key2, strBannerTitle, strBannerDesc); this.Icon = AppIcons.Default; string strStart = (!string.IsNullOrEmpty(m_strCustomTitle) ? m_strCustomTitle : KPRes.OpenDatabase); string strName = UrlUtil.GetFileName(m_ioInfo.Path); if (!string.IsNullOrEmpty(strName)) { this.Text = strStart + " - " + strName; } else { this.Text = strStart; } FontUtil.SetDefaultFont(m_cbPassword); // FontUtil.AssignDefaultBold(m_cbPassword); // FontUtil.AssignDefaultBold(m_cbKeyFile); // FontUtil.AssignDefaultBold(m_cbUserAccount); UIUtil.ConfigureToolTip(m_ttRect); UIUtil.SetToolTip(m_ttRect, m_btnOpenKeyFile, KPRes.KeyFileSelect, true); UIUtil.AccSetName(m_tbPassword, m_cbPassword); UIUtil.AccSetName(m_cmbKeyFile, m_cbKeyFile); PwInputControlGroup.ConfigureHideButton(m_cbHidePassword, m_ttRect); // Enable protection before possibly setting a text m_cbHidePassword.Checked = true; OnHidePasswordCheckedChanged(null, EventArgs.Empty); // Must be set manually due to possible object override m_tbPassword.TextChanged += this.OnPasswordTextChanged; // m_cmbKeyFile.OrderedImageList = m_lKeyFileImages; AddKeyFileItem(KPRes.NoKeyFileSpecifiedMeta, true); Debug.Assert(!AnyComponentOn()); // Do not directly compare with Program.CommandLineArgs.FileName, // because this may be a relative path instead of an absolute one string strCmdLineFile = Program.CommandLineArgs.FileName; if (!string.IsNullOrEmpty(strCmdLineFile) && (Program.MainForm != null)) { strCmdLineFile = Program.MainForm.IocFromCommandLine().Path; } if (!string.IsNullOrEmpty(strCmdLineFile) && strCmdLineFile.Equals( m_ioInfo.Path, StrUtil.CaseIgnoreCmp)) { string str; str = Program.CommandLineArgs[AppDefs.CommandLineOptions.Password]; if (str != null) { m_cbPassword.Checked = true; m_tbPassword.Text = str; } str = Program.CommandLineArgs[AppDefs.CommandLineOptions.PasswordEncrypted]; if (str != null) { m_cbPassword.Checked = true; m_tbPassword.Text = StrUtil.DecryptString(str); } str = Program.CommandLineArgs[AppDefs.CommandLineOptions.PasswordStdIn]; if (str != null) { ProtectedString ps = KeyUtil.ReadPasswordStdIn(true); if (ps != null) { m_cbPassword.Checked = true; m_tbPassword.TextEx = ps; } } str = Program.CommandLineArgs[AppDefs.CommandLineOptions.KeyFile]; if (!string.IsNullOrEmpty(str)) { AddKeyFileItem(str, true); } str = Program.CommandLineArgs[AppDefs.CommandLineOptions.PreSelect]; if (!string.IsNullOrEmpty(str)) { AddKeyFileItem(str, true); } str = Program.CommandLineArgs[AppDefs.CommandLineOptions.UserAccount]; if (str != null) { m_cbUserAccount.Checked = true; } } AceKeyAssoc a = Program.Config.Defaults.GetKeySources(m_ioInfo); if ((a != null) && !AnyComponentOn()) { if (a.Password) { m_cbPassword.Checked = true; } if (!string.IsNullOrEmpty(a.KeyFilePath)) { AddKeyFileItem(a.KeyFilePath, true); } if (!string.IsNullOrEmpty(a.KeyProvider)) { AddKeyFileItem(a.KeyProvider, true); } if (a.UserAccount) { m_cbUserAccount.Checked = true; } } foreach (KeyProvider kp in Program.KeyProviderPool) { AddKeyFileItem(kp.Name, false); } UIUtil.ApplyKeyUIFlags(Program.Config.UI.KeyPromptFlags, m_cbPassword, m_cbKeyFile, m_cbUserAccount, m_cbHidePassword); if (!m_cbPassword.Enabled && !m_cbPassword.Checked) { m_tbPassword.Text = string.Empty; UIUtil.SetEnabledFast(false, m_tbPassword, m_cbHidePassword); } if (!m_cbKeyFile.Enabled && !m_cbKeyFile.Checked) { UIUtil.SetEnabledFast(false, m_cmbKeyFile, m_btnOpenKeyFile); } if (WinUtil.IsWindows9x || NativeLib.IsUnix()) { UIUtil.SetChecked(m_cbUserAccount, false); UIUtil.SetEnabled(m_cbUserAccount, false); } m_btnExit.Enabled = m_bCanExit; m_btnExit.Visible = m_bCanExit; --m_uUIAutoBlocked; UpdateUIState(); // Local, but thread will continue to run anyway Thread th = new Thread(new ThreadStart(this.OnFormLoadAsync)); th.Start(); // ThreadPool.QueueUserWorkItem(new WaitCallback(this.OnFormLoadAsync)); this.BringToFront(); this.Activate(); // UIUtil.SetFocus(m_tbPassword, this); // See OnFormShown }