XmlSignature.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. #if NET_2_0
  45. public const string EncryptedKey = "EncryptedKey";
  46. #endif
  47. public const string HMACOutputLength = "HMACOutputLength";
  48. public const string KeyInfo = "KeyInfo";
  49. public const string KeyName = "KeyName";
  50. public const string KeyValue = "KeyValue";
  51. public const string Manifest = "Manifest";
  52. public const string Object = "Object";
  53. public const string Reference = "Reference";
  54. #if NET_1_0
  55. // RetrievalMethod vs RetrievalElement -> BUG in MS Framework 1.0
  56. public const string RetrievalMethod = "RetrievalElement";
  57. #else
  58. public const string RetrievalMethod = "RetrievalMethod";
  59. #endif
  60. public const string RSAKeyValue = "RSAKeyValue";
  61. public const string Signature = "Signature";
  62. public const string SignatureMethod = "SignatureMethod";
  63. public const string SignatureValue = "SignatureValue";
  64. public const string SignedInfo = "SignedInfo";
  65. public const string Transform = "Transform";
  66. public const string Transforms = "Transforms";
  67. public const string X509Data = "X509Data";
  68. public const string X509IssuerSerial = "X509IssuerSerial";
  69. public const string X509IssuerName = "X509IssuerName";
  70. public const string X509SerialNumber = "X509SerialNumber";
  71. public const string X509SKI = "X509SKI";
  72. public const string X509SubjectName = "X509SubjectName";
  73. public const string X509Certificate = "X509Certificate";
  74. public const string X509CRL = "X509CRL";
  75. public ElementNames () {}
  76. }
  77. public class AttributeNames {
  78. public const string Algorithm = "Algorithm";
  79. public const string Encoding = "Encoding";
  80. public const string Id = "Id";
  81. public const string MimeType = "MimeType";
  82. public const string Type = "Type";
  83. public const string URI = "URI";
  84. public AttributeNames () {}
  85. }
  86. public class AlgorithmNamespaces {
  87. public const string XmlDsigBase64Transform = "http://www.w3.org/2000/09/xmldsig#base64";
  88. public const string XmlDsigC14NTransform = "http://www.w3.org/TR/2001/REC-xml-c14n-20010315";
  89. public const string XmlDsigC14NWithCommentsTransform = "http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments";
  90. public const string XmlDsigEnvelopedSignatureTransform = "http://www.w3.org/2000/09/xmldsig#enveloped-signature";
  91. public const string XmlDsigXPathTransform = "http://www.w3.org/TR/1999/REC-xpath-19991116";
  92. public const string XmlDsigXsltTransform = "http://www.w3.org/TR/1999/REC-xslt-19991116";
  93. #if NET_2_0
  94. public const string XmlDsigExcC14NTransform = "http://www.w3.org/2001/10/xml-exc-c14n#";
  95. public const string XmlDsigExcC14NWithCommentsTransform = "http://www.w3.org/2001/10/xml-exc-c14n#WithComments";
  96. public const string XmlDecryptionTransform = "http://www.w3.org/2002/07/decrypt#XML";
  97. #endif
  98. }
  99. public class Uri {
  100. public const string Manifest = "http://www.w3.org/2000/09/xmldsig#Manifest";
  101. }
  102. public const string NamespaceURI = "http://www.w3.org/2000/09/xmldsig#";
  103. public const string Prefix = "ds";
  104. public XmlSignature ()
  105. {
  106. }
  107. public static XmlElement GetChildElement (XmlElement xel, string element, string ns)
  108. {
  109. for (int i = 0; i < xel.ChildNodes.Count; i++) {
  110. XmlNode n = xel.ChildNodes [i];
  111. if (n.NodeType == XmlNodeType.Element && n.LocalName == element && n.NamespaceURI == ns)
  112. return n as XmlElement;
  113. }
  114. return null;
  115. }
  116. public static string GetAttributeFromElement (XmlElement xel, string attribute, string element)
  117. {
  118. XmlElement el = GetChildElement (xel, element, XmlSignature.NamespaceURI);
  119. return el != null ? el.GetAttribute (attribute) : null;
  120. }
  121. public static XmlElement [] GetChildElements (XmlElement xel, string element)
  122. {
  123. ArrayList al = new ArrayList ();
  124. for (int i = 0; i < xel.ChildNodes.Count; i++) {
  125. XmlNode n = xel.ChildNodes [i];
  126. if (n.NodeType == XmlNodeType.Element && n.LocalName == element && n.NamespaceURI == XmlSignature.NamespaceURI)
  127. al.Add (n);
  128. }
  129. return al.ToArray (typeof (XmlElement)) as XmlElement [];
  130. }
  131. }
  132. }