New test.
[mono.git] / mcs / class / System.Security / System.Security.Cryptography.Xml / XmlDsigExcC14NTransform.cs
1 //
2 // XmlDsigExcC14NTransform.cs - XmlDsigExcC14NTransform implementation for XML Encryption
3 //
4 // Author:
5 //      Tim Coleman (tim@timcoleman.com)
6 //
7 // Copyright (C) Tim Coleman, 2004
8
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 #if NET_2_0
31
32 using Mono.Xml;
33 using System.IO;
34 using System.Xml;
35
36 namespace System.Security.Cryptography.Xml {
37         public class XmlDsigExcC14NTransform : Transform {
38
39                 #region Fields
40
41                 XmlCanonicalizer canonicalizer;
42                 object inputObj;
43                 string inclusiveNamespacesPrefixList;
44                 bool includeComments;
45
46                 #endregion // Fields
47         
48                 #region Constructors
49
50                 public XmlDsigExcC14NTransform ()
51                 {
52                         Algorithm = XmlSignature.AlgorithmNamespaces.XmlDsigExcC14NTransform;
53                         canonicalizer = new XmlCanonicalizer (true, false);
54                 }
55
56                 public XmlDsigExcC14NTransform (bool includeComments)
57                 {
58                         this.includeComments = includeComments;
59                         canonicalizer = new XmlCanonicalizer (true, includeComments);
60                 }
61
62                 [MonoTODO ("What does inclusiveNamespacesPrefixList mean?")]
63                 public XmlDsigExcC14NTransform (string inclusiveNamespacesPrefixList)
64                 {
65                         this.inclusiveNamespacesPrefixList = inclusiveNamespacesPrefixList;
66                 }
67
68                 [MonoTODO ("What does inclusiveNamespacesPrefixList mean?")]
69                 public XmlDsigExcC14NTransform (bool includeComments, string inclusiveNamespacesPrefixList)
70                 {
71                         this.inclusiveNamespacesPrefixList = inclusiveNamespacesPrefixList;
72                         this.includeComments = includeComments;
73                 }
74         
75                 #endregion // Constructors
76         
77                 #region Properties
78
79                 public string InclusiveNamespacesPrefixList {
80                         get { return inclusiveNamespacesPrefixList; }
81                         set { inclusiveNamespacesPrefixList = value; }
82                 }
83
84                 public override Type[] InputTypes {
85                         get { return new Type [3] {typeof (System.IO.Stream), typeof (System.Xml.XmlDocument), typeof (System.Xml.XmlNodeList)}; }
86                 }
87
88                 public override Type[] OutputTypes {
89                         get { return new Type [1] {typeof (System.IO.Stream)}; }
90                 }
91
92                 #endregion // Properties
93
94                 #region Methods
95
96                 public override byte[] GetDigestedOutput (HashAlgorithm hash)
97                 {
98                         return hash.ComputeHash ((Stream) GetOutput());
99                 }
100
101                 protected override XmlNodeList GetInnerXml ()
102                 {
103                         return null;
104                 }
105
106                 public override object GetOutput ()
107                 {
108                         Stream s = null;
109
110                         if (inputObj is Stream) {
111                                 XmlDocument doc = new XmlDocument ();
112                                 doc.PreserveWhitespace = true;  // REALLY IMPORTANT
113                                 doc.Load (inputObj as Stream);
114                                 s = canonicalizer.Canonicalize (doc);
115                         } 
116                         else if (inputObj is XmlDocument)
117                                 s = canonicalizer.Canonicalize (inputObj as XmlDocument);
118                         else if (inputObj is XmlNodeList)
119                                 s = canonicalizer.Canonicalize (inputObj as XmlNodeList);
120
121                         // note: there is no default are other types won't throw an exception
122
123                         return (object) s;
124                 }
125
126                 public override object GetOutput (Type type)
127                 {
128                         if (type == Type.GetType ("Stream"))
129                                 return GetOutput ();
130                         throw new ArgumentException ("type");
131                 }
132
133                 public override void LoadInnerXml (XmlNodeList nodeList)
134                 {
135                 }
136                 
137                 public override void LoadInput (object obj)
138                 {
139                         inputObj = obj;
140                 }
141
142                 #endregion // Methods
143         }
144 }
145
146 #endif