SecurityBindingElementTest.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  1. //
  2. // SecurityBindingElementTest.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.Collections.Generic;
  30. using System.Collections.ObjectModel;
  31. using System.Net;
  32. using System.Net.Security;
  33. using System.Security.Cryptography.X509Certificates;
  34. using System.IdentityModel.Selectors;
  35. using System.IdentityModel.Tokens;
  36. using System.ServiceModel;
  37. using System.ServiceModel.Channels;
  38. using System.ServiceModel.Description;
  39. using System.ServiceModel.Security;
  40. using System.ServiceModel.Security.Tokens;
  41. using System.Xml;
  42. using NUnit.Framework;
  43. namespace MonoTests.System.ServiceModel.Channels
  44. {
  45. [TestFixture]
  46. public class SecurityBindingElementTest
  47. {
  48. #region Factory methods
  49. [Test]
  50. public void CreateAnonymousForCertificateBindingElement ()
  51. {
  52. SymmetricSecurityBindingElement be =
  53. SecurityBindingElement.CreateAnonymousForCertificateBindingElement ();
  54. SecurityAssert.AssertSymmetricSecurityBindingElement (
  55. SecurityAlgorithmSuite.Default,
  56. true, // IncludeTimestamp
  57. SecurityKeyEntropyMode.CombinedEntropy,
  58. MessageProtectionOrder.SignBeforeEncryptAndEncryptSignature,
  59. MessageSecurityVersion.Default,
  60. true, // RequireSignatureConfirmation
  61. SecurityHeaderLayout.Strict,
  62. // EndpointSupportingTokenParameters: endorsing, signed, signedEncrypted, signedEndorsing (by count)
  63. 0, 0, 0, 0,
  64. // ProtectionTokenParameters
  65. true, SecurityTokenInclusionMode.Never, SecurityTokenReferenceStyle.Internal, true,
  66. // LocalClientSettings
  67. true, 60, true,
  68. be, "");
  69. // test ProtectionTokenParameters
  70. X509SecurityTokenParameters tp =
  71. be.ProtectionTokenParameters
  72. as X509SecurityTokenParameters;
  73. Assert.IsNotNull (tp, "#2-1");
  74. SecurityAssert.AssertSecurityTokenParameters (
  75. SecurityTokenInclusionMode.Never,
  76. SecurityTokenReferenceStyle.Internal,
  77. true, tp, "Protection");
  78. Assert.AreEqual (X509KeyIdentifierClauseType.Thumbprint, tp.X509ReferenceStyle, "#2-2");
  79. }
  80. [Test]
  81. public void CreateIssuedTokenBindingElement1 ()
  82. {
  83. IssuedSecurityTokenParameters tp =
  84. new IssuedSecurityTokenParameters ();
  85. SymmetricSecurityBindingElement be =
  86. SecurityBindingElement.CreateIssuedTokenBindingElement (tp);
  87. SecurityAssert.AssertSymmetricSecurityBindingElement (
  88. SecurityAlgorithmSuite.Default,
  89. true, // IncludeTimestamp
  90. SecurityKeyEntropyMode.CombinedEntropy,
  91. MessageProtectionOrder.SignBeforeEncryptAndEncryptSignature,
  92. MessageSecurityVersion.Default,
  93. false, // RequireSignatureConfirmation
  94. SecurityHeaderLayout.Strict,
  95. // EndpointSupportingTokenParameters: endorsing, signed, signedEncrypted, signedEndorsing (by count)
  96. 0, 0, 0, 0,
  97. // ProtectionTokenParameters
  98. true, SecurityTokenInclusionMode.AlwaysToRecipient, SecurityTokenReferenceStyle.Internal, true,
  99. // LocalClientSettings
  100. true, 60, true,
  101. be, "");
  102. // test ProtectionTokenParameters
  103. Assert.AreEqual (tp, be.ProtectionTokenParameters, "#2-1");
  104. SecurityAssert.AssertSecurityTokenParameters (
  105. SecurityTokenInclusionMode.AlwaysToRecipient,
  106. SecurityTokenReferenceStyle.Internal,
  107. true, tp, "Protection");
  108. }
  109. [Test]
  110. public void CreateIssuedTokenForCertificateBindingElement1 ()
  111. {
  112. IssuedSecurityTokenParameters tp =
  113. new IssuedSecurityTokenParameters ();
  114. SymmetricSecurityBindingElement be =
  115. SecurityBindingElement.CreateIssuedTokenForCertificateBindingElement (tp);
  116. SecurityAssert.AssertSymmetricSecurityBindingElement (
  117. SecurityAlgorithmSuite.Default,
  118. true, // IncludeTimestamp
  119. SecurityKeyEntropyMode.CombinedEntropy,
  120. MessageProtectionOrder.SignBeforeEncryptAndEncryptSignature,
  121. MessageSecurityVersion.Default,
  122. true, // RequireSignatureConfirmation
  123. SecurityHeaderLayout.Strict,
  124. // EndpointSupportingTokenParameters: endorsing, signed, signedEncrypted, signedEndorsing (by count)
  125. 1, 0, 0, 0,
  126. // ProtectionTokenParameters
  127. true, SecurityTokenInclusionMode.Never, SecurityTokenReferenceStyle.Internal, true,
  128. // LocalClientSettings
  129. true, 60, true,
  130. be, "");
  131. // test ProtectionTokenParameters
  132. X509SecurityTokenParameters ptp =
  133. be.ProtectionTokenParameters
  134. as X509SecurityTokenParameters;
  135. Assert.IsNotNull (ptp, "#2-1");
  136. SecurityAssert.AssertSecurityTokenParameters (
  137. SecurityTokenInclusionMode.Never,
  138. SecurityTokenReferenceStyle.Internal,
  139. true, ptp, "Protection");
  140. Assert.AreEqual (X509KeyIdentifierClauseType.Thumbprint, ptp.X509ReferenceStyle, "#2-2");
  141. Assert.AreEqual (tp, be.EndpointSupportingTokenParameters.Endorsing [0], "EndpointParams.Endorsing[0]");
  142. }
  143. [Test]
  144. public void CreateIssuedTokenForSslBindingElement1 ()
  145. {
  146. IssuedSecurityTokenParameters tp =
  147. new IssuedSecurityTokenParameters ();
  148. SymmetricSecurityBindingElement be =
  149. SecurityBindingElement.CreateIssuedTokenForSslBindingElement (tp);
  150. SecurityAssert.AssertSymmetricSecurityBindingElement (
  151. SecurityAlgorithmSuite.Default,
  152. true, // IncludeTimestamp
  153. SecurityKeyEntropyMode.CombinedEntropy,
  154. MessageProtectionOrder.SignBeforeEncryptAndEncryptSignature,
  155. MessageSecurityVersion.Default,
  156. true, // RequireSignatureConfirmation
  157. SecurityHeaderLayout.Strict,
  158. // EndpointSupportingTokenParameters: endorsing, signed, signedEncrypted, signedEndorsing (by count)
  159. 1, 0, 0, 0,
  160. // ProtectionTokenParameters
  161. true, SecurityTokenInclusionMode.AlwaysToRecipient, SecurityTokenReferenceStyle.Internal, true,
  162. // LocalClientSettings
  163. true, 60, true,
  164. be, "");
  165. Assert.AreEqual (tp, be.EndpointSupportingTokenParameters.Endorsing [0], "EndpointParams.Endorsing[0]");
  166. // FIXME: test ProtectionTokenParameters
  167. }
  168. [Test]
  169. public void CreateKerberosBindingElement ()
  170. {
  171. SymmetricSecurityBindingElement be =
  172. SecurityBindingElement.CreateKerberosBindingElement ();
  173. SecurityAssert.AssertSymmetricSecurityBindingElement (
  174. SecurityAlgorithmSuite.Basic128,
  175. true, // IncludeTimestamp
  176. SecurityKeyEntropyMode.CombinedEntropy,
  177. MessageProtectionOrder.SignBeforeEncryptAndEncryptSignature,
  178. MessageSecurityVersion.Default,
  179. false, // RequireSignatureConfirmation
  180. SecurityHeaderLayout.Strict,
  181. // EndpointSupportingTokenParameters: endorsing, signed, signedEncrypted, signedEndorsing (by count)
  182. 0, 0, 0, 0,
  183. // ProtectionTokenParameters
  184. true, SecurityTokenInclusionMode.Once, SecurityTokenReferenceStyle.Internal, true,
  185. // LocalClientSettings
  186. true, 60, true,
  187. be, "");
  188. // FIXME: test ProtectionTokenParameters
  189. }
  190. [Test]
  191. public void CreateSslNegotiationBindingElement ()
  192. {
  193. SymmetricSecurityBindingElement be =
  194. SecurityBindingElement.CreateSslNegotiationBindingElement (true, true);
  195. SecurityAssert.AssertSymmetricSecurityBindingElement (
  196. SecurityAlgorithmSuite.Default,
  197. true, // IncludeTimestamp
  198. SecurityKeyEntropyMode.CombinedEntropy,
  199. MessageProtectionOrder.SignBeforeEncryptAndEncryptSignature,
  200. MessageSecurityVersion.Default,
  201. false, // RequireSignatureConfirmation
  202. SecurityHeaderLayout.Strict,
  203. // EndpointSupportingTokenParameters: endorsing, signed, signedEncrypted, signedEndorsing (by count)
  204. 0, 0, 0, 0,
  205. // ProtectionTokenParameters
  206. true, SecurityTokenInclusionMode.AlwaysToRecipient, SecurityTokenReferenceStyle.Internal, true,
  207. // LocalClientSettings
  208. true, 60, true,
  209. be, "");
  210. // FIXME: also try different constructor arguments
  211. // test ProtectionTokenParameters
  212. Assert.AreEqual (typeof (SslSecurityTokenParameters), be.ProtectionTokenParameters.GetType (), "#1");
  213. SslSecurityTokenParameters sp = be.ProtectionTokenParameters as SslSecurityTokenParameters;
  214. Assert.AreEqual (true, sp.RequireCancellation, "#2");
  215. Assert.AreEqual (true, sp.RequireClientCertificate, "#3");
  216. }
  217. [Test]
  218. public void CreateSspiNegotiationBindingElement ()
  219. {
  220. SymmetricSecurityBindingElement be =
  221. SecurityBindingElement.CreateSspiNegotiationBindingElement ();
  222. SecurityAssert.AssertSymmetricSecurityBindingElement (
  223. SecurityAlgorithmSuite.Default,
  224. true, // IncludeTimestamp
  225. SecurityKeyEntropyMode.CombinedEntropy,
  226. MessageProtectionOrder.SignBeforeEncryptAndEncryptSignature,
  227. MessageSecurityVersion.Default,
  228. false, // RequireSignatureConfirmation
  229. SecurityHeaderLayout.Strict,
  230. // EndpointSupportingTokenParameters: endorsing, signed, signedEncrypted, signedEndorsing (by count)
  231. 0, 0, 0, 0,
  232. // ProtectionTokenParameters
  233. true, SecurityTokenInclusionMode.AlwaysToRecipient, SecurityTokenReferenceStyle.Internal, true,
  234. // LocalClientSettings
  235. true, 60, true,
  236. be, "");
  237. // FIXME: Try boolean argument as well.
  238. // FIXME: test ProtectionTokenParameters
  239. }
  240. [Test]
  241. public void CreateUserNameForCertificateBindingElement ()
  242. {
  243. SymmetricSecurityBindingElement be =
  244. SecurityBindingElement.CreateUserNameForCertificateBindingElement ();
  245. SecurityAssert.AssertSymmetricSecurityBindingElement (
  246. SecurityAlgorithmSuite.Default,
  247. true, // IncludeTimestamp
  248. SecurityKeyEntropyMode.CombinedEntropy,
  249. MessageProtectionOrder.SignBeforeEncryptAndEncryptSignature,
  250. MessageSecurityVersion.Default,
  251. false, // RequireSignatureConfirmation
  252. SecurityHeaderLayout.Strict,
  253. // EndpointSupportingTokenParameters: endorsing, signed, signedEncrypted, signedEndorsing (by count)
  254. 0, 0, 1, 0,
  255. // ProtectionTokenParameters
  256. true, SecurityTokenInclusionMode.Never, SecurityTokenReferenceStyle.Internal, true,
  257. // LocalClientSettings
  258. true, 60, true,
  259. be, "");
  260. UserNameSecurityTokenParameters up =
  261. be.EndpointSupportingTokenParameters.SignedEncrypted [0] as UserNameSecurityTokenParameters;
  262. // FIXME: test it
  263. // FIXME: test ProtectionTokenParameters
  264. }
  265. [Test]
  266. public void CreateUserNameForSslBindingElement ()
  267. {
  268. SymmetricSecurityBindingElement be =
  269. SecurityBindingElement.CreateUserNameForSslBindingElement ();
  270. SecurityAssert.AssertSymmetricSecurityBindingElement (
  271. SecurityAlgorithmSuite.Default,
  272. true, // IncludeTimestamp
  273. SecurityKeyEntropyMode.CombinedEntropy,
  274. MessageProtectionOrder.SignBeforeEncryptAndEncryptSignature,
  275. MessageSecurityVersion.Default,
  276. false, // RequireSignatureConfirmation
  277. SecurityHeaderLayout.Strict,
  278. // EndpointSupportingTokenParameters: endorsing, signed, signedEncrypted, signedEndorsing (by count)
  279. 0, 0, 1, 0,
  280. // ProtectionTokenParameters
  281. true, SecurityTokenInclusionMode.AlwaysToRecipient, SecurityTokenReferenceStyle.Internal, true,
  282. // LocalClientSettings
  283. true, 60, true,
  284. be, "");
  285. UserNameSecurityTokenParameters up =
  286. be.EndpointSupportingTokenParameters.SignedEncrypted [0] as UserNameSecurityTokenParameters;
  287. // FIXME: test it
  288. // FIXME: test ProtectionTokenParameters
  289. }
  290. // non-symmetric return value by definition, but still
  291. // returns symmetric binding elements.
  292. [Test]
  293. public void CreateSecureConversationBindingElement ()
  294. {
  295. SymmetricSecurityBindingElement be =
  296. SecurityBindingElement.CreateSecureConversationBindingElement (new SymmetricSecurityBindingElement ())
  297. as SymmetricSecurityBindingElement;
  298. SecurityAssert.AssertSymmetricSecurityBindingElement (
  299. SecurityAlgorithmSuite.Default,
  300. true, // IncludeTimestamp
  301. SecurityKeyEntropyMode.CombinedEntropy,
  302. MessageProtectionOrder.SignBeforeEncryptAndEncryptSignature,
  303. MessageSecurityVersion.Default,
  304. false, // RequireSignatureConfirmation
  305. SecurityHeaderLayout.Strict,
  306. // EndpointSupportingTokenParameters: endorsing, signed, signedEncrypted, signedEndorsing (by count)
  307. 0, 0, 0, 0,
  308. // ProtectionTokenParameters
  309. true, SecurityTokenInclusionMode.AlwaysToRecipient, SecurityTokenReferenceStyle.Internal, true,
  310. // LocalClientSettings
  311. true, 60, true,
  312. be, "");
  313. // test ProtectionTokenParameters
  314. SecureConversationSecurityTokenParameters tp =
  315. be.ProtectionTokenParameters as SecureConversationSecurityTokenParameters;
  316. Assert.IsNotNull (tp, "#2-1");
  317. SecurityAssert.AssertSecurityTokenParameters (
  318. SecurityTokenInclusionMode.AlwaysToRecipient,
  319. SecurityTokenReferenceStyle.Internal,
  320. true, tp, "Protection");
  321. }
  322. #endregion
  323. [Test]
  324. public void SetKeyDerivation ()
  325. {
  326. SetKeyDerivationCorrect (new TransportSecurityBindingElement (), "transport");
  327. SetKeyDerivationIncorrect (new TransportSecurityBindingElement (), "transport");
  328. SetKeyDerivationCorrect (new SymmetricSecurityBindingElement (), "symmetric");
  329. SetKeyDerivationIncorrect (new SymmetricSecurityBindingElement (), "symmetric");
  330. SetKeyDerivationCorrect (new AsymmetricSecurityBindingElement (), "asymmetric");
  331. SetKeyDerivationIncorrect (new AsymmetricSecurityBindingElement (), "asymmetric");
  332. }
  333. void SetKeyDerivationCorrect (SecurityBindingElement be, string label)
  334. {
  335. X509SecurityTokenParameters p, p2;
  336. p = new X509SecurityTokenParameters ();
  337. p2 = new X509SecurityTokenParameters ();
  338. Assert.AreEqual (true, p.RequireDerivedKeys, label + "#1");
  339. Assert.AreEqual (true, p2.RequireDerivedKeys, label + "#2");
  340. be.EndpointSupportingTokenParameters.Endorsing.Add (p);
  341. be.EndpointSupportingTokenParameters.Endorsing.Add (p2);
  342. be.SetKeyDerivation (false);
  343. Assert.AreEqual (false, p.RequireDerivedKeys, label + "#3");
  344. Assert.AreEqual (false, p2.RequireDerivedKeys, label + "#4");
  345. }
  346. void SetKeyDerivationIncorrect (SecurityBindingElement be, string label)
  347. {
  348. X509SecurityTokenParameters p, p2;
  349. p = new X509SecurityTokenParameters ();
  350. p2 = new X509SecurityTokenParameters ();
  351. // setting in prior - makes no sense
  352. be.SetKeyDerivation (false);
  353. be.EndpointSupportingTokenParameters.Endorsing.Add (p);
  354. be.EndpointSupportingTokenParameters.Endorsing.Add (p2);
  355. Assert.AreEqual (true, p.RequireDerivedKeys, label + "#5");
  356. Assert.AreEqual (true, p2.RequireDerivedKeys, label + "#6");
  357. }
  358. [Test]
  359. [ExpectedException (typeof (ArgumentException))]
  360. [Category ("NotWorking")]
  361. public void CheckDuplicateAuthenticatorTypesClient ()
  362. {
  363. SymmetricSecurityBindingElement be =
  364. new SymmetricSecurityBindingElement ();
  365. be.ProtectionTokenParameters =
  366. new X509SecurityTokenParameters ();
  367. be.EndpointSupportingTokenParameters.Endorsing.Add (
  368. new X509SecurityTokenParameters ());
  369. // This causes multiple supporting token authenticator
  370. // of the same type.
  371. be.OptionalEndpointSupportingTokenParameters.Endorsing.Add (
  372. new X509SecurityTokenParameters ());
  373. Binding b = new CustomBinding (be, new HttpTransportBindingElement ());
  374. ClientCredentials cred = new ClientCredentials ();
  375. cred.ClientCertificate.Certificate =
  376. new X509Certificate2 ("Test/Resources/test.pfx", "mono");
  377. IChannelFactory<IReplyChannel> ch = b.BuildChannelFactory<IReplyChannel> (new Uri ("http://localhost:37564"), cred);
  378. try {
  379. ch.Open ();
  380. } finally {
  381. if (ch.State == CommunicationState.Closed)
  382. ch.Close ();
  383. }
  384. }
  385. [Test]
  386. [ExpectedException (typeof (ArgumentException))]
  387. [Category ("NotWorking")]
  388. public void CheckDuplicateAuthenticatorTypesService ()
  389. {
  390. SymmetricSecurityBindingElement be =
  391. new SymmetricSecurityBindingElement ();
  392. be.ProtectionTokenParameters =
  393. new X509SecurityTokenParameters ();
  394. be.EndpointSupportingTokenParameters.Endorsing.Add (
  395. new X509SecurityTokenParameters ());
  396. // This causes multiple supporting token authenticator
  397. // of the same type.
  398. be.OptionalEndpointSupportingTokenParameters.Endorsing.Add (
  399. new X509SecurityTokenParameters ());
  400. Binding b = new CustomBinding (be, new HttpTransportBindingElement ());
  401. ServiceCredentials cred = new ServiceCredentials ();
  402. cred.ServiceCertificate.Certificate =
  403. new X509Certificate2 ("Test/Resources/test.pfx", "mono");
  404. IChannelListener<IReplyChannel> ch = b.BuildChannelListener<IReplyChannel> (new Uri ("http://localhost:37564"), cred);
  405. try {
  406. ch.Open ();
  407. } finally {
  408. if (ch.State == CommunicationState.Closed)
  409. ch.Close ();
  410. }
  411. }
  412. [Test]
  413. [ExpectedException (typeof (InvalidOperationException))]
  414. [Category ("NotWorking")]
  415. public void NonEndorsibleParameterInEndorsingSupport ()
  416. {
  417. SymmetricSecurityBindingElement be =
  418. new SymmetricSecurityBindingElement ();
  419. be.ProtectionTokenParameters =
  420. new X509SecurityTokenParameters ();
  421. be.EndpointSupportingTokenParameters.Endorsing.Add (
  422. new UserNameSecurityTokenParameters ());
  423. Binding b = new CustomBinding (be, new HttpTransportBindingElement ());
  424. X509Certificate2 cert = new X509Certificate2 ("Test/Resources/test.pfx", "mono");
  425. EndpointAddress ea = new EndpointAddress (new Uri ("http://localhost:37564"), new X509CertificateEndpointIdentity (cert));
  426. CalcProxy client = new CalcProxy (b, ea);
  427. client.ClientCredentials.UserName.UserName = "rupert";
  428. client.Sum (1, 2);
  429. }
  430. void AssertSecurityCapabilities (
  431. ProtectionLevel request, ProtectionLevel response,
  432. bool supportsClientAuth, bool supportsClientWinId,
  433. bool supportsServerAuth, ISecurityCapabilities c,
  434. string label)
  435. {
  436. Assert.AreEqual (request, c.SupportedRequestProtectionLevel, label + ".request");
  437. Assert.AreEqual (response, c.SupportedResponseProtectionLevel, label + ".response");
  438. Assert.AreEqual (supportsClientAuth, c.SupportsClientAuthentication, label + ".client-auth");
  439. Assert.AreEqual (supportsClientWinId, c.SupportsClientWindowsIdentity, label + ".client-identity");
  440. Assert.AreEqual (supportsServerAuth, c.SupportsServerAuthentication, label + ".server-auth");
  441. }
  442. ISecurityCapabilities GetSecurityCapabilities (SecurityBindingElement be)
  443. {
  444. BindingContext bc = new BindingContext (
  445. new CustomBinding (),
  446. new BindingParameterCollection ());
  447. return be.GetProperty<ISecurityCapabilities> (bc);
  448. }
  449. [Test]
  450. [ExpectedException (typeof (ArgumentNullException))]
  451. public void GetPropertyNullBindingContext1 ()
  452. {
  453. new SymmetricSecurityBindingElement ()
  454. .GetProperty<ISecurityCapabilities> (null);
  455. }
  456. [Test]
  457. [ExpectedException (typeof (ArgumentNullException))]
  458. public void GetPropertyNullBindingContext2 ()
  459. {
  460. new AsymmetricSecurityBindingElement ()
  461. .GetProperty<ISecurityCapabilities> (null);
  462. }
  463. [Test]
  464. public void GetPropertySecurityCapabilities ()
  465. {
  466. ISecurityCapabilities c;
  467. RsaSecurityTokenParameters rsa =
  468. new RsaSecurityTokenParameters ();
  469. UserNameSecurityTokenParameters user =
  470. new UserNameSecurityTokenParameters ();
  471. X509SecurityTokenParameters x509 =
  472. new X509SecurityTokenParameters ();
  473. SecureConversationSecurityTokenParameters sc1 =
  474. new SecureConversationSecurityTokenParameters ();
  475. sc1.BootstrapSecurityBindingElement =
  476. new SymmetricSecurityBindingElement (); // empty
  477. SecureConversationSecurityTokenParameters sc2 =
  478. new SecureConversationSecurityTokenParameters ();
  479. sc2.BootstrapSecurityBindingElement =
  480. new SymmetricSecurityBindingElement (x509);
  481. SecureConversationSecurityTokenParameters sc3 =
  482. new SecureConversationSecurityTokenParameters ();
  483. sc3.BootstrapSecurityBindingElement =
  484. new AsymmetricSecurityBindingElement (null, x509);
  485. SecureConversationSecurityTokenParameters sc4 =
  486. new SecureConversationSecurityTokenParameters ();
  487. sc4.BootstrapSecurityBindingElement =
  488. new AsymmetricSecurityBindingElement (x509, null);
  489. // no parameters
  490. c = GetSecurityCapabilities (
  491. new SymmetricSecurityBindingElement ());
  492. AssertSecurityCapabilities (
  493. ProtectionLevel.EncryptAndSign,
  494. ProtectionLevel.EncryptAndSign,
  495. false, false, false, c, "#1");
  496. // x509 parameters for both
  497. c = GetSecurityCapabilities (
  498. new SymmetricSecurityBindingElement (x509));
  499. AssertSecurityCapabilities (
  500. ProtectionLevel.EncryptAndSign,
  501. ProtectionLevel.EncryptAndSign,
  502. true, true, true, c, "#2");
  503. // no initiator parameters
  504. c = GetSecurityCapabilities (
  505. new AsymmetricSecurityBindingElement (x509, null));
  506. AssertSecurityCapabilities (
  507. ProtectionLevel.EncryptAndSign,
  508. ProtectionLevel.EncryptAndSign,
  509. false, false, true, c, "#3");
  510. // no recipient parameters
  511. c = GetSecurityCapabilities (
  512. new AsymmetricSecurityBindingElement (null, x509));
  513. AssertSecurityCapabilities (
  514. ProtectionLevel.EncryptAndSign,
  515. ProtectionLevel.EncryptAndSign,
  516. true, true, false, c, "#4");
  517. // initiator does not support identity
  518. c = GetSecurityCapabilities (
  519. new AsymmetricSecurityBindingElement (x509, rsa));
  520. AssertSecurityCapabilities (
  521. ProtectionLevel.EncryptAndSign,
  522. ProtectionLevel.EncryptAndSign,
  523. true, false, true, c, "#5");
  524. // recipient does not support server auth
  525. c = GetSecurityCapabilities (
  526. new AsymmetricSecurityBindingElement (user, x509));
  527. AssertSecurityCapabilities (
  528. ProtectionLevel.EncryptAndSign,
  529. ProtectionLevel.EncryptAndSign,
  530. true, true, false, c, "#6");
  531. // secureconv with no symm. bootstrap params
  532. c = GetSecurityCapabilities (
  533. new SymmetricSecurityBindingElement (sc1));
  534. AssertSecurityCapabilities (
  535. ProtectionLevel.EncryptAndSign,
  536. ProtectionLevel.EncryptAndSign,
  537. false, false, false, c, "#7");
  538. // secureconv with x509 symm. bootstrap params
  539. c = GetSecurityCapabilities (
  540. new SymmetricSecurityBindingElement (sc2));
  541. AssertSecurityCapabilities (
  542. ProtectionLevel.EncryptAndSign,
  543. ProtectionLevel.EncryptAndSign,
  544. true, true, true, c, "#8");
  545. // secureconv with x509 initiator bootstrap params
  546. c = GetSecurityCapabilities (
  547. new SymmetricSecurityBindingElement (sc3));
  548. AssertSecurityCapabilities (
  549. ProtectionLevel.EncryptAndSign,
  550. ProtectionLevel.EncryptAndSign,
  551. true, true, false, c, "#9");
  552. // secureconv with x509 recipient bootstrap params
  553. c = GetSecurityCapabilities (
  554. new SymmetricSecurityBindingElement (sc4));
  555. AssertSecurityCapabilities (
  556. ProtectionLevel.EncryptAndSign,
  557. ProtectionLevel.EncryptAndSign,
  558. false, false, true, c, "#10");
  559. // FIXME: find out such cases that returns other ProtectionLevel values.
  560. }
  561. }
  562. }