EncryptedKey.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 ([email protected])
  7. //
  8. // Copyright (C) Tim Coleman, 2004
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. #if NET_2_0
  30. using System.Xml;
  31. namespace System.Security.Cryptography.Xml {
  32. public sealed class EncryptedKey : EncryptedType {
  33. #region Fields
  34. string carriedKeyName;
  35. string recipient;
  36. ReferenceList referenceList;
  37. #endregion // Fields
  38. #region Constructors
  39. public EncryptedKey ()
  40. : base ()
  41. {
  42. CarriedKeyName = null;
  43. Recipient = null;
  44. ReferenceList = new ReferenceList ();
  45. }
  46. #endregion // Constructors
  47. #region Properties
  48. public string CarriedKeyName {
  49. get { return carriedKeyName; }
  50. set { carriedKeyName = value; }
  51. }
  52. public string Recipient {
  53. get { return recipient; }
  54. set { recipient = value; }
  55. }
  56. public ReferenceList ReferenceList {
  57. get { return referenceList; }
  58. set { referenceList = value; }
  59. }
  60. #endregion // Properties
  61. #region Methods
  62. public void AddReference (DataReference dataReference)
  63. {
  64. ReferenceList.Add (dataReference);
  65. }
  66. public void AddReference (KeyReference keyReference)
  67. {
  68. ReferenceList.Add (keyReference);
  69. }
  70. public override XmlElement GetXml ()
  71. {
  72. return GetXml (new XmlDocument ());
  73. }
  74. internal XmlElement GetXml (XmlDocument document)
  75. {
  76. if (CipherData == null)
  77. throw new CryptographicException ("Cipher data is not specified.");
  78. XmlElement xel = document.CreateElement (XmlEncryption.ElementNames.EncryptedData, EncryptedXml.XmlEncNamespaceUrl);
  79. if (EncryptionMethod != null)
  80. xel.AppendChild (EncryptionMethod.GetXml (document));
  81. if (KeyInfo != null)
  82. xel.AppendChild (document.ImportNode (KeyInfo.GetXml (), true));
  83. if (CipherData != null)
  84. xel.AppendChild (CipherData.GetXml (document));
  85. if (EncryptionProperties.Count > 0) {
  86. XmlElement xep = document.CreateElement (XmlEncryption.ElementNames.EncryptionProperties, EncryptedXml.XmlEncNamespaceUrl);
  87. foreach (EncryptionProperty p in EncryptionProperties)
  88. xep.AppendChild (p.GetXml (document));
  89. xel.AppendChild (xep);
  90. }
  91. if (ReferenceList.Count > 0) {
  92. XmlElement xrl = document.CreateElement (XmlEncryption.ElementNames.ReferenceList, EncryptedXml.XmlEncNamespaceUrl);
  93. foreach (EncryptedReference er in ReferenceList)
  94. xrl.AppendChild (er.GetXml (document));
  95. xel.AppendChild (xrl);
  96. }
  97. if (CarriedKeyName != null) {
  98. XmlElement xck = document.CreateElement (XmlEncryption.ElementNames.CarriedKeyName, EncryptedXml.XmlEncNamespaceUrl);
  99. xck.InnerText = CarriedKeyName;
  100. xel.AppendChild (xck);
  101. }
  102. if (Id != null)
  103. xel.SetAttribute (XmlEncryption.AttributeNames.Id, Id);
  104. if (Type != null)
  105. xel.SetAttribute (XmlEncryption.AttributeNames.Type, Type);
  106. if (MimeType != null)
  107. xel.SetAttribute (XmlEncryption.AttributeNames.MimeType, MimeType);
  108. if (Encoding != null)
  109. xel.SetAttribute (XmlEncryption.AttributeNames.Encoding, Encoding);
  110. if (Recipient != null)
  111. xel.SetAttribute (XmlEncryption.AttributeNames.Recipient, Recipient);
  112. return xel;
  113. }
  114. public override void LoadXml (XmlElement value)
  115. {
  116. if (value == null)
  117. throw new ArgumentNullException ("value");
  118. if ((value.LocalName != XmlEncryption.ElementNames.EncryptedKey) || (value.NamespaceURI != EncryptedXml.XmlEncNamespaceUrl))
  119. throw new CryptographicException ("Malformed EncryptedKey element.");
  120. else {
  121. EncryptionMethod = null;
  122. KeyInfo keyInfo = null;
  123. CipherData cipherData = null;
  124. EncryptionMethod = null;
  125. EncryptionProperties = new EncryptionProperties ();
  126. ReferenceList = new ReferenceList ();
  127. CarriedKeyName = null;
  128. Id = null;
  129. Type = null;
  130. MimeType = null;
  131. Encoding = null;
  132. Recipient = null;
  133. foreach (XmlNode n in value.ChildNodes) {
  134. if (n is XmlWhitespace)
  135. continue;
  136. switch (n.LocalName) {
  137. case XmlEncryption.ElementNames.EncryptionMethod:
  138. EncryptionMethod = new EncryptionMethod ();
  139. EncryptionMethod.LoadXml ((XmlElement) n);
  140. break;
  141. case XmlSignature.ElementNames.KeyInfo:
  142. KeyInfo = new KeyInfo ();
  143. KeyInfo.LoadXml ((XmlElement) n);
  144. break;
  145. case XmlEncryption.ElementNames.CipherData:
  146. CipherData = new CipherData ();
  147. CipherData.LoadXml ((XmlElement) n);
  148. break;
  149. case XmlEncryption.ElementNames.EncryptionProperties:
  150. foreach (XmlElement element in ((XmlElement) n).GetElementsByTagName (XmlEncryption.ElementNames.EncryptionProperty, EncryptedXml.XmlEncNamespaceUrl))
  151. EncryptionProperties.Add (new EncryptionProperty (element));
  152. break;
  153. case XmlEncryption.ElementNames.ReferenceList:
  154. foreach (XmlNode r in ((XmlElement) n).ChildNodes) {
  155. if (r is XmlWhitespace)
  156. continue;
  157. switch (r.LocalName) {
  158. case XmlEncryption.ElementNames.DataReference:
  159. DataReference dr = new DataReference ();
  160. dr.LoadXml ((XmlElement) r);
  161. AddReference (dr);
  162. break;
  163. case XmlEncryption.ElementNames.KeyReference:
  164. KeyReference kr = new KeyReference ();
  165. kr.LoadXml ((XmlElement) r);
  166. AddReference (kr);
  167. break;
  168. }
  169. }
  170. break;
  171. case XmlEncryption.ElementNames.CarriedKeyName:
  172. CarriedKeyName = ((XmlElement) n).InnerText;
  173. break;
  174. }
  175. }
  176. if (value.HasAttribute (XmlEncryption.AttributeNames.Id))
  177. Id = value.Attributes [XmlEncryption.AttributeNames.Id].Value;
  178. if (value.HasAttribute (XmlEncryption.AttributeNames.Type))
  179. Type = value.Attributes [XmlEncryption.AttributeNames.Type].Value;
  180. if (value.HasAttribute (XmlEncryption.AttributeNames.MimeType))
  181. MimeType = value.Attributes [XmlEncryption.AttributeNames.MimeType].Value;
  182. if (value.HasAttribute (XmlEncryption.AttributeNames.Encoding))
  183. Encoding = value.Attributes [XmlEncryption.AttributeNames.Encoding].Value;
  184. if (value.HasAttribute (XmlEncryption.AttributeNames.Recipient))
  185. Encoding = value.Attributes [XmlEncryption.AttributeNames.Recipient].Value;
  186. }
  187. }
  188. #endregion // Methods
  189. }
  190. }
  191. #endif