Coverage Report

Created: 2023-08-07 06:59

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