SecurityBindingElement.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  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.ServiceModel.Channels.Security;
  35. using System.IdentityModel.Selectors;
  36. using System.IdentityModel.Tokens;
  37. using System.ServiceModel.Security.Tokens;
  38. #endif
  39. using System.Text;
  40. namespace System.ServiceModel.Channels
  41. {
  42. public abstract class SecurityBindingElement : BindingElement
  43. {
  44. internal SecurityBindingElement ()
  45. {
  46. #if !NET_2_1
  47. DefaultAlgorithmSuite = SecurityAlgorithmSuite.Default;
  48. MessageSecurityVersion = MessageSecurityVersion.Default;
  49. KeyEntropyMode = SecurityKeyEntropyMode.CombinedEntropy;
  50. endpoint = new SupportingTokenParameters ();
  51. operation = new Dictionary<string,SupportingTokenParameters> ();
  52. opt_endpoint = new SupportingTokenParameters ();
  53. opt_operation = new Dictionary<string,SupportingTokenParameters> ();
  54. service_settings = new LocalServiceSecuritySettings ();
  55. #endif
  56. IncludeTimestamp = true;
  57. LocalClientSettings = new LocalClientSecuritySettings ();
  58. }
  59. internal SecurityBindingElement (SecurityBindingElement other)
  60. {
  61. #if !NET_2_1
  62. alg_suite = other.alg_suite;
  63. key_entropy_mode = other.key_entropy_mode;
  64. security_header_layout = other.security_header_layout;
  65. msg_security_version = other.msg_security_version;
  66. endpoint = other.endpoint.Clone ();
  67. opt_endpoint = other.opt_endpoint.Clone ();
  68. operation = new Dictionary<string,SupportingTokenParameters> ();
  69. foreach (KeyValuePair<string,SupportingTokenParameters> p in other.operation)
  70. operation.Add (p.Key, p.Value.Clone ());
  71. opt_operation = new Dictionary<string,SupportingTokenParameters> ();
  72. foreach (KeyValuePair<string,SupportingTokenParameters> p in other.opt_operation)
  73. opt_operation.Add (p.Key, p.Value.Clone ());
  74. service_settings = other.service_settings.Clone ();
  75. #endif
  76. IncludeTimestamp = other.IncludeTimestamp;
  77. LocalClientSettings = other.LocalClientSettings.Clone ();
  78. }
  79. #if !NET_2_1
  80. SecurityAlgorithmSuite alg_suite;
  81. SecurityKeyEntropyMode key_entropy_mode;
  82. SecurityHeaderLayout security_header_layout;
  83. MessageSecurityVersion msg_security_version;
  84. SupportingTokenParameters endpoint, opt_endpoint;
  85. IDictionary<string,SupportingTokenParameters> operation, opt_operation;
  86. LocalServiceSecuritySettings service_settings;
  87. #endif
  88. public bool IncludeTimestamp { get; set; }
  89. public LocalClientSecuritySettings LocalClientSettings { get; private set; }
  90. #if !NET_2_1
  91. public SecurityAlgorithmSuite DefaultAlgorithmSuite {
  92. get { return alg_suite; }
  93. set { alg_suite = value; }
  94. }
  95. public SecurityKeyEntropyMode KeyEntropyMode {
  96. get { return key_entropy_mode; }
  97. set { key_entropy_mode = value; }
  98. }
  99. public LocalServiceSecuritySettings LocalServiceSettings {
  100. get { return service_settings; }
  101. }
  102. public SecurityHeaderLayout SecurityHeaderLayout {
  103. get { return security_header_layout; }
  104. set { security_header_layout = value; }
  105. }
  106. public MessageSecurityVersion MessageSecurityVersion {
  107. get { return msg_security_version; }
  108. set { msg_security_version = value; }
  109. }
  110. public SupportingTokenParameters EndpointSupportingTokenParameters {
  111. get { return endpoint; }
  112. }
  113. public IDictionary<string,SupportingTokenParameters> OperationSupportingTokenParameters {
  114. get { return operation; }
  115. }
  116. public SupportingTokenParameters OptionalEndpointSupportingTokenParameters {
  117. get { return opt_endpoint; }
  118. }
  119. public IDictionary<string,SupportingTokenParameters> OptionalOperationSupportingTokenParameters {
  120. get { return opt_operation; }
  121. }
  122. #endif
  123. [MonoTODO ("Implement for TransportSecurityBindingElement")]
  124. public override bool CanBuildChannelFactory<TChannel> (BindingContext context)
  125. {
  126. #if NET_2_1
  127. // not sure this should be like this, but there isn't Symmetric/Asymmetric elements in 2.1 anyways.
  128. return context.CanBuildInnerChannelFactory ();
  129. #else
  130. if (this is TransportSecurityBindingElement)
  131. throw new NotImplementedException ();
  132. var symm = this as SymmetricSecurityBindingElement;
  133. var asymm = this as AsymmetricSecurityBindingElement;
  134. var pt = symm != null ? symm.ProtectionTokenParameters : asymm != null ? asymm.InitiatorTokenParameters : null;
  135. if (pt == null)
  136. return false;
  137. var t = typeof (TChannel);
  138. var req = new InitiatorServiceModelSecurityTokenRequirement ();
  139. pt.InitializeSecurityTokenRequirement (req);
  140. object dummy;
  141. if (req.Properties.TryGetValue (ServiceModelSecurityTokenRequirement.IssuedSecurityTokenParametersProperty, out dummy) && dummy != null) {
  142. if (t == typeof (IRequestSessionChannel))
  143. return context.CanBuildInnerChannelFactory<IRequestChannel> () ||
  144. context.CanBuildInnerChannelFactory<IRequestSessionChannel> ();
  145. else if (t == typeof (IDuplexSessionChannel))
  146. return context.CanBuildInnerChannelFactory<IDuplexChannel> () ||
  147. context.CanBuildInnerChannelFactory<IDuplexSessionChannel> ();
  148. } else {
  149. if (t == typeof (IRequestChannel))
  150. return context.CanBuildInnerChannelFactory<IRequestChannel> () ||
  151. context.CanBuildInnerChannelFactory<IRequestSessionChannel> ();
  152. else if (t == typeof (IDuplexChannel))
  153. return context.CanBuildInnerChannelFactory<IDuplexChannel> () ||
  154. context.CanBuildInnerChannelFactory<IDuplexSessionChannel> ();
  155. }
  156. return false;
  157. #endif
  158. }
  159. public override IChannelFactory<TChannel> BuildChannelFactory<TChannel> (
  160. BindingContext context)
  161. {
  162. return BuildChannelFactoryCore<TChannel> (context);
  163. }
  164. protected abstract IChannelFactory<TChannel>
  165. BuildChannelFactoryCore<TChannel> (BindingContext context);
  166. #if !NET_2_1
  167. [MonoTODO ("Implement for TransportSecurityBindingElement")]
  168. public override bool CanBuildChannelListener<TChannel> (BindingContext context)
  169. {
  170. if (this is TransportSecurityBindingElement)
  171. throw new NotImplementedException ();
  172. var symm = this as SymmetricSecurityBindingElement;
  173. var asymm = this as AsymmetricSecurityBindingElement;
  174. var pt = symm != null ? symm.ProtectionTokenParameters : asymm != null ? asymm.RecipientTokenParameters : null;
  175. if (pt == null)
  176. return false;
  177. var t = typeof (TChannel);
  178. var req = new InitiatorServiceModelSecurityTokenRequirement ();
  179. pt.InitializeSecurityTokenRequirement (req);
  180. object dummy;
  181. if (req.Properties.TryGetValue (ServiceModelSecurityTokenRequirement.IssuedSecurityTokenParametersProperty, out dummy) && dummy != null) {
  182. if (t == typeof (IReplySessionChannel))
  183. return context.CanBuildInnerChannelListener<IReplyChannel> () ||
  184. context.CanBuildInnerChannelListener<IReplySessionChannel> ();
  185. else if (t == typeof (IDuplexSessionChannel))
  186. return context.CanBuildInnerChannelListener<IDuplexChannel> () ||
  187. context.CanBuildInnerChannelListener<IDuplexSessionChannel> ();
  188. } else {
  189. if (t == typeof (IReplyChannel))
  190. return context.CanBuildInnerChannelListener<IReplyChannel> () ||
  191. context.CanBuildInnerChannelListener<IReplySessionChannel> ();
  192. else if (t == typeof (IDuplexChannel))
  193. return context.CanBuildInnerChannelListener<IDuplexChannel> () ||
  194. context.CanBuildInnerChannelListener<IDuplexSessionChannel> ();
  195. }
  196. return false;
  197. }
  198. public override IChannelListener<TChannel> BuildChannelListener<TChannel> (
  199. BindingContext context)
  200. {
  201. return BuildChannelListenerCore<TChannel> (context);
  202. }
  203. protected abstract IChannelListener<TChannel>
  204. BuildChannelListenerCore<TChannel> (BindingContext context)
  205. where TChannel : class, IChannel;
  206. public override T GetProperty<T> (BindingContext context)
  207. {
  208. // It is documented that ISecurityCapabilities and IdentityVerifier can be returned.
  209. // Though, this class is not inheritable, and they are returned by the derived types.
  210. // So I don't care about them here.
  211. return context.GetInnerProperty<T> ();
  212. }
  213. public virtual void SetKeyDerivation (bool requireDerivedKeys)
  214. {
  215. endpoint.SetKeyDerivation (requireDerivedKeys);
  216. opt_endpoint.SetKeyDerivation (requireDerivedKeys);
  217. foreach (SupportingTokenParameters p in operation.Values)
  218. p.SetKeyDerivation (requireDerivedKeys);
  219. foreach (SupportingTokenParameters p in opt_operation.Values)
  220. p.SetKeyDerivation (requireDerivedKeys);
  221. }
  222. public override string ToString ()
  223. {
  224. var sb = new StringBuilder ();
  225. sb.Append (GetType ().FullName).Append (":\n");
  226. foreach (var pi in GetType ().GetProperties ()) {
  227. var simple = Type.GetTypeCode (pi.PropertyType) != TypeCode.Object;
  228. var val = pi.GetValue (this, null);
  229. sb.Append (pi.Name).Append (':');
  230. if (val != null)
  231. sb.AppendFormat ("{0}{1}{2}", simple ? " " : "\n", simple ? "" : " ", String.Join ("\n ", val.ToString ().Split ('\n')));
  232. sb.Append ('\n');
  233. }
  234. sb.Length--; // chop trailing EOL.
  235. return sb.ToString ();
  236. }
  237. #else
  238. [MonoTODO]
  239. public override T GetProperty<T> (BindingContext context)
  240. {
  241. return null;
  242. }
  243. #endif
  244. #region Factory methods
  245. #if !NET_2_1
  246. public static SymmetricSecurityBindingElement
  247. CreateAnonymousForCertificateBindingElement ()
  248. {
  249. SymmetricSecurityBindingElement be = new SymmetricSecurityBindingElement ();
  250. be.RequireSignatureConfirmation = true;
  251. be.ProtectionTokenParameters = CreateProtectionTokenParameters (true);
  252. return be;
  253. }
  254. public static TransportSecurityBindingElement
  255. CreateCertificateOverTransportBindingElement ()
  256. {
  257. return CreateCertificateOverTransportBindingElement (MessageSecurityVersion.Default);
  258. }
  259. public static TransportSecurityBindingElement
  260. CreateCertificateOverTransportBindingElement (MessageSecurityVersion version)
  261. {
  262. var be = new TransportSecurityBindingElement () { MessageSecurityVersion = version };
  263. be.EndpointSupportingTokenParameters.SignedEncrypted.Add (new X509SecurityTokenParameters ());
  264. return be;
  265. }
  266. [MonoTODO]
  267. public static AsymmetricSecurityBindingElement
  268. CreateCertificateSignatureBindingElement ()
  269. {
  270. throw new NotImplementedException ();
  271. }
  272. [MonoTODO]
  273. public static SymmetricSecurityBindingElement
  274. CreateIssuedTokenBindingElement (
  275. IssuedSecurityTokenParameters issuedTokenParameters)
  276. {
  277. SymmetricSecurityBindingElement be = new SymmetricSecurityBindingElement ();
  278. be.ProtectionTokenParameters = issuedTokenParameters;
  279. return be;
  280. }
  281. public static SymmetricSecurityBindingElement
  282. CreateIssuedTokenForCertificateBindingElement (
  283. IssuedSecurityTokenParameters issuedTokenParameters)
  284. {
  285. SymmetricSecurityBindingElement be = new SymmetricSecurityBindingElement ();
  286. be.RequireSignatureConfirmation = true;
  287. be.ProtectionTokenParameters = CreateProtectionTokenParameters (true);
  288. be.EndpointSupportingTokenParameters.Endorsing.Add (
  289. issuedTokenParameters);
  290. return be;
  291. }
  292. [MonoTODO]
  293. public static SymmetricSecurityBindingElement
  294. CreateIssuedTokenForSslBindingElement (
  295. IssuedSecurityTokenParameters issuedTokenParameters)
  296. {
  297. return CreateIssuedTokenForSslBindingElement (
  298. issuedTokenParameters, false);
  299. }
  300. [MonoTODO]
  301. public static SymmetricSecurityBindingElement
  302. CreateIssuedTokenForSslBindingElement (
  303. IssuedSecurityTokenParameters issuedTokenParameters,
  304. bool requireCancellation)
  305. {
  306. SymmetricSecurityBindingElement be = new SymmetricSecurityBindingElement ();
  307. be.RequireSignatureConfirmation = true;
  308. be.ProtectionTokenParameters = CreateProtectionTokenParameters (false);
  309. be.EndpointSupportingTokenParameters.Endorsing.Add (
  310. issuedTokenParameters);
  311. return be;
  312. }
  313. [MonoTODO]
  314. public static TransportSecurityBindingElement
  315. CreateIssuedTokenOverTransportBindingElement (
  316. IssuedSecurityTokenParameters issuedTokenParameters)
  317. {
  318. throw new NotImplementedException ();
  319. }
  320. [MonoTODO]
  321. public static SymmetricSecurityBindingElement CreateKerberosBindingElement ()
  322. {
  323. SymmetricSecurityBindingElement be = new SymmetricSecurityBindingElement ();
  324. be.DefaultAlgorithmSuite = SecurityAlgorithmSuite.Basic128;
  325. be.ProtectionTokenParameters = CreateProtectionTokenParameters (false);
  326. be.ProtectionTokenParameters.InclusionMode =
  327. SecurityTokenInclusionMode.Once;
  328. return be;
  329. }
  330. [MonoTODO]
  331. public static TransportSecurityBindingElement
  332. CreateKerberosOverTransportBindingElement ()
  333. {
  334. throw new NotImplementedException ();
  335. }
  336. [MonoTODO]
  337. public static SecurityBindingElement
  338. CreateMutualCertificateBindingElement ()
  339. {
  340. throw new NotImplementedException ();
  341. }
  342. [MonoTODO]
  343. public static SecurityBindingElement
  344. CreateMutualCertificateBindingElement (MessageSecurityVersion version)
  345. {
  346. throw new NotImplementedException ();
  347. }
  348. [MonoTODO]
  349. public static SecurityBindingElement
  350. CreateMutualCertificateBindingElement (
  351. MessageSecurityVersion version,
  352. bool allowSerializedSigningTokenOnReply)
  353. {
  354. throw new NotImplementedException ();
  355. }
  356. [MonoTODO]
  357. public static AsymmetricSecurityBindingElement
  358. CreateMutualCertificateDuplexBindingElement ()
  359. {
  360. throw new NotImplementedException ();
  361. }
  362. [MonoTODO]
  363. public static AsymmetricSecurityBindingElement
  364. CreateMutualCertificateDuplexBindingElement (
  365. MessageSecurityVersion version)
  366. {
  367. throw new NotImplementedException ();
  368. }
  369. public static SecurityBindingElement
  370. CreateSecureConversationBindingElement (SecurityBindingElement binding)
  371. {
  372. return CreateSecureConversationBindingElement (binding, false);
  373. }
  374. public static SecurityBindingElement
  375. CreateSecureConversationBindingElement (
  376. SecurityBindingElement binding, bool requireCancellation)
  377. {
  378. return CreateSecureConversationBindingElement (binding, requireCancellation, null);
  379. }
  380. public static SecurityBindingElement
  381. CreateSecureConversationBindingElement (
  382. SecurityBindingElement binding, bool requireCancellation,
  383. ChannelProtectionRequirements protectionRequirements)
  384. {
  385. SymmetricSecurityBindingElement be =
  386. new SymmetricSecurityBindingElement ();
  387. be.ProtectionTokenParameters =
  388. new SecureConversationSecurityTokenParameters (
  389. binding, requireCancellation, protectionRequirements);
  390. return be;
  391. }
  392. public static SymmetricSecurityBindingElement
  393. CreateSslNegotiationBindingElement (bool requireClientCertificate)
  394. {
  395. return CreateSslNegotiationBindingElement (
  396. requireClientCertificate, false);
  397. }
  398. public static SymmetricSecurityBindingElement
  399. CreateSslNegotiationBindingElement (
  400. bool requireClientCertificate,
  401. bool requireCancellation)
  402. {
  403. SymmetricSecurityBindingElement be = new SymmetricSecurityBindingElement ();
  404. be.ProtectionTokenParameters = new SslSecurityTokenParameters (requireClientCertificate, requireCancellation);
  405. return be;
  406. }
  407. public static SymmetricSecurityBindingElement
  408. CreateSspiNegotiationBindingElement ()
  409. {
  410. return CreateSspiNegotiationBindingElement (true);
  411. }
  412. public static SymmetricSecurityBindingElement
  413. CreateSspiNegotiationBindingElement (bool requireCancellation)
  414. {
  415. SymmetricSecurityBindingElement be = new SymmetricSecurityBindingElement ();
  416. be.ProtectionTokenParameters = CreateProtectionTokenParameters (false);
  417. return be;
  418. }
  419. public static TransportSecurityBindingElement
  420. CreateSspiNegotiationOverTransportBindingElement ()
  421. {
  422. return CreateSspiNegotiationOverTransportBindingElement (false);
  423. }
  424. [MonoTODO]
  425. public static TransportSecurityBindingElement
  426. CreateSspiNegotiationOverTransportBindingElement (bool requireCancellation)
  427. {
  428. throw new NotImplementedException ();
  429. }
  430. static X509SecurityTokenParameters CreateProtectionTokenParameters (bool cert)
  431. {
  432. X509SecurityTokenParameters p =
  433. new X509SecurityTokenParameters ();
  434. p.X509ReferenceStyle = X509KeyIdentifierClauseType.Thumbprint;
  435. if (cert)
  436. p.InclusionMode = SecurityTokenInclusionMode.Never;
  437. return p;
  438. }
  439. public static SymmetricSecurityBindingElement
  440. CreateUserNameForCertificateBindingElement ()
  441. {
  442. SymmetricSecurityBindingElement be = new SymmetricSecurityBindingElement ();
  443. be.ProtectionTokenParameters = CreateProtectionTokenParameters (true);
  444. UserNameSecurityTokenParameters utp =
  445. new UserNameSecurityTokenParameters ();
  446. be.EndpointSupportingTokenParameters.SignedEncrypted.Add (utp);
  447. return be;
  448. }
  449. public static SymmetricSecurityBindingElement
  450. CreateUserNameForSslBindingElement ()
  451. {
  452. return CreateUserNameForSslBindingElement (false);
  453. }
  454. public static SymmetricSecurityBindingElement
  455. CreateUserNameForSslBindingElement (bool requireCancellation)
  456. {
  457. SymmetricSecurityBindingElement be = new SymmetricSecurityBindingElement ();
  458. be.ProtectionTokenParameters = CreateProtectionTokenParameters (false);
  459. UserNameSecurityTokenParameters utp =
  460. new UserNameSecurityTokenParameters ();
  461. be.EndpointSupportingTokenParameters.SignedEncrypted.Add (utp);
  462. return be;
  463. }
  464. #endif
  465. [MonoTODO]
  466. public static TransportSecurityBindingElement
  467. CreateUserNameOverTransportBindingElement ()
  468. {
  469. var be = new TransportSecurityBindingElement ();
  470. #if !NET_2_1 // FIXME: there should be whatever else to do for 2.1 instead.
  471. be.EndpointSupportingTokenParameters.SignedEncrypted.Add (new UserNameSecurityTokenParameters ());
  472. #endif
  473. return be;
  474. }
  475. #endregion
  476. #if !NET_2_1
  477. // It seems almost internal, hardcoded like this (I tried
  478. // custom parameters that sets IssuedTokenSecurityTokenParameters
  479. // like below ones, but that didn't trigger this method).
  480. protected static void SetIssuerBindingContextIfRequired (
  481. SecurityTokenParameters parameters,
  482. BindingContext issuerBindingContext)
  483. {
  484. if (parameters is IssuedSecurityTokenParameters ||
  485. parameters is SecureConversationSecurityTokenParameters ||
  486. parameters is SslSecurityTokenParameters ||
  487. parameters is SspiSecurityTokenParameters) {
  488. parameters.IssuerBindingContext = issuerBindingContext;
  489. }
  490. }
  491. #endif
  492. }
  493. }