WindowsStreamSecurityBindingElement.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. namespace System.ServiceModel.Channels
  5. {
  6. using System.Collections.Generic;
  7. using System.ServiceModel.Description;
  8. using System.Net.Security;
  9. using System.ServiceModel;
  10. using System.ServiceModel.Security;
  11. using System.ComponentModel;
  12. using System.Xml;
  13. public class WindowsStreamSecurityBindingElement : StreamUpgradeBindingElement,
  14. ITransportTokenAssertionProvider,
  15. IPolicyExportExtension
  16. {
  17. ProtectionLevel protectionLevel;
  18. public WindowsStreamSecurityBindingElement()
  19. : base()
  20. {
  21. this.protectionLevel = ConnectionOrientedTransportDefaults.ProtectionLevel;
  22. }
  23. protected WindowsStreamSecurityBindingElement(WindowsStreamSecurityBindingElement elementToBeCloned)
  24. : base(elementToBeCloned)
  25. {
  26. this.protectionLevel = elementToBeCloned.protectionLevel;
  27. }
  28. [DefaultValue(ConnectionOrientedTransportDefaults.ProtectionLevel)]
  29. public ProtectionLevel ProtectionLevel
  30. {
  31. get
  32. {
  33. return this.protectionLevel;
  34. }
  35. set
  36. {
  37. ProtectionLevelHelper.Validate(value);
  38. this.protectionLevel = value;
  39. }
  40. }
  41. public override BindingElement Clone()
  42. {
  43. return new WindowsStreamSecurityBindingElement(this);
  44. }
  45. public override IChannelFactory<TChannel> BuildChannelFactory<TChannel>(BindingContext context)
  46. {
  47. if (context == null)
  48. {
  49. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("context");
  50. }
  51. #pragma warning suppress 56506 // [....], BindingContext.BindingParameters cannot be null
  52. context.BindingParameters.Add(this);
  53. return context.BuildInnerChannelFactory<TChannel>();
  54. }
  55. public override bool CanBuildChannelFactory<TChannel>(BindingContext context)
  56. {
  57. if (context == null)
  58. {
  59. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("context");
  60. }
  61. #pragma warning suppress 56506 // [....], BindingContext.BindingParameters cannot be null
  62. context.BindingParameters.Add(this);
  63. return context.CanBuildInnerChannelFactory<TChannel>();
  64. }
  65. public override IChannelListener<TChannel> BuildChannelListener<TChannel>(BindingContext context)
  66. {
  67. if (context == null)
  68. {
  69. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("context");
  70. }
  71. #pragma warning suppress 56506 // [....], BindingContext.BindingParameters cannot be null
  72. context.BindingParameters.Add(this);
  73. return context.BuildInnerChannelListener<TChannel>();
  74. }
  75. public override bool CanBuildChannelListener<TChannel>(BindingContext context)
  76. {
  77. if (context == null)
  78. {
  79. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("context");
  80. }
  81. #pragma warning suppress 56506 // [....], BindingContext.BindingParameters cannot be null
  82. context.BindingParameters.Add(this);
  83. return context.CanBuildInnerChannelListener<TChannel>();
  84. }
  85. public override StreamUpgradeProvider BuildClientStreamUpgradeProvider(BindingContext context)
  86. {
  87. return new WindowsStreamSecurityUpgradeProvider(this, context, true);
  88. }
  89. public override StreamUpgradeProvider BuildServerStreamUpgradeProvider(BindingContext context)
  90. {
  91. return new WindowsStreamSecurityUpgradeProvider(this, context, false);
  92. }
  93. public override T GetProperty<T>(BindingContext context)
  94. {
  95. if (context == null)
  96. {
  97. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("context");
  98. }
  99. if (typeof(T) == typeof(ISecurityCapabilities))
  100. {
  101. return (T)(object)new SecurityCapabilities(true, true, true, protectionLevel, protectionLevel);
  102. }
  103. else if (typeof(T) == typeof(IdentityVerifier))
  104. {
  105. return (T)(object)IdentityVerifier.CreateDefault();
  106. }
  107. else
  108. {
  109. return context.GetInnerProperty<T>();
  110. }
  111. }
  112. internal static void ImportPolicy(MetadataImporter importer, PolicyConversionContext policyContext)
  113. {
  114. XmlElement assertion = PolicyConversionContext.FindAssertion(policyContext.GetBindingAssertions(),
  115. TransportPolicyConstants.WindowsTransportSecurityName, TransportPolicyConstants.DotNetFramingNamespace, true);
  116. if (assertion != null)
  117. {
  118. WindowsStreamSecurityBindingElement windowsBindingElement
  119. = new WindowsStreamSecurityBindingElement();
  120. XmlReader reader = new XmlNodeReader(assertion);
  121. reader.ReadStartElement();
  122. string protectionLevelString = null;
  123. if (reader.IsStartElement(
  124. TransportPolicyConstants.ProtectionLevelName,
  125. TransportPolicyConstants.DotNetFramingNamespace) && !reader.IsEmptyElement)
  126. {
  127. protectionLevelString = reader.ReadElementContentAsString();
  128. }
  129. if (string.IsNullOrEmpty(protectionLevelString))
  130. {
  131. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(
  132. SR.GetString(SR.ExpectedElementMissing, TransportPolicyConstants.ProtectionLevelName, TransportPolicyConstants.DotNetFramingNamespace)));
  133. }
  134. windowsBindingElement.ProtectionLevel = (ProtectionLevel)Enum.Parse(typeof(ProtectionLevel), protectionLevelString);
  135. policyContext.BindingElements.Add(windowsBindingElement);
  136. }
  137. }
  138. #region ITransportTokenAssertionProvider Members
  139. public XmlElement GetTransportTokenAssertion()
  140. {
  141. XmlDocument document = new XmlDocument();
  142. XmlElement assertion =
  143. document.CreateElement(TransportPolicyConstants.DotNetFramingPrefix,
  144. TransportPolicyConstants.WindowsTransportSecurityName,
  145. TransportPolicyConstants.DotNetFramingNamespace);
  146. XmlElement protectionLevelElement = document.CreateElement(TransportPolicyConstants.DotNetFramingPrefix,
  147. TransportPolicyConstants.ProtectionLevelName, TransportPolicyConstants.DotNetFramingNamespace);
  148. protectionLevelElement.AppendChild(document.CreateTextNode(this.ProtectionLevel.ToString()));
  149. assertion.AppendChild(protectionLevelElement);
  150. return assertion;
  151. }
  152. #endregion
  153. void IPolicyExportExtension.ExportPolicy(MetadataExporter exporter, PolicyConversionContext context)
  154. {
  155. if (exporter == null)
  156. {
  157. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("exporter");
  158. }
  159. if (context == null)
  160. {
  161. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("context");
  162. }
  163. SecurityBindingElement.ExportPolicyForTransportTokenAssertionProviders(exporter, context);
  164. }
  165. internal override bool IsMatch(BindingElement b)
  166. {
  167. if (b == null)
  168. {
  169. return false;
  170. }
  171. WindowsStreamSecurityBindingElement security = b as WindowsStreamSecurityBindingElement;
  172. if (security == null)
  173. {
  174. return false;
  175. }
  176. if (this.protectionLevel != security.protectionLevel)
  177. {
  178. return false;
  179. }
  180. return true;
  181. }
  182. }
  183. }