XmlSignature.cs 4.6 KB

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