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
1809
1810
1811
1812
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <cryptoutil.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <strings.h>
#include "metaGlobal.h"
extern cipher_mechs_threshold_t meta_mechs_threshold[];
static boolean_t threshold_chk_enabled = B_FALSE;
CK_RV
meta_operation_init_defer(CK_FLAGS optype, meta_session_t *session,
CK_MECHANISM *pMechanism, meta_object_t *key)
{
if (session->init.pMech == NULL) {
session->init.pMech = malloc(sizeof (CK_MECHANISM));
if (session->init.pMech == NULL)
return (CKR_HOST_MEMORY);
(void) memcpy(session->init.pMech, pMechanism,
sizeof (CK_MECHANISM));
if ((pMechanism->ulParameterLen > 0) &&
(pMechanism->pParameter != NULL)) {
session->init.pMech->pParameter =
malloc(pMechanism->ulParameterLen);
if (session->init.pMech->pParameter == NULL) {
free(session->init.pMech);
session->init.pMech = NULL;
return (CKR_HOST_MEMORY);
}
(void) memcpy(session->init.pMech->pParameter,
pMechanism->pParameter, pMechanism->ulParameterLen);
} else {
session->init.pMech->pParameter = NULL;
}
} else { /* reuse it */
if ((pMechanism->ulParameterLen > 0) &&
(pMechanism->pParameter != NULL)) {
if (pMechanism->ulParameterLen !=
session->init.pMech->ulParameterLen) {
if (session->init.pMech->pParameter != NULL)
free(session->init.pMech->pParameter);
session->init.pMech->pParameter =
malloc(pMechanism->ulParameterLen);
if (session->init.pMech->pParameter == NULL) {
free(session->init.pMech);
session->init.pMech = NULL;
return (CKR_HOST_MEMORY);
}
} /* otherwise reuse it */
(void) memcpy(session->init.pMech->pParameter,
pMechanism->pParameter, pMechanism->ulParameterLen);
} else {
/*
* free the previous pParameter if not yet freed
* because we don't need it now.
*/
if (session->init.pMech->pParameter != NULL) {
free(session->init.pMech->pParameter);
session->init.pMech->pParameter = NULL;
}
}
/* copy the rest of data */
session->init.pMech->mechanism =
pMechanism->mechanism;
session->init.pMech->ulParameterLen =
pMechanism->ulParameterLen;
}
session->init.session = session;
session->init.optype = optype;
session->init.key = key;
session->init.done = B_FALSE;
session->init.app = B_TRUE;
return (CKR_OK);
}
/*
* meta_operation_init
*
*/
CK_RV
meta_operation_init(CK_FLAGS optype, meta_session_t *session,
CK_MECHANISM *pMechanism, meta_object_t *key)
{
CK_RV rv, save_rv;
mechinfo_t **supporting_slots;
CK_ULONG slotnum;
unsigned long i, slotCount = 0;
slot_session_t *init_session = NULL;
CK_MECHANISM_INFO mech_info;
/*
* If an operation is already active, cleanup existing operation
* and start a new one.
*/
if (session->op1.type != 0) {
CK_MECHANISM mech;
if ((optype == CKF_ENCRYPT) || (optype == CKF_DECRYPT) ||
(optype == CKF_DIGEST)) {
mech = *pMechanism;
if ((pMechanism->ulParameterLen > 0) &&
(pMechanism->pParameter != NULL)) {
mech.pParameter =
malloc(pMechanism->ulParameterLen);
if (mech.pParameter == NULL) {
return (CKR_HOST_MEMORY);
}
(void) memcpy(mech.pParameter,
pMechanism->pParameter,
pMechanism->ulParameterLen);
} else {
mech.pParameter = NULL;
mech.ulParameterLen = 0;
}
meta_operation_cleanup(session, session->op1.type,
B_FALSE);
rv = meta_operation_init_defer(optype, session,
&mech, key);
if (mech.pParameter != NULL) {
free(mech.pParameter);
}
if (rv != CKR_OK)
return (rv);
} else {
meta_operation_cleanup(session, session->op1.type,
B_FALSE);
}
}
mech_info.flags = optype;
/*
* Get a list of capable slots.
*
* If the specified mechanism is used in this session last time,
* the list of capable slots is already retrieved. We can save
* some processing, and just use that list of slots.
*/
if (((session->mech_support_info).mech != pMechanism->mechanism) ||
((session->mech_support_info).num_supporting_slots == 0)) {
(session->mech_support_info).mech = pMechanism->mechanism;
rv = meta_mechManager_get_slots(&(session->mech_support_info),
B_FALSE, &mech_info);
if (rv != CKR_OK) {
goto finish;
}
}
rv = CKR_FUNCTION_FAILED;
/* The following 2 assignment is just to make the code more readable */
slotCount = (session->mech_support_info).num_supporting_slots;
supporting_slots = (session->mech_support_info).supporting_slots;
/* Attempt to initialize operation on slots until one succeeds. */
for (i = 0; i < slotCount; i++) {
slot_object_t *init_key;
CK_SLOT_ID fw_st_id;
init_session = NULL;
slotnum = supporting_slots[i]->slotnum;
/*
* An actual session with the underlying slot is required
* for the operation. When the operation is successfully
* completed, the underlying session with the slot
* is not released back to the list of available sessions
* pool. This will help if the next operation can
* also be done on the same slot, because it avoids
* one extra trip to the session pool to get an idle session.
* If the operation can't be done on that slot,
* we release the session back to the session pool then.
*/
if (session->op1.session != NULL) {
if ((session->op1.session)->slotnum == slotnum) {
init_session = session->op1.session;
/*
* set it to NULL for now, assign it to
* init_session again if it is successful
*/
session->op1.session = NULL;
} else {
init_session = NULL;
}
}
if (!init_session) {
rv = meta_get_slot_session(slotnum, &init_session,
session->session_flags);
if (rv != CKR_OK) {
goto loop_cleanup;
}
}
/* if necessary, ensure a clone of the obj exists in slot */
if (optype != CKF_DIGEST) {
rv = meta_object_get_clone(key, slotnum, init_session,
&init_key);
if (rv != CKR_OK) {
goto loop_cleanup;
}
}
fw_st_id = init_session->fw_st_id;
switch (optype) {
case CKF_ENCRYPT:
rv = FUNCLIST(fw_st_id)->C_EncryptInit(
init_session->hSession, pMechanism,
init_key->hObject);
break;
case CKF_DECRYPT:
rv = FUNCLIST(fw_st_id)->C_DecryptInit(
init_session->hSession, pMechanism,
init_key->hObject);
break;
case CKF_DIGEST:
rv = FUNCLIST(fw_st_id)->C_DigestInit(
init_session->hSession, pMechanism);
break;
case CKF_SIGN:
rv = FUNCLIST(fw_st_id)->C_SignInit(
init_session->hSession, pMechanism,
init_key->hObject);
break;
case CKF_VERIFY:
rv = FUNCLIST(fw_st_id)->C_VerifyInit(
init_session->hSession, pMechanism,
init_key->hObject);
break;
case CKF_SIGN_RECOVER:
rv = FUNCLIST(fw_st_id)->C_SignRecoverInit(
init_session->hSession, pMechanism,
init_key->hObject);
break;
case CKF_VERIFY_RECOVER:
rv = FUNCLIST(fw_st_id)->C_VerifyRecoverInit(
init_session->hSession, pMechanism,
init_key->hObject);
break;
default:
/*NOTREACHED*/
rv = CKR_FUNCTION_FAILED;
break;
}
if (rv == CKR_OK)
break;
loop_cleanup:
if (i == 0) {
save_rv = rv;
}
if (init_session) {
meta_release_slot_session(init_session);
init_session = NULL;
}
}
if (rv == CKR_OK) {
/*
* If currently stored session is not the one being in use now,
* release the previous one and store the current one
*/
if ((session->op1.session) &&
(session->op1.session != init_session)) {
meta_release_slot_session(session->op1.session);
}
/* Save the session */
session->op1.session = init_session;
session->op1.type = optype;
session->init.slotnum = slotnum;
session->init.done = B_TRUE;
} else {
rv = save_rv;
}
finish:
return (rv);
}
/*
* meta_operation_init_softtoken()
* It will always do the crypto init operation on softtoken slot.
*/
CK_RV
meta_operation_init_softtoken(CK_FLAGS optype, meta_session_t *session,
CK_MECHANISM *pMechanism, meta_object_t *key)
{
CK_RV rv = CKR_FUNCTION_FAILED;
slot_session_t *init_session = NULL;
slot_object_t *init_key;
CK_SLOT_ID fw_st_id;
CK_ULONG softtoken_slot_num;
softtoken_slot_num = get_softtoken_slotnum();
/*
* If an operation is already active, cleanup existing operation
* and start a new one.
*/
if (session->op1.type != 0) {
CK_MECHANISM mech;
mech = *pMechanism;
if ((pMechanism->ulParameterLen > 0) &&
(pMechanism->pParameter != NULL)) {
mech.pParameter =
malloc(pMechanism->ulParameterLen);
if (mech.pParameter == NULL) {
return (CKR_HOST_MEMORY);
}
(void) memcpy(mech.pParameter,
pMechanism->pParameter, pMechanism->ulParameterLen);
} else {
mech.pParameter = NULL;
mech.ulParameterLen = 0;
}
meta_operation_cleanup(session, session->op1.type, B_FALSE);
rv = meta_operation_init_defer(optype, session, &mech,
key);
if (mech.pParameter != NULL) {
free(mech.pParameter);
}
if (rv != CKR_OK)
return (rv);
}
/*
* An actual session with the underlying slot is required
* for the operation. When the operation is successfully
* completed, the underlying session with the slot
* is not released back to the list of available sessions
* pool. This will help if the next operation can
* also be done on the same slot, because it avoids
* one extra trip to the session pool to get an idle session.
* If the operation can't be done on that slot,
* we release the session back to the session pool.
*/
if (session->op1.session != NULL) {
if ((session->op1.session)->slotnum ==
softtoken_slot_num) {
init_session = session->op1.session;
/*
* set it to NULL for now, assign it to
* init_session again if it is successful
*/
session->op1.session = NULL;
} else {
init_session = NULL;
}
}
if (init_session == NULL) {
/* get the active session from softtoken slot */
rv = meta_get_slot_session(softtoken_slot_num,
&init_session, session->session_flags);
if (rv != CKR_OK) {
goto finish;
}
}
/* if necessary, ensure a clone of the obj exists in softtoken slot */
if (optype != CKF_DIGEST) {
rv = meta_object_get_clone(key, softtoken_slot_num,
init_session, &init_key);
if (rv != CKR_OK) {
if (init_session != NULL) {
meta_release_slot_session(init_session);
init_session = NULL;
}
goto finish;
}
}
fw_st_id = init_session->fw_st_id;
/*
* Currently, we only support offloading encrypt, decrypt
* and digest operations to softtoken based on kernel
* threshold for the supported mechanisms.
*/
switch (optype) {
case CKF_ENCRYPT:
rv = FUNCLIST(fw_st_id)->C_EncryptInit(
init_session->hSession, pMechanism,
init_key->hObject);
break;
case CKF_DECRYPT:
rv = FUNCLIST(fw_st_id)->C_DecryptInit(
init_session->hSession, pMechanism,
init_key->hObject);
break;
case CKF_DIGEST:
rv = FUNCLIST(fw_st_id)->C_DigestInit(
init_session->hSession, pMechanism);
break;
default:
/*NOTREACHED*/
rv = CKR_FUNCTION_FAILED;
break;
}
if (rv == CKR_OK) {
/*
* If currently stored session is not the one being in use now,
* release the previous one and store the current one
*/
if ((session->op1.session) &&
(session->op1.session != init_session)) {
meta_release_slot_session(session->op1.session);
}
/* Save the session */
session->op1.session = init_session;
session->op1.type = optype;
/*
* The init.done flag will be checked by the meta_do_operation()
* to indicate whether the C_xxxInit has been done against
* softtoken.
*/
session->init.done = B_TRUE;
session->init.slotnum = softtoken_slot_num;
}
finish:
return (rv);
}
int
meta_GetThreshold(CK_MECHANISM_TYPE mechanism)
{
int i;
for (i = 0; i < MAX_NUM_THRESHOLD; i++) {
if (mechanism == meta_mechs_threshold[i].mech_type)
return (meta_mechs_threshold[i].mech_threshold);
}
/* no matching mechanism */
return (0);
}
/*
* meta_do_operation
*
* NOTES:
*
* 1) The spec says you cannot do a C_Encrypt after a C_EncUpdate,
* but we don't explicitly enforce it here (ie, disallow doing MODE_SINGLE
* after a MODE_UPDATE). Instead, we just assume the underlying provider
* will catch the problem and return an appropriate error.
*
* 2) Note that the Verify operations are a little unusual, due to the
* PKCS#11 API. For C_Verify, the last two arguments are used as inputs,
* unlike the other single pass operations (where they are outputs). For
* C_VerifyFinal, in/inLen are passed instead of out/outLen like the other
* Final operations.
*
* 3) C_DigestKey is the only crypto operation that uses an object after
* the operation has been initialized. No other callers should provide
* this argument (use NULL).
*/
CK_RV
meta_do_operation(CK_FLAGS optype, int mode,
meta_session_t *session, meta_object_t *object,
CK_BYTE *in, CK_ULONG inLen, CK_BYTE *out, CK_ULONG *outLen)
{
CK_RV rv;
CK_SESSION_HANDLE hSession;
CK_SLOT_ID fw_st_id;
slot_session_t *slot_session = NULL;
slot_object_t *slot_object = NULL;
int threshold = 0;
boolean_t shutdown, finished_normally;
/*
* We've deferred the init for encrypt, decrypt and digest
* operations. As we know the size of the input data now, we
* can decide where to perform the real init operation based
* on the kernel cipher-specific thresholds for certain
* supported mechanisms.
*/
if ((optype == CKF_ENCRYPT) || (optype == CKF_DECRYPT) ||
(optype == CKF_DIGEST)) {
if (Tmp_GetThreshold != NULL) {
if (!session->init.app) {
return (CKR_OPERATION_NOT_INITIALIZED);
}
threshold = meta_GetThreshold(
session->init.pMech->mechanism);
}
if ((threshold_chk_enabled == B_FALSE) || (inLen > threshold)) {
if ((session->init.app) && (!session->init.done)) {
/*
* Call real init operation only if the
* application has called C_xxxInit
* but the real init operation has not
* been done.
*/
rv = meta_operation_init(optype,
session->init.session,
session->init.pMech,
session->init.key);
if (rv != CKR_OK)
goto exit;
} else if (!session->init.app) {
/*
* This checking detects the case that
* application calls C_En(De)Crypt/Digest
* directly without calling C_xxxInit.
*/
return (CKR_OPERATION_NOT_INITIALIZED);
}
} else {
/*
* The size of the input data is smaller than the
* threshold so we'll use softoken to perform the
* crypto operation for better performance reason.
*/
if ((session->init.app) && (!session->init.done)) {
/*
* Call real init operation only if the
* application has called C_xxxInit
* but the real init operation has not
* been done.
*/
rv = meta_operation_init_softtoken(optype,
session->init.session,
session->init.pMech,
session->init.key);
if (rv != CKR_OK) {
/*
* In case the operation fails in
* softtoken, go back to use the
* original slot again.
*/
rv = meta_operation_init(optype,
session->init.session,
session->init.pMech,
session->init.key);
if (rv != CKR_OK)
goto exit;
}
} else if (!session->init.app) {
/*
* This checking detects the case that
* application calls C_En(De)Crypt/Digest
* directly without calling C_xxxInit.
*/
return (CKR_OPERATION_NOT_INITIALIZED);
}
}
} else if (optype != session->op1.type) {
return (CKR_OPERATION_NOT_INITIALIZED);
}
slot_session = session->op1.session;
if (slot_session) {
hSession = slot_session->hSession;
fw_st_id = slot_session->fw_st_id;
} else {
/* should never be here */
rv = CKR_FUNCTION_FAILED;
goto exit;
}
/* Do the operation... */
if (optype == CKF_ENCRYPT && mode == MODE_SINGLE) {
rv = FUNCLIST(fw_st_id)->C_Encrypt(hSession, in,
inLen, out, outLen);
} else if (optype == CKF_ENCRYPT && mode == MODE_UPDATE) {
rv = FUNCLIST(fw_st_id)->C_EncryptUpdate(hSession, in,
inLen, out, outLen);
} else if (optype == CKF_ENCRYPT && mode == MODE_FINAL) {
rv = FUNCLIST(fw_st_id)->C_EncryptFinal(hSession, out,
outLen);
} else if (optype == CKF_DECRYPT && mode == MODE_SINGLE) {
rv = FUNCLIST(fw_st_id)->C_Decrypt(hSession, in,
inLen, out, outLen);
} else if (optype == CKF_DECRYPT && mode == MODE_UPDATE) {
rv = FUNCLIST(fw_st_id)->C_DecryptUpdate(hSession, in,
inLen, out, outLen);
} else if (optype == CKF_DECRYPT && mode == MODE_FINAL) {
rv = FUNCLIST(fw_st_id)->C_DecryptFinal(hSession, out,
outLen);
} else if (optype == CKF_DIGEST && mode == MODE_SINGLE) {
rv = FUNCLIST(fw_st_id)->C_Digest(hSession, in, inLen,
out, outLen);
} else if (optype == CKF_DIGEST && mode == MODE_UPDATE) {
/* noOutputForOp = TRUE; */
rv = FUNCLIST(fw_st_id)->C_DigestUpdate(hSession, in,
inLen);
} else if (optype == CKF_DIGEST && mode == MODE_UPDATE_WITHKEY) {
/* noOutputForOp = TRUE; */
/*
* For C_DigestKey, a key is provided and
* we need the clone.
*/
rv = meta_object_get_clone(object,
slot_session->slotnum, slot_session, &slot_object);
if (rv == CKR_OK)
rv = FUNCLIST(fw_st_id)->C_DigestKey(hSession,
slot_object->hObject);
} else if (optype == CKF_DIGEST && mode == MODE_FINAL) {
rv = FUNCLIST(fw_st_id)->C_DigestFinal(hSession, out,
outLen);
} else if (optype == CKF_SIGN && mode == MODE_SINGLE) {
rv = FUNCLIST(fw_st_id)->C_Sign(hSession, in, inLen,
out, outLen);
} else if (optype == CKF_SIGN && mode == MODE_UPDATE) {
/* noOutputForOp = TRUE; */
rv = FUNCLIST(fw_st_id)->C_SignUpdate(hSession, in,
inLen);
} else if (optype == CKF_SIGN && mode == MODE_FINAL) {
rv = FUNCLIST(fw_st_id)->C_SignFinal(hSession, out,
outLen);
} else if (optype == CKF_VERIFY && mode == MODE_SINGLE) {
/* noOutputForOp = TRUE; */
/* Yes, use *outLen not outLen (think in2/in2Len) */
rv = FUNCLIST(fw_st_id)->C_Verify(hSession, in,
inLen, out, *outLen);
} else if (optype == CKF_VERIFY && mode == MODE_UPDATE) {
/* noOutputForOp = TRUE; */
rv = FUNCLIST(fw_st_id)->C_VerifyUpdate(hSession, in,
inLen);
} else if (optype == CKF_VERIFY && mode == MODE_FINAL) {
/* noOutputForOp = TRUE; */
/* Yes, use in/inLen instead of out/outLen */
rv = FUNCLIST(fw_st_id)->C_VerifyFinal(hSession, in,
inLen);
} else if (optype == CKF_SIGN_RECOVER && mode == MODE_SINGLE) {
rv = FUNCLIST(fw_st_id)->C_SignRecover(hSession, in,
inLen, out, outLen);
} else if (optype == CKF_VERIFY_RECOVER && mode == MODE_SINGLE) {
rv = FUNCLIST(fw_st_id)->C_VerifyRecover(hSession, in,
inLen, out, outLen);
} else {
rv = CKR_FUNCTION_FAILED;
}
/*
* Mark the operation type as inactive if an abnormal error
* happens, or if the operation normally results in an inactive
* operation state.
*
* NOTE: The spec isn't very explicit about what happens when you
* call C_FooFinal (or C_Foo) with a NULL output buffer (to get the
* output size), but there is no output. Technically this should be
* no different than the normal case (ie, when there is output), and
* the operation should remain active until the second call actually
* terminates it. However, one could make the case that there is no
* need for a second call, since no data is available. This presents
* dilemma for metaslot, because we don't know if the operation is
* going to remain active or not. We will assume a strict reading of
* the spec, the operation will remain active.
*/
exit:
if (rv == CKR_BUFFER_TOO_SMALL ||
(rv == CKR_OK && out == NULL && optype != CKF_VERIFY)) {
/* Leave op active for retry (with larger buffer). */
shutdown = B_FALSE;
} else if (rv != CKR_OK) {
shutdown = B_TRUE;
finished_normally = B_FALSE;
} else { /* CKR_OK */
if (mode == MODE_SINGLE || mode == MODE_FINAL) {
shutdown = B_TRUE;
finished_normally = B_TRUE;
} else { /* mode == MODE_UPDATE */
shutdown = B_FALSE;
}
}
if (shutdown) {
if (mode == MODE_SINGLE || mode == MODE_FINAL) {
session->init.app = B_FALSE;
}
meta_operation_cleanup(session, optype, finished_normally);
}
return (rv);
}
void
free_session_mechanism(meta_session_t *session)
{
if (session->init.pMech != NULL) {
if (session->init.pMech->pParameter != NULL) {
free(session->init.pMech->pParameter);
session->init.pMech->pParameter = NULL;
session->init.pMech->ulParameterLen = 0;
}
free(session->init.pMech);
session->init.pMech = NULL;
}
}
/*
* meta_operation_cleanup
*
* Cleans up an operation in the specified session.
* If the operation did not finish normally, it will force
* the operation to terminate.
*/
void
meta_operation_cleanup(meta_session_t *session, CK_FLAGS optype,
boolean_t finished_normally)
{
operation_info_t *op;
CK_SESSION_HANDLE hSession;
CK_SLOT_ID fw_st_id;
if (!finished_normally) {
CK_BYTE dummy_buf[8];
if (session->op1.type == optype) {
op = &session->op1;
} else {
if ((optype == CKF_ENCRYPT) ||
(optype == CKF_DECRYPT) ||
(optype == CKF_DIGEST)) {
session->op1.type = 0;
session->init.app = B_FALSE;
session->init.done = B_FALSE;
free_session_mechanism(session);
}
return;
}
hSession = op->session->hSession;
fw_st_id = op->session->fw_st_id;
/*
* There's no simple, reliable way to abort an
* operation. So, we'll force the operation to finish.
*
* We are here either because we need to abort either after
* C_xxxxxInit() or C_xxxxxUpdate().
*
* We will call C_xxxxxUpdate() with invalid argument to
* force the operation to abort. According to the PKCS#11
* spec, any call to C_xxxxxUpdate() returns in an error
* will terminate the current operation.
*/
switch (optype) {
case CKF_ENCRYPT:
(void) FUNCLIST(fw_st_id)->C_EncryptUpdate(hSession,
NULL, 8, dummy_buf, NULL);
break;
case CKF_DECRYPT:
(void) FUNCLIST(fw_st_id)->C_DecryptUpdate(hSession,
NULL, 8, dummy_buf, NULL);
break;
case CKF_DIGEST:
(void) FUNCLIST(fw_st_id)->C_DigestUpdate(hSession,
NULL, 8);
break;
case CKF_SIGN:
(void) FUNCLIST(fw_st_id)->C_SignUpdate(hSession,
NULL, 8);
break;
case CKF_SIGN_RECOVER:
(void) FUNCLIST(fw_st_id)->C_SignRecover(hSession,
NULL, 8, dummy_buf, NULL);
break;
case CKF_VERIFY:
(void) FUNCLIST(fw_st_id)->C_VerifyUpdate(hSession,
NULL, 8);
break;
case CKF_VERIFY_RECOVER:
(void) FUNCLIST(fw_st_id)->C_VerifyRecover(hSession,
NULL, 8, dummy_buf, NULL);
break;
default:
/*NOTREACHED*/
break;
}
meta_release_slot_session(session->op1.session);
session->op1.session = NULL;
}
if ((optype == CKF_ENCRYPT) || (optype == CKF_DECRYPT) ||
(optype == CKF_DIGEST)) {
session->init.done = B_FALSE;
free_session_mechanism(session);
}
session->op1.type = 0;
}
/*
* Gets the list of slots that supports the specified mechanism.
*
* If "token_only", check if the keystore slot supports the specified mech,
* if so, return that slot only
*
* Otherwise, get list of all slots that support the mech.
*
*/
static CK_RV
get_slotlist_for_mech(CK_MECHANISM_TYPE mech_type,
mech_support_info_t *mech_support_info,
mechinfo_t ***slots, unsigned long *slot_count, boolean_t token_only,
CK_MECHANISM_INFO *mech_info)
{
boolean_t mech_supported = B_FALSE;
CK_RV rv = CKR_OK;
if (token_only) {
rv = meta_mechManager_slot_supports_mech(mech_type,
get_keystore_slotnum(), &mech_supported,
&((mech_support_info->supporting_slots)[0]), B_FALSE,
mech_info);
if (rv != CKR_OK) {
return (rv);
}
if (mech_supported) {
mech_support_info->mech = mech_type;
/*
* Want to leave this at 0, that way, when
* other operation needs to
* use this mechanism, but not just for the
* keystore slot, we will look at other slots
*/
mech_support_info->num_supporting_slots = 0;
*slots = mech_support_info->supporting_slots;
*slot_count = 1;
} else {
rv = CKR_FUNCTION_FAILED;
}
} else {
/*
* Get a list of slots that support this mech .
*
* If the specified mechanism is used last time,
* the list of capable slots is already retrieved.
* We can save some processing, and just use that list of slots.
*/
if ((mech_support_info->mech != mech_type) ||
(mech_support_info->num_supporting_slots == 0)) {
mech_support_info->mech = mech_type;
rv = meta_mechManager_get_slots(mech_support_info,
B_FALSE, mech_info);
if (rv != CKR_OK) {
return (CKR_FUNCTION_FAILED);
}
}
*slots = mech_support_info->supporting_slots;
*slot_count = mech_support_info->num_supporting_slots;
}
return (rv);
}
/*
* meta_generate_keys
*
* Generates symmetric (k1=key, k2=null) or asymmetric (k1=pub, k2=priv) keys.
*
*/
CK_RV
meta_generate_keys(meta_session_t *session, CK_MECHANISM *pMechanism,
CK_ATTRIBUTE *k1Template, CK_ULONG k1AttrCount, meta_object_t *key1,
CK_ATTRIBUTE *k2Template, CK_ULONG k2AttrCount, meta_object_t *key2)
{
CK_RV rv, save_rv;
slot_session_t *gen_session = NULL;
slot_object_t *slot_key1 = NULL, *slot_key2 = NULL;
mechinfo_t **slots = NULL;
unsigned long i, slotCount = 0;
boolean_t doKeyPair = B_FALSE, token_only = B_FALSE;
CK_ULONG slotnum;
CK_MECHANISM_INFO mech_info;
/*
* Since the keygen call is in a loop, it is performance-wise useful
* to keep track of the token value
*/
CK_BBOOL current_token1_value = FALSE, current_token2_value = FALSE;
(void) get_template_boolean(CKA_TOKEN, k1Template, k1AttrCount,
&(key1->isToken));
(void) get_template_boolean(CKA_SENSITIVE, k1Template, k1AttrCount,
&(key1->isSensitive));
(void) get_template_boolean(CKA_PRIVATE, k1Template, k1AttrCount,
&(key1->isPrivate));
if (!get_template_boolean(CKA_EXTRACTABLE, k1Template, k1AttrCount,
&(key1->isExtractable)))
key1->isExtractable = B_TRUE;
if (key1->isToken)
current_token1_value = TRUE;
mech_info.flags = CKF_GENERATE;
if (key2) {
(void) get_template_boolean(CKA_TOKEN, k2Template, k2AttrCount,
&(key2->isToken));
(void) get_template_boolean(CKA_SENSITIVE, k2Template,
k2AttrCount, &(key2->isSensitive));
(void) get_template_boolean(CKA_PRIVATE, k2Template,
k2AttrCount, &(key2->isPrivate));
if (!get_template_boolean(CKA_EXTRACTABLE, k2Template,
k2AttrCount, &(key2->isExtractable)))
key2->isExtractable = B_TRUE;
if (key2->isToken)
current_token2_value = TRUE;
doKeyPair = B_TRUE;
mech_info.flags = CKF_GENERATE_KEY_PAIR;
}
/* Can't create token objects in a read-only session. */
if ((IS_READ_ONLY_SESSION(session->session_flags)) &&
((key1->isToken) || ((key2) && (key2->isToken)))) {
return (CKR_SESSION_READ_ONLY);
}
if (meta_freeobject_check(session, key1, pMechanism, k1Template,
k1AttrCount, 0)) {
if ((key1->isPrivate || (doKeyPair && key2->isPrivate)) &&
!metaslot_logged_in())
return (CKR_USER_NOT_LOGGED_IN);
if (!meta_freeobject_set(key1, k1Template, k1AttrCount,
B_FALSE))
return (CKR_FUNCTION_FAILED);
if (doKeyPair) {
key2->isFreeObject = FREE_ALLOWED_KEY;
if (!meta_freeobject_set(key2, k2Template, k2AttrCount,
B_FALSE))
return (CKR_FUNCTION_FAILED);
}
} else if (doKeyPair) {
/*
* If this is a keypair operation, the second key cannot be
* a FreeObject if the first is not. Both keys will have the
* same fate when it comes to provider choices
*/
key2->isFreeObject = FREE_DISABLED;
key2->isFreeToken = FREE_DISABLED;
}
if ((key1->isToken) || ((doKeyPair) && (key2->isToken))) {
/*
* Token objects can only be generated in the token object
* slot. If token object slot doesn't support generating
* the key, it will just not be done.
*/
token_only = B_TRUE;
}
rv = get_slotlist_for_mech(pMechanism->mechanism,
&(session->mech_support_info), &slots, &slotCount, token_only,
&mech_info);
if (rv != CKR_OK) {
goto finish;
}
rv = meta_slot_object_alloc(&slot_key1);
if (doKeyPair && rv == CKR_OK)
rv = meta_slot_object_alloc(&slot_key2);
if (rv != CKR_OK)
goto finish;
/* Attempt to generate key on slots until one succeeds. */
for (i = 0; i < slotCount; i++) {
CK_SESSION_HANDLE hSession;
CK_SLOT_ID fw_st_id;
gen_session = NULL;
slotnum = slots[i]->slotnum;
if (session->op1.session != NULL) {
if ((session->op1.session)->slotnum == slotnum) {
gen_session = session->op1.session;
/*
* set it to NULL for now, assign it to
* gen_session again if it is successful
*/
session->op1.session = NULL;
} else {
gen_session = NULL;
}
}
if (gen_session == NULL) {
rv = meta_get_slot_session(slotnum, &gen_session,
session->session_flags);
if (rv != CKR_OK) {
goto loop_cleanup;
}
}
/*
* If this is a freetoken, make sure the templates are
* approriate for the slot being used.
*/
if (key1->isFreeToken == FREE_ENABLED) {
rv = meta_freetoken_set(slotnum,
¤t_token1_value, k1Template, k1AttrCount);
if (rv != CKR_OK)
goto loop_cleanup;
}
if (doKeyPair && key2->isFreeToken == FREE_ENABLED) {
rv = meta_freetoken_set(slotnum,
¤t_token2_value, k2Template, k2AttrCount);
if (rv != CKR_OK)
goto loop_cleanup;
}
fw_st_id = gen_session->fw_st_id;
hSession = gen_session->hSession;
if (doKeyPair) {
rv = FUNCLIST(fw_st_id)->C_GenerateKeyPair(hSession,
pMechanism, k1Template, k1AttrCount,
k2Template, k2AttrCount,
&slot_key1->hObject, &slot_key2->hObject);
} else {
rv = FUNCLIST(fw_st_id)->C_GenerateKey(hSession,
pMechanism, k1Template, k1AttrCount,
&slot_key1->hObject);
}
if (rv == CKR_OK)
break;
loop_cleanup:
if (i == 0) {
save_rv = rv;
}
if (gen_session) {
meta_release_slot_session(gen_session);
gen_session = NULL;
}
}
if (rv != CKR_OK) {
rv = save_rv;
goto finish;
}
rv = meta_object_get_attr(gen_session, slot_key1->hObject, key1);
if (rv != CKR_OK) {
goto finish;
}
if (key2) {
rv = meta_object_get_attr(gen_session, slot_key2->hObject,
key2);
if (rv != CKR_OK) {
goto finish;
}
}
/* Allow FreeToken to activate onto token obj list */
if (key1->isFreeToken == FREE_ENABLED)
key1->isToken = B_TRUE;
meta_slot_object_activate(slot_key1, gen_session, key1->isToken);
key1->clones[slotnum] = slot_key1;
key1->master_clone_slotnum = slotnum;
slot_key1 = NULL;
if (key1->isFreeObject == FREE_ENABLED) {
rv = meta_freeobject_clone(session, key1);
if (rv != CKR_OK)
goto finish;
}
if (doKeyPair) {
/* Allow FreeToken to activate onto token obj list */
if (key2->isFreeToken == FREE_ENABLED)
key2->isToken = B_TRUE;
meta_slot_object_activate(slot_key2, gen_session,
key2->isToken);
key2->clones[slotnum] = slot_key2;
key2->master_clone_slotnum = slotnum;
slot_key2 = NULL;
if (key2->isFreeObject == FREE_ENABLED) {
rv = meta_freeobject_clone(session, key2);
if (rv != CKR_OK)
goto finish;
}
}
finish:
if (slot_key1) {
meta_slot_object_dealloc(slot_key1);
}
if (slot_key2) {
meta_slot_object_dealloc(slot_key2);
}
/* Save the session in case it can be used later */
if (rv == CKR_OK) {
/*
* If currently stored session is not the one being in use now,
* release the previous one and store the current one
*/
if ((session->op1.session) &&
(session->op1.session != gen_session)) {
meta_release_slot_session(session->op1.session);
}
/* Save the session */
session->op1.session = gen_session;
}
return (rv);
}
/*
* meta_wrap_key
*
*/
CK_RV
meta_wrap_key(meta_session_t *session, CK_MECHANISM *pMechanism,
meta_object_t *wrappingkey, meta_object_t *inputkey, CK_BYTE *wrapped_key,
CK_ULONG *wrapped_key_len)
{
CK_RV rv, save_rv;
slot_session_t *wrap_session = NULL;
slot_object_t *slot_wrappingkey, *slot_inputkey;
mechinfo_t **slots = NULL;
unsigned long i, slotCount = 0;
CK_ULONG slotnum;
CK_MECHANISM_INFO mech_info;
/*
* If the key to be wrapped is a token object,
* the operation can only be done in the token object slot.
*/
mech_info.flags = CKF_WRAP;
rv = get_slotlist_for_mech(pMechanism->mechanism,
&(session->mech_support_info), &slots, &slotCount,
inputkey->isToken, &mech_info);
if (rv != CKR_OK) {
return (rv);
}
/* Attempt to wrap key on slots until one succeeds. */
for (i = 0; i < slotCount; i++) {
slotnum = slots[i]->slotnum;
wrap_session = NULL;
if (session->op1.session != NULL) {
if ((session->op1.session)->slotnum == slotnum) {
wrap_session = session->op1.session;
/*
* set it to NULL for now, assign it to
* wrap_session again if it is successful
*/
session->op1.session = NULL;
} else {
wrap_session = NULL;
}
}
if (wrap_session == NULL) {
rv = meta_get_slot_session(slotnum, &wrap_session,
session->session_flags);
if (rv != CKR_OK) {
goto loop_cleanup;
}
}
rv = meta_object_get_clone(wrappingkey, slotnum,
wrap_session, &slot_wrappingkey);
if (rv != CKR_OK)
goto loop_cleanup;
rv = meta_object_get_clone(inputkey, slotnum,
wrap_session, &slot_inputkey);
if (rv != CKR_OK)
goto loop_cleanup;
rv = FUNCLIST(wrap_session->fw_st_id)->C_WrapKey(
wrap_session->hSession, pMechanism,
slot_wrappingkey->hObject, slot_inputkey->hObject,
wrapped_key, wrapped_key_len);
if (rv == CKR_OK || rv == CKR_BUFFER_TOO_SMALL)
break;
loop_cleanup:
if (i == 0) {
save_rv = rv;
}
if (wrap_session) {
meta_release_slot_session(wrap_session);
wrap_session = NULL;
}
}
if (rv != CKR_OK) {
if (rv != CKR_BUFFER_TOO_SMALL) {
if (i == slotCount) {
rv = save_rv;
}
}
}
finish:
/* Save the session in case it can be used later */
if (rv == CKR_OK) {
/*
* If currently stored session is not the one being in use now,
* release the previous one and store the current one
*/
if ((session->op1.session) &&
(session->op1.session != wrap_session)) {
meta_release_slot_session(session->op1.session);
}
/* Save the session */
session->op1.session = wrap_session;
}
return (rv);
}
/*
* meta_unwrap_key
*
*/
CK_RV
meta_unwrap_key(meta_session_t *session,
CK_MECHANISM *pMechanism, meta_object_t *unwrapping_key,
CK_BYTE *wrapped_key, CK_ULONG wrapped_key_len,
CK_ATTRIBUTE *template, CK_ULONG template_size,
meta_object_t *unwrapped_key)
{
CK_RV rv, save_rv;
CK_OBJECT_HANDLE hUnwrappedKey;
slot_session_t *unwrap_session = NULL;
slot_object_t *slot_unwrappingkey, *slot_unwrapped_key;
mechinfo_t **slots = NULL;
unsigned long i, slotCount = 0;
CK_ULONG slotnum;
CK_MECHANISM_INFO mech_info;
/* Can't create token objects in a read-only session. */
if ((IS_READ_ONLY_SESSION(session->session_flags)) &&
unwrapped_key->isToken) {
return (CKR_SESSION_READ_ONLY);
}
/*
* If the the resulting unwrapped key
* needs to be a token object, the operation can only
* be performed in the token slot, if it is supported.
*/
mech_info.flags = CKF_UNWRAP;
rv = get_slotlist_for_mech(pMechanism->mechanism,
&(session->mech_support_info), &slots, &slotCount,
unwrapped_key->isToken, &mech_info);
if (rv != CKR_OK) {
return (rv);
}
rv = meta_slot_object_alloc(&slot_unwrapped_key);
if (rv != CKR_OK) {
goto finish;
}
/* Attempt to unwrap key on slots until one succeeds. */
for (i = 0; i < slotCount; i++) {
slotnum = slots[i]->slotnum;
unwrap_session = NULL;
if (session->op1.session != NULL) {
if ((session->op1.session)->slotnum == slotnum) {
unwrap_session = session->op1.session;
/*
* set it to NULL for now, assign it to
* unwrap_session again if it is successful
*/
session->op1.session = NULL;
} else {
unwrap_session = NULL;
}
}
if (unwrap_session == NULL) {
rv = meta_get_slot_session(slotnum, &unwrap_session,
session->session_flags);
if (rv != CKR_OK) {
goto loop_cleanup;
}
}
rv = meta_object_get_clone(unwrapping_key, slotnum,
unwrap_session, &slot_unwrappingkey);
if (rv != CKR_OK)
goto loop_cleanup;
rv = FUNCLIST(unwrap_session->fw_st_id)->C_UnwrapKey(
unwrap_session->hSession, pMechanism,
slot_unwrappingkey->hObject, wrapped_key, wrapped_key_len,
template, template_size, &hUnwrappedKey);
if (rv == CKR_OK || rv == CKR_BUFFER_TOO_SMALL)
break;
loop_cleanup:
if (i == 0) {
save_rv = rv;
}
if (unwrap_session) {
meta_release_slot_session(unwrap_session);
unwrap_session = NULL;
}
}
if (rv != CKR_OK) {
if (rv != CKR_BUFFER_TOO_SMALL) {
rv = save_rv;
}
goto finish;
}
slot_unwrapped_key->hObject = hUnwrappedKey;
unwrapped_key->clones[slotnum] = slot_unwrapped_key;
unwrapped_key->master_clone_slotnum = slotnum;
rv = meta_object_get_attr(unwrap_session,
slot_unwrapped_key->hObject, unwrapped_key);
if (rv != CKR_OK) {
goto finish;
}
meta_slot_object_activate(slot_unwrapped_key, unwrap_session,
unwrapped_key->isToken);
slot_unwrapped_key = NULL;
finish:
if (slot_unwrapped_key) {
meta_slot_object_dealloc(slot_unwrapped_key);
}
/* Save the session in case it can be used later */
if (rv == CKR_OK) {
/*
* If currently stored session is not the one being in use now,
* release the previous one and store the current one
*/
if ((session->op1.session) &&
(session->op1.session != unwrap_session)) {
meta_release_slot_session(session->op1.session);
}
/* Save the session */
session->op1.session = unwrap_session;
}
return (rv);
}
/*
* meta_derive_key
*
* Core implementation for C_DeriveKey. This function is a bit gross because
* of PKCS#11 kludges that pass extra object handles in the mechanism
* parameters. Normally C_DeriveKey takes a single existing key as input,
* and creates a single new key as output. But a few mechanisms take 2 keys
* as input, and the two SSL/TLS mechanisms create 4 keys as output.
*
* When an extra input key (basekey2) is set, we set *phBaseKey2 to the clone's
* object handle. phBaseKey2 is provided by the caller so we don't have to
* trudge down into different mechanism parameters to set it when issuing the
* operation.
*
* For the SSL/TLS mechanisms, newKey2/newKey3/newKey4 will be set. We pull
* the new handles from pMech->pParameter in order to fill in the appropriate
* meta_object fields.
*/
CK_RV
meta_derive_key(meta_session_t *session, CK_MECHANISM *pMechanism,
meta_object_t *basekey1, meta_object_t *basekey2,
CK_OBJECT_HANDLE *phBaseKey2,
CK_ATTRIBUTE *pTemplate, CK_ULONG ulAttributeCount,
meta_object_t *newKey1, meta_object_t *newKey2,
meta_object_t *newKey3, meta_object_t *newKey4)
{
CK_RV rv, save_rv;
CK_OBJECT_HANDLE hDerivedKey;
CK_ULONG slotnum;
boolean_t isSSL = B_FALSE;
boolean_t isTLSPRF = B_FALSE;
mechinfo_t **slots = NULL;
unsigned long i, slot_count = 0;
slot_session_t *derive_session = NULL;
slot_object_t *slot_basekey1 = NULL, *slot_basekey2 = NULL;
slot_object_t *slotkey1 = NULL, *slotkey2 = NULL, *slotkey3 = NULL,
*slotkey4 = NULL;
CK_MECHANISM_INFO mech_info;
CK_BBOOL current_token_value = FALSE;
/*
* if the derived key needs to be a token object, can only
* perform the derive operation in the token slot
*/
(void) get_template_boolean(CKA_TOKEN, pTemplate, ulAttributeCount,
&(newKey1->isToken));
(void) get_template_boolean(CKA_PRIVATE, pTemplate, ulAttributeCount,
&(newKey1->isPrivate));
(void) get_template_boolean(CKA_SENSITIVE, pTemplate, ulAttributeCount,
&(newKey1->isSensitive));
if (newKey1->isToken)
current_token_value = TRUE;
/* Can't create token objects in a read-only session. */
if ((IS_READ_ONLY_SESSION(session->session_flags)) &&
newKey1->isToken) {
rv = CKR_SESSION_READ_ONLY;
goto finish;
}
if (meta_freeobject_check(session, newKey1, pMechanism, pTemplate,
ulAttributeCount, 0)) {
if (newKey1->isPrivate && !metaslot_logged_in())
return (CKR_USER_NOT_LOGGED_IN);
if (!meta_freeobject_set(newKey1, pTemplate, ulAttributeCount,
B_FALSE))
return (CKR_FUNCTION_FAILED);
}
mech_info.flags = CKF_DERIVE;
rv = get_slotlist_for_mech(pMechanism->mechanism,
&(session->mech_support_info), &slots, &slot_count,
newKey1->isToken, &mech_info);
if (rv != CKR_OK) {
return (rv);
}
if (pMechanism->mechanism == CKM_SSL3_KEY_AND_MAC_DERIVE ||
pMechanism->mechanism == CKM_TLS_KEY_AND_MAC_DERIVE)
isSSL = B_TRUE;
else if (pMechanism->mechanism == CKM_TLS_PRF)
isTLSPRF = B_TRUE;
rv = meta_slot_object_alloc(&slotkey1);
if (isSSL) {
if (rv == CKR_OK)
rv = meta_slot_object_alloc(&slotkey2);
if (rv == CKR_OK)
rv = meta_slot_object_alloc(&slotkey3);
if (rv == CKR_OK)
rv = meta_slot_object_alloc(&slotkey4);
}
if (rv != CKR_OK) {
goto finish;
}
for (i = 0; i < slot_count; i++) {
slotnum = slots[i]->slotnum;
derive_session = NULL;
if (session->op1.session != NULL) {
if ((session->op1.session)->slotnum == slotnum) {
derive_session = session->op1.session;
/*
* set it to NULL for now, assign it to
* derive_session again if it is successful
*/
session->op1.session = NULL;
} else {
derive_session = NULL;
}
}
if (derive_session == NULL) {
rv = meta_get_slot_session(slotnum, &derive_session,
session->session_flags);
if (rv != CKR_OK) {
goto loop_cleanup;
}
}
rv = meta_object_get_clone(basekey1, slotnum,
derive_session, &slot_basekey1);
if (rv != CKR_OK)
goto loop_cleanup;
if (basekey2) {
rv = meta_object_get_clone(basekey2, slotnum,
derive_session, &slot_basekey2);
if (rv != CKR_OK)
goto loop_cleanup;
/* Pass the handle somewhere in the mech params. */
*phBaseKey2 = slot_basekey2->hObject;
}
if (newKey1->isFreeToken == FREE_ENABLED) {
rv = meta_freetoken_set(slotnum, ¤t_token_value,
pTemplate, ulAttributeCount);
if (rv != CKR_OK)
goto loop_cleanup;
}
rv = FUNCLIST(derive_session->fw_st_id)->C_DeriveKey(
derive_session->hSession, pMechanism,
slot_basekey1->hObject, pTemplate, ulAttributeCount,
(isSSL || isTLSPRF) ? NULL : &hDerivedKey);
if (rv == CKR_OK)
break;
loop_cleanup:
if (i == 0) {
save_rv = rv;
}
if (derive_session) {
meta_release_slot_session(derive_session);
derive_session = NULL;
}
/* No need to cleanup clones, so we can reuse them later. */
}
if (rv != CKR_OK) {
rv = save_rv;
goto finish;
}
if (isTLSPRF)
goto finish;
/*
* These SSL/TLS are unique in that the parameter in the API for
* the new key is unused (NULL). Instead, there are 4 keys which
* are derived, and are passed back through the mechanism params.
* Both mechs use the same mechanism parameter type.
*/
if (isSSL) {
CK_SSL3_KEY_MAT_PARAMS *keyparams;
CK_SSL3_KEY_MAT_OUT *keys;
/* NULL checks already done by caller */
keyparams = (CK_SSL3_KEY_MAT_PARAMS*)pMechanism->pParameter;
keys = keyparams->pReturnedKeyMaterial;
slotkey1->hObject = keys->hClientMacSecret;
slotkey2->hObject = keys->hServerMacSecret;
slotkey3->hObject = keys->hClientKey;
slotkey4->hObject = keys->hServerKey;
rv = meta_object_get_attr(derive_session,
slotkey1->hObject, newKey1);
if (rv != CKR_OK) {
goto finish;
}
rv = meta_object_get_attr(derive_session,
slotkey2->hObject, newKey2);
if (rv != CKR_OK) {
goto finish;
}
rv = meta_object_get_attr(derive_session,
slotkey3->hObject, newKey3);
if (rv != CKR_OK) {
goto finish;
}
rv = meta_object_get_attr(derive_session,
slotkey4->hObject, newKey4);
if (rv != CKR_OK) {
goto finish;
}
newKey1->clones[slotnum] = slotkey1;
newKey2->clones[slotnum] = slotkey2;
newKey3->clones[slotnum] = slotkey3;
newKey4->clones[slotnum] = slotkey4;
newKey1->master_clone_slotnum = slotnum;
newKey2->master_clone_slotnum = slotnum;
newKey3->master_clone_slotnum = slotnum;
newKey4->master_clone_slotnum = slotnum;
meta_slot_object_activate(slotkey1, derive_session,
newKey1->isToken);
slotkey1 = NULL;
meta_slot_object_activate(slotkey2, derive_session,
newKey2->isToken);
slotkey2 = NULL;
meta_slot_object_activate(slotkey3, derive_session,
newKey3->isToken);
slotkey3 = NULL;
meta_slot_object_activate(slotkey4, derive_session,
newKey4->isToken);
slotkey4 = NULL;
} else {
slotkey1->hObject = hDerivedKey;
newKey1->clones[slotnum] = slotkey1;
newKey1->master_clone_slotnum = slotnum;
rv = meta_object_get_attr(derive_session,
slotkey1->hObject, newKey1);
if (rv != CKR_OK) {
goto finish;
}
/* Allow FreeToken to activate onto token obj list */
if (newKey1->isFreeToken == FREE_ENABLED)
newKey1->isToken = B_TRUE;
meta_slot_object_activate(slotkey1, derive_session,
newKey1->isToken);
slotkey1 = NULL;
}
if (newKey1->isFreeObject == FREE_ENABLED)
(void) meta_freeobject_clone(session, newKey1);
finish:
if (slotkey1) {
meta_slot_object_dealloc(slotkey1);
}
if (slotkey2) {
meta_slot_object_dealloc(slotkey2);
}
if (slotkey3) {
meta_slot_object_dealloc(slotkey3);
}
if (slotkey4) {
meta_slot_object_dealloc(slotkey4);
}
/* Save the session in case it can be used later */
if (rv == CKR_OK) {
/*
* If currently stored session is not the one being in use now,
* release the previous one and store the current one
*/
if ((session->op1.session) &&
(session->op1.session != derive_session)) {
meta_release_slot_session(session->op1.session);
}
/* Save the session */
session->op1.session = derive_session;
}
return (rv);
}
/*
* Check the following 4 environment variables for user/application's
* configuration for metaslot. User's configuration takes precedence
* over the system wide configuration for metaslot
*
* ${METASLOT_ENABLED}
* ${METASLOT_OBJECTSTORE_SLOT}
* ${METASLOT_OBJECTSTORE_TOKEN}
* ${METASLOT_AUTO_KEY_MIGRATE}
*
* ${_METASLOT_ENABLE_THRESHOLD} - private environmental variable to
* enable the treshold checking which is disabled by default.
*
* values defined in these environment variables will be stored in the
* global variable "metaslot_config". Variable threshold_chk_disabled is an
* exception.
*/
void
get_user_metaslot_config()
{
char *env_val = NULL;
/*
* Check to see if any environment variable is defined
* by the user for configuring metaslot.
*/
bzero(&metaslot_config, sizeof (metaslot_config));
/* METASLOT_ENABLED */
env_val = getenv("METASLOT_ENABLED");
if (env_val) {
metaslot_config.enabled_specified = B_TRUE;
if (strcasecmp(env_val, TRUE_STRING) == 0) {
metaslot_config.enabled = B_TRUE;
} else if (strcasecmp(env_val, FALSE_STRING) == 0) {
metaslot_config.enabled = B_FALSE;
} else {
/* value is neither 1 or 0, ignore this value */
metaslot_config.enabled_specified = B_FALSE;
}
}
/* METASLOT_AUTO_KEY_MIGRATE */
env_val = getenv("METASLOT_AUTO_KEY_MIGRATE");
if (env_val) {
metaslot_config.auto_key_migrate_specified = B_TRUE;
if (strcasecmp(env_val, TRUE_STRING) == 0) {
metaslot_config.auto_key_migrate = B_TRUE;
} else if (strcasecmp(env_val, FALSE_STRING) == 0) {
metaslot_config.auto_key_migrate = B_FALSE;
} else {
/* value is neither 1 or 0, ignore this value */
metaslot_config.auto_key_migrate_specified = B_FALSE;
}
}
/* METASLOT_OBJECTSTORE_SLOT */
env_val = getenv("METASLOT_OBJECTSTORE_SLOT");
if (env_val) {
metaslot_config.keystore_slot_specified = B_TRUE;
(void) strlcpy((char *)metaslot_config.keystore_slot, env_val,
SLOT_DESCRIPTION_SIZE);
}
/* METASLOT_OBJECTSTORE_TOKEN */
env_val = getenv("METASLOT_OBJECTSTORE_TOKEN");
if (env_val) {
metaslot_config.keystore_token_specified = B_TRUE;
(void) strlcpy((char *)metaslot_config.keystore_token, env_val,
TOKEN_LABEL_SIZE);
}
/* _METASLOT_ENABLE_THRESHOLD */
env_val = getenv("_METASLOT_ENABLE_THRESHOLD");
if (env_val) {
threshold_chk_enabled = B_TRUE;
}
}
|