* roottypes.cs: Rename from tree.cs.
[mono.git] / mcs / class / System.Security / System.Security.Cryptography.Xml / EncryptedData.cs
1 //
2 // EncryptedData.cs - EncryptedData implementation for XML Encryption
3 // http://www.w3.org/2001/04/xmlenc#sec-EncryptedData
4 //
5 // Author:
6 //      Tim Coleman (tim@timcoleman.com)
7 //
8 // Copyright (C) Tim Coleman, 2004
9
10 //
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:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
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.
29 //
30
31 #if NET_2_0
32
33 using System.Security.Cryptography.X509Certificates; 
34 using System.Xml;
35
36 namespace System.Security.Cryptography.Xml {
37
38         public sealed class EncryptedData : EncryptedType {
39
40                 #region Constructors
41
42                 public EncryptedData ()
43                 {
44                 }
45
46                 #endregion // Constructors
47
48                 #region Methods
49
50                 public override XmlElement GetXml ()
51                 {
52                         return GetXml (new XmlDocument ());
53                 }
54
55                 internal XmlElement GetXml (XmlDocument document)
56                 {
57                         if (CipherData == null)
58                                 throw new CryptographicException ("Cipher data is not specified.");
59
60                         XmlElement xel = document.CreateElement (XmlEncryption.ElementNames.EncryptedData, EncryptedXml.XmlEncNamespaceUrl);
61
62                         if (EncryptionMethod != null)
63                                 xel.AppendChild (EncryptionMethod.GetXml (document));
64                         if (KeyInfo != null) 
65                                 xel.AppendChild (document.ImportNode (KeyInfo.GetXml (), true));
66                         if (CipherData != null)
67                                 xel.AppendChild (CipherData.GetXml (document));
68
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);
74                         }
75
76                         if (Id != null)
77                                 xel.SetAttribute (XmlEncryption.AttributeNames.Id, Id);
78                         if (Type != null)
79                                 xel.SetAttribute (XmlEncryption.AttributeNames.Type, Type);
80                         if (MimeType != null)
81                                 xel.SetAttribute (XmlEncryption.AttributeNames.MimeType, MimeType);
82                         if (Encoding != null)
83                                 xel.SetAttribute (XmlEncryption.AttributeNames.Encoding, Encoding);
84                         return xel;
85                 }
86
87                 public override void LoadXml (XmlElement value)
88                 {
89                         if (value == null)
90                                 throw new ArgumentNullException ("value");
91
92                         if ((value.LocalName != XmlEncryption.ElementNames.EncryptedData) || (value.NamespaceURI != EncryptedXml.XmlEncNamespaceUrl))
93                                 throw new CryptographicException ("Malformed EncryptedData element.");
94                         else {
95                                 EncryptionMethod = null;
96                                 EncryptionMethod = null;
97                                 EncryptionProperties.Clear ();
98                                 Id = null;
99                                 Type = null;
100                                 MimeType = null;
101                                 Encoding = null;
102
103                                 foreach (XmlNode n in value.ChildNodes) {
104                                         if (n is XmlWhitespace)
105                                                 continue;
106
107                                         switch (n.LocalName) {
108                                         case XmlEncryption.ElementNames.EncryptionMethod:
109                                                 EncryptionMethod = new EncryptionMethod ();
110                                                 EncryptionMethod.LoadXml ((XmlElement) n);
111                                                 break;
112                                         case XmlSignature.ElementNames.KeyInfo:
113                                                 KeyInfo = new KeyInfo ();
114                                                 KeyInfo.LoadXml ((XmlElement) n);
115                                                 break;
116                                         case XmlEncryption.ElementNames.CipherData:
117                                                 CipherData = new CipherData ();
118                                                 CipherData.LoadXml ((XmlElement) n);
119                                                 break;
120                                         case XmlEncryption.ElementNames.EncryptionProperties:
121                                                 foreach (XmlElement element in ((XmlElement) n).GetElementsByTagName (XmlEncryption.ElementNames.EncryptionProperty, EncryptedXml.XmlEncNamespaceUrl))
122                                                         EncryptionProperties.Add (new EncryptionProperty (element));
123                                                 break;
124                                         }
125                                 }
126
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;
135                         }
136                 }
137
138                 #endregion // Methods
139         }
140 }
141
142 #endif