The Labs \ Source Viewer \ SSCLI \ System.Configuration \ ProtectedConfigurationSection

  1. //------------------------------------------------------------------------------
  2. // <copyright file="ProtectedConfigurationSection.cs" company="Microsoft">
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. // </copyright>
  14. //------------------------------------------------------------------------------
  15. namespace System.Configuration
  16. {
  17.     using System.Collections;
  18.     using System.Collections.Specialized;
  19.     using System.Xml;
  20.     using System.Globalization;
  21.     using System.Security.Permissions;
  22.    
  23.     public sealed class ProtectedConfigurationSection : ConfigurationSection
  24.     {
  25.         internal ProtectedConfigurationProvider GetProviderFromName(string providerName)
  26.         {
  27.             ProviderSettings ps = Providers[providerName];
  28.            
  29.             if (ps == null) {
  30.                 throw new Exception(SR.GetString(SR.ProtectedConfigurationProvider_not_found, providerName));
  31.             }
  32.            
  33.             return InstantiateProvider(ps);
  34.         }
  35.        
  36.         internal ProtectedConfigurationProviderCollection GetAllProviders()
  37.         {
  38.             ProtectedConfigurationProviderCollection coll = new ProtectedConfigurationProviderCollection();
  39.             foreach (ProviderSettings ps in Providers) {
  40.                 coll.Add(InstantiateProvider(ps));
  41.             }
  42.             return coll;
  43.         }
  44.        
  45.         [PermissionSet(SecurityAction.Assert, Unrestricted = true)]
  46.         private ProtectedConfigurationProvider CreateAndInitializeProviderWithAssert(Type t, ProviderSettings pn)
  47.         {
  48.             ProtectedConfigurationProvider provider = (ProtectedConfigurationProvider)TypeUtil.CreateInstanceWithReflectionPermission(t);
  49.             NameValueCollection pars = pn.Parameters;
  50.             NameValueCollection cloneParams = new NameValueCollection(pars.Count);
  51.            
  52.             foreach (string key in pars) {
  53.                 cloneParams[key] = pars[key];
  54.             }
  55.            
  56.             provider.Initialize(pn.Name, cloneParams);
  57.             return provider;
  58.         }
  59.        
  60.         private ProtectedConfigurationProvider InstantiateProvider(ProviderSettings pn)
  61.         {
  62.             Type t = TypeUtil.GetTypeWithReflectionPermission(pn.Type, true);
  63.             if (!typeof(ProtectedConfigurationProvider).IsAssignableFrom(t)) {
  64.                 throw new Exception(SR.GetString(SR.WrongType_of_Protected_provider));
  65.             }
  66.            
  67.             if (!TypeUtil.IsTypeAllowedInConfig(t)) {
  68.                 throw new Exception(SR.GetString(SR.Type_from_untrusted_assembly, t.FullName));
  69.             }
  70.            
  71.             return CreateAndInitializeProviderWithAssert(t, pn);
  72.         }
  73.        
  74.         static internal string DecryptSection(string encryptedXml, ProtectedConfigurationProvider provider)
  75.         {
  76.             XmlDocument doc = new XmlDocument();
  77.             doc.LoadXml(encryptedXml);
  78.             XmlNode resultNode = provider.Decrypt(doc.DocumentElement);
  79.             return resultNode.OuterXml;
  80.         }
  81.        
  82.         private const string EncryptedSectionTemplate = "<{0} {1}=\"{2}\"> {3} </{0}>";
  83.        
  84.         static internal string FormatEncryptedSection(string encryptedXml, string sectionName, string providerName)
  85.         {
  86.                 // The section to encrypt
  87.                 // protectionProvider keyword
  88.                 // The provider name
  89.                 // the encrypted xml
  90.             return String.Format(CultureInfo.InvariantCulture, EncryptedSectionTemplate, sectionName, BaseConfigurationRecord.KEYWORD_PROTECTION_PROVIDER, providerName, encryptedXml);
  91.         }
  92.        
  93.         static internal string EncryptSection(string clearXml, ProtectedConfigurationProvider provider)
  94.         {
  95.             XmlDocument xmlDocument = new XmlDocument();
  96.             xmlDocument.PreserveWhitespace = true;
  97.             xmlDocument.LoadXml(clearXml);
  98.             string sectionName = xmlDocument.DocumentElement.Name;
  99.             XmlNode encNode = provider.Encrypt(xmlDocument.DocumentElement);
  100.             return encNode.OuterXml;
  101.         }
  102.        
  103.        
  104.         //////////////////////////////////////////////////////////////////////
  105.         //////////////////////////////////////////////////////////////////////
  106.         //////////////////////////////////////////////////////////////////////
  107.         //////////////////////////////////////////////////////////////////////
  108.        
  109.         private static ConfigurationPropertyCollection _properties;
  110.         private static readonly ConfigurationProperty _propProviders = new ConfigurationProperty("providers", typeof(ProtectedProviderSettings), new ProtectedProviderSettings(), ConfigurationPropertyOptions.None);
  111.        
  112.         private static readonly ConfigurationProperty _propDefaultProvider = new ConfigurationProperty("defaultProvider", typeof(string), "RsaProtectedConfigurationProvider", null, ConfigurationProperty.NonEmptyStringValidator, ConfigurationPropertyOptions.None);
  113.        
  114.         static ProtectedConfigurationSection()
  115.         {
  116.             // Property initialization
  117.             _properties = new ConfigurationPropertyCollection();
  118.             _properties.Add(_propProviders);
  119.             _properties.Add(_propDefaultProvider);
  120.         }
  121.        
  122.         public ProtectedConfigurationSection()
  123.         {
  124.         }
  125.        
  126.         protected internal override ConfigurationPropertyCollection Properties {
  127.             get { return _properties; }
  128.         }
  129.        
  130.         private ProtectedProviderSettings _Providers {
  131.             get { return (ProtectedProviderSettings)base[_propProviders]; }
  132.         }
  133.        
  134.         [ConfigurationProperty("providers")]
  135.         public ProviderSettingsCollection Providers {
  136.             get { return _Providers.Providers; }
  137.         }
  138.        
  139.         [ConfigurationProperty("defaultProvider", DefaultValue = "RsaProtectedConfigurationProvider")]
  140.         public string DefaultProvider {
  141.             get { return (string)base[_propDefaultProvider]; }
  142.             set { base[_propDefaultProvider] = value; }
  143.         }
  144.        
  145.     }
  146. }

Developer Fusion