Exemplo n.º 1
0
            private CompositeKey OTPDB_RequestPassword(bool bSetNewPassword, out bool bCancel)
            {
                bCancel = false;
                if (!bSetNewPassword && Program.Config.Security.MasterKeyOnSecureDesktop &&
                    KeePass.Util.WinUtil.IsAtLeastWindows2000 && !NativeLib.IsUnix())
                {
                    CompositeKey ck = new CompositeKey();
                    try { ck = OTPDB_RequestPasswordSecure(bSetNewPassword, out bCancel); }
                    catch (Exception ex) { Tools.ShowError(ex.Message); }
                    return(ck);
                }
                if (!bSetNewPassword)
                {
                    KeyPromptForm kpf = new KeyPromptForm();
                    SetKeyPromptFormTitle(kpf);
                    bCancel = kpf.ShowDialog() != DialogResult.OK;
                    if (bCancel)
                    {
                        return(new CompositeKey());
                    }
                    return(kpf.CompositeKey);
                }
                KeyCreationForm kcf = new KeyCreationForm();

                kcf.InitEx(null, true);
                bCancel = kcf.ShowDialog() != DialogResult.OK;
                if (bCancel)
                {
                    return(OTPDB.MasterKey);
                }
                return(kcf.CompositeKey);
            }
Exemplo n.º 2
0
        private void GlobalWindowManager_WindowAdded(object sender, GwmWindowEventArgs e)
        {
            // get references for password fields on unlocking and creating a database
            // if a yubikey is inserted, the password field is overwriten with a value
            // derived from the password and the yubikey-challenge-response

            if (e.Form is KeyPromptForm)
            {
                _keyPromtForm            = (KeyPromptForm)e.Form;
                _keyPromtForm_okButton   = (Button)_keyPromtForm.Controls.Find("m_btnOK", false)[0];
                _keyPromtForm_usePw      = (CheckBox)_keyPromtForm.Controls.Find("m_cbPassword", false)[0];
                _keyPromtForm_secureEdit = (SecureEdit)typeof(KeyPromptForm)
                                           .GetField("m_secPassword", BindingFlags.NonPublic | BindingFlags.Instance)
                                           .GetValue(_keyPromtForm);

                injectHandler(_keyPromtForm_okButton, keyPromtForm_onConfirmClick);
            }

            else if (e.Form is KeyCreationForm)
            {
                _createKeyForm              = (KeyCreationForm)e.Form;
                _createKeyForm_okButton     = (Button)_createKeyForm.Controls.Find("m_btnCreate", false)[0];
                _createKeyForm_usePw        = (CheckBox)_createKeyForm.Controls.Find("m_cbPassword", false)[0];
                _createKeyForm_pwInputGroup = (PwInputControlGroup)typeof(KeyCreationForm)
                                              .GetField("m_icgPassword", BindingFlags.NonPublic | BindingFlags.Instance)
                                              .GetValue(_createKeyForm);

                _createKeyForm.VisibleChanged += _createKeyForm_VisibleChanged;

                injectHandler(_createKeyForm_okButton, keyCreateForm_onConfirmClick);
            }
        }
Exemplo n.º 3
0
            private CompositeKey OTPDB_RequestPassword(bool bSetNewPassword, out bool bCancel)
            {
                if (!bSetNewPassword)
                {
                    KeyPromptForm kpf   = new KeyPromptForm();
                    string        title = string.Format(PluginTranslate.OTP_OpenDB, string.IsNullOrEmpty(DB.Name) ? UrlUtil.GetFileName(DB.IOConnectionInfo.Path) : DB.Name);
                    kpf.InitEx(OTPDB.IOConnectionInfo, false, false, title);
                    bCancel = kpf.ShowDialog() != DialogResult.OK;
                    if (bCancel)
                    {
                        return(new CompositeKey());
                    }
                    return(kpf.CompositeKey);
                }
                KeyCreationForm kcf = new KeyCreationForm();

                kcf.InitEx(null, true);
                bCancel = kcf.ShowDialog() != DialogResult.OK;
                if (bCancel)
                {
                    return(OTPDB.MasterKey);
                }
                return(kcf.CompositeKey);
            }
Exemplo n.º 4
0
        /// <summary>
        /// Called when [file new].
        /// </summary>
        /// <remarks>Review whenever private KeePass.MainForm.OnFileNew method changes. Last reviewed 20180416</remarks>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        internal void CreateNewDatabase()
        {
            if (!AppPolicy.Try(AppPolicyId.SaveFile))
            {
                return;
            }

            DialogResult dr;
            string       strPath;

            using (SaveFileDialog sfd = UIUtil.CreateSaveFileDialog(KPRes.CreateNewDatabase,
                                                                    KPRes.NewDatabaseFileName, UIUtil.CreateFileTypeFilter(
                                                                        AppDefs.FileExtension.FileExt, KPRes.KdbxFiles, true), 1,
                                                                    AppDefs.FileExtension.FileExt, false))
            {
                GlobalWindowManager.AddDialog(sfd);
                dr = sfd.ShowDialog(_host.MainWindow);
                GlobalWindowManager.RemoveDialog(sfd);
                strPath = sfd.FileName;
            }

            if (dr != DialogResult.OK)
            {
                return;
            }

            KeePassLib.Keys.CompositeKey key       = null;
            bool showUsualKeePassKeyCreationDialog = false;

            using (KeyCreationSimpleForm kcsf = new KeyCreationSimpleForm())
            {
                // Don't show the simple key creation form if the user has set
                // security policies that restrict the allowable composite key sources
                if (KeePass.Program.Config.UI.KeyCreationFlags == 0)
                {
                    kcsf.InitEx(KeePassLib.Serialization.IOConnectionInfo.FromPath(strPath), true);
                    dr = kcsf.ShowDialog(_host.MainWindow);
                    if ((dr == DialogResult.Cancel) || (dr == DialogResult.Abort))
                    {
                        return;
                    }
                    if (dr == DialogResult.No)
                    {
                        showUsualKeePassKeyCreationDialog = true;
                    }
                    else
                    {
                        key = kcsf.CompositeKey;
                    }
                }
                else
                {
                    showUsualKeePassKeyCreationDialog = true;
                }

                if (showUsualKeePassKeyCreationDialog)
                {
                    using (KeyCreationForm kcf = new KeyCreationForm())
                    {
                        kcf.InitEx(KeePassLib.Serialization.IOConnectionInfo.FromPath(strPath), true);
                        dr = kcf.ShowDialog(_host.MainWindow);
                        if ((dr == DialogResult.Cancel) || (dr == DialogResult.Abort))
                        {
                            return;
                        }
                        key = kcf.CompositeKey;
                    }
                }

                PwDocument dsPrevActive = _host.MainWindow.DocumentManager.ActiveDocument;
                PwDatabase pd           = _host.MainWindow.DocumentManager.CreateNewDocument(true).Database;
                pd.New(KeePassLib.Serialization.IOConnectionInfo.FromPath(strPath), key);

                if (!string.IsNullOrEmpty(kcsf.DatabaseName))
                {
                    pd.Name        = kcsf.DatabaseName;
                    pd.NameChanged = DateTime.Now;
                }

                InsertStandardKeePassData(pd);

                var conf = pd.GetKPRPCConfig();
                pd.SetKPRPCConfig(conf);

                // save the new database & update UI appearance
                pd.Save(_host.MainWindow.CreateStatusBarLogger());
            }
            _host.MainWindow.UpdateUI(true, null, true, null, true, null, false);
        }
Exemplo n.º 5
0
        public static bool Export(PwExportInfo pwExportInfo, FileFormatProvider fileFormat,
                                  IOConnectionInfo iocOutput, IStatusLogger slLogger)
        {
            if (pwExportInfo == null)
            {
                throw new ArgumentNullException("pwExportInfo");
            }
            if (pwExportInfo.DataGroup == null)
            {
                throw new ArgumentException();
            }
            if (fileFormat == null)
            {
                throw new ArgumentNullException("fileFormat");
            }

            bool bFileReq = fileFormat.RequiresFile;

            if (bFileReq && (iocOutput == null))
            {
                throw new ArgumentNullException("iocOutput");
            }
            if (bFileReq && (iocOutput.Path.Length == 0))
            {
                throw new ArgumentException();
            }

            PwDatabase pd = pwExportInfo.ContextDatabase;

            Debug.Assert(pd != null);

            if (!AppPolicy.Try(AppPolicyId.Export))
            {
                return(false);
            }
            if (!AppPolicy.Current.ExportNoKey && (pd != null))
            {
                if (!KeyUtil.ReAskKey(pd, true))
                {
                    return(false);
                }
            }

            if (!fileFormat.SupportsExport)
            {
                return(false);
            }
            if (!fileFormat.TryBeginExport())
            {
                return(false);
            }

            CompositeKey ckOrgMasterKey = null;
            DateTime     dtOrgMasterKey = PwDefs.DtDefaultNow;

            PwGroup pgOrgData     = pwExportInfo.DataGroup;
            PwGroup pgOrgRoot     = ((pd != null) ? pd.RootGroup : null);
            bool    bParentGroups = (pwExportInfo.ExportParentGroups && (pd != null) &&
                                     (pgOrgData != pgOrgRoot));

            bool bExistedAlready = true;             // No deletion by default
            bool bResult         = false;

            try
            {
                if (pwExportInfo.ExportMasterKeySpec && fileFormat.RequiresKey &&
                    (pd != null))
                {
                    KeyCreationForm kcf = new KeyCreationForm();
                    kcf.InitEx((iocOutput ?? new IOConnectionInfo()), true);

                    if (UIUtil.ShowDialogNotValue(kcf, DialogResult.OK))
                    {
                        return(false);
                    }

                    ckOrgMasterKey = pd.MasterKey;
                    dtOrgMasterKey = pd.MasterKeyChanged;

                    pd.MasterKey        = kcf.CompositeKey;
                    pd.MasterKeyChanged = DateTime.UtcNow;

                    UIUtil.DestroyForm(kcf);
                }

                if (bParentGroups)
                {
                    PwGroup pgNew = WithParentGroups(pgOrgData, pd);
                    pwExportInfo.DataGroup = pgNew;
                    pd.RootGroup           = pgNew;
                }

                if (bFileReq)
                {
                    bExistedAlready = IOConnection.FileExists(iocOutput);
                }

                Stream s = (bFileReq ? IOConnection.OpenWrite(iocOutput) : null);
                try { bResult = fileFormat.Export(pwExportInfo, s, slLogger); }
                finally { if (s != null)
                          {
                              s.Close();
                          }
                }

                if (bFileReq && bResult)
                {
                    if (pwExportInfo.ExportPostOpen)
                    {
                        NativeLib.StartProcess(iocOutput.Path);
                    }
                    if (pwExportInfo.ExportPostShow)
                    {
                        WinUtil.ShowFileInFileManager(iocOutput.Path, true);
                    }
                }
            }
            catch (Exception ex) { MessageService.ShowWarning(ex); }
            finally
            {
                if (ckOrgMasterKey != null)
                {
                    pd.MasterKey        = ckOrgMasterKey;
                    pd.MasterKeyChanged = dtOrgMasterKey;
                }

                if (bParentGroups)
                {
                    pwExportInfo.DataGroup = pgOrgData;
                    pd.RootGroup           = pgOrgRoot;
                }
            }

            if (bFileReq && !bResult && !bExistedAlready)
            {
                try { IOConnection.DeleteFile(iocOutput); }
                catch (Exception) { }
            }

            return(bResult);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Called when [file new].
        /// </summary>
        /// <remarks>Review whenever private KeePass.MainForm.OnFileNew method changes.</remarks>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        internal void CreateNewDatabase()
        {
            if (!AppPolicy.Try(AppPolicyId.SaveFile))
            {
                return;
            }

            SaveFileDialog sfd = UIUtil.CreateSaveFileDialog(KPRes.CreateNewDatabase,
                                                             KPRes.NewDatabaseFileName, UIUtil.CreateFileTypeFilter(
                                                                 AppDefs.FileExtension.FileExt, KPRes.KdbxFiles, true), 1,
                                                             AppDefs.FileExtension.FileExt, false);

            GlobalWindowManager.AddDialog(sfd);
            DialogResult dr = sfd.ShowDialog(_host.MainWindow);

            GlobalWindowManager.RemoveDialog(sfd);

            string strPath = sfd.FileName;

            if (dr != DialogResult.OK)
            {
                return;
            }

            KeePassLib.Keys.CompositeKey key;
            KeyCreationSimpleForm        kcsf = new KeyCreationSimpleForm();

            kcsf.InitEx(KeePassLib.Serialization.IOConnectionInfo.FromPath(strPath), true);
            dr = kcsf.ShowDialog(_host.MainWindow);
            if ((dr == DialogResult.Cancel) || (dr == DialogResult.Abort))
            {
                return;
            }
            if (dr == DialogResult.No)
            {
                KeyCreationForm kcf = new KeyCreationForm();
                kcf.InitEx(KeePassLib.Serialization.IOConnectionInfo.FromPath(strPath), true);
                dr = kcf.ShowDialog(_host.MainWindow);
                if ((dr == DialogResult.Cancel) || (dr == DialogResult.Abort))
                {
                    return;
                }
                key = kcf.CompositeKey;
            }
            else
            {
                key = kcsf.CompositeKey;
            }

            PwDocument dsPrevActive = _host.MainWindow.DocumentManager.ActiveDocument;
            PwDatabase pd           = _host.MainWindow.DocumentManager.CreateNewDocument(true).Database;

            pd.New(KeePassLib.Serialization.IOConnectionInfo.FromPath(strPath), key);

            if (!string.IsNullOrEmpty(kcsf.DatabaseName))
            {
                pd.Name        = kcsf.DatabaseName;
                pd.NameChanged = DateTime.Now;
            }

            InsertStandardKeePassData(pd);



            InstallKeeFoxSampleEntries(pd);

            _host.MainWindow.UpdateUI(true, null, true, null, true, null, true);
        }