SignedInfo.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. //
  2. // SignedInfo.cs - SignedInfo implementation for XML Signature
  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 SignedInfo : ICollection, IEnumerable {
  14. private ArrayList references;
  15. private string c14nMethod;
  16. private string id;
  17. private string signatureMethod;
  18. private string signatureLength;
  19. static private string xmldsig = "http://www.w3.org/2000/09/xmldsig#";
  20. public SignedInfo()
  21. {
  22. references = new ArrayList ();
  23. c14nMethod = "http://www.w3.org/TR/2001/REC-xml-c14n-20010315";
  24. }
  25. public string CanonicalizationMethod {
  26. get { return c14nMethod; }
  27. set { c14nMethod = value; }
  28. }
  29. // documented as not supported (and throwing exception)
  30. public int Count {
  31. get { throw new NotSupportedException (); }
  32. }
  33. public string Id {
  34. get { return id; }
  35. set { id = value; }
  36. }
  37. // documented as not supported (and throwing exception)
  38. public bool IsReadOnly {
  39. get { throw new NotSupportedException (); }
  40. }
  41. // documented as not supported (and throwing exception)
  42. public bool IsSynchronized {
  43. get { throw new NotSupportedException (); }
  44. }
  45. public ArrayList References {
  46. get { return references; }
  47. }
  48. public string SignatureLength {
  49. get { return signatureLength; }
  50. set { signatureLength = value; }
  51. }
  52. public string SignatureMethod {
  53. get { return signatureMethod; }
  54. set { signatureMethod = value; }
  55. }
  56. // documented as not supported (and throwing exception)
  57. public object SyncRoot {
  58. get { throw new NotSupportedException (); }
  59. }
  60. public void AddReference (Reference reference)
  61. {
  62. references.Add (reference);
  63. }
  64. // documented as not supported (and throwing exception)
  65. public void CopyTo (Array array, int index)
  66. {
  67. throw new NotSupportedException ();
  68. }
  69. public IEnumerator GetEnumerator ()
  70. {
  71. return references.GetEnumerator ();
  72. }
  73. public XmlElement GetXml()
  74. {
  75. if (signatureMethod == null)
  76. throw new CryptographicException ("SignatureMethod");
  77. if (references.Count == 0)
  78. throw new CryptographicException ("References empty");
  79. StringBuilder sb = new StringBuilder ();
  80. sb.Append ("<SignedInfo");
  81. if (id != null) {
  82. sb.Append (" Id=\"");
  83. sb.Append (id);
  84. sb.Append ("\"");
  85. }
  86. sb.Append (" xmlns=\"");
  87. sb.Append (xmldsig);
  88. sb.Append ("\">");
  89. if (c14nMethod != null) {
  90. sb.Append ("<CanonicalizationMethod Algorithm=\"");
  91. sb.Append (c14nMethod);
  92. sb.Append ("\" />");
  93. }
  94. if (signatureMethod != null) {
  95. sb.Append ("<SignatureMethod Algorithm=\"");
  96. sb.Append (signatureMethod);
  97. if (signatureLength != null) {
  98. sb.Append ("\">");
  99. sb.Append ("<HMACOutputLength>");
  100. sb.Append (signatureLength);
  101. sb.Append ("</HMACOutputLength>");
  102. sb.Append ("</SignatureMethod>");
  103. }
  104. else
  105. sb.Append ("\" />");
  106. }
  107. sb.Append ("</SignedInfo>");
  108. XmlDocument doc = new XmlDocument ();
  109. doc.LoadXml (sb.ToString ());
  110. // we add References afterward so we don't end up with extraneous
  111. // xmlns="..." in each reference elements.
  112. foreach (Reference r in references) {
  113. XmlNode xn = r.GetXml ();
  114. XmlNode newNode = doc.ImportNode (xn, true);
  115. doc.DocumentElement.AppendChild (newNode);
  116. }
  117. return doc.DocumentElement;
  118. }
  119. private string GetAttributeFromElement (XmlElement xel, string attribute, string element)
  120. {
  121. string result = null;
  122. XmlNodeList xnl = xel.GetElementsByTagName (element);
  123. if ((xnl != null) && (xnl.Count > 0)) {
  124. XmlAttribute xa = xnl[0].Attributes [attribute];
  125. if (xa != null)
  126. result = xa.InnerText;
  127. }
  128. return result;
  129. }
  130. private string GetAttribute (XmlElement xel, string attribute)
  131. {
  132. XmlAttribute xa = xel.Attributes [attribute];
  133. return ((xa != null) ? xa.InnerText : null);
  134. }
  135. public void LoadXml (XmlElement value)
  136. {
  137. if (value == null)
  138. throw new ArgumentNullException ("value");
  139. if ((value.LocalName == "SignedInfo") && (value.NamespaceURI == xmldsig)) {
  140. id = GetAttribute (value, "Id");
  141. c14nMethod = GetAttributeFromElement (value, "Algorithm", "CanonicalizationMethod");
  142. signatureMethod = GetAttributeFromElement (value, "Algorithm", "SignatureMethod");
  143. // signatureLength for HMAC
  144. XmlNodeList xnl = value.GetElementsByTagName ("Reference");
  145. foreach (XmlNode xn in xnl) {
  146. Reference r = new Reference ();
  147. r.LoadXml ((XmlElement) xn);
  148. AddReference (r);
  149. }
  150. }
  151. else
  152. throw new CryptographicException ();
  153. }
  154. }
  155. }