EncryptedKey.cs 6.0 KB

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