2004-09-02 Tim Coleman <tim@timcoleman.com>
[mono.git] / mcs / class / System.Security / System.Security.Cryptography.Xml / XmlDsigC14NTransform.cs
1 //
2 // XmlDsigC14NTransform.cs - C14N 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 // (C) 2004 Novell (http://www.novell.com)
14 //
15
16 //
17 // Permission is hereby granted, free of charge, to any person obtaining
18 // a copy of this software and associated documentation files (the
19 // "Software"), to deal in the Software without restriction, including
20 // without limitation the rights to use, copy, modify, merge, publish,
21 // distribute, sublicense, and/or sell copies of the Software, and to
22 // permit persons to whom the Software is furnished to do so, subject to
23 // the following conditions:
24 // 
25 // The above copyright notice and this permission notice shall be
26 // included in all copies or substantial portions of the Software.
27 // 
28 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
32 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
33 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
34 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 //
36
37 using System.Collections;
38 using System.IO;
39 using System.Text;
40 using System.Xml;
41
42 using Mono.Xml;
43
44 namespace System.Security.Cryptography.Xml { 
45
46         public class XmlDsigC14NTransform : Transform {
47                 private Type[] input;
48                 private Type[] output;
49                 private XmlCanonicalizer canonicalizer;
50                 private Stream s;
51                 
52                 public XmlDsigC14NTransform () 
53                 {
54                         Algorithm = "http://www.w3.org/TR/2001/REC-xml-c14n-20010315";
55                         canonicalizer = new XmlCanonicalizer (false, false);
56                 }
57
58                 public XmlDsigC14NTransform (bool includeComments) 
59                 {
60                         canonicalizer = new XmlCanonicalizer (includeComments, false);
61                 }
62
63                 public override Type[] InputTypes {
64                         get {
65                                 if (input == null) {
66                                         lock (this) {
67                                                 // this way the result is cached if called multiple time
68                                                 input = new Type [3];
69                                                 input[0] = typeof (System.IO.Stream);
70                                                 input[1] = typeof (System.Xml.XmlDocument);
71                                                 input[2] = typeof (System.Xml.XmlNodeList);
72                                         }
73                                 }
74                                 return input;
75                         }
76                 }
77
78                 public override Type[] OutputTypes {
79                         get {
80                                 if (output == null) {
81                                         lock (this) {
82                                                 // this way the result is cached if called multiple time
83                                                 output = new Type [1];
84                                                 output[0] = typeof (System.IO.Stream);
85                                         }
86                                 }
87                                 return output;
88                         }
89                 }
90
91                 protected override XmlNodeList GetInnerXml () 
92                 {
93                         return null; // THIS IS DOCUMENTED AS SUCH
94                 }
95
96 #if NET_2_0
97                 [MonoTODO]
98                 public override byte[] GetDigestedOutput (HashAlgorithm hash)
99                 {
100                         throw new NotImplementedException ();
101                 }
102 #endif
103
104                 public override object GetOutput () 
105                 {
106                         return (object) s;
107                 }
108
109                 public override object GetOutput (Type type) 
110                 {
111                         if (type == Type.GetType ("Stream"))
112                                 return GetOutput ();
113                         throw new ArgumentException ("type");
114                 }
115
116                 public override void LoadInnerXml (XmlNodeList nodeList) 
117                 {
118                         // documented as not changing the state of the transform
119                 }
120
121                 public override void LoadInput (object obj) 
122                 {
123                         if (obj is Stream) {
124                                 s = (obj as Stream);
125                                 XmlDocument doc = new XmlDocument ();
126                                 doc.PreserveWhitespace = true;  // REALLY IMPORTANT
127                                 doc.Load (obj as Stream);
128                                 s = canonicalizer.Canonicalize (doc);
129                         } else if (obj is XmlDocument)
130                                 s = canonicalizer.Canonicalize ((obj as XmlDocument));
131                         else if (obj is XmlNodeList)
132                                 s = canonicalizer.Canonicalize ((obj as XmlNodeList));
133                         // note: there is no default are other types won't throw an exception
134                 }
135         }
136 }
137