ServiceCredentialsSecurityTokenManager.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. //
  2. // ServiceCredentialsSecurityTokenManager.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 2006 Novell, Inc. http://www.novell.com
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Net.Security;
  30. using System.IdentityModel.Selectors;
  31. using System.IdentityModel.Tokens;
  32. using System.Security.Cryptography.X509Certificates;
  33. using System.ServiceModel;
  34. using System.ServiceModel.Channels;
  35. using System.ServiceModel.Description;
  36. using System.ServiceModel.Security.Tokens;
  37. using ReqType = System.ServiceModel.Security.Tokens.ServiceModelSecurityTokenRequirement;
  38. namespace System.ServiceModel.Security
  39. {
  40. public class ServiceCredentialsSecurityTokenManager : SecurityTokenManager, IEndpointIdentityProvider
  41. {
  42. ServiceCredentials credentials;
  43. public ServiceCredentialsSecurityTokenManager (
  44. ServiceCredentials credentials)
  45. {
  46. this.credentials = credentials;
  47. }
  48. public ServiceCredentials ServiceCredentials {
  49. get { return credentials; }
  50. }
  51. [MonoTODO]
  52. public virtual EndpointIdentity GetIdentityOfSelf (
  53. SecurityTokenRequirement requirement)
  54. {
  55. throw new NotImplementedException ();
  56. }
  57. [MonoTODO]
  58. public override SecurityTokenAuthenticator CreateSecurityTokenAuthenticator (
  59. SecurityTokenRequirement requirement,
  60. out SecurityTokenResolver outOfBandTokenResolver)
  61. {
  62. outOfBandTokenResolver = null;
  63. if (requirement.TokenType == SecurityTokenTypes.UserName)
  64. return CreateUserNameAuthenticator (requirement);
  65. if (requirement.TokenType == SecurityTokenTypes.X509Certificate)
  66. return CreateX509Authenticator (requirement);
  67. if (requirement.TokenType == SecurityTokenTypes.Rsa)
  68. return new RsaSecurityTokenAuthenticator ();
  69. if (requirement.TokenType == ServiceModelSecurityTokenTypes.SecureConversation) {
  70. // FIXME: get parameters from somewhere
  71. SecurityContextSecurityTokenResolver resolver =
  72. new SecurityContextSecurityTokenResolver (0x1000, true);
  73. outOfBandTokenResolver = resolver;
  74. SecurityContextSecurityTokenAuthenticator sc =
  75. new SecurityContextSecurityTokenAuthenticator ();
  76. return new SecureConversationSecurityTokenAuthenticator (requirement, sc, resolver);
  77. }
  78. if (requirement.TokenType == ServiceModelSecurityTokenTypes.AnonymousSslnego)
  79. return CreateSslTokenAuthenticator (requirement);
  80. if (requirement.TokenType == ServiceModelSecurityTokenTypes.MutualSslnego)
  81. return CreateSslTokenAuthenticator (requirement);
  82. if (requirement.TokenType == ServiceModelSecurityTokenTypes.Spnego)
  83. return CreateSpnegoTokenAuthenticator (requirement);
  84. else
  85. throw new NotImplementedException ("Not implemented token type: " + requirement.TokenType);
  86. }
  87. SpnegoSecurityTokenAuthenticator CreateSpnegoTokenAuthenticator (SecurityTokenRequirement requirement)
  88. {
  89. SpnegoSecurityTokenAuthenticator a =
  90. new SpnegoSecurityTokenAuthenticator (this, requirement);
  91. InitializeAuthenticatorCommunicationObject (a.Communication, requirement);
  92. return a;
  93. }
  94. SslSecurityTokenAuthenticator CreateSslTokenAuthenticator (SecurityTokenRequirement requirement)
  95. {
  96. SslSecurityTokenAuthenticator a =
  97. new SslSecurityTokenAuthenticator (this, requirement);
  98. InitializeAuthenticatorCommunicationObject (a.Communication, requirement);
  99. return a;
  100. }
  101. UserNameSecurityTokenAuthenticator CreateUserNameAuthenticator (SecurityTokenRequirement requirement)
  102. {
  103. UserNamePasswordServiceCredential c = ServiceCredentials.UserNameAuthentication;
  104. switch (c.UserNamePasswordValidationMode) {
  105. case UserNamePasswordValidationMode.MembershipProvider:
  106. if (c.MembershipProvider == null)
  107. throw new InvalidOperationException ("For MembershipProvider validation mode, MembershipProvider is required to create a user name token authenticator.");
  108. return new CustomUserNameSecurityTokenAuthenticator (UserNamePasswordValidator.CreateMembershipProviderValidator (c.MembershipProvider));
  109. case UserNamePasswordValidationMode.Windows:
  110. return new WindowsUserNameSecurityTokenAuthenticator (c.IncludeWindowsGroups);
  111. default:
  112. if (c.CustomUserNamePasswordValidator == null)
  113. throw new InvalidOperationException ("For Custom validation mode, CustomUserNamePasswordValidator is required to create a user name token authenticator.");
  114. return new CustomUserNameSecurityTokenAuthenticator (c.CustomUserNamePasswordValidator);
  115. }
  116. }
  117. X509SecurityTokenAuthenticator CreateX509Authenticator (SecurityTokenRequirement requirement)
  118. {
  119. X509CertificateInitiatorServiceCredential c = ServiceCredentials.ClientCertificate;
  120. switch (c.Authentication.CertificateValidationMode) {
  121. case X509CertificateValidationMode.Custom:
  122. if (c.Authentication.CustomCertificateValidator == null)
  123. throw new InvalidOperationException ("For Custom certificate validation mode, CustomCertificateValidator is required to create a token authenticator for X509 certificate.");
  124. return new X509SecurityTokenAuthenticator (c.Authentication.CustomCertificateValidator);
  125. case X509CertificateValidationMode.None:
  126. return new X509SecurityTokenAuthenticator (X509CertificateValidator.None);
  127. case X509CertificateValidationMode.PeerOrChainTrust:
  128. return new X509SecurityTokenAuthenticator (X509CertificateValidator.PeerOrChainTrust);
  129. case X509CertificateValidationMode.ChainTrust:
  130. return new X509SecurityTokenAuthenticator (X509CertificateValidator.ChainTrust);
  131. default:
  132. return new X509SecurityTokenAuthenticator (X509CertificateValidator.PeerTrust);
  133. }
  134. }
  135. void InitializeAuthenticatorCommunicationObject (AuthenticatorCommunicationObject p, SecurityTokenRequirement r)
  136. {
  137. p.ListenUri = r.GetProperty<Uri> (ReqType.ListenUriProperty);
  138. // FIXME: use it somewhere, probably to build
  139. // IssuerBinding. However, there is also IssuerBinding
  140. // property. SecureConversationSecurityBindingElement
  141. // as well.
  142. SecurityBindingElement sbe =
  143. r.GetProperty<SecurityBindingElement> (ReqType.SecurityBindingElementProperty);
  144. p.SecurityBindingElement = sbe;
  145. /*
  146. // I doubt the binding is acquired this way ...
  147. Binding binding;
  148. if (!r.TryGetProperty<Binding> (ReqType.IssuerBindingProperty, out binding))
  149. binding = new CustomBinding (
  150. new TextMessageEncodingBindingElement (),
  151. new HttpTransportBindingElement ());
  152. p.IssuerBinding = binding;
  153. // not sure if it is used only for this purpose though ...
  154. BindingContext ctx = r.GetProperty<BindingContext> (ReqType.IssuerBindingContextProperty);
  155. foreach (IEndpointBehavior b in ctx.BindingParameters.FindAll<IEndpointBehavior> ())
  156. p.IssuerChannelBehaviors.Add (b);
  157. */
  158. SecurityTokenVersion ver =
  159. r.GetProperty<SecurityTokenVersion> (ReqType.MessageSecurityVersionProperty);
  160. p.SecurityTokenSerializer =
  161. CreateSecurityTokenSerializer (ver);
  162. /*
  163. // seems like they are optional here ... (but possibly
  164. // used later)
  165. EndpointAddress address;
  166. if (!r.TryGetProperty<EndpointAddress> (ReqType.IssuerAddressProperty, out address))
  167. address = p.TargetAddress;
  168. p.IssuerAddress = address;
  169. */
  170. // It is somehow not checked as mandatory ...
  171. SecurityAlgorithmSuite suite = null;
  172. r.TryGetProperty<SecurityAlgorithmSuite> (ReqType.SecurityAlgorithmSuiteProperty, out suite);
  173. p.SecurityAlgorithmSuite = suite;
  174. }
  175. #region CreateSecurityTokenProvider()
  176. [MonoTODO]
  177. public override SecurityTokenProvider CreateSecurityTokenProvider (SecurityTokenRequirement requirement)
  178. {
  179. if (IsIssuedSecurityTokenRequirement (requirement))
  180. return CreateIssuedTokenProvider (requirement);
  181. // not supported: UserName, Rsa, AnonymousSslnego, SecureConv
  182. // huh, they are not constants but properties.
  183. if (requirement.TokenType == SecurityTokenTypes.X509Certificate)
  184. return CreateX509SecurityTokenProvider (requirement);
  185. else if (requirement.TokenType == ServiceModelSecurityTokenTypes.MutualSslnego) {
  186. // FIXME: implement
  187. throw new NotImplementedException ();
  188. } else if (requirement.TokenType == ServiceModelSecurityTokenTypes.SecurityContext) {
  189. // FIXME: implement
  190. throw new NotImplementedException ();
  191. } else if (requirement.TokenType == ServiceModelSecurityTokenTypes.AnonymousSslnego) {
  192. throw new NotSupportedException (String.Format ("Token type '{0}' is not supported", requirement.TokenType));
  193. } else if (requirement.TokenType == ServiceModelSecurityTokenTypes.Spnego) {
  194. // FIXME: implement
  195. throw new NotImplementedException ();
  196. } else if (requirement.TokenType == ServiceModelSecurityTokenTypes.SspiCredential) {
  197. // FIXME: implement
  198. throw new NotImplementedException ();
  199. } else if (requirement.TokenType == SecurityTokenTypes.Saml) {
  200. // FIXME: implement
  201. throw new NotImplementedException ();
  202. } else if (requirement.TokenType == SecurityTokenTypes.Kerberos) {
  203. // FIXME: implement
  204. throw new NotImplementedException ();
  205. }
  206. throw new NotSupportedException (String.Format ("Securirty token requirement '{0}' is not supported", requirement));
  207. }
  208. X509SecurityTokenProvider CreateX509SecurityTokenProvider (SecurityTokenRequirement requirement)
  209. {
  210. bool isInitiator;
  211. requirement.TryGetProperty<bool> (ReqType.IsInitiatorProperty, out isInitiator);
  212. // when it is initiator, then it is for MutualCertificateDuplex.
  213. X509Certificate2 cert;
  214. if (isInitiator) {
  215. cert = credentials.ClientCertificate.Certificate;
  216. if (cert == null)
  217. throw new InvalidOperationException ("Client certificate is not provided in ServiceCredentials.");
  218. if (cert.PrivateKey == null)
  219. throw new ArgumentException ("Client certificate for MutualCertificateDuplex does not have a private key which is required for key exchange.");
  220. } else {
  221. cert = credentials.ServiceCertificate.Certificate;
  222. if (cert == null)
  223. throw new InvalidOperationException ("Service certificate is not provided in ServiceCredentials.");
  224. if (cert.PrivateKey == null)
  225. throw new ArgumentException ("Service certificate does not have a private key which is required for key exchange.");
  226. }
  227. X509SecurityTokenProvider p =
  228. new X509SecurityTokenProvider (cert);
  229. return p;
  230. }
  231. IssuedSecurityTokenProvider CreateIssuedProviderBase (SecurityTokenRequirement r)
  232. {
  233. IssuedSecurityTokenProvider p =
  234. new IssuedSecurityTokenProvider ();
  235. p.TargetAddress = r.GetProperty<EndpointAddress> (ReqType.TargetAddressProperty);
  236. // FIXME: use it somewhere, probably to build
  237. // IssuerBinding. However, there is also IssuerBinding
  238. // property. SecureConversationSecurityBindingElement
  239. // as well.
  240. SecurityBindingElement sbe =
  241. r.GetProperty<SecurityBindingElement> (ReqType.SecurityBindingElementProperty);
  242. // I doubt the binding is acquired this way ...
  243. Binding binding;
  244. if (!r.TryGetProperty<Binding> (ReqType.IssuerBindingProperty, out binding))
  245. binding = new CustomBinding (sbe,
  246. new TextMessageEncodingBindingElement (),
  247. new HttpTransportBindingElement ());
  248. p.IssuerBinding = binding;
  249. // not sure if it is used only for this purpose though ...
  250. BindingContext ctx = r.GetProperty<BindingContext> (ReqType.IssuerBindingContextProperty);
  251. foreach (IEndpointBehavior b in ctx.BindingParameters.FindAll<IEndpointBehavior> ())
  252. p.IssuerChannelBehaviors.Add (b);
  253. SecurityTokenVersion ver =
  254. r.GetProperty<SecurityTokenVersion> (ReqType.MessageSecurityVersionProperty);
  255. p.SecurityTokenSerializer =
  256. CreateSecurityTokenSerializer (ver);
  257. // seems like they are optional here ... (but possibly
  258. // used later)
  259. EndpointAddress address;
  260. if (!r.TryGetProperty<EndpointAddress> (ReqType.IssuerAddressProperty, out address))
  261. address = p.TargetAddress;
  262. p.IssuerAddress = address;
  263. // It is somehow not checked as mandatory ...
  264. SecurityAlgorithmSuite suite = null;
  265. r.TryGetProperty<SecurityAlgorithmSuite> (ReqType.SecurityAlgorithmSuiteProperty, out suite);
  266. p.SecurityAlgorithmSuite = suite;
  267. return p;
  268. }
  269. // FIXME: it is far from done.
  270. SecurityTokenProvider CreateSecureConversationProvider (SecurityTokenRequirement r)
  271. {
  272. IssuedSecurityTokenProvider p =
  273. CreateIssuedProviderBase (r);
  274. // FIXME: use it somewhere.
  275. int keySize = r.KeySize;
  276. return p;
  277. }
  278. IssuedSecurityTokenProvider CreateIssuedTokenProvider (SecurityTokenRequirement requirement)
  279. {
  280. IssuedSecurityTokenProvider p =
  281. new IssuedSecurityTokenProvider ();
  282. // FIXME: fill properties
  283. EndpointAddress address;
  284. if (requirement.TryGetProperty<EndpointAddress> (ReqType.IssuerAddressProperty, out address))
  285. p.IssuerAddress = address;
  286. if (requirement.TryGetProperty<EndpointAddress> (ReqType.TargetAddressProperty, out address))
  287. p.TargetAddress = address;
  288. Binding binding;
  289. if (requirement.TryGetProperty<Binding> (ReqType.IssuerBindingProperty, out binding))
  290. p.IssuerBinding = binding;
  291. MessageSecurityVersion ver;
  292. if (requirement.TryGetProperty<MessageSecurityVersion> (ReqType.MessageSecurityVersionProperty, out ver))
  293. p.SecurityTokenSerializer = CreateSecurityTokenSerializer (ver.SecurityTokenVersion);
  294. SecurityAlgorithmSuite suite;
  295. if (requirement.TryGetProperty<SecurityAlgorithmSuite> (ReqType.SecurityAlgorithmSuiteProperty, out suite))
  296. p.SecurityAlgorithmSuite = suite;
  297. return p;
  298. }
  299. #endregion
  300. [MonoTODO ("pass correct arguments to WSSecurityTokenSerializer..ctor()")]
  301. public override SecurityTokenSerializer CreateSecurityTokenSerializer (SecurityTokenVersion version)
  302. {
  303. bool bsp = version.GetSecuritySpecifications ().Contains (Constants.WSBasicSecurityProfileCore1);
  304. SecurityVersion ver =
  305. version.GetSecuritySpecifications ().Contains (Constants.Wss11Namespace) ?
  306. SecurityVersion.WSSecurity11 :
  307. SecurityVersion.WSSecurity10;
  308. // FIXME: pass correct arguments.
  309. return new WSSecurityTokenSerializer (ver, bsp, null,
  310. ServiceCredentials.SecureConversationAuthentication.SecurityStateEncoder,
  311. Type.EmptyTypes,
  312. int.MaxValue, int.MaxValue, int.MaxValue);
  313. }
  314. protected internal bool IsIssuedSecurityTokenRequirement (
  315. SecurityTokenRequirement requirement)
  316. {
  317. SecurityTokenParameters ret;
  318. if (!requirement.TryGetProperty<SecurityTokenParameters> (ServiceModelSecurityTokenRequirement.IssuedSecurityTokenParametersProperty, out ret))
  319. return false;
  320. return ret is IssuedSecurityTokenParameters;
  321. }
  322. }
  323. }