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;
35 using System.ServiceModel.Configuration;
39 namespace System.ServiceModel
41 public class NetTcpBinding : Binding, IBindingRuntimePreferences
44 OptionalReliableSession reliable_session;
45 NetTcpSecurity security;
46 XmlDictionaryReaderQuotas reader_quotas
47 = new XmlDictionaryReaderQuotas ();
48 bool transaction_flow;
49 #if !MOBILE && !XAMMAC_4_5
50 TransactionProtocol transaction_protocol;
52 TcpTransportBindingElement transport;
54 public NetTcpBinding ()
55 : this (SecurityMode.Transport)
59 public NetTcpBinding (SecurityMode securityMode)
60 : this (securityMode, false)
64 public NetTcpBinding (SecurityMode securityMode,
65 bool reliableSessionEnabled)
67 security = new NetTcpSecurity (securityMode);
68 transport = new TcpTransportBindingElement ();
71 public NetTcpBinding (string configurationName)
74 #if !MOBILE && !XAMMAC_4_5
75 var bindingsSection = ConfigUtil.BindingsSection;
76 var el = bindingsSection.NetTcpBinding.Bindings [configurationName];
77 el.ApplyConfiguration (this);
79 throw new NotImplementedException ();
83 internal NetTcpBinding (TcpTransportBindingElement transport,
84 NetTcpSecurity security,
85 bool reliableSessionEnabled)
87 this.transport = transport;
88 this.security = security;
91 public HostNameComparisonMode HostNameComparisonMode {
92 get { return transport.HostNameComparisonMode; }
93 set { transport.HostNameComparisonMode = value; }
96 public int ListenBacklog {
97 get { return transport.ListenBacklog; }
98 set { transport.ListenBacklog = value; }
101 public long MaxBufferPoolSize {
102 get { return transport.MaxBufferPoolSize; }
103 set { transport.MaxBufferPoolSize = value; }
106 public int MaxBufferSize {
107 get { return transport.MaxBufferSize; }
108 set { transport.MaxBufferSize = value; }
112 public int MaxConnections {
113 get { return max_conn; }
114 set { max_conn = value; }
117 public long MaxReceivedMessageSize {
118 get { return transport.MaxReceivedMessageSize; }
119 set { transport.MaxReceivedMessageSize = value; }
122 public bool PortSharingEnabled {
123 get { return transport.PortSharingEnabled; }
124 set { transport.PortSharingEnabled = value; }
128 public OptionalReliableSession ReliableSession {
129 get { return reliable_session; }
132 public XmlDictionaryReaderQuotas ReaderQuotas {
133 get { return reader_quotas; }
134 set { reader_quotas = value; }
137 public NetTcpSecurity Security {
138 get { return security; }
139 set { security = value; }
142 public EnvelopeVersion EnvelopeVersion {
143 get { return EnvelopeVersion.Soap12; }
146 public TransferMode TransferMode {
147 get { return transport.TransferMode; }
148 set { transport.TransferMode = value; }
151 public bool TransactionFlow {
152 get { return transaction_flow; }
153 set { transaction_flow = value; }
156 #if !MOBILE && !XAMMAC_4_5
157 public TransactionProtocol TransactionProtocol {
158 get { return transaction_protocol; }
159 set { transaction_protocol = value; }
165 public override string Scheme {
166 get { return "net.tcp"; }
169 public override BindingElementCollection CreateBindingElements ()
171 #if !MOBILE && !XAMMAC_4_5
172 BindingElement tx = new TransactionFlowBindingElement (TransactionProtocol.WSAtomicTransactionOctober2004);
173 SecurityBindingElement sec = CreateMessageSecurity ();
175 var msg = new BinaryMessageEncodingBindingElement ();
176 if (ReaderQuotas != null)
177 ReaderQuotas.CopyTo (msg.ReaderQuotas);
178 var trsec = CreateTransportSecurity ();
179 BindingElement tr = GetTransport ();
180 List<BindingElement> list = new List<BindingElement> ();
181 #if !MOBILE && !XAMMAC_4_5
191 return new BindingElementCollection (list.ToArray ());
194 BindingElement GetTransport ()
196 return transport.Clone ();
199 #if !MOBILE && !XAMMAC_4_5
200 // It is problematic, but there is no option to disable establishing security context in this binding unlike WSHttpBinding...
201 SecurityBindingElement CreateMessageSecurity ()
203 if (Security.Mode == SecurityMode.Transport ||
204 Security.Mode == SecurityMode.None)
207 // FIXME: this is wrong. Could be Asymmetric, depends on Security.Message.AlgorithmSuite value.
208 SymmetricSecurityBindingElement element =
209 new SymmetricSecurityBindingElement ();
211 element.MessageSecurityVersion = MessageSecurityVersion.Default;
213 element.SetKeyDerivation (false);
215 switch (Security.Message.ClientCredentialType) {
216 case MessageCredentialType.Certificate:
217 element.EndpointSupportingTokenParameters.Endorsing.Add (
218 new X509SecurityTokenParameters ());
220 case MessageCredentialType.IssuedToken:
221 IssuedSecurityTokenParameters istp =
222 new IssuedSecurityTokenParameters ();
223 // FIXME: issuer binding must be secure.
224 istp.IssuerBinding = new CustomBinding (
225 new TextMessageEncodingBindingElement (),
227 element.EndpointSupportingTokenParameters.Endorsing.Add (istp);
229 case MessageCredentialType.UserName:
230 element.EndpointSupportingTokenParameters.SignedEncrypted.Add (
231 new UserNameSecurityTokenParameters ());
233 case MessageCredentialType.Windows:
234 element.ProtectionTokenParameters =
235 new KerberosSecurityTokenParameters ();
237 default: // including .None
238 X509SecurityTokenParameters p =
239 new X509SecurityTokenParameters ();
240 p.X509ReferenceStyle = X509KeyIdentifierClauseType.Thumbprint;
241 element.ProtectionTokenParameters = p;
245 // SecureConversation enabled
247 ChannelProtectionRequirements reqs =
248 new ChannelProtectionRequirements ();
249 // FIXME: fill the reqs
251 return SecurityBindingElement.CreateSecureConversationBindingElement (
252 // FIXME: requireCancellation
253 element, true, reqs);
257 BindingElement CreateTransportSecurity ()
259 switch (Security.Mode) {
260 case SecurityMode.Transport:
261 return new WindowsStreamSecurityBindingElement () {
262 ProtectionLevel = Security.Transport.ProtectionLevel };
264 case SecurityMode.TransportWithMessageCredential:
265 return new SslStreamSecurityBindingElement ();
271 // FIXME: consider Security.Transport.ExtendedProtectionPolicy.
273 switch (Security.Transport.ClientCredentialType) {
274 case TcpClientCredentialType.Windows:
275 return new WindowsStreamSecurityBindingElement () { ProtectionLevel = Security.Transport.ProtectionLevel };
276 case TcpClientCredentialType.Certificate:
277 // FIXME: set RequireClientCertificate and IdentityVerifier depending on other properties, if applicable.
278 return new SslStreamSecurityBindingElement ();
279 default: // includes None
284 bool IBindingRuntimePreferences.ReceiveSynchronously {
285 get { throw new NotImplementedException (); }