DataObject.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. //
  8. // (C) 2002 Motus Technologies Inc. (http://www.motus.com)
  9. //
  10. using System.Security.Cryptography;
  11. using System.Text;
  12. using System.Xml;
  13. namespace System.Security.Cryptography.Xml {
  14. // XmlElement part of the signature
  15. // Note: Looks like KeyInfoNode (but the later is XmlElement inside KeyInfo)
  16. // required for "enveloping signatures"
  17. public class DataObject {
  18. private string id;
  19. private string mimeType;
  20. private string encoding;
  21. private string data;
  22. private XmlDocument doc;
  23. static private string xmldsig = "http://www.w3.org/2000/09/xmldsig#";
  24. public DataObject ()
  25. {
  26. Build (null, null, null, null);
  27. }
  28. public DataObject (string id, string mimeType, string encoding, XmlElement data)
  29. {
  30. if (data == null)
  31. throw new ArgumentNullException ("data");
  32. Build (id, mimeType, encoding, data);
  33. }
  34. private void Build (string id, string mimeType, string encoding, XmlElement data)
  35. {
  36. StringBuilder sb = new StringBuilder ();
  37. sb.Append ("<Object ");
  38. if (id != null) {
  39. this.id = id;
  40. sb.Append ("Id=\"");
  41. sb.Append (id);
  42. sb.Append ("\" ");
  43. }
  44. if (mimeType != null) {
  45. this.mimeType = mimeType;
  46. sb.Append ("MimeType=\"");
  47. sb.Append (mimeType);
  48. sb.Append ("\" ");
  49. }
  50. if (encoding != null) {
  51. this.encoding = encoding;
  52. sb.Append ("Encoding=\"");
  53. sb.Append (encoding);
  54. sb.Append ("\" ");
  55. }
  56. sb.Append ("xmlns=\"http://www.w3.org/2000/09/xmldsig#\" />");
  57. doc = new XmlDocument ();
  58. doc.LoadXml (sb.ToString ());
  59. if (data != null) {
  60. XmlNodeList xnl = doc.GetElementsByTagName ("Object");
  61. XmlNode newNode = doc.ImportNode (data, true);
  62. xnl[0].AppendChild (newNode);
  63. }
  64. }
  65. // why is data a XmlNodeList instead of a XmlElement ?
  66. public XmlNodeList Data {
  67. get {
  68. XmlNodeList xnl = doc.GetElementsByTagName ("Object");
  69. return xnl[0].ChildNodes;
  70. }
  71. set {
  72. if (value == null)
  73. throw new ArgumentNullException ("value");
  74. Build (id, mimeType, encoding, null);
  75. XmlNodeList xnl = doc.GetElementsByTagName ("Object");
  76. if ((xnl != null) && (xnl.Count > 0)) {
  77. foreach (XmlNode xn in value) {
  78. XmlNode newNode = doc.ImportNode (xn, true);
  79. xnl[0].AppendChild (newNode);
  80. }
  81. }
  82. }
  83. }
  84. // default to null - no encoding
  85. public string Encoding {
  86. get { return encoding; }
  87. set { encoding = value; }
  88. }
  89. // default to null
  90. public string Id {
  91. get { return id; }
  92. set { id = value; }
  93. }
  94. // default to null
  95. public string MimeType {
  96. get { return mimeType; }
  97. set { mimeType = value; }
  98. }
  99. public XmlElement GetXml ()
  100. {
  101. if ((doc.DocumentElement.LocalName == "Object") && (doc.DocumentElement.NamespaceURI == "http://www.w3.org/2000/09/xmldsig#")) {
  102. // recreate all attributes in order
  103. XmlAttribute xa = null;
  104. doc.DocumentElement.Attributes.RemoveAll ();
  105. if (id != null) {
  106. xa = doc.CreateAttribute ("Id");
  107. xa.Value = id;
  108. doc.DocumentElement.Attributes.Append (xa);
  109. }
  110. if (mimeType != null) {
  111. xa = doc.CreateAttribute ("MimeType");
  112. xa.Value = mimeType;
  113. doc.DocumentElement.Attributes.Append (xa);
  114. }
  115. if (encoding != null) {
  116. xa = doc.CreateAttribute ("Encoding");
  117. xa.Value = encoding;
  118. doc.DocumentElement.Attributes.Append (xa);
  119. }
  120. xa = doc.CreateAttribute ("xmlns");
  121. xa.Value = xmldsig;
  122. doc.DocumentElement.Attributes.Append (xa);
  123. }
  124. return doc.DocumentElement;
  125. }
  126. public void LoadXml (XmlElement value)
  127. {
  128. if (value == null)
  129. throw new ArgumentNullException ("value");
  130. if ((value.LocalName == "Object") && (value.NamespaceURI == "http://www.w3.org/2000/09/xmldsig#")) {
  131. doc.LoadXml (value.OuterXml);
  132. XmlAttribute xa = value.Attributes ["Id"];
  133. id = ((xa != null) ? xa.InnerText : null);
  134. xa = value.Attributes ["MimeType"];
  135. mimeType = ((xa != null) ? xa.InnerText : null);
  136. xa = value.Attributes ["Encoding"];
  137. encoding = ((xa != null) ? xa.InnerText : null);
  138. }
  139. else
  140. doc.LoadXml (value.OuterXml);
  141. }
  142. }
  143. }