SignedInfo.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. public SignedInfo()
  43. {
  44. references = new ArrayList ();
  45. c14nMethod = "http://www.w3.org/TR/2001/REC-xml-c14n-20010315";
  46. }
  47. public string CanonicalizationMethod {
  48. get { return c14nMethod; }
  49. set {
  50. c14nMethod = value;
  51. element = null;
  52. }
  53. }
  54. [ComVisible (false)]
  55. [MonoTODO]
  56. public Transform CanonicalizationMethodObject {
  57. get { throw new NotImplementedException (); }
  58. }
  59. // documented as not supported (and throwing exception)
  60. public int Count {
  61. get { throw new NotSupportedException (); }
  62. }
  63. public string Id {
  64. get { return id; }
  65. set {
  66. element = null;
  67. id = value;
  68. }
  69. }
  70. // documented as not supported (and throwing exception)
  71. public bool IsReadOnly {
  72. get { throw new NotSupportedException (); }
  73. }
  74. // documented as not supported (and throwing exception)
  75. public bool IsSynchronized {
  76. get { throw new NotSupportedException (); }
  77. }
  78. // Manipulating this array never affects GetXml() when
  79. // LoadXml() was used.
  80. // (Actually, there is no way to detect modification.)
  81. public ArrayList References {
  82. get { return references; }
  83. }
  84. public string SignatureLength {
  85. get { return signatureLength; }
  86. set {
  87. element = null;
  88. signatureLength = value;
  89. }
  90. }
  91. public string SignatureMethod {
  92. get { return signatureMethod; }
  93. set {
  94. element = null;
  95. signatureMethod = value;
  96. }
  97. }
  98. // documented as not supported (and throwing exception)
  99. public object SyncRoot {
  100. get { throw new NotSupportedException (); }
  101. }
  102. public void AddReference (Reference reference)
  103. {
  104. references.Add (reference);
  105. }
  106. // documented as not supported (and throwing exception)
  107. public void CopyTo (Array array, int index)
  108. {
  109. throw new NotSupportedException ();
  110. }
  111. public IEnumerator GetEnumerator ()
  112. {
  113. return references.GetEnumerator ();
  114. }
  115. public XmlElement GetXml ()
  116. {
  117. if (element != null)
  118. return element;
  119. if (signatureMethod == null)
  120. throw new CryptographicException ("SignatureMethod");
  121. if (references.Count == 0)
  122. throw new CryptographicException ("References empty");
  123. XmlDocument document = new XmlDocument ();
  124. XmlElement xel = document.CreateElement (XmlSignature.ElementNames.SignedInfo, XmlSignature.NamespaceURI);
  125. if (id != null)
  126. xel.SetAttribute (XmlSignature.AttributeNames.Id, id);
  127. if (c14nMethod != null) {
  128. XmlElement c14n = document.CreateElement (XmlSignature.ElementNames.CanonicalizationMethod, XmlSignature.NamespaceURI);
  129. c14n.SetAttribute (XmlSignature.AttributeNames.Algorithm, c14nMethod);
  130. xel.AppendChild (c14n);
  131. }
  132. if (signatureMethod != null) {
  133. XmlElement sm = document.CreateElement (XmlSignature.ElementNames.SignatureMethod, XmlSignature.NamespaceURI);
  134. sm.SetAttribute (XmlSignature.AttributeNames.Algorithm, signatureMethod);
  135. if (signatureLength != null) {
  136. XmlElement hmac = document.CreateElement (XmlSignature.ElementNames.HMACOutputLength, XmlSignature.NamespaceURI);
  137. hmac.InnerText = signatureLength;
  138. sm.AppendChild (hmac);
  139. }
  140. xel.AppendChild (sm);
  141. }
  142. // This check is only done when element is created here.
  143. if (references.Count == 0)
  144. throw new CryptographicException ("At least one Reference element is required in SignedInfo.");
  145. // we add References afterward so we don't end up with extraneous
  146. // xmlns="..." in each reference elements.
  147. foreach (Reference r in references) {
  148. XmlNode xn = r.GetXml ();
  149. XmlNode newNode = document.ImportNode (xn, true);
  150. xel.AppendChild (newNode);
  151. }
  152. return xel;
  153. }
  154. private string GetAttribute (XmlElement xel, string attribute)
  155. {
  156. XmlAttribute xa = xel.Attributes [attribute];
  157. return ((xa != null) ? xa.InnerText : null);
  158. }
  159. public void LoadXml (XmlElement value)
  160. {
  161. if (value == null)
  162. throw new ArgumentNullException ("value");
  163. if ((value.LocalName != XmlSignature.ElementNames.SignedInfo) || (value.NamespaceURI != XmlSignature.NamespaceURI))
  164. throw new CryptographicException ();
  165. id = GetAttribute (value, XmlSignature.AttributeNames.Id);
  166. c14nMethod = XmlSignature.GetAttributeFromElement (value, XmlSignature.AttributeNames.Algorithm, XmlSignature.ElementNames.CanonicalizationMethod);
  167. XmlElement sm = XmlSignature.GetChildElement (value, XmlSignature.ElementNames.SignatureMethod, XmlSignature.NamespaceURI);
  168. if (sm != null) {
  169. signatureMethod = sm.GetAttribute (XmlSignature.AttributeNames.Algorithm);
  170. XmlElement length = XmlSignature.GetChildElement (sm, XmlSignature.ElementNames.HMACOutputLength, XmlSignature.NamespaceURI);
  171. if (length != null) {
  172. signatureLength = length.InnerText;
  173. }
  174. }
  175. for (int i = 0; i < value.ChildNodes.Count; i++) {
  176. XmlNode n = value.ChildNodes [i];
  177. if (n.NodeType == XmlNodeType.Element &&
  178. n.LocalName == XmlSignature.ElementNames.Reference &&
  179. n.NamespaceURI == XmlSignature.NamespaceURI) {
  180. Reference r = new Reference ();
  181. r.LoadXml ((XmlElement) n);
  182. AddReference (r);
  183. }
  184. }
  185. element = value;
  186. }
  187. }
  188. }