SecurityBindingElement.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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 ("It supports only IRequestSessionChannel")]
  124. public override bool CanBuildChannelFactory<TChannel> (BindingContext context)
  125. {
  126. return context.CanBuildInnerChannelFactory<TChannel> ();
  127. }
  128. public override IChannelFactory<TChannel> BuildChannelFactory<TChannel> (
  129. BindingContext context)
  130. {
  131. return BuildChannelFactoryCore<TChannel> (context);
  132. }
  133. protected abstract IChannelFactory<TChannel>
  134. BuildChannelFactoryCore<TChannel> (BindingContext context);
  135. #if !NET_2_1
  136. [MonoTODO ("It probably supports only IReplySessionChannel")]
  137. public override bool CanBuildChannelListener<TChannel> (BindingContext context)
  138. {
  139. return context.CanBuildInnerChannelListener<TChannel> ();
  140. }
  141. public override IChannelListener<TChannel> BuildChannelListener<TChannel> (
  142. BindingContext context)
  143. {
  144. return BuildChannelListenerCore<TChannel> (context);
  145. }
  146. protected abstract IChannelListener<TChannel>
  147. BuildChannelListenerCore<TChannel> (BindingContext context)
  148. where TChannel : class, IChannel;
  149. public virtual void SetKeyDerivation (bool requireDerivedKeys)
  150. {
  151. endpoint.SetKeyDerivation (requireDerivedKeys);
  152. opt_endpoint.SetKeyDerivation (requireDerivedKeys);
  153. foreach (SupportingTokenParameters p in operation.Values)
  154. p.SetKeyDerivation (requireDerivedKeys);
  155. foreach (SupportingTokenParameters p in opt_operation.Values)
  156. p.SetKeyDerivation (requireDerivedKeys);
  157. }
  158. public override string ToString ()
  159. {
  160. var sb = new StringBuilder ();
  161. sb.Append (GetType ().FullName).Append (":\n");
  162. foreach (var pi in GetType ().GetProperties ()) {
  163. var simple = Type.GetTypeCode (pi.PropertyType) != TypeCode.Object;
  164. var val = pi.GetValue (this, null);
  165. sb.Append (pi.Name).Append (':');
  166. if (val != null)
  167. sb.AppendFormat ("{0}{1}{2}", simple ? " " : "\n", simple ? "" : " ", String.Join ("\n ", val.ToString ().Split ('\n')));
  168. sb.Append ('\n');
  169. }
  170. sb.Length--; // chop trailing EOL.
  171. return sb.ToString ();
  172. }
  173. #else
  174. [MonoTODO]
  175. public override T GetProperty<T> (BindingContext context)
  176. {
  177. return null;
  178. }
  179. #endif
  180. #region Factory methods
  181. #if !NET_2_1
  182. public static SymmetricSecurityBindingElement
  183. CreateAnonymousForCertificateBindingElement ()
  184. {
  185. SymmetricSecurityBindingElement be = new SymmetricSecurityBindingElement ();
  186. be.RequireSignatureConfirmation = true;
  187. be.ProtectionTokenParameters = CreateProtectionTokenParameters (true);
  188. return be;
  189. }
  190. public static TransportSecurityBindingElement
  191. CreateCertificateOverTransportBindingElement ()
  192. {
  193. return CreateCertificateOverTransportBindingElement (MessageSecurityVersion.Default);
  194. }
  195. public static TransportSecurityBindingElement
  196. CreateCertificateOverTransportBindingElement (MessageSecurityVersion version)
  197. {
  198. var be = new TransportSecurityBindingElement () { MessageSecurityVersion = version };
  199. be.EndpointSupportingTokenParameters.SignedEncrypted.Add (new X509SecurityTokenParameters ());
  200. return be;
  201. }
  202. [MonoTODO]
  203. public static AsymmetricSecurityBindingElement
  204. CreateCertificateSignatureBindingElement ()
  205. {
  206. throw new NotImplementedException ();
  207. }
  208. [MonoTODO]
  209. public static SymmetricSecurityBindingElement
  210. CreateIssuedTokenBindingElement (
  211. IssuedSecurityTokenParameters issuedTokenParameters)
  212. {
  213. SymmetricSecurityBindingElement be = new SymmetricSecurityBindingElement ();
  214. be.ProtectionTokenParameters = issuedTokenParameters;
  215. return be;
  216. }
  217. public static SymmetricSecurityBindingElement
  218. CreateIssuedTokenForCertificateBindingElement (
  219. IssuedSecurityTokenParameters issuedTokenParameters)
  220. {
  221. SymmetricSecurityBindingElement be = new SymmetricSecurityBindingElement ();
  222. be.RequireSignatureConfirmation = true;
  223. be.ProtectionTokenParameters = CreateProtectionTokenParameters (true);
  224. be.EndpointSupportingTokenParameters.Endorsing.Add (
  225. issuedTokenParameters);
  226. return be;
  227. }
  228. [MonoTODO]
  229. public static SymmetricSecurityBindingElement
  230. CreateIssuedTokenForSslBindingElement (
  231. IssuedSecurityTokenParameters issuedTokenParameters)
  232. {
  233. return CreateIssuedTokenForSslBindingElement (
  234. issuedTokenParameters, false);
  235. }
  236. [MonoTODO]
  237. public static SymmetricSecurityBindingElement
  238. CreateIssuedTokenForSslBindingElement (
  239. IssuedSecurityTokenParameters issuedTokenParameters,
  240. bool requireCancellation)
  241. {
  242. SymmetricSecurityBindingElement be = new SymmetricSecurityBindingElement ();
  243. be.RequireSignatureConfirmation = true;
  244. be.ProtectionTokenParameters = CreateProtectionTokenParameters (false);
  245. be.EndpointSupportingTokenParameters.Endorsing.Add (
  246. issuedTokenParameters);
  247. return be;
  248. }
  249. [MonoTODO]
  250. public static TransportSecurityBindingElement
  251. CreateIssuedTokenOverTransportBindingElement (
  252. IssuedSecurityTokenParameters issuedTokenParameters)
  253. {
  254. throw new NotImplementedException ();
  255. }
  256. [MonoTODO]
  257. public static SymmetricSecurityBindingElement CreateKerberosBindingElement ()
  258. {
  259. SymmetricSecurityBindingElement be = new SymmetricSecurityBindingElement ();
  260. be.DefaultAlgorithmSuite = SecurityAlgorithmSuite.Basic128;
  261. be.ProtectionTokenParameters = CreateProtectionTokenParameters (false);
  262. be.ProtectionTokenParameters.InclusionMode =
  263. SecurityTokenInclusionMode.Once;
  264. return be;
  265. }
  266. [MonoTODO]
  267. public static TransportSecurityBindingElement
  268. CreateKerberosOverTransportBindingElement ()
  269. {
  270. throw new NotImplementedException ();
  271. }
  272. [MonoTODO]
  273. public static SecurityBindingElement
  274. CreateMutualCertificateBindingElement ()
  275. {
  276. throw new NotImplementedException ();
  277. }
  278. [MonoTODO]
  279. public static SecurityBindingElement
  280. CreateMutualCertificateBindingElement (MessageSecurityVersion version)
  281. {
  282. throw new NotImplementedException ();
  283. }
  284. [MonoTODO]
  285. public static SecurityBindingElement
  286. CreateMutualCertificateBindingElement (
  287. MessageSecurityVersion version,
  288. bool allowSerializedSigningTokenOnReply)
  289. {
  290. throw new NotImplementedException ();
  291. }
  292. [MonoTODO]
  293. public static AsymmetricSecurityBindingElement
  294. CreateMutualCertificateDuplexBindingElement ()
  295. {
  296. throw new NotImplementedException ();
  297. }
  298. [MonoTODO]
  299. public static AsymmetricSecurityBindingElement
  300. CreateMutualCertificateDuplexBindingElement (
  301. MessageSecurityVersion version)
  302. {
  303. throw new NotImplementedException ();
  304. }
  305. public static SecurityBindingElement
  306. CreateSecureConversationBindingElement (SecurityBindingElement binding)
  307. {
  308. return CreateSecureConversationBindingElement (binding, false);
  309. }
  310. public static SecurityBindingElement
  311. CreateSecureConversationBindingElement (
  312. SecurityBindingElement binding, bool requireCancellation)
  313. {
  314. return CreateSecureConversationBindingElement (binding, requireCancellation, null);
  315. }
  316. public static SecurityBindingElement
  317. CreateSecureConversationBindingElement (
  318. SecurityBindingElement binding, bool requireCancellation,
  319. ChannelProtectionRequirements protectionRequirements)
  320. {
  321. SymmetricSecurityBindingElement be =
  322. new SymmetricSecurityBindingElement ();
  323. be.ProtectionTokenParameters =
  324. new SecureConversationSecurityTokenParameters (
  325. binding, requireCancellation, protectionRequirements);
  326. return be;
  327. }
  328. public static SymmetricSecurityBindingElement
  329. CreateSslNegotiationBindingElement (bool requireClientCertificate)
  330. {
  331. return CreateSslNegotiationBindingElement (
  332. requireClientCertificate, false);
  333. }
  334. public static SymmetricSecurityBindingElement
  335. CreateSslNegotiationBindingElement (
  336. bool requireClientCertificate,
  337. bool requireCancellation)
  338. {
  339. SymmetricSecurityBindingElement be = new SymmetricSecurityBindingElement ();
  340. be.ProtectionTokenParameters = new SslSecurityTokenParameters (requireClientCertificate, requireCancellation);
  341. return be;
  342. }
  343. public static SymmetricSecurityBindingElement
  344. CreateSspiNegotiationBindingElement ()
  345. {
  346. return CreateSspiNegotiationBindingElement (true);
  347. }
  348. public static SymmetricSecurityBindingElement
  349. CreateSspiNegotiationBindingElement (bool requireCancellation)
  350. {
  351. SymmetricSecurityBindingElement be = new SymmetricSecurityBindingElement ();
  352. be.ProtectionTokenParameters = CreateProtectionTokenParameters (false);
  353. return be;
  354. }
  355. public static TransportSecurityBindingElement
  356. CreateSspiNegotiationOverTransportBindingElement ()
  357. {
  358. return CreateSspiNegotiationOverTransportBindingElement (false);
  359. }
  360. [MonoTODO]
  361. public static TransportSecurityBindingElement
  362. CreateSspiNegotiationOverTransportBindingElement (bool requireCancellation)
  363. {
  364. throw new NotImplementedException ();
  365. }
  366. static X509SecurityTokenParameters CreateProtectionTokenParameters (bool cert)
  367. {
  368. X509SecurityTokenParameters p =
  369. new X509SecurityTokenParameters ();
  370. p.X509ReferenceStyle = X509KeyIdentifierClauseType.Thumbprint;
  371. if (cert)
  372. p.InclusionMode = SecurityTokenInclusionMode.Never;
  373. return p;
  374. }
  375. public static SymmetricSecurityBindingElement
  376. CreateUserNameForCertificateBindingElement ()
  377. {
  378. SymmetricSecurityBindingElement be = new SymmetricSecurityBindingElement ();
  379. be.ProtectionTokenParameters = CreateProtectionTokenParameters (true);
  380. UserNameSecurityTokenParameters utp =
  381. new UserNameSecurityTokenParameters ();
  382. be.EndpointSupportingTokenParameters.SignedEncrypted.Add (utp);
  383. return be;
  384. }
  385. public static SymmetricSecurityBindingElement
  386. CreateUserNameForSslBindingElement ()
  387. {
  388. return CreateUserNameForSslBindingElement (false);
  389. }
  390. public static SymmetricSecurityBindingElement
  391. CreateUserNameForSslBindingElement (bool requireCancellation)
  392. {
  393. SymmetricSecurityBindingElement be = new SymmetricSecurityBindingElement ();
  394. be.ProtectionTokenParameters = CreateProtectionTokenParameters (false);
  395. UserNameSecurityTokenParameters utp =
  396. new UserNameSecurityTokenParameters ();
  397. be.EndpointSupportingTokenParameters.SignedEncrypted.Add (utp);
  398. return be;
  399. }
  400. #endif
  401. [MonoTODO]
  402. public static TransportSecurityBindingElement
  403. CreateUserNameOverTransportBindingElement ()
  404. {
  405. var be = new TransportSecurityBindingElement ();
  406. #if !NET_2_1 // FIXME: there should be whatever else to do for 2.1 instead.
  407. be.EndpointSupportingTokenParameters.SignedEncrypted.Add (new UserNameSecurityTokenParameters ());
  408. #endif
  409. return be;
  410. }
  411. #endregion
  412. #if !NET_2_1
  413. // It seems almost internal, hardcoded like this (I tried
  414. // custom parameters that sets IssuedTokenSecurityTokenParameters
  415. // like below ones, but that didn't trigger this method).
  416. protected static void SetIssuerBindingContextIfRequired (
  417. SecurityTokenParameters parameters,
  418. BindingContext issuerBindingContext)
  419. {
  420. if (parameters is IssuedSecurityTokenParameters ||
  421. parameters is SecureConversationSecurityTokenParameters ||
  422. parameters is SslSecurityTokenParameters ||
  423. parameters is SspiSecurityTokenParameters) {
  424. parameters.IssuerBindingContext = issuerBindingContext;
  425. }
  426. }
  427. #endif
  428. }
  429. }