SecurityBindingElement.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. //
  2. // SecurityBindingElement.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 2005-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.Collections.Generic;
  29. using System.Collections.ObjectModel;
  30. using System.ServiceModel.Description;
  31. using System.ServiceModel.Channels;
  32. using System.ServiceModel.Security;
  33. #if !NET_2_1
  34. using System.IdentityModel.Selectors;
  35. using System.IdentityModel.Tokens;
  36. using System.ServiceModel.Security.Tokens;
  37. #endif
  38. namespace System.ServiceModel.Channels
  39. {
  40. public abstract class SecurityBindingElement : BindingElement
  41. {
  42. internal SecurityBindingElement ()
  43. {
  44. #if !NET_2_1
  45. DefaultAlgorithmSuite = SecurityAlgorithmSuite.Default;
  46. MessageSecurityVersion = MessageSecurityVersion.Default;
  47. KeyEntropyMode = SecurityKeyEntropyMode.CombinedEntropy;
  48. endpoint = new SupportingTokenParameters ();
  49. operation = new Dictionary<string,SupportingTokenParameters> ();
  50. opt_endpoint = new SupportingTokenParameters ();
  51. opt_operation = new Dictionary<string,SupportingTokenParameters> ();
  52. service_settings = new LocalServiceSecuritySettings ();
  53. #endif
  54. IncludeTimestamp = true;
  55. LocalClientSettings = new LocalClientSecuritySettings ();
  56. }
  57. internal SecurityBindingElement (SecurityBindingElement other)
  58. {
  59. #if !NET_2_1
  60. alg_suite = other.alg_suite;
  61. key_entropy_mode = other.key_entropy_mode;
  62. security_header_layout = other.security_header_layout;
  63. msg_security_version = other.msg_security_version;
  64. endpoint = other.endpoint.Clone ();
  65. opt_endpoint = other.opt_endpoint.Clone ();
  66. operation = new Dictionary<string,SupportingTokenParameters> ();
  67. foreach (KeyValuePair<string,SupportingTokenParameters> p in other.operation)
  68. operation.Add (p.Key, p.Value.Clone ());
  69. opt_operation = new Dictionary<string,SupportingTokenParameters> ();
  70. foreach (KeyValuePair<string,SupportingTokenParameters> p in other.opt_operation)
  71. opt_operation.Add (p.Key, p.Value.Clone ());
  72. service_settings = other.service_settings.Clone ();
  73. #endif
  74. IncludeTimestamp = other.IncludeTimestamp;
  75. LocalClientSettings = other.LocalClientSettings.Clone ();
  76. }
  77. #if !NET_2_1
  78. SecurityAlgorithmSuite alg_suite;
  79. SecurityKeyEntropyMode key_entropy_mode;
  80. SecurityHeaderLayout security_header_layout;
  81. MessageSecurityVersion msg_security_version;
  82. SupportingTokenParameters endpoint, opt_endpoint;
  83. IDictionary<string,SupportingTokenParameters> operation, opt_operation;
  84. LocalServiceSecuritySettings service_settings;
  85. #endif
  86. public bool IncludeTimestamp { get; set; }
  87. public LocalClientSecuritySettings LocalClientSettings { get; private set; }
  88. #if !NET_2_1
  89. public SecurityAlgorithmSuite DefaultAlgorithmSuite {
  90. get { return alg_suite; }
  91. set { alg_suite = value; }
  92. }
  93. public SecurityKeyEntropyMode KeyEntropyMode {
  94. get { return key_entropy_mode; }
  95. set { key_entropy_mode = value; }
  96. }
  97. public LocalServiceSecuritySettings LocalServiceSettings {
  98. get { return service_settings; }
  99. }
  100. public SecurityHeaderLayout SecurityHeaderLayout {
  101. get { return security_header_layout; }
  102. set { security_header_layout = value; }
  103. }
  104. public MessageSecurityVersion MessageSecurityVersion {
  105. get { return msg_security_version; }
  106. set { msg_security_version = value; }
  107. }
  108. public SupportingTokenParameters EndpointSupportingTokenParameters {
  109. get { return endpoint; }
  110. }
  111. public IDictionary<string,SupportingTokenParameters> OperationSupportingTokenParameters {
  112. get { return operation; }
  113. }
  114. public SupportingTokenParameters OptionalEndpointSupportingTokenParameters {
  115. get { return opt_endpoint; }
  116. }
  117. public IDictionary<string,SupportingTokenParameters> OptionalOperationSupportingTokenParameters {
  118. get { return opt_operation; }
  119. }
  120. #endif
  121. [MonoTODO ("It supports only IRequestSessionChannel")]
  122. public override bool CanBuildChannelFactory<TChannel> (BindingContext context)
  123. {
  124. return context.CanBuildInnerChannelFactory<TChannel> ();
  125. }
  126. public override IChannelFactory<TChannel> BuildChannelFactory<TChannel> (
  127. BindingContext context)
  128. {
  129. return BuildChannelFactoryCore<TChannel> (context);
  130. }
  131. protected abstract IChannelFactory<TChannel>
  132. BuildChannelFactoryCore<TChannel> (BindingContext context);
  133. #if !NET_2_1
  134. [MonoTODO ("It probably supports only IReplySessionChannel")]
  135. public override bool CanBuildChannelListener<TChannel> (BindingContext context)
  136. {
  137. return context.CanBuildInnerChannelListener<TChannel> ();
  138. }
  139. public override IChannelListener<TChannel> BuildChannelListener<TChannel> (
  140. BindingContext context)
  141. {
  142. return BuildChannelListenerCore<TChannel> (context);
  143. }
  144. protected abstract IChannelListener<TChannel>
  145. BuildChannelListenerCore<TChannel> (BindingContext context)
  146. where TChannel : class, IChannel;
  147. public virtual void SetKeyDerivation (bool requireDerivedKeys)
  148. {
  149. endpoint.SetKeyDerivation (requireDerivedKeys);
  150. opt_endpoint.SetKeyDerivation (requireDerivedKeys);
  151. foreach (SupportingTokenParameters p in operation.Values)
  152. p.SetKeyDerivation (requireDerivedKeys);
  153. foreach (SupportingTokenParameters p in opt_operation.Values)
  154. p.SetKeyDerivation (requireDerivedKeys);
  155. }
  156. [MonoTODO]
  157. public override string ToString ()
  158. {
  159. return base.ToString ();
  160. }
  161. #else
  162. [MonoTODO]
  163. public override T GetProperty<T> (BindingContext context)
  164. {
  165. return null;
  166. }
  167. #endif
  168. #region Factory methods
  169. #if !NET_2_1
  170. public static SymmetricSecurityBindingElement
  171. CreateAnonymousForCertificateBindingElement ()
  172. {
  173. SymmetricSecurityBindingElement be = new SymmetricSecurityBindingElement ();
  174. be.RequireSignatureConfirmation = true;
  175. be.ProtectionTokenParameters = CreateProtectionTokenParameters (true);
  176. return be;
  177. }
  178. public static TransportSecurityBindingElement
  179. CreateCertificateOverTransportBindingElement ()
  180. {
  181. return CreateCertificateOverTransportBindingElement (MessageSecurityVersion.Default);
  182. }
  183. [MonoTODO]
  184. public static TransportSecurityBindingElement
  185. CreateCertificateOverTransportBindingElement (MessageSecurityVersion version)
  186. {
  187. var be = new TransportSecurityBindingElement () { MessageSecurityVersion = version };
  188. be.EndpointSupportingTokenParameters.SignedEncrypted.Add (new X509SecurityTokenParameters ());
  189. return be;
  190. }
  191. [MonoTODO]
  192. public static AsymmetricSecurityBindingElement
  193. CreateCertificateSignatureBindingElement ()
  194. {
  195. throw new NotImplementedException ();
  196. }
  197. [MonoTODO]
  198. public static SymmetricSecurityBindingElement
  199. CreateIssuedTokenBindingElement (
  200. IssuedSecurityTokenParameters issuedTokenParameters)
  201. {
  202. SymmetricSecurityBindingElement be = new SymmetricSecurityBindingElement ();
  203. be.ProtectionTokenParameters = issuedTokenParameters;
  204. return be;
  205. }
  206. public static SymmetricSecurityBindingElement
  207. CreateIssuedTokenForCertificateBindingElement (
  208. IssuedSecurityTokenParameters issuedTokenParameters)
  209. {
  210. SymmetricSecurityBindingElement be = new SymmetricSecurityBindingElement ();
  211. be.RequireSignatureConfirmation = true;
  212. be.ProtectionTokenParameters = CreateProtectionTokenParameters (true);
  213. be.EndpointSupportingTokenParameters.Endorsing.Add (
  214. issuedTokenParameters);
  215. return be;
  216. }
  217. [MonoTODO]
  218. public static SymmetricSecurityBindingElement
  219. CreateIssuedTokenForSslBindingElement (
  220. IssuedSecurityTokenParameters issuedTokenParameters)
  221. {
  222. return CreateIssuedTokenForSslBindingElement (
  223. issuedTokenParameters, false);
  224. }
  225. [MonoTODO]
  226. public static SymmetricSecurityBindingElement
  227. CreateIssuedTokenForSslBindingElement (
  228. IssuedSecurityTokenParameters issuedTokenParameters,
  229. bool requireCancellation)
  230. {
  231. SymmetricSecurityBindingElement be = new SymmetricSecurityBindingElement ();
  232. be.RequireSignatureConfirmation = true;
  233. be.ProtectionTokenParameters = CreateProtectionTokenParameters (false);
  234. be.EndpointSupportingTokenParameters.Endorsing.Add (
  235. issuedTokenParameters);
  236. return be;
  237. }
  238. [MonoTODO]
  239. public static TransportSecurityBindingElement
  240. CreateIssuedTokenOverTransportBindingElement (
  241. IssuedSecurityTokenParameters issuedTokenParameters)
  242. {
  243. throw new NotImplementedException ();
  244. }
  245. [MonoTODO]
  246. public static SymmetricSecurityBindingElement CreateKerberosBindingElement ()
  247. {
  248. SymmetricSecurityBindingElement be = new SymmetricSecurityBindingElement ();
  249. be.DefaultAlgorithmSuite = SecurityAlgorithmSuite.Basic128;
  250. be.ProtectionTokenParameters = CreateProtectionTokenParameters (false);
  251. be.ProtectionTokenParameters.InclusionMode =
  252. SecurityTokenInclusionMode.Once;
  253. return be;
  254. }
  255. [MonoTODO]
  256. public static TransportSecurityBindingElement
  257. CreateKerberosOverTransportBindingElement ()
  258. {
  259. throw new NotImplementedException ();
  260. }
  261. [MonoTODO]
  262. public static SecurityBindingElement
  263. CreateMutualCertificateBindingElement ()
  264. {
  265. throw new NotImplementedException ();
  266. }
  267. [MonoTODO]
  268. public static SecurityBindingElement
  269. CreateMutualCertificateBindingElement (MessageSecurityVersion version)
  270. {
  271. throw new NotImplementedException ();
  272. }
  273. [MonoTODO]
  274. public static SecurityBindingElement
  275. CreateMutualCertificateBindingElement (
  276. MessageSecurityVersion version,
  277. bool allowSerializedSigningTokenOnReply)
  278. {
  279. throw new NotImplementedException ();
  280. }
  281. [MonoTODO]
  282. public static AsymmetricSecurityBindingElement
  283. CreateMutualCertificateDuplexBindingElement ()
  284. {
  285. throw new NotImplementedException ();
  286. }
  287. [MonoTODO]
  288. public static AsymmetricSecurityBindingElement
  289. CreateMutualCertificateDuplexBindingElement (
  290. MessageSecurityVersion version)
  291. {
  292. throw new NotImplementedException ();
  293. }
  294. public static SecurityBindingElement
  295. CreateSecureConversationBindingElement (SecurityBindingElement binding)
  296. {
  297. return CreateSecureConversationBindingElement (binding, false);
  298. }
  299. public static SecurityBindingElement
  300. CreateSecureConversationBindingElement (
  301. SecurityBindingElement binding, bool requireCancellation)
  302. {
  303. return CreateSecureConversationBindingElement (binding, requireCancellation, null);
  304. }
  305. [MonoTODO]
  306. public static SecurityBindingElement
  307. CreateSecureConversationBindingElement (
  308. SecurityBindingElement binding, bool requireCancellation,
  309. ChannelProtectionRequirements protectionRequirements)
  310. {
  311. SymmetricSecurityBindingElement be =
  312. new SymmetricSecurityBindingElement ();
  313. be.ProtectionTokenParameters =
  314. new SecureConversationSecurityTokenParameters (
  315. binding, requireCancellation, protectionRequirements);
  316. return be;
  317. }
  318. [MonoTODO]
  319. public static SymmetricSecurityBindingElement
  320. CreateSslNegotiationBindingElement (bool requireClientCertificate)
  321. {
  322. return CreateSslNegotiationBindingElement (
  323. requireClientCertificate, false);
  324. }
  325. [MonoTODO]
  326. public static SymmetricSecurityBindingElement
  327. CreateSslNegotiationBindingElement (
  328. bool requireClientCertificate,
  329. bool requireCancellation)
  330. {
  331. SymmetricSecurityBindingElement be = new SymmetricSecurityBindingElement ();
  332. be.ProtectionTokenParameters = new SslSecurityTokenParameters (requireClientCertificate, requireCancellation);
  333. return be;
  334. }
  335. [MonoTODO]
  336. public static SymmetricSecurityBindingElement
  337. CreateSspiNegotiationBindingElement ()
  338. {
  339. return CreateSspiNegotiationBindingElement (true);
  340. }
  341. [MonoTODO]
  342. public static SymmetricSecurityBindingElement
  343. CreateSspiNegotiationBindingElement (bool requireCancellation)
  344. {
  345. SymmetricSecurityBindingElement be = new SymmetricSecurityBindingElement ();
  346. be.ProtectionTokenParameters = CreateProtectionTokenParameters (false);
  347. return be;
  348. }
  349. public static TransportSecurityBindingElement
  350. CreateSspiNegotiationOverTransportBindingElement ()
  351. {
  352. return CreateSspiNegotiationOverTransportBindingElement (false);
  353. }
  354. [MonoTODO]
  355. public static TransportSecurityBindingElement
  356. CreateSspiNegotiationOverTransportBindingElement (bool requireCancellation)
  357. {
  358. throw new NotImplementedException ();
  359. }
  360. static X509SecurityTokenParameters CreateProtectionTokenParameters (bool cert)
  361. {
  362. X509SecurityTokenParameters p =
  363. new X509SecurityTokenParameters ();
  364. p.X509ReferenceStyle = X509KeyIdentifierClauseType.Thumbprint;
  365. if (cert)
  366. p.InclusionMode = SecurityTokenInclusionMode.Never;
  367. return p;
  368. }
  369. [MonoTODO]
  370. public static SymmetricSecurityBindingElement
  371. CreateUserNameForCertificateBindingElement ()
  372. {
  373. SymmetricSecurityBindingElement be = new SymmetricSecurityBindingElement ();
  374. be.ProtectionTokenParameters = CreateProtectionTokenParameters (true);
  375. UserNameSecurityTokenParameters utp =
  376. new UserNameSecurityTokenParameters ();
  377. be.EndpointSupportingTokenParameters.SignedEncrypted.Add (utp);
  378. return be;
  379. }
  380. [MonoTODO]
  381. public static SymmetricSecurityBindingElement
  382. CreateUserNameForSslBindingElement ()
  383. {
  384. return CreateUserNameForSslBindingElement (false);
  385. }
  386. [MonoTODO]
  387. public static SymmetricSecurityBindingElement
  388. CreateUserNameForSslBindingElement (bool requireCancellation)
  389. {
  390. SymmetricSecurityBindingElement be = new SymmetricSecurityBindingElement ();
  391. be.ProtectionTokenParameters = CreateProtectionTokenParameters (false);
  392. UserNameSecurityTokenParameters utp =
  393. new UserNameSecurityTokenParameters ();
  394. be.EndpointSupportingTokenParameters.SignedEncrypted.Add (utp);
  395. return be;
  396. }
  397. #endif
  398. [MonoTODO]
  399. public static TransportSecurityBindingElement
  400. CreateUserNameOverTransportBindingElement ()
  401. {
  402. var be = new TransportSecurityBindingElement ();
  403. be.EndpointSupportingTokenParameters.SignedEncrypted.Add (new UserNameSecurityTokenParameters ());
  404. return be;
  405. }
  406. #endregion
  407. #if !NET_2_1
  408. // It seems almost internal, hardcoded like this (I tried
  409. // custom parameters that sets IssuedTokenSecurityTokenParameters
  410. // like below ones, but that didn't trigger this method).
  411. protected static void SetIssuerBindingContextIfRequired (
  412. SecurityTokenParameters parameters,
  413. BindingContext issuerBindingContext)
  414. {
  415. if (parameters is IssuedSecurityTokenParameters ||
  416. parameters is SecureConversationSecurityTokenParameters ||
  417. parameters is SslSecurityTokenParameters ||
  418. parameters is SspiSecurityTokenParameters) {
  419. parameters.IssuerBindingContext = issuerBindingContext;
  420. }
  421. }
  422. #endif
  423. }
  424. }