Ejemplo n.º 1
0
        /// <summary>Register for the FormClosing event.</summary>
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            if (ParentForm != null)
            {
                // Save the settings on FormClosing.
                ParentForm.FormClosing += delegate(object sender2, FormClosingEventArgs e2)
                {
                    if (ParentForm.DialogResult == DialogResult.OK)
                    {
                        var config = KeePassWinHelloExt.Host.CustomConfig;

                        config.SetBool(
                            WinHelloProvider.CfgAutoPrompt,
                            autoPromptCheckBox.Checked
                            );
                        config.SetULong(
                            WinHelloProvider.CfgValidPeriod,
                            IndexToPeriod(validPeriodComboBox.SelectedIndex)
                            );
                    }
                };
            }

            if (!WinHello.IsAvailable())
            {
                autoPromptCheckBox.Enabled  = false;
                validPeriodComboBox.Enabled = false;
                winHelloDisabled.Visible    = true;
            }
        }
        /// <summary>
        /// Gets the masterkey before the database is closed.
        /// </summary>
        private void FileClosingPreHandler(object sender, FileClosingEventArgs e)
        {
            if (e == null)
            {
                Debug.Assert(false); return;
            }
            if (e.Cancel)
            {
                return;
            }

            if (e.Database != null && e.Database.MasterKey != null && WinHello.IsAvailable())
            {
                _provider.CacheKeyForDB(e.Database.IOConnectionInfo.Path, e.Database.MasterKey);
            }
        }
        public void CacheKeyForDB(string databasePath, CompositeKey keys)
        {
            Contract.Requires(!string.IsNullOrEmpty(databasePath));
            Contract.Requires(keys != null);
            Contract.Requires(WinHello.IsAvailable());

            var validPeriod = KeePassWinHelloExt.Host.CustomConfig.GetULong(CfgValidPeriod, VALID_DEFAULT);

            lock (_unlockCache)
            {
                _unlockCache[databasePath] = new WinHelloData
                {
                    ValidUntil  = validPeriod == VALID_UNLIMITED ? DateTime.MaxValue : DateTime.Now.AddSeconds(validPeriod),
                    ComposedKey = Encrypt(keys),
                };
            }
        }
        /// <summary>
        /// Used to modify other form when they load.
        /// </summary>
        private void WindowAddedHandler(object sender, GwmWindowEventArgs e)
        {
            var keyPromptForm = e.Form as KeyPromptForm;

            if (keyPromptForm != null)
            {
                KeyPromptForm = keyPromptForm;

                keyPromptForm.Shown += delegate(object sender2, EventArgs e2)
                {
                    // Warning: If one of the private fields get renamed this method will fail!
                    var m_cmbKeyFile = keyPromptForm.Controls.Find("m_cmbKeyFile", false).FirstOrDefault() as ComboBox;
                    if (m_cmbKeyFile != null)
                    {
                        var fieldInfo = keyPromptForm.GetType().GetField("m_ioInfo", BindingFlags.Instance | BindingFlags.NonPublic);
                        if (fieldInfo != null)
                        {
                            var ioInfo = fieldInfo.GetValue(keyPromptForm) as IOConnectionInfo;
                            if (ioInfo != null)
                            {
                                if (_provider.IsCachedKey(ioInfo.Path) && WinHello.IsAvailable())
                                {
                                    var index = m_cmbKeyFile.Items.IndexOf(ShortProductName);
                                    if (index != -1)
                                    {
                                        m_cmbKeyFile.SelectedIndex = index;

                                        var m_cbPassword = keyPromptForm.Controls.Find("m_cbPassword", false).FirstOrDefault() as CheckBox;
                                        if (m_cbPassword != null)
                                        {
                                            UIUtil.SetChecked(m_cbPassword, false);
                                        }

                                        // If AutoPrompt is enabled click the Ok button.
                                        if (Host.CustomConfig.GetBool(WinHelloProvider.CfgAutoPrompt, true))
                                        {
                                            var m_btnOK = keyPromptForm.Controls.Find("m_btnOK", false).FirstOrDefault() as Button;
                                            if (m_btnOK != null)
                                            {
                                                m_btnOK.PerformClick();
                                            }
                                        }
                                        return;
                                    }
                                }
                            }
                        }

                        // If KeePass autoselected WinHello but there isn't a key available just unselect it.
                        if (m_cmbKeyFile.Text == ShortProductName)
                        {
                            var m_cbKeyFile = keyPromptForm.Controls.Find("m_cbKeyFile", false).FirstOrDefault() as CheckBox;
                            if (m_cbKeyFile != null)
                            {
                                UIUtil.SetChecked(m_cbKeyFile, false);
                            }
                        }
                    }
                };
            }

            var optionsForm = e.Form as OptionsForm;

            if (optionsForm != null)
            {
                optionsForm.Shown += delegate(object sender2, EventArgs e2)
                {
                    try
                    {
                        // Add the WinHello options tab.
                        var m_tabMain = optionsForm.Controls.Find("m_tabMain", true).FirstOrDefault() as TabControl;
                        if (m_tabMain != null)
                        {
                            if (m_tabMain.ImageList == null)
                            {
                                m_tabMain.ImageList = new ImageList();
                            }
                            var imageIndex = m_tabMain.ImageList.Images.Add(Properties.Resources.windows_hello16x16, Color.Transparent);

                            var newTab = new TabPage(ShortProductName);
                            newTab.UseVisualStyleBackColor = true;
                            newTab.ImageIndex = imageIndex;

                            var optionsPanel = new OptionsPanel(this);
                            newTab.Controls.Add(optionsPanel);
                            optionsPanel.Dock = DockStyle.Fill;

                            m_tabMain.TabPages.Add(newTab);
                        }
                    }
                    catch (Exception ex)
                    {
                        Debug.Fail(ex.ToString());
                    }
                };
            }
        }