CryptoConfigTest.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. //
  2. // CryptoConfigTest.cs - NUnit Test Cases for CryptoConfig
  3. //
  4. // Author:
  5. // Sebastien Pouliot ([email protected])
  6. //
  7. // (C) 2002 Motus Technologies Inc. (http://www.motus.com)
  8. //
  9. using NUnit.Framework;
  10. using System;
  11. using System.Security.Cryptography;
  12. namespace MonoTests.System.Security.Cryptography
  13. {
  14. public class CryptoConfigTest : TestCase
  15. {
  16. public CryptoConfigTest () : base ("System.Security.Cryptography.CryptoConfig testsuite") {}
  17. public CryptoConfigTest (string name) : base (name) {}
  18. protected override void SetUp () {}
  19. protected override void TearDown () {}
  20. public static ITest Suite {
  21. get {
  22. return new TestSuite (typeof (CryptoConfigTest));
  23. }
  24. }
  25. public void AssertEquals (string msg, byte[] array1, byte[] array2)
  26. {
  27. AllTests.AssertEquals (msg, array1, array2);
  28. }
  29. void CreateFromName (string name, string objectname)
  30. {
  31. object o = CryptoConfig.CreateFromName (name);
  32. AssertEquals (name, objectname, o.ToString());
  33. }
  34. // validate that CryptoConfig create the exact same implementation between mono and MS
  35. public void TestCreateFromName ()
  36. {
  37. try {
  38. object o = CryptoConfig.CreateFromName (null);
  39. }
  40. catch (ArgumentNullException) {
  41. // do nothing, this is what we expect
  42. }
  43. catch (Exception e) {
  44. Fail ("ArgumentNullException not thrown: " + e.ToString());
  45. }
  46. CreateFromName ("SHA", "System.Security.Cryptography.SHA1CryptoServiceProvider");
  47. // FIXME: We need to support the machine.config file to get exact same results
  48. // with the MS .NET Framework
  49. CreateFromName ("SHA1", "System.Security.Cryptography.SHA1CryptoServiceProvider");
  50. CreateFromName( "System.Security.Cryptography.SHA1", "System.Security.Cryptography.SHA1CryptoServiceProvider");
  51. // after installing the WSDK - changes to the machine.config file (not documented)
  52. // CreateFromName ("SHA1", "System.Security.Cryptography.SHA1Managed");
  53. // CreateFromName ("System.Security.Cryptography.SHA1", "System.Security.Cryptography.SHA1Managed");
  54. CreateFromName ("System.Security.Cryptography.HashAlgorithm", "System.Security.Cryptography.SHA1CryptoServiceProvider");
  55. CreateFromName ("MD5", "System.Security.Cryptography.MD5CryptoServiceProvider");
  56. CreateFromName ("System.Security.Cryptography.MD5", "System.Security.Cryptography.MD5CryptoServiceProvider");
  57. CreateFromName ("SHA256", "System.Security.Cryptography.SHA256Managed");
  58. CreateFromName ("SHA-256", "System.Security.Cryptography.SHA256Managed");
  59. CreateFromName ("System.Security.Cryptography.SHA256", "System.Security.Cryptography.SHA256Managed");
  60. CreateFromName ("SHA384", "System.Security.Cryptography.SHA384Managed");
  61. CreateFromName ("SHA-384", "System.Security.Cryptography.SHA384Managed");
  62. CreateFromName ("System.Security.Cryptography.SHA384", "System.Security.Cryptography.SHA384Managed");
  63. CreateFromName ("SHA512", "System.Security.Cryptography.SHA512Managed");
  64. CreateFromName ("SHA-512", "System.Security.Cryptography.SHA512Managed");
  65. CreateFromName ("System.Security.Cryptography.SHA512", "System.Security.Cryptography.SHA512Managed");
  66. /*
  67. //FIXME: these algorithms are not in corlib yet. re-enable when they are
  68. CreateFromName ("System.Security.Cryptography.KeyedHashAlgorithm", "System.Security.Cryptography.HMACSHA1");
  69. CreateFromName ("HMACSHA1", "System.Security.Cryptography.HMACSHA1");
  70. CreateFromName ("System.Security.Cryptography.HMACSHA1", "System.Security.Cryptography.HMACSHA1");
  71. CreateFromName ("MACTripleDES", "System.Security.Cryptography.MACTripleDES");
  72. CreateFromName ("System.Security.Cryptography.MACTripleDES", "System.Security.Cryptography.MACTripleDES");
  73. CreateFromName ("RSA", "System.Security.Cryptography.RSACryptoServiceProvider");
  74. CreateFromName ("System.Security.Cryptography.RSA", "System.Security.Cryptography.RSACryptoServiceProvider");
  75. CreateFromName ("System.Security.Cryptography.AsymmetricAlgorithm", "System.Security.Cryptography.RSACryptoServiceProvider");
  76. CreateFromName ("DSA", "System.Security.Cryptography.DSACryptoServiceProvider");
  77. CreateFromName ("System.Security.Cryptography.DSA", "System.Security.Cryptography.DSACryptoServiceProvider");
  78. CreateFromName ("DES", "System.Security.Cryptography.DESCryptoServiceProvider");
  79. CreateFromName ("System.Security.Cryptography.DES", "System.Security.Cryptography.DESCryptoServiceProvider");
  80. CreateFromName ("3DES", "System.Security.Cryptography.TripleDESCryptoServiceProvider");
  81. CreateFromName ("TripleDES", "System.Security.Cryptography.TripleDESCryptoServiceProvider");
  82. CreateFromName ("Triple DES", "System.Security.Cryptography.TripleDESCryptoServiceProvider");
  83. CreateFromName ("System.Security.Cryptography.TripleDES", "System.Security.Cryptography.TripleDESCryptoServiceProvider");
  84. */
  85. // LAMESPEC SymmetricAlgorithm documented as TripleDESCryptoServiceProvider
  86. CreateFromName ("System.Security.Cryptography.SymmetricAlgorithm", "System.Security.Cryptography.RijndaelManaged");
  87. CreateFromName ("RC2", "System.Security.Cryptography.RC2CryptoServiceProvider");
  88. CreateFromName ("System.Security.Cryptography.RC2", "System.Security.Cryptography.RC2CryptoServiceProvider");
  89. CreateFromName ("Rijndael", "System.Security.Cryptography.RijndaelManaged");
  90. CreateFromName ("System.Security.Cryptography.Rijndael", "System.Security.Cryptography.RijndaelManaged");
  91. // LAMESPEC Undocumented Names in CryptoConfig
  92. CreateFromName ("RandomNumberGenerator", "System.Security.Cryptography.RNGCryptoServiceProvider");
  93. CreateFromName ("System.Security.Cryptography.RandomNumberGenerator", "System.Security.Cryptography.RNGCryptoServiceProvider");
  94. // non existing algo should return null (without exception)
  95. AssertNull ("NonExistingAlgorithm", CryptoConfig.CreateFromName("NonExistingAlgorithm"));
  96. }
  97. // Tests created using "A Layer Man Guide to ASN.1" from RSA, page 19-20
  98. // Need to find an OID ? goto http://www.alvestrand.no/~hta/objectid/top.html
  99. static byte[] oidETSI = { 0x06, 0x03, 0x04, 0x00, 0x00 };
  100. static byte[] oidSHA1 = { 0x06, 0x05, 0x2B, 0x0E, 0x03, 0x02, 0x1A };
  101. static byte[] oidASN1CharacterModule = { 0x06, 0x04, 0x51, 0x00, 0x00, 0x00 };
  102. static byte[] oidmd5withRSAEncryption = { 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x04 };
  103. // LAMESPEC NullReferenceException is thrown (not ArgumentNullException) if parameter is NULL
  104. public void TestEncodeOID ()
  105. {
  106. try {
  107. byte[] o = CryptoConfig.EncodeOID (null);
  108. }
  109. catch (NullReferenceException) {
  110. // do nothing, this is what we expect
  111. }
  112. catch (Exception e) {
  113. Fail ("NullReferenceException not thrown: " + e.ToString());
  114. }
  115. // OID starts with 0, 1 or 2
  116. AssertEquals ("OID starting with 0.", oidETSI, CryptoConfig.EncodeOID ("0.4.0.0"));
  117. AssertEquals ("OID starting with 1.", oidSHA1, CryptoConfig.EncodeOID ("1.3.14.3.2.26"));
  118. AssertEquals ("OID starting with 2.", oidASN1CharacterModule, CryptoConfig.EncodeOID ("2.1.0.0.0"));
  119. // OID numbers can span multiple bytes
  120. AssertEquals ("OID with numbers spanning multiple bytes", oidmd5withRSAEncryption, CryptoConfig.EncodeOID ("1.2.840.113549.1.1.4"));
  121. // "ms"-invalid OID - greater than 127 bytes (length encoding)
  122. // OID longer than 127 bytes (so length must be encoded on multiple bytes)
  123. // LAMESPEC: OID greater that 0x7F (127) bytes aren't supported by the MS Framework
  124. string baseOID = "1.3.6.1.4.1.11071.0.";
  125. string lastPart = "1111111111"; // must fit in int32
  126. for (int i = 1; i < 30; i++)
  127. {
  128. baseOID += lastPart + ".";
  129. }
  130. baseOID += "0";
  131. try {
  132. byte[] tooLongOID = CryptoConfig.EncodeOID (baseOID);
  133. }
  134. catch (CryptographicUnexpectedOperationException) {
  135. // do nothing, this is what we expect
  136. }
  137. catch (Exception e) {
  138. Fail ("CryptographicUnexpectedOperationException not thrown: " + e.ToString());
  139. }
  140. // "ms"-invalid OID - where a number of the OID > Int32
  141. // LAMESPEC: OID with numbers > Int32 aren't supported by the MS BCL
  142. try {
  143. byte[] tooLongOID = CryptoConfig.EncodeOID ("1.1.4294967295");
  144. }
  145. catch (OverflowException) {
  146. // do nothing, this is what we expect
  147. }
  148. catch (Exception e) {
  149. Fail( "OverflowException not thrown: " + e.ToString ());
  150. }
  151. // invalid OID - must start with 0, 1 or 2
  152. // however it works with MS BCL
  153. byte[] oid3 = CryptoConfig.EncodeOID ("3.0");
  154. byte[] res3 = { 0x06, 0x01, 0x78 };
  155. AssertEquals ("OID: 3.0", res3, oid3);
  156. // invalid OID - must have at least 2 parts (according to X.208)
  157. try {
  158. byte[] tooShortOID = CryptoConfig.EncodeOID ("0");
  159. }
  160. catch (CryptographicUnexpectedOperationException) {
  161. // do nothing, this is what we expect
  162. }
  163. catch (Exception e) {
  164. Fail("CryptographicUnexpectedOperationException not thrown: " + e.ToString());
  165. }
  166. // invalid OID - second value < 40 for 0. and 1. (modulo 40)
  167. // however it works with MS BCL
  168. byte[] tooBigSecondPartOID = CryptoConfig.EncodeOID ("0.40");
  169. byte[] tooBigSecondPartRes = { 0x06, 0x01, 0x28 };
  170. AssertEquals ("OID: 0.40", tooBigSecondPartRes, tooBigSecondPartOID);
  171. }
  172. private void MapNameToOID (string name, string oid)
  173. {
  174. AssertEquals ("oid(" + name + ")", oid, CryptoConfig.MapNameToOID (name));
  175. }
  176. // LAMESPEC doesn't support all names defined in CryptoConfig
  177. // non supported names (in MSFW) are commented or null-ed
  178. public void TestMapNameToOID()
  179. {
  180. try {
  181. CryptoConfig.MapNameToOID (null);
  182. }
  183. catch (ArgumentNullException) {
  184. // do nothing, this is what we expect
  185. }
  186. catch (Exception e) {
  187. Fail( "ArgumentNullException not thrown: " + e.ToString ());
  188. }
  189. // MapNameToOID ("SHA", "1.3.14.3.2.26");
  190. MapNameToOID ("SHA1", "1.3.14.3.2.26");
  191. MapNameToOID ("System.Security.Cryptography.SHA1", "1.3.14.3.2.26");
  192. // MapNameToOID ("System.Security.Cryptography.HashAlgorithm", "1.3.14.3.2.26");
  193. MapNameToOID ("MD5", "1.2.840.113549.2.5");
  194. MapNameToOID ("System.Security.Cryptography.MD5", "1.2.840.113549.2.5");
  195. MapNameToOID ("SHA256", "2.16.840.1.101.3.4.1");
  196. // MapNameToOID ("SHA-256", "2.16.840.1.101.3.4.1");
  197. MapNameToOID ("System.Security.Cryptography.SHA256", "2.16.840.1.101.3.4.1");
  198. MapNameToOID ("SHA384", "2.16.840.1.101.3.4.2");
  199. // MapNameToOID ("SHA-384", "2.16.840.1.101.3.4.2");
  200. MapNameToOID ("System.Security.Cryptography.SHA384", "2.16.840.1.101.3.4.2");
  201. MapNameToOID ("SHA512", "2.16.840.1.101.3.4.3");
  202. // MapNameToOID ("SHA-512", "2.16.840.1.101.3.4.3");
  203. MapNameToOID ("System.Security.Cryptography.SHA512", "2.16.840.1.101.3.4.3");
  204. // no OID defined ?
  205. MapNameToOID ("RSA", null);
  206. MapNameToOID ("System.Security.Cryptography.RSA", null);
  207. MapNameToOID ("System.Security.Cryptography.AsymmetricAlgorithm", null);
  208. MapNameToOID ("DSA", null);
  209. MapNameToOID ("System.Security.Cryptography.DSA", null);
  210. MapNameToOID ("DES", null);
  211. MapNameToOID ("System.Security.Cryptography.DES", null);
  212. MapNameToOID ("3DES", null);
  213. MapNameToOID ("TripleDES", null);
  214. MapNameToOID ("Triple DES", null);
  215. MapNameToOID ("System.Security.Cryptography.TripleDES", null);
  216. MapNameToOID ("RC2", null);
  217. MapNameToOID ("System.Security.Cryptography.RC2", null);
  218. MapNameToOID ("Rijndael", null);
  219. MapNameToOID ("System.Security.Cryptography.Rijndael", null);
  220. MapNameToOID ("System.Security.Cryptography.SymmetricAlgorithm", null);
  221. // LAMESPEC Undocumented Names in CryptoConfig
  222. MapNameToOID ("RandomNumberGenerator", null);
  223. MapNameToOID ("System.Security.Cryptography.RandomNumberGenerator", null);
  224. MapNameToOID ("System.Security.Cryptography.KeyedHashAlgorithm", null);
  225. MapNameToOID ("HMACSHA1", null);
  226. MapNameToOID ("System.Security.Cryptography.HMACSHA1", null);
  227. MapNameToOID ("MACTripleDES", null);
  228. MapNameToOID ("System.Security.Cryptography.MACTripleDES", null);
  229. // non existing algo should return null (without exception)
  230. MapNameToOID ("NonExistingAlgorithm", null);
  231. }
  232. public void TestToString()
  233. {
  234. // under normal circumstance there are no need to create a CryptoConfig object
  235. // because all interesting stuff are in static methods
  236. CryptoConfig cc = new CryptoConfig ();
  237. AssertEquals ("System.Security.Cryptography.CryptoConfig", cc.ToString ());
  238. }
  239. }
  240. }