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.
32 using System.Security.Cryptography.X509Certificates;
35 namespace System.Security.Cryptography.Xml {
37 public sealed class EncryptedData : EncryptedType {
41 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 EncryptionMethod = null;
96 EncryptionProperties.Clear ();
102 foreach (XmlNode n in value.ChildNodes) {
103 if (n is XmlWhitespace)
106 switch (n.LocalName) {
107 case XmlEncryption.ElementNames.EncryptionMethod:
108 EncryptionMethod = new EncryptionMethod ();
109 EncryptionMethod.LoadXml ((XmlElement) n);
111 case XmlSignature.ElementNames.KeyInfo:
112 KeyInfo = new KeyInfo ();
113 KeyInfo.LoadXml ((XmlElement) n);
115 case XmlEncryption.ElementNames.CipherData:
116 CipherData = new CipherData ();
117 CipherData.LoadXml ((XmlElement) n);
119 case XmlEncryption.ElementNames.EncryptionProperties:
120 foreach (XmlElement element in ((XmlElement) n).GetElementsByTagName (XmlEncryption.ElementNames.EncryptionProperty, EncryptedXml.XmlEncNamespaceUrl))
121 EncryptionProperties.Add (new EncryptionProperty (element));
126 if (value.HasAttribute (XmlEncryption.AttributeNames.Id))
127 Id = value.Attributes [XmlEncryption.AttributeNames.Id].Value;
128 if (value.HasAttribute (XmlEncryption.AttributeNames.Type))
129 Type = value.Attributes [XmlEncryption.AttributeNames.Type].Value;
130 if (value.HasAttribute (XmlEncryption.AttributeNames.MimeType))
131 MimeType = value.Attributes [XmlEncryption.AttributeNames.MimeType].Value;
132 if (value.HasAttribute (XmlEncryption.AttributeNames.Encoding))
133 Encoding = value.Attributes [XmlEncryption.AttributeNames.Encoding].Value;
137 #endregion // Methods