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.Tokens;
37 namespace System.ServiceModel
39 public class NetTcpBinding : Binding, IBindingRuntimePreferences
42 OptionalReliableSession reliable_session;
43 NetTcpSecurity security;
44 XmlDictionaryReaderQuotas reader_quotas;
45 bool transaction_flow;
46 TransactionProtocol transaction_protocol;
47 TcpTransportBindingElement transport = new TcpTransportBindingElement ();
49 public NetTcpBinding ()
50 : this (SecurityMode.Transport)
54 public NetTcpBinding (SecurityMode securityMode)
55 : this (securityMode, false)
59 public NetTcpBinding (SecurityMode securityMode,
60 bool reliableSessionEnabled)
62 security = new NetTcpSecurity (securityMode);
65 public HostNameComparisonMode HostNameComparisonMode {
66 get { return transport.HostNameComparisonMode; }
67 set { transport.HostNameComparisonMode = value; }
70 public int ListenBacklog {
71 get { return transport.ListenBacklog; }
72 set { transport.ListenBacklog = value; }
75 public long MaxBufferPoolSize {
76 get { return transport.MaxBufferPoolSize; }
77 set { transport.MaxBufferPoolSize = value; }
80 public int MaxBufferSize {
81 get { return transport.MaxBufferSize; }
82 set { transport.MaxBufferSize = value; }
86 public int MaxConnections {
87 get { return max_conn; }
88 set { max_conn = value; }
91 public long MaxReceivedMessageSize {
92 get { return transport.MaxReceivedMessageSize; }
93 set { transport.MaxReceivedMessageSize = value; }
96 public bool PortSharingEnabled {
97 get { return transport.PortSharingEnabled; }
98 set { transport.PortSharingEnabled = value; }
102 public OptionalReliableSession ReliableSession {
103 get { return reliable_session; }
106 public XmlDictionaryReaderQuotas ReaderQuotas {
107 get { return reader_quotas; }
108 set { reader_quotas = value; }
111 public NetTcpSecurity Security {
112 get { return security; }
115 public EnvelopeVersion EnvelopeVersion {
116 get { return EnvelopeVersion.Soap12; }
119 public TransferMode TransferMode {
120 get { return transport.TransferMode; }
121 set { transport.TransferMode = value; }
124 public bool TransactionFlow {
125 get { return transaction_flow; }
126 set { transaction_flow = value; }
129 public TransactionProtocol TransactionProtocol {
130 get { return transaction_protocol; }
131 set { transaction_protocol = value; }
136 public override string Scheme {
137 get { return "net.tcp"; }
140 public override BindingElementCollection CreateBindingElements ()
142 BindingElement tx = new TransactionFlowBindingElement (TransactionProtocol.WSAtomicTransactionOctober2004);
143 SecurityBindingElement sec = CreateMessageSecurity ();
144 var msg = new BinaryMessageEncodingBindingElement ();
145 if (ReaderQuotas != null)
146 ReaderQuotas.CopyTo (msg.ReaderQuotas);
147 var trsec = CreateTransportSecurity ();
148 BindingElement tr = GetTransport ();
149 List<BindingElement> list = new List<BindingElement> ();
158 return new BindingElementCollection (list.ToArray ());
161 BindingElement GetTransport ()
163 return transport.Clone ();
166 // based on WSHttpBinding.CreateMessageSecurity()
167 SecurityBindingElement CreateMessageSecurity ()
169 if (Security.Mode == SecurityMode.Transport ||
170 Security.Mode == SecurityMode.None)
173 // FIXME: this is wrong. Could be Asymmetric, depends on Security.Message.AlgorithmSuite value.
174 SymmetricSecurityBindingElement element =
175 new SymmetricSecurityBindingElement ();
177 element.MessageSecurityVersion = MessageSecurityVersion.Default;
179 element.SetKeyDerivation (false);
181 switch (Security.Message.ClientCredentialType) {
182 case MessageCredentialType.Certificate:
183 element.EndpointSupportingTokenParameters.Endorsing.Add (
184 new X509SecurityTokenParameters ());
186 case MessageCredentialType.IssuedToken:
187 IssuedSecurityTokenParameters istp =
188 new IssuedSecurityTokenParameters ();
189 // FIXME: issuer binding must be secure.
190 istp.IssuerBinding = new CustomBinding (
191 new TextMessageEncodingBindingElement (),
193 element.EndpointSupportingTokenParameters.Endorsing.Add (istp);
195 case MessageCredentialType.UserName:
196 element.EndpointSupportingTokenParameters.SignedEncrypted.Add (
197 new UserNameSecurityTokenParameters ());
199 case MessageCredentialType.Windows:
200 element.ProtectionTokenParameters =
201 new KerberosSecurityTokenParameters ();
203 default: // including .None
204 X509SecurityTokenParameters p =
205 new X509SecurityTokenParameters ();
206 p.X509ReferenceStyle = X509KeyIdentifierClauseType.Thumbprint;
207 element.ProtectionTokenParameters = p;
214 BindingElement CreateTransportSecurity ()
216 switch (Security.Mode) {
217 case SecurityMode.None:
218 case SecurityMode.Message:
222 // FIXME: consider Security.Transport.ExtendedProtectionPolicy.
224 switch (Security.Transport.ClientCredentialType) {
225 case TcpClientCredentialType.Windows:
226 return new WindowsStreamSecurityBindingElement () { ProtectionLevel = Security.Transport.ProtectionLevel };
227 case TcpClientCredentialType.Certificate:
228 // FIXME: set RequireClientCertificate and IdentityVerifier depending on other properties, if applicable.
229 return new SslStreamSecurityBindingElement ();
230 default: // includes None
235 bool IBindingRuntimePreferences.ReceiveSynchronously {
236 get { throw new NotImplementedException (); }