5 // Atsushi Enomoto <atsushi@ximian.com>
7 // Copyright (C) 2005 Novell, Inc. http://www.novell.com
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
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;
38 namespace System.ServiceModel
40 public class NetTcpBinding : Binding, IBindingRuntimePreferences
43 OptionalReliableSession reliable_session;
44 NetTcpSecurity security;
45 XmlDictionaryReaderQuotas reader_quotas;
46 bool transaction_flow;
47 TransactionProtocol transaction_protocol;
48 TcpTransportBindingElement transport = new TcpTransportBindingElement ();
50 public NetTcpBinding ()
51 : this (SecurityMode.Transport)
55 public NetTcpBinding (SecurityMode securityMode)
56 : this (securityMode, false)
60 public NetTcpBinding (SecurityMode securityMode,
61 bool reliableSessionEnabled)
63 security = new NetTcpSecurity (securityMode);
66 public HostNameComparisonMode HostNameComparisonMode {
67 get { return transport.HostNameComparisonMode; }
68 set { transport.HostNameComparisonMode = value; }
71 public int ListenBacklog {
72 get { return transport.ListenBacklog; }
73 set { transport.ListenBacklog = value; }
76 public long MaxBufferPoolSize {
77 get { return transport.MaxBufferPoolSize; }
78 set { transport.MaxBufferPoolSize = value; }
81 public int MaxBufferSize {
82 get { return transport.MaxBufferSize; }
83 set { transport.MaxBufferSize = value; }
87 public int MaxConnections {
88 get { return max_conn; }
89 set { max_conn = value; }
92 public long MaxReceivedMessageSize {
93 get { return transport.MaxReceivedMessageSize; }
94 set { transport.MaxReceivedMessageSize = value; }
97 public bool PortSharingEnabled {
98 get { return transport.PortSharingEnabled; }
99 set { transport.PortSharingEnabled = value; }
103 public OptionalReliableSession ReliableSession {
104 get { return reliable_session; }
107 public XmlDictionaryReaderQuotas ReaderQuotas {
108 get { return reader_quotas; }
109 set { reader_quotas = value; }
112 public NetTcpSecurity Security {
113 get { return security; }
116 public EnvelopeVersion EnvelopeVersion {
117 get { return EnvelopeVersion.Soap12; }
120 public TransferMode TransferMode {
121 get { return transport.TransferMode; }
122 set { transport.TransferMode = value; }
125 public bool TransactionFlow {
126 get { return transaction_flow; }
127 set { transaction_flow = value; }
130 public TransactionProtocol TransactionProtocol {
131 get { return transaction_protocol; }
132 set { transaction_protocol = value; }
137 public override string Scheme {
138 get { return "net.tcp"; }
141 public override BindingElementCollection CreateBindingElements ()
143 BindingElement tx = new TransactionFlowBindingElement (TransactionProtocol.WSAtomicTransactionOctober2004);
144 SecurityBindingElement sec = CreateMessageSecurity ();
145 var msg = new BinaryMessageEncodingBindingElement ();
146 if (ReaderQuotas != null)
147 ReaderQuotas.CopyTo (msg.ReaderQuotas);
148 var trsec = CreateTransportSecurity ();
149 BindingElement tr = GetTransport ();
150 List<BindingElement> list = new List<BindingElement> ();
159 return new BindingElementCollection (list.ToArray ());
162 BindingElement GetTransport ()
164 return transport.Clone ();
167 // It is problematic, but there is no option to disable establishing security context in this binding unlike WSHttpBinding...
168 SecurityBindingElement CreateMessageSecurity ()
170 if (Security.Mode == SecurityMode.Transport ||
171 Security.Mode == SecurityMode.None)
174 // FIXME: this is wrong. Could be Asymmetric, depends on Security.Message.AlgorithmSuite value.
175 SymmetricSecurityBindingElement element =
176 new SymmetricSecurityBindingElement ();
178 element.MessageSecurityVersion = MessageSecurityVersion.Default;
180 element.SetKeyDerivation (false);
182 switch (Security.Message.ClientCredentialType) {
183 case MessageCredentialType.Certificate:
184 element.EndpointSupportingTokenParameters.Endorsing.Add (
185 new X509SecurityTokenParameters ());
187 case MessageCredentialType.IssuedToken:
188 IssuedSecurityTokenParameters istp =
189 new IssuedSecurityTokenParameters ();
190 // FIXME: issuer binding must be secure.
191 istp.IssuerBinding = new CustomBinding (
192 new TextMessageEncodingBindingElement (),
194 element.EndpointSupportingTokenParameters.Endorsing.Add (istp);
196 case MessageCredentialType.UserName:
197 element.EndpointSupportingTokenParameters.SignedEncrypted.Add (
198 new UserNameSecurityTokenParameters ());
200 case MessageCredentialType.Windows:
201 element.ProtectionTokenParameters =
202 new KerberosSecurityTokenParameters ();
204 default: // including .None
205 X509SecurityTokenParameters p =
206 new X509SecurityTokenParameters ();
207 p.X509ReferenceStyle = X509KeyIdentifierClauseType.Thumbprint;
208 element.ProtectionTokenParameters = p;
212 // SecureConversation enabled
214 ChannelProtectionRequirements reqs =
215 new ChannelProtectionRequirements ();
216 // FIXME: fill the reqs
218 return SecurityBindingElement.CreateSecureConversationBindingElement (
219 // FIXME: requireCancellation
220 element, true, reqs);
223 BindingElement CreateTransportSecurity ()
225 switch (Security.Mode) {
226 case SecurityMode.None:
227 case SecurityMode.Message:
231 // FIXME: consider Security.Transport.ExtendedProtectionPolicy.
233 switch (Security.Transport.ClientCredentialType) {
234 case TcpClientCredentialType.Windows:
235 return new WindowsStreamSecurityBindingElement () { ProtectionLevel = Security.Transport.ProtectionLevel };
236 case TcpClientCredentialType.Certificate:
237 // FIXME: set RequireClientCertificate and IdentityVerifier depending on other properties, if applicable.
238 return new SslStreamSecurityBindingElement ();
239 default: // includes None
244 bool IBindingRuntimePreferences.ReceiveSynchronously {
245 get { throw new NotImplementedException (); }