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