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.
35 namespace System
.Security
.Cryptography
.Xml
{
36 public sealed class EncryptedKey
: EncryptedType
{
40 string carriedKeyName
;
42 ReferenceList referenceList
;
48 public EncryptedKey ()
50 referenceList
= new ReferenceList ();
53 #endregion // Constructors
57 public string CarriedKeyName
{
58 get { return carriedKeyName; }
59 set { carriedKeyName = value; }
62 public string Recipient
{
63 get { return recipient; }
64 set { recipient = value; }
67 public ReferenceList ReferenceList
{
68 get { return referenceList; }
71 #endregion // Properties
75 public void AddReference (DataReference dataReference
)
77 ReferenceList
.Add (dataReference
);
80 public void AddReference (KeyReference keyReference
)
82 ReferenceList
.Add (keyReference
);
85 public override XmlElement
GetXml ()
87 return GetXml (new XmlDocument ());
90 internal XmlElement
GetXml (XmlDocument document
)
92 if (CipherData
== null)
93 throw new CryptographicException ("Cipher data is not specified.");
95 XmlElement xel
= document
.CreateElement (XmlEncryption
.ElementNames
.EncryptedKey
, EncryptedXml
.XmlEncNamespaceUrl
);
97 if (EncryptionMethod
!= null)
98 xel
.AppendChild (EncryptionMethod
.GetXml (document
));
100 xel
.AppendChild (document
.ImportNode (KeyInfo
.GetXml (), true));
101 if (CipherData
!= null)
102 xel
.AppendChild (CipherData
.GetXml (document
));
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
);
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
);
118 if (CarriedKeyName
!= null) {
119 XmlElement xck
= document
.CreateElement (XmlEncryption
.ElementNames
.CarriedKeyName
, EncryptedXml
.XmlEncNamespaceUrl
);
120 xck
.InnerText
= CarriedKeyName
;
121 xel
.AppendChild (xck
);
125 xel
.SetAttribute (XmlEncryption
.AttributeNames
.Id
, Id
);
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
);
137 public override void LoadXml (XmlElement
value)
140 throw new ArgumentNullException ("value");
142 if ((value.LocalName
!= XmlEncryption
.ElementNames
.EncryptedKey
) || (value.NamespaceURI
!= EncryptedXml
.XmlEncNamespaceUrl
))
143 throw new CryptographicException ("Malformed EncryptedKey element.");
145 EncryptionMethod
= null;
146 EncryptionMethod
= null;
147 EncryptionProperties
.Clear ();
148 ReferenceList
.Clear ();
149 CarriedKeyName
= null;
156 foreach (XmlNode n
in value.ChildNodes
) {
157 if (n
is XmlWhitespace
)
160 switch (n
.LocalName
) {
161 case XmlEncryption
.ElementNames
.EncryptionMethod
:
162 EncryptionMethod
= new EncryptionMethod ();
163 EncryptionMethod
.LoadXml ((XmlElement
) n
);
165 case XmlSignature
.ElementNames
.KeyInfo
:
166 KeyInfo
= new KeyInfo ();
167 KeyInfo
.LoadXml ((XmlElement
) n
);
169 case XmlEncryption
.ElementNames
.CipherData
:
170 CipherData
= new CipherData ();
171 CipherData
.LoadXml ((XmlElement
) n
);
173 case XmlEncryption
.ElementNames
.EncryptionProperties
:
174 foreach (XmlElement element
in ((XmlElement
) n
).GetElementsByTagName (XmlEncryption
.ElementNames
.EncryptionProperty
, EncryptedXml
.XmlEncNamespaceUrl
))
175 EncryptionProperties
.Add (new EncryptionProperty (element
));
177 case XmlEncryption
.ElementNames
.ReferenceList
:
178 foreach (XmlNode r
in ((XmlElement
) n
).ChildNodes
) {
179 if (r
is XmlWhitespace
)
182 switch (r
.LocalName
) {
183 case XmlEncryption
.ElementNames
.DataReference
:
184 DataReference dr
= new DataReference ();
185 dr
.LoadXml ((XmlElement
) r
);
188 case XmlEncryption
.ElementNames
.KeyReference
:
189 KeyReference kr
= new KeyReference ();
190 kr
.LoadXml ((XmlElement
) r
);
196 case XmlEncryption
.ElementNames
.CarriedKeyName
:
197 CarriedKeyName
= ((XmlElement
) n
).InnerText
;
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
;
215 #endregion // Methods