NetTcpBinding.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. //
  2. // NetTcpBinding.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;
  29. using System.Collections.Generic;
  30. using System.Net.Security;
  31. using System.ServiceModel.Channels;
  32. using System.ServiceModel.Description;
  33. using System.ServiceModel.Security;
  34. using System.ServiceModel.Security.Tokens;
  35. using System.ServiceModel.Configuration;
  36. using System.Text;
  37. using System.Xml;
  38. namespace System.ServiceModel
  39. {
  40. public class NetTcpBinding : Binding, IBindingRuntimePreferences
  41. {
  42. int max_conn;
  43. OptionalReliableSession reliable_session;
  44. NetTcpSecurity security;
  45. XmlDictionaryReaderQuotas reader_quotas
  46. = new XmlDictionaryReaderQuotas ();
  47. bool transaction_flow;
  48. TransactionProtocol transaction_protocol;
  49. TcpTransportBindingElement transport;
  50. public NetTcpBinding ()
  51. : this (SecurityMode.Transport)
  52. {
  53. }
  54. public NetTcpBinding (SecurityMode securityMode)
  55. : this (securityMode, false)
  56. {
  57. }
  58. public NetTcpBinding (SecurityMode securityMode,
  59. bool reliableSessionEnabled)
  60. {
  61. security = new NetTcpSecurity (securityMode);
  62. transport = new TcpTransportBindingElement ();
  63. }
  64. public NetTcpBinding (string configurationName)
  65. : this ()
  66. {
  67. var bindingsSection = ConfigUtil.BindingsSection;
  68. var el = bindingsSection.NetTcpBinding.Bindings [configurationName];
  69. el.ApplyConfiguration (this);
  70. }
  71. internal NetTcpBinding (TcpTransportBindingElement transport,
  72. NetTcpSecurity security,
  73. bool reliableSessionEnabled)
  74. {
  75. this.transport = transport;
  76. this.security = security;
  77. }
  78. public HostNameComparisonMode HostNameComparisonMode {
  79. get { return transport.HostNameComparisonMode; }
  80. set { transport.HostNameComparisonMode = value; }
  81. }
  82. public int ListenBacklog {
  83. get { return transport.ListenBacklog; }
  84. set { transport.ListenBacklog = value; }
  85. }
  86. public long MaxBufferPoolSize {
  87. get { return transport.MaxBufferPoolSize; }
  88. set { transport.MaxBufferPoolSize = value; }
  89. }
  90. public int MaxBufferSize {
  91. get { return transport.MaxBufferSize; }
  92. set { transport.MaxBufferSize = value; }
  93. }
  94. [MonoTODO]
  95. public int MaxConnections {
  96. get { return max_conn; }
  97. set { max_conn = value; }
  98. }
  99. public long MaxReceivedMessageSize {
  100. get { return transport.MaxReceivedMessageSize; }
  101. set { transport.MaxReceivedMessageSize = value; }
  102. }
  103. public bool PortSharingEnabled {
  104. get { return transport.PortSharingEnabled; }
  105. set { transport.PortSharingEnabled = value; }
  106. }
  107. [MonoTODO]
  108. public OptionalReliableSession ReliableSession {
  109. get { return reliable_session; }
  110. }
  111. public XmlDictionaryReaderQuotas ReaderQuotas {
  112. get { return reader_quotas; }
  113. set { reader_quotas = value; }
  114. }
  115. public NetTcpSecurity Security {
  116. get { return security; }
  117. set { security = value; }
  118. }
  119. public EnvelopeVersion EnvelopeVersion {
  120. get { return EnvelopeVersion.Soap12; }
  121. }
  122. public TransferMode TransferMode {
  123. get { return transport.TransferMode; }
  124. set { transport.TransferMode = value; }
  125. }
  126. public bool TransactionFlow {
  127. get { return transaction_flow; }
  128. set { transaction_flow = value; }
  129. }
  130. public TransactionProtocol TransactionProtocol {
  131. get { return transaction_protocol; }
  132. set { transaction_protocol = value; }
  133. }
  134. // overrides
  135. public override string Scheme {
  136. get { return "net.tcp"; }
  137. }
  138. public override BindingElementCollection CreateBindingElements ()
  139. {
  140. BindingElement tx = new TransactionFlowBindingElement (TransactionProtocol.WSAtomicTransactionOctober2004);
  141. SecurityBindingElement sec = CreateMessageSecurity ();
  142. var msg = new BinaryMessageEncodingBindingElement ();
  143. if (ReaderQuotas != null)
  144. ReaderQuotas.CopyTo (msg.ReaderQuotas);
  145. var trsec = CreateTransportSecurity ();
  146. BindingElement tr = GetTransport ();
  147. List<BindingElement> list = new List<BindingElement> ();
  148. if (tx != null)
  149. list.Add (tx);
  150. if (sec != null)
  151. list.Add (sec);
  152. list.Add (msg);
  153. if (trsec != null)
  154. list.Add (trsec);
  155. list.Add (tr);
  156. return new BindingElementCollection (list.ToArray ());
  157. }
  158. BindingElement GetTransport ()
  159. {
  160. return transport.Clone ();
  161. }
  162. // It is problematic, but there is no option to disable establishing security context in this binding unlike WSHttpBinding...
  163. SecurityBindingElement CreateMessageSecurity ()
  164. {
  165. if (Security.Mode == SecurityMode.Transport ||
  166. Security.Mode == SecurityMode.None)
  167. return null;
  168. // FIXME: this is wrong. Could be Asymmetric, depends on Security.Message.AlgorithmSuite value.
  169. SymmetricSecurityBindingElement element =
  170. new SymmetricSecurityBindingElement ();
  171. element.MessageSecurityVersion = MessageSecurityVersion.Default;
  172. element.SetKeyDerivation (false);
  173. switch (Security.Message.ClientCredentialType) {
  174. case MessageCredentialType.Certificate:
  175. element.EndpointSupportingTokenParameters.Endorsing.Add (
  176. new X509SecurityTokenParameters ());
  177. goto default;
  178. case MessageCredentialType.IssuedToken:
  179. IssuedSecurityTokenParameters istp =
  180. new IssuedSecurityTokenParameters ();
  181. // FIXME: issuer binding must be secure.
  182. istp.IssuerBinding = new CustomBinding (
  183. new TextMessageEncodingBindingElement (),
  184. GetTransport ());
  185. element.EndpointSupportingTokenParameters.Endorsing.Add (istp);
  186. goto default;
  187. case MessageCredentialType.UserName:
  188. element.EndpointSupportingTokenParameters.SignedEncrypted.Add (
  189. new UserNameSecurityTokenParameters ());
  190. goto default;
  191. case MessageCredentialType.Windows:
  192. element.ProtectionTokenParameters =
  193. new KerberosSecurityTokenParameters ();
  194. break;
  195. default: // including .None
  196. X509SecurityTokenParameters p =
  197. new X509SecurityTokenParameters ();
  198. p.X509ReferenceStyle = X509KeyIdentifierClauseType.Thumbprint;
  199. element.ProtectionTokenParameters = p;
  200. break;
  201. }
  202. // SecureConversation enabled
  203. ChannelProtectionRequirements reqs =
  204. new ChannelProtectionRequirements ();
  205. // FIXME: fill the reqs
  206. return SecurityBindingElement.CreateSecureConversationBindingElement (
  207. // FIXME: requireCancellation
  208. element, true, reqs);
  209. }
  210. BindingElement CreateTransportSecurity ()
  211. {
  212. switch (Security.Mode) {
  213. case SecurityMode.Transport:
  214. return new WindowsStreamSecurityBindingElement () {
  215. ProtectionLevel = Security.Transport.ProtectionLevel };
  216. case SecurityMode.TransportWithMessageCredential:
  217. return new SslStreamSecurityBindingElement ();
  218. default:
  219. return null;
  220. }
  221. // FIXME: consider Security.Transport.ExtendedProtectionPolicy.
  222. switch (Security.Transport.ClientCredentialType) {
  223. case TcpClientCredentialType.Windows:
  224. return new WindowsStreamSecurityBindingElement () { ProtectionLevel = Security.Transport.ProtectionLevel };
  225. case TcpClientCredentialType.Certificate:
  226. // FIXME: set RequireClientCertificate and IdentityVerifier depending on other properties, if applicable.
  227. return new SslStreamSecurityBindingElement ();
  228. default: // includes None
  229. return null;
  230. }
  231. }
  232. bool IBindingRuntimePreferences.ReceiveSynchronously {
  233. get { throw new NotImplementedException (); }
  234. }
  235. }
  236. }