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.
33 using System.Security.Cryptography;
37 namespace System.Security.Cryptography.Xml {
38 public sealed class CipherData {
43 CipherReference cipherReference;
53 public CipherData (byte[] cipherValue)
55 CipherValue = cipherValue;
58 public CipherData (CipherReference cipherReference)
60 CipherReference = cipherReference;
63 #endregion // Constructors
67 public CipherReference CipherReference {
68 get { return cipherReference; }
70 if (CipherValue != null)
71 throw new CryptographicException ("A Cipher Data element should have either a CipherValue or a CipherReference element.");
72 cipherReference = value;
76 public byte[] CipherValue {
77 get { return cipherValue; }
79 if (CipherReference != null)
80 throw new CryptographicException ("A Cipher Data element should have either a CipherValue or a CipherReference element.");
85 #endregion // Properties
89 public XmlElement GetXml ()
91 return GetXml (new XmlDocument ());
94 internal XmlElement GetXml (XmlDocument document)
96 if (CipherReference == null && CipherValue == null)
97 throw new CryptographicException ("A Cipher Data element should have either a CipherValue or a CipherReference element.");
99 XmlElement xel = document.CreateElement (XmlEncryption.ElementNames.CipherData, EncryptedXml.XmlEncNamespaceUrl);
100 if (CipherReference != null)
101 xel.AppendChild (document.ImportNode (cipherReference.GetXml (), true));
103 if (CipherValue != null) {
104 XmlElement xcv = document.CreateElement (XmlEncryption.ElementNames.CipherValue, EncryptedXml.XmlEncNamespaceUrl);
105 StreamReader reader = new StreamReader (new CryptoStream (new MemoryStream (cipherValue), new ToBase64Transform (), CryptoStreamMode.Read));
106 xcv.InnerText = reader.ReadToEnd ();
108 xel.AppendChild (xcv);
113 public void LoadXml (XmlElement value)
115 CipherReference = null;
119 throw new ArgumentNullException ("value");
121 if ((value.LocalName != XmlEncryption.ElementNames.CipherData) || (value.NamespaceURI != EncryptedXml.XmlEncNamespaceUrl))
122 throw new CryptographicException ("Malformed Cipher Data element.");
124 foreach (XmlNode n in value.ChildNodes) {
125 if (n is XmlWhitespace)
128 switch (n.LocalName) {
129 case XmlEncryption.ElementNames.CipherReference:
130 cipherReference = new CipherReference ();
131 cipherReference.LoadXml ((XmlElement) n);
133 case XmlEncryption.ElementNames.CipherValue:
134 CipherValue = Convert.FromBase64String (n.InnerText);
139 if (CipherReference == null && CipherValue == null)
140 throw new CryptographicException ("A Cipher Data element should have either a CipherValue or a CipherReference element.");
144 #endregion // Methods