SecurityAssert.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. //
  2. // SecurityAssert.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. public static class SecurityAssert
  46. {
  47. public static void AssertLocalClientSecuritySettings (
  48. bool cacheCookies,
  49. int renewalThresholdPercentage,
  50. bool detectReplays,
  51. LocalClientSecuritySettings lc, string label)
  52. {
  53. Assert.IsNotNull (lc, label + " IsNotNull");
  54. Assert.AreEqual (cacheCookies, lc.CacheCookies, label + ".CacheCookies");
  55. Assert.AreEqual (renewalThresholdPercentage, lc.CookieRenewalThresholdPercentage, label + ".CookieRenewalThresholdPercentage");
  56. Assert.AreEqual (detectReplays, lc.DetectReplays, label + ".DetectReplays");
  57. }
  58. public static void AssertSecurityTokenParameters (
  59. SecurityTokenInclusionMode protectionTokenInclusionMode,
  60. SecurityTokenReferenceStyle protectionTokenReferenceStyle,
  61. bool protectionTokenRequireDerivedKeys,
  62. SecurityTokenParameters tp, string label)
  63. {
  64. Assert.IsNotNull (tp, label + " IsNotNull");
  65. Assert.AreEqual (protectionTokenInclusionMode,
  66. tp.InclusionMode, label + ".InclusionMode");
  67. Assert.AreEqual (protectionTokenReferenceStyle,
  68. tp.ReferenceStyle, label + ".ReferenceStyle");
  69. Assert.AreEqual (protectionTokenRequireDerivedKeys,
  70. tp.RequireDerivedKeys, label + ".RequireDerivedKeys");
  71. }
  72. public static void AssertSupportingTokenParameters (
  73. int endorsing, int signed, int signedEncrypted, int signedEndorsing,
  74. SupportingTokenParameters tp, string label)
  75. {
  76. Assert.IsNotNull (tp, label + " IsNotNull");
  77. Assert.AreEqual (endorsing, tp.Endorsing.Count, label + ".Endoring.Count");
  78. Assert.AreEqual (signed, tp.Signed.Count, label + ".Signed.Count");
  79. Assert.AreEqual (signedEncrypted, tp.SignedEncrypted.Count, label + ".SignedEncrypted.Count");
  80. Assert.AreEqual (signedEndorsing, tp.SignedEndorsing.Count, label + ".SignedEndorsing.Count");
  81. }
  82. public static void AssertSecurityBindingElement (
  83. SecurityAlgorithmSuite algorithm,
  84. bool includeTimestamp,
  85. SecurityKeyEntropyMode keyEntropyMode,
  86. MessageSecurityVersion messageSecurityVersion,
  87. SecurityHeaderLayout securityHeaderLayout,
  88. // EndpointSupportingTokenParameters
  89. int endorsing, int signed, int signedEncrypted, int signedEndorsing,
  90. // LocalClientSettings
  91. bool cacheCookies,
  92. int renewalThresholdPercentage,
  93. bool detectReplays,
  94. SecurityBindingElement be, string label)
  95. {
  96. Assert.AreEqual (algorithm, be.DefaultAlgorithmSuite, label + ".DefaultAlgorithmSuite");
  97. Assert.AreEqual (includeTimestamp, be.IncludeTimestamp, label + ".KeyEntropyMode");
  98. Assert.AreEqual (keyEntropyMode,
  99. be.KeyEntropyMode, label + "#3");
  100. Assert.AreEqual (messageSecurityVersion,
  101. be.MessageSecurityVersion, label + ".MessageSecurityVersion");
  102. Assert.AreEqual (securityHeaderLayout,
  103. be.SecurityHeaderLayout, label + ".SecurityHeaderLayout");
  104. // FIXME: they should be extracted step by step...
  105. // EndpointSupportingTokenParameters
  106. SupportingTokenParameters tp = be.EndpointSupportingTokenParameters;
  107. AssertSupportingTokenParameters (
  108. endorsing, signed, signedEncrypted, signedEndorsing,
  109. tp, label + ".Endpoint");
  110. // OptionalEndpointSupportingTokenParameters
  111. tp = be.OptionalEndpointSupportingTokenParameters;
  112. Assert.IsNotNull (tp, label + "#3-0");
  113. Assert.AreEqual (0, tp.Endorsing.Count, label + "#3-1");
  114. Assert.AreEqual (0, tp.Signed.Count, label + "#3-2");
  115. Assert.AreEqual (0, tp.SignedEncrypted.Count, label + "#3-3");
  116. Assert.AreEqual (0, tp.SignedEndorsing.Count, label + "#3-4");
  117. // OperationSupportingTokenParameters
  118. IDictionary<string,SupportingTokenParameters> oper = be.OperationSupportingTokenParameters;
  119. Assert.IsNotNull (oper, label + "#4-1");
  120. Assert.AreEqual (0, oper.Count, label + "#4-2");
  121. // OptionalOperationSupportingTokenParameters
  122. oper = be.OptionalOperationSupportingTokenParameters;
  123. Assert.IsNotNull (oper, label + "#5-1");
  124. Assert.AreEqual (0, oper.Count, label + "#5-2");
  125. // LocalClientSettings
  126. LocalClientSecuritySettings lc =
  127. be.LocalClientSettings;
  128. AssertLocalClientSecuritySettings (
  129. cacheCookies,
  130. renewalThresholdPercentage,
  131. detectReplays,
  132. lc, "");
  133. // FIXME: IdentityVerifier
  134. Assert.AreEqual (TimeSpan.FromMinutes (5), lc.MaxClockSkew, label + "#7-5");
  135. Assert.AreEqual (TimeSpan.MaxValue, lc.MaxCookieCachingTime, label + "#7-6");
  136. Assert.AreEqual (true, lc.ReconnectTransportOnFailure, label + "#7-7");
  137. Assert.AreEqual (900000, lc.ReplayCacheSize, label + "#7-8");
  138. Assert.AreEqual (TimeSpan.FromMinutes (5), lc.ReplayWindow, label + "#7-9");
  139. Assert.AreEqual (TimeSpan.FromHours (10), lc.SessionKeyRenewalInterval, label + "#7-10");
  140. Assert.AreEqual (TimeSpan.FromMinutes (5), lc.SessionKeyRolloverInterval, label + "#7-11");
  141. Assert.AreEqual (TimeSpan.FromMinutes (5), lc.TimestampValidityDuration, label + "#7-12");
  142. // FIXME: LocalServiceSettings
  143. }
  144. public static void AssertSymmetricSecurityBindingElement (
  145. SecurityAlgorithmSuite algorithm,
  146. bool includeTimestamp,
  147. SecurityKeyEntropyMode keyEntropyMode,
  148. MessageProtectionOrder messageProtectionOrder,
  149. MessageSecurityVersion messageSecurityVersion,
  150. bool requireSignatureConfirmation,
  151. SecurityHeaderLayout securityHeaderLayout,
  152. // EndpointSupportingTokenParameters
  153. int endorsing, int signed, int signedEncrypted, int signedEndorsing,
  154. // ProtectionTokenParameters
  155. bool hasProtectionTokenParameters,
  156. SecurityTokenInclusionMode protectionTokenInclusionMode,
  157. SecurityTokenReferenceStyle protectionTokenReferenceStyle,
  158. bool protectionTokenRequireDerivedKeys,
  159. // LocalClientSettings
  160. bool cacheCookies,
  161. int renewalThresholdPercentage,
  162. bool detectReplays,
  163. SymmetricSecurityBindingElement be, string label)
  164. {
  165. AssertSecurityBindingElement (
  166. algorithm,
  167. includeTimestamp,
  168. keyEntropyMode,
  169. messageSecurityVersion,
  170. securityHeaderLayout,
  171. // EndpointSupportingTokenParameters
  172. endorsing, signed, signedEncrypted, signedEndorsing,
  173. // LocalClientSettings
  174. cacheCookies,
  175. renewalThresholdPercentage,
  176. detectReplays,
  177. be, label);
  178. Assert.AreEqual (messageProtectionOrder, be.MessageProtectionOrder, label + ".MessageProtectionOrder");
  179. Assert.AreEqual (requireSignatureConfirmation, be.RequireSignatureConfirmation, label + ".RequireSignatureConfirmation");
  180. if (!hasProtectionTokenParameters)
  181. Assert.IsNull (be.ProtectionTokenParameters, label + ".ProtectionTokenParameters (null)");
  182. else
  183. AssertSecurityTokenParameters (
  184. protectionTokenInclusionMode,
  185. protectionTokenReferenceStyle,
  186. protectionTokenRequireDerivedKeys,
  187. be.ProtectionTokenParameters, label + ".ProtectionTokenParameters");
  188. }
  189. public static void AssertAsymmetricSecurityBindingElement (
  190. SecurityAlgorithmSuite algorithm,
  191. bool includeTimestamp,
  192. SecurityKeyEntropyMode keyEntropyMode,
  193. MessageProtectionOrder messageProtectionOrder,
  194. MessageSecurityVersion messageSecurityVersion,
  195. bool requireSignatureConfirmation,
  196. SecurityHeaderLayout securityHeaderLayout,
  197. // EndpointSupportingTokenParameters
  198. int endorsing, int signed, int signedEncrypted, int signedEndorsing,
  199. // InitiatorTokenParameters
  200. bool hasInitiatorTokenParameters,
  201. SecurityTokenInclusionMode initiatorTokenInclusionMode,
  202. SecurityTokenReferenceStyle initiatorTokenReferenceStyle,
  203. bool initiatorTokenRequireDerivedKeys,
  204. // RecipientTokenParameters
  205. bool hasRecipientTokenParameters,
  206. SecurityTokenInclusionMode recipientTokenInclusionMode,
  207. SecurityTokenReferenceStyle recipientTokenReferenceStyle,
  208. bool recipientTokenRequireDerivedKeys,
  209. // LocalClientSettings
  210. bool cacheCookies,
  211. int renewalThresholdPercentage,
  212. bool detectReplays,
  213. AsymmetricSecurityBindingElement be, string label)
  214. {
  215. AssertSecurityBindingElement (
  216. algorithm,
  217. includeTimestamp,
  218. keyEntropyMode,
  219. messageSecurityVersion,
  220. securityHeaderLayout,
  221. // EndpointSupportingTokenParameters
  222. endorsing, signed, signedEncrypted, signedEndorsing,
  223. // LocalClientSettings
  224. cacheCookies,
  225. renewalThresholdPercentage,
  226. detectReplays,
  227. be, label);
  228. Assert.AreEqual (messageProtectionOrder, be.MessageProtectionOrder, label + ".MessageProtectionOrder");
  229. Assert.AreEqual (requireSignatureConfirmation, be.RequireSignatureConfirmation, label + ".RequireSignatureConfirmation");
  230. if (!hasInitiatorTokenParameters)
  231. Assert.IsNull (be.InitiatorTokenParameters, label + ".InitiatorTokenParameters (null)");
  232. else
  233. AssertSecurityTokenParameters (
  234. initiatorTokenInclusionMode,
  235. initiatorTokenReferenceStyle,
  236. initiatorTokenRequireDerivedKeys,
  237. be.InitiatorTokenParameters, label + ".InitiatorTokenParameters");
  238. if (!hasRecipientTokenParameters)
  239. Assert.IsNull (be.RecipientTokenParameters, label + ".RecipientTokenParameters (null)");
  240. else
  241. AssertSecurityTokenParameters (
  242. recipientTokenInclusionMode,
  243. recipientTokenReferenceStyle,
  244. recipientTokenRequireDerivedKeys,
  245. be.RecipientTokenParameters, label + ".RecipientTokenParameters");
  246. }
  247. }
  248. }