EncryptedReference.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. //
  2. // EncryptedReference.cs - EncryptedReference implementation for XML Encryption
  3. // http://www.w3.org/2001/04/xmlenc#sec-EncryptedReference
  4. //
  5. // Author:
  6. // Tim Coleman ([email protected])
  7. //
  8. // Copyright (C) Tim Coleman, 2004
  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. #if NET_2_0
  30. using System.Xml;
  31. namespace System.Security.Cryptography.Xml {
  32. public abstract class EncryptedReference {
  33. #region Fields
  34. bool cacheValid;
  35. string referenceType;
  36. string uri;
  37. TransformChain tc;
  38. #endregion // Fields
  39. #region Constructors
  40. protected EncryptedReference ()
  41. {
  42. uri = null;
  43. TransformChain = new TransformChain ();
  44. }
  45. protected EncryptedReference (string uri)
  46. {
  47. Uri = uri;
  48. TransformChain = new TransformChain ();
  49. }
  50. protected EncryptedReference (string uri, TransformChain tc)
  51. : this ()
  52. {
  53. Uri = uri;
  54. TransformChain = tc;
  55. }
  56. #endregion // Constructors
  57. #region Properties
  58. [MonoTODO()]
  59. protected internal bool CacheValid {
  60. get { return cacheValid; }
  61. }
  62. protected string ReferenceType {
  63. get { return referenceType; }
  64. set { referenceType = value; }
  65. }
  66. public TransformChain TransformChain {
  67. get { return tc; }
  68. set { tc = value; }
  69. }
  70. public string Uri {
  71. get { return uri; }
  72. set { uri = value; }
  73. }
  74. #endregion // Properties
  75. #region Methods
  76. public void AddTransform (Transform transform)
  77. {
  78. TransformChain.Add (transform);
  79. }
  80. public virtual XmlElement GetXml ()
  81. {
  82. return GetXml (new XmlDocument ());
  83. }
  84. internal virtual XmlElement GetXml (XmlDocument document)
  85. {
  86. XmlElement xel = document.CreateElement (ReferenceType, EncryptedXml.XmlEncNamespaceUrl);
  87. xel.SetAttribute (XmlEncryption.AttributeNames.URI, Uri);
  88. if (TransformChain != null && TransformChain.Count > 0) {
  89. XmlElement xtr = document.CreateElement (XmlEncryption.ElementNames.Transforms, EncryptedXml.XmlEncNamespaceUrl);
  90. foreach (Transform t in TransformChain)
  91. xtr.AppendChild (document.ImportNode (t.GetXml (), true));
  92. xel.AppendChild (xtr);
  93. }
  94. return xel;
  95. }
  96. [MonoTODO ("Make compliant.")]
  97. public virtual void LoadXml (XmlElement value)
  98. {
  99. if (value == null)
  100. throw new ArgumentNullException ("value");
  101. if ((value.LocalName != XmlEncryption.ElementNames.CipherReference) || (value.NamespaceURI != EncryptedXml.XmlEncNamespaceUrl))
  102. throw new CryptographicException ("Malformed CipherReference element.");
  103. else {
  104. Uri = null;
  105. TransformChain = new TransformChain ();
  106. foreach (XmlNode n in value.ChildNodes) {
  107. if (n is XmlWhitespace)
  108. continue;
  109. switch (n.LocalName) {
  110. case XmlEncryption.ElementNames.Transforms:
  111. foreach (XmlNode xn in ((XmlElement) n).GetElementsByTagName (XmlSignature.ElementNames.Transform, XmlSignature.NamespaceURI)) {
  112. Transform t = null;
  113. switch (((XmlElement) xn).Attributes [XmlSignature.AttributeNames.Algorithm].Value) {
  114. case XmlSignature.AlgorithmNamespaces.XmlDsigBase64Transform:
  115. t = new XmlDsigBase64Transform ();
  116. break;
  117. case XmlSignature.AlgorithmNamespaces.XmlDsigC14NTransform:
  118. t = new XmlDsigC14NTransform ();
  119. break;
  120. case XmlSignature.AlgorithmNamespaces.XmlDsigC14NWithCommentsTransform:
  121. t = new XmlDsigC14NWithCommentsTransform ();
  122. break;
  123. case XmlSignature.AlgorithmNamespaces.XmlDsigEnvelopedSignatureTransform:
  124. t = new XmlDsigEnvelopedSignatureTransform ();
  125. break;
  126. case XmlSignature.AlgorithmNamespaces.XmlDsigXPathTransform:
  127. t = new XmlDsigXPathTransform ();
  128. break;
  129. case XmlSignature.AlgorithmNamespaces.XmlDsigXsltTransform:
  130. t = new XmlDsigXsltTransform ();
  131. break;
  132. default:
  133. continue;
  134. }
  135. t.LoadInnerXml (((XmlElement) xn).ChildNodes);
  136. TransformChain.Add (t);
  137. }
  138. break;
  139. }
  140. }
  141. if (value.HasAttribute (XmlEncryption.AttributeNames.URI))
  142. Uri = value.Attributes [XmlEncryption.AttributeNames.URI].Value;
  143. }
  144. }
  145. #endregion // Methods
  146. }
  147. }
  148. #endif