SymmetricSecurityBindingElementTest.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  1. //
  2. // SymmetricSecurityBindingElementTest.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 SymmetricSecurityBindingElementTest
  47. {
  48. [Test]
  49. public void DefaultValues ()
  50. {
  51. SymmetricSecurityBindingElement be =
  52. new SymmetricSecurityBindingElement ();
  53. SecurityAssert.AssertSymmetricSecurityBindingElement (
  54. SecurityAlgorithmSuite.Default,
  55. true, // IncludeTimestamp
  56. SecurityKeyEntropyMode.CombinedEntropy,
  57. MessageProtectionOrder.SignBeforeEncryptAndEncryptSignature,
  58. MessageSecurityVersion.Default,
  59. false, // RequireSignatureConfirmation
  60. SecurityHeaderLayout.Strict,
  61. // EndpointSupportingTokenParameters: endorsing, signed, signedEncrypted, signedEndorsing (by count)
  62. 0, 0, 0, 0,
  63. // ProtectionTokenParameters
  64. false,
  65. default (SecurityTokenInclusionMode),
  66. default (SecurityTokenReferenceStyle),
  67. default (bool),
  68. // LocalClientSettings
  69. true, 60, true,
  70. be, "");
  71. }
  72. [Test]
  73. [ExpectedException (typeof (InvalidOperationException))]
  74. public void BuildChannelWithoutProtectionTokenParameters ()
  75. {
  76. CustomBinding b = new CustomBinding (
  77. new SymmetricSecurityBindingElement (),
  78. new TextMessageEncodingBindingElement (),
  79. new HttpTransportBindingElement ());
  80. b.BuildChannelFactory<IRequestChannel> (new BindingParameterCollection ());
  81. }
  82. CustomBinding CreateBinding ()
  83. {
  84. RequestSender handler = delegate (Message input) {
  85. throw new Exception ();
  86. };
  87. return CreateBinding (handler);
  88. }
  89. CustomBinding CreateBinding (RequestSender sender)
  90. {
  91. return CreateBinding (sender, new X509SecurityTokenParameters ());
  92. }
  93. CustomBinding CreateBinding (RequestSender sender, bool isOneWay)
  94. {
  95. return CreateBinding (sender, new X509SecurityTokenParameters (), isOneWay);
  96. }
  97. CustomBinding CreateBinding (SecurityTokenParameters protectionTokenParameters)
  98. {
  99. RequestSender handler = delegate (Message input) {
  100. throw new Exception ();
  101. };
  102. return CreateBinding (handler, protectionTokenParameters);
  103. }
  104. CustomBinding CreateBinding (RequestSender sender,
  105. SecurityTokenParameters protectionTokenParameters)
  106. {
  107. return CreateBinding (sender, protectionTokenParameters, false);
  108. }
  109. CustomBinding CreateBinding (RequestSender sender,
  110. SecurityTokenParameters protectionTokenParameters,
  111. bool isOneWay)
  112. {
  113. SymmetricSecurityBindingElement sbe =
  114. new SymmetricSecurityBindingElement ();
  115. sbe.ProtectionTokenParameters = protectionTokenParameters;
  116. List<BindingElement> l = new List<BindingElement> ();
  117. l.Add (sbe);
  118. l.Add (new TextMessageEncodingBindingElement ());
  119. if (isOneWay)
  120. l.Add (new OneWayBindingElement ());
  121. l.Add (new HandlerTransportBindingElement (sender));
  122. CustomBinding b = new CustomBinding (l);
  123. return b;
  124. }
  125. CustomBinding CreateBinding (ReplyHandler replier, RequestReceiver receiver)
  126. {
  127. SymmetricSecurityBindingElement sbe =
  128. new SymmetricSecurityBindingElement ();
  129. sbe.ProtectionTokenParameters =
  130. new X509SecurityTokenParameters ();
  131. CustomBinding b = new CustomBinding (
  132. sbe,
  133. new TextMessageEncodingBindingElement (),
  134. new HandlerTransportBindingElement (replier, receiver));
  135. return b;
  136. }
  137. EndpointAddress CreateX509EndpointAddress (string uri)
  138. {
  139. EndpointIdentity identity =
  140. new X509CertificateEndpointIdentity (new X509Certificate2 ("Test/Resources/test.pfx", "mono"));
  141. return new EndpointAddress (new Uri (uri), identity);
  142. }
  143. IChannelListener<IReplyChannel> CreateListener (ReplyHandler handler, RequestReceiver receiver)
  144. {
  145. CustomBinding rb = CreateBinding (handler, receiver);
  146. BindingParameterCollection bpl =
  147. new BindingParameterCollection ();
  148. ServiceCredentials cred = new ServiceCredentials ();
  149. cred.ServiceCertificate.Certificate =
  150. new X509Certificate2 ("Test/Resources/test.pfx", "mono");
  151. IServiceBehavior sb = cred;
  152. sb.AddBindingParameters (null, null, null, bpl);
  153. IChannelListener<IReplyChannel> listener = rb.BuildChannelListener<IReplyChannel> (bpl);
  154. return listener;
  155. }
  156. [Test]
  157. public void OpenChannelFactory ()
  158. {
  159. CustomBinding b = CreateBinding ();
  160. IChannelFactory<IRequestChannel> f =
  161. b.BuildChannelFactory<IRequestChannel> (new BindingParameterCollection ());
  162. f.Open ();
  163. }
  164. [Test]
  165. [ExpectedException (typeof (InvalidOperationException))]
  166. public void BuildChannelWithoutOpen ()
  167. {
  168. CustomBinding b = CreateBinding ();
  169. IChannelFactory<IRequestChannel> f =
  170. b.BuildChannelFactory<IRequestChannel> (new BindingParameterCollection ());
  171. f.CreateChannel (CreateX509EndpointAddress ("stream:dummy"));
  172. }
  173. [Test]
  174. public void OpenRequestNonAuthenticatable ()
  175. {
  176. SymmetricSecurityBindingElement sbe =
  177. new SymmetricSecurityBindingElement ();
  178. sbe.ProtectionTokenParameters =
  179. new UserNameSecurityTokenParameters ();
  180. Binding binding = new CustomBinding (sbe, new HandlerTransportBindingElement (null));
  181. BindingParameterCollection pl =
  182. new BindingParameterCollection ();
  183. ClientCredentials cred = new ClientCredentials ();
  184. cred.UserName.UserName = "mono";
  185. pl.Add (cred);
  186. IChannelFactory<IRequestChannel> f =
  187. binding.BuildChannelFactory<IRequestChannel> (pl);
  188. f.Open ();
  189. IRequestChannel ch = f.CreateChannel (new EndpointAddress ("stream:dummy"));
  190. try {
  191. ch.Open ();
  192. Assert.Fail ("NotSupportedException is expected.");
  193. } catch (NotSupportedException) {
  194. }
  195. }
  196. // The service certificate is not provided for target
  197. // 'stream:dummy'. Specify a service certificate in
  198. // ClientCredentials.
  199. [Test]
  200. public void OpenRequestWithoutServiceCertificate ()
  201. {
  202. CustomBinding b = CreateBinding ();
  203. IChannelFactory<IRequestChannel> f =
  204. b.BuildChannelFactory<IRequestChannel> (new BindingParameterCollection ());
  205. f.Open ();
  206. // This EndpointAddress does not contain X509 identity
  207. IRequestChannel ch = f.CreateChannel (new EndpointAddress ("stream:dummy"));
  208. try {
  209. ch.Open ();
  210. Assert.Fail ("expected InvalidOperationException here.");
  211. } catch (InvalidOperationException) {
  212. }
  213. }
  214. IChannelFactory<IRequestChannel> CreateDefaultServiceCertFactory ()
  215. {
  216. CustomBinding b = CreateBinding (delegate (Message req) {
  217. return null;
  218. });
  219. ClientCredentials cred = new ClientCredentials ();
  220. cred.ServiceCertificate.DefaultCertificate = new X509Certificate2 ("Test/Resources/test.pfx", "mono");
  221. BindingParameterCollection parameters =
  222. new BindingParameterCollection ();
  223. parameters.Add (cred);
  224. ChannelProtectionRequirements cp =
  225. new ChannelProtectionRequirements ();
  226. cp.IncomingSignatureParts.AddParts (
  227. new MessagePartSpecification (true),
  228. "http://tempuri.org/MyAction");
  229. cp.IncomingEncryptionParts.AddParts (
  230. new MessagePartSpecification (true),
  231. "http://tempuri.org/MyAction");
  232. parameters.Add (cp);
  233. return b.BuildChannelFactory<IRequestChannel> (parameters);
  234. }
  235. [Test]
  236. public void OpenRequestWithDefaultServiceCertificate ()
  237. {
  238. IChannelFactory<IRequestChannel> f =
  239. CreateDefaultServiceCertFactory ();
  240. f.Open ();
  241. // This EndpointAddress does not contain X509 identity
  242. IRequestChannel ch = f.CreateChannel (new EndpointAddress ("stream:dummy"));
  243. ch.Open ();
  244. // stop here.
  245. }
  246. [Test]
  247. [ExpectedException (typeof (MessageSecurityException))]
  248. [Category ("NotWorking")]
  249. // from WinFX:
  250. // MessageSecurityException : Identity check failed for outgoing
  251. // message. The expected DNS identity of the remote endpoint was
  252. // '' but the remote endpoint provided DNS claim 'Poupou's-
  253. // Software-Factory'. If this is a legitimate remote endpoint,
  254. // you can fix the problem by explicitly specifying DNS identity
  255. // 'Poupou's-Software-Factory' as the Identity property of
  256. // EndpointAddress when creating channel proxy.
  257. public void RequestWithDefaultServiceCertificateWithoutDns ()
  258. {
  259. IChannelFactory<IRequestChannel> f =
  260. CreateDefaultServiceCertFactory ();
  261. f.Open ();
  262. // This EndpointAddress does not contain X509 identity
  263. IRequestChannel ch = f.CreateChannel (new EndpointAddress ("stream:dummy"));
  264. ch.Open ();
  265. // -> MessageSecurityException (IdentityVerifier complains DNS claim)
  266. ch.Request (Message.CreateMessage (MessageVersion.Default, "http://tempuri.org/MyAction"));
  267. }
  268. [Test]
  269. [Category ("NotWorking")]
  270. public void RequestWithDefaultServiceCertificateWithDns ()
  271. {
  272. IChannelFactory<IRequestChannel> f =
  273. CreateDefaultServiceCertFactory ();
  274. f.Open ();
  275. // This EndpointAddress does not contain X509 identity
  276. IRequestChannel ch = f.CreateChannel (new EndpointAddress (new Uri ("stream:dummy"), new DnsEndpointIdentity ("Poupou's-Software-Factory")));
  277. ch.Open ();
  278. // -> MessageSecurityException (IdentityVerifier complains DNS claim)
  279. ch.Request (Message.CreateMessage (MessageVersion.Default, "http://tempuri.org/MyAction"));
  280. }
  281. [Test]
  282. [Category ("NotWorking")] // it depends on Kerberos
  283. public void OpenRequestWithoutServiceCertificateForNonX509 ()
  284. {
  285. CustomBinding b = CreateBinding (new MyOwnSecurityTokenParameters ());
  286. IChannelFactory<IRequestChannel> f =
  287. b.BuildChannelFactory<IRequestChannel> (new BindingParameterCollection ());
  288. f.Open ();
  289. // This EndpointAddress does not contain X509 identity
  290. IRequestChannel ch = f.CreateChannel (new EndpointAddress ("stream:dummy"));
  291. ch.Open ();
  292. }
  293. [Test]
  294. public void SendRequestWithoutOpen ()
  295. {
  296. CustomBinding b = CreateBinding ();
  297. IChannelFactory<IRequestChannel> f =
  298. b.BuildChannelFactory<IRequestChannel> (new BindingParameterCollection ());
  299. f.Open ();
  300. IRequestChannel ch = f.CreateChannel (CreateX509EndpointAddress ("stream:dummy"));
  301. try {
  302. ch.Request (Message.CreateMessage (MessageVersion.Default, "myAction"));
  303. Assert.Fail ("expected InvalidOperationException here.");
  304. } catch (InvalidOperationException) {
  305. }
  306. }
  307. [Test]
  308. [Category ("NotWorking")]
  309. public void SendRequestWithoutSignatureMessagePart ()
  310. {
  311. CustomBinding b = CreateBinding ();
  312. // without ChannelProtectionRequirements it won't be
  313. // signed and/or encrypted.
  314. IChannelFactory<IRequestChannel> f =
  315. b.BuildChannelFactory<IRequestChannel> (new BindingParameterCollection ());
  316. f.Open ();
  317. IRequestChannel ch = f.CreateChannel (CreateX509EndpointAddress ("stream:dummy"));
  318. ch.Open ();
  319. // MessageSecurityException : No signature message parts
  320. // were specified for messages with the 'myAction'
  321. // action.
  322. try {
  323. ch.Request (Message.CreateMessage (b.MessageVersion, "myAction"));
  324. Assert.Fail ("MessageSecurityException is expected here.");
  325. } catch (MessageSecurityException) {
  326. }
  327. }
  328. [Test]
  329. [ExpectedException (typeof (Exception))]
  330. [Category ("NotWorking")]
  331. public void SendRequestWithSignatureMessagePart ()
  332. {
  333. CustomBinding b = CreateBinding ();
  334. ChannelProtectionRequirements cp =
  335. new ChannelProtectionRequirements ();
  336. cp.IncomingSignatureParts.AddParts (new MessagePartSpecification (true), "myAction");
  337. cp.IncomingEncryptionParts.AddParts (new MessagePartSpecification (true), "myAction");
  338. BindingParameterCollection parameters =
  339. new BindingParameterCollection ();
  340. parameters.Add (cp);
  341. IChannelFactory<IRequestChannel> f =
  342. b.BuildChannelFactory<IRequestChannel> (parameters);
  343. f.Open ();
  344. IRequestChannel ch = f.CreateChannel (CreateX509EndpointAddress ("stream:dummy"));
  345. ch.Open ();
  346. ch.Request (Message.CreateMessage (b.MessageVersion, "myAction"));
  347. }
  348. [Test]
  349. [Category ("NotWorking")] // it requires OneWay
  350. public void RequestBasedOnContract1 ()
  351. {
  352. CustomBinding b = CreateBinding (delegate (Message input) {
  353. return null;
  354. }, true);
  355. IFoo foo = ChannelFactory<IFoo>.CreateChannel (b, CreateX509EndpointAddress ("stream:dummy"));
  356. foo.Bar (Message.CreateMessage (b.MessageVersion, "http://tempuri.org/IFoo/Bar"));
  357. }
  358. [Test]
  359. public void RequestBasedOnContract2 ()
  360. {
  361. CustomBinding b = CreateBinding (delegate (Message input) {
  362. return null;
  363. }, true);
  364. IFoo foo = ChannelFactory<IFoo>.CreateChannel (b, CreateX509EndpointAddress ("stream:dummy"));
  365. foo.Baz ("TEST");
  366. }
  367. [Test]
  368. // it still does not produce secure message ...
  369. [Category ("NotWorking")]
  370. public void RequestBasedOnContract3 ()
  371. {
  372. CustomBinding b = CreateBinding (delegate (Message input) {
  373. // seems like security message property is not attached to the request.
  374. foreach (object o in input.Properties.Values)
  375. if (o is SecurityMessageProperty)
  376. Assert.Fail ("there should be a SecurityMessageProperty.");
  377. return null;
  378. }, true);
  379. IFoo foo = ChannelFactory<IFoo>.CreateChannel (b, CreateX509EndpointAddress ("stream:dummy"));
  380. foo.Bleh ("TEST");
  381. }
  382. // from WCF (beta2):
  383. // "MessageSecurityException : Security processor was unable
  384. // to find a security header in the message. This might be
  385. // because the message is an unsecured fault or because there
  386. // is a binding mismatch between the communicating parties.
  387. // This can occur if the service is configured for security
  388. // and the client is not using security."
  389. [Test]
  390. [ExpectedException (typeof (MessageSecurityException))]
  391. [Category ("NotWorking")]
  392. public void RequestUnsecuredReply ()
  393. {
  394. CustomBinding b = CreateBinding (delegate (Message input) {
  395. return input;
  396. });
  397. IFoo foo = ChannelFactory<IFoo>.CreateChannel (b, CreateX509EndpointAddress ("stream:dummy"));
  398. foo.Bar (Message.CreateMessage (b.MessageVersion, "http://tempuri.org/IFoo/Bar"));
  399. }
  400. [ServiceContract]
  401. interface IFoo
  402. {
  403. [OperationContract (IsOneWay = true)]
  404. void Bar (Message msg);
  405. [OperationContract (IsOneWay = true)]
  406. void Baz (string src);
  407. [OperationContract (ProtectionLevel = ProtectionLevel.Sign, IsOneWay = true)]
  408. void Bleh (string src);
  409. }
  410. [Test]
  411. [ExpectedException (typeof (InvalidOperationException))]
  412. public void BuildListenerWithoutProtectionTokenParameters ()
  413. {
  414. CustomBinding b = new CustomBinding (
  415. new SymmetricSecurityBindingElement (),
  416. new TextMessageEncodingBindingElement (),
  417. new HttpTransportBindingElement ());
  418. b.BuildChannelListener<IReplyChannel> (new BindingParameterCollection ());
  419. }
  420. [Test]
  421. [ExpectedException (typeof (InvalidOperationException))]
  422. public void OpenListenerWithoutServiceCertificate ()
  423. {
  424. CustomBinding rb = CreateBinding ();
  425. IChannelListener<IReplyChannel> listener = rb.BuildChannelListener<IReplyChannel> (new BindingParameterCollection ());
  426. listener.Open ();
  427. }
  428. [Test]
  429. [ExpectedException (typeof (ArgumentException))]
  430. public void OpenListenerNoPrivateKeyInServiceCertificate ()
  431. {
  432. CustomBinding rb = CreateBinding ();
  433. BindingParameterCollection bpl =
  434. new BindingParameterCollection ();
  435. ServiceCredentials cred = new ServiceCredentials ();
  436. cred.ServiceCertificate.Certificate =
  437. new X509Certificate2 ("Test/Resources/test.cer");
  438. IServiceBehavior sb = cred;
  439. sb.AddBindingParameters (null, null, null, bpl);
  440. IChannelListener<IReplyChannel> listener = rb.BuildChannelListener<IReplyChannel> (bpl);
  441. listener.Open ();
  442. }
  443. [Test]
  444. [ExpectedException (typeof (InvalidOperationException))]
  445. public void AcceptChannelWithoutOpenListener ()
  446. {
  447. IChannelListener<IReplyChannel> listener = CreateListener (null, null);
  448. listener.AcceptChannel ();
  449. }
  450. [Test]
  451. [ExpectedException (typeof (InvalidOperationException))]
  452. [Category ("NotWorking")]
  453. public void ReceiveRequestWithoutOpenChannel ()
  454. {
  455. IChannelListener<IReplyChannel> listener = CreateListener (null, null);
  456. listener.Open ();
  457. IReplyChannel reply = listener.AcceptChannel ();
  458. reply.ReceiveRequest ();
  459. }
  460. [Test]
  461. [Ignore ("It's not working")]
  462. [ExpectedException (typeof (ApplicationException))]
  463. public void ReceiveRequest ()
  464. {
  465. // Seems like this method is invoked to send a reply
  466. // with related to "already created" SOAP fault.
  467. //
  468. // It is still not understandable that this delegate
  469. // is invoked as an infinite loop ...
  470. ReplyHandler handler = delegate (Message input) {
  471. Console.Error.WriteLine ("Processing a reply.");
  472. // a:InvalidSecurity
  473. // An error occurred when verifying security for the message.
  474. Assert.IsTrue (input.IsFault);
  475. throw new ApplicationException ();
  476. };
  477. Message msg = Message.CreateMessage (MessageVersion.Default, "myAction");
  478. RequestReceiver receiver = delegate () {
  479. return msg;
  480. };
  481. IChannelListener<IReplyChannel> listener = CreateListener (handler, receiver);
  482. listener.Open ();
  483. IReplyChannel reply = listener.AcceptChannel ();
  484. reply.Open ();
  485. RequestContext ctx = reply.EndReceiveRequest (reply.BeginReceiveRequest (null, null));
  486. }
  487. // Without SecurityBindingElement it works.
  488. // With it, it causes kind of infinite loop around
  489. // RequestContext.get_RequestMessage() which somehow blocks
  490. // finishing HandlerTransportRequestChannel.Request() (and
  491. // it continues until the timeout).
  492. [Test]
  493. [Ignore ("It's not working")]
  494. [Category ("NotWorking")]
  495. public void FullRequest ()
  496. {
  497. EndpointIdentity identity =
  498. new X509CertificateEndpointIdentity (new X509Certificate2 ("Test/Resources/test.pfx", "mono"));
  499. EndpointAddress address =
  500. new EndpointAddress (new Uri ("stream:dummy"), identity);
  501. Message mreq = Message.CreateMessage (MessageVersion.Default, "myAction");
  502. Message mreply = null;
  503. XmlWriterSettings settings = new XmlWriterSettings ();
  504. settings.Indent = true;
  505. // listener setup
  506. ReplyHandler replyHandler = delegate (Message rinput) {
  507. mreply = rinput;
  508. };
  509. RequestReceiver receiver = delegate () {
  510. return mreq;
  511. };
  512. IChannelListener<IReplyChannel> listener = CreateListener (replyHandler, receiver);
  513. listener.Open ();
  514. IReplyChannel reply = listener.AcceptChannel ();
  515. reply.Open ();
  516. RequestSender reqHandler = delegate (Message input) {
  517. try {
  518. // sync version somehow causes an infinite loop (!?)
  519. RequestContext ctx = reply.EndReceiveRequest (reply.BeginReceiveRequest (TimeSpan.FromSeconds (5), null, null));
  520. // RequestContext ctx = reply.ReceiveRequest (TimeSpan.FromSeconds (5));
  521. Console.Error.WriteLine ("Acquired RequestContext.");
  522. ctx.Reply (input);
  523. } catch (Exception ex) {
  524. Console.Error.WriteLine ("ERROR during processing a request in FullRequest()");
  525. Console.Error.WriteLine (ex);
  526. Console.Error.Flush ();
  527. throw;
  528. }
  529. return mreply;
  530. };
  531. CustomBinding b = CreateBinding (reqHandler);
  532. IRequestChannel ch = ChannelFactory<IRequestChannel>.CreateChannel (b, address);
  533. ch.Open ();
  534. Console.Error.WriteLine ("**** starting a request ****");
  535. IAsyncResult async = ch.BeginRequest (mreq, null, null);
  536. Console.Error.WriteLine ("**** request started. ****");
  537. Message res = ch.EndRequest (async);
  538. }
  539. [Test]
  540. public void SetKeyDerivation ()
  541. {
  542. SymmetricSecurityBindingElement be;
  543. X509SecurityTokenParameters p;
  544. be = new SymmetricSecurityBindingElement ();
  545. p = new X509SecurityTokenParameters ();
  546. be.ProtectionTokenParameters = p;
  547. be.SetKeyDerivation (false);
  548. Assert.AreEqual (false, p.RequireDerivedKeys, "#1");
  549. be = new SymmetricSecurityBindingElement ();
  550. p = new X509SecurityTokenParameters ();
  551. be.SetKeyDerivation (false); // set in prior - makes no sense
  552. be.ProtectionTokenParameters = p;
  553. Assert.AreEqual (true, p.RequireDerivedKeys, "#2");
  554. }
  555. }
  556. class MyOwnSecurityTokenParameters : SecurityTokenParameters
  557. {
  558. public MyOwnSecurityTokenParameters ()
  559. {
  560. }
  561. protected MyOwnSecurityTokenParameters (MyOwnSecurityTokenParameters source)
  562. {
  563. }
  564. protected override bool HasAsymmetricKey {
  565. get { return false; }
  566. }
  567. protected override bool SupportsClientAuthentication {
  568. get { return true; }
  569. }
  570. protected override bool SupportsClientWindowsIdentity {
  571. get { return false; }
  572. }
  573. protected override bool SupportsServerAuthentication {
  574. get { return true; }
  575. }
  576. protected override SecurityTokenParameters CloneCore ()
  577. {
  578. return new MyOwnSecurityTokenParameters (this);
  579. }
  580. protected override SecurityKeyIdentifierClause CreateKeyIdentifierClause (
  581. SecurityToken token, SecurityTokenReferenceStyle referenceStyle)
  582. {
  583. throw new NotImplementedException ();
  584. }
  585. protected override void InitializeSecurityTokenRequirement (SecurityTokenRequirement requirement)
  586. {
  587. // If there were another token type that supports protection
  588. // and does not require X509, it should be used instead ...
  589. requirement.TokenType = SecurityTokenTypes.Kerberos;
  590. }
  591. }
  592. }