summaryrefslogtreecommitdiff
path: root/src/module.c
blob: 453acd4f3c8423e7b62bbba651099dc66a43e68b (plain)
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

#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 void (*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)
{
	if(!mutex)
	{
		p11t_msg_print("CreateMutex: null mutex passed");
		return CKR_ARGUMENTS_BAD;
	}

#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 */

	return CKR_OK;
}

static CK_RV
destroy_mutex (void *mutex)
{
	if(!mutex)
	{
		p11t_msg_print("DestroyMutex: null mutex");
		return CKR_MUTEX_BAD;
	}

#ifdef _WIN32

	if(!CloseHandle((HANDLE)mutex))
	{
		DWORD error = GetLastError();
		if(error == ERROR_INVALID_HANDLE)
		{
			p11t_msg_print("DestroyMutex: mutex is invalid");
			return CKR_MUTEX_BAD;
		}
		else
		{
			p11t_msg_print("DestroyMutex: failed: %d", GetLastError());
			return CKR_GENERAL_ERROR;
		}
	}


#else /* !_WIN32 */

	{
		int res = pthread_mutex_destroy(mutex);
		if(res != 0)
		{
			if(res == EBUSY)
			{
				p11t_msg_print("DestroyMutex: mutex is locked");
				return CKR_GENERAL_ERROR;
			}
			else if(res == EINVAL)
			{
				p11t_msg_print("DestroyMutex: mutex is invalid");
				return CKR_MUTEX_BAD;
			}
			else
			{
				p11t_msg_print("DestroyMutex: failed: %d", res);
				return CKR_GENERAL_ERROR;

			}
		}
	}

#endif /* !_WIN32 */

	return CKR_OK;
}

static CK_RV
lock_mutex (void *mutex)
{
	if(!mutex)
	{
		p11t_msg_print("LockMutex: null mutex");
		return CKR_MUTEX_BAD;
	}

#ifdef _WIN32

	{
		DWORD result = WaitForSingleObject((HANDLE)mutex, INFINITE);
		if(result == WAIT_ABANDONED)
		{
			p11t_msg_print("LockMutex: thread exited without releasing mutex");
			return CKR_CANT_LOCK;
		}
		else if(result == WAIT_FAILED)
		{
			DWORD error = GetLastError();
			if(error == ERROR_INVALID_HANDLE)
			{
				p11t_msg_print("LockMutex: invalid handle");
				return CKR_MUTEX_BAD;
			}
			else
			{
				p11t_msg_print("LockMutex: failed: %d", error);
				return CKR_GENERAL_ERROR;
			}
		}
		else if(result != WAIT_OBJECT_0)
		{
			p11t_msg_print("LockMutex: couldn't lock");
			return CKR_CANT_LOCK;
		}
	}

#else /* !_WIN32 */

	{
		int res = pthread_mutex_lock(mutex);
		if(res != 0)
		{
			if(res == EDEADLK)
			{
				p11t_msg_print("LockMutex: would deadlock");
				return CKR_CANT_LOCK;
			}
			else if(res == EINVAL)
			{
				p11t_msg_print("LockMutex: invalid mutex used");
				return CKR_MUTEX_BAD;
			}
			else
			{
				p11t_msg_print("LockMutex: failed: %d", res);
				return CKR_GENERAL_ERROR;

			}
		}
	}

#endif /* !_WIN32 */

	return CKR_OK;
}

static CK_RV
unlock_mutex (void *mutex)
{
	if(!mutex)
	{
		p11t_msg_print("UnlockMutex: null mutex");
		return CKR_MUTEX_BAD;
	}

#ifdef _WIN32

	if(!ReleaseMutex((HANDLE)mutex))
	{
		DWORD error = GetLastError();
		if(error == ERROR_NOT_OWNER)
		{
			p11t_msg_print("UnlockMutex: mutex not locked");
			return CKR_MUTEX_NOT_LOCKED;
		}
		else if(error == ERROR_INVALID_HANDLE)
		{
			p11t_msg_print("UnlockMutex: invalid mutex used");
			return CKR_MUTEX_BAD;
		}
		else
		{
			p11t_msg_print("UnlockMutex: failed: %d", error);
			return CKR_GENERAL_ERROR;
		}
	}

#else /* !_WIN32 */

	{
		int res = pthread_mutex_lock(mutex);
		if(res != 0)
		{
			if(res == EPERM)
			{
				p11t_msg_print("UnlockMutex: mutex not locked");
				return CKR_MUTEX_NOT_LOCKED;
			}
			else if(res == EINVAL)
			{
				p11t_msg_print("UnlockMutex: invalid mutex used");
				return CKR_MUTEX_BAD;
			}
			else
			{
				p11t_msg_print("UnlockMutex: failed: %d", res);
				return CKR_GENERAL_ERROR;

			}
		}
		else
		{
			free(mutex);
		}
	}

#endif /* !_WIN32 */

	return CKR_OK;
}

void
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)
	{
		/** C_GetFunctionList */
		rv = (p11t_module_funcs->C_GetFunctionList)(&list);
		if(rv != CKR_OK)
			p11t_msg_print("C_GetFunctionList: call through function list failed: %s", p11t_msg_rv(rv));

		/** - See if returns same data as library entry point */
		if(!memcmp(list, p11t_module_funcs, sizeof(*list)) != 0)
			p11t_msg_print("C_GetFunctionList: function lists returned directly and recursively differ");
	}
}

static int
initialize_common(const char *mode)
{
	CK_RV rv;

	/** C_Initialize **/

	assert(p11t_module_funcs);

	/** - Normal call */
	rv = (p11t_module_funcs->C_Initialize) (&init_args);
	if (rv != CKR_OK)
	{
		if(rv != CKR_CANT_LOCK)
			p11t_msg_print("C_Initialize: failed (%s): %s", mode, p11t_msg_rv(rv));
		return 0;
	}
	else
	{
		is_initialized = 1;
		return 1;
	}
}

static void
initialize_locking_1(void)
{
	/** - 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;
}

static void
initialize_locking_2(void)
{
	/** - 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;
}

static void
initialize_locking_3(void)
{
	/** - 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;
}

static void
initialize_locking_4(void)
{
	/** - 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;
}

void
p11t_module_initialize(void)
{
	CK_INFO info;
	CK_RV rv;

	assert(p11t_module_funcs);

	if(p11t_test_unexpected)
	{
		/** - Calls without initializing */
		rv = (p11t_module_funcs->C_GetInfo)(&info);
		p11t_check_returns("C_GetInfo: shouldn't run without initialize", rv, CKR_CRYPTOKI_NOT_INITIALIZED);

		/** - NULL argument */
		rv = (p11t_module_funcs->C_Initialize) (NULL);
		p11t_check_returns("C_Initialize: should succeed with null argument", rv, CKR_OK);
		is_initialized = 1;
		p11t_module_finalize();
	}

	/** - 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("C_Initialize: no initialize succeded cannot continue");
	(init_func)();
	if(!is_initialized)
		p11t_msg_fatal("C_Initialize: couldn't initialize pkcs11 module");

	if(p11t_test_unexpected)
	{
		/** - Double initialize in a row */
		rv = (p11t_module_funcs->C_Initialize) (&init_args);
		if(rv != CKR_CRYPTOKI_ALREADY_INITIALIZED)
		{
			p11t_msg_print("C_Initialize: double initialize should return CKR_CRYPTOKI_ALREADY_INITIALIZED: %s", p11t_msg_rv(rv));
		}
	}

	is_initialized = 1;
}

void
p11t_module_finalize(void)
{
	CK_RV rv;

	/** C_Finalize */
	if(is_initialized)
	{
		if(p11t_test_unexpected)
		{
			/** - With invalid argument */
			rv = p11t_module_funcs->C_Finalize(&rv);
			p11t_check_returns("C_Finalize: bad argument", rv, CKR_ARGUMENTS_BAD);
		}

		/** - Normal call */
		assert(p11t_module_funcs);
		rv = p11t_module_funcs->C_Finalize(NULL);
		p11t_check_returns("C_Finalize", rv, CKR_OK);
		is_initialized = 0;
	}

	if(p11t_test_unexpected)
	{
		/** - Double finalize in a row */
		rv = p11t_module_funcs->C_Finalize(NULL);
		p11t_check_returns("C_Finalize: double finalize", rv, CKR_CRYPTOKI_NOT_INITIALIZED);
	}
}

void
p11t_module_unload(void)
{
	if(module)
	{
#ifdef _WIN32
		FreeLibrary(module);
#else
		dlclose(module);
#endif
	}
	module = NULL;
}