New test.
[mono.git] / mcs / class / System.Security / System.Security.Cryptography.Xml / XmlDsigBase64Transform.cs
1 //
2 // XmlDsigBase64Transform.cs - Base64 Transform implementation for XML Signature
3 //
4 // Author:
5 //      Sebastien Pouliot <sebastien@ximian.com>
6 //
7 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
8 // (C) 2004 Novell (http://www.novell.com)
9 //
10
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32 using System.IO;
33 using System.Security.Cryptography;
34 using System.Text;
35 using System.Xml;
36
37 namespace System.Security.Cryptography.Xml { 
38
39         // http://www.w3.org/2000/09/xmldsig#base64
40         public class XmlDsigBase64Transform : Transform {
41
42                 private CryptoStream cs;
43
44                 public XmlDsigBase64Transform () 
45                 {
46                         Algorithm = "http://www.w3.org/2000/09/xmldsig#base64";
47                 }
48
49                 public override Type[] InputTypes {
50                         get {
51                                 Type[] input = new Type [3];
52                                 input[0] = typeof (System.IO.Stream);
53                                 input[1] = typeof (System.Xml.XmlDocument);
54                                 input[2] = typeof (System.Xml.XmlNodeList);
55                                 return input;
56                         }
57                 }
58
59                 public override Type[] OutputTypes {
60                         get {
61                                 Type[] output = new Type [1];
62                                 output[0] = typeof (System.IO.Stream);
63                                 return output;
64                         }
65                 }
66
67                 protected override XmlNodeList GetInnerXml () 
68                 {
69                         return null; // THIS IS DOCUMENTED AS SUCH
70                 }
71
72                 public override object GetOutput () 
73                 {
74                         return (object) cs;
75                 }
76
77                 public override object GetOutput (Type type) 
78                 {
79                         if (type != typeof (System.IO.Stream))
80                                 throw new ArgumentException ("type");
81                         return GetOutput ();
82                 }
83
84                 public override void LoadInnerXml (XmlNodeList nodeList) 
85                 {
86                         // documented as not changing the state of the transform
87                 }
88
89                 public override void LoadInput (object obj) 
90                 {
91                         XmlNodeList xnl = null;
92                         Stream stream = null;
93
94                         if (obj is Stream) 
95                                 stream = (obj as Stream);
96                         else if (obj is XmlDocument)
97                                 xnl = (obj as XmlDocument).SelectNodes ("//.");
98                         else if (obj is XmlNodeList)
99                                 xnl = (XmlNodeList) obj;
100
101                         if (xnl != null) {
102                                 stream = new MemoryStream ();
103                                 StreamWriter sw = new StreamWriter (stream);
104                                 foreach (XmlNode xn in xnl) {
105                                         switch (xn.NodeType) {
106                                         case XmlNodeType.Attribute:
107                                         case XmlNodeType.Text:
108                                         case XmlNodeType.CDATA:
109                                         case XmlNodeType.SignificantWhitespace:
110                                         case XmlNodeType.Whitespace:
111                                                 sw.Write (xn.Value);
112                                                 break;
113                                         }
114                                 }
115                                 sw.Flush ();
116                                 // ready to be re-used
117                                 stream.Position = 0;
118                         }
119
120                         if (stream != null)
121                                 cs = new CryptoStream (stream, new FromBase64Transform (), CryptoStreamMode.Read);
122                         // note: there is no default are other types won't throw an exception
123                 }
124         }
125 }