do not check order sequence if option /order was not used
[mono.git] / mcs / class / System.ServiceModel / System.ServiceModel / NetTcpBinding.cs
1 //
2 // NetTcpBinding.cs
3 //
4 // Author:
5 //      Atsushi Enomoto <atsushi@ximian.com>
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.Text;
36 using System.Xml;
37
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                 bool transaction_flow;
47                 TransactionProtocol transaction_protocol;
48                 TcpTransportBindingElement transport = new TcpTransportBindingElement ();
49
50                 public NetTcpBinding ()
51                         : this (SecurityMode.Transport)
52                 {
53                 }
54
55                 public NetTcpBinding (SecurityMode securityMode)
56                         : this (securityMode, false)
57                 {
58                 }
59
60                 public NetTcpBinding (SecurityMode securityMode,
61                         bool reliableSessionEnabled)
62                 {
63                         security = new NetTcpSecurity (securityMode);
64                 }
65
66                 public HostNameComparisonMode HostNameComparisonMode {
67                         get { return transport.HostNameComparisonMode; }
68                         set { transport.HostNameComparisonMode = value; }
69                 }
70
71                 public int ListenBacklog {
72                         get { return transport.ListenBacklog; }
73                         set { transport.ListenBacklog = value; }
74                 }
75
76                 public long MaxBufferPoolSize {
77                         get { return transport.MaxBufferPoolSize; }
78                         set { transport.MaxBufferPoolSize = value; }
79                 }
80
81                 public int MaxBufferSize {
82                         get { return transport.MaxBufferSize; }
83                         set { transport.MaxBufferSize = value; }
84                 }
85
86                 [MonoTODO]
87                 public int MaxConnections {
88                         get { return max_conn; }
89                         set { max_conn = value; }
90                 }
91
92                 public long MaxReceivedMessageSize {
93                         get { return transport.MaxReceivedMessageSize; }
94                         set { transport.MaxReceivedMessageSize = value; }
95                 }
96
97                 public bool PortSharingEnabled {
98                         get { return transport.PortSharingEnabled; }
99                         set { transport.PortSharingEnabled = value; }
100                 }
101
102                 [MonoTODO]
103                 public OptionalReliableSession ReliableSession {
104                         get { return reliable_session; }
105                 }
106
107                 public XmlDictionaryReaderQuotas ReaderQuotas {
108                         get { return reader_quotas; }
109                         set { reader_quotas = value; }
110                 }
111
112                 public NetTcpSecurity Security {
113                         get { return security; }
114                 }
115
116                 public EnvelopeVersion EnvelopeVersion {
117                         get { return EnvelopeVersion.Soap12; }
118                 }
119
120                 public TransferMode TransferMode {
121                         get { return transport.TransferMode; }
122                         set { transport.TransferMode = value; }
123                 }
124
125                 public bool TransactionFlow {
126                         get { return transaction_flow; }
127                         set { transaction_flow = value; }
128                 }
129
130                 public TransactionProtocol TransactionProtocol {
131                         get { return transaction_protocol; }
132                         set { transaction_protocol = value; }
133                 }
134
135                 // overrides
136
137                 public override string Scheme {
138                         get { return "net.tcp"; }
139                 }
140
141                 public override BindingElementCollection CreateBindingElements ()
142                 {
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> ();
151                         if (tx != null)
152                                 list.Add (tx);
153                         if (sec != null)
154                                 list.Add (sec);
155                         list.Add (msg);
156                         if (trsec != null)
157                                 list.Add (trsec);
158                         list.Add (tr);
159                         return new BindingElementCollection (list.ToArray ());
160                 }
161
162                 BindingElement GetTransport ()
163                 {
164                         return transport.Clone ();
165                 }
166
167                 // It is problematic, but there is no option to disable establishing security context in this binding unlike WSHttpBinding...
168                 SecurityBindingElement CreateMessageSecurity ()
169                 {
170                         if (Security.Mode == SecurityMode.Transport ||
171                             Security.Mode == SecurityMode.None)
172                                 return null;
173
174                         // FIXME: this is wrong. Could be Asymmetric, depends on Security.Message.AlgorithmSuite value.
175                         SymmetricSecurityBindingElement element =
176                                 new SymmetricSecurityBindingElement ();
177
178                         element.MessageSecurityVersion = MessageSecurityVersion.Default;
179
180                         element.SetKeyDerivation (false);
181
182                         switch (Security.Message.ClientCredentialType) {
183                         case MessageCredentialType.Certificate:
184                                 element.EndpointSupportingTokenParameters.Endorsing.Add (
185                                         new X509SecurityTokenParameters ());
186                                 goto default;
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 (),
193                                         GetTransport ());
194                                 element.EndpointSupportingTokenParameters.Endorsing.Add (istp);
195                                 goto default;
196                         case MessageCredentialType.UserName:
197                                 element.EndpointSupportingTokenParameters.SignedEncrypted.Add (
198                                         new UserNameSecurityTokenParameters ());
199                                 goto default;
200                         case MessageCredentialType.Windows:
201                                 element.ProtectionTokenParameters =
202                                         new KerberosSecurityTokenParameters ();
203                                 break;
204                         default: // including .None
205                                 X509SecurityTokenParameters p =
206                                         new X509SecurityTokenParameters ();
207                                 p.X509ReferenceStyle = X509KeyIdentifierClauseType.Thumbprint;
208                                 element.ProtectionTokenParameters = p;
209                                 break;
210                         }
211
212                         // SecureConversation enabled
213
214                         ChannelProtectionRequirements reqs =
215                                 new ChannelProtectionRequirements ();
216                         // FIXME: fill the reqs
217
218                         return SecurityBindingElement.CreateSecureConversationBindingElement (
219                                 // FIXME: requireCancellation
220                                 element, true, reqs);
221                 }
222
223                 BindingElement CreateTransportSecurity ()
224                 {
225                         switch (Security.Mode) {
226                         case SecurityMode.None:
227                         case SecurityMode.Message:
228                                 return null;
229                         }
230
231                         // FIXME: consider Security.Transport.ExtendedProtectionPolicy.
232
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
240                                 return null;
241                         }
242                 }
243
244                 bool IBindingRuntimePreferences.ReceiveSynchronously {
245                         get { throw new NotImplementedException (); }
246                 }
247         }
248 }