Signature.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 (null);
  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. if (document == null)
  101. document = new XmlDocument ();
  102. XmlElement xel = document.CreateElement (XmlSignature.ElementNames.Signature, XmlSignature.NamespaceURI);
  103. if (id != null)
  104. xel.SetAttribute (XmlSignature.AttributeNames.Id, id);
  105. XmlNode xn = info.GetXml ();
  106. XmlNode newNode = document.ImportNode (xn, true);
  107. xel.AppendChild (newNode);
  108. if (signature != null) {
  109. XmlElement sv = document.CreateElement (XmlSignature.ElementNames.SignatureValue, XmlSignature.NamespaceURI);
  110. sv.InnerText = Convert.ToBase64String (signature);
  111. xel.AppendChild (sv);
  112. }
  113. if (key != null) {
  114. xn = key.GetXml ();
  115. newNode = document.ImportNode (xn, true);
  116. xel.AppendChild (newNode);
  117. }
  118. if (list.Count > 0) {
  119. foreach (DataObject obj in list) {
  120. xn = obj.GetXml ();
  121. newNode = document.ImportNode (xn, true);
  122. xel.AppendChild (newNode);
  123. }
  124. }
  125. return xel;
  126. }
  127. private string GetAttribute (XmlElement xel, string attribute)
  128. {
  129. XmlAttribute xa = xel.Attributes [attribute];
  130. return ((xa != null) ? xa.InnerText : null);
  131. }
  132. public void LoadXml (XmlElement value)
  133. {
  134. if (value == null)
  135. throw new ArgumentNullException ("value");
  136. if ((value.LocalName == XmlSignature.ElementNames.Signature) && (value.NamespaceURI == XmlSignature.NamespaceURI)) {
  137. id = GetAttribute (value, XmlSignature.AttributeNames.Id);
  138. // LAMESPEC: This library is totally useless against eXtensibly Marked-up document.
  139. int i = NextElementPos (value.ChildNodes, 0, XmlSignature.ElementNames.SignedInfo, XmlSignature.NamespaceURI, true);
  140. XmlElement sinfo = (XmlElement) value.ChildNodes [i];
  141. info = new SignedInfo ();
  142. info.LoadXml (sinfo);
  143. i = NextElementPos (value.ChildNodes, ++i, XmlSignature.ElementNames.SignatureValue, XmlSignature.NamespaceURI, true);
  144. XmlElement sigValue = (XmlElement) value.ChildNodes [i];
  145. signature = Convert.FromBase64String (sigValue.InnerText);
  146. // signature isn't required: <element ref="ds:KeyInfo" minOccurs="0"/>
  147. i = NextElementPos (value.ChildNodes, ++i, XmlSignature.ElementNames.KeyInfo, XmlSignature.NamespaceURI, false);
  148. if (i > 0) {
  149. XmlElement kinfo = (XmlElement) value.ChildNodes [i];
  150. key = new KeyInfo ();
  151. key.LoadXml (kinfo);
  152. }
  153. XmlNodeList xnl = value.SelectNodes ("xd:Object", dsigNsmgr);
  154. foreach (XmlElement xn in xnl) {
  155. DataObject obj = new DataObject ();
  156. obj.LoadXml (xn);
  157. AddObject (obj);
  158. }
  159. }
  160. else
  161. throw new CryptographicException ("Malformed element: Signature.");
  162. // if invalid
  163. if (info == null)
  164. throw new CryptographicException ("SignedInfo");
  165. if (signature == null)
  166. throw new CryptographicException ("SignatureValue");
  167. }
  168. private int NextElementPos (XmlNodeList nl, int pos, string name, string ns, bool required)
  169. {
  170. while (pos < nl.Count) {
  171. if (nl [pos].NodeType == XmlNodeType.Element) {
  172. if (nl [pos].LocalName != name || nl [pos].NamespaceURI != ns) {
  173. if (required)
  174. throw new CryptographicException ("Malformed element " + name);
  175. else
  176. return -2;
  177. }
  178. else
  179. return pos;
  180. }
  181. else
  182. pos++;
  183. }
  184. if (required)
  185. throw new CryptographicException ("Malformed element " + name);
  186. return -1;
  187. }
  188. }
  189. }