File: WSTrustMessageConverters.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 (430 lines) | stat: -rw-r--r-- 13,952 bytes parent folder | download | duplicates (12)
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
//
// WSTrustMessageConverters.cs
//
// Author:
//	Atsushi Enomoto  <atsushi@ximian.com>
//
// Copyright (C) 2006-2007 Novell, Inc.
//
// 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.Reflection;
using System.Security.Cryptography;
using System.IdentityModel.Selectors;
using System.IdentityModel.Tokens;
using System.Security.Cryptography.Xml;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.ServiceModel.Security;
using System.ServiceModel.Security.Tokens;
using System.Xml;
using System.Xml.Serialization;

namespace System.ServiceModel.Description
{
	class WSTrustRequestSecurityTokenReader : IDisposable
	{
		WstRequestSecurityToken req = new WstRequestSecurityToken ();
		XmlDictionaryReader reader;
		SecurityTokenSerializer serializer;

		public WSTrustRequestSecurityTokenReader (XmlDictionaryReader reader, SecurityTokenSerializer serializer)
		{
			this.reader = reader;
			this.serializer = serializer;
		}

		public WstRequestSecurityToken Value {
			get { return req; }
		}

		string LineInfo ()
		{
			IXmlLineInfo li = reader as IXmlLineInfo;
			return li != null && li.HasLineInfo () ?
				String.Format ("({0},{1})", li.LineNumber, li.LinePosition) : String.Empty;
		}

		public void Dispose ()
		{
			reader.Close ();
		}

		public WstRequestSecurityToken Read ()
		{
			reader.MoveToContent ();
			req.Context = reader.GetAttribute ("Context");
			reader.ReadStartElement ("RequestSecurityToken", Constants.WstNamespace);
			do {
				reader.MoveToContent ();
				switch (reader.NodeType) {
				case XmlNodeType.EndElement:
					reader.Read (); // consume RequestSecurityToken end element.
					return req;
				case XmlNodeType.Element:
					ReadTokenContent ();
					break;
				default:
					throw new XmlException (String.Format ("Unexpected request XML {0} node, name {1}{2}", reader.NodeType, reader.Name, LineInfo ()));
				}
			} while (true);
		}

		void ReadTokenContent ()
		{
			switch (reader.NamespaceURI) {
			case Constants.WstNamespace:
				switch (reader.LocalName) {
				case "RequestType":
					req.RequestType = reader.ReadElementContentAsString ();
					return;
				case "Entropy":
					ReadEntropy ();
					return;
				case "KeySize":
					req.KeySize = reader.ReadElementContentAsInt ();
					return;
				case "KeyType":
					req.KeyType = reader.ReadElementContentAsString ();
					return;
				case "TokenType":
					string tokenType = reader.ReadElementContentAsString ();
					if (tokenType != Constants.WsscContextToken)
						throw new SecurityTokenException (String.Format ("Unexpected TokenType: {0}", tokenType));
					return;
				case "ComputedKeyAlgorithm":
					req.ComputedKeyAlgorithm = reader.ReadElementContentAsString ();
					return;
				case "BinaryExchange":
					ReadBinaryExchange ();
					return;
				}
				break;
			case Constants.WspNamespace:
				switch (reader.LocalName) {
				case "AppliesTo":
					ReadAppliesTo ();
					return;
				}
				break;
			}
			throw new XmlException (String.Format ("Unexpected RequestSecurityToken content element. Name is {0} and namespace URI is {1}{2}", reader.Name, reader.NamespaceURI, LineInfo ()));
		}

		protected void ReadBinaryExchange ()
		{
			if (reader.IsEmptyElement)
				throw new XmlException (String.Format ("Binary content is expected in 'BinaryExchange' element.{0}", LineInfo ()));
			WstBinaryExchange b = new WstBinaryExchange (reader.GetAttribute ("ValueType"));
			b.EncodingType = reader.GetAttribute ("EncodingType");
			b.Value = Convert.FromBase64String (reader.ReadElementContentAsString ());
			req.BinaryExchange = b;
		}

		void ReadEntropy ()
		{
			if (reader.IsEmptyElement)
				throw new XmlException (String.Format ("WS-Trust Entropy element is empty.{2}", LineInfo ()));
			reader.ReadStartElement ("Entropy", Constants.WstNamespace);
			reader.MoveToContent ();

			req.Entropy = serializer.ReadToken (reader, null);
			// after reading a token, </Entropy> should follow.
			reader.MoveToContent ();
			reader.ReadEndElement ();
		}

		void ReadAppliesTo ()
		{
			WspAppliesTo aTo = new WspAppliesTo ();

			if (reader.IsEmptyElement)
				throw new XmlException (String.Format ("WS-Policy AppliesTo element is empty.{2}", LineInfo ()));
			reader.ReadStartElement ();
			reader.MoveToContent ();
			reader.ReadStartElement ("EndpointReference", Constants.WsaNamespace);
			reader.MoveToContent ();
			WsaEndpointReference er = new WsaEndpointReference ();
			er.Address = reader.ReadElementContentAsString ("Address", Constants.WsaNamespace);
			reader.MoveToContent ();
			reader.ReadEndElement (); // </EndpointReference>
			aTo.EndpointReference = er;
			reader.MoveToContent ();
			reader.ReadEndElement (); // </wsp:AppliesTo>
			req.AppliesTo = aTo;
		}
	}

	// FIXME: it might be extraneous - currently used only for IssuedToken
	class WstRequestSecurityTokenWriter : BodyWriter
	{
		WstRequestSecurityToken value;
		SecurityTokenSerializer serializer;

		public WstRequestSecurityTokenWriter (WstRequestSecurityToken value, SecurityTokenSerializer serializer)
			: base (true)
		{
			this.value = value;
			this.serializer = serializer;
		}

		protected override void OnWriteBodyContents (XmlDictionaryWriter w)
		{
			w.WriteStartElement ("RequestSecurityToken", Constants.WstNamespace);
			w.WriteEndElement ();
		}
	}

	class WSTrustRequestSecurityTokenResponseReader : IDisposable
	{
		string negotiation_type;
		WstRequestSecurityTokenResponse res;
		XmlDictionaryReader reader;
		SecurityTokenSerializer serializer;
		SecurityTokenResolver resolver;

		public WSTrustRequestSecurityTokenResponseReader (
			string negotiationType,
			XmlDictionaryReader reader,
			SecurityTokenSerializer serializer,
			SecurityTokenResolver resolver)
		{
			this.negotiation_type = negotiationType;
			this.reader = reader;
			this.serializer = serializer;
			this.resolver = resolver;
			res = new WstRequestSecurityTokenResponse (serializer);
		}

		public WstRequestSecurityTokenResponse Value {
			get { return res; }
		}

		string LineInfo ()
		{
			IXmlLineInfo li = reader as IXmlLineInfo;
			return li != null && li.HasLineInfo () ?
				String.Format ("({0},{1})", li.LineNumber, li.LinePosition) : String.Empty;
		}

		public void Dispose ()
		{
			reader.Close ();
		}

		public WstRequestSecurityTokenResponse Read ()
		{
			reader.MoveToContent ();
			res.Context = reader.GetAttribute ("Context");
			reader.ReadStartElement ("RequestSecurityTokenResponse", Constants.WstNamespace);
			do {
				reader.MoveToContent ();
				switch (reader.NodeType) {
				case XmlNodeType.EndElement:
					reader.Read (); // consume RequestSecurityTokenResponse end element.
					return res;
				case XmlNodeType.Element:
					ReadTokenContent ();
					break;
				default:
					throw new XmlException (String.Format ("Unexpected request XML {0} node, name {1}{2}", reader.NodeType, reader.Name, LineInfo ()));
				}
			} while (true);
		}

		void ReadTokenContent ()
		{
			switch (reader.NamespaceURI) {
			case Constants.WstNamespace:
				switch (reader.LocalName) {
				case "RequestedSecurityToken":
					res.RequestedSecurityToken = (SecurityContextSecurityToken) ReadToken ();
					return;
				case "RequestedProofToken":
#if true // FIXME: we can't handle it right now
					string ens = EncryptedXml.XmlEncNamespaceUrl;
					reader.Read ();
					reader.ReadStartElement ("EncryptedKey", ens);
					string alg = reader.GetAttribute ("Algorithm");
					bool isEmpty = reader.IsEmptyElement;
					reader.ReadStartElement ("EncryptionMethod", ens);
					if (alg != negotiation_type)
						throw new XmlException (String.Format ("EncryptionMethod '{0}' is not supported in RequestedProofToken.", alg));
					if (!isEmpty)
						reader.ReadEndElement ();
					reader.ReadStartElement ("CipherData", ens);
					reader.ReadStartElement ("CipherValue", ens);
					byte [] pt = reader.ReadContentAsBase64 ();
					res.RequestedProofToken = pt;
					reader.ReadEndElement ();
					reader.ReadEndElement ();
					reader.ReadEndElement ();// EncryptedKey
					reader.ReadEndElement ();// RPT
#else
					reader.Read ();
					reader.MoveToContent ();
					if (serializer.CanReadToken (reader))
						res.RequestedProofToken = serializer.ReadToken (reader, resolver);
					else
						res.RequestedProofToken = serializer.ReadKeyIdentifierClause (reader);
					reader.ReadEndElement ();
#endif
					return;
				case "BinaryExchange":
					ReadBinaryExchange ();
					return;
				case "TokenType":
					res.TokenType = reader.ReadElementContentAsString ();
					return;
				case "Lifetime":
					ReadLifetime ();
					return;
				case "KeySize":
					res.KeySize = reader.ReadElementContentAsInt ();
					return;
				case "RequestedAttachedReference":
					res.RequestedAttachedReference = ReadTokenReference ();
					return;
				case "RequestedUnattachedReference":
					res.RequestedUnattachedReference = ReadTokenReference ();
					return;
				case "Authenticator":
					ReadAuthenticator ();
					return;
				}
				break;
			}
			throw new XmlException (String.Format ("Unexpected RequestSecurityTokenResponse content element. Name is {0} and namespace URI is {1} {2}", reader.Name, reader.NamespaceURI, LineInfo ()));
		}

		void ReadAuthenticator ()
		{
			if (reader.IsEmptyElement)
				throw new XmlException (String.Format ("WS-Trust 'Authenticator' element is expected to have contents. {0}", LineInfo ()));
			reader.Read ();
			reader.MoveToContent ();
			res.Authenticator = Convert.FromBase64String (reader.ReadElementContentAsString ("CombinedHash", Constants.WstNamespace));
			reader.ReadEndElement ();
		}

		void ReadLifetime ()
		{
			WstLifetime lt = new WstLifetime ();
			res.Lifetime = lt;
			if (reader.IsEmptyElement)
				throw new XmlException (String.Format ("WS-Trust 'Lifetime' element is expected to have contents. {0}", LineInfo ()));
			reader.Read ();
			while (true) {
				reader.MoveToContent ();
				if (reader.NodeType != XmlNodeType.Element)
					break;
				if (reader.NamespaceURI == Constants.WsuNamespace) {
					switch (reader.LocalName) {
					case "Created":
						lt.Created = XmlConvert.ToDateTime (reader.ReadElementContentAsString (), XmlDateTimeSerializationMode.RoundtripKind);
						continue;
					case "Expires":
						lt.Expires = XmlConvert.ToDateTime (reader.ReadElementContentAsString (), XmlDateTimeSerializationMode.RoundtripKind);
						continue;
					}
				}
				throw new XmlException (String.Format ("Unexpected Lifetime content. Name is {0} and namespace URI is {1} {2}", reader.Name, reader.NamespaceURI, LineInfo ()));
			}
			reader.ReadEndElement ();
		}

		SecurityToken ReadToken ()
		{
			if (reader.IsEmptyElement)
				throw new XmlException (String.Format ("Security token body is expected in '{0}' element. {1}", reader.LocalName, LineInfo ()));
			reader.Read ();
			reader.MoveToContent ();
			SecurityToken token = serializer.ReadToken (reader, resolver);
			reader.ReadEndElement ();
			return token;
		}

		SecurityKeyIdentifierClause ReadTokenReference ()
		{
			if (reader.IsEmptyElement)
				throw new XmlException (String.Format ("Content is expected in 'RequestedAttachedReference' element. {0}", LineInfo ()));
			reader.Read ();
			SecurityKeyIdentifierClause ret = serializer.ReadKeyIdentifierClause (reader);
			reader.ReadEndElement ();
			return ret;
		}

		void ReadBinaryExchange ()
		{
			if (reader.IsEmptyElement)
				throw new XmlException (String.Format ("Binary content is expected in 'BinaryExchange' element.{0}", LineInfo ()));
			WstBinaryExchange b = new WstBinaryExchange (reader.GetAttribute ("ValueType"));
			b.EncodingType = reader.GetAttribute ("EncodingType");
			b.Value = Convert.FromBase64String (reader.ReadElementContentAsString ());
			res.BinaryExchange = b;
		}
	}

	// FIXME: it might be extraneous - currently unused
	internal class WstRequestSecurityTokenResponseWriter : BodyWriter
	{
		WstRequestSecurityTokenResponse res;
		SecurityTokenSerializer serializer;

		public WstRequestSecurityTokenResponseWriter (WstRequestSecurityTokenResponse res, SecurityTokenSerializer serializer)
			: base (true)
		{
			this.res = res;
			this.serializer = serializer;
		}

		protected override void OnWriteBodyContents (XmlDictionaryWriter writer)
		{
try {
			writer.WriteStartElement ("RequestSecurityTokenResponse", Constants.WstNamespace);

			// RequestedSecurityToken
			writer.WriteStartElement ("RequestedSecurityToken", Constants.WstNamespace);

			serializer.WriteToken (writer, res.RequestedSecurityToken);

			writer.WriteEndElement ();

/*
			// Entropy
			writer.WriteStartElement ("Entropy", Constants.WstNamespace);
			// FIXME: keep generated key
			serializer.WriteToken (writer,
				new BinarySecretSecurityToken (Rijndael.Create ().Key));
			writer.WriteEndElement ();
*/

			writer.WriteEndElement ();
} catch (Exception ex) {
Console.WriteLine (ex);
throw;
}
		}
	}
}