SymmetricSecurityBindingElement.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. //
  2. // SymmetricSecurityBindingElement.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 2005-2007 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.IdentityModel.Selectors;
  31. using System.IdentityModel.Tokens;
  32. using System.Net.Security;
  33. using System.ServiceModel.Channels;
  34. using System.ServiceModel.Description;
  35. using System.ServiceModel.Security;
  36. using System.ServiceModel.Security.Tokens;
  37. using ReqType = System.ServiceModel.Security.Tokens.ServiceModelSecurityTokenRequirement;
  38. namespace System.ServiceModel.Channels
  39. {
  40. public sealed class SymmetricSecurityBindingElement
  41. : SecurityBindingElement, IPolicyExportExtension
  42. {
  43. public SymmetricSecurityBindingElement ()
  44. : this ((SecurityTokenParameters) null)
  45. {
  46. }
  47. public SymmetricSecurityBindingElement (
  48. SecurityTokenParameters protectionTokenParameters)
  49. {
  50. ProtectionTokenParameters = protectionTokenParameters;
  51. }
  52. private SymmetricSecurityBindingElement (
  53. SymmetricSecurityBindingElement other)
  54. : base (other)
  55. {
  56. msg_protection_order = other.msg_protection_order;
  57. require_sig_confirm = other.require_sig_confirm;
  58. if (other.protection_token_params != null)
  59. protection_token_params = other.protection_token_params.Clone ();
  60. }
  61. MessageProtectionOrder msg_protection_order =
  62. MessageProtectionOrder.SignBeforeEncryptAndEncryptSignature;
  63. SecurityTokenParameters protection_token_params;
  64. bool require_sig_confirm;
  65. // make sure that they are also cloned.
  66. [MonoTODO]
  67. public MessageProtectionOrder MessageProtectionOrder {
  68. get { return msg_protection_order; }
  69. set { msg_protection_order = value; }
  70. }
  71. public SecurityTokenParameters ProtectionTokenParameters {
  72. get { return protection_token_params; }
  73. set { protection_token_params = value; }
  74. }
  75. [MonoTODO]
  76. public bool RequireSignatureConfirmation {
  77. get { return require_sig_confirm; }
  78. set { require_sig_confirm = value; }
  79. }
  80. public override void SetKeyDerivation (bool requireDerivedKeys)
  81. {
  82. base.SetKeyDerivation (requireDerivedKeys);
  83. if (ProtectionTokenParameters != null)
  84. ProtectionTokenParameters.RequireDerivedKeys = requireDerivedKeys;
  85. }
  86. [MonoTODO]
  87. public override string ToString ()
  88. {
  89. return base.ToString ();
  90. }
  91. [MonoTODO]
  92. protected override IChannelFactory<TChannel>
  93. BuildChannelFactoryCore<TChannel> (
  94. BindingContext context)
  95. {
  96. if (ProtectionTokenParameters == null)
  97. throw new InvalidOperationException ("Protection token parameters must be set before building channel factory.");
  98. SetIssuerBindingContextIfRequired (ProtectionTokenParameters, context);
  99. ClientCredentials cred = context.BindingParameters.Find<ClientCredentials> ();
  100. if (cred == null)
  101. // it happens when there is no ChannelFactory<T>.
  102. cred = new ClientCredentials ();
  103. SecurityTokenManager manager = cred.CreateSecurityTokenManager ();
  104. ChannelProtectionRequirements requirements =
  105. context.BindingParameters.Find<ChannelProtectionRequirements> ();
  106. return new SecurityChannelFactory<TChannel> (
  107. context.BuildInnerChannelFactory<TChannel> (), new InitiatorMessageSecurityBindingSupport (GetCapabilities (), manager, requirements));
  108. }
  109. [MonoTODO]
  110. protected override IChannelListener<TChannel>
  111. BuildChannelListenerCore<TChannel> (
  112. BindingContext context)
  113. {
  114. if (ProtectionTokenParameters == null)
  115. throw new InvalidOperationException ("Protection token parameters must be set before building channel factory.");
  116. SetIssuerBindingContextIfRequired (ProtectionTokenParameters, context);
  117. ServiceCredentials cred = context.BindingParameters.Find<ServiceCredentials> ();
  118. if (cred == null)
  119. // it happens when there is no ChannelFactory<T>.
  120. cred = new ServiceCredentials ();
  121. ServiceCredentialsSecurityTokenManager manager = (ServiceCredentialsSecurityTokenManager) cred.CreateSecurityTokenManager ();
  122. ChannelProtectionRequirements requirements =
  123. context.BindingParameters.Find<ChannelProtectionRequirements> ();
  124. return new SecurityChannelListener<TChannel> (
  125. context.BuildInnerChannelListener<TChannel> (), new RecipientMessageSecurityBindingSupport (GetCapabilities (), manager, requirements));
  126. }
  127. public override BindingElement Clone ()
  128. {
  129. return new SymmetricSecurityBindingElement (this);
  130. }
  131. [MonoTODO]
  132. public override T GetProperty<T> (BindingContext context)
  133. {
  134. if (context == null)
  135. throw new ArgumentNullException ("context");
  136. if (typeof (T) == typeof (ISecurityCapabilities))
  137. return (T) (object) GetCapabilities ();
  138. if (typeof (T) == typeof (IdentityVerifier))
  139. throw new NotImplementedException ();
  140. return context.GetInnerProperty<T> ();
  141. }
  142. SymmetricSecurityCapabilities GetCapabilities ()
  143. {
  144. return new SymmetricSecurityCapabilities (this);
  145. }
  146. #region explicit interface implementations
  147. [MonoTODO]
  148. void IPolicyExportExtension.ExportPolicy (
  149. MetadataExporter exporter,
  150. PolicyConversionContext policyContext)
  151. {
  152. throw new NotImplementedException ();
  153. }
  154. #endregion
  155. }
  156. }