2 // XmlDecryptionTransform.cs - XmlDecryptionTransform implementation for XML Encryption
5 // Tim Coleman (tim@timcoleman.com)
7 // Copyright (C) Tim Coleman, 2004
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
32 using System.Collections;
36 namespace System.Security.Cryptography.Xml {
38 public class XmlDecryptionTransform : Transform {
42 EncryptedXml encryptedXml;
48 const string NamespaceUri = "http://www.w3.org/2002/07/decrypt#";
54 public XmlDecryptionTransform ()
56 Algorithm = XmlSignature.AlgorithmNamespaces.XmlDecryptionTransform;
57 encryptedXml = new EncryptedXml ();
58 exceptUris = new ArrayList ();
61 #endregion // Constructors
65 public EncryptedXml EncryptedXml {
66 get { return encryptedXml; }
67 set { encryptedXml = value; }
70 public override Type[] InputTypes {
72 if (inputTypes == null)
73 inputTypes = new Type [2] {typeof (System.IO.Stream), typeof (System.Xml.XmlDocument)};
79 public override Type[] OutputTypes {
81 if (outputTypes == null)
82 outputTypes = new Type [1] {typeof (System.Xml.XmlDocument)};
88 #endregion // Properties
92 public void AddExceptUri (string uri)
97 private void ClearExceptUris ()
102 [MonoTODO ("Verify")]
103 protected override XmlNodeList GetInnerXml ()
105 XmlDocument doc = new XmlDocument ();
106 doc.AppendChild (doc.CreateElement ("DecryptionTransform"));
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);
115 return doc.GetElementsByTagName ("Except", NamespaceUri);
118 [MonoTODO ("Verify processing of ExceptURIs")]
119 public override object GetOutput ()
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)));
129 else if (inputObj is XmlDocument) {
130 document = inputObj as XmlDocument;
133 throw new NullReferenceException ();
135 XmlNodeList nodes = document.GetElementsByTagName ("EncryptedData", EncryptedXml.XmlEncNamespaceUrl);
136 foreach (XmlNode node in nodes) {
137 if (node == document.DocumentElement && exceptUris.Contains ("#xpointer(/)"))
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)))
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));
154 public override object GetOutput (Type type)
156 if (type == typeof (Stream))
158 throw new ArgumentException ("type");
161 [MonoTODO ("verify")]
162 protected virtual bool IsTargetElement (XmlElement inputElement, string idValue)
164 if ((inputElement == null) || (idValue == null))
166 return (inputElement.Attributes ["id"].Value == idValue);
169 [MonoTODO ("This doesn't seem to work in .NET")]
170 public override void LoadInnerXml (XmlNodeList nodeList)
172 if (nodeList == null)
173 throw new NullReferenceException ();
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.");
187 public override void LoadInput (object obj)
192 #endregion // Methods