New test.
[mono.git] / mcs / class / System.Security / System.Security.Cryptography.Xml / XmlDecryptionTransform.cs
1 //
2 // XmlDecryptionTransform.cs - XmlDecryptionTransform 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 System.Collections;
33 using System.IO;
34 using System.Xml;
35
36 namespace System.Security.Cryptography.Xml {
37
38         public class XmlDecryptionTransform : Transform {
39
40                 #region Fields
41
42                 EncryptedXml encryptedXml;
43                 Type[] inputTypes;
44                 Type[] outputTypes;
45                 object inputObj;
46                 ArrayList exceptUris;
47                 object lockObject;
48
49                 const string NamespaceUri = "http://www.w3.org/2002/07/decrypt#";
50
51                 #endregion // Fields
52
53                 #region Constructors
54         
55                 public XmlDecryptionTransform ()
56                 {
57                         Algorithm = XmlSignature.AlgorithmNamespaces.XmlDecryptionTransform;
58                         encryptedXml = new EncryptedXml ();
59                         exceptUris = new ArrayList ();
60                         lockObject = new object ();
61                 }
62         
63                 #endregion // Constructors
64
65                 #region Properties
66
67                 public EncryptedXml EncryptedXml {
68                         get { return encryptedXml; }
69                         set { encryptedXml = value; }
70                 }
71
72                 public override Type[] InputTypes {
73                         get { 
74                                 if (inputTypes == null) {
75                                         lock (lockObject) {
76                                                 inputTypes = new Type [2] {typeof (System.IO.Stream), typeof (System.Xml.XmlDocument)}; 
77                                         }
78                                 }
79                                 return inputTypes;
80                         }
81                 }
82
83                 public override Type[] OutputTypes {
84                         get { 
85                                 if (outputTypes == null) {
86                                         lock (lockObject) {
87                                                 outputTypes = new Type [1] {typeof (System.Xml.XmlDocument)};
88                                         }
89                                 }
90                                 return outputTypes;
91                         }
92                 }
93
94                 #endregion // Properties
95
96                 #region Methods
97
98                 public void AddExceptUri (string uri)
99                 {
100                         exceptUris.Add (uri);
101                 }
102
103                 private void ClearExceptUris ()
104                 {
105                         exceptUris.Clear ();
106                 }
107
108                 [MonoTODO ("Verify")]
109                 protected override XmlNodeList GetInnerXml ()
110                 {
111                         XmlDocument doc = new XmlDocument ();
112                         doc.AppendChild (doc.CreateElement ("DecryptionTransform"));
113
114                         foreach (object o in exceptUris) {
115                                 XmlElement element = doc.CreateElement ("Except", NamespaceUri);
116                                 element.Attributes.Append (doc.CreateAttribute ("URI", NamespaceUri));
117                                 element.Attributes ["URI", NamespaceUri].Value = (string) o;
118                                 doc.DocumentElement.AppendChild (element);
119                         }
120
121                         return doc.GetElementsByTagName ("Except", NamespaceUri);
122                 }
123
124                 [MonoTODO ("Verify processing of ExceptURIs")]
125                 public override object GetOutput ()
126                 {
127                         XmlDocument document;
128                         if (inputObj is Stream) {
129                                 document = new XmlDocument ();
130                                 document.PreserveWhitespace = true;
131                                 document.XmlResolver = GetResolver ();
132                                 document.Load (new XmlSignatureStreamReader (
133                                         new StreamReader (inputObj as Stream)));
134                         }
135                         else if (inputObj is XmlDocument) {
136                                 document = inputObj as XmlDocument;
137                         }
138                         else
139                                 throw new NullReferenceException ();
140
141                         XmlNodeList nodes = document.GetElementsByTagName ("EncryptedData", EncryptedXml.XmlEncNamespaceUrl);
142                         foreach (XmlNode node in nodes) {
143                                 if (node == document.DocumentElement && exceptUris.Contains ("#xpointer(/)"))
144                                         break;
145
146                                 // Need to exclude based on ExceptURI.  Only accept #id references.
147                                 foreach (string uri in exceptUris) 
148                                         if (IsTargetElement ((XmlElement) node, uri.Substring (1)))
149                                                 break;
150
151                                 EncryptedData encryptedData = new EncryptedData ();
152                                 encryptedData.LoadXml ((XmlElement) node);
153                                 SymmetricAlgorithm symAlg = EncryptedXml.GetDecryptionKey (encryptedData, encryptedData.EncryptionMethod.KeyAlgorithm);
154                                 EncryptedXml.ReplaceData ((XmlElement) node, EncryptedXml.DecryptData (encryptedData, symAlg));
155                         }
156
157                         return document;
158                 }
159
160                 public override object GetOutput (Type type)
161                 {       
162                         if (type == Type.GetType ("Stream"))
163                                 return GetOutput ();
164                         throw new ArgumentException ("type");
165                 }
166
167                 [MonoTODO ("verify")]
168                 protected virtual bool IsTargetElement (XmlElement inputElement, string idValue)
169                 {
170                         return (inputElement.Attributes ["id"].Value == idValue);
171                 }
172
173                 [MonoTODO ("This doesn't seem to work in .NET")]
174                 public override void LoadInnerXml (XmlNodeList nodeList)
175                 {
176                         if (nodeList == null)
177                                 throw new NullReferenceException ();
178
179                         ClearExceptUris ();
180                         foreach (XmlNode node in nodeList) {
181                                 XmlElement element = node as XmlElement;
182                                 if (element.NamespaceURI.Equals (NamespaceUri) && element.LocalName.Equals ("Except")) {
183                                         string uri = element.Attributes ["URI", NamespaceUri].Value;
184                                         if (!uri.StartsWith ("#"))
185                                                 throw new CryptographicException ("A Uri attribute is required for a CipherReference element.");
186                                         AddExceptUri (uri);
187                                 }
188                         }
189                 }
190
191                 public override void LoadInput (object obj)
192                 {
193                         inputObj = obj;
194                 }
195
196                 #endregion // Methods
197         }
198 }
199
200 #endif