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;
49 const string NamespaceUri = "http://www.w3.org/2002/07/decrypt#";
55 public XmlDecryptionTransform ()
57 Algorithm = XmlSignature.AlgorithmNamespaces.XmlDecryptionTransform;
58 encryptedXml = new EncryptedXml ();
59 exceptUris = new ArrayList ();
60 lockObject = new object ();
63 #endregion // Constructors
67 public EncryptedXml EncryptedXml {
68 get { return encryptedXml; }
69 set { encryptedXml = value; }
72 public override Type[] InputTypes {
74 if (inputTypes == null) {
76 inputTypes = new Type [2] {typeof (System.IO.Stream), typeof (System.Xml.XmlDocument)};
83 public override Type[] OutputTypes {
85 if (outputTypes == null) {
87 outputTypes = new Type [1] {typeof (System.Xml.XmlDocument)};
94 #endregion // Properties
98 public void AddExceptUri (string uri)
100 exceptUris.Add (uri);
103 private void ClearExceptUris ()
108 [MonoTODO ("Verify")]
109 protected override XmlNodeList GetInnerXml ()
111 XmlDocument doc = new XmlDocument ();
112 doc.AppendChild (doc.CreateElement ("DecryptionTransform"));
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);
121 return doc.GetElementsByTagName ("Except", NamespaceUri);
124 [MonoTODO ("Verify processing of ExceptURIs")]
125 public override object GetOutput ()
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)));
135 else if (inputObj is XmlDocument) {
136 document = inputObj as XmlDocument;
139 throw new NullReferenceException ();
141 XmlNodeList nodes = document.GetElementsByTagName ("EncryptedData", EncryptedXml.XmlEncNamespaceUrl);
142 foreach (XmlNode node in nodes) {
143 if (node == document.DocumentElement && exceptUris.Contains ("#xpointer(/)"))
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)))
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));
160 public override object GetOutput (Type type)
162 if (type == Type.GetType ("Stream"))
164 throw new ArgumentException ("type");
167 [MonoTODO ("verify")]
168 protected virtual bool IsTargetElement (XmlElement inputElement, string idValue)
170 return (inputElement.Attributes ["id"].Value == idValue);
173 [MonoTODO ("This doesn't seem to work in .NET")]
174 public override void LoadInnerXml (XmlNodeList nodeList)
176 if (nodeList == null)
177 throw new NullReferenceException ();
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.");
191 public override void LoadInput (object obj)
196 #endregion // Methods