KeyInfo.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. //
  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. public class KeyInfo : IEnumerable {
  33. private ArrayList Info;
  34. private string id;
  35. public KeyInfo()
  36. {
  37. Info = new ArrayList ();
  38. }
  39. public int Count {
  40. get { return Info.Count; }
  41. }
  42. public string Id {
  43. get { return id; }
  44. set { id = value; }
  45. }
  46. public void AddClause (KeyInfoClause clause)
  47. {
  48. Info.Add (clause);
  49. }
  50. public IEnumerator GetEnumerator ()
  51. {
  52. return Info.GetEnumerator ();
  53. }
  54. public IEnumerator GetEnumerator (Type requestedObjectType)
  55. {
  56. // Build a new ArrayList...
  57. ArrayList TypeList = new ArrayList ();
  58. IEnumerator e = Info.GetEnumerator ();
  59. while (true) {
  60. // ...with all object of specified type...
  61. if ((e.Current).GetType().Equals (requestedObjectType))
  62. TypeList.Add (e.Current);
  63. if (!e.MoveNext ())
  64. break;
  65. }
  66. // ...and return its enumerator
  67. return TypeList.GetEnumerator ();
  68. }
  69. public XmlElement GetXml ()
  70. {
  71. XmlDocument document = new XmlDocument ();
  72. XmlElement xel = document.CreateElement (XmlSignature.ElementNames.KeyInfo, XmlSignature.NamespaceURI);
  73. // we add References afterward so we don't end up with extraneous
  74. // xmlns="..." in each reference elements.
  75. foreach (KeyInfoClause kic in Info) {
  76. XmlNode xn = kic.GetXml ();
  77. XmlNode newNode = document.ImportNode (xn, true);
  78. xel.AppendChild (newNode);
  79. }
  80. return xel;
  81. }
  82. public void LoadXml (XmlElement value)
  83. {
  84. if (value == null)
  85. throw new ArgumentNullException ("value");
  86. Id = value.Attributes ["Id"] != null ? value.GetAttribute ("Id") : null;
  87. if ((value.LocalName == XmlSignature.ElementNames.KeyInfo) && (value.NamespaceURI == XmlSignature.NamespaceURI)) {
  88. foreach (XmlNode n in value.ChildNodes) {
  89. if (n.NodeType != XmlNodeType.Element)
  90. continue;
  91. KeyInfoClause kic = null;
  92. switch (n.LocalName) {
  93. case XmlSignature.ElementNames.KeyValue:
  94. XmlNodeList xnl = n.ChildNodes;
  95. if (xnl.Count > 0) {
  96. // we must now treat the whitespace !
  97. foreach (XmlNode m in xnl) {
  98. switch (m.LocalName) {
  99. case XmlSignature.ElementNames.DSAKeyValue:
  100. kic = (KeyInfoClause) new DSAKeyValue ();
  101. break;
  102. case XmlSignature.ElementNames.RSAKeyValue:
  103. kic = (KeyInfoClause) new RSAKeyValue ();
  104. break;
  105. }
  106. }
  107. }
  108. break;
  109. case XmlSignature.ElementNames.KeyName:
  110. kic = (KeyInfoClause) new KeyInfoName ();
  111. break;
  112. case XmlSignature.ElementNames.RetrievalMethod:
  113. kic = (KeyInfoClause) new KeyInfoRetrievalMethod ();
  114. break;
  115. case XmlSignature.ElementNames.X509Data:
  116. kic = (KeyInfoClause) new KeyInfoX509Data ();
  117. break;
  118. case XmlSignature.ElementNames.RSAKeyValue:
  119. kic = (KeyInfoClause) new RSAKeyValue ();
  120. break;
  121. #if NET_2_0
  122. case XmlSignature.ElementNames.EncryptedKey:
  123. kic = (KeyInfoClause) new KeyInfoEncryptedKey ();
  124. break;
  125. #endif
  126. default:
  127. kic = (KeyInfoClause) new KeyInfoNode ();
  128. break;
  129. }
  130. if (kic != null) {
  131. kic.LoadXml ((XmlElement) n);
  132. AddClause (kic);
  133. }
  134. }
  135. }
  136. // No check is performed on MS.NET...
  137. }
  138. }
  139. }