2 // XmlDsigEnvelopedSignatureTransform.cs -
3 // Enveloped Signature Transform implementation for XML Signature
6 // Sebastien Pouliot (spouliot@motus.com)
7 // Atsushi Enomoto (atsushi@ximian.com)
9 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
10 // (C) 2004 Novell Inc.
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:
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
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.
34 using System.Collections;
38 namespace System.Security.Cryptography.Xml {
40 public class XmlDsigEnvelopedSignatureTransform : Transform {
43 private Type[] output;
44 private bool comments;
45 private object inputObj;
47 public XmlDsigEnvelopedSignatureTransform ()
49 Algorithm = "http://www.w3.org/2000/09/xmldsig#enveloped-signature";
53 public XmlDsigEnvelopedSignatureTransform (bool includeComments)
55 comments = includeComments;
58 public override Type[] InputTypes {
62 // this way the result is cached if called multiple time
64 input[0] = typeof (System.IO.Stream);
65 input[1] = typeof (System.Xml.XmlDocument);
66 input[2] = typeof (System.Xml.XmlNodeList);
73 public override Type[] OutputTypes {
77 // this way the result is cached if called multiple time
78 output = new Type [2];
79 output [0] = typeof (System.Xml.XmlDocument);
80 output [1] = typeof (System.Xml.XmlNodeList);
87 protected override XmlNodeList GetInnerXml ()
89 return null; // THIS IS DOCUMENTED AS SUCH
92 // NOTE: This method never supports the requirements written
93 // in xmldsig spec that says its input is canonicalized before
94 // transforming. This method just removes Signature element.
95 // Canonicalization is done in SignedXml.
96 public override object GetOutput ()
98 XmlDocument doc = null;
100 // possible input: Stream, XmlDocument, and XmlNodeList
101 if (inputObj is Stream) {
102 doc = new XmlDocument ();
103 doc.PreserveWhitespace = true;
105 doc.XmlResolver = GetResolver ();
107 doc.Load (new XmlSignatureStreamReader (
108 new StreamReader (inputObj as Stream)));
109 return GetOutputFromNode (doc, GetNamespaceManager (doc), true);
111 else if (inputObj is XmlDocument) {
112 doc = inputObj as XmlDocument;
113 return GetOutputFromNode (doc, GetNamespaceManager (doc), true);
115 else if (inputObj is XmlNodeList) {
116 ArrayList al = new ArrayList ();
117 XmlNodeList nl = (XmlNodeList) inputObj;
119 XmlNamespaceManager m = GetNamespaceManager (nl.Item (0));
120 ArrayList tmp = new ArrayList ();
121 foreach (XmlNode n in nl)
123 foreach (XmlNode n in tmp)
124 if (n.SelectNodes ("ancestor-or-self::dsig:Signature", m).Count == 0)
125 al.Add (GetOutputFromNode (n, m, false));
127 return new XmlDsigNodeList (al);
129 // Note that it is unexpected behavior with related to InputTypes (MS.NET accepts XmlElement)
130 else if (inputObj is XmlElement) {
131 XmlElement el = inputObj as XmlElement;
132 XmlNamespaceManager m = GetNamespaceManager (el);
133 if (el.SelectNodes ("ancestor-or-self::dsig:Signature", m).Count == 0)
134 return GetOutputFromNode (el, m, true);
137 throw new NullReferenceException ();
140 private XmlNamespaceManager GetNamespaceManager (XmlNode n)
142 XmlDocument doc = ((n is XmlDocument) ? (n as XmlDocument) : n.OwnerDocument);
143 XmlNamespaceManager nsmgr = new XmlNamespaceManager (doc.NameTable);
144 nsmgr.AddNamespace ("dsig", XmlSignature.NamespaceURI);
148 private XmlNode GetOutputFromNode (XmlNode input, XmlNamespaceManager nsmgr, bool remove)
151 XmlNodeList nl = input.SelectNodes ("descendant-or-self::dsig:Signature", nsmgr);
152 foreach (XmlNode n in nl)
153 n.ParentNode.RemoveChild (n);
158 public override object GetOutput (Type type)
160 if (type == Type.GetType ("Stream"))
162 throw new ArgumentException ("type");
165 public override void LoadInnerXml (XmlNodeList nodeList)
170 public override void LoadInput (object obj)