CipherReference.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. //
  2. // CipherReference.cs - CipherReference implementation for XML Encryption
  3. // http://www.w3.org/2001/04/xmlenc#sec-CipherReference
  4. //
  5. // Author:
  6. // Tim Coleman ([email protected])
  7. //
  8. // Copyright (C) Tim Coleman, 2004
  9. #if NET_1_2
  10. using System.Xml;
  11. namespace System.Security.Cryptography.Xml {
  12. public sealed class CipherReference : EncryptedReference {
  13. #region Constructors
  14. public CipherReference ()
  15. : base ()
  16. {
  17. }
  18. public CipherReference (string uri)
  19. : base (uri)
  20. {
  21. }
  22. public CipherReference (string uri, TransformChain tc)
  23. : base (uri, tc)
  24. {
  25. }
  26. #endregion // Constructors
  27. #region Methods
  28. public override XmlElement GetXml ()
  29. {
  30. return GetXml (new XmlDocument ());
  31. }
  32. internal override XmlElement GetXml (XmlDocument document)
  33. {
  34. XmlElement xel = document.CreateElement (XmlEncryption.ElementNames.CipherReference, EncryptedXml.XmlEncNamespaceUrl);
  35. xel.SetAttribute (XmlEncryption.AttributeNames.URI, Uri);
  36. if (TransformChain != null && TransformChain.Count > 0) {
  37. XmlElement xtr = document.CreateElement (XmlEncryption.ElementNames.Transforms, EncryptedXml.XmlEncNamespaceUrl);
  38. foreach (Transform t in TransformChain)
  39. xtr.AppendChild (document.ImportNode (t.GetXml (), true));
  40. xel.AppendChild (xtr);
  41. }
  42. return xel;
  43. }
  44. public override void LoadXml (XmlElement value)
  45. {
  46. if (value == null)
  47. throw new ArgumentNullException ("value");
  48. if ((value.LocalName != XmlEncryption.ElementNames.CipherReference) || (value.NamespaceURI != EncryptedXml.XmlEncNamespaceUrl))
  49. throw new CryptographicException ("Malformed CipherReference element.");
  50. else {
  51. Uri = null;
  52. TransformChain = new TransformChain ();
  53. foreach (XmlNode n in value.ChildNodes) {
  54. if (n is XmlWhitespace)
  55. continue;
  56. switch (n.LocalName) {
  57. case XmlEncryption.ElementNames.Transforms:
  58. foreach (XmlNode xn in ((XmlElement) n).GetElementsByTagName (XmlSignature.ElementNames.Transform, XmlSignature.NamespaceURI)) {
  59. Transform t = null;
  60. switch (((XmlElement) xn).Attributes [XmlSignature.AttributeNames.Algorithm].Value) {
  61. case XmlSignature.AlgorithmNamespaces.XmlDsigBase64Transform:
  62. t = new XmlDsigBase64Transform ();
  63. break;
  64. case XmlSignature.AlgorithmNamespaces.XmlDsigC14NTransform:
  65. t = new XmlDsigC14NTransform ();
  66. break;
  67. case XmlSignature.AlgorithmNamespaces.XmlDsigC14NWithCommentsTransform:
  68. t = new XmlDsigC14NWithCommentsTransform ();
  69. break;
  70. case XmlSignature.AlgorithmNamespaces.XmlDsigEnvelopedSignatureTransform:
  71. t = new XmlDsigEnvelopedSignatureTransform ();
  72. break;
  73. case XmlSignature.AlgorithmNamespaces.XmlDsigXPathTransform:
  74. t = new XmlDsigXPathTransform ();
  75. break;
  76. case XmlSignature.AlgorithmNamespaces.XmlDsigXsltTransform:
  77. t = new XmlDsigXsltTransform ();
  78. break;
  79. default:
  80. continue;
  81. }
  82. t.LoadInnerXml (((XmlElement) xn).ChildNodes);
  83. TransformChain.Add (t);
  84. }
  85. break;
  86. }
  87. }
  88. if (value.HasAttribute (XmlEncryption.AttributeNames.URI))
  89. Uri = value.Attributes [XmlEncryption.AttributeNames.URI].Value;
  90. }
  91. }
  92. #endregion // Methods
  93. }
  94. }
  95. #endif