DataObject.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. element = xel;
  68. }
  69. // why is data a XmlNodeList instead of a XmlElement ?
  70. public XmlNodeList Data {
  71. get {
  72. return element.ChildNodes;
  73. }
  74. set {
  75. if (value == null)
  76. throw new ArgumentNullException ("value");
  77. XmlDocument doc = new XmlDocument ();
  78. XmlElement el = (XmlElement) doc.ImportNode (element, true);
  79. while (el.LastChild != null)
  80. el.RemoveChild (el.LastChild);
  81. foreach (XmlNode n in value)
  82. el.AppendChild (doc.ImportNode (n, true));
  83. element = el;
  84. propertyModified = true;
  85. }
  86. }
  87. // default to null - no encoding
  88. public string Encoding {
  89. get { return GetField (XmlSignature.AttributeNames.Encoding); }
  90. set { SetField (XmlSignature.AttributeNames.Encoding, value); }
  91. }
  92. // default to null
  93. public string Id {
  94. get { return GetField (XmlSignature.AttributeNames.Id); }
  95. set { SetField (XmlSignature.AttributeNames.Id, value); }
  96. }
  97. // default to null
  98. public string MimeType {
  99. get { return GetField (XmlSignature.AttributeNames.MimeType); }
  100. set { SetField (XmlSignature.AttributeNames.MimeType, value); }
  101. }
  102. private string GetField (string attribute)
  103. {
  104. XmlNode attr = element.Attributes [attribute];
  105. return attr != null ? attr.Value : null;
  106. }
  107. private void SetField (string attribute, string value)
  108. {
  109. // MS-BUGS: it never cleans attribute value up.
  110. if (value == null)
  111. return;
  112. if (propertyModified)
  113. element.SetAttribute (attribute, value);
  114. else {
  115. XmlDocument document = new XmlDocument ();
  116. XmlElement el = document.ImportNode (element, true) as XmlElement;
  117. el.SetAttribute (attribute, value);
  118. element = el;
  119. propertyModified = true;
  120. }
  121. }
  122. public XmlElement GetXml ()
  123. {
  124. if (propertyModified) {
  125. // It looks MS.NET returns element which comes from new XmlDocument every time
  126. XmlElement oldElement = element;
  127. XmlDocument doc = new XmlDocument ();
  128. element = doc.CreateElement (XmlSignature.ElementNames.Object, XmlSignature.NamespaceURI);
  129. foreach (XmlAttribute attribute in oldElement.Attributes) {
  130. switch (attribute.Name) {
  131. case XmlSignature.AttributeNames.Id:
  132. case XmlSignature.AttributeNames.Encoding:
  133. case XmlSignature.AttributeNames.MimeType:
  134. element.SetAttribute (attribute.Name, attribute.Value);
  135. break;
  136. }
  137. }
  138. foreach (XmlNode n in oldElement.ChildNodes)
  139. element.AppendChild (doc.ImportNode (n, true));
  140. }
  141. return element;
  142. }
  143. public void LoadXml (XmlElement value)
  144. {
  145. if (value == null)
  146. throw new ArgumentNullException ("value");
  147. element = value;
  148. propertyModified = false;
  149. }
  150. }
  151. }