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
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
|
/*
* gnome-keyring
*
* Copyright (C) 2010 Stefan Walter
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This program 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 License for more details.
*
* You should have received a copy of the GNU Lesser General
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
* 02111-1307, USA.
*/
#include "config.h"
#include "gck.h"
#include "gck-mock.h"
#include "pkcs11/pkcs11.h"
#include "pkcs11/pkcs11i.h"
#include <glib.h>
#include <string.h>
/*
* This is *NOT* how you'd want to implement a PKCS#11 module. This
* fake module simply provides enough for gck library to test against.
* It doesn't pass any tests, or behave as expected from a PKCS#11 module.
*/
static gboolean initialized = FALSE;
static gchar *the_pin = NULL;
static gulong n_the_pin = 0;
static gboolean logged_in = FALSE;
static CK_USER_TYPE user_type = 0;
static CK_FUNCTION_LIST functionList;
typedef enum _Operation {
OP_FIND = 1,
OP_CRYPTO
} Operation;
typedef struct _Session {
CK_SESSION_HANDLE handle;
CK_SESSION_INFO info;
GHashTable *objects;
Operation operation;
/* For find operations */
GList *matches;
/* For crypto operations */
CK_OBJECT_HANDLE crypto_key;
CK_ATTRIBUTE_TYPE crypto_method;
CK_MECHANISM_TYPE crypto_mechanism;
CK_BBOOL want_context_login;
/* For 'signing' with CKM_MOCK_PREFIX */
CK_BYTE sign_prefix[128];
CK_ULONG n_sign_prefix;
} Session;
static guint unique_identifier = 100;
static GHashTable *the_sessions = NULL;
static GHashTable *the_objects = NULL;
enum {
PRIVATE_KEY_CAPITALIZE = 3,
PUBLIC_KEY_CAPITALIZE = 4,
PRIVATE_KEY_PREFIX = 5,
PUBLIC_KEY_PREFIX = 6
};
#define SIGNED_PREFIX "signed-prefix:"
static void
free_session (gpointer data)
{
Session *sess = (Session*)data;
if (sess)
g_hash_table_destroy (sess->objects);
g_free (sess);
}
static GckAttributes*
lookup_object (Session *session, CK_OBJECT_HANDLE hObject)
{
GckAttributes *attrs;
attrs = g_hash_table_lookup (the_objects, GUINT_TO_POINTER (hObject));
if (!attrs)
attrs = g_hash_table_lookup (session->objects, GUINT_TO_POINTER (hObject));
return attrs;
}
CK_OBJECT_HANDLE
gck_mock_module_take_object (GckAttributes *attrs)
{
gboolean token;
guint handle;
g_return_val_if_fail (the_objects, 0);
handle = ++unique_identifier;
if (gck_attributes_find_boolean (attrs, CKA_TOKEN, &token))
g_return_val_if_fail (token == TRUE, 0);
else
gck_attributes_add_boolean (attrs, CKA_TOKEN, TRUE);
g_hash_table_insert (the_objects, GUINT_TO_POINTER (handle), attrs);
return handle;
}
void
gck_mock_module_enumerate_objects (CK_SESSION_HANDLE handle, GckMockEnumerator func,
gpointer user_data)
{
GHashTableIter iter;
gpointer key;
gpointer value;
Session *session;
g_assert (the_objects);
g_assert (func);
/* Token objects */
g_hash_table_iter_init (&iter, the_objects);
while (g_hash_table_iter_next (&iter, &key, &value)) {
if (!(func) (GPOINTER_TO_UINT (key), value, user_data))
return;
}
/* session objects */
if (handle) {
session = g_hash_table_lookup (the_sessions, GUINT_TO_POINTER (handle));
if (session) {
g_hash_table_iter_init (&iter, session->objects);
while (g_hash_table_iter_next (&iter, &key, &value)) {
if (!(func) (GPOINTER_TO_UINT (key), value, user_data))
return;
}
}
}
}
typedef struct _FindObject {
CK_ATTRIBUTE_PTR attrs;
CK_ULONG n_attrs;
CK_OBJECT_HANDLE object;
} FindObject;
static gboolean
enumerate_and_find_object (CK_OBJECT_HANDLE object, GckAttributes *attrs, gpointer user_data)
{
FindObject *ctx = user_data;
CK_ATTRIBUTE_PTR match;
GckAttribute *attr;
CK_ULONG i;
for (i = 0; i < ctx->n_attrs; ++i) {
match = ctx->attrs + i;
attr = gck_attributes_find (attrs, match->type);
if (!attr)
return TRUE; /* Continue */
if (attr->length != match->ulValueLen ||
memcmp (attr->value, match->pValue, attr->length) != 0)
return TRUE; /* Continue */
}
ctx->object = object;
return FALSE; /* Stop iteration */
}
CK_OBJECT_HANDLE
gck_mock_module_find_object (CK_SESSION_HANDLE session, CK_ATTRIBUTE_PTR attrs, CK_ULONG n_attrs)
{
FindObject ctx;
ctx.attrs = attrs;
ctx.n_attrs = n_attrs;
ctx.object = 0;
gck_mock_module_enumerate_objects (session, enumerate_and_find_object, &ctx);
return ctx.object;
}
static gboolean
enumerate_and_count_objects (CK_OBJECT_HANDLE object, GckAttributes *attrs, gpointer user_data)
{
guint *n_objects = user_data;
++(*n_objects);
return TRUE; /* Continue */
}
guint
gck_mock_module_count_objects (CK_SESSION_HANDLE session)
{
guint n_objects = 0;
gck_mock_module_enumerate_objects (session, enumerate_and_count_objects, &n_objects);
return n_objects;
}
void
gck_mock_module_set_object (CK_OBJECT_HANDLE object, CK_ATTRIBUTE_PTR attrs,
CK_ULONG n_attrs)
{
CK_ULONG i;
GckAttributes *atts;
GckAttribute *attr;
CK_ATTRIBUTE_PTR set;
g_return_if_fail (object != 0);
g_return_if_fail (the_objects);
atts = g_hash_table_lookup (the_objects, GUINT_TO_POINTER (object));
g_return_if_fail (atts);
for (i = 0; i < n_attrs; ++i) {
set = attrs + i;
attr = gck_attributes_find (atts, set->type);
if (!attr) {
gck_attributes_add_data (atts, set->type, set->pValue, set->ulValueLen);
} else {
gck_attribute_clear (attr);
gck_attribute_init (attr, set->type, set->pValue, set->ulValueLen);
}
}
}
void
gck_mock_module_set_pin (const gchar *password)
{
g_free (the_pin);
the_pin = g_strdup (password);
n_the_pin = strlen (password);
}
CK_RV
gck_mock_C_Initialize (CK_VOID_PTR pInitArgs)
{
GckAttributes *attrs;
CK_ULONG value;
CK_C_INITIALIZE_ARGS_PTR args;
g_return_val_if_fail (initialized == FALSE, CKR_CRYPTOKI_ALREADY_INITIALIZED);
args = (CK_C_INITIALIZE_ARGS_PTR)pInitArgs;
if (args) {
g_return_val_if_fail(
(args->CreateMutex == NULL && args->DestroyMutex == NULL &&
args->LockMutex == NULL && args->UnlockMutex == NULL) ||
(args->CreateMutex != NULL && args->DestroyMutex != NULL &&
args->LockMutex != NULL && args->UnlockMutex != NULL),
CKR_ARGUMENTS_BAD);
/* Flags should allow OS locking and os threads */
g_return_val_if_fail ((args->flags & CKF_OS_LOCKING_OK), CKR_CANT_LOCK);
g_return_val_if_fail ((args->flags & CKF_LIBRARY_CANT_CREATE_OS_THREADS) == 0, CKR_NEED_TO_CREATE_THREADS);
}
the_pin = g_strdup ("booo");
n_the_pin = strlen (the_pin);
the_sessions = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, free_session);
the_objects = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, (GDestroyNotify)gck_attributes_unref);
/* Our token object */
attrs = gck_attributes_new ();
gck_attributes_add_ulong (attrs, CKA_CLASS, CKO_DATA);
gck_attributes_add_string (attrs, CKA_LABEL, "TEST LABEL");
g_hash_table_insert (the_objects, GUINT_TO_POINTER (2), attrs);
/* Private capitalize key */
value = CKM_MOCK_CAPITALIZE;
attrs = gck_attributes_new ();
gck_attributes_add_ulong (attrs, CKA_CLASS, CKO_PRIVATE_KEY);
gck_attributes_add_string (attrs, CKA_LABEL, "Private Capitalize Key");
gck_attributes_add_data (attrs, CKA_ALLOWED_MECHANISMS, &value, sizeof (value));
gck_attributes_add_boolean (attrs, CKA_DECRYPT, CK_TRUE);
gck_attributes_add_boolean (attrs, CKA_PRIVATE, CK_TRUE);
gck_attributes_add_boolean (attrs, CKA_WRAP, CK_TRUE);
gck_attributes_add_boolean (attrs, CKA_UNWRAP, CK_TRUE);
gck_attributes_add_boolean (attrs, CKA_DERIVE, CK_TRUE);
gck_attributes_add_string (attrs, CKA_VALUE, "value");
gck_attributes_add_string (attrs, CKA_GNOME_UNIQUE, "unique1");
g_hash_table_insert (the_objects, GUINT_TO_POINTER (PRIVATE_KEY_CAPITALIZE), attrs);
/* Public capitalize key */
value = CKM_MOCK_CAPITALIZE;
attrs = gck_attributes_new ();
gck_attributes_add_ulong (attrs, CKA_CLASS, CKO_PUBLIC_KEY);
gck_attributes_add_string (attrs, CKA_LABEL, "Public Capitalize Key");
gck_attributes_add_data (attrs, CKA_ALLOWED_MECHANISMS, &value, sizeof (value));
gck_attributes_add_boolean (attrs, CKA_ENCRYPT, CK_TRUE);
gck_attributes_add_boolean (attrs, CKA_PRIVATE, CK_FALSE);
gck_attributes_add_string (attrs, CKA_VALUE, "value");
gck_attributes_add_string (attrs, CKA_GNOME_UNIQUE, "unique2");
g_hash_table_insert (the_objects, GUINT_TO_POINTER (PUBLIC_KEY_CAPITALIZE), attrs);
/* Private prefix key */
value = CKM_MOCK_PREFIX;
attrs = gck_attributes_new ();
gck_attributes_add_ulong (attrs, CKA_CLASS, CKO_PRIVATE_KEY);
gck_attributes_add_string (attrs, CKA_LABEL, "Private prefix key");
gck_attributes_add_data (attrs, CKA_ALLOWED_MECHANISMS, &value, sizeof (value));
gck_attributes_add_boolean (attrs, CKA_SIGN, CK_TRUE);
gck_attributes_add_boolean (attrs, CKA_PRIVATE, CK_TRUE);
gck_attributes_add_boolean (attrs, CKA_ALWAYS_AUTHENTICATE, CK_TRUE);
gck_attributes_add_string (attrs, CKA_VALUE, "value");
gck_attributes_add_string (attrs, CKA_GNOME_UNIQUE, "unique3");
g_hash_table_insert (the_objects, GUINT_TO_POINTER (PRIVATE_KEY_PREFIX), attrs);
/* Private prefix key */
value = CKM_MOCK_PREFIX;
attrs = gck_attributes_new ();
gck_attributes_add_ulong (attrs, CKA_CLASS, CKO_PUBLIC_KEY);
gck_attributes_add_string (attrs, CKA_LABEL, "Public prefix key");
gck_attributes_add_data (attrs, CKA_ALLOWED_MECHANISMS, &value, sizeof (value));
gck_attributes_add_boolean (attrs, CKA_VERIFY, CK_TRUE);
gck_attributes_add_boolean (attrs, CKA_PRIVATE, CK_FALSE);
gck_attributes_add_string (attrs, CKA_VALUE, "value");
gck_attributes_add_string (attrs, CKA_GNOME_UNIQUE, "unique4");
g_hash_table_insert (the_objects, GUINT_TO_POINTER (PUBLIC_KEY_PREFIX), attrs);
initialized = TRUE;
return CKR_OK;
}
CK_RV
gck_mock_validate_and_C_Initialize (CK_VOID_PTR pInitArgs)
{
CK_C_INITIALIZE_ARGS_PTR args;
void *mutex;
CK_RV rv;
args = (CK_C_INITIALIZE_ARGS_PTR)pInitArgs;
if (args) {
g_assert ((args->CreateMutex) (NULL) == CKR_ARGUMENTS_BAD && "CreateMutex succeeded wrong");
g_assert ((args->DestroyMutex) (NULL) == CKR_MUTEX_BAD && "DestroyMutex succeeded wrong");
g_assert ((args->LockMutex) (NULL) == CKR_MUTEX_BAD && "LockMutex succeeded wrong");
g_assert ((args->UnlockMutex) (NULL) == CKR_MUTEX_BAD && "UnlockMutex succeeded wrong");
/* Try to create an actual mutex */
rv = (args->CreateMutex) (&mutex);
g_assert (rv == CKR_OK && "CreateMutex g_assert_not_reacheded");
g_assert (mutex != NULL && "CreateMutex created null mutex");
/* Try and lock the mutex */
rv = (args->LockMutex) (mutex);
g_assert (rv == CKR_OK && "LockMutex g_assert_not_reacheded");
/* Try and unlock the mutex */
rv = (args->UnlockMutex) (mutex);
g_assert (rv == CKR_OK && "UnlockMutex g_assert_not_reacheded");
/* Try and destroy the mutex */
rv = (args->DestroyMutex) (mutex);
g_assert (rv == CKR_OK && "DestroyMutex g_assert_not_reacheded");
}
return gck_mock_C_Initialize (pInitArgs);
}
CK_RV
gck_mock_C_Finalize (CK_VOID_PTR pReserved)
{
g_return_val_if_fail (pReserved == NULL, CKR_ARGUMENTS_BAD);
g_return_val_if_fail (initialized == TRUE, CKR_CRYPTOKI_NOT_INITIALIZED);
initialized = FALSE;
logged_in = FALSE;
g_hash_table_destroy (the_objects);
the_objects = NULL;
g_hash_table_destroy (the_sessions);
the_sessions = NULL;
g_free (the_pin);
return CKR_OK;
}
static const CK_INFO TEST_INFO = {
{ CRYPTOKI_VERSION_MAJOR, CRYPTOKI_VERSION_MINOR },
"TEST MANUFACTURER ",
0,
"TEST LIBRARY ",
{ 45, 145 }
};
CK_RV
gck_mock_C_GetInfo (CK_INFO_PTR pInfo)
{
g_return_val_if_fail (pInfo, CKR_ARGUMENTS_BAD);
memcpy (pInfo, &TEST_INFO, sizeof (*pInfo));
return CKR_OK;
}
CK_RV
gck_mock_C_GetFunctionList (CK_FUNCTION_LIST_PTR_PTR list)
{
g_return_val_if_fail (list, CKR_ARGUMENTS_BAD);
*list = &functionList;
return CKR_OK;
}
/*
* Two slots
* ONE: token present
* TWO: token not present
*/
CK_RV
gck_mock_C_GetSlotList (CK_BBOOL tokenPresent, CK_SLOT_ID_PTR pSlotList, CK_ULONG_PTR pulCount)
{
CK_ULONG count;
g_return_val_if_fail (pulCount, CKR_ARGUMENTS_BAD);
count = tokenPresent ? 1 : 2;
/* Application only wants to know the number of slots. */
if (pSlotList == NULL) {
*pulCount = count;
return CKR_OK;
}
if (*pulCount < count)
g_return_val_if_reached (CKR_BUFFER_TOO_SMALL);
*pulCount = count;
pSlotList[0] = GCK_MOCK_SLOT_ONE_ID;
if (!tokenPresent)
pSlotList[1] = GCK_MOCK_SLOT_TWO_ID;
return CKR_OK;
}
/* Update gck-mock.h URIs when updating this */
static const CK_SLOT_INFO TEST_INFO_ONE = {
"TEST SLOT ",
"TEST MANUFACTURER ",
CKF_TOKEN_PRESENT | CKF_REMOVABLE_DEVICE,
{ 55, 155 },
{ 65, 165 },
};
/* Update gck-mock.h URIs when updating this */
static const CK_SLOT_INFO TEST_INFO_TWO = {
"TEST SLOT ",
"TEST MANUFACTURER ",
CKF_REMOVABLE_DEVICE,
{ 55, 155 },
{ 65, 165 },
};
CK_RV
gck_mock_C_GetSlotInfo (CK_SLOT_ID slotID, CK_SLOT_INFO_PTR pInfo)
{
g_return_val_if_fail (pInfo, CKR_ARGUMENTS_BAD);
if (slotID == GCK_MOCK_SLOT_ONE_ID) {
memcpy (pInfo, &TEST_INFO_ONE, sizeof (*pInfo));
return CKR_OK;
} else if (slotID == GCK_MOCK_SLOT_TWO_ID) {
memcpy (pInfo, &TEST_INFO_TWO, sizeof (*pInfo));
return CKR_OK;
} else {
g_return_val_if_reached (CKR_SLOT_ID_INVALID);
}
}
/* Update gck-mock.h URIs when updating this */
static const CK_TOKEN_INFO TEST_TOKEN_ONE = {
"TEST LABEL ",
"TEST MANUFACTURER ",
"TEST MODEL ",
"TEST SERIAL ",
CKF_LOGIN_REQUIRED | CKF_USER_PIN_INITIALIZED | CKF_CLOCK_ON_TOKEN | CKF_TOKEN_INITIALIZED,
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
{ 75, 175 },
{ 85, 185 },
{ '1', '9', '9', '9', '0', '5', '2', '5', '0', '9', '1', '9', '5', '9', '0', '0' }
};
CK_RV
gck_mock_C_GetTokenInfo (CK_SLOT_ID slotID, CK_TOKEN_INFO_PTR pInfo)
{
g_return_val_if_fail (pInfo != NULL, CKR_ARGUMENTS_BAD);
if (slotID == GCK_MOCK_SLOT_ONE_ID) {
memcpy (pInfo, &TEST_TOKEN_ONE, sizeof (*pInfo));
return CKR_OK;
} else if (slotID == GCK_MOCK_SLOT_TWO_ID) {
return CKR_TOKEN_NOT_PRESENT;
} else {
g_return_val_if_reached (CKR_SLOT_ID_INVALID);
}
}
CK_RV
gck_mock_fail_C_GetTokenInfo (CK_SLOT_ID slotID, CK_TOKEN_INFO_PTR pInfo)
{
return CKR_GENERAL_ERROR;
}
/*
* TWO mechanisms:
* CKM_MOCK_CAPITALIZE
* CKM_MOCK_PREFIX
*/
CK_RV
gck_mock_C_GetMechanismList (CK_SLOT_ID slotID, CK_MECHANISM_TYPE_PTR pMechanismList,
CK_ULONG_PTR pulCount)
{
g_return_val_if_fail (slotID == GCK_MOCK_SLOT_ONE_ID, CKR_SLOT_ID_INVALID);
g_return_val_if_fail (pulCount, CKR_ARGUMENTS_BAD);
/* Application only wants to know the number of slots. */
if (pMechanismList == NULL) {
*pulCount = 2;
return CKR_OK;
}
if (*pulCount != 2)
g_return_val_if_reached (CKR_BUFFER_TOO_SMALL);
pMechanismList[0] = CKM_MOCK_CAPITALIZE;
pMechanismList[1] = CKM_MOCK_PREFIX;
return CKR_OK;
}
static const CK_MECHANISM_INFO TEST_MECH_CAPITALIZE = {
512, 4096, 0
};
static const CK_MECHANISM_INFO TEST_MECH_PREFIX = {
2048, 2048, 0
};
CK_RV
gck_mock_C_GetMechanismInfo (CK_SLOT_ID slotID, CK_MECHANISM_TYPE type,
CK_MECHANISM_INFO_PTR pInfo)
{
g_return_val_if_fail (slotID == GCK_MOCK_SLOT_ONE_ID, CKR_SLOT_ID_INVALID);
g_return_val_if_fail (pInfo, CKR_ARGUMENTS_BAD);
if (type == CKM_MOCK_CAPITALIZE) {
memcpy (pInfo, &TEST_MECH_CAPITALIZE, sizeof (*pInfo));
return CKR_OK;
} else if (type == CKM_MOCK_PREFIX) {
memcpy (pInfo, &TEST_MECH_PREFIX, sizeof (*pInfo));
return CKR_OK;
} else {
g_return_val_if_reached (CKR_MECHANISM_INVALID);
}
}
CK_RV
gck_mock_specific_args_C_InitToken (CK_SLOT_ID slotID, CK_UTF8CHAR_PTR pPin, CK_ULONG ulPinLen,
CK_UTF8CHAR_PTR pLabel)
{
g_return_val_if_fail (slotID == GCK_MOCK_SLOT_ONE_ID, CKR_SLOT_ID_INVALID);
g_return_val_if_fail (pPin, CKR_PIN_INVALID);
g_return_val_if_fail (strlen ("TEST PIN") == ulPinLen, CKR_PIN_INVALID);
g_return_val_if_fail (strncmp ((gchar*)pPin, "TEST PIN", ulPinLen) == 0, CKR_PIN_INVALID);
g_return_val_if_fail (pLabel != NULL, CKR_PIN_INVALID);
g_return_val_if_fail (strcmp ((gchar*)pPin, "TEST LABEL") == 0, CKR_PIN_INVALID);
g_free (the_pin);
the_pin = g_strndup ((gchar*)pPin, ulPinLen);
n_the_pin = ulPinLen;
return CKR_OK;
}
CK_RV
gck_mock_unsupported_C_WaitForSlotEvent (CK_FLAGS flags, CK_SLOT_ID_PTR pSlot, CK_VOID_PTR pReserved)
{
return CKR_FUNCTION_NOT_SUPPORTED;
}
CK_RV
gck_mock_C_OpenSession (CK_SLOT_ID slotID, CK_FLAGS flags, CK_VOID_PTR pApplication,
CK_NOTIFY Notify, CK_SESSION_HANDLE_PTR phSession)
{
Session *sess;
g_return_val_if_fail (slotID == GCK_MOCK_SLOT_ONE_ID || slotID == GCK_MOCK_SLOT_TWO_ID, CKR_SLOT_ID_INVALID);
g_return_val_if_fail (phSession != NULL, CKR_ARGUMENTS_BAD);
g_return_val_if_fail ((flags & CKF_SERIAL_SESSION) == CKF_SERIAL_SESSION, CKR_SESSION_PARALLEL_NOT_SUPPORTED);
sess = g_new0 (Session, 1);
sess->handle = ++unique_identifier;
sess->info.flags = flags;
sess->info.slotID = slotID;
sess->info.state = 0;
sess->info.ulDeviceError = 1414;
sess->objects = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, (GDestroyNotify)gck_attributes_unref);
*phSession = sess->handle;
g_hash_table_replace (the_sessions, GUINT_TO_POINTER (sess->handle), sess);
return CKR_OK;
}
CK_RV
gck_mock_fail_C_OpenSession (CK_SLOT_ID slotID, CK_FLAGS flags, CK_VOID_PTR pApplication,
CK_NOTIFY Notify, CK_SESSION_HANDLE_PTR phSession)
{
return CKR_GENERAL_ERROR;
}
CK_RV
gck_mock_C_CloseSession (CK_SESSION_HANDLE hSession)
{
Session *session;
session = g_hash_table_lookup (the_sessions, GUINT_TO_POINTER (hSession));
g_return_val_if_fail (session, CKR_SESSION_HANDLE_INVALID);
g_hash_table_remove (the_sessions, GUINT_TO_POINTER (hSession));
return CKR_OK;
}
CK_RV
gck_mock_C_CloseAllSessions (CK_SLOT_ID slotID)
{
g_return_val_if_fail (slotID == GCK_MOCK_SLOT_ONE_ID, CKR_SLOT_ID_INVALID);
g_hash_table_remove_all (the_sessions);
return CKR_OK;
}
CK_RV
gck_mock_C_GetFunctionStatus (CK_SESSION_HANDLE hSession)
{
return CKR_FUNCTION_NOT_PARALLEL;
}
CK_RV
gck_mock_C_CancelFunction (CK_SESSION_HANDLE hSession)
{
return CKR_FUNCTION_NOT_PARALLEL;
}
CK_RV
gck_mock_C_GetSessionInfo (CK_SESSION_HANDLE hSession, CK_SESSION_INFO_PTR pInfo)
{
Session *session;
g_return_val_if_fail (pInfo != NULL, CKR_ARGUMENTS_BAD);
session = g_hash_table_lookup (the_sessions, GUINT_TO_POINTER (hSession));
g_assert (session != NULL && "No such session found");
if (!session)
return CKR_SESSION_HANDLE_INVALID;
if (logged_in) {
if (session->info.flags & CKF_RW_SESSION)
session->info.state = CKS_RW_USER_FUNCTIONS;
else
session->info.state = CKS_RO_USER_FUNCTIONS;
} else {
if (session->info.flags & CKF_RW_SESSION)
session->info.state = CKS_RW_PUBLIC_SESSION;
else
session->info.state = CKS_RO_PUBLIC_SESSION;
}
memcpy (pInfo, &session->info, sizeof (*pInfo));
return CKR_OK;
}
CK_RV
gck_mock_fail_C_GetSessionInfo (CK_SESSION_HANDLE hSession, CK_SESSION_INFO_PTR pInfo)
{
return CKR_GENERAL_ERROR;
}
CK_RV
gck_mock_C_InitPIN (CK_SESSION_HANDLE hSession, CK_UTF8CHAR_PTR pPin,
CK_ULONG ulPinLen)
{
Session *session;
session = g_hash_table_lookup (the_sessions, GUINT_TO_POINTER (hSession));
g_return_val_if_fail (session, CKR_SESSION_HANDLE_INVALID);
g_free (the_pin);
the_pin = g_strndup ((gchar*)pPin, ulPinLen);
n_the_pin = ulPinLen;
return CKR_OK;
}
CK_RV
gck_mock_C_SetPIN (CK_SESSION_HANDLE hSession, CK_UTF8CHAR_PTR pOldPin,
CK_ULONG ulOldLen, CK_UTF8CHAR_PTR pNewPin, CK_ULONG ulNewLen)
{
Session *session;
gchar *old;
session = g_hash_table_lookup (the_sessions, GUINT_TO_POINTER (hSession));
g_return_val_if_fail (session, CKR_SESSION_HANDLE_INVALID);
old = g_strndup ((gchar*)pOldPin, ulOldLen);
if (!old || !g_str_equal (old, the_pin))
return CKR_PIN_INCORRECT;
g_free (the_pin);
the_pin = g_strndup ((gchar*)pNewPin, ulNewLen);
n_the_pin = ulNewLen;
return CKR_OK;
}
CK_RV
gck_mock_unsupported_C_GetOperationState (CK_SESSION_HANDLE hSession, CK_BYTE_PTR pOperationState,
CK_ULONG_PTR pulOperationStateLen)
{
return CKR_FUNCTION_NOT_SUPPORTED;
}
CK_RV
gck_mock_unsupported_C_SetOperationState (CK_SESSION_HANDLE hSession, CK_BYTE_PTR pOperationState,
CK_ULONG ulOperationStateLen, CK_OBJECT_HANDLE hEncryptionKey,
CK_OBJECT_HANDLE hAuthenticationKey)
{
return CKR_FUNCTION_NOT_SUPPORTED;
}
CK_RV
gck_mock_C_Login (CK_SESSION_HANDLE hSession, CK_USER_TYPE userType,
CK_UTF8CHAR_PTR pPin, CK_ULONG pPinLen)
{
Session *session;
g_return_val_if_fail (userType == CKU_SO ||
userType == CKU_USER ||
userType == CKU_CONTEXT_SPECIFIC,
CKR_USER_TYPE_INVALID);
session = g_hash_table_lookup (the_sessions, GUINT_TO_POINTER (hSession));
g_return_val_if_fail (session != NULL, CKR_SESSION_HANDLE_INVALID);
g_return_val_if_fail (logged_in == FALSE, CKR_USER_ALREADY_LOGGED_IN);
if (!pPin)
return CKR_PIN_INCORRECT;
if (pPinLen != strlen (the_pin))
return CKR_PIN_INCORRECT;
if (strncmp ((gchar*)pPin, the_pin, pPinLen) != 0)
return CKR_PIN_INCORRECT;
if (userType == CKU_CONTEXT_SPECIFIC) {
g_return_val_if_fail (session->want_context_login == TRUE, CKR_OPERATION_NOT_INITIALIZED);
session->want_context_login = CK_FALSE;
} else {
logged_in = TRUE;
user_type = userType;
}
return CKR_OK;
}
CK_RV
gck_mock_C_Logout (CK_SESSION_HANDLE hSession)
{
Session *session;
session = g_hash_table_lookup (the_sessions, GUINT_TO_POINTER (hSession));
g_assert (session != NULL && "No such session found");
if (!session)
return CKR_SESSION_HANDLE_INVALID;
g_assert (logged_in && "Not logged in");
logged_in = FALSE;
user_type = 0;
return CKR_OK;
}
CK_RV
gck_mock_C_CreateObject (CK_SESSION_HANDLE hSession, CK_ATTRIBUTE_PTR pTemplate,
CK_ULONG ulCount, CK_OBJECT_HANDLE_PTR phObject)
{
GckAttributes *attrs;
Session *session;
gboolean token, priv;
CK_OBJECT_CLASS klass;
CK_OBJECT_HANDLE object;
GckAttribute *attr;
CK_ULONG i;
g_return_val_if_fail (phObject, CKR_ARGUMENTS_BAD);
session = g_hash_table_lookup (the_sessions, GUINT_TO_POINTER (hSession));
g_return_val_if_fail (session, CKR_SESSION_HANDLE_INVALID);
attrs = gck_attributes_new ();
for (i = 0; i < ulCount; ++i)
gck_attributes_add_data (attrs, pTemplate[i].type, pTemplate[i].pValue, pTemplate[i].ulValueLen);
if (gck_attributes_find_boolean (attrs, CKA_PRIVATE, &priv) && priv) {
if (!logged_in) {
gck_attributes_unref (attrs);
return CKR_USER_NOT_LOGGED_IN;
}
}
/* In order to create a credential we must check CK_VALUE */
if (gck_attributes_find_ulong (attrs, CKA_CLASS, &klass) && klass == CKO_G_CREDENTIAL) {
if (gck_attributes_find_ulong (attrs, CKA_G_OBJECT, &object)) {
attr = gck_attributes_find (attrs, CKA_VALUE);
if (!attr || attr->length != n_the_pin ||
memcmp (attr->value, the_pin, attr->length) != 0) {
gck_attributes_unref (attrs);
return CKR_PIN_INCORRECT;
}
}
}
*phObject = ++unique_identifier;
if (gck_attributes_find_boolean (attrs, CKA_TOKEN, &token) && token)
g_hash_table_insert (the_objects, GUINT_TO_POINTER (*phObject), attrs);
else
g_hash_table_insert (session->objects, GUINT_TO_POINTER (*phObject), attrs);
return CKR_OK;
}
CK_RV
gck_mock_fail_C_CreateObject (CK_SESSION_HANDLE hSession, CK_ATTRIBUTE_PTR pTemplate,
CK_ULONG ulCount, CK_OBJECT_HANDLE_PTR phObject)
{
/* Always fails */
return CKR_FUNCTION_FAILED;
}
CK_RV
gck_mock_unsupported_C_CopyObject (CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE hObject,
CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount,
CK_OBJECT_HANDLE_PTR phNewObject)
{
return CKR_FUNCTION_NOT_SUPPORTED;
}
CK_RV
gck_mock_C_DestroyObject (CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE hObject)
{
GckAttributes *attrs;
Session *session;
gboolean priv;
session = g_hash_table_lookup (the_sessions, GUINT_TO_POINTER (hSession));
g_return_val_if_fail (session, CKR_SESSION_HANDLE_INVALID);
attrs = lookup_object (session, hObject);
g_return_val_if_fail (attrs, CKR_OBJECT_HANDLE_INVALID);
if (gck_attributes_find_boolean (attrs, CKA_PRIVATE, &priv) && priv) {
if (!logged_in)
return CKR_USER_NOT_LOGGED_IN;
}
g_hash_table_remove (the_objects, GUINT_TO_POINTER (hObject));
g_hash_table_remove (session->objects, GUINT_TO_POINTER (hObject));
return CKR_OK;
}
CK_RV
gck_mock_unsupported_C_GetObjectSize (CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE hObject,
CK_ULONG_PTR pulSize)
{
return CKR_FUNCTION_NOT_SUPPORTED;
}
CK_RV
gck_mock_C_GetAttributeValue (CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE hObject,
CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount)
{
CK_ATTRIBUTE_PTR result;
CK_RV ret = CKR_OK;
GckAttributes *attrs;
GckAttribute *attr;
Session *session;
CK_ULONG i;
session = g_hash_table_lookup (the_sessions, GUINT_TO_POINTER (hSession));
g_return_val_if_fail (session, CKR_SESSION_HANDLE_INVALID);
attrs = lookup_object (session, hObject);
if (!attrs) {
g_assert_not_reached (); /* "invalid object handle passed" */
return CKR_OBJECT_HANDLE_INVALID;
}
for (i = 0; i < ulCount; ++i) {
result = pTemplate + i;
attr = gck_attributes_find (attrs, result->type);
if (!attr) {
result->ulValueLen = (CK_ULONG)-1;
ret = CKR_ATTRIBUTE_TYPE_INVALID;
continue;
}
if (!result->pValue) {
result->ulValueLen = attr->length;
continue;
}
if (result->ulValueLen >= attr->length) {
memcpy (result->pValue, attr->value, attr->length);
continue;
}
result->ulValueLen = (CK_ULONG)-1;
ret = CKR_BUFFER_TOO_SMALL;
}
return ret;
}
CK_RV
gck_mock_fail_C_GetAttributeValue (CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE hObject,
CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount)
{
return CKR_FUNCTION_FAILED;
}
CK_RV
gck_mock_C_SetAttributeValue (CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE hObject,
CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount)
{
Session *session;
GckAttributes *attrs;
CK_ATTRIBUTE_PTR set;
GckAttribute *attr;
CK_ULONG i;
session = g_hash_table_lookup (the_sessions, GUINT_TO_POINTER (hSession));
g_return_val_if_fail (session, CKR_SESSION_HANDLE_INVALID);
attrs = lookup_object (session, hObject);
g_return_val_if_fail (attrs, CKR_OBJECT_HANDLE_INVALID);
for (i = 0; i < ulCount; ++i) {
set = pTemplate + i;
attr = gck_attributes_find (attrs, set->type);
if (!attr) {
gck_attributes_add_data (attrs, set->type, set->pValue, set->ulValueLen);
} else {
gck_attribute_clear (attr);
gck_attribute_init (attr, set->type, set->pValue, set->ulValueLen);
}
}
return CKR_OK;
}
typedef struct _FindObjects {
CK_ATTRIBUTE_PTR template;
CK_ULONG count;
Session *session;
} FindObjects;
static gboolean
enumerate_and_find_objects (CK_OBJECT_HANDLE object, GckAttributes *attrs, gpointer user_data)
{
FindObjects *ctx = user_data;
CK_ATTRIBUTE_PTR match;
GckAttribute *attr;
CK_ULONG i;
for (i = 0; i < ctx->count; ++i) {
match = ctx->template + i;
attr = gck_attributes_find (attrs, match->type);
if (!attr)
return TRUE; /* Continue */
if (attr->length != match->ulValueLen ||
memcmp (attr->value, match->pValue, attr->length) != 0)
return TRUE; /* Continue */
}
ctx->session->matches = g_list_prepend (ctx->session->matches, GUINT_TO_POINTER (object));
return TRUE; /* Continue */
}
CK_RV
gck_mock_C_FindObjectsInit (CK_SESSION_HANDLE hSession, CK_ATTRIBUTE_PTR pTemplate,
CK_ULONG ulCount)
{
Session *session;
FindObjects ctx;
session = g_hash_table_lookup (the_sessions, GUINT_TO_POINTER (hSession));
g_return_val_if_fail (session != NULL, CKR_SESSION_HANDLE_INVALID);
/* Starting an operation, cancels any previous one */
if (session->operation != 0)
session->operation = 0;
session->operation = OP_FIND;
ctx.template = pTemplate;
ctx.count = ulCount;
ctx.session = session;
gck_mock_module_enumerate_objects (hSession, enumerate_and_find_objects, &ctx);
return CKR_OK;
}
CK_RV
gck_mock_fail_C_FindObjects (CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE_PTR phObject,
CK_ULONG ulMaxObjectCount, CK_ULONG_PTR pulObjectCount)
{
/* Always fails */
return CKR_FUNCTION_FAILED;
}
CK_RV
gck_mock_C_FindObjects (CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE_PTR phObject,
CK_ULONG ulMaxObjectCount, CK_ULONG_PTR pulObjectCount)
{
Session *session;
g_return_val_if_fail (phObject, CKR_ARGUMENTS_BAD);
g_return_val_if_fail (pulObjectCount, CKR_ARGUMENTS_BAD);
g_return_val_if_fail (ulMaxObjectCount != 0, CKR_ARGUMENTS_BAD);
session = g_hash_table_lookup (the_sessions, GUINT_TO_POINTER (hSession));
g_return_val_if_fail (session != NULL, CKR_SESSION_HANDLE_INVALID);
g_return_val_if_fail (session->operation == OP_FIND, CKR_OPERATION_NOT_INITIALIZED);
*pulObjectCount = 0;
while (ulMaxObjectCount > 0 && session->matches) {
*phObject = GPOINTER_TO_UINT (session->matches->data);
++phObject;
--ulMaxObjectCount;
++(*pulObjectCount);
session->matches = g_list_remove (session->matches, session->matches->data);
}
return CKR_OK;
}
CK_RV
gck_mock_C_FindObjectsFinal (CK_SESSION_HANDLE hSession)
{
Session *session;
session = g_hash_table_lookup (the_sessions, GUINT_TO_POINTER (hSession));
g_return_val_if_fail (session != NULL, CKR_SESSION_HANDLE_INVALID);
g_return_val_if_fail (session->operation == OP_FIND, CKR_OPERATION_NOT_INITIALIZED);
session->operation = 0;
g_list_free (session->matches);
session->matches = NULL;
return CKR_OK;
}
CK_RV
gck_mock_C_EncryptInit (CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism,
CK_OBJECT_HANDLE hKey)
{
Session *session;
session = g_hash_table_lookup (the_sessions, GUINT_TO_POINTER (hSession));
g_return_val_if_fail (session != NULL, CKR_SESSION_HANDLE_INVALID);
/* Starting an operation, cancels any previous one */
if (session->operation != 0)
session->operation = 0;
g_assert (pMechanism);
g_assert (pMechanism->mechanism == CKM_MOCK_CAPITALIZE);
g_assert (hKey == PUBLIC_KEY_CAPITALIZE);
session->operation = OP_CRYPTO;
session->crypto_method = CKA_ENCRYPT;
session->crypto_mechanism = CKM_MOCK_CAPITALIZE;
session->crypto_key = hKey;
return CKR_OK;
}
CK_RV
gck_mock_C_Encrypt (CK_SESSION_HANDLE hSession, CK_BYTE_PTR pData, CK_ULONG ulDataLen,
CK_BYTE_PTR pEncryptedData, CK_ULONG_PTR pulEncryptedDataLen)
{
Session *session;
CK_ULONG i;
g_return_val_if_fail (pData, CKR_DATA_INVALID);
g_return_val_if_fail (pulEncryptedDataLen, CKR_ARGUMENTS_BAD);
session = g_hash_table_lookup (the_sessions, GUINT_TO_POINTER (hSession));
g_return_val_if_fail (session != NULL, CKR_SESSION_HANDLE_INVALID);
g_return_val_if_fail (session->operation == OP_CRYPTO, CKR_OPERATION_NOT_INITIALIZED);
g_return_val_if_fail (session->crypto_method == CKA_ENCRYPT, CKR_OPERATION_NOT_INITIALIZED);
g_assert (session->crypto_mechanism == CKM_MOCK_CAPITALIZE);
g_assert (session->crypto_key == PUBLIC_KEY_CAPITALIZE);
if (!pEncryptedData) {
*pulEncryptedDataLen = ulDataLen;
return CKR_OK;
}
if (*pulEncryptedDataLen < ulDataLen) {
*pulEncryptedDataLen = ulDataLen;
return CKR_BUFFER_TOO_SMALL;
}
for (i = 0; i < ulDataLen; ++i)
pEncryptedData[i] = g_ascii_toupper (pData[i]);
*pulEncryptedDataLen = ulDataLen;
session->operation = 0;
session->crypto_method = 0;
session->crypto_mechanism = 0;
session->crypto_key = 0;
return CKR_OK;
}
CK_RV
gck_mock_unsupported_C_EncryptUpdate (CK_SESSION_HANDLE hSession, CK_BYTE_PTR pPart,
CK_ULONG ulPartLen, CK_BYTE_PTR pEncryptedPart,
CK_ULONG_PTR pulEncryptedPartLen)
{
return CKR_FUNCTION_NOT_SUPPORTED;
}
CK_RV
gck_mock_unsupported_C_EncryptFinal (CK_SESSION_HANDLE hSession, CK_BYTE_PTR pLastEncryptedPart,
CK_ULONG_PTR pulLastEncryptedPartLen)
{
return CKR_FUNCTION_NOT_SUPPORTED;
}
CK_RV
gck_mock_C_DecryptInit (CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism,
CK_OBJECT_HANDLE hKey)
{
Session *session;
session = g_hash_table_lookup (the_sessions, GUINT_TO_POINTER (hSession));
g_return_val_if_fail (session != NULL, CKR_SESSION_HANDLE_INVALID);
/* Starting an operation, cancels any previous one */
if (session->operation != 0)
session->operation = 0;
g_assert (pMechanism);
g_assert (pMechanism->mechanism == CKM_MOCK_CAPITALIZE);
g_assert (hKey == PRIVATE_KEY_CAPITALIZE);
session->operation = OP_CRYPTO;
session->crypto_method = CKA_DECRYPT;
session->crypto_mechanism = CKM_MOCK_CAPITALIZE;
session->crypto_key = hKey;
return CKR_OK;
}
CK_RV
gck_mock_C_Decrypt (CK_SESSION_HANDLE hSession, CK_BYTE_PTR pEncryptedData,
CK_ULONG ulEncryptedDataLen, CK_BYTE_PTR pData, CK_ULONG_PTR pulDataLen)
{
Session *session;
CK_ULONG i;
g_return_val_if_fail (pEncryptedData, CKR_ENCRYPTED_DATA_INVALID);
g_return_val_if_fail (pulDataLen, CKR_ARGUMENTS_BAD);
session = g_hash_table_lookup (the_sessions, GUINT_TO_POINTER (hSession));
g_return_val_if_fail (session != NULL, CKR_SESSION_HANDLE_INVALID);
g_return_val_if_fail (session->operation == OP_CRYPTO, CKR_OPERATION_NOT_INITIALIZED);
g_return_val_if_fail (session->crypto_method == CKA_DECRYPT, CKR_OPERATION_NOT_INITIALIZED);
g_assert (session->crypto_method == CKA_DECRYPT);
g_assert (session->crypto_mechanism == CKM_MOCK_CAPITALIZE);
g_assert (session->crypto_key == PRIVATE_KEY_CAPITALIZE);
if (!pData) {
*pulDataLen = ulEncryptedDataLen;
return CKR_OK;
}
if (*pulDataLen < ulEncryptedDataLen) {
*pulDataLen = ulEncryptedDataLen;
return CKR_BUFFER_TOO_SMALL;
}
for (i = 0; i < ulEncryptedDataLen; ++i)
pData[i] = g_ascii_tolower (pEncryptedData[i]);
*pulDataLen = ulEncryptedDataLen;
session->operation = 0;
session->crypto_method = 0;
session->crypto_mechanism = 0;
session->crypto_key = 0;
return CKR_OK;
}
CK_RV
gck_mock_unsupported_C_DecryptUpdate (CK_SESSION_HANDLE hSession, CK_BYTE_PTR pEncryptedPart,
CK_ULONG ulEncryptedPartLen, CK_BYTE_PTR pPart, CK_ULONG_PTR pulPartLen)
{
return CKR_FUNCTION_NOT_SUPPORTED;
}
CK_RV
gck_mock_unsupported_C_DecryptFinal (CK_SESSION_HANDLE hSession, CK_BYTE_PTR pLastPart,
CK_ULONG_PTR pulLastPartLen)
{
return CKR_FUNCTION_NOT_SUPPORTED;
}
CK_RV
gck_mock_unsupported_C_DigestInit (CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism)
{
return CKR_FUNCTION_NOT_SUPPORTED;
}
CK_RV
gck_mock_unsupported_C_Digest (CK_SESSION_HANDLE hSession, CK_BYTE_PTR pData, CK_ULONG ulDataLen,
CK_BYTE_PTR pDigest, CK_ULONG_PTR pulDigestLen)
{
return CKR_FUNCTION_NOT_SUPPORTED;
}
CK_RV
gck_mock_unsupported_C_DigestUpdate (CK_SESSION_HANDLE hSession, CK_BYTE_PTR pPart, CK_ULONG ulPartLen)
{
return CKR_FUNCTION_NOT_SUPPORTED;
}
CK_RV
gck_mock_unsupported_C_DigestKey (CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE hKey)
{
return CKR_FUNCTION_NOT_SUPPORTED;
}
CK_RV
gck_mock_unsupported_C_DigestFinal (CK_SESSION_HANDLE hSession, CK_BYTE_PTR pDigest,
CK_ULONG_PTR pulDigestLen)
{
return CKR_FUNCTION_NOT_SUPPORTED;
}
CK_RV
gck_mock_C_SignInit (CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism,
CK_OBJECT_HANDLE hKey)
{
Session *session;
session = g_hash_table_lookup (the_sessions, GUINT_TO_POINTER (hSession));
g_return_val_if_fail (session != NULL, CKR_SESSION_HANDLE_INVALID);
/* Starting an operation, cancels any previous one */
if (session->operation != 0)
session->operation = 0;
g_assert (pMechanism);
g_assert (pMechanism->mechanism == CKM_MOCK_PREFIX);
g_assert (hKey == PRIVATE_KEY_PREFIX);
session->operation = OP_CRYPTO;
session->crypto_method = CKA_SIGN;
session->crypto_mechanism = CKM_MOCK_PREFIX;
session->crypto_key = hKey;
if (pMechanism->pParameter) {
g_assert (pMechanism->ulParameterLen < sizeof (session->sign_prefix));
memcpy (session->sign_prefix, pMechanism->pParameter, pMechanism->ulParameterLen);
session->n_sign_prefix = pMechanism->ulParameterLen;
} else {
g_assert (strlen (SIGNED_PREFIX) + 1 < sizeof (session->sign_prefix));
strcpy ((gchar*)session->sign_prefix, SIGNED_PREFIX);
session->n_sign_prefix = strlen (SIGNED_PREFIX);
}
/* The private key has CKA_ALWAYS_AUTHENTICATE above */
session->want_context_login = CK_TRUE;
return CKR_OK;
}
CK_RV
gck_mock_C_Sign (CK_SESSION_HANDLE hSession, CK_BYTE_PTR pData, CK_ULONG ulDataLen,
CK_BYTE_PTR pSignature, CK_ULONG_PTR pulSignatureLen)
{
Session *session;
CK_ULONG length;
g_return_val_if_fail (pData, CKR_DATA_INVALID);
g_return_val_if_fail (pulSignatureLen, CKR_ARGUMENTS_BAD);
session = g_hash_table_lookup (the_sessions, GUINT_TO_POINTER (hSession));
g_return_val_if_fail (session != NULL, CKR_SESSION_HANDLE_INVALID);
g_return_val_if_fail (session->operation == OP_CRYPTO, CKR_OPERATION_NOT_INITIALIZED);
g_return_val_if_fail (session->crypto_method == CKA_SIGN, CKR_OPERATION_NOT_INITIALIZED);
if (session->want_context_login)
return CKR_USER_NOT_LOGGED_IN;
g_assert (session->crypto_method == CKA_SIGN);
g_assert (session->crypto_mechanism == CKM_MOCK_PREFIX);
g_assert (session->crypto_key == PRIVATE_KEY_PREFIX);
length = session->n_sign_prefix + ulDataLen;
if (!pSignature) {
*pulSignatureLen = length;
return CKR_OK;
}
if (*pulSignatureLen < length) {
*pulSignatureLen = length;
return CKR_BUFFER_TOO_SMALL;
}
memcpy (pSignature, session->sign_prefix, session->n_sign_prefix);
memcpy (pSignature + session->n_sign_prefix, pData, ulDataLen);
*pulSignatureLen = length;
session->operation = 0;
session->crypto_method = 0;
session->crypto_mechanism = 0;
session->crypto_key = 0;
return CKR_OK;
}
CK_RV
gck_mock_unsupported_C_SignUpdate (CK_SESSION_HANDLE hSession, CK_BYTE_PTR pPart, CK_ULONG ulPartLen)
{
return CKR_FUNCTION_NOT_SUPPORTED;
}
CK_RV
gck_mock_unsupported_C_SignFinal (CK_SESSION_HANDLE hSession, CK_BYTE_PTR pSignature,
CK_ULONG_PTR pulSignatureLen)
{
return CKR_FUNCTION_NOT_SUPPORTED;
}
CK_RV
gck_mock_unsupported_C_SignRecoverInit (CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism,
CK_OBJECT_HANDLE hKey)
{
return CKR_FUNCTION_NOT_SUPPORTED;
}
CK_RV
gck_mock_unsupported_C_SignRecover (CK_SESSION_HANDLE hSession, CK_BYTE_PTR pData, CK_ULONG ulDataLen,
CK_BYTE_PTR pSignature, CK_ULONG_PTR pulSignatureLen)
{
return CKR_FUNCTION_NOT_SUPPORTED;
}
CK_RV
gck_mock_C_VerifyInit (CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism,
CK_OBJECT_HANDLE hKey)
{
Session *session;
session = g_hash_table_lookup (the_sessions, GUINT_TO_POINTER (hSession));
g_return_val_if_fail (session != NULL, CKR_SESSION_HANDLE_INVALID);
/* Starting an operation, cancels any previous one */
if (session->operation != 0)
session->operation = 0;
g_assert (pMechanism);
g_assert (pMechanism->mechanism == CKM_MOCK_PREFIX);
g_assert (hKey == PUBLIC_KEY_PREFIX);
session->operation = OP_CRYPTO;
session->crypto_method = CKA_VERIFY;
session->crypto_mechanism = CKM_MOCK_PREFIX;
session->crypto_key = hKey;
if (pMechanism->pParameter) {
g_assert (pMechanism->ulParameterLen < sizeof (session->sign_prefix));
memcpy (session->sign_prefix, pMechanism->pParameter, pMechanism->ulParameterLen);
session->n_sign_prefix = pMechanism->ulParameterLen;
} else {
g_assert (strlen (SIGNED_PREFIX) + 1 < sizeof (session->sign_prefix));
strcpy ((gchar*)session->sign_prefix, SIGNED_PREFIX);
session->n_sign_prefix = strlen (SIGNED_PREFIX);
}
return CKR_OK;
}
CK_RV
gck_mock_C_Verify (CK_SESSION_HANDLE hSession, CK_BYTE_PTR pData, CK_ULONG ulDataLen,
CK_BYTE_PTR pSignature, CK_ULONG ulSignatureLen)
{
Session *session;
CK_ULONG length;
g_return_val_if_fail (pData, CKR_DATA_INVALID);
g_return_val_if_fail (pSignature, CKR_ARGUMENTS_BAD);
session = g_hash_table_lookup (the_sessions, GUINT_TO_POINTER (hSession));
g_return_val_if_fail (session != NULL, CKR_SESSION_HANDLE_INVALID);
g_return_val_if_fail (session->operation == OP_CRYPTO, CKR_OPERATION_NOT_INITIALIZED);
g_return_val_if_fail (session->crypto_method == CKA_VERIFY, CKR_OPERATION_NOT_INITIALIZED);
g_assert (session->crypto_method == CKA_VERIFY);
g_assert (session->crypto_mechanism == CKM_MOCK_PREFIX);
g_assert (session->crypto_key == PUBLIC_KEY_PREFIX);
length = session->n_sign_prefix + ulDataLen;
if (ulSignatureLen < length) {
g_assert (FALSE);
return CKR_SIGNATURE_LEN_RANGE;
}
if (memcmp (pSignature, session->sign_prefix, session->n_sign_prefix) == 0 &&
memcmp (pSignature + session->n_sign_prefix, pData, ulDataLen) == 0)
return CKR_OK;
return CKR_SIGNATURE_INVALID;
}
CK_RV
gck_mock_unsupported_C_VerifyUpdate (CK_SESSION_HANDLE hSession, CK_BYTE_PTR pPart, CK_ULONG ulPartLen)
{
return CKR_FUNCTION_NOT_SUPPORTED;
}
CK_RV
gck_mock_unsupported_C_VerifyFinal (CK_SESSION_HANDLE hSession, CK_BYTE_PTR pSignature,
CK_ULONG pulSignatureLen)
{
return CKR_FUNCTION_NOT_SUPPORTED;
}
CK_RV
gck_mock_unsupported_C_VerifyRecoverInit (CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism,
CK_OBJECT_HANDLE hKey)
{
return CKR_FUNCTION_NOT_SUPPORTED;
}
CK_RV
gck_mock_unsupported_C_VerifyRecover (CK_SESSION_HANDLE hSession, CK_BYTE_PTR pSignature,
CK_ULONG pulSignatureLen, CK_BYTE_PTR pData, CK_ULONG_PTR pulDataLen)
{
return CKR_FUNCTION_NOT_SUPPORTED;
}
CK_RV
gck_mock_unsupported_C_DigestEncryptUpdate (CK_SESSION_HANDLE hSession, CK_BYTE_PTR pPart,
CK_ULONG ulPartLen, CK_BYTE_PTR pEncryptedPart,
CK_ULONG_PTR ulEncryptedPartLen)
{
return CKR_FUNCTION_NOT_SUPPORTED;
}
CK_RV
gck_mock_unsupported_C_DecryptDigestUpdate (CK_SESSION_HANDLE hSession, CK_BYTE_PTR pEncryptedPart,
CK_ULONG ulEncryptedPartLen, CK_BYTE_PTR pPart,
CK_ULONG_PTR pulPartLen)
{
return CKR_FUNCTION_NOT_SUPPORTED;
}
CK_RV
gck_mock_unsupported_C_SignEncryptUpdate (CK_SESSION_HANDLE hSession, CK_BYTE_PTR pPart,
CK_ULONG ulPartLen, CK_BYTE_PTR pEncryptedPart,
CK_ULONG_PTR ulEncryptedPartLen)
{
return CKR_FUNCTION_NOT_SUPPORTED;
}
CK_RV
gck_mock_unsupported_C_DecryptVerifyUpdate (CK_SESSION_HANDLE hSession, CK_BYTE_PTR pEncryptedPart,
CK_ULONG ulEncryptedPartLen, CK_BYTE_PTR pPart,
CK_ULONG_PTR pulPartLen)
{
return CKR_FUNCTION_NOT_SUPPORTED;
}
CK_RV
gck_mock_unsupported_C_GenerateKey (CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism,
CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount,
CK_OBJECT_HANDLE_PTR phKey)
{
return CKR_FUNCTION_NOT_SUPPORTED;
}
CK_RV
gck_mock_unsupported_C_GenerateKeyPair (CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism,
CK_ATTRIBUTE_PTR pPublicKeyTemplate, CK_ULONG ulPublicKeyAttributeCount,
CK_ATTRIBUTE_PTR pPrivateKeyTemplate, CK_ULONG ulPrivateKeyAttributeCount,
CK_OBJECT_HANDLE_PTR phPublicKey, CK_OBJECT_HANDLE_PTR phPrivateKey)
{
GckAttributes *attrs;
Session *session;
gboolean token;
CK_ULONG i;
g_return_val_if_fail (pMechanism, CKR_MECHANISM_INVALID);
g_return_val_if_fail (pPublicKeyTemplate, CKR_TEMPLATE_INCOMPLETE);
g_return_val_if_fail (ulPublicKeyAttributeCount, CKR_TEMPLATE_INCOMPLETE);
g_return_val_if_fail (pPrivateKeyTemplate, CKR_TEMPLATE_INCOMPLETE);
g_return_val_if_fail (ulPrivateKeyAttributeCount, CKR_TEMPLATE_INCOMPLETE);
g_return_val_if_fail (phPublicKey, CKR_ARGUMENTS_BAD);
g_return_val_if_fail (phPrivateKey, CKR_ARGUMENTS_BAD);
session = g_hash_table_lookup (the_sessions, GUINT_TO_POINTER (hSession));
g_return_val_if_fail (session != NULL, CKR_SESSION_HANDLE_INVALID);
if (pMechanism->mechanism != CKM_MOCK_GENERATE)
return CKR_MECHANISM_INVALID;
if (!pMechanism->pParameter || pMechanism->ulParameterLen != 9 ||
memcmp (pMechanism->pParameter, "generate", 9) != 0)
g_return_val_if_reached (CKR_MECHANISM_PARAM_INVALID);
attrs = gck_attributes_new ();
gck_attributes_add_string (attrs, CKA_VALUE, "generated");
for (i = 0; i < ulPublicKeyAttributeCount; ++i)
gck_attributes_add_data (attrs, pPublicKeyTemplate[i].type,
pPublicKeyTemplate[i].pValue,
pPublicKeyTemplate[i].ulValueLen);
*phPublicKey = ++unique_identifier;
if (gck_attributes_find_boolean (attrs, CKA_TOKEN, &token) && token)
g_hash_table_insert (the_objects, GUINT_TO_POINTER (*phPublicKey), attrs);
else
g_hash_table_insert (session->objects, GUINT_TO_POINTER (*phPublicKey), attrs);
attrs = gck_attributes_new ();
gck_attributes_add_string (attrs, CKA_VALUE, "generated");
for (i = 0; i < ulPrivateKeyAttributeCount; ++i)
gck_attributes_add_data (attrs, pPrivateKeyTemplate[i].type,
pPrivateKeyTemplate[i].pValue,
pPrivateKeyTemplate[i].ulValueLen);
*phPrivateKey = ++unique_identifier;
if (gck_attributes_find_boolean (attrs, CKA_TOKEN, &token) && token)
g_hash_table_insert (the_objects, GUINT_TO_POINTER (*phPrivateKey), attrs);
else
g_hash_table_insert (session->objects, GUINT_TO_POINTER (*phPrivateKey), attrs);
return CKR_OK;
}
CK_RV
gck_mock_unsupported_C_WrapKey (CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism,
CK_OBJECT_HANDLE hWrappingKey, CK_OBJECT_HANDLE hKey,
CK_BYTE_PTR pWrappedKey, CK_ULONG_PTR pulWrappedKeyLen)
{
GckAttributes *attrs;
GckAttribute *attr;
Session *session;
g_return_val_if_fail (pMechanism, CKR_MECHANISM_INVALID);
g_return_val_if_fail (hWrappingKey, CKR_OBJECT_HANDLE_INVALID);
g_return_val_if_fail (hKey, CKR_OBJECT_HANDLE_INVALID);
g_return_val_if_fail (pulWrappedKeyLen, CKR_WRAPPED_KEY_LEN_RANGE);
session = g_hash_table_lookup (the_sessions, GUINT_TO_POINTER (hSession));
g_return_val_if_fail (session != NULL, CKR_SESSION_HANDLE_INVALID);
attrs = lookup_object (session, hWrappingKey);
g_return_val_if_fail (attrs, CKR_WRAPPING_KEY_HANDLE_INVALID);
attrs = lookup_object (session, hKey);
g_return_val_if_fail (attrs, CKR_WRAPPED_KEY_INVALID);
if (pMechanism->mechanism != CKM_MOCK_WRAP)
return CKR_MECHANISM_INVALID;
if (pMechanism->pParameter) {
if (pMechanism->ulParameterLen != 4 ||
memcmp (pMechanism->pParameter, "wrap", 4) != 0)
g_return_val_if_reached (CKR_MECHANISM_PARAM_INVALID);
}
attr = gck_attributes_find (attrs, CKA_VALUE);
if (attr == NULL)
return CKR_WRAPPED_KEY_INVALID;
if (!pWrappedKey) {
*pulWrappedKeyLen = attr->length;
return CKR_OK;
}
if (*pulWrappedKeyLen < attr->length) {
*pulWrappedKeyLen = attr->length;
return CKR_BUFFER_TOO_SMALL;
}
memcpy (pWrappedKey, attr->value, attr->length);
*pulWrappedKeyLen = attr->length;
return CKR_OK;
}
CK_RV
gck_mock_unsupported_C_UnwrapKey (CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism,
CK_OBJECT_HANDLE hUnwrappingKey, CK_BYTE_PTR pWrappedKey,
CK_ULONG ulWrappedKeyLen, CK_ATTRIBUTE_PTR pTemplate,
CK_ULONG ulCount, CK_OBJECT_HANDLE_PTR phKey)
{
GckAttributes *attrs;
Session *session;
gboolean token;
CK_ULONG i;
g_return_val_if_fail (pMechanism, CKR_MECHANISM_INVALID);
g_return_val_if_fail (hUnwrappingKey, CKR_WRAPPING_KEY_HANDLE_INVALID);
g_return_val_if_fail (pWrappedKey, CKR_WRAPPED_KEY_INVALID);
g_return_val_if_fail (ulWrappedKeyLen, CKR_WRAPPED_KEY_LEN_RANGE);
g_return_val_if_fail (phKey, CKR_ARGUMENTS_BAD);
g_return_val_if_fail (pTemplate, CKR_TEMPLATE_INCOMPLETE);
g_return_val_if_fail (ulCount, CKR_TEMPLATE_INCONSISTENT);
session = g_hash_table_lookup (the_sessions, GUINT_TO_POINTER (hSession));
g_return_val_if_fail (session != NULL, CKR_SESSION_HANDLE_INVALID);
attrs = lookup_object (session, hUnwrappingKey);
g_return_val_if_fail (attrs, CKR_WRAPPING_KEY_HANDLE_INVALID);
if (pMechanism->mechanism != CKM_MOCK_WRAP)
return CKR_MECHANISM_INVALID;
if (pMechanism->pParameter) {
if (pMechanism->ulParameterLen != 4 ||
memcmp (pMechanism->pParameter, "wrap", 4) != 0)
g_return_val_if_reached (CKR_MECHANISM_PARAM_INVALID);
}
attrs = gck_attributes_new ();
gck_attributes_add_data (attrs, CKA_VALUE, pWrappedKey, ulWrappedKeyLen);
for (i = 0; i < ulCount; ++i)
gck_attributes_add_data (attrs, pTemplate[i].type,
pTemplate[i].pValue,
pTemplate[i].ulValueLen);
*phKey = ++unique_identifier;
if (gck_attributes_find_boolean (attrs, CKA_TOKEN, &token) && token)
g_hash_table_insert (the_objects, GUINT_TO_POINTER (*phKey), attrs);
else
g_hash_table_insert (session->objects, GUINT_TO_POINTER (*phKey), attrs);
return CKR_OK;
}
CK_RV
gck_mock_unsupported_C_DeriveKey (CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism,
CK_OBJECT_HANDLE hBaseKey, CK_ATTRIBUTE_PTR pTemplate,
CK_ULONG ulCount, CK_OBJECT_HANDLE_PTR phKey)
{
GckAttributes *attrs, *copy;
Session *session;
gboolean token;
CK_ULONG i;
g_return_val_if_fail (pMechanism, CKR_MECHANISM_INVALID);
g_return_val_if_fail (ulCount, CKR_TEMPLATE_INCOMPLETE);
g_return_val_if_fail (pTemplate, CKR_TEMPLATE_INCOMPLETE);
g_return_val_if_fail (phKey, CKR_ARGUMENTS_BAD);
session = g_hash_table_lookup (the_sessions, GUINT_TO_POINTER (hSession));
g_return_val_if_fail (session, CKR_SESSION_HANDLE_INVALID);
attrs = lookup_object (session, hBaseKey);
g_return_val_if_fail (attrs, CKR_KEY_HANDLE_INVALID);
if (pMechanism->mechanism != CKM_MOCK_DERIVE)
return CKR_MECHANISM_INVALID;
if (pMechanism->pParameter) {
if (pMechanism->ulParameterLen != 6 ||
memcmp (pMechanism->pParameter, "derive", 6) != 0)
g_return_val_if_reached (CKR_MECHANISM_PARAM_INVALID);
}
copy = gck_attributes_new ();
gck_attributes_add_string (copy, CKA_VALUE, "derived");
for (i = 0; i < ulCount; ++i)
gck_attributes_add_data (copy, pTemplate[i].type,
pTemplate[i].pValue,
pTemplate[i].ulValueLen);
for (i = 0; i < gck_attributes_count (attrs); ++i)
gck_attributes_add (copy, gck_attributes_at (attrs, i));
*phKey = ++unique_identifier;
if (gck_attributes_find_boolean (copy, CKA_TOKEN, &token) && token)
g_hash_table_insert (the_objects, GUINT_TO_POINTER (*phKey), copy);
else
g_hash_table_insert (session->objects, GUINT_TO_POINTER (*phKey), copy);
return CKR_OK;
}
CK_RV
gck_mock_unsupported_C_SeedRandom (CK_SESSION_HANDLE hSession, CK_BYTE_PTR pSeed, CK_ULONG ulSeedLen)
{
return CKR_FUNCTION_NOT_SUPPORTED;
}
CK_RV
gck_mock_unsupported_C_GenerateRandom (CK_SESSION_HANDLE hSession, CK_BYTE_PTR pRandomData,
CK_ULONG ulRandomLen)
{
return CKR_FUNCTION_NOT_SUPPORTED;
}
static CK_FUNCTION_LIST functionList = {
{ 2, 11 }, /* version */
gck_mock_validate_and_C_Initialize,
gck_mock_C_Finalize,
gck_mock_C_GetInfo,
gck_mock_C_GetFunctionList,
gck_mock_C_GetSlotList,
gck_mock_C_GetSlotInfo,
gck_mock_C_GetTokenInfo,
gck_mock_C_GetMechanismList,
gck_mock_C_GetMechanismInfo,
gck_mock_specific_args_C_InitToken,
gck_mock_C_InitPIN,
gck_mock_C_SetPIN,
gck_mock_C_OpenSession,
gck_mock_C_CloseSession,
gck_mock_C_CloseAllSessions,
gck_mock_C_GetSessionInfo,
gck_mock_unsupported_C_GetOperationState,
gck_mock_unsupported_C_SetOperationState,
gck_mock_C_Login,
gck_mock_C_Logout,
gck_mock_C_CreateObject,
gck_mock_unsupported_C_CopyObject,
gck_mock_C_DestroyObject,
gck_mock_unsupported_C_GetObjectSize,
gck_mock_C_GetAttributeValue,
gck_mock_C_SetAttributeValue,
gck_mock_C_FindObjectsInit,
gck_mock_C_FindObjects,
gck_mock_C_FindObjectsFinal,
gck_mock_C_EncryptInit,
gck_mock_C_Encrypt,
gck_mock_unsupported_C_EncryptUpdate,
gck_mock_unsupported_C_EncryptFinal,
gck_mock_C_DecryptInit,
gck_mock_C_Decrypt,
gck_mock_unsupported_C_DecryptUpdate,
gck_mock_unsupported_C_DecryptFinal,
gck_mock_unsupported_C_DigestInit,
gck_mock_unsupported_C_Digest,
gck_mock_unsupported_C_DigestUpdate,
gck_mock_unsupported_C_DigestKey,
gck_mock_unsupported_C_DigestFinal,
gck_mock_C_SignInit,
gck_mock_C_Sign,
gck_mock_unsupported_C_SignUpdate,
gck_mock_unsupported_C_SignFinal,
gck_mock_unsupported_C_SignRecoverInit,
gck_mock_unsupported_C_SignRecover,
gck_mock_C_VerifyInit,
gck_mock_C_Verify,
gck_mock_unsupported_C_VerifyUpdate,
gck_mock_unsupported_C_VerifyFinal,
gck_mock_unsupported_C_VerifyRecoverInit,
gck_mock_unsupported_C_VerifyRecover,
gck_mock_unsupported_C_DigestEncryptUpdate,
gck_mock_unsupported_C_DecryptDigestUpdate,
gck_mock_unsupported_C_SignEncryptUpdate,
gck_mock_unsupported_C_DecryptVerifyUpdate,
gck_mock_unsupported_C_GenerateKey,
gck_mock_unsupported_C_GenerateKeyPair,
gck_mock_unsupported_C_WrapKey,
gck_mock_unsupported_C_UnwrapKey,
gck_mock_unsupported_C_DeriveKey,
gck_mock_unsupported_C_SeedRandom,
gck_mock_unsupported_C_GenerateRandom,
gck_mock_C_GetFunctionStatus,
gck_mock_C_CancelFunction,
gck_mock_unsupported_C_WaitForSlotEvent
};
|