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