Merge pull request #1218 from AndreyAkinshin/master
[mono.git] / mcs / class / System.Security / System.Security.Cryptography.Xml / XmlDsigExcC14NTransform.cs
1 //
2 // XmlDsigExcC14NTransform.cs - ExcC14N Transform implementation for XML Signature
3 // http://www.w3.org/TR/xml-c14n
4 //
5 // Authors:
6 //      Sebastien Pouliot <sebastien@ximian.com>
7 //      Aleksey Sanin (aleksey@aleksey.com)
8 //      Tim Coleman (tim@timcoleman.com)
9 //
10 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
11 // (C) 2003 Aleksey Sanin (aleksey@aleksey.com)
12 // Copyright (C) Tim Coleman, 2004
13 // Copyright (C) 2004-2005 Novell Inc. (http://www.novell.com)
14 //
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
22 // 
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
25 // 
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 //
34
35 using System.Collections;
36 using System.IO;
37 using System.Runtime.InteropServices;
38 using System.Text;
39 using System.Xml;
40
41 using Mono.Xml;
42
43 namespace System.Security.Cryptography.Xml { 
44
45         public class XmlDsigExcC14NTransform : Transform {
46                 private Type[] input;
47                 private Type[] output;
48                 private XmlCanonicalizer canonicalizer;
49                 private Stream s;
50                 private string inclusiveNamespacesPrefixList;
51                 
52                 public XmlDsigExcC14NTransform ()       
53                         : this (false, null)
54                 {
55                 }
56
57                 public XmlDsigExcC14NTransform (bool includeComments) 
58                         : this (includeComments, null)
59                 {
60                 }
61
62                 public XmlDsigExcC14NTransform (string inclusiveNamespacesPrefixList)
63                         : this (false, inclusiveNamespacesPrefixList)
64                 {
65                 }
66
67                 public XmlDsigExcC14NTransform (bool includeComments, string inclusiveNamespacesPrefixList)
68                 {
69                         if (includeComments)
70                                 Algorithm = XmlSignature.AlgorithmNamespaces.XmlDsigExcC14NWithCommentsTransform;
71                         else
72                                 Algorithm = XmlSignature.AlgorithmNamespaces.XmlDsigExcC14NTransform;
73                         this.inclusiveNamespacesPrefixList = inclusiveNamespacesPrefixList;
74                         canonicalizer = new XmlCanonicalizer (includeComments, true, PropagatedNamespaces);
75                 }
76
77                 public string InclusiveNamespacesPrefixList {
78                         get { return inclusiveNamespacesPrefixList; }
79                         set { inclusiveNamespacesPrefixList = value; }
80                 }
81
82                 public override Type[] InputTypes {
83                         get {
84                                 if (input == null) {
85                                         input = new Type [3];
86                                         input[0] = typeof (System.IO.Stream);
87                                         input[1] = typeof (System.Xml.XmlDocument);
88                                         input[2] = typeof (System.Xml.XmlNodeList);
89                                 }
90                                 return input;
91                         }
92                 }
93
94                 public override Type[] OutputTypes {
95                         get {
96                                 if (output == null) {
97                                         output = new Type [1];
98                                         output[0] = typeof (System.IO.Stream);
99                                 }
100                                 return output;
101                         }
102                 }
103
104                 protected override XmlNodeList GetInnerXml () 
105                 {
106                         return null; // THIS IS DOCUMENTED AS SUCH
107                 }
108
109                 public override byte[] GetDigestedOutput (HashAlgorithm hash)
110                 {
111                         // no null check, MS throws a NullReferenceException here
112                         return hash.ComputeHash ((Stream) GetOutput ());
113                 }
114
115                 public override object GetOutput () 
116                 {
117                         return (object) s;
118                 }
119
120                 public override object GetOutput (Type type) 
121                 {
122                         if (type == typeof (Stream))
123                                 return GetOutput ();
124                         throw new ArgumentException ("type");
125                 }
126
127                 public override void LoadInnerXml (XmlNodeList nodeList) 
128                 {
129                         // documented as not changing the state of the transform
130                 }
131
132                 public override void LoadInput (object obj) 
133                 {
134                         canonicalizer.InclusiveNamespacesPrefixList = InclusiveNamespacesPrefixList;
135                         // possible input: Stream, XmlDocument, and XmlNodeList
136                         Stream stream = (obj as Stream);
137                         if (stream != null) {
138                                 XmlDocument doc = new XmlDocument ();
139                                 doc.PreserveWhitespace = true;  // REALLY IMPORTANT
140                                 doc.XmlResolver = GetResolver ();
141                                 doc.Load (new XmlSignatureStreamReader (new StreamReader (stream)));
142 //                              doc.Load ((Stream) obj);
143                                 s = canonicalizer.Canonicalize (doc);
144                                 return;
145                         }
146
147                         XmlDocument xd = (obj as XmlDocument);
148                         if (xd != null) {
149                                 s = canonicalizer.Canonicalize (xd);
150                                 return;
151                         }
152
153                         XmlNodeList nl = (obj as XmlNodeList);
154                         if (nl != null) {
155                                 s = canonicalizer.Canonicalize (nl);
156                         }
157                         else
158                                 throw new ArgumentException ("obj");
159                 }
160         }
161 }
162