XmlDecryptionTransform.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. //
  2. // XmlDecryptionTransform.cs - XmlDecryptionTransform implementation for XML Encryption
  3. //
  4. // Author:
  5. // Tim Coleman ([email protected])
  6. //
  7. // Copyright (C) Tim Coleman, 2004
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. #if NET_2_0
  29. using System.Collections;
  30. using System.IO;
  31. using System.Xml;
  32. namespace System.Security.Cryptography.Xml {
  33. public class XmlDecryptionTransform : Transform {
  34. #region Fields
  35. EncryptedXml encryptedXml;
  36. Type[] inputTypes;
  37. Type[] outputTypes;
  38. object inputObj;
  39. ArrayList exceptUris;
  40. XmlNodeList innerXml;
  41. object lockObject;
  42. const string NamespaceUri = "http://www.w3.org/2002/07/decrypt#";
  43. #endregion // Fields
  44. #region Constructors
  45. public XmlDecryptionTransform ()
  46. : base ()
  47. {
  48. Algorithm = XmlSignature.AlgorithmNamespaces.XmlDecryptionTransform;
  49. encryptedXml = new EncryptedXml ();
  50. exceptUris = new ArrayList ();
  51. lockObject = new object ();
  52. }
  53. #endregion // Constructors
  54. #region Properties
  55. public EncryptedXml EncryptedXml {
  56. get { return encryptedXml; }
  57. set { encryptedXml = value; }
  58. }
  59. public override Type[] InputTypes {
  60. get {
  61. if (inputTypes == null) {
  62. lock (lockObject) {
  63. inputTypes = new Type [2] {typeof (System.IO.Stream), typeof (System.Xml.XmlDocument)};
  64. }
  65. }
  66. return inputTypes;
  67. }
  68. }
  69. public override Type[] OutputTypes {
  70. get {
  71. if (outputTypes == null) {
  72. lock (lockObject) {
  73. outputTypes = new Type [1] {typeof (System.Xml.XmlDocument)};
  74. }
  75. }
  76. return outputTypes;
  77. }
  78. }
  79. #endregion // Properties
  80. #region Methods
  81. public void AddExceptUri (string uri)
  82. {
  83. exceptUris.Add (uri);
  84. }
  85. private void ClearExceptUris ()
  86. {
  87. exceptUris.Clear ();
  88. }
  89. [MonoTODO ("Verify")]
  90. protected override XmlNodeList GetInnerXml ()
  91. {
  92. XmlDocument doc = new XmlDocument ();
  93. doc.AppendChild (doc.CreateElement ("DecryptionTransform"));
  94. foreach (object o in exceptUris) {
  95. XmlElement element = doc.CreateElement ("Except", NamespaceUri);
  96. element.Attributes.Append (doc.CreateAttribute ("URI", NamespaceUri));
  97. element.Attributes ["URI", NamespaceUri].Value = (string) o;
  98. doc.DocumentElement.AppendChild (element);
  99. }
  100. return doc.GetElementsByTagName ("Except", NamespaceUri);
  101. }
  102. [MonoTODO ("Verify processing of ExceptURIs")]
  103. public override object GetOutput ()
  104. {
  105. XmlDocument document;
  106. if (inputObj is Stream) {
  107. document = new XmlDocument ();
  108. document.PreserveWhitespace = true;
  109. #if NET_1_1
  110. document.XmlResolver = GetResolver ();
  111. #endif
  112. document.Load (inputObj as Stream);
  113. }
  114. else if (inputObj is XmlDocument) {
  115. document = inputObj as XmlDocument;
  116. }
  117. else
  118. throw new NullReferenceException ();
  119. XmlNodeList nodes = document.GetElementsByTagName ("EncryptedData", EncryptedXml.XmlEncNamespaceUrl);
  120. foreach (XmlNode node in nodes) {
  121. if (node == document.DocumentElement && exceptUris.Contains ("#xpointer(/)"))
  122. break;
  123. // Need to exclude based on ExceptURI. Only accept #id references.
  124. foreach (string uri in exceptUris)
  125. if (IsTargetElement ((XmlElement) node, uri.Substring (1)))
  126. break;
  127. EncryptedData encryptedData = new EncryptedData ();
  128. encryptedData.LoadXml ((XmlElement) node);
  129. SymmetricAlgorithm symAlg = EncryptedXml.GetDecryptionKey (encryptedData, encryptedData.EncryptionMethod.KeyAlgorithm);
  130. EncryptedXml.ReplaceData ((XmlElement) node, EncryptedXml.DecryptData (encryptedData, symAlg));
  131. }
  132. return document;
  133. }
  134. public override object GetOutput (Type type)
  135. {
  136. if (type == Type.GetType ("Stream"))
  137. return GetOutput ();
  138. throw new ArgumentException ("type");
  139. }
  140. [MonoTODO ("verify")]
  141. protected virtual bool IsTargetElement (XmlElement inputElement, string idValue)
  142. {
  143. return (inputElement.Attributes ["id"].Value == idValue);
  144. }
  145. [MonoTODO ("This doesn't seem to work in .NET")]
  146. public override void LoadInnerXml (XmlNodeList nodeList)
  147. {
  148. if (nodeList == null)
  149. throw new NullReferenceException ();
  150. ClearExceptUris ();
  151. foreach (XmlNode node in nodeList) {
  152. XmlElement element = node as XmlElement;
  153. if (element.NamespaceURI.Equals (NamespaceUri) && element.LocalName.Equals ("Except")) {
  154. string uri = element.Attributes ["URI", NamespaceUri].Value;
  155. if (!uri.StartsWith ("#"))
  156. throw new CryptographicException ("A Uri attribute is required for a CipherReference element.");
  157. AddExceptUri (uri);
  158. }
  159. }
  160. }
  161. public override void LoadInput (object obj)
  162. {
  163. inputObj = obj;
  164. }
  165. #endregion // Methods
  166. }
  167. }
  168. #endif