PageRenderTime 49ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/ExpressInteropBinding/Microsoft.ServiceModel.Interop/Configuration/SecurityAlgorithmConverter.cs

#
C# | 222 lines | 143 code | 43 blank | 36 comment | 45 complexity | f9a1263a04f55130f9bdbf5e5f543ee6 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, Apache-2.0
  1. // <copyright file="SecurityAlgorithmConverter.cs" company="Microsoft Corporation">
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. // </copyright>
  4. namespace Microsoft.ServiceModel.Interop.Configuration
  5. {
  6. using System;
  7. using System.ComponentModel;
  8. using System.ComponentModel.Design.Serialization;
  9. using System.Globalization;
  10. using System.ServiceModel.Security;
  11. using Microsoft.ServiceModel.Interop.Properties;
  12. /// <summary>
  13. /// Type converter implementation for security algorithms
  14. /// </summary>
  15. internal class SecurityAlgorithmSuiteConverter : TypeConverter
  16. {
  17. /// <summary>
  18. /// Gets a value indicating whether this converter can convert an object in the
  19. /// given source type to a Security algorithm suite using the specified context.
  20. /// </summary>
  21. /// <param name="context">An System.ComponentModel.ITypeDescriptorContext that provides a format context.</param>
  22. /// <param name="sourceType">A System.Type that represents the type you wish to convert from.</param>
  23. /// <returns>true if this converter can perform the conversion; otherwise, false.</returns>
  24. public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
  25. {
  26. return (typeof(string) == sourceType) || base.CanConvertFrom(context, sourceType);
  27. }
  28. /// <summary>
  29. /// Gets a value indicating whether this converter can convert an object to the
  30. /// given destination type using the context.
  31. /// </summary>
  32. /// <param name="context">An System.ComponentModel.ITypeDescriptorContext that provides a format context.</param>
  33. /// <param name="destinationType">A System.Type that represents the type you wish to convert to.</param>
  34. /// <returns>true if this converter can perform the conversion; otherwise, false.</returns>
  35. public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
  36. {
  37. return (typeof(InstanceDescriptor) == destinationType) || base.CanConvertTo(context, destinationType);
  38. }
  39. /// <summary>
  40. /// Converts the given object to a Security Algorithm Suite.
  41. /// </summary>
  42. /// <param name="context">An System.ComponentModel.ITypeDescriptorContext that provides a format context.</param>
  43. /// <param name="culture">An optional System.Globalization.CultureInfo. If not supplied, the current
  44. /// culture is assumed.</param>
  45. /// <param name="value">The System.Object to convert.</param>
  46. /// <returns>An System.Object that represents the converted value.</returns>
  47. public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
  48. {
  49. string str = value as string;
  50. if (str == null)
  51. {
  52. return base.ConvertFrom(context, culture, value);
  53. }
  54. switch (str)
  55. {
  56. case "Default":
  57. return SecurityAlgorithmSuite.Default;
  58. case "Basic256":
  59. return SecurityAlgorithmSuite.Basic256;
  60. case "Basic192":
  61. return SecurityAlgorithmSuite.Basic192;
  62. case "Basic128":
  63. return SecurityAlgorithmSuite.Basic128;
  64. case "TripleDes":
  65. return SecurityAlgorithmSuite.TripleDes;
  66. case "Basic256Rsa15":
  67. return SecurityAlgorithmSuite.Basic256Rsa15;
  68. case "Basic192Rsa15":
  69. return SecurityAlgorithmSuite.Basic192Rsa15;
  70. case "Basic128Rsa15":
  71. return SecurityAlgorithmSuite.Basic128Rsa15;
  72. case "TripleDesRsa15":
  73. return SecurityAlgorithmSuite.TripleDesRsa15;
  74. case "Basic256Sha256":
  75. return SecurityAlgorithmSuite.Basic256Sha256;
  76. case "Basic192Sha256":
  77. return SecurityAlgorithmSuite.Basic192Sha256;
  78. case "Basic128Sha256":
  79. return SecurityAlgorithmSuite.Basic128Sha256;
  80. case "TripleDesSha256":
  81. return SecurityAlgorithmSuite.TripleDesSha256;
  82. case "Basic256Sha256Rsa15":
  83. return SecurityAlgorithmSuite.Basic256Sha256Rsa15;
  84. case "Basic192Sha256Rsa15":
  85. return SecurityAlgorithmSuite.Basic192Sha256Rsa15;
  86. case "Basic128Sha256Rsa15":
  87. return SecurityAlgorithmSuite.Basic128Sha256Rsa15;
  88. case "TripleDesSha256Rsa15":
  89. return SecurityAlgorithmSuite.TripleDesSha256Rsa15;
  90. }
  91. throw new ArgumentOutOfRangeException("value", string.Format(CultureInfo.InvariantCulture, Strings.Not_Supported_Algorithm, typeof(SecurityAlgorithmSuite).FullName));
  92. }
  93. /// <summary>
  94. /// Converts the given object to another type.
  95. /// </summary>
  96. /// <param name="context">A formatter context.</param>
  97. /// <param name="culture">The culture into which value will be converted.</param>
  98. /// <param name="value">The object to convert.</param>
  99. /// <param name="destinationType">The type to convert the object to.</param>
  100. /// <returns>The converted object.</returns>
  101. public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
  102. {
  103. SecurityAlgorithmSuite suite = value as SecurityAlgorithmSuite;
  104. if (!(typeof(string) == destinationType) || suite == null)
  105. {
  106. return base.ConvertTo(context, culture, value, destinationType);
  107. }
  108. if (suite == SecurityAlgorithmSuite.Default)
  109. {
  110. return "Default";
  111. }
  112. if (suite == SecurityAlgorithmSuite.Basic256)
  113. {
  114. return "Basic256";
  115. }
  116. if (suite == SecurityAlgorithmSuite.Basic192)
  117. {
  118. return "Basic192";
  119. }
  120. if (suite == SecurityAlgorithmSuite.Basic128)
  121. {
  122. return "Basic128";
  123. }
  124. if (suite == SecurityAlgorithmSuite.TripleDes)
  125. {
  126. return "TripleDes";
  127. }
  128. if (suite == SecurityAlgorithmSuite.Basic256Rsa15)
  129. {
  130. return "Basic256Rsa15";
  131. }
  132. if (suite == SecurityAlgorithmSuite.Basic192Rsa15)
  133. {
  134. return "Basic192Rsa15";
  135. }
  136. if (suite == SecurityAlgorithmSuite.Basic128Rsa15)
  137. {
  138. return "Basic128Rsa15";
  139. }
  140. if (suite == SecurityAlgorithmSuite.TripleDesRsa15)
  141. {
  142. return "TripleDesRsa15";
  143. }
  144. if (suite == SecurityAlgorithmSuite.Basic256Sha256)
  145. {
  146. return "Basic256Sha256";
  147. }
  148. if (suite == SecurityAlgorithmSuite.Basic192Sha256)
  149. {
  150. return "Basic192Sha256";
  151. }
  152. if (suite == SecurityAlgorithmSuite.Basic128Sha256)
  153. {
  154. return "Basic128Sha256";
  155. }
  156. if (suite == SecurityAlgorithmSuite.TripleDesSha256)
  157. {
  158. return "TripleDesSha256";
  159. }
  160. if (suite == SecurityAlgorithmSuite.Basic256Sha256Rsa15)
  161. {
  162. return "Basic256Sha256Rsa15";
  163. }
  164. if (suite == SecurityAlgorithmSuite.Basic192Sha256Rsa15)
  165. {
  166. return "Basic192Sha256Rsa15";
  167. }
  168. if (suite == SecurityAlgorithmSuite.Basic128Sha256Rsa15)
  169. {
  170. return "Basic128Sha256Rsa15";
  171. }
  172. if (suite != SecurityAlgorithmSuite.TripleDesSha256Rsa15)
  173. {
  174. throw new ArgumentOutOfRangeException("value", string.Format(CultureInfo.InvariantCulture, Strings.Not_Supported_Algorithm, typeof(SecurityAlgorithmSuite).FullName));
  175. }
  176. return "TripleDesSha256Rsa15";
  177. }
  178. }
  179. }