RSAPKCS1SignatureFormatterTest.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. //
  2. // RSAPKCS1SignatureFormatterTest.cs - NUnit tests for PKCS#1 v.1.5 signature.
  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. public class RSAPKCS1SignatureFormatterTest : TestCase {
  14. protected override void SetUp () {}
  15. protected override void TearDown () {}
  16. public void AssertEquals (string msg, byte[] array1, byte[] array2)
  17. {
  18. AllTests.AssertEquals (msg, array1, array2);
  19. }
  20. public void TestConstructors ()
  21. {
  22. RSAPKCS1SignatureFormatter fmt;
  23. fmt = new RSAPKCS1SignatureFormatter ();
  24. AssertNotNull ("RSAPKCS1SignatureFormatter()", fmt);
  25. fmt = new RSAPKCS1SignatureFormatter (null);
  26. AssertNotNull ("RSAPKCS1SignatureFormatter(null)", fmt);
  27. RSA rsa = RSA.Create ();
  28. fmt = new RSAPKCS1SignatureFormatter (rsa);
  29. AssertNotNull ("RSAPKCS1SignatureFormatter(rsa)", fmt);
  30. DSA dsa = DSA.Create ();
  31. try {
  32. fmt = new RSAPKCS1SignatureFormatter (dsa);
  33. Fail ("Expected InvalidCastException but got none");
  34. }
  35. catch (InvalidCastException) {
  36. // do nothing, this is what we expect
  37. }
  38. catch (Exception e) {
  39. Fail ("Expected InvalidCastException but got: " + e.ToString ());
  40. }
  41. }
  42. public void TestSetKey ()
  43. {
  44. RSAPKCS1SignatureFormatter fmt;
  45. fmt = new RSAPKCS1SignatureFormatter ();
  46. try {
  47. fmt.SetKey (RSA.Create ());
  48. }
  49. catch (Exception e) {
  50. Fail ("Unexpected exception: " + e.ToString ());
  51. }
  52. try {
  53. fmt.SetKey (DSA.Create ());
  54. Fail ("Expected InvalidCastException but got none");
  55. }
  56. catch (InvalidCastException) {
  57. // do nothing, this is what we expect
  58. }
  59. catch (Exception e) {
  60. Fail ("Expected InvalidCastException but got: " + e.ToString ());
  61. }
  62. try {
  63. fmt.SetKey (null);
  64. }
  65. catch (Exception e) {
  66. Fail ("Unexpected exception: " + e.ToString ());
  67. }
  68. }
  69. public void TestSetHashAlgorithm ()
  70. {
  71. RSAPKCS1SignatureFormatter fmt;
  72. fmt = new RSAPKCS1SignatureFormatter ();
  73. try {
  74. fmt.SetHashAlgorithm ("SHA1");
  75. }
  76. catch (Exception e) {
  77. Fail ("Unexpected exception: " + e.ToString ());
  78. }
  79. try {
  80. fmt.SetHashAlgorithm ("MD5");
  81. }
  82. catch (Exception e) {
  83. Fail ("Unexpected exception: " + e.ToString ());
  84. }
  85. try {
  86. fmt.SetHashAlgorithm ("SHA256");
  87. }
  88. catch (Exception e) {
  89. Fail ("Unexpected exception: " + e.ToString ());
  90. }
  91. try {
  92. fmt.SetHashAlgorithm ("SHA384");
  93. }
  94. catch (Exception e) {
  95. Fail ("Unexpected exception: " + e.ToString ());
  96. }
  97. try {
  98. fmt.SetHashAlgorithm ("SHA512");
  99. }
  100. catch (Exception e) {
  101. Fail ("Unexpected exception: " + e.ToString ());
  102. }
  103. }
  104. // see: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpcongeneratingsignatures.asp
  105. public void TestCreateSignature ()
  106. {
  107. RSAPKCS1SignatureFormatter fmt = new RSAPKCS1SignatureFormatter ();
  108. // no hash algorithm
  109. byte[] hash = new byte [1];
  110. byte[] signature = null;
  111. try {
  112. signature = fmt.CreateSignature (hash);
  113. Fail ("CreateSignature(?) no hash algo - Expected CryptographicUnexpectedOperationException but none");
  114. }
  115. catch (CryptographicUnexpectedOperationException) {
  116. // this was expected
  117. }
  118. catch (Exception e) {
  119. Fail ("CreateSignature(?) no hash algo - Expected CryptographicUnexpectedOperationException but got: " + e.ToString ());
  120. }
  121. // no key
  122. fmt.SetHashAlgorithm ("SHA1");
  123. hash = new byte [20];
  124. try {
  125. signature = fmt.CreateSignature (hash);
  126. Fail ("CreateSignature(?) no key - Expected CryptographicUnexpectedOperationException but none");
  127. }
  128. catch (CryptographicUnexpectedOperationException) {
  129. // this was expected
  130. }
  131. catch (Exception e) {
  132. Fail ("CreateSignature(?) no key - Expected CryptographicUnexpectedOperationException but got: " + e.ToString ());
  133. }
  134. // we need the private key
  135. RSA rsa = RSA.Create ();
  136. rsa.ImportParameters (AllTests.GetRsaKey (true));
  137. fmt.SetKey (rsa);
  138. // good SHA1
  139. fmt.SetHashAlgorithm ("SHA1");
  140. hash = new byte [20];
  141. signature = fmt.CreateSignature (hash);
  142. AssertNotNull ("CreateSignature(SHA1)", signature);
  143. // wrong length SHA1
  144. fmt.SetHashAlgorithm ("SHA1");
  145. hash = new byte [19];
  146. try {
  147. signature = fmt.CreateSignature (hash);
  148. Fail ("CreateSignature(badSHA1) - Should have thrown an CryptographicException");
  149. }
  150. catch (CryptographicException) {
  151. // this is expected (invalid hash length)
  152. }
  153. catch (Exception e) {
  154. Fail ("CreateSignature(badSHA1) - Unexpected exception: " + e.ToString ());
  155. }
  156. // good MD5
  157. fmt.SetHashAlgorithm ("MD5");
  158. hash = new byte [16];
  159. signature = fmt.CreateSignature (hash);
  160. AssertNotNull ("CreateSignature(MD5)", signature);
  161. // good SHA256
  162. fmt.SetHashAlgorithm ("SHA256");
  163. hash = new byte [32];
  164. try {
  165. signature = fmt.CreateSignature (hash);
  166. }
  167. catch (CryptographicException) {
  168. // unknown OID !!!
  169. }
  170. catch (Exception e) {
  171. Fail ("CreateSignature(badSHA256) - Unexpected exception: " + e.ToString ());
  172. }
  173. // good SHA384
  174. fmt.SetHashAlgorithm ("SHA384");
  175. hash = new byte [48];
  176. try {
  177. signature = fmt.CreateSignature (hash);
  178. }
  179. catch (CryptographicException) {
  180. // unknown OID !!!
  181. }
  182. catch (Exception e) {
  183. Fail ("CreateSignature(badSHA384) - Unexpected exception: " + e.ToString ());
  184. }
  185. // good SHA512
  186. fmt.SetHashAlgorithm ("SHA512");
  187. hash = new byte [64];
  188. try {
  189. signature = fmt.CreateSignature (hash);
  190. }
  191. catch (CryptographicException) {
  192. // unknown OID !!!
  193. }
  194. catch (Exception e) {
  195. Fail ("CreateSignature(badSHA512) - Unexpected exception: " + e.ToString ());
  196. }
  197. // null (bad ;-)
  198. hash = null;
  199. try {
  200. signature = fmt.CreateSignature (hash);
  201. Fail ("Expected ArgumentNullException but none");
  202. }
  203. catch (ArgumentNullException) {
  204. // this is was we expect
  205. }
  206. catch (Exception e) {
  207. Fail ("Expected ArgumentNullException but got: " + e.ToString ());
  208. }
  209. }
  210. public void TestCreateSignatureHash ()
  211. {
  212. RSAPKCS1SignatureFormatter fmt = new RSAPKCS1SignatureFormatter ();
  213. HashAlgorithm hash = null;
  214. byte[] data = new byte [20];
  215. // no hash algorithm
  216. byte[] signature = null;
  217. try {
  218. signature = fmt.CreateSignature (hash);
  219. Fail ("CreateSignature(?) no hash algo - Expected ArgumentNullException but none");
  220. }
  221. catch (ArgumentNullException) {
  222. // this was expected
  223. }
  224. catch (Exception e) {
  225. Fail ("CreateSignature(?) no hash algo - Expected ArgumentNullException but got: " + e.ToString ());
  226. }
  227. // no key
  228. hash = SHA1.Create ();
  229. hash.ComputeHash (data);
  230. try {
  231. signature = fmt.CreateSignature (hash);
  232. Fail ("CreateSignature(?) no key - Expected CryptographicUnexpectedOperationException but none");
  233. }
  234. catch (CryptographicUnexpectedOperationException) {
  235. // this was expected
  236. }
  237. catch (Exception e) {
  238. Fail ("CreateSignature(?) no key - Expected CryptographicUnexpectedOperationException but got: " + e.ToString ());
  239. }
  240. // we need the private key
  241. RSA rsa = RSA.Create ();
  242. rsa.ImportParameters (AllTests.GetRsaKey (true));
  243. fmt.SetKey (rsa);
  244. // good SHA1
  245. hash = SHA1.Create ();
  246. hash.ComputeHash (data);
  247. signature = fmt.CreateSignature (hash);
  248. AssertNotNull ("CreateSignature(SHA1)", signature);
  249. // good MD5
  250. hash = MD5.Create ();
  251. hash.ComputeHash (data);
  252. signature = fmt.CreateSignature (hash);
  253. AssertNotNull ("CreateSignature(MD5)", signature);
  254. // good SHA256
  255. hash = SHA256.Create ();
  256. hash.ComputeHash (data);
  257. try {
  258. signature = fmt.CreateSignature (hash);
  259. }
  260. catch (CryptographicException) {
  261. // unknown OID !!!
  262. }
  263. catch (Exception e) {
  264. Fail ("CreateSignature(badSHA256) - Unexpected exception: " + e.ToString ());
  265. }
  266. // good SHA384
  267. hash = SHA384.Create ();
  268. hash.ComputeHash (data);
  269. try {
  270. signature = fmt.CreateSignature (hash);
  271. }
  272. catch (CryptographicException) {
  273. // unknown OID !!!
  274. }
  275. catch (Exception e) {
  276. Fail ("CreateSignature(badSHA384) - Unexpected exception: " + e.ToString ());
  277. }
  278. // good SHA512
  279. hash = SHA512.Create ();
  280. hash.ComputeHash (data);
  281. try {
  282. signature = fmt.CreateSignature (hash);
  283. }
  284. catch (CryptographicException) {
  285. // unknown OID !!!
  286. }
  287. catch (Exception e) {
  288. Fail ("CreateSignature(badSHA512) - Unexpected exception: " + e.ToString ());
  289. }
  290. }
  291. }
  292. }