CipherData.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. //
  2. // CipherData.cs - CipherData implementation for XML Encryption
  3. // http://www.w3.org/2001/04/xmlenc#sec-CipherData
  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.Security.Cryptography;
  31. using System.IO;
  32. using System.Xml;
  33. namespace System.Security.Cryptography.Xml {
  34. public sealed class CipherData {
  35. #region Fields
  36. byte[] cipherValue;
  37. CipherReference cipherReference;
  38. #endregion // Fields
  39. #region Constructors
  40. public CipherData ()
  41. {
  42. }
  43. public CipherData (byte[] cipherValue)
  44. {
  45. CipherValue = cipherValue;
  46. }
  47. public CipherData (CipherReference cipherReference)
  48. {
  49. CipherReference = cipherReference;
  50. }
  51. #endregion // Constructors
  52. #region Properties
  53. public CipherReference CipherReference {
  54. get { return cipherReference; }
  55. set {
  56. if (CipherValue != null)
  57. throw new CryptographicException ("A Cipher Data element should have either a CipherValue or a CipherReference element.");
  58. cipherReference = value;
  59. }
  60. }
  61. public byte[] CipherValue {
  62. get { return cipherValue; }
  63. set {
  64. if (CipherReference != null)
  65. throw new CryptographicException ("A Cipher Data element should have either a CipherValue or a CipherReference element.");
  66. cipherValue = value;
  67. }
  68. }
  69. #endregion // Properties
  70. #region Methods
  71. public XmlElement GetXml ()
  72. {
  73. return GetXml (new XmlDocument ());
  74. }
  75. internal XmlElement GetXml (XmlDocument document)
  76. {
  77. if (CipherReference == null && CipherValue == null)
  78. throw new CryptographicException ("A Cipher Data element should have either a CipherValue or a CipherReference element.");
  79. XmlElement xel = document.CreateElement (XmlEncryption.ElementNames.CipherData, EncryptedXml.XmlEncNamespaceUrl);
  80. if (CipherReference != null)
  81. xel.AppendChild (document.ImportNode (cipherReference.GetXml (), true));
  82. if (CipherValue != null) {
  83. XmlElement xcv = document.CreateElement (XmlEncryption.ElementNames.CipherValue, EncryptedXml.XmlEncNamespaceUrl);
  84. StreamReader reader = new StreamReader (new CryptoStream (new MemoryStream (cipherValue), new ToBase64Transform (), CryptoStreamMode.Read));
  85. xcv.InnerText = reader.ReadToEnd ();
  86. reader.Close ();
  87. xel.AppendChild (xcv);
  88. }
  89. return xel;
  90. }
  91. public void LoadXml (XmlElement value)
  92. {
  93. CipherReference = null;
  94. CipherValue = null;
  95. if (value == null)
  96. throw new ArgumentNullException ("value");
  97. if ((value.LocalName != XmlEncryption.ElementNames.CipherData) || (value.NamespaceURI != EncryptedXml.XmlEncNamespaceUrl))
  98. throw new CryptographicException ("Malformed Cipher Data element.");
  99. else {
  100. foreach (XmlNode n in value.ChildNodes) {
  101. if (n is XmlWhitespace)
  102. continue;
  103. switch (n.LocalName) {
  104. case XmlEncryption.ElementNames.CipherReference:
  105. cipherReference = new CipherReference ();
  106. cipherReference.LoadXml ((XmlElement) n);
  107. break;
  108. case XmlEncryption.ElementNames.CipherValue:
  109. CipherValue = Convert.FromBase64String (n.InnerText);
  110. break;
  111. }
  112. }
  113. if (CipherReference == null && CipherValue == null)
  114. throw new CryptographicException ("A Cipher Data element should have either a CipherValue or a CipherReference element.");
  115. }
  116. }
  117. #endregion // Methods
  118. }
  119. }
  120. #endif