XmlSignature.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. //
  2. // XmlSignature.cs: Handles Xml Signature
  3. //
  4. // Author:
  5. // Sebastien Pouliot ([email protected])
  6. // Atsushi Enomoto ([email protected])
  7. //
  8. // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
  9. // (C) 2004 Novell Inc.
  10. //
  11. using System;
  12. using System.Collections;
  13. using System.Xml;
  14. namespace System.Security.Cryptography.Xml {
  15. // following the design of WSE
  16. internal class XmlSignature {
  17. public class ElementNames {
  18. public const string CanonicalizationMethod = "CanonicalizationMethod";
  19. public const string DigestMethod = "DigestMethod";
  20. public const string DigestValue = "DigestValue";
  21. public const string DSAKeyValue = "DSAKeyValue";
  22. public const string HMACOutputLength = "HMACOutputLength";
  23. public const string KeyInfo = "KeyInfo";
  24. public const string KeyName = "KeyName";
  25. public const string KeyValue = "KeyValue";
  26. public const string Object = "Object";
  27. public const string Reference = "Reference";
  28. #if NET_1_0
  29. // RetrievalMethod vs RetrievalElement -> BUG in MS Framework 1.0
  30. public const string RetrievalMethod = "RetrievalElement";
  31. #else
  32. public const string RetrievalMethod = "RetrievalMethod";
  33. #endif
  34. public const string RSAKeyValue = "RSAKeyValue";
  35. public const string Signature = "Signature";
  36. public const string SignatureMethod = "SignatureMethod";
  37. public const string SignatureValue = "SignatureValue";
  38. public const string SignedInfo = "SignedInfo";
  39. public const string Transform = "Transform";
  40. public const string Transforms = "Transforms";
  41. public const string X509Data = "X509Data";
  42. public const string X509IssuerSerial = "X509IssuerSerial";
  43. public const string X509IssuerName = "X509IssuerName";
  44. public const string X509SerialNumber = "X509SerialNumber";
  45. public const string X509SKI = "X509SKI";
  46. public const string X509SubjectName = "X509SubjectName";
  47. public const string X509Certificate = "X509Certificate";
  48. public const string X509CRL = "X509CRL";
  49. public ElementNames () {}
  50. }
  51. public class AttributeNames {
  52. public const string Algorithm = "Algorithm";
  53. public const string Encoding = "Encoding";
  54. public const string Id = "Id";
  55. public const string MimeType = "MimeType";
  56. public const string Type = "Type";
  57. public const string URI = "URI";
  58. public AttributeNames () {}
  59. }
  60. public class AlgorithmNamespaces {
  61. public const string XmlDsigBase64Transform = "http://www.w3.org/2000/09/xmldsig#base64";
  62. public const string XmlDsigC14NTransform = "http://www.w3.org/TR/2001/REC-xml-c14n-20010315";
  63. public const string XmlDsigC14NWithCommentsTransform = "http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments";
  64. public const string XmlDsigEnvelopedSignatureTransform = "http://www.w3.org/2000/09/xmldsig#enveloped-signature";
  65. public const string XmlDsigXPathTransform = "http://www.w3.org/TR/1999/REC-xpath-19991116";
  66. public const string XmlDsigXsltTransform = "http://www.w3.org/TR/1999/REC-xslt-19991116";
  67. }
  68. public const string NamespaceURI = "http://www.w3.org/2000/09/xmldsig#";
  69. public const string Prefix = "ds";
  70. public XmlSignature ()
  71. {
  72. }
  73. public static XmlElement GetChildElement (XmlElement xel, string element, string ns)
  74. {
  75. for (int i = 0; i < xel.ChildNodes.Count; i++) {
  76. XmlNode n = xel.ChildNodes [i];
  77. if (n.NodeType == XmlNodeType.Element && n.LocalName == element && n.NamespaceURI == ns)
  78. return n as XmlElement;
  79. }
  80. return null;
  81. }
  82. public static string GetAttributeFromElement (XmlElement xel, string attribute, string element)
  83. {
  84. XmlElement el = GetChildElement (xel, element, XmlSignature.NamespaceURI);
  85. return el != null ? el.GetAttribute (attribute) : null;
  86. }
  87. public static XmlElement [] GetChildElements (XmlElement xel, string element)
  88. {
  89. ArrayList al = new ArrayList ();
  90. for (int i = 0; i < xel.ChildNodes.Count; i++) {
  91. XmlNode n = xel.ChildNodes [i];
  92. if (n.NodeType == XmlNodeType.Element && n.LocalName == element && n.NamespaceURI == XmlSignature.NamespaceURI)
  93. al.Add (n);
  94. }
  95. return al.ToArray (typeof (XmlElement)) as XmlElement [];
  96. }
  97. }
  98. }