Signature.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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.Text;
  12. using System.Xml;
  13. namespace System.Security.Cryptography.Xml {
  14. public class Signature {
  15. static private string xmldsig = "http://www.w3.org/2000/09/xmldsig#";
  16. private ArrayList list;
  17. private SignedInfo info;
  18. private KeyInfo key;
  19. private string id;
  20. private byte[] signature;
  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. sb.Append ("\" />");
  65. XmlDocument doc = new XmlDocument ();
  66. doc.LoadXml (sb.ToString ());
  67. XmlNode xn = null;
  68. XmlNode newNode = null;
  69. if (info != null) {
  70. // this adds the xmlns=xmldsig
  71. xn = info.GetXml ();
  72. newNode = doc.ImportNode (xn, true);
  73. doc.DocumentElement.AppendChild (newNode);
  74. }
  75. if (signature != null) {
  76. XmlElement sv = doc.CreateElement ("SignatureValue", xmldsig);
  77. sv.InnerText = Convert.ToBase64String (signature);
  78. doc.DocumentElement.AppendChild (sv);
  79. }
  80. if (key != null) {
  81. xn = key.GetXml ();
  82. newNode = doc.ImportNode (xn, true);
  83. doc.DocumentElement.AppendChild (newNode);
  84. }
  85. if (list.Count > 0) {
  86. foreach (DataObject obj in list) {
  87. xn = obj.GetXml ();
  88. newNode = doc.ImportNode (xn, true);
  89. doc.DocumentElement.AppendChild (newNode);
  90. }
  91. }
  92. return doc.DocumentElement;
  93. }
  94. private string GetAttribute (XmlElement xel, string attribute)
  95. {
  96. XmlAttribute xa = xel.Attributes [attribute];
  97. return ((xa != null) ? xa.InnerText : null);
  98. }
  99. public void LoadXml (XmlElement value)
  100. {
  101. if (value == null)
  102. throw new ArgumentNullException ("value");
  103. if ((value.LocalName == "Signature") && (value.NamespaceURI == xmldsig)) {
  104. id = GetAttribute (value, "Id");
  105. XmlNodeList xnl = value.GetElementsByTagName ("SignedInfo");
  106. if ((xnl != null) && (xnl.Count == 1)) {
  107. info = new SignedInfo ();
  108. info.LoadXml ((XmlElement) xnl[0]);
  109. }
  110. xnl = value.GetElementsByTagName ("SignatureValue");
  111. if ((xnl != null) && (xnl.Count == 1)) {
  112. signature = Convert.FromBase64String (xnl[0].InnerText);
  113. }
  114. xnl = value.GetElementsByTagName ("KeyInfo");
  115. if ((xnl != null) && (xnl.Count == 1)) {
  116. key = new KeyInfo ();
  117. key.LoadXml ((XmlElement) xnl[0]);
  118. }
  119. xnl = value.GetElementsByTagName ("Object");
  120. if ((xnl != null) && (xnl.Count > 0)) {
  121. foreach (XmlNode xn in xnl) {
  122. DataObject obj = new DataObject ();
  123. obj.LoadXml ((XmlElement) xn);
  124. AddObject (obj);
  125. }
  126. }
  127. }
  128. // if invalid
  129. if (info == null)
  130. throw new CryptographicException ("SignedInfo");
  131. if (signature == null)
  132. throw new CryptographicException ("SignatureValue");
  133. }
  134. }
  135. }