ClientCredentials.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //-----------------------------------------------------------------------------
  4. namespace System.ServiceModel.Description
  5. {
  6. using System.Diagnostics.CodeAnalysis;
  7. using System.IdentityModel.Selectors;
  8. using System.Runtime;
  9. using System.Runtime.CompilerServices;
  10. using System.ServiceModel.Channels;
  11. using System.ServiceModel.Dispatcher;
  12. using System.ServiceModel.Security;
  13. using System.IdentityModel.Tokens;
  14. public class ClientCredentials : SecurityCredentialsManager, IEndpointBehavior
  15. {
  16. internal const bool SupportInteractiveDefault = true;
  17. UserNamePasswordClientCredential userName;
  18. X509CertificateInitiatorClientCredential clientCertificate;
  19. X509CertificateRecipientClientCredential serviceCertificate;
  20. WindowsClientCredential windows;
  21. HttpDigestClientCredential httpDigest;
  22. IssuedTokenClientCredential issuedToken;
  23. PeerCredential peer;
  24. bool supportInteractive;
  25. bool isReadOnly;
  26. GetInfoCardTokenCallback getInfoCardTokenCallback = null;
  27. bool useIdentityConfiguration = false;
  28. SecurityTokenHandlerCollectionManager securityTokenHandlerCollectionManager = null;
  29. object handlerCollectionLock = new object();
  30. public ClientCredentials()
  31. {
  32. this.supportInteractive = SupportInteractiveDefault;
  33. }
  34. protected ClientCredentials(ClientCredentials other)
  35. {
  36. if (other == null)
  37. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("other");
  38. if (other.userName != null)
  39. this.userName = new UserNamePasswordClientCredential(other.userName);
  40. if (other.clientCertificate != null)
  41. this.clientCertificate = new X509CertificateInitiatorClientCredential(other.clientCertificate);
  42. if (other.serviceCertificate != null)
  43. this.serviceCertificate = new X509CertificateRecipientClientCredential(other.serviceCertificate);
  44. if (other.windows != null)
  45. this.windows = new WindowsClientCredential(other.windows);
  46. if (other.httpDigest != null)
  47. this.httpDigest = new HttpDigestClientCredential(other.httpDigest);
  48. if (other.issuedToken != null)
  49. this.issuedToken = new IssuedTokenClientCredential(other.issuedToken);
  50. if (other.peer != null)
  51. this.peer = new PeerCredential(other.peer);
  52. this.getInfoCardTokenCallback = other.getInfoCardTokenCallback;
  53. this.supportInteractive = other.supportInteractive;
  54. this.securityTokenHandlerCollectionManager = other.securityTokenHandlerCollectionManager;
  55. this.useIdentityConfiguration = other.useIdentityConfiguration;
  56. this.isReadOnly = other.isReadOnly;
  57. }
  58. internal GetInfoCardTokenCallback GetInfoCardTokenCallback
  59. {
  60. get
  61. {
  62. if (this.getInfoCardTokenCallback == null)
  63. {
  64. GetInfoCardTokenCallback gtc = new GetInfoCardTokenCallback(this.GetInfoCardSecurityToken);
  65. this.getInfoCardTokenCallback = gtc;
  66. }
  67. return this.getInfoCardTokenCallback;
  68. }
  69. }
  70. public IssuedTokenClientCredential IssuedToken
  71. {
  72. get
  73. {
  74. if (this.issuedToken == null)
  75. {
  76. this.issuedToken = new IssuedTokenClientCredential();
  77. if (isReadOnly)
  78. this.issuedToken.MakeReadOnly();
  79. }
  80. return this.issuedToken;
  81. }
  82. }
  83. public UserNamePasswordClientCredential UserName
  84. {
  85. get
  86. {
  87. if (this.userName == null)
  88. {
  89. this.userName = new UserNamePasswordClientCredential();
  90. if (isReadOnly)
  91. this.userName.MakeReadOnly();
  92. }
  93. return this.userName;
  94. }
  95. }
  96. public X509CertificateInitiatorClientCredential ClientCertificate
  97. {
  98. get
  99. {
  100. if (this.clientCertificate == null)
  101. {
  102. this.clientCertificate = new X509CertificateInitiatorClientCredential();
  103. if (isReadOnly)
  104. this.clientCertificate.MakeReadOnly();
  105. }
  106. return this.clientCertificate;
  107. }
  108. }
  109. public X509CertificateRecipientClientCredential ServiceCertificate
  110. {
  111. get
  112. {
  113. if (this.serviceCertificate == null)
  114. {
  115. this.serviceCertificate = new X509CertificateRecipientClientCredential();
  116. if (isReadOnly)
  117. this.serviceCertificate.MakeReadOnly();
  118. }
  119. return this.serviceCertificate;
  120. }
  121. }
  122. public WindowsClientCredential Windows
  123. {
  124. get
  125. {
  126. if (this.windows == null)
  127. {
  128. this.windows = new WindowsClientCredential();
  129. if (isReadOnly)
  130. this.windows.MakeReadOnly();
  131. }
  132. return this.windows;
  133. }
  134. }
  135. public HttpDigestClientCredential HttpDigest
  136. {
  137. get
  138. {
  139. if (this.httpDigest == null)
  140. {
  141. this.httpDigest = new HttpDigestClientCredential();
  142. if (isReadOnly)
  143. this.httpDigest.MakeReadOnly();
  144. }
  145. return this.httpDigest;
  146. }
  147. }
  148. public PeerCredential Peer
  149. {
  150. get
  151. {
  152. if (this.peer == null)
  153. {
  154. this.peer = new PeerCredential();
  155. if (isReadOnly)
  156. this.peer.MakeReadOnly();
  157. }
  158. return this.peer;
  159. }
  160. }
  161. /// <summary>
  162. /// The <see cref="SecurityTokenHandlerCollectionManager" /> containing the set of <see cref="SecurityTokenHandler" />
  163. /// objects used for serializing and validating tokens found in WS-Trust messages.
  164. /// </summary>
  165. public SecurityTokenHandlerCollectionManager SecurityTokenHandlerCollectionManager
  166. {
  167. get
  168. {
  169. if (this.securityTokenHandlerCollectionManager == null)
  170. {
  171. lock (this.handlerCollectionLock)
  172. {
  173. if (this.securityTokenHandlerCollectionManager == null)
  174. {
  175. this.securityTokenHandlerCollectionManager = SecurityTokenHandlerCollectionManager.CreateDefaultSecurityTokenHandlerCollectionManager();
  176. }
  177. }
  178. }
  179. return this.securityTokenHandlerCollectionManager;
  180. }
  181. set
  182. {
  183. if (this.isReadOnly)
  184. {
  185. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ObjectIsReadOnly)));
  186. }
  187. this.securityTokenHandlerCollectionManager = value;
  188. }
  189. }
  190. public bool UseIdentityConfiguration
  191. {
  192. get
  193. {
  194. return this.useIdentityConfiguration;
  195. }
  196. set
  197. {
  198. if (this.isReadOnly)
  199. {
  200. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ObjectIsReadOnly)));
  201. }
  202. this.useIdentityConfiguration = value;
  203. }
  204. }
  205. public bool SupportInteractive
  206. {
  207. get
  208. {
  209. return this.supportInteractive;
  210. }
  211. set
  212. {
  213. if (this.isReadOnly)
  214. {
  215. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ObjectIsReadOnly)));
  216. }
  217. this.supportInteractive = value;
  218. }
  219. }
  220. internal static ClientCredentials CreateDefaultCredentials()
  221. {
  222. return new ClientCredentials();
  223. }
  224. public override SecurityTokenManager CreateSecurityTokenManager()
  225. {
  226. return new ClientCredentialsSecurityTokenManager(this.Clone());
  227. }
  228. protected virtual ClientCredentials CloneCore()
  229. {
  230. return new ClientCredentials(this);
  231. }
  232. public ClientCredentials Clone()
  233. {
  234. ClientCredentials result = CloneCore();
  235. if (result == null || result.GetType() != this.GetType())
  236. {
  237. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotImplementedException(SR.GetString(SR.CloneNotImplementedCorrectly, this.GetType(), (result != null) ? result.ToString() : "null")));
  238. }
  239. return result;
  240. }
  241. void IEndpointBehavior.Validate(ServiceEndpoint serviceEndpoint)
  242. {
  243. }
  244. void IEndpointBehavior.AddBindingParameters(ServiceEndpoint serviceEndpoint, BindingParameterCollection bindingParameters)
  245. {
  246. if (bindingParameters == null)
  247. {
  248. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("bindingParameters");
  249. }
  250. // throw if bindingParameters already has a SecurityCredentialsManager
  251. SecurityCredentialsManager otherCredentialsManager = bindingParameters.Find<SecurityCredentialsManager>();
  252. if (otherCredentialsManager != null)
  253. {
  254. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.MultipleSecurityCredentialsManagersInChannelBindingParameters, otherCredentialsManager)));
  255. }
  256. bindingParameters.Add(this);
  257. }
  258. void IEndpointBehavior.ApplyDispatchBehavior(ServiceEndpoint serviceEndpoint, EndpointDispatcher endpointDispatcher)
  259. {
  260. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(
  261. SR.GetString(SR.SFXEndpointBehaviorUsedOnWrongSide, typeof(ClientCredentials).Name)));
  262. }
  263. [MethodImpl(MethodImplOptions.NoInlining)]
  264. void AddInteractiveInitializers(ServiceEndpoint serviceEndpoint, ClientRuntime behavior)
  265. {
  266. CardSpacePolicyElement[] dummyPolicyElements;
  267. Uri dummyRelyingPartyIssuer;
  268. // we add the initializer only if infocard is required. At this point, serviceEndpoint.Address is not populated correctly but that's not needed to
  269. // determine whether infocard is required or not.
  270. if (InfoCardHelper.IsInfocardRequired(serviceEndpoint.Binding, this, this.CreateSecurityTokenManager(), EndpointAddress.AnonymousAddress, out dummyPolicyElements, out dummyRelyingPartyIssuer))
  271. {
  272. behavior.InteractiveChannelInitializers.Add(new InfocardInteractiveChannelInitializer(this, serviceEndpoint.Binding));
  273. }
  274. }
  275. public virtual void ApplyClientBehavior(ServiceEndpoint serviceEndpoint, ClientRuntime behavior)
  276. {
  277. if (serviceEndpoint == null)
  278. {
  279. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("serviceEndpoint");
  280. }
  281. if (serviceEndpoint.Binding == null)
  282. {
  283. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("serviceEndpoint.Binding");
  284. }
  285. if (serviceEndpoint.Binding.CreateBindingElements().Find<SecurityBindingElement>() == null)
  286. {
  287. return;
  288. }
  289. try
  290. {
  291. AddInteractiveInitializers(serviceEndpoint, behavior);
  292. }
  293. catch (System.IO.FileNotFoundException)
  294. {
  295. }
  296. }
  297. // RC0 workaround to freeze credentials when the channel factory is opened
  298. internal void MakeReadOnly()
  299. {
  300. this.isReadOnly = true;
  301. if (this.clientCertificate != null)
  302. this.clientCertificate.MakeReadOnly();
  303. if (this.serviceCertificate != null)
  304. this.serviceCertificate.MakeReadOnly();
  305. if (this.userName != null)
  306. this.userName.MakeReadOnly();
  307. if (this.windows != null)
  308. this.windows.MakeReadOnly();
  309. if (this.httpDigest != null)
  310. this.httpDigest.MakeReadOnly();
  311. if (this.issuedToken != null)
  312. this.issuedToken.MakeReadOnly();
  313. if (this.peer != null)
  314. this.peer.MakeReadOnly();
  315. }
  316. // This APTCA method calls CardSpaceSelector.GetToken(..), which is defined in a non-APTCA assembly. It would be a breaking change to add a Demand,
  317. // while we don't have an identified security vulnerability here.
  318. [SuppressMessage(FxCop.Category.Security, FxCop.Rule.AptcaMethodsShouldOnlyCallAptcaMethods)]
  319. internal protected virtual SecurityToken GetInfoCardSecurityToken(bool requiresInfoCard, CardSpacePolicyElement[] chain, SecurityTokenSerializer tokenSerializer)
  320. {
  321. if (!requiresInfoCard)
  322. {
  323. return null;
  324. }
  325. return CardSpaceSelector.GetToken(chain, tokenSerializer);
  326. }
  327. }
  328. }