KeyInfo.cs 3.2 KB

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