Signature.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. private ArrayList list;
  15. private SignedInfo info;
  16. private KeyInfo key;
  17. private string id;
  18. private byte[] signature;
  19. public Signature()
  20. {
  21. list = new ArrayList ();
  22. }
  23. public string Id {
  24. get { return id; }
  25. set { id = value; }
  26. }
  27. public KeyInfo KeyInfo {
  28. get { return key; }
  29. set { key = value; }
  30. }
  31. public IList ObjectList {
  32. get { return list; }
  33. set { list = ArrayList.Adapter (value); }
  34. }
  35. public byte[] SignatureValue {
  36. get { return signature; }
  37. set { signature = value; }
  38. }
  39. public SignedInfo SignedInfo {
  40. get { return info; }
  41. set { info = value; }
  42. }
  43. public void AddObject (DataObject dataObject)
  44. {
  45. list.Add (dataObject);
  46. }
  47. public XmlElement GetXml ()
  48. {
  49. if (info == null)
  50. throw new CryptographicException ("SignedInfo");
  51. if (signature == null)
  52. throw new CryptographicException ("SignatureValue");
  53. XmlDocument document = new XmlDocument ();
  54. XmlElement xel = document.CreateElement (XmlSignature.ElementNames.Signature, XmlSignature.NamespaceURI);
  55. if (id != null)
  56. xel.SetAttribute (XmlSignature.AttributeNames.Id, id);
  57. XmlNode xn = info.GetXml ();
  58. XmlNode newNode = document.ImportNode (xn, true);
  59. xel.AppendChild (newNode);
  60. if (signature != null) {
  61. XmlElement sv = document.CreateElement (XmlSignature.ElementNames.SignatureValue, XmlSignature.NamespaceURI);
  62. sv.InnerText = Convert.ToBase64String (signature);
  63. xel.AppendChild (sv);
  64. }
  65. if (key != null) {
  66. xn = key.GetXml ();
  67. newNode = document.ImportNode (xn, true);
  68. xel.AppendChild (newNode);
  69. }
  70. if (list.Count > 0) {
  71. foreach (DataObject obj in list) {
  72. xn = obj.GetXml ();
  73. newNode = document.ImportNode (xn, true);
  74. xel.AppendChild (newNode);
  75. }
  76. }
  77. return xel;
  78. }
  79. private string GetAttribute (XmlElement xel, string attribute)
  80. {
  81. XmlAttribute xa = xel.Attributes [attribute];
  82. return ((xa != null) ? xa.InnerText : null);
  83. }
  84. public void LoadXml (XmlElement value)
  85. {
  86. if (value == null)
  87. throw new ArgumentNullException ("value");
  88. if ((value.LocalName == XmlSignature.ElementNames.Signature) && (value.NamespaceURI == XmlSignature.NamespaceURI)) {
  89. id = GetAttribute (value, XmlSignature.AttributeNames.Id);
  90. XmlNodeList xnl = value.GetElementsByTagName (XmlSignature.ElementNames.SignedInfo);
  91. if ((xnl != null) && (xnl.Count == 1)) {
  92. info = new SignedInfo ();
  93. info.LoadXml ((XmlElement) xnl[0]);
  94. }
  95. xnl = value.GetElementsByTagName (XmlSignature.ElementNames.SignatureValue);
  96. if ((xnl != null) && (xnl.Count == 1)) {
  97. signature = Convert.FromBase64String (xnl[0].InnerText);
  98. }
  99. xnl = value.GetElementsByTagName (XmlSignature.ElementNames.KeyInfo);
  100. if ((xnl != null) && (xnl.Count == 1)) {
  101. key = new KeyInfo ();
  102. key.LoadXml ((XmlElement) xnl[0]);
  103. }
  104. xnl = value.GetElementsByTagName (XmlSignature.ElementNames.Object);
  105. if ((xnl != null) && (xnl.Count > 0)) {
  106. foreach (XmlNode xn in xnl) {
  107. DataObject obj = new DataObject ();
  108. obj.LoadXml ((XmlElement) xn);
  109. AddObject (obj);
  110. }
  111. }
  112. }
  113. // if invalid
  114. if (info == null)
  115. throw new CryptographicException ("SignedInfo");
  116. if (signature == null)
  117. throw new CryptographicException ("SignatureValue");
  118. }
  119. }
  120. }