Fill NetTcpBinding security implementation a bit.
[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.Tokens;
34 using System.Text;
35 using System.Xml;
36
37 namespace System.ServiceModel
38 {
39         public class NetTcpBinding : Binding, IBindingRuntimePreferences
40         {
41                 int max_conn;
42                 OptionalReliableSession reliable_session;
43                 NetTcpSecurity security;
44                 XmlDictionaryReaderQuotas reader_quotas;
45                 bool transaction_flow;
46                 TransactionProtocol transaction_protocol;
47                 TcpTransportBindingElement transport = new TcpTransportBindingElement ();
48
49                 public NetTcpBinding ()
50                         : this (SecurityMode.Transport)
51                 {
52                 }
53
54                 public NetTcpBinding (SecurityMode securityMode)
55                         : this (securityMode, false)
56                 {
57                 }
58
59                 public NetTcpBinding (SecurityMode securityMode,
60                         bool reliableSessionEnabled)
61                 {
62                         security = new NetTcpSecurity (securityMode);
63                 }
64
65                 public HostNameComparisonMode HostNameComparisonMode {
66                         get { return transport.HostNameComparisonMode; }
67                         set { transport.HostNameComparisonMode = value; }
68                 }
69
70                 public int ListenBacklog {
71                         get { return transport.ListenBacklog; }
72                         set { transport.ListenBacklog = value; }
73                 }
74
75                 public long MaxBufferPoolSize {
76                         get { return transport.MaxBufferPoolSize; }
77                         set { transport.MaxBufferPoolSize = value; }
78                 }
79
80                 public int MaxBufferSize {
81                         get { return transport.MaxBufferSize; }
82                         set { transport.MaxBufferSize = value; }
83                 }
84
85                 [MonoTODO]
86                 public int MaxConnections {
87                         get { return max_conn; }
88                         set { max_conn = value; }
89                 }
90
91                 public long MaxReceivedMessageSize {
92                         get { return transport.MaxReceivedMessageSize; }
93                         set { transport.MaxReceivedMessageSize = value; }
94                 }
95
96                 public bool PortSharingEnabled {
97                         get { return transport.PortSharingEnabled; }
98                         set { transport.PortSharingEnabled = value; }
99                 }
100
101                 [MonoTODO]
102                 public OptionalReliableSession ReliableSession {
103                         get { return reliable_session; }
104                 }
105
106                 public XmlDictionaryReaderQuotas ReaderQuotas {
107                         get { return reader_quotas; }
108                         set { reader_quotas = value; }
109                 }
110
111                 public NetTcpSecurity Security {
112                         get { return security; }
113                 }
114
115                 public EnvelopeVersion EnvelopeVersion {
116                         get { return EnvelopeVersion.Soap12; }
117                 }
118
119                 public TransferMode TransferMode {
120                         get { return transport.TransferMode; }
121                         set { transport.TransferMode = value; }
122                 }
123
124                 public bool TransactionFlow {
125                         get { return transaction_flow; }
126                         set { transaction_flow = value; }
127                 }
128
129                 public TransactionProtocol TransactionProtocol {
130                         get { return transaction_protocol; }
131                         set { transaction_protocol = value; }
132                 }
133
134                 // overrides
135
136                 public override string Scheme {
137                         get { return "net.tcp"; }
138                 }
139
140                 public override BindingElementCollection CreateBindingElements ()
141                 {
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> ();
150                         if (tx != null)
151                                 list.Add (tx);
152                         if (sec != null)
153                                 list.Add (sec);
154                         list.Add (msg);
155                         if (trsec != null)
156                                 list.Add (trsec);
157                         list.Add (tr);
158                         return new BindingElementCollection (list.ToArray ());
159                 }
160
161                 BindingElement GetTransport ()
162                 {
163                         return transport.Clone ();
164                 }
165
166                 // based on WSHttpBinding.CreateMessageSecurity()
167                 SecurityBindingElement CreateMessageSecurity ()
168                 {
169                         if (Security.Mode == SecurityMode.Transport ||
170                             Security.Mode == SecurityMode.None)
171                                 return null;
172
173                         // FIXME: this is wrong. Could be Asymmetric, depends on Security.Message.AlgorithmSuite value.
174                         SymmetricSecurityBindingElement element =
175                                 new SymmetricSecurityBindingElement ();
176
177                         element.MessageSecurityVersion = MessageSecurityVersion.Default;
178
179                         element.SetKeyDerivation (false);
180
181                         switch (Security.Message.ClientCredentialType) {
182                         case MessageCredentialType.Certificate:
183                                 element.EndpointSupportingTokenParameters.Endorsing.Add (
184                                         new X509SecurityTokenParameters ());
185                                 goto default;
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 (),
192                                         GetTransport ());
193                                 element.EndpointSupportingTokenParameters.Endorsing.Add (istp);
194                                 goto default;
195                         case MessageCredentialType.UserName:
196                                 element.EndpointSupportingTokenParameters.SignedEncrypted.Add (
197                                         new UserNameSecurityTokenParameters ());
198                                 goto default;
199                         case MessageCredentialType.Windows:
200                                 element.ProtectionTokenParameters =
201                                         new KerberosSecurityTokenParameters ();
202                                 break;
203                         default: // including .None
204                                 X509SecurityTokenParameters p =
205                                         new X509SecurityTokenParameters ();
206                                 p.X509ReferenceStyle = X509KeyIdentifierClauseType.Thumbprint;
207                                 element.ProtectionTokenParameters = p;
208                                 break;
209                         }
210
211                         return element;
212                 }
213
214                 BindingElement CreateTransportSecurity ()
215                 {
216                         switch (Security.Mode) {
217                         case SecurityMode.None:
218                         case SecurityMode.Message:
219                                 return null;
220                         }
221
222                         // FIXME: consider Security.Transport.ExtendedProtectionPolicy.
223
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
231                                 return null;
232                         }
233                 }
234
235                 bool IBindingRuntimePreferences.ReceiveSynchronously {
236                         get { throw new NotImplementedException (); }
237                 }
238         }
239 }