XmlSignature.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System;
  32. using System.Collections;
  33. using System.Xml;
  34. namespace System.Security.Cryptography.Xml {
  35. // following the design of WSE
  36. internal class XmlSignature {
  37. public class ElementNames {
  38. public const string CanonicalizationMethod = "CanonicalizationMethod";
  39. public const string DigestMethod = "DigestMethod";
  40. public const string DigestValue = "DigestValue";
  41. public const string DSAKeyValue = "DSAKeyValue";
  42. public const string HMACOutputLength = "HMACOutputLength";
  43. public const string KeyInfo = "KeyInfo";
  44. public const string KeyName = "KeyName";
  45. public const string KeyValue = "KeyValue";
  46. public const string Manifest = "Manifest";
  47. public const string Object = "Object";
  48. public const string Reference = "Reference";
  49. #if NET_1_0
  50. // RetrievalMethod vs RetrievalElement -> BUG in MS Framework 1.0
  51. public const string RetrievalMethod = "RetrievalElement";
  52. #else
  53. public const string RetrievalMethod = "RetrievalMethod";
  54. #endif
  55. public const string RSAKeyValue = "RSAKeyValue";
  56. public const string Signature = "Signature";
  57. public const string SignatureMethod = "SignatureMethod";
  58. public const string SignatureValue = "SignatureValue";
  59. public const string SignedInfo = "SignedInfo";
  60. public const string Transform = "Transform";
  61. public const string Transforms = "Transforms";
  62. public const string X509Data = "X509Data";
  63. public const string X509IssuerSerial = "X509IssuerSerial";
  64. public const string X509IssuerName = "X509IssuerName";
  65. public const string X509SerialNumber = "X509SerialNumber";
  66. public const string X509SKI = "X509SKI";
  67. public const string X509SubjectName = "X509SubjectName";
  68. public const string X509Certificate = "X509Certificate";
  69. public const string X509CRL = "X509CRL";
  70. public ElementNames () {}
  71. }
  72. public class AttributeNames {
  73. public const string Algorithm = "Algorithm";
  74. public const string Encoding = "Encoding";
  75. public const string Id = "Id";
  76. public const string MimeType = "MimeType";
  77. public const string Type = "Type";
  78. public const string URI = "URI";
  79. public AttributeNames () {}
  80. }
  81. public class AlgorithmNamespaces {
  82. public const string XmlDsigBase64Transform = "http://www.w3.org/2000/09/xmldsig#base64";
  83. public const string XmlDsigC14NTransform = "http://www.w3.org/TR/2001/REC-xml-c14n-20010315";
  84. public const string XmlDsigC14NWithCommentsTransform = "http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments";
  85. public const string XmlDsigEnvelopedSignatureTransform = "http://www.w3.org/2000/09/xmldsig#enveloped-signature";
  86. public const string XmlDsigXPathTransform = "http://www.w3.org/TR/1999/REC-xpath-19991116";
  87. public const string XmlDsigXsltTransform = "http://www.w3.org/TR/1999/REC-xslt-19991116";
  88. }
  89. public class Uri {
  90. public const string Manifest = "http://www.w3.org/2000/09/xmldsig#Manifest";
  91. }
  92. public const string NamespaceURI = "http://www.w3.org/2000/09/xmldsig#";
  93. public const string Prefix = "ds";
  94. public XmlSignature ()
  95. {
  96. }
  97. public static XmlElement GetChildElement (XmlElement xel, string element, string ns)
  98. {
  99. for (int i = 0; i < xel.ChildNodes.Count; i++) {
  100. XmlNode n = xel.ChildNodes [i];
  101. if (n.NodeType == XmlNodeType.Element && n.LocalName == element && n.NamespaceURI == ns)
  102. return n as XmlElement;
  103. }
  104. return null;
  105. }
  106. public static string GetAttributeFromElement (XmlElement xel, string attribute, string element)
  107. {
  108. XmlElement el = GetChildElement (xel, element, XmlSignature.NamespaceURI);
  109. return el != null ? el.GetAttribute (attribute) : null;
  110. }
  111. public static XmlElement [] GetChildElements (XmlElement xel, string element)
  112. {
  113. ArrayList al = new ArrayList ();
  114. for (int i = 0; i < xel.ChildNodes.Count; i++) {
  115. XmlNode n = xel.ChildNodes [i];
  116. if (n.NodeType == XmlNodeType.Element && n.LocalName == element && n.NamespaceURI == XmlSignature.NamespaceURI)
  117. al.Add (n);
  118. }
  119. return al.ToArray (typeof (XmlElement)) as XmlElement [];
  120. }
  121. }
  122. }