**** Merged from MCS ****
[mono-project.git] / mcs / class / System.Security / System.Security.Cryptography.Xml / EncryptedKey.cs
blob6059b6613942d17cc62904922ec51f01c903f66f
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
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.
31 #if NET_2_0
33 using System.Xml;
35 namespace System.Security.Cryptography.Xml {
36 public sealed class EncryptedKey : EncryptedType {
38 #region Fields
40 string carriedKeyName;
41 string recipient;
42 ReferenceList referenceList;
44 #endregion // Fields
46 #region Constructors
48 public EncryptedKey ()
49 : base ()
51 CarriedKeyName = null;
52 Recipient = null;
53 ReferenceList = new ReferenceList ();
56 #endregion // Constructors
58 #region Properties
60 public string CarriedKeyName {
61 get { return carriedKeyName; }
62 set { carriedKeyName = value; }
65 public string Recipient {
66 get { return recipient; }
67 set { recipient = value; }
70 public ReferenceList ReferenceList {
71 get { return referenceList; }
72 set { referenceList = value; }
75 #endregion // Properties
77 #region Methods
79 public void AddReference (DataReference dataReference)
81 ReferenceList.Add (dataReference);
84 public void AddReference (KeyReference keyReference)
86 ReferenceList.Add (keyReference);
89 public override XmlElement GetXml ()
91 return GetXml (new XmlDocument ());
94 internal XmlElement GetXml (XmlDocument document)
96 if (CipherData == null)
97 throw new CryptographicException ("Cipher data is not specified.");
99 XmlElement xel = document.CreateElement (XmlEncryption.ElementNames.EncryptedKey, EncryptedXml.XmlEncNamespaceUrl);
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));
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);
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);
122 if (CarriedKeyName != null) {
123 XmlElement xck = document.CreateElement (XmlEncryption.ElementNames.CarriedKeyName, EncryptedXml.XmlEncNamespaceUrl);
124 xck.InnerText = CarriedKeyName;
125 xel.AppendChild (xck);
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;
141 public override void LoadXml (XmlElement value)
143 if (value == null)
144 throw new ArgumentNullException ("value");
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;
162 foreach (XmlNode n in value.ChildNodes) {
163 if (n is XmlWhitespace)
164 continue;
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;
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;
201 break;
202 case XmlEncryption.ElementNames.CarriedKeyName:
203 CarriedKeyName = ((XmlElement) n).InnerText;
204 break;
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;
221 #endregion // Methods
225 #endif