EncryptedKey.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. {
  41. referenceList = new ReferenceList ();
  42. }
  43. #endregion // Constructors
  44. #region Properties
  45. public string CarriedKeyName {
  46. get { return carriedKeyName; }
  47. set { carriedKeyName = value; }
  48. }
  49. public string Recipient {
  50. get { return recipient; }
  51. set { recipient = value; }
  52. }
  53. public ReferenceList ReferenceList {
  54. get { return referenceList; }
  55. }
  56. #endregion // Properties
  57. #region Methods
  58. public void AddReference (DataReference dataReference)
  59. {
  60. ReferenceList.Add (dataReference);
  61. }
  62. public void AddReference (KeyReference keyReference)
  63. {
  64. ReferenceList.Add (keyReference);
  65. }
  66. public override XmlElement GetXml ()
  67. {
  68. return GetXml (new XmlDocument ());
  69. }
  70. internal XmlElement GetXml (XmlDocument document)
  71. {
  72. if (CipherData == null)
  73. throw new CryptographicException ("Cipher data is not specified.");
  74. XmlElement xel = document.CreateElement (XmlEncryption.ElementNames.EncryptedKey, EncryptedXml.XmlEncNamespaceUrl);
  75. if (EncryptionMethod != null)
  76. xel.AppendChild (EncryptionMethod.GetXml (document));
  77. if (KeyInfo != null)
  78. xel.AppendChild (document.ImportNode (KeyInfo.GetXml (), true));
  79. if (CipherData != null)
  80. xel.AppendChild (CipherData.GetXml (document));
  81. if (EncryptionProperties.Count > 0) {
  82. XmlElement xep = document.CreateElement (XmlEncryption.ElementNames.EncryptionProperties, EncryptedXml.XmlEncNamespaceUrl);
  83. foreach (EncryptionProperty p in EncryptionProperties)
  84. xep.AppendChild (p.GetXml (document));
  85. xel.AppendChild (xep);
  86. }
  87. if (ReferenceList.Count > 0) {
  88. XmlElement xrl = document.CreateElement (XmlEncryption.ElementNames.ReferenceList, EncryptedXml.XmlEncNamespaceUrl);
  89. foreach (EncryptedReference er in ReferenceList)
  90. xrl.AppendChild (er.GetXml (document));
  91. xel.AppendChild (xrl);
  92. }
  93. if (CarriedKeyName != null) {
  94. XmlElement xck = document.CreateElement (XmlEncryption.ElementNames.CarriedKeyName, EncryptedXml.XmlEncNamespaceUrl);
  95. xck.InnerText = CarriedKeyName;
  96. xel.AppendChild (xck);
  97. }
  98. if (Id != null)
  99. xel.SetAttribute (XmlEncryption.AttributeNames.Id, Id);
  100. if (Type != null)
  101. xel.SetAttribute (XmlEncryption.AttributeNames.Type, Type);
  102. if (MimeType != null)
  103. xel.SetAttribute (XmlEncryption.AttributeNames.MimeType, MimeType);
  104. if (Encoding != null)
  105. xel.SetAttribute (XmlEncryption.AttributeNames.Encoding, Encoding);
  106. if (Recipient != null)
  107. xel.SetAttribute (XmlEncryption.AttributeNames.Recipient, Recipient);
  108. return xel;
  109. }
  110. public override void LoadXml (XmlElement value)
  111. {
  112. if (value == null)
  113. throw new ArgumentNullException ("value");
  114. if ((value.LocalName != XmlEncryption.ElementNames.EncryptedKey) || (value.NamespaceURI != EncryptedXml.XmlEncNamespaceUrl))
  115. throw new CryptographicException ("Malformed EncryptedKey element.");
  116. else {
  117. EncryptionMethod = null;
  118. EncryptionMethod = null;
  119. EncryptionProperties.Clear ();
  120. ReferenceList.Clear ();
  121. CarriedKeyName = null;
  122. Id = null;
  123. Type = null;
  124. MimeType = null;
  125. Encoding = null;
  126. Recipient = null;
  127. foreach (XmlNode n in value.ChildNodes) {
  128. if (n is XmlWhitespace)
  129. continue;
  130. switch (n.LocalName) {
  131. case XmlEncryption.ElementNames.EncryptionMethod:
  132. EncryptionMethod = new EncryptionMethod ();
  133. EncryptionMethod.LoadXml ((XmlElement) n);
  134. break;
  135. case XmlSignature.ElementNames.KeyInfo:
  136. KeyInfo = new KeyInfo ();
  137. KeyInfo.LoadXml ((XmlElement) n);
  138. break;
  139. case XmlEncryption.ElementNames.CipherData:
  140. CipherData = new CipherData ();
  141. CipherData.LoadXml ((XmlElement) n);
  142. break;
  143. case XmlEncryption.ElementNames.EncryptionProperties:
  144. foreach (XmlElement element in ((XmlElement) n).GetElementsByTagName (XmlEncryption.ElementNames.EncryptionProperty, EncryptedXml.XmlEncNamespaceUrl))
  145. EncryptionProperties.Add (new EncryptionProperty (element));
  146. break;
  147. case XmlEncryption.ElementNames.ReferenceList:
  148. foreach (XmlNode r in ((XmlElement) n).ChildNodes) {
  149. if (r is XmlWhitespace)
  150. continue;
  151. switch (r.LocalName) {
  152. case XmlEncryption.ElementNames.DataReference:
  153. DataReference dr = new DataReference ();
  154. dr.LoadXml ((XmlElement) r);
  155. AddReference (dr);
  156. break;
  157. case XmlEncryption.ElementNames.KeyReference:
  158. KeyReference kr = new KeyReference ();
  159. kr.LoadXml ((XmlElement) r);
  160. AddReference (kr);
  161. break;
  162. }
  163. }
  164. break;
  165. case XmlEncryption.ElementNames.CarriedKeyName:
  166. CarriedKeyName = ((XmlElement) n).InnerText;
  167. break;
  168. }
  169. }
  170. if (value.HasAttribute (XmlEncryption.AttributeNames.Id))
  171. Id = value.Attributes [XmlEncryption.AttributeNames.Id].Value;
  172. if (value.HasAttribute (XmlEncryption.AttributeNames.Type))
  173. Type = value.Attributes [XmlEncryption.AttributeNames.Type].Value;
  174. if (value.HasAttribute (XmlEncryption.AttributeNames.MimeType))
  175. MimeType = value.Attributes [XmlEncryption.AttributeNames.MimeType].Value;
  176. if (value.HasAttribute (XmlEncryption.AttributeNames.Encoding))
  177. Encoding = value.Attributes [XmlEncryption.AttributeNames.Encoding].Value;
  178. if (value.HasAttribute (XmlEncryption.AttributeNames.Recipient))
  179. Encoding = value.Attributes [XmlEncryption.AttributeNames.Recipient].Value;
  180. }
  181. }
  182. #endregion // Methods
  183. }
  184. }
  185. #endif