SecurityStandardsManager.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. namespace System.ServiceModel.Security
  5. {
  6. using System.Collections.Generic;
  7. using System.ServiceModel.Channels;
  8. using System.ServiceModel;
  9. using System.ServiceModel.Description;
  10. using System.ServiceModel.Security.Tokens;
  11. using System.Collections.ObjectModel;
  12. using System.IdentityModel.Policy;
  13. using System.IdentityModel.Selectors;
  14. using System.IdentityModel.Tokens;
  15. using System.Xml;
  16. using System.Runtime.CompilerServices;
  17. class SecurityStandardsManager
  18. {
  19. static SecurityStandardsManager instance;
  20. readonly SecureConversationDriver secureConversationDriver;
  21. readonly TrustDriver trustDriver;
  22. readonly SignatureTargetIdManager idManager;
  23. readonly MessageSecurityVersion messageSecurityVersion;
  24. readonly WSUtilitySpecificationVersion wsUtilitySpecificationVersion;
  25. readonly SecurityTokenSerializer tokenSerializer;
  26. WSSecurityTokenSerializer wsSecurityTokenSerializer;
  27. [MethodImpl(MethodImplOptions.NoInlining)]
  28. public SecurityStandardsManager()
  29. : this(WSSecurityTokenSerializer.DefaultInstance)
  30. {
  31. }
  32. public SecurityStandardsManager(SecurityTokenSerializer tokenSerializer)
  33. : this(MessageSecurityVersion.WSSecurity11WSTrustFebruary2005WSSecureConversationFebruary2005WSSecurityPolicy11, tokenSerializer)
  34. {
  35. }
  36. public SecurityStandardsManager(MessageSecurityVersion messageSecurityVersion, SecurityTokenSerializer tokenSerializer)
  37. {
  38. if (messageSecurityVersion == null)
  39. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("messageSecurityVersion"));
  40. if (tokenSerializer == null)
  41. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("tokenSerializer");
  42. this.messageSecurityVersion = messageSecurityVersion;
  43. this.tokenSerializer = tokenSerializer;
  44. if (messageSecurityVersion.SecureConversationVersion == SecureConversationVersion.WSSecureConversation13)
  45. this.secureConversationDriver = new WSSecureConversationDec2005.DriverDec2005();
  46. else
  47. this.secureConversationDriver = new WSSecureConversationFeb2005.DriverFeb2005();
  48. if (this.SecurityVersion == SecurityVersion.WSSecurity10 || this.SecurityVersion == SecurityVersion.WSSecurity11)
  49. {
  50. this.idManager = WSSecurityJan2004.IdManager.Instance;
  51. }
  52. else
  53. {
  54. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("messageSecurityVersion", SR.GetString(SR.MessageSecurityVersionOutOfRange)));
  55. }
  56. this.wsUtilitySpecificationVersion = WSUtilitySpecificationVersion.Default;
  57. if (messageSecurityVersion.MessageSecurityTokenVersion.TrustVersion == TrustVersion.WSTrust13)
  58. this.trustDriver = new WSTrustDec2005.DriverDec2005(this);
  59. else
  60. this.trustDriver = new WSTrustFeb2005.DriverFeb2005(this);
  61. }
  62. public static SecurityStandardsManager DefaultInstance
  63. {
  64. get
  65. {
  66. if (instance == null)
  67. instance = new SecurityStandardsManager();
  68. return instance;
  69. }
  70. }
  71. public SecurityVersion SecurityVersion
  72. {
  73. get { return this.messageSecurityVersion == null ? null : this.messageSecurityVersion.SecurityVersion; }
  74. }
  75. public MessageSecurityVersion MessageSecurityVersion
  76. {
  77. get { return this.messageSecurityVersion; }
  78. }
  79. public TrustVersion TrustVersion
  80. {
  81. get { return this.messageSecurityVersion.TrustVersion; }
  82. }
  83. public SecureConversationVersion SecureConversationVersion
  84. {
  85. get { return this.messageSecurityVersion.SecureConversationVersion; }
  86. }
  87. internal SecurityTokenSerializer SecurityTokenSerializer
  88. {
  89. get { return this.tokenSerializer; }
  90. }
  91. internal WSUtilitySpecificationVersion WSUtilitySpecificationVersion
  92. {
  93. get { return this.wsUtilitySpecificationVersion; }
  94. }
  95. internal SignatureTargetIdManager IdManager
  96. {
  97. get { return this.idManager; }
  98. }
  99. internal SecureConversationDriver SecureConversationDriver
  100. {
  101. get { return this.secureConversationDriver; }
  102. }
  103. internal TrustDriver TrustDriver
  104. {
  105. get { return this.trustDriver; }
  106. }
  107. WSSecurityTokenSerializer WSSecurityTokenSerializer
  108. {
  109. get
  110. {
  111. if (this.wsSecurityTokenSerializer == null)
  112. {
  113. WSSecurityTokenSerializer wsSecurityTokenSerializer = this.tokenSerializer as WSSecurityTokenSerializer;
  114. if (wsSecurityTokenSerializer == null)
  115. {
  116. wsSecurityTokenSerializer = new WSSecurityTokenSerializer(this.SecurityVersion);
  117. }
  118. this.wsSecurityTokenSerializer = wsSecurityTokenSerializer;
  119. }
  120. return this.wsSecurityTokenSerializer;
  121. }
  122. }
  123. internal bool TryCreateKeyIdentifierClauseFromTokenXml(XmlElement element, SecurityTokenReferenceStyle tokenReferenceStyle, out SecurityKeyIdentifierClause securityKeyIdentifierClause)
  124. {
  125. return this.WSSecurityTokenSerializer.TryCreateKeyIdentifierClauseFromTokenXml(element, tokenReferenceStyle, out securityKeyIdentifierClause);
  126. }
  127. internal SecurityKeyIdentifierClause CreateKeyIdentifierClauseFromTokenXml(XmlElement element, SecurityTokenReferenceStyle tokenReferenceStyle)
  128. {
  129. return this.WSSecurityTokenSerializer.CreateKeyIdentifierClauseFromTokenXml(element, tokenReferenceStyle);
  130. }
  131. internal SendSecurityHeader CreateSendSecurityHeader(Message message,
  132. string actor, bool mustUnderstand, bool relay,
  133. SecurityAlgorithmSuite algorithmSuite, MessageDirection direction)
  134. {
  135. return this.SecurityVersion.CreateSendSecurityHeader(message, actor, mustUnderstand, relay, this, algorithmSuite, direction);
  136. }
  137. internal ReceiveSecurityHeader CreateReceiveSecurityHeader(Message message,
  138. string actor,
  139. SecurityAlgorithmSuite algorithmSuite, MessageDirection direction)
  140. {
  141. ReceiveSecurityHeader header = TryCreateReceiveSecurityHeader(message, actor, algorithmSuite, direction);
  142. if (header == null)
  143. {
  144. if (String.IsNullOrEmpty(actor))
  145. throw System.ServiceModel.Diagnostics.TraceUtility.ThrowHelperError(new MessageSecurityException(
  146. SR.GetString(SR.UnableToFindSecurityHeaderInMessageNoActor)), message);
  147. else
  148. throw System.ServiceModel.Diagnostics.TraceUtility.ThrowHelperError(new MessageSecurityException(
  149. SR.GetString(SR.UnableToFindSecurityHeaderInMessage, actor)), message);
  150. }
  151. return header;
  152. }
  153. internal ReceiveSecurityHeader TryCreateReceiveSecurityHeader(Message message,
  154. string actor,
  155. SecurityAlgorithmSuite algorithmSuite, MessageDirection direction)
  156. {
  157. return this.SecurityVersion.TryCreateReceiveSecurityHeader(message, actor, this, algorithmSuite, direction);
  158. }
  159. internal bool DoesMessageContainSecurityHeader(Message message)
  160. {
  161. return this.SecurityVersion.DoesMessageContainSecurityHeader(message);
  162. }
  163. internal bool TryGetSecurityContextIds(Message message, string[] actors, bool isStrictMode, ICollection<UniqueId> results)
  164. {
  165. if (results == null)
  166. {
  167. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("results");
  168. }
  169. SecureConversationDriver driver = this.SecureConversationDriver;
  170. int securityHeaderIndex = this.SecurityVersion.FindIndexOfSecurityHeader(message, actors);
  171. if (securityHeaderIndex < 0)
  172. {
  173. return false;
  174. }
  175. bool addedContextIds = false;
  176. using (XmlDictionaryReader reader = message.Headers.GetReaderAtHeader(securityHeaderIndex))
  177. {
  178. if (!reader.IsStartElement())
  179. {
  180. return false;
  181. }
  182. if (reader.IsEmptyElement)
  183. {
  184. return false;
  185. }
  186. reader.ReadStartElement();
  187. while (reader.IsStartElement())
  188. {
  189. if (driver.IsAtSecurityContextToken(reader))
  190. {
  191. results.Add(driver.GetSecurityContextTokenId(reader));
  192. addedContextIds = true;
  193. if (isStrictMode)
  194. {
  195. break;
  196. }
  197. }
  198. else
  199. {
  200. reader.Skip();
  201. }
  202. }
  203. }
  204. return addedContextIds;
  205. }
  206. }
  207. }