1 /*
2  * Copyright (c) 2011, Collabora Ltd.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  *     * Redistributions of source code must retain the above
9  *       copyright notice, this list of conditions and the
10  *       following disclaimer.
11  *     * Redistributions in binary form must reproduce the
12  *       above copyright notice, this list of conditions and
13  *       the following disclaimer in the documentation and/or
14  *       other materials provided with the distribution.
15  *     * The names of contributors to this software may not be
16  *       used to endorse or promote products derived from this
17  *       software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
29  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
30  * DAMAGE.
31  *
32  * Author: Stef Walter <stefw@collabora.co.uk>
33  */
34 
35 #include "config.h"
36 
37 #include "debug.h"
38 #define CRYPTOKI_EXPORTS
39 #include "pkcs11.h"
40 #include "message.h"
41 
42 #include "mock.h"
43 
44 #include "attrs.h"
45 #define P11_DEBUG_FLAG P11_DEBUG_LIB
46 #include "debug.h"
47 #include "dict.h"
48 #include "array.h"
49 #include "library.h"
50 
51 #include <assert.h>
52 #include <ctype.h>
53 #include <stdarg.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <unistd.h>
58 
59 /* -------------------------------------------------------------------
60  * GLOBALS and SUPPORT STUFF
61  */
62 
63 /* Various mutexes */
64 static p11_mutex_t init_mutex;
65 
66 /* Whether we've been initialized, and on what process id it happened */
67 static bool pkcs11_initialized = false;
68 static pid_t pkcs11_initialized_pid = 0;
69 
70 static CK_UTF8CHAR *the_pin = NULL;
71 static CK_ULONG n_the_pin = 0;
72 
73 static bool logged_in = false;
74 static CK_USER_TYPE the_user_type = 0;
75 
76 typedef struct _Session {
77 	CK_SESSION_HANDLE handle;
78 	p11_dict *objects;
79 	CK_SESSION_INFO info;
80 
81 	/* For find operations */
82 	bool finding;
83 	p11_array *matches;
84 
85 	bool want_context_login;
86 
87 	/* For encrypt, decrypt operations */
88 	CK_OBJECT_HANDLE crypto_key;
89 	CK_ATTRIBUTE_TYPE crypto_method;
90 	CK_MECHANISM_TYPE crypto_mechanism;
91 
92 	/* For sign, verify, digest, CKM_MOCK_COUNT */
93 	CK_MECHANISM_TYPE hash_mechanism;
94 	CK_ATTRIBUTE_TYPE hash_method;
95 	CK_OBJECT_HANDLE hash_key;
96 	CK_ULONG hash_count;
97 
98 	/* For 'signing' with CKM_MOCK_PREFIX */
99 	CK_BYTE sign_prefix[128];
100 	CK_ULONG n_sign_prefix;
101 
102 	/* The random seed */
103 	CK_BYTE random_seed[128];
104 	CK_ULONG random_seed_len;
105 } Session;
106 
107 static unsigned int unique_identifier = 100;
108 static p11_dict *the_sessions = NULL;
109 static p11_dict *the_objects = NULL;
110 
111 #define SIGNED_PREFIX "signed-prefix:"
112 
113 #define handle_to_pointer(handle) \
114 	((void *)(size_t)(handle))
115 
116 #define pointer_to_handle(pointer) \
117 	((CK_ULONG)(size_t)(pointer))
118 
119 static void
free_session(void * data)120 free_session (void *data)
121 {
122 	Session *sess = (Session *)data;
123 	if (sess) {
124 		p11_dict_free (sess->objects);
125 		p11_array_free (sess->matches);
126 	}
127 	free (sess);
128 }
129 
130 static CK_RV
lookup_object(Session * sess,CK_OBJECT_HANDLE object,CK_ATTRIBUTE ** attrs,p11_dict ** table)131 lookup_object (Session *sess,
132                CK_OBJECT_HANDLE object,
133                CK_ATTRIBUTE **attrs,
134                p11_dict **table)
135 {
136 	CK_BBOOL priv;
137 
138 	*attrs = p11_dict_get (the_objects, handle_to_pointer (object));
139 	if (*attrs) {
140 		if (table)
141 			*table = the_objects;
142 	} else {
143 		*attrs = p11_dict_get (sess->objects, handle_to_pointer (object));
144 		if (*attrs) {
145 			if (table)
146 				*table = sess->objects;
147 		}
148 	}
149 
150 	if (!*attrs)
151 		return CKR_OBJECT_HANDLE_INVALID;
152 	else if (!logged_in && p11_attrs_find_bool (*attrs, CKA_PRIVATE, &priv) && priv)
153 		return CKR_USER_NOT_LOGGED_IN;
154 
155 	return CKR_OK;
156 }
157 
158 void
mock_module_add_object(CK_SLOT_ID slot_id,const CK_ATTRIBUTE * attrs)159 mock_module_add_object (CK_SLOT_ID slot_id,
160                         const CK_ATTRIBUTE *attrs)
161 {
162 	CK_ATTRIBUTE *copy;
163 
164 	return_if_fail (slot_id == MOCK_SLOT_ONE_ID);
165 	return_if_fail (attrs != NULL);
166 
167 	copy = p11_attrs_dup (attrs);
168 	return_if_fail (copy != NULL);
169 
170 	mock_module_take_object (slot_id, copy);
171 }
172 
173 void
mock_module_take_object(CK_SLOT_ID slot_id,CK_ATTRIBUTE * attrs)174 mock_module_take_object (CK_SLOT_ID slot_id,
175                         CK_ATTRIBUTE *attrs)
176 {
177 	CK_OBJECT_HANDLE object;
178 
179 	return_if_fail (slot_id == MOCK_SLOT_ONE_ID);
180 	return_if_fail (attrs != NULL);
181 
182 	object = ++unique_identifier;
183 	if (!p11_dict_set (the_objects, handle_to_pointer (object), attrs))
184 		return_if_reached ();
185 }
186 
187 static void
module_reset_objects(CK_SLOT_ID slot_id)188 module_reset_objects (CK_SLOT_ID slot_id)
189 {
190 	return_if_fail (slot_id == MOCK_SLOT_ONE_ID);
191 
192 	if (!the_objects) {
193 		the_objects = p11_dict_new (p11_dict_direct_hash,
194 		                            p11_dict_direct_equal,
195 		                            NULL, p11_attrs_free);
196 		return_if_fail (the_objects != NULL);
197 	}
198 
199 	p11_dict_clear (the_objects);
200 
201 	/* Our token object */
202 	{
203 		CK_OBJECT_CLASS klass = CKO_DATA;
204 		char *label = "TEST LABEL";
205 		CK_ATTRIBUTE attrs[] = {
206 			{ CKA_CLASS, &klass, sizeof (klass) },
207 			{ CKA_LABEL, label, strlen (label) },
208 			{ CKA_INVALID, NULL, 0 },
209 		};
210 		p11_dict_set (the_objects, handle_to_pointer (MOCK_DATA_OBJECT), p11_attrs_dup (attrs));
211 	}
212 
213 	/* Private capitalize key */
214 	{
215 		CK_OBJECT_CLASS klass = CKO_PRIVATE_KEY;
216 		char *label = "Private Capitalize Key";
217 		char *value = "value";
218 		CK_MECHANISM_TYPE type = CKM_MOCK_CAPITALIZE;
219 		CK_BBOOL btrue = CK_TRUE;
220 		CK_ATTRIBUTE attrs[] = {
221 			{ CKA_CLASS, &klass, sizeof (klass) },
222 			{ CKA_LABEL, label, strlen (label) },
223 			{ CKA_ALLOWED_MECHANISMS, &type, sizeof (type) },
224 			{ CKA_DECRYPT, &btrue, sizeof (btrue) },
225 			{ CKA_PRIVATE, &btrue, sizeof (btrue) },
226 			{ CKA_WRAP, &btrue, sizeof (btrue) },
227 			{ CKA_UNWRAP, &btrue, sizeof (btrue) },
228 			{ CKA_DERIVE, &btrue, sizeof (btrue) },
229 			{ CKA_VALUE, value, strlen (value) },
230 			{ CKA_INVALID, NULL, 0 },
231 		};
232 		p11_dict_set (the_objects, handle_to_pointer (MOCK_PRIVATE_KEY_CAPITALIZE), p11_attrs_dup (attrs));
233 
234 	}
235 
236 	{
237 		CK_OBJECT_CLASS klass = CKO_PUBLIC_KEY;
238 		char *label = "Public Capitalize Key";
239 		char *value = "value";
240 		CK_MECHANISM_TYPE type = CKM_MOCK_CAPITALIZE;
241 		CK_BBOOL btrue = CK_TRUE;
242 		CK_BBOOL bfalse = CK_FALSE;
243 		CK_ATTRIBUTE attrs[] = {
244 			{ CKA_CLASS, &klass, sizeof (klass) },
245 			{ CKA_LABEL, label, strlen (label) },
246 			{ CKA_ALLOWED_MECHANISMS, &type, sizeof (type) },
247 			{ CKA_ENCRYPT, &btrue, sizeof (btrue) },
248 			{ CKA_PRIVATE, &bfalse, sizeof (bfalse) },
249 			{ CKA_VALUE, value, strlen (value) },
250 			{ CKA_INVALID, NULL, 0 },
251 		};
252 		p11_dict_set (the_objects, handle_to_pointer (MOCK_PUBLIC_KEY_CAPITALIZE), p11_attrs_dup (attrs));
253 
254 	}
255 
256 	{
257 		CK_OBJECT_CLASS klass = CKO_PRIVATE_KEY;
258 		char *label = "Private prefix key";
259 		char *value = "value";
260 		CK_MECHANISM_TYPE type = CKM_MOCK_PREFIX;
261 		CK_BBOOL btrue = CK_TRUE;
262 		CK_ATTRIBUTE attrs[] = {
263 			{ CKA_CLASS, &klass, sizeof (klass) },
264 			{ CKA_LABEL, label, strlen (label) },
265 			{ CKA_ALLOWED_MECHANISMS, &type, sizeof (type) },
266 			{ CKA_SIGN, &btrue, sizeof (btrue) },
267 			{ CKA_PRIVATE, &btrue, sizeof (btrue) },
268 			{ CKA_ALWAYS_AUTHENTICATE, &btrue, sizeof (btrue) },
269 			{ CKA_VALUE, value, strlen (value) },
270 			{ CKA_INVALID, NULL, 0 },
271 		};
272 		p11_dict_set (the_objects, handle_to_pointer (MOCK_PRIVATE_KEY_PREFIX), p11_attrs_dup (attrs));
273 
274 	}
275 
276 	{
277 		CK_OBJECT_CLASS klass = CKO_PUBLIC_KEY;
278 		char *label = "Public prefix key";
279 		char *value = "value";
280 		CK_MECHANISM_TYPE type = CKM_MOCK_PREFIX;
281 		CK_BBOOL btrue = CK_TRUE;
282 		CK_BBOOL bfalse = CK_FALSE;
283 		CK_ATTRIBUTE attrs[] = {
284 			{ CKA_CLASS, &klass, sizeof (klass) },
285 			{ CKA_LABEL, label, strlen (label) },
286 			{ CKA_ALLOWED_MECHANISMS, &type, sizeof (type) },
287 			{ CKA_VERIFY, &btrue, sizeof (btrue) },
288 			{ CKA_PRIVATE, &bfalse, sizeof (bfalse) },
289 			{ CKA_ALWAYS_AUTHENTICATE, &btrue, sizeof (btrue) },
290 			{ CKA_VALUE, value, strlen (value) },
291 			{ CKA_INVALID, NULL, 0 },
292 		};
293 		p11_dict_set (the_objects, handle_to_pointer (MOCK_PUBLIC_KEY_PREFIX), p11_attrs_dup (attrs));
294 
295 	}
296 }
297 
298 static void
module_finalize(void)299 module_finalize (void)
300 {
301 	p11_mutex_lock (&init_mutex);
302 
303 		/* This should stop all other calls in */
304 		pkcs11_initialized = false;
305 		pkcs11_initialized_pid = 0;
306 
307 		if (the_objects)
308 			p11_dict_free (the_objects);
309 		the_objects = NULL;
310 
311 		if (the_sessions)
312 			p11_dict_free (the_sessions);
313 		the_sessions = NULL;
314 		logged_in = false;
315 		the_user_type = 0;
316 
317 		free (the_pin);
318 		the_pin = NULL;
319 		n_the_pin = 0;
320 
321 	p11_mutex_unlock (&init_mutex);
322 }
323 
324 bool
mock_module_initialized(void)325 mock_module_initialized (void)
326 {
327 	return pkcs11_initialized;
328 }
329 void
mock_module_reset(void)330 mock_module_reset (void)
331 {
332 	module_finalize ();
333 	module_reset_objects (MOCK_SLOT_ONE_ID);
334 
335 }
336 
337 void
mock_module_enumerate_objects(CK_SESSION_HANDLE handle,mock_enumerator func,void * user_data)338 mock_module_enumerate_objects (CK_SESSION_HANDLE handle,
339                                mock_enumerator func,
340                                void *user_data)
341 {
342 	p11_dictiter iter;
343 	void *key;
344 	void *value;
345 	Session *sess;
346 
347 	assert (the_objects != NULL);
348 	assert (func != NULL);
349 
350 	/* Token objects */
351 	p11_dict_iterate (the_objects, &iter);
352 	while (p11_dict_next (&iter, &key, &value)) {
353 		if (!(func) (pointer_to_handle (key), value, user_data))
354 			return;
355 	}
356 
357 	/* session objects */
358 	if (handle) {
359 		sess = p11_dict_get (the_sessions, handle_to_pointer (handle));
360 		if (sess) {
361 			p11_dict_iterate (sess->objects, &iter);
362 			while (p11_dict_next (&iter, &key, &value)) {
363 				if (!(func) (pointer_to_handle (key), value, user_data))
364 					return;
365 			}
366 		}
367 	}
368 }
369 
370 /* -------------------------------------------------------------------
371  * INITIALIZATION and 'GLOBAL' CALLS
372  */
373 
374 CK_RV
mock_C_Initialize(CK_VOID_PTR init_args)375 mock_C_Initialize (CK_VOID_PTR init_args)
376 {
377 	CK_C_INITIALIZE_ARGS_PTR args = NULL;
378 	CK_RV ret = CKR_OK;
379 	pid_t pid;
380 
381 	p11_mutex_lock (&init_mutex);
382 
383 		if (init_args != NULL) {
384 			int supplied_ok;
385 
386 			/* pReserved must be NULL */
387 			args = init_args;
388 
389 			/* ALL supplied function pointers need to have the value either NULL or non-NULL. */
390 			supplied_ok = (args->CreateMutex == NULL && args->DestroyMutex == NULL &&
391 			               args->LockMutex == NULL && args->UnlockMutex == NULL) ||
392 			              (args->CreateMutex != NULL && args->DestroyMutex != NULL &&
393 			               args->LockMutex != NULL && args->UnlockMutex != NULL);
394 			if (!supplied_ok) {
395 				p11_debug_precond ("invalid set of mutex calls supplied\n");
396 				ret = CKR_ARGUMENTS_BAD;
397 				goto done;
398 			}
399 
400 			/*
401 			 * When the CKF_OS_LOCKING_OK flag isn't set return an error.
402 			 * We must be able to use our pthread functionality.
403 			 */
404 			if (!(args->flags & CKF_OS_LOCKING_OK)) {
405 				p11_debug_precond ("can't do without os locking\n");
406 				ret = CKR_CANT_LOCK;
407 				goto done;
408 			}
409 		}
410 
411 		pid = getpid ();
412 		if (pkcs11_initialized) {
413 
414 			/* This process has called C_Initialize already */
415 			if (pid == pkcs11_initialized_pid) {
416 				p11_debug_precond ("p11-kit: C_Initialize called twice for same process\n");
417 				ret = CKR_CRYPTOKI_ALREADY_INITIALIZED;
418 				goto done;
419 			}
420 		}
421 
422 		/* We store CK_ULONG as pointers here, so verify that they fit */
423 		assert (sizeof (CK_ULONG) <= sizeof (void *));
424 
425 		free (the_pin);
426 		the_pin = (CK_UTF8CHAR_PTR)strdup ("booo");
427 		n_the_pin = 4;
428 
429 		if (the_sessions)
430 			p11_dict_free (the_sessions);
431 		the_sessions = p11_dict_new (p11_dict_direct_hash,
432 		                             p11_dict_direct_equal,
433 		                             NULL, free_session);
434 		if (!the_sessions) {
435 			ret = CKR_HOST_MEMORY;
436 			goto done;
437 		}
438 
439 		module_reset_objects (MOCK_SLOT_ONE_ID);
440 
441 done:
442 		/* Mark us as officially initialized */
443 		if (ret == CKR_OK) {
444 			pkcs11_initialized = true;
445 			pkcs11_initialized_pid = pid;
446 		} else if (ret != CKR_CRYPTOKI_ALREADY_INITIALIZED) {
447 			pkcs11_initialized = false;
448 			pkcs11_initialized_pid = 0;
449 		}
450 
451 	p11_mutex_unlock (&init_mutex);
452 
453 	return ret;
454 }
455 
456 CK_RV
mock_X_Initialize(CK_X_FUNCTION_LIST * self,CK_VOID_PTR init_args)457 mock_X_Initialize (CK_X_FUNCTION_LIST *self,
458                    CK_VOID_PTR init_args)
459 {
460 	return mock_C_Initialize (init_args);
461 }
462 
463 CK_RV
mock_C_Initialize__fails(CK_VOID_PTR init_args)464 mock_C_Initialize__fails (CK_VOID_PTR init_args)
465 {
466 	return CKR_FUNCTION_FAILED;
467 }
468 
469 CK_RV
mock_X_Initialize__fails(CK_X_FUNCTION_LIST * self,CK_VOID_PTR init_args)470 mock_X_Initialize__fails (CK_X_FUNCTION_LIST *self,
471                           CK_VOID_PTR init_args)
472 {
473 	return mock_C_Initialize__fails (init_args);
474 }
475 
476 CK_RV
mock_C_Finalize(CK_VOID_PTR reserved)477 mock_C_Finalize (CK_VOID_PTR reserved)
478 {
479 	return_val_if_fail (pkcs11_initialized, CKR_CRYPTOKI_NOT_INITIALIZED);
480 	return_val_if_fail (reserved == NULL, CKR_ARGUMENTS_BAD);
481 
482 	module_finalize ();
483 	return CKR_OK;
484 }
485 
486 CK_RV
mock_X_Finalize(CK_X_FUNCTION_LIST * self,CK_VOID_PTR reserved)487 mock_X_Finalize (CK_X_FUNCTION_LIST *self,
488                  CK_VOID_PTR reserved)
489 {
490 	return mock_C_Finalize (reserved);
491 }
492 
493 CK_RV
mock_C_GetInfo(CK_INFO_PTR info)494 mock_C_GetInfo (CK_INFO_PTR info)
495 {
496 	return_val_if_fail (info, CKR_ARGUMENTS_BAD);
497 
498 	memcpy (info, &MOCK_INFO, sizeof (*info));
499 	return CKR_OK;
500 }
501 
502 CK_RV
mock_X_GetInfo(CK_X_FUNCTION_LIST * self,CK_INFO_PTR info)503 mock_X_GetInfo (CK_X_FUNCTION_LIST *self,
504                 CK_INFO_PTR info)
505 {
506 	return mock_C_GetInfo (info);
507 }
508 
509 CK_RV
mock_C_GetFunctionList_not_supported(CK_FUNCTION_LIST_PTR_PTR list)510 mock_C_GetFunctionList_not_supported (CK_FUNCTION_LIST_PTR_PTR list)
511 {
512 	/* This would be a strange call to receive, should be overridden  */
513 	return_val_if_reached (CKR_FUNCTION_NOT_SUPPORTED);
514 }
515 
516 CK_RV
mock_C_GetSlotList(CK_BBOOL token_present,CK_SLOT_ID_PTR slot_list,CK_ULONG_PTR count)517 mock_C_GetSlotList (CK_BBOOL token_present,
518                     CK_SLOT_ID_PTR slot_list,
519                     CK_ULONG_PTR count)
520 {
521 	CK_ULONG num;
522 
523 	return_val_if_fail (count, CKR_ARGUMENTS_BAD);
524 
525 	num = token_present ? 1 : 2;
526 
527 	/* Application only wants to know the number of slots. */
528 	if (slot_list == NULL) {
529 		*count = num;
530 		return CKR_OK;
531 	}
532 
533 	if (*count < num)
534 		return_val_if_reached (CKR_BUFFER_TOO_SMALL);
535 
536 	*count = num;
537 	slot_list[0] = MOCK_SLOT_ONE_ID;
538 	if (!token_present)
539 		slot_list[1] = MOCK_SLOT_TWO_ID;
540 
541 	return CKR_OK;
542 
543 }
544 
545 CK_RV
mock_C_GetSlotList__no_tokens(CK_BBOOL token_present,CK_SLOT_ID_PTR slot_list,CK_ULONG_PTR count)546 mock_C_GetSlotList__no_tokens (CK_BBOOL token_present,
547                                CK_SLOT_ID_PTR slot_list,
548                                CK_ULONG_PTR count)
549 {
550 	return_val_if_fail (count, CKR_ARGUMENTS_BAD);
551 
552 	/* No tokens */
553 	*count = 0;
554 	return CKR_OK;
555 }
556 
557 CK_RV
mock_X_GetSlotList__no_tokens(CK_X_FUNCTION_LIST * self,CK_BBOOL token_present,CK_SLOT_ID_PTR slot_list,CK_ULONG_PTR count)558 mock_X_GetSlotList__no_tokens (CK_X_FUNCTION_LIST *self,
559                                CK_BBOOL token_present,
560                                CK_SLOT_ID_PTR slot_list,
561                                CK_ULONG_PTR count)
562 {
563 	return mock_C_GetSlotList__no_tokens (token_present,
564 	                                      slot_list,
565 	                                      count);
566 ;
567 }
568 
569 /* Update mock-module.h URIs when updating this */
570 
571 static const CK_SLOT_INFO MOCK_INFO_ONE = {
572 	"TEST SLOT                                                       ",
573 	"TEST MANUFACTURER               ",
574 	CKF_TOKEN_PRESENT | CKF_REMOVABLE_DEVICE,
575 	{ 55, 155 },
576 	{ 65, 165 },
577 };
578 
579 /* Update mock-module.h URIs when updating this */
580 
581 static const CK_SLOT_INFO MOCK_INFO_TWO = {
582 	"TEST SLOT                                                       ",
583 	"TEST MANUFACTURER               ",
584 	CKF_REMOVABLE_DEVICE,
585 	{ 55, 155 },
586 	{ 65, 165 },
587 };
588 
589 CK_RV
mock_C_GetSlotInfo(CK_SLOT_ID slot_id,CK_SLOT_INFO_PTR info)590 mock_C_GetSlotInfo (CK_SLOT_ID slot_id,
591                     CK_SLOT_INFO_PTR info)
592 {
593 	return_val_if_fail (info, CKR_ARGUMENTS_BAD);
594 
595 	if (slot_id == MOCK_SLOT_ONE_ID) {
596 		memcpy (info, &MOCK_INFO_ONE, sizeof (*info));
597 		return CKR_OK;
598 	} else if (slot_id == MOCK_SLOT_TWO_ID) {
599 		memcpy (info, &MOCK_INFO_TWO, sizeof (*info));
600 		return CKR_OK;
601 	} else {
602 		return CKR_SLOT_ID_INVALID;
603 	}
604 }
605 
606 CK_RV
mock_C_GetSlotList__fail_first(CK_BBOOL token_present,CK_SLOT_ID_PTR slot_list,CK_ULONG_PTR count)607 mock_C_GetSlotList__fail_first (CK_BBOOL token_present,
608                                 CK_SLOT_ID_PTR slot_list,
609                                 CK_ULONG_PTR count)
610 {
611 	return CKR_VENDOR_DEFINED;
612 }
613 
614 CK_RV
mock_C_GetSlotList__fail_late(CK_BBOOL token_present,CK_SLOT_ID_PTR slot_list,CK_ULONG_PTR count)615 mock_C_GetSlotList__fail_late (CK_BBOOL token_present,
616                                CK_SLOT_ID_PTR slot_list,
617                                CK_ULONG_PTR count)
618 {
619 	if (!slot_list)
620 		return mock_C_GetSlotList (token_present, slot_list, count);
621 	return CKR_VENDOR_DEFINED;
622 }
623 
624 CK_RV
mock_C_GetSlotInfo__invalid_slotid(CK_SLOT_ID id,CK_SLOT_INFO_PTR info)625 mock_C_GetSlotInfo__invalid_slotid (CK_SLOT_ID id,
626                                     CK_SLOT_INFO_PTR info)
627 {
628 	return_val_if_fail (info, CKR_ARGUMENTS_BAD);
629 
630 	return CKR_SLOT_ID_INVALID;
631 }
632 
633 CK_RV
mock_X_GetSlotInfo__invalid_slotid(CK_X_FUNCTION_LIST * self,CK_SLOT_ID id,CK_SLOT_INFO_PTR info)634 mock_X_GetSlotInfo__invalid_slotid (CK_X_FUNCTION_LIST *self,
635                                     CK_SLOT_ID id,
636                                     CK_SLOT_INFO_PTR info)
637 {
638 	return_val_if_fail (info, CKR_ARGUMENTS_BAD);
639 
640 	return CKR_SLOT_ID_INVALID;
641 }
642 
643 /* Update gck-mock.h URIs when updating this */
644 
645 static const CK_TOKEN_INFO MOCK_TOKEN_ONE = {
646 	"TEST LABEL                      ",
647 	"TEST MANUFACTURER               ",
648 	"TEST MODEL      ",
649 	"TEST SERIAL     ",
650 	CKF_LOGIN_REQUIRED | CKF_USER_PIN_INITIALIZED | CKF_CLOCK_ON_TOKEN | CKF_TOKEN_INITIALIZED,
651 	1,
652 	2,
653 	3,
654 	4,
655 	5,
656 	6,
657 	7,
658 	8,
659 	9,
660 	10,
661 	{ 75, 175 },
662 	{ 85, 185 },
663 	{ '1', '9', '9', '9', '0', '5', '2', '5', '0', '9', '1', '9', '5', '9', '0', '0' }
664 };
665 
666 CK_RV
mock_C_GetTokenInfo(CK_SLOT_ID slot_id,CK_TOKEN_INFO_PTR info)667 mock_C_GetTokenInfo (CK_SLOT_ID slot_id,
668                      CK_TOKEN_INFO_PTR info)
669 {
670 	return_val_if_fail (info != NULL, CKR_ARGUMENTS_BAD);
671 
672 	if (slot_id == MOCK_SLOT_ONE_ID) {
673 		memcpy (info, &MOCK_TOKEN_ONE, sizeof (*info));
674 		return CKR_OK;
675 	} else if (slot_id == MOCK_SLOT_TWO_ID) {
676 		return CKR_TOKEN_NOT_PRESENT;
677 	} else {
678 		return CKR_SLOT_ID_INVALID;
679 	}
680 }
681 
682 CK_RV
mock_C_GetTokenInfo__invalid_slotid(CK_SLOT_ID slot_id,CK_TOKEN_INFO_PTR info)683 mock_C_GetTokenInfo__invalid_slotid (CK_SLOT_ID slot_id,
684                                      CK_TOKEN_INFO_PTR info)
685 {
686 	return_val_if_fail (info, CKR_ARGUMENTS_BAD);
687 
688 	return CKR_SLOT_ID_INVALID;
689 }
690 
691 CK_RV
mock_X_GetTokenInfo__invalid_slotid(CK_X_FUNCTION_LIST * self,CK_SLOT_ID slot_id,CK_TOKEN_INFO_PTR info)692 mock_X_GetTokenInfo__invalid_slotid (CK_X_FUNCTION_LIST *self,
693                                      CK_SLOT_ID slot_id,
694                                      CK_TOKEN_INFO_PTR info)
695 {
696 	return_val_if_fail (info, CKR_ARGUMENTS_BAD);
697 
698 	return CKR_SLOT_ID_INVALID;
699 }
700 
701 /*
702  * TWO mechanisms:
703  *  CKM_MOCK_CAPITALIZE
704  *  CKM_MOCK_PREFIX
705  */
706 
707 CK_RV
mock_C_GetMechanismList(CK_SLOT_ID slot_id,CK_MECHANISM_TYPE_PTR mechanism_list,CK_ULONG_PTR count)708 mock_C_GetMechanismList (CK_SLOT_ID slot_id,
709                          CK_MECHANISM_TYPE_PTR mechanism_list,
710                          CK_ULONG_PTR count)
711 {
712 	return_val_if_fail (count != NULL, CKR_ARGUMENTS_BAD);
713 
714 	if (slot_id == MOCK_SLOT_TWO_ID)
715 		return CKR_TOKEN_NOT_PRESENT;
716 	else if (slot_id != MOCK_SLOT_ONE_ID)
717 		return CKR_SLOT_ID_INVALID;
718 
719 	/* Application only wants to know the number of slots. */
720 	if (mechanism_list == NULL) {
721 		*count = 2;
722 		return CKR_OK;
723 	}
724 
725 	if (*count < 2)
726 		return_val_if_reached (CKR_BUFFER_TOO_SMALL);
727 
728 	mechanism_list[0] = CKM_MOCK_CAPITALIZE;
729 	mechanism_list[1] = CKM_MOCK_PREFIX;
730 	*count = 2;
731 	return CKR_OK;
732 }
733 
734 CK_RV
mock_C_GetTokenInfo__not_initialized(CK_SLOT_ID slot_id,CK_TOKEN_INFO_PTR info)735 mock_C_GetTokenInfo__not_initialized (CK_SLOT_ID slot_id,
736                                       CK_TOKEN_INFO_PTR info)
737 {
738 	CK_RV rv;
739 
740 	rv = mock_C_GetTokenInfo (slot_id, info);
741 	if (rv == CKR_OK)
742 		info->flags &= ~ CKF_TOKEN_INITIALIZED;
743 
744 	return rv;
745 }
746 
747 /*
748  * TWO mechanisms:
749  *  CKM_MOCK_CAPITALIZE
750  *  CKM_MOCK_PREFIX
751  */
752 
753 CK_RV
mock_C_GetMechanismList__invalid_slotid(CK_SLOT_ID id,CK_MECHANISM_TYPE_PTR mechanism_list,CK_ULONG_PTR count)754 mock_C_GetMechanismList__invalid_slotid (CK_SLOT_ID id,
755                                          CK_MECHANISM_TYPE_PTR mechanism_list,
756                                          CK_ULONG_PTR count)
757 {
758 	return_val_if_fail (count, CKR_ARGUMENTS_BAD);
759 
760 	return CKR_SLOT_ID_INVALID;
761 }
762 
763 CK_RV
mock_X_GetMechanismList__invalid_slotid(CK_X_FUNCTION_LIST * self,CK_SLOT_ID id,CK_MECHANISM_TYPE_PTR mechanism_list,CK_ULONG_PTR count)764 mock_X_GetMechanismList__invalid_slotid (CK_X_FUNCTION_LIST *self,
765                                          CK_SLOT_ID id,
766                                          CK_MECHANISM_TYPE_PTR mechanism_list,
767                                          CK_ULONG_PTR count)
768 {
769 	return_val_if_fail (count, CKR_ARGUMENTS_BAD);
770 
771 	return CKR_SLOT_ID_INVALID;
772 }
773 
774 static const CK_MECHANISM_INFO MOCK_MECH_CAPITALIZE = {
775 	512, 4096, CKF_ENCRYPT | CKF_DECRYPT
776 };
777 
778 static const CK_MECHANISM_INFO MOCK_MECH_PREFIX = {
779 	2048, 2048, CKF_SIGN | CKF_VERIFY
780 };
781 
782 CK_RV
mock_C_GetMechanismInfo(CK_SLOT_ID slot_id,CK_MECHANISM_TYPE type,CK_MECHANISM_INFO_PTR info)783 mock_C_GetMechanismInfo (CK_SLOT_ID slot_id,
784                          CK_MECHANISM_TYPE type,
785                          CK_MECHANISM_INFO_PTR info)
786 {
787 	return_val_if_fail (info, CKR_ARGUMENTS_BAD);
788 
789 	if (slot_id == MOCK_SLOT_TWO_ID)
790 		return CKR_TOKEN_NOT_PRESENT;
791 	else if (slot_id != MOCK_SLOT_ONE_ID)
792 		return CKR_SLOT_ID_INVALID;
793 
794 	if (type == CKM_MOCK_CAPITALIZE) {
795 		memcpy (info, &MOCK_MECH_CAPITALIZE, sizeof (*info));
796 		return CKR_OK;
797 	} else if (type == CKM_MOCK_PREFIX) {
798 		memcpy (info, &MOCK_MECH_PREFIX, sizeof (*info));
799 		return CKR_OK;
800 	} else {
801 		return CKR_MECHANISM_INVALID;
802 	}
803 }
804 
805 CK_RV
mock_C_GetMechanismInfo__invalid_slotid(CK_SLOT_ID slot_id,CK_MECHANISM_TYPE type,CK_MECHANISM_INFO_PTR info)806 mock_C_GetMechanismInfo__invalid_slotid (CK_SLOT_ID slot_id,
807                                          CK_MECHANISM_TYPE type,
808                                          CK_MECHANISM_INFO_PTR info)
809 {
810 	return_val_if_fail (info, CKR_ARGUMENTS_BAD);
811 
812 	return CKR_SLOT_ID_INVALID;
813 }
814 
815 CK_RV
mock_X_GetMechanismInfo__invalid_slotid(CK_X_FUNCTION_LIST * self,CK_SLOT_ID slot_id,CK_MECHANISM_TYPE type,CK_MECHANISM_INFO_PTR info)816 mock_X_GetMechanismInfo__invalid_slotid (CK_X_FUNCTION_LIST *self,
817                                          CK_SLOT_ID slot_id,
818                                          CK_MECHANISM_TYPE type,
819                                          CK_MECHANISM_INFO_PTR info)
820 {
821 	return_val_if_fail (info, CKR_ARGUMENTS_BAD);
822 
823 	return CKR_SLOT_ID_INVALID;
824 }
825 
826 CK_RV
mock_C_InitToken__specific_args(CK_SLOT_ID slot_id,CK_UTF8CHAR_PTR pin,CK_ULONG pin_len,CK_UTF8CHAR_PTR label)827 mock_C_InitToken__specific_args (CK_SLOT_ID slot_id,
828                                  CK_UTF8CHAR_PTR pin,
829                                  CK_ULONG pin_len,
830                                  CK_UTF8CHAR_PTR label)
831 {
832 	return_val_if_fail (pin != NULL, CKR_ARGUMENTS_BAD);
833 	return_val_if_fail (label != NULL, CKR_ARGUMENTS_BAD);
834 
835 	if (slot_id == MOCK_SLOT_TWO_ID)
836 		return CKR_TOKEN_NOT_PRESENT;
837 	else if (slot_id != MOCK_SLOT_ONE_ID)
838 		return CKR_SLOT_ID_INVALID;
839 
840 	if (strlen ("TEST PIN") != pin_len ||
841 	    strncmp ((char *)pin, "TEST PIN", pin_len) != 0)
842 		return CKR_PIN_INVALID;
843 	if (strcmp ((char *)label, "TEST LABEL") != 0)
844 		return CKR_ARGUMENTS_BAD;
845 
846 	free (the_pin);
847 	the_pin = memdup (pin, pin_len);
848 	return_val_if_fail (the_pin != NULL, CKR_HOST_MEMORY);
849 	n_the_pin = pin_len;
850 	return CKR_OK;
851 }
852 
853 CK_RV
mock_C_InitToken__invalid_slotid(CK_SLOT_ID slot_id,CK_UTF8CHAR_PTR pin,CK_ULONG pin_len,CK_UTF8CHAR_PTR label)854 mock_C_InitToken__invalid_slotid (CK_SLOT_ID slot_id,
855                                   CK_UTF8CHAR_PTR pin,
856                                   CK_ULONG pin_len,
857                                   CK_UTF8CHAR_PTR label)
858 {
859 	return CKR_SLOT_ID_INVALID;
860 }
861 
862 CK_RV
mock_X_InitToken__invalid_slotid(CK_X_FUNCTION_LIST * self,CK_SLOT_ID slot_id,CK_UTF8CHAR_PTR pin,CK_ULONG pin_len,CK_UTF8CHAR_PTR label)863 mock_X_InitToken__invalid_slotid (CK_X_FUNCTION_LIST *self,
864                                   CK_SLOT_ID slot_id,
865                                   CK_UTF8CHAR_PTR pin,
866                                   CK_ULONG pin_len,
867                                   CK_UTF8CHAR_PTR label)
868 {
869 	return CKR_SLOT_ID_INVALID;
870 }
871 
872 CK_RV
mock_C_WaitForSlotEvent(CK_FLAGS flags,CK_SLOT_ID_PTR slot,CK_VOID_PTR reserved)873 mock_C_WaitForSlotEvent (CK_FLAGS flags,
874                          CK_SLOT_ID_PTR slot,
875                          CK_VOID_PTR reserved)
876 {
877 	return_val_if_fail (slot, CKR_ARGUMENTS_BAD);
878 
879 	if (flags & CKF_DONT_BLOCK)
880 		return CKR_NO_EVENT;
881 
882 	*slot = MOCK_SLOT_TWO_ID;
883 	return CKR_OK;
884 }
885 
886 CK_RV
mock_C_WaitForSlotEvent__no_event(CK_FLAGS flags,CK_SLOT_ID_PTR slot,CK_VOID_PTR reserved)887 mock_C_WaitForSlotEvent__no_event (CK_FLAGS flags,
888                                    CK_SLOT_ID_PTR slot,
889                                    CK_VOID_PTR reserved)
890 {
891 	return_val_if_fail (slot, CKR_ARGUMENTS_BAD);
892 
893 	return CKR_NO_EVENT;
894 }
895 
896 CK_RV
mock_X_WaitForSlotEvent__no_event(CK_X_FUNCTION_LIST * self,CK_FLAGS flags,CK_SLOT_ID_PTR slot,CK_VOID_PTR reserved)897 mock_X_WaitForSlotEvent__no_event (CK_X_FUNCTION_LIST *self,
898                                    CK_FLAGS flags,
899                                    CK_SLOT_ID_PTR slot,
900                                    CK_VOID_PTR reserved)
901 {
902 	return_val_if_fail (slot, CKR_ARGUMENTS_BAD);
903 
904 	return CKR_NO_EVENT;
905 }
906 
907 CK_RV
mock_C_OpenSession(CK_SLOT_ID slot_id,CK_FLAGS flags,CK_VOID_PTR user_data,CK_NOTIFY callback,CK_SESSION_HANDLE_PTR session)908 mock_C_OpenSession (CK_SLOT_ID slot_id,
909                     CK_FLAGS flags,
910                     CK_VOID_PTR user_data,
911                     CK_NOTIFY callback,
912                     CK_SESSION_HANDLE_PTR session)
913 {
914 	Session *sess;
915 
916 	return_val_if_fail (session, CKR_ARGUMENTS_BAD);
917 
918 	if (slot_id == MOCK_SLOT_TWO_ID)
919 		return CKR_TOKEN_NOT_PRESENT;
920 	else if (slot_id != MOCK_SLOT_ONE_ID)
921 		return CKR_SLOT_ID_INVALID;
922 	if ((flags & CKF_SERIAL_SESSION) != CKF_SERIAL_SESSION)
923 		return CKR_SESSION_PARALLEL_NOT_SUPPORTED;
924 
925 	sess = calloc (1, sizeof (Session));
926 	return_val_if_fail (sess != NULL, CKR_HOST_MEMORY);
927 	sess->handle = ++unique_identifier;
928 	sess->info.flags = flags;
929 	sess->info.slotID = slot_id;
930 	sess->info.state = 0;
931 	sess->info.ulDeviceError = 1414;
932 	sess->objects = p11_dict_new (p11_dict_direct_hash, p11_dict_direct_equal,
933 	                              NULL, p11_attrs_free);
934 	*session = sess->handle;
935 
936 	memcpy (sess->random_seed, "random", 6);
937 	sess->random_seed_len = 6;
938 
939 	p11_dict_set (the_sessions, handle_to_pointer (sess->handle), sess);
940 	return CKR_OK;
941 }
942 
943 CK_RV
mock_C_OpenSession__invalid_slotid(CK_SLOT_ID slot_id,CK_FLAGS flags,CK_VOID_PTR user_data,CK_NOTIFY callback,CK_SESSION_HANDLE_PTR session)944 mock_C_OpenSession__invalid_slotid (CK_SLOT_ID slot_id,
945                                     CK_FLAGS flags,
946                                     CK_VOID_PTR user_data,
947                                     CK_NOTIFY callback,
948                                     CK_SESSION_HANDLE_PTR session)
949 {
950 	return_val_if_fail (session, CKR_ARGUMENTS_BAD);
951 
952 	return CKR_SLOT_ID_INVALID;
953 }
954 
955 CK_RV
mock_X_OpenSession__invalid_slotid(CK_X_FUNCTION_LIST * self,CK_SLOT_ID slot_id,CK_FLAGS flags,CK_VOID_PTR user_data,CK_NOTIFY callback,CK_SESSION_HANDLE_PTR session)956 mock_X_OpenSession__invalid_slotid (CK_X_FUNCTION_LIST *self,
957                                     CK_SLOT_ID slot_id,
958                                     CK_FLAGS flags,
959                                     CK_VOID_PTR user_data,
960                                     CK_NOTIFY callback,
961                                     CK_SESSION_HANDLE_PTR session)
962 {
963 	return_val_if_fail (session, CKR_ARGUMENTS_BAD);
964 
965 	return CKR_SLOT_ID_INVALID;
966 }
967 
968 CK_RV
mock_C_OpenSession__fails(CK_SLOT_ID slot_id,CK_FLAGS flags,CK_VOID_PTR user_data,CK_NOTIFY callback,CK_SESSION_HANDLE_PTR session)969 mock_C_OpenSession__fails (CK_SLOT_ID slot_id,
970                            CK_FLAGS flags,
971                            CK_VOID_PTR user_data,
972                            CK_NOTIFY callback,
973                            CK_SESSION_HANDLE_PTR session)
974 {
975 	return_val_if_fail (session, CKR_ARGUMENTS_BAD);
976 
977 	return CKR_DEVICE_ERROR;
978 }
979 
980 CK_RV
mock_C_CloseSession(CK_SESSION_HANDLE session)981 mock_C_CloseSession (CK_SESSION_HANDLE session)
982 {
983 	Session *sess;
984 
985 	sess = p11_dict_get (the_sessions, handle_to_pointer (session));
986 	if (!sess)
987 		return CKR_SESSION_HANDLE_INVALID;
988 
989 	p11_dict_remove (the_sessions, handle_to_pointer (session));
990 	return CKR_OK;
991 }
992 
993 CK_RV
mock_C_CloseSession__invalid_handle(CK_SESSION_HANDLE session)994 mock_C_CloseSession__invalid_handle (CK_SESSION_HANDLE session)
995 {
996 	return CKR_SESSION_HANDLE_INVALID;
997 }
998 
999 CK_RV
mock_X_CloseSession__invalid_handle(CK_X_FUNCTION_LIST * self,CK_SESSION_HANDLE session)1000 mock_X_CloseSession__invalid_handle (CK_X_FUNCTION_LIST *self,
1001                                      CK_SESSION_HANDLE session)
1002 {
1003 	return CKR_SESSION_HANDLE_INVALID;
1004 }
1005 
1006 CK_RV
mock_C_CloseAllSessions(CK_SLOT_ID slot_id)1007 mock_C_CloseAllSessions (CK_SLOT_ID slot_id)
1008 {
1009 	if (slot_id == MOCK_SLOT_TWO_ID)
1010 		return CKR_TOKEN_NOT_PRESENT;
1011 	else if (slot_id != MOCK_SLOT_ONE_ID)
1012 		return CKR_SLOT_ID_INVALID;
1013 
1014 	p11_dict_clear (the_sessions);
1015 	return CKR_OK;
1016 }
1017 
1018 CK_RV
mock_C_CloseAllSessions__invalid_slotid(CK_SLOT_ID slot_id)1019 mock_C_CloseAllSessions__invalid_slotid (CK_SLOT_ID slot_id)
1020 {
1021 	return CKR_SLOT_ID_INVALID;
1022 }
1023 
1024 CK_RV
mock_X_CloseAllSessions__invalid_slotid(CK_X_FUNCTION_LIST * self,CK_SLOT_ID slot_id)1025 mock_X_CloseAllSessions__invalid_slotid (CK_X_FUNCTION_LIST *self,
1026                                          CK_SLOT_ID slot_id)
1027 {
1028 	return CKR_SLOT_ID_INVALID;
1029 }
1030 
1031 CK_RV
mock_C_GetFunctionStatus(CK_SESSION_HANDLE session)1032 mock_C_GetFunctionStatus (CK_SESSION_HANDLE session)
1033 {
1034 	if (!p11_dict_get (the_sessions, handle_to_pointer (session)))
1035 		return CKR_SESSION_HANDLE_INVALID;
1036 	return CKR_FUNCTION_NOT_PARALLEL;
1037 }
1038 
1039 CK_RV
mock_C_GetFunctionStatus__not_parallel(CK_SESSION_HANDLE session)1040 mock_C_GetFunctionStatus__not_parallel (CK_SESSION_HANDLE session)
1041 {
1042 	return CKR_FUNCTION_NOT_PARALLEL;
1043 }
1044 
1045 CK_RV
mock_C_CancelFunction(CK_SESSION_HANDLE session)1046 mock_C_CancelFunction (CK_SESSION_HANDLE session)
1047 {
1048 	if (!p11_dict_get (the_sessions, handle_to_pointer (session)))
1049 		return CKR_SESSION_HANDLE_INVALID;
1050 	return CKR_FUNCTION_NOT_PARALLEL;
1051 }
1052 
1053 CK_RV
mock_C_CancelFunction__not_parallel(CK_SESSION_HANDLE session)1054 mock_C_CancelFunction__not_parallel (CK_SESSION_HANDLE session)
1055 {
1056 	return CKR_FUNCTION_NOT_PARALLEL;
1057 }
1058 
1059 CK_RV
mock_C_GetSessionInfo(CK_SESSION_HANDLE session,CK_SESSION_INFO_PTR info)1060 mock_C_GetSessionInfo (CK_SESSION_HANDLE session,
1061                        CK_SESSION_INFO_PTR info)
1062 {
1063 	Session *sess;
1064 
1065 	return_val_if_fail (info != NULL, CKR_ARGUMENTS_BAD);
1066 
1067 	sess = p11_dict_get (the_sessions, handle_to_pointer (session));
1068 	if (!sess)
1069 		return CKR_SESSION_HANDLE_INVALID;
1070 
1071 	if (logged_in) {
1072 		if (sess->info.flags & CKF_RW_SESSION)
1073 			sess->info.state = CKS_RW_USER_FUNCTIONS;
1074 		else
1075 			sess->info.state = CKS_RO_USER_FUNCTIONS;
1076 	} else {
1077 		if (sess->info.flags & CKF_RW_SESSION)
1078 			sess->info.state = CKS_RW_PUBLIC_SESSION;
1079 		else
1080 			sess->info.state = CKS_RO_PUBLIC_SESSION;
1081 	}
1082 
1083 	memcpy (info, &sess->info, sizeof (*info));
1084 	return CKR_OK;
1085 }
1086 
1087 CK_RV
mock_C_GetSessionInfo__invalid_handle(CK_SESSION_HANDLE session,CK_SESSION_INFO_PTR info)1088 mock_C_GetSessionInfo__invalid_handle (CK_SESSION_HANDLE session,
1089                                        CK_SESSION_INFO_PTR info)
1090 {
1091 	return_val_if_fail (info, CKR_ARGUMENTS_BAD);
1092 
1093 	return CKR_SESSION_HANDLE_INVALID;
1094 }
1095 
1096 CK_RV
mock_X_GetSessionInfo__invalid_handle(CK_X_FUNCTION_LIST * self,CK_SESSION_HANDLE session,CK_SESSION_INFO_PTR info)1097 mock_X_GetSessionInfo__invalid_handle (CK_X_FUNCTION_LIST *self,
1098                                        CK_SESSION_HANDLE session,
1099                                        CK_SESSION_INFO_PTR info)
1100 {
1101 	return_val_if_fail (info, CKR_ARGUMENTS_BAD);
1102 
1103 	return CKR_SESSION_HANDLE_INVALID;
1104 }
1105 
1106 CK_RV
mock_C_InitPIN__specific_args(CK_SESSION_HANDLE session,CK_UTF8CHAR_PTR pin,CK_ULONG pin_len)1107 mock_C_InitPIN__specific_args (CK_SESSION_HANDLE session,
1108                                CK_UTF8CHAR_PTR pin,
1109                                CK_ULONG pin_len)
1110 {
1111 	Session *sess;
1112 
1113 	sess = p11_dict_get (the_sessions, handle_to_pointer (session));
1114 	if (sess == NULL)
1115 		return CKR_SESSION_HANDLE_INVALID;
1116 
1117 	if (strlen ("TEST PIN") != pin_len ||
1118 	    strncmp ((char *)pin, "TEST PIN", pin_len) != 0)
1119 		return CKR_PIN_INVALID;
1120 
1121 	free (the_pin);
1122 	the_pin = memdup (pin, pin_len);
1123 	return_val_if_fail (the_pin != NULL, CKR_HOST_MEMORY);
1124 	n_the_pin = pin_len;
1125 	return CKR_OK;
1126 }
1127 
1128 CK_RV
mock_C_InitPIN__invalid_handle(CK_SESSION_HANDLE session,CK_UTF8CHAR_PTR pin,CK_ULONG pin_len)1129 mock_C_InitPIN__invalid_handle (CK_SESSION_HANDLE session,
1130                                 CK_UTF8CHAR_PTR pin,
1131                                 CK_ULONG pin_len)
1132 {
1133 	return CKR_SESSION_HANDLE_INVALID;
1134 }
1135 
1136 CK_RV
mock_X_InitPIN__invalid_handle(CK_X_FUNCTION_LIST * self,CK_SESSION_HANDLE session,CK_UTF8CHAR_PTR pin,CK_ULONG pin_len)1137 mock_X_InitPIN__invalid_handle (CK_X_FUNCTION_LIST *self,
1138                                 CK_SESSION_HANDLE session,
1139                                 CK_UTF8CHAR_PTR pin,
1140                                 CK_ULONG pin_len)
1141 {
1142 	return CKR_SESSION_HANDLE_INVALID;
1143 }
1144 
1145 CK_RV
mock_C_SetPIN__specific_args(CK_SESSION_HANDLE session,CK_UTF8CHAR_PTR old_pin,CK_ULONG old_pin_len,CK_UTF8CHAR_PTR new_pin,CK_ULONG new_pin_len)1146 mock_C_SetPIN__specific_args (CK_SESSION_HANDLE session,
1147                               CK_UTF8CHAR_PTR old_pin,
1148                               CK_ULONG old_pin_len,
1149                               CK_UTF8CHAR_PTR new_pin,
1150                               CK_ULONG new_pin_len)
1151 {
1152 	Session *sess;
1153 
1154 	sess = p11_dict_get (the_sessions, handle_to_pointer (session));
1155 	if (sess == NULL)
1156 		return CKR_SESSION_HANDLE_INVALID;
1157 
1158 	if (old_pin_len != n_the_pin)
1159 		return CKR_PIN_INCORRECT;
1160 	if (memcmp (old_pin, the_pin, n_the_pin) != 0)
1161 		return CKR_PIN_INCORRECT;
1162 
1163 	if (strlen ("TEST PIN") != new_pin_len ||
1164 	    strncmp ((char *)new_pin, "TEST PIN", new_pin_len) != 0)
1165 		return CKR_PIN_INVALID;
1166 
1167 	free (the_pin);
1168 	the_pin = memdup (new_pin, new_pin_len);
1169 	return_val_if_fail (the_pin != NULL, CKR_HOST_MEMORY);
1170 	n_the_pin = new_pin_len;
1171 	return CKR_OK;
1172 }
1173 
1174 CK_RV
mock_C_SetPIN__invalid_handle(CK_SESSION_HANDLE session,CK_UTF8CHAR_PTR old_pin,CK_ULONG old_pin_len,CK_UTF8CHAR_PTR new_pin,CK_ULONG new_pin_len)1175 mock_C_SetPIN__invalid_handle (CK_SESSION_HANDLE session,
1176                                CK_UTF8CHAR_PTR old_pin,
1177                                CK_ULONG old_pin_len,
1178                                CK_UTF8CHAR_PTR new_pin,
1179                                CK_ULONG new_pin_len)
1180 {
1181 	return CKR_SESSION_HANDLE_INVALID;
1182 }
1183 
1184 CK_RV
mock_X_SetPIN__invalid_handle(CK_X_FUNCTION_LIST * self,CK_SESSION_HANDLE session,CK_UTF8CHAR_PTR old_pin,CK_ULONG old_pin_len,CK_UTF8CHAR_PTR new_pin,CK_ULONG new_pin_len)1185 mock_X_SetPIN__invalid_handle (CK_X_FUNCTION_LIST *self,
1186                                CK_SESSION_HANDLE session,
1187                                CK_UTF8CHAR_PTR old_pin,
1188                                CK_ULONG old_pin_len,
1189                                CK_UTF8CHAR_PTR new_pin,
1190                                CK_ULONG new_pin_len)
1191 {
1192 	return CKR_SESSION_HANDLE_INVALID;
1193 }
1194 
1195 CK_RV
mock_C_GetOperationState(CK_SESSION_HANDLE session,CK_BYTE_PTR operation_state,CK_ULONG_PTR operation_state_len)1196 mock_C_GetOperationState (CK_SESSION_HANDLE session,
1197                           CK_BYTE_PTR operation_state,
1198                           CK_ULONG_PTR operation_state_len)
1199 {
1200 	Session *sess;
1201 
1202 	return_val_if_fail (operation_state_len, CKR_ARGUMENTS_BAD);
1203 
1204 	sess = p11_dict_get (the_sessions, handle_to_pointer (session));
1205 	if (sess == NULL)
1206 		return CKR_SESSION_HANDLE_INVALID;
1207 
1208 	if (!operation_state) {
1209 		*operation_state_len = sizeof (sess);
1210 		return CKR_OK;
1211 	}
1212 
1213 	if (*operation_state_len < sizeof (sess))
1214 		return CKR_BUFFER_TOO_SMALL;
1215 
1216 	memcpy (operation_state, &sess, sizeof (sess));
1217 	*operation_state_len = sizeof (sess);
1218 	return CKR_OK;
1219 }
1220 
1221 CK_RV
mock_C_GetOperationState__invalid_handle(CK_SESSION_HANDLE session,CK_BYTE_PTR operation_state,CK_ULONG_PTR operation_state_len)1222 mock_C_GetOperationState__invalid_handle (CK_SESSION_HANDLE session,
1223                                           CK_BYTE_PTR operation_state,
1224                                           CK_ULONG_PTR operation_state_len)
1225 {
1226 	return CKR_FUNCTION_NOT_SUPPORTED;
1227 }
1228 
1229 CK_RV
mock_X_GetOperationState__invalid_handle(CK_X_FUNCTION_LIST * self,CK_SESSION_HANDLE session,CK_BYTE_PTR operation_state,CK_ULONG_PTR operation_state_len)1230 mock_X_GetOperationState__invalid_handle (CK_X_FUNCTION_LIST *self,
1231                                           CK_SESSION_HANDLE session,
1232                                           CK_BYTE_PTR operation_state,
1233                                           CK_ULONG_PTR operation_state_len)
1234 {
1235 	return CKR_FUNCTION_NOT_SUPPORTED;
1236 }
1237 
1238 CK_RV
mock_C_SetOperationState(CK_SESSION_HANDLE session,CK_BYTE_PTR operation_state,CK_ULONG operation_state_len,CK_OBJECT_HANDLE encryption_key,CK_OBJECT_HANDLE authentication_key)1239 mock_C_SetOperationState (CK_SESSION_HANDLE session,
1240                           CK_BYTE_PTR operation_state,
1241                           CK_ULONG operation_state_len,
1242                           CK_OBJECT_HANDLE encryption_key,
1243                           CK_OBJECT_HANDLE authentication_key)
1244 {
1245 	Session *sess;
1246 
1247 	sess = p11_dict_get (the_sessions, handle_to_pointer (session));
1248 	if (sess == NULL)
1249 		return CKR_SESSION_HANDLE_INVALID;
1250 
1251 	if (!operation_state || operation_state_len != sizeof (sess))
1252 		return CKR_ARGUMENTS_BAD;
1253 
1254 	/* Yes, just arbitrary numbers, to make sure they got through */
1255 	if (encryption_key != 355 || authentication_key != 455)
1256 		return CKR_KEY_HANDLE_INVALID;
1257 	if (memcmp (operation_state, &sess, sizeof (sess)) != 0)
1258 		return CKR_SAVED_STATE_INVALID;
1259 	return CKR_OK;
1260 }
1261 
1262 CK_RV
mock_C_SetOperationState__invalid_handle(CK_SESSION_HANDLE session,CK_BYTE_PTR operation_state,CK_ULONG operation_state_len,CK_OBJECT_HANDLE encryption_key,CK_OBJECT_HANDLE authentication_key)1263 mock_C_SetOperationState__invalid_handle (CK_SESSION_HANDLE session,
1264                                           CK_BYTE_PTR operation_state,
1265                                           CK_ULONG operation_state_len,
1266                                           CK_OBJECT_HANDLE encryption_key,
1267                                           CK_OBJECT_HANDLE authentication_key)
1268 {
1269 	return CKR_SESSION_HANDLE_INVALID;
1270 }
1271 
1272 CK_RV
mock_X_SetOperationState__invalid_handle(CK_X_FUNCTION_LIST * self,CK_SESSION_HANDLE session,CK_BYTE_PTR operation_state,CK_ULONG operation_state_len,CK_OBJECT_HANDLE encryption_key,CK_OBJECT_HANDLE authentication_key)1273 mock_X_SetOperationState__invalid_handle (CK_X_FUNCTION_LIST *self,
1274                                           CK_SESSION_HANDLE session,
1275                                           CK_BYTE_PTR operation_state,
1276                                           CK_ULONG operation_state_len,
1277                                           CK_OBJECT_HANDLE encryption_key,
1278                                           CK_OBJECT_HANDLE authentication_key)
1279 {
1280 	return CKR_SESSION_HANDLE_INVALID;
1281 }
1282 
1283 CK_RV
mock_C_Login(CK_SESSION_HANDLE session,CK_USER_TYPE user_type,CK_UTF8CHAR_PTR pin,CK_ULONG pin_len)1284 mock_C_Login (CK_SESSION_HANDLE session,
1285               CK_USER_TYPE user_type,
1286               CK_UTF8CHAR_PTR pin,
1287               CK_ULONG pin_len)
1288 {
1289 	Session *sess;
1290 
1291 	return_val_if_fail (user_type == CKU_SO ||
1292 	                    user_type == CKU_USER ||
1293 	                    user_type == CKU_CONTEXT_SPECIFIC,
1294 	                    CKR_USER_TYPE_INVALID);
1295 
1296 	sess = p11_dict_get (the_sessions, handle_to_pointer (session));
1297 	if (sess == NULL)
1298 		return CKR_SESSION_HANDLE_INVALID;
1299 
1300 	if (logged_in && user_type != CKU_CONTEXT_SPECIFIC)
1301 		return CKR_USER_ALREADY_LOGGED_IN;
1302 
1303 	if (!pin)
1304 		return CKR_PIN_INCORRECT;
1305 
1306 	if (pin_len != n_the_pin)
1307 		return CKR_PIN_INCORRECT;
1308 	if (strncmp ((char *)pin, (char *)the_pin, pin_len) != 0)
1309 		return CKR_PIN_INCORRECT;
1310 
1311 	if (user_type == CKU_CONTEXT_SPECIFIC) {
1312 		return_val_if_fail (sess->want_context_login, CKR_OPERATION_NOT_INITIALIZED);
1313 		sess->want_context_login = false;
1314 	} else {
1315 		logged_in = true;
1316 		the_user_type = user_type;
1317 	}
1318 
1319 	return CKR_OK;
1320 }
1321 
1322 CK_RV
mock_C_Login__invalid_handle(CK_SESSION_HANDLE session,CK_USER_TYPE user_type,CK_UTF8CHAR_PTR pin,CK_ULONG pin_len)1323 mock_C_Login__invalid_handle (CK_SESSION_HANDLE session,
1324                               CK_USER_TYPE user_type,
1325                               CK_UTF8CHAR_PTR pin,
1326                               CK_ULONG pin_len)
1327 {
1328 	return CKR_SESSION_HANDLE_INVALID;
1329 }
1330 
1331 CK_RV
mock_X_Login__invalid_handle(CK_X_FUNCTION_LIST * self,CK_SESSION_HANDLE session,CK_USER_TYPE user_type,CK_UTF8CHAR_PTR pin,CK_ULONG pin_len)1332 mock_X_Login__invalid_handle (CK_X_FUNCTION_LIST *self,
1333                               CK_SESSION_HANDLE session,
1334                               CK_USER_TYPE user_type,
1335                               CK_UTF8CHAR_PTR pin,
1336                               CK_ULONG pin_len)
1337 {
1338 	return CKR_SESSION_HANDLE_INVALID;
1339 }
1340 
1341 CK_RV
mock_C_Logout(CK_SESSION_HANDLE session)1342 mock_C_Logout (CK_SESSION_HANDLE session)
1343 {
1344 	Session *sess;
1345 
1346 	sess = p11_dict_get (the_sessions, handle_to_pointer (session));
1347 	if (!sess)
1348 		return CKR_SESSION_HANDLE_INVALID;
1349 
1350 	if (!logged_in)
1351 		return CKR_USER_NOT_LOGGED_IN;
1352 
1353 	logged_in = false;
1354 	the_user_type = 0;
1355 	return CKR_OK;
1356 }
1357 
1358 CK_RV
mock_C_Logout__invalid_handle(CK_SESSION_HANDLE session)1359 mock_C_Logout__invalid_handle (CK_SESSION_HANDLE session)
1360 {
1361 	return CKR_SESSION_HANDLE_INVALID;
1362 }
1363 
1364 CK_RV
mock_X_Logout__invalid_handle(CK_X_FUNCTION_LIST * self,CK_SESSION_HANDLE session)1365 mock_X_Logout__invalid_handle (CK_X_FUNCTION_LIST *self,
1366                                CK_SESSION_HANDLE session)
1367 {
1368 	return CKR_SESSION_HANDLE_INVALID;
1369 }
1370 
1371 CK_RV
mock_C_CreateObject(CK_SESSION_HANDLE session,CK_ATTRIBUTE_PTR template,CK_ULONG count,CK_OBJECT_HANDLE_PTR object)1372 mock_C_CreateObject (CK_SESSION_HANDLE session,
1373                      CK_ATTRIBUTE_PTR template,
1374                      CK_ULONG count,
1375                      CK_OBJECT_HANDLE_PTR object)
1376 {
1377 	CK_ATTRIBUTE *attrs;
1378 	Session *sess;
1379 	CK_BBOOL token, priv;
1380 
1381 	return_val_if_fail (object, CKR_ARGUMENTS_BAD);
1382 
1383 	sess = p11_dict_get (the_sessions, handle_to_pointer (session));
1384 	if (!sess)
1385 		return CKR_SESSION_HANDLE_INVALID;
1386 
1387 	attrs = p11_attrs_buildn (NULL, template, count);
1388 
1389 	if (p11_attrs_find_bool (attrs, CKA_PRIVATE, &priv) && priv) {
1390 		if (!logged_in) {
1391 			p11_attrs_free (attrs);
1392 			return CKR_USER_NOT_LOGGED_IN;
1393 		}
1394 	}
1395 
1396 	*object = ++unique_identifier;
1397 	if (p11_attrs_find_bool (attrs, CKA_TOKEN, &token) && token)
1398 		p11_dict_set (the_objects, handle_to_pointer (*object), attrs);
1399 	else
1400 		p11_dict_set (sess->objects, handle_to_pointer (*object), attrs);
1401 
1402 	return CKR_OK;
1403 }
1404 
1405 CK_RV
mock_C_CreateObject__invalid_handle(CK_SESSION_HANDLE session,CK_ATTRIBUTE_PTR template,CK_ULONG count,CK_OBJECT_HANDLE_PTR new_object)1406 mock_C_CreateObject__invalid_handle (CK_SESSION_HANDLE session,
1407                                      CK_ATTRIBUTE_PTR template,
1408                                      CK_ULONG count,
1409                                      CK_OBJECT_HANDLE_PTR new_object)
1410 {
1411 	return_val_if_fail (new_object, CKR_ARGUMENTS_BAD);
1412 
1413 	return CKR_SESSION_HANDLE_INVALID;
1414 }
1415 
1416 CK_RV
mock_X_CreateObject__invalid_handle(CK_X_FUNCTION_LIST * self,CK_SESSION_HANDLE session,CK_ATTRIBUTE_PTR template,CK_ULONG count,CK_OBJECT_HANDLE_PTR new_object)1417 mock_X_CreateObject__invalid_handle (CK_X_FUNCTION_LIST *self,
1418                                      CK_SESSION_HANDLE session,
1419                                      CK_ATTRIBUTE_PTR template,
1420                                      CK_ULONG count,
1421                                      CK_OBJECT_HANDLE_PTR new_object)
1422 {
1423 	return_val_if_fail (new_object, CKR_ARGUMENTS_BAD);
1424 
1425 	return CKR_SESSION_HANDLE_INVALID;
1426 }
1427 
1428 CK_RV
mock_C_CopyObject(CK_SESSION_HANDLE session,CK_OBJECT_HANDLE object,CK_ATTRIBUTE_PTR template,CK_ULONG count,CK_OBJECT_HANDLE_PTR new_object)1429 mock_C_CopyObject (CK_SESSION_HANDLE session,
1430                    CK_OBJECT_HANDLE object,
1431                    CK_ATTRIBUTE_PTR template,
1432                    CK_ULONG count,
1433                    CK_OBJECT_HANDLE_PTR new_object)
1434 {
1435 	CK_ATTRIBUTE *attrs;
1436 	Session *sess;
1437 	CK_BBOOL token, priv;
1438 	CK_RV rv;
1439 
1440 	return_val_if_fail (object, CKR_ARGUMENTS_BAD);
1441 
1442 	sess = p11_dict_get (the_sessions, handle_to_pointer (session));
1443 	if (!sess)
1444 		return CKR_SESSION_HANDLE_INVALID;
1445 
1446 	rv = lookup_object (sess, object, &attrs, NULL);
1447 	if (rv != CKR_OK)
1448 		return rv;
1449 
1450 	if (p11_attrs_find_bool (attrs, CKA_PRIVATE, &priv) && priv) {
1451 		if (!logged_in)
1452 			return CKR_USER_NOT_LOGGED_IN;
1453 	}
1454 
1455 	attrs = p11_attrs_buildn (p11_attrs_dup (attrs), template, count);
1456 
1457 	*new_object = ++unique_identifier;
1458 	if (p11_attrs_find_bool (attrs, CKA_TOKEN, &token) && token)
1459 		p11_dict_set (the_objects, handle_to_pointer (*new_object), attrs);
1460 	else
1461 		p11_dict_set (sess->objects, handle_to_pointer (*new_object), attrs);
1462 
1463 	return CKR_OK;
1464 }
1465 
1466 CK_RV
mock_C_CopyObject__invalid_handle(CK_SESSION_HANDLE session,CK_OBJECT_HANDLE object,CK_ATTRIBUTE_PTR template,CK_ULONG count,CK_OBJECT_HANDLE_PTR new_object)1467 mock_C_CopyObject__invalid_handle (CK_SESSION_HANDLE session,
1468                                    CK_OBJECT_HANDLE object,
1469                                    CK_ATTRIBUTE_PTR template,
1470                                    CK_ULONG count,
1471                                    CK_OBJECT_HANDLE_PTR new_object)
1472 {
1473 	return_val_if_fail (new_object, CKR_ARGUMENTS_BAD);
1474 
1475 	return CKR_SESSION_HANDLE_INVALID;
1476 }
1477 
1478 
1479 CK_RV
mock_X_CopyObject__invalid_handle(CK_X_FUNCTION_LIST * self,CK_SESSION_HANDLE session,CK_OBJECT_HANDLE object,CK_ATTRIBUTE_PTR template,CK_ULONG count,CK_OBJECT_HANDLE_PTR new_object)1480 mock_X_CopyObject__invalid_handle (CK_X_FUNCTION_LIST *self,
1481                                    CK_SESSION_HANDLE session,
1482                                    CK_OBJECT_HANDLE object,
1483                                    CK_ATTRIBUTE_PTR template,
1484                                    CK_ULONG count,
1485                                    CK_OBJECT_HANDLE_PTR new_object)
1486 {
1487 	return_val_if_fail (new_object, CKR_ARGUMENTS_BAD);
1488 
1489 	return CKR_SESSION_HANDLE_INVALID;
1490 }
1491 
1492 CK_RV
mock_C_DestroyObject(CK_SESSION_HANDLE session,CK_OBJECT_HANDLE object)1493 mock_C_DestroyObject (CK_SESSION_HANDLE session,
1494                       CK_OBJECT_HANDLE object)
1495 {
1496 	CK_ATTRIBUTE *attrs;
1497 	Session *sess;
1498 	p11_dict *table;
1499 	CK_RV rv;
1500 
1501 	sess = p11_dict_get (the_sessions, handle_to_pointer (session));
1502 	if (!sess)
1503 		return CKR_SESSION_HANDLE_INVALID;
1504 
1505 	rv = lookup_object (sess, object, &attrs, &table);
1506 	if (rv != CKR_OK)
1507 		return rv;
1508 
1509 	p11_dict_remove (table, handle_to_pointer (object));
1510 	return CKR_OK;
1511 }
1512 
1513 CK_RV
mock_C_DestroyObject__invalid_handle(CK_SESSION_HANDLE session,CK_OBJECT_HANDLE object)1514 mock_C_DestroyObject__invalid_handle (CK_SESSION_HANDLE session,
1515                                       CK_OBJECT_HANDLE object)
1516 {
1517 	return CKR_SESSION_HANDLE_INVALID;
1518 }
1519 
1520 CK_RV
mock_X_DestroyObject__invalid_handle(CK_X_FUNCTION_LIST * self,CK_SESSION_HANDLE session,CK_OBJECT_HANDLE object)1521 mock_X_DestroyObject__invalid_handle (CK_X_FUNCTION_LIST *self,
1522                                       CK_SESSION_HANDLE session,
1523                                       CK_OBJECT_HANDLE object)
1524 {
1525 	return CKR_SESSION_HANDLE_INVALID;
1526 }
1527 
1528 CK_RV
mock_C_GetObjectSize(CK_SESSION_HANDLE session,CK_OBJECT_HANDLE object,CK_ULONG_PTR size)1529 mock_C_GetObjectSize (CK_SESSION_HANDLE session,
1530                       CK_OBJECT_HANDLE object,
1531                       CK_ULONG_PTR size)
1532 {
1533 	CK_ATTRIBUTE *attrs;
1534 	Session *sess;
1535 	CK_RV rv;
1536 	CK_ULONG i;
1537 
1538 	return_val_if_fail (size != NULL, CKR_ARGUMENTS_BAD);
1539 
1540 	sess = p11_dict_get (the_sessions, handle_to_pointer (session));
1541 	if (!sess)
1542 		return CKR_SESSION_HANDLE_INVALID;
1543 
1544 	rv = lookup_object (sess, object, &attrs, NULL);
1545 	if (rv != CKR_OK)
1546 		return rv;
1547 
1548 	*size = 0;
1549 	for (i = 0; !p11_attrs_terminator (attrs + i); i++) {
1550 		if (attrs[i].ulValueLen != (CK_ULONG)-1)
1551 			*size += attrs[i].ulValueLen;
1552 	}
1553 
1554 	return CKR_OK;
1555 }
1556 
1557 CK_RV
mock_C_GetObjectSize__invalid_handle(CK_SESSION_HANDLE session,CK_OBJECT_HANDLE object,CK_ULONG_PTR size)1558 mock_C_GetObjectSize__invalid_handle (CK_SESSION_HANDLE session,
1559                                       CK_OBJECT_HANDLE object,
1560                                       CK_ULONG_PTR size)
1561 {
1562 	return_val_if_fail (size, CKR_ARGUMENTS_BAD);
1563 
1564 	return CKR_SESSION_HANDLE_INVALID;
1565 }
1566 
1567 CK_RV
mock_X_GetObjectSize__invalid_handle(CK_X_FUNCTION_LIST * self,CK_SESSION_HANDLE session,CK_OBJECT_HANDLE object,CK_ULONG_PTR size)1568 mock_X_GetObjectSize__invalid_handle (CK_X_FUNCTION_LIST *self,
1569                                       CK_SESSION_HANDLE session,
1570                                       CK_OBJECT_HANDLE object,
1571                                       CK_ULONG_PTR size)
1572 {
1573 	return_val_if_fail (size, CKR_ARGUMENTS_BAD);
1574 
1575 	return CKR_SESSION_HANDLE_INVALID;
1576 }
1577 
1578 CK_RV
mock_C_GetAttributeValue(CK_SESSION_HANDLE session,CK_OBJECT_HANDLE object,CK_ATTRIBUTE_PTR template,CK_ULONG count)1579 mock_C_GetAttributeValue (CK_SESSION_HANDLE session,
1580                           CK_OBJECT_HANDLE object,
1581                           CK_ATTRIBUTE_PTR template,
1582                           CK_ULONG count)
1583 {
1584 	CK_ATTRIBUTE *result;
1585 	CK_RV ret = CKR_OK;
1586 	CK_ATTRIBUTE *attrs;
1587 	CK_ATTRIBUTE *attr;
1588 	Session *sess;
1589 	CK_ULONG i;
1590 	CK_RV rv;
1591 
1592 	sess = p11_dict_get (the_sessions, handle_to_pointer (session));
1593 	if (sess == NULL)
1594 		return CKR_SESSION_HANDLE_INVALID;
1595 
1596 	rv = lookup_object (sess, object, &attrs, NULL);
1597 	if (rv != CKR_OK)
1598 		return rv;
1599 
1600 	for (i = 0; i < count; ++i) {
1601 		result = template + i;
1602 		attr = p11_attrs_find (attrs, result->type);
1603 		if (!attr) {
1604 			result->ulValueLen = (CK_ULONG)-1;
1605 			ret = CKR_ATTRIBUTE_TYPE_INVALID;
1606 			continue;
1607 		}
1608 
1609 		if (!result->pValue) {
1610 			result->ulValueLen = attr->ulValueLen;
1611 			continue;
1612 		}
1613 
1614 		if (result->ulValueLen >= attr->ulValueLen) {
1615 			memcpy (result->pValue, attr->pValue, attr->ulValueLen);
1616 			result->ulValueLen = attr->ulValueLen;
1617 			continue;
1618 		}
1619 
1620 		result->ulValueLen = (CK_ULONG)-1;
1621 		ret = CKR_BUFFER_TOO_SMALL;
1622 	}
1623 
1624 	return ret;
1625 }
1626 
1627 CK_RV
mock_C_GetAttributeValue__invalid_handle(CK_SESSION_HANDLE session,CK_OBJECT_HANDLE object,CK_ATTRIBUTE_PTR template,CK_ULONG count)1628 mock_C_GetAttributeValue__invalid_handle (CK_SESSION_HANDLE session,
1629                                           CK_OBJECT_HANDLE object,
1630                                           CK_ATTRIBUTE_PTR template,
1631                                           CK_ULONG count)
1632 {
1633 	return CKR_SESSION_HANDLE_INVALID;
1634 }
1635 
1636 CK_RV
mock_X_GetAttributeValue__invalid_handle(CK_X_FUNCTION_LIST * self,CK_SESSION_HANDLE session,CK_OBJECT_HANDLE object,CK_ATTRIBUTE_PTR template,CK_ULONG count)1637 mock_X_GetAttributeValue__invalid_handle (CK_X_FUNCTION_LIST *self,
1638                                           CK_SESSION_HANDLE session,
1639                                           CK_OBJECT_HANDLE object,
1640                                           CK_ATTRIBUTE_PTR template,
1641                                           CK_ULONG count)
1642 {
1643 	return CKR_SESSION_HANDLE_INVALID;
1644 }
1645 
1646 CK_RV
mock_C_GetAttributeValue__fail_first(CK_SESSION_HANDLE session,CK_OBJECT_HANDLE object,CK_ATTRIBUTE_PTR template,CK_ULONG count)1647 mock_C_GetAttributeValue__fail_first (CK_SESSION_HANDLE session,
1648                                       CK_OBJECT_HANDLE object,
1649                                       CK_ATTRIBUTE_PTR template,
1650                                       CK_ULONG count)
1651 {
1652 	return CKR_FUNCTION_REJECTED;
1653 }
1654 
1655 CK_RV
mock_C_GetAttributeValue__fail_late(CK_SESSION_HANDLE session,CK_OBJECT_HANDLE object,CK_ATTRIBUTE_PTR template,CK_ULONG count)1656 mock_C_GetAttributeValue__fail_late (CK_SESSION_HANDLE session,
1657                                      CK_OBJECT_HANDLE object,
1658                                      CK_ATTRIBUTE_PTR template,
1659                                      CK_ULONG count)
1660 {
1661 	CK_ULONG i;
1662 
1663 	for (i = 0; i < count; i++) {
1664 		if (template[i].pValue)
1665 			return CKR_FUNCTION_FAILED;
1666 	}
1667 	return mock_C_GetAttributeValue (session, object, template, count);
1668 }
1669 
1670 CK_RV
mock_C_SetAttributeValue(CK_SESSION_HANDLE session,CK_OBJECT_HANDLE object,CK_ATTRIBUTE_PTR template,CK_ULONG count)1671 mock_C_SetAttributeValue (CK_SESSION_HANDLE session,
1672                           CK_OBJECT_HANDLE object,
1673                           CK_ATTRIBUTE_PTR template,
1674                           CK_ULONG count)
1675 {
1676 	Session *sess;
1677 	CK_ATTRIBUTE *attrs;
1678 	p11_dict *table;
1679 	CK_RV rv;
1680 
1681 	sess = p11_dict_get (the_sessions, handle_to_pointer (session));
1682 	if (!sess)
1683 		return CKR_SESSION_HANDLE_INVALID;
1684 
1685 	rv = lookup_object (sess, object, &attrs, &table);
1686 	if (rv != CKR_OK)
1687 		return rv;
1688 
1689 	p11_dict_steal (table, handle_to_pointer (object), NULL, (void **)&attrs);
1690 	attrs = p11_attrs_buildn (attrs, template, count);
1691 	p11_dict_set (table, handle_to_pointer (object), attrs);
1692 	return CKR_OK;
1693 }
1694 
1695 CK_RV
mock_C_SetAttributeValue__invalid_handle(CK_SESSION_HANDLE session,CK_OBJECT_HANDLE object,CK_ATTRIBUTE_PTR template,CK_ULONG count)1696 mock_C_SetAttributeValue__invalid_handle (CK_SESSION_HANDLE session,
1697                                           CK_OBJECT_HANDLE object,
1698                                           CK_ATTRIBUTE_PTR template,
1699                                           CK_ULONG count)
1700 {
1701 	return CKR_SESSION_HANDLE_INVALID;
1702 }
1703 
1704 CK_RV
mock_X_SetAttributeValue__invalid_handle(CK_X_FUNCTION_LIST * self,CK_SESSION_HANDLE session,CK_OBJECT_HANDLE object,CK_ATTRIBUTE_PTR template,CK_ULONG count)1705 mock_X_SetAttributeValue__invalid_handle (CK_X_FUNCTION_LIST *self,
1706                                           CK_SESSION_HANDLE session,
1707                                           CK_OBJECT_HANDLE object,
1708                                           CK_ATTRIBUTE_PTR template,
1709                                           CK_ULONG count)
1710 {
1711 	return CKR_SESSION_HANDLE_INVALID;
1712 }
1713 
1714 typedef struct _FindObjects {
1715 	CK_ATTRIBUTE *template;
1716 	CK_ULONG count;
1717 	Session *sess;
1718 } FindObjects;
1719 
1720 static bool
enumerate_and_find_objects(CK_OBJECT_HANDLE object,CK_ATTRIBUTE * attrs,void * user_data)1721 enumerate_and_find_objects (CK_OBJECT_HANDLE object,
1722                             CK_ATTRIBUTE *attrs,
1723                             void *user_data)
1724 {
1725 	FindObjects *ctx = user_data;
1726 	CK_ATTRIBUTE *match;
1727 	CK_ATTRIBUTE *attr;
1728 	CK_BBOOL private;
1729 	CK_ULONG i;
1730 
1731 	if (!logged_in) {
1732 		if (p11_attrs_find_bool (attrs, CKA_PRIVATE, &private) && private)
1733 			return 1; /* Continue */
1734 	}
1735 
1736 	for (i = 0; i < ctx->count; ++i) {
1737 		match = ctx->template + i;
1738 		attr = p11_attrs_find (attrs, match->type);
1739 		if (!attr)
1740 			return true; /* Continue */
1741 
1742 		if (attr->ulValueLen != match->ulValueLen ||
1743 		    memcmp (attr->pValue, match->pValue, attr->ulValueLen) != 0)
1744 			return true; /* Continue */
1745 	}
1746 
1747 	p11_array_push (ctx->sess->matches, handle_to_pointer (object));
1748 	return true; /* Continue */
1749 }
1750 
1751 static int
compar_handles(const void * one,const void * two)1752 compar_handles (const void *one,
1753                 const void *two)
1754 {
1755 	void **p1 = (void **)one;
1756 	void **p2 = (void **)two;
1757 	return pointer_to_handle (*p2) - pointer_to_handle (*p1);
1758 }
1759 
1760 CK_RV
mock_C_FindObjectsInit(CK_SESSION_HANDLE session,CK_ATTRIBUTE_PTR template,CK_ULONG count)1761 mock_C_FindObjectsInit (CK_SESSION_HANDLE session,
1762                         CK_ATTRIBUTE_PTR template,
1763                         CK_ULONG count)
1764 {
1765 	Session *sess;
1766 	FindObjects ctx;
1767 
1768 	sess = p11_dict_get (the_sessions, handle_to_pointer (session));
1769 	if (!sess)
1770 		return CKR_SESSION_HANDLE_INVALID;
1771 
1772 	/* Starting an operation, cancels any previous one */
1773 	sess->crypto_mechanism = 0;
1774 	sess->hash_mechanism = 0;
1775 
1776 	sess->finding = true;
1777 	p11_array_free (sess->matches);
1778 	sess->matches = p11_array_new (NULL);
1779 
1780 	ctx.template = template;
1781 	ctx.count = count;
1782 	ctx.sess = sess;
1783 
1784 	mock_module_enumerate_objects (session, enumerate_and_find_objects, &ctx);
1785 	qsort (sess->matches->elem, sess->matches->num, sizeof (void *), compar_handles);
1786 	return CKR_OK;
1787 }
1788 
1789 CK_RV
mock_C_FindObjectsInit__invalid_handle(CK_SESSION_HANDLE session,CK_ATTRIBUTE_PTR template,CK_ULONG count)1790 mock_C_FindObjectsInit__invalid_handle (CK_SESSION_HANDLE session,
1791                                         CK_ATTRIBUTE_PTR template,
1792                                         CK_ULONG count)
1793 {
1794 	return CKR_SESSION_HANDLE_INVALID;
1795 }
1796 
1797 CK_RV
mock_X_FindObjectsInit__invalid_handle(CK_X_FUNCTION_LIST * self,CK_SESSION_HANDLE session,CK_ATTRIBUTE_PTR template,CK_ULONG count)1798 mock_X_FindObjectsInit__invalid_handle (CK_X_FUNCTION_LIST *self,
1799                                         CK_SESSION_HANDLE session,
1800                                         CK_ATTRIBUTE_PTR template,
1801                                         CK_ULONG count)
1802 {
1803 	return CKR_SESSION_HANDLE_INVALID;
1804 }
1805 
1806 CK_RV
mock_C_FindObjectsInit__fails(CK_SESSION_HANDLE session,CK_ATTRIBUTE_PTR template,CK_ULONG count)1807 mock_C_FindObjectsInit__fails (CK_SESSION_HANDLE session,
1808                                CK_ATTRIBUTE_PTR template,
1809                                CK_ULONG count)
1810 {
1811 	return CKR_DEVICE_MEMORY;
1812 }
1813 
1814 CK_RV
mock_C_FindObjects(CK_SESSION_HANDLE session,CK_OBJECT_HANDLE_PTR objects,CK_ULONG max_object_count,CK_ULONG_PTR object_count)1815 mock_C_FindObjects (CK_SESSION_HANDLE session,
1816                     CK_OBJECT_HANDLE_PTR objects,
1817                     CK_ULONG max_object_count,
1818                     CK_ULONG_PTR object_count)
1819 {
1820 	Session *sess;
1821 
1822 	return_val_if_fail (objects, CKR_ARGUMENTS_BAD);
1823 	return_val_if_fail (object_count, CKR_ARGUMENTS_BAD);
1824 	return_val_if_fail (max_object_count != 0, CKR_ARGUMENTS_BAD);
1825 
1826 	sess = p11_dict_get (the_sessions, handle_to_pointer (session));
1827 	if (sess == NULL)
1828 		return CKR_SESSION_HANDLE_INVALID;
1829 	if (!sess->finding)
1830 		return CKR_OPERATION_NOT_INITIALIZED;
1831 
1832 	*object_count = 0;
1833 	while (max_object_count > 0) {
1834 		if (sess->matches->num == 0)
1835 			break;
1836 		*objects = pointer_to_handle (sess->matches->elem[sess->matches->num - 1]);
1837 		++objects;
1838 		--max_object_count;
1839 		++(*object_count);
1840 		p11_array_remove (sess->matches, sess->matches->num - 1);
1841 	}
1842 
1843 	return CKR_OK;
1844 }
1845 
1846 CK_RV
mock_C_FindObjects__invalid_handle(CK_SESSION_HANDLE session,CK_OBJECT_HANDLE_PTR objects,CK_ULONG max_count,CK_ULONG_PTR count)1847 mock_C_FindObjects__invalid_handle (CK_SESSION_HANDLE session,
1848                                     CK_OBJECT_HANDLE_PTR objects,
1849                                     CK_ULONG max_count,
1850                                     CK_ULONG_PTR count)
1851 {
1852 	return_val_if_fail (count, CKR_ARGUMENTS_BAD);
1853 
1854 	return CKR_SESSION_HANDLE_INVALID;
1855 }
1856 
1857 CK_RV
mock_X_FindObjects__invalid_handle(CK_X_FUNCTION_LIST * self,CK_SESSION_HANDLE session,CK_OBJECT_HANDLE_PTR objects,CK_ULONG max_count,CK_ULONG_PTR count)1858 mock_X_FindObjects__invalid_handle (CK_X_FUNCTION_LIST *self,
1859                                     CK_SESSION_HANDLE session,
1860                                     CK_OBJECT_HANDLE_PTR objects,
1861                                     CK_ULONG max_count,
1862                                     CK_ULONG_PTR count)
1863 {
1864 	return_val_if_fail (count, CKR_ARGUMENTS_BAD);
1865 
1866 	return CKR_SESSION_HANDLE_INVALID;
1867 }
1868 
1869 CK_RV
mock_C_FindObjects__fails(CK_SESSION_HANDLE session,CK_OBJECT_HANDLE_PTR objects,CK_ULONG max_count,CK_ULONG_PTR count)1870 mock_C_FindObjects__fails (CK_SESSION_HANDLE session,
1871                            CK_OBJECT_HANDLE_PTR objects,
1872                            CK_ULONG max_count,
1873                            CK_ULONG_PTR count)
1874 {
1875 	return_val_if_fail (count, CKR_ARGUMENTS_BAD);
1876 
1877 	return CKR_DEVICE_REMOVED;
1878 }
1879 
1880 CK_RV
mock_C_FindObjectsFinal(CK_SESSION_HANDLE session)1881 mock_C_FindObjectsFinal (CK_SESSION_HANDLE session)
1882 {
1883 
1884 	Session *sess;
1885 
1886 	sess = p11_dict_get (the_sessions, handle_to_pointer (session));
1887 	if (sess == NULL)
1888 		return CKR_SESSION_HANDLE_INVALID;
1889 	if (!sess->finding)
1890 		return CKR_OPERATION_NOT_INITIALIZED;
1891 
1892 	sess->finding = false;
1893 	p11_array_free (sess->matches);
1894 	sess->matches = NULL;
1895 
1896 	return CKR_OK;
1897 }
1898 
1899 CK_RV
mock_C_FindObjectsFinal__invalid_handle(CK_SESSION_HANDLE session)1900 mock_C_FindObjectsFinal__invalid_handle (CK_SESSION_HANDLE session)
1901 {
1902 	return CKR_SESSION_HANDLE_INVALID;
1903 }
1904 
1905 CK_RV
mock_X_FindObjectsFinal__invalid_handle(CK_X_FUNCTION_LIST * self,CK_SESSION_HANDLE session)1906 mock_X_FindObjectsFinal__invalid_handle (CK_X_FUNCTION_LIST *self,
1907                                          CK_SESSION_HANDLE session)
1908 {
1909 	return CKR_SESSION_HANDLE_INVALID;
1910 }
1911 
1912 CK_RV
mock_C_EncryptInit(CK_SESSION_HANDLE session,CK_MECHANISM_PTR mechanism,CK_OBJECT_HANDLE key)1913 mock_C_EncryptInit (CK_SESSION_HANDLE session,
1914                     CK_MECHANISM_PTR mechanism,
1915                     CK_OBJECT_HANDLE key)
1916 {
1917 	Session *sess;
1918 
1919 	return_val_if_fail (mechanism != NULL, CKR_ARGUMENTS_BAD);
1920 
1921 	sess = p11_dict_get (the_sessions, handle_to_pointer (session));
1922 	if (!sess)
1923 		return CKR_SESSION_HANDLE_INVALID;
1924 
1925 	/* Starting an operation, cancels any previous one */
1926 	sess->finding = CK_FALSE;
1927 
1928 	if (mechanism->mechanism != CKM_MOCK_CAPITALIZE)
1929 		return CKR_MECHANISM_INVALID;
1930 	if (key != MOCK_PUBLIC_KEY_CAPITALIZE)
1931 		return CKR_KEY_HANDLE_INVALID;
1932 
1933 	sess->crypto_method = CKA_ENCRYPT;
1934 	sess->crypto_mechanism = CKM_MOCK_CAPITALIZE;
1935 	sess->crypto_key = key;
1936 	return CKR_OK;
1937 }
1938 
1939 CK_RV
mock_C_EncryptInit__invalid_handle(CK_SESSION_HANDLE session,CK_MECHANISM_PTR mechanism,CK_OBJECT_HANDLE key)1940 mock_C_EncryptInit__invalid_handle (CK_SESSION_HANDLE session,
1941                                     CK_MECHANISM_PTR mechanism,
1942                                     CK_OBJECT_HANDLE key)
1943 {
1944 	return CKR_SESSION_HANDLE_INVALID;
1945 }
1946 
1947 CK_RV
mock_X_EncryptInit__invalid_handle(CK_X_FUNCTION_LIST * self,CK_SESSION_HANDLE session,CK_MECHANISM_PTR mechanism,CK_OBJECT_HANDLE key)1948 mock_X_EncryptInit__invalid_handle (CK_X_FUNCTION_LIST *self,
1949                                     CK_SESSION_HANDLE session,
1950                                     CK_MECHANISM_PTR mechanism,
1951                                     CK_OBJECT_HANDLE key)
1952 {
1953 	return CKR_SESSION_HANDLE_INVALID;
1954 }
1955 
1956 CK_RV
mock_C_Encrypt(CK_SESSION_HANDLE session,CK_BYTE_PTR data,CK_ULONG data_len,CK_BYTE_PTR encrypted_data,CK_ULONG_PTR encrypted_data_len)1957 mock_C_Encrypt (CK_SESSION_HANDLE session,
1958                 CK_BYTE_PTR data,
1959                 CK_ULONG data_len,
1960                 CK_BYTE_PTR encrypted_data,
1961                 CK_ULONG_PTR encrypted_data_len)
1962 {
1963 	CK_ULONG last = 0;
1964 	CK_RV rv;
1965 	rv = mock_C_EncryptUpdate (session, data, data_len, encrypted_data, encrypted_data_len);
1966 	if (rv == CKR_OK)
1967 		rv = mock_C_EncryptFinal (session, encrypted_data, &last);
1968 	return rv;
1969 }
1970 
1971 CK_RV
mock_C_Encrypt__invalid_handle(CK_SESSION_HANDLE session,CK_BYTE_PTR data,CK_ULONG data_len,CK_BYTE_PTR encrypted_data,CK_ULONG_PTR encrypted_data_len)1972 mock_C_Encrypt__invalid_handle (CK_SESSION_HANDLE session,
1973                                 CK_BYTE_PTR data,
1974                                 CK_ULONG data_len,
1975                                 CK_BYTE_PTR encrypted_data,
1976                                 CK_ULONG_PTR encrypted_data_len)
1977 {
1978 	return_val_if_fail (encrypted_data_len, CKR_ARGUMENTS_BAD);
1979 
1980 	return CKR_SESSION_HANDLE_INVALID;
1981 }
1982 
1983 CK_RV
mock_X_Encrypt__invalid_handle(CK_X_FUNCTION_LIST * self,CK_SESSION_HANDLE session,CK_BYTE_PTR data,CK_ULONG data_len,CK_BYTE_PTR encrypted_data,CK_ULONG_PTR encrypted_data_len)1984 mock_X_Encrypt__invalid_handle (CK_X_FUNCTION_LIST *self,
1985                                 CK_SESSION_HANDLE session,
1986                                 CK_BYTE_PTR data,
1987                                 CK_ULONG data_len,
1988                                 CK_BYTE_PTR encrypted_data,
1989                                 CK_ULONG_PTR encrypted_data_len)
1990 {
1991 	return_val_if_fail (encrypted_data_len, CKR_ARGUMENTS_BAD);
1992 
1993 	return CKR_SESSION_HANDLE_INVALID;
1994 }
1995 
1996 CK_RV
mock_C_EncryptUpdate(CK_SESSION_HANDLE session,CK_BYTE_PTR part,CK_ULONG part_len,CK_BYTE_PTR encrypted_part,CK_ULONG_PTR encrypted_part_len)1997 mock_C_EncryptUpdate (CK_SESSION_HANDLE session,
1998                       CK_BYTE_PTR part,
1999                       CK_ULONG part_len,
2000                       CK_BYTE_PTR encrypted_part,
2001                       CK_ULONG_PTR encrypted_part_len)
2002 {
2003 	Session *sess;
2004 	CK_ULONG i;
2005 
2006 	return_val_if_fail (part != NULL, CKR_DATA_INVALID);
2007 	return_val_if_fail (encrypted_part_len != NULL, CKR_ARGUMENTS_BAD);
2008 
2009 	sess = p11_dict_get (the_sessions, handle_to_pointer (session));
2010 	if (!sess)
2011 		return CKR_SESSION_HANDLE_INVALID;
2012 
2013 	if (!sess->crypto_mechanism)
2014 		return CKR_OPERATION_NOT_INITIALIZED;
2015 	if (sess->crypto_method != CKA_ENCRYPT)
2016 		return CKR_OPERATION_NOT_INITIALIZED;
2017 	assert (sess->crypto_mechanism == CKM_MOCK_CAPITALIZE);
2018 	assert (sess->crypto_key == MOCK_PUBLIC_KEY_CAPITALIZE);
2019 
2020 	if (!encrypted_part) {
2021 		*encrypted_part_len = part_len;
2022 		return CKR_OK;
2023 	}
2024 
2025 	if (*encrypted_part_len < part_len) {
2026 		*encrypted_part_len = part_len;
2027 		return CKR_BUFFER_TOO_SMALL;
2028 	}
2029 
2030 	for (i = 0; i < part_len; ++i)
2031 		encrypted_part[i] = p11_ascii_toupper (part[i]);
2032 	*encrypted_part_len = part_len;
2033 	return CKR_OK;
2034 }
2035 
2036 CK_RV
mock_C_EncryptUpdate__invalid_handle(CK_SESSION_HANDLE session,CK_BYTE_PTR part,CK_ULONG part_len,CK_BYTE_PTR encrypted_part,CK_ULONG_PTR encrypted_part_len)2037 mock_C_EncryptUpdate__invalid_handle (CK_SESSION_HANDLE session,
2038                                       CK_BYTE_PTR part,
2039                                       CK_ULONG part_len,
2040                                       CK_BYTE_PTR encrypted_part,
2041                                       CK_ULONG_PTR encrypted_part_len)
2042 {
2043 	return_val_if_fail (encrypted_part_len, CKR_ARGUMENTS_BAD);
2044 
2045 	return CKR_SESSION_HANDLE_INVALID;
2046 }
2047 
2048 CK_RV
mock_X_EncryptUpdate__invalid_handle(CK_X_FUNCTION_LIST * self,CK_SESSION_HANDLE session,CK_BYTE_PTR part,CK_ULONG part_len,CK_BYTE_PTR encrypted_part,CK_ULONG_PTR encrypted_part_len)2049 mock_X_EncryptUpdate__invalid_handle (CK_X_FUNCTION_LIST *self,
2050                                       CK_SESSION_HANDLE session,
2051                                       CK_BYTE_PTR part,
2052                                       CK_ULONG part_len,
2053                                       CK_BYTE_PTR encrypted_part,
2054                                       CK_ULONG_PTR encrypted_part_len)
2055 {
2056 	return_val_if_fail (encrypted_part_len, CKR_ARGUMENTS_BAD);
2057 
2058 	return CKR_SESSION_HANDLE_INVALID;
2059 }
2060 
2061 CK_RV
mock_C_EncryptFinal(CK_SESSION_HANDLE session,CK_BYTE_PTR last_encrypted_part,CK_ULONG_PTR last_encrypted_part_len)2062 mock_C_EncryptFinal (CK_SESSION_HANDLE session,
2063                      CK_BYTE_PTR last_encrypted_part,
2064                      CK_ULONG_PTR last_encrypted_part_len)
2065 {
2066 	Session *sess;
2067 
2068 	return_val_if_fail (last_encrypted_part_len != NULL, CKR_ARGUMENTS_BAD);
2069 
2070 	sess = p11_dict_get (the_sessions, handle_to_pointer (session));
2071 	if (!sess)
2072 		return CKR_SESSION_HANDLE_INVALID;
2073 
2074 	if (!sess->crypto_mechanism)
2075 		return CKR_OPERATION_NOT_INITIALIZED;
2076 	if (sess->crypto_method != CKA_ENCRYPT)
2077 		return CKR_OPERATION_NOT_INITIALIZED;
2078 
2079 	*last_encrypted_part_len = 0;
2080 
2081 	sess->crypto_method = 0;
2082 	sess->crypto_mechanism = 0;
2083 	sess->crypto_key = 0;
2084 	return CKR_OK;
2085 }
2086 
2087 CK_RV
mock_C_EncryptFinal__invalid_handle(CK_SESSION_HANDLE session,CK_BYTE_PTR last_part,CK_ULONG_PTR last_part_len)2088 mock_C_EncryptFinal__invalid_handle (CK_SESSION_HANDLE session,
2089                                      CK_BYTE_PTR last_part,
2090                                      CK_ULONG_PTR last_part_len)
2091 {
2092 	return_val_if_fail (last_part_len, CKR_ARGUMENTS_BAD);
2093 
2094 	return CKR_SESSION_HANDLE_INVALID;
2095 }
2096 
2097 CK_RV
mock_X_EncryptFinal__invalid_handle(CK_X_FUNCTION_LIST * self,CK_SESSION_HANDLE session,CK_BYTE_PTR last_part,CK_ULONG_PTR last_part_len)2098 mock_X_EncryptFinal__invalid_handle (CK_X_FUNCTION_LIST *self,
2099                                      CK_SESSION_HANDLE session,
2100                                      CK_BYTE_PTR last_part,
2101                                      CK_ULONG_PTR last_part_len)
2102 {
2103 	return_val_if_fail (last_part_len, CKR_ARGUMENTS_BAD);
2104 
2105 	return CKR_SESSION_HANDLE_INVALID;
2106 }
2107 
2108 CK_RV
mock_C_DecryptInit(CK_SESSION_HANDLE session,CK_MECHANISM_PTR mechanism,CK_OBJECT_HANDLE key)2109 mock_C_DecryptInit (CK_SESSION_HANDLE session,
2110                     CK_MECHANISM_PTR mechanism,
2111                     CK_OBJECT_HANDLE key)
2112 {
2113 	Session *sess;
2114 
2115 	return_val_if_fail (mechanism != NULL, CKR_ARGUMENTS_BAD);
2116 
2117 	sess = p11_dict_get (the_sessions, handle_to_pointer (session));
2118 	if (!sess)
2119 		return CKR_SESSION_HANDLE_INVALID;
2120 
2121 	/* Starting an operation, cancels any previous one */
2122 	sess->finding = false;
2123 
2124 	if (mechanism->mechanism != CKM_MOCK_CAPITALIZE)
2125 		return CKR_MECHANISM_INVALID;
2126 	if (key != MOCK_PRIVATE_KEY_CAPITALIZE)
2127 		return CKR_KEY_HANDLE_INVALID;
2128 
2129 	sess->crypto_method = CKA_DECRYPT;
2130 	sess->crypto_mechanism = CKM_MOCK_CAPITALIZE;
2131 	sess->crypto_key = key;
2132 	return CKR_OK;
2133 }
2134 
2135 CK_RV
mock_C_DecryptInit__invalid_handle(CK_SESSION_HANDLE session,CK_MECHANISM_PTR mechanism,CK_OBJECT_HANDLE key)2136 mock_C_DecryptInit__invalid_handle (CK_SESSION_HANDLE session,
2137                                     CK_MECHANISM_PTR mechanism,
2138                                     CK_OBJECT_HANDLE key)
2139 {
2140 	return CKR_SESSION_HANDLE_INVALID;
2141 }
2142 
2143 CK_RV
mock_X_DecryptInit__invalid_handle(CK_X_FUNCTION_LIST * self,CK_SESSION_HANDLE session,CK_MECHANISM_PTR mechanism,CK_OBJECT_HANDLE key)2144 mock_X_DecryptInit__invalid_handle (CK_X_FUNCTION_LIST *self,
2145                                     CK_SESSION_HANDLE session,
2146                                     CK_MECHANISM_PTR mechanism,
2147                                     CK_OBJECT_HANDLE key)
2148 {
2149 	return CKR_SESSION_HANDLE_INVALID;
2150 }
2151 
2152 CK_RV
mock_C_Decrypt(CK_SESSION_HANDLE session,CK_BYTE_PTR encrypted_data,CK_ULONG encrypted_data_len,CK_BYTE_PTR data,CK_ULONG_PTR data_len)2153 mock_C_Decrypt (CK_SESSION_HANDLE session,
2154                 CK_BYTE_PTR encrypted_data,
2155                 CK_ULONG encrypted_data_len,
2156                 CK_BYTE_PTR data,
2157                 CK_ULONG_PTR data_len)
2158 {
2159 	CK_ULONG last = 0;
2160 	CK_RV rv;
2161 	rv = mock_C_DecryptUpdate (session, encrypted_data, encrypted_data_len, data, data_len);
2162 	if (rv == CKR_OK)
2163 		rv = mock_C_DecryptFinal (session, data, &last);
2164 	return rv;
2165 }
2166 
2167 CK_RV
mock_C_Decrypt__invalid_handle(CK_SESSION_HANDLE session,CK_BYTE_PTR enc_data,CK_ULONG enc_data_len,CK_BYTE_PTR data,CK_ULONG_PTR data_len)2168 mock_C_Decrypt__invalid_handle (CK_SESSION_HANDLE session,
2169                                 CK_BYTE_PTR enc_data,
2170                                 CK_ULONG enc_data_len,
2171                                 CK_BYTE_PTR data,
2172                                 CK_ULONG_PTR data_len)
2173 {
2174 	return_val_if_fail (data_len, CKR_ARGUMENTS_BAD);
2175 
2176 	return CKR_SESSION_HANDLE_INVALID;
2177 }
2178 
2179 CK_RV
mock_X_Decrypt__invalid_handle(CK_X_FUNCTION_LIST * self,CK_SESSION_HANDLE session,CK_BYTE_PTR enc_data,CK_ULONG enc_data_len,CK_BYTE_PTR data,CK_ULONG_PTR data_len)2180 mock_X_Decrypt__invalid_handle (CK_X_FUNCTION_LIST *self,
2181                                 CK_SESSION_HANDLE session,
2182                                 CK_BYTE_PTR enc_data,
2183                                 CK_ULONG enc_data_len,
2184                                 CK_BYTE_PTR data,
2185                                 CK_ULONG_PTR data_len)
2186 {
2187 	return_val_if_fail (data_len, CKR_ARGUMENTS_BAD);
2188 
2189 	return CKR_SESSION_HANDLE_INVALID;
2190 }
2191 
2192 CK_RV
mock_C_DecryptUpdate(CK_SESSION_HANDLE session,CK_BYTE_PTR encrypted_part,CK_ULONG encrypted_part_len,CK_BYTE_PTR part,CK_ULONG_PTR part_len)2193 mock_C_DecryptUpdate (CK_SESSION_HANDLE session,
2194                       CK_BYTE_PTR encrypted_part,
2195                       CK_ULONG encrypted_part_len,
2196                       CK_BYTE_PTR part,
2197                       CK_ULONG_PTR part_len)
2198 {
2199 	Session *sess;
2200 	CK_ULONG i;
2201 
2202 	return_val_if_fail (encrypted_part, CKR_ENCRYPTED_DATA_INVALID);
2203 	return_val_if_fail (part_len, CKR_ARGUMENTS_BAD);
2204 
2205 	sess = p11_dict_get (the_sessions, handle_to_pointer (session));
2206 	if (!sess)
2207 		return CKR_SESSION_HANDLE_INVALID;
2208 
2209 	if (!sess->crypto_mechanism)
2210 		return CKR_OPERATION_NOT_INITIALIZED;
2211 	if (sess->crypto_method != CKA_DECRYPT)
2212 		return CKR_OPERATION_NOT_INITIALIZED;
2213 	assert (sess->crypto_mechanism == CKM_MOCK_CAPITALIZE);
2214 	assert (sess->crypto_key == MOCK_PRIVATE_KEY_CAPITALIZE);
2215 
2216 	if (!part) {
2217 		*part_len = encrypted_part_len;
2218 		return CKR_OK;
2219 	}
2220 
2221 	if (*part_len < encrypted_part_len) {
2222 		*part_len = encrypted_part_len;
2223 		return CKR_BUFFER_TOO_SMALL;
2224 	}
2225 
2226 	for (i = 0; i < encrypted_part_len; ++i)
2227 		part[i] = p11_ascii_tolower (encrypted_part[i]);
2228 	*part_len = encrypted_part_len;
2229 	return CKR_OK;
2230 }
2231 
2232 CK_RV
mock_C_DecryptUpdate__invalid_handle(CK_SESSION_HANDLE session,CK_BYTE_PTR enc_part,CK_ULONG enc_part_len,CK_BYTE_PTR part,CK_ULONG_PTR part_len)2233 mock_C_DecryptUpdate__invalid_handle (CK_SESSION_HANDLE session,
2234                                       CK_BYTE_PTR enc_part,
2235                                       CK_ULONG enc_part_len,
2236                                       CK_BYTE_PTR part,
2237                                       CK_ULONG_PTR part_len)
2238 {
2239 	return_val_if_fail (part_len, CKR_ARGUMENTS_BAD);
2240 
2241 	return CKR_SESSION_HANDLE_INVALID;
2242 }
2243 
2244 CK_RV
mock_X_DecryptUpdate__invalid_handle(CK_X_FUNCTION_LIST * self,CK_SESSION_HANDLE session,CK_BYTE_PTR enc_part,CK_ULONG enc_part_len,CK_BYTE_PTR part,CK_ULONG_PTR part_len)2245 mock_X_DecryptUpdate__invalid_handle (CK_X_FUNCTION_LIST *self,
2246                                       CK_SESSION_HANDLE session,
2247                                       CK_BYTE_PTR enc_part,
2248                                       CK_ULONG enc_part_len,
2249                                       CK_BYTE_PTR part,
2250                                       CK_ULONG_PTR part_len)
2251 {
2252 	return_val_if_fail (part_len, CKR_ARGUMENTS_BAD);
2253 
2254 	return CKR_SESSION_HANDLE_INVALID;
2255 }
2256 
2257 CK_RV
mock_C_DecryptFinal(CK_SESSION_HANDLE session,CK_BYTE_PTR last_part,CK_ULONG_PTR last_part_len)2258 mock_C_DecryptFinal (CK_SESSION_HANDLE session,
2259                      CK_BYTE_PTR last_part,
2260                      CK_ULONG_PTR last_part_len)
2261 {
2262 	Session *sess;
2263 
2264 	return_val_if_fail (last_part_len != NULL, CKR_ARGUMENTS_BAD);
2265 
2266 	sess = p11_dict_get (the_sessions, handle_to_pointer (session));
2267 	if (!sess)
2268 		return CKR_SESSION_HANDLE_INVALID;
2269 
2270 	if (!sess->crypto_mechanism)
2271 		return CKR_OPERATION_NOT_INITIALIZED;
2272 	if (sess->crypto_method != CKA_DECRYPT)
2273 		return CKR_OPERATION_NOT_INITIALIZED;
2274 
2275 	*last_part_len = 0;
2276 
2277 	sess->crypto_method = 0;
2278 	sess->crypto_mechanism = 0;
2279 	sess->crypto_key = 0;
2280 
2281 	return CKR_OK;
2282 }
2283 
2284 CK_RV
mock_C_DecryptFinal__invalid_handle(CK_SESSION_HANDLE session,CK_BYTE_PTR last_part,CK_ULONG_PTR last_part_len)2285 mock_C_DecryptFinal__invalid_handle (CK_SESSION_HANDLE session,
2286                                      CK_BYTE_PTR last_part,
2287                                      CK_ULONG_PTR last_part_len)
2288 {
2289 	return_val_if_fail (last_part_len, CKR_ARGUMENTS_BAD);
2290 
2291 	return CKR_SESSION_HANDLE_INVALID;
2292 }
2293 
2294 CK_RV
mock_X_DecryptFinal__invalid_handle(CK_X_FUNCTION_LIST * self,CK_SESSION_HANDLE session,CK_BYTE_PTR last_part,CK_ULONG_PTR last_part_len)2295 mock_X_DecryptFinal__invalid_handle (CK_X_FUNCTION_LIST *self,
2296                                      CK_SESSION_HANDLE session,
2297                                      CK_BYTE_PTR last_part,
2298                                      CK_ULONG_PTR last_part_len)
2299 {
2300 	return_val_if_fail (last_part_len, CKR_ARGUMENTS_BAD);
2301 
2302 	return CKR_SESSION_HANDLE_INVALID;
2303 }
2304 
2305 CK_RV
mock_C_DigestInit(CK_SESSION_HANDLE session,CK_MECHANISM_PTR mechanism)2306 mock_C_DigestInit (CK_SESSION_HANDLE session,
2307                    CK_MECHANISM_PTR mechanism)
2308 {
2309 	Session *sess;
2310 
2311 	return_val_if_fail (mechanism != NULL, CKR_ARGUMENTS_BAD);
2312 
2313 	sess = p11_dict_get (the_sessions, handle_to_pointer (session));
2314 	if (!sess)
2315 		return CKR_SESSION_HANDLE_INVALID;
2316 
2317 	/* Starting an operation, cancels any previous one */
2318 	sess->finding = false;
2319 
2320 	if (mechanism->mechanism != CKM_MOCK_COUNT)
2321 		return CKR_MECHANISM_INVALID;
2322 
2323 	sess->hash_mechanism = CKM_MOCK_COUNT;
2324 	sess->hash_method = (CK_ULONG)-1;
2325 	sess->hash_count = 0;
2326 	sess->hash_key = 0;
2327 	return CKR_OK;
2328 }
2329 
2330 CK_RV
mock_C_DigestInit__invalid_handle(CK_SESSION_HANDLE session,CK_MECHANISM_PTR mechanism)2331 mock_C_DigestInit__invalid_handle (CK_SESSION_HANDLE session,
2332                                    CK_MECHANISM_PTR mechanism)
2333 {
2334 	return CKR_SESSION_HANDLE_INVALID;
2335 }
2336 
2337 CK_RV
mock_X_DigestInit__invalid_handle(CK_X_FUNCTION_LIST * self,CK_SESSION_HANDLE session,CK_MECHANISM_PTR mechanism)2338 mock_X_DigestInit__invalid_handle (CK_X_FUNCTION_LIST *self,
2339                                    CK_SESSION_HANDLE session,
2340                                    CK_MECHANISM_PTR mechanism)
2341 {
2342 	return CKR_SESSION_HANDLE_INVALID;
2343 }
2344 
2345 CK_RV
mock_C_Digest(CK_SESSION_HANDLE session,CK_BYTE_PTR data,CK_ULONG data_len,CK_BYTE_PTR digest,CK_ULONG_PTR digest_len)2346 mock_C_Digest (CK_SESSION_HANDLE session,
2347                CK_BYTE_PTR data,
2348                CK_ULONG data_len,
2349                CK_BYTE_PTR digest,
2350                CK_ULONG_PTR digest_len)
2351 {
2352 	CK_RV rv;
2353 
2354 	return_val_if_fail (digest_len, CKR_ARGUMENTS_BAD);
2355 
2356 	rv = mock_C_DigestUpdate (session, data, data_len);
2357 	if (rv == CKR_OK)
2358 		rv = mock_C_DigestFinal (session, digest, digest_len);
2359 	return rv;
2360 }
2361 
2362 CK_RV
mock_C_Digest__invalid_handle(CK_SESSION_HANDLE session,CK_BYTE_PTR data,CK_ULONG data_len,CK_BYTE_PTR digest,CK_ULONG_PTR digest_len)2363 mock_C_Digest__invalid_handle (CK_SESSION_HANDLE session,
2364                                CK_BYTE_PTR data,
2365                                CK_ULONG data_len,
2366                                CK_BYTE_PTR digest,
2367                                CK_ULONG_PTR digest_len)
2368 {
2369 	return_val_if_fail (digest_len, CKR_ARGUMENTS_BAD);
2370 
2371 	return CKR_SESSION_HANDLE_INVALID;
2372 }
2373 
2374 CK_RV
mock_X_Digest__invalid_handle(CK_X_FUNCTION_LIST * self,CK_SESSION_HANDLE session,CK_BYTE_PTR data,CK_ULONG data_len,CK_BYTE_PTR digest,CK_ULONG_PTR digest_len)2375 mock_X_Digest__invalid_handle (CK_X_FUNCTION_LIST *self,
2376                                CK_SESSION_HANDLE session,
2377                                CK_BYTE_PTR data,
2378                                CK_ULONG data_len,
2379                                CK_BYTE_PTR digest,
2380                                CK_ULONG_PTR digest_len)
2381 {
2382 	return_val_if_fail (digest_len, CKR_ARGUMENTS_BAD);
2383 
2384 	return CKR_SESSION_HANDLE_INVALID;
2385 }
2386 
2387 CK_RV
mock_C_DigestUpdate(CK_SESSION_HANDLE session,CK_BYTE_PTR part,CK_ULONG part_len)2388 mock_C_DigestUpdate (CK_SESSION_HANDLE session,
2389                      CK_BYTE_PTR part,
2390                      CK_ULONG part_len)
2391 {
2392 	Session *sess;
2393 
2394 	sess = p11_dict_get (the_sessions, handle_to_pointer (session));
2395 	if (!sess)
2396 		return CKR_SESSION_HANDLE_INVALID;
2397 
2398 	if (!sess->hash_mechanism)
2399 		return CKR_OPERATION_NOT_INITIALIZED;
2400 	if (sess->hash_method != (CK_ULONG)-1)
2401 		return CKR_OPERATION_NOT_INITIALIZED;
2402 	assert (sess->hash_mechanism == CKM_MOCK_COUNT);
2403 
2404 	sess->hash_count += part_len;
2405 	return CKR_OK;
2406 }
2407 
2408 CK_RV
mock_C_DigestUpdate__invalid_handle(CK_SESSION_HANDLE session,CK_BYTE_PTR part,CK_ULONG part_len)2409 mock_C_DigestUpdate__invalid_handle (CK_SESSION_HANDLE session,
2410                                      CK_BYTE_PTR part,
2411                                      CK_ULONG part_len)
2412 {
2413 	return CKR_SESSION_HANDLE_INVALID;
2414 }
2415 
2416 CK_RV
mock_X_DigestUpdate__invalid_handle(CK_X_FUNCTION_LIST * self,CK_SESSION_HANDLE session,CK_BYTE_PTR part,CK_ULONG part_len)2417 mock_X_DigestUpdate__invalid_handle (CK_X_FUNCTION_LIST *self,
2418                                      CK_SESSION_HANDLE session,
2419                                      CK_BYTE_PTR part,
2420                                      CK_ULONG part_len)
2421 {
2422 	return CKR_SESSION_HANDLE_INVALID;
2423 }
2424 
2425 CK_RV
mock_C_DigestKey(CK_SESSION_HANDLE session,CK_OBJECT_HANDLE key)2426 mock_C_DigestKey (CK_SESSION_HANDLE session,
2427                   CK_OBJECT_HANDLE key)
2428 {
2429 	Session *sess;
2430 
2431 	sess = p11_dict_get (the_sessions, handle_to_pointer (session));
2432 	if (!sess)
2433 		return CKR_SESSION_HANDLE_INVALID;
2434 
2435 	if (!sess->hash_mechanism)
2436 		return CKR_OPERATION_NOT_INITIALIZED;
2437 	if (sess->hash_method != (CK_ULONG)-1)
2438 		return CKR_OPERATION_NOT_INITIALIZED;
2439 	assert (sess->hash_mechanism == CKM_MOCK_COUNT);
2440 
2441 	sess->hash_count += key;
2442 	return CKR_OK;
2443 }
2444 
2445 CK_RV
mock_C_DigestKey__invalid_handle(CK_SESSION_HANDLE session,CK_OBJECT_HANDLE key)2446 mock_C_DigestKey__invalid_handle (CK_SESSION_HANDLE session,
2447                                   CK_OBJECT_HANDLE key)
2448 {
2449 	return CKR_SESSION_HANDLE_INVALID;
2450 }
2451 
2452 CK_RV
mock_X_DigestKey__invalid_handle(CK_X_FUNCTION_LIST * self,CK_SESSION_HANDLE session,CK_OBJECT_HANDLE key)2453 mock_X_DigestKey__invalid_handle (CK_X_FUNCTION_LIST *self,
2454                                   CK_SESSION_HANDLE session,
2455                                   CK_OBJECT_HANDLE key)
2456 {
2457 	return CKR_SESSION_HANDLE_INVALID;
2458 }
2459 
2460 CK_RV
mock_C_DigestFinal(CK_SESSION_HANDLE session,CK_BYTE_PTR digest,CK_ULONG_PTR digest_len)2461 mock_C_DigestFinal (CK_SESSION_HANDLE session,
2462                     CK_BYTE_PTR digest,
2463                     CK_ULONG_PTR digest_len)
2464 {
2465 	char buffer[32];
2466 	Session *sess;
2467 	int len;
2468 
2469 	return_val_if_fail (digest_len != NULL, CKR_ARGUMENTS_BAD);
2470 
2471 	sess = p11_dict_get (the_sessions, handle_to_pointer (session));
2472 	if (!sess)
2473 		return CKR_SESSION_HANDLE_INVALID;
2474 
2475 	if (!sess->hash_mechanism)
2476 		return CKR_OPERATION_NOT_INITIALIZED;
2477 	if (sess->hash_method != (CK_ULONG)-1)
2478 		return CKR_OPERATION_NOT_INITIALIZED;
2479 	assert (sess->hash_mechanism == CKM_MOCK_COUNT);
2480 
2481 	len = snprintf (buffer, sizeof (buffer), "%lu", sess->hash_count);
2482 
2483 	if (!digest) {
2484 		*digest_len = len;
2485 		return CKR_OK;
2486 	} else if (*digest_len < len) {
2487 		*digest_len = len;
2488 		return CKR_BUFFER_TOO_SMALL;
2489 	}
2490 
2491 	memcpy (digest, &buffer, len);
2492 	*digest_len = len;
2493 
2494 	sess->hash_count = 0;
2495 	sess->hash_mechanism = 0;
2496 	sess->hash_key = 0;
2497 	sess->hash_method = 0;
2498 
2499 	return CKR_OK;
2500 }
2501 
2502 CK_RV
mock_C_DigestFinal__invalid_handle(CK_SESSION_HANDLE session,CK_BYTE_PTR digest,CK_ULONG_PTR digest_len)2503 mock_C_DigestFinal__invalid_handle (CK_SESSION_HANDLE session,
2504                                     CK_BYTE_PTR digest,
2505                                     CK_ULONG_PTR digest_len)
2506 {
2507 	return_val_if_fail (digest_len, CKR_ARGUMENTS_BAD);
2508 
2509 	return CKR_SESSION_HANDLE_INVALID;
2510 }
2511 
2512 CK_RV
mock_X_DigestFinal__invalid_handle(CK_X_FUNCTION_LIST * self,CK_SESSION_HANDLE session,CK_BYTE_PTR digest,CK_ULONG_PTR digest_len)2513 mock_X_DigestFinal__invalid_handle (CK_X_FUNCTION_LIST *self,
2514                                     CK_SESSION_HANDLE session,
2515                                     CK_BYTE_PTR digest,
2516                                     CK_ULONG_PTR digest_len)
2517 {
2518 	return_val_if_fail (digest_len, CKR_ARGUMENTS_BAD);
2519 
2520 	return CKR_SESSION_HANDLE_INVALID;
2521 }
2522 
2523 static CK_RV
prefix_mechanism_init(CK_SESSION_HANDLE session,CK_ATTRIBUTE_TYPE method,CK_MECHANISM_PTR mechanism,CK_OBJECT_HANDLE key)2524 prefix_mechanism_init (CK_SESSION_HANDLE session,
2525                        CK_ATTRIBUTE_TYPE method,
2526                        CK_MECHANISM_PTR mechanism,
2527                        CK_OBJECT_HANDLE key)
2528 {
2529 	Session *sess;
2530 	CK_ATTRIBUTE *attrs;
2531 	CK_ATTRIBUTE *value;
2532 	CK_BYTE_PTR param;
2533 	CK_ULONG n_param;
2534 	CK_ULONG length;
2535 	CK_RV rv;
2536 
2537 	sess = p11_dict_get (the_sessions, handle_to_pointer (session));
2538 	if (!sess)
2539 		return CKR_SESSION_HANDLE_INVALID;
2540 
2541 	if (mechanism->mechanism != CKM_MOCK_PREFIX)
2542 		return CKR_MECHANISM_INVALID;
2543 	if (method == CKA_SIGN || method == CKA_SIGN_RECOVER) {
2544 		if (key != MOCK_PRIVATE_KEY_PREFIX)
2545 			return CKR_KEY_HANDLE_INVALID;
2546 	} else if (method == CKA_VERIFY || method == CKA_VERIFY_RECOVER) {
2547 		if (key != MOCK_PUBLIC_KEY_PREFIX)
2548 			return CKR_KEY_HANDLE_INVALID;
2549 	} else {
2550 		assert_not_reached ();
2551 	}
2552 
2553 	rv = lookup_object (sess, key, &attrs, NULL);
2554 	if (rv != CKR_OK)
2555 		return rv;
2556 
2557 	value = p11_attrs_find_valid (attrs, CKA_VALUE);
2558 	if (value == NULL)
2559 		return CKR_KEY_TYPE_INCONSISTENT;
2560 
2561 	if (mechanism->pParameter) {
2562 		param = mechanism->pParameter;
2563 		n_param = mechanism->ulParameterLen;
2564 	} else {
2565 		param = (CK_BYTE_PTR)SIGNED_PREFIX;
2566 		n_param = strlen (SIGNED_PREFIX) + 1;
2567 	}
2568 
2569 	length = value->ulValueLen + n_param;
2570 	if (length > sizeof (sess->sign_prefix))
2571 		return CKR_KEY_SIZE_RANGE;
2572 
2573 	/* Starting an operation, cancels any finding */
2574 	sess->finding = false;
2575 
2576 	sess->hash_mechanism = CKM_MOCK_PREFIX;
2577 	sess->hash_method = method;
2578 	sess->hash_key = key;
2579 	sess->hash_count = 0;
2580 
2581 	memcpy (sess->sign_prefix, param, n_param);
2582 	memcpy (sess->sign_prefix + n_param, value->pValue, value->ulValueLen);
2583 	sess->n_sign_prefix = length;
2584 
2585 	/* The private key has CKA_ALWAYS_AUTHENTICATE above */
2586 	if (method == CKA_SIGN || method == CKA_SIGN_RECOVER)
2587 		sess->want_context_login = true;
2588 
2589 	return CKR_OK;
2590 
2591 }
2592 
2593 CK_RV
mock_C_SignInit(CK_SESSION_HANDLE session,CK_MECHANISM_PTR mechanism,CK_OBJECT_HANDLE key)2594 mock_C_SignInit (CK_SESSION_HANDLE session,
2595                  CK_MECHANISM_PTR mechanism,
2596                  CK_OBJECT_HANDLE key)
2597 {
2598 	return_val_if_fail (mechanism != NULL, CKR_ARGUMENTS_BAD);
2599 	return prefix_mechanism_init (session, CKA_SIGN, mechanism, key);
2600 }
2601 
2602 CK_RV
mock_C_SignInit__invalid_handle(CK_SESSION_HANDLE session,CK_MECHANISM_PTR mechanism,CK_OBJECT_HANDLE key)2603 mock_C_SignInit__invalid_handle (CK_SESSION_HANDLE session,
2604                                  CK_MECHANISM_PTR mechanism,
2605                                  CK_OBJECT_HANDLE key)
2606 {
2607 	return CKR_SESSION_HANDLE_INVALID;
2608 }
2609 
2610 CK_RV
mock_X_SignInit__invalid_handle(CK_X_FUNCTION_LIST * self,CK_SESSION_HANDLE session,CK_MECHANISM_PTR mechanism,CK_OBJECT_HANDLE key)2611 mock_X_SignInit__invalid_handle (CK_X_FUNCTION_LIST *self,
2612                                  CK_SESSION_HANDLE session,
2613                                  CK_MECHANISM_PTR mechanism,
2614                                  CK_OBJECT_HANDLE key)
2615 {
2616 	return CKR_SESSION_HANDLE_INVALID;
2617 }
2618 
2619 CK_RV
mock_C_Sign(CK_SESSION_HANDLE session,CK_BYTE_PTR data,CK_ULONG data_len,CK_BYTE_PTR signature,CK_ULONG_PTR signature_len)2620 mock_C_Sign (CK_SESSION_HANDLE session,
2621              CK_BYTE_PTR data,
2622              CK_ULONG data_len,
2623              CK_BYTE_PTR signature,
2624              CK_ULONG_PTR signature_len)
2625 {
2626 	CK_RV rv;
2627 
2628 	rv = mock_C_SignUpdate (session, data, data_len);
2629 	if (rv == CKR_OK)
2630 		rv = mock_C_SignFinal (session, signature, signature_len);
2631 
2632 	return rv;
2633 }
2634 
2635 CK_RV
mock_C_Sign__invalid_handle(CK_SESSION_HANDLE session,CK_BYTE_PTR data,CK_ULONG data_len,CK_BYTE_PTR signature,CK_ULONG_PTR signature_len)2636 mock_C_Sign__invalid_handle (CK_SESSION_HANDLE session,
2637                              CK_BYTE_PTR data,
2638                              CK_ULONG data_len,
2639                              CK_BYTE_PTR signature,
2640                              CK_ULONG_PTR signature_len)
2641 {
2642 	return_val_if_fail (signature_len, CKR_ARGUMENTS_BAD);
2643 
2644 	return CKR_SESSION_HANDLE_INVALID;
2645 }
2646 
2647 CK_RV
mock_X_Sign__invalid_handle(CK_X_FUNCTION_LIST * self,CK_SESSION_HANDLE session,CK_BYTE_PTR data,CK_ULONG data_len,CK_BYTE_PTR signature,CK_ULONG_PTR signature_len)2648 mock_X_Sign__invalid_handle (CK_X_FUNCTION_LIST *self,
2649                              CK_SESSION_HANDLE session,
2650                              CK_BYTE_PTR data,
2651                              CK_ULONG data_len,
2652                              CK_BYTE_PTR signature,
2653                              CK_ULONG_PTR signature_len)
2654 {
2655 	return_val_if_fail (signature_len, CKR_ARGUMENTS_BAD);
2656 
2657 	return CKR_SESSION_HANDLE_INVALID;
2658 }
2659 
2660 CK_RV
mock_C_SignUpdate(CK_SESSION_HANDLE session,CK_BYTE_PTR part,CK_ULONG part_len)2661 mock_C_SignUpdate (CK_SESSION_HANDLE session,
2662                    CK_BYTE_PTR part,
2663                    CK_ULONG part_len)
2664 {
2665 	Session *sess;
2666 
2667 	sess = p11_dict_get (the_sessions, handle_to_pointer (session));
2668 	if (!sess)
2669 		return CKR_SESSION_HANDLE_INVALID;
2670 	if (sess->hash_mechanism != CKM_MOCK_PREFIX ||
2671 	    sess->hash_method != CKA_SIGN)
2672 		return CKR_OPERATION_NOT_INITIALIZED;
2673 	if (sess->want_context_login)
2674 		return CKR_USER_NOT_LOGGED_IN;
2675 
2676 	sess->hash_count += part_len;
2677 	return CKR_OK;
2678 }
2679 
2680 CK_RV
mock_C_SignUpdate__invalid_handle(CK_SESSION_HANDLE session,CK_BYTE_PTR part,CK_ULONG part_len)2681 mock_C_SignUpdate__invalid_handle (CK_SESSION_HANDLE session,
2682                                    CK_BYTE_PTR part,
2683                                    CK_ULONG part_len)
2684 {
2685 	return_val_if_fail (part_len, CKR_ARGUMENTS_BAD);
2686 
2687 	return CKR_SESSION_HANDLE_INVALID;
2688 }
2689 
2690 CK_RV
mock_X_SignUpdate__invalid_handle(CK_X_FUNCTION_LIST * self,CK_SESSION_HANDLE session,CK_BYTE_PTR part,CK_ULONG part_len)2691 mock_X_SignUpdate__invalid_handle (CK_X_FUNCTION_LIST *self,
2692                                    CK_SESSION_HANDLE session,
2693                                    CK_BYTE_PTR part,
2694                                    CK_ULONG part_len)
2695 {
2696 	return_val_if_fail (part_len, CKR_ARGUMENTS_BAD);
2697 
2698 	return CKR_SESSION_HANDLE_INVALID;
2699 }
2700 
2701 CK_RV
mock_C_SignFinal(CK_SESSION_HANDLE session,CK_BYTE_PTR signature,CK_ULONG_PTR signature_len)2702 mock_C_SignFinal (CK_SESSION_HANDLE session,
2703                   CK_BYTE_PTR signature,
2704                   CK_ULONG_PTR signature_len)
2705 {
2706 	char buffer[32];
2707 	Session *sess;
2708 	CK_ULONG length;
2709 	int len;
2710 
2711 	return_val_if_fail (signature_len, CKR_ARGUMENTS_BAD);
2712 
2713 	sess = p11_dict_get (the_sessions, handle_to_pointer (session));
2714 	if (!sess)
2715 		return CKR_SESSION_HANDLE_INVALID;
2716 	if (sess->hash_mechanism != CKM_MOCK_PREFIX ||
2717 	    sess->hash_method != CKA_SIGN)
2718 		return CKR_OPERATION_NOT_INITIALIZED;
2719 	if (sess->want_context_login)
2720 		return CKR_USER_NOT_LOGGED_IN;
2721 
2722 	len = snprintf (buffer, sizeof (buffer), "%lu", sess->hash_count);
2723 	length = sess->n_sign_prefix + len;
2724 
2725 	if (!signature) {
2726 		*signature_len = length;
2727 		return CKR_OK;
2728 	}
2729 
2730 	if (*signature_len < length) {
2731 		*signature_len = length;
2732 		return CKR_BUFFER_TOO_SMALL;
2733 	}
2734 
2735 	memcpy (signature, sess->sign_prefix, sess->n_sign_prefix);
2736 	memcpy (signature + sess->n_sign_prefix, buffer, len);
2737 	*signature_len = length;
2738 
2739 	sess->hash_mechanism = 0;
2740 	sess->hash_method = 0;
2741 	sess->hash_count = 0;
2742 	sess->hash_key = 0;
2743 
2744 	return CKR_OK;
2745 }
2746 
2747 CK_RV
mock_C_SignFinal__invalid_handle(CK_SESSION_HANDLE session,CK_BYTE_PTR signature,CK_ULONG_PTR signature_len)2748 mock_C_SignFinal__invalid_handle (CK_SESSION_HANDLE session,
2749                                   CK_BYTE_PTR signature,
2750                                   CK_ULONG_PTR signature_len)
2751 {
2752 	return_val_if_fail (signature_len, CKR_ARGUMENTS_BAD);
2753 
2754 	return CKR_SESSION_HANDLE_INVALID;
2755 }
2756 
2757 CK_RV
mock_X_SignFinal__invalid_handle(CK_X_FUNCTION_LIST * self,CK_SESSION_HANDLE session,CK_BYTE_PTR signature,CK_ULONG_PTR signature_len)2758 mock_X_SignFinal__invalid_handle (CK_X_FUNCTION_LIST *self,
2759                                   CK_SESSION_HANDLE session,
2760                                   CK_BYTE_PTR signature,
2761                                   CK_ULONG_PTR signature_len)
2762 {
2763 	return_val_if_fail (signature_len, CKR_ARGUMENTS_BAD);
2764 
2765 	return CKR_SESSION_HANDLE_INVALID;
2766 }
2767 
2768 CK_RV
mock_C_SignRecoverInit(CK_SESSION_HANDLE session,CK_MECHANISM_PTR mechanism,CK_OBJECT_HANDLE key)2769 mock_C_SignRecoverInit (CK_SESSION_HANDLE session,
2770                         CK_MECHANISM_PTR mechanism,
2771                         CK_OBJECT_HANDLE key)
2772 {
2773 	return_val_if_fail (mechanism != NULL, CKR_ARGUMENTS_BAD);
2774 	return prefix_mechanism_init (session, CKA_SIGN_RECOVER, mechanism, key);
2775 }
2776 
2777 CK_RV
mock_C_SignRecoverInit__invalid_handle(CK_SESSION_HANDLE session,CK_MECHANISM_PTR mechanism,CK_OBJECT_HANDLE key)2778 mock_C_SignRecoverInit__invalid_handle (CK_SESSION_HANDLE session,
2779                                         CK_MECHANISM_PTR mechanism,
2780                                         CK_OBJECT_HANDLE key)
2781 {
2782 	return CKR_SESSION_HANDLE_INVALID;
2783 }
2784 
2785 CK_RV
mock_X_SignRecoverInit__invalid_handle(CK_X_FUNCTION_LIST * self,CK_SESSION_HANDLE session,CK_MECHANISM_PTR mechanism,CK_OBJECT_HANDLE key)2786 mock_X_SignRecoverInit__invalid_handle (CK_X_FUNCTION_LIST *self,
2787                                         CK_SESSION_HANDLE session,
2788                                         CK_MECHANISM_PTR mechanism,
2789                                         CK_OBJECT_HANDLE key)
2790 {
2791 	return CKR_SESSION_HANDLE_INVALID;
2792 }
2793 
2794 CK_RV
mock_C_SignRecover(CK_SESSION_HANDLE session,CK_BYTE_PTR data,CK_ULONG data_len,CK_BYTE_PTR signature,CK_ULONG_PTR signature_len)2795 mock_C_SignRecover (CK_SESSION_HANDLE session,
2796                     CK_BYTE_PTR data,
2797                     CK_ULONG data_len,
2798                     CK_BYTE_PTR signature,
2799                     CK_ULONG_PTR signature_len)
2800 {
2801 	Session *sess;
2802 	CK_ULONG length;
2803 
2804 	return_val_if_fail (data, CKR_DATA_INVALID);
2805 	return_val_if_fail (signature_len, CKR_ARGUMENTS_BAD);
2806 
2807 	sess = p11_dict_get (the_sessions, handle_to_pointer (session));
2808 	if (!sess)
2809 		return CKR_SESSION_HANDLE_INVALID;
2810 	if (sess->hash_method != CKA_SIGN_RECOVER ||
2811 	    sess->hash_mechanism != CKM_MOCK_PREFIX)
2812 		return CKR_OPERATION_NOT_INITIALIZED;
2813 	if (sess->want_context_login)
2814 		return CKR_USER_NOT_LOGGED_IN;
2815 
2816 	length = sess->n_sign_prefix + data_len;
2817 
2818 	if (!signature) {
2819 		*signature_len = length;
2820 		return CKR_OK;
2821 	}
2822 
2823 	if (*signature_len < length) {
2824 		*signature_len = length;
2825 		return CKR_BUFFER_TOO_SMALL;
2826 	}
2827 
2828 	memcpy (signature, sess->sign_prefix, sess->n_sign_prefix);
2829 	memcpy (signature + sess->n_sign_prefix, data, data_len);
2830 	*signature_len = length;
2831 
2832 	sess->hash_method = 0;
2833 	sess->hash_mechanism = 0;
2834 	sess->hash_key = 0;
2835 	sess->hash_count = 0;
2836 
2837 	return CKR_OK;
2838 }
2839 
2840 CK_RV
mock_C_SignRecover__invalid_handle(CK_SESSION_HANDLE session,CK_BYTE_PTR data,CK_ULONG data_len,CK_BYTE_PTR signature,CK_ULONG_PTR signature_len)2841 mock_C_SignRecover__invalid_handle (CK_SESSION_HANDLE session,
2842                                     CK_BYTE_PTR data,
2843                                     CK_ULONG data_len,
2844                                     CK_BYTE_PTR signature,
2845                                     CK_ULONG_PTR signature_len)
2846 {
2847 	return_val_if_fail (signature_len, CKR_ARGUMENTS_BAD);
2848 
2849 	return CKR_SESSION_HANDLE_INVALID;
2850 }
2851 
2852 CK_RV
mock_X_SignRecover__invalid_handle(CK_X_FUNCTION_LIST * self,CK_SESSION_HANDLE session,CK_BYTE_PTR data,CK_ULONG data_len,CK_BYTE_PTR signature,CK_ULONG_PTR signature_len)2853 mock_X_SignRecover__invalid_handle (CK_X_FUNCTION_LIST *self,
2854                                     CK_SESSION_HANDLE session,
2855                                     CK_BYTE_PTR data,
2856                                     CK_ULONG data_len,
2857                                     CK_BYTE_PTR signature,
2858                                     CK_ULONG_PTR signature_len)
2859 {
2860 	return_val_if_fail (signature_len, CKR_ARGUMENTS_BAD);
2861 
2862 	return CKR_SESSION_HANDLE_INVALID;
2863 }
2864 
2865 CK_RV
mock_C_VerifyInit(CK_SESSION_HANDLE session,CK_MECHANISM_PTR mechanism,CK_OBJECT_HANDLE key)2866 mock_C_VerifyInit (CK_SESSION_HANDLE session,
2867                    CK_MECHANISM_PTR mechanism,
2868                    CK_OBJECT_HANDLE key)
2869 {
2870 	return_val_if_fail (mechanism != NULL, CKR_ARGUMENTS_BAD);
2871 	return prefix_mechanism_init (session, CKA_VERIFY, mechanism, key);
2872 }
2873 
2874 CK_RV
mock_C_VerifyInit__invalid_handle(CK_SESSION_HANDLE session,CK_MECHANISM_PTR mechanism,CK_OBJECT_HANDLE key)2875 mock_C_VerifyInit__invalid_handle (CK_SESSION_HANDLE session,
2876                                    CK_MECHANISM_PTR mechanism,
2877                                    CK_OBJECT_HANDLE key)
2878 {
2879 	return CKR_SESSION_HANDLE_INVALID;
2880 }
2881 
2882 CK_RV
mock_X_VerifyInit__invalid_handle(CK_X_FUNCTION_LIST * self,CK_SESSION_HANDLE session,CK_MECHANISM_PTR mechanism,CK_OBJECT_HANDLE key)2883 mock_X_VerifyInit__invalid_handle (CK_X_FUNCTION_LIST *self,
2884                                    CK_SESSION_HANDLE session,
2885                                    CK_MECHANISM_PTR mechanism,
2886                                    CK_OBJECT_HANDLE key)
2887 {
2888 	return CKR_SESSION_HANDLE_INVALID;
2889 }
2890 
2891 CK_RV
mock_C_Verify(CK_SESSION_HANDLE session,CK_BYTE_PTR data,CK_ULONG data_len,CK_BYTE_PTR signature,CK_ULONG signature_len)2892 mock_C_Verify (CK_SESSION_HANDLE session,
2893                CK_BYTE_PTR data,
2894                CK_ULONG data_len,
2895                CK_BYTE_PTR signature,
2896                CK_ULONG signature_len)
2897 {
2898 	CK_RV rv;
2899 
2900 	rv = mock_C_VerifyUpdate (session, data, data_len);
2901 	if (rv == CKR_OK)
2902 		rv = mock_C_VerifyFinal (session, signature, signature_len);
2903 
2904 	return rv;
2905 }
2906 
2907 CK_RV
mock_C_Verify__invalid_handle(CK_SESSION_HANDLE session,CK_BYTE_PTR data,CK_ULONG data_len,CK_BYTE_PTR signature,CK_ULONG signature_len)2908 mock_C_Verify__invalid_handle (CK_SESSION_HANDLE session,
2909                                CK_BYTE_PTR data,
2910                                CK_ULONG data_len,
2911                                CK_BYTE_PTR signature,
2912                                CK_ULONG signature_len)
2913 {
2914 	return CKR_SESSION_HANDLE_INVALID;
2915 }
2916 
2917 CK_RV
mock_X_Verify__invalid_handle(CK_X_FUNCTION_LIST * self,CK_SESSION_HANDLE session,CK_BYTE_PTR data,CK_ULONG data_len,CK_BYTE_PTR signature,CK_ULONG signature_len)2918 mock_X_Verify__invalid_handle (CK_X_FUNCTION_LIST *self,
2919                                CK_SESSION_HANDLE session,
2920                                CK_BYTE_PTR data,
2921                                CK_ULONG data_len,
2922                                CK_BYTE_PTR signature,
2923                                CK_ULONG signature_len)
2924 {
2925 	return CKR_SESSION_HANDLE_INVALID;
2926 }
2927 
2928 CK_RV
mock_C_VerifyUpdate(CK_SESSION_HANDLE session,CK_BYTE_PTR part,CK_ULONG part_len)2929 mock_C_VerifyUpdate (CK_SESSION_HANDLE session,
2930                      CK_BYTE_PTR part,
2931                      CK_ULONG part_len)
2932 {
2933 	Session *sess;
2934 
2935 	sess = p11_dict_get (the_sessions, handle_to_pointer (session));
2936 	if (!sess)
2937 		return CKR_SESSION_HANDLE_INVALID;
2938 	if (sess->hash_mechanism != CKM_MOCK_PREFIX ||
2939 	    sess->hash_method != CKA_VERIFY)
2940 		return CKR_OPERATION_NOT_INITIALIZED;
2941 	if (sess->want_context_login)
2942 		return CKR_USER_NOT_LOGGED_IN;
2943 
2944 	sess->hash_count += part_len;
2945 	return CKR_OK;
2946 }
2947 
2948 CK_RV
mock_C_VerifyUpdate__invalid_handle(CK_SESSION_HANDLE session,CK_BYTE_PTR part,CK_ULONG part_len)2949 mock_C_VerifyUpdate__invalid_handle (CK_SESSION_HANDLE session,
2950                                      CK_BYTE_PTR part,
2951                                      CK_ULONG part_len)
2952 {
2953 	return CKR_SESSION_HANDLE_INVALID;
2954 }
2955 
2956 CK_RV
mock_X_VerifyUpdate__invalid_handle(CK_X_FUNCTION_LIST * self,CK_SESSION_HANDLE session,CK_BYTE_PTR part,CK_ULONG part_len)2957 mock_X_VerifyUpdate__invalid_handle (CK_X_FUNCTION_LIST *self,
2958                                      CK_SESSION_HANDLE session,
2959                                      CK_BYTE_PTR part,
2960                                      CK_ULONG part_len)
2961 {
2962 	return CKR_SESSION_HANDLE_INVALID;
2963 }
2964 
2965 CK_RV
mock_C_VerifyFinal(CK_SESSION_HANDLE session,CK_BYTE_PTR signature,CK_ULONG signature_len)2966 mock_C_VerifyFinal (CK_SESSION_HANDLE session,
2967                     CK_BYTE_PTR signature,
2968                     CK_ULONG signature_len)
2969 {
2970 	char buffer[32];
2971 	Session *sess;
2972 	CK_ULONG length;
2973 	int len;
2974 
2975 	return_val_if_fail (signature, CKR_ARGUMENTS_BAD);
2976 
2977 	sess = p11_dict_get (the_sessions, handle_to_pointer (session));
2978 	if (!sess)
2979 		return CKR_SESSION_HANDLE_INVALID;
2980 	if (sess->hash_mechanism != CKM_MOCK_PREFIX ||
2981 	    sess->hash_method != CKA_VERIFY)
2982 		return CKR_OPERATION_NOT_INITIALIZED;
2983 	if (sess->want_context_login)
2984 		return CKR_USER_NOT_LOGGED_IN;
2985 
2986 	len = snprintf (buffer, sizeof (buffer), "%lu", sess->hash_count);
2987 	length = sess->n_sign_prefix + len;
2988 
2989 	if (signature_len != length)
2990 		return CKR_SIGNATURE_LEN_RANGE;
2991 
2992 	if (memcmp (signature, sess->sign_prefix, sess->n_sign_prefix) != 0 ||
2993 	    memcmp (signature + sess->n_sign_prefix, buffer, len) != 0)
2994 		return CKR_SIGNATURE_INVALID;
2995 
2996 	sess->hash_mechanism = 0;
2997 	sess->hash_method = 0;
2998 	sess->hash_count = 0;
2999 	sess->hash_key = 0;
3000 
3001 	return CKR_OK;
3002 }
3003 
3004 CK_RV
mock_C_VerifyFinal__invalid_handle(CK_SESSION_HANDLE session,CK_BYTE_PTR signature,CK_ULONG signature_len)3005 mock_C_VerifyFinal__invalid_handle (CK_SESSION_HANDLE session,
3006                                     CK_BYTE_PTR signature,
3007                                     CK_ULONG signature_len)
3008 {
3009 	return CKR_SESSION_HANDLE_INVALID;
3010 }
3011 
3012 CK_RV
mock_X_VerifyFinal__invalid_handle(CK_X_FUNCTION_LIST * self,CK_SESSION_HANDLE session,CK_BYTE_PTR signature,CK_ULONG signature_len)3013 mock_X_VerifyFinal__invalid_handle (CK_X_FUNCTION_LIST *self,
3014                                     CK_SESSION_HANDLE session,
3015                                     CK_BYTE_PTR signature,
3016                                     CK_ULONG signature_len)
3017 {
3018 	return CKR_SESSION_HANDLE_INVALID;
3019 }
3020 
3021 CK_RV
mock_C_VerifyRecoverInit(CK_SESSION_HANDLE session,CK_MECHANISM_PTR mechanism,CK_OBJECT_HANDLE key)3022 mock_C_VerifyRecoverInit (CK_SESSION_HANDLE session,
3023                           CK_MECHANISM_PTR mechanism,
3024                           CK_OBJECT_HANDLE key)
3025 {
3026 	return_val_if_fail (mechanism != NULL, CKR_ARGUMENTS_BAD);
3027 	return prefix_mechanism_init (session, CKA_VERIFY_RECOVER, mechanism, key);
3028 }
3029 
3030 CK_RV
mock_C_VerifyRecoverInit__invalid_handle(CK_SESSION_HANDLE session,CK_MECHANISM_PTR mechanism,CK_OBJECT_HANDLE key)3031 mock_C_VerifyRecoverInit__invalid_handle (CK_SESSION_HANDLE session,
3032                                           CK_MECHANISM_PTR mechanism,
3033                                           CK_OBJECT_HANDLE key)
3034 {
3035 	return CKR_SESSION_HANDLE_INVALID;
3036 }
3037 
3038 CK_RV
mock_X_VerifyRecoverInit__invalid_handle(CK_X_FUNCTION_LIST * self,CK_SESSION_HANDLE session,CK_MECHANISM_PTR mechanism,CK_OBJECT_HANDLE key)3039 mock_X_VerifyRecoverInit__invalid_handle (CK_X_FUNCTION_LIST *self,
3040                                           CK_SESSION_HANDLE session,
3041                                           CK_MECHANISM_PTR mechanism,
3042                                           CK_OBJECT_HANDLE key)
3043 {
3044 	return CKR_SESSION_HANDLE_INVALID;
3045 }
3046 
3047 CK_RV
mock_C_VerifyRecover(CK_SESSION_HANDLE session,CK_BYTE_PTR signature,CK_ULONG signature_len,CK_BYTE_PTR data,CK_ULONG_PTR data_len)3048 mock_C_VerifyRecover (CK_SESSION_HANDLE session,
3049                       CK_BYTE_PTR signature,
3050                       CK_ULONG signature_len,
3051                       CK_BYTE_PTR data,
3052                       CK_ULONG_PTR data_len)
3053 {
3054 	Session *sess;
3055 	CK_ULONG length;
3056 
3057 	return_val_if_fail (signature, CKR_ARGUMENTS_BAD);
3058 
3059 	sess = p11_dict_get (the_sessions, handle_to_pointer (session));
3060 	if (!sess)
3061 		return CKR_SESSION_HANDLE_INVALID;
3062 	if (sess->hash_mechanism != CKM_MOCK_PREFIX ||
3063 	    sess->hash_method != CKA_VERIFY_RECOVER)
3064 		return CKR_OPERATION_NOT_INITIALIZED;
3065 	if (sess->want_context_login)
3066 		return CKR_USER_NOT_LOGGED_IN;
3067 
3068 	if (signature_len < sess->n_sign_prefix)
3069 		return CKR_SIGNATURE_LEN_RANGE;
3070 	if (memcmp (signature, sess->sign_prefix, sess->n_sign_prefix) != 0)
3071 		return CKR_SIGNATURE_INVALID;
3072 
3073 	length = signature_len - sess->n_sign_prefix;
3074 	if (!data) {
3075 		*data_len = length;
3076 		return CKR_OK;
3077 	}
3078 
3079 	if (*data_len < length) {
3080 		*data_len = length;
3081 		return CKR_BUFFER_TOO_SMALL;
3082 	}
3083 
3084 	*data_len = length;
3085 	memcpy (data, signature + sess->n_sign_prefix, length);
3086 	return CKR_OK;
3087 }
3088 
3089 CK_RV
mock_C_VerifyRecover__invalid_handle(CK_SESSION_HANDLE session,CK_BYTE_PTR signature,CK_ULONG signature_len,CK_BYTE_PTR data,CK_ULONG_PTR data_len)3090 mock_C_VerifyRecover__invalid_handle (CK_SESSION_HANDLE session,
3091                                       CK_BYTE_PTR signature,
3092                                       CK_ULONG signature_len,
3093                                       CK_BYTE_PTR data,
3094                                       CK_ULONG_PTR data_len)
3095 {
3096 	return_val_if_fail (data_len, CKR_ARGUMENTS_BAD);
3097 
3098 	return CKR_SESSION_HANDLE_INVALID;
3099 }
3100 
3101 CK_RV
mock_X_VerifyRecover__invalid_handle(CK_X_FUNCTION_LIST * self,CK_SESSION_HANDLE session,CK_BYTE_PTR signature,CK_ULONG signature_len,CK_BYTE_PTR data,CK_ULONG_PTR data_len)3102 mock_X_VerifyRecover__invalid_handle (CK_X_FUNCTION_LIST *self,
3103                                       CK_SESSION_HANDLE session,
3104                                       CK_BYTE_PTR signature,
3105                                       CK_ULONG signature_len,
3106                                       CK_BYTE_PTR data,
3107                                       CK_ULONG_PTR data_len)
3108 {
3109 	return_val_if_fail (data_len, CKR_ARGUMENTS_BAD);
3110 
3111 	return CKR_SESSION_HANDLE_INVALID;
3112 }
3113 
3114 CK_RV
mock_C_DigestEncryptUpdate(CK_SESSION_HANDLE session,CK_BYTE_PTR part,CK_ULONG part_len,CK_BYTE_PTR encrypted_part,CK_ULONG_PTR encrypted_part_len)3115 mock_C_DigestEncryptUpdate (CK_SESSION_HANDLE session,
3116                             CK_BYTE_PTR part,
3117                             CK_ULONG part_len,
3118                             CK_BYTE_PTR encrypted_part,
3119                             CK_ULONG_PTR encrypted_part_len)
3120 {
3121 	CK_RV rv;
3122 
3123 	rv = mock_C_EncryptUpdate (session, part, part_len, encrypted_part, encrypted_part_len);
3124 	if (rv == CKR_OK)
3125 		rv = mock_C_DigestUpdate (session, part, part_len);
3126 
3127 	return rv;
3128 }
3129 
3130 CK_RV
mock_C_DigestEncryptUpdate__invalid_handle(CK_SESSION_HANDLE session,CK_BYTE_PTR part,CK_ULONG part_len,CK_BYTE_PTR enc_part,CK_ULONG_PTR enc_part_len)3131 mock_C_DigestEncryptUpdate__invalid_handle (CK_SESSION_HANDLE session,
3132                                             CK_BYTE_PTR part,
3133                                             CK_ULONG part_len,
3134                                             CK_BYTE_PTR enc_part,
3135                                             CK_ULONG_PTR enc_part_len)
3136 {
3137 	return_val_if_fail (enc_part_len, CKR_ARGUMENTS_BAD);
3138 
3139 	return CKR_SESSION_HANDLE_INVALID;
3140 }
3141 
3142 CK_RV
mock_X_DigestEncryptUpdate__invalid_handle(CK_X_FUNCTION_LIST * self,CK_SESSION_HANDLE session,CK_BYTE_PTR part,CK_ULONG part_len,CK_BYTE_PTR enc_part,CK_ULONG_PTR enc_part_len)3143 mock_X_DigestEncryptUpdate__invalid_handle (CK_X_FUNCTION_LIST *self,
3144                                             CK_SESSION_HANDLE session,
3145                                             CK_BYTE_PTR part,
3146                                             CK_ULONG part_len,
3147                                             CK_BYTE_PTR enc_part,
3148                                             CK_ULONG_PTR enc_part_len)
3149 {
3150 	return_val_if_fail (enc_part_len, CKR_ARGUMENTS_BAD);
3151 
3152 	return CKR_SESSION_HANDLE_INVALID;
3153 }
3154 
3155 CK_RV
mock_C_DecryptDigestUpdate(CK_SESSION_HANDLE session,CK_BYTE_PTR encrypted_part,CK_ULONG encrypted_part_len,CK_BYTE_PTR part,CK_ULONG_PTR part_len)3156 mock_C_DecryptDigestUpdate (CK_SESSION_HANDLE session,
3157                             CK_BYTE_PTR encrypted_part,
3158                             CK_ULONG encrypted_part_len,
3159                             CK_BYTE_PTR part,
3160                             CK_ULONG_PTR part_len)
3161 {
3162 	CK_RV rv;
3163 
3164 	rv = mock_C_DecryptUpdate (session, encrypted_part, encrypted_part_len, part, part_len);
3165 	if (rv == CKR_OK)
3166 		rv = mock_C_DigestUpdate (session, part, *part_len);
3167 
3168 	return rv;
3169 }
3170 
3171 CK_RV
mock_C_DecryptDigestUpdate__invalid_handle(CK_SESSION_HANDLE session,CK_BYTE_PTR enc_part,CK_ULONG enc_part_len,CK_BYTE_PTR part,CK_ULONG_PTR part_len)3172 mock_C_DecryptDigestUpdate__invalid_handle (CK_SESSION_HANDLE session,
3173                                             CK_BYTE_PTR enc_part,
3174                                             CK_ULONG enc_part_len,
3175                                             CK_BYTE_PTR part,
3176                                             CK_ULONG_PTR part_len)
3177 {
3178 	return_val_if_fail (part_len, CKR_ARGUMENTS_BAD);
3179 
3180 	return CKR_SESSION_HANDLE_INVALID;
3181 }
3182 
3183 CK_RV
mock_X_DecryptDigestUpdate__invalid_handle(CK_X_FUNCTION_LIST * self,CK_SESSION_HANDLE session,CK_BYTE_PTR enc_part,CK_ULONG enc_part_len,CK_BYTE_PTR part,CK_ULONG_PTR part_len)3184 mock_X_DecryptDigestUpdate__invalid_handle (CK_X_FUNCTION_LIST *self,
3185                                             CK_SESSION_HANDLE session,
3186                                             CK_BYTE_PTR enc_part,
3187                                             CK_ULONG enc_part_len,
3188                                             CK_BYTE_PTR part,
3189                                             CK_ULONG_PTR part_len)
3190 {
3191 	return_val_if_fail (part_len, CKR_ARGUMENTS_BAD);
3192 
3193 	return CKR_SESSION_HANDLE_INVALID;
3194 }
3195 
3196 CK_RV
mock_C_SignEncryptUpdate(CK_SESSION_HANDLE session,CK_BYTE_PTR part,CK_ULONG part_len,CK_BYTE_PTR encrypted_part,CK_ULONG_PTR encrypted_part_len)3197 mock_C_SignEncryptUpdate (CK_SESSION_HANDLE session,
3198                           CK_BYTE_PTR part,
3199                           CK_ULONG part_len,
3200                           CK_BYTE_PTR encrypted_part,
3201                           CK_ULONG_PTR encrypted_part_len)
3202 {
3203 	CK_RV rv;
3204 
3205 	rv = mock_C_EncryptUpdate (session, part, part_len, encrypted_part, encrypted_part_len);
3206 	if (rv == CKR_OK)
3207 		rv = mock_C_SignUpdate (session, part, part_len);
3208 
3209 	return rv;
3210 }
3211 
3212 CK_RV
mock_C_SignEncryptUpdate__invalid_handle(CK_SESSION_HANDLE session,CK_BYTE_PTR part,CK_ULONG part_len,CK_BYTE_PTR enc_part,CK_ULONG_PTR enc_part_len)3213 mock_C_SignEncryptUpdate__invalid_handle (CK_SESSION_HANDLE session,
3214                                           CK_BYTE_PTR part,
3215                                           CK_ULONG part_len,
3216                                           CK_BYTE_PTR enc_part,
3217                                           CK_ULONG_PTR enc_part_len)
3218 {
3219 	return_val_if_fail (enc_part_len, CKR_ARGUMENTS_BAD);
3220 
3221 	return CKR_SESSION_HANDLE_INVALID;
3222 }
3223 
3224 CK_RV
mock_X_SignEncryptUpdate__invalid_handle(CK_X_FUNCTION_LIST * self,CK_SESSION_HANDLE session,CK_BYTE_PTR part,CK_ULONG part_len,CK_BYTE_PTR enc_part,CK_ULONG_PTR enc_part_len)3225 mock_X_SignEncryptUpdate__invalid_handle (CK_X_FUNCTION_LIST *self,
3226                                           CK_SESSION_HANDLE session,
3227                                           CK_BYTE_PTR part,
3228                                           CK_ULONG part_len,
3229                                           CK_BYTE_PTR enc_part,
3230                                           CK_ULONG_PTR enc_part_len)
3231 {
3232 	return_val_if_fail (enc_part_len, CKR_ARGUMENTS_BAD);
3233 
3234 	return CKR_SESSION_HANDLE_INVALID;
3235 }
3236 
3237 CK_RV
mock_C_DecryptVerifyUpdate(CK_SESSION_HANDLE session,CK_BYTE_PTR encrypted_part,CK_ULONG encrypted_part_len,CK_BYTE_PTR part,CK_ULONG_PTR part_len)3238 mock_C_DecryptVerifyUpdate (CK_SESSION_HANDLE session,
3239                             CK_BYTE_PTR encrypted_part,
3240                             CK_ULONG encrypted_part_len,
3241                             CK_BYTE_PTR part,
3242                             CK_ULONG_PTR part_len)
3243 {
3244 	CK_RV rv;
3245 
3246 	rv = mock_C_DecryptUpdate (session, encrypted_part, encrypted_part_len, part, part_len);
3247 	if (rv == CKR_OK)
3248 		rv = mock_C_VerifyUpdate (session, part, *part_len);
3249 
3250 	return rv;
3251 }
3252 
3253 CK_RV
mock_C_DecryptVerifyUpdate__invalid_handle(CK_SESSION_HANDLE session,CK_BYTE_PTR enc_part,CK_ULONG enc_part_len,CK_BYTE_PTR part,CK_ULONG_PTR part_len)3254 mock_C_DecryptVerifyUpdate__invalid_handle (CK_SESSION_HANDLE session,
3255                                             CK_BYTE_PTR enc_part,
3256                                             CK_ULONG enc_part_len,
3257                                             CK_BYTE_PTR part,
3258                                             CK_ULONG_PTR part_len)
3259 {
3260 	return_val_if_fail (part_len, CKR_ARGUMENTS_BAD);
3261 
3262 	return CKR_SESSION_HANDLE_INVALID;
3263 }
3264 
3265 CK_RV
mock_X_DecryptVerifyUpdate__invalid_handle(CK_X_FUNCTION_LIST * self,CK_SESSION_HANDLE session,CK_BYTE_PTR enc_part,CK_ULONG enc_part_len,CK_BYTE_PTR part,CK_ULONG_PTR part_len)3266 mock_X_DecryptVerifyUpdate__invalid_handle (CK_X_FUNCTION_LIST *self,
3267                                             CK_SESSION_HANDLE session,
3268                                             CK_BYTE_PTR enc_part,
3269                                             CK_ULONG enc_part_len,
3270                                             CK_BYTE_PTR part,
3271                                             CK_ULONG_PTR part_len)
3272 {
3273 	return_val_if_fail (part_len, CKR_ARGUMENTS_BAD);
3274 
3275 	return CKR_SESSION_HANDLE_INVALID;
3276 }
3277 
3278 CK_RV
mock_C_GenerateKey(CK_SESSION_HANDLE session,CK_MECHANISM_PTR mechanism,CK_ATTRIBUTE_PTR template,CK_ULONG count,CK_OBJECT_HANDLE_PTR key)3279 mock_C_GenerateKey (CK_SESSION_HANDLE session,
3280                     CK_MECHANISM_PTR mechanism,
3281                     CK_ATTRIBUTE_PTR template,
3282                     CK_ULONG count,
3283                     CK_OBJECT_HANDLE_PTR key)
3284 {
3285 	CK_ATTRIBUTE *attrs;
3286 	CK_ATTRIBUTE value;
3287 	Session *sess;
3288 	CK_BBOOL token;
3289 
3290 	return_val_if_fail (mechanism, CKR_MECHANISM_INVALID);
3291 	return_val_if_fail (template, CKR_TEMPLATE_INCOMPLETE);
3292 	return_val_if_fail (count, CKR_TEMPLATE_INCOMPLETE);
3293 	return_val_if_fail (key, CKR_ARGUMENTS_BAD);
3294 
3295 	sess = p11_dict_get (the_sessions, handle_to_pointer (session));
3296 	if (!sess)
3297 		return CKR_SESSION_HANDLE_INVALID;
3298 
3299 	if (mechanism->mechanism != CKM_MOCK_GENERATE)
3300 		return CKR_MECHANISM_INVALID;
3301 
3302 	if (!mechanism->pParameter || mechanism->ulParameterLen != 9 ||
3303 	    memcmp (mechanism->pParameter, "generate", 9) != 0)
3304 		return CKR_MECHANISM_PARAM_INVALID;
3305 
3306 	value.type = CKA_VALUE;
3307 	value.pValue = "generated";
3308 	value.ulValueLen = strlen (value.pValue);
3309 
3310 	attrs = p11_attrs_buildn (NULL, template, count);
3311 	attrs = p11_attrs_buildn (attrs, &value, 1);
3312 
3313 	*key = ++unique_identifier;
3314 	if (p11_attrs_find_bool (attrs, CKA_TOKEN, &token) && token)
3315 		p11_dict_set (the_objects, handle_to_pointer (*key), attrs);
3316 	else
3317 		p11_dict_set (sess->objects, handle_to_pointer (*key), attrs);
3318 
3319 	return CKR_OK;
3320 }
3321 
3322 CK_RV
mock_C_GenerateKey__invalid_handle(CK_SESSION_HANDLE session,CK_MECHANISM_PTR mechanism,CK_ATTRIBUTE_PTR template,CK_ULONG count,CK_OBJECT_HANDLE_PTR key)3323 mock_C_GenerateKey__invalid_handle (CK_SESSION_HANDLE session,
3324                                     CK_MECHANISM_PTR mechanism,
3325                                     CK_ATTRIBUTE_PTR template,
3326                                     CK_ULONG count,
3327                                     CK_OBJECT_HANDLE_PTR key)
3328 {
3329 	return CKR_SESSION_HANDLE_INVALID;
3330 }
3331 
3332 CK_RV
mock_X_GenerateKey__invalid_handle(CK_X_FUNCTION_LIST * self,CK_SESSION_HANDLE session,CK_MECHANISM_PTR mechanism,CK_ATTRIBUTE_PTR template,CK_ULONG count,CK_OBJECT_HANDLE_PTR key)3333 mock_X_GenerateKey__invalid_handle (CK_X_FUNCTION_LIST *self,
3334                                     CK_SESSION_HANDLE session,
3335                                     CK_MECHANISM_PTR mechanism,
3336                                     CK_ATTRIBUTE_PTR template,
3337                                     CK_ULONG count,
3338                                     CK_OBJECT_HANDLE_PTR key)
3339 {
3340 	return CKR_SESSION_HANDLE_INVALID;
3341 }
3342 
3343 CK_RV
mock_C_GenerateKeyPair(CK_SESSION_HANDLE session,CK_MECHANISM_PTR mechanism,CK_ATTRIBUTE_PTR public_key_template,CK_ULONG public_key_count,CK_ATTRIBUTE_PTR private_key_template,CK_ULONG private_key_count,CK_OBJECT_HANDLE_PTR public_key,CK_OBJECT_HANDLE_PTR private_key)3344 mock_C_GenerateKeyPair (CK_SESSION_HANDLE session,
3345                         CK_MECHANISM_PTR mechanism,
3346                         CK_ATTRIBUTE_PTR public_key_template,
3347                         CK_ULONG public_key_count,
3348                         CK_ATTRIBUTE_PTR private_key_template,
3349                         CK_ULONG private_key_count,
3350                         CK_OBJECT_HANDLE_PTR public_key,
3351                         CK_OBJECT_HANDLE_PTR private_key)
3352 {
3353 	CK_ATTRIBUTE *attrs;
3354 	CK_ATTRIBUTE value;
3355 	Session *sess;
3356 	CK_BBOOL token;
3357 
3358 	return_val_if_fail (mechanism, CKR_MECHANISM_INVALID);
3359 	return_val_if_fail (public_key_template, CKR_TEMPLATE_INCOMPLETE);
3360 	return_val_if_fail (public_key_count, CKR_TEMPLATE_INCOMPLETE);
3361 	return_val_if_fail (private_key_template, CKR_TEMPLATE_INCOMPLETE);
3362 	return_val_if_fail (private_key_count, CKR_TEMPLATE_INCOMPLETE);
3363 	return_val_if_fail (public_key, CKR_ARGUMENTS_BAD);
3364 	return_val_if_fail (private_key, CKR_ARGUMENTS_BAD);
3365 
3366 	sess = p11_dict_get (the_sessions, handle_to_pointer (session));
3367 	if (!sess)
3368 		return CKR_SESSION_HANDLE_INVALID;
3369 
3370 	if (mechanism->mechanism != CKM_MOCK_GENERATE)
3371 		return CKR_MECHANISM_INVALID;
3372 
3373 	if (!mechanism->pParameter || mechanism->ulParameterLen != 9 ||
3374 	    memcmp (mechanism->pParameter, "generate", 9) != 0)
3375 		return CKR_MECHANISM_PARAM_INVALID;
3376 
3377 	value.type = CKA_VALUE;
3378 	value.pValue = "generated";
3379 	value.ulValueLen = strlen (value.pValue);
3380 
3381 	attrs = p11_attrs_buildn (NULL, public_key_template, public_key_count);
3382 	attrs = p11_attrs_buildn (attrs, &value, 1);
3383 
3384 	*public_key = ++unique_identifier;
3385 	if (p11_attrs_find_bool (attrs, CKA_TOKEN, &token) && token)
3386 		p11_dict_set (the_objects, handle_to_pointer (*public_key), attrs);
3387 	else
3388 		p11_dict_set (sess->objects, handle_to_pointer (*public_key), attrs);
3389 
3390 	attrs = p11_attrs_buildn (NULL, private_key_template, private_key_count);
3391 	attrs = p11_attrs_buildn (attrs, &value, 1);
3392 
3393 	*private_key = ++unique_identifier;
3394 	if (p11_attrs_find_bool (attrs, CKA_TOKEN, &token) && token)
3395 		p11_dict_set (the_objects, handle_to_pointer (*private_key), attrs);
3396 	else
3397 		p11_dict_set (sess->objects, handle_to_pointer (*private_key), attrs);
3398 
3399 	return CKR_OK;
3400 }
3401 
3402 CK_RV
mock_C_GenerateKeyPair__invalid_handle(CK_SESSION_HANDLE session,CK_MECHANISM_PTR mechanism,CK_ATTRIBUTE_PTR pub_template,CK_ULONG pub_count,CK_ATTRIBUTE_PTR priv_template,CK_ULONG priv_count,CK_OBJECT_HANDLE_PTR pub_key,CK_OBJECT_HANDLE_PTR priv_key)3403 mock_C_GenerateKeyPair__invalid_handle (CK_SESSION_HANDLE session,
3404                                         CK_MECHANISM_PTR mechanism,
3405                                         CK_ATTRIBUTE_PTR pub_template,
3406                                         CK_ULONG pub_count,
3407                                         CK_ATTRIBUTE_PTR priv_template,
3408                                         CK_ULONG priv_count,
3409                                         CK_OBJECT_HANDLE_PTR pub_key,
3410                                         CK_OBJECT_HANDLE_PTR priv_key)
3411 {
3412 	return CKR_SESSION_HANDLE_INVALID;
3413 }
3414 
3415 CK_RV
mock_X_GenerateKeyPair__invalid_handle(CK_X_FUNCTION_LIST * self,CK_SESSION_HANDLE session,CK_MECHANISM_PTR mechanism,CK_ATTRIBUTE_PTR pub_template,CK_ULONG pub_count,CK_ATTRIBUTE_PTR priv_template,CK_ULONG priv_count,CK_OBJECT_HANDLE_PTR pub_key,CK_OBJECT_HANDLE_PTR priv_key)3416 mock_X_GenerateKeyPair__invalid_handle (CK_X_FUNCTION_LIST *self,
3417                                         CK_SESSION_HANDLE session,
3418                                         CK_MECHANISM_PTR mechanism,
3419                                         CK_ATTRIBUTE_PTR pub_template,
3420                                         CK_ULONG pub_count,
3421                                         CK_ATTRIBUTE_PTR priv_template,
3422                                         CK_ULONG priv_count,
3423                                         CK_OBJECT_HANDLE_PTR pub_key,
3424                                         CK_OBJECT_HANDLE_PTR priv_key)
3425 {
3426 	return CKR_SESSION_HANDLE_INVALID;
3427 }
3428 
3429 CK_RV
mock_C_WrapKey(CK_SESSION_HANDLE session,CK_MECHANISM_PTR mechanism,CK_OBJECT_HANDLE wrapping_key,CK_OBJECT_HANDLE key,CK_BYTE_PTR wrapped_key,CK_ULONG_PTR wrapped_key_len)3430 mock_C_WrapKey (CK_SESSION_HANDLE session,
3431                 CK_MECHANISM_PTR mechanism,
3432                 CK_OBJECT_HANDLE wrapping_key,
3433                 CK_OBJECT_HANDLE key,
3434                 CK_BYTE_PTR wrapped_key,
3435                 CK_ULONG_PTR wrapped_key_len)
3436 {
3437 	CK_ATTRIBUTE *attrs;
3438 	CK_ATTRIBUTE *attr;
3439 	Session *sess;
3440 	CK_RV rv;
3441 
3442 	return_val_if_fail (mechanism, CKR_MECHANISM_INVALID);
3443 	return_val_if_fail (wrapping_key, CKR_OBJECT_HANDLE_INVALID);
3444 	return_val_if_fail (key, CKR_OBJECT_HANDLE_INVALID);
3445 	return_val_if_fail (wrapped_key_len, CKR_WRAPPED_KEY_LEN_RANGE);
3446 
3447 	sess = p11_dict_get (the_sessions, handle_to_pointer (session));
3448 	if (!sess)
3449 		return CKR_SESSION_HANDLE_INVALID;
3450 
3451 	rv = lookup_object (sess, wrapping_key, &attrs, NULL);
3452 	if (rv == CKR_OBJECT_HANDLE_INVALID)
3453 		return CKR_WRAPPING_KEY_HANDLE_INVALID;
3454 	else if (rv != CKR_OK)
3455 		return rv;
3456 
3457 	rv = lookup_object (sess, key, &attrs, NULL);
3458 	if (rv == CKR_OBJECT_HANDLE_INVALID)
3459 		return CKR_WRAPPING_KEY_HANDLE_INVALID;
3460 	else if (rv != CKR_OK)
3461 		return rv;
3462 
3463 	if (mechanism->mechanism != CKM_MOCK_WRAP)
3464 		return CKR_MECHANISM_INVALID;
3465 
3466 	if (mechanism->pParameter == NULL ||
3467 	    mechanism->ulParameterLen != 4 ||
3468 	    memcmp (mechanism->pParameter, "wrap", 4) != 0) {
3469 		return CKR_MECHANISM_PARAM_INVALID;
3470 	}
3471 
3472 	attr = p11_attrs_find_valid (attrs, CKA_VALUE);
3473 	if (attr == NULL)
3474 		return CKR_WRAPPED_KEY_INVALID;
3475 
3476 	if (!wrapped_key) {
3477 		*wrapped_key_len = attr->ulValueLen;
3478 		return CKR_OK;
3479 	}
3480 
3481 	if (*wrapped_key_len < attr->ulValueLen) {
3482 		*wrapped_key_len = attr->ulValueLen;
3483 		return CKR_BUFFER_TOO_SMALL;
3484 	}
3485 
3486 	memcpy (wrapped_key, attr->pValue, attr->ulValueLen);
3487 	*wrapped_key_len = attr->ulValueLen;
3488 
3489 	return CKR_OK;
3490 }
3491 
3492 CK_RV
mock_C_WrapKey__invalid_handle(CK_SESSION_HANDLE session,CK_MECHANISM_PTR mechanism,CK_OBJECT_HANDLE wrapping_key,CK_OBJECT_HANDLE key,CK_BYTE_PTR wrapped_key,CK_ULONG_PTR wrapped_key_len)3493 mock_C_WrapKey__invalid_handle (CK_SESSION_HANDLE session,
3494                                 CK_MECHANISM_PTR mechanism,
3495                                 CK_OBJECT_HANDLE wrapping_key,
3496                                 CK_OBJECT_HANDLE key,
3497                                 CK_BYTE_PTR wrapped_key,
3498                                 CK_ULONG_PTR wrapped_key_len)
3499 {
3500 	return_val_if_fail (wrapped_key_len, CKR_ARGUMENTS_BAD);
3501 
3502 	return CKR_SESSION_HANDLE_INVALID;
3503 }
3504 
3505 CK_RV
mock_X_WrapKey__invalid_handle(CK_X_FUNCTION_LIST * self,CK_SESSION_HANDLE session,CK_MECHANISM_PTR mechanism,CK_OBJECT_HANDLE wrapping_key,CK_OBJECT_HANDLE key,CK_BYTE_PTR wrapped_key,CK_ULONG_PTR wrapped_key_len)3506 mock_X_WrapKey__invalid_handle (CK_X_FUNCTION_LIST *self,
3507                                 CK_SESSION_HANDLE session,
3508                                 CK_MECHANISM_PTR mechanism,
3509                                 CK_OBJECT_HANDLE wrapping_key,
3510                                 CK_OBJECT_HANDLE key,
3511                                 CK_BYTE_PTR wrapped_key,
3512                                 CK_ULONG_PTR wrapped_key_len)
3513 {
3514 	return_val_if_fail (wrapped_key_len, CKR_ARGUMENTS_BAD);
3515 
3516 	return CKR_SESSION_HANDLE_INVALID;
3517 }
3518 
3519 CK_RV
mock_C_UnwrapKey(CK_SESSION_HANDLE session,CK_MECHANISM_PTR mechanism,CK_OBJECT_HANDLE unwrapping_key,CK_BYTE_PTR wrapped_key,CK_ULONG wrapped_key_len,CK_ATTRIBUTE_PTR template,CK_ULONG count,CK_OBJECT_HANDLE_PTR key)3520 mock_C_UnwrapKey (CK_SESSION_HANDLE session,
3521                   CK_MECHANISM_PTR mechanism,
3522                   CK_OBJECT_HANDLE unwrapping_key,
3523                   CK_BYTE_PTR wrapped_key,
3524                   CK_ULONG wrapped_key_len,
3525                   CK_ATTRIBUTE_PTR template,
3526                   CK_ULONG count,
3527                   CK_OBJECT_HANDLE_PTR key)
3528 {
3529 	CK_ATTRIBUTE *attrs;
3530 	CK_ATTRIBUTE value;
3531 	Session *sess;
3532 	CK_BBOOL token;
3533 	CK_RV rv;
3534 
3535 	return_val_if_fail (mechanism, CKR_MECHANISM_INVALID);
3536 	return_val_if_fail (unwrapping_key, CKR_WRAPPING_KEY_HANDLE_INVALID);
3537 	return_val_if_fail (wrapped_key, CKR_WRAPPED_KEY_INVALID);
3538 	return_val_if_fail (wrapped_key_len, CKR_WRAPPED_KEY_LEN_RANGE);
3539 	return_val_if_fail (key, CKR_ARGUMENTS_BAD);
3540 	return_val_if_fail (template, CKR_TEMPLATE_INCOMPLETE);
3541 	return_val_if_fail (count, CKR_TEMPLATE_INCONSISTENT);
3542 
3543 	sess = p11_dict_get (the_sessions, handle_to_pointer (session));
3544 	if (!sess)
3545 		return CKR_SESSION_HANDLE_INVALID;
3546 
3547 	rv = lookup_object (sess, unwrapping_key, &attrs, NULL);
3548 	if (rv == CKR_OBJECT_HANDLE_INVALID)
3549 		return CKR_WRAPPING_KEY_HANDLE_INVALID;
3550 	else if (rv != CKR_OK)
3551 		return rv;
3552 
3553 	if (mechanism->mechanism != CKM_MOCK_WRAP)
3554 		return CKR_MECHANISM_INVALID;
3555 
3556 	if (mechanism->pParameter == NULL ||
3557 	    mechanism->ulParameterLen != 4 ||
3558 	    memcmp (mechanism->pParameter, "wrap", 4) != 0) {
3559 		return CKR_MECHANISM_PARAM_INVALID;
3560 	}
3561 
3562 	value.type = CKA_VALUE;
3563 	value.pValue = wrapped_key;
3564 	value.ulValueLen = wrapped_key_len;
3565 
3566 	attrs = p11_attrs_buildn (NULL, template, count);
3567 	attrs = p11_attrs_buildn (attrs, &value, 1);
3568 
3569 	*key = ++unique_identifier;
3570 	if (p11_attrs_find_bool (attrs, CKA_TOKEN, &token) && token)
3571 		p11_dict_set (the_objects, handle_to_pointer (*key), attrs);
3572 	else
3573 		p11_dict_set (sess->objects, handle_to_pointer (*key), attrs);
3574 
3575 	return CKR_OK;
3576 }
3577 
3578 CK_RV
mock_C_UnwrapKey__invalid_handle(CK_SESSION_HANDLE session,CK_MECHANISM_PTR mechanism,CK_OBJECT_HANDLE unwrapping_key,CK_BYTE_PTR wrapped_key,CK_ULONG wrapped_key_len,CK_ATTRIBUTE_PTR template,CK_ULONG count,CK_OBJECT_HANDLE_PTR key)3579 mock_C_UnwrapKey__invalid_handle (CK_SESSION_HANDLE session,
3580                                   CK_MECHANISM_PTR mechanism,
3581                                   CK_OBJECT_HANDLE unwrapping_key,
3582                                   CK_BYTE_PTR wrapped_key,
3583                                   CK_ULONG wrapped_key_len,
3584                                   CK_ATTRIBUTE_PTR template,
3585                                   CK_ULONG count,
3586                                   CK_OBJECT_HANDLE_PTR key)
3587 {
3588 	return CKR_SESSION_HANDLE_INVALID;
3589 }
3590 
3591 CK_RV
mock_X_UnwrapKey__invalid_handle(CK_X_FUNCTION_LIST * self,CK_SESSION_HANDLE session,CK_MECHANISM_PTR mechanism,CK_OBJECT_HANDLE unwrapping_key,CK_BYTE_PTR wrapped_key,CK_ULONG wrapped_key_len,CK_ATTRIBUTE_PTR template,CK_ULONG count,CK_OBJECT_HANDLE_PTR key)3592 mock_X_UnwrapKey__invalid_handle (CK_X_FUNCTION_LIST *self,
3593                                   CK_SESSION_HANDLE session,
3594                                   CK_MECHANISM_PTR mechanism,
3595                                   CK_OBJECT_HANDLE unwrapping_key,
3596                                   CK_BYTE_PTR wrapped_key,
3597                                   CK_ULONG wrapped_key_len,
3598                                   CK_ATTRIBUTE_PTR template,
3599                                   CK_ULONG count,
3600                                   CK_OBJECT_HANDLE_PTR key)
3601 {
3602 	return CKR_SESSION_HANDLE_INVALID;
3603 }
3604 
3605 CK_RV
mock_C_DeriveKey(CK_SESSION_HANDLE session,CK_MECHANISM_PTR mechanism,CK_OBJECT_HANDLE base_key,CK_ATTRIBUTE_PTR template,CK_ULONG count,CK_OBJECT_HANDLE_PTR key)3606 mock_C_DeriveKey (CK_SESSION_HANDLE session,
3607                   CK_MECHANISM_PTR mechanism,
3608                   CK_OBJECT_HANDLE base_key,
3609                   CK_ATTRIBUTE_PTR template,
3610                   CK_ULONG count,
3611                   CK_OBJECT_HANDLE_PTR key)
3612 {
3613 	CK_ATTRIBUTE *attrs, *copy;
3614 	CK_ATTRIBUTE value;
3615 	Session *sess;
3616 	CK_BBOOL token;
3617 	CK_RV rv;
3618 
3619 	return_val_if_fail (mechanism, CKR_MECHANISM_INVALID);
3620 	return_val_if_fail (count, CKR_TEMPLATE_INCOMPLETE);
3621 	return_val_if_fail (template, CKR_TEMPLATE_INCOMPLETE);
3622 	return_val_if_fail (key, CKR_ARGUMENTS_BAD);
3623 
3624 	sess = p11_dict_get (the_sessions, handle_to_pointer (session));
3625 	if (!sess)
3626 		return CKR_SESSION_HANDLE_INVALID;
3627 
3628 	rv = lookup_object (sess, base_key, &attrs, NULL);
3629 	if (rv == CKR_OBJECT_HANDLE_INVALID)
3630 		return CKR_KEY_HANDLE_INVALID;
3631 	else if (rv != CKR_OK)
3632 		return rv;
3633 
3634 	if (mechanism->mechanism != CKM_MOCK_DERIVE)
3635 		return CKR_MECHANISM_INVALID;
3636 
3637 	if (mechanism->pParameter == NULL ||
3638 	    mechanism->ulParameterLen != 6 ||
3639 	    memcmp (mechanism->pParameter, "derive", 6) != 0) {
3640 		return CKR_MECHANISM_PARAM_INVALID;
3641 	}
3642 
3643 	value.type = CKA_VALUE;
3644 	value.pValue = "derived";
3645 	value.ulValueLen = strlen (value.pValue);
3646 
3647 	copy = p11_attrs_buildn (NULL, template, count);
3648 	copy = p11_attrs_buildn (copy, &value, 1);
3649 
3650 	*key = ++unique_identifier;
3651 	if (p11_attrs_find_bool (copy, CKA_TOKEN, &token) && token)
3652 		p11_dict_set (the_objects, handle_to_pointer (*key), copy);
3653 	else
3654 		p11_dict_set (sess->objects, handle_to_pointer (*key), copy);
3655 
3656 	return CKR_OK;
3657 }
3658 
3659 CK_RV
mock_C_DeriveKey__invalid_handle(CK_SESSION_HANDLE session,CK_MECHANISM_PTR mechanism,CK_OBJECT_HANDLE base_key,CK_ATTRIBUTE_PTR template,CK_ULONG count,CK_OBJECT_HANDLE_PTR key)3660 mock_C_DeriveKey__invalid_handle (CK_SESSION_HANDLE session,
3661                                   CK_MECHANISM_PTR mechanism,
3662                                   CK_OBJECT_HANDLE base_key,
3663                                   CK_ATTRIBUTE_PTR template,
3664                                   CK_ULONG count,
3665                                   CK_OBJECT_HANDLE_PTR key)
3666 {
3667 	return CKR_SESSION_HANDLE_INVALID;
3668 }
3669 
3670 CK_RV
mock_X_DeriveKey__invalid_handle(CK_X_FUNCTION_LIST * self,CK_SESSION_HANDLE session,CK_MECHANISM_PTR mechanism,CK_OBJECT_HANDLE base_key,CK_ATTRIBUTE_PTR template,CK_ULONG count,CK_OBJECT_HANDLE_PTR key)3671 mock_X_DeriveKey__invalid_handle (CK_X_FUNCTION_LIST *self,
3672                                   CK_SESSION_HANDLE session,
3673                                   CK_MECHANISM_PTR mechanism,
3674                                   CK_OBJECT_HANDLE base_key,
3675                                   CK_ATTRIBUTE_PTR template,
3676                                   CK_ULONG count,
3677                                   CK_OBJECT_HANDLE_PTR key)
3678 {
3679 	return CKR_SESSION_HANDLE_INVALID;
3680 }
3681 
3682 CK_RV
mock_C_SeedRandom(CK_SESSION_HANDLE session,CK_BYTE_PTR seed,CK_ULONG seed_len)3683 mock_C_SeedRandom (CK_SESSION_HANDLE session,
3684                    CK_BYTE_PTR seed,
3685                    CK_ULONG seed_len)
3686 {
3687 	Session *sess;
3688 
3689 	sess = p11_dict_get (the_sessions, handle_to_pointer (session));
3690 	if (!sess)
3691 		return CKR_SESSION_HANDLE_INVALID;
3692 
3693 	if (seed_len > sizeof (sess->random_seed))
3694 		return CKR_RANDOM_SEED_NOT_SUPPORTED;
3695 
3696 	memcpy (sess->random_seed, seed, seed_len);
3697 	sess->random_seed_len = seed_len;
3698 	return CKR_OK;
3699 }
3700 
3701 CK_RV
mock_C_SeedRandom__invalid_handle(CK_SESSION_HANDLE session,CK_BYTE_PTR seed,CK_ULONG seed_len)3702 mock_C_SeedRandom__invalid_handle (CK_SESSION_HANDLE session,
3703                                    CK_BYTE_PTR seed,
3704                                    CK_ULONG seed_len)
3705 {
3706 	return CKR_SESSION_HANDLE_INVALID;
3707 }
3708 
3709 CK_RV
mock_X_SeedRandom__invalid_handle(CK_X_FUNCTION_LIST * self,CK_SESSION_HANDLE session,CK_BYTE_PTR seed,CK_ULONG seed_len)3710 mock_X_SeedRandom__invalid_handle (CK_X_FUNCTION_LIST *self,
3711                                    CK_SESSION_HANDLE session,
3712                                    CK_BYTE_PTR seed,
3713                                    CK_ULONG seed_len)
3714 {
3715 	return CKR_SESSION_HANDLE_INVALID;
3716 }
3717 
3718 CK_RV
mock_C_GenerateRandom(CK_SESSION_HANDLE session,CK_BYTE_PTR random_data,CK_ULONG random_len)3719 mock_C_GenerateRandom (CK_SESSION_HANDLE session,
3720                        CK_BYTE_PTR random_data,
3721                        CK_ULONG random_len)
3722 {
3723 	Session *sess;
3724 	CK_ULONG block;
3725 
3726 	sess = p11_dict_get (the_sessions, handle_to_pointer (session));
3727 	if (!sess)
3728 		return CKR_SESSION_HANDLE_INVALID;
3729 
3730 	while (random_len > 0) {
3731 		block = sess->random_seed_len;
3732 		if (block > random_len)
3733 			block = random_len;
3734 		memcpy (random_data, sess->random_seed, block);
3735 		random_data += block;
3736 		random_len -= block;
3737 	}
3738 
3739 	return CKR_OK;
3740 }
3741 
3742 CK_RV
mock_C_GenerateRandom__invalid_handle(CK_SESSION_HANDLE session,CK_BYTE_PTR random_data,CK_ULONG random_len)3743 mock_C_GenerateRandom__invalid_handle (CK_SESSION_HANDLE session,
3744                                        CK_BYTE_PTR random_data,
3745                                        CK_ULONG random_len)
3746 {
3747 	return CKR_SESSION_HANDLE_INVALID;
3748 }
3749 
3750 CK_RV
mock_X_GenerateRandom__invalid_handle(CK_X_FUNCTION_LIST * self,CK_SESSION_HANDLE session,CK_BYTE_PTR random_data,CK_ULONG random_len)3751 mock_X_GenerateRandom__invalid_handle (CK_X_FUNCTION_LIST *self,
3752                                        CK_SESSION_HANDLE session,
3753                                        CK_BYTE_PTR random_data,
3754                                        CK_ULONG random_len)
3755 {
3756 	return CKR_SESSION_HANDLE_INVALID;
3757 }
3758 
3759 CK_FUNCTION_LIST mock_module_no_slots = {
3760 	{ CRYPTOKI_VERSION_MAJOR, CRYPTOKI_VERSION_MINOR },  /* version */
3761 	mock_C_Initialize,
3762 	mock_C_Finalize,
3763 	mock_C_GetInfo,
3764 	mock_C_GetFunctionList_not_supported,
3765 	mock_C_GetSlotList__no_tokens,
3766 	mock_C_GetSlotInfo__invalid_slotid,
3767 	mock_C_GetTokenInfo__invalid_slotid,
3768 	mock_C_GetMechanismList__invalid_slotid,
3769 	mock_C_GetMechanismInfo__invalid_slotid,
3770 	mock_C_InitToken__invalid_slotid,
3771 	mock_C_InitPIN__invalid_handle,
3772 	mock_C_SetPIN__invalid_handle,
3773 	mock_C_OpenSession__invalid_slotid,
3774 	mock_C_CloseSession__invalid_handle,
3775 	mock_C_CloseAllSessions__invalid_slotid,
3776 	mock_C_GetSessionInfo__invalid_handle,
3777 	mock_C_GetOperationState__invalid_handle,
3778 	mock_C_SetOperationState__invalid_handle,
3779 	mock_C_Login__invalid_handle,
3780 	mock_C_Logout__invalid_handle,
3781 	mock_C_CreateObject__invalid_handle,
3782 	mock_C_CopyObject__invalid_handle,
3783 	mock_C_DestroyObject__invalid_handle,
3784 	mock_C_GetObjectSize__invalid_handle,
3785 	mock_C_GetAttributeValue__invalid_handle,
3786 	mock_C_SetAttributeValue__invalid_handle,
3787 	mock_C_FindObjectsInit__invalid_handle,
3788 	mock_C_FindObjects__invalid_handle,
3789 	mock_C_FindObjectsFinal__invalid_handle,
3790 	mock_C_EncryptInit__invalid_handle,
3791 	mock_C_Encrypt__invalid_handle,
3792 	mock_C_EncryptUpdate__invalid_handle,
3793 	mock_C_EncryptFinal__invalid_handle,
3794 	mock_C_DecryptInit__invalid_handle,
3795 	mock_C_Decrypt__invalid_handle,
3796 	mock_C_DecryptUpdate__invalid_handle,
3797 	mock_C_DecryptFinal__invalid_handle,
3798 	mock_C_DigestInit__invalid_handle,
3799 	mock_C_Digest__invalid_handle,
3800 	mock_C_DigestUpdate__invalid_handle,
3801 	mock_C_DigestKey__invalid_handle,
3802 	mock_C_DigestFinal__invalid_handle,
3803 	mock_C_SignInit__invalid_handle,
3804 	mock_C_Sign__invalid_handle,
3805 	mock_C_SignUpdate__invalid_handle,
3806 	mock_C_SignFinal__invalid_handle,
3807 	mock_C_SignRecoverInit__invalid_handle,
3808 	mock_C_SignRecover__invalid_handle,
3809 	mock_C_VerifyInit__invalid_handle,
3810 	mock_C_Verify__invalid_handle,
3811 	mock_C_VerifyUpdate__invalid_handle,
3812 	mock_C_VerifyFinal__invalid_handle,
3813 	mock_C_VerifyRecoverInit__invalid_handle,
3814 	mock_C_VerifyRecover__invalid_handle,
3815 	mock_C_DigestEncryptUpdate__invalid_handle,
3816 	mock_C_DecryptDigestUpdate__invalid_handle,
3817 	mock_C_SignEncryptUpdate__invalid_handle,
3818 	mock_C_DecryptVerifyUpdate__invalid_handle,
3819 	mock_C_GenerateKey__invalid_handle,
3820 	mock_C_GenerateKeyPair__invalid_handle,
3821 	mock_C_WrapKey__invalid_handle,
3822 	mock_C_UnwrapKey__invalid_handle,
3823 	mock_C_DeriveKey__invalid_handle,
3824 	mock_C_SeedRandom__invalid_handle,
3825 	mock_C_GenerateRandom__invalid_handle,
3826 	mock_C_GetFunctionStatus__not_parallel,
3827 	mock_C_CancelFunction__not_parallel,
3828 	mock_C_WaitForSlotEvent__no_event,
3829 };
3830 
3831 CK_X_FUNCTION_LIST mock_x_module_no_slots = {
3832 	{ CRYPTOKI_VERSION_MAJOR, CRYPTOKI_VERSION_MINOR },  /* version */
3833 	mock_X_Initialize,
3834 	mock_X_Finalize,
3835 	mock_X_GetInfo,
3836 	mock_X_GetSlotList__no_tokens,
3837 	mock_X_GetSlotInfo__invalid_slotid,
3838 	mock_X_GetTokenInfo__invalid_slotid,
3839 	mock_X_GetMechanismList__invalid_slotid,
3840 	mock_X_GetMechanismInfo__invalid_slotid,
3841 	mock_X_InitToken__invalid_slotid,
3842 	mock_X_InitPIN__invalid_handle,
3843 	mock_X_SetPIN__invalid_handle,
3844 	mock_X_OpenSession__invalid_slotid,
3845 	mock_X_CloseSession__invalid_handle,
3846 	mock_X_CloseAllSessions__invalid_slotid,
3847 	mock_X_GetSessionInfo__invalid_handle,
3848 	mock_X_GetOperationState__invalid_handle,
3849 	mock_X_SetOperationState__invalid_handle,
3850 	mock_X_Login__invalid_handle,
3851 	mock_X_Logout__invalid_handle,
3852 	mock_X_CreateObject__invalid_handle,
3853 	mock_X_CopyObject__invalid_handle,
3854 	mock_X_DestroyObject__invalid_handle,
3855 	mock_X_GetObjectSize__invalid_handle,
3856 	mock_X_GetAttributeValue__invalid_handle,
3857 	mock_X_SetAttributeValue__invalid_handle,
3858 	mock_X_FindObjectsInit__invalid_handle,
3859 	mock_X_FindObjects__invalid_handle,
3860 	mock_X_FindObjectsFinal__invalid_handle,
3861 	mock_X_EncryptInit__invalid_handle,
3862 	mock_X_Encrypt__invalid_handle,
3863 	mock_X_EncryptUpdate__invalid_handle,
3864 	mock_X_EncryptFinal__invalid_handle,
3865 	mock_X_DecryptInit__invalid_handle,
3866 	mock_X_Decrypt__invalid_handle,
3867 	mock_X_DecryptUpdate__invalid_handle,
3868 	mock_X_DecryptFinal__invalid_handle,
3869 	mock_X_DigestInit__invalid_handle,
3870 	mock_X_Digest__invalid_handle,
3871 	mock_X_DigestUpdate__invalid_handle,
3872 	mock_X_DigestKey__invalid_handle,
3873 	mock_X_DigestFinal__invalid_handle,
3874 	mock_X_SignInit__invalid_handle,
3875 	mock_X_Sign__invalid_handle,
3876 	mock_X_SignUpdate__invalid_handle,
3877 	mock_X_SignFinal__invalid_handle,
3878 	mock_X_SignRecoverInit__invalid_handle,
3879 	mock_X_SignRecover__invalid_handle,
3880 	mock_X_VerifyInit__invalid_handle,
3881 	mock_X_Verify__invalid_handle,
3882 	mock_X_VerifyUpdate__invalid_handle,
3883 	mock_X_VerifyFinal__invalid_handle,
3884 	mock_X_VerifyRecoverInit__invalid_handle,
3885 	mock_X_VerifyRecover__invalid_handle,
3886 	mock_X_DigestEncryptUpdate__invalid_handle,
3887 	mock_X_DecryptDigestUpdate__invalid_handle,
3888 	mock_X_SignEncryptUpdate__invalid_handle,
3889 	mock_X_DecryptVerifyUpdate__invalid_handle,
3890 	mock_X_GenerateKey__invalid_handle,
3891 	mock_X_GenerateKeyPair__invalid_handle,
3892 	mock_X_WrapKey__invalid_handle,
3893 	mock_X_UnwrapKey__invalid_handle,
3894 	mock_X_DeriveKey__invalid_handle,
3895 	mock_X_SeedRandom__invalid_handle,
3896 	mock_X_GenerateRandom__invalid_handle,
3897 	mock_X_WaitForSlotEvent__no_event,
3898 };
3899 
3900 CK_FUNCTION_LIST mock_module = {
3901 	{ CRYPTOKI_VERSION_MAJOR, CRYPTOKI_VERSION_MINOR },  /* version */
3902 	mock_C_Initialize,
3903 	mock_C_Finalize,
3904 	mock_C_GetInfo,
3905 	mock_C_GetFunctionList_not_supported,
3906 	mock_C_GetSlotList,
3907 	mock_C_GetSlotInfo,
3908 	mock_C_GetTokenInfo,
3909 	mock_C_GetMechanismList,
3910 	mock_C_GetMechanismInfo,
3911 	mock_C_InitToken__specific_args,
3912 	mock_C_InitPIN__specific_args,
3913 	mock_C_SetPIN__specific_args,
3914 	mock_C_OpenSession,
3915 	mock_C_CloseSession,
3916 	mock_C_CloseAllSessions,
3917 	mock_C_GetSessionInfo,
3918 	mock_C_GetOperationState,
3919 	mock_C_SetOperationState,
3920 	mock_C_Login,
3921 	mock_C_Logout,
3922 	mock_C_CreateObject,
3923 	mock_C_CopyObject,
3924 	mock_C_DestroyObject,
3925 	mock_C_GetObjectSize,
3926 	mock_C_GetAttributeValue,
3927 	mock_C_SetAttributeValue,
3928 	mock_C_FindObjectsInit,
3929 	mock_C_FindObjects,
3930 	mock_C_FindObjectsFinal,
3931 	mock_C_EncryptInit,
3932 	mock_C_Encrypt,
3933 	mock_C_EncryptUpdate,
3934 	mock_C_EncryptFinal,
3935 	mock_C_DecryptInit,
3936 	mock_C_Decrypt,
3937 	mock_C_DecryptUpdate,
3938 	mock_C_DecryptFinal,
3939 	mock_C_DigestInit,
3940 	mock_C_Digest,
3941 	mock_C_DigestUpdate,
3942 	mock_C_DigestKey,
3943 	mock_C_DigestFinal,
3944 	mock_C_SignInit,
3945 	mock_C_Sign,
3946 	mock_C_SignUpdate,
3947 	mock_C_SignFinal,
3948 	mock_C_SignRecoverInit,
3949 	mock_C_SignRecover,
3950 	mock_C_VerifyInit,
3951 	mock_C_Verify,
3952 	mock_C_VerifyUpdate,
3953 	mock_C_VerifyFinal,
3954 	mock_C_VerifyRecoverInit,
3955 	mock_C_VerifyRecover,
3956 	mock_C_DigestEncryptUpdate,
3957 	mock_C_DecryptDigestUpdate,
3958 	mock_C_SignEncryptUpdate,
3959 	mock_C_DecryptVerifyUpdate,
3960 	mock_C_GenerateKey,
3961 	mock_C_GenerateKeyPair,
3962 	mock_C_WrapKey,
3963 	mock_C_UnwrapKey,
3964 	mock_C_DeriveKey,
3965 	mock_C_SeedRandom,
3966 	mock_C_GenerateRandom,
3967 	mock_C_GetFunctionStatus,
3968 	mock_C_CancelFunction,
3969 	mock_C_WaitForSlotEvent,
3970 };
3971 
3972 void
mock_module_init(void)3973 mock_module_init (void)
3974 {
3975 	static bool initialized = false;
3976 	if (!initialized) {
3977 		p11_mutex_init (&init_mutex);
3978 		initialized = true;
3979 	}
3980 }
3981