Manifest.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. //
  2. // Manifest.cs - Manifest implementation for XML Signature
  3. //
  4. // Author:
  5. // Sebastien Pouliot <[email protected]>
  6. //
  7. // (C) 2004 Novell (http://www.novell.com)
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System.Collections;
  30. using System.Xml;
  31. namespace System.Security.Cryptography.Xml {
  32. internal class Manifest {
  33. private ArrayList references;
  34. private string id;
  35. private XmlElement element;
  36. public Manifest ()
  37. {
  38. references = new ArrayList ();
  39. }
  40. public Manifest (XmlElement xel) : this ()
  41. {
  42. LoadXml (xel);
  43. }
  44. public string Id {
  45. get { return id; }
  46. set {
  47. element = null;
  48. id = value;
  49. }
  50. }
  51. public ArrayList References {
  52. get { return references; }
  53. }
  54. public void AddReference (Reference reference)
  55. {
  56. references.Add (reference);
  57. }
  58. public XmlElement GetXml ()
  59. {
  60. if (element != null)
  61. return element;
  62. XmlDocument document = new XmlDocument ();
  63. XmlElement xel = document.CreateElement (XmlSignature.ElementNames.SignedInfo, XmlSignature.NamespaceURI);
  64. if (id != null)
  65. xel.SetAttribute (XmlSignature.AttributeNames.Id, id);
  66. // we add References afterward so we don't end up with extraneous
  67. // xmlns="..." in each reference elements.
  68. foreach (Reference r in references) {
  69. XmlNode xn = r.GetXml ();
  70. XmlNode newNode = document.ImportNode (xn, true);
  71. xel.AppendChild (newNode);
  72. }
  73. return xel;
  74. }
  75. private string GetAttribute (XmlElement xel, string attribute)
  76. {
  77. XmlAttribute xa = xel.Attributes [attribute];
  78. return ((xa != null) ? xa.InnerText : null);
  79. }
  80. public void LoadXml (XmlElement value)
  81. {
  82. if (value == null)
  83. throw new ArgumentNullException ("value");
  84. if ((value.LocalName != XmlSignature.ElementNames.Manifest) || (value.NamespaceURI != XmlSignature.NamespaceURI))
  85. throw new CryptographicException ();
  86. id = GetAttribute (value, XmlSignature.AttributeNames.Id);
  87. for (int i = 0; i < value.ChildNodes.Count; i++) {
  88. XmlNode n = value.ChildNodes [i];
  89. if (n.NodeType == XmlNodeType.Element &&
  90. n.LocalName == XmlSignature.ElementNames.Reference &&
  91. n.NamespaceURI == XmlSignature.NamespaceURI) {
  92. Reference r = new Reference ();
  93. r.LoadXml ((XmlElement) n);
  94. AddReference (r);
  95. }
  96. }
  97. element = value;
  98. }
  99. }
  100. }