SignedInfo.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System.Collections;
  31. using System.Xml;
  32. namespace System.Security.Cryptography.Xml {
  33. public class SignedInfo : ICollection, IEnumerable {
  34. private ArrayList references;
  35. private string c14nMethod;
  36. private string id;
  37. private string signatureMethod;
  38. private string signatureLength;
  39. private XmlElement element;
  40. public SignedInfo()
  41. {
  42. references = new ArrayList ();
  43. c14nMethod = "http://www.w3.org/TR/2001/REC-xml-c14n-20010315";
  44. }
  45. public string CanonicalizationMethod {
  46. get { return c14nMethod; }
  47. set {
  48. c14nMethod = value;
  49. element = null;
  50. }
  51. }
  52. // documented as not supported (and throwing exception)
  53. public int Count {
  54. get { throw new NotSupportedException (); }
  55. }
  56. public string Id {
  57. get { return id; }
  58. set {
  59. element = null;
  60. id = value;
  61. }
  62. }
  63. // documented as not supported (and throwing exception)
  64. public bool IsReadOnly {
  65. get { throw new NotSupportedException (); }
  66. }
  67. // documented as not supported (and throwing exception)
  68. public bool IsSynchronized {
  69. get { throw new NotSupportedException (); }
  70. }
  71. // Manipulating this array never affects GetXml() when
  72. // LoadXml() was used.
  73. // (Actually, there is no way to detect modification.)
  74. public ArrayList References {
  75. get { return references; }
  76. }
  77. public string SignatureLength {
  78. get { return signatureLength; }
  79. set {
  80. element = null;
  81. signatureLength = value;
  82. }
  83. }
  84. public string SignatureMethod {
  85. get { return signatureMethod; }
  86. set {
  87. element = null;
  88. signatureMethod = value;
  89. }
  90. }
  91. // documented as not supported (and throwing exception)
  92. public object SyncRoot {
  93. get { throw new NotSupportedException (); }
  94. }
  95. public void AddReference (Reference reference)
  96. {
  97. references.Add (reference);
  98. }
  99. // documented as not supported (and throwing exception)
  100. public void CopyTo (Array array, int index)
  101. {
  102. throw new NotSupportedException ();
  103. }
  104. public IEnumerator GetEnumerator ()
  105. {
  106. return references.GetEnumerator ();
  107. }
  108. public XmlElement GetXml ()
  109. {
  110. if (element != null)
  111. return element;
  112. if (signatureMethod == null)
  113. throw new CryptographicException ("SignatureMethod");
  114. if (references.Count == 0)
  115. throw new CryptographicException ("References empty");
  116. XmlDocument document = new XmlDocument ();
  117. XmlElement xel = document.CreateElement (XmlSignature.ElementNames.SignedInfo, XmlSignature.NamespaceURI);
  118. if (id != null)
  119. xel.SetAttribute (XmlSignature.AttributeNames.Id, id);
  120. if (c14nMethod != null) {
  121. XmlElement c14n = document.CreateElement (XmlSignature.ElementNames.CanonicalizationMethod, XmlSignature.NamespaceURI);
  122. c14n.SetAttribute (XmlSignature.AttributeNames.Algorithm, c14nMethod);
  123. xel.AppendChild (c14n);
  124. }
  125. if (signatureMethod != null) {
  126. XmlElement sm = document.CreateElement (XmlSignature.ElementNames.SignatureMethod, XmlSignature.NamespaceURI);
  127. sm.SetAttribute (XmlSignature.AttributeNames.Algorithm, signatureMethod);
  128. if (signatureLength != null) {
  129. XmlElement hmac = document.CreateElement (XmlSignature.ElementNames.HMACOutputLength, XmlSignature.NamespaceURI);
  130. hmac.InnerText = signatureLength;
  131. sm.AppendChild (hmac);
  132. }
  133. xel.AppendChild (sm);
  134. }
  135. // This check is only done when element is created here.
  136. if (references.Count == 0)
  137. throw new CryptographicException ("At least one Reference element is required in SignedInfo.");
  138. // we add References afterward so we don't end up with extraneous
  139. // xmlns="..." in each reference elements.
  140. foreach (Reference r in references) {
  141. XmlNode xn = r.GetXml ();
  142. XmlNode newNode = document.ImportNode (xn, true);
  143. xel.AppendChild (newNode);
  144. }
  145. return xel;
  146. }
  147. private string GetAttribute (XmlElement xel, string attribute)
  148. {
  149. XmlAttribute xa = xel.Attributes [attribute];
  150. return ((xa != null) ? xa.InnerText : null);
  151. }
  152. public void LoadXml (XmlElement value)
  153. {
  154. if (value == null)
  155. throw new ArgumentNullException ("value");
  156. if ((value.LocalName != XmlSignature.ElementNames.SignedInfo) || (value.NamespaceURI != XmlSignature.NamespaceURI))
  157. throw new CryptographicException ();
  158. id = GetAttribute (value, XmlSignature.AttributeNames.Id);
  159. c14nMethod = XmlSignature.GetAttributeFromElement (value, XmlSignature.AttributeNames.Algorithm, XmlSignature.ElementNames.CanonicalizationMethod);
  160. XmlElement sm = XmlSignature.GetChildElement (value, XmlSignature.ElementNames.SignatureMethod, XmlSignature.NamespaceURI);
  161. if (sm != null) {
  162. signatureMethod = sm.GetAttribute (XmlSignature.AttributeNames.Algorithm);
  163. XmlElement length = XmlSignature.GetChildElement (sm, XmlSignature.ElementNames.HMACOutputLength, XmlSignature.NamespaceURI);
  164. if (length != null) {
  165. signatureLength = length.InnerText;
  166. }
  167. }
  168. for (int i = 0; i < value.ChildNodes.Count; i++) {
  169. XmlNode n = value.ChildNodes [i];
  170. if (n.NodeType == XmlNodeType.Element &&
  171. n.LocalName == XmlSignature.ElementNames.Reference &&
  172. n.NamespaceURI == XmlSignature.NamespaceURI) {
  173. Reference r = new Reference ();
  174. r.LoadXml ((XmlElement) n);
  175. AddReference (r);
  176. }
  177. }
  178. element = value;
  179. }
  180. }
  181. }