forked from caml-pkcs11/caml-crush
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpkcs11_functions.c
3282 lines (2822 loc) · 88.2 KB
/
pkcs11_functions.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
/*------------------------ MIT License HEADER ------------------------------------
Copyright ANSSI (2013-2015)
Contributors : Ryad BENADJILA [[email protected]],
Thomas CALDERON [[email protected]]
Marion DAUBIGNARD [[email protected]]
This software is a computer program whose purpose is to implement
a PKCS#11 proxy as well as a PKCS#11 filter with security features
in mind. The project source tree is subdivided in six parts.
There are five main parts:
1] OCaml/C PKCS#11 bindings (using OCaml IDL).
2] XDR RPC generators (to be used with ocamlrpcgen and/or rpcgen).
3] A PKCS#11 RPC server (daemon) in OCaml using a Netplex RPC basis.
4] A PKCS#11 filtering module used as a backend to the RPC server.
5] A PKCS#11 client module that comes as a dynamic library offering
the PKCS#11 API to the software.
There is one "optional" part:
6] Tests in C and OCaml to be used with client module 5] or with the
bindings 1]
Here is a big picture of how the PKCS#11 proxy works:
---------------------- -------- socket (TCP or Unix) --------------------
| 3] PKCS#11 RPC server|-|2] RPC |<+++++++++++++++++++> | 5] Client library |
---------------------- | Layer | [SSL/TLS optional] | -------- |
| -------- | |2] RPC | PKCS#11 |
---------------------- | | Layer |functions|
| 4] PKCS#11 filter | | -------- |
---------------------- --------------------
| |
---------------------- |
| 1] PKCS#11 OCaml | { PKCS#11 INTERFACE }
| bindings | |
---------------------- APPLICATION
|
|
{ PKCS#11 INTERFACE }
|
REAL PKCS#11 MIDDLEWARE
(shared library)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
Except as contained in this notice, the name(s) of the above copyright holders
shall not be used in advertising or otherwise to promote the sale, use or other
dealings in this Software without prior written authorization.
The current source code is part of the bindings 1] source tree:
----------------------
| 1] PKCS#11 OCaml |
| bindings |
----------------------
|
|
{ PKCS#11 INTERFACE }
|
REAL PKCS#11 MIDDLEWARE
(shared library)
Project: PKCS#11 Filtering Proxy
File: src/bindings-pkcs11/pkcs11_functions.c
-------------------------- MIT License HEADER ----------------------------------*/
#ifdef WIN32
#include <windows.h>
#include <fcntl.h>
#else
#include <dlfcn.h>
#endif
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
/* For custom allocation and free functions */
extern void *custom_malloc(size_t size);
extern void custom_free(void **to_free);
#include "helpers_pkcs11.h"
#include "pkcs11_functions.h"
#include "pkcs11_aliasing.h"
/* Endianness handling */
unsigned long get_local_arch(void)
{
unsigned long rv;
unsigned int test = 0xAABBCCDD;
if (((unsigned char *)&test)[0] == 0xDD) {
/* LittleEndian */
if (sizeof(long) == 8) {
/* 64bit */
rv = LITTLE_ENDIAN_64;
} else {
rv = LITTLE_ENDIAN_32;
}
} else {
/* BigEndian */
if (sizeof(long) == 8) {
/* 64bit */
rv = BIG_ENDIAN_64;
} else {
rv = BIG_ENDIAN_32;
}
}
return rv;
}
/* Global variable holding the current module handle */
void *module_handle = NULL;
CK_C_GetFunctionList get_func_list;
CK_FUNCTION_LIST *pkcs11 = NULL;
CK_RV ML_CK_C_Daemonize(unsigned char *param, unsigned long param_len)
{
CK_RV rv = 0;
DEBUG_CALL(ML_CK_C_Daemonize, " calling\n");
/* TODO: If you decide so, it is possible to implement some privilege
* reduction primitives here. The advantage of doing it here is that you
* would not need the "sandbox" launcher.
* This is called after the OCaml netplex binds the socket.
*/
/* Dummy stuff below */
if (param != NULL) {
param = NULL;
}
if (param_len != 0) {
param_len = 0;
}
return rv;
}
CK_RV ML_CK_C_SetupArch(unsigned long client_arch)
{
CK_RV rv;
rv = get_local_arch();
/* Let's detect the client_arch to activate the 32 bit code */
switch (client_arch) {
case LITTLE_ENDIAN_64:
case LITTLE_ENDIAN_32:
case BIG_ENDIAN_64:
case BIG_ENDIAN_32:
break;
default:
DEBUG_CALL(ML_CK_C_SetupArch,
" unsupported architecture %ld asked by client\n", client_arch);
rv = UNSUPPORTED_ARCHITECTURE;
}
return rv;
}
/* We load the library */
CK_RV ML_CK_C_LoadModule( /*in */ const char *libname)
{
CK_RV rv;
DEBUG_CALL(ML_CK_C_LoadModule, " calling on %s\n", libname);
#ifdef WIN32
module_handle = LoadLibrary(libname);
#else
module_handle = dlopen(libname, RTLD_NOW);
#endif
if (module_handle == NULL) {
#ifdef DEBUG
printf("ML_CK_C_LoadModule: Failed to dlopen(RTLD_NOW) module %s, trying RTLD_LAZY\n", libname);
#endif
#ifndef WIN32
module_handle = dlopen(libname, RTLD_LAZY);
if (module_handle == NULL) {
#ifdef DEBUG
printf("ML_CK_C_LoadModule: Failed to dlopen(RTLD_LAZY) module %s, giving up\n", libname);
#endif
return CKR_FUNCTION_FAILED;
}
#else
return CKR_FUNCTION_FAILED;
#endif
}
/* Weird allocation for ANSI C compliance */
#ifdef WIN32
*(void **)(&get_func_list) = (CK_C_GetFunctionList)GetProcAddress(module_handle, "C_GetFunctionList");
#else
*(void **)(&get_func_list) = dlsym(module_handle, "C_GetFunctionList");
#endif
if (get_func_list == NULL) {
#ifdef DEBUG
printf
("ML_CK_C_LoadModule: Failed to dlsym C_GetFunctionList in module %s\n",
libname);
#endif
return CKR_FUNCTION_FAILED;
}
/* We've got the pointer, now get all the PKCS11 function pointers inside the module */
rv = get_func_list(&pkcs11);
DEBUG_RET(ML_CK_C_LoadModule, rv, " C_GetFunctionList in module %s\n",
libname);
return rv;
}
CK_RV ML_CK_C_Initialize(void)
{
CK_RV rv;
CHECK_MODULE_FUNCTION_INITIALIZE(C_Initialize);
DEBUG_CALL(ML_CK_C_Initialize, " calling\n");
/* We launch C_Initialize with NULL arguments */
rv = pkcs11->C_Initialize(NULL);
DEBUG_RET(ML_CK_C_Initialize, rv, "\n");
return rv;
}
CK_RV ML_CK_C_Finalize(void)
{
CK_RV rv;
CHECK_MODULE_FUNCTION(C_Finalize);
DEBUG_CALL(ML_CK_C_Finalize, " calling\n");
/* We launch C_Finalize with NULL arguments */
rv = pkcs11->C_Finalize(NULL);
DEBUG_RET(ML_CK_C_Finalize, rv, "\n");
return rv;
}
CK_RV ML_CK_C_GetInfo( /*in */ CK_INFO_PTR info)
{
CK_RV rv;
CHECK_MODULE_FUNCTION(C_GetInfo);
DEBUG_CALL(ML_CK_C_GetInfo, " called\n");
/* Fill the output with default invalid values in case */
/* the PKCS#11 call fails */
if (info != NULL) {
info->cryptokiVersion.major = info->cryptokiVersion.minor = info->flags =
0;
memset(info->manufacturerID, 0, sizeof(info->manufacturerID));
memset(info->libraryDescription, 0, sizeof(info->libraryDescription));
info->libraryVersion.major = info->libraryVersion.minor = 0;
}
rv = pkcs11->C_GetInfo(info);
DEBUG_RET(ML_CK_C_GetInfo, rv, "\n");
return rv;
}
CK_RV ML_CK_C_WaitForSlotEvent( /*in */ CK_FLAGS flags, /* out */
CK_SLOT_ID * pSlot)
{
CK_RV rv;
CHECK_MODULE_FUNCTION(C_WaitForSlotEvent);
DEBUG_CALL(ML_CK_C_WaitForSlotEvent, " called with flags %lx\n", flags);
/* Fill the output with default invalid values in case */
/* the PKCS#11 call fails */
if (pSlot != NULL) {
*pSlot = -1;
}
/* Call real C_WaitForSlotEvent with NULL as third argument since it is reserved
for future versions */
rv = pkcs11->C_WaitForSlotEvent(flags, pSlot, NULL_PTR);
DEBUG_RET(ML_CK_C_WaitForSlotEvent, rv, "\n");
#ifdef USE_ALIASING
/* ALIASING */
if (rv == CKR_OK) {
/* alias the slot ID */
if (pSlot != NULL) {
*pSlot = alias(*pSlot, SLOTID);
}
}
/*------------*/
#endif
return rv;
}
CK_RV ML_CK_C_GetSlotList( /*in */ unsigned int token_present, /*out */
CK_SLOT_ID * slot_list, /*in */ unsigned long count,
/*out */ unsigned long *real_count)
{
CK_RV rv;
CHECK_MODULE_FUNCTION(C_GetSlotList);
/****** Safeguard on input values *************/
/* By design, some input values can't be NULL */
/* (see functions in pkcs11_stubs.c where the */
/* functions here are called) */
/* We however check put a safeguard here */
if(real_count == NULL){
return CKR_GENERAL_ERROR;
}
/**********************************************/
/* Initialize the returned number to zero */
*real_count = 0UL;
/* If the token number is > 255, we give up */
if (token_present > 255) {
rv = CKR_TOKEN_NOT_RECOGNIZED;
DEBUG_RET(ML_CK_C_GetSlotList, rv,
" called with token_present = %u > 255\n", token_present);
return rv;
}
/* Do we want to get the number of slots? */
if (count == 0) {
rv = pkcs11->C_GetSlotList((unsigned char)token_present, NULL_PTR,
real_count);
DEBUG_CALL(ML_CK_C_GetSlotList,
" called for token_present %u with count 0, got %ld slots\n",
token_present, *real_count);
return rv;
}
/* Else, we really want to populate a slot_list */
*real_count = count;
rv = pkcs11->C_GetSlotList((unsigned char)token_present, slot_list,
real_count);
DEBUG_RET(ML_CK_C_GetSlotList, rv,
" token %u with count %ld, got %ld slots\n", token_present,
count, *real_count);
#ifdef USE_ALIASING
/* ALIASING */
if (rv == CKR_OK) {
unsigned int i;
if (slot_list != NULL) {
for (i = 0; i < *real_count; i++) {
/* alias the slot ID */
slot_list[i] = alias(slot_list[i], SLOTID);
}
}
}
/*------------*/
#endif
return rv;
}
CK_RV ML_CK_C_GetSlotInfo( /*in */ CK_SLOT_ID slot_id, /*out */
CK_SLOT_INFO * info)
{
CK_RV rv;
CHECK_MODULE_FUNCTION(C_GetSlotInfo);
#ifdef USE_ALIASING
{/* UNALIASING */
boolean found;
slot_id = unalias(slot_id, SLOTID, &found);
if(found != TRUE){
/* Handle the SLOTID aliasing */
/* list refresh in order to catch possible */
/* new slots or deprecated ones */
refresh_slot_id_list(pkcs11);
/* Second chance */
slot_id = unalias(slot_id, SLOTID, &found);
if(found != TRUE){
rv = CKR_SLOT_ID_INVALID;
return rv;
}
}
/*------------*/}
#endif
DEBUG_CALL(ML_CK_C_GetSlotInfo, " called with slot_id = %ld\n", slot_id);
/* Fill the output with default invalid values in case */
/* the PKCS#11 call fails */
if (info != NULL) {
info->flags = 0;
memset(info->slotDescription, 0, sizeof(info->slotDescription));
memset(info->manufacturerID, 0, sizeof(info->manufacturerID));
info->hardwareVersion.major = info->hardwareVersion.minor = 0;
info->firmwareVersion.major = info->firmwareVersion.minor = 0;
}
rv = pkcs11->C_GetSlotInfo(slot_id, info);
DEBUG_RET(ML_CK_C_GetSlotInfo, rv, " slot_id %ld\n", slot_id);
return rv;
}
CK_RV ML_CK_C_GetTokenInfo( /*in */ CK_SLOT_ID slot_id, /*out */
CK_TOKEN_INFO * info)
{
CK_RV rv;
CHECK_MODULE_FUNCTION(C_GetTokenInfo);
#ifdef USE_ALIASING
{/* UNALIASING */
boolean found;
slot_id = unalias(slot_id, SLOTID, &found);
if(found != TRUE){
/* Handle the SLOTID aliasing */
/* list refresh in order to catch possible */
/* new slots or deprecated ones */
refresh_slot_id_list(pkcs11);
/* Second chance */
slot_id = unalias(slot_id, SLOTID, &found);
if(found != TRUE){
rv = CKR_SLOT_ID_INVALID;
return rv;
}
}
/*------------*/}
#endif
/* Fill the output with default invalid values in case */
/* the PKCS#11 call fails */
if (info != NULL) {
memset(info->label, 0, sizeof(info->label));
memset(info->manufacturerID, 0, sizeof(info->manufacturerID));
memset(info->model, 0, sizeof(info->model));
memset(info->serialNumber, 0, sizeof(info->serialNumber));
info->flags = 0;
info->ulMaxSessionCount = info->ulSessionCount = info->ulMaxRwSessionCount =
info->ulRwSessionCount = info->ulMaxPinLen = info->ulMinPinLen =
info->ulTotalPublicMemory = info->ulFreePublicMemory =
info->ulTotalPrivateMemory = info->ulFreePrivateMemory = 0;
memset(info->utcTime, 0, sizeof(info->utcTime));
info->hardwareVersion.major = info->hardwareVersion.minor = 0;
info->firmwareVersion.major = info->firmwareVersion.minor = 0;
}
DEBUG_CALL(ML_CK_C_GetTokenInfo, " called with slot_id = %ld\n", slot_id);
rv = pkcs11->C_GetTokenInfo(slot_id, info);
DEBUG_RET(ML_CK_C_GetTokenInfo, rv, " slot_id %ld\n", slot_id);
return rv;
}
CK_RV ML_CK_C_OpenSession( /*in */ CK_SLOT_ID slot_id, /*in */ CK_FLAGS flags,
/*out */ CK_SESSION_HANDLE * session)
{
CK_RV rv;
CHECK_MODULE_FUNCTION(C_OpenSession);
#ifdef USE_ALIASING
{/* UNALIASING */
boolean found;
slot_id = unalias(slot_id, SLOTID, &found);
if(found != TRUE){
/* Handle the SLOTID aliasing */
/* list refresh in order to catch possible */
/* new slots or deprecated ones */
refresh_slot_id_list(pkcs11);
/* Second chance */
slot_id = unalias(slot_id, SLOTID, &found);
if(found != TRUE){
rv = CKR_SLOT_ID_INVALID;
return rv;
}
}
/*------------*/}
#endif
DEBUG_CALL(ML_CK_C_OpenSession, " called with slot_id = %ld\n", slot_id);
/* Fill the output with default invalid values in case */
/* the PKCS#11 call fails */
if (session != NULL) {
*session = CK_INVALID_HANDLE;
}
rv = pkcs11->C_OpenSession(slot_id, flags, NULL, NULL, session);
DEBUG_RET(ML_CK_C_OpenSession, rv, " slot_id %ld, session handle %ld\n",
slot_id, *session);
#ifdef USE_ALIASING
/* ALIASING */
if (rv == CKR_OK) {
if (session != NULL) {
*session = alias(*session, SESSION);
}
}
/*------------*/
#endif
return rv;
}
CK_RV ML_CK_C_CloseSession( /*in */ CK_SESSION_HANDLE session)
{
CK_RV rv;
CHECK_MODULE_FUNCTION(C_CloseSession);
#ifdef USE_ALIASING
{/* UNALIASING */
boolean found;
session = unalias(session, SESSION, &found);
if(found != TRUE){
rv = CKR_SESSION_HANDLE_INVALID;
return rv;
}
/*------------*/}
#endif
DEBUG_CALL(ML_CK_C_CloseSession, " called with session = %ld\n", session);
rv = pkcs11->C_CloseSession(session);
DEBUG_RET(ML_CK_C_CloseSession, rv, " session = %ld\n", session);
#ifdef USE_ALIASING
/* If we were OK, we remove the session alias */
if (rv == CKR_OK) {
remove_original(session, SESSION);
}
/*------------*/
#endif
return rv;
}
CK_RV ML_CK_C_CloseAllSessions( /*in */ CK_SLOT_ID slot_id)
{
CK_RV rv;
CHECK_MODULE_FUNCTION(C_CloseAllSessions);
#ifdef USE_ALIASING
{/* UNALIASING */
boolean found;
slot_id = unalias(slot_id, SLOTID, &found);
if(found != TRUE){
/* Handle the SLOTID aliasing */
/* list refresh in order to catch possible */
/* new slots or deprecated ones */
refresh_slot_id_list(pkcs11);
/* Second chance */
slot_id = unalias(slot_id, SLOTID, &found);
if(found != TRUE){
rv = CKR_SLOT_ID_INVALID;
return rv;
}
}
/*------------*/}
#endif
DEBUG_CALL(ML_CK_C_CloseAllSessions, " called with slot_id = %ld\n", slot_id);
rv = pkcs11->C_CloseAllSessions(slot_id);
DEBUG_RET(ML_CK_C_CloseAllSessions, rv, " slot_id = %ld\n", slot_id);
#ifdef USE_ALIASING
/* If we were OK, we remove the session alias */
if (rv == CKR_OK) {
/* We only do this if there is one slot */
if (list_size(SLOTID) == 1) {
destroy_list(OBJECT);
destroy_list(SESSION);
}
}
/*------------*/
#endif
return rv;
}
CK_RV ML_CK_C_GetSessionInfo( /*in */ CK_SESSION_HANDLE session, /*out */
CK_SESSION_INFO * session_info)
{
CK_RV rv;
CHECK_MODULE_FUNCTION(C_GetSessionInfo);
#ifdef USE_ALIASING
{/* UNALIASING */
boolean found;
session = unalias(session, SESSION, &found);
if(found != TRUE){
rv = CKR_SESSION_HANDLE_INVALID;
return rv;
}
/*------------*/}
#endif
DEBUG_CALL(ML_CK_C_GetSessionInfo, " called with session = %ld\n", session);
/* Fill the output with default invalid values in case */
/* the PKCS#11 call fails */
if (session_info != NULL) {
session_info->slotID = -1;
session_info->state = session_info->flags = session_info->ulDeviceError = 0;
}
rv = pkcs11->C_GetSessionInfo(session, session_info);
DEBUG_RET(ML_CK_C_GetSessionInfo, rv, " session %ld\n", session);
#ifdef USE_ALIASING
/* Alias the result inside tje session info structure */
/* ALIASING */
if (rv == CKR_OK) {
if (session_info != NULL) {
session_info->slotID = alias(session_info->slotID, SLOTID);
}
}
/*------------*/
#endif
return rv;
}
CK_RV ML_CK_C_Login( /*in */ CK_SESSION_HANDLE session, /*in */
CK_USER_TYPE user_type, /*in */ unsigned char *pin, /*in */
unsigned long pin_len)
{
CK_RV rv;
CHECK_MODULE_FUNCTION(C_Login);
#ifdef USE_ALIASING
{/* UNALIASING */
boolean found;
session = unalias(session, SESSION, &found);
if(found != TRUE){
rv = CKR_SESSION_HANDLE_INVALID;
return rv;
}
/*------------*/}
#endif
DEBUG_CALL(ML_CK_C_Login, " called with session = %ld, user type %ld\n",
session, user_type);
rv = pkcs11->C_Login(session, user_type, pin, pin_len);
DEBUG_RET(ML_CK_C_Login, rv, " session = %ld, user type %ld\n", session,
user_type);
return rv;
}
CK_RV ML_CK_C_Logout( /*in */ CK_SESSION_HANDLE session)
{
CK_RV rv;
CHECK_MODULE_FUNCTION(C_Logout);
#ifdef USE_ALIASING
{/* UNALIASING */
boolean found;
session = unalias(session, SESSION, &found);
if(found != TRUE){
rv = CKR_SESSION_HANDLE_INVALID;
return rv;
}
/*------------*/}
#endif
DEBUG_CALL(ML_CK_C_Logout, " called with session = %ld\n", session);
rv = pkcs11->C_Logout(session);
DEBUG_RET(ML_CK_C_Logout, rv, " session = %ld\n", session);
return rv;
}
CK_RV ML_CK_C_GetMechanismList( /*in */ CK_SLOT_ID slot_id, /*out */
CK_MECHANISM_TYPE * mechanism_list, /*in */
unsigned long count, /*out */
unsigned long *real_count)
{
CK_RV rv;
unsigned long local_count;
CHECK_MODULE_FUNCTION(C_GetMechanismList);
/****** Safeguard on input values *************/
/* By design, some input values can't be NULL */
/* (see functions in pkcs11_stubs.c where the */
/* functions here are called) */
/* We however check put a safeguard here */
if(real_count == NULL){
return CKR_GENERAL_ERROR;
}
/**********************************************/
#ifdef USE_ALIASING
{/* UNALIASING */
boolean found;
slot_id = unalias(slot_id, SLOTID, &found);
if(found != TRUE){
/* Handle the SLOTID aliasing */
/* list refresh in order to catch possible */
/* new slots or deprecated ones */
refresh_slot_id_list(pkcs11);
/* Second chance */
slot_id = unalias(slot_id, SLOTID, &found);
if(found != TRUE){
rv = CKR_SLOT_ID_INVALID;
return rv;
}
}
/*------------*/}
#endif
/* Initialize the returned number to zero */
*real_count = 0UL;
/* Do we want to get the number of mechanisms? */
if (count == 0) {
rv = pkcs11->C_GetMechanismList(slot_id, NULL, &local_count);
*real_count = local_count;
DEBUG_CALL(ML_CK_C_GetMechanismList,
" called for slot_id %ld with count 0 got %ld mechanisms\n",
slot_id, *real_count);
return rv;
}
/* Else, we really wan to populate a mechanism_list */
*real_count = count;
rv = pkcs11->C_GetMechanismList(slot_id, mechanism_list, real_count);
DEBUG_RET(ML_CK_C_GetMechanismList, rv,
" slot_id %ld with count %ld, got %ld mechanisms\n", slot_id,
count, *real_count);
return rv;
}
CK_RV ML_CK_C_GetMechanismInfo( /*in */ CK_SLOT_ID slot_id, /*in */
CK_MECHANISM_TYPE mechanism, /*out */
CK_MECHANISM_INFO * mechanism_info)
{
CK_RV rv;
CHECK_MODULE_FUNCTION(C_GetMechanismInfo);
#ifdef USE_ALIASING
{/* UNALIASING */
boolean found;
slot_id = unalias(slot_id, SLOTID, &found);
if(found != TRUE){
/* Handle the SLOTID aliasing */
/* list refresh in order to catch possible */
/* new slots or deprecated ones */
refresh_slot_id_list(pkcs11);
/* Second chance */
slot_id = unalias(slot_id, SLOTID, &found);
if(found != TRUE){
rv = CKR_SLOT_ID_INVALID;
return rv;
}
}
/*------------*/}
#endif
DEBUG_CALL(ML_CK_C_GetMechanismInfo,
" called with slot_id = %ld and mech_type %ld\n", slot_id,
mechanism);
/* Fill the output with default invalid values in case */
/* the PKCS#11 call fails */
if (mechanism_info != NULL) {
mechanism_info->ulMinKeySize = mechanism_info->ulMaxKeySize =
mechanism_info->flags = 0;
}
rv = pkcs11->C_GetMechanismInfo(slot_id, mechanism, mechanism_info);
DEBUG_RET(ML_CK_C_GetMechanismInfo, rv, " slot_id %ld and mech_type:%ld\n",
slot_id, mechanism);
return rv;
}
CK_RV ML_CK_C_InitToken( /*in */ CK_SLOT_ID slot_id, /*in */ unsigned char *pin,
/*in */ unsigned long pin_len,
/*in */
unsigned char *label)
{
CK_RV rv;
unsigned char tmp_label[33];
CHECK_MODULE_FUNCTION(C_InitToken);
#ifdef USE_ALIASING
{/* UNALIASING */
boolean found;
slot_id = unalias(slot_id, SLOTID, &found);
if(found != TRUE){
/* Handle the SLOTID aliasing */
/* list refresh in order to catch possible */
/* new slots or deprecated ones */
refresh_slot_id_list(pkcs11);
/* Second chance */
slot_id = unalias(slot_id, SLOTID, &found);
if(found != TRUE){
rv = CKR_SLOT_ID_INVALID;
return rv;
}
}
/*------------*/}
#endif
/* The label must be exactly 32 bytes long max as stated by the PKCS#11 standard */
/* It must be padded with blank chars */
memset(tmp_label, ' ', sizeof(tmp_label));
tmp_label[sizeof(tmp_label) - 1] = 0;
if (strnlen((char *)label, 33) > 32) {
memcpy(tmp_label, label, 32);
} else {
memcpy(tmp_label, label, strnlen((char *)label, 32));
}
DEBUG_CALL(ML_CK_C_InitToken,
" called will with slot_id = %ld, label %s\n", slot_id, tmp_label);
/* If pin_len == 0, spec says we try protected authentication path by passing
a NULL_PTR to function */
if (pin_len == 0) {
/* If CKF_PROTECTED_AUTHENTICATION_PATH is in the token features */
/* lauch it */
CK_TOKEN_INFO token_info;
rv = pkcs11->C_GetTokenInfo(slot_id, &token_info);
if (rv != CKR_OK) {
/* If there was an issue with the C_GetTokenInfo, make a transparent call */
rv = pkcs11->C_InitToken(slot_id, pin, pin_len, tmp_label);
DEBUG_RET(ML_CK_C_InitToken, rv, " slot_id = %ld\n", slot_id);
return rv;
}
if ((token_info.flags & CKF_PROTECTED_AUTHENTICATION_PATH) != 0) {
rv = pkcs11->C_InitToken(slot_id, NULL_PTR, pin_len, tmp_label);
DEBUG_RET(ML_CK_C_InitToken, rv, " slot_id = %ld\n", slot_id);
return rv;
} else {
/* If there is no CKF_PROTECTED_AUTHENTICATION_PATH and the pin_len is null, return */
/* CKR_PIN_INCORRECT */
DEBUG_RET(ML_CK_C_InitToken, CKR_ARGUMENTS_BAD, " slot_id = %ld\n",
slot_id);
return CKR_ARGUMENTS_BAD;
}
}
/* Else, we have a PIN */
rv = pkcs11->C_InitToken(slot_id, pin, pin_len, tmp_label);
DEBUG_RET(ML_CK_C_InitToken, rv, " slot_id = %ld\n", slot_id);
return rv;
}
CK_RV ML_CK_C_InitPIN( /*in */ CK_SESSION_HANDLE session, /*in */
unsigned char *pin, /*in */ unsigned long pin_len)
{
CK_RV rv;
CHECK_MODULE_FUNCTION(C_InitPIN);
#ifdef USE_ALIASING
{/* UNALIASING */
boolean found;
session = unalias(session, SESSION, &found);
if(found != TRUE){
rv = CKR_SESSION_HANDLE_INVALID;
return rv;
}
/*------------*/}
#endif
DEBUG_CALL(ML_CK_C_InitPIN, " called with session = %ld\n", session);
/* If pin_len == 0, spec says we try protected authentication path by passing
a NULL_PTR to function */
if (pin_len == 0) {
/* If CKF_PROTECTED_AUTHENTICATION_PATH is in the token features */
/* lauch it */
/* First, get the slot ID of the current session */
CK_SESSION_INFO session_info;
CK_TOKEN_INFO token_info;
rv = pkcs11->C_GetSessionInfo(session, &session_info);
if (rv != CKR_OK) {
/* If there was an issue with the C_GetSessionInfo, make a transparent call */
rv = pkcs11->C_InitPIN(session, pin, pin_len);
DEBUG_RET(ML_CK_C_InitPIN, rv, " session = %ld\n", session);
return rv;
}
rv = pkcs11->C_GetTokenInfo(session_info.slotID, &token_info);
if (rv != CKR_OK) {
/* If there was an issue with the C_GetTokenInfo, make a transparent call */
rv = pkcs11->C_InitPIN(session, pin, pin_len);
DEBUG_RET(ML_CK_C_InitPIN, rv, " session = %ld\n", session);
return rv;
}
if ((token_info.flags & CKF_PROTECTED_AUTHENTICATION_PATH) != 0) {
rv = pkcs11->C_InitPIN(session, NULL_PTR, pin_len);
DEBUG_RET(ML_CK_C_InitPIN, rv, " session = %ld\n", session);
return rv;
} else {
/* If there is no CKF_PROTECTED_AUTHENTICATION_PATH and the pin_len is null, return */
/* CKR_PIN_INVALID */
DEBUG_RET(ML_CK_C_InitPIN, CKR_PIN_INVALID, " session = %ld\n", session);
return CKR_PIN_INVALID;
}
}
/* Else, we have a PIN */
rv = pkcs11->C_InitPIN(session, pin, pin_len);
DEBUG_RET(ML_CK_C_InitPIN, rv, " session = %ld\n", session);
return rv;
}
CK_RV ML_CK_C_SetPIN( /*in */ CK_SESSION_HANDLE session, /*in */
unsigned char *old_pin, /*in */ unsigned long old_pin_len,
/*in */ unsigned char *new_pin,
/*in */
unsigned long new_pin_len)
{
CK_RV rv;
CHECK_MODULE_FUNCTION(C_SetPIN);
#ifdef USE_ALIASING
{/* UNALIASING */
boolean found;
session = unalias(session, SESSION, &found);
if(found != TRUE){
rv = CKR_SESSION_HANDLE_INVALID;
return rv;
}
/*------------*/}
#endif
DEBUG_CALL(ML_CK_C_SetPIN, " called with session = %ld\n", session);
/* If pin_len == 0, spec says we try protected authentication path by passing
a NULL_PTR to function */
if (old_pin_len == 0 && new_pin_len == 0) {
/* If CKF_PROTECTED_AUTHENTICATION_PATH is in the token features */
/* lauch it */
/* First, get the slot ID of the current session */
CK_SESSION_INFO session_info;
CK_TOKEN_INFO token_info;
rv = pkcs11->C_GetSessionInfo(session, &session_info);
if (rv != CKR_OK) {
/* If there was an issue with the C_GetSessionInfo, make a transparent call */
rv = pkcs11->C_SetPIN(session, old_pin, old_pin_len, new_pin,
new_pin_len);
DEBUG_RET(ML_CK_C_SetPIN, rv, " session = %ld\n", session);
return rv;
}
rv = pkcs11->C_GetTokenInfo(session_info.slotID, &token_info);
if (rv != CKR_OK) {
/* If there was an issue with the C_GetTokenInfo, make a transparent call */
rv = pkcs11->C_SetPIN(session, old_pin, old_pin_len, new_pin,
new_pin_len);
DEBUG_RET(ML_CK_C_SetPIN, rv, " session = %ld\n", session);
return rv;
}
if ((token_info.flags & CKF_PROTECTED_AUTHENTICATION_PATH) != 0) {
rv = pkcs11->C_SetPIN(session, NULL_PTR, old_pin_len, NULL_PTR,
new_pin_len);
DEBUG_RET(ML_CK_C_SetPIN, rv, " session = %ld\n", session);
return rv;
} else {
/* If there is no CKF_PROTECTED_AUTHENTICATION_PATH and the pin_len is null, return */
/* CKR_PIN_INVALID */
DEBUG_RET(ML_CK_C_SetPIN, CKR_PIN_INVALID, " session = %ld\n", session);
return CKR_PIN_INVALID;
}
}
/* Else, we have a PIN */
rv = pkcs11->C_SetPIN(session, old_pin, old_pin_len, new_pin, new_pin_len);
DEBUG_RET(ML_CK_C_SetPIN, rv, " session = %ld\n", session);
return rv;
}
CK_RV ML_CK_C_SeedRandom( /*in */ CK_SESSION_HANDLE session, /*in */
unsigned char *seed, /*in */ unsigned long seed_len)
{
CK_RV rv;
CHECK_MODULE_FUNCTION(C_InitPIN);
#ifdef USE_ALIASING
{/* UNALIASING */
boolean found;
session = unalias(session, SESSION, &found);
if(found != TRUE){
rv = CKR_SESSION_HANDLE_INVALID;
return rv;
}
/*------------*/}
#endif
DEBUG_CALL(ML_CK_C_SeedRandom, " called with session = %ld\n", session);