File: WSSecurityMessageHeader.cs

package info (click to toggle)
mono 6.12.0.199%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: sid, 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 (550 lines) | stat: -rw-r--r-- 18,069 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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
//
// WSSecurityMessageHeader.cs
//
// Author:
//	Atsushi Enomoto  <atsushi@ximian.com>
//
// Copyright (C) 2006-2007 Novell, Inc (http://www.novell.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.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.IO;
using System.IdentityModel.Selectors;
using System.IdentityModel.Tokens;
using System.Runtime.Serialization;
using System.Security.Cryptography;
using System.Security.Cryptography.Xml;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Dispatcher;
using System.ServiceModel.Security;
using System.ServiceModel.Security.Tokens;
using System.Text;
using System.Xml;

namespace System.ServiceModel.Channels.Security
{
	internal class WSSecurityMessageHeaderReader
	{
		public WSSecurityMessageHeaderReader (WSSecurityMessageHeader header, SecurityTokenSerializer serializer, SecurityTokenResolver resolver, XmlDocument doc, XmlNamespaceManager nsmgr, List<SecurityToken> tokens)
		{
			this.header = header;
			this.serializer = serializer;
			this.resolver = resolver;
			this.doc = doc;
			this.nsmgr = nsmgr;
			this.tokens = tokens;
		}

		WSSecurityMessageHeader header;
		SecurityTokenSerializer serializer;
		SecurityTokenResolver resolver;
		XmlDocument doc;
		XmlNamespaceManager nsmgr;
		List<SecurityToken> tokens;
		Dictionary<string, EncryptedData> encryptedDataList =
			new Dictionary<string, EncryptedData> ();

		public void ReadContents (XmlReader reader)
		{
			DerivedKeySecurityToken currentToken = null;

			reader.MoveToContent ();
			reader.ReadStartElement ("Security", Constants.WssNamespace);
			do {
				reader.MoveToContent ();
				if (reader.NodeType == XmlNodeType.EndElement)
					break;
				object o = ReadContent (reader);
				if (o is EncryptedData) {
					EncryptedData ed = (EncryptedData) o;
					encryptedDataList [ed.Id] = ed;
				}
				else if (o is ReferenceList && currentToken != null)
					currentToken.ReferenceList = (ReferenceList) o;
				else if (o is SecurityToken) {
					if (o is DerivedKeySecurityToken)
						currentToken = o as DerivedKeySecurityToken;
					tokens.Add ((SecurityToken) o);
				}
				header.Contents.Add (o);
			} while (true);
			reader.ReadEndElement ();
		}

		object ReadContent (XmlReader reader)
		{
			reader.MoveToContent ();
			if (reader.NodeType != XmlNodeType.Element)
				throw new XmlException (String.Format ("Node type {0} is not expected as a WS-Security message header content.", reader.NodeType));
			switch (reader.NamespaceURI) {
			case Constants.WsuNamespace:
				switch (reader.LocalName) {
				case "Timestamp":
					return ReadTimestamp (reader);
				}
				break;
			//case Constants.WstNamespace:
			case Constants.Wss11Namespace:
				if (reader.LocalName == "SignatureConfirmation") {
					return ReadSignatureConfirmation (reader, doc);
				}
				break;
			case SignedXml.XmlDsigNamespaceUrl:
				switch (reader.LocalName) {
				case "Signature":
					WSSignedXml sxml = new WSSignedXml (doc);
					sxml.Signature.LoadXml ((XmlElement) doc.ReadNode (reader));
					UpdateSignatureKeyInfo (sxml.Signature, doc, serializer);
					return sxml;
				}
				break;
			case EncryptedXml.XmlEncNamespaceUrl:
				switch (reader.LocalName) {
				case "EncryptedData":
					XmlElement el = (XmlElement) doc.ReadNode (reader);
					return CreateEncryptedData (el);
				case "ReferenceList":
					ReferenceList rl = new ReferenceList ();
					reader.Read ();
					for (reader.MoveToContent ();
					     reader.NodeType != XmlNodeType.EndElement;
					     reader.MoveToContent ()) {
						switch (reader.LocalName) {
						case "DataReference":
							DataReference dref = new DataReference ();
							dref.LoadXml ((XmlElement) doc.ReadNode (reader));
							rl.Add (dref);
							continue;
						case "KeyReference":
							KeyReference kref = new KeyReference ();
							kref.LoadXml ((XmlElement) doc.ReadNode (reader));
							rl.Add (kref);
							continue;
						}
						throw new XmlException (String.Format ("Unexpected {2} node '{0}' in namespace '{1}' in ReferenceList.", reader.Name, reader.NamespaceURI, reader.NodeType));
					}
					reader.ReadEndElement ();
					return rl;
				}
				break;
			}
			// SecurityTokenReference will be handled here.
			// This order (Token->KeyIdentifierClause) is
			// important because WrappedKey could be read
			// in both context (but must be a token here).
			if (serializer.CanReadToken (reader))
				return serializer.ReadToken (reader, resolver);
			else if (serializer.CanReadKeyIdentifierClause (reader))
				return serializer.ReadKeyIdentifierClause (reader);
			else
				throw new XmlException (String.Format ("Unexpected element '{0}' in namespace '{1}' as a WS-Security message header content.", reader.Name, reader.NamespaceURI));
		}

		void UpdateSignatureKeyInfo (Signature sig, XmlDocument doc, SecurityTokenSerializer serializer)
		{
			KeyInfo ki = new KeyInfo ();
			ki.Id = sig.KeyInfo.Id;
			foreach (KeyInfoClause kic in sig.KeyInfo) {
				SecurityTokenReferenceKeyInfo r = new SecurityTokenReferenceKeyInfo (serializer, doc);
				r.LoadXml (kic.GetXml ());
				ki.AddClause (r);
			}
			sig.KeyInfo = ki;
		}

		#region Decryption

		// returns the protection token
		public void DecryptSecurity (SecureMessageDecryptor decryptor, SymmetricSecurityKey sym, byte [] dummyEncKey)
		{
			WSEncryptedXml encXml = new WSEncryptedXml (doc);

			// default, unless overriden by the default DerivedKeyToken.
			Rijndael aes = RijndaelManaged.Create (); // it is reused with every key
			aes.Mode = CipherMode.CBC;

			if (sym == null)
				throw new MessageSecurityException ("Cannot find the encryption key in this message and context");

			// decrypt the body with the decrypted key
			Collection<string> references = new Collection<string> ();

			foreach (ReferenceList rlist in header.FindAll<ReferenceList> ())
				foreach (EncryptedReference encref in rlist)
					references.Add (StripUri (encref.Uri));

			foreach (WrappedKeySecurityToken wk in header.FindAll<WrappedKeySecurityToken> ())
				foreach (EncryptedReference er in wk.ReferenceList)
					references.Add (StripUri (er.Uri));

			Collection<XmlElement> list = new Collection<XmlElement> ();
			foreach (string uri in references) {
				XmlElement el = encXml.GetIdElement (doc, uri);
				if (el != null)
					list.Add (el);
				else
					throw new MessageSecurityException (String.Format ("On decryption, EncryptedData with Id '{0}', referenced by ReferenceData, was not found.", uri));
			}

			foreach (XmlElement el in list) {
				EncryptedData ed2 = CreateEncryptedData (el);
				byte [] key = GetEncryptionKeyForData (ed2, encXml, dummyEncKey);
				aes.Key = key != null ? key : sym.GetSymmetricKey ();
				byte [] decrypted = DecryptData (encXml, ed2, aes);
				encXml.ReplaceData (el, decrypted);
				EncryptedData existing;
				// if it was a header content, replace 
				// corresponding one.
				if (encryptedDataList.TryGetValue (ed2.Id, out existing)) {
					// FIXME: it is kind of extraneous and could be replaced by XmlNodeReader
//Console.WriteLine ("DECRYPTED EncryptedData:");
//Console.WriteLine (Encoding.UTF8.GetString (decrypted));
					object o = ReadContent (XmlReader.Create (new MemoryStream (decrypted)));
					header.Contents.Remove (existing);
					header.Contents.Add (o);
				}
			}
/*
Console.WriteLine ("======== Decrypted Document ========");
doc.PreserveWhitespace = false;
doc.Save (Console.Out);
doc.PreserveWhitespace = true;
*/
		}

		EncryptedData CreateEncryptedData (XmlElement el)
		{
			EncryptedData ed = new EncryptedData ();
			ed.LoadXml (el);
			if (ed.Id == null)
				ed.Id = el.GetAttribute ("Id", Constants.WsuNamespace);
			return ed;
		}

		byte [] GetEncryptionKeyForData (EncryptedData ed2, EncryptedXml encXml, byte [] dummyEncKey)
		{
			// Since ReferenceList could be embedded directly in wss_header without
			// key indication, it must iterate all the derived keys to find out
			// appropriate one.
			foreach (DerivedKeySecurityToken dk in header.FindAll<DerivedKeySecurityToken> ()) {
				if (dk.ReferenceList == null)
					continue;
				foreach (DataReference dr in dk.ReferenceList)
					if (StripUri (dr.Uri) == ed2.Id)
						return ((SymmetricSecurityKey) dk.SecurityKeys [0]).GetSymmetricKey ();
			}
			foreach (WrappedKeySecurityToken wk in header.FindAll<WrappedKeySecurityToken> ()) {
				if (wk.ReferenceList == null)
					continue;
				foreach (DataReference dr in wk.ReferenceList)
					if (StripUri (dr.Uri) == ed2.Id)
						return ((SymmetricSecurityKey) wk.SecurityKeys [0]).GetSymmetricKey ();
			}

			if (ed2.KeyInfo == null)
				return null;
			foreach (KeyInfoClause kic in ed2.KeyInfo) {
				SecurityKeyIdentifierClause skic = serializer.ReadKeyIdentifierClause (new XmlNodeReader (kic.GetXml ()));

				SecurityKey skey = null;
				if (!resolver.TryResolveSecurityKey (skic, out skey))
					throw new MessageSecurityException (String.Format ("The signing key could not be resolved from {0}", skic));
				SymmetricSecurityKey ssk = skey as SymmetricSecurityKey;
				if (ssk != null)
					return ssk.GetSymmetricKey ();
			}
			return null; // no applicable key info clause.
		}

		// Probably it is a bug in .NET, but sometimes it does not contain
		// proper padding bytes. For such cases, use PaddingMode.None
		// instead. It must not be done in EncryptedXml class as it
		// correctly rejects improper ISO10126 padding.
		byte [] DecryptData (EncryptedXml encXml, EncryptedData ed, SymmetricAlgorithm symAlg)
		{
			PaddingMode bak = symAlg.Padding;
			try {
				byte [] bytes = ed.CipherData.CipherValue;

				if (encXml.Padding != PaddingMode.None &&
				    encXml.Padding != PaddingMode.Zeros &&
				    bytes [bytes.Length - 1] > symAlg.BlockSize / 8)
					symAlg.Padding = PaddingMode.None;
				return encXml.DecryptData (ed, symAlg);
			} finally {
				symAlg.Padding = bak;
			}
		}

		string StripUri (string src)
		{
			if (src == null || src.Length == 0)
				return String.Empty;
			if (src [0] != '#')
				throw new NotSupportedException (String.Format ("Non-fragment URI in DataReference and KeyReference is not supported: '{0}'", src));
			return src.Substring (1);
		}
		#endregion

		static Wss11SignatureConfirmation ReadSignatureConfirmation (XmlReader reader, XmlDocument doc)
		{
			string id = reader.GetAttribute ("Id", Constants.WsuNamespace);
			string value = reader.GetAttribute ("Value");
			reader.Skip ();
			return new Wss11SignatureConfirmation (id, value);
		}

		static WsuTimestamp ReadTimestamp (XmlReader reader)
		{
			WsuTimestamp ret = new WsuTimestamp ();
			ret.Id = reader.GetAttribute ("Id", Constants.WsuNamespace);
			reader.ReadStartElement ();
			do {
				reader.MoveToContent ();
				if (reader.NodeType == XmlNodeType.EndElement)
					break;
				if (reader.NodeType != XmlNodeType.Element)
					throw new XmlException (String.Format ("Node type {0} is not expected as a WS-Security 'Timestamp' content.", reader.NodeType));
				switch (reader.NamespaceURI) {
				case Constants.WsuNamespace:
					switch (reader.LocalName) {
					case "Created":
						ret.Created = (DateTime) reader.ReadElementContentAs (typeof (DateTime), null);
						continue;
					case "Expires":
						ret.Expires = (DateTime) reader.ReadElementContentAs (typeof (DateTime), null);
						continue;
					}
					break;
				}
				throw new XmlException (String.Format ("Unexpected element '{0}' in namespace '{1}' as a WS-Security message header content.", reader.Name, reader.NamespaceURI));
			} while (true);

			reader.ReadEndElement (); // </u:Timestamp>
			return ret;
		}
	}

	internal class WSSecurityMessageHeader : MessageHeader
	{
		public WSSecurityMessageHeader (SecurityTokenSerializer serializer)
		{
			this.serializer = serializer;
		}

		SecurityTokenSerializer serializer;
		Collection<object> contents = new Collection<object> ();

		// Timestamp, BinarySecurityToken, EncryptedKey,
		// [DerivedKeyToken]*, ReferenceList, EncryptedData
		public Collection<object> Contents {
			get { return contents; }
		}

		public override bool MustUnderstand {
			get { return true; }
		}

		public override string Name {
			get { return "Security"; }
		}

		public override string Namespace {
			get { return Constants.WssNamespace; }
		}

		public void AddContent (object obj)
		{
			if (obj == null)
				throw new ArgumentNullException ("obj");
			Contents.Add (obj);
		}

		public T Find<T> ()
		{
			foreach (object o in Contents)
				if (typeof (T).IsAssignableFrom (o.GetType ()))
					return (T) o;
			return default (T);
		}

		public Collection<T> FindAll<T> ()
		{
			Collection<T> c = new Collection<T> ();
			foreach (object o in Contents)
				if (typeof (T).IsAssignableFrom (o.GetType ()))
					c.Add ((T) o);
			return c;
		}

		protected override void OnWriteStartHeader (XmlDictionaryWriter writer, MessageVersion version)
		{
			writer.WriteStartElement ("o", this.Name, this.Namespace);
			WriteHeaderAttributes (writer, version);
		}

		protected override void OnWriteHeaderContents (XmlDictionaryWriter writer, MessageVersion version)
		{
			// FIXME: it should use XmlDictionaryWriter that CanCanonicalize the output (which is not possible in any built-in writer types, so we'll have to hack it).

			foreach (object obj in Contents) {
				if (obj is WsuTimestamp) {
					WsuTimestamp ts = (WsuTimestamp) obj;
					ts.WriteTo (writer);
				} else if (obj is SecurityToken) {
					serializer.WriteToken (writer, (SecurityToken) obj);
				} else if (obj is EncryptedKey) {
					((EncryptedKey) obj).GetXml ().WriteTo (writer);
				} else if (obj is ReferenceList) {
					writer.WriteStartElement ("ReferenceList", EncryptedXml.XmlEncNamespaceUrl);
					foreach (EncryptedReference er in (ReferenceList) obj)
						er.GetXml ().WriteTo (writer);
					writer.WriteEndElement ();
				} else if (obj is EncryptedData) {
					((EncryptedData) obj).GetXml ().WriteTo (writer);
				} else if (obj is Signature) {
					((Signature) obj).GetXml ().WriteTo (writer);
				} else if (obj is Wss11SignatureConfirmation) {
					Wss11SignatureConfirmation sc = (Wss11SignatureConfirmation) obj;
					writer.WriteStartElement ("k", "SignatureConfirmation", Constants.Wss11Namespace);
					writer.WriteAttributeString ("u", "Id", Constants.WsuNamespace, sc.Id);
					writer.WriteAttributeString ("Value", sc.Value);
					writer.WriteEndElement ();
				}
				else
					throw new ArgumentException (String.Format ("Unrecognized header item {0}", obj ?? "(null)"));
			}
		}
	}

	internal class WsuTimestamp
	{
		string id;
		DateTime created, expires;

		public string Id {
			get { return id; }
			set { id = value; }
		}

		public DateTime Created {
			get { return created; }
			set { created = value; }
		}

		public DateTime Expires {
			get { return expires; }
			set { expires = value; }
		}

		public void WriteTo (XmlWriter writer)
		{
			writer.WriteStartElement ("u", "Timestamp", Constants.WsuNamespace);
			writer.WriteAttributeString ("u", "Id", Constants.WsuNamespace, Id);
			writer.WriteStartElement ("u", "Created", Constants.WsuNamespace);
			writer.WriteValue (FormatAsUtc (Created));
			writer.WriteEndElement ();
			writer.WriteStartElement ("u", "Expires", Constants.WsuNamespace);
			writer.WriteValue (FormatAsUtc (Expires));
			writer.WriteEndElement ();
			writer.WriteEndElement ();
		}

		string FormatAsUtc (DateTime date)
		{
			return date.ToUniversalTime ().ToString (
				"yyyy-MM-dd'T'HH:mm:ss.fff'Z'",
				CultureInfo.InvariantCulture);
		}
	}

	internal class SecurityTokenReferenceKeyInfo : KeyInfoClause
	{
		SecurityKeyIdentifierClause clause;
		SecurityTokenSerializer serializer;
		XmlDocument doc;

		// for LoadXml()
		public SecurityTokenReferenceKeyInfo (
			SecurityTokenSerializer serializer,
			XmlDocument doc)
			: this (null, serializer, doc)
		{
		}

		// for GetXml()
		public SecurityTokenReferenceKeyInfo (
			SecurityKeyIdentifierClause clause,
			SecurityTokenSerializer serializer,
			XmlDocument doc)
		{
			this.clause = clause;
			this.serializer = serializer;
			if (doc == null)
				doc = new XmlDocument ();
			this.doc = doc;
		}

		public SecurityKeyIdentifierClause Clause {
			get { return clause; }
		}

		public override XmlElement GetXml ()
		{
			XmlDocumentFragment df = doc.CreateDocumentFragment ();
			XmlWriter w = df.CreateNavigator ().AppendChild ();
			serializer.WriteKeyIdentifierClause (w, clause);
			w.Close ();
			return (XmlElement) df.FirstChild;
		}

		public override void LoadXml (XmlElement element)
		{
			clause = serializer.ReadKeyIdentifierClause (new XmlNodeReader (element));
		}
	}

	internal class Wss11SignatureConfirmation
	{
		string id, value;

		public Wss11SignatureConfirmation (string id, string value)
		{
			this.id = id;
			this.value = value;
		}

		public string Id {
			get { return id; }
			set { id = value; }
		}

		public string Value {
			get { return value; }
			set { this.value = value; }
		}
	}
}