Signature.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. //
  2. // Signature.cs - Signature implementation for XML Signature
  3. //
  4. // Author:
  5. // Sebastien Pouliot ([email protected])
  6. //
  7. // (C) 2002 Motus Technologies Inc. (http://www.motus.com)
  8. //
  9. using System.Collections;
  10. using System.Security.Cryptography;
  11. using System.Text;
  12. using System.Xml;
  13. namespace System.Security.Cryptography.Xml {
  14. public class Signature {
  15. private ArrayList list;
  16. private SignedInfo info;
  17. private KeyInfo key;
  18. private string id;
  19. private byte[] signature;
  20. static private string xmldsig = "http://www.w3.org/2000/09/xmldsig#";
  21. public Signature()
  22. {
  23. list = new ArrayList ();
  24. }
  25. public string Id {
  26. get { return id; }
  27. set { id = value; }
  28. }
  29. public KeyInfo KeyInfo {
  30. get { return key; }
  31. set { key = value; }
  32. }
  33. public IList ObjectList {
  34. get { return list; }
  35. set { list = ArrayList.Adapter (value); }
  36. }
  37. public byte[] SignatureValue {
  38. get { return signature; }
  39. set { signature = value; }
  40. }
  41. public SignedInfo SignedInfo {
  42. get { return info; }
  43. set { info = value; }
  44. }
  45. public void AddObject (DataObject dataObject)
  46. {
  47. list.Add (dataObject);
  48. }
  49. public XmlElement GetXml ()
  50. {
  51. if (info == null)
  52. throw new CryptographicException ("SignedInfo");
  53. if (signature == null)
  54. throw new CryptographicException ("SignatureValue");
  55. StringBuilder sb = new StringBuilder ();
  56. sb.Append ("<Signature");
  57. if (id != null) {
  58. sb.Append (" Id = \"");
  59. sb.Append (id);
  60. sb.Append ("\"");
  61. }
  62. sb.Append (" xmlns=\"");
  63. sb.Append (xmldsig);
  64. /* if (info != null) {
  65. sb.Append ("\">");
  66. sb.Append (info.GetXml ().OuterXml);
  67. sb.Append ("</Signature>");
  68. }
  69. else*/
  70. sb.Append ("\" />");
  71. XmlDocument doc = new XmlDocument ();
  72. doc.LoadXml (sb.ToString ());
  73. XmlNode xn = null;
  74. XmlNode newNode = null;
  75. if (info != null) {
  76. // this adds the xmlns=xmldsig
  77. xn = info.GetXml ();
  78. newNode = doc.ImportNode (xn, true);
  79. doc.DocumentElement.AppendChild (newNode);
  80. }
  81. if (signature != null) {
  82. XmlElement sv = doc.CreateElement ("SignatureValue", xmldsig);
  83. sv.InnerText = Convert.ToBase64String (signature);
  84. doc.DocumentElement.AppendChild (sv);
  85. }
  86. if (key != null) {
  87. xn = key.GetXml ();
  88. newNode = doc.ImportNode (xn, true);
  89. doc.DocumentElement.AppendChild (newNode);
  90. }
  91. if (list.Count > 0) {
  92. foreach (DataObject obj in list) {
  93. xn = obj.GetXml ();
  94. newNode = doc.ImportNode (xn, true);
  95. doc.DocumentElement.AppendChild (newNode);
  96. }
  97. }
  98. return doc.DocumentElement;
  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 == "Signature") && (value.NamespaceURI == xmldsig)) {
  110. id = GetAttribute (value, "Id");
  111. XmlNodeList xnl = value.GetElementsByTagName ("SignedInfo");
  112. if ((xnl != null) && (xnl.Count == 1)) {
  113. info = new SignedInfo ();
  114. info.LoadXml ((XmlElement) xnl[0]);
  115. }
  116. xnl = value.GetElementsByTagName ("SignatureValue");
  117. if ((xnl != null) && (xnl.Count == 1)) {
  118. signature = Convert.FromBase64String (xnl[0].InnerText);
  119. }
  120. xnl = value.GetElementsByTagName ("KeyInfo");
  121. if ((xnl != null) && (xnl.Count == 1)) {
  122. key = new KeyInfo ();
  123. key.LoadXml ((XmlElement) xnl[0]);
  124. }
  125. xnl = value.GetElementsByTagName ("Object");
  126. if ((xnl != null) && (xnl.Count > 0)) {
  127. foreach (XmlNode xn in xnl) {
  128. DataObject obj = new DataObject ();
  129. obj.LoadXml ((XmlElement) xn);
  130. AddObject (obj);
  131. }
  132. }
  133. }
  134. // if invalid
  135. if (info == null)
  136. throw new CryptographicException ("SignedInfo");
  137. if (signature == null)
  138. throw new CryptographicException ("SignatureValue");
  139. }
  140. }
  141. }