BindingFlags.Public needed here as Exception.HResult is now public in .NET 4.5. This...
[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 #if NET_2_0
35
36 using System.Collections;
37 using System.IO;
38 using System.Runtime.InteropServices;
39 using System.Text;
40 using System.Xml;
41
42 using Mono.Xml;
43
44 namespace System.Security.Cryptography.Xml { 
45
46         public class XmlDsigExcC14NTransform : Transform {
47                 private Type[] input;
48                 private Type[] output;
49                 private XmlCanonicalizer canonicalizer;
50                 private Stream s;
51                 private string inclusiveNamespacesPrefixList;
52                 
53                 public XmlDsigExcC14NTransform ()       
54                         : this (false, null)
55                 {
56                 }
57
58                 public XmlDsigExcC14NTransform (bool includeComments) 
59                         : this (includeComments, null)
60                 {
61                 }
62
63                 public XmlDsigExcC14NTransform (string inclusiveNamespacesPrefixList)
64                         : this (false, inclusiveNamespacesPrefixList)
65                 {
66                 }
67
68                 public XmlDsigExcC14NTransform (bool includeComments, string inclusiveNamespacesPrefixList)
69                 {
70                         if (includeComments)
71                                 Algorithm = XmlSignature.AlgorithmNamespaces.XmlDsigExcC14NWithCommentsTransform;
72                         else
73                                 Algorithm = XmlSignature.AlgorithmNamespaces.XmlDsigExcC14NTransform;
74                         this.inclusiveNamespacesPrefixList = inclusiveNamespacesPrefixList;
75                         canonicalizer = new XmlCanonicalizer (includeComments, true, PropagatedNamespaces);
76                 }
77
78                 public string InclusiveNamespacesPrefixList {
79                         get { return inclusiveNamespacesPrefixList; }
80                         set { inclusiveNamespacesPrefixList = value; }
81                 }
82
83                 public override Type[] InputTypes {
84                         get {
85                                 if (input == null) {
86                                         input = new Type [3];
87                                         input[0] = typeof (System.IO.Stream);
88                                         input[1] = typeof (System.Xml.XmlDocument);
89                                         input[2] = typeof (System.Xml.XmlNodeList);
90                                 }
91                                 return input;
92                         }
93                 }
94
95                 public override Type[] OutputTypes {
96                         get {
97                                 if (output == null) {
98                                         output = new Type [1];
99                                         output[0] = typeof (System.IO.Stream);
100                                 }
101                                 return output;
102                         }
103                 }
104
105                 protected override XmlNodeList GetInnerXml () 
106                 {
107                         return null; // THIS IS DOCUMENTED AS SUCH
108                 }
109
110 #if NET_2_0
111                 public override byte[] GetDigestedOutput (HashAlgorithm hash)
112                 {
113                         // no null check, MS throws a NullReferenceException here
114                         return hash.ComputeHash ((Stream) GetOutput ());
115                 }
116 #endif
117
118                 public override object GetOutput () 
119                 {
120                         return (object) s;
121                 }
122
123                 public override object GetOutput (Type type) 
124                 {
125                         if (type == typeof (Stream))
126                                 return GetOutput ();
127                         throw new ArgumentException ("type");
128                 }
129
130                 public override void LoadInnerXml (XmlNodeList nodeList) 
131                 {
132                         // documented as not changing the state of the transform
133                 }
134
135                 public override void LoadInput (object obj) 
136                 {
137                         canonicalizer.InclusiveNamespacesPrefixList = InclusiveNamespacesPrefixList;
138                         // possible input: Stream, XmlDocument, and XmlNodeList
139                         Stream stream = (obj as Stream);
140                         if (stream != null) {
141                                 XmlDocument doc = new XmlDocument ();
142                                 doc.PreserveWhitespace = true;  // REALLY IMPORTANT
143 #if NET_1_1
144                                 doc.XmlResolver = GetResolver ();
145 #endif
146                                 doc.Load (new XmlSignatureStreamReader (new StreamReader (stream)));
147 //                              doc.Load ((Stream) obj);
148                                 s = canonicalizer.Canonicalize (doc);
149                                 return;
150                         }
151
152                         XmlDocument xd = (obj as XmlDocument);
153                         if (xd != null) {
154                                 s = canonicalizer.Canonicalize (xd);
155                                 return;
156                         }
157
158                         XmlNodeList nl = (obj as XmlNodeList);
159                         if (nl != null) {
160                                 s = canonicalizer.Canonicalize (nl);
161                         }
162 #if NET_2_0
163                         else
164                                 throw new ArgumentException ("obj");
165 #else
166                         // note: there is no default are other types won't throw an exception
167 #endif
168                 }
169         }
170 }
171
172 #endif