Welcome to mirror list, hosted at ThFree Co, Russian Federation.

XmlCanonicalizer.cs « Mono.Xml « System.Security « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0ca433dd0c5755234f971e53c7487df64137e9fb (plain)
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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
//
// XmlCanonicalizer.cs - C14N implementation for XML Signature
// http://www.w3.org/TR/xml-c14n
//
// Author:
//	Aleksey Sanin (aleksey@aleksey.com)
//
// (C) 2003 Aleksey Sanin (aleksey@aleksey.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;
using System.IO;
using System.Text;
using System.Xml;

namespace Mono.Xml { 

	internal class XmlCanonicalizer {

		private enum XmlCanonicalizerState
		{
			BeforeDocElement,
			InsideDocElement,
			AfterDocElement
		}
		
		// c14n parameters
		private bool comments;
		private bool exclusive;
		string inclusiveNamespacesPrefixList;

		// input/output
		private XmlNodeList xnl;
		private StringBuilder res;
		
		// namespaces rendering stack
		private XmlCanonicalizerState state;
		private ArrayList visibleNamespaces;
		private int prevVisibleNamespacesStart;
		private int prevVisibleNamespacesEnd;

		public XmlCanonicalizer (bool withComments, bool excC14N)
		{	    
			res = new StringBuilder ();
			comments = withComments;
			exclusive = excC14N;
			state = XmlCanonicalizerState.BeforeDocElement;
			visibleNamespaces = new ArrayList ();
			prevVisibleNamespacesStart = 0;
			prevVisibleNamespacesEnd = 0;
		}
		
		public Stream Canonicalize (XmlDocument doc)
		{
			WriteDocumentNode (doc);
			
			UTF8Encoding utf8 = new UTF8Encoding ();
			byte[] data = utf8.GetBytes (res.ToString ());
			return new MemoryStream (data);
		}
		
		public Stream Canonicalize (XmlNodeList nodes)
		{
			xnl = nodes;
			if (nodes == null || nodes.Count < 1)
				return new MemoryStream ();
			return Canonicalize (nodes[0].OwnerDocument);
		}		

		// See xml-enc-c14n specification
		public string InclusiveNamespacesPrefixList {
			get { return inclusiveNamespacesPrefixList; }
			set { inclusiveNamespacesPrefixList = value; }
		}

		private void WriteNode (XmlNode node)
		{
			// Console.WriteLine ("C14N Debug: node=" + node.Name);

			bool visible = IsNodeVisible (node);
			switch (node.NodeType) {
			case XmlNodeType.Document:
			case XmlNodeType.DocumentFragment:
				WriteDocumentNode (node);
				break;
			case XmlNodeType.Element:
				WriteElementNode (node, visible);
				break;
			case XmlNodeType.CDATA:
			case XmlNodeType.SignificantWhitespace:
			case XmlNodeType.Text:
        			// CDATA sections are processed as text nodes
				WriteTextNode (node, visible);
				break;
			case XmlNodeType.Whitespace:
				if (state == XmlCanonicalizerState.InsideDocElement)
					WriteTextNode (node, visible);
				break;
			case XmlNodeType.Comment:
				WriteCommentNode (node, visible);
				break;
			case XmlNodeType.ProcessingInstruction:
				WriteProcessingInstructionNode (node, visible);
				break;
			case XmlNodeType.EntityReference:
				for (int i = 0; i < node.ChildNodes.Count; i++)
					WriteNode (node.ChildNodes [i]);
				break;
			case XmlNodeType.Attribute:
				throw new XmlException ("Attribute node is impossible here", null);
			case XmlNodeType.EndElement:
				throw new XmlException ("EndElement node is impossible here", null);
			case XmlNodeType.EndEntity:
				throw new XmlException ("EndEntity node is impossible here", null);
			case XmlNodeType.DocumentType:
			case XmlNodeType.Entity:
			case XmlNodeType.Notation:
			case XmlNodeType.XmlDeclaration:
				// just do nothing
				break;
			}
		}

		private void WriteDocumentNode (XmlNode node)
    		{
			state = XmlCanonicalizerState.BeforeDocElement;
			for (XmlNode child = node.FirstChild; child != null; child = child.NextSibling)
				WriteNode (child);
		}
		
		// Element Nodes
		// If the element is not in the node-set, then the result is obtained 
		// by processing the namespace axis, then the attribute axis, then 
		// processing the child nodes of the element that are in the node-set 
		// (in document order). If the element is inthe node-set, then the result 
		// is an open angle bracket (<), the element QName, the result of 
		// processing the namespace axis, the result of processing the attribute 
		// axis, a close angle bracket (>), the result of processing the child 
		// nodes of the element that are in the node-set (in document order), an 
		// open angle bracket, a forward slash (/), the element QName, and a close 
		// angle bracket.
		private void WriteElementNode (XmlNode node, bool visible)
		{
			// Console.WriteLine ("Debug: element node");
		    
			// remember current state 
			int savedPrevVisibleNamespacesStart = prevVisibleNamespacesStart;
			int savedPrevVisibleNamespacesEnd = prevVisibleNamespacesEnd;
			int savedVisibleNamespacesSize = visibleNamespaces.Count;
			XmlCanonicalizerState s = state;
			if (visible && state == XmlCanonicalizerState.BeforeDocElement)
				state = XmlCanonicalizerState.InsideDocElement;
		    
			// write start tag
			if (visible) {
				res.Append ("<");
				res.Append (node.Name);
			}
		    
			// this is odd but you can select namespaces
			// and attributes even if node itself is not visible
			WriteNamespacesAxis (node, visible);
			WriteAttributesAxis (node);			
	
			if (visible)
				res.Append (">");

			// write children
			for (XmlNode child = node.FirstChild; child != null; child = child.NextSibling)
				WriteNode (child);
		    		    
			// write end tag	    
			if (visible) {
				res.Append ("</");
				res.Append (node.Name);
				res.Append (">");
			}
		    
			// restore state
			if (visible && s == XmlCanonicalizerState.BeforeDocElement)
				state = XmlCanonicalizerState.AfterDocElement;
			prevVisibleNamespacesStart = savedPrevVisibleNamespacesStart;
			prevVisibleNamespacesEnd = savedPrevVisibleNamespacesEnd;
			if (visibleNamespaces.Count > savedVisibleNamespacesSize) {
				visibleNamespaces.RemoveRange (savedVisibleNamespacesSize, 
					visibleNamespaces.Count - savedVisibleNamespacesSize);
			}
		}

		// Namespace Axis
		// Consider a list L containing only namespace nodes in the 
		// axis and in the node-set in lexicographic order (ascending). To begin 
		// processing L, if the first node is not the default namespace node (a node 
		// with no namespace URI and no local name), then generate a space followed 
		// by xmlns="" if and only if the following conditions are met:
		//    - the element E that owns the axis is in the node-set
		//    - The nearest ancestor element of E in the node-set has a default 
		//	    namespace node in the node-set (default namespace nodes always 
		//      have non-empty values in XPath)
		// The latter condition eliminates unnecessary occurrences of xmlns="" in 
		// the canonical form since an element only receives an xmlns="" if its 
		// default namespace is empty and if it has an immediate parent in the 
		// canonical form that has a non-empty default namespace. To finish 
		// processing  L, simply process every namespace node in L, except omit 
		// namespace node with local name xml, which defines the xml prefix, 
		// if its string value is http://www.w3.org/XML/1998/namespace.
		private void WriteNamespacesAxis (XmlNode node, bool visible)
		{
			// Console.WriteLine ("Debug: namespaces");

			XmlDocument doc = node.OwnerDocument;    
			bool has_empty_namespace = false;
			ArrayList list = new ArrayList ();
			for (XmlNode cur = node; cur != null && cur != doc; cur = cur.ParentNode) {
			        foreach (XmlAttribute attribute in cur.Attributes) {		
					if (!IsNamespaceNode (attribute)) 
			    			continue;
			    	
					// get namespace prefix
					string prefix = string.Empty;
					if (attribute.Prefix == "xmlns") 
						prefix = attribute.LocalName;
			    
				        // check if it is "xml" namespace			    
					if (prefix == "xml" && attribute.Value == "http://www.w3.org/XML/1998/namespace")
						continue;
			    
					// make sure that this is an active namespace
					// for our node
					string ns = node.GetNamespaceOfPrefix (prefix);
					if (ns != attribute.Value) 
						continue;
			    
					// check that it is selected with XPath
					if (!IsNodeVisible (attribute)) 
						continue;

					// check that we have not rendered it yet
					bool rendered = IsNamespaceRendered (prefix, attribute.Value);

					// For exc-c14n, only visibly utilized
					// namespaces are written.
					if (exclusive && !IsVisiblyUtilized (node as XmlElement, attribute))
						continue;

					// add to the visible namespaces stack
					if (visible)
						visibleNamespaces.Add (attribute);			      
			    
					if (!rendered)
						list.Add (attribute);
				    
					if (prefix == string.Empty)
						has_empty_namespace = true;
				}
			}

			// add empty namespace if needed
			if (visible && !has_empty_namespace && !IsNamespaceRendered (string.Empty, string.Empty) && node.NamespaceURI == String.Empty)
				res.Append (" xmlns=\"\"");
		    
			list.Sort (new XmlDsigC14NTransformNamespacesComparer ());
			foreach (object obj in list) {
				XmlNode attribute = (obj as XmlNode);
				if (attribute != null) {
					res.Append (" ");
					res.Append (attribute.Name);
					res.Append ("=\"");
					res.Append (attribute.Value);
					res.Append ("\"");
				}
			}
		    
			// move the rendered namespaces stack
			if (visible) {
				prevVisibleNamespacesStart = prevVisibleNamespacesEnd;
				prevVisibleNamespacesEnd = visibleNamespaces.Count;	
			}
		}
		
		// Attribute Axis 
	    	// In lexicographic order (ascending), process each node that 
		// is in the element's attribute axis and in the node-set.
		// 
		// The processing of an element node E MUST be modified slightly 
		// when an XPath node-set is given as input and the element's 
		// parent is omitted from the node-set.
		private void WriteAttributesAxis (XmlNode node)
		{
			// Console.WriteLine ("Debug: attributes");
		
			ArrayList list = new ArrayList ();
			foreach (XmlNode attribute in node.Attributes) {	
				if (!IsNamespaceNode (attribute) && IsNodeVisible (attribute))
					list.Add (attribute);
			}
		     
		        // Add attributes from "xml" namespace for "inclusive" c14n only:
			//
			// The method for processing the attribute axis of an element E 
		        // in the node-set is enhanced. All element nodes along E's 
			// ancestor axis are examined for nearest occurrences of 
			// attributes in the xml namespace, such as xml:lang and 
			// xml:space (whether or not they are in the node-set). 
			// From this list of attributes, remove any that are in E's 
			// attribute axis (whether or not they are in the node-set). 
			// Then, lexicographically merge this attribute list with the 
			// nodes of E's attribute axis that are in the node-set. The 
			// result of visiting the attribute axis is computed by 
			// processing the attribute nodes in this merged attribute list.
			if (!exclusive && node.ParentNode != null && node.ParentNode.ParentNode != null && !IsNodeVisible (node.ParentNode.ParentNode)) {
	    			// if we have whole document then the node.ParentNode.ParentNode
				// is always visible
				for (XmlNode cur = node.ParentNode; cur != null; cur = cur.ParentNode) {
					if (cur.Attributes == null)
						continue;
					foreach (XmlNode attribute in cur.Attributes) {
						// we are looking for "xml:*" attributes
						if (attribute.Prefix != "xml")
							continue;
				
						// exclude ones that are in the node's attributes axis
						if (node.Attributes.GetNamedItem (attribute.LocalName, attribute.NamespaceURI) != null)
							continue;
				
						// finally check that we don't have the same attribute in our list
						bool found = false;
						foreach (object obj in list) {
							XmlNode n = (obj as XmlNode);
							if (n.Prefix == "xml" && n.LocalName == attribute.LocalName) {
								found = true;
								break;
							}
						}
				
						if (found) 
							continue;
				
						// now we can add this attribute to our list
						list.Add (attribute);
					}
				}		
			}
		    	
			// sort namespaces and write results	    
			list.Sort (new XmlDsigC14NTransformAttributesComparer ());
			foreach (object obj in list) {
				XmlNode attribute = (obj as XmlNode);
				if (attribute != null) {
					res.Append (" ");
					res.Append (attribute.Name);
					res.Append ("=\"");
					res.Append (NormalizeString (attribute.Value, XmlNodeType.Attribute));
					res.Append ("\"");
				}
			}
		}

            	// Text Nodes
            	// the string value, except all ampersands are replaced 
            	// by &amp;, all open angle brackets (<) are replaced by &lt;, all closing 
            	// angle brackets (>) are replaced by &gt;, and all #xD characters are 
            	// replaced by &#xD;.
		private void WriteTextNode (XmlNode node, bool visible)
		{
			// Console.WriteLine ("Debug: text node");
			if (visible)
				res.Append (NormalizeString (node.Value, node.NodeType));
//				res.Append (NormalizeString (node.Value, XmlNodeType.Text));
		}		

		// Comment Nodes
            	// Nothing if generating canonical XML without comments. For 
            	// canonical XML with comments, generate the opening comment 
            	// symbol (<!--), the string value of the node, and the 
            	// closing comment symbol (-->). Also, a trailing #xA is rendered 
	        // after the closing comment symbol for comment children of the 
        	// root node with a lesser document order than the document 
            	// element, and a leading #xA is rendered before the opening 
            	// comment symbol of comment children of the root node with a 
            	// greater document order than the document element. (Comment 
	        // children of the root node represent comments outside of the 
            	// top-level document element and outside of the document type 
            	// declaration).
		private void WriteCommentNode (XmlNode node, bool visible)
		{
			// Console.WriteLine ("Debug: comment node");
			if (visible && comments) {
			    if (state == XmlCanonicalizerState.AfterDocElement)
				    res.Append ("\x0A<!--");
			    else
				    res.Append ("<!--");
			
			    res.Append (NormalizeString (node.Value, XmlNodeType.Comment));
			    
			    if (state == XmlCanonicalizerState.BeforeDocElement)
				    res.Append ("-->\x0A");
			    else
				    res.Append ("-->");
	    		}
		}
		
        	// Processing Instruction (PI) Nodes- 
            	// The opening PI symbol (<?), the PI target name of the node, 
            	// a leading space and the string value if it is not empty, and 
            	// the closing PI symbol (?>). If the string value is empty, 
            	// then the leading space is not added. Also, a trailing #xA is 
            	// rendered after the closing PI symbol for PI children of the 
            	// root node with a lesser document order than the document 
            	// element, and a leading #xA is rendered before the opening PI 
            	// symbol of PI children of the root node with a greater document 
            	// order than the document element.
		private void WriteProcessingInstructionNode (XmlNode node, bool visible)
		{
			// Console.WriteLine ("Debug: PI node");

			if (visible) {
				if (state == XmlCanonicalizerState.AfterDocElement)
					res.Append ("\x0A<?");
				else
					res.Append ("<?");
			
				res.Append (node.Name);
				if (node.Value.Length > 0) {
					res.Append (" ");
					res.Append (NormalizeString (node.Value, XmlNodeType.ProcessingInstruction));
				}
			
				if (state == XmlCanonicalizerState.BeforeDocElement)
					res.Append ("?>\x0A");
				else
					res.Append ("?>");
	    		}
		}

		// determines whether the node is in the node-set or not.
		private bool IsNodeVisible (XmlNode node)
		{
			// if node list is empty then we process whole document
			if (xnl == null) 
				return true;
		    
			// walk thru the list
			foreach (XmlNode xn in xnl) {
				if (node.Equals (xn)) 
					return true;
			}
		    
			return false;
		}
		
		// This method assumes that the namespace node is *not*
		// rendered yet.
		private bool IsVisiblyUtilized (XmlElement owner, XmlAttribute ns)
		{
			if (owner == null)
				return false;

			string prefix = ns.LocalName == "xmlns" ? String.Empty : ns.LocalName;
			if (owner.Prefix == prefix && owner.NamespaceURI == ns.Value)
				return true;
			if (!owner.HasAttributes)
				return false;
			foreach (XmlAttribute a in owner.Attributes) {
				if (a.Prefix == String.Empty)
					continue;
				if (a.Prefix != prefix || a.NamespaceURI != ns.Value)
					continue;
				if (IsNodeVisible (a))
					return true;
			}
			return false;
		}

		private bool IsNamespaceRendered (string prefix, string uri)
		{
			// if the default namespace xmlns="" is not re-defined yet
			// then we do not want to print it out
			bool IsEmptyNs = prefix == string.Empty && uri == string.Empty;
			int start = (IsEmptyNs) ? 0 : prevVisibleNamespacesStart;
			for (int i = visibleNamespaces.Count - 1; i >= start; i--) {
				XmlNode node = (visibleNamespaces[i] as XmlNode);
				if (node != null) {
					// get namespace prefix
					string p = string.Empty;
					if (node.Prefix == "xmlns") 
						p = node.LocalName;
					if (p == prefix)
						return node.Value == uri;
				}
			}
		    
			return IsEmptyNs;
		}
		
		private bool IsNamespaceNode (XmlNode node)
    		{
			if (node == null || node.NodeType != XmlNodeType.Attribute) 
				return false;
			return node.NamespaceURI == "http://www.w3.org/2000/xmlns/";
		}
    
		private bool IsTextNode (XmlNodeType type)
		{
			switch (type) {
			case XmlNodeType.Text:
			case XmlNodeType.CDATA:
			case XmlNodeType.SignificantWhitespace:
			case XmlNodeType.Whitespace:
				return true;
			}
			return false;
		}

		private string NormalizeString (string input, XmlNodeType type)
		{
			StringBuilder sb = new StringBuilder ();
			for (int i = 0; i < input.Length; i++) {
				char ch = input[i];
				if (ch == '<' && (type == XmlNodeType.Attribute || IsTextNode (type)))
					sb.Append ("&lt;");
				else if (ch == '>' && IsTextNode (type))
					sb.Append ("&gt;");
				else if (ch == '&' && (type == XmlNodeType.Attribute || IsTextNode (type)))
					sb.Append ("&amp;");
				else if (ch == '\"' && type == XmlNodeType.Attribute)
					sb.Append ("&quot;");
				else if (ch == '\x09' && type == XmlNodeType.Attribute)
					sb.Append ("&#x9;");
				else if (ch == '\x0A' && type == XmlNodeType.Attribute)
					sb.Append ("&#xA;");
				else if (ch == '\x0D')
					sb.Append ("&#xD;");
				else
					sb.Append (ch);
			}
		    
			return sb.ToString ();
		}
	}
    
	internal class XmlDsigC14NTransformAttributesComparer : IComparer
	{
		public int Compare (object x, object y)
		{
			XmlNode n1 = (x as XmlNode);
			XmlNode n2 = (y as XmlNode);
		
			// simple cases
			if (n1 == n2) 
				return 0;
			else if (n1 == null) 
				return -1;
			else if (n2 == null) 
				return 1;
	    		else if (n1.Prefix == n2.Prefix) 
				return string.Compare (n1.LocalName, n2.LocalName);
	
    			// Attributes in the default namespace are first
			// because the default namespace is not applied to
    			// unqualified attributes
			if (n1.Prefix == string.Empty) 
				return -1;
			else if (n2.Prefix == string.Empty) 
				return 1;
		    
	    		int ret = string.Compare (n1.NamespaceURI, n2.NamespaceURI);
			if (ret == 0)
				ret = string.Compare (n1.LocalName, n2.LocalName);
			return ret;
		}
	}

	internal class XmlDsigC14NTransformNamespacesComparer : IComparer
	{
		public int Compare (object x, object y)
		{
			XmlNode n1 = (x as XmlNode);
			XmlNode n2 = (y as XmlNode);
		
			// simple cases
			if (n1 == n2) 
				return 0;
			else if (n1 == null) 
				return -1;
			else if (n2 == null) 
				return 1;
			else if (n1.Prefix == string.Empty) 
				return -1;
			else if (n2.Prefix == string.Empty) 
				return 1;
		    
			return string.Compare (n1.LocalName, n2.LocalName);
		}
	}
}