InMemorySymmetricSecurityKeyTest.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. //
  2. // InMemorySymmetricSecurityKeyTest.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.IO;
  30. using System.Text;
  31. using System.IdentityModel.Selectors;
  32. using System.IdentityModel.Tokens;
  33. using System.Security.Cryptography;
  34. using System.Security.Cryptography.X509Certificates;
  35. using NUnit.Framework;
  36. using Key = System.IdentityModel.Tokens.InMemorySymmetricSecurityKey;
  37. using AES = System.Security.Cryptography.RijndaelManaged;
  38. namespace MonoTests.System.IdentityModel.Tokens
  39. {
  40. [TestFixture]
  41. public class InMemorySymmetricSecurityKeyTest
  42. {
  43. static X509Certificate2 cert;
  44. static byte [] raw;
  45. static byte [] wssc_label = Encoding.UTF8.GetBytes ("WS-SecureConversationWS-SecureConversation");
  46. static InMemorySymmetricSecurityKeyTest ()
  47. {
  48. cert = new X509Certificate2 ("Test/Resources/test.pfx", "mono");
  49. // randomly generated with RijndaelManaged
  50. // GenerateIV() and GenerateKey().
  51. raw = Convert.FromBase64String ("eX2EeE969RCv/5Lx8OIGLHtJrSD5PzVzO3tTy9JxU58=");
  52. }
  53. [Test]
  54. public void CreateSimple ()
  55. {
  56. Key key = new Key (raw);
  57. Assert.AreEqual (256, key.KeySize, "#1");
  58. // the returned value must be a clone.
  59. Assert.IsFalse (Object.ReferenceEquals (key.GetSymmetricKey (), raw), "#2");
  60. }
  61. [Test]
  62. public void GetSymmetricAlgorithmAES ()
  63. {
  64. byte [] bytes = new byte [32];
  65. Key key = new Key (bytes);
  66. SymmetricAlgorithm alg = key.GetSymmetricAlgorithm (
  67. SecurityAlgorithms.Aes128Encryption);
  68. Assert.AreEqual (256, alg.KeySize, "#1-1");
  69. Assert.AreEqual (CipherMode.CBC, alg.Mode, "#1-2");
  70. Assert.AreEqual (PaddingMode.PKCS7, alg.Padding, "#1-3");
  71. alg = key.GetSymmetricAlgorithm (SecurityAlgorithms.Aes192Encryption);
  72. Assert.AreEqual (256, alg.KeySize, "#2-1");
  73. Assert.AreEqual (CipherMode.CBC, alg.Mode, "#2-2");
  74. Assert.AreEqual (PaddingMode.PKCS7, alg.Padding, "#2-3");
  75. alg = key.GetSymmetricAlgorithm (SecurityAlgorithms.Aes256Encryption);
  76. Assert.AreEqual (256, alg.KeySize, "#3-1");
  77. Assert.AreEqual (CipherMode.CBC, alg.Mode, "#3-2");
  78. Assert.AreEqual (PaddingMode.PKCS7, alg.Padding, "#3-3");
  79. alg = key.GetSymmetricAlgorithm (SecurityAlgorithms.Aes128KeyWrap);
  80. Assert.IsTrue (alg is AES, "#4");
  81. alg = key.GetSymmetricAlgorithm (SecurityAlgorithms.Aes192KeyWrap);
  82. Assert.IsTrue (alg is AES, "#5");
  83. alg = key.GetSymmetricAlgorithm (SecurityAlgorithms.Aes256KeyWrap);
  84. Assert.IsTrue (alg is AES, "#6");
  85. //alg = key.GetSymmetricAlgorithm (SecurityAlgorithms.TripleDesKeyWrap);
  86. //Assert.IsTrue (alg is TripleDES, "#7");
  87. }
  88. [Test]
  89. [ExpectedException (typeof (CryptographicException))]
  90. public void GetSymmetricAlgorithm3VulnerableTDESEnc ()
  91. {
  92. byte [] bytes = new byte [24];
  93. Key key = new Key (bytes);
  94. // strange, TripleDesEncryption works with 32bytes key,
  95. // but TripleDesKeyWrap doesn't.
  96. key.GetSymmetricAlgorithm (SecurityAlgorithms.TripleDesEncryption);
  97. }
  98. [Test]
  99. [ExpectedException (typeof (CryptographicException))]
  100. public void GetSymmetricAlgorithm3VulnerableTDESWrap ()
  101. {
  102. byte [] bytes = new byte [24];
  103. Key key = new Key (bytes);
  104. // strange, TripleDesEncryption works with 32bytes key,
  105. // but TripleDesKeyWrap doesn't.
  106. key.GetSymmetricAlgorithm (SecurityAlgorithms.TripleDesKeyWrap);
  107. }
  108. // ... so, after all what is the valid key size for TDES?
  109. [Test]
  110. public void GetSymmetricAlgorithmNullKey ()
  111. {
  112. Key key = new Key (raw);
  113. Assert.IsNotNull (key.GetSymmetricAlgorithm (SecurityAlgorithms.Aes192Encryption));
  114. }
  115. [Test]
  116. // hmm, no error
  117. public void GetSymmetricAlgorithmWrongSize ()
  118. {
  119. Key key = new Key (new byte [32]);
  120. Assert.IsNotNull (key.GetSymmetricAlgorithm (SecurityAlgorithms.Aes192Encryption));
  121. }
  122. [Test]
  123. // hmm, error?
  124. [ExpectedException (typeof (CryptographicException))]
  125. public void GetSymmetricAlgorithmWrongSizeDES ()
  126. {
  127. Key key = new Key (new byte [32]);
  128. Assert.IsNotNull (key.GetSymmetricAlgorithm (SecurityAlgorithms.TripleDesKeyWrap));
  129. }
  130. [Test]
  131. // no error???
  132. public void GetSymmetricAlgorithmWrongSize2 ()
  133. {
  134. AES aes = new AES ();
  135. aes.KeySize = 192;
  136. aes.GenerateKey ();
  137. Key key = new Key (aes.Key);
  138. Assert.IsNotNull (key.GetSymmetricAlgorithm (SecurityAlgorithms.Aes256Encryption));
  139. }
  140. [Test]
  141. public void GenerateDerivedKey ()
  142. {
  143. Key key = new Key (raw);
  144. byte [] nonce = new byte [256];
  145. byte [] derived = key.GenerateDerivedKey (
  146. SecurityAlgorithms.Psha1KeyDerivation,
  147. wssc_label, nonce, key.KeySize, 0);
  148. Assert.IsTrue (Convert.ToBase64String (derived) != Convert.ToBase64String (raw), "#4");
  149. // the precomputed derivation value.
  150. byte [] expected = Convert.FromBase64String ("50UfLeg58TbfADujVeafUAS8typGX9LvqLOXezK/eJY=");
  151. Assert.AreEqual (Convert.ToBase64String (expected), Convert.ToBase64String (derived), "#5");
  152. }
  153. [Test]
  154. [ExpectedException (typeof (InvalidOperationException))] // not ArgumentNullException?
  155. public void GenerateDerivedKeyNullAlgorithm ()
  156. {
  157. Key key = new Key (raw);
  158. byte [] nonce = new byte [256];
  159. key.GenerateDerivedKey (null, wssc_label, nonce, key.KeySize, 0);
  160. }
  161. [Test]
  162. [ExpectedException (typeof (InvalidOperationException))] // not ArgumentNullException?
  163. public void GenerateDerivedKeyUnsupportedAlgorithm ()
  164. {
  165. Key key = new Key (raw);
  166. byte [] nonce = new byte [256];
  167. key.GenerateDerivedKey ("urn:my-own-way", wssc_label, nonce, key.KeySize, 0);
  168. }
  169. [Test]
  170. [ExpectedException (typeof (ArgumentNullException))]
  171. public void GenerateDerivedKeyNullLabel ()
  172. {
  173. Key key = new Key (raw);
  174. byte [] nonce = new byte [256];
  175. key.GenerateDerivedKey (
  176. SecurityAlgorithms.Psha1KeyDerivation,
  177. null, nonce, key.KeySize, 0);
  178. }
  179. [Test]
  180. [ExpectedException (typeof (ArgumentNullException))]
  181. public void GenerateDerivedKeyNullNonce ()
  182. {
  183. Key key = new Key (raw);
  184. byte [] nonce = new byte [256];
  185. key.GenerateDerivedKey (
  186. SecurityAlgorithms.Psha1KeyDerivation,
  187. wssc_label, null, key.KeySize, 0);
  188. }
  189. [Test]
  190. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  191. public void GenerateDerivedKeyNegativeLength ()
  192. {
  193. Key key = new Key (raw);
  194. byte [] nonce = new byte [256];
  195. key.GenerateDerivedKey (
  196. SecurityAlgorithms.Psha1KeyDerivation,
  197. wssc_label, nonce, -32, 0);
  198. }
  199. [Test]
  200. public void GenerateDerivedKeyUnusualLength ()
  201. {
  202. Key key = new Key (raw);
  203. byte [] nonce = new byte [256];
  204. key.GenerateDerivedKey (
  205. SecurityAlgorithms.Psha1KeyDerivation,
  206. wssc_label, nonce, 5, 0);
  207. }
  208. [Test]
  209. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  210. public void GenerateDerivedKeyNegativeOffset ()
  211. {
  212. Key key = new Key (raw);
  213. byte [] nonce = new byte [256];
  214. key.GenerateDerivedKey (
  215. SecurityAlgorithms.Psha1KeyDerivation,
  216. wssc_label, nonce, -32, 0);
  217. }
  218. [Test]
  219. public void GenerateDerivedKeyUnusualOffset ()
  220. {
  221. Key key = new Key (raw);
  222. byte [] nonce = new byte [256];
  223. key.GenerateDerivedKey (
  224. SecurityAlgorithms.Psha1KeyDerivation,
  225. wssc_label, nonce, 5, 0);
  226. }
  227. [Test]
  228. public void IsAsymmetricAlgorithm ()
  229. {
  230. Key key = new Key (raw);
  231. Assert.IsFalse (key.IsAsymmetricAlgorithm (SecurityAlgorithms.Aes256KeyWrap), "#1");
  232. Assert.IsFalse (key.IsAsymmetricAlgorithm (SecurityAlgorithms.TripleDesEncryption), "#2");
  233. Assert.IsTrue (key.IsAsymmetricAlgorithm (SecurityAlgorithms.RsaOaepKeyWrap), "#3");
  234. Assert.IsFalse (key.IsAsymmetricAlgorithm (SecurityAlgorithms.Psha1KeyDerivation), "#4");
  235. }
  236. [Test]
  237. public void IsSymmetricAlgorithm ()
  238. {
  239. Key key = new Key (raw);
  240. Assert.IsTrue (key.IsSymmetricAlgorithm (SecurityAlgorithms.Aes256KeyWrap), "#1");
  241. Assert.IsTrue (key.IsSymmetricAlgorithm (SecurityAlgorithms.TripleDesEncryption), "#2");
  242. Assert.IsFalse (key.IsSymmetricAlgorithm (SecurityAlgorithms.RsaOaepKeyWrap), "#3");
  243. Assert.IsTrue (key.IsSymmetricAlgorithm (SecurityAlgorithms.Psha1KeyDerivation), "#4");
  244. }
  245. }
  246. }