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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
|
/*
* Copyright (C) 2007 Stef Walter
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include <string.h>
#include <stdarg.h>
#include <stdio.h>
#include "p11-capi.h"
#include "p11-capi-object.h"
#include "p11-capi-session.h"
#include "p11-capi-rsa.h"
#include "p11-capi-token.h"
/* Warns about all the raw string usage in this file */
#pragma warning (disable : 4996)
/* -------------------------------------------------------------------
* GLOBALS / DEFINES
*/
static int cryptoki_initialized = 0;
static HANDLE global_mutex = NULL;
#define MANUFACTURER_ID "Cryptoki CAPI "
#define LIBRARY_DESCRIPTION "Cryptoki CAPI Provider "
#define LIBRARY_VERSION_MAJOR 1
#define LIBRARY_VERSION_MINOR 1
#define HARDWARE_VERSION_MAJOR 0
#define HARDWARE_VERSION_MINOR 0
#define FIRMWARE_VERSION_MAJOR 0
#define FIRMWARE_VERSION_MINOR 0
#define SLOT_TOKEN_SERIAL "1.0 "
#define SLOT_TOKEN_MODEL "1.0 "
#define MAX_PIN_LEN 256
#define MIN_PIN_LEN 1
static CK_MECHANISM_TYPE all_mechanisms[] = {
CKM_RSA_PKCS
};
/* -------------------------------------------------------------------
* MODULE GLOBAL FUNCTIONS
*/
#define LINE 1024
void
p11c_debug(const char* msg, ...)
{
char buf[LINE];
va_list va;
size_t len;
va_start(va, msg);
_vsnprintf(buf, 1024, msg, va);
va_end(va);
buf[LINE - 1] = 0;
len = strlen (buf);
strncpy(buf + len, "\n", 1024 - len);
buf[LINE - 1] = 0;
// OutputDebugStringA(buf);
}
/* Bah humbug, MSVC doesn't have __func__ */
#define ENTER(func) \
char* _func = #func; \
p11c_debug("%s: enter", _func)
#define RETURN(ret) \
return (p11c_debug("%s: %d", _func, ret), ret)
#define PREREQ(cond, ret) \
if (!(cond)) { p11c_debug("%s: %s failed: %d", _func, #cond, ret); return ret; }
void
p11c_lock_global(void)
{
DWORD r;
ASSERT(global_mutex);
r = WaitForSingleObject(global_mutex, INFINITE);
ASSERT(r == WAIT_OBJECT_0);
}
void
p11c_unlock_global(void)
{
BOOL r;
ASSERT(global_mutex);
r = ReleaseMutex(global_mutex);
ASSERT(r);
}
CK_RV
p11c_winerr_to_ckr(DWORD werr)
{
switch(werr)
{
case ERROR_NOT_ENOUGH_MEMORY:
case ERROR_OUTOFMEMORY:
return CKR_HOST_MEMORY;
break;
case NTE_NO_MEMORY:
return CKR_DEVICE_MEMORY;
break;
case ERROR_MORE_DATA:
return CKR_BUFFER_TOO_SMALL;
case ERROR_INVALID_PARAMETER: /* these params were derived from the */
case ERROR_INVALID_HANDLE: /* inputs, so if they are bad, the input */
case NTE_BAD_ALGID: /* data is bad */
case NTE_BAD_HASH:
case NTE_BAD_TYPE:
case NTE_BAD_PUBLIC_KEY:
return CKR_DATA_INVALID;
case ERROR_BUSY:
case NTE_FAIL:
case NTE_BAD_UID:
return CKR_DEVICE_ERROR;
default:
return CKR_GENERAL_ERROR;
};
}
CK_RV
p11c_return_data_raw(CK_VOID_PTR output, CK_ULONG_PTR n_output,
CK_VOID_PTR input, CK_ULONG n_input)
{
ASSERT(n_output);
ASSERT(input);
/* Just asking for the length */
if(!output)
{
*n_output = n_input;
return CKR_OK;
}
/* Buffer is too short */
if(n_input > *n_output)
{
*n_output = n_input;
return CKR_BUFFER_TOO_SMALL;
}
*n_output = n_input;
memcpy(output, input, n_input);
return CKR_OK;
}
CK_RV
p11c_return_data(CK_ATTRIBUTE_PTR attr, CK_VOID_PTR input, DWORD n_input)
{
return p11c_return_data_raw(attr->pValue, &(attr->ulValueLen),
input, n_input);
}
CK_RV
p11c_return_string(CK_ATTRIBUTE_PTR attr, WCHAR* string)
{
CK_UTF8CHAR_PTR buffer;
int result;
SetLastError(0);
/*
* Sadly WideCharToMultiByte doesn't handle zero
* length strings properly. So we have to special
* case this part.
*/
if(!string[0])
return p11c_return_data(attr, "", 0);
/* The length of the string, including null termination */
result = WideCharToMultiByte(CP_UTF8, 0, string, -1,
NULL, 0, NULL, NULL);
if(result)
{
/* Did the caller just want the length? */
if(!attr->pValue)
{
attr->ulValueLen = result - 1;
return CKR_OK;
}
/* Is the callers buffer too short? */
if((int)attr->ulValueLen < result - 1)
{
attr->ulValueLen = result - 1;
return CKR_BUFFER_TOO_SMALL;
}
/*
* Allocate a buffer for the conversion. We have to
* do this because strings in PKCS#11 are not null
* terminated and strings returned from
* WideCharToMultiByte are always null terminated.
*/
buffer = malloc(result);
if(!buffer)
return CKR_HOST_MEMORY;
/* Do the actual conversion */
result = WideCharToMultiByte(CP_UTF8, 0, string, -1,
buffer, result, NULL, NULL);
if(result)
{
attr->ulValueLen = result - 1;
memcpy(attr->pValue, buffer, attr->ulValueLen);
free(buffer);
return CKR_OK;
}
free(buffer);
}
/*
* We should never have too little buffer, or
* get a zero length success code. It's a very
* strange error that arrives here.
*/
return CKR_GENERAL_ERROR;
}
CK_RV
p11c_return_dword_as_bytes(CK_ATTRIBUTE_PTR attr, DWORD value)
{
int i;
CK_ULONG count = 0;
BOOL first = TRUE;
BYTE* at = attr->pValue;
CK_RV ret = CKR_OK;
for(i = 0; i < sizeof(DWORD); i++)
{
BYTE digit = (BYTE)((value >> (((sizeof(DWORD)-1)*8))) & 0xFF);
value = value << 8;
/* No leading zero */
if (first && digit == 0)
continue;
first = FALSE;
if(at)
{
if(count > attr->ulValueLen)
ret = CKR_BUFFER_TOO_SMALL;
else
*(at++) = digit;
}
count++;
}
attr->ulValueLen = count;
return ret;
}
CK_RV
p11c_return_reversed_data(CK_ATTRIBUTE_PTR attr, CK_VOID_PTR data, CK_ULONG length)
{
CK_RV ret = p11c_return_data(attr, data, length);
if(ret != CKR_OK || !attr->pValue)
return ret;
p11c_reverse_memory(attr->pValue, attr->ulValueLen);
return CKR_OK;
}
static void
print_zero_decimal(CK_BYTE_PTR buffer, CK_ULONG length, WORD value)
{
int i;
for(i = (int)length - 1; i >= 0; --i)
{
BYTE digit = value % 10;
buffer[i] = '0' + digit;
value /= 10;
}
}
CK_RV
p11c_return_filetime(CK_ATTRIBUTE_PTR attr, FILETIME *ftime)
{
SYSTEMTIME stime;
CK_DATE* date;
ASSERT(attr);
ASSERT(ftime);
if(!attr->pValue)
{
attr->ulValueLen = sizeof(CK_DATE);
return CKR_OK;
}
if(attr->ulValueLen < sizeof(CK_DATE))
{
attr->ulValueLen = sizeof(CK_DATE);
return CKR_BUFFER_TOO_SMALL;
}
if(!FileTimeToSystemTime(ftime, &stime))
{
DBG(("An invalid FILETIME was encountered"));
return CKR_GENERAL_ERROR;
}
date = (CK_DATE*)attr->pValue;
attr->ulValueLen = sizeof(CK_DATE);
print_zero_decimal(date->year, sizeof(date->year), stime.wYear);
print_zero_decimal(date->month, sizeof(date->month), stime.wMonth);
print_zero_decimal(date->day, sizeof(date->day), stime.wDay);
return CKR_OK;
}
/* ---------------------------------------------------------------- */
static CK_RV
PC_C_Initialize(CK_VOID_PTR init_args)
{
ENTER(C_Initialize);
PREREQ(!cryptoki_initialized, CKR_CRYPTOKI_ALREADY_INITIALIZED);
if (init_args != NULL) {
CK_C_INITIALIZE_ARGS_PTR args;
int supplied_ok;
/* pReserved must be NULL */
args = init_args;
PREREQ(!args->pReserved, CKR_ARGUMENTS_BAD);
/* ALL supplied function pointers need to have the value either NULL or non-NULL. */
supplied_ok = (args->CreateMutex == NULL && args->DestroyMutex == NULL &&
args->LockMutex == NULL && args->UnlockMutex == NULL) ||
(args->CreateMutex != NULL && args->DestroyMutex != NULL &&
args->LockMutex != NULL && args->UnlockMutex != NULL);
PREREQ(supplied_ok, CKR_ARGUMENTS_BAD);
/*
* When the CKF_OS_LOCKING_OK flag isn't set and mutex function pointers are supplied
* by an application, return an error. We must be able to use our own locks.
*/
if(!(args->flags & CKF_OS_LOCKING_OK) && (args->CreateMutex != NULL))
RETURN(CKR_CANT_LOCK);
}
if(!global_mutex)
{
global_mutex = CreateMutex(NULL, FALSE, NULL);
if(!global_mutex)
RETURN(CKR_CANT_LOCK);
}
cryptoki_initialized = 1;
RETURN(CKR_OK);
}
static CK_RV
PC_C_Finalize(CK_VOID_PTR pReserved)
{
ENTER(C_Finalize);
PREREQ(cryptoki_initialized, CKR_CRYPTOKI_NOT_INITIALIZED);
PREREQ(!pReserved, CKR_ARGUMENTS_BAD);
cryptoki_initialized = 0;
p11c_session_cleanup_all();
p11c_token_cleanup_all();
RETURN(CKR_OK);
}
static CK_RV
PC_C_GetInfo(CK_INFO_PTR info)
{
ENTER(C_GetInfo);
PREREQ(cryptoki_initialized, CKR_CRYPTOKI_NOT_INITIALIZED);
PREREQ(info, CKR_ARGUMENTS_BAD);
ASSERT(strlen(MANUFACTURER_ID) == 32);
ASSERT(strlen(LIBRARY_DESCRIPTION) == 32);
info->cryptokiVersion.major = CRYPTOKI_VERSION_MAJOR;
info->cryptokiVersion.minor = CRYPTOKI_VERSION_MINOR;
info->libraryVersion.major = LIBRARY_VERSION_MAJOR;
info->libraryVersion.minor = LIBRARY_VERSION_MINOR;
info->flags = 0;
strncpy((char*)info->manufacturerID, MANUFACTURER_ID, 32);
strncpy((char*)info->libraryDescription, LIBRARY_DESCRIPTION, 32);
RETURN(CKR_OK);
}
static CK_RV
PC_C_GetFunctionList(CK_FUNCTION_LIST_PTR_PTR list)
{
/* This would be a strange call to receive */
return C_GetFunctionList(list);
}
static CK_RV
PC_C_GetSlotList(CK_BBOOL tokenPresent, CK_SLOT_ID_PTR slot_list, CK_ULONG_PTR count)
{
unsigned int n_tokens, i;
ENTER(C_GetSlotList);
PREREQ(cryptoki_initialized, CKR_CRYPTOKI_NOT_INITIALIZED);
PREREQ(count, CKR_ARGUMENTS_BAD);
/* All tokens are always present */
n_tokens = p11c_token_get_count();
/* Application only wants to know the number of slots. */
if(slot_list == NULL)
{
*count = n_tokens;
RETURN(CKR_OK);
}
if(*count < n_tokens)
{
*count = n_tokens;
RETURN(CKR_BUFFER_TOO_SMALL);
}
*count = n_tokens;
for(i = 0; i < n_tokens; ++i)
slot_list[i] = p11c_token_get_slot_id (i);
RETURN(CKR_OK);
}
static CK_RV
PC_C_GetSlotInfo(CK_SLOT_ID id, CK_SLOT_INFO_PTR info)
{
const char* name;
ENTER(C_GetSlotInfo);
PREREQ(cryptoki_initialized, CKR_CRYPTOKI_NOT_INITIALIZED);
PREREQ(info, CKR_ARGUMENTS_BAD);
/* Make sure the slot ID is valid */
if(!p11c_token_is_valid(id))
RETURN(CKR_SLOT_ID_INVALID);
ASSERT(strlen(MANUFACTURER_ID) == 32);
/* Provide information about the slot in the provided buffer */
strncpy((char*)info->manufacturerID, MANUFACTURER_ID, 32);
info->hardwareVersion.major = HARDWARE_VERSION_MAJOR;
info->hardwareVersion.minor = HARDWARE_VERSION_MINOR;
info->firmwareVersion.major = FIRMWARE_VERSION_MAJOR;
info->firmwareVersion.minor = FIRMWARE_VERSION_MINOR;
/* Token is always present */
info->flags = CKF_TOKEN_PRESENT;
/* Slot name is blank padded, odd */
name = p11c_token_get_display_name(id);
memset((char*)info->slotDescription, ' ',
sizeof(info->slotDescription));
memcpy((char*)info->slotDescription, name,
min(strlen(name), sizeof(info->slotDescription)));
RETURN(CKR_OK);
}
static CK_RV
PC_C_GetTokenInfo(CK_SLOT_ID id, CK_TOKEN_INFO_PTR info)
{
const char* name;
ENTER(C_GetTokenInfo);
PREREQ(cryptoki_initialized, CKR_CRYPTOKI_NOT_INITIALIZED);
PREREQ(info, CKR_ARGUMENTS_BAD);
/* Make sure the slot ID is valid */
if(!p11c_token_is_valid(id))
RETURN(CKR_SLOT_ID_INVALID);
ASSERT(strlen(MANUFACTURER_ID) == 32);
ASSERT(strlen(SLOT_TOKEN_MODEL) == 16);
ASSERT(strlen(SLOT_TOKEN_SERIAL) == 16);
/* Provide information about a token in the provided buffer */
strncpy((char*)info->manufacturerID, MANUFACTURER_ID, 32);
strncpy((char*)info->model, SLOT_TOKEN_MODEL, 16);
strncpy((char*)info->serialNumber, SLOT_TOKEN_SERIAL, 16);
/* Protected authentication path: Windows prompts for it's own PINs */
info->flags = CKF_TOKEN_INITIALIZED | CKF_PROTECTED_AUTHENTICATION_PATH;
info->ulMaxSessionCount = CK_EFFECTIVELY_INFINITE;
info->ulSessionCount = CK_EFFECTIVELY_INFINITE;
info->ulMaxRwSessionCount = CK_EFFECTIVELY_INFINITE;
info->ulRwSessionCount = CK_EFFECTIVELY_INFINITE;
info->ulMaxPinLen = MAX_PIN_LEN;
info->ulMinPinLen = MIN_PIN_LEN;
info->ulTotalPublicMemory = CK_UNAVAILABLE_INFORMATION;
info->ulFreePublicMemory = CK_UNAVAILABLE_INFORMATION;
info->ulTotalPrivateMemory = CK_UNAVAILABLE_INFORMATION;
info->ulFreePrivateMemory = CK_UNAVAILABLE_INFORMATION;
info->hardwareVersion.major = HARDWARE_VERSION_MAJOR;
info->hardwareVersion.minor = HARDWARE_VERSION_MINOR;
info->firmwareVersion.major = FIRMWARE_VERSION_MAJOR;
info->firmwareVersion.minor = FIRMWARE_VERSION_MINOR;
memset(info->utcTime, ' ', 16);
/* Slot name is blank padded, odd */
name = p11c_token_get_display_name(id);
memset((char*)info->label, ' ', sizeof(info->label));
memcpy((char*)info->label, name,
min(strlen(name), sizeof(info->label)));
RETURN(CKR_OK);
}
static CK_RV
PC_C_GetMechanismList(CK_SLOT_ID id, CK_MECHANISM_TYPE_PTR mechanism_list,
CK_ULONG_PTR count)
{
CK_ULONG n_mechs;
ENTER(C_GetMechanismList);
PREREQ(cryptoki_initialized, CKR_CRYPTOKI_NOT_INITIALIZED);
PREREQ(count, CKR_ARGUMENTS_BAD);
if(!p11c_token_is_valid(id))
RETURN(CKR_SLOT_ID_INVALID);
n_mechs = sizeof(all_mechanisms) / sizeof(all_mechanisms[0]);
if(mechanism_list == NULL)
{
*count = n_mechs;
RETURN(CKR_OK);
}
if(*count < n_mechs)
{
*count = n_mechs;
RETURN(CKR_BUFFER_TOO_SMALL);
}
memcpy(mechanism_list, all_mechanisms,
n_mechs * sizeof(CK_MECHANISM_TYPE));
*count = n_mechs;
RETURN(CKR_OK);
}
static CK_RV
PC_C_GetMechanismInfo(CK_SLOT_ID id, CK_MECHANISM_TYPE type,
CK_MECHANISM_INFO_PTR info)
{
ENTER(C_GetMechanismInfo);
PREREQ(cryptoki_initialized, CKR_CRYPTOKI_NOT_INITIALIZED);
PREREQ(info, CKR_ARGUMENTS_BAD);
if(!p11c_token_is_valid(id))
RETURN(CKR_SLOT_ID_INVALID);
if(type == CKM_RSA_PKCS)
{
p11c_rsa_pkcs_get_info(type, info);
RETURN(CKR_OK);
}
RETURN(CKR_MECHANISM_INVALID);
}
static CK_RV
PC_C_InitToken(CK_SLOT_ID slot_id, CK_UTF8CHAR_PTR pin, CK_ULONG pin_len,
CK_UTF8CHAR_PTR label)
{
ENTER(C_InitToken);
PREREQ(cryptoki_initialized, CKR_CRYPTOKI_NOT_INITIALIZED);
RETURN(CKR_FUNCTION_NOT_SUPPORTED);
}
static CK_RV
PC_C_WaitForSlotEvent(CK_FLAGS flags, CK_SLOT_ID_PTR pSlot, CK_VOID_PTR pReserved)
{
ENTER(C_WaitForSlotEvent);
PREREQ(cryptoki_initialized, CKR_CRYPTOKI_NOT_INITIALIZED);
/*
* PKCS#11 GRAY AREA: What happens when we know we'll *never*
* have any slot events, and someone calls us without CKR_DONT_BLOCK?
* In case there's a thread dedicated to calling this function in a
* loop, we wait 1 seconds when called without CKR_DONT_BLOCK.
*/
if(!(flags & CKF_DONT_BLOCK))
Sleep(1000);
RETURN(CKR_NO_EVENT);
}
static CK_RV
PC_C_OpenSession(CK_SLOT_ID id, CK_FLAGS flags, CK_VOID_PTR application,
CK_NOTIFY notify, CK_SESSION_HANDLE_PTR session)
{
P11cSession* sess;
CK_RV ret;
ENTER(C_OpenSession);
PREREQ(cryptoki_initialized, CKR_CRYPTOKI_NOT_INITIALIZED);
PREREQ(session, CKR_ARGUMENTS_BAD);
PREREQ(flags & CKF_SERIAL_SESSION, CKR_SESSION_PARALLEL_NOT_SUPPORTED);
if(!p11c_token_is_valid(id))
RETURN(CKR_SLOT_ID_INVALID);
ret = p11c_session_create(id, &sess);
if(ret != CKR_OK)
RETURN(ret);
sess->notify_callback = notify;
sess->user_data = application;
if(flags & CKF_RW_SESSION)
sess->read_write = TRUE;
ret = p11c_session_register(sess);
if(ret == CKR_OK)
{
/* ID should have been assigned when registering */
ASSERT(sess->id > 0);
*session = sess->id;
}
else
{
p11c_session_destroy(sess);
}
RETURN(ret);
}
static CK_RV
PC_C_CloseSession(CK_SESSION_HANDLE session)
{
P11cSession* sess;
CK_RV ret;
ENTER(C_CloseSession);
PREREQ(cryptoki_initialized, CKR_CRYPTOKI_NOT_INITIALIZED);
ret = p11c_session_remove_lock_ref(session, &sess);
if(ret == CKR_OK)
{
/* This will unref and possibly destroy the session */
p11c_session_unref_unlock(sess);
}
RETURN(ret);
}
static CK_RV
PC_C_CloseAllSessions(CK_SLOT_ID id)
{
ENTER(C_CloseAllSession);
PREREQ(cryptoki_initialized, CKR_CRYPTOKI_NOT_INITIALIZED);
if(!p11c_token_is_valid(id))
RETURN(CKR_SLOT_ID_INVALID);
p11c_session_close_all(id);
RETURN(CKR_OK);
}
static CK_RV
PC_C_GetFunctionStatus(CK_SESSION_HANDLE session)
{
ENTER(C_GetFunctionStatus);
PREREQ(cryptoki_initialized, CKR_CRYPTOKI_NOT_INITIALIZED);
RETURN(CKR_FUNCTION_NOT_PARALLEL);
}
static CK_RV
PC_C_CancelFunction(CK_SESSION_HANDLE session)
{
ENTER(C_CancelFunction);
PREREQ(cryptoki_initialized, CKR_CRYPTOKI_NOT_INITIALIZED);
RETURN(CKR_FUNCTION_NOT_PARALLEL);
}
static CK_RV
PC_C_GetSessionInfo(CK_SESSION_HANDLE session, CK_SESSION_INFO_PTR info)
{
P11cSession* sess;
CK_RV ret;
ENTER(C_GetSessionInfo);
PREREQ(cryptoki_initialized, CKR_CRYPTOKI_NOT_INITIALIZED);
PREREQ(info, CKR_ARGUMENTS_BAD);
ret = p11c_session_get_lock_ref(session, FALSE, &sess);
if(ret == CKR_OK)
{
p11c_session_get_info(sess, info);
p11c_session_unref_unlock(sess);
}
RETURN(ret);
}
static CK_RV
PC_C_InitPIN(CK_SESSION_HANDLE session, CK_UTF8CHAR_PTR pin,
CK_ULONG pin_len)
{
ENTER(C_InitPIN);
PREREQ(cryptoki_initialized, CKR_CRYPTOKI_NOT_INITIALIZED);
/* We don't support this stuff. We don't support 'SO' logins. */
RETURN(CKR_USER_NOT_LOGGED_IN);
}
static CK_RV
PC_C_SetPIN(CK_SESSION_HANDLE session, CK_UTF8CHAR_PTR old_pin,
CK_ULONG old_len, CK_UTF8CHAR_PTR new_pin, CK_ULONG new_len)
{
ENTER(C_SetPIN);
PREREQ(cryptoki_initialized, CKR_CRYPTOKI_NOT_INITIALIZED);
/* Not supported, Windows takes care of this */
RETURN(CKR_FUNCTION_NOT_SUPPORTED);
}
static CK_RV
PC_C_GetOperationState(CK_SESSION_HANDLE session, CK_BYTE_PTR operation_state,
CK_ULONG_PTR operation_state_len)
{
ENTER(C_GetOperationState);
PREREQ(cryptoki_initialized, CKR_CRYPTOKI_NOT_INITIALIZED);
/* Nasty, no sirrr */
RETURN(CKR_FUNCTION_NOT_SUPPORTED);
}
static CK_RV
PC_C_SetOperationState(CK_SESSION_HANDLE session, CK_BYTE_PTR operation_state,
CK_ULONG operation_state_len, CK_OBJECT_HANDLE encryption_key,
CK_OBJECT_HANDLE authentication_key)
{
ENTER(C_SetOperationState);
PREREQ(cryptoki_initialized, CKR_CRYPTOKI_NOT_INITIALIZED);
/* Nasty, no sirrr */
RETURN(CKR_FUNCTION_NOT_SUPPORTED);
}
static CK_RV
PC_C_Login(CK_SESSION_HANDLE session, CK_USER_TYPE user_type,
CK_UTF8CHAR_PTR pin, CK_ULONG pin_len)
{
P11cSession* sess;
CK_RV ret;
ENTER(C_Login);
PREREQ(cryptoki_initialized, CKR_CRYPTOKI_NOT_INITIALIZED);
ret = p11c_session_get_lock_ref(session, FALSE, &sess);
if(ret == CKR_OK)
{
switch(user_type)
{
case CKU_USER:
ret = p11c_token_login(sess->slot);
break;
case CKU_SO:
ret = CKR_USER_TYPE_INVALID;
break;
default:
ret = CKR_USER_TYPE_INVALID;
break;
}
p11c_session_unref_unlock(sess);
}
RETURN(ret);
}
static CK_RV
PC_C_Logout(CK_SESSION_HANDLE session)
{
P11cSession* sess;
CK_RV ret;
ENTER(C_Logout);
PREREQ(cryptoki_initialized, CKR_CRYPTOKI_NOT_INITIALIZED);
ret = p11c_session_get_lock_ref(session, FALSE, &sess);
if(ret == CKR_OK)
{
ret = p11c_token_logout(sess->slot);
p11c_session_unref_unlock(sess);
}
RETURN(ret);
}
static CK_RV
PC_C_CreateObject(CK_SESSION_HANDLE session, CK_ATTRIBUTE_PTR templ,
CK_ULONG count, CK_OBJECT_HANDLE_PTR object)
{
ENTER(C_CreateObject);
PREREQ(cryptoki_initialized, CKR_CRYPTOKI_NOT_INITIALIZED);
/* TODO: See if we need to support this */
RETURN(CKR_FUNCTION_NOT_SUPPORTED);
}
static CK_RV
PC_C_CopyObject(CK_SESSION_HANDLE session, CK_OBJECT_HANDLE object,
CK_ATTRIBUTE_PTR templ, CK_ULONG count,
CK_OBJECT_HANDLE_PTR new_object)
{
ENTER(C_CopyObject);
PREREQ(cryptoki_initialized, CKR_CRYPTOKI_NOT_INITIALIZED);
/* TODO: See if we need to support this */
RETURN(CKR_FUNCTION_NOT_SUPPORTED);
}
static CK_RV
PC_C_DestroyObject(CK_SESSION_HANDLE session, CK_OBJECT_HANDLE object)
{
ENTER(C_DestroyObject);
PREREQ(cryptoki_initialized, CKR_CRYPTOKI_NOT_INITIALIZED);
/* TODO: See if we need to support this */
RETURN(CKR_FUNCTION_NOT_SUPPORTED);
}
static CK_RV
PC_C_GetObjectSize(CK_SESSION_HANDLE session, CK_OBJECT_HANDLE object,
CK_ULONG_PTR size)
{
ENTER(C_GetObjectSize);
PREREQ(cryptoki_initialized, CKR_CRYPTOKI_NOT_INITIALIZED);
/* TODO: Implement this */
RETURN(CKR_FUNCTION_NOT_SUPPORTED);
}
static CK_RV
PC_C_GetAttributeValue(CK_SESSION_HANDLE session, CK_OBJECT_HANDLE object,
CK_ATTRIBUTE_PTR templ, CK_ULONG count)
{
P11cSession* sess;
P11cObjectData* objdata;
CK_RV ret;
ENTER(C_GetAttributeValue);
PREREQ(cryptoki_initialized, CKR_CRYPTOKI_NOT_INITIALIZED);
PREREQ(object, CKR_OBJECT_HANDLE_INVALID);
PREREQ(!count || templ, CKR_ARGUMENTS_BAD);
ret = p11c_session_get_lock_ref(session, FALSE, &sess);
if(ret == CKR_OK)
{
ret = p11c_session_get_object_data_for(sess, object, &objdata);
if(ret == CKR_OK)
ret = p11c_object_data_get_attrs(objdata, templ, count);
p11c_session_unref_unlock(sess);
}
RETURN(ret);
}
static CK_RV
PC_C_SetAttributeValue(CK_SESSION_HANDLE session, CK_OBJECT_HANDLE object,
CK_ATTRIBUTE_PTR templ, CK_ULONG count)
{
ENTER(C_SetAttributeValue);
PREREQ(cryptoki_initialized, CKR_CRYPTOKI_NOT_INITIALIZED);
/* TODO: See if we need to implement this */
RETURN(CKR_FUNCTION_NOT_SUPPORTED);
}
static CK_RV
PC_C_FindObjectsInit(CK_SESSION_HANDLE session, CK_ATTRIBUTE_PTR templ,
CK_ULONG count)
{
P11cSession* sess;
CK_RV ret;
ENTER(C_FindObjectsInit);
PREREQ(cryptoki_initialized, CKR_CRYPTOKI_NOT_INITIALIZED);
PREREQ(!count || templ, CKR_ARGUMENTS_BAD);
ret = p11c_session_get_lock_ref(session, FALSE, &sess);
if(ret == CKR_OK)
{
ret = p11c_session_find_init(sess, templ, count);
p11c_session_unref_unlock(sess);
}
RETURN(ret);
}
static CK_RV
PC_C_FindObjects(CK_SESSION_HANDLE session, CK_OBJECT_HANDLE_PTR objects,
CK_ULONG max_object_count, CK_ULONG_PTR object_count)
{
P11cSession* sess;
CK_RV ret;
ENTER(C_FindObjects);
PREREQ(cryptoki_initialized, CKR_CRYPTOKI_NOT_INITIALIZED);
PREREQ(object_count, CKR_ARGUMENTS_BAD);
PREREQ(!max_object_count || objects, CKR_ARGUMENTS_BAD);
ret = p11c_session_get_lock_ref(session, FALSE, &sess);
if(ret == CKR_OK)
{
ret = p11c_session_find(sess, objects, max_object_count, object_count);
p11c_session_unref_unlock(sess);
}
RETURN(ret);
}
static CK_RV
PC_C_FindObjectsFinal(CK_SESSION_HANDLE session)
{
P11cSession* sess;
CK_RV ret;
ENTER(C_FindObjectsFinal);
PREREQ(cryptoki_initialized, CKR_CRYPTOKI_NOT_INITIALIZED);
ret = p11c_session_get_lock_ref(session, FALSE, &sess);
if(ret == CKR_OK)
{
ret = p11c_session_find_final(sess);
p11c_session_unref_unlock(sess);
}
RETURN(ret);
}
static CK_RV
PC_C_EncryptInit(CK_SESSION_HANDLE session, CK_MECHANISM_PTR mechanism,
CK_OBJECT_HANDLE key)
{
ENTER(C_EncryptInit);
PREREQ(cryptoki_initialized, CKR_CRYPTOKI_NOT_INITIALIZED);
/* TODO: See if we need to implement this */
RETURN(CKR_FUNCTION_NOT_SUPPORTED);
}
static CK_RV
PC_C_Encrypt(CK_SESSION_HANDLE session, CK_BYTE_PTR data, CK_ULONG data_len,
CK_BYTE_PTR encrypted_data, CK_ULONG_PTR encrypted_data_len)
{
ENTER(C_Encrypt);
PREREQ(cryptoki_initialized, CKR_CRYPTOKI_NOT_INITIALIZED);
/* TODO: See if we need to implement this */
RETURN(CKR_FUNCTION_NOT_SUPPORTED);
}
static CK_RV
PC_C_EncryptUpdate(CK_SESSION_HANDLE session, CK_BYTE_PTR part,
CK_ULONG part_len, CK_BYTE_PTR encrypted_part,
CK_ULONG_PTR encrypted_part_len)
{
ENTER(C_EncryptUpdate);
PREREQ(cryptoki_initialized, CKR_CRYPTOKI_NOT_INITIALIZED);
/* RSA/DSA mechs don't support incremental crypto operations. */
RETURN(CKR_FUNCTION_NOT_SUPPORTED);
}
static CK_RV
PC_C_EncryptFinal(CK_SESSION_HANDLE session, CK_BYTE_PTR last_encrypted_part,
CK_ULONG_PTR last_encrypted_part_len)
{
ENTER(C_EncryptFinal);
PREREQ(cryptoki_initialized, CKR_CRYPTOKI_NOT_INITIALIZED);
/* RSA/DSA mechs don't support incremental crypto operations. */
RETURN(CKR_FUNCTION_NOT_SUPPORTED);
}
static CK_RV
PC_C_DecryptInit(CK_SESSION_HANDLE session, CK_MECHANISM_PTR mechanism,
CK_OBJECT_HANDLE key)
{
P11cObjectData* objdata;
P11cSession* sess;
CK_RV ret;
ENTER(C_DecryptInit);
PREREQ(cryptoki_initialized, CKR_CRYPTOKI_NOT_INITIALIZED);
PREREQ(mechanism, CKR_ARGUMENTS_BAD);
PREREQ(key, CKR_ARGUMENTS_BAD);
ret = p11c_session_get_lock_ref(session, FALSE, &sess);
if(ret == CKR_OK)
{
ret = p11c_session_get_object_data_for(sess, key, &objdata);
if(ret == CKR_OK)
ret = p11c_session_decrypt_init(sess, mechanism, objdata);
p11c_session_unref_unlock(sess);
}
RETURN(ret);
}
static CK_RV
PC_C_Decrypt(CK_SESSION_HANDLE session, CK_BYTE_PTR encrypted_data,
CK_ULONG encrypted_data_len, CK_BYTE_PTR data, CK_ULONG_PTR data_len)
{
P11cSession* sess;
CK_RV ret;
ENTER(C_Decrypt);
PREREQ(cryptoki_initialized, CKR_CRYPTOKI_NOT_INITIALIZED);
PREREQ(encrypted_data, CKR_ARGUMENTS_BAD);
PREREQ(encrypted_data_len, CKR_ARGUMENTS_BAD);
ret = p11c_session_get_lock_ref(session, FALSE, &sess);
if(ret == CKR_OK)
{
ret = p11c_session_decrypt(sess, encrypted_data, encrypted_data_len,
data, data_len);
p11c_session_unref_unlock(sess);
}
RETURN(ret);
}
static CK_RV
PC_C_DecryptUpdate(CK_SESSION_HANDLE session, CK_BYTE_PTR encrypted_part,
CK_ULONG encrypted_part_len, CK_BYTE_PTR part, CK_ULONG_PTR part_len)
{
ENTER(C_DecryptUpdate);
PREREQ(cryptoki_initialized, CKR_CRYPTOKI_NOT_INITIALIZED);
/* RSA/DSA mechs don't support incremental crypto operations. */
RETURN(CKR_FUNCTION_NOT_SUPPORTED);
}
static CK_RV
PC_C_DecryptFinal(CK_SESSION_HANDLE session, CK_BYTE_PTR pLastPart,
CK_ULONG_PTR last_part_len)
{
ENTER(C_DecryptFinal);
PREREQ(cryptoki_initialized, CKR_CRYPTOKI_NOT_INITIALIZED);
/* RSA/DSA mechs don't support incremental crypto operations. */
RETURN(CKR_FUNCTION_NOT_SUPPORTED);
}
static CK_RV
PC_C_DigestInit(CK_SESSION_HANDLE session, CK_MECHANISM_PTR mechanism)
{
ENTER(C_DigestInit);
PREREQ(cryptoki_initialized, CKR_CRYPTOKI_NOT_INITIALIZED);
/* RSA/DSA mechs don't support digest. */
RETURN(CKR_FUNCTION_NOT_SUPPORTED);
}
static CK_RV
PC_C_Digest(CK_SESSION_HANDLE session, CK_BYTE_PTR data, CK_ULONG data_len,
CK_BYTE_PTR digest, CK_ULONG_PTR digest_len)
{
ENTER(C_Digest);
PREREQ(cryptoki_initialized, CKR_CRYPTOKI_NOT_INITIALIZED);
/* RSA/DSA mechs don't support digest. */
RETURN(CKR_FUNCTION_NOT_SUPPORTED);
}
static CK_RV
PC_C_DigestUpdate(CK_SESSION_HANDLE session, CK_BYTE_PTR part, CK_ULONG part_len)
{
ENTER(C_DigestUpdate);
PREREQ(cryptoki_initialized, CKR_CRYPTOKI_NOT_INITIALIZED);
/* RSA/DSA mechs don't support digest. */
RETURN(CKR_FUNCTION_NOT_SUPPORTED);
}
static CK_RV
PC_C_DigestKey(CK_SESSION_HANDLE session, CK_OBJECT_HANDLE key)
{
ENTER(C_DigestKey);
PREREQ(cryptoki_initialized, CKR_CRYPTOKI_NOT_INITIALIZED);
/* RSA/DSA mechs don't support digest. */
RETURN(CKR_FUNCTION_NOT_SUPPORTED);
}
static CK_RV
PC_C_DigestFinal(CK_SESSION_HANDLE session, CK_BYTE_PTR digest,
CK_ULONG_PTR digest_len)
{
ENTER(C_DigestFinal);
PREREQ(cryptoki_initialized, CKR_CRYPTOKI_NOT_INITIALIZED);
/* RSA/DSA mechs don't support digest. */
RETURN(CKR_FUNCTION_NOT_SUPPORTED);
}
static CK_RV
PC_C_SignInit(CK_SESSION_HANDLE session, CK_MECHANISM_PTR mechanism,
CK_OBJECT_HANDLE key)
{
P11cObjectData* objdata;
P11cSession* sess;
CK_RV ret;
ENTER(C_SignInit);
PREREQ(cryptoki_initialized, CKR_CRYPTOKI_NOT_INITIALIZED);
PREREQ(mechanism, CKR_ARGUMENTS_BAD);
PREREQ(key, CKR_ARGUMENTS_BAD);
ret = p11c_session_get_lock_ref(session, FALSE, &sess);
if(ret == CKR_OK)
{
ret = p11c_session_get_object_data_for(sess, key, &objdata);
if(ret == CKR_OK)
ret = p11c_session_sign_init(sess, mechanism, objdata);
p11c_session_unref_unlock(sess);
}
RETURN(ret);
}
static CK_RV
PC_C_Sign(CK_SESSION_HANDLE session, CK_BYTE_PTR data, CK_ULONG data_len,
CK_BYTE_PTR signature, CK_ULONG_PTR signature_len)
{
P11cSession* sess;
CK_RV ret;
ENTER(C_Sign);
PREREQ(cryptoki_initialized, CKR_CRYPTOKI_NOT_INITIALIZED);
PREREQ(data, CKR_ARGUMENTS_BAD);
PREREQ(data_len, CKR_ARGUMENTS_BAD);
ret = p11c_session_get_lock_ref(session, FALSE, &sess);
if(ret == CKR_OK)
{
ret = p11c_session_sign(sess, data, data_len, signature, signature_len);
p11c_session_unref_unlock(sess);
}
RETURN(ret);
}
static CK_RV
PC_C_SignUpdate(CK_SESSION_HANDLE session, CK_BYTE_PTR part, CK_ULONG part_len)
{
ENTER(C_SignUpdate);
PREREQ(cryptoki_initialized, CKR_CRYPTOKI_NOT_INITIALIZED);
/* RSA/DSA mechs don't support incremental crypto operations. */
RETURN(CKR_FUNCTION_NOT_SUPPORTED);
}
static CK_RV
PC_C_SignFinal(CK_SESSION_HANDLE session, CK_BYTE_PTR signature,
CK_ULONG_PTR signature_len)
{
ENTER(C_SignFinal);
PREREQ(cryptoki_initialized, CKR_CRYPTOKI_NOT_INITIALIZED);
/* RSA/DSA mechs don't support incremental crypto operations. */
RETURN(CKR_FUNCTION_NOT_SUPPORTED);
}
static CK_RV
PC_C_SignRecoverInit(CK_SESSION_HANDLE session, CK_MECHANISM_PTR mechanism,
CK_OBJECT_HANDLE key)
{
ENTER(C_SignRecoverInit);
PREREQ(cryptoki_initialized, CKR_CRYPTOKI_NOT_INITIALIZED);
/* TODO: Implement this */
RETURN(CKR_FUNCTION_NOT_SUPPORTED);
}
static CK_RV
PC_C_SignRecover(CK_SESSION_HANDLE session, CK_BYTE_PTR data, CK_ULONG data_len,
CK_BYTE_PTR signature, CK_ULONG_PTR signature_len)
{
ENTER(C_SignRecover);
PREREQ(cryptoki_initialized, CKR_CRYPTOKI_NOT_INITIALIZED);
/* TODO: Implement this */
RETURN(CKR_FUNCTION_NOT_SUPPORTED);
}
static CK_RV
PC_C_VerifyInit(CK_SESSION_HANDLE session, CK_MECHANISM_PTR mechanism,
CK_OBJECT_HANDLE key)
{
ENTER(C_VerifyInit);
PREREQ(cryptoki_initialized, CKR_CRYPTOKI_NOT_INITIALIZED);
/* TODO: See if we need to implement this */
RETURN(CKR_FUNCTION_NOT_SUPPORTED);
}
static CK_RV
PC_C_Verify(CK_SESSION_HANDLE session, CK_BYTE_PTR data, CK_ULONG data_len,
CK_BYTE_PTR signature, CK_ULONG signature_len)
{
ENTER(C_Verify);
PREREQ(cryptoki_initialized, CKR_CRYPTOKI_NOT_INITIALIZED);
/* TODO: See if we need to implement this */
RETURN(CKR_FUNCTION_NOT_SUPPORTED);
}
static CK_RV
PC_C_VerifyUpdate(CK_SESSION_HANDLE session, CK_BYTE_PTR part, CK_ULONG part_len)
{
ENTER(C_VerifyUpdate);
PREREQ(cryptoki_initialized, CKR_CRYPTOKI_NOT_INITIALIZED);
/* RSA/DSA mechs don't support incremental crypto operations. */
RETURN(CKR_FUNCTION_NOT_SUPPORTED);
}
static CK_RV
PC_C_VerifyFinal(CK_SESSION_HANDLE session, CK_BYTE_PTR signature,
CK_ULONG signature_len)
{
ENTER(C_VerifyFinal);
PREREQ(cryptoki_initialized, CKR_CRYPTOKI_NOT_INITIALIZED);
/* RSA/DSA mechs don't support incremental crypto operations. */
RETURN(CKR_FUNCTION_NOT_SUPPORTED);
}
static CK_RV
PC_C_VerifyRecoverInit(CK_SESSION_HANDLE session, CK_MECHANISM_PTR mechanism,
CK_OBJECT_HANDLE key)
{
ENTER(C_VerifyRecoverInit);
PREREQ(cryptoki_initialized, CKR_CRYPTOKI_NOT_INITIALIZED);
/* TODO: See if we need to implement this */
RETURN(CKR_FUNCTION_NOT_SUPPORTED);
}
static CK_RV
PC_C_VerifyRecover(CK_SESSION_HANDLE session, CK_BYTE_PTR signature,
CK_ULONG signature_len, CK_BYTE_PTR data, CK_ULONG_PTR data_len)
{
ENTER(C_VerifyRecover);
PREREQ(cryptoki_initialized, CKR_CRYPTOKI_NOT_INITIALIZED);
/* TODO: See if we need to implement this */
RETURN(CKR_FUNCTION_NOT_SUPPORTED);
}
static CK_RV
PC_C_DigestEncryptUpdate(CK_SESSION_HANDLE session, CK_BYTE_PTR part,
CK_ULONG part_len, CK_BYTE_PTR encrypted_part,
CK_ULONG_PTR encrypted_part_len)
{
ENTER(C_DigestEncryptUpdate);
PREREQ(cryptoki_initialized, CKR_CRYPTOKI_NOT_INITIALIZED);
/* RSA/DSA mechs don't support incremental crypto operations. */
RETURN(CKR_FUNCTION_NOT_SUPPORTED);
}
static CK_RV
PC_C_DecryptDigestUpdate(CK_SESSION_HANDLE session, CK_BYTE_PTR encrypted_part,
CK_ULONG encrypted_part_len, CK_BYTE_PTR part,
CK_ULONG_PTR part_len)
{
ENTER(C_DecryptDigestUpdate);
PREREQ(cryptoki_initialized, CKR_CRYPTOKI_NOT_INITIALIZED);
/* RSA/DSA mechs don't support incremental crypto operations. */
RETURN(CKR_FUNCTION_NOT_SUPPORTED);
}
static CK_RV
PC_C_SignEncryptUpdate(CK_SESSION_HANDLE session, CK_BYTE_PTR part,
CK_ULONG part_len, CK_BYTE_PTR encrypted_part,
CK_ULONG_PTR encrypted_part_len)
{
ENTER(C_SignEncryptUpdate);
PREREQ(cryptoki_initialized, CKR_CRYPTOKI_NOT_INITIALIZED);
/* RSA/DSA mechs don't support incremental crypto operations. */
RETURN(CKR_FUNCTION_NOT_SUPPORTED);
}
static CK_RV
PC_C_DecryptVerifyUpdate(CK_SESSION_HANDLE session, CK_BYTE_PTR encrypted_part,
CK_ULONG encrypted_part_len, CK_BYTE_PTR part,
CK_ULONG_PTR part_len)
{
ENTER(C_DecryptVerifyUpdate);
PREREQ(cryptoki_initialized, CKR_CRYPTOKI_NOT_INITIALIZED);
/* RSA/DSA mechs don't support incremental crypto operations. */
RETURN(CKR_FUNCTION_NOT_SUPPORTED);
}
static CK_RV
PC_C_GenerateKey(CK_SESSION_HANDLE session, CK_MECHANISM_PTR mechanism,
CK_ATTRIBUTE_PTR templ, CK_ULONG count,
CK_OBJECT_HANDLE_PTR key)
{
ENTER(C_GenerateKey);
PREREQ(cryptoki_initialized, CKR_CRYPTOKI_NOT_INITIALIZED);
/* Let key generation happen via Windows interfaces */
RETURN(CKR_FUNCTION_NOT_SUPPORTED);
}
static CK_RV
PC_C_GenerateKeyPair(CK_SESSION_HANDLE session, CK_MECHANISM_PTR mechanism,
CK_ATTRIBUTE_PTR public_key_template, CK_ULONG public_key_attribute_count,
CK_ATTRIBUTE_PTR private_key_template, CK_ULONG private_key_attribute_count,
CK_OBJECT_HANDLE_PTR public_key, CK_OBJECT_HANDLE_PTR private_key)
{
ENTER(C_GenerateKeyPair);
PREREQ(cryptoki_initialized, CKR_CRYPTOKI_NOT_INITIALIZED);
/* Let key generation happen via Windows interfaces */
RETURN(CKR_FUNCTION_NOT_SUPPORTED);
}
static CK_RV
PC_C_WrapKey(CK_SESSION_HANDLE session, CK_MECHANISM_PTR mechanism,
CK_OBJECT_HANDLE wrapping_key, CK_OBJECT_HANDLE key,
CK_BYTE_PTR wrapped_key, CK_ULONG_PTR wrapped_key_len)
{
ENTER(C_WrapKey);
PREREQ(cryptoki_initialized, CKR_CRYPTOKI_NOT_INITIALIZED);
/* TODO: See if we need to implement this */
RETURN(CKR_FUNCTION_NOT_SUPPORTED);
}
static CK_RV
PC_C_UnwrapKey(CK_SESSION_HANDLE session, CK_MECHANISM_PTR mechanism,
CK_OBJECT_HANDLE unwrapping_key, CK_BYTE_PTR wrapped_key,
CK_ULONG wrapped_key_len, CK_ATTRIBUTE_PTR templ,
CK_ULONG count, CK_OBJECT_HANDLE_PTR key)
{
ENTER(C_UnwrapKey);
PREREQ(cryptoki_initialized, CKR_CRYPTOKI_NOT_INITIALIZED);
/* TODO: See if we need to implement this */
RETURN(CKR_FUNCTION_NOT_SUPPORTED);
}
static CK_RV
PC_C_DeriveKey(CK_SESSION_HANDLE session, CK_MECHANISM_PTR mechanism,
CK_OBJECT_HANDLE base_key, CK_ATTRIBUTE_PTR templ,
CK_ULONG count, CK_OBJECT_HANDLE_PTR key)
{
ENTER(C_DeriveKey);
PREREQ(cryptoki_initialized, CKR_CRYPTOKI_NOT_INITIALIZED);
/* Can't do this with RSA */
RETURN(CKR_FUNCTION_NOT_SUPPORTED);
}
static CK_RV
PC_C_SeedRandom(CK_SESSION_HANDLE session, CK_BYTE_PTR seed, CK_ULONG seed_len)
{
ENTER(C_SeedRandom);
PREREQ(cryptoki_initialized, CKR_CRYPTOKI_NOT_INITIALIZED);
/*
* TODO: Perhaps at some point in the future we may want
* to see if we can hook into the Windows RNG
*/
RETURN(CKR_FUNCTION_NOT_SUPPORTED);
}
static CK_RV
PC_C_GenerateRandom(CK_SESSION_HANDLE session, CK_BYTE_PTR random_data,
CK_ULONG random_len)
{
ENTER(C_GenerateRandom);
PREREQ(cryptoki_initialized, CKR_CRYPTOKI_NOT_INITIALIZED);
/*
* TODO: Perhaps at some point in the future we may want
* to see if we can hook into the Windows RNG
*/
RETURN(CKR_FUNCTION_NOT_SUPPORTED);
}
static CK_FUNCTION_LIST functionList = {
{ 2, 11 }, /* version */
PC_C_Initialize,
PC_C_Finalize,
PC_C_GetInfo,
PC_C_GetFunctionList,
PC_C_GetSlotList,
PC_C_GetSlotInfo,
PC_C_GetTokenInfo,
PC_C_GetMechanismList,
PC_C_GetMechanismInfo,
PC_C_InitToken,
PC_C_InitPIN,
PC_C_SetPIN,
PC_C_OpenSession,
PC_C_CloseSession,
PC_C_CloseAllSessions,
PC_C_GetSessionInfo,
PC_C_GetOperationState,
PC_C_SetOperationState,
PC_C_Login,
PC_C_Logout,
PC_C_CreateObject,
PC_C_CopyObject,
PC_C_DestroyObject,
PC_C_GetObjectSize,
PC_C_GetAttributeValue,
PC_C_SetAttributeValue,
PC_C_FindObjectsInit,
PC_C_FindObjects,
PC_C_FindObjectsFinal,
PC_C_EncryptInit,
PC_C_Encrypt,
PC_C_EncryptUpdate,
PC_C_EncryptFinal,
PC_C_DecryptInit,
PC_C_Decrypt,
PC_C_DecryptUpdate,
PC_C_DecryptFinal,
PC_C_DigestInit,
PC_C_Digest,
PC_C_DigestUpdate,
PC_C_DigestKey,
PC_C_DigestFinal,
PC_C_SignInit,
PC_C_Sign,
PC_C_SignUpdate,
PC_C_SignFinal,
PC_C_SignRecoverInit,
PC_C_SignRecover,
PC_C_VerifyInit,
PC_C_Verify,
PC_C_VerifyUpdate,
PC_C_VerifyFinal,
PC_C_VerifyRecoverInit,
PC_C_VerifyRecover,
PC_C_DigestEncryptUpdate,
PC_C_DecryptDigestUpdate,
PC_C_SignEncryptUpdate,
PC_C_DecryptVerifyUpdate,
PC_C_GenerateKey,
PC_C_GenerateKeyPair,
PC_C_WrapKey,
PC_C_UnwrapKey,
PC_C_DeriveKey,
PC_C_SeedRandom,
PC_C_GenerateRandom,
PC_C_GetFunctionStatus,
PC_C_CancelFunction,
PC_C_WaitForSlotEvent
};
__declspec(dllexport) CK_RV
C_GetFunctionList(CK_FUNCTION_LIST_PTR_PTR list)
{
if(!list)
return CKR_ARGUMENTS_BAD;
*list = &functionList;
return CKR_OK;
}
|