EncryptedHeaderXml.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. namespace System.ServiceModel.Security
  5. {
  6. using System.IO;
  7. using System.ServiceModel.Channels;
  8. using System.ServiceModel;
  9. using System.IdentityModel.Tokens;
  10. using System.IdentityModel.Selectors;
  11. using System.Security.Cryptography;
  12. using System.Xml;
  13. using DictionaryManager = System.IdentityModel.DictionaryManager;
  14. using ISecurityElement = System.IdentityModel.ISecurityElement;
  15. sealed class EncryptedHeaderXml
  16. {
  17. internal static readonly XmlDictionaryString ElementName = XD.SecurityXXX2005Dictionary.EncryptedHeader;
  18. internal static readonly XmlDictionaryString NamespaceUri = XD.SecurityXXX2005Dictionary.Namespace;
  19. const string Prefix = SecurityXXX2005Strings.Prefix;
  20. string id;
  21. bool mustUnderstand;
  22. bool relay;
  23. string actor;
  24. MessageVersion version;
  25. EncryptedData encryptedData;
  26. public EncryptedHeaderXml(MessageVersion version, bool shouldReadXmlReferenceKeyInfoClause)
  27. {
  28. this.version = version;
  29. encryptedData = new EncryptedData();
  30. // This is for the case when the service send an EncryptedHeader to the client where the KeyInfo clause contains referenceXml clause.
  31. encryptedData.ShouldReadXmlReferenceKeyInfoClause = shouldReadXmlReferenceKeyInfoClause;
  32. }
  33. public string Actor
  34. {
  35. get
  36. {
  37. return this.actor;
  38. }
  39. set
  40. {
  41. this.actor = value;
  42. }
  43. }
  44. public string EncryptionMethod
  45. {
  46. get
  47. {
  48. return encryptedData.EncryptionMethod;
  49. }
  50. set
  51. {
  52. encryptedData.EncryptionMethod = value;
  53. }
  54. }
  55. public XmlDictionaryString EncryptionMethodDictionaryString
  56. {
  57. get
  58. {
  59. return encryptedData.EncryptionMethodDictionaryString;
  60. }
  61. set
  62. {
  63. encryptedData.EncryptionMethodDictionaryString = value;
  64. }
  65. }
  66. public bool HasId
  67. {
  68. get
  69. {
  70. return true;
  71. }
  72. }
  73. public string Id
  74. {
  75. get
  76. {
  77. return id;
  78. }
  79. set
  80. {
  81. id = value;
  82. }
  83. }
  84. public SecurityKeyIdentifier KeyIdentifier
  85. {
  86. get
  87. {
  88. return encryptedData.KeyIdentifier;
  89. }
  90. set
  91. {
  92. encryptedData.KeyIdentifier = value;
  93. }
  94. }
  95. public bool MustUnderstand
  96. {
  97. get
  98. {
  99. return this.mustUnderstand;
  100. }
  101. set
  102. {
  103. this.mustUnderstand = value;
  104. }
  105. }
  106. public bool Relay
  107. {
  108. get
  109. {
  110. return this.relay;
  111. }
  112. set
  113. {
  114. this.relay = value;
  115. }
  116. }
  117. public SecurityTokenSerializer SecurityTokenSerializer
  118. {
  119. get
  120. {
  121. return encryptedData.SecurityTokenSerializer;
  122. }
  123. set
  124. {
  125. encryptedData.SecurityTokenSerializer = value;
  126. }
  127. }
  128. public byte[] GetDecryptedBuffer()
  129. {
  130. return encryptedData.GetDecryptedBuffer();
  131. }
  132. public void ReadFrom(XmlDictionaryReader reader, long maxBufferSize)
  133. {
  134. reader.MoveToStartElement(ElementName, NamespaceUri);
  135. bool isReferenceParameter;
  136. MessageHeader.GetHeaderAttributes(reader, version, out this.actor, out this.mustUnderstand, out this.relay, out isReferenceParameter);
  137. this.id = reader.GetAttribute(XD.UtilityDictionary.IdAttribute, XD.UtilityDictionary.Namespace);
  138. reader.ReadStartElement();
  139. encryptedData.ReadFrom(reader, maxBufferSize);
  140. reader.ReadEndElement();
  141. }
  142. public void SetUpDecryption(SymmetricAlgorithm algorithm)
  143. {
  144. encryptedData.SetUpDecryption(algorithm);
  145. }
  146. public void SetUpEncryption(SymmetricAlgorithm algorithm, MemoryStream source)
  147. {
  148. encryptedData.SetUpEncryption(algorithm, new ArraySegment<byte>(source.GetBuffer(), 0, (int) source.Length));
  149. }
  150. public void WriteHeaderElement(XmlDictionaryWriter writer)
  151. {
  152. writer.WriteStartElement(Prefix, ElementName, NamespaceUri);
  153. }
  154. public void WriteHeaderId(XmlDictionaryWriter writer)
  155. {
  156. writer.WriteAttributeString(XD.UtilityDictionary.Prefix.Value, XD.UtilityDictionary.IdAttribute, XD.UtilityDictionary.Namespace, id);
  157. }
  158. public void WriteHeaderContents(XmlDictionaryWriter writer)
  159. {
  160. this.encryptedData.WriteTo(writer, ServiceModelDictionaryManager.Instance);
  161. }
  162. }
  163. }