Reference.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. //
  2. // Reference.cs - Reference 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. // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System.IO;
  30. using System.Runtime.InteropServices;
  31. using System.Xml;
  32. namespace System.Security.Cryptography.Xml {
  33. // http://www.w3.org/TR/2002/REC-xmldsig-core-20020212/Overview.html#sec-Reference
  34. public class Reference {
  35. private TransformChain chain;
  36. private string digestMethod;
  37. private byte[] digestValue;
  38. private string id;
  39. private string uri;
  40. private string type;
  41. private Stream stream;
  42. private XmlElement element;
  43. public Reference ()
  44. {
  45. chain = new TransformChain ();
  46. digestMethod = XmlSignature.NamespaceURI + "sha1";
  47. }
  48. [MonoTODO ("There is no description about how it is used.")]
  49. public Reference (Stream stream) : this ()
  50. {
  51. this.stream = stream;
  52. }
  53. public Reference (string uri) : this ()
  54. {
  55. this.uri = uri;
  56. }
  57. // default to SHA1
  58. public string DigestMethod {
  59. get { return digestMethod; }
  60. set {
  61. element = null;
  62. digestMethod = value;
  63. }
  64. }
  65. public byte[] DigestValue {
  66. get { return digestValue; }
  67. set {
  68. element = null;
  69. digestValue = value;
  70. }
  71. }
  72. public string Id {
  73. get { return id; }
  74. set {
  75. element = null;
  76. id = value;
  77. }
  78. }
  79. public TransformChain TransformChain {
  80. get { return chain; }
  81. #if NET_2_0
  82. [ComVisible (false)]
  83. set { chain = value; }
  84. #endif
  85. }
  86. public string Type {
  87. get { return type; }
  88. set {
  89. element = null;
  90. type = value;
  91. }
  92. }
  93. public string Uri {
  94. get { return uri; }
  95. set {
  96. element = null;
  97. uri = value;
  98. }
  99. }
  100. public void AddTransform (Transform transform)
  101. {
  102. chain.Add (transform);
  103. }
  104. public XmlElement GetXml ()
  105. {
  106. if (element != null)
  107. return element;
  108. if (digestMethod == null)
  109. throw new CryptographicException ("DigestMethod");
  110. if (digestValue == null)
  111. throw new NullReferenceException ("DigestValue");
  112. XmlDocument document = new XmlDocument ();
  113. XmlElement xel = document.CreateElement (XmlSignature.ElementNames.Reference, XmlSignature.NamespaceURI);
  114. if (id != null)
  115. xel.SetAttribute (XmlSignature.AttributeNames.Id, id);
  116. if (uri != null)
  117. xel.SetAttribute (XmlSignature.AttributeNames.URI, uri);
  118. if (type != null)
  119. xel.SetAttribute (XmlSignature.AttributeNames.Type, type);
  120. if (chain.Count > 0) {
  121. XmlElement ts = document.CreateElement (XmlSignature.ElementNames.Transforms, XmlSignature.NamespaceURI);
  122. foreach (Transform t in chain) {
  123. XmlNode xn = t.GetXml ();
  124. XmlNode newNode = document.ImportNode (xn, true);
  125. ts.AppendChild (newNode);
  126. }
  127. xel.AppendChild (ts);
  128. }
  129. XmlElement dm = document.CreateElement (XmlSignature.ElementNames.DigestMethod, XmlSignature.NamespaceURI);
  130. dm.SetAttribute (XmlSignature.AttributeNames.Algorithm, digestMethod);
  131. xel.AppendChild (dm);
  132. XmlElement dv = document.CreateElement (XmlSignature.ElementNames.DigestValue, XmlSignature.NamespaceURI);
  133. dv.InnerText = Convert.ToBase64String (digestValue);
  134. xel.AppendChild (dv);
  135. return xel;
  136. }
  137. // note: we do NOT return null -on purpose- if attribute isn't found
  138. private string GetAttribute (XmlElement xel, string attribute)
  139. {
  140. XmlAttribute xa = xel.Attributes [attribute];
  141. return ((xa != null) ? xa.InnerText : null);
  142. }
  143. public void LoadXml (XmlElement value)
  144. {
  145. if (value == null)
  146. throw new ArgumentNullException ("value");
  147. if ((value.LocalName != XmlSignature.ElementNames.Reference) || (value.NamespaceURI != XmlSignature.NamespaceURI))
  148. throw new CryptographicException ();
  149. id = GetAttribute (value, XmlSignature.AttributeNames.Id);
  150. uri = GetAttribute (value, XmlSignature.AttributeNames.URI);
  151. type = GetAttribute (value, XmlSignature.AttributeNames.Type);
  152. // Note: order is important for validations
  153. XmlNodeList xnl = value.GetElementsByTagName (XmlSignature.ElementNames.Transform, XmlSignature.NamespaceURI);
  154. if ((xnl != null) && (xnl.Count > 0)) {
  155. Transform t = null;
  156. foreach (XmlNode xn in xnl) {
  157. string a = GetAttribute ((XmlElement)xn, XmlSignature.AttributeNames.Algorithm);
  158. /* This code is useful for debugging in VS.NET because using CryptoConfig
  159. (from MS mscorlib) would throw InvalidCastException because it's
  160. Transform would come from MS System.Security.dll not Mono's.
  161. switch (a) {
  162. case "http://www.w3.org/2000/09/xmldsig#base64":
  163. t = new XmlDsigBase64Transform ();
  164. break;
  165. case "http://www.w3.org/TR/2001/REC-xml-c14n-20010315":
  166. t = new XmlDsigC14NTransform ();
  167. break;
  168. case "http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments":
  169. t = new XmlDsigC14NWithCommentsTransform ();
  170. break;
  171. case "http://www.w3.org/2000/09/xmldsig#enveloped-signature":
  172. t = new XmlDsigEnvelopedSignatureTransform ();
  173. break;
  174. case "http://www.w3.org/TR/1999/REC-xpath-19991116":
  175. t = new XmlDsigXPathTransform ();
  176. break;
  177. case "http://www.w3.org/TR/1999/REC-xslt-19991116":
  178. t = new XmlDsigXsltTransform ();
  179. break;
  180. case "http://www.w3.org/2002/07/decrypt#XML":
  181. t = new XmlDecryptionTransform ();
  182. break;
  183. default:
  184. throw new NotSupportedException ();
  185. }
  186. */
  187. t = (Transform) CryptoConfig.CreateFromName (a);
  188. if (t == null)
  189. throw new CryptographicException ("Unknown transform {0}.", a);
  190. if (xn.ChildNodes.Count > 0) {
  191. t.LoadInnerXml (xn.ChildNodes);
  192. }
  193. AddTransform (t);
  194. }
  195. }
  196. // get DigestMethod
  197. DigestMethod = XmlSignature.GetAttributeFromElement (value, XmlSignature.AttributeNames.Algorithm, XmlSignature.ElementNames.DigestMethod);
  198. // get DigestValue
  199. XmlElement dig = XmlSignature.GetChildElement (value, XmlSignature.ElementNames.DigestValue, XmlSignature.NamespaceURI);
  200. if (dig != null)
  201. DigestValue = Convert.FromBase64String (dig.InnerText);
  202. element = value;
  203. }
  204. }
  205. }