This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[mono.git] / mcs / class / System.Security / System.Security.Cryptography.Xml / EncryptedKey.cs
1 //
2 // EncryptedKey.cs - EncryptedKey implementation for XML Encryption
3 // http://www.w3.org/2001/04/xmlenc#sec-EncryptedKey
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 EncryptedKey : EncryptedType {
37
38                 #region Fields
39
40                 string carriedKeyName;
41                 string recipient;
42                 ReferenceList referenceList;
43
44                 #endregion // Fields
45
46                 #region Constructors
47
48                 public EncryptedKey ()
49                         : base ()
50                 {
51                         CarriedKeyName = null;
52                         Recipient = null;
53                         ReferenceList = new ReferenceList ();
54                 }
55
56                 #endregion // Constructors
57
58                 #region Properties
59
60                 public string CarriedKeyName {
61                         get { return carriedKeyName; }
62                         set { carriedKeyName = value; }
63                 }
64
65                 public string Recipient {
66                         get { return recipient; }
67                         set { recipient = value; }
68                 }
69
70                 public ReferenceList ReferenceList {
71                         get { return referenceList; }
72                         set { referenceList = value; }
73                 }
74
75                 #endregion // Properties
76
77                 #region Methods
78
79                 public void AddReference (DataReference dataReference)
80                 {
81                         ReferenceList.Add (dataReference);
82                 }
83
84                 public void AddReference (KeyReference keyReference)
85                 {
86                         ReferenceList.Add (keyReference);
87                 }
88
89                 public override XmlElement GetXml ()
90                 {
91                         return GetXml (new XmlDocument ());
92                 }
93
94                 internal XmlElement GetXml (XmlDocument document)
95                 {
96                         if (CipherData == null)
97                                 throw new CryptographicException ("Cipher data is not specified.");
98
99                         XmlElement xel = document.CreateElement (XmlEncryption.ElementNames.EncryptedData, EncryptedXml.XmlEncNamespaceUrl);
100
101                         if (EncryptionMethod != null)
102                                 xel.AppendChild (EncryptionMethod.GetXml (document));
103                         if (KeyInfo != null) 
104                                 xel.AppendChild (document.ImportNode (KeyInfo.GetXml (), true));
105                         if (CipherData != null)
106                                 xel.AppendChild (CipherData.GetXml (document));
107
108                         if (EncryptionProperties.Count > 0) {
109                                 XmlElement xep = document.CreateElement (XmlEncryption.ElementNames.EncryptionProperties, EncryptedXml.XmlEncNamespaceUrl);
110                                 foreach (EncryptionProperty p in EncryptionProperties)
111                                         xep.AppendChild (p.GetXml (document));
112                                 xel.AppendChild (xep);
113                         }
114
115                         if (ReferenceList.Count > 0) {
116                                 XmlElement xrl = document.CreateElement (XmlEncryption.ElementNames.ReferenceList, EncryptedXml.XmlEncNamespaceUrl);
117                                 foreach (EncryptedReference er in ReferenceList) 
118                                         xrl.AppendChild (er.GetXml (document));
119                                 xel.AppendChild (xrl);
120                         }
121
122                         if (CarriedKeyName != null) {
123                                 XmlElement xck = document.CreateElement (XmlEncryption.ElementNames.CarriedKeyName, EncryptedXml.XmlEncNamespaceUrl);
124                                 xck.InnerText = CarriedKeyName;
125                                 xel.AppendChild (xck);
126                         }
127
128                         if (Id != null)
129                                 xel.SetAttribute (XmlEncryption.AttributeNames.Id, Id);
130                         if (Type != null)
131                                 xel.SetAttribute (XmlEncryption.AttributeNames.Type, Type);
132                         if (MimeType != null)
133                                 xel.SetAttribute (XmlEncryption.AttributeNames.MimeType, MimeType);
134                         if (Encoding != null)
135                                 xel.SetAttribute (XmlEncryption.AttributeNames.Encoding, Encoding);
136                         if (Recipient != null)
137                                 xel.SetAttribute (XmlEncryption.AttributeNames.Recipient, Recipient);
138                         return xel;
139                 }
140
141                 public override void LoadXml (XmlElement value)
142                 {
143                         if (value == null)
144                                 throw new ArgumentNullException ("value");
145
146                         if ((value.LocalName != XmlEncryption.ElementNames.EncryptedKey) || (value.NamespaceURI != EncryptedXml.XmlEncNamespaceUrl))
147                                 throw new CryptographicException ("Malformed EncryptedKey element.");
148                         else {
149                                 EncryptionMethod = null;
150                                 KeyInfo keyInfo = null;
151                                 CipherData cipherData = null;
152                                 EncryptionMethod = null;
153                                 EncryptionProperties = new EncryptionProperties ();
154                                 ReferenceList = new ReferenceList ();
155                                 CarriedKeyName = null;
156                                 Id = null;
157                                 Type = null;
158                                 MimeType = null;
159                                 Encoding = null;
160                                 Recipient = null;
161
162                                 foreach (XmlNode n in value.ChildNodes) {
163                                         if (n is XmlWhitespace)
164                                                 continue;
165
166                                         switch (n.LocalName) {
167                                         case XmlEncryption.ElementNames.EncryptionMethod:
168                                                 EncryptionMethod = new EncryptionMethod ();
169                                                 EncryptionMethod.LoadXml ((XmlElement) n);
170                                                 break;
171                                         case XmlSignature.ElementNames.KeyInfo:
172                                                 KeyInfo = new KeyInfo ();
173                                                 KeyInfo.LoadXml ((XmlElement) n);
174                                                 break;
175                                         case XmlEncryption.ElementNames.CipherData:
176                                                 CipherData = new CipherData ();
177                                                 CipherData.LoadXml ((XmlElement) n);
178                                                 break;
179                                         case XmlEncryption.ElementNames.EncryptionProperties:
180                                                 foreach (XmlElement element in ((XmlElement) n).GetElementsByTagName (XmlEncryption.ElementNames.EncryptionProperty, EncryptedXml.XmlEncNamespaceUrl))
181                                                         EncryptionProperties.Add (new EncryptionProperty (element));
182                                                 break;
183                                         case XmlEncryption.ElementNames.ReferenceList:
184                                                 foreach (XmlNode r in ((XmlElement) n).ChildNodes) {
185                                                         if (r is XmlWhitespace) 
186                                                                 continue;
187
188                                                         switch (r.LocalName) {
189                                                         case XmlEncryption.ElementNames.DataReference:
190                                                                 DataReference dr = new DataReference ();
191                                                                 dr.LoadXml ((XmlElement) r);
192                                                                 AddReference (dr);
193                                                                 break;
194                                                         case XmlEncryption.ElementNames.KeyReference:
195                                                                 KeyReference kr = new KeyReference ();
196                                                                 kr.LoadXml ((XmlElement) r);
197                                                                 AddReference (kr);
198                                                                 break;
199                                                         }
200                                                 }
201                                                 break;
202                                         case XmlEncryption.ElementNames.CarriedKeyName:
203                                                 CarriedKeyName = ((XmlElement) n).InnerText;
204                                                 break;
205                                         }
206                                 }
207
208                                 if (value.HasAttribute (XmlEncryption.AttributeNames.Id))
209                                         Id = value.Attributes [XmlEncryption.AttributeNames.Id].Value;
210                                 if (value.HasAttribute (XmlEncryption.AttributeNames.Type))
211                                         Type = value.Attributes [XmlEncryption.AttributeNames.Type].Value;
212                                 if (value.HasAttribute (XmlEncryption.AttributeNames.MimeType))
213                                         MimeType = value.Attributes [XmlEncryption.AttributeNames.MimeType].Value;
214                                 if (value.HasAttribute (XmlEncryption.AttributeNames.Encoding))
215                                         Encoding = value.Attributes [XmlEncryption.AttributeNames.Encoding].Value;
216                                 if (value.HasAttribute (XmlEncryption.AttributeNames.Recipient))
217                                         Encoding = value.Attributes [XmlEncryption.AttributeNames.Recipient].Value;
218                         }
219                 }
220
221                 #endregion // Methods
222         }
223 }
224
225 #endif