AsymmetricSecurityBindingElementTest.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. //
  2. // AsymmetricSecurityBindingElementTest.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.IO;
  32. using System.Net;
  33. using System.Net.Security;
  34. using System.Security.Cryptography.X509Certificates;
  35. using System.IdentityModel.Selectors;
  36. using System.IdentityModel.Tokens;
  37. using System.ServiceModel;
  38. using System.ServiceModel.Channels;
  39. using System.ServiceModel.Description;
  40. using System.ServiceModel.Security;
  41. using System.ServiceModel.Security.Tokens;
  42. using System.Threading;
  43. using System.Xml;
  44. using NUnit.Framework;
  45. namespace MonoTests.System.ServiceModel.Channels
  46. {
  47. [TestFixture]
  48. public class AsymmetricSecurityBindingElementTest
  49. {
  50. static X509Certificate2 cert = new X509Certificate2 ("Test/Resources/test.pfx", "mono");
  51. static X509Certificate2 cert2 = new X509Certificate2 ("Test/Resources/test.cer");
  52. // InitiatorTokenParameters should have asymmetric key.
  53. [Test]
  54. [ExpectedException (typeof (InvalidOperationException))]
  55. [Category ("NotWorking")] // this test unnecessarily requires some internal processing order
  56. public void ClientInitiatorHasNoKeys1 ()
  57. {
  58. ClientInitiatorHasNoKeysCore (false, MessageProtectionOrder.SignBeforeEncrypt);
  59. }
  60. [Test]
  61. [ExpectedException (typeof (InvalidOperationException))]
  62. [Category ("NotWorking")] // this test unnecessarily requires some internal processing order
  63. public void ClientInitiatorHasNoKeys2 ()
  64. {
  65. ClientInitiatorHasNoKeysCore (true, MessageProtectionOrder.SignBeforeEncrypt);
  66. }
  67. [Test]
  68. [ExpectedException (typeof (InvalidOperationException))]
  69. [Category ("NotWorking")] // this test unnecessarily requires some internal processing order
  70. public void ClientInitiatorHasNoKeys3 ()
  71. {
  72. ClientInitiatorHasNoKeysCore (false, MessageProtectionOrder.SignBeforeEncryptAndEncryptSignature);
  73. }
  74. [Test]
  75. [ExpectedException (typeof (InvalidOperationException))]
  76. [Category ("NotWorking")] // this test unnecessarily requires some internal processing order
  77. public void ClientInitiatorHasNoKeys4 ()
  78. {
  79. ClientInitiatorHasNoKeysCore (true, MessageProtectionOrder.SignBeforeEncryptAndEncryptSignature);
  80. }
  81. public void ClientInitiatorHasNoKeysCore (bool deriveKeys, MessageProtectionOrder order)
  82. {
  83. AsymmetricSecurityBindingElement sbe =
  84. new AsymmetricSecurityBindingElement ();
  85. sbe.InitiatorTokenParameters =
  86. new UserNameSecurityTokenParameters ();
  87. sbe.RecipientTokenParameters =
  88. new X509SecurityTokenParameters ();
  89. sbe.SetKeyDerivation (deriveKeys);
  90. sbe.MessageProtectionOrder = order;
  91. TransportBindingElement tbe = new HandlerTransportBindingElement (delegate (Message input) {
  92. // funky, but .NET does not raise an error
  93. // until it writes the message to somewhere.
  94. // That is, it won't raise an error if this
  95. // HandlerTransportBindingElement does not
  96. // write the input message to somewhere.
  97. // It is an obvious bug.
  98. input.WriteMessage (XmlWriter.Create (TextWriter.Null));
  99. throw new Exception ();
  100. });
  101. CustomBinding binding = new CustomBinding (sbe, tbe);
  102. EndpointAddress address = new EndpointAddress (
  103. new Uri ("stream:dummy"),
  104. new X509CertificateEndpointIdentity (cert2));
  105. CalcProxy proxy = new CalcProxy (binding, address);
  106. proxy.ClientCredentials.UserName.UserName = "mono";
  107. proxy.Open ();
  108. // Until here the wrong parameters are not checked.
  109. proxy.Sum (1, 2);
  110. }
  111. [Test]
  112. [ExpectedException (typeof (NotSupportedException))]
  113. [Category ("NotWorking")]
  114. public void ServiceRecipientHasNoKeys ()
  115. {
  116. AsymmetricSecurityBindingElement sbe =
  117. new AsymmetricSecurityBindingElement ();
  118. sbe.InitiatorTokenParameters =
  119. new X509SecurityTokenParameters ();
  120. sbe.RecipientTokenParameters =
  121. new UserNameSecurityTokenParameters ();
  122. //sbe.SetKeyDerivation (false);
  123. //sbe.MessageProtectionOrder = MessageProtectionOrder.SignBeforeEncrypt;
  124. CustomBinding binding = new CustomBinding (sbe,
  125. new HttpTransportBindingElement ());
  126. IChannelListener<IReplyChannel> l =
  127. binding.BuildChannelListener<IReplyChannel> (new Uri ("http://localhost:37564"), new BindingParameterCollection ());
  128. try {
  129. l.Open ();
  130. } finally {
  131. if (l.State == CommunicationState.Opened)
  132. l.Close ();
  133. }
  134. }
  135. Message tmp_request, tmp_reply;
  136. [Test]
  137. [ExpectedException (typeof (MessageSecurityException))]
  138. // after having to fix several issues, I forgot what I originally wanted to test here ...
  139. [Ignore ("It causes some weird failure and port blocking ...")]
  140. public void VerifyX509MessageSecurityAtService ()
  141. {
  142. AsymmetricSecurityBindingElement clisbe =
  143. new AsymmetricSecurityBindingElement ();
  144. clisbe.InitiatorTokenParameters =
  145. new X509SecurityTokenParameters ();
  146. clisbe.RecipientTokenParameters =
  147. new X509SecurityTokenParameters ();
  148. AsymmetricSecurityBindingElement svcsbe =
  149. new AsymmetricSecurityBindingElement ();
  150. svcsbe.InitiatorTokenParameters =
  151. new X509SecurityTokenParameters ();
  152. svcsbe.RecipientTokenParameters =
  153. new X509SecurityTokenParameters ();
  154. CustomBinding b_req = new CustomBinding (clisbe,
  155. new HttpTransportBindingElement ());
  156. b_req.ReceiveTimeout = b_req.SendTimeout = TimeSpan.FromSeconds (10);
  157. CustomBinding b_res = new CustomBinding (svcsbe, new HttpTransportBindingElement ());
  158. b_res.ReceiveTimeout = b_res.SendTimeout = TimeSpan.FromSeconds (10);
  159. EndpointAddress remaddr = new EndpointAddress (
  160. new Uri ("http://localhost:37564"),
  161. new X509CertificateEndpointIdentity (cert2));
  162. CalcProxy proxy = null;
  163. ServiceHost host = new ServiceHost (typeof (CalcService));
  164. host.AddServiceEndpoint (typeof (ICalc), b_res, "http://localhost:37564");
  165. ServiceCredentials cred = new ServiceCredentials ();
  166. cred.ServiceCertificate.Certificate = cert;
  167. host.Description.Behaviors.Add (cred);
  168. try {
  169. host.Open ();
  170. proxy = new CalcProxy (b_req, remaddr);
  171. proxy.ClientCredentials.ClientCertificate.Certificate = cert;
  172. // FIXME: on WinFX, when this Begin method
  173. // is invoked before the listener setup, it
  174. // somehow works, while ours doesn't.
  175. //IAsyncResult result = proxy.BeginSum (1, 2, null, null);
  176. //Assert.AreEqual (3, proxy.EndSum (result));
  177. Assert.AreEqual (3, proxy.Sum (1, 2));
  178. } finally {
  179. if (host.State == CommunicationState.Opened)
  180. host.Close ();
  181. }
  182. }
  183. [Test]
  184. public void SetKeyDerivation ()
  185. {
  186. AsymmetricSecurityBindingElement be;
  187. X509SecurityTokenParameters p, p2;
  188. be = new AsymmetricSecurityBindingElement ();
  189. p = new X509SecurityTokenParameters ();
  190. p2 = new X509SecurityTokenParameters ();
  191. be.InitiatorTokenParameters = p;
  192. be.RecipientTokenParameters = p2;
  193. be.SetKeyDerivation (false);
  194. Assert.AreEqual (false, p.RequireDerivedKeys, "#1");
  195. Assert.AreEqual (false, p2.RequireDerivedKeys, "#2");
  196. be = new AsymmetricSecurityBindingElement ();
  197. p = new X509SecurityTokenParameters ();
  198. p2 = new X509SecurityTokenParameters ();
  199. be.SetKeyDerivation (false); // set in prior - makes no sense
  200. be.InitiatorTokenParameters = p;
  201. be.RecipientTokenParameters = p2;
  202. Assert.AreEqual (true, p.RequireDerivedKeys, "#3");
  203. Assert.AreEqual (true, p2.RequireDerivedKeys, "#4");
  204. }
  205. [Test]
  206. [ExpectedException (typeof (InvalidOperationException))]
  207. [Category ("NotWorking")]
  208. public void RejectInclusionModeNever ()
  209. {
  210. AsymmetricSecurityBindingElement sbe =
  211. new AsymmetricSecurityBindingElement ();
  212. sbe.InitiatorTokenParameters = sbe.RecipientTokenParameters =
  213. new X509SecurityTokenParameters (
  214. X509KeyIdentifierClauseType.Thumbprint,
  215. // this leads to the failure.
  216. SecurityTokenInclusionMode.Never);
  217. ServiceHost host = new ServiceHost (typeof (Foo));
  218. HttpTransportBindingElement hbe =
  219. new HttpTransportBindingElement ();
  220. CustomBinding binding = new CustomBinding (sbe, hbe);
  221. host.AddServiceEndpoint (typeof (IFoo),
  222. binding, new Uri ("http://localhost:37564"));
  223. ServiceCredentials cred = new ServiceCredentials ();
  224. cred.ServiceCertificate.Certificate =
  225. new X509Certificate2 ("Test/Resources/test.pfx", "mono");
  226. cred.ClientCertificate.Authentication.CertificateValidationMode =
  227. X509CertificateValidationMode.None;
  228. host.Description.Behaviors.Add (cred);
  229. try {
  230. host.Open ();
  231. } finally {
  232. if (host.State == CommunicationState.Opened)
  233. host.Close ();
  234. }
  235. }
  236. }
  237. }