SignedInfo.cs 6.8 KB

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