KeyInfo.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. if ((value.LocalName == XmlSignature.ElementNames.KeyInfo) && (value.NamespaceURI == XmlSignature.NamespaceURI)) {
  67. foreach (XmlNode n in value.ChildNodes) {
  68. KeyInfoClause kic = null;
  69. if (n is XmlWhitespace)
  70. continue;
  71. switch (n.LocalName) {
  72. case XmlSignature.ElementNames.KeyValue:
  73. XmlNodeList xnl = n.ChildNodes;
  74. if (xnl.Count > 0) {
  75. // we must now treat the whitespace !
  76. foreach (XmlNode m in xnl) {
  77. switch (m.LocalName) {
  78. case XmlSignature.ElementNames.DSAKeyValue:
  79. kic = (KeyInfoClause) new DSAKeyValue ();
  80. break;
  81. case XmlSignature.ElementNames.RSAKeyValue:
  82. kic = (KeyInfoClause) new RSAKeyValue ();
  83. break;
  84. }
  85. }
  86. }
  87. break;
  88. case XmlSignature.ElementNames.KeyName:
  89. kic = (KeyInfoClause) new KeyInfoName ();
  90. break;
  91. case XmlSignature.ElementNames.RetrievalMethod:
  92. kic = (KeyInfoClause) new KeyInfoRetrievalMethod ();
  93. break;
  94. case XmlSignature.ElementNames.X509Data:
  95. kic = (KeyInfoClause) new KeyInfoX509Data ();
  96. break;
  97. /* case XmlSignature.ElementNames.RSAKeyValue:
  98. kic = (KeyInfoClause) new RSAKeyValue ();
  99. break;*/
  100. default:
  101. kic = (KeyInfoClause) new KeyInfoNode ();
  102. break;
  103. }
  104. if (kic != null) {
  105. kic.LoadXml ((XmlElement) n);
  106. AddClause (kic);
  107. }
  108. }
  109. }
  110. }
  111. }
  112. }