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 / XmlDsigEnvelopedSignatureTransform.cs
1 //
2 // XmlDsigEnvelopedSignatureTransform.cs - 
3 //      Enveloped Signature Transform implementation for XML Signature
4 //
5 // Author:
6 //      Sebastien Pouliot (spouliot@motus.com)
7 //      Atsushi Enomoto (atsushi@ximian.com)
8 //
9 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
10 // (C) 2004 Novell Inc.
11 //
12
13 //
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
21 // 
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 // 
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 //
33
34 using System.Collections;
35 using System.IO;
36 using System.Xml;
37
38 namespace System.Security.Cryptography.Xml { 
39
40         public class XmlDsigEnvelopedSignatureTransform : Transform {
41
42                 private Type[] input;
43                 private Type[] output;
44                 private bool comments;
45                 private object inputObj;
46
47                 public XmlDsigEnvelopedSignatureTransform ()
48                         : this (false)
49                 {
50                 }
51
52                 public XmlDsigEnvelopedSignatureTransform (bool includeComments) 
53                 {
54                         Algorithm = XmlSignature.AlgorithmNamespaces.XmlDsigEnvelopedSignatureTransform;
55                         comments = includeComments;
56                 }
57
58                 public override Type[] InputTypes {
59                         get {
60                                 if (input == null) {
61                                         input = new Type [3];
62                                         input[0] = typeof (System.IO.Stream);
63                                         input[1] = typeof (System.Xml.XmlDocument);
64                                         input[2] = typeof (System.Xml.XmlNodeList);
65                                 }
66                                 return input;
67                         }
68                 }
69
70                 public override Type[] OutputTypes {
71                         get {
72                                 if (output == null) {
73                                         output = new Type [2];
74                                         output [0] = typeof (System.Xml.XmlDocument);
75                                         output [1] = typeof (System.Xml.XmlNodeList);
76                                 }
77                                 return output;
78                         }
79                 }
80
81                 protected override XmlNodeList GetInnerXml () 
82                 {
83                         return null; // THIS IS DOCUMENTED AS SUCH
84                 }
85
86                 // NOTE: This method never supports the requirements written
87                 // in xmldsig spec that says its input is canonicalized before
88                 // transforming. This method just removes Signature element.
89                 // Canonicalization is done in SignedXml.
90                 public override object GetOutput ()
91                 {
92                         XmlDocument doc = null;
93
94                         // possible input: Stream, XmlDocument, and XmlNodeList
95                         if (inputObj is Stream) {
96                                 doc = new XmlDocument ();
97                                 doc.PreserveWhitespace = true;
98 #if NET_1_1
99                                 doc.XmlResolver = GetResolver ();
100 #endif
101                                 doc.Load (new XmlSignatureStreamReader (
102                                         new StreamReader (inputObj as Stream)));
103                                 return GetOutputFromNode (doc, GetNamespaceManager (doc), true);
104                         }
105                         else if (inputObj is XmlDocument) {
106                                 doc = inputObj as XmlDocument;
107                                 return GetOutputFromNode (doc, GetNamespaceManager (doc), true);
108                         }
109                         else if (inputObj is XmlNodeList) {
110                                 ArrayList al = new ArrayList ();
111                                 XmlNodeList nl = (XmlNodeList) inputObj;
112                                 if (nl.Count > 0) {
113                                         XmlNamespaceManager m = GetNamespaceManager (nl.Item (0));
114                                         ArrayList tmp = new ArrayList ();
115                                         foreach (XmlNode n in nl)
116                                                 tmp.Add (n);
117                                         foreach (XmlNode n in tmp)
118                                                 if (n.SelectNodes ("ancestor-or-self::dsig:Signature", m).Count == 0)
119                                                         al.Add (GetOutputFromNode (n, m, false));
120                                 }
121                                 return new XmlDsigNodeList (al);
122                         }
123                         // Note that it is unexpected behavior with related to InputTypes (MS.NET accepts XmlElement)
124                         else if (inputObj is XmlElement) {
125                                 XmlElement el = inputObj as XmlElement;
126                                 XmlNamespaceManager m = GetNamespaceManager (el);
127                                 if (el.SelectNodes ("ancestor-or-self::dsig:Signature", m).Count == 0)
128                                         return GetOutputFromNode (el, m, true);
129                         }
130
131                         throw new NullReferenceException ();
132                 }
133
134                 private XmlNamespaceManager GetNamespaceManager (XmlNode n)
135                 {
136                         XmlDocument doc = ((n is XmlDocument) ? (n as XmlDocument) : n.OwnerDocument);
137                         XmlNamespaceManager nsmgr = new XmlNamespaceManager (doc.NameTable);
138                         nsmgr.AddNamespace ("dsig", XmlSignature.NamespaceURI);
139                         return nsmgr;
140                 }
141
142                 private XmlNode GetOutputFromNode (XmlNode input, XmlNamespaceManager nsmgr, bool remove)
143                 {
144                         if (remove) {
145                                 XmlNodeList nl = input.SelectNodes ("descendant-or-self::dsig:Signature", nsmgr);
146                                 ArrayList al = new ArrayList ();
147                                 foreach (XmlNode n in nl)
148                                         al.Add (n);
149                                 foreach (XmlNode n in al)
150                                         n.ParentNode.RemoveChild (n);
151                         }
152                         return input;
153                 }
154
155                 public override object GetOutput (Type type) 
156                 {
157                         if (type == typeof (Stream))
158                                 return GetOutput ();
159                         throw new ArgumentException ("type");
160                 }
161
162                 public override void LoadInnerXml (XmlNodeList nodeList) 
163                 {
164                         // NO CHANGE
165                 }
166
167                 public override void LoadInput (object obj) 
168                 {
169                         inputObj = obj;
170                 }
171         }
172 }