t-support.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /* t-support.h - Helper routines for regression tests.
  2. Copyright (C) 2006 g10 Code GmbH
  3. This file is part of Scute.
  4. Scute is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. Scute is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with Scute; if not, write to the Free Software Foundation,
  14. Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  15. In addition, as a special exception, g10 Code GmbH gives permission
  16. to link this library: with the Mozilla Foundation's code for
  17. Mozilla (or with modified versions of it that use the same license
  18. as the "Mozilla" code), and distribute the linked executables. You
  19. must obey the GNU General Public License in all respects for all of
  20. the code used other than "Mozilla". If you modify this file, you
  21. may extend this exception to your version of the file, but you are
  22. not obligated to do so. If you do not wish to do so, delete this
  23. exception statement from your version. */
  24. #include <unistd.h>
  25. #include <errno.h>
  26. #include <stdlib.h>
  27. #include <locale.h>
  28. #include <cryptoki.h>
  29. #define DIM(x) (sizeof (x) / sizeof (x[0]))
  30. /* Check for compiler features. */
  31. #if __GNUC__
  32. #define _GCC_VERSION (__GNUC__ * 10000 \
  33. + __GNUC_MINOR__ * 100 \
  34. + __GNUC_PATCHLEVEL__)
  35. #if _GCC_VERSION > 30100
  36. #define UNUSED __attribute__ ((__unused__))
  37. #endif
  38. #endif
  39. #ifndef UNUSED
  40. #define UNUSED
  41. #endif
  42. const char *msg[] =
  43. {
  44. "OK", "Cancel", "Host memory", "Slot ID invalid", "Flags invalid",
  45. "General error", "Function failed", "Arguments bad", "No event",
  46. "Need to create threads", "Can't lock", "0x0000000b", "0x0000000c",
  47. "0x0000000d", "0x0000000e", "0x0000000f", "Attribute read only",
  48. "Attribute sensitive", "Attribute type invalid", "Attribute value invalid",
  49. "0x00000014", "0x00000015", "0x00000016", "0x00000017", "0x00000018",
  50. "0x00000019", "0x0000001a", "0x0000001b", "0x0000001c", "0x0000001d",
  51. "0x0000001e", "0x0000001f", "Data invalid", "Data length range",
  52. "0x00000022", "0x00000023", "0x00000024", "0x00000025", "0x00000026",
  53. "0x00000027", "0x00000028", "0x00000029", "0x0000002a", "0x0000002b",
  54. "0x0000002c", "0x0000002d", "0x0000002e", "0x0000002f", "Device error",
  55. "Device memory", "Devire removed", "0x00000033", "0x00000034",
  56. "0x00000035", "0x00000036", "0x00000037", "0x00000038", "0x00000039",
  57. "0x0000003a", "0x0000003b", "0x0000003c", "0x0000003d", "0x0000003e",
  58. "0x0000003f", "Encrypted data invalid", "Encrypted data length range",
  59. "0x00000042", "0x00000043", "0x00000044", "0x00000045", "0x00000046",
  60. "0x00000047", "0x00000048", "0x00000049", "0x0000004a", "0x0000004b",
  61. "0x0000004c", "0x0000004d", "0x0000004e", "0x0000004f",
  62. "Function canceled", "Function not parallel",
  63. "0x00000052", "0x00000053", "Function not supported", "0x00000055",
  64. "0x00000056", "0x00000057", "0x00000058", "0x00000059", "0x0000005a",
  65. "0x0000005b", "0x0000005c", "0x0000005d", "0x0000005e", "0x0000005f",
  66. "Key handle invalid", "Key sensitive", "Key size range",
  67. "Key type inconsistent", "Key not needed", "Key changed", "Key needed",
  68. "Key indigestible", "Key function not permitted", "Key not wrappable",
  69. "Key unextractable", "0x0000006b", "0x0000006c", "0x0000006d",
  70. "0x0000006e", "0x0000006f", "Mechanism invalid",
  71. "Mechanism parameter invalid",
  72. "0x00000072", "0x00000073", "0x00000074", "0x00000075", "0x00000076",
  73. "0x00000077", "0x00000078", "0x00000079", "0x0000007a", "0x0000007b",
  74. "0x0000007c", "0x0000007d", "0x0000007e", "0x0000007f",
  75. "Object class inconsistent", "Object class invalid",
  76. "Object handle invalid",
  77. "0x00000083", "0x00000084", "0x00000085", "0x00000086",
  78. "0x00000087", "0x00000088", "0x00000089", "0x0000008a", "0x0000008b",
  79. "0x0000008c", "0x0000008d", "0x0000008e", "0x0000008f",
  80. "Operation active", "Operation not initialized",
  81. "0x00000092", "0x00000093", "0x00000094", "0x00000095", "0x00000096",
  82. "0x00000097", "0x00000098", "0x00000099", "0x0000009a", "0x0000009b",
  83. "0x0000009c", "0x0000009d", "0x0000009e", "0x0000009f",
  84. "PIN incorrect", "PIN invalid", "PIN length range", "PIN expired",
  85. "PIN locked", "0x000000a5", "0x000000a6", "0x000000a7", "0x000000a8",
  86. "0x000000a9", "0x000000aa", "0x000000ab", "0x000000ac", "0x000000ad",
  87. "0x000000ae", "0x000000af",
  88. "Session closed", "Session count", "0x000000b2", "Session handle invalid",
  89. "Session parallel not supported", "Session read only", "Session exists",
  90. "Session read only exists", "Session read write SO exists",
  91. "0x000000b9", "0x000000ba", "0x000000bb", "0x000000bc", "0x000000bd",
  92. "0x000000be", "0x000000bf",
  93. "Signature invalid", "Signature length range",
  94. "0x000000c2", "0x000000c3", "0x000000c4", "0x000000c5", "0x000000c6",
  95. "0x000000c7", "0x000000c8", "0x000000c9", "0x000000ca", "0x000000cb",
  96. "0x000000cc", "0x000000cd", "0x000000ce", "0x000000cf",
  97. "Template incomplete", "Template inconsistent",
  98. "0x000000d2", "0x000000d3", "0x000000d4", "0x000000d5", "0x000000d6",
  99. "0x000000d7", "0x000000d8", "0x000000d9", "0x000000da", "0x000000db",
  100. "0x000000dc", "0x000000dd", "0x000000de", "0x000000df",
  101. "Token not present", "Token not recognized", "Token write protected",
  102. "0x000000e3", "0x000000e4", "0x000000e5", "0x000000e6", "0x000000e7",
  103. "0x000000e8", "0x000000e9", "0x000000ea", "0x000000eb", "0x000000ec",
  104. "0x000000ed", "0x000000ee", "0x000000ef",
  105. "Unwrapping key handle invalid", "Unwrapping key size range",
  106. "Unwrapping key type inconsistent",
  107. "0x000000f3", "0x000000f4", "0x000000f5", "0x000000f6", "0x000000f7",
  108. "0x000000f8", "0x000000f9", "0x000000fa", "0x000000fb", "0x000000fc",
  109. "0x000000fd", "0x000000fe", "0x000000ff",
  110. "User already logged in", "User not logged in", "User PIN not initialized",
  111. "User type invalid", "Another user already logged in",
  112. "User too many types",
  113. "0x00000106", "0x00000107", "0x00000108", "0x00000109", "0x0000010a",
  114. "0x0000010b", "0x0000010c", "0x0000010d", "0x0000010e", "0x0000010f",
  115. "Wrapped key invalid", "0x00000110", "Wrapped key length range",
  116. "Wrapping key handle invalid", "Wrapping key size range",
  117. "Wrapping key type inconsistent",
  118. "0x00000116", "0x00000117", "0x00000118", "0x00000119", "0x0000011a",
  119. "0x0000011b", "0x0000011c", "0x0000011d", "0x0000011e", "0x0000011f",
  120. "Random seed not supported", "No random number generator",
  121. "0x00000122", "0x00000123", "0x00000124", "0x00000125", "0x00000126",
  122. "0x00000127", "0x00000128", "0x00000129", "0x0000012a", "0x0000012b",
  123. "0x0000012c", "0x0000012d", "0x0000012e", "0x0000012f",
  124. "Domain parameters invalid",
  125. "0x00000131", "0x00000132", "0x00000133", "0x00000134", "0x00000135",
  126. "0x00000136", "0x00000137", "0x00000138", "0x00000139", "0x0000013a",
  127. "0x0000013b", "0x0000013c", "0x0000013d", "0x0000013e", "0x0000013f",
  128. "0x00000140", "0x00000141", "0x00000142", "0x00000143", "0x00000144",
  129. "0x00000145", "0x00000146", "0x00000147", "0x00000148", "0x00000149",
  130. "0x0000014a", "0x0000014b", "0x0000014c", "0x0000014d", "0x0000014e",
  131. "0x0000014f",
  132. "Buffer too small",
  133. "0x00000151", "0x00000152", "0x00000153", "0x00000154", "0x00000155",
  134. "0x00000156", "0x00000157", "0x00000158", "0x00000159", "0x0000015a",
  135. "0x0000015b", "0x0000015c", "0x0000015d", "0x0000015e", "0x0000015f",
  136. "Saved state invalid",
  137. "0x00000161", "0x00000162", "0x00000163", "0x00000164", "0x00000165",
  138. "0x00000166", "0x00000167", "0x00000168", "0x00000169", "0x0000016a",
  139. "0x0000016b", "0x0000016c", "0x0000016d", "0x0000016e", "0x0000016f",
  140. "Information sensitive",
  141. "0x00000171", "0x00000172", "0x00000173", "0x00000174", "0x00000175",
  142. "0x00000176", "0x00000177", "0x00000178", "0x00000179", "0x0000017a",
  143. "0x0000017b", "0x0000017c", "0x0000017d", "0x0000017e", "0x0000017f",
  144. "State unsaveable",
  145. "0x00000181", "0x00000182", "0x00000183", "0x00000184", "0x00000185",
  146. "0x00000186", "0x00000187", "0x00000188", "0x00000189", "0x0000018a",
  147. "0x0000018b", "0x0000018c", "0x0000018d", "0x0000018e", "0x0000018f",
  148. "Cryptoki not initialized", "Cryptoki already initialized",
  149. "0x00000192", "0x00000193", "0x00000194", "0x00000195", "0x00000196",
  150. "0x00000197", "0x00000198", "0x00000199", "0x0000019a", "0x0000019b",
  151. "0x0000019c", "0x0000019d", "0x0000019e", "0x0000019f",
  152. "Mutex bad", "Mutex not locked",
  153. "0x000001a2", "0x000001a3", "0x000001a4", "0x000001a5", "0x000001a6",
  154. "0x000001a7", "0x000001a8", "0x000001a9", "0x000001aa", "0x000001ab",
  155. "0x000001ac", "0x000001ad", "0x000001ae", "0x000001af",
  156. "0x000001b0", "0x000001b1", "0x000001b2", "0x000001b3", "0x000001b4",
  157. "0x000001b5", "0x000001b6", "0x000001b7", "0x000001b8", "0x000001b9",
  158. "0x000001ba", "0x000001bb", "0x000001bc", "0x000001bd", "0x000001be",
  159. "0x000001bf",
  160. "0x000001c0", "0x000001c1", "0x000001c2", "0x000001c3", "0x000001c4",
  161. "0x000001c5", "0x000001c6", "0x000001c7", "0x000001c8", "0x000001c9",
  162. "0x000001ca", "0x000001cb", "0x000001cc", "0x000001cd", "0x000001ce",
  163. "0x000001cf",
  164. "0x000001d0", "0x000001d1", "0x000001d2", "0x000001d3", "0x000001d4",
  165. "0x000001d5", "0x000001d6", "0x000001d7", "0x000001d8", "0x000001d9",
  166. "0x000001da", "0x000001db", "0x000001dc", "0x000001dd", "0x000001de",
  167. "0x000001df",
  168. "0x000001e0", "0x000001e1", "0x000001e2", "0x000001e3", "0x000001e4",
  169. "0x000001e5", "0x000001e6", "0x000001e7", "0x000001e8", "0x000001e9",
  170. "0x000001ea", "0x000001eb", "0x000001ec", "0x000001ed", "0x000001ee",
  171. "0x000001ef",
  172. "0x000001f0", "0x000001f1", "0x000001f2", "0x000001f3", "0x000001f4",
  173. "0x000001f5", "0x000001f6", "0x000001f7", "0x000001f8", "0x000001f9",
  174. "0x000001fa", "0x000001fb", "0x000001fc", "0x000001fd", "0x000001fe",
  175. "0x000001ff",
  176. "Function rejected" };
  177. #define ERRMSG(nr) ((nr) == CKR_VENDOR_DEFINED ? "Vendor defined" : \
  178. (((nr) < 0 || (nr) > sizeof (msg) / sizeof (msg[0])) ? \
  179. "(unknown error code)" : msg[(nr)]))
  180. static const char *
  181. mechanism_type_str (CK_MECHANISM_TYPE mechanism_type) UNUSED;
  182. static const char *
  183. mechanism_type_str (CK_MECHANISM_TYPE mechanism_type)
  184. {
  185. switch (mechanism_type)
  186. {
  187. #define CKM_ONE(mechanism) \
  188. case mechanism: \
  189. return #mechanism;
  190. CKM_ONE (CKM_RSA_PKCS_KEY_PAIR_GEN);
  191. CKM_ONE (CKM_RSA_PKCS);
  192. default:
  193. return NULL;
  194. }
  195. }
  196. static const char *session_state_str (CK_STATE state) UNUSED;
  197. static const char *
  198. session_state_str (CK_STATE state)
  199. {
  200. switch (state)
  201. {
  202. #define CKS_ONE(state) \
  203. case state: \
  204. return #state;
  205. CKS_ONE (CKS_RO_PUBLIC_SESSION);
  206. CKS_ONE (CKS_RO_USER_FUNCTIONS);
  207. CKS_ONE (CKS_RW_PUBLIC_SESSION);
  208. CKS_ONE (CKS_RW_USER_FUNCTIONS);
  209. CKS_ONE (CKS_RW_SO_FUNCTIONS);
  210. default:
  211. return NULL;
  212. }
  213. }
  214. #define fail_if_err(err) \
  215. do \
  216. { \
  217. if (err) \
  218. { \
  219. fprintf (stderr, "%s:%d: %s\n", \
  220. __FILE__, __LINE__, ERRMSG(err)); \
  221. exit (1); \
  222. } \
  223. } \
  224. while (0)
  225. #define fail(errmsg) \
  226. do \
  227. { \
  228. if (err) \
  229. { \
  230. fprintf (stderr, "%s:%d: %s\n", \
  231. __FILE__, __LINE__, errmsg); \
  232. exit (1); \
  233. } \
  234. } \
  235. while (0)
  236. #ifdef _WIN32
  237. #include <windows.h>
  238. #endif
  239. void
  240. init_cryptoki (void)
  241. {
  242. CK_RV err;
  243. #ifdef _WIN32
  244. WSADATA wsadat;
  245. WSAStartup (0x202, &wsadat);
  246. #endif
  247. err = C_Initialize (NULL);
  248. fail_if_err (err);
  249. }