public static AuthProviderUIContext With(string message, IntPtr windowHandle)
        {
            var result = new AuthProviderUIContext(message, windowHandle);

            Current = result;
            return(result);
        }
        private void SaveSettings(Settings settings)
        {
            settings.Enabled = isEnabledCheckBox.Checked;

            if (revokeAllCheckBox.Checked)
            {
                RevokeAllKeys();
            }

            if (isEnabledCheckBox.Checked)
            {
                var newInvalidatingTime = TimeSpan.FromMilliseconds(IndexToPeriod(validPeriodComboBox.SelectedIndex));
                if (settings.InvalidatingTime != newInvalidatingTime)
                {
                    settings.InvalidatingTime = newInvalidatingTime;
                }

                settings.RevokeOnCancel = revokeOnCancel.Checked;

                if (settings.WinStorageEnabled != winKeyStorageCheckBox.Checked)
                {
                    if (_keyManager != null)
                    {
                        using (AuthProviderUIContext.With(Settings.KeyCreationConfirmationMessage, this.Handle))
                        {
                            if (SystemInformation.TerminalServerSession) // RDP
                            {
                                MessageBox.Show(AuthProviderUIContext.Current,
                                                "Changing storage location setting on a remote session is not permitted",
                                                Settings.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
                                return;
                            }

                            try
                            {
                                settings.WinStorageEnabled = winKeyStorageCheckBox.Checked;
                                var authCacheType = settings.GetAuthCacheType();
                                _keyManager.ClaimCurrentCacheType(authCacheType);
                            }
                            catch (AuthProviderUserCancelledException)
                            {
                                TryClaimLocalCacheType();
                                MessageBox.Show(AuthProviderUIContext.Current,
                                                "Creating persistent key for Credential Manager has been canceled",
                                                Settings.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
                            }
                            catch (Exception ex)
                            {
                                TryClaimLocalCacheType();
                                ErrorHandler.ShowError(ex);
                            }
                        }
                    }
                }
            }
        }
Exemple #3
0
        private bool ExtractCompositeKey(string dbPath, out CompositeKey compositeKey)
        {
            compositeKey = null;

            if (String.IsNullOrEmpty(dbPath))
            {
                return(false);
            }

            ProtectedKey encryptedData;

            if (!_keyStorage.TryGetValue(dbPath, out encryptedData))
            {
                return(false);
            }

            try
            {
                using (AuthProviderUIContext.With(Settings.DecryptConfirmationMessage, _keePassMainWindowHandle))
                {
                    compositeKey = encryptedData.GetCompositeKey(_keyCipher);
                    return(true);
                }
            }
            catch (AuthProviderInvalidKeyException)
            {
                // The key might be compromised so we revoke all stored passwords
                ClaimCurrentCacheType(AuthCacheType.Local);
                throw;
            }
            catch (AuthProviderUserCancelledException)
            {
                if (Settings.Instance.RevokeOnCancel)
                {
                    _keyStorage.Remove(dbPath);
                }
                throw;
            }
            catch (Exception)
            {
                _keyStorage.Remove(dbPath);
                throw;
            }
        }
        public void Dispose()
        {
#pragma warning disable S2696 // Instance members should not write to "static" fields
            Current = null;
#pragma warning restore S2696 // Instance members should not write to "static" fields
        }