File: TransportBindingElementImporter.cs

package info (click to toggle)
mono 6.12.0.199%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 1,296,836 kB
  • sloc: cs: 11,181,803; xml: 2,850,076; ansic: 699,709; cpp: 123,344; perl: 59,361; javascript: 30,841; asm: 21,853; makefile: 20,405; sh: 15,009; python: 4,839; pascal: 925; sql: 859; sed: 16; php: 1
file content (347 lines) | stat: -rw-r--r-- 11,622 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
//
// TransportBindingElementImporter.cs
//
// Author:
//       Martin Baulig <martin.baulig@xamarin.com>
//
// Copyright (c) 2012 Xamarin Inc. (http://www.xamarin.com)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

using System;
using System.Net;
using System.Net.Security;
using System.Xml;
using System.Xml.Schema;
using System.Collections.Generic;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;

using WS = System.Web.Services.Description;
using QName = System.Xml.XmlQualifiedName;

namespace System.ServiceModel.Channels {

	public class TransportBindingElementImporter : IWsdlImportExtension, IPolicyImportExtension {
		#region IWsdlImportExtension implementation

		public void BeforeImport (WS.ServiceDescriptionCollection wsdlDocuments, XmlSchemaSet xmlSchemas,
		                          ICollection<XmlElement> policy)
		{
		}

		public void ImportContract (WsdlImporter importer, WsdlContractConversionContext contractContext)
		{
		}

		public void ImportEndpoint (WsdlImporter importer, WsdlEndpointConversionContext context)
		{
			// Only import the binding, not the endpoint.
			if (context.WsdlPort == null)
				return;
			
			DoImportEndpoint (context);
		}

		bool DoImportEndpoint (WsdlEndpointConversionContext context)
		{
			WS.SoapAddressBinding address = null;
			foreach (var extension in context.WsdlPort.Extensions) {
				var check = extension as WS.SoapAddressBinding;
				if (check != null) {
					address = check;
					break;
				}
			}
			
			if (address == null)
				return false;
			
			context.Endpoint.Address = new EndpointAddress (address.Location);
			context.Endpoint.ListenUri = new Uri (address.Location);
			context.Endpoint.ListenUriMode = ListenUriMode.Explicit;
			return true;
		}

		#endregion

		#region IPolicyImportExtension implementation

		public void ImportPolicy (MetadataImporter importer, PolicyConversionContext context)
		{
			var customCtx = context as CustomPolicyConversionContext;
			var customBinding = context.Endpoint.Binding as CustomBinding;
			if ((customCtx == null) || (customBinding == null))
				// FIXME: Should we allow this ?
				throw new InvalidOperationException ();

			var soapHttp = StandardBindingImporter.GetHttpSoapBinding (customCtx.WsdlBinding);
			if (soapHttp != null) {
				if (!ImportHttpPolicy (importer, customCtx, soapHttp))
					context.BindingElements.Add (new HttpTransportBindingElement ());
				return;
			}

			var soapTcp = StandardBindingImporter.GetTcpSoapBinding (customCtx.WsdlBinding);
			if (soapTcp != null) {
				if (!ImportTcpPolicy (importer, customCtx, soapTcp))
					context.BindingElements.Add (new TcpTransportBindingElement ());
				return;
			}
		}

		#endregion

		bool ImportHttpAuthScheme (MetadataImporter importer,
		                           HttpTransportBindingElement bindingElement,
		                           PolicyConversionContext context)
		{
			var assertions = context.GetBindingAssertions ();
			var authSchemes = AuthenticationSchemes.None;

			var httpsTransport = bindingElement as HttpsTransportBindingElement;
			bool certificate = httpsTransport != null ?
				httpsTransport.RequireClientCertificate : false;

			var authElements = PolicyImportHelper.FindAssertionByNS (
				assertions, PolicyImportHelper.HttpAuthNS);
			foreach (XmlElement authElement in authElements) {
				assertions.Remove (authElement);

				if (certificate) {
					importer.AddWarning (
						"Invalid authentication assertion while " +
						"using client certificate: {0}", authElement.OuterXml);
					return false;
				}

				switch (authElement.LocalName) {
				case "BasicAuthentication":
					authSchemes |= AuthenticationSchemes.Basic;
					break;
				case "NtlmAuthentication":
					authSchemes |= AuthenticationSchemes.Ntlm;
					break;
				case "DigestAuthentication":
					authSchemes |= AuthenticationSchemes.Digest;
					break;
				case "NegotiateAuthentication":
					authSchemes |= AuthenticationSchemes.Negotiate;
					break;
				default:
					importer.AddWarning (
						"Invalid policy assertion: {0}", authElement.OuterXml);
					return false;
				}
			}

			bindingElement.AuthenticationScheme = authSchemes;
			return true;
		}

		bool ImportWindowsTransportSecurity (MetadataImporter importer,
		                                     PolicyConversionContext context,
		                                     XmlElement policyElement)
		{
			var protectionLevel = PolicyImportHelper.GetElement (
				importer, policyElement, "ProtectionLevel",
				PolicyImportHelper.FramingPolicyNS, true);
			if (protectionLevel == null) {
				importer.AddWarning (
					"Invalid policy assertion: {0}", policyElement.OuterXml);
				return false;
			}

			var element = new WindowsStreamSecurityBindingElement ();

			switch (protectionLevel.InnerText.ToLowerInvariant ()) {
			case "none":
				element.ProtectionLevel = ProtectionLevel.None;
				break;
			case "sign":
				element.ProtectionLevel = ProtectionLevel.Sign;
				break;
			case "encryptandsign":
				element.ProtectionLevel = ProtectionLevel.EncryptAndSign;
				break;
			default:
				importer.AddWarning (
					"Invalid policy assertion: {0}", protectionLevel.OuterXml);
				return false;
			}

			context.BindingElements.Add (element);
			return true;
		}

		bool ImportTransport (MetadataImporter importer, TransportBindingElement bindingElement,
		                      XmlElement transportPolicy)
		{
			XmlElement algorithmSuite, layout;
			if (!PolicyImportHelper.FindPolicyElement (
				importer, transportPolicy,
				new QName ("AlgorithmSuite", PolicyImportHelper.SecurityPolicyNS),
				false, true, out algorithmSuite) ||
			    !PolicyImportHelper.FindPolicyElement (
				importer, transportPolicy,
				new QName ("Layout", PolicyImportHelper.SecurityPolicyNS),
				false, true, out layout))
				return false;

			bool foundUnknown = false;
			foreach (var node in transportPolicy.ChildNodes) {
				var e = node as XmlElement;
				if (e == null)
					continue;
				importer.AddWarning ("Unknown policy assertion: {0}", e.OuterXml);
				foundUnknown = true;
			}

			return !foundUnknown;
		}

		bool GetTransportToken (MetadataImporter importer, XmlElement transportPolicy,
		                        out XmlElement transportToken)
		{
			return PolicyImportHelper.FindPolicyElement (
				importer, transportPolicy,
				new QName ("TransportToken", PolicyImportHelper.SecurityPolicyNS),
				false, true, out transportToken);
		}

		bool ImportHttpTransport (MetadataImporter importer, PolicyConversionContext context,
		                          XmlElement transportPolicy,
		                          out HttpTransportBindingElement bindingElement)
		{
			XmlElement transportToken;
			if (!GetTransportToken (importer, transportPolicy, out transportToken)) {
				bindingElement = null;
				return false;
			}

			if (transportToken == null) {
				bindingElement = new HttpTransportBindingElement ();
				return true;
			}
			
			bool error;
			var tokenElementList = PolicyImportHelper.GetPolicyElements (transportToken, out error);
			if (error || (tokenElementList.Count != 1)) {
				importer.AddWarning ("Invalid policy assertion: {0}", transportToken.OuterXml);
				bindingElement = null;
				return false;
			}

			var tokenElement = tokenElementList [0];
			if (!PolicyImportHelper.SecurityPolicyNS.Equals (tokenElement.NamespaceURI) ||
				!tokenElement.LocalName.Equals ("HttpsToken")) {
				importer.AddWarning ("Invalid policy assertion: {0}", tokenElement.OuterXml);
				bindingElement = null;
				return false;
			}

			var httpsTransport = new HttpsTransportBindingElement ();
			bindingElement = httpsTransport;

			var certAttr = tokenElement.GetAttribute ("RequireClientCertificate");
			if (!String.IsNullOrEmpty (certAttr))
				httpsTransport.RequireClientCertificate = Boolean.Parse (certAttr);
			return true;
		}

		bool ImportTcpTransport (MetadataImporter importer, PolicyConversionContext context,
		                         XmlElement transportPolicy)
		{
			XmlElement transportToken;
			if (!GetTransportToken (importer, transportPolicy, out transportToken))
				return false;

			if (transportToken == null)
				return true;

			bool error;
			var tokenElementList = PolicyImportHelper.GetPolicyElements (transportToken, out error);
			if (error || (tokenElementList.Count != 1)) {
				importer.AddWarning ("Invalid policy assertion: {0}", transportToken.OuterXml);
				return false;
			}

			var tokenElement = tokenElementList [0];
			if (!PolicyImportHelper.FramingPolicyNS.Equals (tokenElement.NamespaceURI)) {
				importer.AddWarning ("Invalid policy assertion: {0}", tokenElement.OuterXml);
				return false;
			}

			if (tokenElement.LocalName.Equals ("WindowsTransportSecurity")) {
				if (!ImportWindowsTransportSecurity (importer, context, tokenElement))
					return false;
			} else if (tokenElement.LocalName.Equals ("SslTransportSecurity")) {
				context.BindingElements.Add (new SslStreamSecurityBindingElement ());
			}

			return true;
		}

		bool ImportHttpPolicy (MetadataImporter importer, PolicyConversionContext context,
		                       WS.SoapBinding soap)
		{
			HttpTransportBindingElement httpTransport;
			var assertions = context.GetBindingAssertions ();
			var transportPolicy = PolicyImportHelper.GetTransportBindingPolicy (assertions);
			if (transportPolicy != null) {
				if (!ImportHttpTransport (importer, context, transportPolicy, out httpTransport))
					return false;
				if (!ImportTransport (importer, httpTransport, transportPolicy))
					return false;
			} else {
				httpTransport = new HttpTransportBindingElement ();
			}

			if (!ImportHttpAuthScheme (importer, httpTransport, context))
				return false;

			context.BindingElements.Add (httpTransport);
			return true;
		}

		bool ImportTcpPolicy (MetadataImporter importer, PolicyConversionContext context,
		                      WS.Soap12Binding soap)
		{
			var assertions = context.GetBindingAssertions ();

			var tcpTransport = new TcpTransportBindingElement ();

			var transportPolicy = PolicyImportHelper.GetTransportBindingPolicy (assertions);
			if (transportPolicy != null) {
				if (!ImportTcpTransport (importer, context, transportPolicy))
					return false;
				if (!ImportTransport (importer, tcpTransport, transportPolicy))
					return false;
			}

			var streamed = PolicyImportHelper.GetStreamedMessageFramingPolicy (assertions);
			if (streamed != null)
				tcpTransport.TransferMode = TransferMode.Streamed;
			
			context.BindingElements.Add (tcpTransport);
			return true;
		}
	}
}