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