-
Notifications
You must be signed in to change notification settings - Fork 72
/
gck-rpc-dispatch.c
2414 lines (1982 loc) · 54.9 KB
/
gck-rpc-dispatch.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* gck-rpc-dispatch.h - receiver of our PKCS#11 protocol.
Copyright (C) 2008, Stef Walter
The Gnome Keyring Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The Gnome Keyring Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the Gnome Library; see the file COPYING.LIB. If not,
write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
Author: Stef Walter <[email protected]>
*/
#include "config.h"
#include "gck-rpc-layer.h"
#include "gck-rpc-private.h"
#include "pkcs11/pkcs11.h"
#include "pkcs11/pkcs11g.h"
#include "pkcs11/pkcs11i.h"
#include <sys/types.h>
#include <sys/param.h>
#ifdef __MINGW32__
# include <winsock2.h>
#else
# include <sys/socket.h>
# include <sys/un.h>
# include <arpa/inet.h>
# include <netinet/in.h>
# include <netinet/tcp.h>
#endif
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
/* Where we dispatch the calls to */
static CK_FUNCTION_LIST_PTR pkcs11_module = NULL;
/* The error returned on protocol failures */
#define PARSE_ERROR CKR_DEVICE_ERROR
#define PREP_ERROR CKR_DEVICE_MEMORY
typedef struct _CallState {
GckRpcMessage *req;
GckRpcMessage *resp;
void *allocated;
uint64_t appid;
int call;
int sock;
} CallState;
typedef struct _DispatchState {
struct _DispatchState *next;
pthread_t thread;
CallState cs;
int socket;
} DispatchState;
/* A linked list of dispatcher threads */
static DispatchState *pkcs11_dispatchers = NULL;
/* A mutex to protect the dispatcher list */
static pthread_mutex_t pkcs11_dispatchers_mutex = PTHREAD_MUTEX_INITIALIZER;
/* -----------------------------------------------------------------------------
* LOGGING and DEBUGGING
*/
#undef DEBUG_OUTPUT
#define DEBUG_OUTPUT 1
#if DEBUG_OUTPUT
#define debug(x) gck_rpc_debug x
#else
#define debug(x)
#endif
#define warning(x) gck_rpc_warn x
#define return_val_if_fail(x, v) \
if (!(x)) { rpc_warn ("'%s' not true at %s", #x, __func__); return v; }
void gck_rpc_log(const char *msg, ...)
{
va_list ap;
va_start(ap, msg);
vfprintf(stdout, msg, ap);
printf("\n");
va_end(ap);
}
/* -------------------------------------------------------------------------------
* CALL STRUCTURES
*/
static int call_init(CallState * cs)
{
assert(cs);
cs->req = gck_rpc_message_new((EggBufferAllocator) realloc);
cs->resp = gck_rpc_message_new((EggBufferAllocator) realloc);
if (!cs->req || !cs->resp) {
gck_rpc_message_free(cs->req);
gck_rpc_message_free(cs->resp);
return 0;
}
cs->allocated = NULL;
return 1;
}
static void *call_alloc(CallState * cs, size_t length)
{
void **data;
assert(cs);
if (length > 0x7fffffff)
return NULL;
data = malloc(sizeof(void *) + length);
if (!data)
return NULL;
/* Munch up the memory to help catch bugs */
memset(data, 0xff, sizeof(void *) + length);
/* Store pointer to next allocated block at beginning */
*data = cs->allocated;
cs->allocated = data;
/* Data starts after first pointer */
return (void *)(data + 1);
}
static void call_reset(CallState * cs)
{
void *allocated;
void **data;
assert(cs);
allocated = cs->allocated;
while (allocated) {
data = (void **)allocated;
/* Pointer to the next allocation */
allocated = *data;
free(data);
}
cs->allocated = NULL;
gck_rpc_message_reset(cs->req);
gck_rpc_message_reset(cs->resp);
}
static void call_uninit(CallState * cs)
{
assert(cs);
call_reset(cs);
gck_rpc_message_free(cs->req);
gck_rpc_message_free(cs->resp);
}
/* -------------------------------------------------------------------
* PROTOCOL CODE
*/
static CK_RV
proto_read_byte_buffer(CallState * cs, CK_BYTE_PTR * buffer,
CK_ULONG * n_buffer)
{
GckRpcMessage *msg;
uint32_t length;
assert(cs);
assert(buffer);
assert(n_buffer);
msg = cs->req;
/* Check that we're supposed to be reading this at this point */
assert(!msg->signature || gck_rpc_message_verify_part(msg, "fy"));
/* The number of ulongs there's room for on the other end */
if (!egg_buffer_get_uint32
(&msg->buffer, msg->parsed, &msg->parsed, &length))
return PARSE_ERROR;
*n_buffer = length;
*buffer = NULL;
/* If set to zero, then they just want the length */
if (!length)
return CKR_OK;
*buffer = call_alloc(cs, length * sizeof(CK_BYTE));
if (!*buffer)
return CKR_DEVICE_MEMORY;
return CKR_OK;
}
static CK_RV
proto_read_byte_array(CallState * cs, CK_BYTE_PTR * array, CK_ULONG * n_array)
{
GckRpcMessage *msg;
const unsigned char *data;
unsigned char valid;
size_t n_data;
assert(cs);
msg = cs->req;
/* Check that we're supposed to have this at this point */
assert(!msg->signature || gck_rpc_message_verify_part(msg, "ay"));
/* Read out the byte which says whether data is present or not */
if (!egg_buffer_get_byte
(&msg->buffer, msg->parsed, &msg->parsed, &valid))
return PARSE_ERROR;
if (!valid) {
*array = NULL;
*n_array = 0;
return CKR_OK;
}
/* Point our arguments into the buffer */
if (!egg_buffer_get_byte_array(&msg->buffer, msg->parsed, &msg->parsed,
&data, &n_data))
return PARSE_ERROR;
*array = (CK_BYTE_PTR) data;
*n_array = n_data;
return CKR_OK;
}
static CK_RV
proto_write_byte_array(CallState * cs, CK_BYTE_PTR array, CK_ULONG len,
CK_RV ret)
{
assert(cs);
/*
* When returning an byte array, in many cases we need to pass
* an invalid array along with a length, which signifies CKR_BUFFER_TOO_SMALL.
*/
switch (ret) {
case CKR_BUFFER_TOO_SMALL:
array = NULL;
/* fall through */
case CKR_OK:
break;
/* Pass all other errors straight through */
default:
return ret;
};
if (!gck_rpc_message_write_byte_array(cs->resp, array, len))
return PREP_ERROR;
return CKR_OK;
}
static CK_RV
proto_read_ulong_buffer(CallState * cs, CK_ULONG_PTR * buffer,
CK_ULONG * n_buffer)
{
GckRpcMessage *msg;
uint32_t length;
assert(cs);
assert(buffer);
assert(n_buffer);
msg = cs->req;
/* Check that we're supposed to be reading this at this point */
assert(!msg->signature || gck_rpc_message_verify_part(msg, "fu"));
/* The number of ulongs there's room for on the other end */
if (!egg_buffer_get_uint32
(&msg->buffer, msg->parsed, &msg->parsed, &length))
return PARSE_ERROR;
*n_buffer = length;
*buffer = NULL;
/* If set to zero, then they just want the length */
if (!length)
return CKR_OK;
*buffer = call_alloc(cs, length * sizeof(CK_ULONG));
if (!*buffer)
return CKR_DEVICE_MEMORY;
return CKR_OK;
}
static CK_RV
proto_write_ulong_array(CallState * cs, CK_ULONG_PTR array, CK_ULONG len,
CK_RV ret)
{
assert(cs);
/*
* When returning an ulong array, in many cases we need to pass
* an invalid array along with a length, which signifies CKR_BUFFER_TOO_SMALL.
*/
switch (ret) {
case CKR_BUFFER_TOO_SMALL:
array = NULL;
/* fall through */
case CKR_OK:
break;
/* Pass all other errors straight through */
default:
return ret;
};
if (!gck_rpc_message_write_ulong_array(cs->resp, array, len))
return PREP_ERROR;
return CKR_OK;
}
static CK_RV
proto_read_attribute_buffer(CallState * cs, CK_ATTRIBUTE_PTR * result,
CK_ULONG * n_result)
{
CK_ATTRIBUTE_PTR attrs;
GckRpcMessage *msg;
uint32_t n_attrs, i;
uint32_t value;
assert(cs);
assert(result);
assert(n_result);
msg = cs->req;
/* Make sure this is in the rigth order */
assert(!msg->signature || gck_rpc_message_verify_part(msg, "fA"));
/* Read the number of attributes */
if (!egg_buffer_get_uint32
(&msg->buffer, msg->parsed, &msg->parsed, &n_attrs))
return PARSE_ERROR;
/* Allocate memory for the attribute structures */
attrs = call_alloc(cs, n_attrs * sizeof(CK_ATTRIBUTE));
if (!attrs)
return CKR_DEVICE_MEMORY;
/* Now go through and fill in each one */
for (i = 0; i < n_attrs; ++i) {
/* The attribute type */
if (!egg_buffer_get_uint32
(&msg->buffer, msg->parsed, &msg->parsed, &value))
return PARSE_ERROR;
attrs[i].type = value;
/* The number of bytes to allocate */
if (!egg_buffer_get_uint32
(&msg->buffer, msg->parsed, &msg->parsed, &value))
return PARSE_ERROR;
if (value == 0) {
attrs[i].pValue = NULL;
attrs[i].ulValueLen = 0;
} else {
attrs[i].pValue = call_alloc(cs, value);
if (!attrs[i].pValue)
return CKR_DEVICE_MEMORY;
attrs[i].ulValueLen = value;
}
}
*result = attrs;
*n_result = n_attrs;
return CKR_OK;
}
static CK_RV
proto_read_attribute_array(CallState * cs, CK_ATTRIBUTE_PTR * result,
CK_ULONG * n_result)
{
CK_ATTRIBUTE_PTR attrs;
const unsigned char *data;
unsigned char valid;
GckRpcMessage *msg;
uint32_t n_attrs, i;
uint32_t value;
size_t n_data;
assert(cs);
assert(result);
assert(n_result);
msg = cs->req;
/* Make sure this is in the rigth order */
assert(!msg->signature || gck_rpc_message_verify_part(msg, "aA"));
/* Read the number of attributes */
if (!egg_buffer_get_uint32
(&msg->buffer, msg->parsed, &msg->parsed, &n_attrs))
return PARSE_ERROR;
/* Allocate memory for the attribute structures */
attrs = call_alloc(cs, n_attrs * sizeof(CK_ATTRIBUTE));
if (!attrs)
return CKR_DEVICE_MEMORY;
/* Now go through and fill in each one */
for (i = 0; i < n_attrs; ++i) {
/* The attribute type */
if (!egg_buffer_get_uint32
(&msg->buffer, msg->parsed, &msg->parsed, &value))
return PARSE_ERROR;
attrs[i].type = value;
/* Whether this one is valid or not */
if (!egg_buffer_get_byte
(&msg->buffer, msg->parsed, &msg->parsed, &valid))
return PARSE_ERROR;
if (valid) {
if (!egg_buffer_get_uint32
(&msg->buffer, msg->parsed, &msg->parsed, &value))
return PARSE_ERROR;
if (!egg_buffer_get_byte_array
(&msg->buffer, msg->parsed, &msg->parsed, &data,
&n_data))
return PARSE_ERROR;
if (data != NULL && n_data != value) {
gck_rpc_warn
("attribute length and data do not match");
return PARSE_ERROR;
}
CK_ULONG a;
if (value == sizeof (uint64_t) &&
value != sizeof (CK_ULONG) &&
gck_rpc_has_ulong_parameter(attrs[i].type)) {
value = sizeof (CK_ULONG);
a = *(uint64_t *)data;
*(CK_ULONG *)data = a;
}
attrs[i].pValue = (CK_VOID_PTR) data;
attrs[i].ulValueLen = value;
} else {
attrs[i].pValue = NULL;
attrs[i].ulValueLen = -1;
}
}
*result = attrs;
*n_result = n_attrs;
return CKR_OK;
}
static CK_RV
proto_write_attribute_array(CallState * cs, CK_ATTRIBUTE_PTR array,
CK_ULONG len, CK_RV ret)
{
assert(cs);
/*
* When returning an attribute array, certain errors aren't
* actually real errors, these are passed through to the other
* side along with the attribute array.
*/
switch (ret) {
case CKR_ATTRIBUTE_SENSITIVE:
case CKR_ATTRIBUTE_TYPE_INVALID:
case CKR_BUFFER_TOO_SMALL:
case CKR_OK:
break;
/* Pass all other errors straight through */
default:
return ret;
};
if (!gck_rpc_message_write_attribute_array(cs->resp, array, len) ||
!gck_rpc_message_write_ulong(cs->resp, ret))
return PREP_ERROR;
return CKR_OK;
}
static CK_RV proto_read_null_string(CallState * cs, CK_UTF8CHAR_PTR * val)
{
GckRpcMessage *msg;
const unsigned char *data;
size_t n_data;
assert(cs);
assert(val);
msg = cs->req;
/* Check that we're supposed to have this at this point */
assert(!msg->signature || gck_rpc_message_verify_part(msg, "z"));
if (!egg_buffer_get_byte_array
(&msg->buffer, msg->parsed, &msg->parsed, &data, &n_data))
return PARSE_ERROR;
/* Allocate a block of memory for it */
*val = call_alloc(cs, n_data);
if (!*val)
return CKR_DEVICE_MEMORY;
memcpy(*val, data, n_data);
(*val)[n_data] = 0;
return CKR_OK;
}
static CK_RV proto_read_mechanism(CallState * cs, CK_MECHANISM_PTR mech)
{
GckRpcMessage *msg;
const unsigned char *data;
uint32_t value;
size_t n_data;
assert(cs);
assert(mech);
msg = cs->req;
/* Make sure this is in the right order */
assert(!msg->signature || gck_rpc_message_verify_part(msg, "M"));
/* The mechanism type */
if (!egg_buffer_get_uint32
(&msg->buffer, msg->parsed, &msg->parsed, &value))
return PARSE_ERROR;
/* The mechanism data */
if (!egg_buffer_get_byte_array
(&msg->buffer, msg->parsed, &msg->parsed, &data, &n_data))
return PARSE_ERROR;
mech->mechanism = value;
mech->pParameter = (CK_VOID_PTR) data;
mech->ulParameterLen = n_data;
return CKR_OK;
}
static CK_RV proto_write_info(CallState * cs, CK_INFO_PTR info)
{
GckRpcMessage *msg;
assert(cs);
assert(info);
msg = cs->resp;
if (!gck_rpc_message_write_version(msg, &info->cryptokiVersion) ||
!gck_rpc_message_write_space_string(msg, info->manufacturerID, 32)
|| !gck_rpc_message_write_ulong(msg, info->flags)
|| !gck_rpc_message_write_space_string(msg,
info->libraryDescription, 32)
|| !gck_rpc_message_write_version(msg, &info->libraryVersion))
return PREP_ERROR;
return CKR_OK;
}
static CK_RV proto_write_slot_info(CallState * cs, CK_SLOT_INFO_PTR info)
{
GckRpcMessage *msg;
assert(cs);
assert(info);
msg = cs->resp;
if (!gck_rpc_message_write_space_string(msg, info->slotDescription, 64)
|| !gck_rpc_message_write_space_string(msg, info->manufacturerID,
32)
|| !gck_rpc_message_write_ulong(msg, info->flags)
|| !gck_rpc_message_write_version(msg, &info->hardwareVersion)
|| !gck_rpc_message_write_version(msg, &info->firmwareVersion))
return PREP_ERROR;
return CKR_OK;
}
static CK_RV proto_write_token_info(CallState * cs, CK_TOKEN_INFO_PTR info)
{
GckRpcMessage *msg;
assert(cs);
assert(info);
msg = cs->resp;
if (!gck_rpc_message_write_space_string(msg, info->label, 32) ||
!gck_rpc_message_write_space_string(msg, info->manufacturerID, 32)
|| !gck_rpc_message_write_space_string(msg, info->model, 16)
|| !gck_rpc_message_write_space_string(msg, info->serialNumber, 16)
|| !gck_rpc_message_write_ulong(msg, info->flags)
|| !gck_rpc_message_write_ulong(msg, info->ulMaxSessionCount)
|| !gck_rpc_message_write_ulong(msg, info->ulSessionCount)
|| !gck_rpc_message_write_ulong(msg, info->ulMaxRwSessionCount)
|| !gck_rpc_message_write_ulong(msg, info->ulRwSessionCount)
|| !gck_rpc_message_write_ulong(msg, info->ulMaxPinLen)
|| !gck_rpc_message_write_ulong(msg, info->ulMinPinLen)
|| !gck_rpc_message_write_ulong(msg, info->ulTotalPublicMemory)
|| !gck_rpc_message_write_ulong(msg, info->ulFreePublicMemory)
|| !gck_rpc_message_write_ulong(msg, info->ulTotalPrivateMemory)
|| !gck_rpc_message_write_ulong(msg, info->ulFreePrivateMemory)
|| !gck_rpc_message_write_version(msg, &info->hardwareVersion)
|| !gck_rpc_message_write_version(msg, &info->firmwareVersion)
|| !gck_rpc_message_write_space_string(msg, info->utcTime, 16))
return PREP_ERROR;
return CKR_OK;
}
static CK_RV
proto_write_mechanism_info(CallState * cs, CK_MECHANISM_INFO_PTR info)
{
GckRpcMessage *msg;
assert(cs);
assert(info);
msg = cs->resp;
if (!gck_rpc_message_write_ulong(msg, info->ulMinKeySize) ||
!gck_rpc_message_write_ulong(msg, info->ulMaxKeySize) ||
!gck_rpc_message_write_ulong(msg, info->flags))
return PREP_ERROR;
return CKR_OK;
}
static CK_RV proto_write_session_info(CallState * cs, CK_SESSION_INFO_PTR info)
{
GckRpcMessage *msg;
assert(cs);
assert(info);
msg = cs->resp;
if (!gck_rpc_message_write_ulong(msg, info->slotID) ||
!gck_rpc_message_write_ulong(msg, info->state) ||
!gck_rpc_message_write_ulong(msg, info->flags) ||
!gck_rpc_message_write_ulong(msg, info->ulDeviceError))
return PREP_ERROR;
return CKR_OK;
}
/* -------------------------------------------------------------------
* CALL MACROS
*/
#define BEGIN_CALL(call_id) \
debug ((#call_id ": enter")); \
assert (cs); \
assert (pkcs11_module); \
{ \
CK_ ## call_id _func = pkcs11_module-> call_id; \
CK_RV _ret = CKR_OK; \
if (!_func) { _ret = CKR_GENERAL_ERROR; goto _cleanup; }
#define PROCESS_CALL(args)\
assert (gck_rpc_message_is_verified (cs->req)); \
_ret = _func args
#define END_CALL \
_cleanup: \
debug (("ret: %d", _ret)); \
return _ret; \
}
#define IN_BYTE(val) \
if (!gck_rpc_message_read_byte (cs->req, &val)) \
{ _ret = PARSE_ERROR; goto _cleanup; }
#define IN_ULONG(val) \
if (!gck_rpc_message_read_ulong (cs->req, &val)) \
{ _ret = PARSE_ERROR; goto _cleanup; }
#define IN_STRING(val) \
_ret = proto_read_null_string (cs, &val); \
if (_ret != CKR_OK) goto _cleanup;
#define IN_BYTE_BUFFER(buffer, buffer_len) \
_ret = proto_read_byte_buffer (cs, &buffer, &buffer_len); \
if (_ret != CKR_OK) goto _cleanup;
#define IN_BYTE_ARRAY(buffer, buffer_len) \
_ret = proto_read_byte_array (cs, &buffer, &buffer_len); \
if (_ret != CKR_OK) goto _cleanup;
#define IN_ULONG_BUFFER(buffer, buffer_len) \
_ret = proto_read_ulong_buffer (cs, &buffer, &buffer_len); \
if (_ret != CKR_OK) goto _cleanup;
#define IN_ATTRIBUTE_BUFFER(buffer, buffer_len) \
_ret = proto_read_attribute_buffer (cs, &buffer, &buffer_len); \
if (_ret != CKR_OK) goto _cleanup;
#define IN_ATTRIBUTE_ARRAY(attrs, n_attrs) \
_ret = proto_read_attribute_array (cs, &attrs, &n_attrs); \
if (_ret != CKR_OK) goto _cleanup;
#define IN_MECHANISM(mech) \
_ret = proto_read_mechanism (cs, &mech); \
if (_ret != CKR_OK) goto _cleanup;
#define OUT_ULONG(val) \
if (_ret == CKR_OK && !gck_rpc_message_write_ulong (cs->resp, val)) \
_ret = PREP_ERROR;
#define OUT_BYTE_ARRAY(array, len) \
/* Note how we filter return codes */ \
_ret = proto_write_byte_array (cs, array, len, _ret);
#define OUT_ULONG_ARRAY(array, len) \
/* Note how we filter return codes */ \
_ret = proto_write_ulong_array (cs, array, len, _ret);
#define OUT_ATTRIBUTE_ARRAY(array, len) \
/* Note how we filter return codes */ \
_ret = proto_write_attribute_array (cs, array, len, _ret);
#define OUT_INFO(val) \
if (_ret == CKR_OK) \
_ret = proto_write_info (cs, &val);
#define OUT_SLOT_INFO(val) \
if (_ret == CKR_OK) \
_ret = proto_write_slot_info (cs, &val);
#define OUT_TOKEN_INFO(val) \
if (_ret == CKR_OK) \
_ret = proto_write_token_info (cs, &val);
#define OUT_MECHANISM_INFO(val) \
if (_ret == CKR_OK) \
_ret = proto_write_mechanism_info (cs, &val);
#define OUT_SESSION_INFO(val) \
if (_ret == CKR_OK) \
_ret = proto_write_session_info (cs, &val);
/* ---------------------------------------------------------------------------
* DISPATCH SPECIFIC CALLS
*/
static CK_RV rpc_C_Initialize(CallState * cs)
{
CK_BYTE_PTR handshake;
CK_ULONG n_handshake;
CK_RV ret = CKR_OK;
debug(("C_Initialize: enter"));
assert(cs);
assert(pkcs11_module);
ret = proto_read_byte_array(cs, &handshake, &n_handshake);
if (ret == CKR_OK) {
/* Check to make sure the header matches */
if (n_handshake != GCK_RPC_HANDSHAKE_LEN ||
memcmp(handshake, GCK_RPC_HANDSHAKE, n_handshake) != 0) {
gck_rpc_warn
("invalid handshake received from connecting module");
ret = CKR_GENERAL_ERROR;
}
assert(gck_rpc_message_is_verified(cs->req));
}
/*
* We don't actually C_Initialize lower layers. It's assumed
* that they'll already be initialzied by the code that loaded us.
*/
debug(("ret: %d", ret));
return ret;
}
static CK_RV rpc_C_Finalize(CallState * cs)
{
CK_SLOT_ID_PTR slots;
CK_ULONG n_slots, i;
CK_SLOT_ID appartment;
CK_RV ret;
DispatchState *ds, *next;
debug(("C_Finalize: enter"));
assert(cs);
assert(pkcs11_module);
/*
* We don't actually C_Finalize lower layers, since this would finalize
* for all appartments, client applications. Anyway this is done by
* the code that loaded us.
*
* But we do need to cleanup resources used by this client, so instead
* we call C_CloseAllSessions for each appartment for this client.
*/
ret = (pkcs11_module->C_GetSlotList) (TRUE, NULL, &n_slots);
if (ret == CKR_OK) {
slots = calloc(n_slots, sizeof(CK_SLOT_ID));
if (slots == NULL) {
ret = CKR_DEVICE_MEMORY;
} else {
ret =
(pkcs11_module->C_GetSlotList) (TRUE, slots,
&n_slots);
for (i = 0; ret == CKR_OK && i < n_slots; ++i) {
appartment = slots[i];
ret =
(pkcs11_module->
C_CloseAllSessions) (appartment);
}
free(slots);
}
}
/* Make all C_WaitForSlotEvent calls return */
pthread_mutex_lock(&pkcs11_dispatchers_mutex);
for (ds = pkcs11_dispatchers; ds; ds = next) {
CallState *c = &ds->cs;
next = ds->next;
if (c->appid != cs->appid)
continue ;
if (c->sock == cs->sock)
continue ;
if (c->req &&
c->req->call_id == GCK_RPC_CALL_C_WaitForSlotEvent) {
gck_rpc_log("Sending interuption signal to %d\n",
ds->socket);
if (ds->socket)
shutdown(ds->socket, SHUT_RDWR);
//pthread_kill(ds->thread, SIGINT);
}
}
pthread_mutex_unlock(&pkcs11_dispatchers_mutex);
debug(("ret: %d", ret));
return ret;
}
static CK_RV rpc_C_GetInfo(CallState * cs)
{
CK_INFO info;
BEGIN_CALL(C_GetInfo);
PROCESS_CALL((&info));
OUT_INFO(info);
END_CALL;
}
static CK_RV rpc_C_GetSlotList(CallState * cs)
{
CK_BBOOL token_present;
CK_SLOT_ID_PTR slot_list;
CK_ULONG count;
BEGIN_CALL(C_GetSlotList);
IN_BYTE(token_present);
IN_ULONG_BUFFER(slot_list, count);
PROCESS_CALL((token_present, slot_list, &count));
OUT_ULONG_ARRAY(slot_list, count);
END_CALL;
}
static CK_RV rpc_C_GetSlotInfo(CallState * cs)
{
CK_SLOT_ID slot_id;
CK_SLOT_INFO info;
/* Slot id becomes appartment so lower layers can tell clients apart. */
BEGIN_CALL(C_GetSlotInfo);
IN_ULONG(slot_id);
PROCESS_CALL((slot_id, &info));
OUT_SLOT_INFO(info);
END_CALL;
}
static CK_RV rpc_C_GetTokenInfo(CallState * cs)
{
CK_SLOT_ID slot_id;
CK_TOKEN_INFO info;
/* Slot id becomes appartment so lower layers can tell clients apart. */
BEGIN_CALL(C_GetTokenInfo);
IN_ULONG(slot_id);
PROCESS_CALL((slot_id, &info));
OUT_TOKEN_INFO(info);
END_CALL;
}
static CK_RV rpc_C_GetMechanismList(CallState * cs)
{
CK_SLOT_ID slot_id;
CK_MECHANISM_TYPE_PTR mechanism_list;
CK_ULONG count;
/* Slot id becomes appartment so lower layers can tell clients apart. */
BEGIN_CALL(C_GetMechanismList);
IN_ULONG(slot_id);
IN_ULONG_BUFFER(mechanism_list, count);
PROCESS_CALL((slot_id, mechanism_list, &count));
OUT_ULONG_ARRAY(mechanism_list, count);
END_CALL;
}
static CK_RV rpc_C_GetMechanismInfo(CallState * cs)
{
CK_SLOT_ID slot_id;
CK_MECHANISM_TYPE type;
CK_MECHANISM_INFO info;
/* Slot id becomes appartment so lower layers can tell clients apart. */
BEGIN_CALL(C_GetMechanismInfo);
IN_ULONG(slot_id);
IN_ULONG(type);
PROCESS_CALL((slot_id, type, &info));
OUT_MECHANISM_INFO(info);
END_CALL;
}
static CK_RV rpc_C_InitToken(CallState * cs)
{
CK_SLOT_ID slot_id;
CK_UTF8CHAR_PTR pin;
CK_ULONG pin_len;
CK_UTF8CHAR_PTR label;
/* Slot id becomes appartment so lower layers can tell clients apart. */
BEGIN_CALL(C_InitToken);
IN_ULONG(slot_id);
IN_BYTE_ARRAY(pin, pin_len);
IN_STRING(label);
PROCESS_CALL((slot_id, pin, pin_len, label));
END_CALL;
}
static CK_RV rpc_C_WaitForSlotEvent(CallState * cs)
{
CK_FLAGS flags;
CK_SLOT_ID slot_id;
/* Get slot id from appartment lower layers use. */