2 // CipherData.cs - CipherData implementation for XML Encryption
3 // http://www.w3.org/2001/04/xmlenc#sec-CipherData
6 // Tim Coleman (tim@timcoleman.com)
8 // Copyright (C) Tim Coleman, 2004
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Security.Cryptography;
36 namespace System.Security.Cryptography.Xml {
37 public sealed class CipherData {
42 CipherReference cipherReference;
52 public CipherData (byte[] cipherValue)
54 CipherValue = cipherValue;
57 public CipherData (CipherReference cipherReference)
59 CipherReference = cipherReference;
62 #endregion // Constructors
66 public CipherReference CipherReference {
67 get { return cipherReference; }
69 if (CipherValue != null)
70 throw new CryptographicException ("A Cipher Data element should have either a CipherValue or a CipherReference element.");
71 cipherReference = value;
75 public byte[] CipherValue {
76 get { return cipherValue; }
78 if (CipherReference != null)
79 throw new CryptographicException ("A Cipher Data element should have either a CipherValue or a CipherReference element.");
84 #endregion // Properties
88 public XmlElement GetXml ()
90 return GetXml (new XmlDocument ());
93 internal XmlElement GetXml (XmlDocument document)
95 if (CipherReference == null && CipherValue == null)
96 throw new CryptographicException ("A Cipher Data element should have either a CipherValue or a CipherReference element.");
98 XmlElement xel = document.CreateElement (XmlEncryption.ElementNames.CipherData, EncryptedXml.XmlEncNamespaceUrl);
99 if (CipherReference != null)
100 xel.AppendChild (document.ImportNode (cipherReference.GetXml (), true));
102 if (CipherValue != null) {
103 XmlElement xcv = document.CreateElement (XmlEncryption.ElementNames.CipherValue, EncryptedXml.XmlEncNamespaceUrl);
104 StreamReader reader = new StreamReader (new CryptoStream (new MemoryStream (cipherValue), new ToBase64Transform (), CryptoStreamMode.Read));
105 xcv.InnerText = reader.ReadToEnd ();
107 xel.AppendChild (xcv);
112 public void LoadXml (XmlElement value)
114 CipherReference = null;
118 throw new ArgumentNullException ("value");
120 if ((value.LocalName != XmlEncryption.ElementNames.CipherData) || (value.NamespaceURI != EncryptedXml.XmlEncNamespaceUrl))
121 throw new CryptographicException ("Malformed Cipher Data element.");
123 foreach (XmlNode n in value.ChildNodes) {
124 if (n is XmlWhitespace)
127 switch (n.LocalName) {
128 case XmlEncryption.ElementNames.CipherReference:
129 cipherReference = new CipherReference ();
130 cipherReference.LoadXml ((XmlElement) n);
132 case XmlEncryption.ElementNames.CipherValue:
133 CipherValue = Convert.FromBase64String (n.InnerText);
138 if (CipherReference == null && CipherValue == null)
139 throw new CryptographicException ("A Cipher Data element should have either a CipherValue or a CipherReference element.");
143 #endregion // Methods