public void ReadAndWriteToEncryptedConfiguration()
        {
            const string sectionName = "MyEncryptedConfig";

            KeyAlgorithmPair keyAlgorithmPair = new KeyAlgorithmPair();

            keyAlgorithmPair.Key = new SymmetricAlgorithmKeyCreator(typeof(RijndaelManaged).AssemblyQualifiedName).GenerateKey();
            keyAlgorithmPair.AlgorithmTypeName = typeof(RijndaelManaged).AssemblyQualifiedName;

            FileKeyAlgorithmPairStorageProvider provider = new FileKeyAlgorithmPairStorageProvider();

            provider.ConfigurationName = "FileKeyAlgorithmPairStorageProviderData";

            using (ConfigurationBuilder builder = new ConfigurationBuilder())
            {
                // change wher the file is
                RuntimeConfigurationView view = new RuntimeConfigurationView(new ConfigurationContext(new NonDisposingWrapper(builder)));

                FileKeyAlgorithmPairStorageProviderData fileData = (FileKeyAlgorithmPairStorageProviderData)view.GetKeyAlgorithmPairStorageProviderData();
                fileData.Path = Path.GetTempFileName();
                provider.Initialize(view);
                provider.Save(keyAlgorithmPair);
                builder.WriteConfiguration(sectionName, data);
                MockConfigurationData myData = (MockConfigurationData)builder.ReadConfiguration(sectionName);
                Assert.AreEqual(myData.ToString(), data.ToString());
                builder.ClearSectionCache();
                myData = (MockConfigurationData)builder.ReadConfiguration(sectionName);
                Assert.AreEqual(myData.ToString(), data.ToString());
            }
        }
 private void SaveKeyAlgorithmPair(string xmlToUse)
 {
     using (ConfigurationContext context = CreateConfigurationContext(xmlToUse))
     {
         FileKeyAlgorithmPairStorageProvider provider = new FileKeyAlgorithmPairStorageProvider();
         provider.Initialize(new RuntimeConfigurationView(context));
         provider.Save(DefaultKey);
     }
 }
Example #3
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);
        }
        private FileKeyAlgorithmPairStorageProvider GetCurrentProvider()
        {
            ConfigurationContext context = GetContext();
            FileKeyAlgorithmPairStorageProvider     provider = new FileKeyAlgorithmPairStorageProvider();
            FileKeyAlgorithmPairStorageProviderData data     = new FileKeyAlgorithmPairStorageProviderData();

            if (this.dpapiSettings != null)
            {
                data.DpapiSettings = new DpapiSettingsData(this.dpapiSettings.Entropy, this.dpapiSettings.Mode);
            }

            data.Path = this.path;
            data.Name = SR.DefaultFileKeyAlgorithmStorageProviderNodeName;
            ConfigurationSettings settings = context.GetMetaConfiguration();

            settings.KeyAlgorithmPairStorageProviderData = data;
            provider.ConfigurationName = data.Name;
            provider.Initialize(new RuntimeConfigurationView(context));

            return(provider);
        }
Example #5
0
        private void SaveAndLoad(RuntimeConfigurationView configurationView)
        {
            FileKeyAlgorithmPairStorageProvider provider = new FileKeyAlgorithmPairStorageProvider();

            provider.Initialize(configurationView);

            provider.Save(DefaultKey);
            KeyAlgorithmPair key = provider.Load();

            Assert.IsNotNull(key);
            Assert.AreEqual(DefaultKey.AlgorithmTypeName, key.AlgorithmTypeName);
            Assert.IsTrue(CryptographyUtility.CompareBytes(DefaultKey.Key, key.Key));
            FileKeyAlgorithmPairStorageProviderData data = (FileKeyAlgorithmPairStorageProviderData)configurationView.GetKeyAlgorithmPairStorageProviderData();

            // If protected, ensure it's protected properly
            if (null != data.DpapiSettings)
            {
                using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
                {
                    BinaryFormatter formatter    = new BinaryFormatter();
                    bool            deserialized = true;
                    try
                    {
                        KeyAlgorithmPair deserializedKey = (KeyAlgorithmPair)formatter.Deserialize(fs);
                        Assert.IsNotNull(deserializedKey);
                    }
                    catch
                    {
                        deserialized = false;
                    }

                    // Shouldn't deserialize if it's DPAPI protected
                    Assert.IsFalse(deserialized);
                }
            }
        }
        private void SaveAndLoad(RuntimeConfigurationView configurationView)
        {
            FileKeyAlgorithmPairStorageProvider provider = new FileKeyAlgorithmPairStorageProvider();
            provider.Initialize(configurationView);

            provider.Save(DefaultKey);
            KeyAlgorithmPair key = provider.Load();

            Assert.IsNotNull(key);
            Assert.AreEqual(DefaultKey.AlgorithmTypeName, key.AlgorithmTypeName);
            Assert.IsTrue(CryptographyUtility.CompareBytes(DefaultKey.Key, key.Key));
            FileKeyAlgorithmPairStorageProviderData data = (FileKeyAlgorithmPairStorageProviderData)configurationView.GetKeyAlgorithmPairStorageProviderData();
            // If protected, ensure it's protected properly
            if (null != data.DpapiSettings)
            {
                using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
                {
                    BinaryFormatter formatter = new BinaryFormatter();
                    bool deserialized = true;
                    try
                    {
                        KeyAlgorithmPair deserializedKey = (KeyAlgorithmPair)formatter.Deserialize(fs);
                        Assert.IsNotNull(deserializedKey);
                    }
                    catch
                    {
                        deserialized = false;
                    }

                    // Shouldn't deserialize if it's DPAPI protected
                    Assert.IsFalse(deserialized);
                }
            }
        }