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