HttpTransportSecurity.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. namespace System.ServiceModel
  5. {
  6. using System;
  7. using System.ComponentModel;
  8. using System.Security.Authentication.ExtendedProtection;
  9. using System.ServiceModel.Channels;
  10. using System.ServiceModel.Security;
  11. using System.Net;
  12. using System.Net.Security;
  13. public sealed class HttpTransportSecurity
  14. {
  15. internal const HttpClientCredentialType DefaultClientCredentialType = HttpClientCredentialType.None;
  16. internal const HttpProxyCredentialType DefaultProxyCredentialType = HttpProxyCredentialType.None;
  17. internal const string DefaultRealm = System.ServiceModel.Channels.HttpTransportDefaults.Realm;
  18. HttpClientCredentialType clientCredentialType;
  19. HttpProxyCredentialType proxyCredentialType;
  20. string realm;
  21. ExtendedProtectionPolicy extendedProtectionPolicy;
  22. public HttpTransportSecurity()
  23. {
  24. this.clientCredentialType = DefaultClientCredentialType;
  25. this.proxyCredentialType = DefaultProxyCredentialType;
  26. this.realm = DefaultRealm;
  27. this.extendedProtectionPolicy = ChannelBindingUtility.DefaultPolicy;
  28. }
  29. public HttpClientCredentialType ClientCredentialType
  30. {
  31. get { return this.clientCredentialType; }
  32. set
  33. {
  34. if (!HttpClientCredentialTypeHelper.IsDefined(value))
  35. {
  36. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("value"));
  37. }
  38. this.clientCredentialType = value;
  39. }
  40. }
  41. public HttpProxyCredentialType ProxyCredentialType
  42. {
  43. get { return this.proxyCredentialType; }
  44. set
  45. {
  46. if (!HttpProxyCredentialTypeHelper.IsDefined(value))
  47. {
  48. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("value"));
  49. }
  50. this.proxyCredentialType = value;
  51. }
  52. }
  53. public string Realm
  54. {
  55. get { return this.realm; }
  56. set { this.realm = value; }
  57. }
  58. public ExtendedProtectionPolicy ExtendedProtectionPolicy
  59. {
  60. get
  61. {
  62. return this.extendedProtectionPolicy;
  63. }
  64. set
  65. {
  66. if (value == null)
  67. {
  68. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("value");
  69. }
  70. if (value.PolicyEnforcement == PolicyEnforcement.Always &&
  71. !System.Security.Authentication.ExtendedProtection.ExtendedProtectionPolicy.OSSupportsExtendedProtection)
  72. {
  73. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
  74. new PlatformNotSupportedException(SR.GetString(SR.ExtendedProtectionNotSupported)));
  75. }
  76. this.extendedProtectionPolicy = value;
  77. }
  78. }
  79. internal void ConfigureTransportProtectionOnly(HttpsTransportBindingElement https)
  80. {
  81. DisableAuthentication(https);
  82. https.RequireClientCertificate = false;
  83. }
  84. void ConfigureAuthentication(HttpTransportBindingElement http)
  85. {
  86. http.AuthenticationScheme = HttpClientCredentialTypeHelper.MapToAuthenticationScheme(this.clientCredentialType);
  87. http.ProxyAuthenticationScheme = HttpProxyCredentialTypeHelper.MapToAuthenticationScheme(this.proxyCredentialType);
  88. http.Realm = this.Realm;
  89. http.ExtendedProtectionPolicy = this.extendedProtectionPolicy;
  90. }
  91. static void ConfigureAuthentication(HttpTransportBindingElement http, HttpTransportSecurity transportSecurity)
  92. {
  93. transportSecurity.clientCredentialType = HttpClientCredentialTypeHelper.MapToClientCredentialType(http.AuthenticationScheme);
  94. transportSecurity.proxyCredentialType = HttpProxyCredentialTypeHelper.MapToProxyCredentialType(http.ProxyAuthenticationScheme);
  95. transportSecurity.Realm = http.Realm;
  96. transportSecurity.extendedProtectionPolicy = http.ExtendedProtectionPolicy;
  97. }
  98. void DisableAuthentication(HttpTransportBindingElement http)
  99. {
  100. http.AuthenticationScheme = AuthenticationSchemes.Anonymous;
  101. http.ProxyAuthenticationScheme = AuthenticationSchemes.Anonymous;
  102. http.Realm = DefaultRealm;
  103. //ExtendedProtectionPolicy is always copied - even for security mode None, Message and TransportWithMessageCredential,
  104. //because the settings for ExtendedProtectionPolicy are always below the <security><transport> element
  105. http.ExtendedProtectionPolicy = this.extendedProtectionPolicy;
  106. }
  107. static bool IsDisabledAuthentication(HttpTransportBindingElement http)
  108. {
  109. return http.AuthenticationScheme == AuthenticationSchemes.Anonymous && http.ProxyAuthenticationScheme == AuthenticationSchemes.Anonymous && http.Realm == DefaultRealm;
  110. }
  111. internal void ConfigureTransportProtectionAndAuthentication(HttpsTransportBindingElement https)
  112. {
  113. ConfigureAuthentication(https);
  114. https.RequireClientCertificate = (this.clientCredentialType == HttpClientCredentialType.Certificate);
  115. }
  116. internal static void ConfigureTransportProtectionAndAuthentication(HttpsTransportBindingElement https, HttpTransportSecurity transportSecurity)
  117. {
  118. ConfigureAuthentication(https, transportSecurity);
  119. if (https.RequireClientCertificate)
  120. transportSecurity.ClientCredentialType = HttpClientCredentialType.Certificate;
  121. }
  122. internal void ConfigureTransportAuthentication(HttpTransportBindingElement http)
  123. {
  124. if (this.clientCredentialType == HttpClientCredentialType.Certificate)
  125. {
  126. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.CertificateUnsupportedForHttpTransportCredentialOnly)));
  127. }
  128. ConfigureAuthentication(http);
  129. }
  130. internal static bool IsConfiguredTransportAuthentication(HttpTransportBindingElement http, HttpTransportSecurity transportSecurity)
  131. {
  132. if (HttpClientCredentialTypeHelper.MapToClientCredentialType(http.AuthenticationScheme) == HttpClientCredentialType.Certificate)
  133. return false;
  134. ConfigureAuthentication(http, transportSecurity);
  135. return true;
  136. }
  137. internal void DisableTransportAuthentication(HttpTransportBindingElement http)
  138. {
  139. DisableAuthentication(http);
  140. }
  141. internal static bool IsDisabledTransportAuthentication(HttpTransportBindingElement http)
  142. {
  143. return IsDisabledAuthentication(http);
  144. }
  145. internal bool InternalShouldSerialize()
  146. {
  147. return this.ShouldSerializeClientCredentialType()
  148. || this.ShouldSerializeProxyCredentialType()
  149. || this.ShouldSerializeRealm()
  150. || this.ShouldSerializeExtendedProtectionPolicy();
  151. }
  152. [EditorBrowsable(EditorBrowsableState.Never)]
  153. public bool ShouldSerializeClientCredentialType()
  154. {
  155. return this.ClientCredentialType != DefaultClientCredentialType;
  156. }
  157. [EditorBrowsable(EditorBrowsableState.Never)]
  158. public bool ShouldSerializeProxyCredentialType()
  159. {
  160. return this.proxyCredentialType != DefaultProxyCredentialType;
  161. }
  162. [EditorBrowsable(EditorBrowsableState.Never)]
  163. public bool ShouldSerializeRealm()
  164. {
  165. return this.Realm != DefaultRealm;
  166. }
  167. [EditorBrowsable(EditorBrowsableState.Never)]
  168. public bool ShouldSerializeExtendedProtectionPolicy()
  169. {
  170. return !ChannelBindingUtility.AreEqual(this.ExtendedProtectionPolicy, ChannelBindingUtility.DefaultPolicy);
  171. }
  172. }
  173. }