XmlDecryptionTransform.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. document.XmlResolver = GetResolver ();
  110. document.Load (new XmlSignatureStreamReader (
  111. new StreamReader (inputObj as Stream)));
  112. }
  113. else if (inputObj is XmlDocument) {
  114. document = inputObj as XmlDocument;
  115. }
  116. else
  117. throw new NullReferenceException ();
  118. XmlNodeList nodes = document.GetElementsByTagName ("EncryptedData", EncryptedXml.XmlEncNamespaceUrl);
  119. foreach (XmlNode node in nodes) {
  120. if (node == document.DocumentElement && exceptUris.Contains ("#xpointer(/)"))
  121. break;
  122. // Need to exclude based on ExceptURI. Only accept #id references.
  123. foreach (string uri in exceptUris)
  124. if (IsTargetElement ((XmlElement) node, uri.Substring (1)))
  125. break;
  126. EncryptedData encryptedData = new EncryptedData ();
  127. encryptedData.LoadXml ((XmlElement) node);
  128. SymmetricAlgorithm symAlg = EncryptedXml.GetDecryptionKey (encryptedData, encryptedData.EncryptionMethod.KeyAlgorithm);
  129. EncryptedXml.ReplaceData ((XmlElement) node, EncryptedXml.DecryptData (encryptedData, symAlg));
  130. }
  131. return document;
  132. }
  133. public override object GetOutput (Type type)
  134. {
  135. if (type == Type.GetType ("Stream"))
  136. return GetOutput ();
  137. throw new ArgumentException ("type");
  138. }
  139. [MonoTODO ("verify")]
  140. protected virtual bool IsTargetElement (XmlElement inputElement, string idValue)
  141. {
  142. return (inputElement.Attributes ["id"].Value == idValue);
  143. }
  144. [MonoTODO ("This doesn't seem to work in .NET")]
  145. public override void LoadInnerXml (XmlNodeList nodeList)
  146. {
  147. if (nodeList == null)
  148. throw new NullReferenceException ();
  149. ClearExceptUris ();
  150. foreach (XmlNode node in nodeList) {
  151. XmlElement element = node as XmlElement;
  152. if (element.NamespaceURI.Equals (NamespaceUri) && element.LocalName.Equals ("Except")) {
  153. string uri = element.Attributes ["URI", NamespaceUri].Value;
  154. if (!uri.StartsWith ("#"))
  155. throw new CryptographicException ("A Uri attribute is required for a CipherReference element.");
  156. AddExceptUri (uri);
  157. }
  158. }
  159. }
  160. public override void LoadInput (object obj)
  161. {
  162. inputObj = obj;
  163. }
  164. #endregion // Methods
  165. }
  166. }
  167. #endif