WrappedKeySecurityTokenTest.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. //
  2. // WrappedKeySecurityTokenTest.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 2007 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.IdentityModel.Selectors;
  32. using System.IdentityModel.Tokens;
  33. using System.Security.Cryptography.X509Certificates;
  34. using System.ServiceModel;
  35. using System.ServiceModel.Channels;
  36. using System.ServiceModel.Security;
  37. using System.ServiceModel.Security.Tokens;
  38. using NUnit.Framework;
  39. namespace MonoTests.System.ServiceModel
  40. {
  41. [TestFixture]
  42. public class WrappedKeySecurityTokenTest
  43. {
  44. static readonly X509Certificate2 cert =
  45. new X509Certificate2 ("Test/Resources/test.pfx", "mono");
  46. WrappedKeySecurityToken GetReferent ()
  47. {
  48. string id = "referent";
  49. byte [] key = new byte [32];
  50. X509SecurityToken token = new X509SecurityToken (cert);
  51. SecurityKeyIdentifierClause kic =
  52. new X509ThumbprintKeyIdentifierClause (cert);
  53. string alg = SecurityAlgorithms.RsaOaepKeyWrap;
  54. return new WrappedKeySecurityToken (id, key, alg, token,
  55. new SecurityKeyIdentifier (kic));
  56. }
  57. [Test]
  58. [ExpectedException (typeof (ArgumentNullException))]
  59. public void CtorNullId ()
  60. {
  61. WrappedKeySecurityToken w = GetReferent ();
  62. new WrappedKeySecurityToken (null, new byte [32],
  63. w.WrappingAlgorithm,
  64. w.WrappingToken,
  65. w.WrappingTokenReference);
  66. }
  67. [Test]
  68. [ExpectedException (typeof (ArgumentNullException))]
  69. public void CtorNullKey ()
  70. {
  71. WrappedKeySecurityToken w = GetReferent ();
  72. new WrappedKeySecurityToken (w.Id, null,
  73. w.WrappingAlgorithm,
  74. w.WrappingToken,
  75. w.WrappingTokenReference);
  76. }
  77. [Test]
  78. [ExpectedException (typeof (ArgumentNullException))]
  79. public void CtorNullWrappingAlgorithm ()
  80. {
  81. WrappedKeySecurityToken w = GetReferent ();
  82. new WrappedKeySecurityToken (w.Id, new byte [32],
  83. null,
  84. w.WrappingToken,
  85. w.WrappingTokenReference);
  86. }
  87. [Test]
  88. [ExpectedException (typeof (ArgumentNullException))]
  89. public void CtorNullWrappingToken ()
  90. {
  91. WrappedKeySecurityToken w = GetReferent ();
  92. new WrappedKeySecurityToken (w.Id, new byte [32],
  93. w.WrappingAlgorithm,
  94. null,
  95. w.WrappingTokenReference);
  96. }
  97. [Test]
  98. // null SecurityKeyIdentifier is allowed.
  99. //[ExpectedException (typeof (ArgumentNullException))]
  100. public void CtorNullWrappingTokenReference ()
  101. {
  102. WrappedKeySecurityToken w = GetReferent ();
  103. new WrappedKeySecurityToken (w.Id, new byte [32],
  104. w.WrappingAlgorithm,
  105. w.WrappingToken,
  106. null);
  107. }
  108. [Test]
  109. [ExpectedException (typeof (ArgumentException))]
  110. public void UserNameToken () // it does not support any encryption operation.
  111. {
  112. byte [] bytes = new byte [32];
  113. SecurityToken wt = new UserNameSecurityToken ("eno", "enopass");
  114. SecurityKeyIdentifierClause kic =
  115. new X509ThumbprintKeyIdentifierClause (cert);
  116. new WrappedKeySecurityToken ("urn:gyabo",
  117. bytes, SecurityAlgorithms.RsaOaepKeyWrap, wt,
  118. new SecurityKeyIdentifier (kic));
  119. }
  120. [Test]
  121. [ExpectedException (typeof (ArgumentException))]
  122. public void X509TokenForSymmetricKeyWrap ()
  123. {
  124. byte [] bytes = new byte [32];
  125. SecurityToken wt = new X509SecurityToken (cert);
  126. SecurityKeyIdentifierClause kic =
  127. new X509ThumbprintKeyIdentifierClause (cert);
  128. new WrappedKeySecurityToken ("urn:gyabo",
  129. bytes, SecurityAlgorithms.Aes256KeyWrap, wt,
  130. new SecurityKeyIdentifier (kic));
  131. }
  132. [Test]
  133. public void BinarySecretTokenForSymmetricKeyWrap ()
  134. {
  135. byte [] bytes = new byte [32];
  136. SecurityToken wt = new BinarySecretSecurityToken (bytes);
  137. SecurityKeyIdentifierClause kic =
  138. new X509ThumbprintKeyIdentifierClause (cert);
  139. new WrappedKeySecurityToken ("urn:gyabo",
  140. bytes, SecurityAlgorithms.Aes256KeyWrap, wt,
  141. new SecurityKeyIdentifier (kic));
  142. }
  143. [Test]
  144. [ExpectedException (typeof (ArgumentException))]
  145. public void BinarySecretTokenForAsymmetricKeyWrap ()
  146. {
  147. byte [] bytes = new byte [32];
  148. SecurityToken wt = new BinarySecretSecurityToken (bytes);
  149. SecurityKeyIdentifierClause kic =
  150. new X509ThumbprintKeyIdentifierClause (cert);
  151. new WrappedKeySecurityToken ("urn:gyabo",
  152. bytes, SecurityAlgorithms.RsaOaepKeyWrap, wt,
  153. new SecurityKeyIdentifier (kic));
  154. }
  155. [Test]
  156. [ExpectedException (typeof (NotSupportedException))]
  157. public void CreateBinarySecretKeyIdentifierClause ()
  158. {
  159. byte [] bytes = new byte [32];
  160. SecurityToken wt = new BinarySecretSecurityToken (bytes);
  161. SecurityKeyIdentifierClause kic =
  162. new BinarySecretKeyIdentifierClause (bytes);
  163. WrappedKeySecurityToken token = new WrappedKeySecurityToken ("urn:gyabo",
  164. bytes, SecurityAlgorithms.Aes256KeyWrap, wt,
  165. new SecurityKeyIdentifier (kic));
  166. token.CreateKeyIdentifierClause<BinarySecretKeyIdentifierClause> ();
  167. }
  168. [Test]
  169. public void X509WrappingToken1 ()
  170. {
  171. byte [] bytes = new byte [32];
  172. X509SecurityToken xt = new X509SecurityToken (cert);
  173. SecurityKeyIdentifierClause kic =
  174. new X509ThumbprintKeyIdentifierClause (cert);
  175. string alg = SecurityAlgorithms.RsaOaepKeyWrap;
  176. WrappedKeySecurityToken token = new WrappedKeySecurityToken ("urn:gyabo",
  177. bytes, alg, xt,
  178. new SecurityKeyIdentifier (kic));
  179. Assert.AreEqual ("urn:gyabo", token.Id, "#1");
  180. Assert.AreEqual (alg, token.WrappingAlgorithm, "#3");
  181. Assert.AreEqual (xt, token.WrappingToken, "#4");
  182. Assert.AreEqual (1, token.WrappingTokenReference.Count, "#5");
  183. Assert.AreEqual (1, token.SecurityKeys.Count, "#6");
  184. Assert.IsTrue (token.SecurityKeys [0] is InMemorySymmetricSecurityKey, "#7");
  185. Assert.AreEqual (bytes, new X509AsymmetricSecurityKey (cert).DecryptKey (token.WrappingAlgorithm, token.GetWrappedKey ()), "#8");
  186. // wrapped keys cannot be compared, due to the nature of rsa-oaep.
  187. // Assert.AreEqual (new X509AsymmetricSecurityKey (cert).EncryptKey (token.WrappingAlgorithm, bytes), token.GetWrappedKey (), "#9-1");
  188. // Assert.AreEqual (token.GetWrappedKey (), new WrappedKeySecurityToken ("urn:gyabo",
  189. // bytes, alg, xt,
  190. // new SecurityKeyIdentifier (kic)).GetWrappedKey (), "#9");
  191. }
  192. }
  193. }