XmlDecryptionTransform.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. object lockObject;
  41. const string NamespaceUri = "http://www.w3.org/2002/07/decrypt#";
  42. #endregion // Fields
  43. #region Constructors
  44. public XmlDecryptionTransform ()
  45. {
  46. Algorithm = XmlSignature.AlgorithmNamespaces.XmlDecryptionTransform;
  47. encryptedXml = new EncryptedXml ();
  48. exceptUris = new ArrayList ();
  49. lockObject = new object ();
  50. }
  51. #endregion // Constructors
  52. #region Properties
  53. public EncryptedXml EncryptedXml {
  54. get { return encryptedXml; }
  55. set { encryptedXml = value; }
  56. }
  57. public override Type[] InputTypes {
  58. get {
  59. if (inputTypes == null) {
  60. lock (lockObject) {
  61. inputTypes = new Type [2] {typeof (System.IO.Stream), typeof (System.Xml.XmlDocument)};
  62. }
  63. }
  64. return inputTypes;
  65. }
  66. }
  67. public override Type[] OutputTypes {
  68. get {
  69. if (outputTypes == null) {
  70. lock (lockObject) {
  71. outputTypes = new Type [1] {typeof (System.Xml.XmlDocument)};
  72. }
  73. }
  74. return outputTypes;
  75. }
  76. }
  77. #endregion // Properties
  78. #region Methods
  79. public void AddExceptUri (string uri)
  80. {
  81. exceptUris.Add (uri);
  82. }
  83. private void ClearExceptUris ()
  84. {
  85. exceptUris.Clear ();
  86. }
  87. [MonoTODO ("Verify")]
  88. protected override XmlNodeList GetInnerXml ()
  89. {
  90. XmlDocument doc = new XmlDocument ();
  91. doc.AppendChild (doc.CreateElement ("DecryptionTransform"));
  92. foreach (object o in exceptUris) {
  93. XmlElement element = doc.CreateElement ("Except", NamespaceUri);
  94. element.Attributes.Append (doc.CreateAttribute ("URI", NamespaceUri));
  95. element.Attributes ["URI", NamespaceUri].Value = (string) o;
  96. doc.DocumentElement.AppendChild (element);
  97. }
  98. return doc.GetElementsByTagName ("Except", NamespaceUri);
  99. }
  100. [MonoTODO ("Verify processing of ExceptURIs")]
  101. public override object GetOutput ()
  102. {
  103. XmlDocument document;
  104. if (inputObj is Stream) {
  105. document = new XmlDocument ();
  106. document.PreserveWhitespace = true;
  107. document.XmlResolver = GetResolver ();
  108. document.Load (new XmlSignatureStreamReader (
  109. new StreamReader (inputObj as Stream)));
  110. }
  111. else if (inputObj is XmlDocument) {
  112. document = inputObj as XmlDocument;
  113. }
  114. else
  115. throw new NullReferenceException ();
  116. XmlNodeList nodes = document.GetElementsByTagName ("EncryptedData", EncryptedXml.XmlEncNamespaceUrl);
  117. foreach (XmlNode node in nodes) {
  118. if (node == document.DocumentElement && exceptUris.Contains ("#xpointer(/)"))
  119. break;
  120. // Need to exclude based on ExceptURI. Only accept #id references.
  121. foreach (string uri in exceptUris)
  122. if (IsTargetElement ((XmlElement) node, uri.Substring (1)))
  123. break;
  124. EncryptedData encryptedData = new EncryptedData ();
  125. encryptedData.LoadXml ((XmlElement) node);
  126. SymmetricAlgorithm symAlg = EncryptedXml.GetDecryptionKey (encryptedData, encryptedData.EncryptionMethod.KeyAlgorithm);
  127. EncryptedXml.ReplaceData ((XmlElement) node, EncryptedXml.DecryptData (encryptedData, symAlg));
  128. }
  129. return document;
  130. }
  131. public override object GetOutput (Type type)
  132. {
  133. if (type == Type.GetType ("Stream"))
  134. return GetOutput ();
  135. throw new ArgumentException ("type");
  136. }
  137. [MonoTODO ("verify")]
  138. protected virtual bool IsTargetElement (XmlElement inputElement, string idValue)
  139. {
  140. return (inputElement.Attributes ["id"].Value == idValue);
  141. }
  142. [MonoTODO ("This doesn't seem to work in .NET")]
  143. public override void LoadInnerXml (XmlNodeList nodeList)
  144. {
  145. if (nodeList == null)
  146. throw new NullReferenceException ();
  147. ClearExceptUris ();
  148. foreach (XmlNode node in nodeList) {
  149. XmlElement element = node as XmlElement;
  150. if (element.NamespaceURI.Equals (NamespaceUri) && element.LocalName.Equals ("Except")) {
  151. string uri = element.Attributes ["URI", NamespaceUri].Value;
  152. if (!uri.StartsWith ("#"))
  153. throw new CryptographicException ("A Uri attribute is required for a CipherReference element.");
  154. AddExceptUri (uri);
  155. }
  156. }
  157. }
  158. public override void LoadInput (object obj)
  159. {
  160. inputObj = obj;
  161. }
  162. #endregion // Methods
  163. }
  164. }
  165. #endif