DataObject.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. //
  2. // DataObject.cs - DataObject implementation for XML Signature
  3. // http://www.w3.org/2000/09/xmldsig#Object
  4. //
  5. // Author:
  6. // Sebastien Pouliot <[email protected]>
  7. // Atsushi Enomoto ([email protected])
  8. //
  9. // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
  10. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System.Xml;
  32. namespace System.Security.Cryptography.Xml {
  33. // XmlElement part of the signature
  34. // Note: Looks like KeyInfoNode (but the later is XmlElement inside KeyInfo)
  35. // required for "enveloping signatures"
  36. public class DataObject {
  37. private XmlElement element;
  38. private bool propertyModified;
  39. public DataObject ()
  40. {
  41. Build (null, null, null, null);
  42. }
  43. public DataObject (string id, string mimeType, string encoding, XmlElement data)
  44. {
  45. if (data == null)
  46. throw new ArgumentNullException ("data");
  47. Build (id, mimeType, encoding, data);
  48. }
  49. // this one accept a null "data" parameter
  50. private void Build (string id, string mimeType, string encoding, XmlElement data)
  51. {
  52. XmlDocument document = new XmlDocument ();
  53. XmlElement xel = document.CreateElement (XmlSignature.ElementNames.Object, XmlSignature.NamespaceURI);
  54. if (id != null) {
  55. xel.SetAttribute (XmlSignature.AttributeNames.Id, id);
  56. }
  57. if (mimeType != null) {
  58. xel.SetAttribute (XmlSignature.AttributeNames.MimeType, mimeType);
  59. }
  60. if (encoding != null) {
  61. xel.SetAttribute (XmlSignature.AttributeNames.Encoding, encoding);
  62. }
  63. if (data != null) {
  64. XmlNode newNode = document.ImportNode (data, true);
  65. xel.AppendChild (newNode);
  66. }
  67. document.AppendChild (xel); // FIXME: it should not be appended
  68. element = document.DocumentElement;
  69. }
  70. // why is data a XmlNodeList instead of a XmlElement ?
  71. public XmlNodeList Data {
  72. get {
  73. return element.ChildNodes;
  74. }
  75. set {
  76. if (value == null)
  77. throw new ArgumentNullException ("value");
  78. XmlDocument doc = new XmlDocument ();
  79. XmlElement el = (XmlElement) doc.ImportNode (element, true);
  80. doc.AppendChild (el); // FIXME: it should not be appended
  81. el.RemoveAll ();
  82. foreach (XmlNode n in value)
  83. el.AppendChild (doc.ImportNode (n, true));
  84. element = el;
  85. propertyModified = true;
  86. }
  87. }
  88. // default to null - no encoding
  89. public string Encoding {
  90. get { return GetField (XmlSignature.AttributeNames.Encoding); }
  91. set { SetField (XmlSignature.AttributeNames.Encoding, value); }
  92. }
  93. // default to null
  94. public string Id {
  95. get { return GetField (XmlSignature.AttributeNames.Id); }
  96. set { SetField (XmlSignature.AttributeNames.Id, value); }
  97. }
  98. // default to null
  99. public string MimeType {
  100. get { return GetField (XmlSignature.AttributeNames.MimeType); }
  101. set { SetField (XmlSignature.AttributeNames.MimeType, value); }
  102. }
  103. private string GetField (string attribute)
  104. {
  105. XmlNode attr = element.Attributes [attribute];
  106. return attr != null ? attr.Value : null;
  107. }
  108. private void SetField (string attribute, string value)
  109. {
  110. // MS-BUGS: it never cleans attribute value up.
  111. if (value == null)
  112. return;
  113. if (propertyModified)
  114. element.SetAttribute (attribute, value);
  115. else {
  116. XmlDocument document = new XmlDocument ();
  117. XmlElement el = document.ImportNode (element, true) as XmlElement;
  118. el.Attributes.RemoveAll ();
  119. document.AppendChild (el); // FIXME: it should not be appended
  120. el.SetAttribute (attribute, value);
  121. element = el;
  122. propertyModified = true;
  123. }
  124. }
  125. public XmlElement GetXml ()
  126. {
  127. if (propertyModified) {
  128. // It looks MS.NET returns element which comes from new XmlDocument every time
  129. XmlElement oldElement = element;
  130. XmlDocument doc = new XmlDocument ();
  131. element = doc.CreateElement (XmlSignature.ElementNames.Object, XmlSignature.NamespaceURI);
  132. doc.AppendChild (element); // FIXME: it should not be appended
  133. foreach (XmlAttribute attribute in oldElement.Attributes) {
  134. switch (attribute.Name) {
  135. case XmlSignature.AttributeNames.Id:
  136. case XmlSignature.AttributeNames.Encoding:
  137. case XmlSignature.AttributeNames.MimeType:
  138. element.SetAttribute (attribute.Name, attribute.Value);
  139. break;
  140. }
  141. }
  142. foreach (XmlNode n in oldElement.ChildNodes)
  143. element.AppendChild (doc.ImportNode (n, true));
  144. }
  145. return element;
  146. }
  147. public void LoadXml (XmlElement value)
  148. {
  149. if (value == null)
  150. throw new ArgumentNullException ("value");
  151. element = value;
  152. propertyModified = false;
  153. }
  154. }
  155. }