2 // EncryptedKey.cs - EncryptedKey implementation for XML Encryption
3 // http://www.w3.org/2001/04/xmlenc#sec-EncryptedKey
6 // Tim Coleman (tim@timcoleman.com)
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
34 namespace System.Security.Cryptography.Xml {
35 public sealed class EncryptedKey : EncryptedType {
39 string carriedKeyName;
41 ReferenceList referenceList;
47 public EncryptedKey ()
49 referenceList = new ReferenceList ();
52 #endregion // Constructors
56 public string CarriedKeyName {
57 get { return carriedKeyName; }
58 set { carriedKeyName = value; }
61 public string Recipient {
62 get { return recipient; }
63 set { recipient = value; }
66 public ReferenceList ReferenceList {
67 get { return referenceList; }
70 #endregion // Properties
74 public void AddReference (DataReference dataReference)
76 ReferenceList.Add (dataReference);
79 public void AddReference (KeyReference keyReference)
81 ReferenceList.Add (keyReference);
84 public override XmlElement GetXml ()
86 return GetXml (new XmlDocument ());
89 internal XmlElement GetXml (XmlDocument document)
91 if (CipherData == null)
92 throw new CryptographicException ("Cipher data is not specified.");
94 XmlElement xel = document.CreateElement (XmlEncryption.ElementNames.EncryptedKey, EncryptedXml.XmlEncNamespaceUrl);
96 if (EncryptionMethod != null)
97 xel.AppendChild (EncryptionMethod.GetXml (document));
99 xel.AppendChild (document.ImportNode (KeyInfo.GetXml (), true));
100 if (CipherData != null)
101 xel.AppendChild (CipherData.GetXml (document));
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);
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);
117 if (CarriedKeyName != null) {
118 XmlElement xck = document.CreateElement (XmlEncryption.ElementNames.CarriedKeyName, EncryptedXml.XmlEncNamespaceUrl);
119 xck.InnerText = CarriedKeyName;
120 xel.AppendChild (xck);
124 xel.SetAttribute (XmlEncryption.AttributeNames.Id, Id);
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);
136 public override void LoadXml (XmlElement value)
139 throw new ArgumentNullException ("value");
141 if ((value.LocalName != XmlEncryption.ElementNames.EncryptedKey) || (value.NamespaceURI != EncryptedXml.XmlEncNamespaceUrl))
142 throw new CryptographicException ("Malformed EncryptedKey element.");
144 EncryptionMethod = null;
145 EncryptionMethod = null;
146 EncryptionProperties.Clear ();
147 ReferenceList.Clear ();
148 CarriedKeyName = null;
155 foreach (XmlNode n in value.ChildNodes) {
156 if (n is XmlWhitespace)
159 switch (n.LocalName) {
160 case XmlEncryption.ElementNames.EncryptionMethod:
161 EncryptionMethod = new EncryptionMethod ();
162 EncryptionMethod.LoadXml ((XmlElement) n);
164 case XmlSignature.ElementNames.KeyInfo:
165 KeyInfo = new KeyInfo ();
166 KeyInfo.LoadXml ((XmlElement) n);
168 case XmlEncryption.ElementNames.CipherData:
169 CipherData = new CipherData ();
170 CipherData.LoadXml ((XmlElement) n);
172 case XmlEncryption.ElementNames.EncryptionProperties:
173 foreach (XmlElement element in ((XmlElement) n).GetElementsByTagName (XmlEncryption.ElementNames.EncryptionProperty, EncryptedXml.XmlEncNamespaceUrl))
174 EncryptionProperties.Add (new EncryptionProperty (element));
176 case XmlEncryption.ElementNames.ReferenceList:
177 foreach (XmlNode r in ((XmlElement) n).ChildNodes) {
178 if (r is XmlWhitespace)
181 switch (r.LocalName) {
182 case XmlEncryption.ElementNames.DataReference:
183 DataReference dr = new DataReference ();
184 dr.LoadXml ((XmlElement) r);
187 case XmlEncryption.ElementNames.KeyReference:
188 KeyReference kr = new KeyReference ();
189 kr.LoadXml ((XmlElement) r);
195 case XmlEncryption.ElementNames.CarriedKeyName:
196 CarriedKeyName = ((XmlElement) n).InnerText;
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;
214 #endregion // Methods