1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
|
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#define _WIN32_WINNT 0x400
#include <windows.h>
static HMODULE module = NULL;
#else /* !_WIN32 */
#include <dlfcn.h>
#include <pthread.h>
static void *module = NULL;
#endif /* !_WIN32 */
#include "p11-tests.h"
#include <errno.h>
#include <stdlib.h>
#include <string.h>
CK_FUNCTION_LIST_PTR p11t_module_funcs = NULL;
static const char *init_string = NULL;
static CK_C_INITIALIZE_ARGS init_args;
static int (*init_func)(void) = NULL;
static int is_initialized = 0;
void
p11t_module_config(const char *name, const char *value)
{
if(strcmp(name, "init-string") == 0)
init_string = value;
}
static CK_RV
create_mutex(void **mutex)
{
const char *old = P11T_SECTION("CreateMutex");
CK_RV ret = CKR_OK;
if(!mutex)
{
P11T_CHECK_FAIL("Arguments should not be null");
ret = CKR_ARGUMENTS_BAD;
}
else
{
#ifdef _WIN32
*mutex = CreateMutex(NULL, FALSE, NULL);
assert(*mutex != NULL);
#else /* !_WIN32 */
pthread_mutex_t *mut;
mut = malloc(sizeof(pthread_mutex_t));
assert(mut);
if(pthread_mutex_init(mut, NULL) != 0)
assert(0);
*mutex = mut;
#endif /* !_WIN32 */
}
p11t_msg_prefix(old);
return ret;
}
static CK_RV
destroy_mutex (void *mutex)
{
const char *old = P11T_SECTION("DestroyMutex");
CK_RV ret = CKR_OK;
if(!mutex)
{
P11T_CHECK_FAIL("Mutex should not be null");
ret = CKR_MUTEX_BAD;
}
else
{
#ifdef _WIN32
if(!CloseHandle((HANDLE)mutex))
{
DWORD error = GetLastError();
if(error == ERROR_INVALID_HANDLE)
{
P11T_CHECK_FAIL("Mutex should not be invalid");
ret = CKR_MUTEX_BAD;
}
else
{
p11t_check_warn("failed: %d", GetLastError());
ret = CKR_GENERAL_ERROR;
}
}
#else /* !_WIN32 */
int res = pthread_mutex_destroy(mutex);
if(res != 0)
{
if(res == EBUSY)
{
P11T_CHECK_FAIL("Mutex should not be locked");
ret = CKR_GENERAL_ERROR;
}
else if(res == EINVAL)
{
P11T_CHECK_FAIL("Mutex should not be invalid");
ret = CKR_MUTEX_BAD;
}
else
{
p11t_check_warn("failed: %d", res);
ret = CKR_GENERAL_ERROR;
}
}
#endif /* !_WIN32 */
}
p11t_msg_prefix(old);
return ret;
}
static CK_RV
lock_mutex (void *mutex)
{
const char *old = P11T_SECTION("LockMutex");
CK_RV ret = CKR_OK;
if(!mutex)
{
P11T_CHECK_FAIL("null mutex");
ret = CKR_MUTEX_BAD;
}
else
{
#ifdef _WIN32
DWORD result = WaitForSingleObject((HANDLE)mutex, INFINITE);
if(result == WAIT_ABANDONED)
{
P11T_CHECK_FAIL("Thread should not exit without releasing mutex");
ret = CKR_CANT_LOCK;
}
else if(result == WAIT_FAILED)
{
DWORD error = GetLastError();
if(error == ERROR_INVALID_HANDLE)
{
P11T_CHECK_FAIL("Mutex should not be invalid");
ret = CKR_MUTEX_BAD;
}
else
{
p11t_check_warn("failed: %d", error);
ret = CKR_GENERAL_ERROR;
}
}
else if(result != WAIT_OBJECT_0)
{
p11t_check_warn("couldn't lock");
ret = CKR_CANT_LOCK;
}
#else /* !_WIN32 */
int res = pthread_mutex_lock(mutex);
if(res != 0)
{
if(res == EDEADLK)
{
P11T_CHECK_FAIL("Mutex should not deadlock");
ret = CKR_CANT_LOCK;
}
else if(res == EINVAL)
{
P11T_CHECK_FAIL("Mutex should not be invalid");
ret = CKR_MUTEX_BAD;
}
else
{
p11t_check_warn("failed: %d", res);
ret = CKR_GENERAL_ERROR;
}
}
#endif /* !_WIN32 */
}
p11t_msg_prefix(old);
return ret;
}
static CK_RV
unlock_mutex (void *mutex)
{
const char *old = P11T_SECTION("UnlockMutex");
CK_RV ret = CKR_OK;
if(!mutex)
{
P11T_CHECK_FAIL("Mutex should not be null");
ret = CKR_MUTEX_BAD;
}
else
{
#ifdef _WIN32
if(!ReleaseMutex((HANDLE)mutex))
{
DWORD error = GetLastError();
if(error == ERROR_NOT_OWNER)
{
P11T_CHECK_FAIL("Mutex should be locked");
ret = CKR_MUTEX_NOT_LOCKED;
}
else if(error == ERROR_INVALID_HANDLE)
{
P11T_CHECK_FAIL("Mutex should not be invalid");
ret = CKR_MUTEX_BAD;
}
else
{
p11t_check_warn("failed: %d", error);
ret = CKR_GENERAL_ERROR;
}
}
#else /* !_WIN32 */
int res = pthread_mutex_lock(mutex);
if(res != 0)
{
if(res == EPERM)
{
P11T_CHECK_FAIL("Mutex should not be locked");
ret = CKR_MUTEX_NOT_LOCKED;
}
else if(res == EINVAL)
{
P11T_CHECK_FAIL("Mutex should be valid");
ret = CKR_MUTEX_BAD;
}
else
{
p11t_check_warn("failed: %d", res);
ret = CKR_GENERAL_ERROR;
}
}
else
{
free(mutex);
}
#endif /* !_WIN32 */
}
p11t_msg_prefix(old);
return ret;
}
int
p11t_module_load(const char *filename)
{
CK_FUNCTION_LIST_PTR list;
CK_C_GetFunctionList get_function_list;
CK_RV rv;
#ifdef _WIN32
module = LoadLibrary(filename);
if(!module)
p11t_msg_fatal("couldn't load library: %s: %s", filename, p11t_msg_lasterr());
/* Lookup the appropriate function in the library */
get_function_list = (CK_C_GetFunctionList)GetProcAddress(module, "C_GetFunctionList");
if(!get_function_list)
p11t_msg_fatal("C_GetFunctionList: couldn't find function in library: %s: %s",
filename, p11t_msg_lasterr());
#else /* !_WIN32 */
module = dlopen(filename, RTLD_NOW);
if(!module)
p11t_msg_fatal("couldn't open library: %s: %s", filename, dlerror());
/* Lookup the appropriate function in library */
get_function_list = (CK_C_GetFunctionList)dlsym (module, "C_GetFunctionList");
if(!get_function_list)
p11t_msg_fatal("C_GetFunctionList: couldn't find function in library: %s: %s",
filename, dlerror());
#endif /* !_WIN32 */
/* Get the function list */
rv = (get_function_list)(&p11t_module_funcs);
if(rv != CKR_OK)
p11t_msg_fatal("C_GetFunctiontList: couldn't get function list: %s", p11t_msg_rv(rv));
if(p11t_test_unexpected)
{
P11T_SECTION("C_GetFunctionList");
rv = (p11t_module_funcs->C_GetFunctionList)(&list);
P11T_CHECK_RV("Call through function list", rv, CKR_OK);
if(memcmp(list, p11t_module_funcs, sizeof(*list)) != 0)
p11t_check_info("Call doesn't return same data as library C_GetFunctionList entry point");
}
return CONTINUE;
}
static int
initialize_common(const char *mode)
{
CK_RV rv;
P11T_SECTION("C_Initialize");
assert(p11t_module_funcs);
rv = (p11t_module_funcs->C_Initialize) (&init_args);
if(rv == CKR_CANT_LOCK)
{
p11t_check_info("Module didn't accept %s", mode);
return 0;
}
if(rv != CKR_OK)
{
p11t_check_fail("Couldn't initialize with %s: %s", mode, p11t_msg_rv(rv));
return 0;
}
is_initialized = 1;
return 1;
}
static int
initialize_locking_1(void)
{
P11T_CHECK_NOTE("Locking: no threads");
memset(&init_args, 0, sizeof (init_args));
init_args.pReserved = (void*)init_string;
if(initialize_common("locking mode 1: no threads"))
init_func = initialize_locking_1;
return CONTINUE;
}
static int
initialize_locking_2(void)
{
P11T_CHECK_NOTE("Locking: os locking");
memset(&init_args, 0, sizeof (init_args));
init_args.flags = CKF_OS_LOCKING_OK;
init_args.pReserved = (void*)init_string;
if(initialize_common("locking mode 2: os locking"))
init_func = initialize_locking_2;
return CONTINUE;
}
static int
initialize_locking_3(void)
{
P11T_CHECK_NOTE("Locking: app locking");
memset(&init_args, 0, sizeof (init_args));
init_args.flags = 0;
init_args.CreateMutex = create_mutex;
init_args.DestroyMutex = destroy_mutex;
init_args.LockMutex = lock_mutex;
init_args.UnlockMutex = unlock_mutex;
init_args.pReserved = (void*)init_string;
if(initialize_common("locking mode 3: app locking"))
init_func = initialize_locking_3;
return CONTINUE;
}
static int
initialize_locking_4(void)
{
P11T_CHECK_NOTE("Locking: either locking");
memset(&init_args, 0, sizeof (init_args));
init_args.flags = CKF_OS_LOCKING_OK;
init_args.CreateMutex = create_mutex;
init_args.DestroyMutex = destroy_mutex;
init_args.LockMutex = lock_mutex;
init_args.UnlockMutex = unlock_mutex;
init_args.pReserved = (void*)init_string;
if(initialize_common("locking mode 4: either locking"))
init_func = initialize_locking_4;
return CONTINUE;
}
int
p11t_module_initialize(void)
{
CK_INFO info;
CK_RV rv;
assert(p11t_module_funcs);
P11T_SECTION("C_Initialize");
if(p11t_test_unexpected)
{
rv = (p11t_module_funcs->C_GetInfo)(&info);
P11T_CHECK_RV("Calls without initializing", rv, CKR_CRYPTOKI_NOT_INITIALIZED);
rv = (p11t_module_funcs->C_Initialize) (NULL);
P11T_CHECK_RV("Null argument", rv, CKR_OK);
is_initialized = 1;
p11t_module_finalize();
}
P11T_CHECK_NOTE("Multiple initialize with C_Finalize between");
initialize_locking_1();
p11t_module_finalize();
initialize_locking_2();
p11t_module_finalize();
initialize_locking_3();
p11t_module_finalize();
initialize_locking_4();
p11t_module_finalize();
/* Actually initialize with whatever succeeded last */
if(!init_func)
p11t_msg_fatal("Couldn't initialize module via any method");
(init_func)();
if(!is_initialized)
p11t_msg_fatal("Failed to initialize module");
if(p11t_test_unexpected)
{
rv = (p11t_module_funcs->C_Initialize) (&init_args);
P11T_CHECK_RV("Double initialize in a row", rv, CKR_CRYPTOKI_ALREADY_INITIALIZED);
}
is_initialized = 1;
return CONTINUE;
}
int
p11t_module_finalize(void)
{
CK_RV rv;
P11T_SECTION("C_Finalize");
if(is_initialized)
{
if(p11t_test_unexpected)
{
rv = p11t_module_funcs->C_Finalize(&rv);
P11T_CHECK_RV("With invalid argument", rv, CKR_ARGUMENTS_BAD);
}
assert(p11t_module_funcs);
rv = p11t_module_funcs->C_Finalize(NULL);
P11T_CHECK_RV("Normal call", rv, CKR_OK);
is_initialized = 0;
}
if(p11t_test_unexpected)
{
rv = p11t_module_funcs->C_Finalize(NULL);
P11T_CHECK_RV("Double finalize in a row", rv, CKR_CRYPTOKI_NOT_INITIALIZED);
}
return CONTINUE;
}
int
p11t_module_unload(void)
{
if(module)
{
#ifdef _WIN32
FreeLibrary(module);
#else
dlclose(module);
#endif
}
module = NULL;
return CONTINUE;
}
|