SignedInfo.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. //
  2. // SignedInfo.cs - SignedInfo implementation for XML Signature
  3. //
  4. // Author:
  5. // Sebastien Pouliot <[email protected]>
  6. //
  7. // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
  8. // (C) 2004 Novell (http://www.novell.com)
  9. //
  10. using System.Collections;
  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. public SignedInfo()
  20. {
  21. references = new ArrayList ();
  22. c14nMethod = "http://www.w3.org/TR/2001/REC-xml-c14n-20010315";
  23. }
  24. public string CanonicalizationMethod {
  25. get { return c14nMethod; }
  26. set { c14nMethod = value; }
  27. }
  28. // documented as not supported (and throwing exception)
  29. public int Count {
  30. get { throw new NotSupportedException (); }
  31. }
  32. public string Id {
  33. get { return id; }
  34. set { id = value; }
  35. }
  36. // documented as not supported (and throwing exception)
  37. public bool IsReadOnly {
  38. get { throw new NotSupportedException (); }
  39. }
  40. // documented as not supported (and throwing exception)
  41. public bool IsSynchronized {
  42. get { throw new NotSupportedException (); }
  43. }
  44. public ArrayList References {
  45. get { return references; }
  46. }
  47. public string SignatureLength {
  48. get { return signatureLength; }
  49. set { signatureLength = value; }
  50. }
  51. public string SignatureMethod {
  52. get { return signatureMethod; }
  53. set { signatureMethod = value; }
  54. }
  55. // documented as not supported (and throwing exception)
  56. public object SyncRoot {
  57. get { throw new NotSupportedException (); }
  58. }
  59. public void AddReference (Reference reference)
  60. {
  61. references.Add (reference);
  62. }
  63. // documented as not supported (and throwing exception)
  64. public void CopyTo (Array array, int index)
  65. {
  66. throw new NotSupportedException ();
  67. }
  68. public IEnumerator GetEnumerator ()
  69. {
  70. return references.GetEnumerator ();
  71. }
  72. public XmlElement GetXml()
  73. {
  74. if (signatureMethod == null)
  75. throw new CryptographicException ("SignatureMethod");
  76. if (references.Count == 0)
  77. throw new CryptographicException ("References empty");
  78. XmlDocument document = new XmlDocument ();
  79. XmlElement xel = document.CreateElement (XmlSignature.ElementNames.SignedInfo, XmlSignature.NamespaceURI);
  80. if (id != null)
  81. xel.SetAttribute (XmlSignature.AttributeNames.Id, id);
  82. if (c14nMethod != null) {
  83. XmlElement c14n = document.CreateElement (XmlSignature.ElementNames.CanonicalizationMethod, XmlSignature.NamespaceURI);
  84. c14n.SetAttribute (XmlSignature.AttributeNames.Algorithm, c14nMethod);
  85. xel.AppendChild (c14n);
  86. }
  87. if (signatureMethod != null) {
  88. XmlElement sm = document.CreateElement (XmlSignature.ElementNames.SignatureMethod, XmlSignature.NamespaceURI);
  89. sm.SetAttribute (XmlSignature.AttributeNames.Algorithm, signatureMethod);
  90. if (signatureLength != null) {
  91. XmlElement hmac = document.CreateElement (XmlSignature.ElementNames.HMACOutputLength, XmlSignature.NamespaceURI);
  92. hmac.InnerText = signatureLength;
  93. sm.AppendChild (hmac);
  94. }
  95. xel.AppendChild (sm);
  96. }
  97. // we add References afterward so we don't end up with extraneous
  98. // xmlns="..." in each reference elements.
  99. foreach (Reference r in references) {
  100. XmlNode xn = r.GetXml ();
  101. XmlNode newNode = document.ImportNode (xn, true);
  102. xel.AppendChild (newNode);
  103. }
  104. return xel;
  105. }
  106. private string GetAttribute (XmlElement xel, string attribute)
  107. {
  108. XmlAttribute xa = xel.Attributes [attribute];
  109. return ((xa != null) ? xa.InnerText : null);
  110. }
  111. public void LoadXml (XmlElement value)
  112. {
  113. if (value == null)
  114. throw new ArgumentNullException ("value");
  115. if ((value.LocalName != XmlSignature.ElementNames.SignedInfo) || (value.NamespaceURI != XmlSignature.NamespaceURI))
  116. throw new CryptographicException ();
  117. id = GetAttribute (value, XmlSignature.AttributeNames.Id);
  118. c14nMethod = XmlSignature.GetAttributeFromElement (value, XmlSignature.AttributeNames.Algorithm, XmlSignature.ElementNames.CanonicalizationMethod);
  119. XmlElement sm = XmlSignature.GetChildElement (value, XmlSignature.ElementNames.SignatureMethod, XmlSignature.NamespaceURI);
  120. if (sm != null) {
  121. signatureMethod = sm.GetAttribute (XmlSignature.AttributeNames.Algorithm);
  122. XmlElement length = XmlSignature.GetChildElement (sm, XmlSignature.ElementNames.HMACOutputLength, XmlSignature.NamespaceURI);
  123. if (length != null) {
  124. signatureLength = length.InnerText;
  125. }
  126. }
  127. for (int i = 0; i < value.ChildNodes.Count; i++) {
  128. XmlNode n = value.ChildNodes [i];
  129. if (n.NodeType == XmlNodeType.Element &&
  130. n.LocalName == XmlSignature.ElementNames.Reference &&
  131. n.NamespaceURI == XmlSignature.NamespaceURI) {
  132. Reference r = new Reference ();
  133. r.LoadXml ((XmlElement) n);
  134. AddReference (r);
  135. }
  136. }
  137. }
  138. }
  139. }