Signature.cs 4.8 KB

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