EncryptedKey.cs 6.9 KB

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