2 // XmlDsigC14NTransform.cs - C14N Transform implementation for XML Signature
3 // http://www.w3.org/TR/xml-c14n
6 // Sebastien Pouliot <sebastien@ximian.com>
7 // Aleksey Sanin (aleksey@aleksey.com)
8 // Tim Coleman (tim@timcoleman.com)
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)
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:
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
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.
35 using System.Collections;
37 using System.Runtime.InteropServices;
43 namespace System.Security.Cryptography.Xml {
45 public class XmlDsigC14NTransform : Transform {
47 private Type[] output;
48 private XmlCanonicalizer canonicalizer;
51 public XmlDsigC14NTransform () : this (false)
55 public XmlDsigC14NTransform (bool includeComments)
58 Algorithm = XmlSignature.AlgorithmNamespaces.XmlDsigC14NWithCommentsTransform;
60 Algorithm = XmlSignature.AlgorithmNamespaces.XmlDsigC14NTransform;
61 canonicalizer = new XmlCanonicalizer (includeComments, false, PropagatedNamespaces);
64 public override Type[] InputTypes {
68 input[0] = typeof (System.IO.Stream);
69 input[1] = typeof (System.Xml.XmlDocument);
70 input[2] = typeof (System.Xml.XmlNodeList);
76 public override Type[] OutputTypes {
79 output = new Type [1];
80 output[0] = typeof (System.IO.Stream);
86 protected override XmlNodeList GetInnerXml ()
88 return null; // THIS IS DOCUMENTED AS SUCH
93 public override byte[] GetDigestedOutput (HashAlgorithm hash)
95 // no null check, MS throws a NullReferenceException here
96 return hash.ComputeHash ((Stream) GetOutput ());
100 public override object GetOutput ()
105 public override object GetOutput (Type type)
107 if (type == typeof (Stream))
109 throw new ArgumentException ("type");
112 public override void LoadInnerXml (XmlNodeList nodeList)
114 // documented as not changing the state of the transform
117 public override void LoadInput (object obj)
119 // possible input: Stream, XmlDocument, and XmlNodeList
120 Stream stream = (obj as Stream);
121 if (stream != null) {
122 XmlDocument doc = new XmlDocument ();
123 doc.PreserveWhitespace = true; // REALLY IMPORTANT
124 doc.XmlResolver = GetResolver ();
125 doc.Load (new XmlSignatureStreamReader (new StreamReader (stream)));
126 // doc.Load ((Stream) obj);
127 s = canonicalizer.Canonicalize (doc);
131 XmlDocument xd = (obj as XmlDocument);
133 s = canonicalizer.Canonicalize (xd);
137 XmlNodeList nl = (obj as XmlNodeList);
139 s = canonicalizer.Canonicalize (nl);
143 throw new ArgumentException ("obj");
145 // note: there is no default are other types won't throw an exception