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.
33 using System.Security.Cryptography.X509Certificates;
36 namespace System.Security.Cryptography.Xml {
38 public sealed class EncryptedData : EncryptedType {
42 public EncryptedData ()
46 #endregion // Constructors
50 public override XmlElement GetXml ()
52 return GetXml (new XmlDocument ());
55 internal XmlElement GetXml (XmlDocument document)
57 if (CipherData == null)
58 throw new CryptographicException ("Cipher data is not specified.");
60 XmlElement xel = document.CreateElement (XmlEncryption.ElementNames.EncryptedData, EncryptedXml.XmlEncNamespaceUrl);
62 if (EncryptionMethod != null)
63 xel.AppendChild (EncryptionMethod.GetXml (document));
65 xel.AppendChild (document.ImportNode (KeyInfo.GetXml (), true));
66 if (CipherData != null)
67 xel.AppendChild (CipherData.GetXml (document));
69 if (EncryptionProperties.Count > 0) {
70 XmlElement xep = document.CreateElement (XmlEncryption.ElementNames.EncryptionProperties, EncryptedXml.XmlEncNamespaceUrl);
71 foreach (EncryptionProperty p in EncryptionProperties)
72 xep.AppendChild (p.GetXml (document));
73 xel.AppendChild (xep);
77 xel.SetAttribute (XmlEncryption.AttributeNames.Id, Id);
79 xel.SetAttribute (XmlEncryption.AttributeNames.Type, Type);
81 xel.SetAttribute (XmlEncryption.AttributeNames.MimeType, MimeType);
83 xel.SetAttribute (XmlEncryption.AttributeNames.Encoding, Encoding);
87 public override void LoadXml (XmlElement value)
90 throw new ArgumentNullException ("value");
92 if ((value.LocalName != XmlEncryption.ElementNames.EncryptedData) || (value.NamespaceURI != EncryptedXml.XmlEncNamespaceUrl))
93 throw new CryptographicException ("Malformed EncryptedData element.");
95 EncryptionMethod = null;
96 EncryptionMethod = null;
97 EncryptionProperties.Clear ();
103 foreach (XmlNode n in value.ChildNodes) {
104 if (n is XmlWhitespace)
107 switch (n.LocalName) {
108 case XmlEncryption.ElementNames.EncryptionMethod:
109 EncryptionMethod = new EncryptionMethod ();
110 EncryptionMethod.LoadXml ((XmlElement) n);
112 case XmlSignature.ElementNames.KeyInfo:
113 KeyInfo = new KeyInfo ();
114 KeyInfo.LoadXml ((XmlElement) n);
116 case XmlEncryption.ElementNames.CipherData:
117 CipherData = new CipherData ();
118 CipherData.LoadXml ((XmlElement) n);
120 case XmlEncryption.ElementNames.EncryptionProperties:
121 foreach (XmlElement element in ((XmlElement) n).GetElementsByTagName (XmlEncryption.ElementNames.EncryptionProperty, EncryptedXml.XmlEncNamespaceUrl))
122 EncryptionProperties.Add (new EncryptionProperty (element));
127 if (value.HasAttribute (XmlEncryption.AttributeNames.Id))
128 Id = value.Attributes [XmlEncryption.AttributeNames.Id].Value;
129 if (value.HasAttribute (XmlEncryption.AttributeNames.Type))
130 Type = value.Attributes [XmlEncryption.AttributeNames.Type].Value;
131 if (value.HasAttribute (XmlEncryption.AttributeNames.MimeType))
132 MimeType = value.Attributes [XmlEncryption.AttributeNames.MimeType].Value;
133 if (value.HasAttribute (XmlEncryption.AttributeNames.Encoding))
134 Encoding = value.Attributes [XmlEncryption.AttributeNames.Encoding].Value;
138 #endregion // Methods