KeyInfo.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. //
  2. // KeyInfo.cs - Xml Signature KeyInfo implementation
  3. //
  4. // Author:
  5. // Sebastien Pouliot ([email protected])
  6. //
  7. // (C) 2002 Motus Technologies Inc. (http://www.motus.com)
  8. //
  9. using System.Collections;
  10. using System.Text;
  11. using System.Xml;
  12. namespace System.Security.Cryptography.Xml {
  13. public class KeyInfo : IEnumerable {
  14. static private string xmldsig = "http://www.w3.org/2000/09/xmldsig#";
  15. protected ArrayList Info;
  16. protected string id;
  17. public KeyInfo()
  18. {
  19. Info = new ArrayList ();
  20. }
  21. public int Count {
  22. get { return Info.Count; }
  23. }
  24. public string Id {
  25. get { return id; }
  26. set { id = value; }
  27. }
  28. public void AddClause (KeyInfoClause clause)
  29. {
  30. Info.Add (clause);
  31. }
  32. public IEnumerator GetEnumerator ()
  33. {
  34. return Info.GetEnumerator ();
  35. }
  36. public IEnumerator GetEnumerator (Type requestedObjectType)
  37. {
  38. // Build a new ArrayList...
  39. ArrayList TypeList = new ArrayList ();
  40. IEnumerator e = Info.GetEnumerator ();
  41. while (true) {
  42. // ...with all object of specified type...
  43. if ((e.Current).GetType().Equals (requestedObjectType))
  44. TypeList.Add (e.Current);
  45. if (!e.MoveNext ())
  46. break;
  47. }
  48. // ...and return its enumerator
  49. return TypeList.GetEnumerator ();
  50. }
  51. public XmlElement GetXml ()
  52. {
  53. StringBuilder sb = new StringBuilder ();
  54. sb.Append ("<KeyInfo xmlns=\"");
  55. sb.Append (xmldsig);
  56. sb.Append ("\" />");
  57. XmlDocument doc = new XmlDocument ();
  58. doc.LoadXml (sb.ToString ());
  59. // we add References afterward so we don't end up with extraneous
  60. // xmlns="..." in each reference elements.
  61. foreach (KeyInfoClause kic in Info) {
  62. XmlNode xn = kic.GetXml ();
  63. XmlNode newNode = doc.ImportNode (xn, true);
  64. doc.DocumentElement.AppendChild (newNode);
  65. }
  66. return doc.DocumentElement;
  67. }
  68. public void LoadXml (XmlElement value)
  69. {
  70. if (value == null)
  71. throw new ArgumentNullException ("value");
  72. if ((value.LocalName == "KeyInfo") && (value.NamespaceURI == xmldsig)) {
  73. foreach (XmlNode n in value.ChildNodes) {
  74. KeyInfoClause kic = null;
  75. if (n is XmlWhitespace)
  76. continue;
  77. switch (n.LocalName) {
  78. case "KeyValue":
  79. XmlNodeList xnl = n.ChildNodes;
  80. if (xnl.Count > 0) {
  81. // we must now treat the whitespace !
  82. foreach (XmlNode m in xnl) {
  83. switch (m.LocalName) {
  84. case "DSAKeyValue":
  85. kic = (KeyInfoClause) new DSAKeyValue ();
  86. break;
  87. case "RSAKeyValue":
  88. kic = (KeyInfoClause) new RSAKeyValue ();
  89. break;
  90. }
  91. }
  92. }
  93. break;
  94. case "KeyName":
  95. kic = (KeyInfoClause) new KeyInfoName ();
  96. break;
  97. case "RetrievalMethod":
  98. kic = (KeyInfoClause) new KeyInfoRetrievalMethod ();
  99. break;
  100. case "X509Data":
  101. kic = (KeyInfoClause) new KeyInfoX509Data ();
  102. break;
  103. case "RSAKeyValue":
  104. kic = (KeyInfoClause) new RSAKeyValue ();
  105. break;
  106. default:
  107. kic = (KeyInfoClause) new KeyInfoNode ();
  108. break;
  109. }
  110. if (kic != null) {
  111. kic.LoadXml ((XmlElement) n);
  112. AddClause (kic);
  113. }
  114. }
  115. }
  116. }
  117. }
  118. }