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
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
|
/*
* Copyright (C) 2012 Red Hat Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above
* copyright notice, this list of conditions and the
* following disclaimer.
* * Redistributions in binary form must reproduce the
* above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or
* other materials provided with the distribution.
* * The names of contributors to this software may not be
* used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*
* Author: Stef Walter <stefw@redhat.com>
*/
#include "config.h"
#define CRYPTOKI_EXPORTS
#include "array.h"
#include "attrs.h"
#define P11_DEBUG_FLAG P11_DEBUG_TRUST
#include "debug.h"
#include "dict.h"
#include "library.h"
#include "module.h"
#include "parser.h"
#include "pkcs11.h"
#include "pkcs11x.h"
#include "session.h"
#include "token.h"
#include <assert.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#define MANUFACTURER_ID "PKCS#11 Kit "
#define LIBRARY_DESCRIPTION "PKCS#11 Kit Trust Module "
#define TOKEN_MODEL "p11-kit-trust "
#define TOKEN_SERIAL_NUMBER "1 "
/* Initial slot id: non-zero and non-one */
#define BASE_SLOT_ID 18UL
static struct _Shared {
p11_dict *sessions;
p11_array *tokens;
char *paths;
} gl = { NULL, NULL };
/* Used during FindObjects */
typedef struct _FindObjects {
CK_ATTRIBUTE *match;
CK_OBJECT_HANDLE *snapshot;
CK_ULONG iterator;
} FindObjects;
static CK_FUNCTION_LIST sys_function_list;
static void
find_objects_free (void *data)
{
FindObjects *find = data;
p11_attrs_free (find->match);
free (find->snapshot);
free (find);
}
static CK_RV
lookup_session (CK_SESSION_HANDLE handle,
p11_session **session)
{
p11_session *sess;
if (!gl.sessions)
return CKR_CRYPTOKI_NOT_INITIALIZED;
sess = p11_dict_get (gl.sessions, &handle);
if (!sess)
return CKR_SESSION_HANDLE_INVALID;
if (sess && session)
*session = sess;
return CKR_OK;
}
static CK_ATTRIBUTE *
lookup_object_inlock (p11_session *session,
CK_OBJECT_HANDLE handle,
p11_index **index)
{
CK_ATTRIBUTE *attrs;
assert (session != NULL);
attrs = p11_index_lookup (session->index, handle);
if (attrs) {
if (index)
*index = session->index;
return attrs;
}
attrs = p11_index_lookup (p11_token_index (session->token), handle);
if (attrs) {
if (index)
*index = p11_token_index (session->token);
return attrs;
}
return NULL;
}
static CK_RV
lookup_slot_inlock (CK_SLOT_ID id,
p11_token **token)
{
/*
* These are invalid inputs, that well behaved callers should
* not produce, so have them fail precondations
*/
return_val_if_fail (gl.tokens != NULL,
CKR_CRYPTOKI_NOT_INITIALIZED);
return_val_if_fail (id >= BASE_SLOT_ID && id - BASE_SLOT_ID < gl.tokens->num,
CKR_SLOT_ID_INVALID);
if (token)
*token = gl.tokens->elem[id - BASE_SLOT_ID];
return CKR_OK;
}
static bool
check_slot (CK_SLOT_ID id)
{
bool ret;
p11_lock ();
ret = lookup_slot_inlock (id, NULL) == CKR_OK;
p11_unlock ();
return ret;
}
static bool
create_tokens_inlock (p11_array *tokens,
const char *paths)
{
/*
* TRANSLATORS: These label strings are used in PKCS#11 URIs and
* unfortunately cannot be marked translatable. If localization is
* desired they should be translated in GUI applications. These
* strings will not change arbitrarily.
*/
struct {
const char *prefix;
const char *label;
} labels[] = {
{ DATADIR, "Default Trust" },
{ SYSCONFDIR, "System Trust" },
{ NULL },
};
p11_token *token;
p11_token *check;
CK_SLOT_ID slot;
const char *path;
const char *label;
char *alloc;
char *remaining;
char *base;
char *pos;
int i;
p11_debug ("using paths: %s", paths);
alloc = remaining = strdup (paths);
return_val_if_fail (remaining != NULL, false);
while (remaining) {
path = remaining;
pos = strchr (remaining, ':');
if (pos == NULL) {
remaining = NULL;
} else {
pos[0] = '\0';
remaining = pos + 1;
}
if (path[0] != '\0') {
/* The slot for the new token */
slot = BASE_SLOT_ID + tokens->num;
label = NULL;
base = NULL;
/* Claim the various labels based on prefix */
for (i = 0; label == NULL && labels[i].prefix != NULL; i++) {
if (strncmp (path, labels[i].prefix, strlen (labels[i].prefix)) == 0) {
label = labels[i].label;
labels[i].label = NULL;
}
}
/* Didn't find a label above, then make one based on the path */
if (!label) {
label = base = p11_basename (path);
return_val_if_fail (base != NULL, false);
}
token = p11_token_new (slot, path, label);
return_val_if_fail (token != NULL, false);
if (!p11_array_push (tokens, token))
return_val_if_reached (false);
free (base);
assert (lookup_slot_inlock (slot, &check) == CKR_OK && check == token);
}
}
free (alloc);
return true;
}
static void
parse_argument (char *arg)
{
char *value;
value = arg + strcspn (arg, ":=");
if (!*value)
value = NULL;
else
*(value++) = 0;
if (strcmp (arg, "paths") == 0) {
free (gl.paths);
gl.paths = value ? strdup (value) : NULL;
} else {
p11_message ("unrecognized module argument: %s", arg);
}
}
static void
parse_arguments (const char *string)
{
char quote = '\0';
char *src, *dup, *at, *arg;
if (!string)
return;
src = dup = strdup (string);
if (!dup) {
p11_message ("couldn't allocate memory for argument string");
return;
}
arg = at = src;
for (src = dup; *src; src++) {
/* Matching quote */
if (quote == *src) {
quote = '\0';
/* Inside of quotes */
} else if (quote != '\0') {
if (*src == '\\') {
*at++ = *src++;
if (!*src) {
p11_message ("couldn't parse argument string: %s", string);
goto done;
}
if (*src != quote)
*at++ = '\\';
}
*at++ = *src;
/* Space, not inside of quotes */
} else if (isspace(*src)) {
*at = 0;
parse_argument (arg);
arg = at;
/* Other character outside of quotes */
} else {
switch (*src) {
case '\'':
case '"':
quote = *src;
break;
case '\\':
*at++ = *src++;
if (!*src) {
p11_message ("couldn't parse argument string: %s", string);
goto done;
}
/* fall through */
default:
*at++ = *src;
break;
}
}
}
if (at != arg) {
*at = 0;
parse_argument (arg);
}
done:
free (dup);
}
static CK_RV
sys_C_Finalize (CK_VOID_PTR reserved)
{
CK_RV rv = CKR_OK;
p11_debug ("in");
/* WARNING: This function must be reentrant */
if (reserved) {
rv = CKR_ARGUMENTS_BAD;
} else {
p11_lock ();
if (!gl.sessions) {
rv = CKR_CRYPTOKI_NOT_INITIALIZED;
} else {
free (gl.paths);
gl.paths = NULL;
p11_dict_free (gl.sessions);
gl.sessions = NULL;
p11_array_free (gl.tokens);
gl.tokens = NULL;
rv = CKR_OK;
}
p11_unlock ();
}
p11_debug ("out: 0x%lx", rv);
return rv;
}
static CK_RV
sys_C_Initialize (CK_VOID_PTR init_args)
{
CK_C_INITIALIZE_ARGS *args = NULL;
int supplied_ok;
CK_RV rv;
p11_library_init_once ();
/* WARNING: This function must be reentrant */
p11_debug ("in");
p11_lock ();
rv = CKR_OK;
/* pReserved must be NULL */
args = init_args;
/* 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);
if (!supplied_ok) {
p11_message ("invalid set of mutex calls supplied");
rv = CKR_ARGUMENTS_BAD;
}
/*
* When the CKF_OS_LOCKING_OK flag isn't set return an error.
* We must be able to use our pthread functionality.
*/
if (!(args->flags & CKF_OS_LOCKING_OK)) {
p11_message ("can't do without os locking");
rv = CKR_CANT_LOCK;
}
/*
* We support setting the socket path and other arguments from from the
* pReserved pointer, similar to how NSS PKCS#11 components are initialized.
*/
if (rv == CKR_OK) {
if (args->pReserved)
parse_arguments ((const char*)args->pReserved);
gl.sessions = p11_dict_new (p11_dict_ulongptr_hash,
p11_dict_ulongptr_equal,
NULL, p11_session_free);
gl.tokens = p11_array_new ((p11_destroyer)p11_token_free);
if (gl.tokens && !create_tokens_inlock (gl.tokens, gl.paths ? gl.paths : TRUST_PATHS))
gl.tokens = NULL;
if (gl.sessions == NULL || gl.tokens == NULL) {
warn_if_reached ();
rv = CKR_GENERAL_ERROR;
}
}
p11_unlock ();
if (rv != CKR_OK)
sys_C_Finalize (NULL);
p11_debug ("out: 0x%lx", rv);
return rv;
}
static CK_RV
sys_C_GetInfo (CK_INFO_PTR info)
{
CK_RV rv = CKR_OK;
p11_library_init_once ();
p11_debug ("in");
return_val_if_fail (info != NULL, CKR_ARGUMENTS_BAD);
p11_lock ();
if (!gl.sessions)
rv = CKR_CRYPTOKI_NOT_INITIALIZED;
p11_unlock ();
if (rv == CKR_OK) {
memset (info, 0, sizeof (*info));
info->cryptokiVersion.major = CRYPTOKI_VERSION_MAJOR;
info->cryptokiVersion.minor = CRYPTOKI_VERSION_MINOR;
info->libraryVersion.major = PACKAGE_MAJOR;
info->libraryVersion.minor = PACKAGE_MINOR;
info->flags = 0;
strncpy ((char*)info->manufacturerID, MANUFACTURER_ID, 32);
strncpy ((char*)info->libraryDescription, LIBRARY_DESCRIPTION, 32);
}
p11_debug ("out: 0x%lx", rv);
return rv;
}
static CK_RV
sys_C_GetFunctionList (CK_FUNCTION_LIST_PTR_PTR list)
{
/* Can be called before C_Initialize */
return_val_if_fail (list != NULL, CKR_ARGUMENTS_BAD);
*list = &sys_function_list;
return CKR_OK;
}
static CK_RV
sys_C_GetSlotList (CK_BBOOL token_present,
CK_SLOT_ID_PTR slot_list,
CK_ULONG_PTR count)
{
CK_RV rv = CKR_OK;
int i;
return_val_if_fail (count != NULL, CKR_ARGUMENTS_BAD);
p11_debug ("in");
p11_lock ();
if (!gl.sessions)
rv = CKR_CRYPTOKI_NOT_INITIALIZED;
p11_unlock ();
if (rv != CKR_OK) {
/* already failed */
} else if (!slot_list) {
*count = gl.tokens->num;
rv = CKR_OK;
} else if (*count < gl.tokens->num) {
*count = gl.tokens->num;
rv = CKR_BUFFER_TOO_SMALL;
} else {
for (i = 0; i < gl.tokens->num; i++)
slot_list[i] = BASE_SLOT_ID + i;
*count = gl.tokens->num;
rv = CKR_OK;
}
p11_debug ("out: 0x%lx", rv);
return rv;
}
static CK_RV
sys_C_GetSlotInfo (CK_SLOT_ID id,
CK_SLOT_INFO_PTR info)
{
CK_RV rv = CKR_OK;
p11_token *token;
const char *path;
size_t length;
return_val_if_fail (info != NULL, CKR_ARGUMENTS_BAD);
p11_debug ("in");
p11_lock ();
rv = lookup_slot_inlock (id, &token);
if (rv == CKR_OK) {
memset (info, 0, sizeof (*info));
info->firmwareVersion.major = 0;
info->firmwareVersion.minor = 0;
info->hardwareVersion.major = PACKAGE_MAJOR;
info->hardwareVersion.minor = PACKAGE_MINOR;
info->flags = CKF_TOKEN_PRESENT;
strncpy ((char*)info->manufacturerID, MANUFACTURER_ID, 32);
/* If too long, copy the first 64 characters into buffer */
path = p11_token_get_path (token);
length = strlen (path);
if (length > sizeof (info->slotDescription))
length = sizeof (info->slotDescription);
memset (info->slotDescription, ' ', sizeof (info->slotDescription));
memcpy (info->slotDescription, path, length);
}
p11_unlock ();
p11_debug ("out: 0x%lx", rv);
return rv;
}
static CK_RV
sys_C_GetTokenInfo (CK_SLOT_ID id,
CK_TOKEN_INFO_PTR info)
{
CK_RV rv = CKR_OK;
p11_token *token;
const char *label;
size_t length;
return_val_if_fail (info != NULL, CKR_ARGUMENTS_BAD);
p11_debug ("in");
p11_lock ();
rv = lookup_slot_inlock (id, &token);
if (rv == CKR_OK) {
memset (info, 0, sizeof (*info));
info->firmwareVersion.major = 0;
info->firmwareVersion.minor = 0;
info->hardwareVersion.major = PACKAGE_MAJOR;
info->hardwareVersion.minor = PACKAGE_MINOR;
info->flags = CKF_TOKEN_INITIALIZED | CKF_WRITE_PROTECTED;
strncpy ((char*)info->manufacturerID, MANUFACTURER_ID, 32);
strncpy ((char*)info->model, TOKEN_MODEL, 16);
strncpy ((char*)info->serialNumber, TOKEN_SERIAL_NUMBER, 16);
info->ulMaxSessionCount = CK_EFFECTIVELY_INFINITE;
info->ulSessionCount = CK_UNAVAILABLE_INFORMATION;
info->ulMaxRwSessionCount = 0;
info->ulRwSessionCount = CK_UNAVAILABLE_INFORMATION;
info->ulMaxPinLen = 0;
info->ulMinPinLen = 0;
info->ulTotalPublicMemory = CK_UNAVAILABLE_INFORMATION;
info->ulFreePublicMemory = CK_UNAVAILABLE_INFORMATION;
info->ulTotalPrivateMemory = CK_UNAVAILABLE_INFORMATION;
info->ulFreePrivateMemory = CK_UNAVAILABLE_INFORMATION;
/* If too long, copy the first 32 characters into buffer */
label = p11_token_get_label (token);
length = strlen (label);
if (length > sizeof (info->label))
length = sizeof (info->label);
memset (info->label, ' ', sizeof (info->label));
memcpy (info->label, label, length);
}
p11_unlock ();
p11_debug ("out: 0x%lx", rv);
return rv;
}
static CK_RV
sys_C_GetMechanismList (CK_SLOT_ID id,
CK_MECHANISM_TYPE_PTR mechanism_list,
CK_ULONG_PTR count)
{
CK_RV rv = CKR_OK;
return_val_if_fail (count != NULL, CKR_ARGUMENTS_BAD);
p11_debug ("in");
*count = 0;
p11_debug ("out: 0x%lx", rv);
return rv;
}
static CK_RV
sys_C_GetMechanismInfo (CK_SLOT_ID id,
CK_MECHANISM_TYPE type,
CK_MECHANISM_INFO_PTR info)
{
return_val_if_fail (info != NULL, CKR_ARGUMENTS_BAD);
return_val_if_fail (check_slot (id), CKR_SLOT_ID_INVALID);
return_val_if_reached (CKR_MECHANISM_INVALID);
}
static CK_RV
sys_C_InitToken (CK_SLOT_ID id,
CK_UTF8CHAR_PTR pin,
CK_ULONG pin_len,
CK_UTF8CHAR_PTR label)
{
return_val_if_fail (check_slot (id), CKR_SLOT_ID_INVALID);
return_val_if_reached (CKR_TOKEN_WRITE_PROTECTED);
}
static CK_RV
sys_C_WaitForSlotEvent (CK_FLAGS flags,
CK_SLOT_ID_PTR slot,
CK_VOID_PTR reserved)
{
p11_debug ("not supported");
return CKR_FUNCTION_NOT_SUPPORTED;
}
static CK_RV
sys_C_OpenSession (CK_SLOT_ID id,
CK_FLAGS flags,
CK_VOID_PTR user_data,
CK_NOTIFY callback,
CK_SESSION_HANDLE_PTR handle)
{
p11_session *session;
p11_token *token;
CK_RV rv = CKR_OK;
return_val_if_fail (check_slot (id), CKR_SLOT_ID_INVALID);
return_val_if_fail (handle != NULL, CKR_ARGUMENTS_BAD);
p11_debug ("in");
p11_lock ();
rv = lookup_slot_inlock (id, &token);
if (rv != CKR_OK) {
/* fail below */;
} else if (!(flags & CKF_SERIAL_SESSION)) {
rv = CKR_SESSION_PARALLEL_NOT_SUPPORTED;
} else if (flags & CKF_RW_SESSION) {
rv = CKR_TOKEN_WRITE_PROTECTED;
} else {
session = p11_session_new (token);
if (p11_dict_set (gl.sessions, &session->handle, session)) {
rv = CKR_OK;
*handle = session->handle;
p11_debug ("session: %lu", *handle);
} else {
warn_if_reached ();
rv = CKR_GENERAL_ERROR;
}
}
p11_unlock ();
p11_debug ("out: 0x%lx", rv);
return rv;
}
static CK_RV
sys_C_CloseSession (CK_SESSION_HANDLE handle)
{
CK_RV rv = CKR_OK;
p11_debug ("in");
p11_lock ();
if (!gl.sessions) {
rv = CKR_CRYPTOKI_NOT_INITIALIZED;
} else if (p11_dict_remove (gl.sessions, &handle)) {
rv = CKR_OK;
} else {
rv = CKR_SESSION_HANDLE_INVALID;
}
p11_unlock ();
p11_debug ("out: 0x%lx", rv);
return rv;
}
static CK_RV
sys_C_CloseAllSessions (CK_SLOT_ID id)
{
CK_SESSION_HANDLE *handle;
p11_session *session;
p11_token *token;
p11_dictiter iter;
CK_RV rv;
p11_debug ("in");
p11_lock ();
rv = lookup_slot_inlock (id, &token);
if (rv == CKR_OK) {
p11_dict_iterate (gl.sessions, &iter);
while (p11_dict_next (&iter, (void **)&handle, (void **)&session)) {
if (session->token == token)
p11_dict_remove (gl.sessions, handle);
}
}
p11_unlock ();
p11_debug ("out: 0x%lx", rv);
return rv;
}
static CK_RV
sys_C_GetFunctionStatus (CK_SESSION_HANDLE handle)
{
return CKR_SESSION_PARALLEL_NOT_SUPPORTED;
}
static CK_RV
sys_C_CancelFunction (CK_SESSION_HANDLE handle)
{
return CKR_SESSION_PARALLEL_NOT_SUPPORTED;
}
static CK_RV
sys_C_GetSessionInfo (CK_SESSION_HANDLE handle,
CK_SESSION_INFO_PTR info)
{
p11_session *session;
CK_RV rv;
return_val_if_fail (info != NULL, CKR_ARGUMENTS_BAD);
p11_debug ("in");
p11_lock ();
rv = lookup_session (handle, &session);
if (rv == CKR_OK) {
info->flags = CKF_SERIAL_SESSION;
info->state = CKS_RO_PUBLIC_SESSION;
info->slotID = p11_token_get_slot (session->token);
info->ulDeviceError = 0;
}
p11_unlock ();
p11_debug ("out: 0x%lx", rv);
return rv;
}
static CK_RV
sys_C_InitPIN (CK_SESSION_HANDLE handle,
CK_UTF8CHAR_PTR pin,
CK_ULONG pin_len)
{
return_val_if_reached (CKR_TOKEN_WRITE_PROTECTED);
}
static CK_RV
sys_C_SetPIN (CK_SESSION_HANDLE handle,
CK_UTF8CHAR_PTR old_pin,
CK_ULONG old_pin_len,
CK_UTF8CHAR_PTR new_pin,
CK_ULONG new_pin_len)
{
return_val_if_reached (CKR_TOKEN_WRITE_PROTECTED);
}
static CK_RV
sys_C_GetOperationState (CK_SESSION_HANDLE handle,
CK_BYTE_PTR operation_state,
CK_ULONG_PTR operation_state_len)
{
p11_debug ("not supported");
return CKR_FUNCTION_NOT_SUPPORTED;
}
static CK_RV
sys_C_SetOperationState (CK_SESSION_HANDLE handle,
CK_BYTE_PTR operation_state,
CK_ULONG operation_state_len,
CK_OBJECT_HANDLE encryption_key,
CK_OBJECT_HANDLE authentication_key)
{
p11_debug ("not supported");
return CKR_FUNCTION_NOT_SUPPORTED;
}
static CK_RV
sys_C_Login (CK_SESSION_HANDLE handle,
CK_USER_TYPE user_type,
CK_UTF8CHAR_PTR pin,
CK_ULONG pin_len)
{
return_val_if_reached (CKR_FUNCTION_NOT_SUPPORTED);
}
static CK_RV
sys_C_Logout (CK_SESSION_HANDLE handle)
{
return_val_if_reached (CKR_FUNCTION_NOT_SUPPORTED);
}
static CK_RV
sys_C_CreateObject (CK_SESSION_HANDLE handle,
CK_ATTRIBUTE_PTR template,
CK_ULONG count,
CK_OBJECT_HANDLE_PTR new_object)
{
p11_session *session;
CK_BBOOL token;
CK_RV rv;
return_val_if_fail (new_object != NULL, CKR_ARGUMENTS_BAD);
p11_debug ("in");
p11_lock ();
rv = lookup_session (handle, &session);
if (rv == CKR_OK) {
if (p11_attrs_findn_bool (template, count, CKA_TOKEN, &token) && token)
rv = CKR_TOKEN_WRITE_PROTECTED;
}
if (rv == CKR_OK)
rv = p11_index_add (session->index, template, count, new_object);
p11_unlock ();
p11_debug ("out: 0x%lx", rv);
return rv;
}
static CK_RV
sys_C_CopyObject (CK_SESSION_HANDLE handle,
CK_OBJECT_HANDLE object,
CK_ATTRIBUTE_PTR template,
CK_ULONG count,
CK_OBJECT_HANDLE_PTR new_object)
{
CK_BBOOL vfalse = CK_FALSE;
CK_ATTRIBUTE token = { CKA_TOKEN, &vfalse, sizeof (vfalse) };
p11_session *session;
CK_ATTRIBUTE *original;
CK_ATTRIBUTE *attrs;
CK_BBOOL val;
CK_RV rv;
return_val_if_fail (new_object != NULL, CKR_ARGUMENTS_BAD);
p11_debug ("in");
p11_lock ();
rv = lookup_session (handle, &session);
if (rv == CKR_OK) {
original = lookup_object_inlock (session, object, NULL);
if (original == NULL)
rv = CKR_OBJECT_HANDLE_INVALID;
}
if (rv == CKR_OK) {
if (p11_attrs_findn_bool (template, count, CKA_TOKEN, &val) && val)
rv = CKR_TOKEN_WRITE_PROTECTED;
}
if (rv == CKR_OK) {
attrs = p11_attrs_dup (original);
attrs = p11_attrs_buildn (attrs, template, count);
attrs = p11_attrs_build (attrs, &token, NULL);
rv = p11_index_take (session->index, attrs, new_object);
}
p11_unlock ();
p11_debug ("out: 0x%lx", rv);
return rv;
}
static CK_RV
sys_C_DestroyObject (CK_SESSION_HANDLE handle,
CK_OBJECT_HANDLE object)
{
p11_session *session;
CK_RV rv;
p11_debug ("in");
p11_lock ();
rv = lookup_session (handle, &session);
if (rv == CKR_OK) {
rv = p11_index_remove (session->index, object);
if (rv == CKR_OBJECT_HANDLE_INVALID) {
if (p11_index_lookup (p11_token_index (session->token), object))
rv = CKR_TOKEN_WRITE_PROTECTED;
}
}
p11_unlock ();
p11_debug ("out: 0x%lx", rv);
return rv;
}
static CK_RV
sys_C_GetObjectSize (CK_SESSION_HANDLE handle,
CK_OBJECT_HANDLE object,
CK_ULONG_PTR size)
{
p11_session *session;
CK_RV rv;
return_val_if_fail (size != NULL, CKR_ARGUMENTS_BAD);
p11_debug ("in");
p11_lock ();
rv = lookup_session (handle, &session);
if (rv == CKR_OK) {
if (lookup_object_inlock (session, object, NULL)) {
*size = CK_UNAVAILABLE_INFORMATION;
rv = CKR_OK;
} else {
rv = CKR_OBJECT_HANDLE_INVALID;
}
}
p11_unlock ();
p11_debug ("out: 0x%lx", rv);
return rv;
}
static CK_RV
sys_C_GetAttributeValue (CK_SESSION_HANDLE handle,
CK_OBJECT_HANDLE object,
CK_ATTRIBUTE_PTR template,
CK_ULONG count)
{
CK_ATTRIBUTE *attrs;
CK_ATTRIBUTE *result;
CK_ATTRIBUTE *attr;
p11_session *session;
char *string;
CK_ULONG i;
CK_RV rv;
p11_debug ("in: %lu, %lu", handle, object);
p11_lock ();
rv = lookup_session (handle, &session);
if (rv == CKR_OK) {
attrs = lookup_object_inlock (session, object, NULL);
if (attrs == NULL)
rv = CKR_OBJECT_HANDLE_INVALID;
}
if (rv == CKR_OK) {
for (i = 0; i < count; i++) {
result = template + i;
attr = p11_attrs_find (attrs, result->type);
if (!attr) {
result->ulValueLen = (CK_ULONG)-1;
rv = CKR_ATTRIBUTE_TYPE_INVALID;
continue;
}
if (!result->pValue) {
result->ulValueLen = attr->ulValueLen;
continue;
}
if (result->ulValueLen >= attr->ulValueLen) {
memcpy (result->pValue, attr->pValue, attr->ulValueLen);
result->ulValueLen = attr->ulValueLen;
continue;
}
result->ulValueLen = (CK_ULONG)-1;
rv = CKR_BUFFER_TOO_SMALL;
}
}
p11_unlock ();
if (p11_debugging) {
string = p11_attrs_to_string (template, count);
p11_debug ("out: 0x%lx %s", rv, string);
free (string);
}
return rv;
}
static CK_RV
sys_C_SetAttributeValue (CK_SESSION_HANDLE handle,
CK_OBJECT_HANDLE object,
CK_ATTRIBUTE_PTR template,
CK_ULONG count)
{
p11_session *session;
CK_RV rv;
p11_debug ("in");
p11_lock ();
rv = lookup_session (handle, &session);
if (rv == CKR_OK) {
rv = p11_index_set (session->index, object, template, count);
if (rv == CKR_OBJECT_HANDLE_INVALID) {
if (p11_index_lookup (p11_token_index (session->token), object))
rv = CKR_TOKEN_WRITE_PROTECTED;
}
}
p11_unlock ();
p11_debug ("out: 0x%lx", rv);
return rv;
}
static CK_ATTRIBUTE *
work_around_broken_nss_serial_number_lookups (CK_ATTRIBUTE *attrs)
{
/*
* WORKAROUND: NSS calls us asking for CKA_SERIAL_NUMBER items that are
* not DER encoded. It shouldn't be doing this. We never return any certificate
* serial numbers that are not DER encoded.
*
* So work around the issue here while the NSS guys fix this issue.
* This code should be removed in future versions.
*/
CK_OBJECT_CLASS klass;
CK_ATTRIBUTE *serial;
unsigned char *der;
size_t der_len;
int len_len;
if (!p11_attrs_find_ulong (attrs, CKA_CLASS, &klass) ||
klass != CKO_NSS_TRUST)
return attrs;
serial = p11_attrs_find_valid (attrs, CKA_SERIAL_NUMBER);
if (!serial || p11_asn1_tlv_length (serial->pValue, serial->ulValueLen) >= 0)
return attrs;
p11_debug ("working around serial number lookup that's not DER encoded");
/* Assumption that 32 bytes is more than enough to store a ulong */
der_len = 1 + 32 + serial->ulValueLen;
der = malloc (der_len);
return_val_if_fail (der != NULL, NULL);
der[0] = ASN1_TAG_INTEGER | ASN1_CLASS_UNIVERSAL;
len_len = der_len - 1;
asn1_length_der (serial->ulValueLen, der + 1, &len_len);
assert (len_len < der_len - serial->ulValueLen);
memcpy (der + 1 + len_len, serial->pValue, serial->ulValueLen);
der_len = 1 + len_len + serial->ulValueLen;
return p11_attrs_take (attrs, CKA_SERIAL_NUMBER, der, der_len);
}
static CK_RV
sys_C_FindObjectsInit (CK_SESSION_HANDLE handle,
CK_ATTRIBUTE_PTR template,
CK_ULONG count)
{
p11_index *indices[2] = { NULL, NULL };
CK_BBOOL want_token_objects;
CK_BBOOL want_session_objects;
CK_BBOOL token;
FindObjects *find;
p11_session *session;
char *string;
CK_RV rv;
int n = 0;
if (p11_debugging) {
string = p11_attrs_to_string (template, count);
p11_debug ("in: %lu, %s", handle, string);
free (string);
}
p11_lock ();
/* Are we searching for token objects? */
if (p11_attrs_findn_bool (template, count, CKA_TOKEN, &token)) {
want_token_objects = token;
want_session_objects = !token;
} else {
want_token_objects = CK_TRUE;
want_session_objects = CK_TRUE;
}
rv = lookup_session (handle, &session);
/* Refresh from disk if this session hasn't yet */
if (rv == CKR_OK) {
if (want_session_objects)
indices[n++] = session->index;
if (want_token_objects) {
if (!session->loaded)
p11_token_load (session->token);
session->loaded = CK_TRUE;
indices[n++] = p11_token_index (session->token);
}
find = calloc (1, sizeof (FindObjects));
warn_if_fail (find != NULL);
/* Make a snapshot of what we're matching */
if (find) {
find->match = p11_attrs_buildn (NULL, template, count);
warn_if_fail (find->match != NULL);
find->match = work_around_broken_nss_serial_number_lookups (find->match);
warn_if_fail (find->match != NULL);
/* Build a session snapshot of all objects */
find->iterator = 0;
find->snapshot = p11_index_snapshot (indices[0], indices[1], template, count);
warn_if_fail (find->snapshot != NULL);
}
if (!find || !find->snapshot || !find->match)
rv = CKR_HOST_MEMORY;
else
p11_session_set_operation (session, find_objects_free, find);
}
p11_unlock ();
p11_debug ("out: 0x%lx", rv);
return rv;
}
static CK_RV
sys_C_FindObjects (CK_SESSION_HANDLE handle,
CK_OBJECT_HANDLE_PTR objects,
CK_ULONG max_count,
CK_ULONG_PTR count)
{
CK_OBJECT_HANDLE object;
CK_ATTRIBUTE *attrs;
FindObjects *find = NULL;
p11_session *session;
CK_ULONG matched;
p11_index *index;
CK_RV rv;
return_val_if_fail (count != NULL, CKR_ARGUMENTS_BAD);
p11_debug ("in: %lu, %lu", handle, max_count);
p11_lock ();
rv = lookup_session (handle, &session);
if (rv == CKR_OK) {
if (session->cleanup != find_objects_free)
rv = CKR_OPERATION_NOT_INITIALIZED;
find = session->operation;
}
if (rv == CKR_OK) {
matched = 0;
while (matched < max_count) {
object = find->snapshot[find->iterator];
if (!object)
break;
find->iterator++;
attrs = lookup_object_inlock (session, object, &index);
if (attrs == NULL)
continue;
if (p11_attrs_match (attrs, find->match)) {
objects[matched] = object;
matched++;
}
}
*count = matched;
}
p11_unlock ();
p11_debug ("out: 0x%lx, %lu", handle, *count);
return rv;
}
static CK_RV
sys_C_FindObjectsFinal (CK_SESSION_HANDLE handle)
{
p11_session *session;
CK_RV rv;
p11_debug ("in");
p11_lock ();
rv = lookup_session (handle, &session);
if (rv == CKR_OK) {
if (session->cleanup != find_objects_free)
rv = CKR_OPERATION_NOT_INITIALIZED;
else
p11_session_set_operation (session, NULL, NULL);
}
p11_unlock ();
p11_debug ("out: 0x%lx", rv);
return rv;
}
static CK_RV
sys_C_EncryptInit (CK_SESSION_HANDLE handle,
CK_MECHANISM_PTR mechanism,
CK_OBJECT_HANDLE key)
{
return_val_if_reached (CKR_MECHANISM_INVALID);
}
static CK_RV
sys_C_Encrypt (CK_SESSION_HANDLE handle,
CK_BYTE_PTR data,
CK_ULONG data_len,
CK_BYTE_PTR encrypted_data,
CK_ULONG_PTR encrypted_data_len)
{
return_val_if_reached (CKR_OPERATION_NOT_INITIALIZED);
}
static CK_RV
sys_C_EncryptUpdate (CK_SESSION_HANDLE handle,
CK_BYTE_PTR part,
CK_ULONG part_len,
CK_BYTE_PTR encrypted_part,
CK_ULONG_PTR encrypted_part_len)
{
return_val_if_reached (CKR_OPERATION_NOT_INITIALIZED);
}
static CK_RV
sys_C_EncryptFinal (CK_SESSION_HANDLE handle,
CK_BYTE_PTR last_part,
CK_ULONG_PTR last_part_len)
{
return_val_if_reached (CKR_OPERATION_NOT_INITIALIZED);
}
static CK_RV
sys_C_DecryptInit (CK_SESSION_HANDLE handle,
CK_MECHANISM_PTR mechanism,
CK_OBJECT_HANDLE key)
{
return_val_if_reached (CKR_MECHANISM_INVALID);
}
static CK_RV
sys_C_Decrypt (CK_SESSION_HANDLE handle,
CK_BYTE_PTR enc_data,
CK_ULONG enc_data_len,
CK_BYTE_PTR data,
CK_ULONG_PTR data_len)
{
return_val_if_reached (CKR_OPERATION_NOT_INITIALIZED);
}
static CK_RV
sys_C_DecryptUpdate (CK_SESSION_HANDLE handle,
CK_BYTE_PTR enc_part,
CK_ULONG enc_part_len,
CK_BYTE_PTR part,
CK_ULONG_PTR part_len)
{
return_val_if_reached (CKR_OPERATION_NOT_INITIALIZED);
}
static CK_RV
sys_C_DecryptFinal (CK_SESSION_HANDLE handle,
CK_BYTE_PTR last_part,
CK_ULONG_PTR last_part_len)
{
return_val_if_reached (CKR_OPERATION_NOT_INITIALIZED);
}
static CK_RV
sys_C_DigestInit (CK_SESSION_HANDLE handle,
CK_MECHANISM_PTR mechanism)
{
return_val_if_reached (CKR_MECHANISM_INVALID);
}
static CK_RV
sys_C_Digest (CK_SESSION_HANDLE handle,
CK_BYTE_PTR data,
CK_ULONG data_len,
CK_BYTE_PTR digest,
CK_ULONG_PTR digest_len)
{
return_val_if_reached (CKR_OPERATION_NOT_INITIALIZED);
}
static CK_RV
sys_C_DigestUpdate (CK_SESSION_HANDLE handle,
CK_BYTE_PTR part,
CK_ULONG part_len)
{
return_val_if_reached (CKR_OPERATION_NOT_INITIALIZED);
}
static CK_RV
sys_C_DigestKey (CK_SESSION_HANDLE handle,
CK_OBJECT_HANDLE key)
{
return_val_if_reached (CKR_OPERATION_NOT_INITIALIZED);
}
static CK_RV
sys_C_DigestFinal (CK_SESSION_HANDLE handle,
CK_BYTE_PTR digest,
CK_ULONG_PTR digest_len)
{
return_val_if_reached (CKR_OPERATION_NOT_INITIALIZED);
}
static CK_RV
sys_C_SignInit (CK_SESSION_HANDLE handle,
CK_MECHANISM_PTR mechanism,
CK_OBJECT_HANDLE key)
{
return_val_if_reached (CKR_MECHANISM_INVALID);
}
static CK_RV
sys_C_Sign (CK_SESSION_HANDLE handle,
CK_BYTE_PTR data,
CK_ULONG data_len,
CK_BYTE_PTR signature,
CK_ULONG_PTR signature_len)
{
return_val_if_reached (CKR_OPERATION_NOT_INITIALIZED);
}
static CK_RV
sys_C_SignUpdate (CK_SESSION_HANDLE handle,
CK_BYTE_PTR part,
CK_ULONG part_len)
{
return_val_if_reached (CKR_OPERATION_NOT_INITIALIZED);
}
static CK_RV
sys_C_SignFinal (CK_SESSION_HANDLE handle,
CK_BYTE_PTR signature,
CK_ULONG_PTR signature_len)
{
return_val_if_reached (CKR_OPERATION_NOT_INITIALIZED);
}
static CK_RV
sys_C_SignRecoverInit (CK_SESSION_HANDLE handle,
CK_MECHANISM_PTR mechanism,
CK_OBJECT_HANDLE key)
{
return_val_if_reached (CKR_MECHANISM_INVALID);
}
static CK_RV
sys_C_SignRecover (CK_SESSION_HANDLE handle,
CK_BYTE_PTR data,
CK_ULONG data_len,
CK_BYTE_PTR signature,
CK_ULONG_PTR signature_len)
{
return_val_if_reached (CKR_OPERATION_NOT_INITIALIZED);
}
static CK_RV
sys_C_VerifyInit (CK_SESSION_HANDLE handle,
CK_MECHANISM_PTR mechanism,
CK_OBJECT_HANDLE key)
{
return_val_if_reached (CKR_MECHANISM_INVALID);
}
static CK_RV
sys_C_Verify (CK_SESSION_HANDLE handle,
CK_BYTE_PTR data,
CK_ULONG data_len,
CK_BYTE_PTR signature,
CK_ULONG signature_len)
{
return_val_if_reached (CKR_OPERATION_NOT_INITIALIZED);
}
static CK_RV
sys_C_VerifyUpdate (CK_SESSION_HANDLE handle,
CK_BYTE_PTR part,
CK_ULONG part_len)
{
return_val_if_reached (CKR_OPERATION_NOT_INITIALIZED);
}
static CK_RV
sys_C_VerifyFinal (CK_SESSION_HANDLE handle,
CK_BYTE_PTR signature,
CK_ULONG signature_len)
{
return_val_if_reached (CKR_OPERATION_NOT_INITIALIZED);
}
static CK_RV
sys_C_VerifyRecoverInit (CK_SESSION_HANDLE handle,
CK_MECHANISM_PTR mechanism,
CK_OBJECT_HANDLE key)
{
return_val_if_reached (CKR_MECHANISM_INVALID);
}
static CK_RV
sys_C_VerifyRecover (CK_SESSION_HANDLE handle,
CK_BYTE_PTR signature,
CK_ULONG signature_len,
CK_BYTE_PTR data,
CK_ULONG_PTR data_len)
{
return_val_if_reached (CKR_OPERATION_NOT_INITIALIZED);
}
static CK_RV
sys_C_DigestEncryptUpdate (CK_SESSION_HANDLE handle,
CK_BYTE_PTR part,
CK_ULONG part_len,
CK_BYTE_PTR enc_part,
CK_ULONG_PTR enc_part_len)
{
return_val_if_reached (CKR_OPERATION_NOT_INITIALIZED);
}
static CK_RV
sys_C_DecryptDigestUpdate (CK_SESSION_HANDLE handle,
CK_BYTE_PTR enc_part,
CK_ULONG enc_part_len,
CK_BYTE_PTR part,
CK_ULONG_PTR part_len)
{
return_val_if_reached (CKR_OPERATION_NOT_INITIALIZED);
}
static CK_RV
sys_C_SignEncryptUpdate (CK_SESSION_HANDLE handle,
CK_BYTE_PTR part,
CK_ULONG part_len,
CK_BYTE_PTR enc_part,
CK_ULONG_PTR enc_part_len)
{
return_val_if_reached (CKR_OPERATION_NOT_INITIALIZED);
}
static CK_RV
sys_C_DecryptVerifyUpdate (CK_SESSION_HANDLE handle,
CK_BYTE_PTR enc_part,
CK_ULONG enc_part_len,
CK_BYTE_PTR part,
CK_ULONG_PTR part_len)
{
return_val_if_reached (CKR_OPERATION_NOT_INITIALIZED);
}
static CK_RV
sys_C_GenerateKey (CK_SESSION_HANDLE handle,
CK_MECHANISM_PTR mechanism,
CK_ATTRIBUTE_PTR template,
CK_ULONG count,
CK_OBJECT_HANDLE_PTR key)
{
return_val_if_reached (CKR_MECHANISM_INVALID);
}
static CK_RV
sys_C_GenerateKeyPair (CK_SESSION_HANDLE handle,
CK_MECHANISM_PTR mechanism,
CK_ATTRIBUTE_PTR pub_template,
CK_ULONG pub_count,
CK_ATTRIBUTE_PTR priv_template,
CK_ULONG priv_count,
CK_OBJECT_HANDLE_PTR pub_key,
CK_OBJECT_HANDLE_PTR priv_key)
{
return_val_if_reached (CKR_MECHANISM_INVALID);
}
static CK_RV
sys_C_WrapKey (CK_SESSION_HANDLE handle,
CK_MECHANISM_PTR mechanism,
CK_OBJECT_HANDLE wrapping_key,
CK_OBJECT_HANDLE key,
CK_BYTE_PTR wrapped_key,
CK_ULONG_PTR wrapped_key_len)
{
return_val_if_reached (CKR_MECHANISM_INVALID);
}
static CK_RV
sys_C_UnwrapKey (CK_SESSION_HANDLE handle,
CK_MECHANISM_PTR mechanism,
CK_OBJECT_HANDLE unwrapping_key,
CK_BYTE_PTR wrapped_key,
CK_ULONG wrapped_key_len,
CK_ATTRIBUTE_PTR template,
CK_ULONG count,
CK_OBJECT_HANDLE_PTR key)
{
return_val_if_reached (CKR_MECHANISM_INVALID);
}
static CK_RV
sys_C_DeriveKey (CK_SESSION_HANDLE handle,
CK_MECHANISM_PTR mechanism,
CK_OBJECT_HANDLE base_key,
CK_ATTRIBUTE_PTR template,
CK_ULONG count,
CK_OBJECT_HANDLE_PTR key)
{
return_val_if_reached (CKR_MECHANISM_INVALID);
}
static CK_RV
sys_C_SeedRandom (CK_SESSION_HANDLE handle,
CK_BYTE_PTR seed,
CK_ULONG seed_len)
{
return_val_if_reached (CKR_RANDOM_NO_RNG);
}
static CK_RV
sys_C_GenerateRandom (CK_SESSION_HANDLE handle,
CK_BYTE_PTR random_data,
CK_ULONG random_len)
{
return_val_if_reached (CKR_RANDOM_NO_RNG);
}
/* --------------------------------------------------------------------
* MODULE ENTRY POINT
*/
static CK_FUNCTION_LIST sys_function_list = {
{ CRYPTOKI_VERSION_MAJOR, CRYPTOKI_VERSION_MINOR }, /* version */
sys_C_Initialize,
sys_C_Finalize,
sys_C_GetInfo,
sys_C_GetFunctionList,
sys_C_GetSlotList,
sys_C_GetSlotInfo,
sys_C_GetTokenInfo,
sys_C_GetMechanismList,
sys_C_GetMechanismInfo,
sys_C_InitToken,
sys_C_InitPIN,
sys_C_SetPIN,
sys_C_OpenSession,
sys_C_CloseSession,
sys_C_CloseAllSessions,
sys_C_GetSessionInfo,
sys_C_GetOperationState,
sys_C_SetOperationState,
sys_C_Login,
sys_C_Logout,
sys_C_CreateObject,
sys_C_CopyObject,
sys_C_DestroyObject,
sys_C_GetObjectSize,
sys_C_GetAttributeValue,
sys_C_SetAttributeValue,
sys_C_FindObjectsInit,
sys_C_FindObjects,
sys_C_FindObjectsFinal,
sys_C_EncryptInit,
sys_C_Encrypt,
sys_C_EncryptUpdate,
sys_C_EncryptFinal,
sys_C_DecryptInit,
sys_C_Decrypt,
sys_C_DecryptUpdate,
sys_C_DecryptFinal,
sys_C_DigestInit,
sys_C_Digest,
sys_C_DigestUpdate,
sys_C_DigestKey,
sys_C_DigestFinal,
sys_C_SignInit,
sys_C_Sign,
sys_C_SignUpdate,
sys_C_SignFinal,
sys_C_SignRecoverInit,
sys_C_SignRecover,
sys_C_VerifyInit,
sys_C_Verify,
sys_C_VerifyUpdate,
sys_C_VerifyFinal,
sys_C_VerifyRecoverInit,
sys_C_VerifyRecover,
sys_C_DigestEncryptUpdate,
sys_C_DecryptDigestUpdate,
sys_C_SignEncryptUpdate,
sys_C_DecryptVerifyUpdate,
sys_C_GenerateKey,
sys_C_GenerateKeyPair,
sys_C_WrapKey,
sys_C_UnwrapKey,
sys_C_DeriveKey,
sys_C_SeedRandom,
sys_C_GenerateRandom,
sys_C_GetFunctionStatus,
sys_C_CancelFunction,
sys_C_WaitForSlotEvent
};
#ifdef OS_WIN32
__declspec(dllexport)
#endif
CK_RV
C_GetFunctionList (CK_FUNCTION_LIST_PTR_PTR list)
{
p11_library_init_once ();
return sys_C_GetFunctionList (list);
}
CK_ULONG
p11_module_next_id (void)
{
static CK_ULONG unique = 0x10;
return (unique)++;
}
#ifdef OS_UNIX
void p11_trust_module_init (void);
void p11_trust_module_fini (void);
#ifdef __GNUC__
__attribute__((constructor))
#endif
void
p11_trust_module_init (void)
{
p11_library_init_once ();
}
#ifdef __GNUC__
__attribute__((destructor))
#endif
void
p11_trust_module_fini (void)
{
p11_library_uninit ();
}
#endif /* OS_UNIX */
#ifdef OS_WIN32
BOOL WINAPI DllMain (HINSTANCE, DWORD, LPVOID);
BOOL WINAPI
DllMain (HINSTANCE instance,
DWORD reason,
LPVOID reserved)
{
switch (reason) {
case DLL_PROCESS_ATTACH:
p11_library_init ();
break;
case DLL_THREAD_DETACH:
p11_library_thread_cleanup ();
break;
case DLL_PROCESS_DETACH:
p11_library_uninit ();
break;
default:
break;
}
return TRUE;
}
#endif /* OS_WIN32 */
|