2 // EncryptedData.cs - EncryptedData implementation for XML Encryption
3 // http://www.w3.org/2001/04/xmlenc#sec-EncryptedData
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.
35 namespace System.Security.Cryptography.Xml {
36 public sealed class EncryptedData : EncryptedType {
40 public EncryptedData ()
45 #endregion // Constructors
49 public override XmlElement GetXml ()
51 return GetXml (new XmlDocument ());
54 internal XmlElement GetXml (XmlDocument document)
56 if (CipherData == null)
57 throw new CryptographicException ("Cipher data is not specified.");
59 XmlElement xel = document.CreateElement (XmlEncryption.ElementNames.EncryptedData, EncryptedXml.XmlEncNamespaceUrl);
61 if (EncryptionMethod != null)
62 xel.AppendChild (EncryptionMethod.GetXml (document));
64 xel.AppendChild (document.ImportNode (KeyInfo.GetXml (), true));
65 if (CipherData != null)
66 xel.AppendChild (CipherData.GetXml (document));
68 if (EncryptionProperties.Count > 0) {
69 XmlElement xep = document.CreateElement (XmlEncryption.ElementNames.EncryptionProperties, EncryptedXml.XmlEncNamespaceUrl);
70 foreach (EncryptionProperty p in EncryptionProperties)
71 xep.AppendChild (p.GetXml (document));
72 xel.AppendChild (xep);
76 xel.SetAttribute (XmlEncryption.AttributeNames.Id, Id);
78 xel.SetAttribute (XmlEncryption.AttributeNames.Type, Type);
80 xel.SetAttribute (XmlEncryption.AttributeNames.MimeType, MimeType);
82 xel.SetAttribute (XmlEncryption.AttributeNames.Encoding, Encoding);
86 public override void LoadXml (XmlElement value)
89 throw new ArgumentNullException ("value");
91 if ((value.LocalName != XmlEncryption.ElementNames.EncryptedData) || (value.NamespaceURI != EncryptedXml.XmlEncNamespaceUrl))
92 throw new CryptographicException ("Malformed EncryptedData element.");
94 EncryptionMethod = null;
95 KeyInfo keyInfo = null;
96 CipherData cipherData = null;
97 EncryptionMethod = null;
98 EncryptionProperties = new EncryptionProperties ();
104 foreach (XmlNode n in value.ChildNodes) {
105 if (n is XmlWhitespace)
108 switch (n.LocalName) {
109 case XmlEncryption.ElementNames.EncryptionMethod:
110 EncryptionMethod = new EncryptionMethod ();
111 EncryptionMethod.LoadXml ((XmlElement) n);
113 case XmlSignature.ElementNames.KeyInfo:
114 KeyInfo = new KeyInfo ();
115 KeyInfo.LoadXml ((XmlElement) n);
117 case XmlEncryption.ElementNames.CipherData:
118 CipherData = new CipherData ();
119 CipherData.LoadXml ((XmlElement) n);
121 case XmlEncryption.ElementNames.EncryptionProperties:
122 foreach (XmlElement element in ((XmlElement) n).GetElementsByTagName (XmlEncryption.ElementNames.EncryptionProperty, EncryptedXml.XmlEncNamespaceUrl))
123 EncryptionProperties.Add (new EncryptionProperty (element));
128 if (value.HasAttribute (XmlEncryption.AttributeNames.Id))
129 Id = value.Attributes [XmlEncryption.AttributeNames.Id].Value;
130 if (value.HasAttribute (XmlEncryption.AttributeNames.Type))
131 Type = value.Attributes [XmlEncryption.AttributeNames.Type].Value;
132 if (value.HasAttribute (XmlEncryption.AttributeNames.MimeType))
133 MimeType = value.Attributes [XmlEncryption.AttributeNames.MimeType].Value;
134 if (value.HasAttribute (XmlEncryption.AttributeNames.Encoding))
135 Encoding = value.Attributes [XmlEncryption.AttributeNames.Encoding].Value;
139 #endregion // Methods