ChannelCredentials.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //-----------------------------------------------------------------------------
  4. namespace System.ServiceModel.ComIntegration
  5. {
  6. using System;
  7. using System.ServiceModel.Description;
  8. using System.Reflection;
  9. using System.Net;
  10. using System.Security;
  11. using System.Security.AccessControl;
  12. using System.Security.Principal;
  13. using System.Runtime.InteropServices;
  14. using System.Collections.Generic;
  15. using System.ServiceModel;
  16. using System.ServiceModel.Channels;
  17. using System.Security.Cryptography.X509Certificates;
  18. using System.ServiceModel.Security;
  19. using System.ServiceModel.Security.Tokens;
  20. internal class ChannelCredentials : IChannelCredentials, IDisposable
  21. {
  22. protected IProvideChannelBuilderSettings channelBuilderSettings;
  23. internal ChannelCredentials(IProvideChannelBuilderSettings channelBuilderSettings)
  24. {
  25. this.channelBuilderSettings = channelBuilderSettings;
  26. }
  27. internal static ComProxy Create(IntPtr outer, IProvideChannelBuilderSettings channelBuilderSettings)
  28. {
  29. if (channelBuilderSettings == null)
  30. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.CannotCreateChannelOption)));
  31. ChannelCredentials ChannelCredentials = null;
  32. ComProxy proxy = null;
  33. try
  34. {
  35. ChannelCredentials = new ChannelCredentials(channelBuilderSettings);
  36. proxy = ComProxy.Create(outer, ChannelCredentials, ChannelCredentials);
  37. return proxy;
  38. }
  39. finally
  40. {
  41. if (proxy == null)
  42. {
  43. if (ChannelCredentials != null)
  44. ((IDisposable)ChannelCredentials).Dispose();
  45. }
  46. }
  47. }
  48. void IDisposable.Dispose()
  49. {
  50. }
  51. void IChannelCredentials.SetWindowsCredential(string domain, string userName, string password, int impersonationLevel, bool allowNtlm)
  52. {
  53. lock (channelBuilderSettings)
  54. {
  55. KeyedByTypeCollection<IEndpointBehavior> behaviors = channelBuilderSettings.Behaviors;
  56. NetworkCredential newCredentials = null;
  57. if ((!String.IsNullOrEmpty(domain)) || (!String.IsNullOrEmpty(userName)) || (!String.IsNullOrEmpty(password)))
  58. {
  59. if (String.IsNullOrEmpty(userName))
  60. {
  61. userName = "";
  62. }
  63. System.ServiceModel.Security.SecurityUtils.PrepareNetworkCredential();
  64. newCredentials = new NetworkCredential(userName, password, domain);
  65. }
  66. ClientCredentials channelCredentials = behaviors.Find<ClientCredentials>();
  67. if (channelCredentials == null)
  68. {
  69. channelCredentials = new ClientCredentials();
  70. behaviors.Add(channelCredentials);
  71. }
  72. channelCredentials.Windows.AllowedImpersonationLevel = (TokenImpersonationLevel)impersonationLevel;
  73. // To disable AllowNtlm warning.
  74. #pragma warning disable 618
  75. channelCredentials.Windows.AllowNtlm = allowNtlm;
  76. #pragma warning restore 618
  77. channelCredentials.Windows.ClientCredential = newCredentials;
  78. }
  79. }
  80. void IChannelCredentials.SetUserNameCredential(string userName, string password)
  81. {
  82. lock (channelBuilderSettings)
  83. {
  84. KeyedByTypeCollection<IEndpointBehavior> behaviors = channelBuilderSettings.Behaviors;
  85. ClientCredentials channelCredentials = behaviors.Find<ClientCredentials>();
  86. if (channelCredentials == null)
  87. {
  88. channelCredentials = new ClientCredentials();
  89. behaviors.Add(channelCredentials);
  90. }
  91. channelCredentials.UserName.UserName = userName;
  92. channelCredentials.UserName.Password = password;
  93. }
  94. }
  95. void IChannelCredentials.SetServiceCertificateAuthentication(string storeLocation, string revocationMode, string certificationValidationMode)
  96. {
  97. lock (channelBuilderSettings)
  98. {
  99. StoreLocation location = (StoreLocation)Enum.Parse(typeof(StoreLocation), storeLocation);
  100. X509RevocationMode mode = (X509RevocationMode)Enum.Parse(typeof(X509RevocationMode), revocationMode);
  101. X509CertificateValidationMode validationMode = X509ServiceCertificateAuthentication.DefaultCertificateValidationMode;
  102. if (!String.IsNullOrEmpty(certificationValidationMode))
  103. validationMode = (X509CertificateValidationMode)Enum.Parse(typeof(X509CertificateValidationMode), certificationValidationMode);
  104. KeyedByTypeCollection<IEndpointBehavior> behaviors = channelBuilderSettings.Behaviors;
  105. ClientCredentials channelCredentials = behaviors.Find<ClientCredentials>();
  106. if (channelCredentials == null)
  107. {
  108. channelCredentials = new ClientCredentials();
  109. behaviors.Add(channelCredentials);
  110. }
  111. channelCredentials.ServiceCertificate.Authentication.TrustedStoreLocation = location;
  112. channelCredentials.ServiceCertificate.Authentication.RevocationMode = mode;
  113. channelCredentials.ServiceCertificate.Authentication.CertificateValidationMode = validationMode;
  114. }
  115. }
  116. void IChannelCredentials.SetClientCertificateFromStore(string storeLocation, string storeName, string findType, object findValue)
  117. {
  118. lock (channelBuilderSettings)
  119. {
  120. StoreLocation location = (StoreLocation)Enum.Parse(typeof(StoreLocation), storeLocation);
  121. StoreName name = (StoreName)Enum.Parse(typeof(StoreName), storeName);
  122. X509FindType type = (X509FindType)Enum.Parse(typeof(X509FindType), findType);
  123. KeyedByTypeCollection<IEndpointBehavior> behaviors = channelBuilderSettings.Behaviors;
  124. ClientCredentials channelCredentials = behaviors.Find<ClientCredentials>();
  125. if (channelCredentials == null)
  126. {
  127. channelCredentials = new ClientCredentials();
  128. behaviors.Add(channelCredentials);
  129. }
  130. channelCredentials.ClientCertificate.SetCertificate(location, name, type, findValue);
  131. }
  132. }
  133. void IChannelCredentials.SetClientCertificateFromStoreByName(string subjectName, string storeLocation, string storeName)
  134. {
  135. ((IChannelCredentials)this).SetClientCertificateFromStore(storeLocation, storeName, X509CertificateInitiatorClientCredential.DefaultFindType.ToString("G"), subjectName);
  136. }
  137. void IChannelCredentials.SetClientCertificateFromFile(string fileName, string password, string keyStorageFlags)
  138. {
  139. lock (channelBuilderSettings)
  140. {
  141. KeyedByTypeCollection<IEndpointBehavior> behaviors = channelBuilderSettings.Behaviors;
  142. X509Certificate2 cert;
  143. if (!String.IsNullOrEmpty(keyStorageFlags))
  144. {
  145. X509KeyStorageFlags flags = (X509KeyStorageFlags)Enum.Parse(typeof(X509KeyStorageFlags), keyStorageFlags);
  146. cert = new X509Certificate2(fileName, password, flags);
  147. }
  148. else
  149. {
  150. cert = new X509Certificate2(fileName, password);
  151. }
  152. ClientCredentials channelCredentials = behaviors.Find<ClientCredentials>();
  153. if (channelCredentials == null)
  154. {
  155. channelCredentials = new ClientCredentials();
  156. behaviors.Add(channelCredentials);
  157. }
  158. channelCredentials.ClientCertificate.Certificate = cert;
  159. }
  160. }
  161. void IChannelCredentials.SetDefaultServiceCertificateFromStore(string storeLocation, string storeName, string findType, object findValue)
  162. {
  163. lock (channelBuilderSettings)
  164. {
  165. StoreLocation location = (StoreLocation)Enum.Parse(typeof(StoreLocation), storeLocation);
  166. StoreName name = (StoreName)Enum.Parse(typeof(StoreName), storeName);
  167. X509FindType type = (X509FindType)Enum.Parse(typeof(X509FindType), findType);
  168. KeyedByTypeCollection<IEndpointBehavior> behaviors = channelBuilderSettings.Behaviors;
  169. ClientCredentials channelCredentials = behaviors.Find<ClientCredentials>();
  170. if (channelCredentials == null)
  171. {
  172. channelCredentials = new ClientCredentials();
  173. behaviors.Add(channelCredentials);
  174. }
  175. channelCredentials.ServiceCertificate.SetDefaultCertificate(location, name, type, findValue);
  176. }
  177. }
  178. void IChannelCredentials.SetDefaultServiceCertificateFromStoreByName(string subjectName, string storeLocation, string storeName)
  179. {
  180. ((IChannelCredentials)this).SetDefaultServiceCertificateFromStore(storeLocation, storeName, X509CertificateInitiatorClientCredential.DefaultFindType.ToString("G"), subjectName);
  181. }
  182. void IChannelCredentials.SetDefaultServiceCertificateFromFile(string fileName, string password, string keyStorageFlags)
  183. {
  184. lock (channelBuilderSettings)
  185. {
  186. KeyedByTypeCollection<IEndpointBehavior> behaviors = channelBuilderSettings.Behaviors;
  187. X509Certificate2 cert;
  188. if (!String.IsNullOrEmpty(keyStorageFlags))
  189. {
  190. X509KeyStorageFlags flags = (X509KeyStorageFlags)Enum.Parse(typeof(X509KeyStorageFlags), keyStorageFlags);
  191. cert = new X509Certificate2(fileName, password, flags);
  192. }
  193. else
  194. {
  195. cert = new X509Certificate2(fileName, password);
  196. }
  197. ClientCredentials channelCredentials = behaviors.Find<ClientCredentials>();
  198. if (channelCredentials == null)
  199. {
  200. channelCredentials = new ClientCredentials();
  201. behaviors.Add(channelCredentials);
  202. }
  203. channelCredentials.ServiceCertificate.DefaultCertificate = cert;
  204. }
  205. }
  206. void IChannelCredentials.SetIssuedToken(string localIssuerAddres, string localIssuerBindingType, string localIssuerBinding)
  207. {
  208. lock (channelBuilderSettings)
  209. {
  210. Binding binding = null;
  211. binding = ConfigLoader.LookupBinding(localIssuerBindingType, localIssuerBinding);
  212. KeyedByTypeCollection<IEndpointBehavior> behaviors = channelBuilderSettings.Behaviors;
  213. ClientCredentials channelCredentials = behaviors.Find<ClientCredentials>();
  214. if (channelCredentials == null)
  215. {
  216. channelCredentials = new ClientCredentials();
  217. behaviors.Add(channelCredentials);
  218. }
  219. channelCredentials.IssuedToken.LocalIssuerAddress = new EndpointAddress(localIssuerAddres);
  220. channelCredentials.IssuedToken.LocalIssuerBinding = binding;
  221. }
  222. }
  223. }
  224. }