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
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
9618
9619
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
9684
9685
9686
9687
9688
9689
9690
9691
9692
9693
9694
9695
9696
9697
9698
9699
9700
9701
9702
9703
9704
9705
9706
9707
9708
9709
9710
9711
9712
9713
9714
9715
9716
9717
9718
9719
9720
9721
9722
9723
9724
9725
9726
9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738
9739
9740
9741
9742
9743
9744
9745
9746
9747
9748
9749
9750
9751
9752
9753
9754
9755
9756
9757
9758
9759
9760
9761
9762
9763
9764
9765
9766
9767
9768
9769
9770
9771
9772
9773
9774
9775
9776
9777
9778
9779
9780
9781
9782
9783
9784
9785
9786
9787
9788
9789
9790
9791
9792
9793
9794
9795
9796
9797
9798
9799
9800
9801
9802
9803
9804
9805
9806
9807
9808
9809
9810
9811
9812
9813
9814
9815
9816
9817
9818
9819
9820
9821
9822
9823
9824
9825
9826
9827
9828
9829
9830
9831
9832
9833
9834
9835
9836
9837
9838
9839
9840
9841
9842
9843
9844
9845
9846
9847
9848
9849
9850
9851
9852
9853
9854
9855
9856
9857
9858
9859
9860
9861
9862
9863
9864
9865
9866
9867
9868
9869
9870
9871
9872
9873
9874
9875
9876
9877
9878
9879
9880
9881
9882
9883
9884
9885
9886
9887
9888
9889
9890
9891
9892
9893
9894
9895
9896
9897
9898
9899
9900
9901
9902
9903
9904
9905
9906
9907
9908
9909
9910
9911
9912
9913
9914
9915
9916
9917
9918
9919
9920
9921
9922
9923
9924
9925
9926
9927
9928
9929
9930
9931
9932
9933
9934
9935
9936
9937
9938
9939
9940
9941
9942
9943
9944
9945
9946
9947
9948
9949
9950
9951
9952
9953
9954
9955
9956
9957
9958
9959
9960
9961
9962
9963
9964
9965
9966
9967
9968
9969
9970
9971
9972
9973
9974
9975
9976
9977
9978
9979
9980
9981
9982
9983
9984
9985
9986
9987
9988
9989
9990
9991
9992
9993
9994
9995
9996
9997
9998
9999
10000
10001
10002
10003
10004
10005
10006
10007
10008
10009
10010
10011
10012
10013
10014
10015
10016
10017
10018
10019
10020
10021
10022
10023
10024
10025
10026
10027
10028
10029
10030
10031
10032
10033
10034
10035
10036
10037
10038
10039
10040
10041
10042
10043
10044
10045
10046
10047
10048
10049
10050
10051
10052
10053
10054
10055
10056
10057
10058
10059
10060
10061
10062
10063
10064
10065
10066
10067
10068
10069
10070
10071
10072
10073
10074
10075
10076
10077
10078
10079
10080
10081
10082
10083
10084
10085
10086
10087
10088
10089
10090
10091
10092
10093
10094
10095
10096
10097
10098
10099
10100
10101
10102
10103
10104
10105
10106
10107
10108
10109
10110
10111
10112
10113
10114
10115
10116
10117
10118
10119
10120
10121
10122
10123
10124
10125
10126
10127
10128
10129
10130
10131
10132
10133
10134
10135
10136
10137
10138
10139
10140
10141
10142
10143
10144
10145
10146
10147
10148
10149
10150
10151
10152
10153
10154
10155
10156
10157
10158
10159
10160
10161
10162
10163
10164
10165
10166
10167
10168
10169
10170
10171
10172
10173
10174
10175
10176
10177
10178
10179
10180
10181
10182
10183
10184
10185
10186
10187
10188
10189
10190
10191
10192
10193
10194
10195
10196
10197
10198
10199
10200
10201
10202
10203
10204
10205
10206
10207
10208
10209
10210
10211
10212
10213
10214
10215
10216
10217
10218
10219
10220
10221
10222
10223
10224
10225
10226
10227
10228
10229
10230
10231
10232
10233
10234
10235
10236
10237
10238
10239
10240
10241
10242
10243
10244
10245
10246
10247
10248
10249
10250
10251
10252
10253
10254
10255
10256
10257
10258
10259
10260
10261
10262
10263
10264
10265
10266
10267
10268
10269
10270
10271
10272
10273
10274
10275
10276
10277
10278
10279
10280
10281
10282
10283
10284
10285
10286
10287
10288
10289
10290
10291
10292
10293
10294
10295
10296
10297
10298
10299
10300
10301
10302
10303
10304
10305
10306
10307
10308
10309
10310
10311
10312
10313
10314
10315
10316
10317
10318
10319
10320
10321
10322
10323
10324
10325
10326
10327
10328
10329
10330
10331
10332
10333
10334
10335
10336
10337
10338
10339
10340
10341
10342
10343
10344
10345
10346
10347
10348
10349
10350
10351
10352
10353
10354
10355
10356
10357
10358
10359
10360
10361
10362
10363
10364
10365
10366
10367
10368
10369
10370
10371
10372
10373
10374
10375
10376
10377
10378
10379
10380
10381
10382
10383
10384
10385
10386
10387
10388
10389
10390
10391
10392
10393
10394
10395
10396
10397
10398
10399
10400
10401
10402
10403
10404
10405
10406
10407
10408
10409
10410
10411
10412
10413
10414
10415
10416
10417
10418
10419
10420
10421
10422
10423
10424
10425
10426
10427
10428
10429
10430
10431
10432
10433
10434
10435
10436
10437
10438
10439
10440
10441
10442
10443
10444
10445
10446
10447
10448
10449
10450
10451
10452
10453
10454
10455
10456
10457
10458
10459
10460
10461
10462
10463
10464
10465
10466
10467
10468
10469
10470
10471
10472
10473
10474
10475
10476
10477
10478
10479
10480
10481
10482
10483
10484
10485
10486
10487
10488
10489
10490
10491
10492
10493
10494
10495
10496
10497
10498
10499
10500
10501
10502
10503
10504
10505
10506
10507
10508
10509
10510
10511
10512
10513
10514
10515
10516
10517
10518
10519
10520
10521
10522
10523
10524
10525
10526
10527
10528
10529
10530
10531
10532
10533
10534
10535
10536
10537
10538
10539
10540
10541
10542
10543
10544
10545
10546
10547
10548
10549
10550
10551
10552
10553
10554
10555
10556
10557
10558
10559
10560
10561
10562
10563
10564
10565
10566
10567
10568
10569
10570
10571
10572
10573
10574
10575
10576
10577
10578
10579
10580
10581
10582
10583
10584
10585
10586
10587
10588
10589
10590
10591
10592
10593
10594
10595
10596
10597
10598
10599
10600
10601
10602
10603
10604
10605
10606
10607
10608
10609
10610
10611
10612
10613
10614
10615
10616
10617
10618
10619
10620
10621
10622
10623
10624
10625
10626
10627
10628
10629
10630
10631
10632
10633
10634
10635
10636
10637
10638
10639
10640
10641
10642
10643
10644
10645
10646
10647
10648
10649
10650
10651
10652
10653
10654
10655
10656
10657
10658
10659
10660
10661
10662
10663
10664
10665
10666
10667
10668
10669
10670
10671
10672
10673
10674
10675
10676
10677
10678
10679
10680
10681
10682
10683
10684
10685
10686
10687
10688
10689
10690
10691
10692
10693
10694
10695
10696
10697
10698
10699
10700
10701
10702
10703
10704
10705
10706
10707
10708
10709
10710
10711
10712
10713
10714
10715
10716
10717
10718
10719
10720
10721
10722
10723
10724
10725
10726
10727
10728
10729
10730
10731
10732
10733
10734
10735
10736
10737
10738
10739
10740
10741
10742
10743
10744
10745
10746
10747
10748
10749
10750
10751
10752
10753
10754
10755
10756
10757
10758
10759
10760
10761
10762
10763
10764
10765
10766
10767
10768
10769
10770
10771
10772
10773
10774
10775
10776
10777
10778
10779
10780
10781
10782
10783
10784
10785
10786
10787
10788
10789
10790
10791
10792
10793
10794
10795
10796
10797
10798
10799
10800
10801
10802
10803
10804
10805
10806
10807
10808
10809
10810
10811
10812
10813
10814
10815
10816
10817
10818
10819
10820
10821
10822
10823
10824
10825
10826
10827
10828
10829
10830
10831
10832
10833
10834
10835
10836
10837
10838
10839
10840
10841
10842
10843
10844
10845
10846
10847
10848
10849
10850
10851
10852
10853
10854
10855
10856
10857
10858
10859
10860
10861
10862
10863
10864
10865
10866
10867
10868
10869
10870
10871
10872
10873
10874
10875
10876
10877
10878
10879
10880
10881
10882
10883
10884
10885
10886
10887
10888
10889
10890
10891
10892
10893
10894
10895
10896
10897
10898
10899
10900
10901
10902
10903
10904
10905
10906
10907
10908
10909
10910
10911
10912
10913
10914
10915
10916
10917
10918
10919
10920
10921
10922
10923
10924
10925
10926
10927
10928
10929
10930
10931
10932
10933
10934
10935
10936
10937
10938
10939
10940
10941
10942
10943
10944
10945
10946
10947
10948
10949
10950
10951
10952
10953
10954
10955
10956
10957
10958
10959
10960
10961
10962
10963
10964
10965
10966
10967
10968
10969
10970
10971
10972
10973
10974
10975
10976
10977
10978
10979
10980
10981
10982
10983
10984
10985
10986
10987
10988
10989
10990
10991
10992
10993
10994
10995
10996
10997
10998
10999
11000
11001
11002
11003
11004
11005
11006
11007
11008
11009
11010
11011
11012
11013
11014
11015
11016
11017
11018
11019
11020
11021
11022
11023
11024
11025
11026
11027
11028
11029
11030
11031
11032
11033
11034
11035
11036
11037
11038
11039
11040
11041
11042
11043
11044
11045
11046
11047
11048
11049
11050
11051
11052
11053
11054
11055
11056
11057
11058
11059
11060
11061
11062
11063
11064
11065
11066
11067
11068
11069
11070
11071
11072
11073
11074
11075
11076
11077
11078
11079
11080
11081
11082
11083
11084
11085
11086
11087
11088
11089
11090
11091
11092
11093
11094
11095
11096
11097
11098
11099
11100
11101
11102
11103
11104
11105
11106
11107
11108
11109
11110
11111
11112
11113
11114
11115
11116
11117
11118
11119
11120
11121
11122
11123
11124
11125
11126
11127
11128
11129
11130
11131
11132
11133
11134
11135
11136
11137
11138
11139
11140
11141
11142
11143
11144
11145
11146
11147
11148
11149
11150
11151
11152
11153
11154
11155
11156
11157
11158
11159
11160
11161
11162
11163
11164
11165
11166
11167
11168
11169
11170
11171
11172
11173
11174
11175
11176
11177
11178
11179
11180
11181
11182
11183
11184
11185
11186
11187
11188
11189
11190
11191
11192
11193
11194
11195
11196
11197
11198
11199
11200
11201
11202
11203
11204
11205
11206
11207
11208
11209
11210
11211
11212
11213
11214
11215
11216
11217
11218
11219
11220
11221
11222
11223
11224
11225
11226
11227
11228
11229
11230
11231
11232
11233
11234
11235
11236
11237
11238
11239
11240
11241
11242
11243
11244
11245
11246
11247
11248
11249
11250
11251
11252
11253
11254
11255
11256
11257
11258
11259
11260
11261
11262
11263
11264
11265
11266
11267
11268
11269
11270
11271
11272
11273
11274
11275
11276
11277
11278
11279
11280
11281
11282
11283
11284
11285
11286
11287
11288
11289
11290
11291
11292
11293
11294
11295
11296
11297
11298
11299
11300
11301
11302
11303
11304
11305
11306
11307
11308
11309
11310
11311
11312
11313
11314
11315
11316
11317
11318
11319
11320
11321
11322
11323
11324
11325
11326
11327
11328
11329
11330
11331
11332
11333
11334
11335
11336
11337
11338
11339
11340
11341
11342
11343
11344
11345
11346
11347
11348
11349
11350
11351
11352
11353
11354
11355
11356
11357
11358
11359
11360
11361
11362
11363
11364
11365
11366
11367
11368
11369
11370
11371
11372
11373
11374
11375
11376
11377
11378
11379
11380
11381
11382
11383
11384
11385
11386
11387
11388
11389
11390
11391
11392
11393
11394
11395
11396
11397
11398
11399
11400
11401
11402
11403
11404
11405
11406
11407
11408
11409
11410
11411
11412
11413
11414
11415
11416
11417
11418
11419
11420
11421
11422
11423
11424
11425
11426
11427
11428
11429
11430
11431
11432
11433
11434
11435
11436
11437
11438
11439
11440
11441
11442
11443
11444
11445
11446
11447
11448
11449
11450
11451
11452
11453
11454
11455
11456
11457
11458
11459
11460
11461
11462
11463
11464
11465
11466
11467
11468
11469
11470
11471
11472
11473
11474
11475
11476
11477
11478
11479
11480
11481
11482
11483
11484
11485
11486
11487
11488
11489
11490
11491
11492
11493
11494
11495
11496
11497
11498
11499
11500
11501
11502
11503
11504
11505
11506
11507
11508
11509
11510
11511
11512
11513
11514
11515
11516
11517
11518
11519
11520
11521
11522
11523
11524
11525
11526
11527
11528
11529
11530
11531
11532
11533
11534
11535
11536
11537
11538
11539
11540
11541
11542
11543
11544
11545
11546
11547
11548
11549
11550
11551
11552
11553
11554
11555
11556
11557
11558
11559
11560
11561
11562
11563
11564
11565
11566
11567
11568
11569
11570
11571
11572
11573
11574
11575
11576
11577
11578
11579
11580
11581
11582
11583
11584
11585
11586
11587
11588
11589
11590
11591
11592
11593
11594
11595
11596
11597
11598
11599
11600
11601
11602
11603
11604
11605
11606
11607
11608
11609
11610
11611
11612
11613
11614
11615
11616
11617
11618
11619
11620
11621
11622
11623
11624
11625
11626
11627
11628
11629
11630
11631
11632
11633
11634
11635
11636
11637
11638
11639
11640
11641
11642
11643
11644
11645
11646
11647
11648
11649
11650
11651
11652
11653
11654
11655
11656
11657
11658
11659
11660
11661
11662
11663
11664
11665
11666
11667
11668
11669
11670
11671
11672
11673
11674
11675
11676
11677
11678
11679
11680
11681
11682
11683
11684
11685
11686
11687
11688
11689
11690
11691
11692
11693
11694
11695
11696
11697
11698
11699
11700
11701
11702
11703
11704
11705
11706
11707
11708
11709
11710
11711
11712
11713
11714
11715
11716
11717
11718
11719
11720
11721
11722
11723
11724
11725
11726
11727
11728
11729
11730
11731
11732
11733
11734
11735
11736
11737
11738
11739
11740
11741
11742
11743
11744
11745
11746
11747
11748
11749
11750
11751
11752
11753
11754
11755
11756
11757
11758
11759
11760
11761
11762
11763
11764
11765
11766
11767
11768
11769
11770
11771
11772
11773
11774
11775
11776
11777
11778
11779
11780
11781
11782
11783
11784
11785
11786
11787
11788
11789
11790
11791
11792
11793
11794
11795
11796
11797
11798
11799
11800
11801
11802
11803
11804
11805
11806
11807
11808
11809
11810
11811
11812
11813
11814
11815
11816
11817
11818
11819
11820
11821
11822
11823
11824
11825
11826
11827
11828
11829
11830
11831
11832
11833
11834
11835
11836
11837
11838
11839
11840
11841
11842
11843
11844
11845
11846
11847
11848
11849
11850
11851
11852
11853
11854
11855
11856
11857
11858
11859
11860
11861
11862
11863
11864
11865
11866
11867
11868
11869
11870
11871
11872
11873
11874
11875
11876
11877
11878
11879
11880
11881
11882
11883
11884
11885
11886
11887
11888
11889
11890
11891
11892
11893
11894
11895
11896
11897
11898
11899
11900
11901
11902
11903
11904
11905
11906
11907
11908
11909
11910
11911
11912
11913
11914
11915
11916
11917
11918
11919
11920
11921
11922
11923
11924
11925
11926
11927
11928
11929
11930
11931
11932
11933
11934
11935
11936
11937
11938
11939
11940
11941
11942
11943
11944
11945
11946
11947
11948
11949
11950
11951
11952
11953
11954
11955
11956
11957
11958
11959
11960
11961
11962
11963
11964
11965
11966
11967
11968
11969
11970
11971
11972
11973
11974
11975
11976
11977
11978
11979
11980
11981
11982
11983
11984
11985
11986
11987
11988
11989
11990
11991
11992
11993
11994
11995
11996
11997
11998
11999
12000
12001
12002
12003
12004
12005
12006
12007
12008
12009
12010
12011
12012
12013
12014
12015
12016
12017
12018
12019
12020
12021
12022
12023
12024
12025
12026
12027
12028
12029
12030
12031
12032
12033
12034
12035
12036
12037
12038
12039
12040
12041
12042
12043
12044
12045
12046
12047
12048
12049
12050
12051
12052
12053
12054
12055
12056
12057
12058
12059
12060
12061
12062
12063
12064
12065
12066
12067
12068
12069
12070
12071
12072
12073
12074
12075
12076
12077
12078
12079
12080
12081
12082
12083
12084
12085
12086
12087
12088
12089
12090
12091
12092
12093
12094
12095
12096
12097
12098
12099
12100
12101
12102
12103
12104
12105
12106
12107
12108
12109
12110
12111
12112
12113
12114
12115
12116
12117
12118
12119
12120
12121
12122
12123
12124
12125
12126
12127
12128
12129
12130
12131
12132
12133
12134
12135
12136
12137
12138
12139
12140
12141
12142
12143
12144
12145
12146
12147
12148
12149
12150
12151
12152
12153
12154
12155
12156
12157
12158
12159
12160
12161
12162
12163
12164
12165
12166
12167
12168
12169
12170
12171
12172
12173
12174
12175
12176
12177
12178
12179
12180
12181
12182
12183
12184
12185
12186
12187
12188
12189
12190
12191
12192
12193
12194
12195
12196
12197
12198
12199
12200
12201
12202
12203
12204
12205
12206
12207
12208
12209
12210
12211
12212
12213
12214
12215
12216
12217
12218
12219
12220
12221
12222
12223
12224
12225
12226
12227
12228
12229
12230
12231
12232
12233
12234
12235
12236
12237
12238
12239
12240
12241
12242
12243
12244
12245
12246
12247
12248
12249
12250
12251
12252
12253
12254
12255
12256
12257
12258
12259
12260
12261
12262
12263
12264
12265
12266
12267
12268
12269
12270
12271
12272
12273
12274
12275
12276
12277
12278
12279
12280
12281
12282
12283
12284
12285
12286
12287
12288
12289
12290
12291
12292
12293
12294
12295
12296
12297
12298
12299
12300
12301
12302
12303
12304
12305
12306
12307
12308
12309
12310
12311
12312
12313
12314
12315
12316
12317
12318
12319
12320
12321
12322
12323
12324
12325
12326
12327
12328
12329
12330
12331
12332
12333
12334
12335
12336
12337
12338
12339
12340
12341
12342
12343
12344
12345
12346
12347
12348
12349
12350
12351
12352
12353
12354
12355
12356
12357
12358
12359
12360
12361
12362
12363
12364
12365
12366
12367
12368
12369
12370
12371
12372
12373
12374
12375
12376
12377
12378
12379
12380
12381
12382
12383
12384
12385
12386
12387
12388
12389
12390
12391
12392
12393
12394
12395
12396
12397
12398
12399
12400
12401
12402
12403
12404
12405
12406
12407
12408
12409
12410
12411
12412
12413
12414
12415
12416
12417
12418
12419
12420
12421
12422
12423
12424
12425
12426
12427
12428
12429
12430
12431
12432
12433
12434
12435
12436
12437
12438
12439
12440
12441
12442
12443
12444
12445
12446
12447
12448
12449
12450
12451
12452
12453
12454
12455
12456
12457
12458
12459
12460
12461
12462
12463
12464
12465
12466
12467
12468
12469
12470
12471
12472
12473
12474
12475
12476
12477
12478
12479
12480
12481
12482
12483
12484
12485
12486
12487
12488
12489
12490
12491
12492
12493
12494
12495
12496
12497
12498
12499
12500
12501
12502
12503
12504
12505
12506
12507
12508
12509
12510
12511
12512
12513
12514
12515
12516
12517
12518
12519
12520
12521
12522
12523
12524
12525
12526
12527
12528
12529
12530
12531
12532
12533
12534
12535
12536
12537
12538
12539
12540
12541
12542
12543
12544
12545
12546
12547
12548
12549
12550
12551
12552
12553
12554
12555
12556
12557
12558
12559
12560
12561
12562
12563
12564
12565
12566
12567
12568
12569
12570
12571
12572
12573
12574
12575
12576
12577
12578
12579
12580
12581
12582
12583
12584
12585
12586
12587
12588
12589
12590
12591
12592
12593
12594
12595
12596
12597
12598
12599
12600
12601
12602
12603
12604
12605
12606
12607
12608
12609
12610
12611
12612
12613
12614
12615
12616
12617
12618
12619
12620
12621
12622
12623
12624
12625
12626
12627
12628
12629
12630
12631
12632
12633
12634
12635
12636
12637
12638
12639
12640
12641
12642
12643
12644
12645
12646
12647
12648
12649
12650
12651
12652
12653
12654
12655
12656
12657
12658
12659
12660
12661
12662
12663
12664
12665
12666
12667
12668
12669
12670
12671
12672
12673
12674
12675
12676
12677
12678
12679
12680
12681
12682
12683
12684
12685
12686
12687
12688
12689
12690
12691
12692
12693
12694
12695
12696
12697
12698
12699
12700
12701
12702
12703
12704
12705
12706
12707
12708
12709
12710
12711
12712
12713
12714
12715
12716
12717
12718
12719
12720
12721
12722
12723
12724
12725
12726
12727
12728
12729
12730
12731
12732
12733
12734
12735
12736
12737
12738
12739
12740
12741
12742
12743
12744
12745
12746
12747
12748
12749
12750
12751
12752
12753
12754
12755
12756
12757
12758
12759
12760
12761
12762
12763
12764
12765
12766
12767
12768
12769
12770
12771
12772
12773
12774
12775
12776
12777
12778
12779
12780
12781
12782
12783
12784
12785
12786
12787
12788
12789
12790
12791
12792
12793
12794
12795
12796
12797
12798
12799
12800
12801
12802
12803
12804
12805
12806
12807
12808
12809
12810
12811
12812
12813
12814
12815
12816
12817
12818
12819
12820
12821
12822
12823
12824
12825
12826
12827
12828
12829
12830
12831
12832
12833
12834
12835
12836
12837
12838
12839
12840
12841
12842
12843
12844
12845
12846
12847
12848
12849
12850
12851
12852
12853
12854
12855
12856
12857
12858
12859
12860
12861
12862
12863
12864
12865
12866
12867
12868
12869
12870
12871
12872
12873
12874
12875
12876
12877
12878
12879
12880
12881
12882
12883
12884
12885
12886
12887
12888
12889
12890
12891
12892
12893
12894
12895
12896
12897
12898
12899
12900
12901
12902
12903
12904
12905
12906
12907
12908
12909
12910
12911
12912
12913
12914
12915
12916
12917
12918
12919
12920
12921
12922
12923
12924
12925
12926
12927
12928
12929
12930
12931
12932
12933
12934
12935
12936
12937
12938
12939
12940
12941
12942
12943
12944
12945
12946
12947
12948
12949
12950
12951
12952
12953
12954
12955
12956
12957
12958
12959
12960
12961
12962
12963
12964
12965
12966
12967
12968
12969
12970
12971
12972
12973
12974
12975
12976
12977
12978
12979
12980
12981
12982
12983
12984
12985
12986
12987
12988
12989
12990
12991
12992
12993
12994
12995
12996
12997
12998
12999
13000
13001
13002
13003
13004
13005
13006
13007
13008
13009
13010
13011
13012
13013
13014
13015
13016
13017
13018
13019
13020
13021
13022
13023
13024
13025
13026
13027
13028
13029
13030
13031
13032
13033
13034
13035
13036
13037
13038
13039
13040
13041
13042
13043
13044
13045
13046
13047
13048
13049
13050
13051
13052
13053
13054
13055
13056
13057
13058
13059
13060
13061
13062
13063
13064
13065
13066
13067
13068
13069
13070
13071
13072
13073
13074
13075
13076
13077
13078
13079
13080
13081
13082
13083
13084
13085
13086
13087
13088
13089
13090
13091
13092
13093
13094
13095
13096
13097
13098
13099
13100
13101
13102
13103
13104
13105
13106
13107
13108
13109
13110
13111
13112
13113
13114
13115
13116
13117
13118
13119
13120
13121
13122
13123
13124
13125
13126
13127
13128
13129
13130
13131
13132
13133
13134
13135
13136
13137
13138
13139
13140
13141
13142
13143
13144
13145
13146
13147
13148
13149
13150
13151
13152
13153
13154
13155
13156
13157
13158
13159
13160
13161
13162
13163
13164
13165
13166
13167
13168
13169
13170
13171
13172
13173
13174
13175
13176
13177
13178
13179
13180
13181
13182
13183
13184
13185
13186
13187
13188
13189
13190
13191
13192
13193
13194
13195
13196
13197
13198
13199
13200
13201
13202
13203
13204
13205
13206
13207
13208
13209
13210
13211
13212
13213
13214
13215
13216
13217
13218
13219
13220
13221
13222
13223
13224
13225
13226
13227
13228
13229
13230
13231
13232
13233
13234
13235
13236
13237
13238
13239
13240
13241
13242
13243
13244
13245
13246
13247
13248
13249
13250
13251
13252
13253
13254
13255
13256
13257
13258
13259
13260
13261
13262
13263
13264
13265
13266
13267
13268
13269
13270
13271
13272
13273
13274
13275
13276
13277
13278
13279
13280
13281
13282
13283
13284
13285
13286
13287
13288
13289
13290
13291
13292
13293
13294
13295
13296
13297
13298
13299
13300
13301
13302
13303
13304
13305
13306
13307
13308
13309
13310
13311
13312
13313
13314
13315
13316
13317
13318
13319
13320
13321
13322
13323
13324
13325
13326
13327
13328
13329
13330
13331
13332
13333
13334
13335
13336
13337
13338
13339
13340
13341
13342
13343
13344
13345
13346
13347
13348
13349
13350
13351
13352
13353
13354
13355
13356
13357
13358
13359
13360
13361
13362
13363
13364
13365
13366
13367
13368
13369
13370
13371
13372
13373
13374
13375
13376
13377
13378
13379
13380
13381
13382
13383
13384
13385
13386
13387
13388
13389
13390
13391
13392
13393
13394
13395
13396
13397
13398
13399
13400
13401
13402
13403
13404
13405
13406
13407
13408
13409
13410
13411
13412
13413
13414
13415
13416
13417
13418
13419
13420
13421
13422
13423
13424
13425
13426
13427
13428
13429
13430
13431
13432
13433
13434
13435
13436
13437
13438
13439
13440
13441
13442
13443
13444
13445
13446
13447
13448
13449
13450
13451
13452
13453
13454
13455
13456
13457
13458
13459
13460
13461
13462
13463
13464
13465
13466
13467
13468
13469
13470
13471
13472
13473
13474
13475
13476
13477
13478
13479
13480
13481
13482
13483
13484
13485
13486
13487
13488
13489
13490
13491
13492
13493
13494
13495
13496
13497
13498
13499
13500
13501
13502
13503
13504
13505
13506
13507
13508
13509
13510
13511
13512
13513
13514
13515
13516
13517
13518
13519
13520
13521
13522
13523
13524
13525
13526
13527
13528
13529
13530
13531
13532
13533
13534
13535
13536
13537
13538
13539
13540
13541
13542
13543
13544
13545
13546
13547
13548
13549
13550
13551
13552
13553
13554
13555
13556
13557
13558
13559
13560
13561
13562
13563
13564
13565
13566
13567
13568
13569
13570
13571
13572
13573
13574
13575
13576
13577
13578
13579
13580
13581
13582
13583
13584
13585
13586
13587
13588
13589
13590
13591
13592
13593
13594
13595
13596
13597
13598
13599
13600
13601
13602
13603
13604
13605
13606
13607
13608
13609
13610
13611
13612
13613
13614
13615
13616
13617
13618
13619
13620
13621
13622
13623
13624
13625
13626
13627
13628
13629
13630
13631
13632
13633
13634
13635
13636
13637
13638
13639
13640
13641
13642
13643
13644
13645
13646
13647
13648
13649
13650
13651
13652
13653
13654
13655
13656
13657
13658
13659
13660
13661
13662
13663
13664
13665
13666
13667
13668
13669
13670
13671
13672
13673
13674
13675
13676
13677
13678
13679
13680
13681
13682
13683
13684
13685
13686
13687
13688
13689
13690
13691
13692
13693
13694
13695
13696
13697
13698
13699
13700
13701
13702
13703
13704
13705
13706
13707
13708
13709
13710
13711
13712
13713
13714
13715
13716
13717
13718
13719
13720
13721
13722
13723
13724
13725
13726
13727
13728
13729
13730
13731
13732
13733
13734
13735
13736
13737
13738
13739
13740
13741
13742
13743
13744
13745
13746
13747
13748
13749
13750
13751
13752
13753
13754
13755
13756
13757
13758
13759
13760
13761
13762
13763
13764
13765
13766
13767
13768
13769
13770
13771
13772
13773
13774
13775
13776
13777
13778
13779
13780
13781
13782
13783
13784
13785
13786
13787
13788
13789
13790
13791
13792
13793
13794
13795
13796
13797
13798
13799
13800
13801
13802
13803
13804
13805
13806
13807
13808
13809
13810
13811
13812
13813
13814
13815
13816
13817
13818
13819
13820
13821
13822
13823
13824
13825
13826
13827
13828
13829
13830
13831
13832
13833
13834
13835
13836
13837
13838
13839
13840
13841
13842
13843
13844
13845
13846
13847
13848
13849
13850
13851
13852
13853
13854
13855
13856
13857
13858
13859
13860
13861
13862
13863
13864
13865
13866
13867
13868
13869
13870
13871
13872
13873
13874
13875
13876
13877
13878
13879
13880
13881
13882
13883
13884
13885
13886
13887
13888
13889
13890
13891
13892
13893
13894
13895
13896
13897
13898
13899
13900
13901
13902
13903
13904
13905
13906
13907
13908
13909
13910
13911
13912
13913
13914
13915
13916
13917
13918
13919
13920
13921
13922
13923
13924
13925
13926
13927
13928
13929
13930
13931
13932
13933
13934
13935
13936
13937
13938
13939
13940
13941
13942
13943
13944
13945
13946
13947
13948
13949
13950
13951
13952
13953
13954
13955
13956
13957
13958
13959
13960
13961
13962
13963
13964
13965
13966
13967
13968
13969
13970
13971
13972
13973
13974
13975
13976
13977
13978
13979
13980
13981
13982
13983
13984
13985
13986
13987
13988
13989
13990
13991
13992
13993
13994
13995
13996
13997
13998
13999
14000
14001
14002
14003
14004
14005
14006
14007
14008
14009
14010
14011
14012
14013
14014
14015
14016
14017
14018
14019
14020
14021
14022
14023
14024
14025
14026
14027
14028
14029
14030
14031
14032
14033
14034
14035
14036
14037
14038
14039
14040
14041
14042
14043
14044
14045
14046
14047
14048
14049
14050
14051
14052
14053
14054
14055
14056
14057
14058
14059
14060
14061
14062
14063
14064
14065
14066
14067
14068
14069
14070
14071
14072
14073
14074
14075
14076
14077
14078
14079
14080
14081
14082
14083
14084
14085
14086
14087
14088
14089
14090
14091
14092
14093
14094
14095
14096
14097
14098
14099
14100
14101
14102
14103
14104
14105
14106
14107
14108
14109
14110
14111
14112
14113
14114
14115
14116
14117
14118
14119
14120
14121
14122
14123
14124
14125
14126
14127
14128
14129
14130
14131
14132
14133
14134
14135
14136
14137
14138
14139
14140
14141
14142
14143
14144
14145
14146
14147
14148
14149
14150
14151
14152
14153
14154
14155
14156
14157
14158
14159
14160
14161
14162
14163
14164
14165
14166
14167
14168
14169
14170
14171
14172
14173
14174
14175
14176
14177
14178
14179
14180
14181
14182
14183
14184
14185
14186
14187
14188
14189
14190
14191
14192
14193
14194
14195
14196
14197
14198
14199
14200
14201
14202
14203
14204
14205
14206
14207
14208
14209
14210
14211
14212
14213
14214
14215
14216
14217
14218
14219
14220
14221
14222
14223
14224
14225
14226
14227
14228
14229
14230
14231
14232
14233
14234
14235
14236
14237
14238
14239
14240
14241
14242
14243
14244
14245
14246
14247
14248
14249
14250
14251
14252
14253
14254
14255
14256
14257
14258
14259
14260
14261
14262
14263
14264
14265
14266
14267
14268
14269
14270
14271
14272
14273
14274
14275
14276
14277
14278
14279
14280
14281
14282
14283
14284
14285
14286
14287
14288
14289
14290
14291
14292
14293
14294
14295
14296
14297
14298
14299
14300
14301
14302
14303
14304
14305
14306
14307
14308
14309
14310
14311
14312
14313
14314
14315
14316
14317
14318
14319
14320
14321
14322
14323
14324
14325
14326
14327
14328
14329
14330
14331
14332
14333
14334
14335
14336
14337
14338
14339
14340
14341
14342
14343
14344
14345
14346
14347
14348
14349
14350
14351
14352
14353
14354
14355
14356
14357
14358
14359
14360
14361
14362
14363
14364
14365
14366
14367
14368
14369
14370
14371
14372
14373
14374
14375
14376
14377
14378
14379
14380
14381
14382
14383
14384
14385
14386
14387
14388
14389
14390
14391
14392
14393
14394
14395
14396
14397
14398
14399
14400
14401
14402
14403
14404
14405
14406
14407
14408
14409
14410
14411
14412
14413
14414
14415
14416
14417
14418
14419
14420
14421
14422
14423
14424
14425
14426
14427
14428
14429
14430
14431
14432
14433
14434
14435
14436
14437
14438
14439
14440
14441
14442
14443
14444
14445
14446
14447
14448
14449
14450
14451
14452
14453
14454
14455
14456
14457
14458
14459
14460
14461
14462
14463
14464
14465
14466
14467
14468
14469
14470
14471
14472
14473
14474
14475
14476
14477
14478
14479
14480
14481
14482
14483
14484
14485
14486
14487
14488
14489
14490
14491
14492
14493
14494
14495
14496
14497
14498
14499
14500
14501
14502
14503
14504
14505
14506
14507
14508
14509
14510
14511
14512
14513
14514
14515
14516
14517
14518
14519
14520
14521
14522
14523
14524
14525
14526
14527
14528
14529
14530
14531
14532
14533
14534
14535
14536
14537
14538
14539
14540
14541
14542
14543
14544
14545
14546
14547
14548
14549
14550
14551
14552
14553
14554
14555
14556
14557
14558
14559
14560
14561
14562
14563
14564
14565
14566
14567
14568
14569
14570
14571
14572
14573
14574
14575
14576
14577
14578
14579
14580
14581
14582
14583
14584
14585
14586
14587
14588
14589
14590
14591
14592
14593
14594
14595
14596
14597
14598
14599
14600
14601
14602
14603
14604
14605
14606
14607
14608
14609
14610
14611
14612
14613
14614
14615
14616
14617
14618
14619
14620
14621
14622
14623
14624
14625
14626
14627
14628
14629
14630
14631
14632
14633
14634
14635
14636
14637
14638
14639
14640
14641
14642
14643
14644
14645
14646
14647
14648
14649
14650
14651
14652
14653
14654
14655
14656
14657
14658
14659
14660
14661
14662
14663
14664
14665
14666
14667
14668
14669
14670
14671
14672
14673
14674
14675
14676
14677
14678
14679
14680
14681
14682
14683
14684
14685
14686
14687
14688
14689
14690
14691
14692
14693
14694
14695
14696
14697
14698
14699
14700
14701
14702
14703
14704
14705
14706
14707
14708
14709
14710
14711
14712
14713
14714
14715
14716
14717
14718
14719
14720
14721
14722
14723
14724
14725
14726
14727
14728
14729
14730
14731
14732
14733
14734
14735
14736
14737
14738
14739
14740
14741
14742
14743
14744
14745
14746
14747
14748
14749
14750
14751
14752
14753
14754
14755
14756
14757
14758
14759
14760
14761
14762
14763
14764
14765
14766
14767
14768
14769
14770
14771
14772
14773
14774
14775
14776
14777
14778
14779
14780
14781
14782
14783
14784
14785
14786
14787
14788
14789
14790
14791
14792
14793
14794
14795
14796
14797
14798
14799
14800
14801
14802
14803
14804
14805
14806
14807
14808
14809
14810
14811
14812
14813
14814
14815
14816
14817
14818
14819
14820
14821
14822
14823
14824
14825
14826
14827
14828
14829
14830
14831
14832
14833
14834
14835
14836
14837
14838
14839
14840
14841
14842
14843
14844
14845
14846
14847
14848
14849
14850
14851
14852
14853
14854
14855
14856
14857
14858
14859
14860
14861
14862
14863
14864
14865
14866
14867
14868
14869
14870
14871
14872
14873
14874
14875
14876
14877
14878
14879
14880
14881
14882
14883
14884
14885
14886
14887
14888
14889
14890
14891
14892
14893
14894
14895
14896
14897
14898
14899
14900
14901
14902
14903
14904
14905
14906
14907
14908
14909
14910
14911
14912
14913
14914
14915
14916
14917
14918
14919
14920
14921
14922
14923
14924
14925
14926
14927
14928
14929
14930
14931
14932
14933
14934
14935
14936
14937
14938
14939
14940
14941
14942
14943
14944
14945
14946
14947
14948
14949
14950
14951
14952
14953
14954
14955
14956
14957
14958
14959
14960
14961
14962
14963
14964
14965
14966
14967
14968
14969
14970
14971
14972
14973
14974
14975
14976
14977
14978
14979
14980
14981
14982
14983
14984
14985
14986
14987
14988
14989
14990
14991
14992
14993
14994
14995
14996
14997
14998
14999
15000
15001
15002
15003
15004
15005
15006
15007
15008
15009
15010
15011
15012
15013
15014
15015
15016
15017
15018
15019
15020
15021
15022
15023
15024
15025
15026
15027
15028
15029
15030
15031
15032
15033
15034
15035
15036
15037
15038
15039
15040
15041
15042
15043
15044
15045
15046
15047
15048
15049
15050
15051
15052
15053
15054
15055
15056
15057
15058
15059
15060
15061
15062
15063
15064
15065
15066
15067
15068
15069
15070
15071
15072
15073
15074
15075
15076
15077
15078
15079
15080
15081
15082
15083
15084
15085
15086
15087
15088
15089
15090
15091
15092
15093
15094
15095
15096
15097
15098
15099
15100
15101
15102
15103
15104
15105
15106
15107
15108
15109
15110
15111
15112
15113
15114
15115
15116
15117
15118
15119
15120
15121
15122
15123
15124
15125
15126
15127
15128
15129
15130
15131
15132
15133
15134
15135
15136
15137
15138
15139
15140
15141
15142
15143
15144
15145
15146
15147
15148
15149
15150
15151
15152
15153
15154
15155
15156
15157
15158
15159
15160
15161
15162
15163
15164
15165
15166
15167
15168
15169
15170
15171
15172
15173
15174
15175
15176
15177
15178
15179
15180
15181
15182
15183
15184
15185
15186
15187
15188
15189
15190
15191
15192
15193
15194
15195
15196
15197
15198
15199
15200
15201
15202
15203
15204
15205
15206
15207
15208
15209
15210
15211
15212
15213
15214
15215
15216
15217
15218
15219
15220
15221
15222
15223
15224
15225
15226
15227
15228
15229
15230
15231
15232
15233
15234
15235
15236
15237
15238
15239
15240
15241
15242
15243
15244
15245
15246
15247
15248
15249
15250
15251
15252
15253
15254
15255
15256
15257
15258
15259
15260
15261
15262
15263
15264
15265
15266
15267
15268
15269
15270
15271
15272
15273
15274
15275
15276
15277
15278
15279
15280
15281
15282
15283
15284
15285
15286
15287
15288
15289
15290
15291
15292
15293
15294
15295
15296
15297
15298
15299
15300
15301
15302
15303
15304
15305
15306
15307
15308
15309
15310
15311
15312
15313
15314
15315
15316
15317
15318
15319
15320
15321
15322
15323
15324
15325
15326
15327
15328
15329
15330
15331
15332
15333
15334
15335
15336
15337
15338
15339
15340
15341
15342
15343
15344
15345
15346
15347
15348
15349
15350
15351
15352
15353
15354
15355
15356
15357
15358
15359
15360
15361
15362
15363
15364
15365
15366
15367
15368
15369
15370
15371
15372
15373
15374
15375
15376
15377
15378
15379
15380
15381
15382
15383
15384
15385
15386
15387
15388
15389
15390
15391
15392
15393
15394
15395
15396
15397
15398
15399
15400
15401
15402
15403
15404
15405
15406
15407
15408
15409
15410
15411
15412
15413
15414
15415
15416
15417
15418
15419
15420
15421
15422
15423
15424
15425
15426
15427
15428
15429
15430
15431
15432
15433
15434
15435
15436
15437
15438
15439
15440
15441
15442
15443
15444
15445
15446
15447
15448
15449
15450
15451
15452
15453
15454
15455
15456
15457
15458
15459
15460
15461
15462
15463
15464
15465
15466
15467
15468
15469
15470
15471
15472
15473
15474
15475
15476
15477
15478
15479
15480
15481
15482
15483
15484
15485
15486
15487
15488
15489
15490
15491
15492
15493
15494
15495
15496
15497
15498
15499
15500
15501
15502
15503
15504
15505
15506
15507
15508
15509
15510
15511
15512
15513
15514
15515
15516
15517
15518
15519
15520
15521
15522
15523
15524
15525
15526
15527
15528
15529
15530
15531
15532
15533
15534
15535
15536
15537
15538
15539
15540
15541
15542
15543
15544
15545
15546
15547
15548
15549
15550
15551
15552
15553
15554
15555
15556
15557
15558
15559
15560
15561
15562
15563
15564
15565
15566
15567
15568
15569
15570
15571
15572
15573
15574
15575
15576
15577
15578
15579
15580
15581
15582
15583
15584
15585
15586
15587
15588
15589
15590
15591
15592
15593
15594
15595
15596
15597
15598
15599
15600
15601
15602
15603
15604
15605
15606
15607
15608
15609
15610
15611
15612
15613
15614
15615
15616
15617
15618
15619
15620
15621
15622
15623
15624
15625
15626
15627
15628
15629
15630
15631
15632
15633
15634
15635
15636
15637
15638
15639
15640
15641
15642
15643
15644
15645
15646
15647
15648
15649
15650
15651
15652
15653
15654
15655
15656
15657
15658
15659
15660
15661
15662
15663
15664
15665
15666
15667
15668
15669
15670
15671
15672
15673
15674
15675
15676
15677
15678
15679
15680
15681
15682
15683
15684
15685
15686
15687
15688
15689
15690
15691
15692
15693
15694
15695
15696
15697
15698
15699
15700
15701
15702
15703
15704
15705
15706
15707
15708
15709
15710
15711
15712
15713
15714
15715
15716
15717
15718
15719
15720
15721
15722
15723
15724
15725
15726
15727
15728
15729
15730
15731
15732
15733
15734
15735
15736
15737
15738
15739
15740
15741
15742
15743
15744
15745
15746
15747
15748
15749
15750
15751
15752
15753
15754
15755
15756
15757
15758
15759
15760
15761
15762
15763
15764
15765
15766
15767
15768
15769
15770
15771
15772
15773
15774
15775
15776
15777
15778
15779
15780
15781
15782
15783
15784
15785
15786
15787
15788
15789
15790
15791
15792
15793
15794
15795
15796
15797
15798
15799
15800
15801
15802
15803
15804
15805
15806
15807
15808
15809
15810
15811
15812
15813
15814
15815
15816
15817
15818
15819
15820
15821
15822
15823
15824
15825
15826
15827
15828
15829
15830
15831
15832
15833
15834
15835
15836
15837
15838
15839
15840
15841
15842
15843
15844
15845
15846
15847
15848
15849
15850
15851
15852
15853
15854
15855
15856
15857
15858
15859
15860
15861
15862
15863
15864
15865
15866
15867
15868
15869
15870
15871
15872
15873
15874
15875
15876
15877
15878
15879
15880
15881
15882
15883
15884
15885
15886
15887
15888
15889
15890
15891
15892
15893
15894
15895
15896
15897
15898
15899
15900
15901
15902
15903
15904
15905
15906
15907
15908
15909
15910
15911
15912
15913
15914
15915
15916
15917
15918
15919
15920
15921
15922
15923
15924
15925
15926
15927
15928
15929
15930
15931
15932
15933
15934
15935
15936
15937
15938
15939
15940
15941
15942
15943
15944
15945
15946
15947
15948
15949
15950
15951
15952
15953
15954
15955
15956
15957
15958
15959
15960
15961
15962
15963
15964
15965
15966
15967
15968
15969
15970
15971
15972
15973
15974
15975
15976
15977
15978
15979
15980
15981
15982
15983
15984
15985
15986
15987
15988
15989
15990
15991
15992
15993
15994
15995
15996
15997
15998
15999
16000
16001
16002
16003
16004
16005
16006
16007
16008
16009
16010
16011
16012
16013
16014
16015
16016
16017
16018
16019
16020
16021
16022
16023
16024
16025
16026
16027
16028
16029
16030
16031
16032
16033
16034
16035
16036
16037
16038
16039
16040
16041
16042
16043
16044
16045
16046
16047
16048
16049
16050
16051
16052
16053
16054
16055
16056
16057
16058
16059
16060
16061
16062
16063
16064
16065
16066
16067
16068
16069
16070
16071
16072
16073
16074
16075
16076
16077
16078
16079
16080
16081
16082
16083
16084
16085
16086
16087
16088
16089
16090
16091
16092
16093
16094
16095
16096
16097
16098
16099
16100
16101
16102
16103
16104
16105
16106
16107
16108
16109
16110
16111
16112
16113
16114
16115
16116
16117
16118
16119
16120
16121
16122
16123
16124
16125
16126
16127
16128
16129
16130
16131
16132
16133
16134
16135
16136
16137
16138
16139
16140
16141
16142
16143
16144
16145
16146
16147
16148
16149
16150
16151
16152
16153
16154
16155
16156
16157
16158
16159
16160
16161
16162
16163
16164
16165
16166
16167
16168
16169
16170
16171
16172
16173
16174
16175
16176
16177
16178
16179
16180
16181
16182
16183
16184
16185
16186
16187
16188
16189
16190
16191
16192
16193
16194
16195
16196
16197
16198
16199
16200
16201
16202
16203
16204
16205
16206
16207
16208
16209
16210
16211
16212
16213
16214
16215
16216
16217
16218
16219
16220
16221
16222
16223
16224
16225
16226
16227
16228
16229
16230
16231
16232
16233
16234
16235
16236
16237
16238
16239
16240
16241
16242
16243
16244
16245
16246
16247
16248
16249
16250
16251
16252
16253
16254
16255
16256
16257
16258
16259
16260
16261
16262
16263
16264
16265
16266
16267
16268
16269
16270
16271
16272
16273
16274
16275
16276
16277
16278
16279
16280
16281
16282
16283
16284
16285
16286
16287
16288
16289
16290
16291
16292
16293
16294
16295
16296
16297
16298
16299
16300
16301
16302
16303
16304
16305
16306
16307
16308
16309
16310
16311
16312
16313
16314
16315
16316
16317
16318
16319
16320
16321
16322
16323
16324
16325
16326
16327
16328
16329
16330
16331
16332
16333
16334
16335
16336
16337
16338
16339
16340
16341
16342
16343
16344
16345
16346
16347
16348
16349
16350
16351
16352
16353
16354
16355
16356
16357
16358
16359
16360
16361
16362
16363
16364
16365
16366
16367
16368
16369
16370
16371
16372
16373
16374
16375
16376
16377
16378
16379
16380
16381
16382
16383
16384
16385
16386
16387
16388
16389
16390
16391
16392
16393
16394
16395
16396
16397
16398
16399
16400
16401
16402
16403
16404
16405
16406
16407
16408
16409
16410
16411
16412
16413
16414
16415
16416
16417
16418
16419
16420
16421
16422
16423
16424
16425
16426
16427
16428
16429
16430
16431
16432
16433
16434
16435
16436
16437
16438
16439
16440
16441
16442
16443
16444
16445
16446
16447
16448
16449
16450
16451
16452
16453
16454
16455
16456
16457
16458
16459
16460
16461
16462
16463
16464
16465
16466
16467
16468
16469
16470
16471
16472
16473
16474
16475
16476
16477
16478
16479
16480
16481
16482
16483
16484
16485
16486
16487
16488
16489
16490
16491
16492
16493
16494
16495
16496
16497
16498
16499
16500
16501
16502
16503
16504
16505
16506
16507
16508
16509
16510
16511
16512
16513
16514
16515
16516
16517
16518
16519
16520
16521
16522
16523
16524
16525
16526
16527
16528
16529
16530
16531
16532
16533
16534
16535
16536
16537
16538
16539
16540
16541
16542
16543
16544
16545
16546
16547
16548
16549
16550
16551
16552
16553
16554
16555
16556
16557
16558
16559
16560
16561
16562
16563
16564
16565
16566
16567
16568
16569
16570
16571
16572
16573
16574
16575
16576
16577
16578
16579
16580
16581
16582
16583
16584
16585
16586
16587
16588
16589
16590
16591
16592
16593
16594
16595
16596
16597
16598
16599
16600
16601
16602
16603
16604
16605
16606
16607
16608
16609
16610
16611
16612
16613
16614
16615
16616
16617
16618
16619
16620
16621
16622
16623
16624
16625
16626
16627
16628
16629
16630
16631
16632
16633
16634
16635
16636
16637
16638
16639
16640
16641
16642
16643
16644
16645
16646
16647
16648
16649
16650
16651
16652
16653
16654
16655
16656
16657
16658
16659
16660
16661
16662
16663
16664
16665
16666
16667
16668
16669
16670
16671
16672
16673
16674
16675
16676
16677
16678
16679
16680
16681
16682
16683
16684
16685
16686
16687
16688
16689
16690
16691
16692
16693
16694
16695
16696
16697
16698
16699
16700
16701
16702
16703
16704
16705
16706
16707
16708
16709
16710
16711
16712
16713
16714
16715
16716
16717
16718
16719
16720
16721
16722
16723
16724
16725
16726
16727
16728
16729
16730
16731
16732
16733
16734
16735
16736
16737
16738
16739
16740
16741
16742
16743
16744
16745
16746
16747
16748
16749
16750
16751
16752
16753
16754
16755
16756
16757
16758
16759
16760
16761
16762
16763
16764
16765
16766
16767
16768
16769
16770
16771
16772
16773
16774
16775
16776
16777
16778
16779
16780
16781
16782
16783
16784
16785
16786
16787
16788
16789
16790
16791
16792
16793
16794
16795
16796
16797
16798
16799
16800
16801
16802
16803
16804
16805
16806
16807
16808
16809
16810
16811
16812
16813
16814
16815
16816
16817
16818
16819
16820
16821
16822
16823
16824
16825
16826
16827
16828
16829
16830
16831
16832
16833
16834
16835
16836
16837
16838
16839
16840
16841
16842
16843
16844
16845
16846
16847
16848
16849
16850
16851
16852
16853
16854
16855
16856
16857
16858
16859
16860
16861
16862
16863
16864
16865
16866
16867
16868
16869
16870
16871
16872
16873
16874
16875
16876
16877
16878
16879
16880
16881
16882
16883
16884
16885
16886
16887
16888
16889
16890
16891
16892
16893
16894
16895
16896
16897
16898
16899
16900
16901
16902
16903
16904
16905
16906
16907
16908
16909
16910
16911
16912
16913
16914
16915
16916
16917
16918
16919
16920
16921
16922
16923
16924
16925
16926
16927
16928
16929
16930
16931
16932
16933
16934
16935
16936
16937
16938
16939
16940
16941
16942
16943
16944
16945
16946
16947
16948
16949
16950
16951
16952
16953
16954
16955
16956
16957
16958
16959
16960
16961
16962
16963
16964
16965
16966
16967
16968
16969
16970
16971
16972
16973
16974
16975
16976
16977
16978
16979
16980
16981
16982
16983
16984
16985
16986
16987
16988
16989
16990
16991
16992
16993
16994
16995
16996
16997
16998
16999
17000
17001
17002
17003
17004
17005
17006
17007
17008
17009
17010
17011
17012
17013
17014
17015
17016
17017
17018
17019
17020
17021
17022
17023
17024
17025
17026
17027
17028
17029
17030
17031
17032
17033
17034
17035
17036
17037
17038
17039
17040
17041
17042
17043
17044
17045
17046
17047
17048
17049
17050
17051
17052
17053
17054
17055
17056
17057
17058
17059
17060
17061
17062
17063
17064
17065
17066
17067
17068
17069
17070
17071
17072
17073
17074
17075
17076
17077
17078
17079
17080
17081
17082
17083
17084
17085
17086
17087
17088
17089
17090
17091
17092
17093
17094
17095
17096
17097
17098
17099
17100
17101
17102
17103
17104
17105
17106
17107
17108
17109
17110
17111
17112
17113
17114
17115
17116
17117
17118
17119
17120
17121
17122
17123
17124
17125
17126
17127
17128
17129
17130
17131
17132
17133
17134
17135
17136
17137
17138
17139
17140
17141
17142
17143
17144
17145
17146
17147
17148
17149
17150
17151
17152
17153
17154
17155
17156
17157
17158
17159
17160
17161
17162
17163
17164
17165
17166
17167
17168
17169
17170
17171
17172
17173
17174
17175
17176
17177
17178
17179
17180
17181
17182
17183
17184
17185
17186
17187
17188
17189
17190
17191
17192
17193
17194
17195
17196
17197
17198
17199
17200
17201
17202
17203
17204
17205
17206
17207
17208
17209
17210
17211
17212
17213
17214
17215
17216
17217
17218
17219
17220
17221
17222
17223
17224
17225
17226
17227
17228
17229
17230
17231
17232
17233
17234
17235
17236
17237
17238
17239
17240
17241
17242
17243
17244
17245
17246
17247
17248
17249
17250
17251
17252
17253
17254
17255
17256
17257
17258
17259
17260
17261
17262
17263
17264
17265
17266
17267
17268
17269
17270
17271
17272
17273
17274
17275
17276
17277
17278
17279
17280
17281
17282
17283
17284
17285
17286
17287
17288
17289
17290
17291
17292
17293
17294
17295
17296
17297
17298
17299
17300
17301
17302
17303
17304
17305
17306
17307
17308
17309
17310
17311
17312
17313
17314
17315
17316
17317
17318
17319
17320
17321
17322
17323
17324
17325
17326
17327
17328
17329
17330
17331
17332
17333
17334
17335
17336
17337
17338
17339
17340
17341
17342
17343
17344
17345
17346
17347
17348
17349
17350
17351
17352
17353
17354
17355
17356
17357
17358
17359
17360
17361
17362
17363
17364
17365
17366
17367
17368
17369
17370
17371
17372
17373
17374
17375
17376
17377
17378
17379
17380
17381
17382
17383
17384
17385
17386
17387
17388
17389
17390
17391
17392
17393
17394
17395
17396
17397
17398
17399
17400
17401
17402
17403
17404
17405
17406
17407
17408
17409
17410
17411
17412
17413
17414
17415
17416
17417
17418
17419
17420
17421
17422
17423
17424
17425
17426
17427
17428
17429
17430
17431
17432
17433
17434
17435
17436
17437
17438
17439
17440
17441
17442
17443
17444
17445
17446
17447
17448
17449
17450
17451
17452
17453
17454
17455
17456
17457
17458
17459
17460
17461
17462
17463
17464
17465
17466
17467
17468
17469
17470
17471
17472
17473
17474
17475
17476
17477
17478
17479
17480
17481
17482
17483
17484
17485
17486
17487
17488
17489
17490
17491
17492
17493
17494
17495
17496
17497
17498
17499
17500
17501
17502
17503
17504
17505
17506
17507
17508
17509
17510
17511
17512
17513
17514
17515
17516
17517
17518
17519
17520
17521
17522
17523
17524
17525
17526
17527
17528
17529
17530
17531
17532
17533
17534
17535
17536
17537
17538
17539
17540
17541
17542
17543
17544
17545
17546
17547
17548
17549
17550
17551
17552
17553
17554
17555
17556
17557
17558
17559
17560
17561
17562
17563
17564
17565
17566
17567
17568
17569
17570
17571
17572
17573
17574
17575
17576
17577
17578
17579
17580
17581
17582
17583
17584
17585
17586
17587
17588
17589
17590
17591
17592
17593
17594
17595
17596
17597
17598
17599
17600
17601
17602
17603
17604
17605
17606
17607
17608
17609
17610
17611
17612
17613
17614
17615
17616
17617
17618
17619
17620
17621
17622
17623
17624
17625
17626
17627
17628
17629
17630
17631
17632
17633
17634
17635
17636
17637
17638
17639
17640
17641
17642
17643
17644
17645
17646
17647
17648
17649
17650
17651
17652
17653
17654
17655
17656
17657
17658
17659
17660
17661
17662
17663
17664
17665
17666
17667
17668
17669
17670
17671
17672
17673
17674
17675
17676
17677
17678
17679
17680
17681
17682
17683
17684
17685
17686
17687
17688
17689
17690
17691
17692
17693
17694
17695
17696
17697
17698
17699
17700
17701
17702
17703
17704
17705
17706
17707
17708
17709
17710
17711
17712
17713
17714
17715
17716
17717
17718
17719
17720
17721
17722
17723
17724
17725
17726
17727
17728
17729
17730
17731
17732
17733
17734
17735
17736
17737
17738
17739
17740
17741
17742
17743
17744
17745
17746
17747
17748
17749
17750
17751
17752
17753
17754
17755
17756
17757
17758
17759
17760
17761
17762
17763
17764
17765
17766
17767
17768
17769
17770
17771
17772
17773
17774
17775
17776
17777
17778
17779
17780
17781
17782
17783
17784
17785
17786
17787
17788
17789
17790
17791
17792
17793
17794
17795
17796
17797
17798
17799
17800
17801
17802
17803
17804
17805
17806
17807
17808
17809
17810
17811
17812
17813
17814
17815
17816
17817
17818
17819
17820
17821
17822
17823
17824
17825
17826
17827
17828
17829
17830
17831
17832
17833
17834
17835
17836
17837
17838
17839
17840
17841
17842
17843
17844
17845
17846
17847
17848
17849
17850
17851
17852
17853
17854
17855
17856
17857
17858
17859
17860
17861
17862
17863
17864
17865
17866
17867
17868
17869
17870
17871
17872
17873
17874
17875
17876
17877
17878
17879
17880
17881
17882
17883
17884
17885
17886
17887
17888
17889
17890
17891
17892
17893
17894
17895
17896
17897
17898
17899
17900
17901
17902
17903
17904
17905
17906
17907
17908
17909
17910
17911
17912
17913
17914
17915
17916
17917
17918
17919
17920
17921
17922
17923
17924
17925
17926
17927
17928
17929
17930
17931
17932
17933
17934
17935
17936
17937
17938
17939
17940
17941
17942
17943
17944
17945
17946
17947
17948
17949
17950
17951
17952
17953
17954
17955
17956
17957
17958
17959
17960
17961
17962
17963
17964
17965
17966
17967
17968
17969
17970
17971
17972
17973
17974
17975
17976
17977
17978
17979
17980
17981
17982
17983
17984
17985
17986
17987
17988
17989
17990
17991
17992
17993
17994
17995
17996
17997
17998
17999
18000
18001
18002
18003
18004
18005
18006
18007
18008
18009
18010
18011
18012
18013
18014
18015
18016
18017
18018
18019
18020
18021
18022
18023
18024
18025
18026
18027
18028
18029
18030
18031
18032
18033
18034
18035
18036
18037
18038
18039
18040
18041
18042
18043
18044
18045
18046
18047
18048
18049
18050
18051
18052
18053
18054
18055
18056
18057
18058
18059
18060
18061
18062
18063
18064
18065
18066
18067
18068
18069
18070
18071
18072
18073
18074
18075
18076
18077
18078
18079
18080
18081
18082
18083
18084
18085
18086
18087
18088
18089
18090
18091
18092
18093
18094
18095
18096
18097
18098
18099
18100
18101
18102
18103
18104
18105
18106
18107
18108
18109
18110
18111
18112
18113
18114
18115
18116
18117
18118
18119
18120
18121
18122
18123
18124
18125
18126
18127
18128
18129
18130
18131
18132
18133
18134
18135
18136
18137
18138
18139
18140
18141
18142
18143
18144
18145
18146
18147
18148
18149
18150
18151
18152
18153
18154
18155
18156
18157
18158
18159
18160
18161
18162
18163
18164
18165
18166
18167
18168
18169
18170
18171
18172
18173
18174
18175
18176
18177
18178
18179
18180
18181
18182
18183
18184
18185
18186
18187
18188
18189
18190
18191
18192
18193
18194
18195
18196
18197
18198
18199
18200
18201
18202
18203
18204
18205
18206
18207
18208
18209
18210
18211
18212
18213
18214
18215
18216
18217
18218
18219
18220
18221
18222
18223
18224
18225
18226
18227
18228
18229
18230
18231
18232
18233
18234
18235
18236
18237
18238
18239
18240
18241
18242
18243
18244
18245
18246
18247
18248
18249
18250
18251
18252
18253
18254
18255
18256
18257
18258
18259
18260
18261
18262
18263
18264
18265
18266
18267
18268
18269
18270
18271
18272
18273
18274
18275
18276
18277
18278
18279
18280
18281
18282
18283
18284
18285
18286
18287
18288
18289
18290
18291
18292
18293
18294
18295
18296
18297
18298
18299
18300
18301
18302
18303
18304
18305
18306
18307
18308
18309
18310
18311
18312
18313
18314
18315
18316
18317
18318
18319
18320
18321
18322
18323
18324
18325
18326
18327
18328
18329
18330
18331
18332
18333
18334
18335
18336
18337
18338
18339
18340
18341
18342
18343
18344
18345
18346
18347
18348
18349
18350
18351
18352
18353
18354
18355
18356
18357
18358
18359
18360
18361
18362
18363
18364
18365
18366
18367
18368
18369
18370
18371
18372
18373
18374
18375
18376
18377
18378
18379
18380
18381
18382
18383
18384
18385
18386
18387
18388
18389
18390
18391
18392
18393
18394
18395
18396
18397
18398
18399
18400
18401
18402
18403
18404
18405
18406
18407
18408
18409
18410
18411
18412
18413
18414
18415
18416
18417
18418
18419
18420
18421
18422
18423
18424
18425
18426
18427
18428
18429
18430
18431
18432
18433
18434
18435
18436
18437
18438
18439
18440
18441
18442
18443
18444
18445
18446
18447
18448
18449
18450
18451
18452
18453
18454
18455
18456
18457
18458
18459
18460
18461
18462
18463
18464
18465
18466
18467
18468
18469
18470
18471
18472
18473
18474
18475
18476
18477
18478
18479
18480
18481
18482
18483
18484
18485
18486
18487
18488
18489
18490
18491
18492
18493
18494
18495
18496
18497
18498
18499
18500
18501
18502
18503
18504
18505
18506
18507
18508
18509
18510
18511
18512
18513
18514
18515
18516
18517
18518
18519
18520
18521
18522
18523
18524
18525
18526
18527
18528
18529
18530
18531
18532
18533
18534
18535
18536
18537
18538
18539
18540
18541
18542
18543
18544
18545
18546
18547
18548
18549
18550
18551
18552
18553
18554
18555
18556
18557
18558
18559
18560
18561
18562
18563
18564
18565
18566
18567
18568
18569
18570
18571
18572
18573
18574
18575
18576
18577
18578
18579
18580
18581
18582
18583
18584
18585
18586
18587
18588
18589
18590
18591
18592
18593
18594
18595
18596
18597
18598
18599
18600
18601
18602
18603
18604
18605
18606
18607
18608
18609
18610
18611
18612
18613
18614
18615
18616
18617
18618
18619
18620
18621
18622
18623
18624
18625
18626
18627
18628
18629
18630
18631
18632
18633
18634
18635
18636
18637
18638
18639
18640
18641
18642
18643
18644
18645
18646
18647
18648
18649
18650
18651
18652
18653
18654
18655
18656
18657
18658
18659
18660
18661
18662
18663
18664
18665
18666
18667
18668
18669
18670
18671
18672
18673
18674
18675
18676
18677
18678
18679
18680
18681
18682
18683
18684
18685
18686
18687
18688
18689
18690
18691
18692
18693
18694
18695
18696
18697
18698
18699
18700
18701
18702
18703
18704
18705
18706
18707
18708
18709
18710
18711
18712
18713
18714
18715
18716
18717
18718
18719
18720
18721
18722
18723
18724
18725
18726
18727
18728
18729
18730
18731
18732
18733
18734
18735
18736
18737
18738
18739
18740
18741
18742
18743
18744
18745
18746
18747
18748
18749
18750
18751
18752
18753
18754
18755
18756
18757
18758
18759
18760
18761
18762
18763
18764
18765
18766
18767
18768
18769
18770
18771
18772
18773
18774
18775
18776
18777
18778
18779
18780
18781
18782
18783
18784
18785
18786
18787
18788
18789
18790
18791
18792
18793
18794
18795
18796
18797
18798
18799
18800
18801
18802
18803
18804
18805
18806
18807
18808
18809
18810
18811
18812
18813
18814
18815
18816
18817
18818
18819
18820
18821
18822
18823
18824
18825
18826
18827
18828
18829
18830
18831
18832
18833
18834
18835
18836
18837
18838
18839
18840
18841
18842
18843
18844
18845
18846
18847
18848
18849
18850
18851
18852
18853
18854
18855
18856
18857
18858
18859
18860
18861
18862
18863
18864
18865
18866
18867
18868
18869
18870
18871
18872
18873
18874
18875
18876
18877
18878
18879
18880
18881
18882
18883
18884
18885
18886
18887
18888
18889
18890
18891
18892
18893
18894
18895
18896
18897
18898
18899
18900
18901
18902
18903
18904
18905
18906
18907
18908
18909
18910
18911
18912
18913
18914
18915
18916
18917
18918
18919
18920
18921
18922
18923
18924
18925
18926
18927
18928
18929
18930
18931
18932
18933
18934
18935
18936
18937
18938
18939
18940
18941
18942
18943
18944
18945
18946
18947
18948
18949
18950
18951
18952
18953
18954
18955
18956
18957
18958
18959
18960
18961
18962
18963
18964
18965
18966
18967
18968
18969
18970
18971
18972
18973
18974
18975
18976
18977
18978
18979
18980
18981
18982
18983
18984
18985
18986
18987
18988
18989
18990
18991
18992
18993
18994
18995
18996
18997
18998
18999
19000
19001
19002
19003
19004
19005
19006
19007
19008
19009
19010
19011
19012
19013
19014
19015
19016
19017
19018
19019
19020
19021
19022
19023
19024
19025
19026
19027
19028
19029
19030
19031
19032
19033
19034
19035
19036
19037
19038
19039
19040
19041
19042
19043
19044
19045
19046
19047
19048
19049
19050
19051
19052
19053
19054
19055
19056
19057
19058
19059
19060
19061
19062
19063
19064
19065
19066
19067
19068
19069
19070
19071
19072
19073
19074
19075
19076
19077
19078
19079
19080
19081
19082
19083
19084
19085
19086
19087
19088
19089
19090
19091
19092
19093
19094
19095
19096
19097
19098
19099
19100
19101
19102
19103
19104
19105
19106
19107
19108
19109
19110
19111
19112
19113
19114
19115
19116
19117
19118
19119
19120
19121
19122
19123
19124
19125
19126
19127
19128
19129
19130
19131
19132
19133
19134
19135
19136
19137
19138
19139
19140
19141
19142
19143
19144
19145
19146
19147
19148
19149
19150
19151
19152
19153
19154
19155
19156
19157
19158
19159
19160
19161
19162
19163
19164
19165
19166
19167
19168
19169
19170
19171
19172
19173
19174
19175
19176
19177
19178
19179
19180
19181
19182
19183
19184
19185
|
#
# 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 2007 Sun Microsystems, Inc. All rights reserved.
# Use is subject to license terms.
#
include ../../../Makefile.master
include ../../Makefile.lib
# Hammerhead: amd64-only
SUBDIRS= $(MACH64)
all : TARGET= all
check : TARGET= check
clean : TARGET= clean
clobber : TARGET= clobber
install : TARGET= install
.KEEP_STATE:
all clean clobber install: $(SUBDIRS)
$(SUBDIRS): FRC
@cd $@; pwd; $(MAKE) $(TARGET)
FRC:
include ../../Makefile.targ
#
# 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.
#
# Copyright (c) 2018, Joyent, Inc.
LIBRARY= libpkcs11.a
VERS= .1
OBJECTS= \
metaAttrManager.o \
metaCrypt.o \
metaDigest.o \
metaDualCrypt.o \
metaGeneral.o \
metaKeys.o \
metaMechManager.o \
metaObject.o \
metaObjectManager.o \
metaRand.o \
metaSession.o \
metaSessionManager.o \
metaSign.o \
metaSlotManager.o \
metaSlotToken.o \
metaUtil.o \
metaVerify.o \
pkcs11General.o \
pkcs11SlotToken.o \
pkcs11Session.o \
pkcs11Object.o \
pkcs11Crypt.o \
pkcs11Digest.o \
pkcs11Sign.o \
pkcs11Verify.o \
pkcs11DualCrypt.o \
pkcs11Keys.o \
pkcs11Rand.o \
pkcs11Slottable.o \
pkcs11Conf.o \
pkcs11Sessionlist.o \
pkcs11SUNWExtensions.o
include ../../../Makefile.lib
SRCDIR= ../common
INCDIR= ../../include
LIBS = $(DYNLIB)
# Hammerhead: Add -ldl for dlopen/dlclose/dlsym (GNU ld requires explicit linkage)
LDLIBS += -lcryptoutil -ldl -lc
CFLAGS += $(CCVERBOSE)
CPPFLAGS += -I$(INCDIR) -I$(SRCDIR) -D_REENTRANT
CERRWARN += $(CNOWARN_UNINIT)
# not linted
SMATCH=off
.KEEP_STATE:
all: $(LIBS)
include $(SRC)/lib/Makefile.targ
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License, Version 1.0 only
# (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 2004 Sun Microsystems, Inc. All rights reserved.
# Use is subject to license terms.
#
# ident "%Z%%M% %I% %E% SMI"
#
include ../Makefile.com
include ../../../Makefile.lib.64
install: all $(ROOTLIBS64) $(ROOTLINKS64)
#
# 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 (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
#
#
# MAPFILE HEADER START
#
# WARNING: STOP NOW. DO NOT MODIFY THIS FILE.
# Object versioning must comply with the rules detailed in
#
# usr/src/lib/README.mapfiles
#
# You should not be making modifications here until you've read the most current
# copy of that file. If you need help, contact a gatekeeper for guidance.
#
# MAPFILE HEADER END
#
$mapfile_version 2
SYMBOL_VERSION SUNW_1.1 {
global:
C_CancelFunction;
C_CloseAllSessions;
C_CloseSession;
C_CopyObject;
C_CreateObject;
C_Decrypt;
C_DecryptDigestUpdate;
C_DecryptFinal;
C_DecryptInit;
C_DecryptUpdate;
C_DecryptVerifyUpdate;
C_DeriveKey;
C_DestroyObject;
C_Digest;
C_DigestEncryptUpdate;
C_DigestFinal;
C_DigestInit;
C_DigestKey;
C_DigestUpdate;
C_Encrypt;
C_EncryptFinal;
C_EncryptInit;
C_EncryptUpdate;
C_Finalize;
C_FindObjects;
C_FindObjectsFinal;
C_FindObjectsInit;
C_GenerateKey;
C_GenerateKeyPair;
C_GenerateRandom;
C_GetAttributeValue;
C_GetFunctionList;
C_GetFunctionStatus;
C_GetInfo;
C_GetMechanismInfo;
C_GetMechanismList;
C_GetObjectSize;
C_GetOperationState;
C_GetSessionInfo;
C_GetSlotInfo;
C_GetSlotList;
C_GetTokenInfo;
C_Initialize;
C_InitPIN;
C_InitToken;
C_Login;
C_Logout;
C_OpenSession;
C_SeedRandom;
C_SetAttributeValue;
C_SetOperationState;
C_SetPIN;
C_Sign;
C_SignEncryptUpdate;
C_SignFinal;
C_SignInit;
C_SignRecover;
C_SignRecoverInit;
C_SignUpdate;
C_UnwrapKey;
C_Verify;
C_VerifyFinal;
C_VerifyInit;
C_VerifyRecover;
C_VerifyRecoverInit;
C_VerifyUpdate;
C_WaitForSlotEvent;
C_WrapKey;
SUNW_C_GetMechSession;
SUNW_C_KeyToObject;
local:
*;
};
SYMBOL_VERSION SUNWprivate {
global:
pkcs11_GetCriteriaSession;
pkcs11_ObjectToKey;
pkcs11_PasswdToPBKD2Object;
pkcs11_PasswdToKey;
local:
*;
};
/*
* 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 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
* Copyright 2012 Milan Jurik. All rights reserved.
* Copyright (c) 2018, Joyent, Inc.
*/
#include <string.h>
#include <stdlib.h>
#include <strings.h>
#include "metaGlobal.h"
#include "metaAttrMasters.h"
static void
find_attribute(CK_ATTRIBUTE_TYPE attrtype, generic_attr_t *attributes,
size_t num_attributes, generic_attr_t **found_attribute);
/*
* get_master_attributes_by_object
*
* Returns an (statically allocated) set of object attributes, as determined by
* class and keytype of the supplied object. The attributes are only
* initialized to default values.
*/
CK_RV
get_master_attributes_by_object(slot_session_t *session,
slot_object_t *slot_object, generic_attr_t **attributes,
size_t *num_attributes)
{
CK_RV rv;
CK_ATTRIBUTE attr;
CK_OBJECT_CLASS class;
CK_ULONG subtype = CK_UNAVAILABLE_INFORMATION;
/* first get the class */
attr.type = CKA_CLASS;
attr.pValue = &class;
attr.ulValueLen = sizeof (class);
rv = FUNCLIST(session->fw_st_id)->C_GetAttributeValue(
session->hSession, slot_object->hObject, &attr, 1);
if (rv != CKR_OK) {
return (rv);
}
attr.pValue = &subtype;
attr.ulValueLen = sizeof (subtype);
switch (class) {
case CKO_CERTIFICATE:
attr.type = CKA_CERTIFICATE_TYPE;
break;
case CKO_HW_FEATURE:
attr.type = CKA_HW_FEATURE_TYPE;
break;
case CKO_PUBLIC_KEY:
case CKO_PRIVATE_KEY:
case CKO_SECRET_KEY:
case CKO_DOMAIN_PARAMETERS:
attr.type = CKA_KEY_TYPE;
break;
case CKO_DATA:
goto get_attr;
default:
/* should never be here */
return (CKR_ATTRIBUTE_VALUE_INVALID);
}
rv = FUNCLIST(session->fw_st_id)->C_GetAttributeValue(
session->hSession, slot_object->hObject, &attr, 1);
if (rv != CKR_OK) {
return (rv);
}
get_attr:
rv = get_master_attributes_by_type(class, subtype,
attributes, num_attributes);
return (rv);
}
/*
* get_master_attributes_by_template
*
* Returns an (statically allocated) set of object attributes, as determined by
* the supplied object template. The template is only used to determine the
* class/subclass of the object. The attributes are only initialized to
* default values.
*/
CK_RV
get_master_attributes_by_template(
CK_ATTRIBUTE *template, CK_ULONG template_size,
generic_attr_t **attributes, size_t *num_attributes)
{
CK_OBJECT_CLASS class;
CK_ULONG subtype = CK_UNAVAILABLE_INFORMATION;
boolean_t found;
found = get_template_ulong(CKA_CLASS, template, template_size, &class);
if (!found) {
return (CKR_TEMPLATE_INCOMPLETE);
}
switch (class) {
case CKO_CERTIFICATE:
found = get_template_ulong(CKA_CERTIFICATE_TYPE,
template, template_size, &subtype);
break;
case CKO_HW_FEATURE:
found = get_template_ulong(CKA_HW_FEATURE_TYPE,
template, template_size, &subtype);
break;
case CKO_PUBLIC_KEY:
case CKO_PRIVATE_KEY:
case CKO_SECRET_KEY:
case CKO_DOMAIN_PARAMETERS:
found = get_template_ulong(CKA_KEY_TYPE,
template, template_size, &subtype);
break;
case CKO_DATA:
/* CKO_DATA has no subtype, just pretend it is found */
found = B_TRUE;
break;
default:
/* unknown object class */
return (CKR_ATTRIBUTE_VALUE_INVALID);
}
if (!found) {
return (CKR_TEMPLATE_INCOMPLETE);
}
return (get_master_attributes_by_type(class, subtype,
attributes, num_attributes));
}
/*
* get_master_template_by_type
*
* Returns an (statically allocated) set of object attributes, as determined
* by the specified class and subtype. The attributes are initialized to default
* values.
*/
CK_RV
get_master_template_by_type(CK_OBJECT_CLASS class, CK_ULONG subtype,
generic_attr_t **attributes, size_t *num_attributes)
{
generic_attr_t *master_template = NULL;
size_t master_template_size = 0;
switch (class) {
case CKO_HW_FEATURE:
switch (subtype) {
case CKO_HW_FEATURE:
master_template = (generic_attr_t *)OBJ_HW_CLOCK;
master_template_size = sizeof (OBJ_HW_CLOCK);
break;
case CKH_MONOTONIC_COUNTER:
master_template = (generic_attr_t *)OBJ_HW_MONOTONIC;
master_template_size = sizeof (OBJ_HW_MONOTONIC);
break;
default:
/* Unsupported. */
break;
}
break;
case CKO_DATA:
/* Objects of this class have no subtype. */
master_template = (generic_attr_t *)OBJ_DATA;
master_template_size = sizeof (OBJ_DATA);
break;
case CKO_CERTIFICATE:
switch (subtype) {
case CKC_X_509:
master_template = (generic_attr_t *)OBJ_CERT_X509;
master_template_size = sizeof (OBJ_CERT_X509);
break;
case CKC_X_509_ATTR_CERT:
master_template = (generic_attr_t *)OBJ_CERT_X509ATTR;
master_template_size = sizeof (OBJ_CERT_X509ATTR);
break;
default:
/* Unsupported. */
break;
}
break;
case CKO_PUBLIC_KEY:
switch (subtype) {
case CKK_RSA:
master_template = (generic_attr_t *)OBJ_PUBKEY_RSA;
master_template_size = sizeof (OBJ_PUBKEY_RSA);
break;
case CKK_DSA:
master_template = (generic_attr_t *)OBJ_PUBKEY_DSA;
master_template_size = sizeof (OBJ_PUBKEY_DSA);
break;
case CKK_EC:
master_template = (generic_attr_t *)OBJ_PUBKEY_EC;
master_template_size = sizeof (OBJ_PUBKEY_EC);
break;
case CKK_DH:
master_template = (generic_attr_t *)OBJ_PUBKEY_DH;
master_template_size = sizeof (OBJ_PUBKEY_DH);
break;
case CKK_X9_42_DH:
master_template = (generic_attr_t *)OBJ_PUBKEY_X942DH;
master_template_size = sizeof (OBJ_PUBKEY_X942DH);
break;
case CKK_KEA:
master_template = (generic_attr_t *)OBJ_PUBKEY_KEA;
master_template_size = sizeof (OBJ_PUBKEY_KEA);
break;
default:
/* Unsupported. */
break;
}
break;
case CKO_PRIVATE_KEY:
switch (subtype) {
case CKK_RSA:
master_template = (generic_attr_t *)OBJ_PRIVKEY_RSA;
master_template_size = sizeof (OBJ_PRIVKEY_RSA);
break;
case CKK_DSA:
master_template = (generic_attr_t *)OBJ_PRIVKEY_DSA;
master_template_size = sizeof (OBJ_PRIVKEY_DSA);
break;
case CKK_EC:
master_template = (generic_attr_t *)OBJ_PRIVKEY_EC;
master_template_size = sizeof (OBJ_PRIVKEY_EC);
break;
case CKK_DH:
master_template = (generic_attr_t *)OBJ_PRIVKEY_DH;
master_template_size = sizeof (OBJ_PRIVKEY_DH);
break;
case CKK_X9_42_DH:
master_template = (generic_attr_t *)OBJ_PRIVKEY_X942DH;
master_template_size = sizeof (OBJ_PRIVKEY_X942DH);
break;
case CKK_KEA:
master_template = (generic_attr_t *)OBJ_PRIVKEY_KEA;
master_template_size = sizeof (OBJ_PRIVKEY_KEA);
break;
default:
/* Unsupported. */
break;
}
break;
case CKO_SECRET_KEY:
/*
* The only difference between secret keys is that some
* are valiable length (eg CKK_AES), while others are not
* (eg CKK_DES) -- and do not have a CKA_VALUE_LEN attribute.
*
* FUTURE(?): Consider using obj_seckey_withlen for unknown
* keytypes. This is the most likely choice, as new algorithms
* seem to support variable length keys. That's not the default
* now, because if people have implemented new key types with
* different attribute sets (like the mess of public/private
* key types), then incorrect behaviour would result. It's
* easier to relax this restriction than to tighten it (which
* would introduce a regression to anyone relying on this
* working for unknown key types).
*
*/
switch (subtype) {
case CKK_DES:
case CKK_DES2:
case CKK_DES3:
case CKK_IDEA:
case CKK_CDMF:
case CKK_SKIPJACK:
case CKK_BATON:
case CKK_JUNIPER:
master_template = (generic_attr_t *)OBJ_SECKEY;
master_template_size = sizeof (OBJ_SECKEY);
break;
case CKK_GENERIC_SECRET:
case CKK_RC2:
case CKK_RC4:
case CKK_RC5:
case CKK_AES:
case CKK_BLOWFISH:
case CKK_CAST:
case CKK_CAST3:
case CKK_CAST128:
master_template = (generic_attr_t *)OBJ_SECKEY_WITHLEN;
master_template_size = sizeof (OBJ_SECKEY_WITHLEN);
break;
default:
/* Unsupported. */
break;
}
break;
case CKO_DOMAIN_PARAMETERS:
switch (subtype) {
case CKK_DSA:
master_template = (generic_attr_t *)OBJ_DOM_DSA;
master_template_size = sizeof (OBJ_DOM_DSA);
break;
case CKK_DH:
master_template = (generic_attr_t *)OBJ_DOM_DH;
master_template_size = sizeof (OBJ_DOM_DH);
break;
case CKK_X9_42_DH:
master_template = (generic_attr_t *)OBJ_DOM_X942DH;
master_template_size = sizeof (OBJ_DOM_X942DH);
break;
default:
/* Unsupported. */
break;
}
break;
default:
/* Unsupported. */
break;
}
/* Requested object is unknown or invalid. */
if (master_template == NULL)
return (CKR_ATTRIBUTE_VALUE_INVALID);
else {
*attributes = master_template;
*num_attributes = master_template_size;
return (CKR_OK);
}
}
/*
* get_master_attributes_by_type
*
* Returns an (statically allocated) set of object attributes, as determined by
* the specified class and subtype. The attributes are initialized to default
* values.
*/
CK_RV
get_master_attributes_by_type(CK_OBJECT_CLASS class, CK_ULONG subtype,
generic_attr_t **attributes, size_t *num_attributes)
{
CK_RV rv;
generic_attr_t *master_template = NULL;
generic_attr_t *new_attributes;
size_t i, num_new_attributes, master_template_size = 0;
/* Determine the appropriate master template needed. */
rv = get_master_template_by_type(class, subtype,
&master_template, &master_template_size);
if (rv != CKR_OK)
return (rv);
/* Duplicate the master template. */
new_attributes = malloc(master_template_size);
if (new_attributes == NULL)
return (CKR_HOST_MEMORY);
(void) memcpy(new_attributes, master_template, master_template_size);
num_new_attributes = master_template_size / sizeof (generic_attr_t);
/* Set the pointer in the appropriate storage area. */
for (i = 0; i < num_new_attributes; i++) {
generic_attr_t *attr;
attr = new_attributes + i;
switch (attr->attribute.ulValueLen) {
case (sizeof (CK_ULONG)):
attr->attribute.pValue = &attr->generic_ulong;
break;
case (sizeof (CK_BBOOL)):
attr->attribute.pValue = &attr->generic_bbool;
break;
default:
attr->attribute.pValue = attr->generic_data;
break;
}
}
/* Secret keys share a common template, so set the key type here. */
if (class == CKO_SECRET_KEY) {
/* Keytype / subtype is always the second attribute. */
new_attributes[1].generic_ulong = subtype;
}
*attributes = new_attributes;
*num_attributes = num_new_attributes;
return (CKR_OK);
}
/*
* get_master_attributes_by_duplication
*
* Returns an (statically allocated) set of object attributes, as copied from an
* existing set of attributes. The new attributes inherit the values from
* the old attributes.
*/
CK_RV
get_master_attributes_by_duplication(
generic_attr_t *src_attrs, size_t num_src_attrs,
generic_attr_t **dst_attrs, size_t *num_dst_attrs)
{
CK_RV rv = CKR_OK;
generic_attr_t *new_attrs, *src, *dst;
size_t i;
new_attrs = malloc(sizeof (generic_attr_t) * num_src_attrs);
if (new_attrs == NULL)
return (CKR_HOST_MEMORY);
for (i = 0; i < num_src_attrs; i++) {
src = src_attrs + i;
dst = new_attrs + i;
*dst = *src;
/* Adjust pointers in dst so that they don't point to src. */
if (src->isMalloced) {
dst->attribute.pValue =
malloc(src->attribute.ulValueLen);
if (dst->attribute.pValue == NULL) {
/*
* Continue on error, so that the cleanup
* routine doesn't see pointers to src_attrs.
*/
dst->attribute.ulValueLen = 0;
rv = CKR_HOST_MEMORY;
continue;
}
} else if (src->attribute.pValue == &src->generic_bbool) {
dst->attribute.pValue = &dst->generic_bbool;
} else if (src->attribute.pValue == &src->generic_ulong) {
dst->attribute.pValue = &dst->generic_ulong;
} else if (src->attribute.pValue == &src->generic_data) {
dst->attribute.pValue = &dst->generic_data;
} else {
/* This shouldn't happen. */
dst->attribute.pValue = NULL;
dst->attribute.ulValueLen = 0;
rv = CKR_GENERAL_ERROR;
num_src_attrs = i + 1;
break;
}
(void) memcpy(dst->attribute.pValue, src->attribute.pValue,
src->attribute.ulValueLen);
}
if (rv != CKR_OK) {
dealloc_attributes(new_attrs, num_src_attrs);
} else {
*dst_attrs = new_attrs;
*num_dst_attrs = num_src_attrs;
}
return (rv);
}
/*
* dealloc_attributes
*
* Deallocates the storage used for a set of attributes. The attribute
* values are zeroed out before being free'd.
*/
void
dealloc_attributes(generic_attr_t *attributes, size_t num_attributes)
{
size_t i;
generic_attr_t *attr;
for (i = 0; i < num_attributes; i++) {
attr = attributes + i;
/*
* Zero-out any attribute values. We could do this just for
* attributes with isSensitive == True, but it's not much
* extra work to just do them all. [Most attributes are just
* 1 or 4 bytes]
*/
explicit_bzero(attr->attribute.pValue,
attr->attribute.ulValueLen);
if (attr->isMalloced)
free(attr->attribute.pValue);
}
free(attributes);
}
/*
* attribute_set_value
*
* Sets the value of the specified attribute. Any portion of the old value
* which will not be overwritten by the new value is zeroed out.
*/
CK_RV
attribute_set_value(CK_ATTRIBUTE *new_attr,
generic_attr_t *attributes, size_t num_attributes)
{
generic_attr_t *attr = NULL;
if (new_attr == NULL)
return (CKR_TEMPLATE_INCOMPLETE);
else if (new_attr->pValue == NULL) {
return (CKR_ATTRIBUTE_VALUE_INVALID);
}
find_attribute(new_attr->type, attributes, num_attributes, &attr);
if (attr == NULL) {
return (CKR_ATTRIBUTE_TYPE_INVALID);
}
/* Store the new value. */
if (attr->attribute.ulValueLen >= new_attr->ulValueLen) {
/* Existing storage is sufficient to store new value. */
/* bzero() out any data that won't be overwritten. */
explicit_bzero((char *)attr->attribute.pValue +
new_attr->ulValueLen,
attr->attribute.ulValueLen - new_attr->ulValueLen);
} else if (new_attr->ulValueLen <= sizeof (attr->generic_data)) {
/* Use generic storage to avoid a malloc. */
explicit_bzero(attr->attribute.pValue,
attr->attribute.ulValueLen);
if (attr->isMalloced) {
/*
* If app sets a large value (triggering a malloc),
* then sets a tiny value, and finally again sets
* a large value (phew!) we could end up here.
*
* FUTURE?: Store the original malloc size, so that
* we can regrow the value up to the original size.
* This might avoid some heap churn for pathalogic
* applications.
*/
free(attr->attribute.pValue);
attr->isMalloced = B_FALSE;
}
attr->attribute.pValue = attr->generic_data;
} else {
/* Need to allocate storage for the new value. */
void *newStorage;
newStorage = malloc(new_attr->ulValueLen);
if (newStorage == NULL)
return (CKR_HOST_MEMORY);
bzero(attr->attribute.pValue, attr->attribute.ulValueLen);
attr->attribute.pValue = newStorage;
attr->isMalloced = B_TRUE;
}
(void) memcpy(attr->attribute.pValue, new_attr->pValue,
new_attr->ulValueLen);
attr->attribute.ulValueLen = new_attr->ulValueLen;
attr->hasValueForClone = B_TRUE;
return (CKR_OK);
}
/*
* find_attribute
*
* Passes a pointer to the requested attribute, or NULL if not found.
*/
static void
find_attribute(CK_ATTRIBUTE_TYPE attrtype, generic_attr_t *attributes,
size_t num_attributes, generic_attr_t **found_attribute)
{
generic_attr_t *attr;
boolean_t found = B_FALSE;
size_t i;
/* Find the requested attribute. */
for (i = 0, attr = attributes; i < num_attributes; i++, attr++) {
if (attr->attribute.type == attrtype) {
found = B_TRUE;
break;
}
}
*found_attribute = found ? attr : NULL;
}
/*
* get_template_ulong
*
* Look for the specified ulong-size attribute, and retrieve its value. The
* return value specifies if the attribute was found (or not).
*/
boolean_t
get_template_ulong(CK_ATTRIBUTE_TYPE type, CK_ATTRIBUTE *attributes,
CK_ULONG num_attributes, CK_ULONG *result)
{
boolean_t found = B_FALSE;
CK_ULONG i;
for (i = 0; i < num_attributes; i++) {
if (attributes[i].type == type) {
CK_ULONG *value = attributes[i].pValue;
*result = *value;
found = B_TRUE;
break;
}
}
return (found);
}
/*
* get_template_boolean
*
* Look for the specified boolean attribute, and retrieve its value. The
* return value specifies if the attribute was found (or not).
*/
boolean_t
get_template_boolean(CK_ATTRIBUTE_TYPE type, CK_ATTRIBUTE *attributes,
CK_ULONG num_attributes, boolean_t *result)
{
boolean_t found = B_FALSE;
CK_ULONG i;
for (i = 0; i < num_attributes; i++) {
if (attributes[i].type == type) {
CK_BBOOL *value = attributes[i].pValue;
if (*value == CK_FALSE)
*result = B_FALSE;
else
*result = B_TRUE;
found = B_TRUE;
break;
}
}
return (found);
}
/*
* set_template_boolean
*
* Look for the specified boolean attribute, and set its value.
*
* if 'local' is true, it sets the pointer to the value in the template a new
* location. There should be no memory leak created by this because we are
* only doing this to booleans which should not be malloc'ed.
*
* if 'local' is false, it sets its value.
*
* The return value specifies if the attribute was found (or not).
*/
int
set_template_boolean(CK_ATTRIBUTE_TYPE type, CK_ATTRIBUTE *attributes,
CK_ULONG num_attributes, boolean_t local, CK_BBOOL *value)
{
int i;
for (i = 0; i < num_attributes; i++) {
if (attributes[i].type == type) {
if (local)
attributes[i].pValue = value;
else
*((CK_BBOOL *)attributes[i].pValue) = *value;
return (i);
}
}
return (-1);
}
/*
* 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 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#ifndef _META_ATTRMASTERS_H
#define _META_ATTRMASTERS_H
#ifdef __cplusplus
extern "C" {
#endif
/*
* Master object templates
*
* [This file should only be included by a single source file. This is a
* non-traditional header file in that it simply contains a bunch of large,
* preinitialized static const structures. They're stored here to keep them
* "out of the way."]
*
* In PKCS#11, each object is well-defined... Each object type has an exact
* set of attributes, and each attribute always has some value. Some
* attribute values must be specificed when the object is created, others
* are optional (ie, a default value exisits). Thus, the template an
* application provides when creating a new object may be a subset of the
* allowed attributes. The "master" templates presented here, however,
* are complete.
*/
/*
* Aliases for some field values in generic_attr_t, so that the initialization
* below isn't just a confusing mess of B_TRUE and B_FALSE. Lint
* complaints about using "!Foo" in const initializers,
* so we #define each value.
*/
#define unused 0
#define Mallocd B_TRUE
#define Clone B_TRUE
#define EmptyValue B_TRUE
#define NotMallocd B_FALSE
#define NotClone B_FALSE
#define NotEmptyValue B_FALSE
#define EMPTYDATE ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '
#define EMPTY '\0'
/*
* A note regarding CKA_CLASS and sub-type (eg CKA_KEY_TYPE)
*
* These two attributes have been moved to the top of the master template
* definitions. All the metaslot code assumes that CKA_CLASS resides in index=0,
* and the sub-type resides in index=1.
*/
/*
* Common storage object attributes, Table 19 (p81) of PKCS#11 2.11r1 spec.
*/
#define COMMON_STORAGE_ATTRIBUTES \
{ { CKA_TOKEN, NULL, sizeof (CK_BBOOL) }, \
NotMallocd, Clone, NotEmptyValue, B_FALSE, \
CK_FALSE, unused, { unused } }, \
{ { CKA_PRIVATE, NULL, sizeof (CK_BBOOL) }, \
NotMallocd, Clone, NotEmptyValue, B_FALSE, \
CK_FALSE, unused, { unused } }, \
{ { CKA_MODIFIABLE, NULL, sizeof (CK_BBOOL) }, \
NotMallocd, Clone, NotEmptyValue, B_FALSE, \
CK_TRUE, unused, { unused } }, \
{ { CKA_LABEL, NULL, 0 }, \
NotMallocd, Clone, EmptyValue, B_FALSE, \
unused, unused, { EMPTY } }
/*
* Common certificate attributes, Table 21 (p83) of PKCS#11 2.11r1 spec.
* (CKA_CERTIFICATE_TYPE has been moved, to place at top of template)
*
*/
#define COMMON_CERTIFICATE_ATTRIBUTES \
{ { CKA_TRUSTED, NULL, sizeof (CK_BBOOL) }, \
NotMallocd, NotClone, NotEmptyValue, B_FALSE, \
CK_FALSE, unused, { unused } }
/*
* Common key attributes, Table 25 (p89) of PKCS#11 2.11r1 spec.
* (CKA_KEY_TYPE has been moved, to place at top of template)
*
*/
#define COMMON_KEY_ATTRIBUTES \
{ { CKA_ID, NULL, 0 }, \
NotMallocd, Clone, EmptyValue, B_FALSE, \
unused, unused, { EMPTY } }, \
{ { CKA_START_DATE, NULL, sizeof (CK_DATE) }, \
NotMallocd, Clone, EmptyValue, B_FALSE, \
unused, unused, { EMPTYDATE } }, \
{ { CKA_END_DATE, NULL, sizeof (CK_DATE) }, \
NotMallocd, Clone, EmptyValue, B_FALSE, \
unused, unused, { EMPTYDATE } }, \
{ { CKA_DERIVE, NULL, sizeof (CK_BBOOL) }, \
NotMallocd, Clone, NotEmptyValue, B_FALSE, \
CK_FALSE, unused, { unused } }, \
{ { CKA_LOCAL, NULL, sizeof (CK_BBOOL) }, \
NotMallocd, NotClone, NotEmptyValue, B_FALSE, \
CK_FALSE, unused, { unused } }, \
{ { CKA_KEY_GEN_MECHANISM, NULL, sizeof (CK_MECHANISM_TYPE) }, \
NotMallocd, NotClone, EmptyValue, B_FALSE, \
unused, CK_UNAVAILABLE_INFORMATION, { unused } }
/*
* Common public-key attributes, Table 26 (p90) of PKCS#11 2.11r1 spec.
*
* CKA_SUBJECT has the PKCS#11-specified default. The object-usage attributes
* are token-specific defaults.
*
*/
#define COMMON_PUBKEY_ATTRIBUTES \
{ { CKA_SUBJECT, NULL, 0 }, \
NotMallocd, Clone, EmptyValue, B_FALSE, \
unused, unused, { EMPTY } }, \
{ { CKA_ENCRYPT, NULL, sizeof (CK_BBOOL) }, \
NotMallocd, Clone, NotEmptyValue, B_FALSE, \
CK_TRUE, unused, { unused } }, \
{ { CKA_VERIFY, NULL, sizeof (CK_BBOOL) }, \
NotMallocd, Clone, NotEmptyValue, B_FALSE, \
CK_TRUE, unused, { unused } }, \
{ { CKA_VERIFY_RECOVER, NULL, sizeof (CK_BBOOL) }, \
NotMallocd, Clone, NotEmptyValue, B_FALSE, \
CK_TRUE, unused, { unused } }, \
{ { CKA_WRAP, NULL, sizeof (CK_BBOOL) }, \
NotMallocd, Clone, NotEmptyValue, B_FALSE, \
CK_TRUE, unused, { unused } }, \
{ { CKA_TRUSTED, NULL, sizeof (CK_BBOOL) }, \
NotMallocd, NotClone, NotEmptyValue, B_FALSE, \
CK_FALSE, unused, { unused } }
/*
* Common private-key attributes, Table 34 (p97) of PKCS#11 2.11r1 spec.
*/
#define COMMON_PRIVKEY_ATTRIBUTES \
{ { CKA_SUBJECT, NULL, 0 }, \
NotMallocd, Clone, EmptyValue, B_FALSE, \
unused, unused, { EMPTY } }, \
{ { CKA_SENSITIVE, NULL, sizeof (CK_BBOOL) }, \
NotMallocd, Clone, NotEmptyValue, B_FALSE, \
CK_FALSE, unused, { unused } }, \
{ { CKA_SECONDARY_AUTH, NULL, sizeof (CK_BBOOL) }, \
NotMallocd, Clone, EmptyValue, B_FALSE, \
CK_FALSE, unused, { unused } }, \
{ { CKA_DECRYPT, NULL, sizeof (CK_BBOOL) }, \
NotMallocd, Clone, NotEmptyValue, B_FALSE, \
CK_TRUE, unused, { unused } }, \
{ { CKA_SIGN, NULL, sizeof (CK_BBOOL) }, \
NotMallocd, Clone, NotEmptyValue, B_FALSE, \
CK_TRUE, unused, { unused } }, \
{ { CKA_SIGN_RECOVER, NULL, sizeof (CK_BBOOL) }, \
NotMallocd, Clone, NotEmptyValue, B_FALSE, \
CK_TRUE, unused, { unused } }, \
{ { CKA_UNWRAP, NULL, sizeof (CK_BBOOL) }, \
NotMallocd, Clone, NotEmptyValue, B_FALSE, \
CK_TRUE, unused, { unused } }, \
{ { CKA_EXTRACTABLE, NULL, sizeof (CK_BBOOL) }, \
NotMallocd, Clone, NotEmptyValue, B_FALSE, \
CK_TRUE, unused, { unused } }, \
{ { CKA_ALWAYS_SENSITIVE, NULL, sizeof (CK_BBOOL) }, \
NotMallocd, NotClone, NotEmptyValue, B_FALSE, \
CK_FALSE, unused, { unused } }, \
{ { CKA_NEVER_EXTRACTABLE, NULL, sizeof (CK_BBOOL) }, \
NotMallocd, NotClone, NotEmptyValue, B_FALSE, \
CK_FALSE, unused, { unused } }
/*
* Common secret-key attributes, Table 42 (p108) of PKCS#11 2.11r1 spec.
*/
#define COMMON_SECKEY_ATTRIBUTES \
{ { CKA_SENSITIVE, NULL, sizeof (CK_BBOOL) }, \
NotMallocd, Clone, NotEmptyValue, B_FALSE, \
CK_FALSE, unused, { unused } }, \
{ { CKA_ENCRYPT, NULL, sizeof (CK_BBOOL) }, \
NotMallocd, Clone, NotEmptyValue, B_FALSE, \
CK_TRUE, unused, { unused } }, \
{ { CKA_DECRYPT, NULL, sizeof (CK_BBOOL) }, \
NotMallocd, Clone, NotEmptyValue, B_FALSE, \
CK_TRUE, unused, { unused } }, \
{ { CKA_SIGN, NULL, sizeof (CK_BBOOL) }, \
NotMallocd, Clone, NotEmptyValue, B_FALSE, \
CK_TRUE, unused, { unused } }, \
{ { CKA_VERIFY, NULL, sizeof (CK_BBOOL) }, \
NotMallocd, Clone, NotEmptyValue, B_FALSE, \
CK_TRUE, unused, { unused } }, \
{ { CKA_WRAP, NULL, sizeof (CK_BBOOL) }, \
NotMallocd, Clone, NotEmptyValue, B_FALSE, \
CK_TRUE, unused, { unused } }, \
{ { CKA_UNWRAP, NULL, sizeof (CK_BBOOL) }, \
NotMallocd, Clone, NotEmptyValue, B_FALSE, \
CK_TRUE, unused, { unused } }, \
{ { CKA_EXTRACTABLE, NULL, sizeof (CK_BBOOL) }, \
NotMallocd, Clone, NotEmptyValue, B_FALSE, \
CK_TRUE, unused, { unused } }, \
{ { CKA_ALWAYS_SENSITIVE, NULL, sizeof (CK_BBOOL) }, \
NotMallocd, NotClone, NotEmptyValue, B_FALSE, \
CK_FALSE, unused, { unused } }, \
{ { CKA_NEVER_EXTRACTABLE, NULL, sizeof (CK_BBOOL) }, \
NotMallocd, NotClone, NotEmptyValue, B_FALSE, \
CK_FALSE, unused, { unused } }
/*
* Common domain-paramaters attributes, Table 60 (p123) of PKCS#11 2.11r1 spec.
* (CKA_KEY_TYPE has been removed, to place elsewhere)
*/
#define COMMON_DOMAIN_ATTRIBUTES \
{ { CKA_LOCAL, NULL, sizeof (CK_BBOOL) }, \
NotMallocd, Clone, NotEmptyValue, B_FALSE, \
CK_FALSE, unused, { unused } }
/* ========================= HW Objects ========================= */
/*
* Master template for: CKO_HW_FEATURE + CKH_CLOCK
*/
static const generic_attr_t OBJ_HW_CLOCK[] =
{
{ { CKA_CLASS, NULL, sizeof (CK_OBJECT_CLASS) },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, CKO_HW_FEATURE, { unused } },
{ { CKA_HW_FEATURE_TYPE, NULL, sizeof (CK_HW_FEATURE_TYPE) },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, CKH_CLOCK, { unused } },
{ { CKA_VALUE, NULL, 16 },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, unused, { EMPTYDATE, EMPTYDATE } }
};
/*
* Master template for: CKO_HW_FEATURE + CKH_MONOTONIC_COUNTER
*
* NOTE: no sub-type for this class!
*/
static const generic_attr_t OBJ_HW_MONOTONIC[] =
{
{ { CKA_CLASS, NULL, sizeof (CK_OBJECT_CLASS) },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, CKO_HW_FEATURE, { unused } },
{ { CKA_HW_FEATURE_TYPE, NULL, sizeof (CK_HW_FEATURE_TYPE) },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, CKH_MONOTONIC_COUNTER, { unused } },
{ { CKA_VALUE, NULL, 0 },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, unused, { EMPTY } },
{ { CKA_RESET_ON_INIT, NULL, sizeof (CK_BBOOL) },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
CK_FALSE, unused, { unused } },
{ { CKA_HAS_RESET, NULL, sizeof (CK_BBOOL) },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
CK_FALSE, unused, { unused } }
};
/* ========================= Data Objects ========================= */
/*
* Master template for CKO_DATA + (no subtypes for this class)
*
* Defaults are according to PKCS#11.
*
* NOTE: no sub-type for this class!
*/
static const generic_attr_t OBJ_DATA[] =
{
{ { CKA_CLASS, NULL, sizeof (CK_OBJECT_CLASS) },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, CKO_DATA, { unused } },
COMMON_STORAGE_ATTRIBUTES,
{ { CKA_APPLICATION, NULL, 0 },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, unused, { EMPTY } },
{ { CKA_OBJECT_ID, NULL, 0 },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, unused, { EMPTY } },
{ { CKA_VALUE, NULL, 0 },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, unused, { EMPTY } }
};
/* ========================= Certificate Objects ========================= */
/*
* Master template for CKO_CERTIFICATE + CKC_X_509
*
* Defaults are according to PKCS#11.
*/
static const generic_attr_t OBJ_CERT_X509[] =
{
{ { CKA_CLASS, NULL, sizeof (CK_OBJECT_CLASS) },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, CKO_CERTIFICATE, { unused } },
{ { CKA_CERTIFICATE_TYPE, NULL, sizeof (CK_CERTIFICATE_TYPE) },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, CKC_X_509, { unused } },
COMMON_STORAGE_ATTRIBUTES,
COMMON_CERTIFICATE_ATTRIBUTES,
{ { CKA_SUBJECT, NULL, 0 },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, unused, { EMPTY } },
{ { CKA_ID, NULL, 0 },
NotMallocd, Clone, EmptyValue, B_FALSE,
unused, unused, { EMPTY } },
{ { CKA_ISSUER, NULL, 0 },
NotMallocd, Clone, EmptyValue, B_FALSE,
unused, unused, { EMPTY } },
{ { CKA_SERIAL_NUMBER, NULL, 0 },
NotMallocd, Clone, EmptyValue, B_FALSE,
unused, unused, { EMPTY } },
{ { CKA_VALUE, NULL, 0 },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, unused, { EMPTY } }
};
/*
* Master template for CKO_CERTIFICATE + CKC_X_509_ATTR_CERT
*
* Defaults are according to PKCS#11.
*/
static const generic_attr_t OBJ_CERT_X509ATTR[] =
{
{ { CKA_CLASS, NULL, sizeof (CK_OBJECT_CLASS) },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, CKO_CERTIFICATE, { unused } },
{ { CKA_CERTIFICATE_TYPE, NULL, sizeof (CK_CERTIFICATE_TYPE) },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, CKC_X_509_ATTR_CERT, { unused } },
COMMON_STORAGE_ATTRIBUTES,
COMMON_CERTIFICATE_ATTRIBUTES,
{ { CKA_OWNER, NULL, 0 },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, unused, { EMPTY } },
{ { CKA_AC_ISSUER, NULL, 0 },
NotMallocd, Clone, EmptyValue, B_FALSE,
unused, unused, { EMPTY } },
{ { CKA_SERIAL_NUMBER, NULL, 0 },
NotMallocd, Clone, EmptyValue, B_FALSE,
unused, unused, { EMPTY } },
{ { CKA_ATTR_TYPES, NULL, 0 },
NotMallocd, Clone, EmptyValue, B_FALSE,
unused, unused, { EMPTY } },
{ { CKA_VALUE, NULL, 0 },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, unused, { EMPTY } }
};
/* ========================= Public Keys ========================= */
/*
* Master template for CKO_PUBLIC_KEY + CKK_RSA
*/
static const generic_attr_t OBJ_PUBKEY_RSA[] =
{
{ { CKA_CLASS, NULL, sizeof (CK_OBJECT_CLASS) },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, CKO_PUBLIC_KEY, { unused } },
{ { CKA_KEY_TYPE, NULL, sizeof (CK_KEY_TYPE) },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, CKK_RSA, { unused } },
COMMON_STORAGE_ATTRIBUTES,
COMMON_KEY_ATTRIBUTES,
COMMON_PUBKEY_ATTRIBUTES,
{ { CKA_MODULUS, NULL, 0 },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, unused, { EMPTY } },
{ { CKA_MODULUS_BITS, NULL, sizeof (CK_ULONG)},
NotMallocd, NotClone, NotEmptyValue, B_FALSE,
unused, 0, { unused } },
{ { CKA_PUBLIC_EXPONENT, NULL, 0 },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, unused, { EMPTY } }
};
/*
* Master template for CKO_PUBLIC_KEY + CKK_DSA
*
*/
static const generic_attr_t OBJ_PUBKEY_DSA[] =
{
{ { CKA_CLASS, NULL, sizeof (CK_OBJECT_CLASS) },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, CKO_PUBLIC_KEY, { unused } },
{ { CKA_KEY_TYPE, NULL, sizeof (CK_KEY_TYPE) },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, CKK_DSA, { unused } },
COMMON_STORAGE_ATTRIBUTES,
COMMON_KEY_ATTRIBUTES,
COMMON_PUBKEY_ATTRIBUTES,
{ { CKA_PRIME, NULL, 0 },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, unused, { EMPTY } },
{ { CKA_SUBPRIME, NULL, 0 },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, unused, { EMPTY } },
{ { CKA_BASE, NULL, 0 },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, unused, { EMPTY } },
{ { CKA_VALUE, NULL, 0 },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, unused, { EMPTY } }
};
/*
* Master template for CKO_PUBLIC_KEY + CKK_EC
*
*/
static const generic_attr_t OBJ_PUBKEY_EC[] =
{
{ { CKA_CLASS, NULL, sizeof (CK_OBJECT_CLASS) },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, CKO_PUBLIC_KEY, { unused } },
{ { CKA_KEY_TYPE, NULL, sizeof (CK_KEY_TYPE) },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, CKK_EC, { unused } },
COMMON_STORAGE_ATTRIBUTES,
COMMON_KEY_ATTRIBUTES,
COMMON_PUBKEY_ATTRIBUTES,
{ { CKA_EC_PARAMS, NULL, 0 },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, unused, { EMPTY } },
{ { CKA_EC_POINT, NULL, 0 },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, unused, { EMPTY } }
};
/*
* Master template for CKO_PUBLIC_KEY + CKK_DH
*
*/
static const generic_attr_t OBJ_PUBKEY_DH[] =
{
{ { CKA_CLASS, NULL, sizeof (CK_OBJECT_CLASS) },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, CKO_PUBLIC_KEY, { unused } },
{ { CKA_KEY_TYPE, NULL, sizeof (CK_KEY_TYPE) },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, CKK_DH, { unused } },
COMMON_STORAGE_ATTRIBUTES,
COMMON_KEY_ATTRIBUTES,
COMMON_PUBKEY_ATTRIBUTES,
{ { CKA_PRIME, NULL, 0 },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, unused, { EMPTY } },
{ { CKA_BASE, NULL, 0 },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, unused, { EMPTY } },
{ { CKA_VALUE, NULL, 0 },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, unused, { EMPTY } }
};
/*
* Master template for CKO_PUBLIC_KEY + CKK_X9_42_DH
*
*/
static const generic_attr_t OBJ_PUBKEY_X942DH[] =
{
{ { CKA_CLASS, NULL, sizeof (CK_OBJECT_CLASS) },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, CKO_PUBLIC_KEY, { unused } },
{ { CKA_KEY_TYPE, NULL, sizeof (CK_KEY_TYPE) },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, CKK_X9_42_DH, { unused } },
COMMON_STORAGE_ATTRIBUTES,
COMMON_KEY_ATTRIBUTES,
COMMON_PUBKEY_ATTRIBUTES,
{ { CKA_PRIME, NULL, 0 },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, unused, { EMPTY } },
{ { CKA_BASE, NULL, 0 },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, unused, { EMPTY } },
{ { CKA_SUBPRIME, NULL, 0 },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, unused, { EMPTY } },
{ { CKA_VALUE, NULL, 0 },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, unused, { EMPTY } }
};
/*
* Master template for CKO_PUBLIC_KEY + CKK_KEA
*
*/
static const generic_attr_t OBJ_PUBKEY_KEA[] =
{
{ { CKA_CLASS, NULL, sizeof (CK_OBJECT_CLASS) },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, CKO_PUBLIC_KEY, { unused } },
{ { CKA_KEY_TYPE, NULL, sizeof (CK_KEY_TYPE) },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, CKK_KEA, { unused } },
COMMON_STORAGE_ATTRIBUTES,
COMMON_KEY_ATTRIBUTES,
COMMON_PUBKEY_ATTRIBUTES,
{ { CKA_PRIME, NULL, 0 },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, unused, { EMPTY } },
{ { CKA_BASE, NULL, 0 },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, unused, { EMPTY } },
{ { CKA_SUBPRIME, NULL, 0 },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, unused, { EMPTY } },
{ { CKA_VALUE, NULL, 0 },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, unused, { EMPTY } }
};
/* ========================= Private Keys ========================= */
/*
* Master template for CKO_PRIVATE_KEY + CKK_RSA
*
*/
static const generic_attr_t OBJ_PRIVKEY_RSA[] =
{
{ { CKA_CLASS, NULL, sizeof (CK_OBJECT_CLASS) },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, CKO_PRIVATE_KEY, { unused } },
{ { CKA_KEY_TYPE, NULL, sizeof (CK_KEY_TYPE) },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, CKK_RSA, { unused } },
COMMON_STORAGE_ATTRIBUTES,
COMMON_KEY_ATTRIBUTES,
COMMON_PRIVKEY_ATTRIBUTES,
{ { CKA_MODULUS, NULL, 0 },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, unused, { EMPTY } },
{ { CKA_PRIVATE_EXPONENT, NULL, 0 },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, unused, { EMPTY } },
{ { CKA_PUBLIC_EXPONENT, NULL, 0 },
NotMallocd, Clone, EmptyValue, B_FALSE,
unused, unused, { EMPTY } },
{ { CKA_PRIME_1, NULL, 0 },
NotMallocd, Clone, EmptyValue, B_FALSE,
unused, unused, { EMPTY } },
{ { CKA_PRIME_2, NULL, 0 },
NotMallocd, Clone, EmptyValue, B_FALSE,
unused, unused, { EMPTY } },
{ { CKA_EXPONENT_1, NULL, 0 },
NotMallocd, Clone, EmptyValue, B_FALSE,
unused, unused, { EMPTY } },
{ { CKA_EXPONENT_2, NULL, 0 },
NotMallocd, Clone, EmptyValue, B_FALSE,
unused, unused, { EMPTY } },
{ { CKA_COEFFICIENT, NULL, 0 },
NotMallocd, Clone, EmptyValue, B_FALSE,
unused, unused, { EMPTY } }
};
/*
* Master template for CKO_PRIVATE_KEY + CKK_DSA
*
*/
static const generic_attr_t OBJ_PRIVKEY_DSA[] =
{
{ { CKA_CLASS, NULL, sizeof (CK_OBJECT_CLASS) },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, CKO_PRIVATE_KEY, { unused } },
{ { CKA_KEY_TYPE, NULL, sizeof (CK_KEY_TYPE) },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, CKK_DSA, { unused } },
COMMON_STORAGE_ATTRIBUTES,
COMMON_KEY_ATTRIBUTES,
COMMON_PRIVKEY_ATTRIBUTES,
{ { CKA_PRIME, NULL, 0 },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, unused, { EMPTY } },
{ { CKA_SUBPRIME, NULL, 0 },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, unused, { EMPTY } },
{ { CKA_BASE, NULL, 0 },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, unused, { EMPTY } },
{ { CKA_VALUE, NULL, 0 },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, unused, { EMPTY } }
};
/*
* Master template for CKO_PRIVATE_KEY + CKK_EC
*
*/
static const generic_attr_t OBJ_PRIVKEY_EC[] =
{
{ { CKA_CLASS, NULL, sizeof (CK_OBJECT_CLASS) },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, CKO_PRIVATE_KEY, { unused } },
{ { CKA_KEY_TYPE, NULL, sizeof (CK_KEY_TYPE) },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, CKK_EC, { unused } },
COMMON_STORAGE_ATTRIBUTES,
COMMON_KEY_ATTRIBUTES,
COMMON_PRIVKEY_ATTRIBUTES,
{ { CKA_EC_PARAMS, NULL, 0 },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, unused, { EMPTY } },
{ { CKA_VALUE, NULL, 0 },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, unused, { EMPTY } }
};
/*
* Master template for CKO_PRIVATE_KEY + CKK_DH
*/
static const generic_attr_t OBJ_PRIVKEY_DH[] =
{
{ { CKA_CLASS, NULL, sizeof (CK_OBJECT_CLASS) },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, CKO_PRIVATE_KEY, { unused } },
{ { CKA_KEY_TYPE, NULL, sizeof (CK_KEY_TYPE) },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, CKK_DH, { unused } },
COMMON_STORAGE_ATTRIBUTES,
COMMON_KEY_ATTRIBUTES,
COMMON_PRIVKEY_ATTRIBUTES,
{ { CKA_PRIME, NULL, 0 },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, unused, { EMPTY } },
{ { CKA_BASE, NULL, 0 },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, unused, { EMPTY } },
{ { CKA_VALUE, NULL, 0 },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, unused, { EMPTY } },
{ { CKA_VALUE_BITS, NULL, sizeof (CK_ULONG) },
NotMallocd, NotClone, NotEmptyValue, B_FALSE,
unused, 0, { unused } }
};
/*
* Master template for CKO_PRIVATE_KEY + CKK_X9_42_DH
*
*/
static const generic_attr_t OBJ_PRIVKEY_X942DH[] =
{
{ { CKA_CLASS, NULL, sizeof (CK_OBJECT_CLASS) },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, CKO_PRIVATE_KEY, { unused } },
{ { CKA_KEY_TYPE, NULL, sizeof (CK_KEY_TYPE) },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, CKK_X9_42_DH, { unused } },
COMMON_STORAGE_ATTRIBUTES,
COMMON_KEY_ATTRIBUTES,
COMMON_PRIVKEY_ATTRIBUTES,
{ { CKA_PRIME, NULL, 0 },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, unused, { EMPTY } },
{ { CKA_SUBPRIME, NULL, 0 },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, unused, { EMPTY } },
{ { CKA_BASE, NULL, 0 },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, unused, { EMPTY } },
{ { CKA_VALUE, NULL, 0 },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, unused, { EMPTY } }
};
/*
* Master template for CKO_PRIVATE_KEY + CKK_KEA
*
*/
static const generic_attr_t OBJ_PRIVKEY_KEA[] =
{
{ { CKA_CLASS, NULL, sizeof (CK_OBJECT_CLASS) },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, CKO_PRIVATE_KEY, { unused } },
{ { CKA_KEY_TYPE, NULL, sizeof (CK_KEY_TYPE) },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, CKK_KEA, { unused } },
COMMON_STORAGE_ATTRIBUTES,
COMMON_KEY_ATTRIBUTES,
COMMON_PRIVKEY_ATTRIBUTES,
{ { CKA_PRIME, NULL, 0 },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, unused, { EMPTY } },
{ { CKA_BASE, NULL, 0 },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, unused, { EMPTY } },
{ { CKA_SUBPRIME, NULL, 0 },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, unused, { EMPTY } },
{ { CKA_VALUE, NULL, 0 },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, unused, { EMPTY } }
};
/* ========================= Secret Keys ========================= */
/*
* Master template for CKO_SECRET_KEY + (fixed-length keytype)
*/
static const generic_attr_t OBJ_SECKEY[] =
{
{ { CKA_CLASS, NULL, sizeof (CK_OBJECT_CLASS) },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, CKO_SECRET_KEY, { unused } },
{ { CKA_KEY_TYPE, NULL, sizeof (CK_KEY_TYPE) },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, CKK_GENERIC_SECRET, { unused } },
COMMON_STORAGE_ATTRIBUTES,
COMMON_KEY_ATTRIBUTES,
COMMON_SECKEY_ATTRIBUTES,
{ { CKA_VALUE, NULL, 0 },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, unused, { EMPTY } }
};
/*
* Master template for CKO_SECRET_KEY + (variable-length keytype)
*
*/
static const generic_attr_t OBJ_SECKEY_WITHLEN[] =
{
{ { CKA_CLASS, NULL, sizeof (CK_OBJECT_CLASS) },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, CKO_SECRET_KEY, { unused } },
{ { CKA_KEY_TYPE, NULL, sizeof (CK_KEY_TYPE) },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, CKK_GENERIC_SECRET, { unused } },
COMMON_STORAGE_ATTRIBUTES,
COMMON_KEY_ATTRIBUTES,
COMMON_SECKEY_ATTRIBUTES,
{ { CKA_VALUE, NULL, 0 },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, unused, { EMPTY } },
{ { CKA_VALUE_LEN, NULL, sizeof (CK_ULONG) },
NotMallocd, NotClone, NotEmptyValue, B_FALSE,
unused, 0, { unused } }
};
/* ========================= Domain Parameters ========================= */
/*
* Master template for CKO_DOMAIN_PARAMETERS + CKK_DSA
*
*/
static const generic_attr_t OBJ_DOM_DSA[] =
{
{ { CKA_CLASS, NULL, sizeof (CK_OBJECT_CLASS) },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, CKO_DOMAIN_PARAMETERS, { unused } },
{ { CKA_KEY_TYPE, NULL, sizeof (CK_KEY_TYPE) },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, CKK_DSA, { unused } },
COMMON_STORAGE_ATTRIBUTES,
COMMON_DOMAIN_ATTRIBUTES,
{ { CKA_PRIME, NULL, 0 },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, unused, { EMPTY } },
{ { CKA_SUBPRIME, NULL, 0 },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, unused, { EMPTY } },
{ { CKA_BASE, NULL, 0 },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, unused, { EMPTY } },
{ { CKA_PRIME_BITS, NULL, sizeof (CK_ULONG) },
NotMallocd, NotClone, NotEmptyValue, B_FALSE,
unused, 0, { unused } }
};
/*
* Master template for CKO_DOMAIN_PARAMETERS + CKK_DH
*
*/
static const generic_attr_t OBJ_DOM_DH[] =
{
{ { CKA_CLASS, NULL, sizeof (CK_OBJECT_CLASS) },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, CKO_DOMAIN_PARAMETERS, { unused } },
{ { CKA_KEY_TYPE, NULL, sizeof (CK_KEY_TYPE) },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, CKK_DH, { unused } },
COMMON_STORAGE_ATTRIBUTES,
COMMON_DOMAIN_ATTRIBUTES,
{ { CKA_PRIME, NULL, 0 },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, unused, { EMPTY } },
{ { CKA_BASE, NULL, 0 },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, unused, { EMPTY } },
{ { CKA_PRIME_BITS, NULL, sizeof (CK_ULONG) },
NotMallocd, NotClone, NotEmptyValue, B_FALSE,
unused, 0, { unused } }
};
/*
* Master template for CKO_DOMAIN_PARAMETERS + CKK_X9_42_DH
*
*/
static const generic_attr_t OBJ_DOM_X942DH[] =
{
{ { CKA_CLASS, NULL, sizeof (CK_OBJECT_CLASS) },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, CKO_DOMAIN_PARAMETERS, { unused } },
{ { CKA_KEY_TYPE, NULL, sizeof (CK_KEY_TYPE) },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, CKK_X9_42_DH, { unused } },
COMMON_STORAGE_ATTRIBUTES,
COMMON_DOMAIN_ATTRIBUTES,
{ { CKA_PRIME, NULL, 0 },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, unused, { EMPTY } },
{ { CKA_BASE, NULL, 0 },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, unused, { EMPTY } },
{ { CKA_SUBPRIME, NULL, 0 },
NotMallocd, Clone, NotEmptyValue, B_FALSE,
unused, unused, { EMPTY } },
{ { CKA_PRIME_BITS, NULL, sizeof (CK_ULONG) },
NotMallocd, NotClone, NotEmptyValue, B_FALSE,
unused, 0, { unused } },
{ { CKA_SUBPRIME_BITS, NULL, sizeof (CK_ULONG) },
NotMallocd, NotClone, NotEmptyValue, B_FALSE,
unused, 0, { unused } }
};
#ifdef __cplusplus
}
#endif
#endif /* _META_ATTRMASTERS_H */
/*
* 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 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
* Copyright 2018, Joyent, Inc.
*/
/*
* Encryption and Decryption Functions
* (as defined in PKCS#11 spec sections 11.8 and 11.9)
*/
#include "metaGlobal.h"
/*
* meta_EncryptInit
*
*/
CK_RV
meta_EncryptInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism,
CK_OBJECT_HANDLE hKey)
{
CK_RV rv;
meta_session_t *session;
meta_object_t *key;
if (pMechanism == NULL)
return (CKR_ARGUMENTS_BAD);
rv = meta_handle2session(hSession, &session);
if (rv != CKR_OK)
return (rv);
rv = meta_handle2object(hKey, &key);
if (rv != CKR_OK) {
REFRELEASE(session);
return (rv);
}
rv = meta_operation_init_defer(CKF_ENCRYPT, session, pMechanism, key);
OBJRELEASE(key);
REFRELEASE(session);
return (rv);
}
/*
* meta_Encrypt
*
*/
CK_RV
meta_Encrypt(CK_SESSION_HANDLE hSession,
CK_BYTE_PTR pData, CK_ULONG ulDataLen,
CK_BYTE_PTR pEncryptedData, CK_ULONG_PTR pulEncryptedDataLen)
{
CK_RV rv;
meta_session_t *session;
rv = meta_handle2session(hSession, &session);
if (rv != CKR_OK)
return (rv);
if (pulEncryptedDataLen == NULL) {
meta_operation_cleanup(session, CKF_ENCRYPT, FALSE);
REFRELEASE(session);
return (CKR_ARGUMENTS_BAD);
}
/*
* Allow pData to be NULL as long as the length is 0 in order to
* support ciphers that permit 0 byte inputs (e.g. combined mode
* ciphers), otherwise consider pData being NULL as invalid.
*/
if (pData == NULL && ulDataLen != 0) {
meta_operation_cleanup(session, CKF_ENCRYPT, FALSE);
REFRELEASE(session);
return (CKR_ARGUMENTS_BAD);
}
rv = meta_do_operation(CKF_ENCRYPT, MODE_SINGLE, session, NULL,
pData, ulDataLen, pEncryptedData, pulEncryptedDataLen);
REFRELEASE(session);
return (rv);
}
/*
* meta_EncryptUpdate
*
*/
CK_RV
meta_EncryptUpdate(CK_SESSION_HANDLE hSession,
CK_BYTE_PTR pPart, CK_ULONG ulPartLen,
CK_BYTE_PTR pEncryptedPart, CK_ULONG_PTR pulEncryptedPartLen)
{
CK_RV rv;
meta_session_t *session;
rv = meta_handle2session(hSession, &session);
if (rv != CKR_OK)
return (rv);
if (pPart == NULL || pulEncryptedPartLen == NULL) {
meta_operation_cleanup(session, CKF_ENCRYPT, FALSE);
REFRELEASE(session);
return (CKR_ARGUMENTS_BAD);
}
rv = meta_do_operation(CKF_ENCRYPT, MODE_UPDATE, session, NULL,
pPart, ulPartLen, pEncryptedPart, pulEncryptedPartLen);
REFRELEASE(session);
return (rv);
}
/*
* meta_EncryptFinal
*
*/
CK_RV
meta_EncryptFinal(CK_SESSION_HANDLE hSession,
CK_BYTE_PTR pLastEncryptedPart, CK_ULONG_PTR pulLastEncryptedPartLen)
{
CK_RV rv;
meta_session_t *session;
rv = meta_handle2session(hSession, &session);
if (rv != CKR_OK)
return (rv);
if (pulLastEncryptedPartLen == NULL) {
meta_operation_cleanup(session, CKF_ENCRYPT, FALSE);
REFRELEASE(session);
return (CKR_ARGUMENTS_BAD);
}
rv = meta_do_operation(CKF_ENCRYPT, MODE_FINAL, session, NULL,
NULL, 0, pLastEncryptedPart, pulLastEncryptedPartLen);
REFRELEASE(session);
return (rv);
}
/*
* meta_DecryptInit
*
*/
CK_RV
meta_DecryptInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism,
CK_OBJECT_HANDLE hKey)
{
CK_RV rv;
meta_session_t *session;
meta_object_t *key;
if (pMechanism == NULL)
return (CKR_ARGUMENTS_BAD);
rv = meta_handle2session(hSession, &session);
if (rv != CKR_OK)
return (rv);
rv = meta_handle2object(hKey, &key);
if (rv != CKR_OK) {
REFRELEASE(session);
return (rv);
}
rv = meta_operation_init_defer(CKF_DECRYPT, session, pMechanism, key);
OBJRELEASE(key);
REFRELEASE(session);
return (rv);
}
/*
* meta_Decrypt
*
*/
CK_RV
meta_Decrypt(CK_SESSION_HANDLE hSession,
CK_BYTE_PTR pEncryptedData, CK_ULONG ulEncryptedDataLen,
CK_BYTE_PTR pData, CK_ULONG_PTR pulDataLen)
{
CK_RV rv;
meta_session_t *session;
rv = meta_handle2session(hSession, &session);
if (rv != CKR_OK)
return (rv);
if (pEncryptedData == NULL || pulDataLen == NULL) {
meta_operation_cleanup(session, CKF_DECRYPT, FALSE);
REFRELEASE(session);
return (CKR_ARGUMENTS_BAD);
}
rv = meta_do_operation(CKF_DECRYPT, MODE_SINGLE, session, NULL,
pEncryptedData, ulEncryptedDataLen, pData, pulDataLen);
REFRELEASE(session);
return (rv);
}
/*
* meta_DecryptUpdate
*
*/
CK_RV
meta_DecryptUpdate(CK_SESSION_HANDLE hSession,
CK_BYTE_PTR pEncryptedPart, CK_ULONG ulEncryptedPartLen,
CK_BYTE_PTR pPart, CK_ULONG_PTR pulPartLen)
{
CK_RV rv;
meta_session_t *session;
rv = meta_handle2session(hSession, &session);
if (rv != CKR_OK)
return (rv);
if (pEncryptedPart == NULL || pulPartLen == NULL) {
meta_operation_cleanup(session, CKF_DECRYPT, FALSE);
REFRELEASE(session);
return (CKR_ARGUMENTS_BAD);
}
rv = meta_do_operation(CKF_DECRYPT, MODE_UPDATE, session, NULL,
pEncryptedPart, ulEncryptedPartLen, pPart, pulPartLen);
REFRELEASE(session);
return (rv);
}
/*
* meta_DecryptFinal
*
*/
CK_RV
meta_DecryptFinal(CK_SESSION_HANDLE hSession,
CK_BYTE_PTR pLastPart, CK_ULONG_PTR pulLastPartLen)
{
CK_RV rv;
meta_session_t *session;
rv = meta_handle2session(hSession, &session);
if (rv != CKR_OK)
return (rv);
if (pulLastPartLen == NULL) {
meta_operation_cleanup(session, CKF_DECRYPT, FALSE);
REFRELEASE(session);
return (CKR_ARGUMENTS_BAD);
}
rv = meta_do_operation(CKF_DECRYPT, MODE_FINAL, session, NULL,
NULL, 0, pLastPart, pulLastPartLen);
REFRELEASE(session);
return (rv);
}
/*
* 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 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
* Copyright 2018, Joyent, Inc.
*/
/*
* Message Digesting Functions
* (as defined in PKCS#11 spec section 11.10)
*/
#include "metaGlobal.h"
/*
* meta_DigestInit
*
*/
CK_RV
meta_DigestInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism)
{
CK_RV rv;
meta_session_t *session;
if (pMechanism == NULL)
return (CKR_ARGUMENTS_BAD);
rv = meta_handle2session(hSession, &session);
if (rv != CKR_OK)
return (rv);
rv = meta_operation_init_defer(CKF_DIGEST, session, pMechanism, NULL);
REFRELEASE(session);
return (rv);
}
/*
* meta_Digest
*
*/
CK_RV
meta_Digest(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pData, CK_ULONG ulDataLen,
CK_BYTE_PTR pDigest, CK_ULONG_PTR pulDigestLen)
{
CK_RV rv;
meta_session_t *session;
if ((pData == NULL && ulDataLen != 0) || pulDigestLen == NULL)
return (CKR_ARGUMENTS_BAD);
rv = meta_handle2session(hSession, &session);
if (rv != CKR_OK)
return (rv);
rv = meta_do_operation(CKF_DIGEST, MODE_SINGLE, session, NULL,
pData, ulDataLen, pDigest, pulDigestLen);
REFRELEASE(session);
return (rv);
}
/*
* meta_DigestUpdate
*
*/
CK_RV
meta_DigestUpdate(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pPart,
CK_ULONG ulPartLen)
{
CK_RV rv;
meta_session_t *session;
if (pPart == NULL)
return (CKR_ARGUMENTS_BAD);
rv = meta_handle2session(hSession, &session);
if (rv != CKR_OK)
return (rv);
rv = meta_do_operation(CKF_DIGEST, MODE_UPDATE, session, NULL,
pPart, ulPartLen, NULL, NULL);
REFRELEASE(session);
return (rv);
}
/*
* meta_DigestKey
*
* NOTE: This function can fail under certain circumstances!
* Unlike the other crypto functions, we didn't get the key object
* when the operation was initialized with C_DigestInit().
* Thus, the slot we're using for the digest operation may
* not be the slot containing the key -- if the key is extractible we can
* deal with it, but if it's not the operation will FAIL.
*/
CK_RV
meta_DigestKey(CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE hKey)
{
CK_RV rv;
meta_session_t *session;
meta_object_t *key;
rv = meta_handle2session(hSession, &session);
if (rv != CKR_OK)
return (rv);
rv = meta_handle2object(hKey, &key);
if (rv != CKR_OK) {
REFRELEASE(session);
return (rv);
}
/* meta_do_operation() will clone the key, if needed. */
rv = meta_do_operation(CKF_DIGEST, MODE_UPDATE_WITHKEY, session, key,
NULL, 0, NULL, NULL);
OBJRELEASE(key);
REFRELEASE(session);
return (rv);
}
/*
* meta_DigestFinal
*
*/
CK_RV
meta_DigestFinal(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pDigest,
CK_ULONG_PTR pulDigestLen)
{
CK_RV rv;
meta_session_t *session;
if (pulDigestLen == NULL)
return (CKR_ARGUMENTS_BAD);
rv = meta_handle2session(hSession, &session);
if (rv != CKR_OK)
return (rv);
rv = meta_do_operation(CKF_DIGEST, MODE_FINAL, session, NULL,
NULL, 0, pDigest, pulDigestLen);
REFRELEASE(session);
return (rv);
}
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (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 2004 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* Dual-Function Cryptographic Functions
* (as defined in PKCS#11 spec section 11.13)
*
* These functions will not be supported in the this release.
* A call to these functions returns CKR_FUNCTION_NOT_SUPPORTED.
*
* Providing the support for dual-function crypto functions is
* not trivial. C_FooInit() need to be called for the 2 crypto
* operations before any of these function can be called.
* When C_FooInit() is called, metaslot doesn't know if it is going
* to do dual-function crypto or single crypto operation.
* So, it has no way to pick the slot that supports both the mechanism
* it specified and supports dual-functions.
*
* In order for these dual functions to be supported in the future,
* metaslot need to simulate the dual-function crypto operations
* when both operations are not lucky enough be to initialized in
* the same slots that supports dual-functions.
*/
#include "metaGlobal.h"
/*
* meta_DigestEncryptUpdate
*
*/
/*ARGSUSED*/
CK_RV
meta_DigestEncryptUpdate(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pPart,
CK_ULONG ulPartLen, CK_BYTE_PTR pEncryptedPart,
CK_ULONG_PTR pulEncryptedPartLen)
{
return (CKR_FUNCTION_NOT_SUPPORTED);
}
/*
* meta_DecryptDigestUpdate
*
*/
/*ARGSUSED*/
CK_RV
meta_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);
}
/*
* meta_SignEncryptUpdate
*
*/
/*ARGSUSED*/
CK_RV
meta_SignEncryptUpdate(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pPart,
CK_ULONG ulPartLen, CK_BYTE_PTR pEncryptedPart,
CK_ULONG_PTR pulEncryptedPartLen)
{
return (CKR_FUNCTION_NOT_SUPPORTED);
}
/*
* meta_DecryptVerifyUpdate
*
*/
/*ARGSUSED*/
CK_RV
meta_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);
}
/*
* 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.
*
* Copyright 2014, OmniTI Computer Consulting, Inc. All rights reserved.
*/
/*
* General-Purpose Functions
* (as defined in PKCS#11 spec section 11.4)
*/
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include "metaGlobal.h"
extern meta_session_t *meta_sessionlist_head;
struct CK_FUNCTION_LIST metaslot_functionList = {
{ 2, 20 }, /* version */
meta_Initialize,
meta_Finalize,
meta_GetInfo,
meta_GetFunctionList,
meta_GetSlotList,
meta_GetSlotInfo,
meta_GetTokenInfo,
meta_GetMechanismList,
meta_GetMechanismInfo,
meta_InitToken,
meta_InitPIN,
meta_SetPIN,
meta_OpenSession,
meta_CloseSession,
meta_CloseAllSessions,
meta_GetSessionInfo,
meta_GetOperationState,
meta_SetOperationState,
meta_Login,
meta_Logout,
meta_CreateObject,
meta_CopyObject,
meta_DestroyObject,
meta_GetObjectSize,
meta_GetAttributeValue,
meta_SetAttributeValue,
meta_FindObjectsInit,
meta_FindObjects,
meta_FindObjectsFinal,
meta_EncryptInit,
meta_Encrypt,
meta_EncryptUpdate,
meta_EncryptFinal,
meta_DecryptInit,
meta_Decrypt,
meta_DecryptUpdate,
meta_DecryptFinal,
meta_DigestInit,
meta_Digest,
meta_DigestUpdate,
meta_DigestKey,
meta_DigestFinal,
meta_SignInit,
meta_Sign,
meta_SignUpdate,
meta_SignFinal,
meta_SignRecoverInit,
meta_SignRecover,
meta_VerifyInit,
meta_Verify,
meta_VerifyUpdate,
meta_VerifyFinal,
meta_VerifyRecoverInit,
meta_VerifyRecover,
meta_DigestEncryptUpdate,
meta_DecryptDigestUpdate,
meta_SignEncryptUpdate,
meta_DecryptVerifyUpdate,
meta_GenerateKey,
meta_GenerateKeyPair,
meta_WrapKey,
meta_UnwrapKey,
meta_DeriveKey,
meta_SeedRandom,
meta_GenerateRandom,
meta_GetFunctionStatus,
meta_CancelFunction,
meta_WaitForSlotEvent
};
pthread_mutex_t initmutex = PTHREAD_MUTEX_INITIALIZER;
ses_to_be_freed_list_t ses_delay_freed;
object_to_be_freed_list_t obj_delay_freed;
/*
* meta_Initialize
*
* This function is never called by the application. It is only
* called by uCF to initialize metaslot. The pInitArgs argument is ignored.
*
*/
/*ARGSUSED*/
CK_RV
meta_Initialize(CK_VOID_PTR pInitArgs)
{
CK_RV rv;
/* Make sure function hasn't been called twice */
(void) pthread_mutex_lock(&initmutex);
rv = meta_slotManager_initialize();
if (rv != CKR_OK) {
(void) pthread_mutex_unlock(&initmutex);
return (rv);
}
rv = meta_mechManager_initialize();
if (rv != CKR_OK) {
(void) meta_slotManager_finalize();
(void) pthread_mutex_unlock(&initmutex);
return (rv);
}
rv = meta_objectManager_initialize();
if (rv != CKR_OK) {
(void) meta_slotManager_finalize();
(void) meta_mechManager_finalize();
(void) pthread_mutex_unlock(&initmutex);
return (rv);
}
rv = meta_sessionManager_initialize();
if (rv != CKR_OK) {
(void) meta_slotManager_finalize();
(void) meta_mechManager_finalize();
(void) meta_objectManager_finalize();
(void) pthread_mutex_unlock(&initmutex);
return (rv);
}
meta_slotManager_find_object_token();
/* Initialize the object_to_be_freed list */
(void) pthread_mutex_init(&obj_delay_freed.obj_to_be_free_mutex, NULL);
obj_delay_freed.count = 0;
obj_delay_freed.first = NULL;
obj_delay_freed.last = NULL;
/* Initialize the session_to_be_freed list */
(void) pthread_mutex_init(&ses_delay_freed.ses_to_be_free_mutex, NULL);
ses_delay_freed.count = 0;
ses_delay_freed.first = NULL;
ses_delay_freed.last = NULL;
(void) pthread_mutex_unlock(&initmutex);
return (CKR_OK);
}
/*
* meta_Finalize
*
* Called by uCF only, "pReserved" argument is ignored.
*/
/*ARGSUSED*/
CK_RV
meta_Finalize(CK_VOID_PTR pReserved)
{
CK_RV rv = CKR_OK;
meta_object_t *delay_free_obj, *tmpo;
meta_session_t *delay_free_ses, *tmps;
if (pReserved != NULL)
return (CKR_ARGUMENTS_BAD);
(void) pthread_mutex_lock(&initmutex);
/*
* There used to be calls to cleanup libcryptoutil here. Given that
* libcryptoutil can be linked and invoked independently of PKCS#11,
* cleaning up libcryptoutil here makes no sense. Decoupling these
* two also prevent deadlocks and other artificial dependencies.
*/
meta_objectManager_finalize();
meta_sessionManager_finalize();
meta_mechManager_finalize();
meta_slotManager_finalize();
/*
* free all entries in the delay_freed list
*/
delay_free_obj = obj_delay_freed.first;
while (delay_free_obj != NULL) {
tmpo = delay_free_obj->next;
free(delay_free_obj);
delay_free_obj = tmpo;
}
(void) pthread_mutex_destroy(&obj_delay_freed.obj_to_be_free_mutex);
delay_free_ses = ses_delay_freed.first;
while (delay_free_ses != NULL) {
tmps = delay_free_ses->next;
free(delay_free_ses);
delay_free_ses = tmps;
}
(void) pthread_mutex_destroy(&ses_delay_freed.ses_to_be_free_mutex);
(void) pthread_mutex_unlock(&initmutex);
return (rv);
}
/*
* meta_GetInfo
*
* NOTE: This function will never be called by applications because it's
* hidden behind the uCF C_GetInfo. So, it is not implemented.
*/
/*ARGSUSED*/
CK_RV
meta_GetInfo(CK_INFO_PTR pInfo)
{
return (CKR_FUNCTION_NOT_SUPPORTED);
}
/*
* meta_GetFunctionList
*
* This function is not implemented because metaslot is part of the framework,
* so, the framework can just do a static assignment to metaslot's
* function list instead of calling this function.
*/
/*ARGSUSED*/
CK_RV
meta_GetFunctionList(CK_FUNCTION_LIST_PTR_PTR ppFunctionList)
{
return (CKR_FUNCTION_NOT_SUPPORTED);
}
/*
* Parallel Function Management Function
* (as defined in PKCS#11 spec section 11.16)
*/
/*
* This function is no longer supported in this revision of the PKCS#11
* standard. It is maintained for backwards compatibility only.
*/
/* ARGSUSED */
CK_RV
meta_GetFunctionStatus(CK_SESSION_HANDLE hSession)
{
return (CKR_FUNCTION_NOT_PARALLEL);
}
/*
* This function is no longer supported in this revision of the PKCS#11
* standard. It is maintained for backwards compatibility only.
*/
/* ARGSUSED */
CK_RV
meta_CancelFunction(CK_SESSION_HANDLE hSession)
{
return (CKR_FUNCTION_NOT_PARALLEL);
}
/*
* 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.
*/
#ifndef _METAGLOBAL_H
#define _METAGLOBAL_H
/*
* This file contains all the data structures used for the meta slot
*/
#ifdef __cplusplus
extern "C" {
#endif
#include <assert.h>
#include <pthread.h>
#include <synch.h>
#include <unistd.h>
#include <security/cryptoki.h>
#include <stdio.h>
#include <cryptoutil.h>
#include <pkcs11Session.h>
#include <pkcs11Slot.h>
#include <sys/crypto/ioctl.h>
/*
* In "generic_attr_t", attributes that are not CK_BBOOL and
* CK_ULONG, the data will be stored in generic_data.
* Currently, 16 bytes will be pre-allocated for this.
* This is just a _WILD_ guess. If actual
* experience shows that 16 bytes is too small for most of the
* data that will be stored here, and cause this
* memory to be reallocated all the time, this should be increased.
*/
#define INITIAL_ATTR_LEN 16
/* We provide one slot, with the following arbitrary identifier. */
#define METASLOT_SLOTID 42
/* Metaslot is always the first slot in the framdwork, with slotID=0 */
#define METASLOT_FRAMEWORK_ID 0
/*
* These are the 2 acceptable string values for ${METASLOT_ENABLE} and
* ${METASLOT_AUTO_KEY_MIGRATE} environment variable
*/
#define TRUE_STRING "true"
#define FALSE_STRING "false"
/* Magic values for different data structures */
#define METASLOT_SESSION_MAGIC 0xECF00004
#define METASLOT_SESSION_BADMAGIC 0xBAD00004
#define METASLOT_OBJECT_MAGIC 0xECF0B004
#define METASLOT_OBJECT_BADMAGIC 0xBAD0B004
#define METASLOT_OPSTATE_MAGIC 0xECF09004
#define METASLOT_OPSTATE_BADMAGIC 0xBAD09004
#define IS_READ_ONLY_SESSION(session_flag) \
(!(session_flag & CKF_RW_SESSION))
/*
* Operation modes passed to meta_do_operation()
* MODE_UPDATE_WITHKEY is only used for C_DigestKey.
*/
#define MODE_SINGLE 0x0100
#define MODE_UPDATE 0x0200
#define MODE_UPDATE_WITHKEY 0x0400
#define MODE_FINAL 0x1000
/* CK_INFO: Information about cryptoki */
#define METASLOT_CRYPTOKI_VERSION_MAJOR 2
#define METASLOT_CRYPTOKI_VERSION_MINOR 40
#define METASLOT_MANUFACTURER_ID "Sun Microsystems, Inc. "
#define METASLOT_LIBRARY_DESCRIPTION "Sun Metaslot "
#define METASLOT_LIBRARY_VERSION_MAJOR 1
#define METASLOT_LIBRARY_VERSION_MINOR 1
/* CK_SLOT_INFO */
#define METASLOT_SLOT_DESCRIPTION "Sun Metaslot " \
" "
#define METASLOT_HARDWARE_VERSION_MAJOR 0
#define METASLOT_HARDWARE_VERSION_MINOR 0
#define METASLOT_FIRMWARE_VERSION_MAJOR 0
#define METASLOT_FIRMWARE_VERSION_MINOR 0
/* CK_TOKEN_INFO: More information about token */
#define METASLOT_TOKEN_LABEL "Sun Metaslot "
#define METASLOT_TOKEN_MODEL "1.0 "
/*
* Maximum number of objects and sessions to queue up before actually
* freeing them using the free() system. This is necessary to workaround
* a problem in which applications re-uses handles that are no longer valid
*/
#define MAX_OBJ_TO_BE_FREED 300
#define MAX_SESSION_TO_BE_FREED 300
/*
* The following 2 functions deals with inserting and deleting
* from double linked lists. It can work with any data structure
* that have "prev" and "next" defined.
*/
/* This always inserts into the head of the list */
#define INSERT_INTO_LIST(list, item) \
{ \
if ((list) == NULL) { \
(item)->prev = NULL; \
(item)->next = NULL; \
(list) = (item); \
} else { \
(item)->next = (list); \
(item)->prev = NULL; \
(list)->prev = (item); \
(list) = (item); \
} \
}
/*
* Remove item from list
*/
#define REMOVE_FROM_LIST(list, item) \
{ \
/* item is at the beginning of the list */ \
if ((list) == item) { \
if ((item)->next == NULL) { \
(list) = NULL; \
} else { \
(item)->next->prev = NULL; \
(list) = (item)->next; \
} \
} else { \
/* \
* let the items which are initialized and not \
* connected to the list trip over the asserts \
*/ \
if ((item)->next) { \
(item)->next->prev = item->prev; \
assert((item)->prev != NULL); \
(item)->prev->next = (item)->next; \
} else { \
assert((item)->prev != NULL); \
(item)->prev->next = NULL; \
} \
} \
}
/*
* OBJRELEASE
*
* Signal that a metaobject is no longer in use (but is still valid).
*/
#define OBJRELEASE(object) \
if (object != NULL) { \
(void) pthread_rwlock_unlock(&object->object_lock); \
}
/*
* REFRELEASE
*
* Signal that a metasession is no longer in use (but is still valid).
*
*/
#define REFRELEASE(session) \
if (session != NULL) { \
(void) pthread_rwlock_unlock(&session->session_lock); \
}
/* FreeObject/FreeToken Enumeration */
typedef enum {
FREE_UNCHECKED = 0, /* Has not been checked */
FREE_DISABLED = 1, /* No supported provider or key type */
FREE_ALLOWED_KEY = 2, /* Supported key type */
FREE_ENABLED = 3 /* FreeObject/Token enabled */
} freeobject_state_t;
/* Generic attribute type, for storing and managing PKCS#11 attributes. */
typedef struct _attr {
CK_ATTRIBUTE attribute;
boolean_t isMalloced;
/* attr is necessary for creating a clone of the object */
boolean_t isCloneAttr;
/*
* depends on the PKCS#11 implementation, this attr might or might
* not have a value. It's OK for it to not have a value
* (ie: the default value is empty)
*/
boolean_t canBeEmptyValue;
boolean_t hasValueForClone;
CK_BBOOL generic_bbool;
CK_ULONG generic_ulong;
CK_BYTE generic_data[INITIAL_ATTR_LEN];
} generic_attr_t;
/*
* These need to be defined here before the actual structures are defined
* because they are used in some of the structure definitions.
*/
typedef struct slotobject slot_object_t;
typedef struct metasession meta_session_t;
typedef struct metaobject meta_object_t;
typedef struct metaopstate meta_opstate_t;
/*
* slot_session_t
*
* Wrapper for a session on a provider. This structure is only used internally
* in metaslot; it is never revealed to applications.
*/
typedef struct slotsession {
CK_ULONG slotnum;
CK_SLOT_ID fw_st_id; /* used for accessing framework's slottable */
CK_SESSION_HANDLE hSession;
boolean_t is_dualop_capable;
CK_FLAGS session_flags; /* what type of session */
struct slotsession *next;
struct slotsession *prev;
pthread_rwlock_t object_list_lock;
slot_object_t *object_list_head;
} slot_session_t;
/*
* slot_object_t
*
* Wrapper for an object on a provider. This structure is only used internally
* in metaslot; it is never revealed to applications.
*/
struct slotobject {
CK_OBJECT_HANDLE hObject;
struct slotobject *next;
struct slotobject *prev;
slot_session_t *creator_session;
boolean_t isToken;
};
/*
* mechinfo_t
*
* A mechinfo_t is created for each mechanism on a slot.
*
* This information is used for selecting which slots support the given
* mechanism for a crypto operation.
*
*/
typedef struct mechinfo {
CK_ULONG slotnum;
boolean_t initialized;
boolean_t supported;
CK_MECHANISM_INFO mechanism_info;
} mechinfo_t;
/*
* operation_info_t
*
* Part of a meta_session_t, used to track active operations.
*/
typedef struct opinfo {
CK_FLAGS type;
slot_session_t *session;
mechinfo_t *stats;
} operation_info_t;
typedef struct find_objs_info {
boolean_t op_active; /* Indicate whether FindObjects is active */
meta_object_t **matched_objs;
int num_matched_objs;
int next_result_index; /* index of next object to be returned */
} find_objs_info_t;
typedef struct mech_support_info {
CK_MECHANISM_TYPE mech;
/* Array of mechinfo_t allocated based on number of slots */
mechinfo_t **supporting_slots;
unsigned long num_supporting_slots;
} mech_support_info_t;
typedef struct crypto_init {
CK_FLAGS optype; /* place holder for init parameters */
struct metasession *session; /* place holder for init parameters */
CK_MECHANISM *pMech; /* place holder for init parameters */
struct metaobject *key; /* place holder for init parameters */
CK_ULONG slotnum; /* slot where the init operation took place */
boolean_t done; /* set when the real init is done */
boolean_t app; /* set when C_xxxInit is called by app */
} crypto_init_t;
/*
* meta_session_t
*
* The internal state for a meta-session is kept here. The session handles
* given to applications are always pointers to a structure of this type.
*
*/
struct metasession {
ulong_t magic_marker;
pthread_rwlock_t session_lock;
pthread_mutex_t isClosingSession_lock;
boolean_t isClosingSession;
struct metasession *next;
struct metasession *prev;
CK_FLAGS session_flags;
/*
* Could have just declared this as "op", but declaring it as
* op1 so that "op2" can be easily added when dual-op support
* is implemented in the future
*/
operation_info_t op1;
/*
* This is for keeping track of which slots support a particular
* mechanism. This information doesn't
* have to be kept on a per session bases, but having the
* memory pre-allocated per session would make things much simpiler,
* because memory doesn't need to be allocated/deallocated everytime
* we do an operation.
*/
mech_support_info_t mech_support_info;
/* Session objects created by this session. */
pthread_rwlock_t object_list_lock;
meta_object_t *object_list_head;
/* C_FindObjects support. */
find_objs_info_t find_objs_info;
/* deferred init to be used by digest, encrypt, decrypt */
crypto_init_t init;
};
/*
* meta_object_t
*
* The internal state for a meta-object is kept here. The object handles
* given to applications are always pointers to a structure of this type.
*/
struct metaobject {
ulong_t magic_marker;
pthread_rwlock_t object_lock;
pthread_mutex_t isClosingObject_lock;
boolean_t isClosingObject;
struct metaobject *next;
struct metaobject *prev;
meta_session_t *creator_session; /* Only set for session objects */
boolean_t isToken; /* alias for CKA_TOKEN */
boolean_t isPrivate; /* alias for CKA_PRIVATE */
boolean_t isSensitive; /* alias for CKA_SENSITIVE */
boolean_t isExtractable; /* alias for CKA_EXTRACTABLE */
freeobject_state_t isFreeToken;
freeobject_state_t isFreeObject;
CK_ULONG master_clone_slotnum; /* set when object is created */
slot_object_t **clones;
/* indicate if tried to create clone object in a slot */
boolean_t *tried_create_clone;
pthread_rwlock_t attribute_lock;
size_t num_attributes;
generic_attr_t *attributes;
pthread_mutex_t clone_create_lock;
size_t clone_template_size; /* 0 if not yet known. */
CK_ATTRIBUTE *clone_template; /* NULL if not yet known. */
};
/*
* struct metaopstate
*
* Used as the format for the operation state returned via
* C_GetOperationState.
*/
typedef struct opstate_data {
CK_FLAGS op_type;
CK_ULONG op_slotnum;
CK_ULONG op_state_len;
boolean_t op_init_app;
boolean_t op_init_done;
} opstate_data_t;
struct metaopstate {
ulong_t magic_marker;
/*
* Could have just declared this as "state", but declaring it like this
* so that when dual-op support is implemented in the future, the
* changes will be simplier.
*/
struct opstate_data state[1];
};
/*
* session_pool_t
*
* Used to cache open sessions in a slot.
*/
typedef struct sessionpool {
pthread_mutex_t list_lock;
/* list of sessions that's currently in use */
slot_session_t *active_list_head;
/*
* list of sessions that are not in use, but can't be deleted because
* either session/token objects are created using these sessions
* or we need to have one session left with the provider to maintain
* the logged in state. Any of these sessions could be re-used if
* a session is needed to be established with a provider.
*/
slot_session_t *persist_list_head;
/*
* List of sessions that are not in use at the moment. We keep
* a list of sessions with a particular provider instead of
* creating a new session everytime for efficiency
*/
slot_session_t *idle_list_head;
boolean_t keep_one_alive;
int num_idle_sessions; /* number of sessions in "idle_list_head" */
} session_pool_t;
/*
* slot_data_t
*
* Each slot has a session pool, a collection of persistant sessions to
* allow for more efficient operation. Specifically, to allow reuse of
* previously session objects (which need the creating session to stick
* around), as well as being frugal with creating/closing sessions.
*/
typedef struct slotdata {
CK_SLOT_ID fw_st_id; /* framework slot table ID */
session_pool_t session_pool;
pthread_rwlock_t tokenobject_list_lock;
slot_object_t *tokenobject_list_head;
} slot_data_t;
typedef enum {
ALL_TOKEN = 0,
PUBLIC_TOKEN = 1,
PRIVATE_TOKEN = 2
} token_obj_type_t;
/*
* metaslot_config_t
*
* This holds the configuration information for meta slot.
* It will first be filled with values that users defined
* in environment variables. Any value not defined by the user
* will be filled with values from the system wide configuration file.
*/
typedef struct _metaslot_config {
/* token to be used as the keystore for metaslot */
boolean_t keystore_token_specified;
CK_UTF8CHAR keystore_token[TOKEN_LABEL_SIZE + 1];
/* slot to be used as the keystore for metaslot */
boolean_t keystore_slot_specified;
CK_UTF8CHAR keystore_slot[SLOT_DESCRIPTION_SIZE + 1];
/* should meta slot be enabled or not */
boolean_t enabled_specified;
boolean_t enabled;
/* should auto migration of sensitive token objects be enabled or not */
boolean_t auto_key_migrate_specified;
boolean_t auto_key_migrate;
} metaslot_config_t;
/*
* The following 2 structures are used to link the to-be-freed
* meta sessions and meta objects into linked lists.
* The items on these linked list have not yet been freed via free(); instead
* they are added to this list. The actual free will take place when
* the number of objects queued reaches MAX_OBJ_TO_BE_FREED or
* MAX_SESSION_TO_BE_FREED, at which time the first object in the
* list will be freed.
*/
typedef struct obj_to_be_freed_list {
meta_object_t *first; /* points to first obj in the list */
meta_object_t *last; /* points to last obj in the list */
uint32_t count; /* current total objs in the list */
pthread_mutex_t obj_to_be_free_mutex;
} object_to_be_freed_list_t;
typedef struct ses_to_be_freed_list {
meta_session_t *first; /* points to first session in the list */
meta_session_t *last; /* points to last session in the list */
uint32_t count; /* current total session in the list */
pthread_mutex_t ses_to_be_free_mutex;
} ses_to_be_freed_list_t;
typedef struct cipher_mechs_threshold {
int mech_type;
uint32_t mech_threshold;
} cipher_mechs_threshold_t;
/* Global variables */
extern metaslot_config_t metaslot_config;
extern boolean_t metaslot_enabled;
extern CK_SLOT_ID metaslot_keystore_slotid;
extern boolean_t metaslot_auto_key_migrate;
extern struct CK_FUNCTION_LIST metaslot_functionList;
extern pthread_mutex_t initmutex;
extern ses_to_be_freed_list_t ses_delay_freed;
extern object_to_be_freed_list_t obj_delay_freed;
extern void (*Tmp_GetThreshold)(void *);
extern CK_BBOOL falsevalue;
extern CK_BBOOL truevalue;
/* --- Prototypes --- */
CK_RV meta_slotManager_initialize();
void meta_slotManager_finalize();
void meta_slotManager_find_object_token();
CK_RV meta_get_slot_session(CK_ULONG slotnum, slot_session_t **session,
CK_FLAGS flags);
void meta_release_slot_session(slot_session_t *session);
CK_RV meta_mechManager_initialize();
void meta_mechManager_finalize();
CK_RV meta_mechManager_get_mechs(CK_MECHANISM_TYPE *list, CK_ULONG *listsize);
CK_RV meta_mechManager_get_slots(mech_support_info_t *mech_support_info,
boolean_t force_update, CK_MECHANISM_INFO *mech_info);
CK_RV meta_mechManager_slot_supports_mech(CK_MECHANISM_TYPE mechanism,
CK_ULONG slotnum, boolean_t *supports, mechinfo_t **slot_info,
boolean_t force_update, CK_MECHANISM_INFO *mech_info);
CK_RV meta_operation_init(CK_FLAGS optype, meta_session_t *session,
CK_MECHANISM *pMechanism, meta_object_t *key);
CK_RV meta_operation_init_defer(CK_FLAGS optype, meta_session_t *session,
CK_MECHANISM *pMechanism, meta_object_t *key);
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);
void meta_operation_cleanup(meta_session_t *session, CK_FLAGS optype,
boolean_t finished_normally);
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 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 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 meta_derive_key(meta_session_t *session, CK_MECHANISM *pMech,
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);
void get_user_metaslot_config();
CK_RV meta_sessionManager_initialize();
void meta_sessionManager_finalize();
CK_RV meta_handle2session(CK_SESSION_HANDLE hSession,
meta_session_t **session_p);
CK_RV meta_session_alloc(meta_session_t **newSession);
CK_RV meta_session_activate(meta_session_t *session);
CK_RV meta_session_deactivate(meta_session_t *session,
boolean_t have_sessionlist_lock);
void meta_session_dealloc(meta_session_t *session);
void meta_session_delay_free(meta_session_t *sp);
CK_RV meta_objectManager_initialize();
void meta_objectManager_finalize();
CK_RV meta_handle2object(CK_OBJECT_HANDLE hObject, meta_object_t **object);
CK_RV meta_object_alloc(meta_session_t *session, meta_object_t **object);
CK_RV meta_object_get_attr(slot_session_t *slot_session,
CK_OBJECT_HANDLE hObject, meta_object_t *object);
void meta_object_activate(meta_object_t *object);
CK_RV meta_object_deactivate(meta_object_t *object, boolean_t have_list_lock,
boolean_t have_object_lock);
CK_RV meta_object_dealloc(meta_session_t *session, meta_object_t *object,
boolean_t nukeSourceObj);
CK_RV meta_slot_object_alloc(slot_object_t **object);
void meta_slot_object_activate(slot_object_t *object, slot_session_t *session,
boolean_t isToken);
void meta_slot_object_deactivate(slot_object_t *object);
void meta_slot_object_dealloc(slot_object_t *object);
CK_RV meta_object_copyin(meta_object_t *object);
CK_RV meta_object_get_clone(meta_object_t *object,
CK_ULONG slot_num, slot_session_t *slot_session,
slot_object_t **clone);
meta_object_t *meta_object_find_by_handle(CK_OBJECT_HANDLE hObject,
CK_ULONG slotnum, boolean_t token_only);
CK_RV meta_token_object_deactivate(token_obj_type_t token_type);
void meta_object_delay_free(meta_object_t *objp);
boolean_t meta_freeobject_set(meta_object_t *object, CK_ATTRIBUTE *tmpl,
CK_ULONG tmpl_len, boolean_t create);
CK_RV meta_freetoken_set(CK_ULONG slot_num, CK_BBOOL *current_value,
CK_ATTRIBUTE *tmpl, CK_ULONG tmpl_len);
boolean_t meta_freeobject_check(meta_session_t *session, meta_object_t *obj,
CK_MECHANISM *pMech, CK_ATTRIBUTE *tmpl, CK_ULONG tmpl_len,
CK_KEY_TYPE keytype);
boolean_t meta_freeobject_clone(meta_session_t *session, meta_object_t *object);
CK_RV get_master_attributes_by_object(slot_session_t *session,
slot_object_t *slot_object, generic_attr_t **attributes,
size_t *num_attributes);
CK_RV get_master_attributes_by_template(
CK_ATTRIBUTE *template, CK_ULONG template_size,
generic_attr_t **attributes, size_t *num_attributes);
CK_RV get_master_template_by_type(CK_OBJECT_CLASS class, CK_ULONG subtype,
generic_attr_t **attributes, size_t *num_attributes);
CK_RV get_master_attributes_by_type(CK_OBJECT_CLASS class, CK_ULONG subtype,
generic_attr_t **attributes, size_t *num_attributes);
CK_RV get_master_attributes_by_duplication(
generic_attr_t *src_attrs, size_t num_src_attrs,
generic_attr_t **dst_attrs, size_t *num_dst_attrs);
void dealloc_attributes(generic_attr_t *attributes, size_t num_attributes);
CK_RV attribute_set_value(CK_ATTRIBUTE *new_attr,
generic_attr_t *attributes, size_t num_attributes);
boolean_t get_template_ulong(CK_ATTRIBUTE_TYPE type, CK_ATTRIBUTE *attributes,
CK_ULONG num_attributes, CK_ULONG *result);
boolean_t get_template_boolean(CK_ATTRIBUTE_TYPE type,
CK_ATTRIBUTE *attributes, CK_ULONG num_attributes, boolean_t *result);
int set_template_boolean(CK_ATTRIBUTE_TYPE type,
CK_ATTRIBUTE *attributes, CK_ULONG num_attributes, boolean_t local,
CK_BBOOL *value);
CK_ULONG get_keystore_slotnum(void);
CK_ULONG get_softtoken_slotnum(void);
CK_SLOT_ID meta_slotManager_get_framework_table_id(CK_ULONG slotnum);
CK_ULONG meta_slotManager_get_slotcount(void);
boolean_t meta_slotManager_token_write_protected(void);
boolean_t metaslot_logged_in();
void metaslot_set_logged_in_flag(boolean_t value);
/*
* Prototypes for the various meta_Foo implementations of C_Foo.
*
*/
CK_RV meta_GetFunctionList(CK_FUNCTION_LIST_PTR_PTR ppFunctionList);
CK_RV meta_Initialize(CK_VOID_PTR pInitArgs);
CK_RV meta_Finalize(CK_VOID_PTR pReserved);
CK_RV meta_GetInfo(CK_INFO_PTR pInfo);
CK_RV meta_GetSlotList(CK_BBOOL tokenPresent, CK_SLOT_ID_PTR pSlotList,
CK_ULONG_PTR pulCount);
CK_RV meta_GetSlotInfo(CK_SLOT_ID slotID, CK_SLOT_INFO_PTR pInfo);
CK_RV meta_GetTokenInfo(CK_SLOT_ID slotID, CK_TOKEN_INFO_PTR pInfo);
CK_RV meta_GetMechanismList(CK_SLOT_ID slotID,
CK_MECHANISM_TYPE_PTR pMechanismList, CK_ULONG_PTR pulCount);
CK_RV meta_GetMechanismInfo(CK_SLOT_ID slotID, CK_MECHANISM_TYPE type,
CK_MECHANISM_INFO_PTR pInfo);
CK_RV meta_InitToken(CK_SLOT_ID slotID, CK_UTF8CHAR_PTR pPin,
CK_ULONG ulPinLen, CK_UTF8CHAR_PTR pLabel);
CK_RV meta_InitPIN(CK_SESSION_HANDLE hSession, CK_UTF8CHAR_PTR pPin,
CK_ULONG ulPinLen);
CK_RV meta_SetPIN(CK_SESSION_HANDLE hSession, CK_UTF8CHAR_PTR pOldPin,
CK_ULONG ulOldPinLen, CK_UTF8CHAR_PTR pNewPin, CK_ULONG ulNewPinLen);
CK_RV meta_OpenSession(CK_SLOT_ID slotID, CK_FLAGS flags,
CK_VOID_PTR pApplication, CK_NOTIFY Notify,
CK_SESSION_HANDLE_PTR phSession);
CK_RV meta_CloseSession(CK_SESSION_HANDLE hSession);
CK_RV meta_CloseAllSessions(CK_SLOT_ID slotID);
CK_RV meta_GetSessionInfo(CK_SESSION_HANDLE hSession,
CK_SESSION_INFO_PTR pInfo);
CK_RV meta_GetOperationState(CK_SESSION_HANDLE hSession,
CK_BYTE_PTR pOperationState, CK_ULONG_PTR pulOperationStateLen);
CK_RV meta_SetOperationState(CK_SESSION_HANDLE hSession,
CK_BYTE_PTR pOperationState, CK_ULONG ulOperationStateLen,
CK_OBJECT_HANDLE hEncryptionKey, CK_OBJECT_HANDLE hAuthenticationKey);
CK_RV meta_Login(CK_SESSION_HANDLE hSession, CK_USER_TYPE userType,
CK_UTF8CHAR_PTR pPin, CK_ULONG ulPinLen);
CK_RV meta_Logout(CK_SESSION_HANDLE hSession);
CK_RV meta_CreateObject(CK_SESSION_HANDLE hSession, CK_ATTRIBUTE_PTR pTemplate,
CK_ULONG ulCount, CK_OBJECT_HANDLE_PTR phObject);
CK_RV meta_CopyObject(CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE hObject,
CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount,
CK_OBJECT_HANDLE_PTR phNewObject);
CK_RV meta_DestroyObject(CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE hObject);
CK_RV meta_GetObjectSize(CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE hObject,
CK_ULONG_PTR pulSize);
CK_RV meta_GetAttributeValue(CK_SESSION_HANDLE hSession,
CK_OBJECT_HANDLE hObject, CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount);
CK_RV meta_SetAttributeValue(CK_SESSION_HANDLE hSession,
CK_OBJECT_HANDLE hObject, CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount);
CK_RV meta_FindObjectsInit(CK_SESSION_HANDLE hSession,
CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount);
CK_RV meta_FindObjects(CK_SESSION_HANDLE hSession,
CK_OBJECT_HANDLE_PTR phObject, CK_ULONG ulMaxObjectCount,
CK_ULONG_PTR pulObjectCount);
CK_RV meta_FindObjectsFinal(CK_SESSION_HANDLE hSession);
CK_RV meta_EncryptInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism,
CK_OBJECT_HANDLE hKey);
CK_RV meta_Encrypt(CK_SESSION_HANDLE hSession,
CK_BYTE_PTR pData, CK_ULONG ulDataLen,
CK_BYTE_PTR pEncryptedData, CK_ULONG_PTR pulEncryptedDataLen);
CK_RV meta_EncryptUpdate(CK_SESSION_HANDLE hSession,
CK_BYTE_PTR pPart, CK_ULONG ulPartLen,
CK_BYTE_PTR pEncryptedPart, CK_ULONG_PTR pulEncryptedPartLen);
CK_RV meta_EncryptFinal(CK_SESSION_HANDLE hSession,
CK_BYTE_PTR pLastEncryptedPart, CK_ULONG_PTR pulLastEncryptedPartLen);
CK_RV meta_DecryptInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism,
CK_OBJECT_HANDLE hKey);
CK_RV meta_Decrypt(CK_SESSION_HANDLE hSession,
CK_BYTE_PTR pEncryptedData, CK_ULONG ulEncryptedDataLen,
CK_BYTE_PTR pData, CK_ULONG_PTR pulDataLen);
CK_RV meta_DecryptUpdate(CK_SESSION_HANDLE hSession,
CK_BYTE_PTR pEncryptedPart, CK_ULONG ulEncryptedPartLen,
CK_BYTE_PTR pPart, CK_ULONG_PTR pulPartLen);
CK_RV meta_DecryptFinal(CK_SESSION_HANDLE hSession,
CK_BYTE_PTR pLastPart, CK_ULONG_PTR pulLastPartLen);
CK_RV meta_DigestInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism);
CK_RV meta_Digest(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pData,
CK_ULONG ulDataLen, CK_BYTE_PTR pDigest, CK_ULONG_PTR pulDigestLen);
CK_RV meta_DigestUpdate(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pPart,
CK_ULONG ulPartLen);
CK_RV meta_DigestKey(CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE hKey);
CK_RV meta_DigestFinal(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pDigest,
CK_ULONG_PTR pulDigestLen);
CK_RV meta_SignInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism,
CK_OBJECT_HANDLE hKey);
CK_RV meta_Sign(CK_SESSION_HANDLE hSession,
CK_BYTE_PTR pData, CK_ULONG ulDataLen,
CK_BYTE_PTR pSignature, CK_ULONG_PTR pulSignatureLen);
CK_RV meta_SignUpdate(CK_SESSION_HANDLE hSession,
CK_BYTE_PTR pPart, CK_ULONG ulPartLen);
CK_RV meta_SignFinal(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pSignature,
CK_ULONG_PTR pulSignatureLen);
CK_RV meta_SignRecoverInit(CK_SESSION_HANDLE hSession,
CK_MECHANISM_PTR pMechanism, CK_OBJECT_HANDLE hKey);
CK_RV meta_SignRecover(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pData,
CK_ULONG ulDataLen, CK_BYTE_PTR pSignature, CK_ULONG_PTR pulSignatureLen);
CK_RV meta_VerifyInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism,
CK_OBJECT_HANDLE hKey);
CK_RV meta_Verify(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pData,
CK_ULONG ulDataLen, CK_BYTE_PTR pSignature, CK_ULONG ulSignatureLen);
CK_RV meta_VerifyUpdate(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pPart,
CK_ULONG ulPartLen);
CK_RV meta_VerifyFinal(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pSignature,
CK_ULONG ulSignatureLen);
CK_RV meta_VerifyRecoverInit(CK_SESSION_HANDLE hSession,
CK_MECHANISM_PTR pMechanism, CK_OBJECT_HANDLE hKey);
CK_RV meta_VerifyRecover(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pSignature,
CK_ULONG ulSignatureLen, CK_BYTE_PTR pData, CK_ULONG_PTR pulDataLen);
CK_RV meta_DigestEncryptUpdate(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pPart,
CK_ULONG ulPartLen, CK_BYTE_PTR pEncryptedPart,
CK_ULONG_PTR pulEncryptedPartLen);
CK_RV meta_DecryptDigestUpdate(CK_SESSION_HANDLE hSession,
CK_BYTE_PTR pEncryptedPart, CK_ULONG ulEncryptedPartLen,
CK_BYTE_PTR pPart, CK_ULONG_PTR pulPartLen);
CK_RV meta_SignEncryptUpdate(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pPart,
CK_ULONG ulPartLen, CK_BYTE_PTR pEncryptedPart,
CK_ULONG_PTR pulEncryptedPartLen);
CK_RV meta_DecryptVerifyUpdate(CK_SESSION_HANDLE hSession,
CK_BYTE_PTR pEncryptedPart, CK_ULONG ulEncryptedPartLen,
CK_BYTE_PTR pPart, CK_ULONG_PTR pulPartLen);
CK_RV meta_GenerateKey(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism,
CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount, CK_OBJECT_HANDLE_PTR phKey);
CK_RV meta_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);
CK_RV meta_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);
CK_RV meta_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 ulAttributeCount, CK_OBJECT_HANDLE_PTR phKey);
CK_RV meta_DeriveKey(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism,
CK_OBJECT_HANDLE hBaseKey, CK_ATTRIBUTE_PTR pTemplate,
CK_ULONG ulAttributeCount, CK_OBJECT_HANDLE_PTR phKey);
CK_RV meta_SeedRandom(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pSeed,
CK_ULONG ulSeedLen);
CK_RV meta_GenerateRandom(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pRandomData,
CK_ULONG ulRandomLen);
CK_RV meta_GetFunctionStatus(CK_SESSION_HANDLE hSession);
CK_RV meta_CancelFunction(CK_SESSION_HANDLE hSession);
CK_RV meta_WaitForSlotEvent(CK_FLAGS flags, CK_SLOT_ID_PTR pSlot,
CK_VOID_PTR pReserved);
#ifdef __cplusplus
}
#endif
#endif /* _METAGLOBAL_H */
/*
* 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 2010 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* Key Management Functions
* (as defined in PKCS#11 spec section 11.14)
*/
#include "metaGlobal.h"
/*
* meta_GenerateKey
*
*/
CK_RV
meta_GenerateKey(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism,
CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount, CK_OBJECT_HANDLE_PTR phKey)
{
CK_RV rv;
meta_session_t *session;
meta_object_t *key = NULL;
if (pMechanism == NULL || phKey == NULL)
return (CKR_ARGUMENTS_BAD);
rv = meta_handle2session(hSession, &session);
if (rv != CKR_OK)
return (rv);
rv = meta_object_alloc(session, &key);
if (rv != CKR_OK)
goto finish;
rv = meta_generate_keys(session, pMechanism, pTemplate, ulCount, key,
NULL, 0, NULL);
if (rv != CKR_OK)
goto finish;
meta_object_activate(key);
*phKey = (CK_OBJECT_HANDLE) key;
finish:
if (rv != CKR_OK) {
if (key)
(void) meta_object_dealloc(session, key, B_TRUE);
}
REFRELEASE(session);
return (rv);
}
/*
* meta_GenerateKeyPair
*
*/
CK_RV
meta_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)
{
CK_RV rv;
meta_session_t *session;
meta_object_t *key1 = NULL, *key2 = NULL;
if (pMechanism == NULL || phPublicKey == NULL || phPrivateKey == NULL)
return (CKR_ARGUMENTS_BAD);
rv = meta_handle2session(hSession, &session);
if (rv != CKR_OK)
return (rv);
rv = meta_object_alloc(session, &key1);
if (rv != CKR_OK)
goto finish;
rv = meta_object_alloc(session, &key2);
if (rv != CKR_OK)
goto finish;
rv = meta_generate_keys(session, pMechanism,
pPublicKeyTemplate, ulPublicKeyAttributeCount, key1,
pPrivateKeyTemplate, ulPrivateKeyAttributeCount, key2);
if (rv != CKR_OK)
goto finish;
meta_object_activate(key1);
meta_object_activate(key2);
*phPublicKey = (CK_OBJECT_HANDLE) key1;
*phPrivateKey = (CK_OBJECT_HANDLE) key2;
finish:
if (rv != CKR_OK) {
if (key1)
(void) meta_object_dealloc(session, key1, B_TRUE);
if (key2)
(void) meta_object_dealloc(session, key2, B_TRUE);
}
REFRELEASE(session);
return (rv);
}
/*
* meta_WrapKey
*
*/
CK_RV
meta_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)
{
CK_RV rv;
meta_session_t *session;
meta_object_t *wrappingKey, *inputKey;
if (pMechanism == NULL || pulWrappedKeyLen == NULL)
return (CKR_ARGUMENTS_BAD);
rv = meta_handle2session(hSession, &session);
if (rv != CKR_OK)
return (rv);
rv = meta_handle2object(hKey, &inputKey);
if (rv != CKR_OK) {
REFRELEASE(session);
return (rv);
}
rv = meta_handle2object(hWrappingKey, &wrappingKey);
if (rv != CKR_OK) {
OBJRELEASE(inputKey);
REFRELEASE(session);
return (rv);
}
rv = meta_wrap_key(session, pMechanism, wrappingKey,
inputKey, pWrappedKey, pulWrappedKeyLen);
OBJRELEASE(inputKey);
OBJRELEASE(wrappingKey);
REFRELEASE(session);
return (rv);
}
/*
* meta_UnwrapKey
*
*/
CK_RV
meta_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 ulAttributeCount, CK_OBJECT_HANDLE_PTR phKey)
{
CK_RV rv;
meta_session_t *session;
meta_object_t *unwrappingKey, *outputKey = NULL;
if (pMechanism == NULL || pWrappedKey == NULL || phKey == NULL)
return (CKR_ARGUMENTS_BAD);
rv = meta_handle2session(hSession, &session);
if (rv != CKR_OK)
return (rv);
rv = meta_handle2object(hUnwrappingKey, &unwrappingKey);
if (rv != CKR_OK) {
REFRELEASE(session);
return (rv);
}
rv = meta_object_alloc(session, &outputKey);
if (rv != CKR_OK)
goto finish;
(void) get_template_boolean(CKA_TOKEN, pTemplate, ulAttributeCount,
&(outputKey->isToken));
rv = meta_unwrap_key(session, pMechanism, unwrappingKey,
pWrappedKey, ulWrappedKeyLen,
pTemplate, ulAttributeCount, outputKey);
if (rv != CKR_OK)
goto finish;
meta_object_activate(outputKey);
*phKey = (CK_OBJECT_HANDLE) outputKey;
finish:
if (rv != CKR_OK) {
if (outputKey)
(void) meta_object_dealloc(session, outputKey, B_TRUE);
}
OBJRELEASE(unwrappingKey);
REFRELEASE(session);
return (rv);
}
/*
* meta_DeriveKey
*
* This function is a bit gross because of PKCS#11 kludges that pass extra
* object handles in some mechanism parameters. It probably needs to be
* broken up into more managable pieces.
*/
CK_RV
meta_DeriveKey(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism,
CK_OBJECT_HANDLE hBaseKey, CK_ATTRIBUTE_PTR pTemplate,
CK_ULONG ulAttributeCount, CK_OBJECT_HANDLE_PTR phKey)
{
CK_RV rv;
CK_MECHANISM *pMech = pMechanism;
meta_session_t *session;
meta_object_t *basekey1 = NULL, *basekey2 = NULL;
meta_object_t *newKey1 = NULL, *newKey2 = NULL, *newKey3 = NULL,
*newKey4 = NULL;
boolean_t ssl_keys = B_FALSE;
boolean_t tlsprf = B_FALSE;
CK_MECHANISM metaMech;
CK_OBJECT_HANDLE *phBaseKey2 = NULL;
CK_X9_42_DH2_DERIVE_PARAMS x942_params, *x9_tmpptr;
CK_ECDH2_DERIVE_PARAMS ecdh_params, *ec_tmpptr;
CK_SSL3_KEY_MAT_OUT *ssl_key_mat;
CK_SSL3_KEY_MAT_PARAMS *keyparams;
if (pMech == NULL) {
return (CKR_ARGUMENTS_BAD);
}
/*
* Special case: Normally, the caller must always provide storage
* for the derived key handle at phKey. Two (related) mechanisms
* are special, in that multiple keys are instead returned via
* pMech->pParameter. In these cases the spec says (see 12.38.4
* and 12.39.4) that phKey should be a NULL pointer, as it is not used.
*/
switch (pMech->mechanism) {
case CKM_SSL3_KEY_AND_MAC_DERIVE:
case CKM_TLS_KEY_AND_MAC_DERIVE:
keyparams = (CK_SSL3_KEY_MAT_PARAMS*)pMech->pParameter;
if ((keyparams == NULL) || (pMech->ulParameterLen
!= sizeof (CK_SSL3_KEY_MAT_PARAMS)))
return (CKR_ARGUMENTS_BAD);
ssl_key_mat = keyparams->pReturnedKeyMaterial;
if (ssl_key_mat == NULL)
return (CKR_ARGUMENTS_BAD);
ssl_keys = B_TRUE;
break;
case CKM_TLS_PRF:
tlsprf = B_TRUE;
break;
default:
if (phKey == NULL)
return (CKR_ARGUMENTS_BAD);
};
rv = meta_handle2session(hSession, &session);
if (rv != CKR_OK)
return (rv);
rv = meta_handle2object(hBaseKey, &basekey1);
if (rv != CKR_OK)
goto finish;
/*
* A few oddball mechanisms pass a 2nd object handle in the parameters.
* Here we validate that handle, and create a duplicate copy of the
* mechanism and parameters. This is done because the application
* does not expect these values to be changing, and could be using the
* same data in multiple threads (eg concurrent calls to C_DeriveKey).
* We copy the data to make sure there are no MT-Safe problems.
*/
switch (pMech->mechanism) {
case CKM_ECMQV_DERIVE:
/* uses CK_ECDH2_DERIVE_PARAMS struct as the parameter */
if ((pMech->pParameter == NULL) || (pMech->ulParameterLen
!= sizeof (CK_ECDH2_DERIVE_PARAMS))) {
rv = CKR_ARGUMENTS_BAD;
goto finish;
}
/* Duplicate the mechanism and paramaters */
ec_tmpptr = (CK_ECDH2_DERIVE_PARAMS *)pMech->pParameter;
ecdh_params = *ec_tmpptr;
metaMech = *pMech;
metaMech.pParameter = &ecdh_params;
pMech = &metaMech;
/* Get the key the application is providing */
phBaseKey2 = &ecdh_params.hPrivateData;
break;
case CKM_X9_42_DH_HYBRID_DERIVE:
case CKM_X9_42_MQV_DERIVE:
/* both use CK_X9_42_DH2_DERIVE_PARAMS as the parameter */
if ((pMech->pParameter == NULL) || (pMech->ulParameterLen
!= sizeof (CK_X9_42_DH2_DERIVE_PARAMS))) {
rv = CKR_ARGUMENTS_BAD;
goto finish;
}
/* Duplicate the mechanism and paramaters */
x9_tmpptr = (CK_X9_42_DH2_DERIVE_PARAMS *)pMech->pParameter;
x942_params = *x9_tmpptr;
metaMech = *pMech;
metaMech.pParameter = &x942_params;
pMech = &metaMech;
/* Get the key the application is providing */
phBaseKey2 = &x942_params.hPrivateData;
break;
case CKM_CONCATENATE_BASE_AND_KEY:
/* uses a CK_OBJECT_HANDLE as the parameter */
if ((pMech->pParameter == NULL) || (pMech->ulParameterLen
!= sizeof (CK_OBJECT_HANDLE))) {
rv = CKR_ARGUMENTS_BAD;
goto finish;
}
/* Duplicate the mechanism and paramaters */
metaMech = *pMech;
pMech = &metaMech;
/* Get the key the application is providing */
phBaseKey2 = (CK_OBJECT_HANDLE *) &metaMech.pParameter;
break;
default:
/* nothing special to do. */
break;
}
if (phBaseKey2) {
rv = meta_handle2object(*phBaseKey2, &basekey2);
if (rv != CKR_OK)
goto finish;
}
/*
* Allocate meta objects to store the derived key(s). Normally just
* a single key is created, but the SSL/TLS mechanisms generate four.
*/
rv = meta_object_alloc(session, &newKey1);
if (rv != CKR_OK)
goto finish;
if (ssl_keys) {
rv = meta_object_alloc(session, &newKey2);
if (rv != CKR_OK)
goto finish;
rv = meta_object_alloc(session, &newKey3);
if (rv != CKR_OK)
goto finish;
rv = meta_object_alloc(session, &newKey4);
if (rv != CKR_OK)
goto finish;
}
/* Perform the actual key derive operation. */
rv = meta_derive_key(session, pMech, basekey1, basekey2, phBaseKey2,
pTemplate, ulAttributeCount, newKey1, newKey2, newKey3, newKey4);
if (rv != CKR_OK)
goto finish;
if (tlsprf) {
(void) meta_object_dealloc(session, newKey1, B_TRUE);
newKey1 = NULL;
/* phKey isn't used (is NULL) for mechanism CKM_TLS_PRF. */
} else {
/* Make derived key(s) active and visible to other threads. */
meta_object_activate(newKey1);
if (ssl_keys) {
meta_object_activate(newKey2);
meta_object_activate(newKey3);
meta_object_activate(newKey4);
ssl_key_mat->hClientMacSecret
= (CK_OBJECT_HANDLE) newKey1;
ssl_key_mat->hServerMacSecret
= (CK_OBJECT_HANDLE) newKey2;
ssl_key_mat->hClientKey = (CK_OBJECT_HANDLE) newKey3;
ssl_key_mat->hServerKey = (CK_OBJECT_HANDLE) newKey4;
/* phKey isn't used (is NULL) for these SSL/TLS mechs */
} else {
*phKey = (CK_OBJECT_HANDLE) newKey1;
}
}
finish:
if (rv != CKR_OK) {
if (newKey1)
(void) meta_object_dealloc(session, newKey1, B_TRUE);
if (newKey2)
(void) meta_object_dealloc(session, newKey2, B_TRUE);
if (newKey3)
(void) meta_object_dealloc(session, newKey3, B_TRUE);
if (newKey4)
(void) meta_object_dealloc(session, newKey4, B_TRUE);
}
if (basekey1)
OBJRELEASE(basekey1);
if (basekey2)
OBJRELEASE(basekey2);
REFRELEASE(session);
return (rv);
}
/*
* 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 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* Mechanism Manager - centralized knowledge of mechanisms.
*
* The core of the mechmanager is the "mechlist" data structure. It contains
* information about all mechanisms available from providers that have been
* exposed to the application.
*
* Each element in the array represents a particular mechanism type. The
* array is sorted by type, so that searching by mechanism can be done
* quickly. Each element also contains the mechanism data for each slot.
*
* The mechlist is constructed on an as-needed basis, entries are not added
* until the application triggers an action that requires an entry to be
* added (or updated).
*
*/
#include <string.h>
#include <strings.h>
#include "pkcs11Conf.h"
#include "metaGlobal.h"
/* Global data... */
#define INITIAL_MECHLIST_SIZE 256
typedef struct mechliststruct {
CK_MECHANISM_TYPE type;
mechinfo_t *slots;
} mechlist_t;
static pthread_rwlock_t mechlist_lock = PTHREAD_RWLOCK_INITIALIZER;
static mechlist_t *mechlist;
static unsigned long num_mechs;
static unsigned long true_mechlist_size;
/* Prototypes... */
static CK_RV meta_mechManager_update_mech(CK_MECHANISM_TYPE, boolean_t);
static CK_RV meta_mechManager_update_slot(CK_ULONG);
static CK_RV update_slotmech(CK_MECHANISM_TYPE, CK_ULONG, unsigned long);
static CK_RV meta_mechManager_allocmechs(CK_MECHANISM_TYPE *, unsigned long,
unsigned long *);
static boolean_t find_mech_index(CK_MECHANISM_TYPE, unsigned long *);
static int qsort_mechtypes(const void *, const void *);
/*
* meta_mechManager_initialize
*
* Called from C_Initialize. Allocates and initializes storage needed
* by the slot manager.
*/
CK_RV
meta_mechManager_initialize()
{
/* The mechlist can dynamically grow, but let's preallocate space. */
mechlist = calloc(INITIAL_MECHLIST_SIZE, sizeof (mechlist_t));
if (mechlist == NULL)
return (CKR_HOST_MEMORY);
true_mechlist_size = INITIAL_MECHLIST_SIZE;
num_mechs = 0;
return (CKR_OK);
}
/*
* meta_mechManager_finalize
*
* Called from C_Finalize. Deallocates any storage held by the slot manager.
*/
void
meta_mechManager_finalize()
{
int i;
/* No need to lock list, we assume all sessions are closed. */
for (i = 0; i < num_mechs; i++) {
free(mechlist[i].slots);
}
free(mechlist);
mechlist = NULL;
num_mechs = 0;
true_mechlist_size = 0;
}
/*
* meta_mechManager_get_mechs
*
* Get list of all available mechanisms.
*
* Follows PKCS#11 semantics, where list may be NULL to only request a
* count of available mechanisms.
*/
CK_RV
meta_mechManager_get_mechs(CK_MECHANISM_TYPE *list, CK_ULONG *listsize)
{
CK_RV rv = CKR_OK;
CK_ULONG num_found = 0;
CK_ULONG slotnum, num_slots;
unsigned long i;
/* get number of slots */
num_slots = meta_slotManager_get_slotcount();
/*
* Update slot info. Ignore any errors.
*
* NOTE: Due to the PKCS#11 convention of calling C_GetMechanismList
* twice (once to get the count, again to get the actual list), this
* is somewhat inefficient... However, I don't see an easy way to fix
* that without impacting other cases (eg, when the first call contains
* an "optimistic" pre-allocated buffer).
*/
for (slotnum = 0; slotnum < num_slots; slotnum++) {
(void) meta_mechManager_update_slot(slotnum);
}
/*
* Count the number of mechanisms. We can't just use num_mechs,
* because some mechs may not currently be supported on any slot.
* Also, it may not be allowed based on the mechanism policy.
*/
(void) pthread_rwlock_rdlock(&mechlist_lock);
for (i = 0; i < num_mechs; i++) {
CK_ULONG j;
boolean_t supported;
if (pkcs11_is_dismech(METASLOT_FRAMEWORK_ID,
mechlist[i].type)) {
/* skip mechs disabled by policy */
continue;
}
supported = FALSE;
for (j = 0; j < num_slots; j++) {
if (!mechlist[i].slots[j].initialized)
continue;
if (mechlist[i].slots[j].supported) {
supported = B_TRUE;
break;
}
}
if (supported) {
num_found++;
if (list && *listsize >= num_found) {
list[num_found - 1] = mechlist[i].type;
}
}
}
(void) pthread_rwlock_unlock(&mechlist_lock);
if (num_found > *listsize)
rv = CKR_BUFFER_TOO_SMALL;
*listsize = num_found;
return (rv);
}
/*
* meta_mechManager_get_slots
*
* Get list of all slots supporting the specified mechanism.
*
* The "mech_support_info" argument should have allocated enough
* space to accomodate the list of slots that supports the
* specified mechanism. The "num_supporting_slots" field
* in the "mech_support_info" structure will indicate how
* many slots are found to support the mechanism.
*
* If any error occurred in getting the list, info in
* mech_support_info argument is not updated.
*
*/
CK_RV
meta_mechManager_get_slots(mech_support_info_t *mech_support_info,
boolean_t force_update, CK_MECHANISM_INFO *mech_info)
{
CK_RV rv;
boolean_t found;
CK_ULONG i, num_slots;
unsigned long index, num_found = 0;
CK_MECHANISM_INFO info;
rv = meta_mechManager_update_mech(mech_support_info->mech,
force_update);
if (rv != CKR_OK) {
return (rv);
}
(void) pthread_rwlock_rdlock(&mechlist_lock);
found = find_mech_index(mech_support_info->mech, &index);
if (!found) {
goto finish;
}
num_slots = meta_slotManager_get_slotcount();
for (i = 0; i < num_slots; i++) {
if (!mechlist[index].slots[i].initialized ||
!mechlist[index].slots[i].supported)
continue;
if (mech_info) {
info = mechlist[index].slots[i].mechanism_info;
if (!(info.flags & mech_info->flags)) {
continue;
}
}
num_found++;
(mech_support_info->supporting_slots)[num_found - 1]
= &mechlist[index].slots[i];
}
finish:
(void) pthread_rwlock_unlock(&mechlist_lock);
if (num_found == 0) {
rv = CKR_MECHANISM_INVALID;
} else {
mech_support_info->num_supporting_slots = num_found;
}
return (rv);
}
/*
* meta_mechManager_update_mech
*
* Updates a mechanism in the mechlist. If the mechanism is not
* listed, all providers will be queried. If the mechanism
* is present, but not initialized for some providers, those providers
* will be queried. Existing entries will not be updated unless the
* force_refresh flag is set.
*
* The force_refresh flag is used by C_GetMechanismInfo, to force an
* update. Updates are not forced during the common usage by operations
* [eg C_EncryptInit] to avoid poor performance.
*/
static CK_RV
meta_mechManager_update_mech(CK_MECHANISM_TYPE mech, boolean_t force_refresh)
{
CK_RV rv;
CK_ULONG slot, num_slots;
unsigned long index = 0;
boolean_t found;
/* Ensure list contains the mechanism. */
rv = meta_mechManager_allocmechs(&mech, 1, &index);
if (rv != CKR_OK)
return (rv);
(void) pthread_rwlock_wrlock(&mechlist_lock);
/*
* We didn't retain a lock after the first search, so it's possible
* that the mechlist was updated. Search again, but use the last
* index as a hint to quickly find the mechanism.
*/
found = find_mech_index(mech, &index);
if (!found) {
/* Shouldn't happen - entries are not removed from list. */
rv = CKR_GENERAL_ERROR;
goto finish;
}
num_slots = meta_slotManager_get_slotcount();
for (slot = 0; slot < num_slots; slot++) {
if (force_refresh || !mechlist[index].slots[slot].initialized) {
rv = update_slotmech(mech, slot, index);
if (rv != CKR_OK) {
/* Ignore error and continue with next slot. */
rv = CKR_OK;
}
}
}
finish:
(void) pthread_rwlock_unlock(&mechlist_lock);
return (rv);
}
/*
* meta_mechManager_update_slot
*
* Updates a slot in the mechlist. Called by C_GetMechanismList
* [by way of meta_mechManager_get_mechs()]. Unlike
* meta_mechManager_get_slots(), the context is always to force a refresh
* of the mechlist.
*
*/
static CK_RV
meta_mechManager_update_slot(CK_ULONG slotnum)
{
unsigned long index = 0;
CK_MECHANISM_TYPE *slot_mechlist = NULL, *tmp_slot_mechlist = NULL;
CK_ULONG slot_mechlistsize, mechnum, tmp_mechlistsize;
CK_RV rv;
boolean_t found;
CK_SLOT_ID fw_st_id, true_id;
int i;
fw_st_id = meta_slotManager_get_framework_table_id(slotnum);
true_id = TRUEID(fw_st_id);
/* First, get the count. */
rv = FUNCLIST(fw_st_id)->C_GetMechanismList(true_id, NULL,
&slot_mechlistsize);
if (rv != CKR_OK) {
goto finish;
}
tmp_slot_mechlist = malloc(
slot_mechlistsize * sizeof (CK_MECHANISM_TYPE));
if (tmp_slot_mechlist == NULL) {
rv = CKR_HOST_MEMORY;
goto finish;
}
/* Next, get the actual list. */
rv = FUNCLIST(fw_st_id)->C_GetMechanismList(true_id,
tmp_slot_mechlist, &slot_mechlistsize);
if (rv != CKR_OK) {
goto finish;
}
/*
* filter the list of mechanisms returned by the underlying slot
* to remove any mechanisms that are explicitly disabled
* in the configuration file.
*/
slot_mechlist = malloc(slot_mechlistsize * sizeof (CK_MECHANISM_TYPE));
if (slot_mechlist == NULL) {
rv = CKR_HOST_MEMORY;
goto finish;
}
tmp_mechlistsize = 0;
for (i = 0; i < slot_mechlistsize; i++) {
/* filter out the disabled mechanisms */
if (pkcs11_is_dismech(fw_st_id, tmp_slot_mechlist[i])) {
continue;
}
slot_mechlist[tmp_mechlistsize] = tmp_slot_mechlist[i];
tmp_mechlistsize++;
}
slot_mechlistsize = tmp_mechlistsize;
/* Sort the mechanisms by value. */
qsort(slot_mechlist, slot_mechlistsize, sizeof (CK_MECHANISM_TYPE),
qsort_mechtypes);
/* Ensure list contains the mechanisms. */
rv = meta_mechManager_allocmechs(slot_mechlist, slot_mechlistsize,
&index);
if (rv != CKR_OK)
goto finish;
/* Update the mechanism info. */
(void) pthread_rwlock_wrlock(&mechlist_lock);
for (mechnum = 0; mechnum < slot_mechlistsize; mechnum++) {
found = find_mech_index(slot_mechlist[mechnum], &index);
if (!found) {
/* This shouldn't happen. */
rv = CKR_GENERAL_ERROR;
goto finish;
}
rv = update_slotmech(slot_mechlist[mechnum], slotnum, index);
if (rv != CKR_OK) {
/* Ignore error, make best effort to finish update. */
rv = CKR_OK;
continue;
}
}
(void) pthread_rwlock_unlock(&mechlist_lock);
finish:
if (slot_mechlist) {
free(slot_mechlist);
}
if (tmp_slot_mechlist) {
free(tmp_slot_mechlist);
}
return (rv);
}
/*
* update_slotmech
*
* Updates the information for a particular mechanism for a particular slot.
* (ie, slotlist[foo].slots[bar])
*
* It is assumed that the caller to this function (all of which are
* in this file) holds the write-lock to "mechlist_lock".
*
*/
static CK_RV
update_slotmech(CK_MECHANISM_TYPE mech, CK_ULONG slotnum,
unsigned long index)
{
CK_RV rv = CKR_OK;
CK_MECHANISM_INFO info;
CK_SLOT_ID fw_st_id, true_id;
mechlist[index].slots[slotnum].slotnum = slotnum;
fw_st_id = meta_slotManager_get_framework_table_id(slotnum);
true_id = TRUEID(fw_st_id);
/*
* Check if the specified mechanism is in the disabled list
* of the specified slot. If so, we can immediately conclude
* that it is not supported by the specified slot.
*/
if (pkcs11_is_dismech(fw_st_id, mech)) {
/*
* we mark this as initialized so that we won't try
* to do this check later
*/
mechlist[index].slots[slotnum].initialized = B_TRUE;
mechlist[index].slots[slotnum].supported = B_FALSE;
bzero(&mechlist[index].slots[slotnum].mechanism_info,
sizeof (CK_MECHANISM_INFO));
goto finish;
}
rv = FUNCLIST(fw_st_id)->C_GetMechanismInfo(true_id, mech, &info);
if (rv == CKR_OK) {
mechlist[index].slots[slotnum].initialized = B_TRUE;
mechlist[index].slots[slotnum].supported = B_TRUE;
mechlist[index].slots[slotnum].mechanism_info = info;
} else {
/* record that the mechanism isn't supported for the slot */
mechlist[index].slots[slotnum].initialized = B_TRUE;
mechlist[index].slots[slotnum].supported = B_FALSE;
bzero(&mechlist[index].slots[slotnum].mechanism_info,
sizeof (CK_MECHANISM_INFO));
}
finish:
return (rv);
}
/*
* meta_mechManager_allocmechs
*
* Ensures that all of the specified mechanisms are present in the
* mechlist. If a mechanism is not present, an uninitialized entry is
* added for it.
*
* The returned index can be used by the caller as a hint to where the
* first mechanism was located.
*/
static CK_RV
meta_mechManager_allocmechs(CK_MECHANISM_TYPE *new_mechs,
unsigned long num_new_mechs, unsigned long *index_hint)
{
CK_RV rv = CKR_OK;
unsigned long i, index = 0;
boolean_t found;
/* The optimistic assumption is that the mech is already present. */
(void) pthread_rwlock_rdlock(&mechlist_lock);
for (i = 0; i < num_new_mechs; i++) {
found = find_mech_index(new_mechs[i], &index);
if (i == 0)
*index_hint = index;
if (!found)
break;
}
(void) pthread_rwlock_unlock(&mechlist_lock);
if (found) {
return (CKR_OK);
}
/*
* We stopped searching when the first unknown mech was found. Now
* obtain a write-lock, and continue from where we left off, inserting
* unknown mechanisms.
*/
(void) pthread_rwlock_wrlock(&mechlist_lock);
for (; i < num_new_mechs; i++) {
found = find_mech_index(new_mechs[i], &index);
if (!found) {
mechinfo_t *new_mechinfos;
new_mechinfos = calloc(meta_slotManager_get_slotcount(),
sizeof (mechinfo_t));
if (new_mechinfos == NULL) {
rv = CKR_HOST_MEMORY;
goto finish;
}
/*
* If the current storage for the mechlist is too
* small, allocate a new list twice as large.
*/
if (num_mechs == true_mechlist_size) {
mechlist_t *newmechlist;
newmechlist = realloc(mechlist,
2 * true_mechlist_size *
sizeof (mechlist_t));
if (newmechlist == NULL) {
rv = CKR_HOST_MEMORY;
free(new_mechinfos);
goto finish;
}
mechlist = newmechlist;
true_mechlist_size *= 2;
}
/* Shift existing entries to make space. */
(void) memmove(&mechlist[index+1], &mechlist[index],
(num_mechs - index) * sizeof (mechlist_t));
num_mechs++;
mechlist[index].type = new_mechs[i];
mechlist[index].slots = new_mechinfos;
}
}
finish:
(void) pthread_rwlock_unlock(&mechlist_lock);
return (rv);
}
/*
* find_mech_index
*
* Performs a search of mechlist for the specified mechanism, and
* returns if the mechanism was found or not. The value of the "index"
* argument will be where the mech is (if found), or where it should
* be (if not found).
*
* The current value of "index" will be used as a starting point, if the
* caller already knows where the mechanism is likely to be.
*
* The caller is assumed to have a lock on the mechlist, preventing it
* from being changed while searching (also to ensure the returned index
* will remain valid until the list is unlocked).
*
* FUTURE: convert to binary search [from O(N) to a O(log(N))].
*
* NOTES:
* 1) This function assumes that mechMap is a sorted list.
*/
static boolean_t
find_mech_index(CK_MECHANISM_TYPE mechanism, unsigned long *index)
{
boolean_t found = B_FALSE;
unsigned long i;
for (i = 0; i < num_mechs; i++) {
if (mechlist[i].type == mechanism) {
found = B_TRUE;
break;
}
if (mechlist[i].type > mechanism)
break;
}
*index = i;
return (found);
}
static int
qsort_mechtypes(const void *arg1, const void *arg2)
{
CK_MECHANISM_TYPE mech1 = *((CK_MECHANISM_TYPE *)arg1);
CK_MECHANISM_TYPE mech2 = *((CK_MECHANISM_TYPE *)arg2);
if (mech1 > mech2)
return (1);
if (mech1 < mech2)
return (-1);
return (0);
}
/*
* Check if the specified mechanism is supported by the specified slot.
* The result is returned in the "supports" argument. If the "slot_info"
* argument is not NULL, it will be filled with information about
* the slot.
*/
CK_RV
meta_mechManager_slot_supports_mech(CK_MECHANISM_TYPE mechanism,
CK_ULONG slotnum, boolean_t *supports, mechinfo_t **slot_info,
boolean_t force_update, CK_MECHANISM_INFO *mech_info)
{
boolean_t found;
CK_RV rv;
unsigned long index;
CK_MECHANISM_INFO info;
*supports = B_FALSE;
rv = meta_mechManager_update_mech(mechanism, force_update);
if (rv != CKR_OK)
return (rv);
(void) pthread_rwlock_rdlock(&mechlist_lock);
found = find_mech_index(mechanism, &index);
if (!found) {
goto finish;
}
if ((mechlist[index].slots[slotnum].initialized) &&
(mechlist[index].slots[slotnum].supported)) {
if (mech_info) {
info = mechlist[index].slots[slotnum].mechanism_info;
if (!(info.flags & mech_info->flags)) {
goto finish;
}
}
*supports = B_TRUE;
if (slot_info) {
*slot_info = &(mechlist[index].slots[slotnum]);
}
}
finish:
(void) pthread_rwlock_unlock(&mechlist_lock);
return (rv);
}
/*
* 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.
*/
/*
* Object Management Functions
* (as defined in PKCS#11 spec section 11.7)
*/
#include <strings.h>
#include "metaGlobal.h"
#include <stdio.h>
#define FIND_OBJ_BUF_SIZE 512 /* size of buf used for C_FindObjects */
/*
* Argument related return codes. Will return to the caller immediately,
* and not try the operation on another slot.
*/
static CK_RV stop_rv[] = {
CKR_ARGUMENTS_BAD,
CKR_ATTRIBUTE_TYPE_INVALID,
CKR_DOMAIN_PARAMS_INVALID,
CKR_TEMPLATE_INCOMPLETE
};
static int num_stop_rv = sizeof (stop_rv) / sizeof (CK_RV);
/*
* Return codes that are related to a specific slot.
* Will try to perform the operation in the next available slot.
* If all attempts failed, will return the error code from the first slot.
*
* This list is here for reference only, it is commented out because
* it doesn't need to be used by the code at this point.
*
* static CK_RV try_again_rv[] = {
* CKR_DEVICE_ERROR,
* CKR_DEVICE_MEMORY,
* CKR_DEVICE_REMOVED,
* CKR_FUNCTION_FAILED,
* CKR_GENERAL_ERROR,
* CKR_HOST_MEMORY,
* CKR_TEMPLATE_INCONSISTENT,
* CKR_ATTRIBUTE_READ_ONLY,
* CKR_ATTRIBUTE_VALUE_INVALID
* };
* static int num_try_again_rv = sizeof (try_again_rv) / sizeof (CK_RV);
*/
/*
* We should never get these return codes because
* MetaSlot is the one that actually created the
* sessions. When we get these errors in C_CreateObject,
* will try to create the object in the next available slot.
* If all attempts failed, will return CKR_FUNCTION_FAILED
* to the caller.
*/
static CK_RV other_rv[] = {
CKR_CRYPTOKI_NOT_INITIALIZED,
CKR_SESSION_CLOSED,
CKR_SESSION_HANDLE_INVALID,
CKR_SESSION_READ_ONLY
};
static int num_other_rv = sizeof (other_rv) / sizeof (CK_RV);
/*
* This function is only used by the C_CreateObject and C_CopyObject.
*
* It is used to determine if the operation should be tried on another slot
* based on the return code
*/
static boolean_t
try_again(CK_RV rv)
{
int i;
for (i = 0; i < num_stop_rv; i++) {
if (rv == stop_rv[i]) {
return (B_FALSE);
}
}
return (B_TRUE);
}
/*
* meta_CreateObject
*
*/
CK_RV
meta_CreateObject(CK_SESSION_HANDLE hSession, CK_ATTRIBUTE_PTR pTemplate,
CK_ULONG ulCount, CK_OBJECT_HANDLE_PTR phObject)
{
CK_RV rv;
meta_session_t *session;
slot_session_t *slot_session = NULL;
meta_object_t *object = NULL;
slot_object_t *slot_object = NULL;
CK_OBJECT_HANDLE hNewObject;
CK_ULONG slot_num, keystore_slotnum;
CK_RV first_rv;
if (pTemplate == NULL || ulCount < 1 || phObject == NULL)
return (CKR_ARGUMENTS_BAD);
rv = meta_handle2session(hSession, &session);
if (rv != CKR_OK)
return (rv);
rv = meta_object_alloc(session, &object);
if (rv != CKR_OK)
goto cleanup;
/*
* Create a clone of the object
*/
rv = meta_slot_object_alloc(&slot_object);
if (rv != CKR_OK)
goto cleanup;
/*
* Set to true (token object) if template has CKA_TOKEN=true;
* otherwise, it is false (session object).
*/
(void) get_template_boolean(CKA_TOKEN, pTemplate, ulCount,
&(object->isToken));
/* Can't create token objects in a read-only session. */
if ((IS_READ_ONLY_SESSION(session->session_flags)) && object->isToken) {
rv = CKR_SESSION_READ_ONLY;
goto cleanup;
}
/*
* Set to true (private object) if template has CKA_PRIVATE=true;
* otherwise, it is false (public object).
*/
(void) get_template_boolean(CKA_PRIVATE, pTemplate, ulCount,
&(object->isPrivate));
/* Assume object is extractable unless template has otherwise */
object->isExtractable = B_TRUE;
(void) get_template_boolean(CKA_EXTRACTABLE, pTemplate, ulCount,
&(object->isExtractable));
/*
* Set to true (sensitive object) if template has CKA_SENSITIVE=true;
* otherwise, it is false.
*/
(void) get_template_boolean(CKA_SENSITIVE, pTemplate, ulCount,
&(object->isSensitive));
/*
* Check if this can be a FreeObject.
*
* For creating objects, this check is mostly for preventing
* non-keystore hardware from creating CKA_PRIVATE objects without
* logging in.
*/
if (meta_freeobject_check(session, object, NULL, pTemplate, ulCount,
0)) {
/*
* Make sure we are logged into the keystore if this is a
* private freetoken object.
*/
if (object->isPrivate && !metaslot_logged_in())
return (CKR_USER_NOT_LOGGED_IN);
if (!meta_freeobject_set(object, pTemplate, ulCount, B_TRUE))
goto cleanup;
}
keystore_slotnum = get_keystore_slotnum();
if (object->isToken || object->isFreeToken == FREE_ENABLED) {
/*
* If this is a token object or a FreeToken then create it
* on the keystore slot.
*/
slot_num = keystore_slotnum;
rv = meta_get_slot_session(slot_num, &slot_session,
session->session_flags);
if (rv != CKR_OK)
goto cleanup;
object->tried_create_clone[slot_num] = B_TRUE;
rv = FUNCLIST(slot_session->fw_st_id)->C_CreateObject(
slot_session->hSession, pTemplate, ulCount, &hNewObject);
if (rv != CKR_OK)
goto cleanup;
} else {
/*
* Create a clone of the object in the first available slot.
*
* If creating a clone in a specific slot failed, it will
* either stop and return the error to the user, or try
* again in the next available slot until it succeeds. The
* decision to stop or continue is made based on the return
* code.
*/
CK_ULONG num_slots = meta_slotManager_get_slotcount();
for (slot_num = 0; slot_num < num_slots; slot_num++) {
/*
* If this is a free token and we are on the keystore
* slot, bypass this because it was already created
*/
rv = meta_get_slot_session(slot_num, &slot_session,
session->session_flags);
if (rv != CKR_OK)
goto cleanup;
object->tried_create_clone[slot_num] = B_TRUE;
rv = FUNCLIST(slot_session->fw_st_id)->C_CreateObject(
slot_session->hSession, pTemplate, ulCount,
&hNewObject);
if (rv == CKR_OK)
break;
if (!try_again(rv))
goto cleanup;
/* save first rv for other errors */
if (slot_num == 0)
first_rv = rv;
meta_release_slot_session(slot_session);
slot_session = NULL;
}
}
if (rv == CKR_OK) {
slot_object->hObject = hNewObject;
object->clones[slot_num] = slot_object;
object->master_clone_slotnum = slot_num;
/* Allow FreeToken to activate onto token obj list */
if (object->isFreeToken == FREE_ENABLED)
object->isToken = B_TRUE;
meta_slot_object_activate(slot_object, slot_session,
object->isToken);
slot_object = NULL;
meta_release_slot_session(slot_session);
slot_session = NULL;
} else {
/*
* return either first error code or
* CKR_FUNCTION_FAILED depending on the failure
*/
int i;
for (i = 0; i < num_other_rv; i++) {
if (rv == other_rv[i]) {
rv = CKR_FUNCTION_FAILED;
goto cleanup;
}
}
/* need to return first rv */
rv = first_rv;
goto cleanup;
}
/*
* always keep a copy of the template for C_CreateObject,
* so clones can be created on other slots if necessary.
* This is done even when the CKA_EXTRACTABLE=FALSE flag
* is set for the object. The supplied template is
* "owned" by metaslot. The application should not be
* penalized just because metaslot choose to try creating
* the object in a slot that's not capable of performing
* any future operation.
*/
rv = get_master_attributes_by_template(pTemplate, ulCount,
&object->attributes, &object->num_attributes);
if (rv == CKR_OK) {
CK_ULONG i;
for (i = 0; i < ulCount; i++) {
rv = attribute_set_value(&(pTemplate[i]),
object->attributes, object->num_attributes);
}
}
meta_object_activate(object);
*phObject = (CK_OBJECT_HANDLE) object;
REFRELEASE(session);
return (CKR_OK);
cleanup:
if (slot_object)
meta_slot_object_dealloc(slot_object);
if (slot_session)
meta_release_slot_session(slot_session);
if (object)
(void) meta_object_dealloc(session, object, B_TRUE);
REFRELEASE(session);
return (rv);
}
/*
* meta_CopyObject
*
*/
CK_RV
meta_CopyObject(CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE hObject,
CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount,
CK_OBJECT_HANDLE_PTR phNewObject)
{
CK_RV rv, first_rv;
meta_session_t *session;
meta_object_t *src_object, *dst_object = NULL;
slot_session_t *slot_session = NULL;
slot_object_t *dst_slot_object = NULL;
CK_ULONG i;
slot_object_t *src_slot_object;
CK_ULONG slotnum, num_slots;
boolean_t found;
if (pTemplate == NULL && ulCount != 0)
return (CKR_ARGUMENTS_BAD);
if (phNewObject == NULL)
return (CKR_ARGUMENTS_BAD);
rv = meta_handle2session(hSession, &session);
if (rv != CKR_OK)
return (rv);
rv = meta_handle2object(hObject, &src_object);
if (rv != CKR_OK) {
REFRELEASE(session);
return (rv);
}
rv = meta_object_alloc(session, &dst_object);
if (rv != CKR_OK)
goto finish;
found = get_template_boolean(CKA_TOKEN,
pTemplate, ulCount, &(dst_object->isToken));
if (!found) {
dst_object->isToken = src_object->isToken;
if (src_object->isFreeToken == FREE_ENABLED)
dst_object->isToken = TRUE;
else
dst_object->isToken = src_object->isToken;
}
/* Can't create token objects in a read-only session. */
if ((IS_READ_ONLY_SESSION(session->session_flags)) &&
(dst_object->isToken)) {
rv = CKR_SESSION_READ_ONLY;
goto finish;
}
if (dst_object->isToken) {
/*
* if the dst object is a token object, and the source
* object is not, the source object needs to be extractable.
* Otherwise, the source object needs to reside in the
* token object slot
*/
if ((!src_object->isExtractable) &&
(src_object->master_clone_slotnum
!= get_keystore_slotnum())) {
rv = CKR_FUNCTION_FAILED;
goto finish;
}
/* determine if dst is going to be private object or not */
found = get_template_boolean(CKA_PRIVATE,
pTemplate, ulCount, &(dst_object->isPrivate));
if (!found) {
/* will be the same as the source object */
dst_object->isPrivate = src_object->isPrivate;
}
slotnum = get_keystore_slotnum();
} else {
/* try create the obj in the same slot as the source obj */
slotnum = src_object->master_clone_slotnum;
}
rv = meta_slot_object_alloc(&dst_slot_object);
if (rv != CKR_OK)
goto finish;
rv = meta_get_slot_session(slotnum, &slot_session,
session->session_flags);
if (rv != CKR_OK)
goto finish;
rv = meta_object_get_clone(src_object, slotnum,
slot_session, &src_slot_object);
if (rv != CKR_OK)
goto finish;
dst_object->tried_create_clone[slotnum] = B_TRUE;
rv = FUNCLIST(slot_session->fw_st_id)->C_CopyObject(
slot_session->hSession, src_slot_object->hObject, pTemplate,
ulCount, &(dst_slot_object->hObject));
if (rv != CKR_OK) {
if (dst_object->isToken) {
/*
* token obj can only be created in the
* token slot. No need to try anywhere else
*/
goto finish;
}
if ((!src_object->isExtractable) ||
((src_object->isSensitive) && (src_object->isToken) &&
(!metaslot_auto_key_migrate))) {
/* source object isn't clonable in another slot */
goto finish;
}
if (!try_again(rv)) {
goto finish;
}
first_rv = rv;
meta_release_slot_session(slot_session);
slot_session = NULL;
num_slots = meta_slotManager_get_slotcount();
/* Try operation on other slots if the object is clonable */
for (slotnum = 0; slotnum < num_slots; slotnum++) {
if (slotnum == src_object->master_clone_slotnum) {
/* already tried, don't need to try again */
continue;
}
rv = meta_get_slot_session(slotnum, &slot_session,
session->session_flags);
if (rv != CKR_OK) {
goto finish;
}
rv = meta_object_get_clone(src_object, slotnum,
slot_session, &src_slot_object);
if (rv != CKR_OK)
goto finish;
dst_object->tried_create_clone[slotnum] = B_TRUE;
rv = FUNCLIST(slot_session->fw_st_id)->C_CopyObject(
slot_session->hSession, src_slot_object->hObject,
pTemplate, ulCount, &dst_slot_object->hObject);
if (rv == CKR_OK) {
break;
}
if (!try_again(rv)) {
goto finish;
}
meta_release_slot_session(slot_session);
slot_session = NULL;
}
}
if (rv == CKR_OK) {
rv = meta_object_get_attr(slot_session,
dst_slot_object->hObject, dst_object);
if (rv != CKR_OK) {
goto finish;
}
if (src_object->attributes != NULL) {
/* Keep a copy of the template for the future */
/*
* Don't allow attributes to change while
* we look at them.
*/
(void) pthread_rwlock_rdlock(
&src_object->attribute_lock);
rv = get_master_attributes_by_duplication(
src_object->attributes,
src_object->num_attributes,
&dst_object->attributes,
&dst_object->num_attributes);
(void) pthread_rwlock_unlock(
&src_object->attribute_lock);
if (rv != CKR_OK)
goto finish;
for (i = 0; i < ulCount; i++) {
rv = attribute_set_value(pTemplate + i,
dst_object->attributes,
dst_object->num_attributes);
if (rv != CKR_OK)
goto finish;
}
}
/* Allow FreeToken to activate onto token obj list */
if (dst_object->isFreeToken == FREE_ENABLED)
dst_object->isToken = TRUE;
meta_slot_object_activate(dst_slot_object,
slot_session, dst_object->isToken);
dst_object->clones[slotnum] = dst_slot_object;
dst_object->master_clone_slotnum = slotnum;
dst_slot_object = NULL; /* for error cleanup */
meta_release_slot_session(slot_session);
slot_session = NULL; /* for error cleanup */
} else {
/*
* return either first error code or
* CKR_FUNCTION_FAILED depending on the failure
*/
int j;
for (j = 0; j < num_other_rv; j++) {
if (rv == other_rv[j]) {
rv = CKR_FUNCTION_FAILED;
goto finish;
}
}
/* need to return first rv */
rv = first_rv;
goto finish;
}
meta_object_activate(dst_object);
*phNewObject = (CK_OBJECT_HANDLE) dst_object;
finish:
if (rv != CKR_OK) {
if (dst_slot_object)
meta_slot_object_dealloc(dst_slot_object);
if (dst_object)
(void) meta_object_dealloc(session, dst_object,
B_TRUE);
if (slot_session)
meta_release_slot_session(slot_session);
}
OBJRELEASE(src_object);
REFRELEASE(session);
return (rv);
}
/*
* meta_DestroyObject
*
* This function destroys an object by first removing it from the
* list of valid objects for a given session (if session object) or
* the global token object list. And then, calling C_DestroyObject
* on all the slots on which we have created a clone of this object.
*/
CK_RV
meta_DestroyObject(CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE hObject)
{
CK_RV rv;
meta_session_t *session;
meta_object_t *object;
rv = meta_handle2session(hSession, &session);
if (rv != CKR_OK)
return (rv);
rv = meta_handle2object(hObject, &object);
if (rv != CKR_OK) {
REFRELEASE(session);
return (rv);
}
/* Can't delete token objects from a read-only session. */
if ((IS_READ_ONLY_SESSION(session->session_flags)) &&
(object->isToken || object->isFreeToken == FREE_ENABLED)) {
OBJRELEASE(object);
REFRELEASE(session);
return (CKR_SESSION_READ_ONLY);
}
/* Remove object from list of valid meta_objects */
rv = meta_object_deactivate(object, B_FALSE, B_TRUE);
/*
* Actually call C_DestroyObject on all the slots on which we have
* created a clone of this object.
*/
if (rv == CKR_OK)
rv = meta_object_dealloc(session, object, B_TRUE);
REFRELEASE(session);
return (rv);
}
/*
* meta_GetObjectSize
*
* NOTES:
* 1) Because the "size" is so poorly defined in the spec, we have deemed
* it useless and won't support it. This is especially true for the
* metaslot, because the mulitple providers it uses may each interpret
* the size differently.
*/
/* ARGSUSED */
CK_RV
meta_GetObjectSize(CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE hObject,
CK_ULONG_PTR pulSize)
{
return (CKR_FUNCTION_NOT_SUPPORTED);
}
/*
* meta_GetAttributeValue
*
*/
CK_RV
meta_GetAttributeValue(CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE hObject,
CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount)
{
CK_RV rv;
meta_session_t *session;
meta_object_t *object;
CK_ULONG slotnum;
slot_session_t *slot_session;
if (pTemplate == NULL || ulCount < 1)
return (CKR_ARGUMENTS_BAD);
rv = meta_handle2session(hSession, &session);
if (rv != CKR_OK)
return (rv);
rv = meta_handle2object(hObject, &object);
if (rv != CKR_OK) {
REFRELEASE(session);
return (rv);
}
slotnum = object->master_clone_slotnum;
rv = meta_get_slot_session(slotnum, &slot_session,
session->session_flags);
if (rv == CKR_OK) {
rv = FUNCLIST(slot_session->fw_st_id)->C_GetAttributeValue(
slot_session->hSession, object->clones[slotnum]->hObject,
pTemplate, ulCount);
meta_release_slot_session(slot_session);
}
OBJRELEASE(object);
REFRELEASE(session);
return (rv);
}
/*
* meta_SetAttributeValue
*
* Call C_SetAttributeValue on all the clones. If the operation fails on
* all clones, return the failure.
*
* If the operation fails on some clones and not the others, delete all the
* clones that have failed the operation. If any of the deleted clone is the
* master clone, use one of the remaining clone as the master clone.
*
* If the operation is successful and the master template already exists,
* update the master template with new values.
*/
CK_RV
meta_SetAttributeValue(CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE hObject,
CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount)
{
CK_RV rv = CKR_OK, save_rv = CKR_OK;
meta_session_t *session;
meta_object_t *object;
CK_ULONG slotnum, num_slots;
/* Keep track of which slot's SetAttributeValue failed */
boolean_t *clone_failed_op = NULL;
int num_clones = 0, num_clones_failed = 0;
slot_session_t *slot_session;
slot_object_t *slot_object;
boolean_t need_update_master_clone = B_FALSE;
if (pTemplate == NULL || ulCount < 1)
return (CKR_ARGUMENTS_BAD);
rv = meta_handle2session(hSession, &session);
if (rv != CKR_OK)
return (rv);
rv = meta_handle2object(hObject, &object);
if (rv != CKR_OK) {
REFRELEASE(session);
return (rv);
}
if ((IS_READ_ONLY_SESSION(session->session_flags)) &&
(object->isToken || object->isFreeToken == FREE_ENABLED)) {
rv = CKR_SESSION_READ_ONLY;
goto finish;
}
if ((!object->isExtractable) && (object->attributes == NULL)) {
/*
* object has no clone, just need to do the operation
* in the master clone slot
*/
slot_session_t *slot_session;
slotnum = object->master_clone_slotnum;
rv = meta_get_slot_session(slotnum, &slot_session,
session->session_flags);
if (rv == CKR_OK) {
rv = FUNCLIST(slot_session->fw_st_id)->\
C_SetAttributeValue(slot_session->hSession,
object->clones[slotnum]->hObject, pTemplate,
ulCount);
meta_release_slot_session(slot_session);
}
goto finish;
}
num_slots = meta_slotManager_get_slotcount();
/*
* object might have clones, need to do operation in all clones
*
* If the C_SetAttributeValue() call fails in a clone, the
* clone that failed the operation can not be deleted right
* away. The clone with the failed operation is recorded, and
* the deletion will happen in a separate loop.
*
* This is necessary because if ALL the clones failed
* C_SetAttributeVAlue(), then, the app's call to C_SetAttributeValue()
* is considered failed, and there shouldn't be any changes to the
* object, none of the clones should be deleted.
* On the other hand, if C_SetAttributeValue() fails in some clones
* and succeeds in other clones, the C_SetAttributeValue() operation
* is considered successful, and those clones that failed the
* operation is deleted.
*/
clone_failed_op = calloc(num_slots, sizeof (boolean_t));
if (clone_failed_op == NULL) {
rv = CKR_HOST_MEMORY;
goto finish;
}
for (slotnum = 0; slotnum < num_slots; slotnum++) {
if (object->clones[slotnum] != NULL) {
num_clones++;
rv = meta_get_slot_session(slotnum, &slot_session,
session->session_flags);
if (rv != CKR_OK) {
goto finish;
}
rv = FUNCLIST(slot_session->fw_st_id)->\
C_SetAttributeValue(slot_session->hSession,
object->clones[slotnum]->hObject, pTemplate,
ulCount);
if (rv != CKR_OK) {
num_clones_failed++;
clone_failed_op[slotnum] = B_TRUE;
if (save_rv == CKR_OK) {
save_rv = rv;
}
}
meta_release_slot_session(slot_session);
}
}
if (num_clones_failed == num_clones) {
/* all operations failed */
rv = save_rv;
goto finish;
}
if (num_clones_failed > 0) {
/*
* C_SetAttributeValue in some of the clones failed.
* Find out which ones failed, and delete the clones
* in those failed slots
*/
for (slotnum = 0; slotnum < num_slots; slotnum++) {
if (clone_failed_op[slotnum]) {
slot_object_t *clone = object->clones[slotnum];
rv = meta_get_slot_session(slotnum,
&slot_session, session->session_flags);
if (rv == CKR_OK) {
(void) FUNCLIST(
slot_session->fw_st_id)->
C_DestroyObject(
slot_session->hSession,
clone->hObject);
meta_release_slot_session(slot_session);
}
meta_slot_object_deactivate(clone);
meta_slot_object_dealloc(clone);
object->clones[slotnum] = NULL;
if (slotnum == object->master_clone_slotnum) {
need_update_master_clone = B_TRUE;
}
}
}
if (need_update_master_clone) {
/* make first available clone the master */
for (slotnum = 0; slotnum < num_slots; slotnum++) {
if (object->clones[slotnum]) {
object->master_clone_slotnum = slotnum;
need_update_master_clone = B_FALSE;
break;
}
}
}
if (need_update_master_clone) {
/*
* something is very wrong, can't continue
* it should never be this case.
*/
rv = CKR_FUNCTION_FAILED;
goto finish;
}
rv = CKR_OK;
}
/*
* Update the attribute information we keep in our metaslot object
*/
slot_object = object->clones[object->master_clone_slotnum];
rv = meta_get_slot_session(object->master_clone_slotnum,
&slot_session, session->session_flags);
if (rv == CKR_OK) {
(void) meta_object_get_attr(slot_session,
slot_object->hObject, object);
meta_release_slot_session(slot_session);
}
/* if there's a copy of the attributes, keep it up to date */
if (object->attributes != NULL) {
CK_ULONG i;
/* Make sure no one else is looking at attributes. */
(void) pthread_rwlock_wrlock(&object->attribute_lock);
for (i = 0; i < ulCount; i++) {
(void) attribute_set_value(pTemplate + i,
object->attributes, object->num_attributes);
}
(void) pthread_rwlock_unlock(&object->attribute_lock);
}
finish:
if (clone_failed_op) {
free(clone_failed_op);
}
OBJRELEASE(object);
REFRELEASE(session);
return (rv);
}
static boolean_t
meta_object_in_list(meta_object_t *obj, meta_object_t **objs_list, int num_objs)
{
int i;
for (i = 0; i < num_objs; i++) {
if (objs_list[i] == obj) {
return (B_TRUE);
}
}
return (B_FALSE);
}
static CK_RV
add_to_search_result(meta_object_t *object, find_objs_info_t *info,
int *num_results_alloc)
{
/*
* allocate space for storing results if the currently
* allocated space is not enough
*/
if (*num_results_alloc <= info->num_matched_objs) {
*num_results_alloc += FIND_OBJ_BUF_SIZE;
info->matched_objs = realloc(info->matched_objs,
sizeof (meta_object_t *) * (*num_results_alloc));
if (info->matched_objs == NULL) {
return (CKR_HOST_MEMORY);
}
}
(info->matched_objs)[(info->num_matched_objs)++] = object;
return (CKR_OK);
}
static CK_RV
process_find_results(CK_OBJECT_HANDLE *results, CK_ULONG num_results,
int *num_results_allocated, find_objs_info_t *info, CK_ULONG slotnum,
boolean_t token_only, slot_session_t *slot_session,
meta_session_t *session)
{
CK_ULONG i;
meta_object_t *object;
CK_RV rv;
for (i = 0; i < num_results; i++) {
object = meta_object_find_by_handle(results[i], slotnum,
token_only);
/*
* a token object is found from the keystore,
* need to create a meta object for it
*/
if (object == NULL) {
slot_object_t *slot_object;
rv = meta_object_alloc(session, &object);
if (rv != CKR_OK) {
return (rv);
}
rv = meta_slot_object_alloc(&slot_object);
if (rv != CKR_OK) {
(void) meta_object_dealloc(session, object,
B_TRUE);
return (rv);
}
slot_object->hObject = results[i];
object->master_clone_slotnum = slotnum;
object->clones[slotnum] = slot_object;
/* get in the attributes we keep in meta_object */
rv = meta_object_get_attr(slot_session,
slot_object->hObject, object);
if (rv != CKR_OK) {
(void) meta_object_dealloc(session, object,
B_TRUE);
return (rv);
}
meta_slot_object_activate(slot_object, slot_session,
B_TRUE);
meta_object_activate(object);
slot_object = NULL;
}
if (!meta_object_in_list(object, info->matched_objs,
info->num_matched_objs)) {
rv = add_to_search_result(object, info,
num_results_allocated);
if (rv != CKR_OK) {
return (rv);
}
}
}
return (CKR_OK);
}
static CK_RV
meta_search_for_objects(meta_session_t *session, find_objs_info_t *info,
slot_session_t *slot_session, CK_ATTRIBUTE_PTR pTemplate,
CK_ULONG ulCount, CK_ULONG slotnum, boolean_t token_only,
int *num_results_alloc)
{
CK_ULONG tmp_num_results;
CK_OBJECT_HANDLE tmp_results[FIND_OBJ_BUF_SIZE];
CK_SESSION_HANDLE hSession = slot_session->hSession;
CK_RV rv;
CK_SLOT_ID fw_st_id = slot_session->fw_st_id;
rv = FUNCLIST(fw_st_id)->C_FindObjectsInit(hSession,
pTemplate, ulCount);
if (rv != CKR_OK) {
return (rv);
}
tmp_num_results = 0;
rv = FUNCLIST(fw_st_id)->C_FindObjects(hSession, tmp_results,
FIND_OBJ_BUF_SIZE, &tmp_num_results);
if (rv != CKR_OK) {
return (rv);
}
rv = process_find_results(tmp_results, tmp_num_results,
num_results_alloc, info, slotnum, token_only,
slot_session, session);
if (rv != CKR_OK) {
return (rv);
}
while (tmp_num_results == FIND_OBJ_BUF_SIZE) {
/* might be more results, need to call C_FindObjects again */
rv = FUNCLIST(fw_st_id)->C_FindObjects(hSession, tmp_results,
FIND_OBJ_BUF_SIZE, &tmp_num_results);
if (rv != CKR_OK) {
return (rv);
}
rv = process_find_results(tmp_results, tmp_num_results,
num_results_alloc, info, slotnum, token_only,
slot_session, session);
if (rv != CKR_OK) {
return (rv);
}
}
rv = FUNCLIST(fw_st_id)->C_FindObjectsFinal(hSession);
return (rv);
}
/*
* meta_FindObjectsInit
*
* This function actually will do ALL the work of searching for objects
* that match all requirements specified in the template.
*
* Objects that matched the template will be stored in the
* session's data structure. When the subsequent C_FindObjects()
* calls are made, results saved will be returned.
*
*/
CK_RV
meta_FindObjectsInit(CK_SESSION_HANDLE hSession, CK_ATTRIBUTE_PTR pTemplate,
CK_ULONG ulCount)
{
CK_RV rv;
meta_session_t *session;
CK_ULONG slot_num = 0;
boolean_t have_token_attr, tokenTrue = B_FALSE;
slot_session_t *slot_find_session = NULL;
int num_results_allocated = 0;
CK_ULONG keystore_slotnum;
rv = meta_handle2session(hSession, &session);
if (rv != CKR_OK)
return (rv);
if ((session->find_objs_info).op_active) {
REFRELEASE(session);
return (CKR_OPERATION_ACTIVE);
}
(session->find_objs_info).op_active = B_TRUE;
REFRELEASE(session);
/* see if the template indicates token object only or not */
have_token_attr = get_template_boolean(CKA_TOKEN, pTemplate, ulCount,
&tokenTrue);
keystore_slotnum = get_keystore_slotnum();
if (have_token_attr && tokenTrue) {
/*
* only interested in token objects, just need to search
* token object slot
*/
rv = meta_get_slot_session(keystore_slotnum,
&slot_find_session, session->session_flags);
if (rv != CKR_OK) {
goto finish;
}
rv = meta_search_for_objects(session,
&(session->find_objs_info), slot_find_session, pTemplate,
ulCount, keystore_slotnum, B_TRUE, &num_results_allocated);
if (rv != CKR_OK) {
goto finish;
}
} else {
CK_ULONG num_slots = meta_slotManager_get_slotcount();
for (slot_num = 0; slot_num < num_slots; slot_num++) {
rv = meta_get_slot_session(slot_num,
&slot_find_session, session->session_flags);
if (rv != CKR_OK) {
goto finish;
}
/*
* if the slot is NOT the token object slot, and
* CKA_TOKEN is not specified, need to specified
* it to be false explicitly. This will prevent
* us from using token objects that doesn't
* belong to the token slot in the case that
* more than one slot supports token objects.
*/
if ((slot_num != keystore_slotnum) &&
(!have_token_attr)) {
CK_BBOOL false = FALSE;
CK_ATTRIBUTE_PTR newTemplate;
newTemplate = malloc((ulCount + 1) *
sizeof (CK_ATTRIBUTE));
if (newTemplate == NULL) {
rv = CKR_HOST_MEMORY;
goto finish;
}
(void) memcpy(newTemplate + 1, pTemplate,
ulCount * sizeof (CK_ATTRIBUTE));
newTemplate[0].type = CKA_TOKEN;
newTemplate[0].pValue = &false;
newTemplate[0].ulValueLen = sizeof (false);
rv = meta_search_for_objects(session,
&(session->find_objs_info),
slot_find_session, newTemplate,
ulCount+1, slot_num, B_FALSE,
&num_results_allocated);
free(newTemplate);
} else {
rv = meta_search_for_objects(session,
&(session->find_objs_info),
slot_find_session, pTemplate, ulCount,
slot_num, B_FALSE,
&num_results_allocated);
}
if (rv != CKR_OK) {
goto finish;
}
meta_release_slot_session(slot_find_session);
slot_find_session = NULL;
}
}
finish:
if (slot_find_session != NULL) {
meta_release_slot_session(slot_find_session);
}
if (rv != CKR_OK) {
(void) pthread_rwlock_wrlock(&session->session_lock);
if (((session->find_objs_info).matched_objs) != NULL) {
free((session->find_objs_info).matched_objs);
}
bzero(&(session->find_objs_info), sizeof (find_objs_info_t));
(void) pthread_rwlock_unlock(&(session->session_lock));
}
return (rv);
}
/*
* meta_FindObjects
*
* This function actually doesn't do any real work in search for the
* matching object. All the work is done in FindObjectsInit(). This
* function will only return the matching objects store in the session's
* "find_objs_info" variable.
*
*/
CK_RV
meta_FindObjects(CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE_PTR phObject,
CK_ULONG ulMaxObjectCount, CK_ULONG_PTR pulObjectCount)
{
CK_RV rv;
find_objs_info_t *info;
CK_ULONG num_objs_found = 0;
meta_object_t *obj;
meta_session_t *session;
int i;
rv = meta_handle2session(hSession, &session);
if (rv != CKR_OK)
return (rv);
info = &(session->find_objs_info);
if (!(info->op_active)) {
REFRELEASE(session);
return (CKR_OPERATION_NOT_INITIALIZED);
}
for (i = info->next_result_index;
((num_objs_found < ulMaxObjectCount) &&
(i < info->num_matched_objs));
i++) {
obj = info->matched_objs[i];
if (obj != NULL) {
/* sanity check to see if object is still valid */
(void) pthread_rwlock_rdlock(&obj->object_lock);
if (obj->magic_marker == METASLOT_OBJECT_MAGIC) {
phObject[num_objs_found++] =
(CK_OBJECT_HANDLE)obj;
}
(void) pthread_rwlock_unlock(&obj->object_lock);
}
}
info->next_result_index = i;
*pulObjectCount = num_objs_found;
REFRELEASE(session);
return (rv);
}
/*
* meta_FindObjectsFinal
*
*/
CK_RV
meta_FindObjectsFinal(CK_SESSION_HANDLE hSession)
{
CK_RV rv;
find_objs_info_t *info;
meta_session_t *session;
rv = meta_handle2session(hSession, &session);
if (rv != CKR_OK)
return (rv);
info = &(session->find_objs_info);
if (!info->op_active) {
REFRELEASE(session);
return (CKR_OPERATION_NOT_INITIALIZED);
}
if (info->matched_objs) {
free(info->matched_objs);
}
bzero(info, sizeof (find_objs_info_t));
REFRELEASE(session);
return (rv);
}
/*
* 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 (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, Joyent, Inc.
*/
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "metaGlobal.h"
/* Size of the template for creating key used for wrap/unwrap */
#define WRAP_KEY_TEMPLATE_SIZE 7
/*
* Information necessary to create keys for C_WrapKey/C_UnwrapKey
*/
typedef struct _wrap_info {
CK_OBJECT_CLASS class; /* class of the key for wrap/unwrap */
CK_KEY_TYPE key_type; /* key type of key for wrap/unwrap */
CK_ULONG key_length; /* length of key */
CK_MECHANISM_TYPE mech_type; /* mech used for wrap/unwrap */
CK_ULONG iv_length; /* length of iv for mech */
boolean_t src_supports;
boolean_t dst_supports;
} wrap_info_t;
extern pthread_rwlock_t meta_sessionlist_lock;
extern meta_session_t *meta_sessionlist_head;
static wrap_info_t common_wrap_info[] = {
{CKO_SECRET_KEY, CKK_AES, 16, CKM_AES_CBC_PAD, 16, B_FALSE, B_FALSE},
{CKO_SECRET_KEY, CKK_DES3, 24, CKM_DES3_CBC_PAD, 8, B_FALSE, B_FALSE},
{CKO_SECRET_KEY, CKK_DES, 8, CKM_DES_CBC_PAD, 8, B_FALSE, B_FALSE},
};
static unsigned int num_common_wrap_info =
sizeof (common_wrap_info) / sizeof (wrap_info_t);
static wrap_info_t special_wrap_info[] = {
{CKO_SECRET_KEY, CKK_SKIPJACK, 12, CKM_SKIPJACK_WRAP, 0,
B_FALSE, B_FALSE},
{CKO_SECRET_KEY, CKK_BATON, 40, CKM_BATON_WRAP, 0,
B_FALSE, B_FALSE},
{CKO_SECRET_KEY, CKK_JUNIPER, 40, CKM_JUNIPER_WRAP, 0,
B_FALSE, B_FALSE},
};
static unsigned int num_special_wrap_info =
sizeof (special_wrap_info) / sizeof (wrap_info_t);
static wrap_info_t rsa_wrap_info[] = {
{CKO_PUBLIC_KEY, CKK_RSA, 0, CKM_RSA_PKCS, 0,
B_FALSE, B_FALSE},
{CKO_PUBLIC_KEY, CKK_RSA, 0, CKM_RSA_X_509, 0,
B_FALSE, B_FALSE},
};
static unsigned int num_rsa_wrap_info =
sizeof (rsa_wrap_info) / sizeof (wrap_info_t);
static pthread_rwlock_t meta_objectclose_lock;
static pthread_rwlock_t tokenobject_list_lock;
static meta_object_t *tokenobject_list_head;
CK_BBOOL falsevalue = FALSE;
CK_BBOOL truevalue = TRUE;
/*
* Public and private exponent, and Module value for
* creating the RSA public/private key.
*
*/
static CK_BYTE PubExpo[3] = {0x01, 0x00, 0x01};
CK_BYTE PriExpo[128] = {
0x8e, 0xc9, 0x70, 0x57, 0x6b, 0xcd, 0xfb, 0xa9,
0x19, 0xad, 0xcd, 0x91, 0x69, 0xd5, 0x52, 0xec,
0x72, 0x1e, 0x45, 0x15, 0x06, 0xdc, 0x65, 0x2d,
0x98, 0xc4, 0xce, 0x33, 0x54, 0x15, 0x70, 0x8d,
0xfa, 0x65, 0xea, 0x53, 0x44, 0xf3, 0x3e, 0x3f,
0xb4, 0x4c, 0x60, 0xd5, 0x01, 0x2d, 0xa4, 0x12,
0x99, 0xbf, 0x3f, 0x0b, 0xcd, 0xbb, 0x24, 0x10,
0x60, 0x30, 0x5e, 0x58, 0xf8, 0x59, 0xaa, 0xd1,
0x63, 0x3b, 0xbc, 0xcb, 0x94, 0x58, 0x38, 0x24,
0xfc, 0x65, 0x25, 0xc5, 0xa6, 0x51, 0xa2, 0x2e,
0xf1, 0x5e, 0xf5, 0xc1, 0xf5, 0x46, 0xf7, 0xbd,
0xc7, 0x62, 0xa8, 0xe2, 0x27, 0xd6, 0x94, 0x5b,
0xd3, 0xa2, 0xb5, 0x76, 0x42, 0x67, 0x6b, 0x86,
0x91, 0x97, 0x4d, 0x07, 0x92, 0x00, 0x4a, 0xdf,
0x0b, 0x65, 0x64, 0x05, 0x03, 0x48, 0x27, 0xeb,
0xce, 0x9a, 0x49, 0x7f, 0x3e, 0x10, 0xe0, 0x01};
static CK_BYTE Modulus[128] = {
0x94, 0x32, 0xb9, 0x12, 0x1d, 0x68, 0x2c, 0xda,
0x2b, 0xe0, 0xe4, 0x97, 0x1b, 0x4d, 0xdc, 0x43,
0xdf, 0x38, 0x6e, 0x7b, 0x9f, 0x07, 0x58, 0xae,
0x9d, 0x82, 0x1e, 0xc7, 0xbc, 0x92, 0xbf, 0xd3,
0xce, 0x00, 0xbb, 0x91, 0xc9, 0x79, 0x06, 0x03,
0x1f, 0xbc, 0x9f, 0x94, 0x75, 0x29, 0x5f, 0xd7,
0xc5, 0xf3, 0x73, 0x8a, 0xa4, 0x35, 0x43, 0x7a,
0x00, 0x32, 0x97, 0x3e, 0x86, 0xef, 0x70, 0x6f,
0x18, 0x56, 0x15, 0xaa, 0x6a, 0x87, 0xe7, 0x8d,
0x7d, 0xdd, 0x1f, 0xa4, 0xe4, 0x31, 0xd4, 0x7a,
0x8c, 0x0e, 0x20, 0xd2, 0x23, 0xf5, 0x57, 0x3c,
0x1b, 0xa8, 0x44, 0xa4, 0x57, 0x8f, 0x33, 0x52,
0xad, 0x83, 0xae, 0x4a, 0x97, 0xa6, 0x1e, 0xa6,
0x2b, 0xfa, 0xea, 0xeb, 0x6e, 0x71, 0xb8, 0xb6,
0x0a, 0x36, 0xed, 0x83, 0xce, 0xb0, 0xdf, 0xc1,
0xd4, 0x3a, 0xe9, 0x99, 0x6f, 0xf3, 0x96, 0xb7};
static CK_RV
meta_clone_template_setup(meta_object_t *object,
const generic_attr_t *attributes, size_t num_attributes);
/*
* meta_objectManager_initialize
*
* Called from meta_Initialize. Initializes all the variables used
* by the object manager.
*/
CK_RV
meta_objectManager_initialize()
{
if (pthread_rwlock_init(&meta_objectclose_lock, NULL) != 0) {
return (CKR_FUNCTION_FAILED);
}
if (pthread_rwlock_init(&tokenobject_list_lock, NULL) != 0) {
(void) pthread_rwlock_destroy(&meta_objectclose_lock);
return (CKR_FUNCTION_FAILED);
}
tokenobject_list_head = NULL;
return (CKR_OK);
}
void
meta_objectManager_finalize()
{
/*
* If there are still any token object in the list, need to
* deactivate all of them.
*/
(void) meta_token_object_deactivate(ALL_TOKEN);
(void) pthread_rwlock_destroy(&meta_objectclose_lock);
(void) pthread_rwlock_destroy(&tokenobject_list_lock);
}
/*
* meta_handle2object
*
* Convert a CK_OBJECT_HANDLE to the corresponding metaobject. If
* successful, a reader-lock on the object will be held to indicate
* that it's in use. Call OBJRELEASE() when finished.
*
*/
CK_RV
meta_handle2object(CK_OBJECT_HANDLE hObject, meta_object_t **object)
{
meta_object_t *tmp_object = (meta_object_t *)(hObject);
/* Check for bad args (eg CK_INVALID_HANDLE, which is 0/NULL). */
if (tmp_object == NULL) {
*object = NULL;
return (CKR_OBJECT_HANDLE_INVALID);
}
/* Lock to ensure the magic-check + read-lock is atomic. */
(void) pthread_rwlock_rdlock(&meta_objectclose_lock);
if (tmp_object->magic_marker != METASLOT_OBJECT_MAGIC) {
(void) pthread_rwlock_unlock(&meta_objectclose_lock);
*object = NULL;
return (CKR_OBJECT_HANDLE_INVALID);
}
(void) pthread_rwlock_rdlock(&tmp_object->object_lock);
(void) pthread_rwlock_unlock(&meta_objectclose_lock);
*object = tmp_object;
return (CKR_OK);
}
/*
* meta_object_alloc
*
* Creates a new metaobject, but does not yet add it to the object list.
* Once the caller has finished initializing the object (by setting
* object attributes), meta_object_add should be called. This two-step
* process prevents others from seeing the object until fully intitialized.
*
*/
CK_RV
meta_object_alloc(meta_session_t *session, meta_object_t **object)
{
meta_object_t *new_object;
CK_ULONG num_slots;
/* Allocate memory for the object. */
new_object = calloc(1, sizeof (meta_object_t));
if (new_object == NULL)
return (CKR_HOST_MEMORY);
num_slots = meta_slotManager_get_slotcount();
new_object->clones = calloc(num_slots, sizeof (slot_object_t *));
if (new_object->clones == NULL) {
free(new_object);
return (CKR_HOST_MEMORY);
}
new_object->tried_create_clone = calloc(num_slots, sizeof (boolean_t));
if (new_object->tried_create_clone == NULL) {
free(new_object->clones);
free(new_object);
return (CKR_HOST_MEMORY);
}
/* Initialize the object fields. */
new_object->magic_marker = METASLOT_OBJECT_MAGIC;
(void) pthread_rwlock_init(&new_object->object_lock, NULL);
(void) pthread_rwlock_init(&new_object->attribute_lock, NULL);
(void) pthread_mutex_init(&new_object->clone_create_lock, NULL);
(void) pthread_mutex_init(&new_object->isClosingObject_lock, NULL);
new_object->creator_session = session;
*object = new_object;
return (CKR_OK);
}
/*
* meta_object_get_attr
*
* Get attribute values to fill in attribute values
* being kept in the metaslot object. The following 4 attributes
* in the meta_object_t structure will be filled in:
* isToken, isPrivate, isSensitive, isExtractable
*
* It's basically an easy way to do a C_GetAttributeValue.
* So, the hSession argument is assumed
* to be valid, and the pointer to meta_object_t is also assumed
* to be valid.
*/
CK_RV
meta_object_get_attr(slot_session_t *slot_session, CK_OBJECT_HANDLE hObject,
meta_object_t *object)
{
CK_BBOOL is_sensitive = object->isSensitive;
CK_BBOOL is_extractable = object->isExtractable;
CK_BBOOL is_token = B_FALSE, is_private = B_FALSE;
CK_KEY_TYPE keytype;
CK_OBJECT_CLASS class;
CK_ATTRIBUTE attrs[3];
CK_RV rv;
CK_SESSION_HANDLE hSession = slot_session->hSession;
CK_SLOT_ID fw_st_id = slot_session->fw_st_id;
int count = 1;
attrs[0].type = CKA_CLASS;
attrs[0].pValue = &class;
attrs[0].ulValueLen = sizeof (class);
if (object->isFreeObject != FREE_ENABLED) {
attrs[1].type = CKA_TOKEN;
attrs[1].pValue = &is_token;
attrs[1].ulValueLen = sizeof (is_token);
count++;
}
/*
* If this is a freeobject, we already know the Private value
* and we don't want to overwrite it with the wrong value
*/
if (object->isFreeObject <= FREE_DISABLED) {
attrs[count].type = CKA_PRIVATE;
attrs[count].pValue = &is_private;
attrs[count].ulValueLen = sizeof (is_private);
count++;
} else
is_private = object->isPrivate;
rv = FUNCLIST(fw_st_id)->C_GetAttributeValue(hSession, hObject,
attrs, count);
if (rv != CKR_OK) {
return (rv);
}
count = 0;
switch (class) {
case CKO_PRIVATE_KEY:
case CKO_SECRET_KEY:
/* Only need to check these for private & secret keys */
attrs[0].type = CKA_EXTRACTABLE;
attrs[0].pValue = &is_extractable;
attrs[0].ulValueLen = sizeof (is_extractable);
count = 1;
/*
* If this is a freeobject, we already know the Sensitive
* value and we don't want to overwrite it with the wrong
* value.
*/
if (object->isFreeObject <= FREE_DISABLED) {
attrs[1].type = CKA_SENSITIVE;
attrs[1].pValue = &is_sensitive;
attrs[1].ulValueLen = sizeof (is_sensitive);
count = 2;
/*
* We only need the key type if this is the first
* time we've looked at the object
*/
if (object->isFreeObject == FREE_UNCHECKED) {
attrs[2].type = CKA_KEY_TYPE;
attrs[2].pValue = &keytype;
attrs[2].ulValueLen = sizeof (keytype);
count = 3;
}
}
break;
case CKO_PUBLIC_KEY:
if (object->isFreeObject == FREE_UNCHECKED) {
attrs[count].type = CKA_KEY_TYPE;
attrs[count].pValue = &keytype;
attrs[count].ulValueLen = sizeof (keytype);
count++;
}
is_sensitive = CK_FALSE;
is_extractable = CK_TRUE;
break;
default:
object->isFreeObject = FREE_DISABLED;
is_sensitive = CK_FALSE;
is_extractable = CK_TRUE;
};
if (count > 0) {
rv = FUNCLIST(fw_st_id)->C_GetAttributeValue(hSession, hObject,
attrs, count);
if (rv != CKR_OK) {
return (rv);
}
if (object->isFreeObject == FREE_UNCHECKED) {
if (keytype == CKK_EC || keytype == CKK_RSA ||
keytype == CKK_DH) {
if (metaslot_config.auto_key_migrate) {
object->isFreeObject = FREE_DISABLED;
object->isFreeToken = FREE_DISABLED;
}
object->isFreeObject = FREE_ENABLED;
if (is_token)
object->isFreeToken = FREE_ENABLED;
} else
object->isFreeObject = FREE_DISABLED;
}
}
object->isToken = is_token;
object->isPrivate = is_private;
object->isSensitive = is_sensitive;
object->isExtractable = is_extractable;
return (CKR_OK);
}
/*
* meta_object_activate
*
* Add a new metaobject to the list of objects. See also meta_object_create,
* which would be called to create an object before it is added.
*/
void
meta_object_activate(meta_object_t *new_object)
{
pthread_rwlock_t *list_lock;
meta_object_t **list_head;
/*
* For session objects, we keep the list in the session that created
* this object, because this object will be destroyed when that session
* is closed.
*
* For token objects, the list is global (ie, not associated with any
* particular session).
*/
if (new_object->isToken) {
list_lock = &tokenobject_list_lock;
list_head = &tokenobject_list_head;
} else {
list_lock = &new_object->creator_session->object_list_lock;
list_head = &new_object->creator_session->object_list_head;
}
/* Add object to the list of objects. */
(void) pthread_rwlock_wrlock(list_lock);
INSERT_INTO_LIST(*list_head, new_object);
(void) pthread_rwlock_unlock(list_lock);
}
/*
* meta_object_deactivate
*
* Removes the object from the list of valid meta objects. Note
* that this function does not clean up any allocated
* resources (memory, object clones, etc). Cleaning up of
* allocated resources is done by calling the meta_object_dealloc()
*
*/
CK_RV
meta_object_deactivate(meta_object_t *object, boolean_t have_list_lock,
boolean_t have_object_lock)
{
pthread_rwlock_t *list_lock;
meta_object_t **list_head;
if (!have_object_lock) {
(void) pthread_rwlock_rdlock(&object->object_lock);
}
(void) pthread_mutex_lock(&object->isClosingObject_lock);
if (object->isClosingObject) {
/* Lost a delete race. */
(void) pthread_mutex_unlock(&object->isClosingObject_lock);
OBJRELEASE(object);
return (CKR_OBJECT_HANDLE_INVALID);
}
object->isClosingObject = B_TRUE;
(void) pthread_mutex_unlock(&object->isClosingObject_lock);
if (object->isToken || (object->isFreeToken == FREE_ENABLED)) {
list_lock = &tokenobject_list_lock;
list_head = &tokenobject_list_head;
} else {
list_lock = &object->creator_session->object_list_lock;
list_head = &object->creator_session->object_list_head;
}
/*
* Remove object from the object list. Once removed, it will not
* be possible for another thread to begin using the object.
*/
(void) pthread_rwlock_wrlock(&meta_objectclose_lock);
if (!have_list_lock) {
(void) pthread_rwlock_wrlock(list_lock);
}
object->magic_marker = METASLOT_OBJECT_BADMAGIC;
/*
* Can't use the regular REMOVE_FROM_LIST() function because
* that will miss the "error cleanup" situation where object is not yet
* in the list (object->next == NULL && object->prev == NULL)
*/
if (*list_head == object) {
/* Object is the first one in the list */
if (object->next) {
*list_head = object->next;
object->next->prev = NULL;
} else {
/* Object is the only one in the list */
*list_head = NULL;
}
} else if (object->next != NULL || object->prev != NULL) {
if (object->next) {
object->prev->next = object->next;
object->next->prev = object->prev;
} else {
/* Object is the last one in the list */
object->prev->next = NULL;
}
}
if (!have_list_lock) {
(void) pthread_rwlock_unlock(list_lock);
}
(void) pthread_rwlock_unlock(&meta_objectclose_lock);
/*
* Wait for anyone already using object to finish, by obtaining
* a writer-lock (need to release our reader-lock first). Once we
* get the write lock, we can just release it and finish cleaning
* up the object.
*/
(void) pthread_rwlock_unlock(&object->object_lock); /* rdlock */
(void) pthread_rwlock_wrlock(&object->object_lock);
(void) pthread_rwlock_unlock(&object->object_lock); /* wrlock */
return (CKR_OK);
}
/*
* meta_object_dealloc
*
* Performs final object cleanup, releasing any allocated memory and
* destroying any clones on other slots. Caller is assumed to have
* called meta_object_deactivate() before this function.
*
* Caller is assumed to have only reference to object, but should have
* released any lock.
*
* If "nukeSourceObj" argument is true, we will actually delete the
* object from the underlying slot.
*/
CK_RV
meta_object_dealloc(meta_session_t *session, meta_object_t *object,
boolean_t nukeSourceObj)
{
CK_RV rv, save_rv = CKR_OK;
CK_ULONG slotnum, num_slots;
CK_ULONG i;
/* First, delete all the clones of this object on other slots. */
num_slots = meta_slotManager_get_slotcount();
for (slotnum = 0; slotnum < num_slots; slotnum++) {
slot_session_t *obj_session;
slot_object_t *clone;
clone = object->clones[slotnum];
if (clone == NULL)
continue;
if (nukeSourceObj || (!object->isToken &&
!(object->isFreeToken == FREE_ENABLED &&
get_keystore_slotnum() == slotnum))) {
rv = meta_get_slot_session(slotnum, &obj_session,
(session == NULL) ?
object->creator_session->session_flags :
session->session_flags);
if (rv == CKR_OK) {
rv = FUNCLIST(obj_session->fw_st_id)->\
C_DestroyObject(obj_session->hSession,
clone->hObject);
meta_release_slot_session(obj_session);
if ((rv != CKR_OK) && (save_rv == CKR_OK)) {
save_rv = rv;
}
}
}
meta_slot_object_deactivate(clone);
meta_slot_object_dealloc(clone);
object->clones[slotnum] = NULL;
}
/* Now erase and delete any attributes in the metaobject. */
dealloc_attributes(object->attributes, object->num_attributes);
free(object->clones);
free(object->tried_create_clone);
if (object->clone_template) {
for (i = 0; i < object->clone_template_size; i++) {
freezero((object->clone_template)[i].pValue,
(object->clone_template)[i].ulValueLen);
}
free(object->clone_template);
}
/* Cleanup remaining object fields. */
(void) pthread_rwlock_destroy(&object->object_lock);
(void) pthread_rwlock_destroy(&object->attribute_lock);
(void) pthread_mutex_destroy(&object->isClosingObject_lock);
(void) pthread_mutex_destroy(&object->clone_create_lock);
meta_object_delay_free(object);
return (save_rv);
}
/*
* meta_slot_object_alloc
*/
CK_RV
meta_slot_object_alloc(slot_object_t **object)
{
slot_object_t *new_object;
new_object = calloc(1, sizeof (slot_object_t));
if (new_object == NULL)
return (CKR_HOST_MEMORY);
*object = new_object;
return (CKR_OK);
}
/*
* meta_slot_object_activate
*/
void
meta_slot_object_activate(slot_object_t *object,
slot_session_t *creator_session, boolean_t isToken)
{
object->creator_session = creator_session;
if (isToken) {
extern slot_data_t *slots;
slot_data_t *slot;
slot = &(slots[object->creator_session->slotnum]);
(void) pthread_rwlock_wrlock(&slot->tokenobject_list_lock);
INSERT_INTO_LIST(slot->tokenobject_list_head, object);
(void) pthread_rwlock_unlock(&slot->tokenobject_list_lock);
} else {
slot_session_t *session = object->creator_session;
/* Add to session's list of session objects. */
(void) pthread_rwlock_wrlock(&session->object_list_lock);
INSERT_INTO_LIST(session->object_list_head, object);
(void) pthread_rwlock_unlock(&session->object_list_lock);
}
/*
* This set tells the slot object that we are in the token list,
* but does not cause harm with the metaobject knowing the object
* isn't a token, but a freetoken
*/
object->isToken = isToken;
}
/*
* meta_slot_object_deactivate
*
* Remove the specified slot object from the appropriate object list.
*/
void
meta_slot_object_deactivate(slot_object_t *object)
{
slot_object_t **list_head;
pthread_rwlock_t *list_lock;
if (object->isToken) {
extern slot_data_t *slots;
slot_data_t *slot;
slot = &(slots[object->creator_session->slotnum]);
list_head = &slot->tokenobject_list_head;
list_lock = &slot->tokenobject_list_lock;
} else {
list_head = &object->creator_session->object_list_head;
list_lock = &object->creator_session->object_list_lock;
}
(void) pthread_rwlock_wrlock(list_lock);
REMOVE_FROM_LIST(*list_head, object);
(void) pthread_rwlock_unlock(list_lock);
}
/*
* meta_slot_object_dealloc
*/
void
meta_slot_object_dealloc(slot_object_t *object)
{
/* Not much cleanup for slot objects, unlike meta objects... */
free(object);
}
/*
* meta_object_copyin
*
* When a key is generated/derived/unwrapped, the attribute values
* created by the token are not immediately read into our copy of the
* attributes. We defer this work until we actually need to know.
*/
CK_RV
meta_object_copyin(meta_object_t *object)
{
CK_RV rv = CKR_OK;
slot_session_t *session = NULL;
CK_ATTRIBUTE *attrs = NULL, *attrs_with_val = NULL;
slot_object_t *slot_object = NULL;
CK_ULONG num_attrs = 0, i, num_attrs_with_val;
CK_SESSION_HANDLE hSession;
CK_SLOT_ID fw_st_id;
/* Make sure no one else is looking at attributes. */
(void) pthread_rwlock_wrlock(&object->attribute_lock);
/* Did we just lose a copyin race with another thread */
if (object->attributes != NULL) {
goto finish;
}
slot_object = object->clones[object->master_clone_slotnum];
rv = meta_get_slot_session(object->master_clone_slotnum, &session,
object->creator_session->session_flags);
if (rv != CKR_OK) {
goto finish;
}
/*
* first, get the master template of all the attributes
* for this object
*/
rv = get_master_attributes_by_object(session, slot_object,
&(object->attributes), &(object->num_attributes));
if (rv != CKR_OK) {
goto finish;
}
/*
* Get value for each attribute items.
*
* Some attributes are required by the given object type.
* Some are optional. Get all the values first, and then
* make sure we have value for all required values,
*/
attrs = calloc(object->num_attributes, sizeof (CK_ATTRIBUTE));
if (attrs == NULL) {
rv = CKR_HOST_MEMORY;
goto finish;
}
for (i = 0; i < object->num_attributes; i++) {
attrs[i].type =
((object->attributes[i]).attribute).type;
}
num_attrs = object->num_attributes;
hSession = session->hSession;
fw_st_id = session->fw_st_id;
/* first, call C_GetAttributeValue() to get size for each attribute */
rv = FUNCLIST(fw_st_id)->C_GetAttributeValue(hSession,
slot_object->hObject, attrs, num_attrs);
/*
* If the return value is not CKR_OK, allow it to be
* CKR_ATTRIBUTE_TYPE_INVALID for now.
* Some attributes defined in PKCS#11 version 2.11
* might not be defined in earlier versions. We will
* TRY to work with those providers if the attribute
* is optional.
*/
if ((rv != CKR_OK) && (rv != CKR_ATTRIBUTE_TYPE_INVALID)) {
rv = CKR_FUNCTION_FAILED; /* make sure rv is appropriate */
goto finish;
}
/*
* allocate space.
* Since we don't know how many attributes have
* values at this time, just assume all of them
* have values so we save one loop to count the number
* of attributes that have value.
*/
attrs_with_val = calloc(num_attrs, sizeof (CK_ATTRIBUTE));
if (attrs_with_val == NULL) {
rv = CKR_HOST_MEMORY;
goto finish;
}
num_attrs_with_val = 0;
for (i = 0; i < num_attrs; i++) {
if (!(((CK_LONG)(attrs[i].ulValueLen)) > 0)) {
/* if it isn't an optional attr, len should be > 0 */
if (!object->attributes[i].canBeEmptyValue) {
rv = CKR_FUNCTION_FAILED;
goto finish;
}
} else {
attrs_with_val[num_attrs_with_val].type = attrs[i].type;
attrs_with_val[num_attrs_with_val].ulValueLen =
attrs[i].ulValueLen;
attrs_with_val[num_attrs_with_val].pValue =
malloc(attrs[i].ulValueLen);
if (attrs_with_val[num_attrs_with_val].pValue == NULL) {
rv = CKR_HOST_MEMORY;
goto finish;
}
num_attrs_with_val++;
}
}
rv = FUNCLIST(fw_st_id)->C_GetAttributeValue(hSession,
slot_object->hObject, attrs_with_val, num_attrs_with_val);
if (rv != CKR_OK) {
goto finish;
}
/* store these values into the meta object */
for (i = 0; i < num_attrs_with_val; i++) {
rv = attribute_set_value(&(attrs_with_val[i]),
object->attributes, object->num_attributes);
if (rv != CKR_OK) {
goto finish;
}
}
finish:
(void) pthread_rwlock_unlock(&object->attribute_lock);
if (session)
meta_release_slot_session(session);
if (attrs) {
for (i = 0; i < num_attrs; i++) {
if (attrs[i].pValue != NULL) {
free(attrs[i].pValue);
}
}
free(attrs);
}
if (attrs_with_val) {
for (i = 0; i < num_attrs; i++) {
if (attrs_with_val[i].pValue != NULL) {
freezero(attrs_with_val[i].pValue,
attrs_with_val[i].ulValueLen);
}
}
free(attrs_with_val);
}
return (rv);
}
/*
* Create an object to be used for wrapping and unwrapping.
* The same template will be used for all wrapping/unwrapping keys all
* the time
*/
static CK_RV
create_wrap_unwrap_key(slot_session_t *slot_session, CK_OBJECT_HANDLE *hObject,
wrap_info_t *wrap_info, char *key_data, CK_ULONG key_len)
{
CK_OBJECT_CLASS objclass;
CK_KEY_TYPE keytype;
CK_RV rv = CKR_OK;
int i;
CK_ATTRIBUTE template[WRAP_KEY_TEMPLATE_SIZE];
i = 0;
objclass = wrap_info->class;
template[i].type = CKA_CLASS;
template[i].pValue = &objclass;
template[i].ulValueLen = sizeof (objclass);
i++;
keytype = wrap_info->key_type;
template[i].type = CKA_KEY_TYPE;
template[i].pValue = &keytype;
template[i].ulValueLen = sizeof (keytype);
i++;
template[i].type = CKA_TOKEN;
template[i].pValue = &falsevalue;
template[i].ulValueLen = sizeof (falsevalue);
if (objclass == CKO_SECRET_KEY) {
i++;
template[i].type = CKA_VALUE;
template[i].pValue = key_data;
template[i].ulValueLen = key_len;
i++;
template[i].type = CKA_WRAP;
template[i].pValue = &truevalue;
template[i].ulValueLen = sizeof (truevalue);
i++;
template[i].type = CKA_UNWRAP;
template[i].pValue = &truevalue;
template[i].ulValueLen = sizeof (truevalue);
} else {
/* Modulus is the same for rsa public and private key */
i++;
template[i].type = CKA_MODULUS;
template[i].pValue = Modulus;
template[i].ulValueLen = sizeof (Modulus);
if (objclass == CKO_PUBLIC_KEY) {
/* RSA public key */
i++;
template[i].type = CKA_PUBLIC_EXPONENT;
template[i].pValue = PubExpo;
template[i].ulValueLen = sizeof (PubExpo);
i++;
template[i].type = CKA_WRAP;
template[i].pValue = &truevalue;
template[i].ulValueLen = sizeof (truevalue);
} else {
/* RSA private key */
i++;
template[i].type = CKA_PRIVATE_EXPONENT;
template[i].pValue = PriExpo;
template[i].ulValueLen = sizeof (PriExpo);
i++;
template[i].type = CKA_UNWRAP;
template[i].pValue = &truevalue;
template[i].ulValueLen = sizeof (truevalue);
}
}
rv = FUNCLIST(slot_session->fw_st_id)->C_CreateObject(
slot_session->hSession, template, i + 1, hObject);
return (rv);
}
/*
* Create a clone of a non-sensitive and extractable object.
* If the template required for creating the clone doesn't exist,
* it will be retrieved from the master clone.
*/
static CK_RV
clone_by_create(meta_object_t *object, slot_object_t *new_clone,
slot_session_t *dst_slot_session)
{
CK_RV rv;
int free_token_index = -1;
if (object->attributes == NULL) {
rv = meta_object_copyin(object);
if (rv != CKR_OK) {
return (rv);
}
}
if (object->clone_template == NULL) {
rv = meta_clone_template_setup(object, object->attributes,
object->num_attributes);
if (rv != CKR_OK) {
return (rv);
}
}
if (object->isFreeToken == FREE_ENABLED) {
if (dst_slot_session->slotnum == get_keystore_slotnum())
free_token_index = set_template_boolean(CKA_TOKEN,
object->clone_template,
object->clone_template_size, B_FALSE, &truevalue);
else
free_token_index = set_template_boolean(CKA_TOKEN,
object->clone_template,
object->clone_template_size, B_FALSE, &falsevalue);
}
/* Create the clone... */
rv = FUNCLIST(dst_slot_session->fw_st_id)->C_CreateObject(
dst_slot_session->hSession, object->clone_template,
object->clone_template_size, &(new_clone->hObject));
if (free_token_index != -1) {
free_token_index = set_template_boolean(CKA_TOKEN,
object->clone_template, object->clone_template_size,
B_FALSE, &falsevalue);
}
if (rv != CKR_OK) {
return (rv);
}
return (CKR_OK);
}
/*
* Goes through the list of wraping mechanisms, and returns the first
* one that is supported by both the source and the destination slot.
* If none of the mechanisms are supported by both slot, return the
* first mechanism that's supported by the source slot
*/
static CK_RV
find_best_match_wrap_mech(wrap_info_t *wrap_info, int num_info,
CK_ULONG src_slotnum, CK_ULONG dst_slotnum, int *first_both_mech,
int *first_src_mech)
{
int i;
boolean_t src_supports, dst_supports;
CK_RV rv;
CK_MECHANISM_INFO mech_info;
mech_info.flags = CKF_WRAP;
for (i = 0; i < num_info; i++) {
src_supports = B_FALSE;
dst_supports = B_FALSE;
rv = meta_mechManager_slot_supports_mech(
(wrap_info[i]).mech_type, src_slotnum,
&src_supports, NULL, B_FALSE, &mech_info);
if (rv != CKR_OK) {
return (rv);
}
rv = meta_mechManager_slot_supports_mech(
(wrap_info[i]).mech_type, dst_slotnum,
&dst_supports, NULL, B_FALSE, &mech_info);
if (rv != CKR_OK) {
return (rv);
}
/* both source and destination supports the mech */
if ((src_supports) && (dst_supports)) {
*first_both_mech = i;
return (CKR_OK);
}
if ((src_supports) && (*first_src_mech == -1)) {
*first_src_mech = i;
}
}
return (CKR_OK);
}
/*
* Determine the wrapping/unwrapping mechanism to be used
*
* If possible, select a mechanism that's supported by both source
* and destination slot. If none of the mechanisms are supported
* by both slot, then, select the first one supported by
* the source slot.
*/
static CK_RV
get_wrap_mechanism(CK_OBJECT_CLASS obj_class, CK_KEY_TYPE key_type,
CK_ULONG src_slotnum, CK_ULONG dst_slotnum, wrap_info_t *wrap_info)
{
wrap_info_t *wrap_info_to_search = NULL;
unsigned int num_wrap_info;
CK_RV rv;
int i;
boolean_t src_supports = B_FALSE, dst_supports = B_FALSE;
int first_src_mech, rsa_first_src_mech, first_both_mech;
CK_MECHANISM_INFO mech_info;
mech_info.flags = CKF_WRAP;
if ((obj_class == CKO_PRIVATE_KEY) && (key_type == CKK_KEA)) {
/*
* only SKIPJACK keys can be used for wrapping
* KEA private keys
*/
for (i = 0; i < num_special_wrap_info; i++) {
if ((special_wrap_info[i]).mech_type
!= CKM_SKIPJACK_WRAP) {
continue;
}
src_supports = B_FALSE;
dst_supports = B_FALSE;
rv = meta_mechManager_slot_supports_mech(
(special_wrap_info[i]).mech_type, src_slotnum,
&src_supports, NULL, B_FALSE, &mech_info);
if (rv != CKR_OK) {
goto finish;
}
rv = meta_mechManager_slot_supports_mech(
(special_wrap_info[i]).mech_type, dst_slotnum,
&dst_supports, NULL, B_FALSE, &mech_info);
if (rv != CKR_OK) {
goto finish;
}
if (src_supports) {
/*
* both src and dst supports the mech or
* only the src supports the mech
*/
(void) memcpy(wrap_info,
&(special_wrap_info[i]),
sizeof (wrap_info_t));
wrap_info->src_supports = src_supports;
wrap_info->dst_supports = dst_supports;
rv = CKR_OK;
goto finish;
}
}
/*
* if we are here, that means neither the source slot
* nor the destination slots supports CKM_SKIPJACK_WRAP.
*/
rv = CKR_FUNCTION_FAILED;
goto finish;
}
if ((key_type == CKK_SKIPJACK) || (key_type == CKK_BATON) ||
(key_type == CKK_JUNIPER)) {
/* special key types */
wrap_info_to_search = special_wrap_info;
num_wrap_info = num_special_wrap_info;
} else {
/* use the regular wrapping mechanisms */
wrap_info_to_search = common_wrap_info;
num_wrap_info = num_common_wrap_info;
}
first_both_mech = -1;
first_src_mech = -1;
rv = find_best_match_wrap_mech(wrap_info_to_search, num_wrap_info,
src_slotnum, dst_slotnum, &first_both_mech, &first_src_mech);
if (rv != CKR_OK) {
goto finish;
}
if (first_both_mech != -1) {
(void) memcpy(wrap_info,
&(wrap_info_to_search[first_both_mech]),
sizeof (wrap_info_t));
wrap_info->src_supports = B_TRUE;
wrap_info->dst_supports = B_TRUE;
rv = CKR_OK;
goto finish;
}
/*
* If we are here, we did not find a mechanism that's supported
* by both source and destination slot.
*
* If it is a secret key, can also try to wrap it with
* a RSA public key
*/
if (obj_class == CKO_SECRET_KEY) {
first_both_mech = -1;
rsa_first_src_mech = -1;
rv = find_best_match_wrap_mech(rsa_wrap_info,
num_rsa_wrap_info, src_slotnum, dst_slotnum,
&first_both_mech, &rsa_first_src_mech);
if (rv != CKR_OK) {
goto finish;
}
if (first_both_mech > -1) {
(void) memcpy(wrap_info,
&(rsa_wrap_info[first_both_mech]),
sizeof (wrap_info_t));
wrap_info->src_supports = B_TRUE;
wrap_info->dst_supports = B_TRUE;
rv = CKR_OK;
goto finish;
}
}
/*
* if we are here, that means none of the mechanisms are supported
* by both the source and the destination
*/
if (first_src_mech > -1) {
/* source slot support one of the secret key mechs */
(void) memcpy(wrap_info,
&(wrap_info_to_search[first_src_mech]),
sizeof (wrap_info_t));
wrap_info->src_supports = B_TRUE;
wrap_info->dst_supports = B_FALSE;
rv = CKR_OK;
} else if (rsa_first_src_mech > -1) {
/* source slot support one of the RSA mechs */
(void) memcpy(wrap_info, &(rsa_wrap_info[rsa_first_src_mech]),
sizeof (wrap_info_t));
wrap_info->src_supports = B_TRUE;
wrap_info->dst_supports = B_FALSE;
rv = CKR_OK;
} else {
/* neither source nor destination support any wrap mechs */
rv = CKR_FUNCTION_FAILED;
}
finish:
return (rv);
}
/*
* This is called if the object to be cloned is a sensitive object
*/
static CK_RV
clone_by_wrap(meta_object_t *object, slot_object_t *new_clone,
slot_session_t *dst_slot_session)
{
slot_session_t *src_slot_session = NULL;
CK_OBJECT_HANDLE wrappingKey = 0, unwrappingKey = 0;
CK_MECHANISM wrappingMech;
CK_BYTE *wrappedKey = NULL;
CK_ULONG wrappedKeyLen = 0;
slot_object_t *slot_object = NULL;
CK_RV rv = CKR_OK;
CK_OBJECT_HANDLE unwrapped_obj;
meta_object_t *tmp_meta_obj = NULL;
slot_object_t *tmp_slot_obj = NULL;
CK_OBJECT_CLASS obj_class;
CK_KEY_TYPE key_type;
meta_session_t *tmp_meta_session = NULL;
CK_ATTRIBUTE unwrap_template[4];
char key_data[1024]; /* should be big enough for any key size */
char ivbuf[1024]; /* should be big enough for any mech */
wrap_info_t wrap_info;
CK_ULONG key_len, unwrap_template_size;
slot_object = object->clones[object->master_clone_slotnum];
rv = meta_get_slot_session(object->master_clone_slotnum,
&src_slot_session, object->creator_session->session_flags);
if (rv != CKR_OK) {
return (rv);
}
/*
* get the object class and key type for unwrap template
* This information will also be used for determining
* which wrap mechanism and which key to use for
* doing the wrapping
*/
unwrap_template[0].type = CKA_CLASS;
unwrap_template[0].pValue = &obj_class;
unwrap_template[0].ulValueLen = sizeof (obj_class);
unwrap_template[1].type = CKA_KEY_TYPE;
unwrap_template[1].pValue = &key_type;
unwrap_template[1].ulValueLen = sizeof (key_type);
rv = FUNCLIST(src_slot_session->fw_st_id)->C_GetAttributeValue(
src_slot_session->hSession, slot_object->hObject,
unwrap_template, 2);
if (rv != CKR_OK) {
goto finish;
}
rv = get_wrap_mechanism(obj_class, key_type, src_slot_session->slotnum,
dst_slot_session->slotnum, &wrap_info);
if (rv != CKR_OK) {
goto finish;
}
/*
* read number of bytes required from random device for
* creating a secret key for wrapping and unwrapping
*/
if (wrap_info.class == CKO_SECRET_KEY) {
/*
* /dev/urandom will be used for generating the key used
* for doing the wrap/unwrap. It's should be ok to
* use /dev/urandom because this key is used for this
* one time operation only. It doesn't need to be stored.
*/
key_len = wrap_info.key_length;
if (pkcs11_get_urandom(key_data, key_len) < 0) {
rv = CKR_FUNCTION_FAILED;
goto finish;
}
if (wrap_info.iv_length > 0) {
if (pkcs11_get_urandom(
ivbuf, wrap_info.iv_length) < 0) {
rv = CKR_FUNCTION_FAILED;
goto finish;
}
}
}
/* create the wrapping key */
rv = create_wrap_unwrap_key(src_slot_session, &wrappingKey,
&wrap_info, key_data, key_len);
if (rv != CKR_OK) {
goto finish;
}
wrappingMech.mechanism = wrap_info.mech_type;
wrappingMech.pParameter = ((wrap_info.iv_length > 0) ? ivbuf : NULL);
wrappingMech.ulParameterLen = wrap_info.iv_length;
/* get the size of the wrapped key */
rv = FUNCLIST(src_slot_session->fw_st_id)->C_WrapKey(
src_slot_session->hSession, &wrappingMech,
wrappingKey, slot_object->hObject, NULL, &wrappedKeyLen);
if (rv != CKR_OK) {
goto finish;
}
wrappedKey = malloc(wrappedKeyLen * sizeof (CK_BYTE));
if (wrappedKey == NULL) {
rv = CKR_HOST_MEMORY;
goto finish;
}
/* do the actual key wrapping */
rv = FUNCLIST(src_slot_session->fw_st_id)->C_WrapKey(
src_slot_session->hSession, &wrappingMech,
wrappingKey, slot_object->hObject, wrappedKey, &wrappedKeyLen);
if (rv != CKR_OK) {
goto finish;
}
/* explicitly force the unwrapped object to be not sensitive */
unwrap_template[2].type = CKA_SENSITIVE;
unwrap_template[2].pValue = &falsevalue;
unwrap_template[2].ulValueLen = sizeof (falsevalue);
unwrap_template[3].type = CKA_TOKEN;
unwrap_template[3].pValue = &falsevalue;
unwrap_template[3].ulValueLen = sizeof (falsevalue);
unwrap_template_size =
sizeof (unwrap_template) / sizeof (CK_ATTRIBUTE);
if (!wrap_info.dst_supports) {
/*
* if we know for sure that the destination slot doesn't
* support the wrapping mechanism, no point in trying.
* go directly to unwrap in source slot, and create key
* in destination
*/
goto unwrap_in_source;
}
/* create the unwrapping key in destination slot */
if (wrap_info.key_type == CKK_RSA) {
/* for RSA key, the unwrapping key need to be private key */
wrap_info.class = CKO_PRIVATE_KEY;
}
rv = create_wrap_unwrap_key(dst_slot_session,
&unwrappingKey, &wrap_info, key_data, key_len);
if (rv != CKR_OK) {
goto finish;
}
rv = FUNCLIST(dst_slot_session->fw_st_id)->C_UnwrapKey(
dst_slot_session->hSession, &wrappingMech,
unwrappingKey, wrappedKey, wrappedKeyLen, unwrap_template,
unwrap_template_size, &(new_clone->hObject));
if (rv != CKR_OK) {
unwrap_in_source:
/*
* There seemed to be a problem with unwrapping in the
* destination slot.
* Try to do the unwrap in the src slot so it becomes
* a non-sensitive object, then, get all the attributes
* and create the object in the destination slot
*/
if (wrap_info.class == CKO_SECRET_KEY) {
/* unwrap with same key used for wrapping */
rv = FUNCLIST(src_slot_session->fw_st_id)->C_UnwrapKey(
src_slot_session->hSession,
&wrappingMech, wrappingKey, wrappedKey,
wrappedKeyLen, unwrap_template,
unwrap_template_size, &(unwrapped_obj));
} else {
/*
* If the object is wrapping with RSA public key, need
* need to create RSA private key for unwrapping
*/
wrap_info.class = CKO_PRIVATE_KEY;
rv = create_wrap_unwrap_key(src_slot_session,
&unwrappingKey, &wrap_info, key_data, key_len);
if (rv != CKR_OK) {
goto finish;
}
rv = FUNCLIST(src_slot_session->fw_st_id)->C_UnwrapKey(
src_slot_session->hSession,
&wrappingMech, unwrappingKey, wrappedKey,
wrappedKeyLen, unwrap_template,
unwrap_template_size, &(unwrapped_obj));
}
if (rv != CKR_OK) {
goto finish;
}
rv = meta_session_alloc(&tmp_meta_session);
if (rv != CKR_OK) {
goto finish;
}
tmp_meta_session->session_flags = CKF_SERIAL_SESSION;
rv = meta_object_alloc(tmp_meta_session, &tmp_meta_obj);
if (rv != CKR_OK) {
goto finish;
}
rv = meta_slot_object_alloc(&tmp_slot_obj);
if (rv != CKR_OK) {
goto finish;
}
tmp_meta_obj->master_clone_slotnum = src_slot_session->slotnum;
tmp_slot_obj->hObject = unwrapped_obj;
tmp_meta_obj->clones[tmp_meta_obj->master_clone_slotnum]
= tmp_slot_obj;
meta_slot_object_activate(tmp_slot_obj, src_slot_session,
B_FALSE);
tmp_slot_obj = NULL;
rv = clone_by_create(tmp_meta_obj, new_clone,
dst_slot_session);
if (rv != CKR_OK) {
goto finish;
}
}
finish:
if (unwrappingKey) {
(void) FUNCLIST(dst_slot_session->fw_st_id)->C_DestroyObject(
dst_slot_session->hSession, unwrappingKey);
}
if (wrappingKey) {
(void) FUNCLIST(src_slot_session->fw_st_id)->C_DestroyObject(
src_slot_session->hSession, wrappingKey);
}
if (tmp_slot_obj) {
(void) meta_slot_object_dealloc(tmp_slot_obj);
}
if (tmp_meta_obj) {
(void) meta_object_dealloc(tmp_meta_session, tmp_meta_obj,
B_TRUE);
}
if (tmp_meta_session) {
(void) meta_session_dealloc(tmp_meta_session);
}
if (wrappedKey) {
freezero(wrappedKey, wrappedKeyLen);
}
if (src_slot_session) {
meta_release_slot_session(src_slot_session);
}
return (rv);
}
/*
* meta_object_get_clone
*
* Creates a "clone" of a metaobject on the specified slot. A clone is a
* copy of the object.
*
* Clones are cached, so that they can be reused with subsequent operations.
*/
CK_RV
meta_object_get_clone(meta_object_t *object,
CK_ULONG slot_num, slot_session_t *slot_session, slot_object_t **clone)
{
CK_RV rv = CKR_OK;
slot_object_t *newclone = NULL;
/* Does a clone already exist? */
if (object->clones[slot_num] != NULL) {
*clone = object->clones[slot_num];
return (CKR_OK);
}
if ((object->isSensitive) && (object->isToken) &&
(!metaslot_auto_key_migrate)) {
/*
* if the object is a sensitive token object, and auto
* key migrate is not allowed, will not create the clone
* in another slot
*/
return (CKR_FUNCTION_FAILED);
}
/* object attributes can't be extracted and attributes are not known */
if ((!object->isExtractable) && (object->attributes == NULL)) {
return (CKR_FUNCTION_FAILED);
}
(void) pthread_mutex_lock(&object->clone_create_lock);
/* Maybe someone just created one? */
if (object->clones[slot_num] != NULL) {
*clone = object->clones[slot_num];
goto finish;
}
/*
* has an attempt already been made to create this object in
* slot? If yes, and there's no clone, as indicated above,
* that means this object can't be created in this slot.
*/
if (object->tried_create_clone[slot_num]) {
(void) pthread_mutex_unlock(&object->clone_create_lock);
return (CKR_FUNCTION_FAILED);
}
rv = meta_slot_object_alloc(&newclone);
if (rv != CKR_OK)
goto finish;
object->tried_create_clone[slot_num] = B_TRUE;
/*
* If this object is sensitive and we do not have not copied in the
* attributes via FreeObject functionality, then we need to wrap it off
* the provider. If we do have attributes, we can just create the
* clone
*/
if (object->isSensitive && object->attributes == NULL) {
rv = clone_by_wrap(object, newclone, slot_session);
} else {
rv = clone_by_create(object, newclone, slot_session);
}
if (rv != CKR_OK) {
goto finish;
}
object->clones[slot_num] = newclone;
meta_slot_object_activate(newclone, slot_session, object->isToken);
*clone = newclone;
newclone = NULL;
finish:
(void) pthread_mutex_unlock(&object->clone_create_lock);
if (newclone)
meta_slot_object_dealloc(newclone);
return (rv);
}
/*
* meta_setup_clone_template
*
* Create a clone template for the specified object.
*/
static CK_RV
meta_clone_template_setup(meta_object_t *object,
const generic_attr_t *attributes, size_t num_attributes)
{
CK_RV rv = CKR_OK;
CK_ATTRIBUTE *clone_template;
size_t i, c = 0;
clone_template = malloc(num_attributes * sizeof (CK_ATTRIBUTE));
if (clone_template == NULL) {
rv = CKR_HOST_MEMORY;
goto finish;
}
/* Don't allow attributes to change while we look at them. */
(void) pthread_rwlock_rdlock(&object->attribute_lock);
for (i = 0; i < num_attributes; i++) {
if (!attributes[i].isCloneAttr ||
(attributes[i].attribute.type == CKA_TOKEN &&
object->isFreeToken == FREE_DISABLED)) {
continue;
}
if ((!(attributes[i].hasValueForClone)) &&
(attributes[i].canBeEmptyValue)) {
continue;
}
clone_template[c].type = attributes[i].attribute.type;
clone_template[c].ulValueLen =
attributes[i].attribute.ulValueLen;
/* Allocate space to store the attribute value. */
clone_template[c].pValue = malloc(clone_template[c].ulValueLen);
if (clone_template[c].pValue == NULL) {
free(clone_template);
rv = CKR_HOST_MEMORY;
(void) pthread_rwlock_unlock(&object->attribute_lock);
goto finish;
}
(void) memcpy(clone_template[c].pValue,
object->attributes[i].attribute.pValue,
clone_template[c].ulValueLen);
c++;
}
(void) pthread_rwlock_unlock(&object->attribute_lock);
object->clone_template = clone_template;
object->clone_template_size = c;
finish:
return (rv);
}
/*
* meta_object_find_by_handle
*
* Search for an existing metaobject, using the object handle of a clone
* on a particular slot.
*
* Returns a matching metaobject, or NULL if no match was found.
*/
meta_object_t *
meta_object_find_by_handle(CK_OBJECT_HANDLE hObject, CK_ULONG slotnum,
boolean_t token_only)
{
meta_object_t *object = NULL, *tmp_obj;
meta_session_t *session;
if (!token_only) {
(void) pthread_rwlock_rdlock(&meta_sessionlist_lock);
session = meta_sessionlist_head;
while (session != NULL) {
/* lock the objects list while we look at it */
(void) pthread_rwlock_rdlock(
&(session->object_list_lock));
tmp_obj = session->object_list_head;
while (tmp_obj != NULL) {
slot_object_t *slot_object;
(void) pthread_rwlock_rdlock(
&(tmp_obj->object_lock));
slot_object = tmp_obj->clones[slotnum];
if (slot_object != NULL) {
if (slot_object->hObject == hObject) {
object = tmp_obj;
}
}
(void) pthread_rwlock_unlock(
&(tmp_obj->object_lock));
if (object != NULL) {
break;
}
tmp_obj = tmp_obj->next;
}
(void) pthread_rwlock_unlock(
&(session->object_list_lock));
if (object != NULL) {
break;
}
session = session->next;
}
(void) pthread_rwlock_unlock(&meta_sessionlist_lock);
}
if (object != NULL) {
/* found the object, no need to look further */
return (object);
}
/*
* Look at list of token objects
*/
(void) pthread_rwlock_rdlock(&tokenobject_list_lock);
tmp_obj = tokenobject_list_head;
while (tmp_obj != NULL) {
slot_object_t *slot_object;
(void) pthread_rwlock_rdlock(&(tmp_obj->object_lock));
slot_object = tmp_obj->clones[slotnum];
if (slot_object != NULL) {
if (slot_object->hObject == hObject)
object = tmp_obj;
}
(void) pthread_rwlock_unlock(&(tmp_obj->object_lock));
if (object != NULL) {
break;
}
tmp_obj = tmp_obj->next;
}
(void) pthread_rwlock_unlock(&tokenobject_list_lock);
return (object);
}
CK_RV
meta_token_object_deactivate(token_obj_type_t token_type)
{
meta_object_t *object, *tmp_object;
CK_RV save_rv = CKR_OK, rv;
/* get a write lock on the token object list */
(void) pthread_rwlock_wrlock(&tokenobject_list_lock);
object = tokenobject_list_head;
/* go through each object and delete the one with matching type */
while (object != NULL) {
tmp_object = object->next;
if ((token_type == ALL_TOKEN) ||
((object->isPrivate) && (token_type == PRIVATE_TOKEN)) ||
((!object->isPrivate) && (token_type == PUBLIC_TOKEN))) {
rv = meta_object_deactivate(object, B_TRUE, B_FALSE);
if ((rv != CKR_OK) && (save_rv == CKR_OK)) {
save_rv = rv;
goto finish;
}
rv = meta_object_dealloc(NULL, object, B_FALSE);
if ((rv != CKR_OK) && (save_rv == CKR_OK)) {
save_rv = rv;
goto finish;
}
}
object = tmp_object;
}
finish:
(void) pthread_rwlock_unlock(&tokenobject_list_lock);
return (save_rv);
}
/*
* This function adds the to-be-freed meta object to a linked list.
* When the number of objects queued in the linked list reaches the
* maximum threshold MAX_OBJ_TO_BE_FREED, it will free the first
* object (FIFO) in the list.
*/
void
meta_object_delay_free(meta_object_t *objp)
{
meta_object_t *tmp;
(void) pthread_mutex_lock(&obj_delay_freed.obj_to_be_free_mutex);
/* Add the newly deleted object at the end of the list */
objp->next = NULL;
if (obj_delay_freed.first == NULL) {
obj_delay_freed.last = objp;
obj_delay_freed.first = objp;
} else {
obj_delay_freed.last->next = objp;
obj_delay_freed.last = objp;
}
if (++obj_delay_freed.count >= MAX_OBJ_TO_BE_FREED) {
/*
* Free the first object in the list only if
* the total count reaches maximum threshold.
*/
obj_delay_freed.count--;
tmp = obj_delay_freed.first->next;
free(obj_delay_freed.first);
obj_delay_freed.first = tmp;
}
(void) pthread_mutex_unlock(&obj_delay_freed.obj_to_be_free_mutex);
}
/*
* This function checks if the object passed can be a freeobject.
*
* If there is more than one provider that supports the supported freeobject
* mechanisms then allow freeobjects to be an option.
*/
boolean_t
meta_freeobject_check(meta_session_t *session, meta_object_t *object,
CK_MECHANISM *pMech, CK_ATTRIBUTE *tmpl, CK_ULONG tmpl_len,
CK_KEY_TYPE keytype)
{
mech_support_info_t *info = &(session->mech_support_info);
/*
* If key migration is turned off, or the object does not has any of
* the required flags and there is only one slot, then we don't need
* FreeObjects.
*/
if (!metaslot_auto_key_migrate ||
(!object->isToken && !object->isSensitive &&
meta_slotManager_get_slotcount() < 2))
goto failure;
/*
* If this call is for key generation, check pMech for supported
* FreeObject mechs
*/
if (pMech != NULL) {
if (pMech->mechanism == CKM_RSA_PKCS_KEY_PAIR_GEN ||
pMech->mechanism == CKM_EC_KEY_PAIR_GEN ||
pMech->mechanism == CKM_DH_PKCS_KEY_PAIR_GEN ||
pMech->mechanism == CKM_DH_PKCS_DERIVE)
info->mech = pMech->mechanism;
else
goto failure;
/*
* If this call is for an object creation, look inside the template
* for supported FreeObject mechs
*/
} else if (tmpl_len > 0) {
if (!get_template_ulong(CKA_KEY_TYPE, tmpl, tmpl_len, &keytype))
goto failure;
switch (keytype) {
case CKK_RSA:
info->mech = CKM_RSA_PKCS_KEY_PAIR_GEN;
break;
case CKK_EC:
info->mech = CKM_EC_KEY_PAIR_GEN;
break;
case CKK_DH:
info->mech = CKM_DH_PKCS_KEY_PAIR_GEN;
break;
default:
goto failure;
}
} else
goto failure;
/* Get the slot that support this mech... */
if (meta_mechManager_get_slots(info, B_FALSE, NULL) != CKR_OK)
goto failure;
/*
* If there is only one slot with the mech or the first slot in
* the list is the keystore slot, we should bail.
*/
if (info->num_supporting_slots < 2 &&
info->supporting_slots[0]->slotnum == get_keystore_slotnum())
goto failure;
if (object->isToken)
object->isFreeToken = FREE_ALLOWED_KEY;
else
object->isFreeToken = FREE_DISABLED;
object->isFreeObject = FREE_ALLOWED_KEY;
return (B_TRUE);
failure:
object->isFreeToken = FREE_DISABLED;
object->isFreeObject = FREE_DISABLED;
return (B_FALSE);
}
/*
* This function assumes meta_freeobject_check() has just been called and set
* the isFreeObject and/or isFreeToken vars to FREE_ALLOWED_KEY.
*
* If the template value for CKA_PRIVATE, CKA_SENSITIVE and/or CKA_TOKEN are
* true, then isFreeObject is fully enabled. In addition isFreeToken is
* enabled if is CKA_TOKEN true.
*
* If create is true, we are doing a C_CreateObject operation and don't
* handle CKA_PRIVATE & CKA_SENSITIVE flags, we only care about CKA_TOKEN.
*/
boolean_t
meta_freeobject_set(meta_object_t *object, CK_ATTRIBUTE *tmpl,
CK_ULONG tmpl_len, boolean_t create)
{
/* This check should never be true, if it is, it's a bug */
if (object->isFreeObject < FREE_ALLOWED_KEY)
return (B_FALSE);
if (!create) {
/* Turn off the Sensitive flag */
if (object->isSensitive) {
if (set_template_boolean(CKA_SENSITIVE, tmpl, tmpl_len,
B_TRUE, &falsevalue) == -1)
goto failure;
object->isFreeObject = FREE_ENABLED;
}
/* Turn off the Private flag */
if (object->isPrivate) {
if (set_template_boolean(CKA_PRIVATE, tmpl, tmpl_len,
B_TRUE, &falsevalue) == -1)
goto failure;
object->isFreeObject = FREE_ENABLED;
}
}
if (object->isToken) {
object->isToken = B_FALSE;
object->isFreeToken = FREE_ENABLED;
object->isFreeObject = FREE_ENABLED;
} else
object->isFreeToken = FREE_DISABLED;
/*
* If isFreeObject is not in the FREE_ENABLED state yet, it can be
* turned off because the object doesn't not need to be a FreeObject.
*/
if (object->isFreeObject == FREE_ALLOWED_KEY)
object->isFreeObject = FREE_DISABLED;
return (B_TRUE);
failure:
object->isFreeToken = FREE_DISABLED;
object->isFreeObject = FREE_DISABLED;
return (B_FALSE);
}
/*
* This function sets the CKA_TOKEN flag on a given object template depending
* if the slot being used is a keystore.
*
* If the object is a token, but the slot is not the system keystore or has
* no keystore, then set the template to token = false; otherwise it's true.
* In addition we know ahead of time what the value is, so if the value is
* already correct, bypass the setting function
*/
CK_RV
meta_freetoken_set(CK_ULONG slot_num, CK_BBOOL *current_value,
CK_ATTRIBUTE *tmpl, CK_ULONG tmpl_len)
{
if (slot_num == get_keystore_slotnum()) {
if (*current_value == TRUE)
return (CKR_OK);
if (set_template_boolean(CKA_TOKEN, tmpl, tmpl_len, B_TRUE,
&truevalue) == -1)
return (CKR_FUNCTION_FAILED);
} else {
if (*current_value == FALSE)
return (CKR_OK);
if (set_template_boolean(CKA_TOKEN, tmpl, tmpl_len, B_TRUE,
&falsevalue) == -1)
return (CKR_FUNCTION_FAILED);
*current_value = FALSE;
}
return (CKR_OK);
}
/*
* Cloning function for meta_freeobject_clone() to use. This function
* is streamlined because we know what the object is and this should
* not be called as a generic cloner.
*/
static CK_RV
meta_freeobject_clone_maker(meta_session_t *session, meta_object_t *object,
CK_ULONG slotnum)
{
slot_object_t *slot_object = NULL;
slot_session_t *slot_session = NULL;
CK_RV rv;
rv = meta_slot_object_alloc(&slot_object);
if (rv != CKR_OK)
goto cleanup;
rv = meta_get_slot_session(slotnum, &slot_session,
session->session_flags);
if (rv != CKR_OK)
goto cleanup;
rv = clone_by_create(object, slot_object, slot_session);
if (rv == CKR_OK) {
object->clones[slotnum] = slot_object;
meta_slot_object_activate(slot_object, slot_session, B_TRUE);
}
cleanup:
meta_release_slot_session(slot_session);
return (rv);
}
/*
* This function is called when a object is a FreeObject.
*
* What we are given is an object that has been generated on a provider
* that is not its final usage place. That maybe because:
* 1) it's a token and needs to be stored in keystore.
* 2) it was to be a private/sensitive object that we modified so we could know
* the important attributes for cloning before we make it private/sensitive.
*/
boolean_t
meta_freeobject_clone(meta_session_t *session, meta_object_t *object)
{
CK_RV rv;
CK_ULONG keystore_slotnum;
CK_ATTRIBUTE attr[2];
boolean_t failover = B_FALSE;
if (object->attributes == NULL) {
rv = meta_object_copyin(object);
if (rv != CKR_OK)
return (rv);
}
if (object->isPrivate) {
CK_OBJECT_HANDLE new_clone;
CK_ULONG slotnum = object->master_clone_slotnum;
slot_session_t *slot_session;
attr[0].type = CKA_PRIVATE;
attr[0].pValue = &truevalue;
attr[0].ulValueLen = sizeof (truevalue);
/* Set the master attribute list */
rv = attribute_set_value(attr, object->attributes,
object->num_attributes);
if (rv > 0)
return (CKR_FUNCTION_FAILED);
/* Get a slot session */
rv = meta_get_slot_session(slotnum, &slot_session,
session->session_flags);
if (rv > 0)
return (rv);
/* Create the new CKA_PRIVATE one */
rv = FUNCLIST(slot_session->fw_st_id)->\
C_CopyObject(slot_session->hSession,
object->clones[slotnum]->hObject, attr, 1, &new_clone);
if (rv == CKR_USER_NOT_LOGGED_IN) {
/*
* If the CopyObject fails, we may be using a provider
* that has a keystore that is not the default
* keystore set in metaslot or has object management
* abilities. In which case we should write this
* object to metaslot's keystore and let the failover.
* rest of the function know we've changed providers.
*/
failover = B_TRUE;
keystore_slotnum = get_keystore_slotnum();
if (object->clones[keystore_slotnum] == NULL) {
rv = meta_freeobject_clone_maker(session,
object, keystore_slotnum);
if (rv != CKR_OK) {
goto failure;
}
}
object->master_clone_slotnum = keystore_slotnum;
} else if (rv != CKR_OK) {
meta_release_slot_session(slot_session);
goto failure;
}
/* Remove the old object */
rv = FUNCLIST(slot_session->fw_st_id)-> \
C_DestroyObject(slot_session->hSession,
object->clones[slotnum]->hObject);
if (rv != CKR_OK) {
meta_release_slot_session(slot_session);
goto failure;
}
if (!failover)
object->clones[slotnum]->hObject = new_clone;
else
object->clones[slotnum] = NULL;
meta_release_slot_session(slot_session);
}
if (object->isSensitive) {
slot_session_t *slot_session;
CK_ULONG slotnum = object->master_clone_slotnum;
attr[0].type = CKA_SENSITIVE;
attr[0].pValue = &truevalue;
attr[0].ulValueLen = sizeof (truevalue);
rv = attribute_set_value(attr, object->attributes,
object->num_attributes);
if (rv != CKR_OK)
goto failure;
rv = meta_get_slot_session(slotnum, &slot_session,
session->session_flags);
if (rv == CKR_OK) {
rv = FUNCLIST(slot_session->fw_st_id)-> \
C_SetAttributeValue(slot_session->hSession,
object->clones[slotnum]->hObject, attr, 1);
meta_release_slot_session(slot_session);
}
}
if (object->isFreeToken == FREE_ENABLED || failover) {
keystore_slotnum = get_keystore_slotnum();
if (object->clones[keystore_slotnum] == NULL) {
rv = meta_freeobject_clone_maker(session, object,
keystore_slotnum);
if (rv != CKR_OK)
goto failure;
object->master_clone_slotnum = keystore_slotnum;
}
object->isFreeToken = FREE_ENABLED;
}
object->isFreeObject = FREE_ENABLED;
return (CKR_OK);
failure:
object->isFreeToken = FREE_DISABLED;
object->isFreeObject = FREE_DISABLED;
return (rv);
}
/*
* 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.
*/
/*
* Random Number Generation Functions
* (as defined in PKCS#11 spec section 11.15)
*
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include "metaGlobal.h"
/*
* meta_SeedRandom
*
* Unlike most other metaslot functions, meta_SeedRandom does not distribute
* the call to a specific provider. Rather, we assume that the /dev/urandom
* implementation is a kCF consumer, and is pulling randomness from everywhere
* it can. Thus, by seeding /dev/urandom we let kCF potentially do all the
* work.
*
* NOTES:
* 1) /dev/urandom vs. /dev/random... Unfortunately P11 does not allow app
* to request a "quality", so we'll just assume urandom is good enough.
* Concerned apps can pull hardcore randomness from specific places they
* trust (eg by checking for CKF_HW?)..
*
*/
CK_RV
meta_SeedRandom(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pSeed,
CK_ULONG ulSeedLen)
{
CK_RV rv;
meta_session_t *session;
if (pSeed == NULL || ulSeedLen == 0)
return (CKR_ARGUMENTS_BAD);
/* Just check handle for validity, we don't need it for anything. */
rv = meta_handle2session(hSession, &session);
if (rv != CKR_OK)
return (rv);
REFRELEASE(session);
if (pkcs11_seed_urandom(pSeed, ulSeedLen) < 0) {
if (errno == EACCES)
return (CKR_RANDOM_SEED_NOT_SUPPORTED);
return (CKR_DEVICE_ERROR);
}
return (CKR_OK);
}
/*
* meta_GenerateRandom
*
* Unlike most other metaslot functions, meta_GenerateRandom does not distribute
* the call to a specific provider. Rather, we assume that the /dev/urandom
* implementation is a kCF consumer, and is pulling randomness from everywhere
* it can. Thus, by reading /dev/urandom we let kCF potentially do all the
* work.
*
*/
CK_RV
meta_GenerateRandom(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pRandomData,
CK_ULONG ulRandomLen)
{
CK_RV rv;
meta_session_t *session;
if (pRandomData == NULL || ulRandomLen < 1)
return (CKR_ARGUMENTS_BAD);
/* Just check handle for validity, we don't need it for anything. */
rv = meta_handle2session(hSession, &session);
if (rv != CKR_OK)
return (rv);
REFRELEASE(session);
if (pkcs11_get_urandom(pRandomData, ulRandomLen) < 0) {
return (CKR_DEVICE_ERROR);
}
return (CKR_OK);
}
/*
* 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.
*/
/*
* Session Management Functions
* (as defined in PKCS#11 spec spection 11.6)
*/
#include <string.h>
#include "metaGlobal.h"
extern meta_session_t *meta_sessionlist_head;
extern pthread_rwlock_t meta_sessionlist_lock;
extern CK_ULONG num_meta_sessions;
extern CK_ULONG num_rw_meta_sessions;
/*
* meta_OpenSession
*
* NOTES:
* 1) The pApplication and Notify args are not used, as the metaslot does not
* support application callbacks.
* 2) the slotID argument is not checked or used because this function
* is only called from the framework.
*/
/* ARGSUSED */
CK_RV
meta_OpenSession(CK_SLOT_ID slotID, CK_FLAGS flags, CK_VOID_PTR pApplication,
CK_NOTIFY Notify, CK_SESSION_HANDLE_PTR phSession)
{
meta_session_t *new_session;
CK_RV rv;
if (!metaslot_enabled) {
return (CKR_SLOT_ID_INVALID);
}
if (phSession == NULL) {
return (CKR_ARGUMENTS_BAD);
}
/* Check for any unknown flags. */
if (flags & ~(CKF_SERIAL_SESSION | CKF_RW_SESSION)) {
return (CKR_ARGUMENTS_BAD);
}
if (!(flags & CKF_SERIAL_SESSION)) {
return (CKR_SESSION_PARALLEL_NOT_SUPPORTED);
}
if (meta_slotManager_token_write_protected() &&
(flags & CKF_RW_SESSION)) {
return (CKR_TOKEN_WRITE_PROTECTED);
}
rv = meta_session_alloc(&new_session);
if (rv != CKR_OK)
return (rv);
new_session->session_flags = flags;
rv = meta_session_activate(new_session);
if (rv != CKR_OK) {
meta_session_dealloc(new_session);
return (rv);
}
*phSession = (CK_SESSION_HANDLE) new_session;
num_meta_sessions++;
if (flags & CKF_RW_SESSION) {
num_rw_meta_sessions++;
}
return (CKR_OK);
}
/*
* meta_CloseSession
*
*/
CK_RV
meta_CloseSession(CK_SESSION_HANDLE hSession)
{
CK_RV rv;
meta_session_t *session;
CK_FLAGS flags;
rv = meta_handle2session(hSession, &session);
if (rv != CKR_OK)
return (rv);
/* save info about session flags before they are destroyed */
flags = session->session_flags;
rv = meta_session_deactivate(session, B_FALSE);
if (rv == CKR_OK)
meta_session_dealloc(session);
num_meta_sessions--;
if (flags & CKF_RW_SESSION) {
num_rw_meta_sessions--;
}
return (rv);
}
/*
* meta_CloseAllSessions
*
* This is a simple loop that closes the sessionlist head (resulting in a
* new list head) until the list is empty.
*
*/
CK_RV
meta_CloseAllSessions(CK_SLOT_ID slotID)
{
CK_RV rv;
meta_session_t *session;
if (!metaslot_enabled) {
return (CKR_SLOT_ID_INVALID);
}
if (slotID != METASLOT_SLOTID)
return (CKR_SLOT_ID_INVALID);
(void) pthread_rwlock_wrlock(&meta_sessionlist_lock);
while ((session = meta_sessionlist_head) != NULL) {
rv = meta_handle2session((CK_SESSION_HANDLE)session, &session);
if (rv != CKR_OK) {
/*NOTREACHED*/
(void) pthread_rwlock_unlock(&meta_sessionlist_lock);
return (CKR_FUNCTION_FAILED);
}
(void) meta_session_deactivate(session, B_TRUE);
meta_session_dealloc(session);
}
(void) pthread_rwlock_unlock(&meta_sessionlist_lock);
/* All open sessions should be closed, just reset the variables */
num_meta_sessions = 0;
num_rw_meta_sessions = 0;
return (CKR_OK);
}
/*
* meta_GetSessionInfo
*
*/
CK_RV
meta_GetSessionInfo(CK_SESSION_HANDLE hSession, CK_SESSION_INFO_PTR pInfo)
{
CK_RV rv;
meta_session_t *session;
if (pInfo == NULL)
return (CKR_ARGUMENTS_BAD);
rv = meta_handle2session(hSession, &session);
if (rv != CKR_OK)
return (rv);
pInfo->slotID = METASLOT_SLOTID;
pInfo->flags = session->session_flags;
if (metaslot_logged_in()) {
if (IS_READ_ONLY_SESSION(session->session_flags)) {
pInfo->state = CKS_RO_USER_FUNCTIONS;
} else {
pInfo->state = CKS_RW_USER_FUNCTIONS;
}
} else {
if (IS_READ_ONLY_SESSION(session->session_flags)) {
pInfo->state = CKS_RO_PUBLIC_SESSION;
} else {
pInfo->state = CKS_RW_PUBLIC_SESSION;
}
}
pInfo->ulDeviceError = 0;
REFRELEASE(session);
return (CKR_OK);
}
CK_RV
meta_getopstatelen(meta_session_t *session, CK_ULONG *out_length)
{
CK_RV rv = CKR_OK;
slot_session_t *slot_session;
CK_ULONG length;
*out_length = sizeof (meta_opstate_t);
if (session->op1.type != 0) {
slot_session = session->op1.session;
rv = FUNCLIST(slot_session->fw_st_id)->C_GetOperationState(
slot_session->hSession, NULL, &length);
if (rv == CKR_OK)
*out_length += length;
}
return (rv);
}
/*
* meta_GetOperationState
*
*/
CK_RV
meta_GetOperationState(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pOperationState,
CK_ULONG_PTR pulOperationStateLen)
{
CK_RV rv;
meta_session_t *session;
slot_session_t *slot_session = NULL;
meta_opstate_t opstate;
if (pulOperationStateLen == NULL)
return (CKR_ARGUMENTS_BAD);
rv = meta_handle2session(hSession, &session);
if (rv != CKR_OK)
return (rv);
/*
* If no operation is active, then bail out.
*/
if (session->op1.type == 0) {
rv = CKR_OPERATION_NOT_INITIALIZED;
goto endgetopstate;
}
/*
* If the caller did not give an OpState buffer,
* shortcut and just return the size needed to hold
* a metaslot OpState record later.
* The actual size of the returned state will be the
* sizeof(meta_opstate_t) + SIZE (op1 state),
* so we have to get the size of
* the operation states now.
*/
if (pOperationState == NULL) {
rv = meta_getopstatelen(session, pulOperationStateLen);
REFRELEASE(session);
return (rv);
}
/*
* To be here, the caller must have supplied an
* already initialized meta_opstate_t pointer.
* Use it to get the real state info from the operation(s).
*
* The format of the Metaslot Opstate record:
* {
* struct metaopstate
* [ op1 state data ]
* }
*/
/*
* If the buffer is not even big enough for the metaslot
* opstate data, return error and set the returned
* state length to indicate the minimum needed.
*/
if (*pulOperationStateLen < sizeof (meta_opstate_t)) {
rv = meta_getopstatelen(session, pulOperationStateLen);
/*
* Remap the error so the caller knows that they
* used an invalid buffer size in the first place.
*/
if (rv == CKR_OK)
rv = CKR_BUFFER_TOO_SMALL;
goto endgetopstate;
}
(void) memset(&opstate, 0, sizeof (meta_opstate_t));
opstate.magic_marker = METASLOT_OPSTATE_MAGIC;
if (session->op1.type != 0) {
slot_session = session->op1.session;
opstate.state[0].op_type = session->op1.type;
opstate.state[0].op_slotnum = slot_session->slotnum;
opstate.state[0].op_state_len = *pulOperationStateLen -
sizeof (meta_opstate_t);
opstate.state[0].op_init_app = session->init.app;
opstate.state[0].op_init_done = session->init.done;
rv = FUNCLIST(slot_session->fw_st_id)->C_GetOperationState(
slot_session->hSession,
pOperationState + sizeof (meta_opstate_t),
&(opstate.state[0].op_state_len));
if (rv == CKR_BUFFER_TOO_SMALL) {
/*
* This should not happen, but if it does,
* recalculate the entire size needed
* and return the error.
*/
rv = meta_getopstatelen(session, pulOperationStateLen);
if (rv == CKR_OK)
rv = CKR_BUFFER_TOO_SMALL;
}
if (rv != CKR_OK)
goto endgetopstate;
}
endgetopstate:
if (rv == CKR_OK && pOperationState != NULL) {
(void) memcpy(pOperationState, (void *)&opstate,
sizeof (meta_opstate_t));
*pulOperationStateLen = sizeof (meta_opstate_t) +
opstate.state[0].op_state_len;
}
REFRELEASE(session);
return (rv);
}
static CK_RV
meta_set_opstate(slot_session_t *slot_session,
meta_object_t *meta_enc_key,
meta_object_t *meta_auth_key,
struct opstate_data *state,
CK_BYTE *databuf)
{
CK_RV rv;
static CK_ULONG encrypt_optypes = (CKF_ENCRYPT | CKF_DECRYPT);
static CK_ULONG sign_optypes = (CKF_SIGN | CKF_VERIFY |
CKF_SIGN_RECOVER | CKF_VERIFY_RECOVER);
slot_object_t *enc_key_obj = NULL, *auth_key_obj = NULL;
if (state->op_type & encrypt_optypes) {
rv = meta_object_get_clone(meta_enc_key, slot_session->slotnum,
slot_session, &enc_key_obj);
if (rv != CKR_OK) {
return (rv);
}
}
if (state->op_type & sign_optypes) {
rv = meta_object_get_clone(meta_auth_key, slot_session->slotnum,
slot_session, &auth_key_obj);
if (rv != CKR_OK) {
return (rv);
}
}
/*
* Check to see if the keys are needed to restore the
* state on the first operation.
*/
rv = FUNCLIST(slot_session->fw_st_id)->C_SetOperationState(
slot_session->hSession, databuf, state->op_state_len,
enc_key_obj ? enc_key_obj->hObject : CK_INVALID_HANDLE,
auth_key_obj ? auth_key_obj->hObject : CK_INVALID_HANDLE);
/*
* If the operation did not need a key, try again.
*/
if (rv == CKR_KEY_NOT_NEEDED) {
rv = FUNCLIST(slot_session->fw_st_id)->C_SetOperationState(
slot_session->hSession, databuf, state->op_state_len,
CK_INVALID_HANDLE, CK_INVALID_HANDLE);
/*
* Strange case... If the first try returned
* KEY_NOT_NEEDED, and this one returns KEY_NEEDED,
* we want to remap the return so the caller sees
* the original "CKR_KEY_NOT_NEEDED" return value.
* This ensures that a correct caller will retry
* without the unnecessary key argument and this
* 2nd attempt will not happen again.
*/
if (rv == CKR_KEY_NEEDED) {
rv = CKR_KEY_NOT_NEEDED;
}
}
return (rv);
}
/*
* meta_SetOperationState
*
*/
CK_RV
meta_SetOperationState(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pOperationState,
CK_ULONG ulOperationStateLen, CK_OBJECT_HANDLE hEncryptionKey,
CK_OBJECT_HANDLE hAuthenticationKey)
{
CK_RV rv = CKR_OK;
meta_session_t *session;
slot_session_t *slot_session = NULL;
meta_opstate_t opstate;
meta_object_t *meta_enc_key = NULL, *meta_auth_key = NULL;
/*
* Make sure the opstate info buffer is big enough to be valid.
*/
if (ulOperationStateLen < sizeof (meta_opstate_t) ||
pOperationState == NULL)
return (CKR_ARGUMENTS_BAD);
/* Copy the opstate info into the structure */
(void) memcpy(&opstate, pOperationState, sizeof (meta_opstate_t));
/* verify that a metaslot operation state was supplied */
if (opstate.magic_marker != METASLOT_OPSTATE_MAGIC)
return (CKR_SAVED_STATE_INVALID);
/*
* Now, check the size again to make sure the "real" state
* data is present. Length of state provided must be exact.
*/
if (ulOperationStateLen != (sizeof (meta_opstate_t) +
opstate.state[0].op_state_len))
return (CKR_SAVED_STATE_INVALID);
rv = meta_handle2session(hSession, &session);
if (rv != CKR_OK)
return (rv);
if (hEncryptionKey != CK_INVALID_HANDLE) {
rv = meta_handle2object(hEncryptionKey, &meta_enc_key);
if (rv != CKR_OK)
goto cleanup;
}
if (hAuthenticationKey != CK_INVALID_HANDLE) {
rv = meta_handle2object(hAuthenticationKey, &meta_auth_key);
if (rv != CKR_OK)
goto cleanup;
}
if (opstate.state[0].op_type != 0) {
if (session->op1.type != 0)
meta_operation_cleanup(session, session->op1.type,
B_FALSE);
if (session->op1.session != NULL) {
slot_session = session->op1.session;
} else {
rv = meta_get_slot_session(opstate.state[0].op_slotnum,
&slot_session, session->session_flags);
if (rv != CKR_OK)
goto cleanup;
}
session->op1.type = opstate.state[0].op_type;
session->op1.session = slot_session;
session->init.app = opstate.state[0].op_init_app;
session->init.done = opstate.state[0].op_init_done;
rv = meta_set_opstate(slot_session, meta_enc_key,
meta_auth_key, &(opstate.state[0]),
pOperationState + sizeof (meta_opstate_t));
if (rv != CKR_OK) {
meta_operation_cleanup(session, session->op1.type,
FALSE);
goto cleanup;
}
}
cleanup:
if (meta_enc_key != NULL)
OBJRELEASE(meta_enc_key);
if (meta_auth_key != NULL)
OBJRELEASE(meta_auth_key);
REFRELEASE(session);
return (rv);
}
/*
* meta_Login
*
* This allows the user to login to the object token. The metaslot itself
* does not have any kind of PIN.
*
*/
CK_RV
meta_Login(CK_SESSION_HANDLE hSession, CK_USER_TYPE userType,
CK_UTF8CHAR_PTR pPin, CK_ULONG ulPinLen)
{
CK_RV rv;
meta_session_t *session;
slot_session_t *login_session = NULL;
CK_TOKEN_INFO token_info;
CK_SLOT_ID true_id, fw_st_id;
rv = meta_handle2session(hSession, &session);
if (rv != CKR_OK)
return (rv);
if (metaslot_logged_in()) {
rv = CKR_USER_ALREADY_LOGGED_IN;
goto finish;
}
/* Note: CKU_SO is not supported. */
if (userType != CKU_USER) {
rv = CKR_USER_TYPE_INVALID;
goto finish;
}
rv = meta_get_slot_session(get_keystore_slotnum(), &login_session,
session->session_flags);
if (rv != CKR_OK)
goto finish;
fw_st_id = login_session->fw_st_id;
rv = FUNCLIST(fw_st_id)->C_Login(login_session->hSession, userType,
pPin, ulPinLen);
if (rv != CKR_OK) {
goto finish;
}
/*
* Note:
*
* For some slots (eg: the pkcs11_softtoken.so), C_Login()
* returning OK don't mean that the login is truely
* successful. For pkcs11_softtoken.so, the CKF_USER_PIN_TO_BE_CHANGED
* is set to indicate that the pin needs to be changed, and
* the login is not really successful. We will check
* that flag for this special condition. Checking for
* this flag shouldn't be harmful for other slots that doesn't
* behave like pkcs11_softtoken.so.
*/
true_id = TRUEID(fw_st_id);
rv = FUNCLIST(fw_st_id)->C_GetTokenInfo(true_id, &token_info);
if (rv != CKR_OK) {
goto finish;
}
metaslot_set_logged_in_flag(B_TRUE);
if (token_info.flags & CKF_USER_PIN_TO_BE_CHANGED) {
metaslot_set_logged_in_flag(B_FALSE);
}
finish:
if (login_session)
meta_release_slot_session(login_session);
REFRELEASE(session);
return (rv);
}
/*
* meta_Logout
*
*/
CK_RV
meta_Logout(CK_SESSION_HANDLE hSession)
{
CK_RV rv = CKR_OK;
meta_session_t *session;
slot_session_t *logout_session = NULL;
rv = meta_handle2session(hSession, &session);
if (rv != CKR_OK)
return (rv);
if (!metaslot_logged_in()) {
rv = CKR_USER_NOT_LOGGED_IN;
goto finish;
}
rv = meta_get_slot_session(get_keystore_slotnum(), &logout_session,
session->session_flags);
if (rv != CKR_OK)
goto finish;
rv = FUNCLIST(logout_session->fw_st_id)->C_Logout(
logout_session->hSession);
/* If the C_Logout fails, just ignore the error. */
metaslot_set_logged_in_flag(B_FALSE);
(void) meta_token_object_deactivate(PRIVATE_TOKEN);
finish:
if (logout_session)
meta_release_slot_session(logout_session);
REFRELEASE(session);
return (rv);
}
/*
* 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 <stdlib.h>
#include <string.h>
#include "metaGlobal.h"
/*
* The list and the list lock are global for two external uses:
* 1) C_CloseAllSessions need to close the head (repeatedly,
* until no more sessions exist).
* 2) meta_object_find_by_handle needs to walk all sessions,
* searching each session object list for matching objects.
*/
pthread_rwlock_t meta_sessionlist_lock;
meta_session_t *meta_sessionlist_head;
/*
* The following 2 variables are used for tracking the number of
* sessions and number of rw sessios that are currently open
*
* They are being manipulated in the metaSession.c file, and being
* referenced in the metaSlotToken.c file
*/
CK_ULONG num_meta_sessions;
CK_ULONG num_rw_meta_sessions;
static pthread_rwlock_t meta_sessionclose_lock;
/*
* meta_sessionManager_initialize
*
* Called from meta_Initialize. Initializes all the variables used
* by the session manager.
*/
CK_RV
meta_sessionManager_initialize()
{
if (pthread_rwlock_init(&meta_sessionlist_lock, NULL) != 0) {
return (CKR_FUNCTION_FAILED);
}
if (pthread_rwlock_init(&meta_sessionclose_lock, NULL) != 0) {
(void) pthread_rwlock_destroy(&meta_sessionlist_lock);
return (CKR_FUNCTION_FAILED);
}
meta_sessionlist_head = NULL;
num_meta_sessions = 0;
num_rw_meta_sessions = 0;
return (CKR_OK);
}
/*
* meta_sessionManager_finalize
*
* Close all sessions, and destroy all the locks
*/
void
meta_sessionManager_finalize()
{
/*
* Close any remaining metasessions, can just simply call
* meta_CloseAllSessions. The METASLOT_SLOTID argument is
* not used, but need to be passed in.
*/
(void) meta_CloseAllSessions(METASLOT_SLOTID);
(void) pthread_rwlock_destroy(&meta_sessionclose_lock);
(void) pthread_rwlock_destroy(&meta_sessionlist_lock);
}
/*
* meta_handle2session
*
* Convert a CK_SESSION_HANDLE to the corresponding metasession. If
* successful, a write-lock on the session will be held to indicate
* that it's in use. Call REFRELEASE() when finished.
*
*/
CK_RV
meta_handle2session(CK_SESSION_HANDLE hSession, meta_session_t **session)
{
meta_session_t *tmp_session = (meta_session_t *)(hSession);
/* Check for bad args (eg CK_INVALID_HANDLE, which is 0/NULL). */
if (tmp_session == NULL ||
tmp_session->magic_marker != METASLOT_SESSION_MAGIC) {
return (CKR_SESSION_HANDLE_INVALID);
}
/*
* sessions can only be used by a single thread at a time.
* So, we need to get a write-lock.
*/
(void) pthread_rwlock_wrlock(&tmp_session->session_lock);
/* Make sure this session is not in the process of being deleted */
(void) pthread_mutex_lock(&tmp_session->isClosingSession_lock);
if (tmp_session->isClosingSession) {
(void) pthread_mutex_unlock(
&tmp_session->isClosingSession_lock);
(void) pthread_rwlock_unlock(&tmp_session->session_lock);
return (CKR_SESSION_HANDLE_INVALID);
}
(void) pthread_mutex_unlock(&tmp_session->isClosingSession_lock);
*session = tmp_session;
return (CKR_OK);
}
/*
* meta_session_alloc
*/
CK_RV
meta_session_alloc(meta_session_t **session)
{
meta_session_t *new_session;
/* Allocate memory for the session. */
new_session = calloc(1, sizeof (meta_session_t));
if (new_session == NULL)
return (CKR_HOST_MEMORY);
(new_session->mech_support_info).supporting_slots
= malloc(meta_slotManager_get_slotcount() * sizeof (mechinfo_t *));
if ((new_session->mech_support_info).supporting_slots == NULL) {
free(new_session);
return (CKR_HOST_MEMORY);
}
(new_session->mech_support_info).num_supporting_slots = 0;
new_session->magic_marker = METASLOT_SESSION_MAGIC;
(void) pthread_rwlock_init(&new_session->session_lock, NULL);
(void) pthread_mutex_init(&new_session->isClosingSession_lock, NULL);
(void) pthread_rwlock_init(&new_session->object_list_lock, NULL);
*session = new_session;
return (CKR_OK);
}
/*
* meta_session_activate
*
* Create and add a session to the list of active meta sessions.
*/
CK_RV
meta_session_activate(meta_session_t *session)
{
CK_RV rv = CKR_OK;
/* Add session to the list of sessions. */
(void) pthread_rwlock_wrlock(&meta_sessionlist_lock);
INSERT_INTO_LIST(meta_sessionlist_head, session);
(void) pthread_rwlock_unlock(&meta_sessionlist_lock);
return (rv);
}
/*
* meta_session_deactivate
*
*
*/
CK_RV
meta_session_deactivate(meta_session_t *session,
boolean_t have_sessionlist_lock)
{
boolean_t isLastSession = B_FALSE;
meta_object_t *object;
/* Safely resolve attempts of concurrent-close */
(void) pthread_mutex_lock(&session->isClosingSession_lock);
if (session->isClosingSession) {
/* Lost a delete race. */
(void) pthread_mutex_unlock(&session->isClosingSession_lock);
REFRELEASE(session);
return (CKR_SESSION_HANDLE_INVALID);
}
session->isClosingSession = B_TRUE;
session->magic_marker = METASLOT_SESSION_BADMAGIC;
(void) pthread_mutex_unlock(&session->isClosingSession_lock);
/*
* Remove session from the session list. Once removed, it will not
* be possible for another thread to begin using the session.
*/
(void) pthread_rwlock_wrlock(&meta_sessionclose_lock);
if (!have_sessionlist_lock) {
(void) pthread_rwlock_wrlock(&meta_sessionlist_lock);
}
REMOVE_FROM_LIST(meta_sessionlist_head, session);
if (meta_sessionlist_head == NULL) {
isLastSession = B_TRUE;
}
if (!have_sessionlist_lock) {
(void) pthread_rwlock_unlock(&meta_sessionlist_lock);
}
(void) pthread_rwlock_unlock(&meta_sessionclose_lock);
(void) pthread_rwlock_unlock(&session->session_lock);
/* Cleanup any in-progress operations. */
if (session->op1.type != 0) {
meta_operation_cleanup(session, session->op1.type, FALSE);
}
if (session->op1.session != NULL) {
meta_release_slot_session(session->op1.session);
session->op1.session = NULL;
}
/* Remove all the session metaobjects created in this session. */
/* Basically, emulate C_DestroyObject, including safety h2s */
while ((object = session->object_list_head) != NULL) {
CK_RV rv;
rv = meta_handle2object((CK_OBJECT_HANDLE)object, &object);
if (rv != CKR_OK) {
/* Can only happen if someone else just closed it. */
continue;
}
rv = meta_object_deactivate(object, B_FALSE, B_TRUE);
if (rv != CKR_OK) {
continue;
}
rv = meta_object_dealloc(NULL, object, B_FALSE);
if (rv != CKR_OK) {
continue;
}
}
if ((isLastSession) && (metaslot_logged_in())) {
slot_session_t *slotsessp;
CK_RV rv;
rv = meta_get_slot_session(get_keystore_slotnum(), &slotsessp,
session->session_flags);
if (rv != CKR_OK)
return (rv);
rv = FUNCLIST(slotsessp->fw_st_id)->C_Logout(
slotsessp->hSession);
meta_release_slot_session(slotsessp);
/* if C_Logout fails, just ignore the error */
metaslot_set_logged_in_flag(B_FALSE);
if (rv != CKR_OK)
return (rv);
/* need to deactivate all the PRIVATE token objects */
rv = meta_token_object_deactivate(PRIVATE_TOKEN);
if (rv != CKR_OK) {
return (rv);
}
}
return (CKR_OK);
}
/*
* meta_session_dealloc
*
* Release the resources held by a metasession. If the session has been
* activated, it must be deactivated first.
*/
void
meta_session_dealloc(meta_session_t *session)
{
if ((session->find_objs_info).matched_objs) {
free((session->find_objs_info).matched_objs);
}
free((session->mech_support_info).supporting_slots);
/*
* If there were active operations, cleanup the slot session so that
* it can be reused (otherwise provider might complain that an
* operation is active).
*/
if (session->op1.type != 0)
meta_operation_cleanup(session, session->op1.type, FALSE);
/* Final object cleanup. */
(void) pthread_rwlock_destroy(&session->session_lock);
(void) pthread_mutex_destroy(&session->isClosingSession_lock);
(void) pthread_rwlock_destroy(&session->object_list_lock);
meta_session_delay_free(session);
}
/*
* This function adds the to-be-freed meta session to a linked list.
* When the number of sessions queued in the linked list reaches the
* maximum threshold MAX_SESSION_TO_BE_FREED, it will free the first
* session (FIFO) in the list.
*/
void
meta_session_delay_free(meta_session_t *sp)
{
meta_session_t *tmp;
(void) pthread_mutex_lock(&ses_delay_freed.ses_to_be_free_mutex);
/* Add the newly deleted session at the end of the list */
sp->next = NULL;
if (ses_delay_freed.first == NULL) {
ses_delay_freed.last = sp;
ses_delay_freed.first = sp;
} else {
ses_delay_freed.last->next = sp;
ses_delay_freed.last = sp;
}
if (++ses_delay_freed.count >= MAX_SESSION_TO_BE_FREED) {
/*
* Free the first session in the list only if
* the total count reaches maximum threshold.
*/
ses_delay_freed.count--;
tmp = ses_delay_freed.first->next;
free(ses_delay_freed.first);
ses_delay_freed.first = tmp;
}
(void) pthread_mutex_unlock(&ses_delay_freed.ses_to_be_free_mutex);
}
/*
* 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 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* Signing and MACing Functions
* (as defined in PKCs#11 spec section 11.11)
*/
#include "metaGlobal.h"
/*
* meta_SignInit
*
*/
CK_RV
meta_SignInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism,
CK_OBJECT_HANDLE hKey)
{
CK_RV rv;
meta_session_t *session;
meta_object_t *key;
if (pMechanism == NULL)
return (CKR_ARGUMENTS_BAD);
rv = meta_handle2session(hSession, &session);
if (rv != CKR_OK)
return (rv);
rv = meta_handle2object(hKey, &key);
if (rv != CKR_OK) {
REFRELEASE(session);
return (rv);
}
rv = meta_operation_init(CKF_SIGN, session, pMechanism, key);
OBJRELEASE(key);
REFRELEASE(session);
return (rv);
}
/*
* meta_Sign
*
*/
CK_RV
meta_Sign(CK_SESSION_HANDLE hSession,
CK_BYTE_PTR pData, CK_ULONG ulDataLen,
CK_BYTE_PTR pSignature, CK_ULONG_PTR pulSignatureLen)
{
CK_RV rv;
meta_session_t *session;
rv = meta_handle2session(hSession, &session);
if (rv != CKR_OK)
return (rv);
if (pData == NULL || pulSignatureLen == NULL) {
meta_operation_cleanup(session, CKF_SIGN, FALSE);
REFRELEASE(session);
return (CKR_ARGUMENTS_BAD);
}
rv = meta_do_operation(CKF_SIGN, MODE_SINGLE, session, NULL,
pData, ulDataLen, pSignature, pulSignatureLen);
REFRELEASE(session);
return (rv);
}
/*
* meta_SignUpdate
*
*/
CK_RV
meta_SignUpdate(CK_SESSION_HANDLE hSession,
CK_BYTE_PTR pPart, CK_ULONG ulPartLen)
{
CK_RV rv;
meta_session_t *session;
rv = meta_handle2session(hSession, &session);
if (rv != CKR_OK)
return (rv);
if (pPart == NULL) {
meta_operation_cleanup(session, CKF_SIGN, FALSE);
REFRELEASE(session);
return (CKR_ARGUMENTS_BAD);
}
rv = meta_do_operation(CKF_SIGN, MODE_UPDATE, session, NULL,
pPart, ulPartLen, NULL, NULL);
REFRELEASE(session);
return (rv);
}
/*
* meta_SignFinal
*
*/
CK_RV
meta_SignFinal(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pSignature,
CK_ULONG_PTR pulSignatureLen)
{
CK_RV rv;
meta_session_t *session;
rv = meta_handle2session(hSession, &session);
if (rv != CKR_OK)
return (rv);
if (pulSignatureLen == NULL) {
meta_operation_cleanup(session, CKF_SIGN, FALSE);
REFRELEASE(session);
return (CKR_ARGUMENTS_BAD);
}
rv = meta_do_operation(CKF_SIGN, MODE_FINAL, session, NULL,
NULL, 0, pSignature, pulSignatureLen);
REFRELEASE(session);
return (rv);
}
/*
* meta_SignRecoverInit
*
*/
CK_RV
meta_SignRecoverInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism,
CK_OBJECT_HANDLE hKey)
{
CK_RV rv;
meta_session_t *session;
meta_object_t *key;
if (pMechanism == NULL)
return (CKR_ARGUMENTS_BAD);
rv = meta_handle2session(hSession, &session);
if (rv != CKR_OK)
return (rv);
rv = meta_handle2object(hKey, &key);
if (rv != CKR_OK) {
REFRELEASE(session);
return (rv);
}
rv = meta_operation_init(CKF_SIGN_RECOVER, session, pMechanism, key);
OBJRELEASE(key);
REFRELEASE(session);
return (rv);
}
/*
* meta_SignRecover
*
*/
CK_RV
meta_SignRecover(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pData,
CK_ULONG ulDataLen, CK_BYTE_PTR pSignature, CK_ULONG_PTR pulSignatureLen)
{
CK_RV rv;
meta_session_t *session;
rv = meta_handle2session(hSession, &session);
if (rv != CKR_OK)
return (rv);
if (pData == NULL || pulSignatureLen == NULL) {
meta_operation_cleanup(session, CKF_SIGN_RECOVER, FALSE);
REFRELEASE(session);
return (CKR_ARGUMENTS_BAD);
}
rv = meta_do_operation(CKF_SIGN_RECOVER, MODE_SINGLE, session, NULL,
pData, ulDataLen, pSignature, pulSignatureLen);
REFRELEASE(session);
return (rv);
}
/*
* 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 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* Functions for dealing with provider sessions
*/
#include <string.h>
#include <cryptoutil.h>
#include "metaGlobal.h"
#include "pkcs11Session.h"
#include "pkcs11Global.h"
/*
* This is just a **WILD** guess for the maximum idle sessions to
* keep for each slot. This number should probably be adjusted
* when there's more data from actual application use
*/
#define MAX_IDLE_SESSIONS 100
/*
* The following 5 variables are initialized at the time metaslot
* is initialized. They are not modified after they are initialized
*
* During initialization time, they are protected by the "initmutex"
* defined in metaGeneral.c
*/
slot_data_t *slots;
CK_SLOT_ID metaslot_keystore_slotid;
static CK_ULONG num_slots;
static CK_ULONG objtok_slotnum;
static CK_ULONG softtoken_slotnum;
static boolean_t write_protected;
/* protects the "metaslotLoggedIn" variable */
static pthread_mutex_t metaslotLoggedIn_mutex = PTHREAD_MUTEX_INITIALIZER;
static boolean_t metaslotLoggedIn;
/*
* meta_slotManager_initialize
*
* Called from C_Initialize. Allocates and initializes the storage needed
* by the slot manager.
*/
CK_RV
meta_slotManager_initialize() {
CK_ULONG slot_count = 0;
CK_RV rv;
CK_SLOT_ID i;
/* Initialize the static variables */
write_protected = B_FALSE;
metaslotLoggedIn = B_FALSE;
/*
* Count the number of slots in the framework.
* We start at ((slottable->st_first) + 1) instead of
* slottable->st_first because when we are here, metaslot is
* enabled, and st_first is always metaslot, which doesn't
* need to be counted.
*/
for (i = (slottable->st_first) + 1; i <= slottable->st_last; i++) {
slot_count++;
}
/*
* This shouldn't happen, because there should at least
* be 1 other slot besides metaslot.
*/
if (slot_count < 1) {
rv = CKR_FUNCTION_FAILED;
goto clean_exit;
}
slots = calloc(slot_count, sizeof (slot_data_t));
if (slots == NULL) {
rv = CKR_HOST_MEMORY;
goto clean_exit;
}
/*
* Store the slot IDs. Adjust for the fact that the first slot is
* actually us (metaslot).
*/
for (i = 0; i < slot_count; i++) {
slots[i].fw_st_id = i + 1;
(void) pthread_rwlock_init(
&(slots[i].tokenobject_list_lock), NULL);
}
num_slots = slot_count;
return (CKR_OK);
clean_exit:
if (slots) {
free(slots);
slots = NULL;
}
num_slots = 0;
return (rv);
}
/*
* meta_slotManager_finalize
*
* Called from C_Finalize. Deallocates any storage held by the slot manager.
*/
void
meta_slotManager_finalize() {
CK_ULONG slot;
/* If no slots to free, return */
if (slots == NULL)
return;
/*
* No need to lock pool, we assume all meta sessions are closed.
*
* Close all sessions in the idle and persist list.
* The active list is empty. It doesn't need to be checked.
*/
for (slot = 0; slot < num_slots; slot++) {
slot_session_t *session, *next_session;
/*
* The slotobjects associated with the session should have
* been closed when the metaobjects were closed. Thus, no
* need to do anything here.
*/
session = slots[slot].session_pool.idle_list_head;
while (session) {
next_session = session->next;
(void) FUNCLIST(session->fw_st_id)->C_CloseSession(
session->hSession);
(void) pthread_rwlock_destroy(
&session->object_list_lock);
free(session);
session = next_session;
}
session = slots[slot].session_pool.persist_list_head;
while (session) {
next_session = session->next;
(void) FUNCLIST(session->fw_st_id)->C_CloseSession(
session->hSession);
(void) pthread_rwlock_destroy(
&session->object_list_lock);
free(session);
session = next_session;
}
(void) pthread_rwlock_destroy(
&slots[slot].tokenobject_list_lock);
}
free(slots);
slots = NULL;
num_slots = 0;
}
/*
* meta_slotManager_find_object_token()
*
* Called from meta_Initialize. Searches for the "object token," which is used
* for storing token objects and loging into.
*
* We do the search using the following algorithm.
*
* If either ${METASLOT_OBJECTSTORE_SLOT} or ${METASLOT_OBJECTSTORE_TOKEN}
* environment variable is defined, the value of the defined variable(s)
* will be used for the match. All token and slot values defined system-wide
* will be ignored.
*
* If neither variables above are defined, the system-wide values defined
* in pkcs11.conf are used.
*
* If neither environment variables or system-wide values are defined,
* or if none of the existing slots/tokens match the defined
* values, the first slot after metaslot will be used as the default.
*
*/
void
meta_slotManager_find_object_token() {
CK_ULONG slot;
boolean_t found = B_FALSE;
CK_RV rv;
unsigned int num_match_needed = 0;
CK_SLOT_INFO slotinfo;
CK_TOKEN_INFO tokeninfo;
if (metaslot_config.keystore_token_specified) {
num_match_needed++;
}
if (metaslot_config.keystore_slot_specified) {
num_match_needed++;
}
if (num_match_needed == 0) {
goto skip_search;
}
for (slot = 0; slot < num_slots; slot++) {
unsigned int num_matched = 0;
boolean_t have_tokeninfo = B_FALSE;
CK_SLOT_ID true_id, fw_st_id;
fw_st_id = slots[slot].fw_st_id;
true_id = TRUEID(fw_st_id);
(void) memset(&slotinfo, 0, sizeof (CK_SLOT_INFO));
rv = FUNCLIST(fw_st_id)->C_GetSlotInfo(true_id,
&slotinfo);
if (rv != CKR_OK)
continue;
if (strncmp((char *)SOFT_SLOT_DESCRIPTION,
(char *)slotinfo.slotDescription,
SLOT_DESCRIPTION_SIZE) == 0) {
softtoken_slotnum = slot;
}
if (metaslot_config.keystore_slot_specified) {
unsigned char *slot;
size_t slot_str_len;
rv = FUNCLIST(fw_st_id)->C_GetSlotInfo(true_id,
&slotinfo);
if (rv != CKR_OK)
continue;
/*
* pad slot description from user/system configuration
* with spaces
*/
slot = metaslot_config.keystore_slot;
slot_str_len = strlen((char *)slot);
(void) memset(slot + slot_str_len, ' ',
SLOT_DESCRIPTION_SIZE - slot_str_len);
/*
* The PKCS#11 strings are not null-terminated, so,
* we just compare SLOT_DESCRIPTION_SIZE bytes
*/
if (strncmp((char *)slot,
(char *)slotinfo.slotDescription,
SLOT_DESCRIPTION_SIZE) == 0) {
num_matched++;
}
}
if (metaslot_config.keystore_token_specified) {
unsigned char *token;
size_t token_str_len;
rv = FUNCLIST(fw_st_id)->C_GetTokenInfo(true_id,
&tokeninfo);
if (rv != CKR_OK) {
continue;
}
have_tokeninfo = B_TRUE;
/*
* pad slot description from user/system configuration
* with spaces
*/
token = metaslot_config.keystore_token;
token_str_len = strlen((char *)token);
(void) memset(token + token_str_len, ' ',
TOKEN_LABEL_SIZE - token_str_len);
/*
* The PKCS#11 strings are not null-terminated.
* So, just compare TOKEN_LABEL_SIZE bytes
*/
if (strncmp((char *)token, (char *)tokeninfo.label,
TOKEN_LABEL_SIZE) == 0) {
num_matched++;
}
}
if (num_match_needed == num_matched) {
/* match is found */
if (!have_tokeninfo) {
rv = FUNCLIST(fw_st_id)->C_GetTokenInfo(true_id,
&tokeninfo);
if (rv != CKR_OK) {
continue;
}
}
if (tokeninfo.flags & CKF_WRITE_PROTECTED) {
/*
* Currently this is the only time that
* the write_protected state is set, and
* it is never cleared. The token could
* clear (or set!) this flag later on.
* We might want to adjust the state
* of metaslot, but there's know way to know
* when a token changes this flag.
*/
write_protected = B_TRUE;
}
found = B_TRUE;
break;
}
}
skip_search:
if (found) {
objtok_slotnum = slot;
} else {
/*
* if slot and/or token is not defined for the keystore,
* just use the first available slot as keystore
*/
objtok_slotnum = 0;
}
slots[objtok_slotnum].session_pool.keep_one_alive = B_TRUE;
metaslot_keystore_slotid = slots[objtok_slotnum].fw_st_id;
}
CK_ULONG
get_keystore_slotnum()
{
return (objtok_slotnum);
}
CK_ULONG
get_softtoken_slotnum()
{
return (softtoken_slotnum);
}
CK_SLOT_ID
meta_slotManager_get_framework_table_id(CK_ULONG slotnum)
{
/*
* This is only used internally, and so the slotnum should always
* be valid.
*/
return (slots[slotnum].fw_st_id);
}
CK_ULONG
meta_slotManager_get_slotcount()
{
return (num_slots);
}
boolean_t
meta_slotManager_token_write_protected()
{
return (write_protected);
}
/*
* Find a session in the given list that matches the specified flags.
* If such a session is found, it will be removed from the list, and
* returned to the caller. If such a session is not found, will
* return NULL
*/
static slot_session_t *
get_session(slot_session_t **session_list, CK_FLAGS flags)
{
slot_session_t *tmp_session;
tmp_session = *session_list;
while (tmp_session != NULL) {
if (tmp_session->session_flags == flags) {
break;
} else {
tmp_session = tmp_session->next;
}
}
if (tmp_session == NULL) {
/* no match */
return (NULL);
}
/* Remove from list */
REMOVE_FROM_LIST(*session_list, tmp_session);
return (tmp_session);
}
/*
* meta_get_slot_session
*
* Call to get a session with a specific slot/token.
*
* NOTE - We assume the slot allows an unlimited number of sessions. We
* could look at what's reported in the token info, but that information is
* not always set. It's also unclear when we should (A) wait for one to become
* available, (B) skip the slot for now or (C) return a fatal error. The
* extra complexity is not worth it.
*
*/
CK_RV
meta_get_slot_session(CK_ULONG slotnum, slot_session_t **session,
CK_FLAGS flags) {
session_pool_t *pool;
slot_session_t *new_session, *tmp_session;
CK_RV rv;
CK_SLOT_ID fw_st_id, true_id;
if (slotnum >= num_slots) {
return (CKR_SLOT_ID_INVALID);
}
pool = &slots[slotnum].session_pool;
/*
* Try to reuse an existing session.
*/
(void) pthread_mutex_lock(&pool->list_lock);
if (pool->idle_list_head != NULL) {
tmp_session = get_session(&(pool->idle_list_head), flags);
if (tmp_session != NULL) {
/* Add to active list */
INSERT_INTO_LIST(pool->active_list_head, tmp_session);
*session = tmp_session;
pool->num_idle_sessions--;
(void) pthread_mutex_unlock(&pool->list_lock);
return (CKR_OK);
}
}
if (pool->persist_list_head != NULL) {
tmp_session = get_session(&(pool->persist_list_head), flags);
if (tmp_session != NULL) {
/* Add to active list */
INSERT_INTO_LIST(pool->active_list_head, tmp_session);
*session = tmp_session;
(void) pthread_mutex_unlock(&pool->list_lock);
return (CKR_OK);
}
}
(void) pthread_mutex_unlock(&pool->list_lock);
fw_st_id = slots[slotnum].fw_st_id;
true_id = TRUEID(fw_st_id);
new_session = calloc(1, sizeof (slot_session_t));
if (new_session == NULL) {
return (CKR_HOST_MEMORY);
}
/* initialize slotsession */
new_session->slotnum = slotnum;
new_session->fw_st_id = fw_st_id;
new_session->object_list_head = NULL;
new_session->session_flags = flags;
(void) pthread_rwlock_init(&new_session->object_list_lock, NULL);
rv = FUNCLIST(fw_st_id)->C_OpenSession(true_id, flags, NULL, NULL,
&new_session->hSession);
if (rv == CKR_TOKEN_WRITE_PROTECTED) {
/* Retry with a RO session. */
new_session->session_flags &= ~CKF_SERIAL_SESSION;
rv = FUNCLIST(fw_st_id)->C_OpenSession(true_id,
new_session->session_flags, NULL, NULL,
&new_session->hSession);
}
if (rv != CKR_OK) {
free(new_session);
return (CKR_FUNCTION_FAILED);
}
/* Insert session into active list */
(void) pthread_mutex_lock(&pool->list_lock);
INSERT_INTO_LIST(pool->active_list_head, new_session);
(void) pthread_mutex_unlock(&pool->list_lock);
*session = new_session;
return (CKR_OK);
}
/*
* meta_release_slot_session
*
* Call to release a session obtained via meta_get_slot_session()
*/
void
meta_release_slot_session(slot_session_t *session) {
session_pool_t *pool;
boolean_t must_retain, can_close = B_FALSE;
boolean_t this_is_last_session = B_FALSE;
pool = &slots[session->slotnum].session_pool;
/* Note that the active_list must have >= 1 entry (this session) */
if (pool->persist_list_head == NULL &&
pool->idle_list_head == NULL &&
pool->active_list_head->next == NULL)
this_is_last_session = B_TRUE;
/*
* If the session has session objects, we need to retain it. Also
* retain it if it's the only session holding login state (or handles
* to public token objects)
*/
must_retain = session->object_list_head != NULL ||
(pool->keep_one_alive && this_is_last_session);
if ((!must_retain) && (pool->num_idle_sessions > MAX_IDLE_SESSIONS)) {
can_close = B_TRUE;
}
(void) pthread_mutex_lock(&pool->list_lock);
/* remove from active list */
REMOVE_FROM_LIST(pool->active_list_head, session);
if (must_retain) {
/* insert into persist list */
INSERT_INTO_LIST(pool->persist_list_head, session);
(void) pthread_mutex_unlock(&pool->list_lock);
return;
} else if (!can_close) {
/* insert into idle list */
INSERT_INTO_LIST(pool->idle_list_head, session);
pool->num_idle_sessions++;
(void) pthread_mutex_unlock(&pool->list_lock);
return;
}
(void) pthread_mutex_unlock(&pool->list_lock);
(void) FUNCLIST(session->fw_st_id)->C_CloseSession(session->hSession);
(void) pthread_rwlock_destroy(&session->object_list_lock);
free(session);
}
/*
* Returns whether metaslot has directly logged in
*/
boolean_t
metaslot_logged_in()
{
return (metaslotLoggedIn);
}
void
metaslot_set_logged_in_flag(boolean_t value)
{
(void) pthread_mutex_lock(&metaslotLoggedIn_mutex);
metaslotLoggedIn = value;
(void) pthread_mutex_unlock(&metaslotLoggedIn_mutex);
}
/*
* 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 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* Slot and Token Management functions
* (as defined in PKCS#11 spec section 11.5)
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "metaGlobal.h"
extern CK_ULONG num_meta_sessions;
extern CK_ULONG num_rw_meta_sessions;
/*
* meta_GetSlotList
*
* For the metaslot, this is a trivial function. The metaslot module,
* by defination, provides exactly one slot. The token is always present.
*
* This function is actually not called.
*/
/* ARGSUSED */
CK_RV
meta_GetSlotList(CK_BBOOL tokenPresent, CK_SLOT_ID_PTR pSlotList,
CK_ULONG_PTR pulCount)
{
CK_RV rv;
if (pulCount == NULL)
return (CKR_ARGUMENTS_BAD);
if (pSlotList == NULL) {
*pulCount = 1;
return (CKR_OK);
}
if (*pulCount < 1) {
rv = CKR_BUFFER_TOO_SMALL;
} else {
pSlotList[0] = METASLOT_SLOTID;
rv = CKR_OK;
}
*pulCount = 1;
return (rv);
}
/*
* meta_GetSlotInfo
*
* Returns basic information about the metaslot.
*
* The slotID argument is ignored.
*/
/*ARGSUSED*/
CK_RV
meta_GetSlotInfo(CK_SLOT_ID slotID, CK_SLOT_INFO_PTR pInfo)
{
CK_SLOT_INFO slotinfo;
CK_SLOT_ID true_id;
CK_RV rv;
if (!metaslot_enabled) {
return (CKR_SLOT_ID_INVALID);
}
if (pInfo == NULL) {
return (CKR_ARGUMENTS_BAD);
}
/* Provide information about the slot in the provided buffer */
(void) memcpy(pInfo->slotDescription, METASLOT_SLOT_DESCRIPTION, 64);
(void) memcpy(pInfo->manufacturerID, METASLOT_MANUFACTURER_ID, 32);
pInfo->hardwareVersion.major = METASLOT_HARDWARE_VERSION_MAJOR;
pInfo->hardwareVersion.minor = METASLOT_HARDWARE_VERSION_MINOR;
pInfo->firmwareVersion.major = METASLOT_FIRMWARE_VERSION_MAJOR;
pInfo->firmwareVersion.minor = METASLOT_FIRMWARE_VERSION_MINOR;
/* Find out token is present in the underlying keystore */
true_id = TRUEID(metaslot_keystore_slotid);
rv = FUNCLIST(metaslot_keystore_slotid)->C_GetSlotInfo(true_id,
&slotinfo);
if ((rv == CKR_OK) && (slotinfo.flags & CKF_TOKEN_PRESENT)) {
/*
* store the token present flag if it is successfully
* received from the keystore slot.
* If not, this flag will not be set.
*/
pInfo->flags = CKF_TOKEN_PRESENT;
}
return (CKR_OK);
}
/*
* meta_GetTokenInfo
*
* Returns basic information about the metaslot "token."
*
* The slotID argument is ignored.
*
*/
/*ARGSUSED*/
CK_RV
meta_GetTokenInfo(CK_SLOT_ID slotID, CK_TOKEN_INFO_PTR pInfo)
{
CK_RV rv;
CK_TOKEN_INFO metainfo;
CK_SLOT_ID true_id;
if (!metaslot_enabled) {
return (CKR_SLOT_ID_INVALID);
}
if (pInfo == NULL)
return (CKR_ARGUMENTS_BAD);
true_id = TRUEID(metaslot_keystore_slotid);
rv = FUNCLIST(metaslot_keystore_slotid)->C_GetTokenInfo(true_id,
&metainfo);
/*
* If we could not get information about the object token, use
* default values. This allows metaslot to be used even if there
* are problems with the object token (eg, it's not present).
*/
if (rv != CKR_OK) {
metainfo.ulTotalPublicMemory = CK_UNAVAILABLE_INFORMATION;
metainfo.ulFreePublicMemory = CK_UNAVAILABLE_INFORMATION;
metainfo.ulTotalPrivateMemory = CK_UNAVAILABLE_INFORMATION;
metainfo.ulFreePrivateMemory = CK_UNAVAILABLE_INFORMATION;
metainfo.flags = CKF_WRITE_PROTECTED;
metainfo.ulMaxPinLen = 0;
metainfo.ulMinPinLen = 0;
metainfo.hardwareVersion.major =
METASLOT_HARDWARE_VERSION_MAJOR;
metainfo.hardwareVersion.minor =
METASLOT_HARDWARE_VERSION_MINOR;
metainfo.firmwareVersion.major =
METASLOT_FIRMWARE_VERSION_MAJOR;
metainfo.firmwareVersion.minor =
METASLOT_FIRMWARE_VERSION_MINOR;
}
/*
* Override some values that the object token may have set. They
* can be inappropriate/misleading when used in the context of
* metaslot.
*/
(void) memcpy(metainfo.label, METASLOT_TOKEN_LABEL, 32);
(void) memcpy(metainfo.manufacturerID,
METASLOT_MANUFACTURER_ID, 32);
(void) memcpy(metainfo.model, METASLOT_TOKEN_MODEL, 16);
(void) memset(metainfo.serialNumber, ' ', 16);
metainfo.ulMaxSessionCount = CK_EFFECTIVELY_INFINITE;
metainfo.ulSessionCount = num_meta_sessions;
metainfo.ulMaxRwSessionCount = CK_EFFECTIVELY_INFINITE;
metainfo.ulRwSessionCount = num_rw_meta_sessions;
metainfo.flags |= CKF_RNG;
metainfo.flags &= ~CKF_RESTORE_KEY_NOT_NEEDED;
metainfo.flags |= CKF_TOKEN_INITIALIZED;
metainfo.flags &= ~CKF_SECONDARY_AUTHENTICATION;
/* Clear the time field if the token does not have a clock. */
if (!(metainfo.flags & CKF_CLOCK_ON_TOKEN))
(void) memset(metainfo.utcTime, ' ', 16);
*pInfo = metainfo;
return (CKR_OK);
}
/*
* meta_WaitForSlotEvent
*
* The metaslot never generates events, so this function doesn't do anything
* useful. We do not pass on provider events because we want to hide details
* of the providers.
*
* If CKF_DONT_BLOCK flag is turned on, CKR_NO_EVENT will be return.
* Otherwise, return CKR_FUNCTION_FAILED.
*
*/
/* ARGSUSED */
CK_RV
meta_WaitForSlotEvent(CK_FLAGS flags, CK_SLOT_ID_PTR pSlot,
CK_VOID_PTR pReserved)
{
if (flags & CKF_DONT_BLOCK) {
return (CKR_NO_EVENT);
} else {
return (CKR_FUNCTION_FAILED);
}
}
/*
* meta_GetMechanismList
*
* The slotID argument is not used.
*
*/
/*ARGSUSED*/
CK_RV
meta_GetMechanismList(CK_SLOT_ID slotID, CK_MECHANISM_TYPE_PTR pMechanismList,
CK_ULONG_PTR pulCount)
{
CK_RV rv;
if (!metaslot_enabled) {
return (CKR_SLOT_ID_INVALID);
}
if (pulCount == NULL)
return (CKR_ARGUMENTS_BAD);
rv = meta_mechManager_get_mechs(pMechanismList, pulCount);
if ((rv == CKR_BUFFER_TOO_SMALL) && (pMechanismList == NULL)) {
/*
* if pMechanismList is not provided, just need to
* return count
*/
rv = CKR_OK;
}
return (rv);
}
/*
* meta_GetMechanismInfo
*
* The slotID argument is not used.
*/
/*ARGSUSED*/
CK_RV
meta_GetMechanismInfo(CK_SLOT_ID slotID, CK_MECHANISM_TYPE type,
CK_MECHANISM_INFO_PTR pInfo)
{
CK_RV rv;
mechinfo_t **slots = NULL;
unsigned long i, slotCount = 0;
mech_support_info_t mech_support_info;
if (!metaslot_enabled) {
return (CKR_SLOT_ID_INVALID);
}
if (pInfo == NULL) {
return (CKR_ARGUMENTS_BAD);
}
mech_support_info.supporting_slots =
malloc(meta_slotManager_get_slotcount() * sizeof (mechinfo_t *));
if (mech_support_info.supporting_slots == NULL) {
return (CKR_HOST_MEMORY);
}
mech_support_info.mech = type;
rv = meta_mechManager_get_slots(&mech_support_info, TRUE, NULL);
if (rv != CKR_OK) {
free(mech_support_info.supporting_slots);
return (rv);
}
slotCount = mech_support_info.num_supporting_slots;
slots = mech_support_info.supporting_slots;
/* Merge mechanism info from all slots. */
(void) memcpy(pInfo, &(slots[0]->mechanism_info),
sizeof (CK_MECHANISM_INFO));
/* no need to look at index 0, since that's what we started with */
for (i = 1; i < slotCount; i++) {
CK_ULONG thisValue;
/* MinKeySize should be smallest of all slots. */
thisValue = slots[i]->mechanism_info.ulMinKeySize;
if (thisValue < pInfo->ulMinKeySize) {
pInfo->ulMinKeySize = thisValue;
}
/* MaxKeySize should be largest of all slots. */
thisValue = slots[i]->mechanism_info.ulMaxKeySize;
if (thisValue > pInfo->ulMaxKeySize) {
pInfo->ulMaxKeySize = thisValue;
}
pInfo->flags |= slots[i]->mechanism_info.flags;
}
/* Clear the CKF_HW flag. We might select a software provider later. */
pInfo->flags &= ~CKF_HW;
/* Clear the extenstion flag. Spec says is should never even be set. */
pInfo->flags &= ~CKF_EXTENSION;
free(mech_support_info.supporting_slots);
return (CKR_OK);
}
/*
* meta_InitToken
*
* Not supported. The metaslot "token" is always initialized. The token object
* token must already be initialized. Other vendors don't seem to support
* this anyway.
*/
/* ARGSUSED */
CK_RV
meta_InitToken(CK_SLOT_ID slotID, CK_UTF8CHAR_PTR pPin, CK_ULONG ulPinLen,
CK_UTF8CHAR_PTR pLabel)
{
return (CKR_FUNCTION_NOT_SUPPORTED);
}
/*
* meta_InitPIN
*
* Not supported. Same reason as C_InitToken.
*/
/* ARGSUSED */
CK_RV
meta_InitPIN(CK_SESSION_HANDLE hSession,
CK_UTF8CHAR_PTR pPin, CK_ULONG ulPinLen)
{
return (CKR_FUNCTION_NOT_SUPPORTED);
}
/*
* meta_SetPIN
*
* This is basically just a pass-thru to the object token. No need to
* even check the arguments, since we don't use them.
*/
CK_RV
meta_SetPIN(CK_SESSION_HANDLE hSession, CK_UTF8CHAR_PTR pOldPin,
CK_ULONG ulOldPinLen, CK_UTF8CHAR_PTR pNewPin, CK_ULONG ulNewPinLen)
{
CK_RV rv;
meta_session_t *session;
slot_session_t *slot_session;
rv = meta_handle2session(hSession, &session);
if (rv != CKR_OK)
return (rv);
if (IS_READ_ONLY_SESSION(session->session_flags)) {
REFRELEASE(session);
return (CKR_SESSION_READ_ONLY);
}
rv = meta_get_slot_session(get_keystore_slotnum(), &slot_session,
session->session_flags);
if (rv != CKR_OK) {
REFRELEASE(session);
return (rv);
}
rv = FUNCLIST(slot_session->fw_st_id)->C_SetPIN(slot_session->hSession,
pOldPin, ulOldPinLen, pNewPin, ulNewPinLen);
meta_release_slot_session(slot_session);
REFRELEASE(session);
return (rv);
}
/*
* 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;
}
}
}
/* 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;
}
}
/*
* 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 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* Functions for Verifying Signatures and MACS
* (as defined in PKCS#11 spec section 11.13)
*/
#include "metaGlobal.h"
/*
* meta_VerifyInit
*
*/
CK_RV
meta_VerifyInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism,
CK_OBJECT_HANDLE hKey)
{
CK_RV rv;
meta_session_t *session;
meta_object_t *key;
if (pMechanism == NULL)
return (CKR_ARGUMENTS_BAD);
rv = meta_handle2session(hSession, &session);
if (rv != CKR_OK)
return (rv);
rv = meta_handle2object(hKey, &key);
if (rv != CKR_OK) {
REFRELEASE(session);
return (rv);
}
rv = meta_operation_init(CKF_VERIFY, session, pMechanism, key);
OBJRELEASE(key);
REFRELEASE(session);
return (rv);
}
/*
* meta_Verify
*
*/
CK_RV
meta_Verify(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pData, CK_ULONG ulDataLen,
CK_BYTE_PTR pSignature, CK_ULONG ulSignatureLen)
{
CK_RV rv;
meta_session_t *session;
rv = meta_handle2session(hSession, &session);
if (rv != CKR_OK)
return (rv);
/* Note: unlike other ops, both buffers are inputs, and required. */
if (pData == NULL || pSignature == NULL) {
meta_operation_cleanup(session, CKF_VERIFY, FALSE);
REFRELEASE(session);
return (CKR_ARGUMENTS_BAD);
}
rv = meta_do_operation(CKF_VERIFY, MODE_SINGLE, session, NULL,
pData, ulDataLen, pSignature, &ulSignatureLen);
REFRELEASE(session);
return (rv);
}
/*
* meta_VerifyUpdate
*
*/
CK_RV
meta_VerifyUpdate(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pPart,
CK_ULONG ulPartLen)
{
CK_RV rv;
meta_session_t *session;
rv = meta_handle2session(hSession, &session);
if (rv != CKR_OK)
return (rv);
if (pPart == NULL) {
meta_operation_cleanup(session, CKF_VERIFY, FALSE);
REFRELEASE(session);
return (CKR_ARGUMENTS_BAD);
}
rv = meta_do_operation(CKF_VERIFY, MODE_UPDATE, session, NULL,
pPart, ulPartLen, NULL, NULL);
REFRELEASE(session);
return (rv);
}
/*
* meta_VerifyFinal
*
*/
CK_RV
meta_VerifyFinal(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pSignature,
CK_ULONG ulSignatureLen)
{
CK_RV rv;
meta_session_t *session;
rv = meta_handle2session(hSession, &session);
if (rv != CKR_OK)
return (rv);
/*
* Unlike other ops the buffer is an input. Allow NULL if there's
* no more input.
*/
if (pSignature == NULL && ulSignatureLen != 0) {
meta_operation_cleanup(session, CKF_VERIFY, FALSE);
REFRELEASE(session);
return (CKR_ARGUMENTS_BAD);
}
rv = meta_do_operation(CKF_VERIFY, MODE_FINAL, session, NULL,
pSignature, ulSignatureLen, NULL, NULL);
REFRELEASE(session);
return (rv);
}
/*
* meta_VerifyRecoverInit
*
*/
CK_RV
meta_VerifyRecoverInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism,
CK_OBJECT_HANDLE hKey)
{
CK_RV rv;
meta_session_t *session;
meta_object_t *key;
if (pMechanism == NULL)
return (CKR_ARGUMENTS_BAD);
rv = meta_handle2session(hSession, &session);
if (rv != CKR_OK)
return (rv);
rv = meta_handle2object(hKey, &key);
if (rv != CKR_OK) {
REFRELEASE(session);
return (rv);
}
rv = meta_operation_init(CKF_VERIFY_RECOVER, session, pMechanism, key);
OBJRELEASE(key);
REFRELEASE(session);
return (rv);
}
/*
* meta_VerifyRecover
*
*/
CK_RV
meta_VerifyRecover(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pSignature,
CK_ULONG ulSignatureLen, CK_BYTE_PTR pData, CK_ULONG_PTR pulDataLen)
{
CK_RV rv;
meta_session_t *session;
rv = meta_handle2session(hSession, &session);
if (rv != CKR_OK)
return (rv);
if (pSignature == NULL || pulDataLen == NULL) {
meta_operation_cleanup(session, CKF_VERIFY_RECOVER, FALSE);
REFRELEASE(session);
return (CKR_ARGUMENTS_BAD);
}
rv = meta_do_operation(CKF_VERIFY_RECOVER, MODE_SINGLE, session, NULL,
pSignature, ulSignatureLen, pData, pulDataLen);
REFRELEASE(session);
return (rv);
}
/*
* 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 (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
*/
/*
* Copyright 2010 Nexenta Systems, Inc. All rights reserved.
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <dlfcn.h>
#include <fcntl.h>
#include <link.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <errno.h>
#include <pthread.h>
#include <sys/mman.h>
#include <sys/crypto/elfsign.h>
#include <cryptoutil.h>
#include <security/cryptoki.h>
#include "pkcs11Global.h"
#include "pkcs11Conf.h"
#include "pkcs11Slot.h"
#include "metaGlobal.h"
/*
* Fastpath is used when there is only one slot available from a single provider
* plugged into the framework this is the common case.
* These globals are used to track the function pointers and policy when
* the fast-path is activated.
* This will need to be revisted if per-slot policy is ever
* implemented.
*/
boolean_t purefastpath = B_FALSE;
boolean_t policyfastpath = B_FALSE;
CK_FUNCTION_LIST_PTR fast_funcs = NULL;
CK_SLOT_ID fast_slot = 0;
boolean_t metaslot_enabled = B_FALSE;
boolean_t metaslot_auto_key_migrate = B_FALSE;
metaslot_config_t metaslot_config;
void (*Tmp_GetThreshold)(void *) = NULL;
cipher_mechs_threshold_t meta_mechs_threshold[MAX_NUM_THRESHOLD];
static const char *conf_err = "See cryptoadm(8). Skipping this plug-in.";
/*
* Set up metaslot for the framework using either user configuration
* or system wide configuration options
*
* Also sets up the global "slottable" to have the first slot be metaslot.
*/
static CK_RV
setup_metaslot(uentry_t *metaslot_entry) {
CK_RV rv;
CK_MECHANISM_TYPE_PTR prov_pol_mechs = NULL;
pkcs11_slot_t *cur_slot;
/* process policies for mechanisms */
if ((metaslot_entry) && (metaslot_entry->count > 0)) {
rv = pkcs11_mech_parse(metaslot_entry->policylist,
&prov_pol_mechs, metaslot_entry->count);
if (rv == CKR_HOST_MEMORY) {
cryptoerror(LOG_ERR,
"libpkcs11: Could not parse configuration,"
"out of memory. Cannot continue parsing "
"%s.\n", _PATH_PKCS11_CONF);
return (rv);
} else if (rv == CKR_MECHANISM_INVALID) {
/*
* Configuration file is corrupted for metaslot
*/
cryptoerror(LOG_ERR,
"libpkcs11: Policy invalid or corrupted "
"for metaslot. Use cryptoadm(8) to fix "
"this. Disabling metaslot functionality.\n");
metaslot_enabled = B_FALSE;
return (rv);
}
}
/*
* Check for metaslot policy. If all mechanisms are
* disabled, disable metaslot since there is nothing
* interesting for it to do
*/
if ((metaslot_entry) && (metaslot_entry->flag_enabledlist) &&
(prov_pol_mechs == NULL)) {
metaslot_enabled = B_FALSE;
return (rv);
}
/*
* save system wide value for metaslot's keystore.
* If either slot description or token label is specified by
* the user, the system wide value for both is ignored.
*/
if ((metaslot_entry) &&
(!metaslot_config.keystore_token_specified) &&
(!metaslot_config.keystore_slot_specified)) {
/*
* blank_str is used for comparing with token label,
* and slot description, make sure it is better than
* the larger of both
*/
char blank_str[TOKEN_LABEL_SIZE + SLOT_DESCRIPTION_SIZE];
bzero(blank_str, sizeof (blank_str));
if (memcmp(metaslot_entry->metaslot_ks_token,
blank_str, TOKEN_LABEL_SIZE) != 0) {
metaslot_config.keystore_token_specified = B_TRUE;
(void) strlcpy(
(char *)metaslot_config.keystore_token,
(const char *)metaslot_entry->metaslot_ks_token,
TOKEN_LABEL_SIZE);
}
if (memcmp(metaslot_entry->metaslot_ks_slot,
blank_str, SLOT_DESCRIPTION_SIZE) != 0) {
metaslot_config.keystore_slot_specified = B_TRUE;
(void) strlcpy(
(char *)metaslot_config.keystore_slot,
(const char *)metaslot_entry->metaslot_ks_slot,
SLOT_DESCRIPTION_SIZE);
}
}
/* check system-wide value for auto_key_migrate */
if (metaslot_config.auto_key_migrate_specified) {
/* take user's specified value */
metaslot_auto_key_migrate = metaslot_config.auto_key_migrate;
} else {
if (metaslot_entry) {
/* use system-wide default */
metaslot_auto_key_migrate =
metaslot_entry->flag_metaslot_auto_key_migrate;
} else {
/*
* there's no system wide metaslot entry,
* default auto_key_migrate to true
*/
metaslot_auto_key_migrate = B_TRUE;
}
}
/* Make first slotID be 0, for metaslot. */
slottable->st_first = 0;
/* Set up the slottable entry for metaslot */
slottable->st_slots[0] = NULL;
cur_slot = calloc(1, sizeof (pkcs11_slot_t));
if (cur_slot == NULL) {
rv = CKR_HOST_MEMORY;
return (rv);
}
cur_slot->sl_wfse_state = WFSE_CLEAR;
cur_slot->sl_enabledpol = B_FALSE;
cur_slot->sl_no_wfse = B_FALSE;
(void) pthread_mutex_init(&cur_slot->sl_mutex, NULL);
/*
* The metaslot entry was prealloc'd by
* pkcs11_slottable_increase()
*/
(void) pthread_mutex_lock(&slottable->st_mutex);
slottable->st_slots[0] = cur_slot;
(void) pthread_mutex_unlock(&slottable->st_mutex);
(void) pthread_mutex_lock(&cur_slot->sl_mutex);
cur_slot->sl_id = METASLOT_SLOTID;
cur_slot->sl_func_list = &metaslot_functionList;
if (metaslot_entry) {
cur_slot->sl_enabledpol = metaslot_entry->flag_enabledlist;
cur_slot->sl_pol_count = metaslot_entry->count;
} else {
/* if no metaslot entry, assume all mechs are enabled */
cur_slot->sl_enabledpol = B_FALSE;
cur_slot->sl_pol_count = 0;
}
cur_slot->sl_pol_mechs = prov_pol_mechs;
cur_slot->sl_dldesc = NULL; /* not applicable */
cur_slot->sl_prov_id = 0;
(void) pthread_mutex_unlock(&cur_slot->sl_mutex);
/* Call the meta_Initialize() to initialize metaslot */
rv = meta_Initialize(NULL);
if (rv != CKR_OK) {
cryptoerror(LOG_ERR,
"libpkcs11: Can't initialize metaslot (%s)",
pkcs11_strerror(rv));
goto cleanup;
}
return (CKR_OK);
cleanup:
metaslot_enabled = B_FALSE;
slottable->st_slots[0] = NULL;
if (cur_slot) {
(void) pthread_mutex_destroy(&cur_slot->sl_mutex);
free(cur_slot);
}
return (rv);
}
/*
* For each provider found in pkcs11.conf: expand $ISA if necessary,
* verify the module is signed, load the provider, find all of its
* slots, and store the function list and disabled policy.
*
* This function requires that the uentrylist_t and pkcs11_slottable_t
* already have memory allocated, and that the uentrylist_t is already
* populated with provider and policy information.
*
* pInitArgs can be set to NULL, but is normally the same value
* the framework's C_Initialize() was called with.
*
* Unless metaslot is explicitly disabled, it is setup when all other
* providers are loaded.
*/
CK_RV
pkcs11_slot_mapping(uentrylist_t *pplist, CK_VOID_PTR pInitArgs)
{
CK_RV rv = CKR_OK;
CK_RV prov_rv; /* Provider's return code */
CK_INFO prov_info;
CK_RV (*Tmp_C_GetFunctionList)(CK_FUNCTION_LIST_PTR_PTR);
CK_FUNCTION_LIST_PTR prov_funcs = NULL; /* Provider's function list */
CK_ULONG prov_slot_count; /* Number of slots */
CK_SLOT_ID slot_id; /* slotID assigned for framework */
CK_SLOT_ID_PTR prov_slots = NULL; /* Provider's slot list */
/* Enabled or Disabled policy */
CK_MECHANISM_TYPE_PTR prov_pol_mechs = NULL;
void *dldesc = NULL;
char *isa, *fullpath = NULL, *dl_error;
uentrylist_t *phead;
uint_t prov_count = 0;
pkcs11_slot_t *cur_slot;
CK_ULONG i;
size_t len;
uentry_t *metaslot_entry = NULL;
/* number of slots in the framework, not including metaslot */
uint_t slot_count = 0;
phead = pplist;
/* Loop through all of the provider listed in pkcs11.conf */
while (phead != NULL) {
if (!strcasecmp(phead->puent->name, "metaslot")) {
/*
* Skip standard processing for metaslot
* entry since it is not an actual library
* that can be dlopened.
* It will be initialized later.
*/
if (metaslot_entry != NULL) {
cryptoerror(LOG_ERR,
"libpkcs11: multiple entries for metaslot "
"detected. All but the first entry will "
"be ignored");
} else {
metaslot_entry = phead->puent;
}
goto contparse;
}
if (!strcasecmp(phead->puent->name, FIPS_KEYWORD)) {
/*
* Skip standard processing for fips-140
* entry since it is not an actual library
* that can be dlopened.
*/
goto contparse;
}
/* Check for Instruction Set Architecture indicator */
if ((isa = strstr(phead->puent->name, PKCS11_ISA)) != NULL) {
/* Substitute the architecture dependent path */
len = strlen(phead->puent->name) -
strlen(PKCS11_ISA) +
strlen(PKCS11_ISA_DIR) + 1;
if ((fullpath = (char *)malloc(len)) == NULL) {
cryptoerror(LOG_ERR,
"libpksc11: parsing %s, out of memory. "
"Cannot continue parsing.",
_PATH_PKCS11_CONF);
rv = CKR_HOST_MEMORY;
goto conferror;
}
*isa = '\000';
isa += strlen(PKCS11_ISA);
(void) snprintf(fullpath, len, "%s%s%s",
phead->puent->name, PKCS11_ISA_DIR, isa);
} else if ((fullpath = strdup(phead->puent->name)) == 0) {
cryptoerror(LOG_ERR,
"libpkcs11: parsing %s, out of memory. "
"Cannot continue parsing.",
_PATH_PKCS11_CONF);
rv = CKR_HOST_MEMORY;
goto conferror;
}
/*
* Open the provider. We assume all of our plugins have
* their symbols properly defined, so the use of RTLD_NOW
* to flush out errors immediately is not necessary.
*
* Note that for proper operation, all plugins must be
* built with direct bindings enabled.
*/
dldesc = dlopen(fullpath, RTLD_LAZY);
/*
* If we failed to load it, we will just skip this
* provider and move on to the next one.
*/
if (dldesc == NULL) {
dl_error = dlerror();
cryptoerror(LOG_ERR,
"libpkcs11: Cannot load PKCS#11 library %s. "
"dlerror: %s. %s",
fullpath, dl_error != NULL ? dl_error : "Unknown",
conf_err);
goto contparse;
}
/* Get the pointer to provider's C_GetFunctionList() */
Tmp_C_GetFunctionList =
(CK_RV(*)())dlsym(dldesc, "C_GetFunctionList");
/*
* If we failed to get the pointer to C_GetFunctionList(),
* skip this provider and continue to the next one.
*/
if (Tmp_C_GetFunctionList == NULL) {
cryptoerror(LOG_ERR,
"libpkcs11: Could not dlsym() C_GetFunctionList() "
"for %s. May not be a PKCS#11 library. %s",
fullpath, conf_err);
(void) dlclose(dldesc);
goto contparse;
}
/* Get the provider's function list */
prov_rv = Tmp_C_GetFunctionList(&prov_funcs);
/*
* If we failed to get the provider's function list,
* skip this provider and continue to the next one.
*/
if (prov_rv != CKR_OK) {
cryptoerror(LOG_ERR,
"libpkcs11: Could not get function list for %s. "
"%s Error: %s.",
fullpath, conf_err, pkcs11_strerror(prov_rv));
(void) dlclose(dldesc);
goto contparse;
}
/* Initialize this provider */
prov_rv = prov_funcs->C_Initialize(pInitArgs);
/*
* If we failed to initialize this provider,
* skip this provider and continue to the next one.
*/
if ((prov_rv != CKR_OK) &&
(prov_rv != CKR_CRYPTOKI_ALREADY_INITIALIZED)) {
cryptoerror(LOG_ERR,
"libpkcs11: Could not initialize %s. "
"%s Error: %s.",
fullpath, conf_err, pkcs11_strerror(prov_rv));
(void) dlclose(dldesc);
goto contparse;
}
/*
* Make sure this provider is implementing the same
* major version, and at least the same minor version
* that we are.
*/
prov_rv = prov_funcs->C_GetInfo(&prov_info);
/*
* If we can't verify that we are implementing the
* same major version, or if it is definitely not the same
* version, we need to skip this provider.
*/
if ((prov_rv != CKR_OK) ||
(prov_info.cryptokiVersion.major !=
CRYPTOKI_VERSION_MAJOR)) {
if (prov_rv != CKR_OK) {
cryptoerror(LOG_ERR,
"libpkcs11: Could not verify version of "
"%s. %s Error: %s.", fullpath,
conf_err, pkcs11_strerror(prov_rv));
} else {
cryptoerror(LOG_ERR,
"libpkcs11: Only CRYPTOKI major version "
"%d is supported. %s is major "
"version %d. %s",
CRYPTOKI_VERSION_MAJOR, fullpath,
prov_info.cryptokiVersion.major, conf_err);
}
(void) prov_funcs->C_Finalize(NULL);
(void) dlclose(dldesc);
goto contparse;
}
/*
* Warn the administrator (at debug) that a provider with
* a significantly older or newer version of
* CRYPTOKI is being used. It should not cause
* problems, but logging a warning makes it easier
* to debug later.
*/
if ((prov_info.cryptokiVersion.minor <
CRYPTOKI_VERSION_WARN_MINOR) ||
(prov_info.cryptokiVersion.minor >
CRYPTOKI_VERSION_MINOR)) {
cryptoerror(LOG_DEBUG,
"libpkcs11: %s CRYPTOKI minor version, %d, may "
"not be compatible with minor version %d.",
fullpath, prov_info.cryptokiVersion.minor,
CRYPTOKI_VERSION_MINOR);
}
/*
* Find out how many slots this provider has,
* call with tokenPresent set to FALSE so all
* potential slots are returned.
*/
prov_rv = prov_funcs->C_GetSlotList(FALSE,
NULL, &prov_slot_count);
/*
* If the call failed, or if no slots are returned,
* then skip this provider and continue to next one.
*/
if (prov_rv != CKR_OK) {
cryptoerror(LOG_ERR,
"libpksc11: Could not get slot list from %s. "
"%s Error: %s.",
fullpath, conf_err, pkcs11_strerror(prov_rv));
(void) prov_funcs->C_Finalize(NULL);
(void) dlclose(dldesc);
goto contparse;
}
if (prov_slot_count == 0) {
cryptodebug("libpkcs11: No slots presented from %s. "
"Skipping this plug-in at this time.\n",
fullpath);
(void) prov_funcs->C_Finalize(NULL);
(void) dlclose(dldesc);
goto contparse;
}
/* Allocate memory for the slot list */
prov_slots = calloc(prov_slot_count, sizeof (CK_SLOT_ID));
if (prov_slots == NULL) {
cryptoerror(LOG_ERR,
"libpkcs11: Could not allocate memory for "
"plug-in slots. Cannot continue parsing %s\n",
_PATH_PKCS11_CONF);
rv = CKR_HOST_MEMORY;
goto conferror;
}
/* Get slot list from provider */
prov_rv = prov_funcs->C_GetSlotList(FALSE,
prov_slots, &prov_slot_count);
/* if second call fails, drop this provider */
if (prov_rv != CKR_OK) {
cryptoerror(LOG_ERR,
"libpkcs11: Second call to C_GetSlotList() for %s "
"failed. %s Error: %s.",
fullpath, conf_err, pkcs11_strerror(prov_rv));
(void) prov_funcs->C_Finalize(NULL);
(void) dlclose(dldesc);
goto contparse;
}
/*
* Parse the list of disabled or enabled mechanisms, will
* apply to each of the provider's slots.
*/
if (phead->puent->count > 0) {
rv = pkcs11_mech_parse(phead->puent->policylist,
&prov_pol_mechs, phead->puent->count);
if (rv == CKR_HOST_MEMORY) {
cryptoerror(LOG_ERR,
"libpkcs11: Could not parse configuration,"
"out of memory. Cannot continue parsing "
"%s.", _PATH_PKCS11_CONF);
goto conferror;
} else if (rv == CKR_MECHANISM_INVALID) {
/*
* Configuration file is corrupted for this
* provider.
*/
cryptoerror(LOG_ERR,
"libpkcs11: Policy invalid or corrupted "
"for %s. Use cryptoadm(8) to fix "
"this. Skipping this plug-in.",
fullpath);
(void) prov_funcs->C_Finalize(NULL);
(void) dlclose(dldesc);
goto contparse;
}
}
/* Allocate memory in our slottable for these slots */
rv = pkcs11_slottable_increase(prov_slot_count);
/*
* If any error is returned, it will be memory related,
* so we need to abort the attempt at filling the
* slottable.
*/
if (rv != CKR_OK) {
cryptoerror(LOG_ERR,
"libpkcs11: slottable could not increase. "
"Cannot continue parsing %s.",
_PATH_PKCS11_CONF);
goto conferror;
}
/* Configure information for each new slot */
for (i = 0; i < prov_slot_count; i++) {
/* allocate slot in framework */
rv = pkcs11_slot_allocate(&slot_id);
if (rv != CKR_OK) {
cryptoerror(LOG_ERR,
"libpkcs11: Could not allocate "
"new slot. Cannot continue parsing %s.",
_PATH_PKCS11_CONF);
goto conferror;
}
slot_count++;
cur_slot = slottable->st_slots[slot_id];
(void) pthread_mutex_lock(&cur_slot->sl_mutex);
cur_slot->sl_id = prov_slots[i];
cur_slot->sl_func_list = prov_funcs;
cur_slot->sl_enabledpol =
phead->puent->flag_enabledlist;
cur_slot->sl_pol_mechs = prov_pol_mechs;
cur_slot->sl_pol_count = phead->puent->count;
cur_slot->sl_norandom = phead->puent->flag_norandom;
cur_slot->sl_dldesc = dldesc;
cur_slot->sl_prov_id = prov_count + 1;
(void) pthread_mutex_unlock(&cur_slot->sl_mutex);
}
/*
* Get the pointer to private interface _SUNW_GetThreshold()
* in pkcs11_kernel.
*/
if (Tmp_GetThreshold == NULL) {
Tmp_GetThreshold =
(void(*)())dlsym(dldesc, "_SUNW_GetThreshold");
/* Get the threshold values for the supported mechs */
if (Tmp_GetThreshold != NULL) {
(void) memset(meta_mechs_threshold, 0,
sizeof (meta_mechs_threshold));
Tmp_GetThreshold(meta_mechs_threshold);
}
}
/* Set and reset values to process next provider */
prov_count++;
contparse:
prov_slot_count = 0;
Tmp_C_GetFunctionList = NULL;
prov_funcs = NULL;
dldesc = NULL;
if (fullpath != NULL) {
free(fullpath);
fullpath = NULL;
}
if (prov_slots != NULL) {
free(prov_slots);
prov_slots = NULL;
}
phead = phead->next;
}
if (slot_count == 0) {
/*
* there's no other slot in the framework,
* there is nothing to do
*/
goto config_complete;
}
/* determine if metaslot should be enabled */
/*
* Check to see if any environment variable is defined
* by the user for configuring metaslot. Users'
* setting always take precedence over the system wide
* setting. So, we will first check for any user's
* defined env variables before looking at the system-wide
* configuration.
*/
get_user_metaslot_config();
/* no metaslot entry in /etc/crypto/pkcs11.conf */
if (!metaslot_entry) {
/*
* If user env variable indicates metaslot should be enabled,
* but there's no entry in /etc/crypto/pkcs11.conf for
* metaslot at all, will respect the user's defined value
*/
if ((metaslot_config.enabled_specified) &&
(metaslot_config.enabled)) {
metaslot_enabled = B_TRUE;
}
} else {
if (!metaslot_config.enabled_specified) {
/*
* take system wide value if
* it is not specified by user
*/
metaslot_enabled
= metaslot_entry->flag_metaslot_enabled;
} else {
metaslot_enabled = metaslot_config.enabled;
}
}
/*
*
* As long as the user or system configuration file does not
* disable metaslot, it will be enabled regardless of the
* number of slots plugged into the framework. Therefore,
* metaslot is enabled even when there's only one slot
* plugged into the framework. This is necessary for
* presenting a consistent token label view to applications.
*
* However, for the case where there is only 1 slot plugged into
* the framework, we can use "fastpath".
*
* "fastpath" will pass all of the application's requests
* directly to the underlying provider. Only when policy is in
* effect will we need to keep slotID around.
*
* When metaslot is enabled, and fastpath is enabled,
* all the metaslot processing will be skipped.
* When there is only 1 slot, there's
* really not much metaslot can do in terms of combining functionality
* of different slots, and object migration.
*
*/
/* check to see if fastpath can be used */
if (slottable->st_last == slottable->st_first) {
cur_slot = slottable->st_slots[slottable->st_first];
(void) pthread_mutex_lock(&cur_slot->sl_mutex);
if ((cur_slot->sl_pol_count == 0) &&
(!cur_slot->sl_enabledpol) && (!cur_slot->sl_norandom)) {
/* No policy is in effect, don't need slotid */
fast_funcs = cur_slot->sl_func_list;
purefastpath = B_TRUE;
} else {
fast_funcs = cur_slot->sl_func_list;
fast_slot = slottable->st_first;
policyfastpath = B_TRUE;
}
(void) pthread_mutex_unlock(&cur_slot->sl_mutex);
}
if ((purefastpath || policyfastpath) && (!metaslot_enabled)) {
goto config_complete;
}
/*
* If we get here, there are more than 2 slots in the framework,
* we need to set up metaslot if it is enabled
*/
if (metaslot_enabled) {
rv = setup_metaslot(metaslot_entry);
if (rv != CKR_OK) {
goto conferror;
}
}
config_complete:
return (CKR_OK);
conferror:
/*
* This cleanup code is only exercised when a major,
* unrecoverable error like "out of memory".
*/
if (prov_funcs != NULL) {
(void) prov_funcs->C_Finalize(NULL);
}
if (dldesc != NULL) {
(void) dlclose(dldesc);
}
if (fullpath != NULL) {
free(fullpath);
fullpath = NULL;
}
if (prov_slots != NULL) {
free(prov_slots);
prov_slots = NULL;
}
return (rv);
}
/*
* pkcs11_mech_parse will take hex mechanism ids, as a list of
* strings, and convert them to CK_MECHANISM_TYPE_PTR.
*/
CK_RV
pkcs11_mech_parse(umechlist_t *str_list, CK_MECHANISM_TYPE_PTR *mech_list,
int mech_count)
{
CK_MECHANISM_TYPE_PTR tmp_list;
umechlist_t *shead = str_list;
tmp_list = malloc(mech_count * sizeof (CK_MECHANISM_TYPE));
if (tmp_list == NULL) {
cryptoerror(LOG_ERR, "libpkcs11: parsing %s, out of memory. "
"Cannot continue.",
_PATH_PKCS11_CONF);
return (CKR_HOST_MEMORY);
}
*mech_list = tmp_list;
/*
* The following will loop mech_count times, as there are
* exactly mech_count items in the str_list.
*/
while (shead != NULL) {
CK_MECHANISM_TYPE cur_mech;
errno = 0;
/*
* "name" is a hexadecimal number, preceded by 0x.
*/
cur_mech = strtoul(shead->name, NULL, 16);
if ((cur_mech == 0) &&
((errno == EINVAL) || (errno == ERANGE))) {
free(mech_list);
return (CKR_MECHANISM_INVALID);
}
*tmp_list = (CK_MECHANISM_TYPE)cur_mech;
tmp_list++;
shead = shead->next;
}
return (CKR_OK);
}
/*
* pkcs11_is_dismech is provided a slotid and a mechanism.
* If mech is not disabled, then return B_FALSE.
*/
boolean_t
pkcs11_is_dismech(CK_SLOT_ID slotid, CK_MECHANISM_TYPE mech)
{
ulong_t i;
boolean_t enabled_pol;
CK_MECHANISM_TYPE_PTR pol_mechs;
ulong_t pol_count;
/* Find the associated slot and get the mech policy info */
(void) pthread_mutex_lock(&slottable->st_slots[slotid]->sl_mutex);
enabled_pol = slottable->st_slots[slotid]->sl_enabledpol;
pol_mechs = slottable->st_slots[slotid]->sl_pol_mechs;
pol_count = slottable->st_slots[slotid]->sl_pol_count;
(void) pthread_mutex_unlock(&slottable->st_slots[slotid]->sl_mutex);
/* Check for policy */
if ((!enabled_pol) && (pol_mechs == NULL)) {
/* no policy */
return (B_FALSE);
} else if (pol_mechs == NULL) {
/*
* We have an empty enabled list, which means no
* mechanisms are exempted from this policy: all
* are disabled.
*/
return (B_TRUE);
}
for (i = 0; i < pol_count; i++) {
/*
* If it matches, return status based on this
* being and enabled or a disabled list of mechs.
*/
if (pol_mechs[i] == mech) {
return (enabled_pol ? B_FALSE : B_TRUE);
}
}
/* mech was not found in list */
return (enabled_pol ? B_TRUE : B_FALSE);
}
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (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 2003 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#ifndef _PKCS11_CONF_H
#define _PKCS11_CONF_H
#ifdef __cplusplus
extern "C" {
#endif
#include <cryptoutil.h>
#include "pkcs11Slot.h"
extern CK_RV pkcs11_slot_mapping(uentrylist_t *pplist, CK_VOID_PTR pInitArgs);
extern CK_RV pkcs11_mech_parse(umechlist_t *str_list,
CK_MECHANISM_TYPE_PTR *mech_list, int mech_count);
extern boolean_t pkcs11_is_dismech(CK_SLOT_ID slotid, CK_MECHANISM_TYPE mech);
#ifdef __cplusplus
}
#endif
#endif /* _PKCS11_CONF_H */
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (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 2003 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <security/cryptoki.h>
#include "pkcs11Global.h"
#include "pkcs11Conf.h"
#include "pkcs11Session.h"
#include "pkcs11Slot.h"
/*
* C_EncryptInit will verify that the session handle is valid within
* the framework, that the mechanism is not disabled for the slot
* associated with this session, and then redirect to the underlying
* provider. Policy is checked for C_EncryptInit, and not C_Encrypt
* or C_EncryptUpdate, since C_EncryptInit is required to be called
* before C_Encrypt and C_EncryptUpdate.
*/
CK_RV
C_EncryptInit(CK_SESSION_HANDLE hSession,
CK_MECHANISM_PTR pMechanism,
CK_OBJECT_HANDLE hKey)
{
CK_RV rv;
pkcs11_session_t *sessp;
CK_SLOT_ID slotid;
/* Check for a fastpath */
if (purefastpath || policyfastpath) {
if (policyfastpath &&
pkcs11_is_dismech(fast_slot, pMechanism->mechanism)) {
return (CKR_MECHANISM_INVALID);
}
return (fast_funcs->C_EncryptInit(hSession, pMechanism, hKey));
}
if (!pkcs11_initialized) {
return (CKR_CRYPTOKI_NOT_INITIALIZED);
}
/* Obtain the session pointer */
HANDLE2SESSION(hSession, sessp, rv);
if (rv != CKR_OK) {
return (rv);
}
slotid = sessp->se_slotid;
/* Make sure this is not a disabled mechanism */
if (pkcs11_is_dismech(slotid, pMechanism->mechanism)) {
return (CKR_MECHANISM_INVALID);
}
/* Initialize the digest with the underlying provider */
rv = FUNCLIST(slotid)->C_EncryptInit(sessp->se_handle,
pMechanism, hKey);
/* Present consistent interface to the application */
if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
return (CKR_FUNCTION_FAILED);
}
return (rv);
}
/*
* C_Encrypt is a pure wrapper to the underlying provider.
* The only argument checked is whether or not hSession is valid.
*/
CK_RV
C_Encrypt(CK_SESSION_HANDLE hSession,
CK_BYTE_PTR pData,
CK_ULONG ulDataLen,
CK_BYTE_PTR pEncryptedData,
CK_ULONG_PTR pulEncryptedDataLen)
{
CK_RV rv;
pkcs11_session_t *sessp;
/* Check for a fastpath */
if (purefastpath || policyfastpath) {
return (fast_funcs->C_Encrypt(hSession, pData, ulDataLen,
pEncryptedData, pulEncryptedDataLen));
}
if (!pkcs11_initialized) {
return (CKR_CRYPTOKI_NOT_INITIALIZED);
}
/* Obtain the session pointer */
HANDLE2SESSION(hSession, sessp, rv);
if (rv != CKR_OK) {
return (rv);
}
/* Initialize the digest with the underlying provider */
rv = FUNCLIST(sessp->se_slotid)->C_Encrypt(sessp->se_handle, pData,
ulDataLen, pEncryptedData, pulEncryptedDataLen);
/* Present consistent interface to the application */
if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
return (CKR_FUNCTION_FAILED);
}
return (rv);
}
/*
* C_EncryptUpdate is a pure wrapper to the underlying provider.
* The only argument checked is whether or not hSession is valid.
*/
CK_RV
C_EncryptUpdate(CK_SESSION_HANDLE hSession,
CK_BYTE_PTR pPart,
CK_ULONG ulPartLen,
CK_BYTE_PTR pEncryptedPart,
CK_ULONG_PTR pulEncryptedPartLen)
{
CK_RV rv;
pkcs11_session_t *sessp;
/* Check for a fastpath */
if (purefastpath || policyfastpath) {
return (fast_funcs->C_EncryptUpdate(hSession, pPart, ulPartLen,
pEncryptedPart, pulEncryptedPartLen));
}
if (!pkcs11_initialized) {
return (CKR_CRYPTOKI_NOT_INITIALIZED);
}
/* Obtain the session pointer */
HANDLE2SESSION(hSession, sessp, rv);
if (rv != CKR_OK) {
return (rv);
}
/* Initialize the digest with the underlying provider */
rv = FUNCLIST(sessp->se_slotid)->C_EncryptUpdate(sessp->se_handle,
pPart, ulPartLen, pEncryptedPart, pulEncryptedPartLen);
/* Present consistent interface to the application */
if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
return (CKR_FUNCTION_FAILED);
}
return (rv);
}
/*
* C_EncryptFinal is a pure wrapper to the underlying provider.
* The only argument checked is whether or not hSession is valid.
*/
CK_RV
C_EncryptFinal(CK_SESSION_HANDLE hSession,
CK_BYTE_PTR pLastEncryptedPart,
CK_ULONG_PTR pulLastEncryptedPartLen)
{
CK_RV rv;
pkcs11_session_t *sessp;
/* Check for a fastpath */
if (purefastpath || policyfastpath) {
return (fast_funcs->C_EncryptFinal(hSession,
pLastEncryptedPart, pulLastEncryptedPartLen));
}
if (!pkcs11_initialized) {
return (CKR_CRYPTOKI_NOT_INITIALIZED);
}
/* Obtain the session pointer */
HANDLE2SESSION(hSession, sessp, rv);
if (rv != CKR_OK) {
return (rv);
}
/* Initialize the digest with the underlying provider */
rv = FUNCLIST(sessp->se_slotid)->C_EncryptFinal(sessp->se_handle,
pLastEncryptedPart, pulLastEncryptedPartLen);
/* Present consistent interface to the application */
if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
return (CKR_FUNCTION_FAILED);
}
return (rv);
}
/*
* C_DecryptInit will verify that the session handle is valid within
* the framework, that the mechanism is not disabled for the slot
* associated with this session, and then redirect to the underlying
* provider. Policy is checked for C_DecryptInit, and not C_Decrypt
* or C_DecryptUpdate, since C_DecryptInit is required to be called
* before C_Decrypt and C_DecryptUpdate.
*/
CK_RV
C_DecryptInit(CK_SESSION_HANDLE hSession,
CK_MECHANISM_PTR pMechanism,
CK_OBJECT_HANDLE hKey)
{
CK_RV rv;
pkcs11_session_t *sessp;
CK_SLOT_ID slotid;
/* Check for a fastpath */
if (purefastpath || policyfastpath) {
if (policyfastpath &&
pkcs11_is_dismech(fast_slot, pMechanism->mechanism)) {
return (CKR_MECHANISM_INVALID);
}
return (fast_funcs->C_DecryptInit(hSession, pMechanism, hKey));
}
if (!pkcs11_initialized) {
return (CKR_CRYPTOKI_NOT_INITIALIZED);
}
/* Obtain the session pointer */
HANDLE2SESSION(hSession, sessp, rv);
if (rv != CKR_OK) {
return (rv);
}
slotid = sessp->se_slotid;
/* Make sure this is not a disabled mechanism */
if (pkcs11_is_dismech(slotid, pMechanism->mechanism)) {
return (CKR_MECHANISM_INVALID);
}
/* Initialize the digest with the underlying provider */
rv = FUNCLIST(slotid)->C_DecryptInit(sessp->se_handle,
pMechanism, hKey);
/* Present consistent interface to the application */
if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
return (CKR_FUNCTION_FAILED);
}
return (rv);
}
/*
* C_Decrypt is a pure wrapper to the underlying provider.
* The only argument checked is whether or not hSession is valid.
*/
CK_RV
C_Decrypt(CK_SESSION_HANDLE hSession,
CK_BYTE_PTR pEncryptedData,
CK_ULONG ulEncryptedDataLen,
CK_BYTE_PTR pData,
CK_ULONG_PTR pulDataLen)
{
CK_RV rv;
pkcs11_session_t *sessp;
/* Check for a fastpath */
if (purefastpath || policyfastpath) {
return (fast_funcs->C_Decrypt(hSession, pEncryptedData,
ulEncryptedDataLen, pData, pulDataLen));
}
if (!pkcs11_initialized) {
return (CKR_CRYPTOKI_NOT_INITIALIZED);
}
/* Obtain the session pointer */
HANDLE2SESSION(hSession, sessp, rv);
if (rv != CKR_OK) {
return (rv);
}
/* Initialize the digest with the underlying provider */
rv = FUNCLIST(sessp->se_slotid)->C_Decrypt(sessp->se_handle,
pEncryptedData, ulEncryptedDataLen, pData, pulDataLen);
/* Present consistent interface to the application */
if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
return (CKR_FUNCTION_FAILED);
}
return (rv);
}
/*
* C_DecryptUpdate is a pure wrapper to the underlying provider.
* The only argument checked is whether or not hSession is valid.
*/
CK_RV
C_DecryptUpdate(CK_SESSION_HANDLE hSession,
CK_BYTE_PTR pEncryptedPart,
CK_ULONG ulEncryptedPartLen,
CK_BYTE_PTR pPart,
CK_ULONG_PTR pulPartLen)
{
CK_RV rv;
pkcs11_session_t *sessp;
/* Check for a fastpath */
if (purefastpath || policyfastpath) {
return (fast_funcs->C_DecryptUpdate(hSession, pEncryptedPart,
ulEncryptedPartLen, pPart, pulPartLen));
}
if (!pkcs11_initialized) {
return (CKR_CRYPTOKI_NOT_INITIALIZED);
}
/* Obtain the session pointer */
HANDLE2SESSION(hSession, sessp, rv);
if (rv != CKR_OK) {
return (rv);
}
/* Initialize the digest with the underlying provider */
rv = FUNCLIST(sessp->se_slotid)->C_DecryptUpdate(sessp->se_handle,
pEncryptedPart, ulEncryptedPartLen, pPart, pulPartLen);
/* Present consistent interface to the application */
if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
return (CKR_FUNCTION_FAILED);
}
return (rv);
}
/*
* C_DecryptFinal is a pure wrapper to the underlying provider.
* The only argument checked is whether or not hSession is valid.
*/
CK_RV
C_DecryptFinal(CK_SESSION_HANDLE hSession,
CK_BYTE_PTR pLastPart,
CK_ULONG_PTR pulLastPartLen)
{
CK_RV rv;
pkcs11_session_t *sessp;
/* Check for a fastpath */
if (purefastpath || policyfastpath) {
return (fast_funcs->C_DecryptFinal(hSession, pLastPart,
pulLastPartLen));
}
if (!pkcs11_initialized) {
return (CKR_CRYPTOKI_NOT_INITIALIZED);
}
/* Obtain the session pointer */
HANDLE2SESSION(hSession, sessp, rv);
if (rv != CKR_OK) {
return (rv);
}
/* Initialize the digest with the underlying provider */
rv = FUNCLIST(sessp->se_slotid)->C_DecryptFinal(sessp->se_handle,
pLastPart, pulLastPartLen);
/* Present consistent interface to the application */
if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
return (CKR_FUNCTION_FAILED);
}
return (rv);
}
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (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 2003 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <security/cryptoki.h>
#include "pkcs11Global.h"
#include "pkcs11Conf.h"
#include "pkcs11Session.h"
#include "pkcs11Slot.h"
/*
* C_DigestInit will verify that the session handle is valid within
* the framework, that the mechanism is not disabled for the slot
* associated with this session, and then redirect to the underlying
* provider. Policy is only checked for C_DigestInit, since it is
* required to be called before C_Digest and C_DigestUpdate.
*/
CK_RV
C_DigestInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism)
{
CK_RV rv;
pkcs11_session_t *sessp;
CK_SLOT_ID slotid;
/* Check for a fastpath */
if (purefastpath || policyfastpath) {
if (policyfastpath &&
pkcs11_is_dismech(fast_slot, pMechanism->mechanism)) {
return (CKR_MECHANISM_INVALID);
}
return (fast_funcs->C_DigestInit(hSession, pMechanism));
}
if (!pkcs11_initialized) {
return (CKR_CRYPTOKI_NOT_INITIALIZED);
}
/* Obtain the session pointer */
HANDLE2SESSION(hSession, sessp, rv);
if (rv != CKR_OK) {
return (rv);
}
slotid = sessp->se_slotid;
/* Make sure this is not a disabled mechanism */
if (pkcs11_is_dismech(slotid, pMechanism->mechanism)) {
return (CKR_MECHANISM_INVALID);
}
/* Initialize the digest with the underlying provider */
rv = FUNCLIST(slotid)->C_DigestInit(sessp->se_handle,
pMechanism);
/* Present consistent interface to the application */
if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
return (CKR_FUNCTION_FAILED);
}
return (rv);
}
/*
* C_Digest is a pure wrapper to the underlying provider.
* The only argument checked is whether or not hSession is valid.
*/
CK_RV
C_Digest(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pData, CK_ULONG ulDataLen,
CK_BYTE_PTR pDigest, CK_ULONG_PTR pulDigestLen)
{
CK_RV rv;
pkcs11_session_t *sessp;
/* Check for a fastpath */
if (purefastpath || policyfastpath) {
return (fast_funcs->C_Digest(hSession, pData, ulDataLen,
pDigest, pulDigestLen));
}
if (!pkcs11_initialized) {
return (CKR_CRYPTOKI_NOT_INITIALIZED);
}
/* Obtain the session pointer */
HANDLE2SESSION(hSession, sessp, rv);
if (rv != CKR_OK) {
return (rv);
}
/* Pass data to the provider */
rv = FUNCLIST(sessp->se_slotid)->C_Digest(sessp->se_handle, pData,
ulDataLen, pDigest, pulDigestLen);
/* Present consistent interface to the application */
if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
return (CKR_FUNCTION_FAILED);
}
return (rv);
}
/*
* C_DigestUpdate is a pure wrapper to the underlying provider.
* The only argument checked is whether or not hSession is valid.
*/
CK_RV
C_DigestUpdate(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pPart,
CK_ULONG ulPartLen)
{
CK_RV rv;
pkcs11_session_t *sessp;
/* Check for a fastpath */
if (purefastpath || policyfastpath) {
return (fast_funcs->C_DigestUpdate(hSession, pPart,
ulPartLen));
}
if (!pkcs11_initialized) {
return (CKR_CRYPTOKI_NOT_INITIALIZED);
}
/* Obtain the session pointer */
HANDLE2SESSION(hSession, sessp, rv);
if (rv != CKR_OK) {
return (rv);
}
/* Pass data to the provider */
rv = FUNCLIST(sessp->se_slotid)->C_DigestUpdate(sessp->se_handle,
pPart, ulPartLen);
/* Present consistent interface to the application */
if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
return (CKR_FUNCTION_FAILED);
}
return (rv);
}
/*
* C_DigestKey is a pure wrapper to the underlying provider.
* The only argument checked is whether or not hSession is valid.
*/
CK_RV
C_DigestKey(CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE hKey)
{
CK_RV rv;
pkcs11_session_t *sessp;
/* Check for a fastpath */
if (purefastpath || policyfastpath) {
return (fast_funcs->C_DigestKey(hSession, hKey));
}
if (!pkcs11_initialized) {
return (CKR_CRYPTOKI_NOT_INITIALIZED);
}
/* Obtain the session pointer */
HANDLE2SESSION(hSession, sessp, rv);
if (rv != CKR_OK) {
return (rv);
}
/* Pass data to the provider */
rv = FUNCLIST(sessp->se_slotid)->C_DigestKey(sessp->se_handle, hKey);
/* Present consistent interface to the application */
if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
return (CKR_FUNCTION_FAILED);
}
return (rv);
}
/*
* C_DigestFinal is a pure wrapper to the underlying provider.
* The only argument checked is whether or not hSession is valid.
*/
CK_RV
C_DigestFinal(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pDigest,
CK_ULONG_PTR pulDigestLen)
{
CK_RV rv;
pkcs11_session_t *sessp;
/* Check for a fastpath */
if (purefastpath || policyfastpath) {
return (fast_funcs->C_DigestFinal(hSession, pDigest,
pulDigestLen));
}
if (!pkcs11_initialized) {
return (CKR_CRYPTOKI_NOT_INITIALIZED);
}
/* Obtain the session pointer */
HANDLE2SESSION(hSession, sessp, rv);
if (rv != CKR_OK) {
return (rv);
}
/* Pass data to the provider */
rv = FUNCLIST(sessp->se_slotid)->C_DigestFinal(sessp->se_handle,
pDigest, pulDigestLen);
/* Present consistent interface to the application */
if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
return (CKR_FUNCTION_FAILED);
}
return (rv);
}
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (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 2003 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <security/cryptoki.h>
#include "pkcs11Global.h"
#include "pkcs11Session.h"
#include "pkcs11Slot.h"
/*
* C_DigestEncryptUpdate is a pure wrapper to the underlying provider.
* Policy enforcement was done earlier by the mandatory calls to
* C_DigestInit and C_EncryptInit.
*
* The only argument checked is whether or not hSession is valid.
*/
CK_RV
C_DigestEncryptUpdate(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pPart,
CK_ULONG ulPartLen, CK_BYTE_PTR pEncryptedPart,
CK_ULONG_PTR pulEncryptedPartLen)
{
CK_RV rv;
pkcs11_session_t *sessp;
/* Check for a fastpath */
if (purefastpath || policyfastpath) {
return (fast_funcs->C_DigestEncryptUpdate(hSession, pPart,
ulPartLen, pEncryptedPart, pulEncryptedPartLen));
}
if (!pkcs11_initialized) {
return (CKR_CRYPTOKI_NOT_INITIALIZED);
}
/* Obtain the session pointer */
HANDLE2SESSION(hSession, sessp, rv);
if (rv != CKR_OK) {
return (rv);
}
/* Pass data to the provider */
rv = FUNCLIST(sessp->se_slotid)->
C_DigestEncryptUpdate(sessp->se_handle, pPart, ulPartLen,
pEncryptedPart, pulEncryptedPartLen);
/* Present consistent interface to the application */
if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
return (CKR_FUNCTION_FAILED);
}
return (rv);
}
/*
* C_DecryptDigestUpdate is a pure wrapper to the underlying provider.
* Policy enforcement was done earlier by the mandatory calls to
* C_DigestInit and C_DecryptInit.
*
* The only argument checked is whether or not hSession is valid.
*/
CK_RV
C_DecryptDigestUpdate(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pEncryptedPart,
CK_ULONG ulEncryptedPartLen, CK_BYTE_PTR pPart, CK_ULONG_PTR pulPartLen)
{
CK_RV rv;
pkcs11_session_t *sessp;
/* Check for a fastpath */
if (purefastpath || policyfastpath) {
return (fast_funcs->C_DecryptDigestUpdate(hSession,
pEncryptedPart, ulEncryptedPartLen, pPart,
pulPartLen));
}
if (!pkcs11_initialized) {
return (CKR_CRYPTOKI_NOT_INITIALIZED);
}
/* Obtain the session pointer */
HANDLE2SESSION(hSession, sessp, rv);
if (rv != CKR_OK) {
return (rv);
}
/* Pass data to the provider */
rv = FUNCLIST(sessp->se_slotid)->
C_DecryptDigestUpdate(sessp->se_handle, pEncryptedPart,
ulEncryptedPartLen, pPart, pulPartLen);
/* Present consistent interface to the application */
if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
return (CKR_FUNCTION_FAILED);
}
return (rv);
}
/*
* C_SignEncryptUpdate is a pure wrapper to the underlying provider.
* Policy enforcement was done earlier by the mandatory calls to
* C_SignInit and C_EncryptInit.
*
* The only argument checked is whether or not hSession is valid.
*/
CK_RV
C_SignEncryptUpdate(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pPart,
CK_ULONG ulPartLen, CK_BYTE_PTR pEncryptedPart,
CK_ULONG_PTR pulEncryptedPartLen)
{
CK_RV rv;
pkcs11_session_t *sessp;
/* Check for a fastpath */
if (purefastpath || policyfastpath) {
return (fast_funcs->C_SignEncryptUpdate(hSession, pPart,
ulPartLen, pEncryptedPart, pulEncryptedPartLen));
}
if (!pkcs11_initialized) {
return (CKR_CRYPTOKI_NOT_INITIALIZED);
}
/* Obtain the session pointer */
HANDLE2SESSION(hSession, sessp, rv);
if (rv != CKR_OK) {
return (rv);
}
/* Pass data to the provider */
rv = FUNCLIST(sessp->se_slotid)->
C_SignEncryptUpdate(sessp->se_handle, pPart, ulPartLen,
pEncryptedPart, pulEncryptedPartLen);
/* Present consistent interface to the application */
if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
return (CKR_FUNCTION_FAILED);
}
return (rv);
}
/*
* C_DecryptVerifyUpdate is a pure wrapper to the underlying provider.
* Policy enforcement was done earlier by the mandatory calls to
* C_SignInit and C_EncryptInit.
*
* The only argument checked is whether or not hSession is valid.
*/
CK_RV
C_DecryptVerifyUpdate(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pEncryptedPart,
CK_ULONG ulEncryptedPartLen, CK_BYTE_PTR pPart, CK_ULONG_PTR pulPartLen)
{
CK_RV rv;
pkcs11_session_t *sessp;
/* Check for a fastpath */
if (purefastpath || policyfastpath) {
return (fast_funcs->C_DecryptVerifyUpdate(hSession,
pEncryptedPart, ulEncryptedPartLen, pPart,
pulPartLen));
}
if (!pkcs11_initialized) {
return (CKR_CRYPTOKI_NOT_INITIALIZED);
}
/* Obtain the session pointer */
HANDLE2SESSION(hSession, sessp, rv);
if (rv != CKR_OK) {
return (rv);
}
/* Pass data to the provider */
rv = FUNCLIST(sessp->se_slotid)->
C_DecryptVerifyUpdate(sessp->se_handle, pEncryptedPart,
ulEncryptedPartLen, pPart, pulPartLen);
/* Present consistent interface to the application */
if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
return (CKR_FUNCTION_FAILED);
}
return (rv);
}
/*
* 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 <unistd.h>
#include <string.h>
#include <cryptoutil.h>
#include <pthread.h>
#include <security/cryptoki.h>
#include "pkcs11Global.h"
#include "pkcs11Slot.h"
#include "pkcs11Conf.h"
#include "pkcs11Session.h"
#include "metaGlobal.h"
#pragma init(pkcs11_init)
#pragma fini(pkcs11_fini)
static struct CK_FUNCTION_LIST functionList = {
{ 2, 20 }, /* version */
C_Initialize,
C_Finalize,
C_GetInfo,
C_GetFunctionList,
C_GetSlotList,
C_GetSlotInfo,
C_GetTokenInfo,
C_GetMechanismList,
C_GetMechanismInfo,
C_InitToken,
C_InitPIN,
C_SetPIN,
C_OpenSession,
C_CloseSession,
C_CloseAllSessions,
C_GetSessionInfo,
C_GetOperationState,
C_SetOperationState,
C_Login,
C_Logout,
C_CreateObject,
C_CopyObject,
C_DestroyObject,
C_GetObjectSize,
C_GetAttributeValue,
C_SetAttributeValue,
C_FindObjectsInit,
C_FindObjects,
C_FindObjectsFinal,
C_EncryptInit,
C_Encrypt,
C_EncryptUpdate,
C_EncryptFinal,
C_DecryptInit,
C_Decrypt,
C_DecryptUpdate,
C_DecryptFinal,
C_DigestInit,
C_Digest,
C_DigestUpdate,
C_DigestKey,
C_DigestFinal,
C_SignInit,
C_Sign,
C_SignUpdate,
C_SignFinal,
C_SignRecoverInit,
C_SignRecover,
C_VerifyInit,
C_Verify,
C_VerifyUpdate,
C_VerifyFinal,
C_VerifyRecoverInit,
C_VerifyRecover,
C_DigestEncryptUpdate,
C_DecryptDigestUpdate,
C_SignEncryptUpdate,
C_DecryptVerifyUpdate,
C_GenerateKey,
C_GenerateKeyPair,
C_WrapKey,
C_UnwrapKey,
C_DeriveKey,
C_SeedRandom,
C_GenerateRandom,
C_GetFunctionStatus,
C_CancelFunction,
C_WaitForSlotEvent
};
boolean_t pkcs11_initialized = B_FALSE;
boolean_t pkcs11_cant_create_threads = B_FALSE;
boolean_t fini_called = B_FALSE;
static boolean_t pkcs11_atfork_initialized = B_FALSE;
static pid_t pkcs11_pid = 0;
/* protects pkcs11_[initialized|pid], and fastpath */
static pthread_mutex_t globalmutex = PTHREAD_MUTEX_INITIALIZER;
static CK_RV finalize_common(CK_VOID_PTR pReserved);
static void pkcs11_init();
static void pkcs11_fini();
/*
* Ensure that before a fork, all mutexes are taken.
* We cannot acquire globalmutex, because it can cause deadlock when
* atfork() and fork() are called in parallel. It can happen when
* C_Ininitialize() tries to dlopen() a provider. The dlopen() operation
* is protected by globalmutex and when another thread calls fork()
* pkcs11_fork_prepare cannot acquire the mutex again and thus it must wait.
* When a provider tries to register its atfork handler, atfork() must
* wait on fork(). See the comment in fork() libc function for more details.
*
* Order:
* 1. slottable->st_mutex
* 2. all slottable->st_slots' mutexes
*/
static void
pkcs11_fork_prepare(void)
{
int i;
if (pkcs11_initialized) {
if (slottable != NULL) {
(void) pthread_mutex_lock(&slottable->st_mutex);
/* Take the sl_mutex of all slots */
for (i = slottable->st_first;
i <= slottable->st_last; i++) {
if (slottable->st_slots[i] != NULL) {
(void) pthread_mutex_lock(
&slottable->st_slots[i]->sl_mutex);
}
}
}
}
}
/*
* Ensure that after a fork, in the parent, all mutexes are released in opposite
* order to pkcs11_fork_prepare().
*/
static void
pkcs11_fork_parent(void)
{
int i;
if (pkcs11_initialized) {
if (slottable != NULL) {
/* Release the sl_mutex of all slots */
for (i = slottable->st_first;
i <= slottable->st_last; i++) {
if (slottable->st_slots[i] != NULL) {
(void) pthread_mutex_unlock(
&slottable->st_slots[i]->sl_mutex);
}
}
}
(void) pthread_mutex_unlock(&slottable->st_mutex);
}
}
/*
* Ensure that after a fork, in the child, all mutexes are released in opposite
* order to pkcs11_fork_prepare() and cleanup is done.
* Because we need to handle fork correctly before library is initialized two
* handlers are necessary.
*
* 1) pkcs11_fork_child() - unlock mutexes
* 2) pkcs11_fork_child_fini() - cleanup library after fork, it is registered in
* C_Initialize() after providers initialization.
*/
static void
pkcs11_fork_child(void)
{
int i;
if (pkcs11_initialized) {
if (slottable != NULL) {
/* Release the sl_mutex of all slots */
for (i = slottable->st_first;
i <= slottable->st_last; i++) {
if (slottable->st_slots[i] != NULL) {
(void) pthread_mutex_unlock(
&slottable->st_slots[i]->sl_mutex);
}
}
}
(void) pthread_mutex_unlock(&slottable->st_mutex);
}
(void) pthread_mutex_destroy(&globalmutex);
(void) pthread_mutex_init(&globalmutex, NULL);
}
/* Library cleanup have to be last afterfork child handler. */
static void
pkcs11_fork_child_fini(void)
{
pkcs11_fini();
}
CK_RV
C_Initialize(CK_VOID_PTR pInitArgs)
{
CK_RV rv;
uentrylist_t *pliblist = NULL;
int initialize_pid;
/*
* Grab lock to insure only one thread enters
* this function at a time.
*/
(void) pthread_mutex_lock(&globalmutex);
initialize_pid = getpid();
/* Make sure function hasn't been called twice */
if (pkcs11_initialized) {
if (initialize_pid == pkcs11_pid) {
(void) pthread_mutex_unlock(&globalmutex);
return (CKR_CRYPTOKI_ALREADY_INITIALIZED);
} else {
/*
* A fork has happened and the child is
* reinitializing. Do a finalize_common() to close
* out any state from the parent, and then
* continue on.
*/
(void) finalize_common(NULL);
}
}
/* Check if application has provided mutex-handling functions */
if (pInitArgs != NULL) {
CK_C_INITIALIZE_ARGS_PTR initargs =
(CK_C_INITIALIZE_ARGS_PTR) pInitArgs;
/* pReserved should not be set */
if (initargs->pReserved != NULL) {
rv = CKR_ARGUMENTS_BAD;
goto errorexit;
}
/*
* Make sure function pointers are either all NULL or
* all set.
*/
if (!(((initargs->CreateMutex != NULL) &&
(initargs->LockMutex != NULL) &&
(initargs->UnlockMutex != NULL) &&
(initargs->DestroyMutex != NULL)) ||
((initargs->CreateMutex == NULL) &&
(initargs->LockMutex == NULL) &&
(initargs->UnlockMutex == NULL) &&
(initargs->DestroyMutex == NULL)))) {
rv = CKR_ARGUMENTS_BAD;
goto errorexit;
}
if (!(initargs->flags & CKF_OS_LOCKING_OK)) {
if (initargs->CreateMutex != NULL) {
/*
* Do not accept application supplied
* locking primitives.
*/
rv = CKR_CANT_LOCK;
goto errorexit;
}
}
if (initargs->flags & CKF_LIBRARY_CANT_CREATE_OS_THREADS) {
/*
* Calling application does not want the library
* to create threads. This will effect
* C_WaitForSlotEvent().
*/
pkcs11_cant_create_threads = B_TRUE;
}
}
/* Initialize slot table */
rv = pkcs11_slottable_initialize();
if (rv != CKR_OK)
goto errorexit;
/* Get the list of providers */
if (get_pkcs11conf_info(&pliblist) != SUCCESS) {
rv = CKR_FUNCTION_FAILED;
goto errorexit;
}
/*
* Load each provider, check for accessible slots,
* and populate slottable. If metaslot is enabled,
* it will be initialized as well.
*/
rv = pkcs11_slot_mapping(pliblist, pInitArgs);
if (rv != CKR_OK)
goto errorexit;
pkcs11_initialized = B_TRUE;
pkcs11_pid = initialize_pid;
/* Children inherit parent's atfork handlers */
if (!pkcs11_atfork_initialized) {
(void) pthread_atfork(NULL, NULL, pkcs11_fork_child_fini);
pkcs11_atfork_initialized = B_TRUE;
}
(void) pthread_mutex_unlock(&globalmutex);
/* Cleanup data structures no longer needed */
free_uentrylist(pliblist);
return (CKR_OK);
errorexit:
/* Cleanup any data structures that have already been allocated */
if (slottable)
(void) pkcs11_slottable_delete();
if (pliblist)
(void) free_uentrylist(pliblist);
(void) pthread_mutex_unlock(&globalmutex);
return (rv);
}
/*
* C_Finalize is a wrapper around finalize_common. The
* globalmutex should be locked by C_Finalize().
*
* When an explicit C_Finalize() call is received, all
* plugins currently in the slottable will also be
* finalized. This must occur, even if libpkcs11(3lib)
* was not the first one to initialize the plugins, since it
* is the only way in PKCS#11 to force a refresh of the
* slot listings (ie to get new hardware devices).
*/
CK_RV
C_Finalize(CK_VOID_PTR pReserved)
{
CK_RV rv;
(void) pthread_mutex_lock(&globalmutex);
rv = finalize_common(pReserved);
(void) pthread_mutex_unlock(&globalmutex);
return (rv);
}
/*
* finalize_common() does the work for C_Finalize. globalmutex
* must be held before calling this function.
*/
static CK_RV
finalize_common(CK_VOID_PTR pReserved)
{
CK_RV rv;
if (!pkcs11_initialized) {
return (CKR_CRYPTOKI_NOT_INITIALIZED);
}
if (pReserved != NULL) {
return (CKR_ARGUMENTS_BAD);
}
purefastpath = B_FALSE;
policyfastpath = B_FALSE;
fast_funcs = NULL;
fast_slot = 0;
pkcs11_initialized = B_FALSE;
pkcs11_cant_create_threads = B_FALSE;
pkcs11_pid = 0;
/* Check if C_WaitForSlotEvent() is currently active */
(void) pthread_mutex_lock(&slottable->st_mutex);
if (slottable->st_wfse_active) {
/*
* Wait for this thread to proceed far enough to block or
* end on its own. Otherwise, teardown of slottable may
* occurr before this active function completes.
*/
while (slottable->st_wfse_active) {
/*
* If C_WaitForSlotEvent is blocking, wake it up and
* return error to calling application.
*/
if (slottable->st_blocking) {
slottable->st_list_signaled = B_TRUE;
(void) pthread_cond_signal(
&slottable->st_wait_cond);
(void) pthread_mutex_unlock(
&slottable->st_mutex);
(void) pthread_join(slottable->st_tid, NULL);
}
}
} else {
(void) pthread_mutex_unlock(&slottable->st_mutex);
}
rv = pkcs11_slottable_delete();
return (rv);
}
static void
pkcs11_init()
{
(void) pthread_atfork(pkcs11_fork_prepare,
pkcs11_fork_parent, pkcs11_fork_child);
}
/*
* pkcs11_fini() function required to make sure complete cleanup
* is done of plugins if the framework is ever unloaded without
* a C_Finalize() call. This would be common when applications
* load and unload other libraries that use libpkcs11(3lib), since
* shared libraries should not call C_Finalize().
*
* If pkcs11_fini() is used, we set fini_called to B_TRUE so that
* pkcs11_slottable_delete() will not call C_Finalize() on the plugins.
*
* This is to protect in cases where the application has dlopened
* an object (for example, dlobj) that links to libpkcs11(3lib), but
* the application is unaware that the object is doing PKCS#11 calls
* underneath. This application may later directly dlopen one of the
* plugins (like pkcs11_softtoken.so, or any other 3rd party provided
* plugin) in order to directly perform PKCS#11 operations.
*
* While it is still actively using the PKCS#11 plugin directly,
* the application may finish with dlobj and dlclose it. As the
* reference count for libpkcs11(3lib) has become 0, pkcs11_fini()
* will be run by the linker. Even though libpkcs11(3lib) was the
* first to initialize the plugin in this case, it is not safe for
* libpkcs11(3lib) to finalize the plugin, as the application would
* lose state.
*/
static void
pkcs11_fini()
{
(void) pthread_mutex_lock(&globalmutex);
/* if we're not initilized, do not attempt to finalize */
if (!pkcs11_initialized) {
(void) pthread_mutex_unlock(&globalmutex);
return;
}
fini_called = B_TRUE;
(void) finalize_common(NULL_PTR);
(void) pthread_mutex_unlock(&globalmutex);
}
CK_RV
C_GetInfo(CK_INFO_PTR pInfo)
{
/* Check for a fastpath */
if (purefastpath || policyfastpath) {
return (fast_funcs->C_GetInfo(pInfo));
}
if (!pkcs11_initialized)
return (CKR_CRYPTOKI_NOT_INITIALIZED);
if (pInfo == NULL) {
return (CKR_ARGUMENTS_BAD);
}
/*
* Copy data into the provided buffer, use strncpy() instead
* of strlcpy() so that the strings are NOT NULL terminated,
* as required by the PKCS#11 standard
*/
(void) strncpy((char *)pInfo->manufacturerID, MANUFACTURER_ID,
PKCS11_STRING_LENGTH);
(void) strncpy((char *)pInfo->libraryDescription,
LIBRARY_DESCRIPTION, PKCS11_STRING_LENGTH);
pInfo->cryptokiVersion.major = CRYPTOKI_VERSION_MAJOR;
pInfo->cryptokiVersion.minor = CRYPTOKI_VERSION_MINOR;
pInfo->flags = 0;
pInfo->libraryVersion.major = LIBRARY_VERSION_MAJOR;
pInfo->libraryVersion.minor = LIBRARY_VERSION_MINOR;
return (CKR_OK);
}
/*
* This function is unaffected by the fast-path, since it is likely
* called before C_Initialize is, so we will not yet know the status
* of the fast-path. Additionally, policy will still need to be
* enforced if applicable.
*/
CK_RV
C_GetFunctionList(CK_FUNCTION_LIST_PTR_PTR ppFunctionList)
{
if (ppFunctionList == NULL) {
return (CKR_ARGUMENTS_BAD);
}
*ppFunctionList = &functionList;
return (CKR_OK);
}
/*
* This function is no longer supported in this revision of the PKCS#11
* standard. It is maintained for backwards compatibility only.
*/
/*ARGSUSED*/
CK_RV
C_GetFunctionStatus(CK_SESSION_HANDLE hSession)
{
return (CKR_FUNCTION_NOT_PARALLEL);
}
/*
* This function is no longer supported in this revision of the PKCS#11
* standard. It is maintained for backwards compatibility only.
*/
/*ARGSUSED*/
CK_RV
C_CancelFunction(CK_SESSION_HANDLE hSession)
{
return (CKR_FUNCTION_NOT_PARALLEL);
}
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (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 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#ifndef _PKCS11_GLOBAL_H
#define _PKCS11_GLOBAL_H
#ifdef __cplusplus
extern "C" {
#endif
#include <thread.h>
#include <synch.h>
#include "pkcs11t.h"
extern boolean_t pkcs11_initialized;
extern boolean_t pkcs11_cant_create_threads;
extern boolean_t purefastpath;
extern boolean_t policyfastpath;
extern boolean_t fini_called;
extern CK_FUNCTION_LIST_PTR fast_funcs;
extern CK_SLOT_ID fast_slot;
#define PKCS11_STRING_LENGTH 32
/* CK_INFO: Information about cryptoki */
#define CRYPTOKI_VERSION_WARN_MINOR 10
#define MANUFACTURER_ID "Sun Microsystems, Inc. "
#define LIBRARY_DESCRIPTION "Sun Crypto PKCS#11 Library "
#define LIBRARY_VERSION_MAJOR 1
#define LIBRARY_VERSION_MINOR 1
/* CK_SLOT_INFO */
#define HARDWARE_VERSION_MAJOR 0
#define HARDWARE_VERSION_MINOR 0
#define FIRMWARE_VERSION_MAJOR 0
#define FIRMWARE_VERSION_MINOR 0
#ifdef __cplusplus
}
#endif
#endif /* _PKCS11_GLOBAL_H */
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (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 2003 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <security/cryptoki.h>
#include "pkcs11Global.h"
#include "pkcs11Conf.h"
#include "pkcs11Session.h"
#include "pkcs11Slot.h"
/*
* C_GenerateKey will verify that the session handle is valid within
* the framework, that the mechanism is not disabled for the slot
* associated with this session, and then redirect to the underlying
* provider.
*/
CK_RV
C_GenerateKey(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism,
CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount, CK_OBJECT_HANDLE_PTR phKey)
{
CK_RV rv;
pkcs11_session_t *sessp;
CK_SLOT_ID slotid;
/* Check for a fastpath */
if (purefastpath || policyfastpath) {
if (policyfastpath &&
pkcs11_is_dismech(fast_slot, pMechanism->mechanism)) {
return (CKR_MECHANISM_INVALID);
}
return (fast_funcs->C_GenerateKey(hSession, pMechanism,
pTemplate, ulCount, phKey));
}
if (!pkcs11_initialized) {
return (CKR_CRYPTOKI_NOT_INITIALIZED);
}
/* Obtain the session pointer */
HANDLE2SESSION(hSession, sessp, rv);
if (rv != CKR_OK) {
return (rv);
}
slotid = sessp->se_slotid;
/* Make sure this is not a disabled mechanism */
if (pkcs11_is_dismech(slotid, pMechanism->mechanism)) {
return (CKR_MECHANISM_INVALID);
}
/* Initialize the digest with the underlying provider */
rv = FUNCLIST(slotid)->C_GenerateKey(sessp->se_handle,
pMechanism, pTemplate, ulCount, phKey);
/* Present consistent interface to the application */
if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
return (CKR_FUNCTION_FAILED);
}
return (rv);
}
/*
* C_GenerateKeyPair will verify that the session handle is valid within
* the framework, that the mechanism is not disabled for the slot
* associated with this session, and then redirect to the underlying
* provider.
*/
CK_RV
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)
{
CK_RV rv;
pkcs11_session_t *sessp;
CK_SLOT_ID slotid;
/* Check for a fastpath */
if (purefastpath || policyfastpath) {
if (policyfastpath &&
pkcs11_is_dismech(fast_slot, pMechanism->mechanism)) {
return (CKR_MECHANISM_INVALID);
}
return (fast_funcs->C_GenerateKeyPair(hSession, pMechanism,
pPublicKeyTemplate, ulPublicKeyAttributeCount,
pPrivateKeyTemplate, ulPrivateKeyAttributeCount,
phPublicKey, phPrivateKey));
}
if (!pkcs11_initialized) {
return (CKR_CRYPTOKI_NOT_INITIALIZED);
}
/* Obtain the session pointer */
HANDLE2SESSION(hSession, sessp, rv);
if (rv != CKR_OK) {
return (rv);
}
slotid = sessp->se_slotid;
/* Make sure this is not a disabled mechanism */
if (pkcs11_is_dismech(slotid, pMechanism->mechanism)) {
return (CKR_MECHANISM_INVALID);
}
/* Initialize the digest with the underlying provider */
rv = FUNCLIST(slotid)->C_GenerateKeyPair(sessp->se_handle,
pMechanism, pPublicKeyTemplate, ulPublicKeyAttributeCount,
pPrivateKeyTemplate, ulPrivateKeyAttributeCount,
phPublicKey, phPrivateKey);
/* Present consistent interface to the application */
if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
return (CKR_FUNCTION_FAILED);
}
return (rv);
}
/*
* C_WrapKey will verify that the session handle is valid within
* the framework, that the mechanism is not disabled for the slot
* associated with this session, and then redirect to the underlying
* provider.
*/
CK_RV
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)
{
CK_RV rv;
pkcs11_session_t *sessp;
CK_SLOT_ID slotid;
/* Check for a fastpath */
if (purefastpath || policyfastpath) {
if (policyfastpath &&
pkcs11_is_dismech(fast_slot, pMechanism->mechanism)) {
return (CKR_MECHANISM_INVALID);
}
return (fast_funcs->C_WrapKey(hSession, pMechanism,
hWrappingKey, hKey, pWrappedKey,
pulWrappedKeyLen));
}
if (!pkcs11_initialized) {
return (CKR_CRYPTOKI_NOT_INITIALIZED);
}
/* Obtain the session pointer */
HANDLE2SESSION(hSession, sessp, rv);
if (rv != CKR_OK) {
return (rv);
}
slotid = sessp->se_slotid;
/* Make sure this is not a disabled mechanism */
if (pkcs11_is_dismech(slotid, pMechanism->mechanism)) {
return (CKR_MECHANISM_INVALID);
}
/* Initialize the digest with the underlying provider */
rv = FUNCLIST(slotid)->C_WrapKey(sessp->se_handle,
pMechanism, hWrappingKey, hKey, pWrappedKey, pulWrappedKeyLen);
/* Present consistent interface to the application */
if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
return (CKR_FUNCTION_FAILED);
}
return (rv);
}
/*
* C_UnwrapKey will verify that the session handle is valid within
* the framework, that the mechanism is not disabled for the slot
* associated with this session, and then redirect to the underlying
* provider.
*/
CK_RV
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 ulAttributeCount, CK_OBJECT_HANDLE_PTR phKey)
{
CK_RV rv;
pkcs11_session_t *sessp;
CK_SLOT_ID slotid;
/* Check for a fastpath */
if (purefastpath || policyfastpath) {
if (policyfastpath &&
pkcs11_is_dismech(fast_slot, pMechanism->mechanism)) {
return (CKR_MECHANISM_INVALID);
}
return (fast_funcs->C_UnwrapKey(hSession, pMechanism,
hUnwrappingKey, pWrappedKey, ulWrappedKeyLen,
pTemplate, ulAttributeCount, phKey));
}
if (!pkcs11_initialized) {
return (CKR_CRYPTOKI_NOT_INITIALIZED);
}
/* Obtain the session pointer */
HANDLE2SESSION(hSession, sessp, rv);
if (rv != CKR_OK) {
return (rv);
}
slotid = sessp->se_slotid;
/* Make sure this is not a disabled mechanism */
if (pkcs11_is_dismech(slotid, pMechanism->mechanism)) {
return (CKR_MECHANISM_INVALID);
}
/* Initialize the digest with the underlying provider */
rv = FUNCLIST(slotid)->C_UnwrapKey(sessp->se_handle,
pMechanism, hUnwrappingKey, pWrappedKey, ulWrappedKeyLen,
pTemplate, ulAttributeCount, phKey);
/* Present consistent interface to the application */
if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
return (CKR_FUNCTION_FAILED);
}
return (rv);
}
/*
* C_DeriveKey will verify that the session handle is valid within
* the framework, that the mechanism is not disabled for the slot
* associated with this session, and then redirect to the underlying
* provider.
*/
CK_RV
C_DeriveKey(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism,
CK_OBJECT_HANDLE hBaseKey, CK_ATTRIBUTE_PTR pTemplate,
CK_ULONG ulAttributeCount, CK_OBJECT_HANDLE_PTR phKey)
{
CK_RV rv;
pkcs11_session_t *sessp;
CK_SLOT_ID slotid;
/* Check for a fastpath */
if (purefastpath || policyfastpath) {
if (policyfastpath &&
pkcs11_is_dismech(fast_slot, pMechanism->mechanism)) {
return (CKR_MECHANISM_INVALID);
}
return (fast_funcs->C_DeriveKey(hSession, pMechanism,
hBaseKey, pTemplate, ulAttributeCount, phKey));
}
if (!pkcs11_initialized) {
return (CKR_CRYPTOKI_NOT_INITIALIZED);
}
/* Obtain the session pointer */
HANDLE2SESSION(hSession, sessp, rv);
if (rv != CKR_OK) {
return (rv);
}
slotid = sessp->se_slotid;
/* Make sure this is not a disabled mechanism */
if (pkcs11_is_dismech(slotid, pMechanism->mechanism)) {
return (CKR_MECHANISM_INVALID);
}
/* Initialize the digest with the underlying provider */
rv = FUNCLIST(slotid)->C_DeriveKey(sessp->se_handle,
pMechanism, hBaseKey, pTemplate, ulAttributeCount, phKey);
/* Present consistent interface to the application */
if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
return (CKR_FUNCTION_FAILED);
}
return (rv);
}
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (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 2003 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <security/cryptoki.h>
#include "pkcs11Global.h"
#include "pkcs11Session.h"
#include "pkcs11Slot.h"
/*
* C_CreateObject is a pure wrapper to the underlying provider.
* The only argument checked is whether or not hSession is valid.
*/
CK_RV
C_CreateObject(CK_SESSION_HANDLE hSession,
CK_ATTRIBUTE_PTR pTemplate,
CK_ULONG ulCount,
CK_OBJECT_HANDLE_PTR phObject)
{
CK_RV rv;
pkcs11_session_t *sessp;
/* Check for a fastpath */
if (purefastpath || policyfastpath) {
return (fast_funcs->C_CreateObject(hSession, pTemplate,
ulCount, phObject));
}
if (!pkcs11_initialized) {
return (CKR_CRYPTOKI_NOT_INITIALIZED);
}
/* Obtain the session pointer */
HANDLE2SESSION(hSession, sessp, rv);
if (rv != CKR_OK) {
return (rv);
}
/* Pass data to the provider */
rv = FUNCLIST(sessp->se_slotid)->C_CreateObject(sessp->se_handle,
pTemplate, ulCount, phObject);
/* Present consistent interface to the application */
if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
return (CKR_FUNCTION_FAILED);
}
return (rv);
}
/*
* C_CopyObject is a pure wrapper to the underlying provider.
* The only argument checked is whether or not hSession is valid.
*/
CK_RV
C_CopyObject(CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE hObject,
CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount,
CK_OBJECT_HANDLE_PTR phNewObject)
{
CK_RV rv;
pkcs11_session_t *sessp;
/* Check for a fastpath */
if (purefastpath || policyfastpath) {
return (fast_funcs->C_CopyObject(hSession, hObject,
pTemplate, ulCount, phNewObject));
}
if (!pkcs11_initialized) {
return (CKR_CRYPTOKI_NOT_INITIALIZED);
}
/* Obtain the session pointer */
HANDLE2SESSION(hSession, sessp, rv);
if (rv != CKR_OK) {
return (rv);
}
/* Pass data to the provider */
rv = FUNCLIST(sessp->se_slotid)->C_CopyObject(sessp->se_handle,
hObject, pTemplate, ulCount, phNewObject);
/* Present consistent interface to the application */
if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
return (CKR_FUNCTION_FAILED);
}
return (rv);
}
/*
* C_DestroyObject is a pure wrapper to the underlying provider.
* The only argument checked is whether or not hSession is valid.
*/
CK_RV
C_DestroyObject(CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE hObject)
{
CK_RV rv;
pkcs11_session_t *sessp;
/* Check for a fastpath */
if (purefastpath || policyfastpath) {
return (fast_funcs->C_DestroyObject(hSession, hObject));
}
if (!pkcs11_initialized) {
return (CKR_CRYPTOKI_NOT_INITIALIZED);
}
/* Obtain the session pointer */
HANDLE2SESSION(hSession, sessp, rv);
if (rv != CKR_OK) {
return (rv);
}
/* Pass data to the provider */
rv = FUNCLIST(sessp->se_slotid)->C_DestroyObject(sessp->se_handle,
hObject);
/* Present consistent interface to the application */
if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
return (CKR_FUNCTION_FAILED);
}
return (rv);
}
/*
* C_GetAttributeValue is a pure wrapper to the underlying provider.
* The only argument checked is whether or not hSession is valid.
*/
CK_RV
C_GetAttributeValue(CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE hObject,
CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount)
{
CK_RV rv;
pkcs11_session_t *sessp;
/* Check for a fastpath */
if (purefastpath || policyfastpath) {
return (fast_funcs->C_GetAttributeValue(hSession, hObject,
pTemplate, ulCount));
}
if (!pkcs11_initialized) {
return (CKR_CRYPTOKI_NOT_INITIALIZED);
}
/* Obtain the session pointer */
HANDLE2SESSION(hSession, sessp, rv);
if (rv != CKR_OK) {
return (rv);
}
/* Pass data to the provider */
rv = FUNCLIST(sessp->se_slotid)->C_GetAttributeValue(sessp->se_handle,
hObject, pTemplate, ulCount);
/* Present consistent interface to the application */
if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
return (CKR_FUNCTION_FAILED);
}
return (rv);
}
/*
* C_SetAttributeValue is a pure wrapper to the underlying provider.
* The only argument checked is whether or not hSession is valid.
*/
CK_RV
C_SetAttributeValue(CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE hObject,
CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount)
{
CK_RV rv;
pkcs11_session_t *sessp;
/* Check for a fastpath */
if (purefastpath || policyfastpath) {
return (fast_funcs->C_SetAttributeValue(hSession, hObject,
pTemplate, ulCount));
}
if (!pkcs11_initialized) {
return (CKR_CRYPTOKI_NOT_INITIALIZED);
}
/* Obtain the session pointer */
HANDLE2SESSION(hSession, sessp, rv);
if (rv != CKR_OK) {
return (rv);
}
/* Pass data to the provider */
rv = FUNCLIST(sessp->se_slotid)->C_SetAttributeValue(sessp->se_handle,
hObject, pTemplate, ulCount);
/* Present consistent interface to the application */
if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
return (CKR_FUNCTION_FAILED);
}
return (rv);
}
/*
* C_GetObjectSize is a pure wrapper to the underlying provider.
* The only argument checked is whether or not hSession is valid.
*/
CK_RV
C_GetObjectSize(CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE hObject,
CK_ULONG_PTR pulSize)
{
CK_RV rv;
pkcs11_session_t *sessp;
/* Check for a fastpath */
if (purefastpath || policyfastpath) {
return (fast_funcs->C_GetObjectSize(hSession, hObject,
pulSize));
}
if (!pkcs11_initialized) {
return (CKR_CRYPTOKI_NOT_INITIALIZED);
}
/* Obtain the session pointer */
HANDLE2SESSION(hSession, sessp, rv);
if (rv != CKR_OK) {
return (rv);
}
/* Pass data to the provider */
rv = FUNCLIST(sessp->se_slotid)->C_GetObjectSize(sessp->se_handle,
hObject, pulSize);
/* Present consistent interface to the application */
if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
return (CKR_FUNCTION_FAILED);
}
return (rv);
}
/*
* C_FindObjectsInit is a pure wrapper to the underlying provider.
* The only argument checked is whether or not hSession is valid.
*/
CK_RV
C_FindObjectsInit(CK_SESSION_HANDLE hSession, CK_ATTRIBUTE_PTR pTemplate,
CK_ULONG ulCount)
{
CK_RV rv;
pkcs11_session_t *sessp;
/* Check for a fastpath */
if (purefastpath || policyfastpath) {
return (fast_funcs->C_FindObjectsInit(hSession, pTemplate,
ulCount));
}
if (!pkcs11_initialized) {
return (CKR_CRYPTOKI_NOT_INITIALIZED);
}
/* Obtain the session pointer */
HANDLE2SESSION(hSession, sessp, rv);
if (rv != CKR_OK) {
return (rv);
}
/* Pass data to the provider */
rv = FUNCLIST(sessp->se_slotid)->C_FindObjectsInit(sessp->se_handle,
pTemplate, ulCount);
/* Present consistent interface to the application */
if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
return (CKR_FUNCTION_FAILED);
}
return (rv);
}
/*
* C_FindObjects is a pure wrapper to the underlying provider.
* The only argument checked is whether or not hSession is valid.
*/
CK_RV
C_FindObjects(CK_SESSION_HANDLE hSession,
CK_OBJECT_HANDLE_PTR phObject,
CK_ULONG ulMaxObjectCount,
CK_ULONG_PTR pulObjectCount)
{
CK_RV rv;
pkcs11_session_t *sessp;
/* Check for a fastpath */
if (purefastpath || policyfastpath) {
return (fast_funcs->C_FindObjects(hSession, phObject,
ulMaxObjectCount, pulObjectCount));
}
if (!pkcs11_initialized) {
return (CKR_CRYPTOKI_NOT_INITIALIZED);
}
/* Obtain the session pointer */
HANDLE2SESSION(hSession, sessp, rv);
if (rv != CKR_OK) {
return (rv);
}
/* Pass data to the provider */
rv = FUNCLIST(sessp->se_slotid)->C_FindObjects(sessp->se_handle,
phObject, ulMaxObjectCount, pulObjectCount);
/* Present consistent interface to the application */
if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
return (CKR_FUNCTION_FAILED);
}
return (rv);
}
/*
* C_FindObjectsFinal is a pure wrapper to the underlying provider.
* The only argument checked is whether or not hSession is valid.
*/
CK_RV
C_FindObjectsFinal(CK_SESSION_HANDLE hSession)
{
CK_RV rv;
pkcs11_session_t *sessp;
/* Check for a fastpath */
if (purefastpath || policyfastpath) {
return (fast_funcs->C_FindObjectsFinal(hSession));
}
if (!pkcs11_initialized) {
return (CKR_CRYPTOKI_NOT_INITIALIZED);
}
/* Obtain the session pointer */
HANDLE2SESSION(hSession, sessp, rv);
if (rv != CKR_OK) {
return (rv);
}
/* Pass data to the provider */
rv = FUNCLIST(sessp->se_slotid)->C_FindObjectsFinal(sessp->se_handle);
/* Present consistent interface to the application */
if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
return (CKR_FUNCTION_FAILED);
}
return (rv);
}
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (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 2004 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <security/cryptoki.h>
#include "pkcs11Global.h"
#include "pkcs11Session.h"
#include "pkcs11Slot.h"
/*
* C_SeedRandom will verify that the session handle is valid within
* the framework, that random numbers are not disabled for the slot
* associated with this session, and then redirect to the underlying
* provider.
*/
CK_RV
C_SeedRandom(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pSeed, CK_ULONG ulSeedLen)
{
CK_RV rv;
pkcs11_session_t *sessp;
CK_SLOT_ID slotid;
/* Check for a fastpath */
if (purefastpath || policyfastpath) {
/* Check if random number functions are allowed */
if (policyfastpath &&
slottable->st_slots[fast_slot]->sl_norandom) {
return (CKR_FUNCTION_FAILED);
}
return (fast_funcs->C_SeedRandom(hSession, pSeed, ulSeedLen));
}
if (!pkcs11_initialized) {
return (CKR_CRYPTOKI_NOT_INITIALIZED);
}
/* Obtain the session pointer */
HANDLE2SESSION(hSession, sessp, rv);
if (rv != CKR_OK) {
return (rv);
}
slotid = sessp->se_slotid;
/* Check if random number functions are allowed */
if (slottable->st_slots[slotid]->sl_norandom)
return (CKR_FUNCTION_FAILED);
/* Pass data to the provider */
rv = FUNCLIST(slotid)->C_SeedRandom(sessp->se_handle, pSeed,
ulSeedLen);
/* Present consistent interface to the application */
if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
return (CKR_FUNCTION_FAILED);
}
return (rv);
}
/*
* C_GenerateRandom will verify that the session handle is valid within
* the framework, that random numbers are not disabled for the slot
* associated with this session, and then redirect to the underlying
* provider.
*/
CK_RV
C_GenerateRandom(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pRandomData,
CK_ULONG ulRandomLen)
{
CK_RV rv;
pkcs11_session_t *sessp;
CK_SLOT_ID slotid;
/* Check for a fastpath */
if (purefastpath || policyfastpath) {
/* Check if random number functions are allowed */
if (policyfastpath &&
slottable->st_slots[fast_slot]->sl_norandom) {
return (CKR_FUNCTION_FAILED);
}
return (fast_funcs->C_GenerateRandom(hSession, pRandomData,
ulRandomLen));
}
if (!pkcs11_initialized) {
return (CKR_CRYPTOKI_NOT_INITIALIZED);
}
/* Obtain the session pointer */
HANDLE2SESSION(hSession, sessp, rv);
if (rv != CKR_OK) {
return (rv);
}
slotid = sessp->se_slotid;
/* Check if random number functions are allowed */
if (slottable->st_slots[slotid]->sl_norandom)
return (CKR_FUNCTION_FAILED);
/* Pass data to the provider */
rv = FUNCLIST(sessp->se_slotid)->C_GenerateRandom(sessp->se_handle,
pRandomData, ulRandomLen);
/* Present consistent interface to the application */
if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
return (CKR_FUNCTION_FAILED);
}
return (rv);
}
/*
* 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 (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
*/
/*
* Solaris specific functions to reduce the initialization
* overhead of using PKCS #11
*/
#include <stdlib.h>
#include <sys/types.h>
#include <security/cryptoki.h>
#include <assert.h>
#include <cryptoutil.h>
#include <pkcs11Global.h>
static CK_OBJECT_CLASS objclass = CKO_SECRET_KEY;
static CK_BBOOL falsevalue = FALSE;
static CK_BBOOL truevalue = TRUE;
#define NUM_SECRETKEY_ATTRS 12
typedef struct _ATTRTYPE_MECHINFO_MAPPING {
CK_ATTRIBUTE_TYPE attr;
CK_FLAGS flag;
} ATTRTYPE_MECHINFO_MAPPING;
/* possible attribute types for creating key */
ATTRTYPE_MECHINFO_MAPPING mapping[] = {
{CKA_ENCRYPT, CKF_ENCRYPT},
{CKA_DECRYPT, CKF_DECRYPT},
{CKA_SIGN, CKF_SIGN},
{CKA_VERIFY, CKF_VERIFY},
{CKA_WRAP, CKF_WRAP},
{CKA_UNWRAP, CKF_UNWRAP}
};
/*
* List of mechanisms that only supports asymmetric key operations
* in PKCS #11 V2.11
*/
CK_MECHANISM_TYPE asymmetric_mechs[] = {
CKM_RSA_PKCS_KEY_PAIR_GEN, CKM_RSA_PKCS, CKM_RSA_9796, CKM_RSA_X_509,
CKM_RSA_PKCS_OAEP, CKM_RSA_X9_31_KEY_PAIR_GEN, CKM_RSA_X9_31,
CKM_RSA_PKCS_PSS, CKM_DSA_KEY_PAIR_GEN, CKM_DSA, CKM_DSA_SHA1,
CKM_DSA_PARAMETER_GEN, CKM_ECDSA_KEY_PAIR_GEN, CKM_EC_KEY_PAIR_GEN,
CKM_ECDSA, CKM_ECDSA_SHA1, CKM_ECDH1_DERIVE,
CKM_ECDH1_COFACTOR_DERIVE, CKM_ECMQV_DERIVE
};
typedef struct _KEY_TYPE_SIZE_MAPPING {
CK_KEY_TYPE type;
CK_ULONG len;
} KEY_TYPE_SIZE_MAPPING;
/*
* List of secret key types that have fixed sizes and their sizes.
* These key types do not allow CKA_VALUE_LEN for key generation.
* The sizes are in bytes.
*
* Discrete-sized keys, such as AES and Twofish, and variable sized
* keys, such as Blowfish, are not in this list.
*/
KEY_TYPE_SIZE_MAPPING fixed_size_secrets[] = {
{CKK_DES, 8}, {CKK_DES2, 16}, {CKK_DES3, 24}, {CKK_IDEA, 16},
{CKK_CDMF, 8}, {CKK_SKIPJACK, 12}, {CKK_BATON, 40}, {CKK_JUNIPER, 40}
};
/*
* match_mech is an example of many possible criteria functions.
* It matches the given mech type (in args) with the slot's mech info.
* If no match is found, pkcs11_GetCriteriaSession is asked to return
* CKR_MECHANISM_INVALID.
*/
boolean_t
match_mech(CK_SLOT_ID slot_id, void *args, CK_RV *rv)
{
CK_MECHANISM_INFO mech_info;
CK_MECHANISM_TYPE mech = (CK_MECHANISM_TYPE)args;
*rv = CKR_MECHANISM_INVALID;
return (C_GetMechanismInfo(slot_id, mech, &mech_info) == CKR_OK);
}
/*
* pkcs11_GetCriteriaSession will initialize the framework and do all
* the necessary work of calling C_GetSlotList(), C_GetMechanismInfo()
* C_OpenSession() to create a session that meets all the criteria in
* the given function pointer.
*
* The criteria function must return a boolean value of true or false.
* The arguments to the function are the current slot id, an opaque
* args value that is passed through to the function, and the error
* value pkcs11_GetCriteriaSession should return if no slot id meets the
* criteria.
*
* If the function is called multiple times, it will return a new session
* without reinitializing the framework.
*/
CK_RV
pkcs11_GetCriteriaSession(
boolean_t (*criteria)(CK_SLOT_ID slot_id, void *args, CK_RV *rv),
void *args, CK_SESSION_HANDLE_PTR hSession)
{
CK_RV rv;
CK_ULONG slotcount;
CK_SLOT_ID_PTR slot_list;
CK_SLOT_ID slot_id;
CK_ULONG i;
if (hSession == NULL || criteria == NULL) {
return (CKR_ARGUMENTS_BAD);
}
/* initialize PKCS #11 */
if (!pkcs11_initialized) {
rv = C_Initialize(NULL);
if ((rv != CKR_OK) &&
(rv != CKR_CRYPTOKI_ALREADY_INITIALIZED)) {
return (rv);
}
}
/* get slot count */
rv = C_GetSlotList(0, NULL, &slotcount);
if (rv != CKR_OK) {
return (rv);
}
if (slotcount == 0) {
return (CKR_FUNCTION_FAILED);
}
/* allocate memory for slot list */
slot_list = malloc(slotcount * sizeof (CK_SLOT_ID));
if (slot_list == NULL) {
return (CKR_HOST_MEMORY);
}
if ((rv = C_GetSlotList(0, slot_list, &slotcount)) != CKR_OK) {
free(slot_list);
return (rv);
}
/* find slot with matching criteria */
for (i = 0; i < slotcount; i++) {
slot_id = slot_list[i];
if ((*criteria)(slot_id, args, &rv)) {
break;
}
}
if (i == slotcount) {
/* no matching slot found */
free(slot_list);
return (rv); /* this rv is from the criteria function */
}
rv = C_OpenSession(slot_id, CKF_SERIAL_SESSION, NULL,
NULL, hSession);
free(slot_list);
return (rv);
}
/*
* SUNW_C_GetMechSession will initialize the framework and do all
* of the neccessary work of calling C_GetSlotList(), C_GetMechanismInfo()
* C_OpenSession() to create a session capable of providing the requested
* mechanism.
*
* If the function is called multiple times, it will return a new session
* without reinitializing the framework.
*/
CK_RV
SUNW_C_GetMechSession(CK_MECHANISM_TYPE mech, CK_SESSION_HANDLE_PTR hSession)
{
/*
* All the code in this function can be replaced with one line:
*
* return (pkcs11_GetCriteriaSession(match_mech, (void *)mech,
* hSession));
*
*/
CK_RV rv;
CK_ULONG slotcount;
CK_SLOT_ID_PTR slot_list;
CK_SLOT_ID slot_id;
CK_MECHANISM_INFO mech_info;
CK_ULONG i;
if (hSession == NULL) {
return (CKR_ARGUMENTS_BAD);
}
/* initialize PKCS #11 */
if (!pkcs11_initialized) {
rv = C_Initialize(NULL);
if ((rv != CKR_OK) &&
(rv != CKR_CRYPTOKI_ALREADY_INITIALIZED)) {
return (rv);
}
}
/* get slot count */
rv = C_GetSlotList(0, NULL, &slotcount);
if (rv != CKR_OK) {
return (rv);
}
if (slotcount == 0) {
return (CKR_FUNCTION_FAILED);
}
/* allocate memory for slot list */
slot_list = malloc(slotcount * sizeof (CK_SLOT_ID));
if (slot_list == NULL) {
return (CKR_HOST_MEMORY);
}
if ((rv = C_GetSlotList(0, slot_list, &slotcount)) != CKR_OK) {
free(slot_list);
return (rv);
}
/* find slot with matching mechanism */
for (i = 0; i < slotcount; i++) {
slot_id = slot_list[i];
if (C_GetMechanismInfo(slot_id, mech, &mech_info) == CKR_OK) {
/* found mechanism */
break;
}
}
if (i == slotcount) {
/* no matching mechanism found */
free(slot_list);
return (CKR_MECHANISM_INVALID);
}
rv = C_OpenSession(slot_id, CKF_SERIAL_SESSION, NULL,
NULL, hSession);
free(slot_list);
return (rv);
}
/*
* SUNW_C_KeyToObject creates a secret key object for the given
* mechanism from the rawkey data.
*/
CK_RV
SUNW_C_KeyToObject(CK_SESSION_HANDLE hSession, CK_MECHANISM_TYPE mech,
const void *rawkey, size_t rawkey_len, CK_OBJECT_HANDLE_PTR obj)
{
CK_RV rv;
CK_SESSION_INFO session_info;
CK_SLOT_ID slot_id;
CK_MECHANISM_INFO mech_info;
CK_ULONG i, j;
CK_KEY_TYPE keytype;
CK_ULONG num_asym_mechs, num_mapping;
/* template for creating generic secret key object */
CK_ATTRIBUTE template[NUM_SECRETKEY_ATTRS];
if ((hSession == CK_INVALID_HANDLE) || (obj == NULL) ||
(rawkey == NULL) || (rawkey_len == 0)) {
return (CKR_ARGUMENTS_BAD);
}
/*
* Check to make sure mechanism type is not for asymmetric key
* only operations. This function is only applicable to
* generating secret key.
*/
num_asym_mechs = sizeof (asymmetric_mechs) / sizeof (CK_MECHANISM_TYPE);
for (i = 0; i < num_asym_mechs; i++) {
if (mech == asymmetric_mechs[i]) {
return (CKR_MECHANISM_INVALID);
}
}
rv = C_GetSessionInfo(hSession, &session_info);
if (rv != CKR_OK) {
return (rv);
}
slot_id = session_info.slotID;
i = 0;
template[i].type = CKA_CLASS;
template[i].pValue = &objclass;
template[i].ulValueLen = sizeof (objclass);
i++;
/* get the key type for this mechanism */
if ((rv = pkcs11_mech2keytype(mech, &keytype)) != CKR_OK) {
return (rv);
}
assert(i < NUM_SECRETKEY_ATTRS);
template[i].type = CKA_KEY_TYPE;
template[i].pValue = &keytype;
template[i].ulValueLen = sizeof (keytype);
i++;
rv = C_GetMechanismInfo(slot_id, mech, &mech_info);
if (rv != CKR_OK) {
return (rv);
}
/* set the attribute type flag on object based on mechanism */
num_mapping = sizeof (mapping) / sizeof (ATTRTYPE_MECHINFO_MAPPING);
for (j = 0; j < num_mapping; j++) {
assert(i < NUM_SECRETKEY_ATTRS);
template[i].type = mapping[j].attr;
template[i].ulValueLen = sizeof (falsevalue);
if (mech_info.flags & ((mapping[j]).flag)) {
template[i].pValue = &truevalue;
} else {
template[i].pValue = &falsevalue;
}
i++;
}
assert(i < NUM_SECRETKEY_ATTRS);
template[i].type = CKA_TOKEN;
template[i].pValue = &falsevalue;
template[i].ulValueLen = sizeof (falsevalue);
i++;
assert(i < NUM_SECRETKEY_ATTRS);
template[i].type = CKA_VALUE;
template[i].pValue = (CK_VOID_PTR)rawkey;
template[i].ulValueLen = (CK_ULONG)rawkey_len;
i++;
rv = C_CreateObject(hSession, template, i, obj);
return (rv);
}
/*
* pkcs11_PasswdToPBKD2Object will create a secret key from the given string
* (e.g. passphrase) using PKCS#5 Password-Based Key Derivation Function 2
* (PBKD2).
*
* Session must be open. Salt and iterations use defaults.
*/
CK_RV
pkcs11_PasswdToPBKD2Object(CK_SESSION_HANDLE hSession, char *passphrase,
size_t passphrase_len, void *salt, size_t salt_len, CK_ULONG iterations,
CK_KEY_TYPE key_type, CK_ULONG key_len, CK_FLAGS key_flags,
CK_OBJECT_HANDLE_PTR obj)
{
CK_RV rv;
CK_PKCS5_PBKD2_PARAMS params;
CK_MECHANISM mechanism;
CK_KEY_TYPE asym_key_type;
CK_ULONG i, j, num_asym_mechs, num_fixed_secs, num_mapping;
CK_ATTRIBUTE template[NUM_SECRETKEY_ATTRS];
if (hSession == CK_INVALID_HANDLE || obj == NULL ||
passphrase == NULL || passphrase_len == 0 ||
iterations == 0UL) {
return (CKR_ARGUMENTS_BAD);
}
/*
* Check to make sure key type is not asymmetric. This function
* is only applicable to generating secret key.
*/
num_asym_mechs = sizeof (asymmetric_mechs) / sizeof (CK_MECHANISM_TYPE);
for (i = 0; i < num_asym_mechs; i++) {
rv = pkcs11_mech2keytype(asymmetric_mechs[i], &asym_key_type);
assert(rv == CKR_OK);
if (key_type == asym_key_type) {
return (CKR_KEY_TYPE_INCONSISTENT);
}
}
/*
* Key length must either be 0 or the correct size for PBKD of
* fixed-size secret keys. However, underlying key generation
* cannot have CKA_VALUE_LEN set for the key length attribute.
*/
num_fixed_secs =
sizeof (fixed_size_secrets) / sizeof (KEY_TYPE_SIZE_MAPPING);
for (i = 0; i < num_fixed_secs; i++) {
if (key_type == fixed_size_secrets[i].type) {
if (key_len == fixed_size_secrets[i].len) {
key_len = 0;
}
if (key_len == 0) {
break;
}
return (CKR_KEY_SIZE_RANGE);
}
}
if (salt == NULL || salt_len == 0) {
params.saltSource = 0;
params.pSaltSourceData = NULL;
params.ulSaltSourceDataLen = 0;
} else {
params.saltSource = CKZ_SALT_SPECIFIED;
params.pSaltSourceData = salt;
params.ulSaltSourceDataLen = salt_len;
}
params.iterations = iterations;
params.prf = CKP_PKCS5_PBKD2_HMAC_SHA1;
params.pPrfData = NULL;
params.ulPrfDataLen = 0;
params.pPassword = (CK_UTF8CHAR_PTR)passphrase;
params.ulPasswordLen = (CK_ULONG_PTR)&passphrase_len;
/*
* PKCS#11 spec error, ulPasswordLen should have been pulPasswordLen,
* or its type should have been CK_ULONG instead of CK_ULONG_PTR,
* but it's legacy now
*/
mechanism.mechanism = CKM_PKCS5_PBKD2;
mechanism.pParameter = ¶ms;
mechanism.ulParameterLen = sizeof (params);
i = 0;
template[i].type = CKA_CLASS;
template[i].pValue = &objclass;
template[i].ulValueLen = sizeof (objclass);
i++;
assert(i < NUM_SECRETKEY_ATTRS);
template[i].type = CKA_KEY_TYPE;
template[i].pValue = &key_type;
template[i].ulValueLen = sizeof (key_type);
i++;
assert(i < NUM_SECRETKEY_ATTRS);
template[i].type = CKA_TOKEN;
template[i].pValue = &falsevalue;
template[i].ulValueLen = sizeof (falsevalue);
i++;
if (key_len != 0) {
assert(i < NUM_SECRETKEY_ATTRS);
template[i].type = CKA_VALUE_LEN;
template[i].pValue = &key_len;
template[i].ulValueLen = sizeof (key_len);
i++;
}
/*
* C_GenerateKey may not implicitly set capability attributes,
* e.g. CKA_ENCRYPT, CKA_DECRYPT, CKA_WRAP, CKA_UNWRAP, ...
*/
num_mapping = sizeof (mapping) / sizeof (ATTRTYPE_MECHINFO_MAPPING);
for (j = 0; j < num_mapping; j++) {
assert(i < NUM_SECRETKEY_ATTRS);
template[i].type = mapping[j].attr;
template[i].pValue = (key_flags & ((mapping[j]).flag)) ?
&truevalue : &falsevalue;
template[i].ulValueLen = sizeof (falsevalue);
i++;
}
rv = C_GenerateKey(hSession, &mechanism, template, i, obj);
return (rv);
}
/*
* pkcs11_ObjectToKey gets the rawkey data from a secret key object.
* The caller is responsible to free the allocated rawkey data.
*
* Optionally the object can be destroyed after the value is retrieved.
* As an example, after using pkcs11_PasswdToPBKD2Object() to create a
* secret key object from a passphrase, an app may call pkcs11_ObjectToKey
* to get the rawkey data. The intermediate object may no longer be needed
* and should be destroyed.
*/
CK_RV
pkcs11_ObjectToKey(CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE obj,
void **rawkey, size_t *rawkey_len, boolean_t destroy_obj)
{
CK_RV rv;
CK_ATTRIBUTE template;
if (hSession == CK_INVALID_HANDLE)
return (CKR_SESSION_HANDLE_INVALID);
if (obj == 0)
return (CKR_OBJECT_HANDLE_INVALID);
if (rawkey == NULL || rawkey_len == NULL)
return (CKR_ARGUMENTS_BAD);
template.type = CKA_VALUE;
template.pValue = NULL;
template.ulValueLen = 0;
/* First get the size of the rawkey */
rv = C_GetAttributeValue(hSession, obj, &template, 1);
if (rv != CKR_OK) {
return (rv);
}
template.pValue = malloc(template.ulValueLen);
if (template.pValue == NULL) {
return (CKR_HOST_MEMORY);
}
/* Then get the rawkey data */
rv = C_GetAttributeValue(hSession, obj, &template, 1);
if (rv != CKR_OK) {
free(template.pValue);
return (rv);
}
if (destroy_obj) {
/*
* Could have asserted rv == CKR_OK, making threaded
* apps that share objects see stars. Here mercy is ok.
*/
(void) C_DestroyObject(hSession, obj);
}
*rawkey = template.pValue;
*rawkey_len = template.ulValueLen;
return (CKR_OK);
}
/*
* pkcs11_PasswdToKey will create PKCS#5 PBKD2 rawkey data from the
* given passphrase. The caller is responsible to free the allocated
* rawkey data.
*/
CK_RV
pkcs11_PasswdToKey(CK_SESSION_HANDLE hSession, char *passphrase,
size_t passphrase_len, void *salt, size_t salt_len, CK_KEY_TYPE key_type,
CK_ULONG key_len, void **rawkey, size_t *rawkey_len)
{
CK_RV rv;
CK_OBJECT_HANDLE obj;
rv = pkcs11_PasswdToPBKD2Object(hSession, passphrase, passphrase_len,
salt, salt_len, CK_PKCS5_PBKD2_ITERATIONS, key_type, key_len, 0,
&obj);
if (rv != CKR_OK)
return (rv);
rv = pkcs11_ObjectToKey(hSession, obj, rawkey, rawkey_len, B_TRUE);
return (rv);
}
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (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 2004 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <pthread.h>
#include <security/cryptoki.h>
#include "pkcs11Global.h"
#include "pkcs11Session.h"
#include "pkcs11Slot.h"
#include "metaGlobal.h"
/*
* C_OpenSession will need to create a pseudo session associated
* with the session created by the plugged in provider. Only
* minimal argument checking is done here, as we rely on the
* underlying provider to catch most errors.
*/
CK_RV
C_OpenSession(CK_SLOT_ID slotID, CK_FLAGS flags, CK_VOID_PTR pApplication,
CK_NOTIFY Notify, CK_SESSION_HANDLE_PTR phSession)
{
CK_RV rv;
CK_SLOT_ID true_id;
CK_SLOT_ID fw_st_id; /* id for accessing framework's slottable */
CK_SESSION_HANDLE prov_sess;
if (!pkcs11_initialized) {
return (CKR_CRYPTOKI_NOT_INITIALIZED);
}
/* Check for a fastpath */
if (purefastpath || policyfastpath) {
if (metaslot_enabled) {
/*
* if metaslot is enabled and we are in fastpath
* mode, only one other slot is in the framework
* so, need to go to that slot's entry
* to look up the true slot ID for the slot
*/
return (fast_funcs->C_OpenSession(TRUEID(slotID+1),
flags, pApplication, Notify, phSession));
} else {
return (fast_funcs->C_OpenSession(slotID, flags,
pApplication, Notify, phSession));
}
}
if (slotID == METASLOT_FRAMEWORK_ID) {
rv = meta_OpenSession(METASLOT_SLOTID, flags,
pApplication, Notify, &prov_sess);
} else {
/* Check that slotID is valid */
if (pkcs11_validate_and_convert_slotid(slotID, &fw_st_id)
!= CKR_OK) {
return (CKR_SLOT_ID_INVALID);
}
true_id = TRUEID(fw_st_id);
rv = FUNCLIST(fw_st_id)->C_OpenSession(true_id, flags,
pApplication, Notify, &prov_sess);
}
/* Present consistent interface for framework */
if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
return (CKR_FUNCTION_FAILED);
} else if (rv != CKR_OK) {
/* could not create session with provider, return now */
return (rv);
}
/* Provider was successful, now create session in framework */
if (slotID == METASLOT_FRAMEWORK_ID) {
rv = pkcs11_session_add(
slottable->st_slots[METASLOT_FRAMEWORK_ID],
METASLOT_FRAMEWORK_ID, phSession, prov_sess);
} else {
rv = pkcs11_session_add(slottable->st_slots[fw_st_id],
fw_st_id, phSession, prov_sess);
}
if (rv != CKR_OK) {
/* Trouble in the framework, clean up provider session */
FUNCLIST(slotID)->C_CloseSession(prov_sess);
}
return (rv);
}
/*
* C_CloseSession will close a session with the underlying provider,
* and if that's successful will close it in the framework.
*/
CK_RV
C_CloseSession(CK_SESSION_HANDLE hSession)
{
CK_RV rv;
pkcs11_session_t *sessp;
/* Check for a fastpath */
if (purefastpath || policyfastpath) {
return (fast_funcs->C_CloseSession(hSession));
}
if (!pkcs11_initialized) {
return (CKR_CRYPTOKI_NOT_INITIALIZED);
}
/* Obtain the session pointer */
HANDLE2SESSION(hSession, sessp, rv);
if (rv != CKR_OK) {
return (rv);
}
/* Delete the session with the provider */
rv = FUNCLIST(sessp->se_slotid)->C_CloseSession(sessp->se_handle);
/* Present consistent interface for framework */
if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
return (CKR_FUNCTION_FAILED);
} else if (rv != CKR_OK) {
/* could not delete session with provider, return now */
return (rv);
}
/* Delete session from the framework */
pkcs11_session_delete(slottable->st_slots[sessp->se_slotid], sessp);
return (rv);
}
/*
* C_CloseAllSessions will close all sessions associated with this
* slot with the underlying provider. If that is successful, will
* close the associated sessions in the framework. If the provider
* has not implemented C_CloseAllSessions, then we will loop through
* the list of sessions and individually call C_CloseSession.
*/
CK_RV
C_CloseAllSessions(CK_SLOT_ID slotID)
{
CK_RV rv, rv1;
CK_SLOT_ID true_id;
CK_SLOT_ID fw_st_id; /* id for accessing framework's slottable */
pkcs11_session_t *sessp, *sess_nextp;
pkcs11_slot_t *slotp;
if (!pkcs11_initialized) {
return (CKR_CRYPTOKI_NOT_INITIALIZED);
}
/* Check for a fastpath */
if (purefastpath || policyfastpath) {
if (metaslot_enabled) {
/*
* if metaslot is enabled and we are in fastpath
* mode, only one other slot is in the framework
* so, need to go to that slot's entry
* to look up the true slot ID for the slot
*/
return (fast_funcs->C_CloseAllSessions(
TRUEID(slotID+1)));
} else {
return (fast_funcs->C_CloseAllSessions(slotID));
}
}
/* Check that slotID is valid */
if (pkcs11_validate_and_convert_slotid(slotID, &fw_st_id) != CKR_OK) {
return (CKR_SLOT_ID_INVALID);
}
slotp = slottable->st_slots[fw_st_id];
true_id = TRUEID(fw_st_id);
rv = FUNCLIST(fw_st_id)->C_CloseAllSessions(true_id);
/* Present consistent interface for framework */
if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
/* Need to attempt to individually delete sessions */
/* reset rv */
rv = CKR_OK;
(void) pthread_mutex_lock(&slotp->sl_mutex);
sessp = slotp->sl_sess_list;
while (sessp) {
sess_nextp = sessp->se_next;
rv1 = FUNCLIST(fw_st_id)->
C_CloseSession(sessp->se_handle);
/* Record the first error encountered */
if ((rv == CKR_OK) && (rv1 != CKR_OK)) {
rv = rv1;
}
sessp = sess_nextp;
}
(void) pthread_mutex_unlock(&slotp->sl_mutex);
}
if (rv != CKR_OK) {
/* could not delete sessionlist with provider, return now */
return (rv);
}
/* Delete sessions from the framework */
pkcs11_sessionlist_delete(slotp);
return (rv);
}
/*
* C_GetSessionInfo is a pure wrapper to the underlying provider.
* The only argument checked is whether or not hSession is valid.
*/
CK_RV
C_GetSessionInfo(CK_SESSION_HANDLE hSession, CK_SESSION_INFO_PTR pInfo)
{
CK_RV rv;
CK_SLOT_ID slot_id;
pkcs11_session_t *sessp;
/* Check for a fastpath */
if (purefastpath || policyfastpath) {
rv = fast_funcs->C_GetSessionInfo(hSession, pInfo);
/*
* If metaslot is enabled, and we are here, that
* that means there's only 1 other slot in the
* framework, and that slot should be hidden.
* so, override value of slot id to be metaslot's
* slot id.
*/
if (metaslot_enabled) {
pInfo->slotID = METASLOT_FRAMEWORK_ID;
}
return (rv);
}
if (!pkcs11_initialized) {
return (CKR_CRYPTOKI_NOT_INITIALIZED);
}
/* Obtain the session pointer */
HANDLE2SESSION(hSession, sessp, rv);
if (rv != CKR_OK) {
return (rv);
}
/* Find the slot id for the framework */
slot_id = sessp->se_slotid;
/* Get session info from the provider */
rv = FUNCLIST(slot_id)->
C_GetSessionInfo(sessp->se_handle, pInfo);
/* Present consistent interface to the application */
if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
return (CKR_FUNCTION_FAILED);
}
/* Override value of slot id to framework's */
pInfo->slotID = slot_id;
return (rv);
}
/*
* C_GetOperationState is a pure wrapper to the underlying provider.
* The only argument checked is whether or not hSession is valid.
*/
CK_RV
C_GetOperationState(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pOperationState,
CK_ULONG_PTR pulOperationStateLen)
{
CK_RV rv;
pkcs11_session_t *sessp;
/* Check for a fastpath */
if (purefastpath || policyfastpath) {
return (fast_funcs->C_GetOperationState(hSession,
pOperationState, pulOperationStateLen));
}
if (!pkcs11_initialized) {
return (CKR_CRYPTOKI_NOT_INITIALIZED);
}
/* Obtain the session pointer */
HANDLE2SESSION(hSession, sessp, rv);
if (rv != CKR_OK) {
return (rv);
}
/* Get the operation state with the provider */
rv = FUNCLIST(sessp->se_slotid)->C_GetOperationState(sessp->se_handle,
pOperationState, pulOperationStateLen);
/* Present consistent interface to the application */
if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
return (CKR_FUNCTION_FAILED);
}
return (rv);
}
/*
* C_SetOperationState is a pure wrapper to the underlying provider.
* The only argument checked is whether or not hSession is valid.
*/
CK_RV
C_SetOperationState(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pOperationState,
CK_ULONG ulOperationStateLen, CK_OBJECT_HANDLE hEncryptionKey,
CK_OBJECT_HANDLE hAuthenticationKey)
{
CK_RV rv;
pkcs11_session_t *sessp;
/* Check for a fastpath */
if (purefastpath || policyfastpath) {
return (fast_funcs->C_SetOperationState(hSession,
pOperationState, ulOperationStateLen,
hEncryptionKey, hAuthenticationKey));
}
if (!pkcs11_initialized) {
return (CKR_CRYPTOKI_NOT_INITIALIZED);
}
/* Obtain the session pointer */
HANDLE2SESSION(hSession, sessp, rv);
if (rv != CKR_OK) {
return (rv);
}
/* Set the operation state with the provider */
rv = FUNCLIST(sessp->se_slotid)->C_SetOperationState(sessp->se_handle,
pOperationState, ulOperationStateLen, hEncryptionKey,
hAuthenticationKey);
/* Present consistent interface to the application */
if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
return (CKR_FUNCTION_FAILED);
}
return (rv);
}
/*
* C_Login is a pure wrapper to the underlying provider.
* The only argument checked is whether or not hSession is valid.
*/
CK_RV
C_Login(CK_SESSION_HANDLE hSession, CK_USER_TYPE userType,
CK_UTF8CHAR_PTR pPin, CK_ULONG ulPinLen)
{
CK_RV rv;
pkcs11_session_t *sessp;
/* Check for a fastpath */
if (purefastpath || policyfastpath) {
return (fast_funcs->C_Login(hSession, userType, pPin,
ulPinLen));
}
if (!pkcs11_initialized) {
return (CKR_CRYPTOKI_NOT_INITIALIZED);
}
/* Obtain the session pointer */
HANDLE2SESSION(hSession, sessp, rv);
if (rv != CKR_OK) {
return (rv);
}
/* Login with the provider */
rv = FUNCLIST(sessp->se_slotid)->C_Login(sessp->se_handle,
userType, pPin, ulPinLen);
/* Present consistent interface to the application */
if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
return (CKR_FUNCTION_FAILED);
}
return (rv);
}
/*
* C_Logout is a pure wrapper to the underlying provider.
* The only argument checked is whether or not hSession is valid.
*/
CK_RV
C_Logout(CK_SESSION_HANDLE hSession)
{
CK_RV rv;
pkcs11_session_t *sessp;
/* Check for a fastpath */
if (purefastpath || policyfastpath) {
return (fast_funcs->C_Logout(hSession));
}
if (!pkcs11_initialized) {
return (CKR_CRYPTOKI_NOT_INITIALIZED);
}
/* Obtain the session pointer */
HANDLE2SESSION(hSession, sessp, rv);
if (rv != CKR_OK) {
return (rv);
}
rv = FUNCLIST(sessp->se_slotid)->C_Logout(sessp->se_handle);
/* Present consistent interface to the application */
if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
return (CKR_FUNCTION_FAILED);
}
return (rv);
}
/*
* 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 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#ifndef _PKCS11_SESSION_H
#define _PKCS11_SESSION_H
#ifdef __cplusplus
extern "C" {
#endif
#define PKCS11_SESSION_MAGIC 0xECF00001
typedef struct pkcs11_session {
ulong_t se_magic; /* ensure this is a valid session */
CK_SESSION_HANDLE se_handle; /* from slot's C_OpenSession() */
CK_SLOT_ID se_slotid; /* slotID in framework */
struct pkcs11_session *se_prev; /* Chain of sessions */
struct pkcs11_session *se_next; /* in this slot */
} pkcs11_session_t;
/*
* This macro is used to typecast a session handle to a pointer
* to a session structure. It also checks to see if the session
* is tagged with a session magic number. This is to detect when an
* application passes a bogus session pointer.
*/
#define HANDLE2SESSION(hSession, sessionp, rv)\
sessionp = (pkcs11_session_t *)(hSession); \
rv = CKR_OK; \
if ((sessionp == NULL) || \
(sessionp->se_magic != PKCS11_SESSION_MAGIC)) \
rv = CKR_SESSION_HANDLE_INVALID;
struct pkcs11_slot;
extern CK_RV pkcs11_session_add(struct pkcs11_slot *pslot, CK_SLOT_ID slot_id,
CK_SESSION_HANDLE_PTR pfwhandle, CK_SESSION_HANDLE prov_sess);
extern void pkcs11_session_delete(struct pkcs11_slot *pslot,
pkcs11_session_t *psess);
extern void pkcs11_sessionlist_delete(struct pkcs11_slot *pslot);
#ifdef __cplusplus
}
#endif
#endif /* _PKCS11_SESSION_H */
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (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 2003 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <pthread.h>
#include <stdlib.h>
#include <security/cryptoki.h>
#include "pkcs11Global.h"
#include "pkcs11Slot.h"
#include "pkcs11Session.h"
/*
* pkcs11_session_add:
* Create a session and add it to the list of sessions associated
* with the slot it is being opened on. The session handle, fwsessionp,
* will be the memory address of the session, typecast to a CK_SESSION_HANDLE.
*
* Assumptions: slotp is a valid slot, mutexes are not held, and
* the provider already successfully opened related session.
*/
CK_RV
pkcs11_session_add(pkcs11_slot_t *slotp, CK_SLOT_ID slot_id,
CK_SESSION_HANDLE_PTR fwsessionp, CK_SESSION_HANDLE prov_sess)
{
pkcs11_session_t *newhandle = malloc(sizeof (pkcs11_session_t));
if (newhandle == NULL) {
return (CKR_HOST_MEMORY);
}
newhandle->se_magic = PKCS11_SESSION_MAGIC;
newhandle->se_handle = prov_sess;
newhandle->se_slotid = slot_id;
(void) pthread_mutex_lock(&slotp->sl_mutex);
/* Insert the new session in the front of the slot's session list */
if (slotp->sl_sess_list == NULL) {
slotp->sl_sess_list = newhandle;
newhandle->se_prev = NULL;
newhandle->se_next = NULL;
} else {
slotp->sl_sess_list->se_prev = newhandle;
newhandle->se_next = slotp->sl_sess_list;
newhandle->se_prev = NULL;
slotp->sl_sess_list = newhandle;
}
/* Typecast the address of session structure to a session handle */
*fwsessionp = (CK_SESSION_HANDLE)newhandle;
(void) pthread_mutex_unlock(&slotp->sl_mutex);
return (CKR_OK);
}
/*
* pkcs11_session_delete:
* Delete a session from a particular slot's session list.
*
* Assumptions: slotp is a valid slot, sessp is a valid session,
* provider has already successfully closed this session, and
* mutexes are not held.
*/
void
pkcs11_session_delete(pkcs11_slot_t *slotp, pkcs11_session_t *sessp)
{
/* Acquire the slot's lock */
(void) pthread_mutex_lock(&slotp->sl_mutex);
if (slotp->sl_sess_list == sessp) {
/* This is the first session in the list */
if (sessp->se_next != NULL) {
slotp->sl_sess_list = sessp->se_next;
sessp->se_next->se_prev = NULL;
} else {
/* Session is the only one in the list */
slotp->sl_sess_list = NULL;
}
} else {
/* Session is not the first one in the list */
if (sessp->se_next != NULL) {
/* Session is in the middle of the list */
sessp->se_prev->se_next = sessp->se_next;
sessp->se_next->se_prev = sessp->se_prev;
} else {
/* Session is the last one in the list */
sessp->se_prev->se_next = NULL;
}
}
/* Mark session as no longer valid */
sessp->se_magic = 0;
free(sessp);
(void) pthread_mutex_unlock(&slotp->sl_mutex);
}
/*
* pkcs11_sessionlist_delete:
* Delete all sessions associated with a particular slot's session list.
*
* Assumptions: slotp is a valid slot, no mutexes are held, and the
* sessions were successfully closed with the provider already.
*/
void
pkcs11_sessionlist_delete(pkcs11_slot_t *slotp)
{
pkcs11_session_t *sessp, *sess_nextp;
sessp = slotp->sl_sess_list;
/* Delete all the sessions in this slot's session list */
while (sessp) {
sess_nextp = sessp->se_next;
pkcs11_session_delete(slotp, sessp);
sessp = sess_nextp;
}
slotp->sl_sess_list = NULL;
}
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (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 2003 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <security/cryptoki.h>
#include "pkcs11Global.h"
#include "pkcs11Conf.h"
#include "pkcs11Session.h"
#include "pkcs11Slot.h"
/*
* C_SignInit will verify that the session handle is valid within the
* framework, that the mechanism is not disabled for the slot
* associated with this session, and then redirect to the underlying
* provider. Policy is only checked for C_SignInit, since it is
* required to be called before C_Sign and C_SignUpdate.
*/
CK_RV
C_SignInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism,
CK_OBJECT_HANDLE hKey)
{
CK_RV rv;
pkcs11_session_t *sessp;
CK_SLOT_ID slotid;
/* Check for a fastpath */
if (purefastpath || policyfastpath) {
if (policyfastpath &&
pkcs11_is_dismech(fast_slot, pMechanism->mechanism)) {
return (CKR_MECHANISM_INVALID);
}
return (fast_funcs->C_SignInit(hSession, pMechanism, hKey));
}
if (!pkcs11_initialized) {
return (CKR_CRYPTOKI_NOT_INITIALIZED);
}
/* Obtain the session pointer */
HANDLE2SESSION(hSession, sessp, rv);
if (rv != CKR_OK) {
return (rv);
}
slotid = sessp->se_slotid;
/* Make sure this is not a disabled mechanism */
if (pkcs11_is_dismech(slotid, pMechanism->mechanism)) {
return (CKR_MECHANISM_INVALID);
}
/* Initialize the digest with the underlying provider */
rv = FUNCLIST(slotid)->C_SignInit(sessp->se_handle,
pMechanism, hKey);
/* Present consistent interface to the application */
if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
return (CKR_FUNCTION_FAILED);
}
return (rv);
}
/*
* C_Sign is a pure wrapper to the underlying provider.
* The only argument checked is whether or not hSession is valid.
*/
CK_RV
C_Sign(CK_SESSION_HANDLE hSession,
CK_BYTE_PTR pData,
CK_ULONG ulDataLen,
CK_BYTE_PTR pSignature,
CK_ULONG_PTR pulSignatureLen)
{
CK_RV rv;
pkcs11_session_t *sessp;
/* Check for a fastpath */
if (purefastpath || policyfastpath) {
return (fast_funcs->C_Sign(hSession, pData, ulDataLen,
pSignature, pulSignatureLen));
}
if (!pkcs11_initialized) {
return (CKR_CRYPTOKI_NOT_INITIALIZED);
}
/* Obtain the session pointer */
HANDLE2SESSION(hSession, sessp, rv);
if (rv != CKR_OK) {
return (rv);
}
/* Pass data to the provider */
rv = FUNCLIST(sessp->se_slotid)->C_Sign(sessp->se_handle, pData,
ulDataLen, pSignature, pulSignatureLen);
/* Present consistent interface to the application */
if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
return (CKR_FUNCTION_FAILED);
}
return (rv);
}
/*
* C_SignUpdate is a pure wrapper to the underlying provider.
* The only argument checked is whether or not hSession is valid.
*/
CK_RV
C_SignUpdate(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pPart, CK_ULONG ulPartLen)
{
CK_RV rv;
pkcs11_session_t *sessp;
/* Check for a fastpath */
if (purefastpath || policyfastpath) {
return (fast_funcs->C_SignUpdate(hSession, pPart, ulPartLen));
}
if (!pkcs11_initialized) {
return (CKR_CRYPTOKI_NOT_INITIALIZED);
}
/* Obtain the session pointer */
HANDLE2SESSION(hSession, sessp, rv);
if (rv != CKR_OK) {
return (rv);
}
/* Pass data to the provider */
rv = FUNCLIST(sessp->se_slotid)->C_SignUpdate(sessp->se_handle, pPart,
ulPartLen);
/* Present consistent interface to the application */
if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
return (CKR_FUNCTION_FAILED);
}
return (rv);
}
/*
* C_SignFinal is a pure wrapper to the underlying provider.
* The only argument checked is whether or not hSession is valid.
*/
CK_RV
C_SignFinal(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pSignature,
CK_ULONG_PTR pulSignatureLen)
{
CK_RV rv;
pkcs11_session_t *sessp;
/* Check for a fastpath */
if (purefastpath || policyfastpath) {
return (fast_funcs->C_SignFinal(hSession, pSignature,
pulSignatureLen));
}
if (!pkcs11_initialized) {
return (CKR_CRYPTOKI_NOT_INITIALIZED);
}
/* Obtain the session pointer */
HANDLE2SESSION(hSession, sessp, rv);
if (rv != CKR_OK) {
return (rv);
}
/* Pass data to the provider */
rv = FUNCLIST(sessp->se_slotid)->C_SignFinal(sessp->se_handle,
pSignature, pulSignatureLen);
/* Present consistent interface to the application */
if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
return (CKR_FUNCTION_FAILED);
}
return (rv);
}
/*
* C_SignRecoverInit will verify that the session handle is valid within
* the framework, that the mechanism is not disabled for the slot
* associated with this session, and then redirect to the underlying
* provider. Policy is only checked for C_SignRecoverInit, since it is
* required to be called before C_SignRecover.
*/
CK_RV
C_SignRecoverInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism,
CK_OBJECT_HANDLE hKey)
{
CK_RV rv;
pkcs11_session_t *sessp;
CK_SLOT_ID slotid;
/* Check for a fastpath */
if (purefastpath || policyfastpath) {
if (policyfastpath &&
pkcs11_is_dismech(fast_slot, pMechanism->mechanism)) {
return (CKR_MECHANISM_INVALID);
}
return (fast_funcs->C_SignRecoverInit(hSession, pMechanism,
hKey));
}
if (!pkcs11_initialized) {
return (CKR_CRYPTOKI_NOT_INITIALIZED);
}
/* Obtain the session pointer */
HANDLE2SESSION(hSession, sessp, rv);
if (rv != CKR_OK) {
return (rv);
}
slotid = sessp->se_slotid;
/* Make sure this is not a disabled mechanism */
if (pkcs11_is_dismech(slotid, pMechanism->mechanism)) {
return (CKR_MECHANISM_INVALID);
}
/* Initialize the digest with the underlying provider */
rv = FUNCLIST(slotid)->C_SignRecoverInit(sessp->se_handle,
pMechanism, hKey);
/* Present consistent interface to the application */
if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
return (CKR_FUNCTION_FAILED);
}
return (rv);
}
/*
* C_SignRecover is a pure wrapper to the underlying provider.
* The only argument checked is whether or not hSession is valid.
*/
CK_RV
C_SignRecover(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pData,
CK_ULONG ulDataLen, CK_BYTE_PTR pSignature, CK_ULONG_PTR pulSignatureLen)
{
CK_RV rv;
pkcs11_session_t *sessp;
/* Check for a fastpath */
if (purefastpath || policyfastpath) {
return (fast_funcs->C_SignRecover(hSession, pData, ulDataLen,
pSignature, pulSignatureLen));
}
if (!pkcs11_initialized) {
return (CKR_CRYPTOKI_NOT_INITIALIZED);
}
/* Obtain the session pointer */
HANDLE2SESSION(hSession, sessp, rv);
if (rv != CKR_OK) {
return (rv);
}
/* Pass data to the provider */
rv = FUNCLIST(sessp->se_slotid)->C_SignRecover(sessp->se_handle, pData,
ulDataLen, pSignature, pulSignatureLen);
/* Present consistent interface to the application */
if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
return (CKR_FUNCTION_FAILED);
}
return (rv);
}
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (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 2004 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#ifndef _PKCS11_SLOT_H
#define _PKCS11_SLOT_H
#ifdef __cplusplus
extern "C" {
#endif
#include "pkcs11Session.h"
#define MECHLIST_SIZE 32
/*
* Used to pass arguments to child threads for C_WaitForSlotEvent.
*/
typedef struct wfse_args {
CK_FLAGS flags;
CK_VOID_PTR pReserved;
CK_SLOT_ID slotid;
} wfse_args_t;
typedef struct pkcs11_slot {
CK_SLOT_ID sl_id; /* real slotID from provider */
struct pkcs11_session *sl_sess_list; /* all open sessions */
pthread_mutex_t sl_mutex; /* protects: sl_sess_list, */
/* sl_tid, sl_wfse_state, */
/* and sl_wfse_args */
CK_FUNCTION_LIST_PTR sl_func_list; /* function entry points */
boolean_t sl_enabledpol; /* TRUE if policy for enabled */
CK_MECHANISM_TYPE_PTR sl_pol_mechs; /* policy restricted */
uint_t sl_pol_count; /* policy restricted */
boolean_t sl_norandom; /* TRUE if random is disabled */
void *sl_dldesc; /* from dlopen */
uint_t sl_prov_id; /* set by order read in */
uchar_t sl_wfse_state; /* Used by C_WaitForSlotEvent */
boolean_t sl_no_wfse; /* WaitForSlotEvent not impl */
pthread_t sl_tid; /* Used to track child thread */
wfse_args_t *sl_wfse_args; /* Used for WaitForSlotEvent */
} pkcs11_slot_t;
/*
* State definitions used for C_WaitForSlotEvent, stored in sl_wfse_state
* for each slot. These states are mutually exclusive, ie only one should
* be set at a time.
*/
#define WFSE_CLEAR 0x0
#define WFSE_EVENT 0x1
#define WFSE_ACTIVE 0x2
/*
* Dynamically allocated array of slots, indexed by the slotID assigned
* by the framework. st_first will be initialized to 1. Only if there
* is more than one other slot present, triggering the existence of the
* metaslot, with st_first be set to 0. st_last will be set to the
* last slotID assigned, also used for looping through the slottable.
*/
typedef struct pkcs11_slottable {
pkcs11_slot_t **st_slots;
pthread_mutex_t st_mutex; /* Protects all data in the slottable */
/* except for st_start_cond. */
CK_SLOT_ID st_first; /* First used slot ID, used for loops */
CK_SLOT_ID st_last; /* Last slot ID allocated */
ulong_t st_cur_size; /* current memory allocated */
pthread_cond_t st_wait_cond; /* Used for C_WaitForSlotEvent */
CK_SLOT_ID st_event_slot; /* Slot with event */
boolean_t st_wfse_active; /* A thread is actively running WFSE */
boolean_t st_blocking; /* Blocking for C_WaitForSlotEvent */
boolean_t st_list_signaled; /* Listener has been signaled */
uint_t st_thr_count; /* Used for C_WaitForSlotEvent */
pthread_t st_tid;
pthread_mutex_t st_start_mutex; /* wait for listener to start */
pthread_cond_t st_start_cond; /* signal when listener has started */
} pkcs11_slottable_t;
/*
* This macro is used to quickly derefence from a framework slot ID,
* provided by an application, to the function pointers for the correct
* underlying provider.
*/
#define FUNCLIST(slotID) (slottable->st_slots[(slotID)]->sl_func_list)
/*
* This macro is used to quickly get the slot ID associated with this
* slot ID, that is used by the underlying provider.
*/
#define TRUEID(slotID) (slottable->st_slots[(slotID)]->sl_id)
extern pkcs11_slottable_t *slottable;
extern CK_RV pkcs11_slottable_initialize();
extern CK_RV pkcs11_slottable_increase(ulong_t increase);
extern CK_RV pkcs11_slot_allocate(CK_SLOT_ID *slot);
extern CK_RV pkcs11_slottable_delete();
extern CK_RV pkcs11_is_valid_slot(CK_SLOT_ID slot_id);
extern CK_RV pkcs11_validate_and_convert_slotid(CK_SLOT_ID slot_id,
CK_SLOT_ID *real_slot_id);
#ifdef __cplusplus
}
#endif
#endif /* _PKCS11_SLOT_H */
/*
* 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 <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <time.h>
#include <security/cryptoki.h>
#include "pkcs11Global.h"
#include "pkcs11Conf.h"
#include "pkcs11Slot.h"
#include "metaGlobal.h"
static void *listener_waitforslotevent(void *arg);
static void *child_waitforslotevent(void *arg);
/*
* C_GetSlotList is implemented entirely within this framework,
* using the slottable that was created during the call to
* C_Initialize in pkcs11_slot_mapping(). The plugged in providers
* are only queried when tokenPresent is set.
*
* If metaslot is enabled, the slot that provides keystore support
* needs to be hidden. Therefore, even when fastpath is enabled,
* we can't go through fastpath because the slot needs to be
* hidden.
*/
CK_RV
C_GetSlotList(CK_BBOOL tokenPresent, CK_SLOT_ID_PTR pSlotList,
CK_ULONG_PTR pulCount)
{
CK_RV rv;
CK_RV prov_rv;
CK_SLOT_ID true_id;
CK_SLOT_INFO_PTR pinfo;
CK_SLOT_ID count = 0, i;
CK_SLOT_ID slot_id; /* slot ID for returning to the application */
/* Check for a fastpath */
if ((purefastpath || policyfastpath) && (!metaslot_enabled)) {
return (fast_funcs->C_GetSlotList(tokenPresent, pSlotList,
pulCount));
}
if (!pkcs11_initialized) {
return (CKR_CRYPTOKI_NOT_INITIALIZED);
}
if (pulCount == NULL) {
return (CKR_ARGUMENTS_BAD);
}
if (tokenPresent) {
/* Need to allocate memory for pinfo */
pinfo = malloc(sizeof (CK_SLOT_INFO));
if (pinfo == NULL) {
return (CKR_HOST_MEMORY);
}
}
/*
* Count the number of valid slots for returning to the application.
* If metaslot is enabled, the slot providing keystore support for
* metaslot is skipped. Therefore, we can't simply sequentially
* assign "i" as the slot id to be returned to the application.
* The variable "slot_id" is used for keeping track of the
* next slot id to be assigned.
*/
slot_id = slottable->st_first;
for (i = slottable->st_first; i <= slottable->st_last; i++) {
if ((pkcs11_is_valid_slot(i) == CKR_OK) &&
((!metaslot_enabled) || (i != metaslot_keystore_slotid))) {
/* Check if token present is required */
if (tokenPresent) {
/* Check with provider */
true_id = TRUEID(i);
prov_rv = FUNCLIST(i)->
C_GetSlotInfo(true_id, pinfo);
if ((prov_rv != CKR_OK) ||
!(pinfo->flags & CKF_TOKEN_PRESENT)) {
continue;
}
}
/* Fill in the given buffer if it is sufficient */
if (pSlotList && (*pulCount > count)) {
pSlotList[count] = slot_id;
slot_id++;
}
count++;
}
}
/* pSlotList set to NULL means caller only wants count */
if ((*pulCount < count) && (pSlotList != NULL)) {
rv = CKR_BUFFER_TOO_SMALL;
} else {
rv = CKR_OK;
}
*pulCount = count;
if (tokenPresent) {
free(pinfo);
}
return (rv);
}
CK_RV
C_GetSlotInfo(CK_SLOT_ID slotID, CK_SLOT_INFO_PTR pInfo)
{
CK_RV rv;
CK_SLOT_ID fw_st_id; /* id for accessing framework's slottable */
if (!pkcs11_initialized) {
return (CKR_CRYPTOKI_NOT_INITIALIZED);
}
/* Check for a fastpath */
if ((purefastpath || policyfastpath) && !metaslot_enabled)
return (fast_funcs->C_GetSlotInfo(slotID, pInfo));
if (slotID == METASLOT_FRAMEWORK_ID) {
/* just need to get metaslot information */
return (meta_GetSlotInfo(METASLOT_SLOTID, pInfo));
}
/* Check that slotID is valid */
if (pkcs11_validate_and_convert_slotid(slotID, &fw_st_id) != CKR_OK) {
return (CKR_SLOT_ID_INVALID);
}
rv = FUNCLIST(fw_st_id)->C_GetSlotInfo(TRUEID(fw_st_id), pInfo);
/* Present consistent interface to the application */
if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
return (CKR_FUNCTION_FAILED);
}
return (rv);
}
CK_RV
C_GetTokenInfo(CK_SLOT_ID slotID, CK_TOKEN_INFO_PTR pInfo)
{
CK_RV rv;
CK_SLOT_ID fw_st_id; /* id for accessing framework's slottable */
if (!pkcs11_initialized) {
return (CKR_CRYPTOKI_NOT_INITIALIZED);
}
/* Check for a fastpath */
if ((purefastpath || policyfastpath) && !metaslot_enabled)
return (fast_funcs->C_GetTokenInfo(slotID, pInfo));
if (slotID == METASLOT_FRAMEWORK_ID) {
/* just need to get metaslot information */
return (meta_GetTokenInfo(METASLOT_SLOTID, pInfo));
}
/* Check that slotID is valid */
if (pkcs11_validate_and_convert_slotid(slotID, &fw_st_id) != CKR_OK) {
return (CKR_SLOT_ID_INVALID);
}
rv = FUNCLIST(fw_st_id)->C_GetTokenInfo(TRUEID(fw_st_id), pInfo);
/* Present consistent interface to the application */
if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
return (CKR_FUNCTION_FAILED);
}
return (rv);
}
/*
* C_WaitForSlotEvent cannot be a direct pass through to the underlying
* provider (except in the case of fastpath), due to the complex nature
* of this function. The calling application is asking to be alerted
* when an event has occurred on any of the slots in the framework, so
* we need to check with all underlying providers and ask for events
* on any of their slots. If this is called in blocking mode, we will
* need to start threads to wait for slot events for each provider
* plugged into the framework.
*/
CK_RV
C_WaitForSlotEvent(CK_FLAGS flags, CK_SLOT_ID_PTR pSlot, CK_VOID_PTR pReserved)
{
CK_SLOT_ID i, j;
uint32_t prov_id;
int32_t last_prov_id = -1;
CK_RV rv = CKR_OK;
CK_SLOT_ID event_slot;
pkcs11_slot_t *cur_slot;
/* Check for a fastpath */
if (purefastpath || policyfastpath) {
return (fast_funcs->C_WaitForSlotEvent(flags, pSlot,
pReserved));
}
if (!pkcs11_initialized) {
return (CKR_CRYPTOKI_NOT_INITIALIZED);
}
if (pReserved != NULL) {
return (CKR_ARGUMENTS_BAD);
}
/*
* Check to see if we're already blocking on another threads
* call to this function. If so, behaviour is undefined so
* we should return to application.
*/
(void) pthread_mutex_lock(&slottable->st_mutex);
if ((slottable->st_blocking) || (slottable->st_wfse_active)) {
(void) pthread_mutex_unlock(&slottable->st_mutex);
return (CKR_FUNCTION_FAILED);
} else {
slottable->st_wfse_active = B_TRUE;
(void) pthread_mutex_unlock(&slottable->st_mutex);
}
/*
* Check first to see if any events have been recorded
* already on any of the slots, regardless of blocking or
* thread status.
*/
for (i = slottable->st_first; i <= slottable->st_last; i++) {
cur_slot = slottable->st_slots[i];
if (cur_slot->sl_wfse_state == WFSE_EVENT) {
/* found one, clear event and notify application */
(void) pthread_mutex_lock(&cur_slot->sl_mutex);
cur_slot->sl_wfse_state = WFSE_CLEAR;
(void) pthread_mutex_unlock(&cur_slot->sl_mutex);
*pSlot = i;
/*
* This event has been captured, clear the function's
* active status. Other threads may now enter this
* function.
*/
(void) pthread_mutex_lock(&slottable->st_mutex);
slottable->st_wfse_active = B_FALSE;
(void) pthread_mutex_unlock(&slottable->st_mutex);
return (CKR_OK);
}
}
/*
* We could not find any existing event, so let's see
* if we can block and start threads to watch for events.
*/
if (flags & CKF_DONT_BLOCK) {
/*
* Application does not want us to block so check with
* underlying providers to see if any events have occurred.
* Not every provider will have implemented this function,
* so error codes or CKR_NO_EVENT can be ignored.
*/
for (i = slottable->st_first; i <= slottable->st_last; i++) {
prov_id = slottable->st_slots[i]->sl_prov_id;
cur_slot = slottable->st_slots[i];
/*
* Only do process once per provider.
*/
if (prov_id == last_prov_id) {
continue;
}
/*
* Check to make sure a child thread is not already
* running, due to another of the application's
* thread calling this function.
*/
(void) pthread_mutex_lock(&cur_slot->sl_mutex);
if (cur_slot->sl_wfse_state == WFSE_ACTIVE) {
(void) pthread_mutex_unlock(
&cur_slot->sl_mutex);
continue;
}
cur_slot->sl_wfse_state = WFSE_ACTIVE;
/*
* Release the hold on the slot's mutex while we
* are waiting for this function to complete.
*/
(void) pthread_mutex_unlock(&cur_slot->sl_mutex);
rv = FUNCLIST(i)->C_WaitForSlotEvent(flags,
pSlot, pReserved);
(void) pthread_mutex_lock(&cur_slot->sl_mutex);
cur_slot->sl_wfse_state = WFSE_CLEAR;
(void) pthread_mutex_unlock(&cur_slot->sl_mutex);
/* See if we've found a slot with an event */
if ((rv == CKR_OK) && (pSlot != NULL)) {
/*
* Try to map the returned slotid to a slot
* allocated by the framework. All slots from
* one provider are adjacent in the framework's
* slottable, so search for a mapping while
* the prov_id field is the same.
*/
j = i;
while (prov_id ==
slottable->st_slots[j]->sl_prov_id) {
/* Find the slot, remap pSlot */
if (*pSlot == TRUEID(j)) {
*pSlot = j;
(void) pthread_mutex_lock(
&slottable->st_mutex);
slottable->st_wfse_active =
B_FALSE;
(void) pthread_mutex_unlock(
&slottable->st_mutex);
return (CKR_OK);
}
j++;
}
}
/*
* If we reach this part of the loop, this
* provider either had no events, did not support
* this function, or set pSlot to a value we
* could not find in the slots associated with
* this provider. Continue checking with remaining
* providers.
*/
last_prov_id = prov_id;
}
/* No provider had any events */
(void) pthread_mutex_lock(&slottable->st_mutex);
slottable->st_wfse_active = B_FALSE;
(void) pthread_mutex_unlock(&slottable->st_mutex);
return (CKR_NO_EVENT);
} else if (!(flags & CKF_DONT_BLOCK) && (pkcs11_cant_create_threads)) {
/*
* Application has asked us to block, but forbidden
* us from creating threads. This is too risky to perform
* with underlying providers (we may block indefinitely),
* so will return an error in this case.
*/
(void) pthread_mutex_lock(&slottable->st_mutex);
slottable->st_wfse_active = B_FALSE;
(void) pthread_mutex_unlock(&slottable->st_mutex);
return (CKR_FUNCTION_FAILED);
}
/*
* Grab the st_start_mutex now, which will prevent the listener
* thread from signaling on st_start_cond before we're ready to
* wait for it.
*/
(void) pthread_mutex_lock(&slottable->st_start_mutex);
/*
* Application allows us to create threads and has
* asked us to block. Create listener thread to wait for
* child threads to return.
*/
(void) pthread_mutex_lock(&slottable->st_mutex);
if (pthread_create(&slottable->st_tid, NULL,
listener_waitforslotevent, NULL) != 0) {
slottable->st_wfse_active = B_FALSE;
(void) pthread_mutex_unlock(&slottable->st_mutex);
(void) pthread_mutex_unlock(&slottable->st_start_mutex);
return (CKR_FUNCTION_FAILED);
}
(void) pthread_mutex_unlock(&slottable->st_mutex);
/*
* Wait for the listening thread to get started before
* we spawn child threads.
*/
(void) pthread_cond_wait(&slottable->st_start_cond,
&slottable->st_start_mutex);
(void) pthread_mutex_unlock(&slottable->st_start_mutex);
/*
* Need to hold the mutex on the entire slottable for the
* entire setup of the child threads. Otherwise, the first
* child thread may complete before a later child thread is
* fully started, resulting in an inaccurate value of
* st_thr_count and a potential race condition.
*/
(void) pthread_mutex_lock(&slottable->st_mutex);
/*
* Create child threads to check with the plugged in providers
* to check for events. Keep a count of the current open threads,
* so the listener thread knows when there are no more children
* to listen for. Also, make sure a thread is not already active
* for that provider.
*/
for (i = slottable->st_first; i <= slottable->st_last; i++) {
prov_id = slottable->st_slots[i]->sl_prov_id;
cur_slot = slottable->st_slots[i];
/*
* Only do process once per provider.
*/
if (prov_id == last_prov_id) {
continue;
}
/*
* Check to make sure a child thread is not already running,
* due to another of the application's threads calling
* this function. Also, check that the provider has actually
* implemented this function.
*/
(void) pthread_mutex_lock(&cur_slot->sl_mutex);
if ((cur_slot->sl_wfse_state == WFSE_ACTIVE) ||
(cur_slot->sl_no_wfse)) {
(void) pthread_mutex_unlock(&cur_slot->sl_mutex);
last_prov_id = prov_id;
continue;
}
/* Set slot to active */
cur_slot->sl_wfse_state = WFSE_ACTIVE;
/*
* set up variable to pass arguments to child threads.
* Only need to set up once, as values will remain the
* same for each successive call.
*/
if (cur_slot->sl_wfse_args == NULL) {
cur_slot->sl_wfse_args = malloc(sizeof (wfse_args_t));
if (cur_slot->sl_wfse_args == NULL) {
(void) pthread_mutex_unlock(
&cur_slot->sl_mutex);
slottable->st_wfse_active = B_FALSE;
(void) pthread_mutex_unlock(
&slottable->st_mutex);
return (CKR_HOST_MEMORY);
}
cur_slot->sl_wfse_args->flags = flags;
cur_slot->sl_wfse_args->pReserved = pReserved;
cur_slot->sl_wfse_args->slotid = i;
}
/* Create child thread */
if (pthread_create(&cur_slot->sl_tid, NULL,
child_waitforslotevent,
(void *)cur_slot->sl_wfse_args) != 0) {
(void) pthread_mutex_unlock(&cur_slot->sl_mutex);
continue;
}
(void) pthread_mutex_unlock(&cur_slot->sl_mutex);
/*
* This counter is decremented every time a
* child_waitforslotevent() wakes up the listener.
*/
slottable->st_thr_count++;
last_prov_id = prov_id;
}
/* If no children are listening, kill the listener */
if (slottable->st_thr_count == 0) {
(void) pthread_cancel(slottable->st_tid);
/* If there are no child threads, no event will occur */
slottable->st_wfse_active = B_FALSE;
(void) pthread_mutex_unlock(&slottable->st_mutex);
return (CKR_NO_EVENT);
}
(void) pthread_mutex_unlock(&slottable->st_mutex);
/* Wait for listener thread to terminate */
(void) pthread_join(slottable->st_tid, NULL);
/* Make sure C_Finalize has not been called */
if (!pkcs11_initialized) {
(void) pthread_mutex_lock(&slottable->st_mutex);
slottable->st_wfse_active = B_FALSE;
(void) pthread_mutex_unlock(&slottable->st_mutex);
return (CKR_CRYPTOKI_NOT_INITIALIZED);
}
/* See if any events actually occurred */
(void) pthread_mutex_lock(&slottable->st_mutex);
event_slot = slottable->st_event_slot;
(void) pthread_mutex_unlock(&slottable->st_mutex);
if (pkcs11_is_valid_slot(event_slot) == CKR_OK) {
(void) pthread_mutex_lock(&slottable->
st_slots[event_slot]->sl_mutex);
if (slottable->st_slots[event_slot]->
sl_wfse_state == WFSE_EVENT) {
/* An event has occurred on this slot */
slottable->st_slots[event_slot]->sl_wfse_state =
WFSE_CLEAR;
(void) pthread_mutex_unlock(&slottable->
st_slots[event_slot]->sl_mutex);
*pSlot = event_slot;
(void) pthread_mutex_lock(&slottable->st_mutex);
slottable->st_blocking = B_FALSE;
slottable->st_wfse_active = B_FALSE;
(void) pthread_mutex_unlock(&slottable->st_mutex);
return (CKR_OK);
} else {
(void) pthread_mutex_unlock(&slottable->
st_slots[event_slot]->sl_mutex);
}
}
(void) pthread_mutex_lock(&slottable->st_mutex);
slottable->st_blocking = B_FALSE;
slottable->st_wfse_active = B_FALSE;
(void) pthread_mutex_unlock(&slottable->st_mutex);
/* No provider reported any events, or no provider implemented this */
return (CKR_NO_EVENT);
}
/*
* C_GetMechanismList cannot just be a direct pass through to the
* underlying provider, because we allow the administrator to
* disable certain mechanisms from specific providers. This affects
* both pulCount and pMechanismList. Only when the fastpath with
* no policy is in effect can we pass through directly to the
* underlying provider.
*
* It is necessary, for policy filtering, to get the actual list
* of mechanisms from the underlying provider, even if the calling
* application is just requesting a count. It is the only way to
* get an accurate count of the number of mechanisms actually available.
*/
CK_RV
C_GetMechanismList(CK_SLOT_ID slotID, CK_MECHANISM_TYPE_PTR pMechanismList,
CK_ULONG_PTR pulCount)
{
CK_RV rv = CKR_OK;
CK_ULONG mech_count;
CK_ULONG tmpmech_count;
CK_MECHANISM_TYPE_PTR pmech_list, tmpmech_list;
CK_SLOT_ID true_id;
CK_SLOT_ID fw_st_id; /* id for accessing framework's slottable */
CK_FUNCTION_LIST_PTR prov_funcs;
CK_ULONG i;
if (!pkcs11_initialized) {
return (CKR_CRYPTOKI_NOT_INITIALIZED);
}
/* Check for a fastpath */
if ((purefastpath || policyfastpath) && !metaslot_enabled)
return (fast_funcs->C_GetMechanismList(slotID,
pMechanismList, pulCount));
if (slotID == METASLOT_FRAMEWORK_ID) {
return (meta_GetMechanismList(METASLOT_SLOTID, pMechanismList,
pulCount));
}
/* Check that slotID is valid */
if (pkcs11_validate_and_convert_slotid(slotID, &fw_st_id) != CKR_OK) {
return (CKR_SLOT_ID_INVALID);
}
if (policyfastpath) {
true_id = fw_st_id;
slotID = fast_slot;
prov_funcs = fast_funcs;
} else {
true_id = TRUEID(fw_st_id);
prov_funcs = FUNCLIST(fw_st_id);
}
mech_count = 0;
tmpmech_count = MECHLIST_SIZE;
/*
* Allocate memory for a mechanism list. We are assuming
* that most mechanism lists will be less than MECHLIST_SIZE.
* If that is not enough memory, we will try a second time
* with more memory allocated.
*/
pmech_list = malloc(tmpmech_count * sizeof (CK_MECHANISM_TYPE));
if (pmech_list == NULL) {
return (CKR_HOST_MEMORY);
}
/*
* Attempt to get the mechanism list. PKCS11 supports
* removable media, so the mechanism list of a slot can vary
* over the life of the application.
*/
rv = prov_funcs->C_GetMechanismList(true_id,
pmech_list, &tmpmech_count);
if (rv == CKR_BUFFER_TOO_SMALL) {
/* Need to use more space */
tmpmech_list = pmech_list;
pmech_list = realloc
(tmpmech_list, tmpmech_count * sizeof (CK_MECHANISM_TYPE));
if (pmech_list == NULL) {
free(tmpmech_list);
return (CKR_HOST_MEMORY);
}
/* Try again to get mechanism list. */
rv = prov_funcs->C_GetMechanismList(true_id,
pmech_list, &tmpmech_count);
}
/*
* Present consistent face to calling application.
* If something strange has happened, or this function
* is not supported by this provider, return a count
* of zero mechanisms.
*/
if (rv != CKR_OK) {
*pulCount = 0;
free(pmech_list);
return (CKR_OK);
}
/*
* Process the mechanism list, removing any mechanisms
* that are disabled via the framework. Even if the
* application is only asking for a count, we must
* process the actual mechanisms being offered by this slot.
* We could not just subtract our stored count of disabled
* mechanisms, since it is not guaranteed that those
* mechanisms are actually supported by the slot.
*/
for (i = 0; i < tmpmech_count; i++) {
/* Filter out the disabled mechanisms */
if (pkcs11_is_dismech(fw_st_id, pmech_list[i])) {
continue;
}
/*
* Only set pMechanismList if enough memory
* is available. If it was set to NULL
* originally, this loop will just be counting
* mechanims.
*/
if (pMechanismList && (*pulCount > mech_count)) {
pMechanismList[mech_count] = pmech_list[i];
}
mech_count++;
}
/*
* Catch the case where pMechanismList was not set to NULL,
* yet the buffer was not large enough. If pMechanismList is
* set to NULL, this function will simply set pulCount and
* return CKR_OK.
*/
if ((*pulCount < mech_count) && (pMechanismList != NULL)) {
*pulCount = mech_count;
free(pmech_list);
return (CKR_BUFFER_TOO_SMALL);
}
*pulCount = mech_count;
free(pmech_list);
return (CKR_OK);
}
CK_RV
C_GetMechanismInfo(CK_SLOT_ID slotID, CK_MECHANISM_TYPE type,
CK_MECHANISM_INFO_PTR pInfo)
{
CK_RV rv;
CK_SLOT_ID true_id;
CK_SLOT_ID fw_st_id; /* id for accessing framework's slottable */
CK_FUNCTION_LIST_PTR prov_funcs;
if (!pkcs11_initialized) {
return (CKR_CRYPTOKI_NOT_INITIALIZED);
}
/* Check for a fastpath */
if ((purefastpath || policyfastpath) && !metaslot_enabled)
return (fast_funcs->C_GetMechanismInfo(slotID, type, pInfo));
if (slotID == METASLOT_FRAMEWORK_ID) {
/* just need to get metaslot information */
return (meta_GetMechanismInfo(METASLOT_SLOTID, type, pInfo));
}
/* Check that slotID is valid */
if (pkcs11_validate_and_convert_slotid(slotID, &fw_st_id) != CKR_OK) {
return (CKR_SLOT_ID_INVALID);
}
if (policyfastpath) {
true_id = fw_st_id;
slotID = fast_slot;
prov_funcs = fast_funcs;
} else {
true_id = TRUEID(fw_st_id);
prov_funcs = FUNCLIST(fw_st_id);
}
/* Make sure this is not a disabled mechanism */
if (pkcs11_is_dismech(fw_st_id, type)) {
return (CKR_MECHANISM_INVALID);
}
rv = prov_funcs->C_GetMechanismInfo(true_id, type, pInfo);
/* Present consistent interface to the application */
if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
return (CKR_FUNCTION_FAILED);
}
return (rv);
}
CK_RV
C_InitToken(CK_SLOT_ID slotID, CK_UTF8CHAR_PTR pPin, CK_ULONG ulPinLen,
CK_UTF8CHAR_PTR pLabel)
{
CK_RV rv;
CK_SLOT_ID fw_st_id; /* id for accessing framework's slottable */
if (!pkcs11_initialized) {
return (CKR_CRYPTOKI_NOT_INITIALIZED);
}
/* Check for a fastpath */
if ((purefastpath || policyfastpath) && !metaslot_enabled)
return (fast_funcs->C_InitToken(slotID, pPin, ulPinLen,
pLabel));
if (slotID == METASLOT_FRAMEWORK_ID) {
/* just need to get metaslot information */
return (meta_InitToken(METASLOT_SLOTID, pPin, ulPinLen,
pLabel));
}
/* Check that slotID is valid */
if (pkcs11_validate_and_convert_slotid(slotID, &fw_st_id) != CKR_OK) {
return (CKR_SLOT_ID_INVALID);
}
rv = FUNCLIST(fw_st_id)->C_InitToken(TRUEID(fw_st_id), pPin, ulPinLen,
pLabel);
/* Present consistent interface to the application */
if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
return (CKR_FUNCTION_FAILED);
}
return (rv);
}
CK_RV
C_InitPIN(CK_SESSION_HANDLE hSession, CK_UTF8CHAR_PTR pPin, CK_ULONG ulPinLen)
{
CK_RV rv;
pkcs11_session_t *sessp;
/* Check for a fastpath */
if (purefastpath || policyfastpath) {
return (fast_funcs->C_InitPIN(hSession, pPin, ulPinLen));
}
if (!pkcs11_initialized) {
return (CKR_CRYPTOKI_NOT_INITIALIZED);
}
/* Obtain the session pointer */
HANDLE2SESSION(hSession, sessp, rv);
if (rv != CKR_OK) {
return (rv);
}
/* Initialize the PIN with the provider */
rv = FUNCLIST(sessp->se_slotid)->C_InitPIN(sessp->se_handle,
pPin, ulPinLen);
/* Present consistent interface to the application */
if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
return (CKR_FUNCTION_FAILED);
}
return (rv);
}
CK_RV
C_SetPIN(CK_SESSION_HANDLE hSession, CK_UTF8CHAR_PTR pOldPin,
CK_ULONG ulOldPinLen, CK_UTF8CHAR_PTR pNewPin,
CK_ULONG ulNewPinLen)
{
CK_RV rv;
pkcs11_session_t *sessp;
/* Check for a fastpath */
if (purefastpath || policyfastpath) {
return (fast_funcs->C_SetPIN(hSession, pOldPin, ulOldPinLen,
pNewPin, ulNewPinLen));
}
if (!pkcs11_initialized) {
return (CKR_CRYPTOKI_NOT_INITIALIZED);
}
/* Obtain the session pointer */
HANDLE2SESSION(hSession, sessp, rv);
if (rv != CKR_OK) {
return (rv);
}
/* Set the PIN with the provider */
rv = FUNCLIST(sessp->se_slotid)->C_SetPIN(sessp->se_handle,
pOldPin, ulOldPinLen, pNewPin, ulNewPinLen);
/* Present consistent interface to the application */
if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
return (CKR_FUNCTION_FAILED);
}
return (rv);
}
/*
* listener_waitforslotevent is spawned by the main C_WaitForSlotEvent()
* to listen for events from any of the providers. It also watches the
* count of threads, which may go to zero with no recorded events, if
* none of the underlying providers have actually implemented this
* function.
*/
/*ARGSUSED*/
static void *
listener_waitforslotevent(void *arg) {
CK_SLOT_ID eventID;
/* Mark slottable in state blocking */
(void) pthread_mutex_lock(&slottable->st_mutex);
slottable->st_blocking = B_TRUE;
/* alert calling thread that this thread has started */
(void) pthread_mutex_lock(&slottable->st_start_mutex);
(void) pthread_cond_signal(&slottable->st_start_cond);
(void) pthread_mutex_unlock(&slottable->st_start_mutex);
/* wait for an event, or number of threads to reach zero */
for (;;) {
/*
* Make sure we've really been signaled, and not waking
* for another reason.
*/
while (slottable->st_list_signaled != B_TRUE) {
(void) pthread_cond_wait(&slottable->st_wait_cond,
&slottable->st_mutex);
}
slottable->st_list_signaled = B_FALSE;
/* See why we were woken up */
if (!pkcs11_initialized) {
/* Another thread has called C_Finalize() */
(void) pthread_mutex_unlock(&slottable->st_mutex);
return (NULL);
}
/* A thread has finished, decrement counter */
slottable->st_thr_count--;
eventID = slottable->st_event_slot;
if (pkcs11_is_valid_slot(eventID) == CKR_OK) {
(void) pthread_mutex_lock(&slottable->
st_slots[eventID]->sl_mutex);
if (slottable->st_slots[eventID]->
sl_wfse_state == WFSE_EVENT) {
(void) pthread_mutex_unlock(&slottable->
st_slots[eventID]->sl_mutex);
/*
* st_event_slot is set to a valid value, event
* flag is set for that slot. The flag will
* be cleared by main C_WaitForSlotEvent().
*/
(void) pthread_mutex_unlock(
&slottable->st_mutex);
pthread_exit(0);
} else {
(void) pthread_mutex_unlock(&slottable->
st_slots[eventID]->sl_mutex);
}
}
if (slottable->st_thr_count == 0) {
(void) pthread_mutex_unlock(&slottable->st_mutex);
/* No more threads, no events found */
pthread_exit(0);
}
}
/*NOTREACHED*/
return (NULL);
}
/*
* child_waitforslotevent is used as a child thread to contact
* underlying provider's C_WaitForSlotEvent().
*/
static void *
child_waitforslotevent(void *arg) {
wfse_args_t *wfse = (wfse_args_t *)arg;
CK_SLOT_ID slot;
CK_RV rv;
uint32_t cur_prov;
CK_SLOT_ID i;
rv = FUNCLIST(wfse->slotid)->C_WaitForSlotEvent(wfse->flags, &slot,
wfse->pReserved);
/*
* Need to hold the mutex while processing the results, to
* keep things synchronized with the listener thread and
* the slottable. Otherwise, due to the timing
* at which some underlying providers complete, the listener
* thread may not actually be blocking on st_wait_cond when
* this child signals. Holding the lock a bit longer prevents
* this from happening.
*/
(void) pthread_mutex_lock(&slottable->st_mutex);
while (slottable->st_list_signaled == B_TRUE) {
/*
* We've taken the mutex when the listener should have
* control. Release the mutex, thread scheduler should
* give control back to the listener.
*/
(void) pthread_mutex_unlock(&slottable->st_mutex);
(void) sleep(1);
(void) pthread_mutex_lock(&slottable->st_mutex);
}
if (rv == CKR_OK) {
/* we've had an event, find slot and store it */
cur_prov = slottable->st_slots[wfse->slotid]->sl_prov_id;
/*
* It is safe to unset active status now, since call to
* underlying provider has already terminated, and we
* hold the slottable wide mutex (st_mutex).
*/
(void) pthread_mutex_lock(&slottable->
st_slots[wfse->slotid]->sl_mutex);
slottable->st_slots[wfse->slotid]->sl_wfse_state = WFSE_CLEAR;
(void) pthread_mutex_unlock(&slottable->
st_slots[wfse->slotid]->sl_mutex);
for (i = wfse->slotid; i <= slottable->st_last; i++) {
if (cur_prov != slottable->st_slots[i]->sl_prov_id) {
break;
}
if (slot == slottable->st_slots[i]->sl_id) {
(void) pthread_mutex_lock(&slottable->
st_slots[i]->sl_mutex);
slottable->st_slots[i]->
sl_wfse_state = WFSE_EVENT;
(void) pthread_mutex_unlock(&slottable->
st_slots[i]->sl_mutex);
slottable->st_event_slot = i;
if (slottable->st_blocking) {
slottable->st_list_signaled = B_TRUE;
(void) pthread_cond_signal(&slottable->
st_wait_cond);
}
(void) pthread_mutex_unlock(
&slottable->st_mutex);
pthread_exit(0);
}
}
}
(void) pthread_mutex_lock(&slottable->
st_slots[wfse->slotid]->sl_mutex);
/*
* If the provider told us that it does not support
* this function, we should mark it so we do not waste
* time later with it. If an error returned, we'll clean
* up this thread now and possibly try it again later.
*/
if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
slottable->st_slots[wfse->slotid]->sl_no_wfse = B_TRUE;
}
/*
* It is safe to unset active status now, since call to
* underlying provider has already terminated, and we
* hold the slottable wide mutex (st_mutex).
*/
slottable->st_slots[wfse->slotid]->sl_wfse_state = WFSE_CLEAR;
(void) pthread_mutex_unlock(&slottable->
st_slots[wfse->slotid]->sl_mutex);
if (slottable->st_blocking) {
slottable->st_list_signaled = B_TRUE;
(void) pthread_cond_signal(&slottable->st_wait_cond);
}
(void) pthread_mutex_unlock(&slottable->st_mutex);
/* Manually exit the thread, since nobody will join to it */
pthread_exit(0);
/*NOTREACHED*/
return (NULL);
}
/*
* 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 (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
*/
#include <dlfcn.h>
#include <stdlib.h>
#include <pthread.h>
#include <strings.h>
#include <security/cryptoki.h>
#include "pkcs11Global.h"
#include "pkcs11Slot.h"
#include "metaGlobal.h"
pkcs11_slottable_t *slottable = NULL;
/*
* pkcs11_slottable_initialize initizializes the global slottable.
* This slottable will contain information about the plugged in
* slots, including their mapped slotID. This function should only
* be called by C_Intialize.
*/
CK_RV
pkcs11_slottable_initialize() {
pkcs11_slottable_t *stmp = malloc(sizeof (pkcs11_slottable_t));
if (stmp == NULL)
return (CKR_HOST_MEMORY);
stmp->st_first = 1;
stmp->st_cur_size = 0;
stmp->st_last = 0;
stmp->st_slots = NULL;
if (pthread_mutex_init(&stmp->st_mutex, NULL) != 0) {
free(stmp);
return (CKR_FUNCTION_FAILED);
}
/* Set up for possible threads later */
stmp->st_event_slot = 0;
stmp->st_thr_count = 0;
stmp->st_wfse_active = B_FALSE;
stmp->st_blocking = B_FALSE;
stmp->st_list_signaled = B_FALSE;
(void) pthread_cond_init(&stmp->st_wait_cond, NULL);
(void) pthread_mutex_init(&stmp->st_start_mutex, NULL);
(void) pthread_cond_init(&stmp->st_start_cond, NULL);
slottable = stmp;
return (CKR_OK);
}
/*
* pkcs11_slottable_increase should only be called from C_Initialize().
* It is called after the first call to C_GetSlotList() and is used to
* increase the size of the slottable, as needed, to contain the next
* set of slots that C_Initialize() is currently mapping into the framework.
*/
CK_RV
pkcs11_slottable_increase(ulong_t increment) {
pkcs11_slot_t **tmpslots;
ulong_t newsize;
(void) pthread_mutex_lock(&slottable->st_mutex);
/* Add 1 to cover space for the metaslot */
newsize = slottable->st_last + increment + 1;
/* Check to see if we already have enough space */
if (slottable->st_cur_size >= newsize) {
(void) pthread_mutex_unlock(&slottable->st_mutex);
return (CKR_OK);
}
tmpslots = realloc
(slottable->st_slots, newsize * sizeof (pkcs11_slot_t *));
if (tmpslots == NULL) {
(void) pthread_mutex_unlock(&slottable->st_mutex);
return (CKR_HOST_MEMORY);
}
slottable->st_slots = tmpslots;
slottable->st_cur_size = newsize;
(void) pthread_mutex_unlock(&slottable->st_mutex);
return (CKR_OK);
}
/*
* pkcs11_slot_allocate should only be called from C_Initialize().
* We won't know if the metaslot will be used until after all of
* the other slots have been allocated.
*/
CK_RV
pkcs11_slot_allocate(CK_SLOT_ID *pslot_id) {
pkcs11_slot_t *tmpslot;
tmpslot = malloc(sizeof (pkcs11_slot_t));
if (tmpslot == NULL)
return (CKR_HOST_MEMORY);
bzero(tmpslot, sizeof (pkcs11_slot_t));
tmpslot->sl_wfse_state = WFSE_CLEAR;
tmpslot->sl_enabledpol = B_FALSE;
tmpslot->sl_no_wfse = B_FALSE;
/* Initialize this slot's mutex */
if (pthread_mutex_init(&tmpslot->sl_mutex, NULL) != 0) {
free(tmpslot);
return (CKR_FUNCTION_FAILED);
}
(void) pthread_mutex_lock(&slottable->st_mutex);
slottable->st_last++;
*pslot_id = slottable->st_last;
slottable->st_slots[*pslot_id] = tmpslot;
(void) pthread_mutex_unlock(&slottable->st_mutex);
return (CKR_OK);
}
/*
* pkcs11_slottable_delete should only be called by C_Finalize(),
* or by C_Initialize() in error conditions.
*/
CK_RV
pkcs11_slottable_delete() {
ulong_t i;
uint32_t prov_id;
int32_t last_prov_id = -1;
pkcs11_slot_t *cur_slot;
(void) pthread_mutex_lock(&slottable->st_mutex);
for (i = slottable->st_first; i <= slottable->st_last; i++) {
if (slottable->st_slots[i] != NULL) {
cur_slot = slottable->st_slots[i];
prov_id = cur_slot->sl_prov_id;
(void) pthread_mutex_lock(&cur_slot->sl_mutex);
/*
* For the first slot from this provider, do
* extra cleanup.
*/
if (prov_id != last_prov_id) {
if (cur_slot->sl_wfse_state == WFSE_ACTIVE) {
(void) pthread_cancel
(cur_slot->sl_tid);
}
/*
* Only call C_Finalize of plug-in if we
* get here from an explicit C_Finalize
* call from an application. Otherwise,
* there is a risk that the application may
* have directly dlopened this provider and
* we could interrupt their work. Plug-ins
* should have their own _fini function to
* clean up when they are no longer referenced.
*/
if ((cur_slot->sl_func_list != NULL) &&
(!fini_called)) {
(void) cur_slot->
sl_func_list->C_Finalize(NULL);
}
/* metaslot won't have a sl_dldesc! */
if (cur_slot->sl_dldesc != NULL) {
(void) dlclose(cur_slot->sl_dldesc);
}
/*
* Each provider maintains one disabled
* mechanism list for each of its slots to use.
*/
if (cur_slot->sl_pol_mechs != NULL)
free(cur_slot->sl_pol_mechs);
}
if (cur_slot->sl_wfse_args != NULL) {
free(cur_slot->sl_wfse_args);
}
(void) pthread_mutex_unlock(&cur_slot->sl_mutex);
/*
* Cleanup the session list. This must
* happen after the mutext is unlocked
* because session_delete tries to lock it
* again.
*/
pkcs11_sessionlist_delete(cur_slot);
(void) pthread_mutex_destroy(&cur_slot->sl_mutex);
free(cur_slot);
cur_slot = NULL;
last_prov_id = prov_id;
}
}
(void) pthread_cond_destroy(&slottable->st_wait_cond);
(void) pthread_mutex_destroy(&slottable->st_start_mutex);
(void) pthread_cond_destroy(&slottable->st_start_cond);
free(slottable->st_slots);
(void) pthread_mutex_unlock(&slottable->st_mutex);
(void) pthread_mutex_destroy(&slottable->st_mutex);
free(slottable);
slottable = NULL;
return (CKR_OK);
}
/*
* pkcs11_is_valid_slot verifies that the slot ID passed to the
* framework is valid.
*/
CK_RV
pkcs11_is_valid_slot(CK_SLOT_ID slot_id) {
if ((slot_id < slottable->st_first) ||
(slot_id > slottable->st_last)) {
return (CKR_SLOT_ID_INVALID);
} else if (slottable->st_slots[slot_id] != NULL) {
return (CKR_OK);
} else {
return (CKR_SLOT_ID_INVALID);
}
}
/*
* pkcs11_validate_and_convert_slotid verifies whether the slot ID
* passed to the framework is valid, and convert it to the
* true slot ID maintained in the framework data structures
* accordingly.
*
* This is necessary because when metaslot is enabled, the slot
* providing persistent object storage is "hidden".
*
* The real ID is returned in the "real_slot_id" argument regardless conversion
* is done or not.
*/
CK_RV
pkcs11_validate_and_convert_slotid(CK_SLOT_ID slot_id,
CK_SLOT_ID *real_slot_id) {
if (!metaslot_enabled) {
*real_slot_id = slot_id;
} else {
/* need to do conversion */
if (slot_id >= metaslot_keystore_slotid) {
*real_slot_id = slot_id + 1;
} else {
*real_slot_id = slot_id;
}
}
return (pkcs11_is_valid_slot(*real_slot_id));
}
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (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 2003 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <security/cryptoki.h>
#include "pkcs11Global.h"
#include "pkcs11Conf.h"
#include "pkcs11Session.h"
#include "pkcs11Slot.h"
/*
* C_VerifyInit will verify that the session handle is valid within the
* framework, that the mechanism is not disabled for the slot
* associated with this session, and then redirect to the underlying
* provider. Policy is only checked for C_VerifyInit, since it is
* required to be called before C_Verify and C_VerifyUpdate.
*/
CK_RV
C_VerifyInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism,
CK_OBJECT_HANDLE hKey)
{
CK_RV rv;
pkcs11_session_t *sessp;
CK_SLOT_ID slotid;
/* Check for a fastpath */
if (purefastpath || policyfastpath) {
if (policyfastpath &&
pkcs11_is_dismech(fast_slot, pMechanism->mechanism)) {
return (CKR_MECHANISM_INVALID);
}
return (fast_funcs->C_VerifyInit(hSession, pMechanism, hKey));
}
if (!pkcs11_initialized) {
return (CKR_CRYPTOKI_NOT_INITIALIZED);
}
/* Obtain the session pointer */
HANDLE2SESSION(hSession, sessp, rv);
if (rv != CKR_OK) {
return (rv);
}
slotid = sessp->se_slotid;
/* Make sure this is not a disabled mechanism */
if (pkcs11_is_dismech(slotid, pMechanism->mechanism)) {
return (CKR_MECHANISM_INVALID);
}
/* Initialize the digest with the underlying provider */
rv = FUNCLIST(slotid)->C_VerifyInit(sessp->se_handle,
pMechanism, hKey);
/* Present consistent interface to the application */
if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
return (CKR_FUNCTION_FAILED);
}
return (rv);
}
/*
* C_Verify is a pure wrapper to the underlying provider.
* The only argument checked is whether or not hSession is valid.
*/
CK_RV
C_Verify(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pData, CK_ULONG ulDataLen,
CK_BYTE_PTR pSignature, CK_ULONG ulSignatureLen)
{
CK_RV rv;
pkcs11_session_t *sessp;
/* Check for a fastpath */
if (purefastpath || policyfastpath) {
return (fast_funcs->C_Verify(hSession, pData, ulDataLen,
pSignature, ulSignatureLen));
}
if (!pkcs11_initialized) {
return (CKR_CRYPTOKI_NOT_INITIALIZED);
}
/* Obtain the session pointer */
HANDLE2SESSION(hSession, sessp, rv);
if (rv != CKR_OK) {
return (rv);
}
/* Pass data to the provider */
rv = FUNCLIST(sessp->se_slotid)->C_Verify(sessp->se_handle, pData,
ulDataLen, pSignature, ulSignatureLen);
/* Present consistent interface to the application */
if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
return (CKR_FUNCTION_FAILED);
}
return (rv);
}
/*
* C_VerifyUpdate is a pure wrapper to the underlying provider.
* The only argument checked is whether or not hSession is valid.
*/
CK_RV
C_VerifyUpdate(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pPart,
CK_ULONG ulPartLen)
{
CK_RV rv;
pkcs11_session_t *sessp;
/* Check for a fastpath */
if (purefastpath || policyfastpath) {
return (fast_funcs->C_VerifyUpdate(hSession, pPart,
ulPartLen));
}
if (!pkcs11_initialized) {
return (CKR_CRYPTOKI_NOT_INITIALIZED);
}
/* Obtain the session pointer */
HANDLE2SESSION(hSession, sessp, rv);
if (rv != CKR_OK) {
return (rv);
}
/* Pass data to the provider */
rv = FUNCLIST(sessp->se_slotid)->C_VerifyUpdate(sessp->se_handle,
pPart, ulPartLen);
/* Present consistent interface to the application */
if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
return (CKR_FUNCTION_FAILED);
}
return (rv);
}
/*
* C_VerifyFinal is a pure wrapper to the underlying provider.
* The only argument checked is whether or not hSession is valid.
*/
CK_RV
C_VerifyFinal(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pSignature,
CK_ULONG ulSignatureLen)
{
CK_RV rv;
pkcs11_session_t *sessp;
/* Check for a fastpath */
if (purefastpath || policyfastpath) {
return (fast_funcs->C_VerifyFinal(hSession, pSignature,
ulSignatureLen));
}
if (!pkcs11_initialized) {
return (CKR_CRYPTOKI_NOT_INITIALIZED);
}
/* Obtain the session pointer */
HANDLE2SESSION(hSession, sessp, rv);
if (rv != CKR_OK) {
return (rv);
}
/* Pass data to the provider */
rv = FUNCLIST(sessp->se_slotid)->C_VerifyFinal(sessp->se_handle,
pSignature, ulSignatureLen);
/* Present consistent interface to the application */
if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
return (CKR_FUNCTION_FAILED);
}
return (rv);
}
/*
* C_VerifyRecoverInit will verify that the session handle is valid within
* the framework, that the mechanism is not disabled for the slot
* associated with this session, and then redirect to the underlying
* provider. Policy is only checked for C_VerifyRecoverInit, since it is
* required to be called before C_VerifyRecover.
*/
CK_RV
C_VerifyRecoverInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism,
CK_OBJECT_HANDLE hKey)
{
CK_RV rv;
pkcs11_session_t *sessp;
CK_SLOT_ID slotid;
/* Check for a fastpath */
if (purefastpath || policyfastpath) {
if (policyfastpath &&
pkcs11_is_dismech(fast_slot, pMechanism->mechanism)) {
return (CKR_MECHANISM_INVALID);
}
return (fast_funcs->C_VerifyRecoverInit(hSession, pMechanism,
hKey));
}
if (!pkcs11_initialized) {
return (CKR_CRYPTOKI_NOT_INITIALIZED);
}
/* Obtain the session pointer */
HANDLE2SESSION(hSession, sessp, rv);
if (rv != CKR_OK) {
return (rv);
}
slotid = sessp->se_slotid;
/* Make sure this is not a disabled mechanism */
if (pkcs11_is_dismech(slotid, pMechanism->mechanism)) {
return (CKR_MECHANISM_INVALID);
}
/* Initialize the digest with the underlying provider */
rv = FUNCLIST(slotid)->C_VerifyRecoverInit(sessp->se_handle,
pMechanism, hKey);
/* Present consistent interface to the application */
if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
return (CKR_FUNCTION_FAILED);
}
return (rv);
}
/*
* C_VerifyRecover is a pure wrapper to the underlying provider.
* The only argument checked is whether or not hSession is valid.
*/
CK_RV
C_VerifyRecover(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pSignature,
CK_ULONG ulSignatureLen, CK_BYTE_PTR pData, CK_ULONG_PTR pulDataLen)
{
CK_RV rv;
pkcs11_session_t *sessp;
/* Check for a fastpath */
if (purefastpath || policyfastpath) {
return (fast_funcs->C_VerifyRecover(hSession, pSignature,
ulSignatureLen, pData, pulDataLen));
}
if (!pkcs11_initialized) {
return (CKR_CRYPTOKI_NOT_INITIALIZED);
}
/* Obtain the session pointer */
HANDLE2SESSION(hSession, sessp, rv);
if (rv != CKR_OK) {
return (rv);
}
/* Pass data to the provider */
rv = FUNCLIST(sessp->se_slotid)->C_VerifyRecover(sessp->se_handle,
pSignature, ulSignatureLen, pData, pulDataLen);
/* Present consistent interface to the application */
if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
return (CKR_FUNCTION_FAILED);
}
return (rv);
}
|