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