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.
31 using System.Collections;
35 namespace System.Security.Cryptography.Xml {
37 public class XmlDecryptionTransform : Transform {
41 EncryptedXml encryptedXml;
47 const string NamespaceUri = "http://www.w3.org/2002/07/decrypt#";
53 public XmlDecryptionTransform ()
55 Algorithm = XmlSignature.AlgorithmNamespaces.XmlDecryptionTransform;
56 encryptedXml = new EncryptedXml ();
57 exceptUris = new ArrayList ();
60 #endregion // Constructors
64 public EncryptedXml EncryptedXml {
65 get { return encryptedXml; }
66 set { encryptedXml = value; }
69 public override Type[] InputTypes {
71 if (inputTypes == null)
72 inputTypes = new Type [2] {typeof (System.IO.Stream), typeof (System.Xml.XmlDocument)};
78 public override Type[] OutputTypes {
80 if (outputTypes == null)
81 outputTypes = new Type [1] {typeof (System.Xml.XmlDocument)};
87 #endregion // Properties
91 public void AddExceptUri (string uri)
96 private void ClearExceptUris ()
101 [MonoTODO ("Verify")]
102 protected override XmlNodeList GetInnerXml ()
104 XmlDocument doc = new XmlDocument ();
105 doc.AppendChild (doc.CreateElement ("DecryptionTransform"));
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);
114 return doc.GetElementsByTagName ("Except", NamespaceUri);
117 [MonoTODO ("Verify processing of ExceptURIs")]
118 public override object GetOutput ()
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)));
128 else if (inputObj is XmlDocument) {
129 document = inputObj as XmlDocument;
132 throw new NullReferenceException ();
134 XmlNodeList nodes = document.GetElementsByTagName ("EncryptedData", EncryptedXml.XmlEncNamespaceUrl);
135 foreach (XmlNode node in nodes) {
136 if (node == document.DocumentElement && exceptUris.Contains ("#xpointer(/)"))
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)))
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));
153 public override object GetOutput (Type type)
155 if (type == typeof (Stream))
157 throw new ArgumentException ("type");
160 [MonoTODO ("verify")]
161 protected virtual bool IsTargetElement (XmlElement inputElement, string idValue)
163 if ((inputElement == null) || (idValue == null))
165 return (inputElement.Attributes ["id"].Value == idValue);
168 [MonoTODO ("This doesn't seem to work in .NET")]
169 public override void LoadInnerXml (XmlNodeList nodeList)
171 if (nodeList == null)
172 throw new NullReferenceException ();
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.");
186 public override void LoadInput (object obj)
191 #endregion // Methods