PageRenderTime 4992ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/Blocks/Security.Cryptography/Src/Cryptography/Configuration/CryptographySettings.cs

#
C# | 209 lines | 140 code | 20 blank | 49 comment | 8 complexity | 0dc66a0f4190a5cda668d0bbc6b5d8ef MD5 | raw file
  1. //===============================================================================
  2. // Microsoft patterns & practices Enterprise Library
  3. // Cryptography Application Block
  4. //===============================================================================
  5. // Copyright Š Microsoft Corporation. All rights reserved.
  6. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
  7. // OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
  8. // LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  9. // FITNESS FOR A PARTICULAR PURPOSE.
  10. //===============================================================================
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Configuration;
  14. using System.Linq;
  15. using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
  16. using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ContainerModel;
  17. using Microsoft.Practices.EnterpriseLibrary.Common.Instrumentation.Configuration;
  18. using Microsoft.Practices.EnterpriseLibrary.Security.Cryptography.Instrumentation;
  19. using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Design;
  20. namespace Microsoft.Practices.EnterpriseLibrary.Security.Cryptography.Configuration
  21. {
  22. /// <summary>
  23. /// <para>Configuration settings for cryptography.</para>
  24. /// </summary>
  25. [ViewModel(CryptographyDesignTime.ViewModelTypeNames.CryptographySectionViewModel)]
  26. [ResourceDescription(typeof(DesignResources), "CryptographySettingsDescription")]
  27. [ResourceDisplayName(typeof(DesignResources), "CryptographySettingsDisplayName")]
  28. public class CryptographySettings : SerializableConfigurationSection, ITypeRegistrationsProvider
  29. {
  30. private const string hashProvidersProperty = "hashProviders";
  31. private const string defaultHashProviderNameProperty = "defaultHashInstance";
  32. private const string symmetricCryptoProvidersProperty = "symmetricCryptoProviders";
  33. private const string defaultSymmetricCryptoProviderNameProperty = "defaultSymmetricCryptoInstance";
  34. /// <summary>
  35. /// Gets the configuration section name for the Cryptography Application Block.
  36. /// </summary>
  37. public const string SectionName = "securityCryptographyConfiguration";
  38. /// <summary>
  39. /// <para>Initialize a new instance of the <see cref="CryptographySettings"/> class.</para>
  40. /// </summary>
  41. public CryptographySettings()
  42. {
  43. }
  44. /// <summary>
  45. /// The instance name of the default <see cref="IHashProvider"/> instance.
  46. /// </summary>
  47. [ConfigurationProperty(defaultHashProviderNameProperty, IsRequired= false)]
  48. [Reference(typeof(NameTypeConfigurationElementCollection<HashProviderData, CustomHashProviderData>), typeof(HashProviderData))]
  49. [ResourceDescription(typeof(DesignResources), "CryptographySettingsDefaultHashProviderNameDescription")]
  50. [ResourceDisplayName(typeof(DesignResources), "CryptographySettingsDefaultHashProviderNameDisplayName")]
  51. public string DefaultHashProviderName
  52. {
  53. get { return (string)this[defaultHashProviderNameProperty]; }
  54. set { this[defaultHashProviderNameProperty] = value; }
  55. }
  56. /// <summary>
  57. /// <para>Gets the <see cref="HashProviders"/>.</para>
  58. /// </summary>
  59. /// <value>
  60. /// <para>The hash providers available in configuration. The default is an empty collection.</para>
  61. /// </value>
  62. /// <remarks>
  63. /// <para>This property maps to the <c>hashProviders</c> element in configuration.</para>
  64. /// </remarks>
  65. [ConfigurationProperty(hashProvidersProperty, IsRequired= false)]
  66. [ConfigurationCollection(typeof(HashProviderData))]
  67. [ResourceDescription(typeof(DesignResources), "CryptographySettingsHashProvidersDescription")]
  68. [ResourceDisplayName(typeof(DesignResources), "CryptographySettingsHashProvidersDisplayName")]
  69. public NameTypeConfigurationElementCollection<HashProviderData, CustomHashProviderData> HashProviders
  70. {
  71. get { return (NameTypeConfigurationElementCollection<HashProviderData, CustomHashProviderData>)base[hashProvidersProperty]; }
  72. }
  73. /// <summary>
  74. /// The instance name of the default <see cref="ISymmetricCryptoProvider"/> instance.
  75. /// </summary>
  76. [ConfigurationProperty(defaultSymmetricCryptoProviderNameProperty, IsRequired= false)]
  77. [Reference(typeof(NameTypeConfigurationElementCollection<SymmetricProviderData, CustomSymmetricCryptoProviderData>), typeof(SymmetricProviderData))]
  78. [ResourceDescription(typeof(DesignResources), "CryptographySettingsDefaultSymmetricCryptoProviderNameDescription")]
  79. [ResourceDisplayName(typeof(DesignResources), "CryptographySettingsDefaultSymmetricCryptoProviderNameDisplayName")]
  80. public string DefaultSymmetricCryptoProviderName
  81. {
  82. get { return (string)this[defaultSymmetricCryptoProviderNameProperty]; }
  83. set { this[defaultSymmetricCryptoProviderNameProperty] = value; }
  84. }
  85. /// <summary>
  86. /// TODOC
  87. /// </summary>
  88. [ConfigurationProperty(symmetricCryptoProvidersProperty, IsRequired = false)]
  89. [ConfigurationCollection(typeof(SymmetricProviderData))]
  90. [ResourceDescription(typeof(DesignResources), "CryptographySettingsSymmetricCryptoProvidersDescription")]
  91. [ResourceDisplayName(typeof(DesignResources), "CryptographySettingsSymmetricCryptoProvidersDisplayName")]
  92. public NameTypeConfigurationElementCollection<SymmetricProviderData, CustomSymmetricCryptoProviderData> SymmetricCryptoProviders
  93. {
  94. get { return (NameTypeConfigurationElementCollection<SymmetricProviderData, CustomSymmetricCryptoProviderData>)base[symmetricCryptoProvidersProperty]; }
  95. }
  96. /// <summary>
  97. /// Creates <see cref="TypeRegistration"/> entries registration by a container.
  98. /// </summary>
  99. /// <returns>The type registration entries.</returns>
  100. public IEnumerable<TypeRegistration> GetRegistrations(IConfigurationSource configurationSource)
  101. {
  102. var hashProviderRegistrations = HashProviders.SelectMany(hpd => hpd.GetRegistrations(configurationSource));
  103. hashProviderRegistrations = SetDefaultHashProviderRegistration(hashProviderRegistrations);
  104. var symmetricCryptoProviderRegistrations = SymmetricCryptoProviders.SelectMany(scpd => scpd.GetRegistrations(configurationSource));
  105. symmetricCryptoProviderRegistrations = SetDefaultSymmetricCryptoProvider(symmetricCryptoProviderRegistrations);
  106. var cryptoManagerRegistrations = CreateCryptographyManagerRegistrations(configurationSource);
  107. return hashProviderRegistrations
  108. .Concat(symmetricCryptoProviderRegistrations)
  109. .Concat(cryptoManagerRegistrations)
  110. .Select(r => MarkPublicRegistration<IHashProvider>(r))
  111. .Select(r => MarkPublicRegistration<ISymmetricCryptoProvider>(r));
  112. }
  113. /// <summary>
  114. /// Return the <see cref="TypeRegistration"/> objects needed to reconfigure
  115. /// the container after a configuration source has changed.
  116. /// </summary>
  117. /// <remarks>If there are no reregistrations, return an empty sequence.</remarks>
  118. /// <param name="configurationSource">The <see cref="IConfigurationSource"/> containing
  119. /// the configuration information.</param>
  120. /// <returns>The sequence of <see cref="TypeRegistration"/> objects.</returns>
  121. public IEnumerable<TypeRegistration> GetUpdatedRegistrations(IConfigurationSource configurationSource)
  122. {
  123. return GetRegistrations(configurationSource);
  124. }
  125. private static TypeRegistration MarkPublicRegistration<TService>(TypeRegistration registration)
  126. {
  127. if(registration.ServiceType == typeof(TService))
  128. {
  129. registration.IsPublicName = true;
  130. }
  131. return registration;
  132. }
  133. private IEnumerable<TypeRegistration> SetDefaultHashProviderRegistration(IEnumerable<TypeRegistration> hashProviderRegisrations)
  134. {
  135. foreach (TypeRegistration registration in hashProviderRegisrations)
  136. {
  137. if (registration.ServiceType == typeof(IHashProvider) && string.Equals(registration.Name, DefaultHashProviderName))
  138. {
  139. registration.IsDefault = true;
  140. yield return registration;
  141. }
  142. else
  143. {
  144. yield return registration;
  145. }
  146. }
  147. }
  148. private IEnumerable<TypeRegistration> SetDefaultSymmetricCryptoProvider(IEnumerable<TypeRegistration> symmetricCryptoProviders)
  149. {
  150. foreach (TypeRegistration registration in symmetricCryptoProviders)
  151. {
  152. if (registration.ServiceType == typeof(ISymmetricCryptoProvider) && string.Equals(registration.Name, DefaultSymmetricCryptoProviderName))
  153. {
  154. registration.IsDefault = true;
  155. yield return registration;
  156. }
  157. else
  158. {
  159. yield return registration;
  160. }
  161. }
  162. }
  163. private IEnumerable<TypeRegistration> CreateCryptographyManagerRegistrations(IConfigurationSource configurationSource)
  164. {
  165. var hashProviderNames = (from data in HashProviders select data.Name).ToArray();
  166. var algorithmProviderNames = (from data in SymmetricCryptoProviders select data.Name).ToArray();
  167. var instrumentationSection = InstrumentationConfigurationSection.GetSection(configurationSource);
  168. yield return new TypeRegistration<IDefaultCryptographyInstrumentationProvider>(
  169. () => new DefaultCryptographyEventLogger(instrumentationSection.PerformanceCountersEnabled,
  170. instrumentationSection.EventLoggingEnabled,
  171. instrumentationSection.ApplicationInstanceName))
  172. {
  173. Lifetime = TypeRegistrationLifetime.Transient,
  174. IsDefault = true
  175. };
  176. yield return new TypeRegistration<CryptographyManager>(() =>
  177. new CryptographyManagerImpl(
  178. hashProviderNames,
  179. Container.ResolvedEnumerable<IHashProvider>(hashProviderNames),
  180. algorithmProviderNames,
  181. Container.ResolvedEnumerable<ISymmetricCryptoProvider>(algorithmProviderNames),
  182. Container.Resolved<IDefaultCryptographyInstrumentationProvider>()))
  183. {
  184. IsDefault = true,
  185. IsPublicName = true
  186. };
  187. }
  188. }
  189. }