CipherData.cs 4.3 KB

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