Signature.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. //
  2. // Signature.cs - Signature implementation for XML Signature
  3. //
  4. // Author:
  5. // Sebastien Pouliot ([email protected])
  6. //
  7. // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System.Collections;
  30. using System.Security.Cryptography;
  31. using System.Xml;
  32. namespace System.Security.Cryptography.Xml {
  33. public class Signature {
  34. static XmlNamespaceManager dsigNsmgr;
  35. static Signature ()
  36. {
  37. dsigNsmgr = new XmlNamespaceManager (new NameTable ());
  38. dsigNsmgr.AddNamespace ("xd", XmlSignature.NamespaceURI);
  39. }
  40. private ArrayList list;
  41. private SignedInfo info;
  42. private KeyInfo key;
  43. private string id;
  44. private byte[] signature;
  45. private XmlElement element;
  46. public Signature ()
  47. {
  48. list = new ArrayList ();
  49. }
  50. public string Id {
  51. get { return id; }
  52. set {
  53. element = null;
  54. id = value;
  55. }
  56. }
  57. public KeyInfo KeyInfo {
  58. get { return key; }
  59. set {
  60. element = null;
  61. key = value;
  62. }
  63. }
  64. public IList ObjectList {
  65. get { return list; }
  66. set { list = ArrayList.Adapter (value); }
  67. }
  68. public byte[] SignatureValue {
  69. get { return signature; }
  70. set {
  71. element = null;
  72. signature = value;
  73. }
  74. }
  75. public SignedInfo SignedInfo {
  76. get { return info; }
  77. set {
  78. element = null;
  79. info = value;
  80. }
  81. }
  82. public void AddObject (DataObject dataObject)
  83. {
  84. list.Add (dataObject);
  85. }
  86. public XmlElement GetXml ()
  87. {
  88. if (element != null)
  89. return element;
  90. if (info == null)
  91. throw new CryptographicException ("SignedInfo");
  92. if (signature == null)
  93. throw new CryptographicException ("SignatureValue");
  94. XmlDocument document = new XmlDocument ();
  95. XmlElement xel = document.CreateElement (XmlSignature.ElementNames.Signature, XmlSignature.NamespaceURI);
  96. if (id != null)
  97. xel.SetAttribute (XmlSignature.AttributeNames.Id, id);
  98. XmlNode xn = info.GetXml ();
  99. XmlNode newNode = document.ImportNode (xn, true);
  100. xel.AppendChild (newNode);
  101. if (signature != null) {
  102. XmlElement sv = document.CreateElement (XmlSignature.ElementNames.SignatureValue, XmlSignature.NamespaceURI);
  103. sv.InnerText = Convert.ToBase64String (signature);
  104. xel.AppendChild (sv);
  105. }
  106. if (key != null) {
  107. xn = key.GetXml ();
  108. newNode = document.ImportNode (xn, true);
  109. xel.AppendChild (newNode);
  110. }
  111. if (list.Count > 0) {
  112. foreach (DataObject obj in list) {
  113. xn = obj.GetXml ();
  114. newNode = document.ImportNode (xn, true);
  115. xel.AppendChild (newNode);
  116. }
  117. }
  118. return xel;
  119. }
  120. private string GetAttribute (XmlElement xel, string attribute)
  121. {
  122. XmlAttribute xa = xel.Attributes [attribute];
  123. return ((xa != null) ? xa.InnerText : null);
  124. }
  125. public void LoadXml (XmlElement value)
  126. {
  127. if (value == null)
  128. throw new ArgumentNullException ("value");
  129. if ((value.LocalName == XmlSignature.ElementNames.Signature) && (value.NamespaceURI == XmlSignature.NamespaceURI)) {
  130. id = GetAttribute (value, XmlSignature.AttributeNames.Id);
  131. // LAMESPEC: This library is totally useless against eXtensibly Marked-up document.
  132. int i = NextElementPos (value.ChildNodes, 0, XmlSignature.ElementNames.SignedInfo, XmlSignature.NamespaceURI, true);
  133. XmlElement sinfo = (XmlElement) value.ChildNodes [i];
  134. info = new SignedInfo ();
  135. info.LoadXml (sinfo);
  136. i = NextElementPos (value.ChildNodes, ++i, XmlSignature.ElementNames.SignatureValue, XmlSignature.NamespaceURI, true);
  137. XmlElement sigValue = (XmlElement) value.ChildNodes [i];
  138. signature = Convert.FromBase64String (sigValue.InnerText);
  139. // signature isn't required: <element ref="ds:KeyInfo" minOccurs="0"/>
  140. i = NextElementPos (value.ChildNodes, ++i, XmlSignature.ElementNames.KeyInfo, XmlSignature.NamespaceURI, false);
  141. if (i > 0) {
  142. XmlElement kinfo = (XmlElement) value.ChildNodes [i];
  143. key = new KeyInfo ();
  144. key.LoadXml (kinfo);
  145. }
  146. XmlNodeList xnl = value.SelectNodes ("xd:Object", dsigNsmgr);
  147. foreach (XmlElement xn in xnl) {
  148. DataObject obj = new DataObject ();
  149. obj.LoadXml (xn);
  150. AddObject (obj);
  151. }
  152. }
  153. else
  154. throw new CryptographicException ("Malformed element: Signature.");
  155. // if invalid
  156. if (info == null)
  157. throw new CryptographicException ("SignedInfo");
  158. if (signature == null)
  159. throw new CryptographicException ("SignatureValue");
  160. }
  161. private int NextElementPos (XmlNodeList nl, int pos, string name, string ns, bool required)
  162. {
  163. while (pos < nl.Count) {
  164. if (nl [pos].NodeType == XmlNodeType.Element) {
  165. if (nl [pos].LocalName != name && nl [pos].NamespaceURI != ns) {
  166. if (required)
  167. throw new CryptographicException ("Malformed element " + name);
  168. else
  169. return -2;
  170. }
  171. return pos;
  172. }
  173. else
  174. pos++;
  175. }
  176. if (required)
  177. throw new CryptographicException ("Malformed element " + name);
  178. return -1;
  179. }
  180. }
  181. }