SecurityBindingElement.cs 15 KB

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