This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[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.Xml;
34
35 namespace System.Security.Cryptography.Xml {
36         public sealed class EncryptedData : EncryptedType {
37
38                 #region Constructors
39
40                 public EncryptedData ()
41                         : base ()
42                 {
43                 }
44
45                 #endregion // Constructors
46
47                 #region Methods
48
49                 public override XmlElement GetXml ()
50                 {
51                         return GetXml (new XmlDocument ());
52                 }
53
54                 internal XmlElement GetXml (XmlDocument document)
55                 {
56                         if (CipherData == null)
57                                 throw new CryptographicException ("Cipher data is not specified.");
58
59                         XmlElement xel = document.CreateElement (XmlEncryption.ElementNames.EncryptedData, EncryptedXml.XmlEncNamespaceUrl);
60
61                         if (EncryptionMethod != null)
62                                 xel.AppendChild (EncryptionMethod.GetXml (document));
63                         if (KeyInfo != null) 
64                                 xel.AppendChild (document.ImportNode (KeyInfo.GetXml (), true));
65                         if (CipherData != null)
66                                 xel.AppendChild (CipherData.GetXml (document));
67
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);
73                         }
74
75                         if (Id != null)
76                                 xel.SetAttribute (XmlEncryption.AttributeNames.Id, Id);
77                         if (Type != null)
78                                 xel.SetAttribute (XmlEncryption.AttributeNames.Type, Type);
79                         if (MimeType != null)
80                                 xel.SetAttribute (XmlEncryption.AttributeNames.MimeType, MimeType);
81                         if (Encoding != null)
82                                 xel.SetAttribute (XmlEncryption.AttributeNames.Encoding, Encoding);
83                         return xel;
84                 }
85
86                 public override void LoadXml (XmlElement value)
87                 {
88                         if (value == null)
89                                 throw new ArgumentNullException ("value");
90
91                         if ((value.LocalName != XmlEncryption.ElementNames.EncryptedData) || (value.NamespaceURI != EncryptedXml.XmlEncNamespaceUrl))
92                                 throw new CryptographicException ("Malformed EncryptedData element.");
93                         else {
94                                 EncryptionMethod = null;
95                                 KeyInfo keyInfo = null;
96                                 CipherData cipherData = null;
97                                 EncryptionMethod = null;
98                                 EncryptionProperties = new EncryptionProperties ();
99                                 Id = null;
100                                 Type = null;
101                                 MimeType = null;
102                                 Encoding = null;
103
104                                 foreach (XmlNode n in value.ChildNodes) {
105                                         if (n is XmlWhitespace)
106                                                 continue;
107
108                                         switch (n.LocalName) {
109                                         case XmlEncryption.ElementNames.EncryptionMethod:
110                                                 EncryptionMethod = new EncryptionMethod ();
111                                                 EncryptionMethod.LoadXml ((XmlElement) n);
112                                                 break;
113                                         case XmlSignature.ElementNames.KeyInfo:
114                                                 KeyInfo = new KeyInfo ();
115                                                 KeyInfo.LoadXml ((XmlElement) n);
116                                                 break;
117                                         case XmlEncryption.ElementNames.CipherData:
118                                                 CipherData = new CipherData ();
119                                                 CipherData.LoadXml ((XmlElement) n);
120                                                 break;
121                                         case XmlEncryption.ElementNames.EncryptionProperties:
122                                                 foreach (XmlElement element in ((XmlElement) n).GetElementsByTagName (XmlEncryption.ElementNames.EncryptionProperty, EncryptedXml.XmlEncNamespaceUrl))
123                                                         EncryptionProperties.Add (new EncryptionProperty (element));
124                                                 break;
125                                         }
126                                 }
127
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;
136                         }
137                 }
138
139                 #endregion // Methods
140         }
141 }
142
143 #endif