AsymmetricSecurityBindingElement.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. //
  2. // AsymmetricSecurityBindingElement.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 2005 Novell, Inc. http://www.novell.com
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System.Collections.Generic;
  29. using System.Collections.ObjectModel;
  30. using System.Net.Security;
  31. using System.IdentityModel.Selectors;
  32. using System.ServiceModel.Channels;
  33. using System.ServiceModel.Description;
  34. using System.ServiceModel.Dispatcher;
  35. using System.ServiceModel.Security;
  36. using System.ServiceModel.Security.Tokens;
  37. namespace System.ServiceModel.Channels
  38. {
  39. public sealed class AsymmetricSecurityBindingElement
  40. : SecurityBindingElement, IPolicyExportExtension
  41. {
  42. public AsymmetricSecurityBindingElement ()
  43. : this (null, null)
  44. {
  45. }
  46. public AsymmetricSecurityBindingElement (
  47. SecurityTokenParameters recipientTokenParameters)
  48. : this (recipientTokenParameters, null)
  49. {
  50. }
  51. public AsymmetricSecurityBindingElement (
  52. SecurityTokenParameters recipientTokenParameters,
  53. SecurityTokenParameters initiatorTokenParameters)
  54. {
  55. this.initiator_token_params = initiatorTokenParameters;
  56. this.recipient_token_params = recipientTokenParameters;
  57. msg_protection_order = MessageProtectionOrder.SignBeforeEncryptAndEncryptSignature;
  58. }
  59. private AsymmetricSecurityBindingElement (
  60. AsymmetricSecurityBindingElement other)
  61. : base (other)
  62. {
  63. msg_protection_order = other.msg_protection_order;
  64. require_sig_confirm = other.require_sig_confirm;
  65. if (other.initiator_token_params != null)
  66. initiator_token_params = other.initiator_token_params.Clone ();
  67. if (other.recipient_token_params != null)
  68. recipient_token_params = other.recipient_token_params.Clone ();
  69. allow_serialized_sign = other.allow_serialized_sign;
  70. }
  71. MessageProtectionOrder msg_protection_order;
  72. SecurityTokenParameters initiator_token_params,
  73. recipient_token_params;
  74. bool allow_serialized_sign, require_sig_confirm;
  75. public bool AllowSerializedSigningTokenOnReply {
  76. get { return allow_serialized_sign; }
  77. set { allow_serialized_sign = value; }
  78. }
  79. public MessageProtectionOrder MessageProtectionOrder {
  80. get { return msg_protection_order; }
  81. set { msg_protection_order = value; }
  82. }
  83. public SecurityTokenParameters InitiatorTokenParameters {
  84. get { return initiator_token_params; }
  85. set { initiator_token_params = value; }
  86. }
  87. public SecurityTokenParameters RecipientTokenParameters {
  88. get { return recipient_token_params; }
  89. set { recipient_token_params = value; }
  90. }
  91. public bool RequireSignatureConfirmation {
  92. get { return require_sig_confirm; }
  93. set { require_sig_confirm = value; }
  94. }
  95. public override void SetKeyDerivation (bool requireDerivedKeys)
  96. {
  97. base.SetKeyDerivation (requireDerivedKeys);
  98. if (InitiatorTokenParameters != null)
  99. InitiatorTokenParameters.RequireDerivedKeys = requireDerivedKeys;
  100. if (RecipientTokenParameters != null)
  101. RecipientTokenParameters.RequireDerivedKeys = requireDerivedKeys;
  102. }
  103. [MonoTODO]
  104. public override string ToString ()
  105. {
  106. return base.ToString ();
  107. }
  108. [MonoTODO]
  109. protected override IChannelFactory<TChannel>
  110. BuildChannelFactoryCore<TChannel> (
  111. BindingContext context)
  112. {
  113. if (InitiatorTokenParameters == null)
  114. throw new InvalidOperationException ("InitiatorTokenParameters must be set before building channel factory.");
  115. if (RecipientTokenParameters == null)
  116. throw new InvalidOperationException ("RecipientTokenParameters must be set before building channel factory.");
  117. SetIssuerBindingContextIfRequired (InitiatorTokenParameters, context);
  118. SetIssuerBindingContextIfRequired (RecipientTokenParameters, context);
  119. ClientCredentials cred = context.BindingParameters.Find<ClientCredentials> ();
  120. if (cred == null)
  121. // it happens when there is no ChannelFactory<T>.
  122. cred = new ClientCredentials ();
  123. SecurityTokenManager manager = cred.CreateSecurityTokenManager ();
  124. ChannelProtectionRequirements requirements =
  125. context.BindingParameters.Find<ChannelProtectionRequirements> ();
  126. return new SecurityChannelFactory<TChannel> (
  127. context.BuildInnerChannelFactory<TChannel> (), new InitiatorMessageSecurityBindingSupport (GetCapabilities (), manager, requirements));
  128. }
  129. [MonoTODO]
  130. protected override IChannelListener<TChannel>
  131. BuildChannelListenerCore<TChannel> (
  132. BindingContext context)
  133. {
  134. if (InitiatorTokenParameters == null)
  135. throw new InvalidOperationException ("InitiatorTokenParameters must be set before building channel factory.");
  136. if (RecipientTokenParameters == null)
  137. throw new InvalidOperationException ("RecipientTokenParameters must be set before building channel factory.");
  138. SetIssuerBindingContextIfRequired (InitiatorTokenParameters, context);
  139. SetIssuerBindingContextIfRequired (RecipientTokenParameters, context);
  140. ServiceCredentials cred = context.BindingParameters.Find<ServiceCredentials> ();
  141. if (cred == null)
  142. // it happens when there is no ChannelFactory<T>.
  143. cred = new ServiceCredentials ();
  144. ServiceCredentialsSecurityTokenManager manager = (ServiceCredentialsSecurityTokenManager) cred.CreateSecurityTokenManager ();
  145. ChannelProtectionRequirements requirements =
  146. context.BindingParameters.Find<ChannelProtectionRequirements> ();
  147. return new SecurityChannelListener<TChannel> (
  148. context.BuildInnerChannelListener<TChannel> (), new RecipientMessageSecurityBindingSupport (GetCapabilities (), manager, requirements));
  149. }
  150. public override BindingElement Clone ()
  151. {
  152. return new AsymmetricSecurityBindingElement (this);
  153. }
  154. [MonoTODO]
  155. public override T GetProperty<T> (BindingContext context)
  156. {
  157. if (context == null)
  158. throw new ArgumentNullException ("context");
  159. if (typeof (T) == typeof (ISecurityCapabilities))
  160. return (T) (object) GetCapabilities ();
  161. if (typeof (T) == typeof (IdentityVerifier))
  162. throw new NotImplementedException ();
  163. return context.GetInnerProperty<T> ();
  164. }
  165. AsymmetricSecurityCapabilities GetCapabilities ()
  166. {
  167. return new AsymmetricSecurityCapabilities (this);
  168. }
  169. #region explicit interface implementations
  170. [MonoTODO]
  171. void IPolicyExportExtension.ExportPolicy (
  172. MetadataExporter exporter,
  173. PolicyConversionContext policyContext)
  174. {
  175. throw new NotImplementedException ();
  176. }
  177. #endregion
  178. }
  179. }