Exemplo n.º 1
0
        /// <summary>
        /// <para>If DPAPI settings change, this saves the key algorithm out with the new settings.</para>
        /// </summary>
        /// <param name="dialogResult">
        /// <para>One of the <see cref="DialogResult"/> values.</para>
        /// </param>
        /// <param name="originalSettings">
        /// <para>The original <see cref="DpapiSettings"/> before editing.</para>
        /// </param>
        /// <param name="newSettingsData">
        /// <para>The new <see cref="DpapiSettingsData"/> from the editor.</para>
        /// </param>
        /// <returns>
        /// <para>If accepted, the new <see cref="DpapiSettings"/>; otherwise the <paramref name="originalSettings"/>.</para>
        /// </returns>
        protected override object HandleResult(DialogResult dialogResult, DpapiSettings originalSettings, DpapiSettingsData newSettingsData)
        {
            bool returnBaseResult = true;

            if (dialogResult == DialogResult.OK)
            {
                DpapiSettings newDpapiSettings = null;
                if (newSettingsData != null)
                {
                    newDpapiSettings = new DpapiSettings(newSettingsData);
                }

                if (newDpapiSettings != originalSettings)
                {
                    returnBaseResult = SaveKeyAlgorithmPairWithNewDapiSettings(newDpapiSettings, originalSettings);
                }
            }

            if (returnBaseResult)
            {
                return(base.HandleResult(dialogResult, originalSettings, newSettingsData));
            }
            else
            {
                return(originalSettings);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// <para>Edits the specified object's value using the editor style indicated by <see cref="GetEditStyle"/>. This should be a <see cref="DpapiSettings"/> object.</para>
        /// </summary>
        /// <param name="context"><para>An <see cref="ITypeDescriptorContext"/> that can be used to gain additional context information.</para></param>
        /// <param name="provider"><para>An <see cref="IServiceProvider"/> that this editor can use to obtain services.</para></param>
        /// <param name="value"><para>The object to edit. This should be a <see cref="DpapiSettings"/> object.</para></param>
        /// <returns><para>The new value of the <see cref="DpapiSettings"/> object.</para></returns>
        /// <seealso cref="UITypeEditor.EditValue(ITypeDescriptorContext, IServiceProvider, object)"/>
        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            Debug.Assert(provider != null, "No service provider; we cannot edit the value");
            if (provider != null)
            {
                IWindowsFormsEditorService edSvc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));

                Debug.Assert(edSvc != null, "No editor service; we cannot edit the value");
                if (edSvc != null)
                {
                    IWindowsFormsEditorService service = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));

                    using (DpapiSettingsEditorUI dialog = new DpapiSettingsEditorUI(required))
                    {
                        if (ConfirmMessage.Length > 0)
                        {
                            dialog.ConfirmMessage = this.ConfirmMessage;
                        }

                        DpapiSettings dpapiSettings = value as DpapiSettings;
                        InitializeDialog(dialog, dpapiSettings);
                        DpapiSettingsData newSettingsData = dialog.DpapiSettingsData;
                        return(HandleResult(service.ShowDialog(dialog), dpapiSettings, newSettingsData));
                    }
                }
            }
            return(value);
        }
        /// <summary>
        /// <para>If DPAPI settings change, this saves the key algorithm out with the new settings.</para>
        /// </summary>
        /// <param name="dialogResult">
        /// <para>One of the <see cref="DialogResult"/> values.</para>
        /// </param>
        /// <param name="originalSettings">
        /// <para>The original <see cref="DpapiSettings"/> before editing.</para>
        /// </param>
        /// <param name="newSettingsData">
        /// <para>The new <see cref="DpapiSettingsData"/> from the editor.</para>
        /// </param>
        /// <returns>
        /// <para>If accepted, the new <see cref="DpapiSettings"/>; otherwise the <paramref name="originalSettings"/>.</para>
        /// </returns>
        protected override object HandleResult(DialogResult dialogResult, DpapiSettings originalSettings, DpapiSettingsData newSettingsData)
        {
            bool returnBaseResult = true;

            if (dialogResult == DialogResult.OK)
            {
                DpapiSettings newDpapiSettings = null;
                if (newSettingsData != null)
                {
                    newDpapiSettings = new DpapiSettings(newSettingsData);
                }

                if (newDpapiSettings != originalSettings)
                {
                    returnBaseResult = SaveKeyAlgorithmPairWithNewDapiSettings(newDpapiSettings, originalSettings);
                }
            }

            if (returnBaseResult)
            {
                return base.HandleResult(dialogResult, originalSettings, newSettingsData);
            }
            else
            {
                return originalSettings;
            }
        }
Exemplo n.º 4
0
        private void InitializeDialog(DpapiSettingsEditorUI dialog, DpapiSettings settings)
        {
            if (settings != null)
            {
                dialog.DpapiSettingsData         = new DpapiSettingsData();
                dialog.DpapiSettingsData.Mode    = settings.Mode;
                dialog.DpapiSettingsData.Entropy = settings.Entropy;
            }

            dialog.DisplaySettings();
        }
Exemplo n.º 5
0
 private DpapiSettingsData GetDpapiSettingsData(DpapiSettings settings)
 {
     if (settings == null)
     {
         return(null);
     }
     else
     {
         return(new DpapiSettingsData(settings.Entropy, settings.Mode));
     }
 }
        protected override void ExecuteCore(ConfigurationNode node)
        {
            DpapiSettingsEditorUI dpapi = new DpapiSettingsEditorUI();
            DialogResult result = dpapi.ShowDialog();

            if (result == DialogResult.OK)
            {
                base.ExecuteCore(node);
                DpapiSymmetricCryptoProviderNode cryptoNode = (DpapiSymmetricCryptoProviderNode)ChildNode;
                DpapiSettings settings = new DpapiSettings(dpapi.DpapiSettingsData);
                cryptoNode.DataProtectionMode = settings;
            }
        }
Exemplo n.º 7
0
        protected virtual object HandleResult(DialogResult dialogResult, DpapiSettings originalSettings, DpapiSettingsData newSettingsData)
        {
            if (dialogResult == DialogResult.OK)
            {
                if (newSettingsData == null)
                {
                    return(null);
                }

                return(new DpapiSettings(newSettingsData));
            }
            else
            {
                return(originalSettings);
            }
        }
Exemplo n.º 8
0
        private bool SaveKeyAlgorithmPairWithNewDapiSettings(DpapiSettings newDpapiSettings, DpapiSettings originalDpapiSettings)
        {
            ConfigurationContext context = GetContext();

            ConfigurationSettings settings = context.GetMetaConfiguration();

            FileKeyAlgorithmPairStorageProvider     loadProvider = new FileKeyAlgorithmPairStorageProvider();
            FileKeyAlgorithmPairStorageProviderData loadData     = new FileKeyAlgorithmPairStorageProviderData(
                SR.DefaultFileKeyAlgorithmStorageProviderNodeName, currentNode.File, GetDpapiSettingsData(originalDpapiSettings));

            settings.KeyAlgorithmPairStorageProviderData = loadData;
            loadProvider.ConfigurationName = loadData.Name;
            loadProvider.Initialize(new RuntimeConfigurationView(context));


            FileKeyAlgorithmPairStorageProvider     saveProvider = new FileKeyAlgorithmPairStorageProvider();
            FileKeyAlgorithmPairStorageProviderData saveData     = new FileKeyAlgorithmPairStorageProviderData(
                SR.DefaultFileKeyAlgorithmStorageProviderNodeName, currentNode.File, GetDpapiSettingsData(newDpapiSettings));

            settings.KeyAlgorithmPairStorageProviderData = saveData;
            saveProvider.ConfigurationName = saveData.Name;
            saveProvider.Initialize(new RuntimeConfigurationView(context));

            try
            {
                KeyAlgorithmPair key = loadProvider.Load();
                saveProvider.Save(key);
            }
            catch (Exception ex)
            {
                MessageBox.Show(
                    SR.FileKeyAlgorithmDpapiSettingsEditorUnableToSaveNewDpapiSettingsErrorMessage(ex.Message),
                    SR.FileKeyAlgorithmDpapiSettingsEditorUnableToSaveNewDpapiSettingsCaption,
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error
                    );
                return(false);
            }

            return(true);
        }
Exemplo n.º 9
0
        /// <summary>
        /// Determines whether or not two DpapiSettings objects have equal properties.
        /// </summary>
        /// <param name="obj">The object to compare.</param>
        /// <returns><c>true</c> if equal, otherwise <c>false</c>.</returns>
        public override bool Equals(object obj)
        {
            if (obj == this)
            {
                return(true);
            }

            DpapiSettings compareObj = obj as DpapiSettings;

            if (compareObj == null)
            {
                return(false);
            }
            if (compareObj.Mode != this.Mode)
            {
                return(false);
            }
            if (!CryptographyUtility.CompareBytes(compareObj.Entropy, this.Entropy))
            {
                return(false);
            }

            return(true);
        }
Exemplo n.º 10
0
        private void InitializeDialog(DpapiSettingsEditorUI dialog, DpapiSettings settings)
        {
            if (settings != null)
            {
                dialog.DpapiSettingsData = new DpapiSettingsData();
                dialog.DpapiSettingsData.Mode = settings.Mode;
                dialog.DpapiSettingsData.Entropy = settings.Entropy;
            }

            dialog.DisplaySettings();
        }
Exemplo n.º 11
0
        protected virtual object HandleResult(DialogResult dialogResult, DpapiSettings originalSettings, DpapiSettingsData newSettingsData)
        {
            if (dialogResult == DialogResult.OK)
            {
                if (newSettingsData == null)
                {
                    return null;
                }

                return new DpapiSettings(newSettingsData);
            }
            else
            {
                return originalSettings;
            }
        }
        private bool SaveKeyAlgorithmPairWithNewDapiSettings(DpapiSettings newDpapiSettings, DpapiSettings originalDpapiSettings)
        {
            ConfigurationContext context = GetContext();

            ConfigurationSettings settings = context.GetMetaConfiguration();

            FileKeyAlgorithmPairStorageProvider loadProvider = new FileKeyAlgorithmPairStorageProvider();
            FileKeyAlgorithmPairStorageProviderData loadData = new FileKeyAlgorithmPairStorageProviderData(
                SR.DefaultFileKeyAlgorithmStorageProviderNodeName, currentNode.File, GetDpapiSettingsData(originalDpapiSettings));
            settings.KeyAlgorithmPairStorageProviderData = loadData;
            loadProvider.ConfigurationName = loadData.Name;
            loadProvider.Initialize(new RuntimeConfigurationView(context));

            FileKeyAlgorithmPairStorageProvider saveProvider = new FileKeyAlgorithmPairStorageProvider();
            FileKeyAlgorithmPairStorageProviderData saveData = new FileKeyAlgorithmPairStorageProviderData(
                SR.DefaultFileKeyAlgorithmStorageProviderNodeName, currentNode.File, GetDpapiSettingsData(newDpapiSettings));
            settings.KeyAlgorithmPairStorageProviderData = saveData;
            saveProvider.ConfigurationName = saveData.Name;
            saveProvider.Initialize(new RuntimeConfigurationView(context));

            try
            {
                KeyAlgorithmPair key = loadProvider.Load();
                saveProvider.Save(key);
            }
            catch (Exception ex)
            {
                MessageBox.Show(
                    SR.FileKeyAlgorithmDpapiSettingsEditorUnableToSaveNewDpapiSettingsErrorMessage(ex.Message),
                    SR.FileKeyAlgorithmDpapiSettingsEditorUnableToSaveNewDpapiSettingsCaption,
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error
                    );
                return false;
            }

            return true;
        }
 private DpapiSettingsData GetDpapiSettingsData(DpapiSettings settings)
 {
     if (settings == null)
     {
         return null;
     }
     else
     {
         return new DpapiSettingsData(settings.Entropy, settings.Mode);
     }
 }
        private void OKButton_Click(object sender, EventArgs e)
        {
            using (new WaitCursor())
            {
                if (dpapiCheckBox.Checked)
                {
                    if (dpapiSettingsControl.DpapiStorageMode == DpapiStorageMode.Machine && dpapiSettingsControl.Entropy.Length != EntropySize)
                    {
                        MessageBox.Show(SR.DpapiEntropyConvertErrorMessage, SR.DpapiEntropyMinSizeCaption,
                                        MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return;
                    }

                    this.dpapiSettings         = new DpapiSettings(null);
                    this.dpapiSettings.Entropy = dpapiSettingsControl.Entropy;
                    this.dpapiSettings.Mode    = dpapiSettingsControl.DpapiStorageMode;
                }
                else
                {
                    this.dpapiSettings = null;
                }

                if (openMode)
                {
                    // Make sure we can load the key
                    try
                    {
                        LoadKeyAlgorithmPair();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(
                            SR.FileKeyAlgorithmWizardUnableToLoadKeyErrorMessage(ex.Message),
                            SR.FileKeyAlgorithmWizardUnableToLoadKeyCaption,
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Error
                            );
                        return;
                    }
                }
                else
                {
                    try
                    {
                        SaveKeyAlgorithmPair();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(
                            ex.Message,
                            ex.Message,
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Error
                            );
                        return;
                    }
                }

                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }
        private void OKButton_Click(object sender, EventArgs e)
        {
            using (new WaitCursor())
            {
                if (dpapiCheckBox.Checked)
                {
                    if (dpapiSettingsControl.DpapiStorageMode == DpapiStorageMode.Machine && dpapiSettingsControl.Entropy.Length != EntropySize)
                    {
                        MessageBox.Show(SR.DpapiEntropyConvertErrorMessage, SR.DpapiEntropyMinSizeCaption,
                                        MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return;
                    }

                    this.dpapiSettings = new DpapiSettings(null);
                    this.dpapiSettings.Entropy = dpapiSettingsControl.Entropy;
                    this.dpapiSettings.Mode = dpapiSettingsControl.DpapiStorageMode;
                }
                else
                {
                    this.dpapiSettings = null;
                }

                if (openMode)
                {
                    // Make sure we can load the key
                    try
                    {
                        LoadKeyAlgorithmPair();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(
                            SR.FileKeyAlgorithmWizardUnableToLoadKeyErrorMessage(ex.Message),
                            SR.FileKeyAlgorithmWizardUnableToLoadKeyCaption,
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Error
                            );
                        return;
                    }
                }
                else
                {
                    try
                    {
                        SaveKeyAlgorithmPair();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(
                            ex.Message,
                            ex.Message,
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Error
                            );
                        return;
                    }
                }

                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }