BasicHttpMessageSecurity.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. namespace System.ServiceModel
  5. {
  6. using System.Runtime;
  7. using System.ServiceModel.Channels;
  8. using System.ServiceModel.Security;
  9. using System.ComponentModel;
  10. public sealed class BasicHttpMessageSecurity
  11. {
  12. internal const BasicHttpMessageCredentialType DefaultClientCredentialType = BasicHttpMessageCredentialType.UserName;
  13. BasicHttpMessageCredentialType clientCredentialType;
  14. SecurityAlgorithmSuite algorithmSuite;
  15. public BasicHttpMessageSecurity()
  16. {
  17. clientCredentialType = DefaultClientCredentialType;
  18. algorithmSuite = SecurityAlgorithmSuite.Default;
  19. }
  20. public BasicHttpMessageCredentialType ClientCredentialType
  21. {
  22. get { return this.clientCredentialType; }
  23. set
  24. {
  25. if (!BasicHttpMessageCredentialTypeHelper.IsDefined(value))
  26. {
  27. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("value"));
  28. }
  29. this.clientCredentialType = value;
  30. }
  31. }
  32. public SecurityAlgorithmSuite AlgorithmSuite
  33. {
  34. get { return this.algorithmSuite; }
  35. set
  36. {
  37. if (value == null)
  38. {
  39. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("value");
  40. }
  41. this.algorithmSuite = value;
  42. }
  43. }
  44. // if any changes are made to this method, please reflect them in the corresponding TryCrete() method
  45. internal SecurityBindingElement CreateMessageSecurity(bool isSecureTransportMode)
  46. {
  47. SecurityBindingElement result;
  48. if (isSecureTransportMode)
  49. {
  50. MessageSecurityVersion version = MessageSecurityVersion.WSSecurity10WSTrustFebruary2005WSSecureConversationFebruary2005WSSecurityPolicy11BasicSecurityProfile10;
  51. switch (this.clientCredentialType)
  52. {
  53. case BasicHttpMessageCredentialType.Certificate:
  54. result = SecurityBindingElement.CreateCertificateOverTransportBindingElement(version);
  55. break;
  56. case BasicHttpMessageCredentialType.UserName:
  57. result = SecurityBindingElement.CreateUserNameOverTransportBindingElement();
  58. result.MessageSecurityVersion = version;
  59. break;
  60. default:
  61. Fx.Assert("Unsupported basic http message credential type");
  62. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException());
  63. }
  64. }
  65. else
  66. {
  67. if (this.clientCredentialType != BasicHttpMessageCredentialType.Certificate)
  68. {
  69. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.BasicHttpMessageSecurityRequiresCertificate)));
  70. }
  71. result = SecurityBindingElement.CreateMutualCertificateBindingElement(MessageSecurityVersion.WSSecurity10WSTrustFebruary2005WSSecureConversationFebruary2005WSSecurityPolicy11BasicSecurityProfile10, true);
  72. }
  73. result.DefaultAlgorithmSuite = this.AlgorithmSuite;
  74. result.SecurityHeaderLayout = SecurityHeaderLayout.Lax;
  75. result.SetKeyDerivation(false);
  76. result.DoNotEmitTrust = true;
  77. return result;
  78. }
  79. // This method reverses the CreateMessageSecurity(bool) method
  80. internal static bool TryCreate(SecurityBindingElement sbe, out BasicHttpMessageSecurity security, out bool isSecureTransportMode)
  81. {
  82. Fx.Assert(null != sbe, string.Empty);
  83. security = null;
  84. isSecureTransportMode = false;
  85. if (sbe.DoNotEmitTrust == false)
  86. return false;
  87. if (!sbe.IsSetKeyDerivation(false))
  88. return false;
  89. if (sbe.SecurityHeaderLayout != SecurityHeaderLayout.Lax)
  90. return false;
  91. if (sbe.MessageSecurityVersion != MessageSecurityVersion.WSSecurity10WSTrustFebruary2005WSSecureConversationFebruary2005WSSecurityPolicy11BasicSecurityProfile10)
  92. return false;
  93. BasicHttpMessageCredentialType credentialType;
  94. if (!SecurityBindingElement.IsMutualCertificateBinding(sbe, true))
  95. {
  96. isSecureTransportMode = true;
  97. if (SecurityBindingElement.IsCertificateOverTransportBinding(sbe))
  98. {
  99. credentialType = BasicHttpMessageCredentialType.Certificate;
  100. }
  101. else if (SecurityBindingElement.IsUserNameOverTransportBinding(sbe))
  102. {
  103. credentialType = BasicHttpMessageCredentialType.UserName;
  104. }
  105. else
  106. {
  107. return false;
  108. }
  109. }
  110. else
  111. {
  112. credentialType = BasicHttpMessageCredentialType.Certificate;
  113. }
  114. security = new BasicHttpMessageSecurity();
  115. security.ClientCredentialType = credentialType;
  116. security.AlgorithmSuite = sbe.DefaultAlgorithmSuite;
  117. return true;
  118. }
  119. internal bool InternalShouldSerialize()
  120. {
  121. return this.ShouldSerializeAlgorithmSuite()
  122. || this.ShouldSerializeClientCredentialType();
  123. }
  124. [EditorBrowsable(EditorBrowsableState.Never)]
  125. public bool ShouldSerializeAlgorithmSuite()
  126. {
  127. return this.algorithmSuite.GetType() != SecurityAlgorithmSuite.Default.GetType();
  128. }
  129. [EditorBrowsable(EditorBrowsableState.Never)]
  130. public bool ShouldSerializeClientCredentialType()
  131. {
  132. return this.clientCredentialType != DefaultClientCredentialType;
  133. }
  134. }
  135. }