DataObject.cs 5.6 KB

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