///<summary>
        /// Imports a protected key from a file and updates a <see cref="ProtectedKeySettings"/> structure with the imported values.
        ///</summary>
        ///<param name="uiService">The user-interface service for displaying messages and windows to the user.</param>
        ///<param name="keySettings">The key settings structure to update.</param>
        public static void ImportProtectedKey(IUIServiceWpf uiService, ProtectedKeySettings keySettings)
        {
            string protectedKeyFilename           = keySettings.FileName;
            DataProtectionScope protectedKeyScope = keySettings.Scope;

            if (!File.Exists(protectedKeyFilename))
            {
                var dialogResult = uiService.ShowMessageWpf(
                    string.Format(CultureInfo.CurrentCulture, Resources.MessageboxMessageUnableToLocateCryptoKey, keySettings.FileName),
                    Resources.MessageboxTitleUnableToLocateCryptoKey,
                    System.Windows.MessageBoxButton.YesNo);
                if (dialogResult == System.Windows.MessageBoxResult.Yes)
                {
                    Microsoft.Win32.OpenFileDialog locateCryptoKeyDialog = new Microsoft.Win32.OpenFileDialog
                    {
                        Title = Resources.MessageboxTitleUnableToLocateCryptoKey
                    };

                    var locateFileDislogresult = uiService.ShowFileDialog(locateCryptoKeyDialog);
                    if (true == locateFileDislogresult.DialogResult)
                    {
                        protectedKeyFilename = locateCryptoKeyDialog.FileName;
                    }
                }
            }

            using (Stream keyFileContents = File.OpenRead(protectedKeyFilename))
            {
                var key = KeyManager.Read(keyFileContents, protectedKeyScope);
                keySettings.ProtectedKey = key;
                keySettings.Scope        = protectedKeyScope;
                keySettings.FileName     = protectedKeyFilename;
            }
        }
        ///<summary>
        /// Imports a protected key from a file and updates a <see cref="ProtectedKeySettings"/> structure with the imported values.
        ///</summary>
        ///<param name="uiService">The user-interface service for displaying messages and windows to the user.</param>
        ///<param name="keySettings">The key settings structure to update.</param>
        public static void ImportProtectedKey(IUIServiceWpf uiService, ProtectedKeySettings keySettings)
        {
            string protectedKeyFilename = keySettings.FileName;
            DataProtectionScope protectedKeyScope = keySettings.Scope;

            if (!File.Exists(protectedKeyFilename))
            {
                var dialogResult = uiService.ShowMessageWpf(
                                        string.Format(CultureInfo.CurrentCulture, Resources.MessageboxMessageUnableToLocateCryptoKey, keySettings.FileName),
                                        Resources.MessageboxTitleUnableToLocateCryptoKey,
                                        System.Windows.MessageBoxButton.YesNo);
                if (dialogResult == System.Windows.MessageBoxResult.Yes)
                {

                    Microsoft.Win32.OpenFileDialog locateCryptoKeyDialog = new Microsoft.Win32.OpenFileDialog
                    {
                        Title = Resources.MessageboxTitleUnableToLocateCryptoKey
                    };

                    var locateFileDislogresult = uiService.ShowFileDialog(locateCryptoKeyDialog);
                    if (true == locateFileDislogresult.DialogResult)
                    {
                        protectedKeyFilename = locateCryptoKeyDialog.FileName;
                    }
                }
            }

            using (Stream keyFileContents = File.OpenRead(protectedKeyFilename))
            {
                var key = KeyManager.Read(keyFileContents, protectedKeyScope);
                keySettings.ProtectedKey = key;
                keySettings.Scope = protectedKeyScope;
                keySettings.FileName = protectedKeyFilename;
            }
        }
        protected override bool AfterSelectType(Type selectedType)
        {
            CryptographicKeyWizard keyManager = new CryptographicKeyWizard(new SymmetricAlgorithmKeyCreator(selectedType));
            DialogResult keyResult = keyManager.ShowDialog();

            keySettings = keyManager.KeySettings;
            return keyResult == DialogResult.OK;
        }
        private static void SaveProtectedKey(ProtectedKeySettings protectedKeySettings)
        {
            if (protectedKeySettings.ProtectedKey == null)
            {
                return;
            }

            using (Stream keyOutput = File.OpenWrite(protectedKeySettings.FileName))
            {
                KeyManager.Write(keyOutput, protectedKeySettings.ProtectedKey.EncryptedKey, protectedKeySettings.Scope);
            }
        }
Exemple #5
0
        private static void SaveProtectedKey(ICryptographicKeyConfigurationNode nodeWithKey, IServiceProvider serviceProvider)
        {
            ProtectedKeySettings keySettings = nodeWithKey.KeySettings;

            if (keySettings.ProtectedKey == null)
            {
                return;
            }

            using (Stream keyOutput = File.OpenWrite(keySettings.Filename))
            {
                KeyManager.Write(keyOutput, keySettings.ProtectedKey.EncryptedKey, keySettings.Scope);
            }
        }
Exemple #6
0
        /// <summary>
        /// Edits the specified object using the editor style provided by the GetEditStyle method.
        /// </summary>
        /// <param name="context">
        /// An ITypeDescriptorContext that can be used to gain additional context information.
        /// </param>
        /// <param name="provider">
        /// A service provider object through which editing services may be obtained.
        /// </param>
        /// <param name="value">
        /// An instance of the value being edited.
        /// </param>
        /// <returns>
        /// The new value of the object. If the value of the object hasn't changed, this should return the same object it was passed.
        /// </returns>
        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));

                    ProtectedKeySettings keySettings = value as ProtectedKeySettings;
                    if (keySettings == null)
                    {
                        throw new ArgumentException(Resources.KeyManagerEditorRequiresKeyInfoProperty);
                    }

                    ICryptographicKeyConfigurationNode cryptographicKeyContainer = context.Instance as ICryptographicKeyConfigurationNode;

                    try
                    {
                        ImportProtectedKey(cryptographicKeyContainer);
                    }
                    catch (IOException ioe)
                    {
                        MessageBox.Show(String.Format(Resources.OpenExistingKeyFileFailureErrorMessage, keySettings.Filename, ioe.Message), Resources.OpenExistingKeyFileFailureTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return(value);
                    }

                    CryptographicKeyWizard dialog = new CryptographicKeyWizard(CryptographicKeyWizardStep.CreateNewKey, cryptographicKeyContainer.KeyCreator);
                    dialog.KeySettings = keySettings;

                    DialogResult dialogResult = service.ShowDialog(dialog);

                    if (dialogResult == DialogResult.Cancel)
                    {
                        return(keySettings);
                    }
                    else
                    {
                        return(dialog.KeySettings);
                    }
                }
            }
            return(value);
        }
        protected override bool AfterSelectType(Type selectedType)
        {
            if (selectedType.IsSubclassOf(typeof(KeyedHashAlgorithm)))
            {
                CreatedElementType = typeof(KeyedHashAlgorithmProviderData);
                CryptographicKeyWizard keyManager = new CryptographicKeyWizard(new KeyedHashAlgorithmKeyCreator(selectedType));
                DialogResult keyResult = keyManager.ShowDialog();

                keySettings = keyManager.KeySettings;
                return keyResult == DialogResult.OK;
            }
            else
            {
                CreatedElementType = typeof(HashAlgorithmProviderData);
                return true;
            }
        }
 /// <summary>
 /// Constructs a new instance 
 /// with the corresponding runtime configuration data.
 /// </summary>
 /// <param name="hashAlgorithmProviderData">The corresponding runtime configuration data.</param>
 public KeyedHashAlgorithmProviderNode(KeyedHashAlgorithmProviderData hashAlgorithmProviderData)
     : base(hashAlgorithmProviderData)
 {
     key = new ProtectedKeySettings(hashAlgorithmProviderData.ProtectedKeyFilename, hashAlgorithmProviderData.ProtectedKeyProtectionScope);
 }
 /// <summary>
 /// Constructs a new instance 
 /// with the corresponding runtime configuration data.
 /// </summary>
 /// <param name="symmetricAlgorithmProviderData">The corresponding runtime configuration data.</param>
 public SymmetricAlgorithmProviderNode(SymmetricAlgorithmProviderData symmetricAlgorithmProviderData)
     : base(symmetricAlgorithmProviderData)
 {
     key = new ProtectedKeySettings(symmetricAlgorithmProviderData.ProtectedKeyFilename, symmetricAlgorithmProviderData.ProtectedKeyProtectionScope);
     algorithmType = symmetricAlgorithmProviderData.AlgorithmType;
 }
        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));

                    ProtectedKeySettings keySettings = value as ProtectedKeySettings;
                    if (keySettings == null)
                    {
                        throw new ArgumentException(KeyManagerResources.KeyManagerEditorRequiresKeyInfoProperty);
                    }

                    var bindableProperty = EditorUtility.GetBindableProperty(context);
                    ICryptographicKeyProperty cryptographicKeyContainer = bindableProperty.Property as ICryptographicKeyProperty;
                    CryptographicKeyWizard    dialog = new CryptographicKeyWizard(CryptographicKeyWizardStep.CreateNewKey, cryptographicKeyContainer.KeyCreator);

                    try
                    {
                        var uiService = (IUIServiceWpf)provider.GetService(typeof(IUIServiceWpf));
                        KeyManagerUtilities.ImportProtectedKey(uiService, keySettings);
                        dialog.KeySettings = keySettings;
                    }
                    catch (FileNotFoundException)
                    {
                        //this is handled by the KeyManager utilites;
                        return(value);
                    }
                    catch (IOException ioe)
                    {
                        MessageBox.Show(String.Format(CultureInfo.CurrentCulture, KeyManagerResources.OpenExistingKeyFileFailureErrorMessage, keySettings.FileName, ioe.Message), KeyManagerResources.OpenExistingKeyFileFailureTitle, MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                        return(value);
                    }
                    catch (Exception e)
                    {
                        IUIServiceWpf uiService = (IUIServiceWpf)provider.GetService(typeof(IUIServiceWpf));
                        string        unableToLoadKeyMessage      = string.Format(CultureInfo.CurrentCulture, KeyManagerResources.LoadExistingKeyFileFailureErrorMessage, keySettings.FileName, e.Message);
                        var           unableToLoadKeyDialogResult = uiService.ShowMessageWpf(unableToLoadKeyMessage, KeyManagerResources.OpenExistingKeyFileFailureTitle, System.Windows.MessageBoxButton.YesNo);
                        if (unableToLoadKeyDialogResult == System.Windows.MessageBoxResult.No)
                        {
                            return(value);
                        }
                    }

                    DialogResult editorDialogResult = service.ShowDialog(dialog);

                    if (editorDialogResult == DialogResult.Cancel)
                    {
                        return(keySettings);
                    }
                    else
                    {
                        return(dialog.KeySettings);
                    }
                }
            }
            return(value);
        }
 /// <summary>
 /// Constructs a new instance
 /// with the corresponding runtime configuration data.
 /// </summary>
 /// <param name="hashAlgorithmProviderData">The corresponding runtime configuration data.</param>
 public KeyedHashAlgorithmProviderNode(KeyedHashAlgorithmProviderData hashAlgorithmProviderData) : base(hashAlgorithmProviderData)
 {
     key = new ProtectedKeySettings(hashAlgorithmProviderData.ProtectedKeyFilename, hashAlgorithmProviderData.ProtectedKeyProtectionScope);
 }
 /// <summary>
 /// Constructs a new instance
 /// with the corresponding runtime configuration data.
 /// </summary>
 /// <param name="symmetricAlgorithmProviderData">The corresponding runtime configuration data.</param>
 public SymmetricAlgorithmProviderNode(SymmetricAlgorithmProviderData symmetricAlgorithmProviderData) : base(symmetricAlgorithmProviderData)
 {
     key           = new ProtectedKeySettings(symmetricAlgorithmProviderData.ProtectedKeyFilename, symmetricAlgorithmProviderData.ProtectedKeyProtectionScope);
     algorithmType = symmetricAlgorithmProviderData.AlgorithmType;
 }