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
14 namespace System.Security.Cryptography.Xml {
15 public sealed class EncryptedKey : EncryptedType {
19 string carriedKeyName;
21 ReferenceList referenceList;
27 public EncryptedKey ()
30 CarriedKeyName = null;
32 ReferenceList = new ReferenceList ();
35 #endregion // Constructors
39 public string CarriedKeyName {
40 get { return carriedKeyName; }
41 set { carriedKeyName = value; }
44 public string Recipient {
45 get { return recipient; }
46 set { recipient = value; }
49 public ReferenceList ReferenceList {
50 get { return referenceList; }
51 set { referenceList = value; }
54 #endregion // Properties
58 public void AddReference (DataReference dataReference)
60 ReferenceList.Add (dataReference);
63 public void AddReference (KeyReference keyReference)
65 ReferenceList.Add (keyReference);
68 public override XmlElement GetXml ()
70 return GetXml (new XmlDocument ());
73 internal XmlElement GetXml (XmlDocument document)
75 if (CipherData == null)
76 throw new CryptographicException ("Cipher data is not specified.");
78 XmlElement xel = document.CreateElement (XmlEncryption.ElementNames.EncryptedData, EncryptedXml.XmlEncNamespaceUrl);
80 if (EncryptionMethod != null)
81 xel.AppendChild (EncryptionMethod.GetXml (document));
83 xel.AppendChild (document.ImportNode (KeyInfo.GetXml (), true));
84 if (CipherData != null)
85 xel.AppendChild (CipherData.GetXml (document));
87 if (EncryptionProperties.Count > 0) {
88 XmlElement xep = document.CreateElement (XmlEncryption.ElementNames.EncryptionProperties, EncryptedXml.XmlEncNamespaceUrl);
89 foreach (EncryptionProperty p in EncryptionProperties)
90 xep.AppendChild (p.GetXml (document));
91 xel.AppendChild (xep);
94 if (ReferenceList.Count > 0) {
95 XmlElement xrl = document.CreateElement (XmlEncryption.ElementNames.ReferenceList, EncryptedXml.XmlEncNamespaceUrl);
96 foreach (EncryptedReference er in ReferenceList)
97 xrl.AppendChild (er.GetXml (document));
98 xel.AppendChild (xrl);
101 if (CarriedKeyName != null) {
102 XmlElement xck = document.CreateElement (XmlEncryption.ElementNames.CarriedKeyName, EncryptedXml.XmlEncNamespaceUrl);
103 xck.InnerText = CarriedKeyName;
104 xel.AppendChild (xck);
108 xel.SetAttribute (XmlEncryption.AttributeNames.Id, Id);
110 xel.SetAttribute (XmlEncryption.AttributeNames.Type, Type);
111 if (MimeType != null)
112 xel.SetAttribute (XmlEncryption.AttributeNames.MimeType, MimeType);
113 if (Encoding != null)
114 xel.SetAttribute (XmlEncryption.AttributeNames.Encoding, Encoding);
115 if (Recipient != null)
116 xel.SetAttribute (XmlEncryption.AttributeNames.Recipient, Recipient);
120 public override void LoadXml (XmlElement value)
123 throw new ArgumentNullException ("value");
125 if ((value.LocalName != XmlEncryption.ElementNames.EncryptedKey) || (value.NamespaceURI != EncryptedXml.XmlEncNamespaceUrl))
126 throw new CryptographicException ("Malformed EncryptedKey element.");
128 EncryptionMethod = null;
129 KeyInfo keyInfo = null;
130 CipherData cipherData = null;
131 EncryptionMethod = null;
132 EncryptionProperties = new EncryptionProperties ();
133 ReferenceList = new ReferenceList ();
134 CarriedKeyName = null;
141 foreach (XmlNode n in value.ChildNodes) {
142 if (n is XmlWhitespace)
145 switch (n.LocalName) {
146 case XmlEncryption.ElementNames.EncryptionMethod:
147 EncryptionMethod = new EncryptionMethod ();
148 EncryptionMethod.LoadXml ((XmlElement) n);
150 case XmlSignature.ElementNames.KeyInfo:
151 KeyInfo = new KeyInfo ();
152 KeyInfo.LoadXml ((XmlElement) n);
154 case XmlEncryption.ElementNames.CipherData:
155 CipherData = new CipherData ();
156 CipherData.LoadXml ((XmlElement) n);
158 case XmlEncryption.ElementNames.EncryptionProperties:
159 foreach (XmlElement element in ((XmlElement) n).GetElementsByTagName (XmlEncryption.ElementNames.EncryptionProperty, EncryptedXml.XmlEncNamespaceUrl))
160 EncryptionProperties.Add (new EncryptionProperty (element));
162 case XmlEncryption.ElementNames.ReferenceList:
163 foreach (XmlNode r in ((XmlElement) n).ChildNodes) {
164 if (r is XmlWhitespace)
167 switch (r.LocalName) {
168 case XmlEncryption.ElementNames.DataReference:
169 DataReference dr = new DataReference ();
170 dr.LoadXml ((XmlElement) r);
173 case XmlEncryption.ElementNames.KeyReference:
174 KeyReference kr = new KeyReference ();
175 kr.LoadXml ((XmlElement) r);
181 case XmlEncryption.ElementNames.CarriedKeyName:
182 CarriedKeyName = ((XmlElement) n).InnerText;
187 if (value.HasAttribute (XmlEncryption.AttributeNames.Id))
188 Id = value.Attributes [XmlEncryption.AttributeNames.Id].Value;
189 if (value.HasAttribute (XmlEncryption.AttributeNames.Type))
190 Type = value.Attributes [XmlEncryption.AttributeNames.Type].Value;
191 if (value.HasAttribute (XmlEncryption.AttributeNames.MimeType))
192 MimeType = value.Attributes [XmlEncryption.AttributeNames.MimeType].Value;
193 if (value.HasAttribute (XmlEncryption.AttributeNames.Encoding))
194 Encoding = value.Attributes [XmlEncryption.AttributeNames.Encoding].Value;
195 if (value.HasAttribute (XmlEncryption.AttributeNames.Recipient))
196 Encoding = value.Attributes [XmlEncryption.AttributeNames.Recipient].Value;
200 #endregion // Methods