SignedInfo.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. [MonoTODO ("when does it return non-null string?")]
  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. [MonoTODO("signatureLength for HMAC")]
  112. public void LoadXml (XmlElement value)
  113. {
  114. if (value == null)
  115. throw new ArgumentNullException ("value");
  116. if ((value.LocalName != XmlSignature.ElementNames.SignedInfo) || (value.NamespaceURI != XmlSignature.NamespaceURI))
  117. throw new CryptographicException ();
  118. id = GetAttribute (value, XmlSignature.AttributeNames.Id);
  119. c14nMethod = XmlSignature.GetAttributeFromElement (value, XmlSignature.AttributeNames.Algorithm, XmlSignature.ElementNames.CanonicalizationMethod);
  120. signatureMethod = XmlSignature.GetAttributeFromElement (value, XmlSignature.AttributeNames.Algorithm, XmlSignature.ElementNames.SignatureMethod);
  121. // TODO signatureLength for HMAC
  122. for (int i = 0; i < value.ChildNodes.Count; i++) {
  123. XmlNode n = value.ChildNodes [i];
  124. if (n.NodeType == XmlNodeType.Element &&
  125. n.LocalName == XmlSignature.ElementNames.Reference &&
  126. n.NamespaceURI == XmlSignature.NamespaceURI) {
  127. Reference r = new Reference ();
  128. r.LoadXml ((XmlElement) n);
  129. AddReference (r);
  130. }
  131. }
  132. }
  133. }
  134. }