Signature.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. //
  2. // Signature.cs - Signature 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. //
  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.Security.Cryptography;
  33. using System.Xml;
  34. namespace System.Security.Cryptography.Xml {
  35. public class Signature {
  36. static XmlNamespaceManager dsigNsmgr;
  37. static Signature ()
  38. {
  39. dsigNsmgr = new XmlNamespaceManager (new NameTable ());
  40. dsigNsmgr.AddNamespace ("xd", XmlSignature.NamespaceURI);
  41. }
  42. private ArrayList list;
  43. private SignedInfo info;
  44. private KeyInfo key;
  45. private string id;
  46. private byte[] signature;
  47. private XmlElement element;
  48. public Signature ()
  49. {
  50. list = new ArrayList ();
  51. }
  52. public string Id {
  53. get { return id; }
  54. set {
  55. element = null;
  56. id = value;
  57. }
  58. }
  59. public KeyInfo KeyInfo {
  60. get { return key; }
  61. set {
  62. element = null;
  63. key = value;
  64. }
  65. }
  66. public IList ObjectList {
  67. get { return list; }
  68. set { list = ArrayList.Adapter (value); }
  69. }
  70. public byte[] SignatureValue {
  71. get { return signature; }
  72. set {
  73. element = null;
  74. signature = value;
  75. }
  76. }
  77. public SignedInfo SignedInfo {
  78. get { return info; }
  79. set {
  80. element = null;
  81. info = value;
  82. }
  83. }
  84. public void AddObject (DataObject dataObject)
  85. {
  86. list.Add (dataObject);
  87. }
  88. public XmlElement GetXml ()
  89. {
  90. return GetXml (new XmlDocument ());
  91. }
  92. internal XmlElement GetXml (XmlDocument document)
  93. {
  94. if (element != null)
  95. return element;
  96. if (info == null)
  97. throw new CryptographicException ("SignedInfo");
  98. if (signature == null)
  99. throw new CryptographicException ("SignatureValue");
  100. XmlElement xel = document.CreateElement (XmlSignature.ElementNames.Signature, XmlSignature.NamespaceURI);
  101. if (id != null)
  102. xel.SetAttribute (XmlSignature.AttributeNames.Id, id);
  103. XmlNode xn = info.GetXml ();
  104. XmlNode newNode = document.ImportNode (xn, true);
  105. xel.AppendChild (newNode);
  106. if (signature != null) {
  107. XmlElement sv = document.CreateElement (XmlSignature.ElementNames.SignatureValue, XmlSignature.NamespaceURI);
  108. sv.InnerText = Convert.ToBase64String (signature);
  109. xel.AppendChild (sv);
  110. }
  111. if (key != null) {
  112. xn = key.GetXml ();
  113. newNode = document.ImportNode (xn, true);
  114. xel.AppendChild (newNode);
  115. }
  116. if (list.Count > 0) {
  117. foreach (DataObject obj in list) {
  118. xn = obj.GetXml ();
  119. newNode = document.ImportNode (xn, true);
  120. xel.AppendChild (newNode);
  121. }
  122. }
  123. return xel;
  124. }
  125. private string GetAttribute (XmlElement xel, string attribute)
  126. {
  127. XmlAttribute xa = xel.Attributes [attribute];
  128. return ((xa != null) ? xa.InnerText : null);
  129. }
  130. public void LoadXml (XmlElement value)
  131. {
  132. if (value == null)
  133. throw new ArgumentNullException ("value");
  134. if ((value.LocalName == XmlSignature.ElementNames.Signature) && (value.NamespaceURI == XmlSignature.NamespaceURI)) {
  135. id = GetAttribute (value, XmlSignature.AttributeNames.Id);
  136. // LAMESPEC: This library is totally useless against eXtensibly Marked-up document.
  137. int i = NextElementPos (value.ChildNodes, 0, XmlSignature.ElementNames.SignedInfo, XmlSignature.NamespaceURI, true);
  138. XmlElement sinfo = (XmlElement) value.ChildNodes [i];
  139. info = new SignedInfo ();
  140. info.LoadXml (sinfo);
  141. i = NextElementPos (value.ChildNodes, ++i, XmlSignature.ElementNames.SignatureValue, XmlSignature.NamespaceURI, true);
  142. XmlElement sigValue = (XmlElement) value.ChildNodes [i];
  143. signature = Convert.FromBase64String (sigValue.InnerText);
  144. // signature isn't required: <element ref="ds:KeyInfo" minOccurs="0"/>
  145. i = NextElementPos (value.ChildNodes, ++i, XmlSignature.ElementNames.KeyInfo, XmlSignature.NamespaceURI, false);
  146. if (i > 0) {
  147. XmlElement kinfo = (XmlElement) value.ChildNodes [i];
  148. key = new KeyInfo ();
  149. key.LoadXml (kinfo);
  150. }
  151. XmlNodeList xnl = value.SelectNodes ("xd:Object", dsigNsmgr);
  152. foreach (XmlElement xn in xnl) {
  153. DataObject obj = new DataObject ();
  154. obj.LoadXml (xn);
  155. AddObject (obj);
  156. }
  157. }
  158. else
  159. throw new CryptographicException ("Malformed element: Signature.");
  160. // if invalid
  161. if (info == null)
  162. throw new CryptographicException ("SignedInfo");
  163. if (signature == null)
  164. throw new CryptographicException ("SignatureValue");
  165. }
  166. private int NextElementPos (XmlNodeList nl, int pos, string name, string ns, bool required)
  167. {
  168. while (pos < nl.Count) {
  169. if (nl [pos].NodeType == XmlNodeType.Element) {
  170. if (nl [pos].LocalName != name || nl [pos].NamespaceURI != ns) {
  171. if (required)
  172. throw new CryptographicException ("Malformed element " + name);
  173. else
  174. return -2;
  175. }
  176. else
  177. return pos;
  178. }
  179. else
  180. pos++;
  181. }
  182. if (required)
  183. throw new CryptographicException ("Malformed element " + name);
  184. return -1;
  185. }
  186. }
  187. }