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