EncryptedData.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. //
  2. // EncryptedData.cs - EncryptedData implementation for XML Encryption
  3. // http://www.w3.org/2001/04/xmlenc#sec-EncryptedData
  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 EncryptedData : EncryptedType {
  13. #region Constructors
  14. public EncryptedData ()
  15. : base ()
  16. {
  17. }
  18. #endregion // Constructors
  19. #region Methods
  20. public override XmlElement GetXml ()
  21. {
  22. return GetXml (new XmlDocument ());
  23. }
  24. internal XmlElement GetXml (XmlDocument document)
  25. {
  26. if (CipherData == null)
  27. throw new CryptographicException ("Cipher data is not specified.");
  28. XmlElement xel = document.CreateElement (XmlEncryption.ElementNames.EncryptedData, XmlEncryption.NamespaceURI);
  29. if (EncryptionMethod != null)
  30. xel.AppendChild (EncryptionMethod.GetXml (document));
  31. if (KeyInfo != null)
  32. xel.AppendChild (document.ImportNode (KeyInfo.GetXml (), true));
  33. if (CipherData != null)
  34. xel.AppendChild (CipherData.GetXml (document));
  35. if (EncryptionProperties.Count > 0) {
  36. XmlElement xep = document.CreateElement (XmlEncryption.ElementNames.EncryptionProperties, XmlEncryption.NamespaceURI);
  37. foreach (EncryptionProperty p in EncryptionProperties)
  38. xep.AppendChild (p.GetXml (document));
  39. xel.AppendChild (xep);
  40. }
  41. if (Id != null)
  42. xel.SetAttribute (XmlEncryption.AttributeNames.Id, Id);
  43. if (Type != null)
  44. xel.SetAttribute (XmlEncryption.AttributeNames.Type, Type);
  45. if (MimeType != null)
  46. xel.SetAttribute (XmlEncryption.AttributeNames.MimeType, MimeType);
  47. if (Encoding != null)
  48. xel.SetAttribute (XmlEncryption.AttributeNames.Encoding, Encoding);
  49. return xel;
  50. }
  51. public override void LoadXml (XmlElement value)
  52. {
  53. if (value == null)
  54. throw new ArgumentNullException ("value");
  55. if ((value.LocalName != XmlEncryption.ElementNames.EncryptedData) || (value.NamespaceURI != XmlEncryption.NamespaceURI))
  56. throw new CryptographicException ("Malformed EncryptedData element.");
  57. else {
  58. EncryptionMethod = null;
  59. KeyInfo keyInfo = null;
  60. CipherData cipherData = null;
  61. EncryptionMethod = null;
  62. EncryptionProperties = new EncryptionProperties ();
  63. Id = null;
  64. Type = null;
  65. MimeType = null;
  66. Encoding = null;
  67. foreach (XmlNode n in value.ChildNodes) {
  68. if (n is XmlWhitespace)
  69. continue;
  70. switch (n.LocalName) {
  71. case XmlEncryption.ElementNames.EncryptionMethod:
  72. EncryptionMethod = new EncryptionMethod ();
  73. EncryptionMethod.LoadXml ((XmlElement) n);
  74. break;
  75. case XmlSignature.ElementNames.KeyInfo:
  76. KeyInfo = new KeyInfo ();
  77. KeyInfo.LoadXml ((XmlElement) n);
  78. break;
  79. case XmlEncryption.ElementNames.CipherData:
  80. CipherData = new CipherData ();
  81. CipherData.LoadXml ((XmlElement) n);
  82. break;
  83. case XmlEncryption.ElementNames.EncryptionProperties:
  84. foreach (XmlElement element in ((XmlElement) n).GetElementsByTagName (XmlEncryption.ElementNames.EncryptionProperty, XmlEncryption.NamespaceURI))
  85. EncryptionProperties.Add (new EncryptionProperty (element));
  86. break;
  87. }
  88. }
  89. if (value.HasAttribute (XmlEncryption.AttributeNames.Id))
  90. Id = value.Attributes [XmlEncryption.AttributeNames.Id].Value;
  91. if (value.HasAttribute (XmlEncryption.AttributeNames.Type))
  92. Type = value.Attributes [XmlEncryption.AttributeNames.Type].Value;
  93. if (value.HasAttribute (XmlEncryption.AttributeNames.MimeType))
  94. MimeType = value.Attributes [XmlEncryption.AttributeNames.MimeType].Value;
  95. if (value.HasAttribute (XmlEncryption.AttributeNames.Encoding))
  96. Encoding = value.Attributes [XmlEncryption.AttributeNames.Encoding].Value;
  97. }
  98. }
  99. #endregion // Methods
  100. }
  101. }
  102. #endif