summaryrefslogtreecommitdiffstats
path: root/src/tpm12/tpm_process.c
blob: 02c2f49c7a4b266869bfb7f2085e0373791263cb (plain)
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
/********************************************************************************/
/*										*/
/*			     TPM Command Processor				*/
/*			     Written by Ken Goldman				*/
/*		       IBM Thomas J. Watson Research Center			*/
/*	      $Id: tpm_process.c 4621 2011-09-09 20:19:42Z kgoldman $		*/
/*										*/
/* (c) Copyright IBM Corporation 2006, 2010.					*/
/*										*/
/* All rights reserved.								*/
/* 										*/
/* Redistribution and use in source and binary forms, with or without		*/
/* modification, are permitted provided that the following conditions are	*/
/* met:										*/
/* 										*/
/* Redistributions of source code must retain the above copyright notice,	*/
/* this list of conditions and the following disclaimer.			*/
/* 										*/
/* Redistributions in binary form must reproduce the above copyright		*/
/* notice, this list of conditions and the following disclaimer in the		*/
/* documentation and/or other materials provided with the distribution.		*/
/* 										*/
/* Neither the names of the IBM Corporation nor the names of its		*/
/* contributors may be used to endorse or promote products derived from		*/
/* this software without specific prior written permission.			*/
/* 										*/
/* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS		*/
/* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT		*/
/* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR	*/
/* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT		*/
/* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,	*/
/* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT		*/
/* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,	*/
/* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY	*/
/* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT		*/
/* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE	*/
/* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.		*/
/********************************************************************************/

#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#ifdef TPM_POSIX
#include <sys/types.h>
#include <unistd.h>
#endif

#include "tpm_admin.h"
#include "tpm_audit.h"
#include "tpm_auth.h"
#include "tpm_constants.h"
#include "tpm_commands.h"
#include "tpm_counter.h"
#include "tpm_cryptoh.h"
#include "tpm_crypto.h"
#include "tpm_daa.h"
#include "tpm_debug.h"
#include "tpm_delegate.h"
#include "tpm_error.h"
#include "tpm_identity.h"
#include "tpm_init.h"
#include "tpm_io.h"
#include "tpm_key.h"
#include "tpm_maint.h"
#include "tpm_memory.h"
#include "tpm_migration.h"
#include "tpm_nonce.h"
#include "tpm_nvram.h"
#include "tpm_owner.h"
#include "tpm_pcr.h"
#include "tpm_permanent.h"
#include "tpm_platform.h"
#include "tpm_session.h"
#include "tpm_sizedbuffer.h"
#include "tpm_startup.h"
#include "tpm_storage.h"
#include "tpm_ticks.h"
#include "tpm_transport.h"
#include "tpm_ver.h"

#include "tpm_process.h"

/* local prototypes */

/* get capabilities */

static TPM_RESULT TPM_GetCapability_CapOrd(TPM_STORE_BUFFER *capabilityResponse,
					   uint32_t ordinal);
static TPM_RESULT TPM_GetCapability_CapAlg(TPM_STORE_BUFFER *capabilityResponse,
					   uint32_t algorithmID);
static TPM_RESULT TPM_GetCapability_CapPid(TPM_STORE_BUFFER *capabilityResponse,
					   uint16_t protocolID);
static TPM_RESULT TPM_GetCapability_CapFlag(TPM_STORE_BUFFER *capabilityResponse,
					    tpm_state_t *tpm_state,
					    uint32_t capFlag);
static TPM_RESULT TPM_GetCapability_CapProperty(TPM_STORE_BUFFER *capabilityResponse,
						tpm_state_t *tpm_state,
						uint32_t capProperty);
static TPM_RESULT TPM_GetCapability_CapVersion(TPM_STORE_BUFFER *capabilityResponse);
static TPM_RESULT TPM_GetCapability_CapCheckLoaded(TPM_STORE_BUFFER *capabilityResponse,
						   const TPM_KEY_HANDLE_ENTRY *tpm_key_handle_entry,
						   TPM_SIZED_BUFFER *subCap);
static TPM_RESULT TPM_GetCapability_CapSymMode(TPM_STORE_BUFFER *capabilityResponse,
					       TPM_SYM_MODE symMode);
static TPM_RESULT TPM_GetCapability_CapKeyStatus(TPM_STORE_BUFFER *capabilityResponse,
						 TPM_KEY_HANDLE_ENTRY *tpm_key_handle_entries,
						 uint32_t tpm_key_handle);
static TPM_RESULT TPM_GetCapability_CapMfr(TPM_STORE_BUFFER *capabilityResponse,
					   tpm_state_t *tpm_state,
					   TPM_SIZED_BUFFER *subCap);
static TPM_RESULT TPM_GetCapability_CapNVIndex(TPM_STORE_BUFFER *capabilityResponse,
					       tpm_state_t *tpm_state,
					       uint32_t nvIndex);
static TPM_RESULT TPM_GetCapability_CapTransAlg(TPM_STORE_BUFFER *capabilityResponse,
						TPM_ALGORITHM_ID algorithmID);
static TPM_RESULT TPM_GetCapability_CapHandle(TPM_STORE_BUFFER *capabilityResponse,
					      tpm_state_t *tpm_state,
					      TPM_RESOURCE_TYPE resourceType);
static TPM_RESULT TPM_GetCapability_CapTransEs(TPM_STORE_BUFFER *capabilityResponse,
					       TPM_ENC_SCHEME encScheme);
static TPM_RESULT TPM_GetCapability_CapAuthEncrypt(TPM_STORE_BUFFER *capabilityResponse,
						   uint32_t algorithmID);
static TPM_RESULT TPM_GetCapability_CapSelectSize(TPM_STORE_BUFFER *capabilityResponse,
						  TPM_SIZED_BUFFER *subCap);
#if  (TPM_REVISION >= 103)	/* added for rev 103 */
static TPM_RESULT TPM_GetCapability_CapDaLogic(TPM_STORE_BUFFER *capabilityResponse,
					       TPM_SIZED_BUFFER *subCap,
					       tpm_state_t *tpm_state);
#endif
static TPM_RESULT TPM_GetCapability_CapVersionVal(TPM_STORE_BUFFER *capabilityResponse,
						  TPM_PERMANENT_DATA *tpm_permanent_data);

static TPM_RESULT TPM_GetCapability_CapPropTisTimeout(TPM_STORE_BUFFER *capabilityResponse);
static TPM_RESULT TPM_GetCapability_CapPropDuration(TPM_STORE_BUFFER *capabilityResponse);

/* set capabilities */

static TPM_RESULT TPM_SetCapability_CapPermFlags(tpm_state_t *tpm_state,
						 TPM_BOOL ownerAuthorized,
						 TPM_BOOL presenceAuthorized,
						 uint32_t subCap32,
						 TPM_BOOL valueBool);
static TPM_RESULT TPM_SetCapability_CapPermData(tpm_state_t *tpm_state,
						TPM_BOOL ownerAuthorized,
						TPM_BOOL presenceAuthorized,
						uint32_t subCap32,
						uint32_t valueUint32);
static TPM_RESULT TPM_SetCapability_CapStclearFlags(tpm_state_t *tpm_state,
						    TPM_BOOL ownerAuthorized,
						    TPM_BOOL presenceAuthorized,
						    uint32_t subCap32,
						    TPM_BOOL valueBool);
static TPM_RESULT TPM_SetCapability_CapStclearData(tpm_state_t *tpm_state,
						   TPM_BOOL ownerAuthorized,
						   TPM_BOOL presenceAuthorized,
						   uint32_t subCap32,
						   uint32_t valueUint32);
static TPM_RESULT TPM_SetCapability_CapStanyFlags(tpm_state_t *tpm_state,
						  TPM_BOOL ownerAuthorized,
						  TPM_BOOL presenceAuthorized,
						  uint32_t subCap32,
						  TPM_BOOL valueBool);
static TPM_RESULT TPM_SetCapability_CapStanyData(tpm_state_t *tpm_state,
						 TPM_BOOL ownerAuthorized,
						 TPM_BOOL presenceAuthorized,
						 uint32_t subCap32,
						 TPM_SIZED_BUFFER *setValue);
static TPM_RESULT TPM_SetCapability_CapVendor(tpm_state_t *tpm_state,
					      TPM_BOOL ownerAuthorized,
					      TPM_BOOL presenceAuthorized,
					      uint32_t subCap32,
					      TPM_SIZED_BUFFER *setValue);

/*
  TPM_CAP_VERSION_INFO
*/

/* TPM_CapVersionInfo_Init()

   sets members to default values
   sets all pointers to NULL and sizes to 0
   always succeeds - no return code
*/

void TPM_CapVersionInfo_Init(TPM_CAP_VERSION_INFO *tpm_cap_version_info)
{
    printf(" TPM_CapVersionInfo_Init:\n");
    TPM_Version_Init(&(tpm_cap_version_info->version));
    tpm_cap_version_info->specLevel = TPM_SPEC_LEVEL;
    tpm_cap_version_info->errataRev = TPM_ERRATA_REV;
    memcpy(&(tpm_cap_version_info->tpmVendorID), TPM_VENDOR_ID,
	   sizeof(tpm_cap_version_info->tpmVendorID));
    tpm_cap_version_info->vendorSpecificSize = 0;
    tpm_cap_version_info->vendorSpecific = NULL;
    return;
}

/* TPM_CapVersionInfo_Set() sets members to software specific data */

void TPM_CapVersionInfo_Set(TPM_CAP_VERSION_INFO *tpm_cap_version_info,
			    TPM_PERMANENT_DATA *tpm_permanent_data)
{
    printf(" TPM_CapVersionInfo_Set:\n");
    TPM_Version_Set(&(tpm_cap_version_info->version), tpm_permanent_data);
    tpm_cap_version_info->specLevel = TPM_SPEC_LEVEL;
    tpm_cap_version_info->errataRev = TPM_ERRATA_REV;
    memcpy(&(tpm_cap_version_info->tpmVendorID), TPM_VENDOR_ID,
	   sizeof(tpm_cap_version_info->tpmVendorID));
    tpm_cap_version_info->vendorSpecificSize = 0;
    tpm_cap_version_info->vendorSpecific = NULL;
    return;
}

#if 0	/* not required */
/* TPM_CapVersionInfo_Load()

   deserialize the structure from a 'stream'
   'stream_size' is checked for sufficient data
   returns 0 or error codes
   
   Before use, call TPM_CapVersionInfo_Init() or TPM_CapVersionInfo_Set()
   After use, call TPM_CapVersionInfo_Delete() to free memory
*/

TPM_RESULT TPM_CapVersionInfo_Load(TPM_CAP_VERSION_INFO *tpm_cap_version_info,
				   unsigned char **stream,
				   uint32_t *stream_size)
{
    TPM_RESULT		rc = 0;

    printf(" TPM_CapVersionInfo_Load:\n");
    /* check tag */
    if (rc == 0) {
	rc = TPM_CheckTag(TPM_TAG_CAP_VERSION_INFO, stream, stream_size);
    }
    /* load version */
    if (rc == 0) {
	rc = TPM_Version_Load(&(tpm_cap_version_info->version), stream, stream_size);
    }
    /* load specLevel */
    if (rc == 0) {
	rc = TPM_Load16(&(tpm_cap_version_info->specLevel), stream, stream_size);
    }
    /* load errataRev */
    if (rc == 0) {
	rc = TPM_Loadn(&(tpm_cap_version_info->errataRev), sizeof(tpm_cap_version_info->errataRev),
		       stream, stream_size);
    }
    /* load tpmVendorID */
    if (rc == 0) {
	rc = TPM_Loadn(tpm_cap_version_info->tpmVendorID, sizeof(tpm_cap_version_info->tpmVendorID),
		       stream, stream_size);
    }
    /* load vendorSpecificSize */
    if (rc == 0) {
	rc = TPM_Load16(&(tpm_cap_version_info->vendorSpecificSize), stream, stream_size);
    }
    /* allocate memory for vendorSpecific */
    if ((rc == 0) && (tpm_cap_version_info->vendorSpecificSize > 0)) {
	rc = TPM_Malloc(&(tpm_cap_version_info->vendorSpecific),
			tpm_cap_version_info->vendorSpecificSize);
    }
    /* load vendorSpecific */
    if ((rc == 0) && (tpm_cap_version_info->vendorSpecificSize > 0)) {
	rc = TPM_Loadn(tpm_cap_version_info->vendorSpecific,
		       tpm_cap_version_info->vendorSpecificSize,
		       stream, stream_size);
    }
    return rc;
}
#endif

/* TPM_CapVersionInfo_Store()
   
   serialize the structure to a stream contained in 'sbuffer'
   returns 0 or error codes
*/

TPM_RESULT TPM_CapVersionInfo_Store(TPM_STORE_BUFFER *sbuffer,
				    const TPM_CAP_VERSION_INFO *tpm_cap_version_info)
{
    TPM_RESULT		rc = 0;

    printf(" TPM_CapVersionInfo_Store:\n");
    /* store tag */
    if (rc == 0) {
	rc = TPM_Sbuffer_Append16(sbuffer, TPM_TAG_CAP_VERSION_INFO); 
    }
    /* store version */
    if (rc == 0) {
	rc = TPM_Version_Store(sbuffer, &(tpm_cap_version_info->version));
    }
    /* store specLevel */
    if (rc == 0) {
	rc = TPM_Sbuffer_Append16(sbuffer, tpm_cap_version_info->specLevel);
    }
    /* store errataRev */
    if (rc == 0) {
	rc = TPM_Sbuffer_Append(sbuffer, &(tpm_cap_version_info->errataRev),
				sizeof(tpm_cap_version_info->errataRev));
    }
    /* store tpmVendorID */
    if (rc == 0) {
	rc = TPM_Sbuffer_Append(sbuffer, tpm_cap_version_info->tpmVendorID,
				sizeof(tpm_cap_version_info->tpmVendorID));
    }
    /* store vendorSpecificSize */
    if (rc == 0) {
	rc = TPM_Sbuffer_Append16(sbuffer, tpm_cap_version_info->vendorSpecificSize);
    }
    /* store vendorSpecific */
    if (rc == 0) {
	rc = TPM_Sbuffer_Append(sbuffer,
				tpm_cap_version_info->vendorSpecific,
				tpm_cap_version_info->vendorSpecificSize);
    }
    return rc;
}

/* TPM_CapVersionInfo_Delete()

   No-OP if the parameter is NULL, else:
   frees memory allocated for the object
   sets pointers to NULL
   calls TPM_CapVersionInfo_Init to set members back to default values
   The object itself is not freed
*/   

void TPM_CapVersionInfo_Delete(TPM_CAP_VERSION_INFO *tpm_cap_version_info)
{
    printf(" TPM_CapVersionInfo_Delete:\n");
    if (tpm_cap_version_info != NULL) {
	free(tpm_cap_version_info->vendorSpecific);
	TPM_CapVersionInfo_Init(tpm_cap_version_info);
    }
    return;
}

/*
  Processing Commands
*/


/* 17. Ordinals rev 107

   This structure maps the specification Ordinals table to software functions and parameters.

   It provides direct mapping that easier to understand and maintain than scattering and hard coding
   these values.

   The functions currently supported are:

	- processing jump table for 1.1 and 1.2 (implied get capability - ordinals supported)
	- allow audit
	- audit default value
	- owner delegation permissions
	- key delegation permissions
	- wrappable

   Future possibilities include:

	- no owner, disabled, deactivated
	- 0,1,2 auth

   typedef struct tdTPM_ORDINAL_TABLE {

   TPM_COMMAND_CODE ordinal;
   tpm_process_function_t process_function_v11;
   tpm_process_function_t process_function_v12;
   TPM_BOOL auditable;				       
   TPM_BOOL auditDefault;			       
   uint16_t ownerPermissionBlock;			
   uint32_t ownerPermissionPosition;			
   uint16_t keyPermissionBlock;			
   uint32_t keyPermissionPosition;
   uint32_t inputHandleSize;
   uint32_t keyHandles;
   uint32_t outputHandleSize;
   TPM_BOOL transportWrappable;
   TPM_BOOL instanceWrappable;				
   TPM_BOOL hardwareWrappable;
   } TPM_ORDINAL_TABLE;
*/

static TPM_ORDINAL_TABLE tpm_ordinal_table[] =
{
    {TPM_ORD_ActivateIdentity,
     TPM_Process_ActivateIdentity, TPM_Process_ActivateIdentity,
     TRUE,
     TRUE,
     1, TPM_DELEGATE_ActivateIdentity,
     1, TPM_KEY_DELEGATE_ActivateIdentity,
     sizeof(TPM_KEY_HANDLE),
     1,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_AuthorizeMigrationKey,
     TPM_Process_AuthorizeMigrationKey, TPM_Process_AuthorizeMigrationKey,
     TRUE,
     TRUE,
     1, TPM_DELEGATE_AuthorizeMigrationKey,
     0, 0,
     0,
     0,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_CertifyKey,
     TPM_Process_CertifyKey, TPM_Process_CertifyKey,
     TRUE,
     FALSE,
     0, 0,
     1, TPM_KEY_DELEGATE_CertifyKey,
     sizeof(TPM_KEY_HANDLE) + sizeof(TPM_KEY_HANDLE),
     2,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_CertifyKey2,
     TPM_Process_Unused, TPM_Process_CertifyKey2,
     TRUE,
     FALSE,
     0, 0,
     1, TPM_KEY_DELEGATE_CertifyKey2,
     sizeof(TPM_KEY_HANDLE) + sizeof(TPM_KEY_HANDLE),
     2,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_CertifySelfTest,
     TPM_Process_CertifySelfTest, TPM_Process_Unused,
     TRUE,
     FALSE,
     0, 0,
     0, 0,
     sizeof(TPM_KEY_HANDLE),
     1,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_ChangeAuth,
     TPM_Process_ChangeAuth, TPM_Process_ChangeAuth,
     TRUE,
     FALSE,
     0, 0,
     1, TPM_KEY_DELEGATE_ChangeAuth,
     sizeof(TPM_KEY_HANDLE),
     1,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_ChangeAuthAsymFinish,
     TPM_Process_ChangeAuthAsymFinish, TPM_Process_ChangeAuthAsymFinish,
     TRUE,
     FALSE,
     0, 0,
     1, TPM_KEY_DELEGATE_ChangeAuthAsymFinish,
     sizeof(TPM_KEY_HANDLE),
     1,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_ChangeAuthAsymStart,
     TPM_Process_ChangeAuthAsymStart, TPM_Process_ChangeAuthAsymStart,
     TRUE,
     FALSE,
     0, 0,
     1, TPM_KEY_DELEGATE_ChangeAuthAsymStart,
     sizeof(TPM_KEY_HANDLE),
     1,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_ChangeAuthOwner,
     TPM_Process_ChangeAuthOwner, TPM_Process_ChangeAuthOwner,
     TRUE,
     TRUE,
     0, 0,
     0, 0,
     0,
     0,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_CMK_ApproveMA,
     TPM_Process_Unused, TPM_Process_CMK_ApproveMA,
     TRUE,
     FALSE,
     1, TPM_DELEGATE_CMK_ApproveMA,
     0, 0,
     0,
     0,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_CMK_ConvertMigration,
     TPM_Process_Unused, TPM_Process_CMK_ConvertMigration,
     TRUE,
     FALSE,
     1, TPM_KEY_DELEGATE_CMK_ConvertMigration,
     0, 0,
     sizeof(TPM_KEY_HANDLE),
     1,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_CMK_CreateBlob,
     TPM_Process_Unused, TPM_Process_CMK_CreateBlob,
     TRUE,
     FALSE,
     0, 0,
     1, TPM_KEY_DELEGATE_CMK_CreateBlob,
     sizeof(TPM_KEY_HANDLE),
     1,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_CMK_CreateKey,
     TPM_Process_Unused, TPM_Process_CMK_CreateKey,
     TRUE,
     FALSE,
     0, 0,
     1, TPM_KEY_DELEGATE_CMK_CreateKey,
     sizeof(TPM_KEY_HANDLE),
     1,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_CMK_CreateTicket,
     TPM_Process_Unused, TPM_Process_CMK_CreateTicket,
     TRUE,
     FALSE,
     1, TPM_DELEGATE_CMK_CreateTicket,
     0, 0,
     0,
     0,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_CMK_SetRestrictions,
     TPM_Process_Unused, TPM_Process_CMK_SetRestrictions,
     TRUE,
     FALSE,
     0, 0,
     0, 0,
     0,
     0,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_ContinueSelfTest,
     TPM_Process_ContinueSelfTest, TPM_Process_ContinueSelfTest,
     TRUE,
     FALSE,
     0, 0,
     0, 0,
     0,
     0,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_ConvertMigrationBlob,
     TPM_Process_ConvertMigrationBlob, TPM_Process_ConvertMigrationBlob,
     TRUE,
     TRUE,
     0, 0,
     1, TPM_KEY_DELEGATE_ConvertMigrationBlob,
     sizeof(TPM_KEY_HANDLE),
     1,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_CreateCounter,
     TPM_Process_Unused, TPM_Process_CreateCounter,
     TRUE,
     FALSE,
     1, TPM_DELEGATE_CreateCounter,
     0, 0,
     0,
     0,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_CreateEndorsementKeyPair,
     TPM_Process_CreateEndorsementKeyPair, TPM_Process_CreateEndorsementKeyPair,
     TRUE,
     FALSE,
     0, 0,
     0, 0,
     0,
     0,
     0,
     TRUE,
     TRUE,
     FALSE},
    
    {TPM_ORD_CreateMaintenanceArchive,
#if defined(TPM_NOMAINTENANCE) || defined(TPM_NOMAINTENANCE_COMMANDS)
     TPM_Process_Unused, TPM_Process_Unused,
     FALSE,
     FALSE,
#else
     TPM_Process_CreateMaintenanceArchive, TPM_Process_CreateMaintenanceArchive,
     TRUE,
     TRUE,
#endif
     1, TPM_DELEGATE_CreateMaintenanceArchive,
     0, 0,
     0,
     0,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_CreateMigrationBlob,
     TPM_Process_CreateMigrationBlob, TPM_Process_CreateMigrationBlob,
     TRUE,
     TRUE,
     0, 0,
     1, TPM_KEY_DELEGATE_CreateMigrationBlob,
     sizeof(TPM_KEY_HANDLE),
     1,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_CreateRevocableEK,
     TPM_Process_Unused, TPM_Process_CreateRevocableEK,
     TRUE,
     FALSE,
     0, 0,
     0, 0,
     0,
     0,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_CreateWrapKey,
     TPM_Process_CreateWrapKey, TPM_Process_CreateWrapKey,
     TRUE,
     TRUE,
     0, 0,
     1, TPM_KEY_DELEGATE_CreateWrapKey,
     sizeof(TPM_KEY_HANDLE),
     1,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_DAA_Join,
     TPM_Process_Unused, TPM_Process_DAAJoin,
     TRUE,
     FALSE,
     1, TPM_DELEGATE_DAA_Join,
     0, 0,
     sizeof(TPM_HANDLE),
     0,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_DAA_Sign,
     TPM_Process_Unused, TPM_Process_DAASign,
     TRUE,
     FALSE,
     1, TPM_DELEGATE_DAA_Sign,
     0, 0,
     sizeof(TPM_HANDLE),
     0,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_Delegate_CreateKeyDelegation,
     TPM_Process_Unused, TPM_Process_DelegateCreateKeyDelegation,
     TRUE,
     FALSE,
     0, 0,
     1, TPM_KEY_DELEGATE_Delegate_CreateKeyDelegation,
     sizeof(TPM_KEY_HANDLE),
     1,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_Delegate_CreateOwnerDelegation,
     TPM_Process_Unused, TPM_Process_DelegateCreateOwnerDelegation,
     TRUE,
     FALSE,
     1, TPM_DELEGATE_Delegate_CreateOwnerDelegation,
     0, 0,
     0,
     0,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_Delegate_LoadOwnerDelegation,
     TPM_Process_Unused, TPM_Process_DelegateLoadOwnerDelegation,
     TRUE,
     FALSE,
     1, TPM_DELEGATE_Delegate_LoadOwnerDelegation,
     0, 0,
     0,
     0,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_Delegate_Manage,
     TPM_Process_Unused, TPM_Process_DelegateManage,
     TRUE,
     FALSE,
     1, TPM_DELEGATE_Delegate_Manage,
     0, 0,
     0,
     0,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_Delegate_ReadTable,
     TPM_Process_Unused, TPM_Process_DelegateReadTable,
     TRUE,
     FALSE,
     0, 0,
     0, 0,
     0,
     0,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_Delegate_UpdateVerification,
     TPM_Process_Unused, TPM_Process_DelegateUpdateVerification,
     TRUE,
     FALSE,
     1, TPM_DELEGATE_Delegate_UpdateVerification,
     0, 0,
     0,
     0,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_Delegate_VerifyDelegation,
     TPM_Process_Unused, TPM_Process_DelegateVerifyDelegation,
     TRUE,
     FALSE,
     0, 0,
     0, 0,
     0,
     0,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_DirRead,
     TPM_Process_DirRead, TPM_Process_DirRead,
     TRUE,
     FALSE,
     0, 0,
     0, 0,
     0,
     0,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_DirWriteAuth,
     TPM_Process_DirWriteAuth, TPM_Process_DirWriteAuth,
     TRUE,
     FALSE,
     1, TPM_DELEGATE_DirWriteAuth,
     0, 0,
     0,
     0,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_DisableForceClear,
     TPM_Process_DisableForceClear, TPM_Process_DisableForceClear,
     TRUE,
     TRUE,
     0, 0,
     0, 0,
     0,
     0,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_DisableOwnerClear,
     TPM_Process_DisableOwnerClear, TPM_Process_DisableOwnerClear,
     TRUE,
     TRUE,
     1, TPM_DELEGATE_DisableOwnerClear,
     0, 0,
     0,
     0,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_DisablePubekRead,
     TPM_Process_DisablePubekRead, TPM_Process_DisablePubekRead,
     TRUE,
     TRUE,
     1, TPM_DELEGATE_DisablePubekRead,
     0, 0,
     0,
     0,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_DSAP,
     TPM_Process_Unused, TPM_Process_DSAP,
     TRUE,
     FALSE,
     0, 0,
     0, 0,
     sizeof(TPM_ENTITY_TYPE) + sizeof(TPM_KEY_HANDLE) + TPM_NONCE_SIZE + sizeof(uint32_t),
     0xffffffff,
     sizeof(TPM_AUTHHANDLE) + TPM_NONCE_SIZE + TPM_NONCE_SIZE,
     TRUE,
     TRUE,
     TRUE},
    
    {TPM_ORD_EstablishTransport,
     TPM_Process_Unused, TPM_Process_EstablishTransport,
     TRUE,
     FALSE,
     0, 0,
     1, TPM_KEY_DELEGATE_EstablishTransport,
     sizeof(TPM_KEY_HANDLE),
     1,
     0,
     FALSE,
     FALSE,
     FALSE},
    
    {TPM_ORD_EvictKey,
     TPM_Process_EvictKey, TPM_Process_EvictKey,
     TRUE,
     FALSE,
     0, 0,
     0, 0,
     sizeof(TPM_KEY_HANDLE),
     1,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_ExecuteTransport,
     TPM_Process_Unused, TPM_Process_ExecuteTransport,
     TRUE,
     FALSE,
     0, 0,
     0, 0,
     0,
     0,
     0,
     FALSE,
     FALSE,
     FALSE},
    
    {TPM_ORD_Extend,
     TPM_Process_Extend, TPM_Process_Extend,
     TRUE,
     FALSE,
     0, 0,
     0, 0,
     0,
     0,
     0,
     TRUE,
     TRUE,
     FALSE},
    
    {TPM_ORD_FieldUpgrade,
     TPM_Process_Unused, TPM_Process_Unused,
     TRUE,
     FALSE,
     1, TPM_DELEGATE_FieldUpgrade,
     0, 0,
     0,
     0,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_FlushSpecific,
     TPM_Process_Unused, TPM_Process_FlushSpecific,
     TRUE,
     FALSE,
     0, 0,
     0, 0,
     sizeof(TPM_HANDLE),
     0xffffffff,
     0,
     TRUE,
     TRUE,
     TRUE},
    
    {TPM_ORD_ForceClear,
     TPM_Process_ForceClear, TPM_Process_ForceClear,
     TRUE,
     TRUE,
     0, 0,
     0, 0,
     0,
     0,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_GetAuditDigest,
     TPM_Process_Unused, TPM_Process_GetAuditDigest,
     FALSE,
     FALSE,
     0, 0,
     0, 0,
     0,
     0,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_GetAuditDigestSigned,
     TPM_Process_Unused, TPM_Process_GetAuditDigestSigned,
     FALSE,
     FALSE,
     0, 0,
     1, TPM_KEY_DELEGATE_GetAuditDigestSigned,
     sizeof(TPM_KEY_HANDLE),
     1,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_GetAuditEvent,
     TPM_Process_Unused, TPM_Process_Unused,
     FALSE,
     FALSE,
     0, 0,
     0, 0,
     0,
     0,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_GetAuditEventSigned,
     TPM_Process_Unused, TPM_Process_Unused,
     FALSE,
     FALSE,
     0, 0,
     0, 0,
     0,
     0,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_GetCapability,
     TPM_Process_GetCapability, TPM_Process_GetCapability,
     TRUE,
     FALSE,
     0, 0,
     0, 0,
     0,
     0,
     0,
     TRUE,
     TRUE,
     FALSE},
    
    {TPM_ORD_GetCapabilityOwner,
     TPM_Process_GetCapabilityOwner, TPM_Process_GetCapabilityOwner,
     TRUE,
     FALSE,
     0, 0,
     0, 0,
     0,
     0,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_GetCapabilitySigned,
     TPM_Process_GetCapabilitySigned, TPM_Process_Unused,
     TRUE,
     FALSE,
     0, 0,
     0, 0,
     0,
     0,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_GetOrdinalAuditStatus,
     TPM_Process_Unused, TPM_Process_Unused,
     FALSE,
     FALSE,
     0, 0,
     0, 0,
     0,
     0,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_GetPubKey,
     TPM_Process_GetPubKey, TPM_Process_GetPubKey,
     TRUE,
     FALSE,
     0, 0,
     1, TPM_KEY_DELEGATE_GetPubKey,
     sizeof(TPM_KEY_HANDLE),
     1,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_GetRandom,
     TPM_Process_GetRandom, TPM_Process_GetRandom,
     TRUE,
     FALSE,
     0, 0,
     0, 0,
     0,
     0,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_GetTestResult,
     TPM_Process_GetTestResult, TPM_Process_GetTestResult,
     TRUE,
     FALSE,
     0, 0,
     0, 0,
     0,
     0,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_GetTicks,
     TPM_Process_Unused, TPM_Process_GetTicks,
     TRUE,
     FALSE,
     0, 0,
     0, 0,
     0,
     0,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_IncrementCounter,
     TPM_Process_Unused, TPM_Process_IncrementCounter,
     TRUE,
     FALSE,
     0, 0,
     0, 0,
     0,
     0,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_Init,
     TPM_Process_Init, TPM_Process_Init,
     TRUE,
     FALSE,
     0, 0,
     0, 0,
     0,
     0,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_KeyControlOwner,
     TPM_Process_Unused, TPM_Process_KeyControlOwner,
     TRUE,
     FALSE,
     1, TPM_DELEGATE_KeyControlOwner,
     0, 0,
     sizeof(TPM_KEY_HANDLE),
     1,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_KillMaintenanceFeature,
#if defined(TPM_NOMAINTENANCE) || defined(TPM_NOMAINTENANCE_COMMANDS)
     TPM_Process_Unused, TPM_Process_Unused,
     FALSE,
     FALSE,
#else
     TPM_Process_KillMaintenanceFeature, TPM_Process_KillMaintenanceFeature,
     TRUE,
     TRUE,
#endif
     1, TPM_DELEGATE_KillMaintenanceFeature,
     0, 0,
     0,
     0,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_LoadAuthContext,
     TPM_Process_LoadAuthContext, TPM_Process_LoadAuthContext,
     TRUE,
     FALSE,
     0, 0,
     0, 0,
     0,
     0,
     sizeof(TPM_HANDLE),
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_LoadContext,
     TPM_Process_Unused, TPM_Process_LoadContext,
     TRUE,
     FALSE,
     0, 0,
     0, 0,
     sizeof(TPM_HANDLE),
     0,
     sizeof(TPM_HANDLE),
     TRUE,
     TRUE,
     FALSE},
    
    {TPM_ORD_LoadKey,
     TPM_Process_LoadKey, TPM_Process_LoadKey,
     TRUE,
     FALSE,
     0, 0,
     1, TPM_KEY_DELEGATE_LoadKey,
     sizeof(TPM_KEY_HANDLE),
     1,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_LoadKey2,
     TPM_Process_Unused, TPM_Process_LoadKey2,
     TRUE,
     FALSE,
     0, 0,
     1, TPM_KEY_DELEGATE_LoadKey2,
     sizeof(TPM_KEY_HANDLE),
     1,
     sizeof(TPM_KEY_HANDLE),
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_LoadKeyContext,
     TPM_Process_LoadKeyContext, TPM_Process_LoadKeyContext,
     TRUE,
     FALSE,
     0, 0,
     0, 0,
     0,
     0,
     sizeof(TPM_KEY_HANDLE),
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_LoadMaintenanceArchive,
#if defined(TPM_NOMAINTENANCE) || defined(TPM_NOMAINTENANCE_COMMANDS)
     TPM_Process_Unused, TPM_Process_Unused,
     FALSE,
     FALSE,
#else
     TPM_Process_LoadMaintenanceArchive, TPM_Process_LoadMaintenanceArchive,
     TRUE,
     TRUE,
#endif
     1, TPM_DELEGATE_LoadMaintenanceArchive,
     0, 0,
     0,
     0,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_LoadManuMaintPub,
#if defined(TPM_NOMAINTENANCE) || defined(TPM_NOMAINTENANCE_COMMANDS)
     TPM_Process_Unused, TPM_Process_Unused,
     FALSE,
     FALSE,
#else
     TPM_Process_LoadManuMaintPub, TPM_Process_LoadManuMaintPub,
     TRUE,
     TRUE,
#endif
     0, 0,
     0, 0,
     0,
     0,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_MakeIdentity,
     TPM_Process_MakeIdentity, TPM_Process_MakeIdentity,
     TRUE,
     TRUE,
     1, TPM_DELEGATE_MakeIdentity,
     1, TPM_KEY_DELEGATE_MakeIdentity,
     0,
     0,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_MigrateKey,
     TPM_Process_Unused, TPM_Process_MigrateKey,
     TRUE,
     FALSE,
     0, 0,
     1, TPM_KEY_DELEGATE_MigrateKey,
     sizeof(TPM_KEY_HANDLE),
     1,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_NV_DefineSpace,
     TPM_Process_Unused, TPM_Process_NVDefineSpace,
     TRUE,
     FALSE,
     1, TPM_DELEGATE_NV_DefineSpace,
     0, 0,
     0,
     0,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_NV_ReadValue,
     TPM_Process_Unused, TPM_Process_NVReadValue,
     TRUE,
     FALSE,
     1, TPM_DELEGATE_NV_ReadValue,
     0, 0,
     0,
     0,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_NV_ReadValueAuth,
     TPM_Process_Unused, TPM_Process_NVReadValueAuth,
     TRUE,
     FALSE,
     0, 0,
     0, 0,
     0,
     0,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_NV_WriteValue,
     TPM_Process_Unused, TPM_Process_NVWriteValue,
     TRUE,
     FALSE,
     1, TPM_DELEGATE_NV_WriteValue,
     0, 0,
     0,
     0,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_NV_WriteValueAuth,
     TPM_Process_Unused, TPM_Process_NVWriteValueAuth,
     TRUE,
     FALSE,
     0, 0,
     0, 0,
     0,
     0,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_OIAP,
     TPM_Process_OIAP, TPM_Process_OIAP,
     TRUE,
     FALSE,
     0, 0,
     0, 0,
     0,
     0,
     sizeof(TPM_AUTHHANDLE) + TPM_NONCE_SIZE,
     TRUE,
     TRUE,
     TRUE},
    
    {TPM_ORD_OSAP,
     TPM_Process_OSAP, TPM_Process_OSAP,
     TRUE,
     FALSE,
     0, 0,
     0, 0,
     sizeof(TPM_ENTITY_TYPE) + sizeof(uint32_t) + TPM_NONCE_SIZE,
     0,		/* TPM_OSAP: no input or output parameters are encrypted or logged */
     sizeof(TPM_AUTHHANDLE) + TPM_NONCE_SIZE + TPM_NONCE_SIZE,
     TRUE,
     TRUE,
     TRUE},
    
    {TPM_ORD_OwnerClear,
     TPM_Process_OwnerClear, TPM_Process_OwnerClear,
     TRUE,
     TRUE,
     1, TPM_DELEGATE_OwnerClear,
     0, 0,
     0,
     0,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_OwnerReadInternalPub,
     TPM_Process_Unused, TPM_Process_OwnerReadInternalPub,
     TRUE,
     FALSE,
     1, TPM_DELEGATE_OwnerReadInternalPub,
     0, 0,
     0,
     1,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_OwnerReadPubek,
     TPM_Process_OwnerReadPubek, TPM_Process_OwnerReadPubek,
     TRUE,
     TRUE,
     1, TPM_DELEGATE_OwnerReadPubek,
     0, 0,
     0,
     0,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_OwnerSetDisable,
     TPM_Process_OwnerSetDisable, TPM_Process_OwnerSetDisable,
     TRUE,
     TRUE,
     1, TPM_DELEGATE_OwnerSetDisable,
     0, 0,
     0,
     0,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_PCR_Reset,
     TPM_Process_Unused, TPM_Process_PcrReset,
     TRUE,
     FALSE,
     0, 0,
     0, 0,
     0,
     0,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_PcrRead,
     TPM_Process_PcrRead, TPM_Process_PcrRead,
     TRUE,
     FALSE,
     0, 0,
     0, 0,
     0,
     0,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_PhysicalDisable,
     TPM_Process_PhysicalDisable, TPM_Process_PhysicalDisable,
     TRUE,
     TRUE,
     0, 0,
     0, 0,
     0,
     0,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_PhysicalEnable,
     TPM_Process_PhysicalEnable, TPM_Process_PhysicalEnable,
     TRUE,
     TRUE,
     0, 0,
     0, 0,
     0,
     0,
     0,
     TRUE,
     TRUE,
     FALSE},
    
    {TPM_ORD_PhysicalSetDeactivated,
     TPM_Process_PhysicalSetDeactivated, TPM_Process_PhysicalSetDeactivated,
     TRUE,
     TRUE,
     0, 0,
     0, 0,
     0,
     0,
     0,
     TRUE,
     TRUE,
     FALSE},
    
    {TPM_ORD_Quote,
     TPM_Process_Quote, TPM_Process_Quote,
     TRUE,
     FALSE,
     0, 0,
     1, TPM_KEY_DELEGATE_Quote,
     sizeof(TPM_KEY_HANDLE),
     1,
     0,
     TRUE,
     FALSE,
     TRUE},
    
    {TPM_ORD_Quote2,
     TPM_Process_Unused, TPM_Process_Quote2,
     TRUE,
     FALSE,
     0, 0,
     1, TPM_KEY_DELEGATE_Quote2,
     sizeof(TPM_KEY_HANDLE),
     1,
     0,
     TRUE,
     FALSE,
     TRUE},
    
    {TPM_ORD_ReadCounter,
     TPM_Process_Unused, TPM_Process_ReadCounter,
     TRUE,
     FALSE,
     0, 0,
     0, 0,
     0,
     0,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_ReadManuMaintPub,
#if defined(TPM_NOMAINTENANCE) || defined(TPM_NOMAINTENANCE_COMMANDS)
     TPM_Process_Unused, TPM_Process_Unused,
     FALSE,
     FALSE,
#else
     TPM_Process_ReadManuMaintPub, TPM_Process_ReadManuMaintPub,
     TRUE,
     TRUE,
#endif
     0, 0,
     0, 0,
     0,
     0,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_ReadPubek,
     TPM_Process_ReadPubek, TPM_Process_ReadPubek,
     TRUE,
     TRUE,
     0, 0,
     0, 0,
     0,
     0,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_ReleaseCounter,
     TPM_Process_Unused, TPM_Process_ReleaseCounter,
     TRUE,
     FALSE,
     0, 0,
     0, 0,
     0,
     0,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_ReleaseCounterOwner,
     TPM_Process_Unused, TPM_Process_ReleaseCounterOwner,
     TRUE,
     FALSE,
     1, TPM_DELEGATE_ReleaseCounterOwner,
     0, 0,
     0,
     0,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_ReleaseTransportSigned,
     TPM_Process_Unused, TPM_Process_ReleaseTransportSigned,
     TRUE,
     FALSE,
     0, 0,
     1, TPM_KEY_DELEGATE_ReleaseTransportSigned,
     0,
     0,
     0,
     FALSE,
     FALSE,
     FALSE},
    
    {TPM_ORD_Reset,
     TPM_Process_Reset, TPM_Process_Reset,
     TRUE,
     FALSE,
     0, 0,
     0, 0,
     0,
     0,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_ResetLockValue,
     TPM_Process_Unused, TPM_Process_ResetLockValue,
     TRUE,
     FALSE,
     1, TPM_DELEGATE_ResetLockValue,
     0, 0,
     0,
     0,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_RevokeTrust,
     TPM_Process_Unused, TPM_Process_RevokeTrust,
     TRUE,
     FALSE,
     0, 0,
     0, 0,
     0,
     0,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_SaveAuthContext,
     TPM_Process_SaveAuthContext, TPM_Process_SaveAuthContext,
     TRUE,
     FALSE,
     0, 0,
     0, 0,
     sizeof(TPM_AUTHHANDLE),
     0,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_SaveContext,
     TPM_Process_Unused, TPM_Process_SaveContext,
     TRUE,
     FALSE,
     0, 0,
     0, 0,
     sizeof(TPM_HANDLE),
     0xffffffff,
     0,
     TRUE,
     TRUE,
     FALSE},
    
    {TPM_ORD_SaveKeyContext,
     TPM_Process_SaveKeyContext, TPM_Process_SaveKeyContext,
     TRUE,
     FALSE,
     0, 0,
     0, 0,
     sizeof(TPM_KEY_HANDLE),
     1,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_SaveState,
     TPM_Process_SaveState, TPM_Process_SaveState,
     TRUE,
     FALSE,
     0, 0,
     0, 0,
     0,
     0,
     0,
     TRUE,
     TRUE,
     FALSE},
    
    {TPM_ORD_Seal,
     TPM_Process_Seal, TPM_Process_Seal,
     TRUE,
     FALSE,
     0, 0,
     1, TPM_KEY_DELEGATE_Seal,
     sizeof(TPM_KEY_HANDLE),
     1,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_Sealx,
     TPM_Process_Unused, TPM_Process_Sealx,
     TRUE,
     FALSE,
     0, 0,
     1, TPM_KEY_DELEGATE_Sealx,
     sizeof(TPM_KEY_HANDLE),
     1,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_SelfTestFull,
     TPM_Process_SelfTestFull, TPM_Process_SelfTestFull,
     TRUE,
     FALSE,
     0, 0,
     0, 0,
     0,
     0,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_SetCapability,
     TPM_Process_Unused, TPM_Process_SetCapability,
     TRUE,
     FALSE,
     1, TPM_DELEGATE_SetCapability,
     0, 0,
     0,
     0,
     0,
     TRUE,
     TRUE,
     FALSE},
    
    {TPM_ORD_SetOperatorAuth,
     TPM_Process_Unused, TPM_Process_SetOperatorAuth,
     TRUE,
     FALSE,
     0, 0,
     0, 0,
     0,
     0,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_SetOrdinalAuditStatus,
     TPM_Process_SetOrdinalAuditStatus, TPM_Process_SetOrdinalAuditStatus,
     TRUE,
     TRUE,
     1, TPM_DELEGATE_SetOrdinalAuditStatus,
     0, 0,
     0,
     0,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_SetOwnerInstall,
     TPM_Process_SetOwnerInstall, TPM_Process_SetOwnerInstall,
     TRUE,
     TRUE,
     0, 0,
     0, 0,
     0,
     0,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_SetOwnerPointer,
     TPM_Process_Unused, TPM_Process_SetOwnerPointer,
     TRUE,
     FALSE,
     0, 0,
     0, 0,
     0,
     0,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_SetRedirection,
     TPM_Process_Unused, TPM_Process_Unused,
     TRUE,
     FALSE,
     1, TPM_DELEGATE_SetRedirection,
     0, 0,
     sizeof(TPM_KEY_HANDLE),
     1,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_SetTempDeactivated,
     TPM_Process_SetTempDeactivated, TPM_Process_SetTempDeactivated,
     TRUE,
     TRUE,
     0, 0,
     0, 0,
     0,
     0,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_SHA1Complete,
     TPM_Process_SHA1Complete, TPM_Process_SHA1Complete,
     TRUE,
     FALSE,
     0, 0,
     0, 0,
     0,
     0,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_SHA1CompleteExtend,
     TPM_Process_SHA1CompleteExtend, TPM_Process_SHA1CompleteExtend,
     TRUE,
     FALSE,
     0, 0,
     0, 0,
     0,
     0,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_SHA1Start,
     TPM_Process_SHA1Start, TPM_Process_SHA1Start,
     TRUE,
     FALSE,
     0, 0,
     0, 0,
     0,
     0,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_SHA1Update,
     TPM_Process_SHA1Update, TPM_Process_SHA1Update,
     TRUE,
     FALSE,
     0, 0,
     0, 0,
     0,
     0,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_Sign,
     TPM_Process_Sign, TPM_Process_Sign,
     TRUE,
     FALSE,
     0, 0,
     1, TPM_KEY_DELEGATE_Sign,
     sizeof(TPM_KEY_HANDLE),
     1,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_Startup,
     TPM_Process_Startup, TPM_Process_Startup,
     TRUE,
     FALSE,
     0, 0,
     0, 0,
     0,
     0,
     0,
     TRUE,
     TRUE,
     FALSE},
    
    {TPM_ORD_StirRandom,
     TPM_Process_StirRandom, TPM_Process_StirRandom,
     TRUE,
     FALSE,
     0, 0,
     0, 0,
     0,
     0,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_TakeOwnership,
     TPM_Process_TakeOwnership, TPM_Process_TakeOwnership,
     TRUE,
     TRUE,
     0, 0,
     0, 0,
     0,
     0,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_Terminate_Handle,
     TPM_Process_TerminateHandle, TPM_Process_TerminateHandle,
     TRUE,
     FALSE,
     0, 0,
     0, 0,
     sizeof(TPM_AUTHHANDLE),
     0,
     0,
     TRUE,
     TRUE,
     TRUE},
    
    {TPM_ORD_TickStampBlob,
     TPM_Process_Unused, TPM_Process_TickStampBlob,
     TRUE,
     FALSE,
     0, 0,
     1, TPM_KEY_DELEGATE_TickStampBlob,
     sizeof(TPM_KEY_HANDLE),
     1,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_UnBind,
     TPM_Process_UnBind, TPM_Process_UnBind,
     TRUE,
     FALSE,
     0, 0,
     1, TPM_KEY_DELEGATE_UnBind,
     sizeof(TPM_KEY_HANDLE),
     1,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TPM_ORD_Unseal,
     TPM_Process_Unseal, TPM_Process_Unseal,
     TRUE,
     FALSE,
     0, 0,
     1, TPM_KEY_DELEGATE_Unseal,
     sizeof(TPM_KEY_HANDLE),
     1,
     0,
     TRUE,
     FALSE,
     FALSE},
    
    {TSC_ORD_PhysicalPresence,
     TPM_Process_PhysicalPresence, TPM_Process_PhysicalPresence,
     TRUE,
     FALSE,
     0, 0,
     0, 0,
     0,
     0,
     0,
     TRUE,
     TRUE,
     FALSE},
    
    {TSC_ORD_ResetEstablishmentBit,
     TPM_Process_Unused, TPM_Process_ResetEstablishmentBit,
     TRUE,
     FALSE,
     0, 0,
     0, 0,
     0,
     0,
     0,
     TRUE,
     FALSE,
     FALSE}
    

    
};

/* 
   Ordinal Table Utilities
*/

/* TPM_OrdinalTable_GetEntry() gets the table entry for the ordinal.

   If the ordinal is not in the table, TPM_BAD_ORDINAL is returned
*/

TPM_RESULT TPM_OrdinalTable_GetEntry(TPM_ORDINAL_TABLE **entry,
				     TPM_ORDINAL_TABLE *ordinalTable,
				     TPM_COMMAND_CODE ordinal)
{
    TPM_RESULT	rc = TPM_BAD_ORDINAL;
    size_t	i;

    /* printf(" TPM_OrdinalTable_GetEntry: Ordinal %08x\n", ordinal); */
    *entry = NULL;
    for (i = 0 ; i < (sizeof(tpm_ordinal_table)/sizeof(TPM_ORDINAL_TABLE)) ; i++) {
	if (ordinalTable[i].ordinal == ordinal) {	/* if found */
	    *entry = &(ordinalTable[i]);		/* return the entry */
	    rc = 0;					/* return found */
	    break;
	}
    }
    return rc;
}

/* TPM_OrdinalTable_GetProcessFunction() returns the processing function for the ordinal.

   If the ordinal is not in the table, the function TPM_Process_Unused() is returned.
*/

void TPM_OrdinalTable_GetProcessFunction(tpm_process_function_t *tpm_process_function,
					 TPM_ORDINAL_TABLE *ordinalTable,
					 TPM_COMMAND_CODE ordinal)
{
    TPM_RESULT	rc = 0;
    TPM_ORDINAL_TABLE *entry;
    
    printf(" TPM_OrdinalTable_GetProcessFunction: Ordinal %08x\n", ordinal);

    if (rc == 0) {
	rc = TPM_OrdinalTable_GetEntry(&entry, ordinalTable, ordinal);
    }
    if (rc == 0) {	/* if found */
#ifdef TPM_V12
	*tpm_process_function = entry->process_function_v12;
#else
	*tpm_process_function = entry->process_function_v11;
#endif
    }
    else {	/* if not found, default processing function */
	*tpm_process_function = TPM_Process_Unused;
    }
    return;
}

/* TPM_OrdinalTable_GetAuditable() determines whether the ordinal can ever be audited.

   Used by TPM_Process_SetOrdinalAuditStatus()
*/

void TPM_OrdinalTable_GetAuditable(TPM_BOOL *auditable,
				   TPM_COMMAND_CODE ordinal)
{
    TPM_RESULT	rc = 0;
    TPM_ORDINAL_TABLE *entry;
    
    printf(" TPM_OrdinalTable_GetAuditable: Ordinal %08x\n", ordinal);
    if (rc == 0) {
	rc = TPM_OrdinalTable_GetEntry(&entry, tpm_ordinal_table, ordinal);
    }
    /* if not found, unimplemented, not auditable */
    if (rc != 0) {
	*auditable = FALSE;
    }
    /* if unimplemented, not auditable */
#ifdef TPM_V12
    else if (entry->process_function_v12 == TPM_Process_Unused) {
	*auditable = FALSE;
    }
#else
    else if (entry->process_function_v11 == TPM_Process_Unused) {
	*auditable = FALSE;
    }
#endif
    /* if found an entry, use it */
    else {
	*auditable = entry->auditable;
    }
    return;
}

/* TPM_OrdinalTable_GetAuditDefault() determines whether the ordinal is audited by default.

   Used to initialize TPM_PERMANENT_DATA -> ordinalAuditStatus

   Returns FALSE if the ordinal is not in the ordinals table.
*/

void TPM_OrdinalTable_GetAuditDefault(TPM_BOOL *auditDefault,
				      TPM_COMMAND_CODE ordinal)
{
    TPM_RESULT rc = 0;
    TPM_ORDINAL_TABLE *entry;

    if (rc == 0) {
	rc = TPM_OrdinalTable_GetEntry(&entry, tpm_ordinal_table, ordinal);
    }
    /* if not found, unimplemented, not auditable */
    if (rc != 0) {
	*auditDefault = FALSE;
    }
    /* found an entry, return it */
    else {
	*auditDefault = entry->auditDefault;
    }
    return;
}


/* TPM_OrdinalTable_GetOwnerPermission() gets the owner permission block and the position within the
   block for a permission bit based on the ordinal
*/

TPM_RESULT TPM_OrdinalTable_GetOwnerPermission(uint16_t *ownerPermissionBlock,
					       uint32_t *ownerPermissionPosition,
					       TPM_COMMAND_CODE ordinal)
{
    TPM_RESULT rc = 0;
    TPM_ORDINAL_TABLE *entry;

    if (rc == 0) {
	rc = TPM_OrdinalTable_GetEntry(&entry, tpm_ordinal_table, ordinal);
    }
    if (rc == 0) {
	*ownerPermissionBlock = entry->ownerPermissionBlock;
	*ownerPermissionPosition = entry->ownerPermissionPosition;
	/* sanity check ordinal table entry value */
	if (*ownerPermissionPosition >= (sizeof(uint32_t) * CHAR_BIT)) {
	    printf("TPM_OrdinalTable_GetOwnerPermission: Error (fatal): "
		   "ownerPermissionPosition out of range %u\n", *ownerPermissionPosition);
	    rc = TPM_FAIL;	/* should never occur */
	}
    }
    return rc;
}

/* TPM_OrdinalTable_GetKeyPermission() gets the key permission block and the position within the
   block for a permission bit based on the ordinal
*/

TPM_RESULT TPM_OrdinalTable_GetKeyPermission(uint16_t *keyPermissionBlock,
					     uint32_t *keyPermissionPosition,
					     TPM_COMMAND_CODE ordinal)
{	
    TPM_RESULT rc = 0;
    TPM_ORDINAL_TABLE *entry;

    if (rc == 0) {
	rc = TPM_OrdinalTable_GetEntry(&entry, tpm_ordinal_table, ordinal);
    }
    if (rc == 0) {
	*keyPermissionBlock = entry->keyPermissionBlock;
	*keyPermissionPosition = entry->keyPermissionPosition;
	if (*keyPermissionPosition >= (sizeof(uint32_t) * CHAR_BIT)) {
	    printf("TPM_OrdinalTable_GetKeyPermission: Error (fatal): "
		   "keyPermissionPosition out of range %u\n", *keyPermissionPosition);
	    rc = TPM_FAIL;	/* should never occur */
	}
    }
    return rc;
}

/* TPM_OrdinalTable_ParseWrappedCmd() parses a transport wrapped command, extracting

	- index into DATAw
	- length of DATAw
	- number of key handles and their indexes
	- ordinal
	- transportWrappable FALSE if the command cannot be wrapped in a transport session

   FIXME if audit has to occur before command parsing, this command becomes more generally useful,
   and might do the auditing and return the inParamDigest as well.

   This function cannot get the actual key handle(s) because the value may be encrypted, and the
   decryption has not occurred yet.
*/

TPM_RESULT TPM_OrdinalTable_ParseWrappedCmd(uint32_t *datawStart,
					    uint32_t *datawLen,
					    uint32_t *keyHandles,
					    uint32_t *keyHandle1Index,
					    uint32_t *keyHandle2Index,
					    TPM_COMMAND_CODE *ordinal,
					    TPM_BOOL *transportWrappable,
					    TPM_SIZED_BUFFER *wrappedCmd)
{
    TPM_RESULT		rc = 0;
    uint32_t		stream_size;
    unsigned char	*stream;
    TPM_TAG		tag = 0;
    uint32_t		paramSize = 0;
    TPM_ORDINAL_TABLE	*entry;		/* table entry for the ordinal */
    uint32_t		authLen;	/* length of below the line parameters */

    printf(" TPM_OrdinalTable_ParseWrappedCmd:\n");
    /* Extract the standard command parameters from the command stream.	 This also validates
       paramSize against wrappedCmdSize */
    if (rc == 0) {
	/* make temporary copies so the wrappedCmd is not touched */
	/* FIXME might want to return paramSize and tag and move the wrappedCmd pointers */
	stream = wrappedCmd->buffer;
	stream_size = wrappedCmd->size;
	/* parse the three standard input parameters, check paramSize against wrappedCmd->size */
	rc = TPM_Process_GetCommandParams(&tag, &paramSize, ordinal,
					  &stream, &stream_size);
    }
    /* get the entry from the ordinal table */
    if (rc == 0) {
	printf("  TPM_OrdinalTable_ParseWrappedCmd: ordinal %08x\n", *ordinal);
	rc = TPM_OrdinalTable_GetEntry(&entry, tpm_ordinal_table, *ordinal);
    }
    if (rc == 0) {
	/* datawStart indexes into the dataW area, skip the standard 3 inputs and the handles */
	*datawStart = sizeof(TPM_TAG) + sizeof(uint32_t) + sizeof(TPM_COMMAND_CODE) +
		      entry->inputHandleSize;
	/* authLen is the length of the below-the-line auth parameters that are excluded from the
	   dataW area */
	switch (tag) {
	  case TPM_TAG_RQU_AUTH1_COMMAND:
	    authLen = sizeof(TPM_AUTHHANDLE) + TPM_NONCE_SIZE +
		      sizeof(TPM_BOOL) + TPM_AUTHDATA_SIZE;
	    break;
	  case TPM_TAG_RQU_AUTH2_COMMAND:
	    authLen = 2 *
		      (sizeof(TPM_AUTHHANDLE) + TPM_NONCE_SIZE +
		       sizeof(TPM_BOOL) + TPM_AUTHDATA_SIZE);
	    break;
	  case TPM_TAG_RQU_COMMAND:
	    /* if the tag is illegal, assume the dataW area goes to the end of the command */
	  default:
	    authLen = 0;
	    break;
	}
	if (paramSize < *datawStart + authLen) {
	    printf("TPM_OrdinalTable_ParseWrappedCmd: Error, "
		   "paramSize %u less than datawStart %u + authLen %u\n",
		   paramSize, *datawStart, authLen);
	    rc = TPM_BAD_PARAM_SIZE;
	}
    }
    if (rc == 0) {
	/* subtract safe, cannot be negative after above check */
	*datawLen = paramSize - *datawStart - authLen;
	printf("  TPM_OrdinalTable_ParseWrappedCmd: datawStart %u datawLen %u\n",
	       *datawStart, *datawLen);
	/* determine whether the command can be wrapped in a transport session */
	*transportWrappable = entry->transportWrappable;
	/* return the number of key handles */
	*keyHandles = entry->keyHandles;
    }
    if (rc == 0) {
	printf("  TPM_OrdinalTable_ParseWrappedCmd: key handles %u\n", *keyHandles);
	switch (*keyHandles) {
	  case 0:
	    /* no key handles */
	    break;
	  case 1:
	    /* one key handle */
	    *keyHandle1Index = sizeof(TPM_TAG) + sizeof(uint32_t) + sizeof(TPM_COMMAND_CODE);
	    break;
	  case 2: 
	    /* first key handle */
	    *keyHandle1Index = sizeof(TPM_TAG) + sizeof(uint32_t) + sizeof(TPM_COMMAND_CODE);
	    /* second key handle */
	    *keyHandle2Index = sizeof(TPM_TAG) + sizeof(uint32_t) + sizeof(TPM_COMMAND_CODE) +
			       sizeof(TPM_KEY_HANDLE);
	    break;
	  case 0xffffffff:
	    printf("  TPM_OrdinalTable_ParseWrappedCmd: key handles special case\n");
	    /* potential key handle */
	    *keyHandle1Index = sizeof(TPM_TAG) + sizeof(uint32_t) + sizeof(TPM_COMMAND_CODE);
	    /* can't determine handle type here since resourceType is encrypted */
	    break;
	  default:
	    /* sanity check ordinal table */
	    printf("TPM_OrdinalTable_ParseWrappedCmd: Error (fatal), "
		   "invalid key handles for %08x for ordinal %08x\n", *keyHandles, *ordinal);
	    rc = TPM_FAIL;	/* should never occur */
	    break;
	}
    }
    return rc;
}

/* TPM_OrdinalTable_ParseWrappedRsp() parses a transport wrapped response, extracting

   - index into DATAw
   - length of DATAw
   - return code RCw

   FIXME this command might do the auditing and return the outParamDigest as well.
*/

TPM_RESULT TPM_OrdinalTable_ParseWrappedRsp(uint32_t *datawStart,
					    uint32_t *datawLen,
					    TPM_RESULT *rcw,
					    TPM_COMMAND_CODE ordinal,
					    const unsigned char *wrappedRspStream,
					    uint32_t wrappedRspStreamSize)
{
    TPM_RESULT		rc = 0;
    TPM_TAG		tag = 0;
    uint32_t		paramSize = 0;
    TPM_ORDINAL_TABLE	*entry;		/* table entry for the ordinal */
    uint32_t		authLen;	/* length of below the line parameters */

    printf(" TPM_OrdinalTable_ParseWrappedRsp: ordinal %08x\n", ordinal);
    /* Extract the standard response parameters from the response stream.  This also validates
       paramSize against wrappedRspSize */
    if (rc == 0) {
	rc = TPM_Process_GetResponseParams(&tag, &paramSize, rcw,
					   (unsigned char **)&wrappedRspStream,
					   &wrappedRspStreamSize);
    }
    /* get the entry from the ordinal table */
    if (rc == 0) {
	printf(" TPM_OrdinalTable_ParseWrappedRsp: returnCode %08x\n", *rcw);
	rc = TPM_OrdinalTable_GetEntry(&entry, tpm_ordinal_table, ordinal);
    }
    /* parse the success return code case */
    if ((rc == 0) && (*rcw == TPM_SUCCESS)) {
	if (rc == 0) {
	    /* datawStart indexes into the dataW area, skip the standard 3 inputs and the handles */
	    *datawStart = sizeof(TPM_TAG) + sizeof(uint32_t) + sizeof(TPM_RESULT) +
			  entry->outputHandleSize;
	    /* authLen is the length of the below-the-line auth parameters that are excluded from
	       the dataW area */
	    switch (tag) {
	      case TPM_TAG_RSP_AUTH1_COMMAND:
		authLen = TPM_NONCE_SIZE + sizeof(TPM_BOOL) + TPM_AUTHDATA_SIZE;
		break;
	      case TPM_TAG_RSP_AUTH2_COMMAND:
		authLen = 2 * (TPM_NONCE_SIZE + sizeof(TPM_BOOL) + TPM_AUTHDATA_SIZE);
		break;
	      case TPM_TAG_RSP_COMMAND:
		/* if the tag is illegal, assume the dataW area goes to the end of the response */
	      default:
		authLen = 0;
		break;
	    }
	    if (paramSize < *datawStart + authLen) {
		printf("TPM_OrdinalTable_ParseWrappedRsp: Error, "
		       "paramSize %u less than datawStart %u + authLen %u\n",
		       paramSize, *datawStart, authLen);
		rc = TPM_BAD_PARAM_SIZE;	/* FIXME not clear what to do here */
	    }
	}
	if (rc == 0) {
	    /* subtract safe, cannot be negative after about check */
	    *datawLen = paramSize - *datawStart - authLen;
	    printf("  TPM_OrdinalTable_ParseWrappedRsp: datawStart %u datawLen %u\n",
		   *datawStart, *datawLen);
	}
    }
    /* if the wrapped command failed, datawStart is not used, and datawLen is 0 */
    else if ((rc == 0) && (*rcw != TPM_SUCCESS)) {
	*datawStart = sizeof(TPM_TAG) + sizeof(uint32_t) + sizeof(TPM_RESULT);
	*datawLen = 0;
	printf("  TPM_OrdinalTable_ParseWrappedRsp: datawLen %u\n", *datawLen);
    }
    return rc;
}

void TPM_KeyHandleEntries_Trace(TPM_KEY_HANDLE_ENTRY *tpm_key_handle_entries);

void TPM_KeyHandleEntries_Trace(TPM_KEY_HANDLE_ENTRY *tpm_key_handle_entries)
{
    size_t i;
    for (i = 0 ; (i < 4) && (i < TPM_KEY_HANDLES) ; i++) {
	printf("TPM_KeyHandleEntries_Trace: %lu handle %08x tpm_key %p\n",
	       (unsigned long)i, tpm_key_handle_entries[i].handle, tpm_key_handle_entries[i].key);
    }
    return;
}

void TPM_State_Trace(tpm_state_t *tpm_state);

void TPM_State_Trace(tpm_state_t *tpm_state)
{
    printf("TPM_State_Trace: disable %u p_deactive %u v_deactive %u owned %u state %u\n",
	   tpm_state->tpm_permanent_flags.disable,
	   tpm_state->tpm_permanent_flags.deactivated,
	   tpm_state->tpm_stclear_flags.deactivated,
	   tpm_state->tpm_permanent_data.ownerInstalled,
	   tpm_state->testState);
    return;
}

/* TPM_ProcessA() is an alternate to TPM_Process() that uses standard C types.  It provides an entry
   point to the TPM without requiring the TPM_STORE_BUFFER class.

   The design pattern for the response is:

   - set '*response' to NULL at the first call

   - on subsequent calls, pass 'response' and 'response_total' back in.  Set 'response_size' back
     to 0.

   On input:
   
   '*response' - pointer to a buffer that was allocated (can be NULL)

   'response_size' - the number of valid bytes in buffer (ignored if buffer is NULL, can be 0,
   cannot be greater than total.  Set to zero, unless one wants the TPM_Process() function to append
   a response to some existing data.

   '*response_total' - the total number of allocated bytes (ignored if buffer is NULL)

   On output:

   '*response' - pointer to a buffer that was allocated or reallocated

   'response_size' - the number of valid bytes in buffer
   
   '*response_total' - the total number of allocated or reallocated bytes
*/

TPM_RESULT TPM_ProcessA(unsigned char **response,
			uint32_t *response_size,
			uint32_t *response_total,
			unsigned char *command,		/* complete command array */
			uint32_t command_size)		/* actual bytes in command */

{
    TPM_RESULT rc = 0;
    TPM_STORE_BUFFER responseSbuffer;

    /* set the sbuffer from the response parameters */
    if (rc == 0) {
	rc = TPM_Sbuffer_Set(&responseSbuffer,
			     *response,
			     *response_size,
			     *response_total);
    }
    if (rc == 0) {
	rc = TPM_Process(&responseSbuffer,
			 command,		/* complete command array */
			 command_size);		/* actual bytes in command */

    }
    /* get the response parameters from the sbuffer */
    if (rc == 0) {
	TPM_Sbuffer_GetAll(&responseSbuffer,
			   response,
			   response_size,
			   response_total);
    }
    return rc;
}

/* Process the command from the host to the TPM.

   'command_size' is the actual size of the command stream.

   Returns:
       0 on success

       non-zero on a fatal error preventing the command from being processed.  The response is
       invalid in this case.
*/

TPM_RESULT TPM_Process(TPM_STORE_BUFFER *response,
		       unsigned char *command,		/* complete command array */
		       uint32_t command_size)		/* actual bytes in command */
{
    TPM_RESULT		rc = 0;				/* fatal error, no response */
    TPM_RESULT		returnCode = TPM_SUCCESS;	/* fatal error in ordinal processing,
							   can be returned */
    TPM_TAG		tag = 0;
    uint32_t		paramSize = 0;
    TPM_COMMAND_CODE	ordinal = 0;
    tpm_process_function_t tpm_process_function = NULL;	/* based on ordinal */
    tpm_state_t		*targetInstance = NULL;		/* TPM global state */
    TPM_STORE_BUFFER	localBuffer;		/* for response if instance was not found */
    TPM_STORE_BUFFER	*sbuffer;		/* either localBuffer or the instance response
						   buffer */

    TPM_Sbuffer_Init(&localBuffer);	/* freed @1 */
    /* get the global TPM state */
    if ((rc == 0) && (returnCode == TPM_SUCCESS)) {
	targetInstance = tpm_instances[0];
    }
    if ((rc == 0) && (returnCode == TPM_SUCCESS)) {
	/* clear the response form the previous ordinal, the response buffer is reused */
	TPM_Sbuffer_Clear(&(targetInstance->tpm_stclear_data.ordinalResponse));
	/* extract the standard command parameters from the command stream */
	returnCode = TPM_Process_GetCommandParams(&tag, &paramSize, &ordinal,
						  &command, &command_size);
    }	 
    /* preprocessing common to all ordinals */
    if ((rc == 0) && (returnCode == TPM_SUCCESS)) {
	returnCode = TPM_Process_Preprocess(targetInstance, ordinal, NULL);
    }
    /* NOTE Only for debugging */
    if ((rc == 0) && (returnCode == TPM_SUCCESS)) {
	TPM_KeyHandleEntries_Trace(targetInstance->tpm_key_handle_entries);
    }
    /* process the ordinal */
    if ((rc == 0) && (returnCode == TPM_SUCCESS)) {
	/* get the processing function from the ordinal table */
	TPM_OrdinalTable_GetProcessFunction(&tpm_process_function, tpm_ordinal_table, ordinal);
	/* call the processing function to execute the command */
	returnCode = tpm_process_function(targetInstance,
					  &(targetInstance->tpm_stclear_data.ordinalResponse),
					  tag, command_size, ordinal, command,
					  NULL);	/* not from encrypted transport */
    }
    /* NOTE Only for debugging */
    if ((rc == 0) && (returnCode == TPM_SUCCESS)) {
	TPM_KeyHandleEntries_Trace(targetInstance->tpm_key_handle_entries);
    }
    /* NOTE Only for debugging */
    if ((rc == 0) && (returnCode == TPM_SUCCESS)) {
	TPM_State_Trace(targetInstance);
    }
#ifdef TPM_VOLATILE_STORE
    /* save the volatile state after each command to handle fail-over restart */
    if ((rc == 0) && (returnCode == TPM_SUCCESS)) {
	returnCode = TPM_VolatileAll_NVStore(targetInstance);
    }
#endif	/* TPM_VOLATILE_STORE */
    /* If the ordinal processing function returned without a fatal error, append its ordinalResponse
       to the output response buffer */
    if ((rc == 0) && (returnCode == TPM_SUCCESS)) {
	returnCode = TPM_Sbuffer_AppendSBuffer(response,
					       &(targetInstance->tpm_stclear_data.ordinalResponse));
    }
    if ((rc == 0) && (returnCode != TPM_SUCCESS)) {
	/* gets here if:
	   
	   - there was an error before the ordinal was processed	
	   - the ordinal returned a fatal error
	   - an error occurred appending the ordinal response
	    
	   returnCode should be the response
	   errors here are fatal, can't create an error response
	*/
	/* if it failed after the target instance was found, use the instance's response buffer */
	if (targetInstance != NULL) {
	    sbuffer = &(targetInstance->tpm_stclear_data.ordinalResponse);
	}
	/* if it failed before even the target instance was found, use a local buffer */
	else {
	    sbuffer = &localBuffer;
	}
	if (rc == 0) {
	    /* it's not even known whether the initial response was stored, so just start
	       over */
	    TPM_Sbuffer_Clear(sbuffer);
	    /* store the tag, paramSize, and returnCode */
	    printf("TPM_Process: Ordinal returnCode %08x %u\n",
		   returnCode, returnCode);
	    rc = TPM_Sbuffer_StoreInitialResponse(sbuffer, TPM_TAG_RQU_COMMAND, returnCode);
	}
	/* call this to handle the TPM_FAIL causing the TPM going into failure mode */
	if (rc == 0) {
	    rc = TPM_Sbuffer_StoreFinalResponse(sbuffer, returnCode, targetInstance);
	}
	if (rc == 0) {
	    rc = TPM_Sbuffer_AppendSBuffer(response, sbuffer);
	}
    }
    /*
      cleanup
    */
    TPM_Sbuffer_Delete(&localBuffer);	/* @1 */
    return rc;
}

/* TPM_Process_Wrapped() is called recursively to process a wrapped command.

   'command_size' is the actual size of the command stream.

   'targetInstance' is an input indicating the TPM instance being called.

   'transportInternal' not NULL indicates that this function was called recursively from
   TPM_ExecuteTransport

   For wrapped commands, this function cannot trust that command_size and the incoming paramSize in
   the command stream are consistent.  Therefore, this function checks for consistency.

   The processor ensures that the response bytes are set according to the outgoing paramSize on
   return.

   Returns:
	0 on success

	non-zero on a fatal error preventing the command from being processed.	The response is
	invalid in this case.
*/

TPM_RESULT TPM_Process_Wrapped(TPM_STORE_BUFFER *response,
			       unsigned char *command,		/* complete command array */
			       uint32_t command_size,		/* actual bytes in command */
			       tpm_state_t *targetInstance,	/* global TPM state */
			       TPM_TRANSPORT_INTERNAL *transportInternal)
{
    TPM_RESULT		rc = 0;				/* fatal error, no response */
    TPM_RESULT		returnCode = TPM_SUCCESS;	/* non-fatal error, returned in response */
    TPM_TAG		tag = 0;
    uint32_t		paramSize = 0;
    TPM_COMMAND_CODE	ordinal = 0;
    tpm_process_function_t tpm_process_function = NULL; /* based on ordinal */
    TPM_STORE_BUFFER	ordinalResponse;		/* response for this ordinal */
    
    printf("TPM_Process_Wrapped:\n");
    TPM_Sbuffer_Init(&ordinalResponse);		/* freed @1 */
    /* Set the tag, paramSize, and ordinal from the wrapped command stream */
    /* If paramSize does not equal the command stream size, return TPM_BAD_PARAM_SIZE */
    if ((rc == 0) && (returnCode == TPM_SUCCESS)) {
	returnCode = TPM_Process_GetCommandParams(&tag, &paramSize, &ordinal,
						  &command, &command_size);
    }
    /* preprocessing common to all ordinals */
    if ((rc == 0) && (returnCode == TPM_SUCCESS)) {
	returnCode = TPM_Process_Preprocess(targetInstance, ordinal, transportInternal);
    }
    /* process the ordinal */
    if ((rc == 0) && (returnCode == TPM_SUCCESS)) {
	/* get the processing function from the ordinal table */
	TPM_OrdinalTable_GetProcessFunction(&tpm_process_function, tpm_ordinal_table, ordinal);
	/* call the processing function to execute the command */
	returnCode = tpm_process_function(targetInstance, &ordinalResponse,
					  tag, command_size, ordinal, command,
					  transportInternal);
    }
    /* If the ordinal processing function returned without a fatal error, append its ordinalResponse
       to the output response buffer */
    if ((rc == 0) && (returnCode == TPM_SUCCESS)) {
	returnCode = TPM_Sbuffer_AppendSBuffer(response, &ordinalResponse);
    }
    /* If:

       - an error in this function occurred before the ordinal was processed
       - the ordinal processing function returned a fatal error
       - an error occurred appending the ordinal response

       then use the return code of that failure as the final response.	Failure here is fatal, since
       no error code can be returned.
    */
    if ((rc == 0) && (returnCode != TPM_SUCCESS)) {
	rc = TPM_Sbuffer_StoreFinalResponse(response, returnCode, targetInstance);
    }
    /*
      cleanup
    */
    TPM_Sbuffer_Delete(&ordinalResponse);	/* @1 */
    return rc;
}

/* TPM_Process_GetCommandParams() gets the standard 3 parameters from the command input stream

   The stream is adjusted to point past the parameters.

   The resulting paramSize is checked against the stream size for consistency.	paramSize is
   returned for reference, but command_size reflects the remaining bytes in the stream.
*/

TPM_RESULT TPM_Process_GetCommandParams(TPM_TAG *tag,
					uint32_t *paramSize ,
					TPM_COMMAND_CODE *ordinal,
					unsigned char **command,
					uint32_t *command_size)
{
    TPM_RESULT	rc = 0;

    printf(" TPM_Process_GetCommandParams:\n");
    /* get tag */
    if (rc == 0) {
	rc = TPM_Load16(tag, command, command_size);
    }
    /* get paramSize */
    if (rc == 0) {
	rc = TPM_Load32(paramSize, command, command_size);
    }
    /* get ordinal */
    if (rc == 0) {
	rc = TPM_Load32(ordinal, command, command_size);
    }
    /* check the paramSize against the command_size */
    if (rc == 0) {
	if (*paramSize !=
	    *command_size + sizeof(TPM_TAG) + sizeof(uint32_t) + sizeof(TPM_COMMAND_CODE)) {

	    printf("TPM_Process_GetCommandParams: Error, "
		   "command size %lu not equal to paramSize %u\n",
		   (unsigned long)
		   (*command_size + sizeof(TPM_TAG) + sizeof(uint32_t) + sizeof(TPM_COMMAND_CODE)),
		   *paramSize);
	    rc = TPM_BAD_PARAM_SIZE;
	}
	else {
	    printf("  TPM_Process_GetCommandParams: tag %04x paramSize %u ordinal %08x\n",
		   *tag, *paramSize, *ordinal);
	}
    }
    return rc;
}

/* TPM_Process_GetResponseParams() gets the standard 3 parameters from the response output stream

   The stream is adjusted to point past the parameters.

   The resulting paramSize is checked against the stream size for consistency.	paramSize is
   returned for reference, but response_size reflects the remaining bytes in the stream.
*/

TPM_RESULT TPM_Process_GetResponseParams(TPM_TAG *tag,
					 uint32_t *paramSize ,
					 TPM_RESULT *returnCode,
					 unsigned char **response,
					 uint32_t *response_size)
{
    TPM_RESULT	rc = 0;

    printf(" TPM_Process_GetResponseParams:\n");
    /* get tag */
    if (rc == 0) {
	rc = TPM_Load16(tag, response, response_size);
    }
    /* get paramSize */
    if (rc == 0) {
	rc = TPM_Load32(paramSize, response, response_size);
    }
    /* get returnCode */
    if (rc == 0) {
	rc = TPM_Load32(returnCode, response, response_size);
    }
    /* check the paramSize against the response_size */
    if (rc == 0) {
	if (*paramSize != (*response_size + sizeof(TPM_TAG) +
			   sizeof(uint32_t) + sizeof(TPM_RESULT))) {
	    
	    printf("TPM_Process_GetResponseParams: Error, "
		   "response size %lu not equal to paramSize %u\n",
		   (unsigned long)
		   (*response_size + sizeof(TPM_TAG) + sizeof(uint32_t) + sizeof(TPM_RESULT)),
		   *paramSize);
	    rc = TPM_BAD_PARAM_SIZE;
	}
	else {
	    printf("  TPM_Process_GetResponseParams: tag %04x paramSize %u ordinal %08x\n",
		   *tag, *paramSize, *returnCode);
	}
    }
    return rc;
}

/* TPM_CheckRequestTagnnn() is common code to verify the command tag */

TPM_RESULT TPM_CheckRequestTag210(TPM_TAG tpm_tag)
{
    TPM_RESULT	rc = 0;

    if ((tpm_tag != TPM_TAG_RQU_AUTH2_COMMAND) &&
	(tpm_tag != TPM_TAG_RQU_AUTH1_COMMAND) &&
	(tpm_tag != TPM_TAG_RQU_COMMAND)) {
	printf("TPM_CheckRequestTag210: Error, tag %04hx\n", tpm_tag);
	rc = TPM_BADTAG;
    }
    return rc;
}

TPM_RESULT TPM_CheckRequestTag21(TPM_TAG tpm_tag)
{
    TPM_RESULT	rc = 0;
    
    if ((tpm_tag != TPM_TAG_RQU_AUTH2_COMMAND) &&
	(tpm_tag != TPM_TAG_RQU_AUTH1_COMMAND)) {
	printf("TPM_CheckRequestTag21: Error, tag %04hx\n", tpm_tag);
	rc = TPM_BADTAG;
    }
    return rc;
}

TPM_RESULT TPM_CheckRequestTag2(TPM_TAG tpm_tag)
{
    TPM_RESULT	rc = 0;
    
    if (tpm_tag != TPM_TAG_RQU_AUTH2_COMMAND) {
	printf("TPM_CheckRequestTag2: Error, tag %04hx\n", tpm_tag);
	rc = TPM_BADTAG;
    }
    return rc;
}

TPM_RESULT TPM_CheckRequestTag10(TPM_TAG tpm_tag)
{
    TPM_RESULT	rc = 0;
    
    if ((tpm_tag != TPM_TAG_RQU_AUTH1_COMMAND) &&
	(tpm_tag != TPM_TAG_RQU_COMMAND)) {
	printf("TPM_CheckRequestTag10: Error, tag %04hx\n", tpm_tag);
	rc = TPM_BADTAG;
    }
    return rc;
}

TPM_RESULT TPM_CheckRequestTag1(TPM_TAG tpm_tag)
{
    TPM_RESULT	rc = 0;
    
    if (tpm_tag != TPM_TAG_RQU_AUTH1_COMMAND) {
	printf("TPM_CheckRequestTag1: Error, tag %04hx\n", tpm_tag);
	rc = TPM_BADTAG;
    }
    return rc;
}

TPM_RESULT TPM_CheckRequestTag0(TPM_TAG tpm_tag)
{
    TPM_RESULT	rc = 0;
    
    if (tpm_tag != TPM_TAG_RQU_COMMAND) {
	printf("TPM_CheckRequestTag0: Error, tag %04hx\n", tpm_tag);
	rc = TPM_BADTAG;
    }
    return rc;
}

TPM_RESULT TPM_Process_Unused(tpm_state_t *tpm_state,
			      TPM_STORE_BUFFER *response,
			      TPM_TAG tag,
			      uint32_t paramSize,
			      TPM_COMMAND_CODE ordinal,
			      unsigned char *command,
			      TPM_TRANSPORT_INTERNAL *transportInternal)
{
    TPM_RESULT	rcf = 0;

    printf("TPM_Process_Unused:\n");
    tpm_state = tpm_state;			/* not used */
    paramSize = paramSize;			/* not used */
    ordinal = ordinal;				/* not used */
    command = command;				/* not used */
    transportInternal = transportInternal;	/* not used */
    printf("TPM_Process_Unused: Ordinal returnCode %08x %u\n",
	   TPM_BAD_ORDINAL, TPM_BAD_ORDINAL);
    rcf = TPM_Sbuffer_StoreInitialResponse(response, tag, TPM_BAD_ORDINAL);
    return rcf;
}

/* TPM_CheckState() should be called by all commands.  It checks a set of flags specified by
   tpm_check_map to determine whether the command can execute in that state.

   Returns: 0 if the command can execute
	    non-zero error code that should be returned as a response
*/

TPM_RESULT TPM_CheckState(tpm_state_t *tpm_state,
			  TPM_TAG tag,
			  uint32_t tpm_check_map)
{
    TPM_RESULT	rc = 0;

    printf(" TPM_CheckState: Check map %08x\n", tpm_check_map);
    /* check the dictionary attack lockout, only for authorized commands */
    if (rc == 0) {
	if ((tpm_check_map & TPM_CHECK_NO_LOCKOUT) && (tag != TPM_TAG_RQU_COMMAND)) {
	    rc = TPM_Authdata_CheckState(tpm_state);
	}
    }
    /* TPM_GetTestResult. This command can assist the TPM manufacturer in determining the cause of
       the self-test failure.  iii.  All other operations will return the error code
       TPM_FAILEDSELFTEST.  */
    if (rc == 0) {
	if (tpm_check_map & TPM_CHECK_NOT_SHUTDOWN) {
	    if (tpm_state->testState == TPM_TEST_STATE_FAILURE) {
		printf("TPM_CheckState: Error, shutdown is TRUE\n");
		rc = TPM_FAILEDSELFTEST;
	    }
	}
    }
    /* TPM_Startup SHALL execute as normal, and is the only function that does not call
       TPM_CheckState().  All other commands SHALL return TPM_INVALID_POSTINIT */
    if (rc == 0) {
	if (tpm_state->tpm_stany_flags.postInitialise) {
	    printf("TPM_CheckState: Error, postInitialise is TRUE\n");
	    rc = TPM_INVALID_POSTINIT;
	}
    }
    /*
      For checking disabled and deactivated, the check is NOT done if it's one of the special NV
      commands (indicated by TPM_CHECK_NV_NOAUTH) and nvLocked is FALSE, indicating that the NV
      store does not require authorization
    */
    /* For commands available only when enabled. */
    if (rc == 0) {
	if ((tpm_check_map & TPM_CHECK_ENABLED) &&
	    !((tpm_check_map & TPM_CHECK_NV_NOAUTH) && !tpm_state->tpm_permanent_flags.nvLocked)) {
	    if (tpm_state->tpm_permanent_flags.disable) {
		printf("TPM_CheckState: Error, disable is TRUE\n");
		rc = TPM_DISABLED;
	    }
	}
    }
    /* For commands only available when activated.  */
    if (rc == 0) {
	if ((tpm_check_map & TPM_CHECK_ACTIVATED) &&
	    !((tpm_check_map & TPM_CHECK_NV_NOAUTH) && !tpm_state->tpm_permanent_flags.nvLocked)) {
	    if (tpm_state->tpm_stclear_flags.deactivated) {
		printf("TPM_CheckState: Error, deactivated is TRUE\n");
		rc = TPM_DEACTIVATED;
	    }
	}
    }
    /* For commands available only after an owner is installed.	 see Ordinals chart */
    if (rc == 0) {
	if (tpm_check_map & TPM_CHECK_OWNER) {
	    if (!tpm_state->tpm_permanent_data.ownerInstalled) {
		printf("TPM_CheckState: Error, ownerInstalled is FALSE\n");
		rc = TPM_NOSRK;
	    }
	}
    }
    return rc;
}

/* TPM_Process_Preprocess() handles check functions common to all ordinals

   'transportPublic' not NULL indicates that this function was called recursively from
   TPM_ExecuteTransport
*/

TPM_RESULT TPM_Process_Preprocess(tpm_state_t *tpm_state,
				  TPM_COMMAND_CODE ordinal,
				  TPM_TRANSPORT_INTERNAL *transportInternal)
{
    TPM_RESULT		rc = 0;				/* fatal error, no response */

    printf(" TPM_Process_Preprocess: Ordinal %08x\n", ordinal);
    /* Preprocess to check if command can be run in limited operation mode */
    if (rc == 0) {
	if (tpm_state->testState == TPM_TEST_STATE_LIMITED) {
	    /* 1. At startup, a TPM MUST self-test all internal functions that are necessary to do
	       TPM_SHA1Start, TPM_SHA1Update, TPM_SHA1Complete, TPM_SHA1CompleteExtend, TPM_Extend,
	       TPM_Startup, TPM_ContinueSelfTest, a subset of TPM_GetCapability, and
	       TPM_GetTestResult..
	    */
	    if (!((ordinal == TPM_ORD_Startup) ||
		  (ordinal == TPM_ORD_SHA1Start) ||
		  (ordinal == TPM_ORD_SHA1Update) ||
		  (ordinal == TPM_ORD_SHA1Complete) ||
		  (ordinal == TPM_ORD_SHA1CompleteExtend) ||
		  (ordinal == TPM_ORD_Extend) ||
		  (ordinal == TPM_ORD_Startup) ||
		  (ordinal == TPM_ORD_ContinueSelfTest) ||
		  /* a subset of TPM_GetCapability does not require self-test.	The ordinal itself
		     decides whether to run TPM_ContinueSelfTest() */
		  (ordinal == TPM_ORD_GetCapability) ||
		  /* 3. The TPM MAY allow TPM_SelfTestFull to be used before completion of the
		     actions of TPM_ContinueSelfTest. */
		  (ordinal == TPM_ORD_SelfTestFull) ||
		  (ordinal == TPM_ORD_GetTestResult) ||
		  /* 2. The TSC_PhysicalPresence and TSC_ResetEstablishmentBit commands do not
		     operate on shielded-locations and have no requirement to be self-tested before
		     any use. TPM's SHOULD test these functions before operation. */
		  (ordinal == TSC_ORD_PhysicalPresence) ||
		  (ordinal == TSC_ORD_ResetEstablishmentBit)
		  )) {
		/* One of the optional actions. */
		/* rc = TPM_NEEDS_SELFTEST; */
		/* Alternatively, could run the actions of continue self-test */
		rc = TPM_ContinueSelfTestCmd(tpm_state);
	    }
	}
    }
    /* special pre-processing for SHA1 context */
    if (rc == 0) {
	rc = TPM_Check_SHA1Context(tpm_state, ordinal, transportInternal);
    }
    /* Special pre-processing to invalidate the saved state if it exists.  Omit this processing for
       TPM_Startup, since that function might restore the state first */
    if (rc == 0) {
	if (tpm_state->tpm_stany_flags.stateSaved &&
	    !((ordinal == TPM_ORD_Startup) ||
	      (ordinal == TPM_ORD_Init))) {
	    /* For any other ordinal, invalidate the saved state if it exists.	*/
	    rc = TPM_SaveState_NVDelete(tpm_state, TRUE);
	}
    }
    /* When an exclusive session is running, execution of any command other then
       TPM_ExecuteTransport or TPM_ReleaseTransportSigned targeting the exclusive session causes the
       abnormal invalidation of the exclusive transport session. */
    if ((rc == 0) && (transportInternal == NULL)) {	/* do test only for the outer ordinal */
	if ((tpm_state->tpm_stany_flags.transportExclusive != 0) &&	/* active exclusive */
	    /* These two ordinals terminate the exclusive transport session if the transport handle
	       is not the specified handle.  So the check is deferred until the command is parsed
	       for the transport handle. */
	    !((ordinal == TPM_ORD_ExecuteTransport) ||
	      (ordinal == TPM_ORD_ReleaseTransportSigned))) {
	    rc = TPM_TransportSessions_TerminateHandle
		 (tpm_state->tpm_stclear_data.transSessions,
		  tpm_state->tpm_stany_flags.transportExclusive,
		  &(tpm_state->tpm_stany_flags.transportExclusive));
	}
    }
    /* call platform specific code to set the localityModifier */
    if ((rc == 0) && (transportInternal == NULL)) {	/* do only for the outer ordinal */
	rc = TPM_IO_GetLocality(&(tpm_state->tpm_stany_flags.localityModifier),
				tpm_state->tpm_number);
    }
    return rc;
}


/* TPM_Check_SHA1Context() checks the current SHA1 context

   The TPM may not allow any other types of processing during the execution of a SHA-1
   session. There is only one SHA-1 session active on a TPM.  After the execution of SHA1Start, and
   prior to SHA1End, the receipt of any command other than SHA1Update will cause the invalidation of
   the SHA-1 session.

   2. After receipt of TPM_SHA1Start, and prior to the receipt of TPM_SHA1Complete or
   TPM_SHA1CompleteExtend, receipt of any command other than TPM_SHA1Update invalidates the SHA-1
   session.
   
   a. If the command received is TPM_ExecuteTransport, the SHA-1 session invalidation is based on
   the wrapped command, not the TPM_ExecuteTransport ordinal.
   
   b. A SHA-1 thread (start, update, complete) MUST take place either completely outside a transport
   session or completely within a single transport session.
*/

TPM_RESULT TPM_Check_SHA1Context(tpm_state_t *tpm_state,
				 TPM_COMMAND_CODE ordinal,
				 TPM_TRANSPORT_INTERNAL *transportInternal)
{
    TPM_RESULT rc = 0;

    if ((tpm_state->sha1_context != NULL) &&	/* if there was a SHA-1 context set up */
	(ordinal != TPM_ORD_ExecuteTransport))	/* depends on the wrapped command */
	{
	/* the non-SHA1 ordinals invalidate the SHA-1 session */
	if (
	    ((ordinal != TPM_ORD_SHA1Update) &&
	     (ordinal != TPM_ORD_SHA1Complete) &&
	     (ordinal != TPM_ORD_SHA1CompleteExtend)) ||
	    
	    /* invalidate if the SHA1 ordinal is within a transport session and the session was not
	       set up within the same transport session. */
	    ((transportInternal != NULL) &&
	     (tpm_state->transportHandle != transportInternal->transHandle)) ||

	    /* invalidate if the SHA1 ordinal is not within a transport session and the session was
	       set up with a transport session */
	    ((transportInternal == NULL) &&
	     (tpm_state->transportHandle != 0))
	    
	    ) {

	    printf("TPM_Check_SHA1Context: Invalidating SHA1 context\n");
	    TPM_SHA1Delete(&(tpm_state->sha1_context));
	}
    }
    return rc;
}

/* TPM_GetInParamDigest() does common processing of input parameters.
   
   Common processing includes:

   - determining if the ordinal is being run within an encrypted transport session, since the
     inParamDigest does not have to be calculated for audit in that case.

   - retrieving the audit status.  It is determinant of whether the input parameter digest should be
     calculated.

   - calculating the input parameter digest for HMAC authorization and/or auditing

   This function is called before authorization for several reasons.

   1 - It makes ordinal processing code more uniform, since authorization sometimes occurs far into
   the actions.

   2 - It is a minor optimization, since the resulting inParamDigest can be used twice in an auth-2
   command, as well as extending the audit digest.
*/
   
TPM_RESULT TPM_GetInParamDigest(TPM_DIGEST inParamDigest,		/* output */
				TPM_BOOL *auditStatus,			/* output */
				TPM_BOOL *transportEncrypt,		/* output */
				tpm_state_t *tpm_state,
				TPM_TAG tag,
				TPM_COMMAND_CODE ordinal,
				unsigned char *inParamStart,
				unsigned char *inParamEnd,
				TPM_TRANSPORT_INTERNAL *transportInternal)
{
    TPM_RESULT		rc = 0;			/* this function return code */ 
    TPM_COMMAND_CODE	nOrdinal;		/* ordinal in network byte order */
    
    printf(" TPM_GetInParamDigest:\n");
    if (rc == 0) {
	/* TRUE if called from encrypted transport session.  This is currently only needed when
	   auditing, but it's safer to always initialize it */
	*transportEncrypt = 
	    (transportInternal != NULL) &&	
	    (transportInternal->transPublic.transAttributes & TPM_TRANSPORT_ENCRYPT);
	printf("  TPM_GetInParamDigest: transportEncrypt %02x\n", *transportEncrypt);
	/* Determine if the ordinal should be audited. */
	rc = TPM_OrdinalAuditStatus_GetAuditStatus(auditStatus,
						   ordinal,
						   &(tpm_state->tpm_permanent_data));
    }
    /* If inParamDigest is needed for:

       1 - for auditing (auditStatus == TRUE) and not called from an encrypted transport.  Different
       parameters are audited if the ordinal is called through an encrypted transport session.

       2 - for authorization (tag != auth-0)
    */
    if (rc == 0) {
	if ((*auditStatus && !(*transportEncrypt))	||	/* digest for auditing */
	    (tag != TPM_TAG_RQU_COMMAND)) {		/* digest for authorization */

	    /* convert ordinal to network byte order */
	    nOrdinal = htonl(ordinal);

	    /* a. Create inParamDigest - digest of inputs above the double line.  NOTE: If there
	       are no inputs other than the ordinal, inParamEnd - inParamStart will be 0,
	       terminating the SHA1 vararg hash.  It is important that the termination condition
	       be the length and not the NULL pointer. */
	    rc = TPM_SHA1(inParamDigest,
			  sizeof(TPM_COMMAND_CODE), &nOrdinal,	   /* 1S */
			  inParamEnd - inParamStart, inParamStart, /* 2S - ... */
			  0, NULL);
	    if (rc == 0) {
		TPM_PrintFour("  TPM_GetInParamDigest: inParamDigest", inParamDigest);
	    }
	}
    }
    return rc;
}

/* TPM_GetOutParamDigest() does common processing of output parameters.
   
   It calculates the output parameter digest for HMAC generation and/or auditing if required.	
*/	

TPM_RESULT TPM_GetOutParamDigest(TPM_DIGEST outParamDigest,	/* output */
				 TPM_BOOL auditStatus,		/* input audit status */
				 TPM_BOOL transportEncrypt,	/* wrapped in encrypt transport */
				 TPM_TAG tag,			
				 TPM_RESULT returnCode,
				 TPM_COMMAND_CODE ordinal,	/* command ordinal (hbo) */
				 unsigned char *outParamStart,	/* starting point of param's */
				 uint32_t outParamLength)	/* length of param's */
{
    TPM_RESULT		rc = 0; 
    TPM_RESULT		nreturnCode;	/* returnCode in nbo */
    TPM_COMMAND_CODE	nOrdinal;	/* ordinal in network byte order */
    
    printf(" TPM_GetOutParamDigest:\n");
    if (rc == 0)	{
	if ((auditStatus && !transportEncrypt) || (tag != TPM_TAG_RQU_COMMAND)) {
	    nreturnCode = htonl(returnCode);
	    nOrdinal = htonl(ordinal);	
	    /* a. Create outParamDigest - digest of outputs above the double line.  NOTE: If there
	       are no outputs other than the returnCode and ordinal, outParamLength
	       will be 0, terminating the SHA1 vararg hash.  It is important that the termination
	       condition be the length and not the NULL pointer. */
	    rc = TPM_SHA1(outParamDigest,
			  sizeof(TPM_RESULT), &nreturnCode,		/* 1S */
			  sizeof(TPM_COMMAND_CODE), &nOrdinal,		/* 2S */
			  outParamLength, outParamStart,		/* 3S - ...*/
			  0, NULL);
	    if (rc == 0) {
		TPM_PrintFour("  TPM_GetOutParamDigest: outParamDigest", outParamDigest);
	    }
	}	
    }	
    return rc;	
}	

/* TPM_ProcessAudit() rev 109

   This function is called when command auditing is required.
   
   This function must be called after the output authorization, since it requires the (almost) final
   return code.
*/
   
TPM_RESULT TPM_ProcessAudit(tpm_state_t *tpm_state,
			    TPM_BOOL transportEncrypt,	/* wrapped in encrypt transport */
			    TPM_DIGEST inParamDigest,
			    TPM_DIGEST outParamDigest,
			    TPM_COMMAND_CODE ordinal)
{
    TPM_RESULT		rc = 0;			/* audit return code */
    TPM_BOOL		isZero;
    TPM_RESULT		nreturnCode;		/* returnCode in nbo */
    TPM_COMMAND_CODE	nOrdinal;		/* ordinal in network byte order */
    TPM_DIGEST		transportDigest;	/* special case digest in encrypted transport */
    
    printf(" TPM_ProcessAudit:\n");

    /* The TPM will execute the ordinal and perform auditing in the following manner: */
    /* 1. Execute command */
    /* a. Execution implies the performance of the listed actions for the ordinal. */
    /* 2. If the command will return TPM_SUCCESS */
    /* a. If TPM_STANY_DATA -> auditDigest is all zeros */
    if (rc == 0) {
	TPM_Digest_IsZero(&isZero, tpm_state->tpm_stclear_data.auditDigest);
	if (isZero) {
	    /* i. Increment TPM_PERMANENT_DATA -> auditMonotonicCounter by 1 */
	    tpm_state->tpm_permanent_data.auditMonotonicCounter.counter++;
	    printf("  TPM_ProcessAudit: Incrementing auditMonotonicCounter to %u\n",
		   tpm_state->tpm_permanent_data.auditMonotonicCounter.counter);
	    rc = TPM_PermanentAll_NVStore(tpm_state,
					  TRUE,		/* write NV */
					  0);		/* no roll back */
	}
    }
    /* b. Create A1 a TPM_AUDIT_EVENT_IN structure */
    /* i. Set A1 -> inputParms to the digest of the input parameters from the command */
    /* (1) Digest value according to the HMAC digest rules of the "above the line" parameters
       (i.e. the first HMAC digest calculation). */
    /* ii. Set A1 -> auditCount to TPM_PERMANENT_DATA -> auditMonotonicCounter */
    /* c. Set TPM_STANY_DATA -> auditDigest to SHA-1 (TPM_STANY_DATA -> auditDigest || A1) */
    if (rc == 0) {
	/* normal case, audit uses inParamDigest */
	if (!transportEncrypt) {
	    rc = TPM_AuditDigest_ExtendIn(tpm_state, inParamDigest);
	}
	/* 1. When the wrapped command requires auditing and the transport session specifies
	   encryption, the TPM MUST perform the audit. However, when computing the audit digest:
	*/
	else {
	    /* a. For input, only the ordinal is audited. */
	    if (rc == 0) {
		nOrdinal = htonl(ordinal);
		rc = TPM_SHA1(transportDigest,
			      sizeof(TPM_COMMAND_CODE), &nOrdinal,
			      0, NULL);
	    }
	    if (rc == 0) {
		rc = TPM_AuditDigest_ExtendIn(tpm_state, transportDigest);
	    }
	}
    }
    /* d. Create A2 a TPM_AUDIT_EVENT_OUT structure */
    /* i. Set A2 -> outputParms to the digest of the output parameters from the command */
    /* (1). Digest value according to the HMAC digest rules of the "above the line" parameters
       (i.e. the first HMAC digest calculation). */
    /* ii. Set A2 -> auditCount to TPM_PERMANENT_DATA -> auditMonotonicCounter */
    /* e. Set TPM_STANY_DATA -> auditDigest to SHA-1 (TPM_STANY_DATA -> auditDigest || A2) */

    /* Audit Generation Corner cases 3.a. TPM_SaveState: Only the input parameters are audited, and
       the audit occurs before the state is saved.  If an error occurs while or after the state is
       saved, the audit still occurs.
    */
    if ((rc == 0) && (ordinal != TPM_ORD_SaveState)) {
	/* normal case, audit uses outParamDigest */
	if (!transportEncrypt) {
	    rc = TPM_AuditDigest_ExtendOut(tpm_state, outParamDigest);
	}
	/* 1. When the wrapped command requires auditing and the transport session specifies
	   encryption, the TPM MUST perform the audit. However, when computing the audit digest:
	*/
	else {
	    /* b. For output, only the ordinal and return code are audited. */
	    if (rc == 0) {
		nreturnCode = htonl(TPM_SUCCESS);	/* only called when TPM_SUCCESS */
		nOrdinal = htonl(ordinal);
		rc = TPM_SHA1(transportDigest,
			      sizeof(TPM_RESULT), &nreturnCode,
			      sizeof(TPM_COMMAND_CODE), &nOrdinal,
			      0, NULL);
	    }
	    if (rc == 0) {
		rc = TPM_AuditDigest_ExtendOut(tpm_state, transportDigest);
	    }
	}
    }
    /* 1. When, in performing the audit process, the TPM has an internal failure (unable to write,
       SHA-1 failure etc.) the TPM MUST set the internal TPM state such that the TPM returns the
       TPM_FAILEDSELFTEST error on subsequent attempts to execute a command. */
    /* 2. The return code for the command uses the following rules */
    /* a. Command result success, audit success -> return TPM_SUCCESS */
    /* b. Command result failure, no audit -> return command result failure */
    /* c. Command result success, audit failure -> return TPM_AUDITFAIL_SUCCESSFUL */
    /* 3. If the TPM is permanently nonrecoverable after an audit failure, then the TPM MUST always
       return TPM_FAILEDSELFTEST for every command other than TPM_GetTestResult.  This state must
       persist regardless of power cycling, the execution of TPM_Init or any other actions. */
    if (rc != 0) {
	rc = TPM_AUDITFAIL_SUCCESSFUL;
	tpm_state->testState = TPM_TEST_STATE_FAILURE;
    }
    return rc;
}

/*
  Processing Functions
*/

/* 7.1 TPM_GetCapability rev 99

   This command returns current information regarding the TPM.

   The limitation on what can be returned in failure mode restricts the information a manufacturer
   may return when capArea indicates TPM_CAP_MFR.
*/

TPM_RESULT TPM_Process_GetCapability(tpm_state_t *tpm_state,
				     TPM_STORE_BUFFER *response,
				     TPM_TAG tag,
				     uint32_t paramSize,
				     TPM_COMMAND_CODE ordinal,
				     unsigned char *command,
				     TPM_TRANSPORT_INTERNAL *transportInternal)
{
    TPM_RESULT	rcf = 0;		/* fatal error precluding response */
    TPM_TAG	returnCode = 0;		/* command return code */

    /* input parameters */
    TPM_CAPABILITY_AREA capArea;	/* Partition of capabilities to be interrogated */
    TPM_SIZED_BUFFER	subCap;		/* Further definition of information */

    /* processing parameters */
    unsigned char *	inParamStart;		/* starting point of inParam's */
    unsigned char *	inParamEnd;		/* ending point of inParam's */
    TPM_DIGEST		inParamDigest;
    TPM_BOOL		auditStatus;		/* audit the ordinal */
    TPM_BOOL		transportEncrypt = FALSE;/* wrapped in encrypted transport session */
    uint16_t		subCap16 = 0;		/* the subCap as a uint16_t */
    uint32_t		subCap32 = 0;		/* the subCap as a uint32_t */
    TPM_STORE_BUFFER	capabilityResponse;	/* response */

    /* output parameters */
    uint32_t		outParamStart;	/* starting point of outParam's */
    uint32_t		outParamEnd;	/* ending point of outParam's */
    TPM_DIGEST		outParamDigest;
    
    printf("TPM_Process_GetCapability: Ordinal Entry\n");
    TPM_SizedBuffer_Init(&subCap);		/* freed @1 */
    TPM_Sbuffer_Init(&capabilityResponse);	/* freed @2 */
    /*
      get inputs
    */
    /* save the starting point of inParam's for authorization and auditing */
    inParamStart = command;
    /* get capArea parameter */
    if (returnCode == TPM_SUCCESS) {
	returnCode = TPM_Load32(&capArea, &command, &paramSize);
    }
    /* get subCap parameter */
    if (returnCode == TPM_SUCCESS) {
	printf("TPM_Process_GetCapability: capArea %08x\n", capArea);
	returnCode = TPM_SizedBuffer_Load(&subCap, &command, &paramSize);
    }
    /* subCap is often a uint16_t or uint32_t, create them now */
    if (returnCode == TPM_SUCCESS) {
	TPM_GetSubCapInt(&subCap16, &subCap32, &subCap);
    }
    /* save the ending point of inParam's for authorization and auditing */
    inParamEnd = command;
    /* digest the input parameters */
    if (returnCode == TPM_SUCCESS) {
	returnCode = TPM_GetInParamDigest(inParamDigest,	/* output */
					  &auditStatus,		/* output */
					  &transportEncrypt,	/* output */
					  tpm_state,
					  tag,
					  ordinal,
					  inParamStart,
					  inParamEnd,
					  transportInternal);
    }
    /* check state */
    /* The shutdown test is delayed until after the subcap is calculated */
    if (returnCode == TPM_SUCCESS) {
	returnCode = TPM_CheckState(tpm_state, tag, TPM_CHECK_NO_LOCKOUT);
    }
    /* check tag */
    if (returnCode == TPM_SUCCESS) {
	returnCode = TPM_CheckRequestTag0(tag);
    }
    if (returnCode == TPM_SUCCESS) {
	if (paramSize != 0) {
	    printf("TPM_Process_GetCapability: Error, command has %u extra bytes\n",
		   paramSize);
	    returnCode = TPM_BAD_PARAM_SIZE;
	}
    }
    /*
      check state
    */
    /* 1. The TPM validates the capArea and subCap indicators. If the information is available, the
	  TPM creates the response field and fills in the actual information. */
    /* 2. The structure document contains the list of caparea and subCap values */
    if (returnCode == TPM_SUCCESS) {
	/* 3. If the TPM is in failure mode or limited operation mode, the TPM MUST return */
	if ((tpm_state->testState == TPM_TEST_STATE_FAILURE) ||
	    (tpm_state->testState == TPM_TEST_STATE_LIMITED)) {
	    /* a. TPM_CAP_VERSION */
	    /* b. TPM_CAP_VERSION_VAL */
	    /* c. TPM_CAP_MFR */
	    /* d. TPM_CAP_PROPERTY -> TPM_CAP_PROP_MANUFACTURER */
	    /* e. TPM_CAP_PROPERTY -> TPM_CAP_PROP_DURATION */
	    /* f. TPM_CAP_PROPERTY -> TPM_CAP_PROP_TIS_TIMEOUT */
	    /* g. The TPM MAY return any other capability. */
	    if (
		!(capArea == TPM_CAP_VERSION) &&
		!(capArea == TPM_CAP_VERSION_VAL) &&
		!(capArea == TPM_CAP_MFR) &&
		!((capArea == TPM_CAP_PROPERTY) && (subCap32 == TPM_CAP_PROP_MANUFACTURER)) &&
		!((capArea == TPM_CAP_PROPERTY) && (subCap32 == TPM_CAP_PROP_DURATION)) &&
		!((capArea == TPM_CAP_PROPERTY) && (subCap32 == TPM_CAP_PROP_TIS_TIMEOUT))
		) {
		if (tpm_state->testState == TPM_TEST_STATE_FAILURE)  {
		    printf("TPM_Process_GetCapability: Error, shutdown capArea %08x subCap %08x\n",
			   capArea, subCap32);
		    returnCode = TPM_FAILEDSELFTEST;
		}
		else {
		    printf("TPM_Process_GetCapability: Limited operation, run self-test\n");
		    returnCode = TPM_ContinueSelfTestCmd(tpm_state);
		}
	    }
	}
    }
    /*
      Processing
    */
    if (returnCode == TPM_SUCCESS) {
	printf("TPM_Process_GetCapability: capArea %08x subCap32 subCap16 %08x %04x\n",
	       capArea, subCap32, subCap16);
	returnCode = TPM_GetCapabilityCommon(&capabilityResponse, tpm_state,
					     capArea, subCap16, subCap32, &subCap);
    }
    /*
      response
    */
    if (rcf == 0) {
	printf("TPM_Process_GetCapability: Ordinal returnCode %08x %u\n",
	       returnCode, returnCode);
	rcf = TPM_Sbuffer_StoreInitialResponse(response, tag, returnCode);
    }
    if (rcf == 0) {
	if (returnCode == TPM_SUCCESS) {
	    /* checkpoint the beginning of the outParam's */
	    outParamStart = response->buffer_current - response->buffer;
	    /* store the capabilityResponse */
	    returnCode = TPM_Sbuffer_AppendAsSizedBuffer(response, &capabilityResponse);
	    /* checkpoint the end of the outParam's */
	    outParamEnd = response->buffer_current - response->buffer;
	}
	/* digest the above the line output parameters */
	if (returnCode == TPM_SUCCESS) {
	    returnCode = TPM_GetOutParamDigest(outParamDigest,	/* output */
					       auditStatus,	/* input audit status */
					       transportEncrypt,
					       tag,			
					       returnCode,
					       ordinal,		/* command ordinal */
					       response->buffer + outParamStart,	/* start */
					       outParamEnd - outParamStart);	/* length */
	}
	/* audit if required */
	if ((returnCode == TPM_SUCCESS) && auditStatus) {
	    returnCode = TPM_ProcessAudit(tpm_state,
					  transportEncrypt,
					  inParamDigest,
					  outParamDigest,
					  ordinal);
	}
	/* adjust the initial response */
	rcf = TPM_Sbuffer_StoreFinalResponse(response, returnCode, tpm_state);
    }
    /*
      cleanup
    */
    TPM_SizedBuffer_Delete(&subCap);		/* @1 */
    TPM_Sbuffer_Delete(&capabilityResponse);	/* @2 */
    return rcf;
}

/* TPM_GetSubCapInt() converts from a TPM_SIZED_BUFFER to either a uint16_t or uint32_t as
   applicable

   No return code is needed.  If the size it not applicable, a 0 value is returned, which is
   (fortunately) always illegal for subCap integral values.
*/

void TPM_GetSubCapInt(uint16_t *subCap16,
		      uint32_t *subCap32,
		      TPM_SIZED_BUFFER *subCap)
{
    *subCap16 = 0;	/* default, means was not a uint16_t */
    *subCap32 = 0;	/* default, means was not a uint32_t */
    if (subCap->size == sizeof(uint32_t)) {
	*subCap32 = htonl(*(uint32_t *)subCap->buffer);
	printf(" TPM_GetSubCapInt: subCap %08x\n", *subCap32);
    }
    else if (subCap->size == sizeof(uint16_t)) {
	*subCap16 = htons(*(uint16_t *)subCap->buffer);
	printf(" TPM_GetSubCapInt: subCap %04x\n", *subCap16);
    }
}


/* TPM_GetCapabilityCommon() is common code for getting a capability.
   
   It loads the result to 'capabilityResponse'

   A previously called TPM_GetSubCapInt() converts the subCap buffer into a subCap16 if the size is
   2 or subCap32 if the size is 4.  If the values are used, this function checks the size to ensure
   that the incoming subCap parameter was correct for the capArea.
*/

TPM_RESULT TPM_GetCapabilityCommon(TPM_STORE_BUFFER *capabilityResponse,
				   tpm_state_t *tpm_state, 
				   TPM_CAPABILITY_AREA capArea, 
				   uint16_t subCap16, 
				   uint32_t subCap32,
				   TPM_SIZED_BUFFER *subCap)
			     
{
    TPM_RESULT rc = 0;

    printf(" TPM_GetCapabilityCommon: capArea %08x\n", capArea);
    switch (capArea) {
      case TPM_CAP_ORD:
	if (subCap->size == sizeof(uint32_t)) {
	    rc = TPM_GetCapability_CapOrd(capabilityResponse, subCap32);
	}
	else {
	    printf("TPM_GetCapabilityCommon: Error, Bad subCap size %u\n", subCap->size);
	    rc = TPM_BAD_MODE;
	}
	break;
      case TPM_CAP_ALG:
	if (subCap->size == sizeof(uint32_t)) {
	    rc = TPM_GetCapability_CapAlg(capabilityResponse, subCap32);
	}
	else {
	    printf("TPM_GetCapabilityCommon: Error, Bad subCap size %u\n", subCap->size);
	    rc = TPM_BAD_MODE;
	}
	break;
      case TPM_CAP_PID:
	if (subCap->size == sizeof(uint16_t)) {
	    rc = TPM_GetCapability_CapPid(capabilityResponse, subCap16);
	}
	else {
	    printf("TPM_GetCapabilityCommon: Error, Bad subCap size %u\n", subCap->size);
	    rc = TPM_BAD_MODE;
	}
	break;
      case TPM_CAP_FLAG: 
	if (subCap->size == sizeof(uint32_t)) {
	    rc = TPM_GetCapability_CapFlag(capabilityResponse, tpm_state, subCap32);
	}
	else {
	    printf("TPM_GetCapabilityCommon: Error, Bad subCap size %u\n", subCap->size);
	    rc = TPM_BAD_MODE;
	}
	break;
      case TPM_CAP_PROPERTY: 
	if (subCap->size == sizeof(uint32_t)) {
	    rc = TPM_GetCapability_CapProperty(capabilityResponse, tpm_state, subCap32);
	}
	else {
	    printf("TPM_GetCapabilityCommon: Error, Bad subCap size %u\n", subCap->size);
	    rc = TPM_BAD_MODE;
	}
	break;
      case TPM_CAP_VERSION: 
	rc = TPM_GetCapability_CapVersion(capabilityResponse);
	break;
      case TPM_CAP_KEY_HANDLE:
	/* This is command is available for backwards compatibility. It is the same as
	   TPM_CAP_HANDLE with a resource type of keys. */
	rc = TPM_KeyHandleEntries_StoreHandles(capabilityResponse,
					       tpm_state->tpm_key_handle_entries);
	break;
      case TPM_CAP_CHECK_LOADED: 
	rc = TPM_GetCapability_CapCheckLoaded(capabilityResponse,
					      tpm_state->tpm_key_handle_entries,
					      subCap);
	break;
      case TPM_CAP_SYM_MODE:
	if (subCap->size == sizeof(uint32_t)) {
	    rc = TPM_GetCapability_CapSymMode(capabilityResponse, subCap32);
	}
	else {
	    printf("TPM_GetCapabilityCommon: Error, Bad subCap size %u\n", subCap->size);
	    rc = TPM_BAD_MODE;
	}
	break;
      case TPM_CAP_KEY_STATUS: 
	if (subCap->size == sizeof(uint32_t)) {
	    rc = TPM_GetCapability_CapKeyStatus(capabilityResponse,
						tpm_state->tpm_key_handle_entries,
						subCap32);
	}
	else {
	    printf("TPM_GetCapabilityCommon: Error, Bad subCap size %u\n", subCap->size);
	    rc = TPM_BAD_MODE;
	}
	break;
      case TPM_CAP_NV_LIST:
	rc = TPM_NVIndexEntries_GetNVList(capabilityResponse, &(tpm_state->tpm_nv_index_entries));
	break;
      case TPM_CAP_MFR:
	rc = TPM_GetCapability_CapMfr(capabilityResponse, tpm_state, subCap);
	break;
      case TPM_CAP_NV_INDEX: 
	if (subCap->size == sizeof(uint32_t)) {
	    rc = TPM_GetCapability_CapNVIndex(capabilityResponse, tpm_state, subCap32);
	}
	else {
	    printf("TPM_GetCapabilityCommon: Error, Bad subCap size %u\n", subCap->size);
	    rc = TPM_BAD_MODE;
	}
	break;
      case TPM_CAP_TRANS_ALG:
	if (subCap->size == sizeof(uint32_t)) {
	    rc = TPM_GetCapability_CapTransAlg(capabilityResponse, subCap32);
	}
	else {
	    printf("TPM_GetCapabilityCommon: Error, Bad subCap size %u\n", subCap->size);
	    rc = TPM_BAD_MODE;
	}
	break;
      case TPM_CAP_HANDLE:
	if (subCap->size == sizeof(uint32_t)) {
	    rc = TPM_GetCapability_CapHandle(capabilityResponse, tpm_state, subCap32);
	}
	else {
	    printf("TPM_GetCapabilityCommon: Error, Bad subCap size %u\n", subCap->size);
	    rc = TPM_BAD_MODE;
	}
	break;
      case TPM_CAP_TRANS_ES:
	if (subCap->size == sizeof(uint16_t)) {
	    rc = TPM_GetCapability_CapTransEs(capabilityResponse, subCap16);
	}
	else {
	    printf("TPM_GetCapabilityCommon: Error, Bad subCap size %u\n", subCap->size);
	    rc = TPM_BAD_MODE;
	}
	break;
      case TPM_CAP_AUTH_ENCRYPT:
	if (subCap->size == sizeof(uint32_t)) {
	    rc = TPM_GetCapability_CapAuthEncrypt(capabilityResponse, subCap32);
	}
	else {
	    printf("TPM_GetCapabilityCommon: Error, Bad subCap size %u\n", subCap->size);
	    rc = TPM_BAD_MODE;
	}
	break;
      case TPM_CAP_SELECT_SIZE:
	rc = TPM_GetCapability_CapSelectSize(capabilityResponse, subCap);
	break;
#if  (TPM_REVISION >= 103)	/* added for rev 103 */
      case TPM_CAP_DA_LOGIC:
	rc = TPM_GetCapability_CapDaLogic(capabilityResponse, subCap, tpm_state);
	break;
#endif
      case TPM_CAP_VERSION_VAL:
	rc = TPM_GetCapability_CapVersionVal(capabilityResponse,
					     &(tpm_state->tpm_permanent_data));
	break;
      default:
	printf("TPM_GetCapabilityCommon: Error, unsupported capArea %08x", capArea);
	rc = TPM_BAD_MODE;
	break;
    }
    return rc;
}

/* Boolean value.

   TRUE indicates that the TPM supports the ordinal.

   FALSE indicates that the TPM does not support the ordinal.
*/

static TPM_RESULT TPM_GetCapability_CapOrd(TPM_STORE_BUFFER *capabilityResponse,
					   uint32_t ordinal)
{
    TPM_RESULT			rc = 0;
    tpm_process_function_t	tpm_process_function;
    TPM_BOOL			supported;

    TPM_OrdinalTable_GetProcessFunction(&tpm_process_function, tpm_ordinal_table, ordinal);
    /* determine of the ordinal is supported */
    if (tpm_process_function != TPM_Process_Unused) {
	supported = TRUE;
    }
    /* if the processing function is 'Unused', it's not supported */
    else {
	supported = FALSE;
    }	
    printf("  TPM_GetCapability_CapOrd: Ordinal %08x, result %02x\n",
	   ordinal, supported);
    rc = TPM_Sbuffer_Append(capabilityResponse, &supported, sizeof(TPM_BOOL));
    return rc;
}

/* algorithmID is TPM_ALG_XX: A value from TPM_ALGORITHM_ID

   Boolean value. TRUE means that the TPM supports the asymmetric algorithm for TPM_Sign, TPM_Seal,
   TPM_UnSeal and TPM_UnBind and related commands. FALSE indicates that the asymmetric algorithm is
   not supported for these types of commands. The TPM MAY return TRUE or FALSE for other than
   asymmetric algorithms that it supports. Unassigned and unsupported algorithm IDs return FALSE.
*/

static TPM_RESULT TPM_GetCapability_CapAlg(TPM_STORE_BUFFER *capabilityResponse,
					   uint32_t algorithmID)
{
    TPM_RESULT	rc = 0;
    TPM_BOOL	supported;

    printf(" TPM_GetCapability_CapAlg: algorithmID %08x\n", algorithmID);
    if (algorithmID == TPM_ALG_RSA) {
	supported = TRUE;
    }
    else {
	supported = FALSE;
    }
    printf("  TPM_GetCapability_CapAlg: Result %08x\n", supported);
    rc = TPM_Sbuffer_Append(capabilityResponse, &supported, sizeof(TPM_BOOL));
    return rc;
}    

/* Boolean value.

   TRUE indicates that the TPM supports the protocol,

   FALSE indicates that the TPM does not support the protocol.
*/

static TPM_RESULT TPM_GetCapability_CapPid(TPM_STORE_BUFFER *capabilityResponse,
					   uint16_t protocolID)
{
    TPM_RESULT	rc = 0;
    TPM_BOOL	supported;

    printf(" TPM_GetCapability_CapPid: protocolID %04hx\n", protocolID);
    switch (protocolID) {
	/* supported protocols */
      case TPM_PID_OIAP:
      case TPM_PID_OSAP:
      case TPM_PID_ADIP:
      case TPM_PID_ADCP:
      case TPM_PID_DSAP:
      case TPM_PID_TRANSPORT:
      case TPM_PID_OWNER:
	supported = TRUE;
	break;
	/* unsupported protocols */
      default:
	supported = FALSE;
	break;
    }	
    printf("  TPM_GetCapability_CapPid: Result %08x\n", supported);
    rc = TPM_Sbuffer_Append(capabilityResponse, &supported, sizeof(TPM_BOOL));
    return rc;
}    

/*
  Either of the next two subcaps

  TPM_CAP_FLAG_PERMANENT  Return the TPM_PERMANENT_FLAGS structure

  TPM_CAP_FLAG_VOLATILE	 Return the TPM_STCLEAR_FLAGS structure
*/

static TPM_RESULT TPM_GetCapability_CapFlag(TPM_STORE_BUFFER *capabilityResponse,
					    tpm_state_t *tpm_state,
					    uint32_t capFlag)
{
    TPM_RESULT	rc = 0;

    printf(" TPM_GetCapability_CapFlag: capFlag %08x\n", capFlag);
    switch (capFlag) {
      case TPM_CAP_FLAG_PERMANENT:
	printf("  TPM_GetCapability_CapFlag: TPM_CAP_FLAG_PERMANENT\n");;
	rc = TPM_PermanentFlags_StoreBytes(capabilityResponse, &(tpm_state->tpm_permanent_flags));
	break;
      case TPM_CAP_FLAG_VOLATILE:
	printf("  TPM_GetCapability_CapFlag: TPM_CAP_FLAG_VOLATILE\n");
	rc = TPM_StclearFlags_Store(capabilityResponse, &(tpm_state->tpm_stclear_flags));
	break;
      default:
	printf("TPM_GetCapability_CapFlag: Error, illegal capFlag %08x\n", capFlag);
	rc = TPM_BAD_MODE;
	break;
    }
    return rc;
}    

/* TPM_GetCapability_CapProperty() handles Subcap values for CAP_PROPERTY rev 100
 */

static TPM_RESULT TPM_GetCapability_CapProperty(TPM_STORE_BUFFER *capabilityResponse,
						tpm_state_t *tpm_state,
						uint32_t capProperty)
{
    TPM_RESULT	rc = 0;
    uint32_t 	uint32;
    uint32_t 	uint32a;
    uint32_t 	dummy;	/* to hold unused response parameter */

    printf(" TPM_GetCapability_CapProperty: capProperty %08x\n", capProperty);
    switch (capProperty) {
      case TPM_CAP_PROP_PCR:	/* Returns the number of PCR registers supported by the TPM */
	printf(" TPM_GetCapability_CapProperty: TPM_CAP_PROP_PCR %u\n", TPM_NUM_PCR);
	rc = TPM_Sbuffer_Append32(capabilityResponse, TPM_NUM_PCR);
	break;
      case TPM_CAP_PROP_DIR:	/* Returns the number of DIR registers under control of the TPM
				   owner supported by the TPM. */
	printf(" TPM_GetCapability_CapProperty: TPM_CAP_PROP_DIR %u\n", TPM_AUTHDIR_SIZE);
	rc = TPM_Sbuffer_Append32(capabilityResponse, TPM_AUTHDIR_SIZE);
	break;
      case TPM_CAP_PROP_MANUFACTURER:	/* Returns the Identifier of the TPM manufacturer. */
	printf(" TPM_GetCapability_CapProperty: TPM_CAP_PROP_MANUFACTURER %.4s\n",
	       TPM_MANUFACTURER);
	rc = TPM_Sbuffer_Append(capabilityResponse, (const unsigned char *)TPM_MANUFACTURER, 4);
	break;
      case TPM_CAP_PROP_KEYS:	/* Returns the number of 2048-bit RSA keys that can be loaded. This
				   MAY vary with time and circumstances. */
	TPM_KeyHandleEntries_GetSpace(&uint32, tpm_state->tpm_key_handle_entries);
	printf(" TPM_GetCapability_CapProperty: TPM_CAP_PROP_KEYS %u\n", uint32);
	rc = TPM_Sbuffer_Append32(capabilityResponse, uint32);
	break;
      case TPM_CAP_PROP_MIN_COUNTER: /* uint32_t. The minimum amount of time in 10ths of a second
					that must pass between invocations of incrementing the
					monotonic counter. */
	printf(" TPM_GetCapability_CapProperty: TPM_CAP_PROP_MIN_COUNTER\n");
	rc = TPM_Sbuffer_Append32(capabilityResponse, 0);
	break;
      case TPM_CAP_PROP_AUTHSESS:	/* The number of available authorization sessions. This MAY
					   vary with time and circumstances. */
	TPM_AuthSessions_GetSpace(&uint32, tpm_state->tpm_stclear_data.authSessions);
	printf(" TPM_GetCapability_CapProperty: TPM_CAP_PROP_AUTHSESS space %u\n", uint32);
	rc = TPM_Sbuffer_Append32(capabilityResponse, uint32);
	break;
      case TPM_CAP_PROP_TRANSESS:	/* The number of available transport sessions. This MAY vary
					   with time and circumstances.	 */
	TPM_TransportSessions_GetSpace(&uint32, tpm_state->tpm_stclear_data.transSessions);
	printf(" TPM_GetCapability_CapProperty: TPM_CAP_PROP_TRANSESS space %u\n", uint32);
	rc = TPM_Sbuffer_Append32(capabilityResponse, uint32);
	break;
      case TPM_CAP_PROP_COUNTERS:	/* The number of available monotonic counters. This MAY vary
					   with time and circumstances. */
	TPM_Counters_GetSpace(&uint32, tpm_state->tpm_permanent_data.monotonicCounter);
	printf(" TPM_GetCapability_CapProperty: TPM_CAP_PROP_COUNTERS %u\n", uint32);
	rc = TPM_Sbuffer_Append32(capabilityResponse, uint32);
	break;
      case TPM_CAP_PROP_MAX_AUTHSESS:	/* The maximum number of loaded authorization sessions the
					   TPM supports. */
	printf(" TPM_GetCapability_CapProperty: TPM_CAP_PROP_MAX_AUTHSESS %u\n",
	       TPM_MIN_AUTH_SESSIONS);
	rc = TPM_Sbuffer_Append32(capabilityResponse, TPM_MIN_AUTH_SESSIONS);
	break;
      case TPM_CAP_PROP_MAX_TRANSESS:	/* The maximum number of loaded transport sessions the TPM
					   supports. */
	printf(" TPM_GetCapability_CapProperty: TPM_CAP_PROP_MAX_TRANSESS %u\n",
	       TPM_MIN_TRANS_SESSIONS);
	rc = TPM_Sbuffer_Append32(capabilityResponse, TPM_MIN_TRANS_SESSIONS);
	break;
      case TPM_CAP_PROP_MAX_COUNTERS:	/* The maximum number of monotonic counters under control of
					   TPM_CreateCounter */
	printf(" TPM_GetCapability_CapProperty: TPM_CAP_PROP_MAX_COUNTERS %u\n",
	       TPM_MIN_COUNTERS);
	rc = TPM_Sbuffer_Append32(capabilityResponse, TPM_MIN_COUNTERS);
	break;
      case TPM_CAP_PROP_MAX_KEYS:	/* The maximum number of 2048 RSA keys that the TPM can
					   support. The number does not include the EK or SRK. */
	printf(" TPM_GetCapability_CapProperty: TPM_CAP_PROP_MAX_KEYS %u\n", TPM_KEY_HANDLES);
	rc = TPM_Sbuffer_Append32(capabilityResponse, TPM_KEY_HANDLES);
	break;
      case TPM_CAP_PROP_OWNER:	/* A value of TRUE indicates that the TPM has successfully installed
				   an owner. */
	printf(" TPM_GetCapability_CapProperty: TPM_CAP_PROP_OWNER %02x\n",
	       tpm_state->tpm_permanent_data.ownerInstalled);
	rc = TPM_Sbuffer_Append(capabilityResponse,
				&(tpm_state->tpm_permanent_data.ownerInstalled), sizeof(TPM_BOOL));
	break;
      case TPM_CAP_PROP_CONTEXT:	/* The number of available saved session slots. This MAY
					   vary with time and circumstances. */
	TPM_ContextList_GetSpace(&uint32, &dummy, tpm_state->tpm_stclear_data.contextList);
	printf(" TPM_GetCapability_CapProperty: TPM_CAP_PROP_CONTEXT %u\n", uint32);
	rc = TPM_Sbuffer_Append32(capabilityResponse, uint32);
	break;
      case TPM_CAP_PROP_MAX_CONTEXT:	/* The maximum number of saved session slots. */
	printf(" TPM_GetCapability_CapProperty: TPM_CAP_PROP_MAX_CONTEXT %u\n",
	       TPM_MIN_SESSION_LIST);
	rc = TPM_Sbuffer_Append32(capabilityResponse, TPM_MIN_SESSION_LIST);
	break;
      case TPM_CAP_PROP_FAMILYROWS:	/* The number of rows in the family table */
	printf(" TPM_GetCapability_CapProperty: TPM_CAP_PROP_FAMILYROWS %u\n",
	       TPM_NUM_FAMILY_TABLE_ENTRY_MIN);
	rc = TPM_Sbuffer_Append32(capabilityResponse, TPM_NUM_FAMILY_TABLE_ENTRY_MIN);
	break;
      case TPM_CAP_PROP_TIS_TIMEOUT:
	printf(" TPM_GetCapability_CapProperty: TPM_CAP_PROP_TIS_TIMEOUT\n");
	rc = TPM_GetCapability_CapPropTisTimeout(capabilityResponse);
	break;
      case TPM_CAP_PROP_STARTUP_EFFECT: /* The TPM_STARTUP_EFFECTS structure */
	printf(" TPM_GetCapability_CapProperty: TPM_CAP_PROP_STARTUP_EFFECT %08x\n",
	       TPM_STARTUP_EFFECTS_VALUE);
	rc = TPM_Sbuffer_Append32(capabilityResponse, TPM_STARTUP_EFFECTS_VALUE);
	break;
      case TPM_CAP_PROP_DELEGATE_ROW:	/* The size of the delegate table in rows. */
	printf(" TPM_GetCapability_CapProperty: TPM_CAP_PROP_DELEGATE_ENTRIES %u\n",
	       TPM_NUM_DELEGATE_TABLE_ENTRY_MIN);
	rc = TPM_Sbuffer_Append32(capabilityResponse, TPM_NUM_DELEGATE_TABLE_ENTRY_MIN);
	break;
      case TPM_CAP_PROP_MAX_DAASESS:	/* The maximum number of loaded DAA sessions (join or sign)
					   that the TPM supports */
	printf(" TPM_GetCapability_CapProperty: TPM_CAP_PROP_DAA_MAX\n");
	rc = TPM_Sbuffer_Append32(capabilityResponse, TPM_MIN_DAA_SESSIONS);
	break;
      case TPM_CAP_PROP_DAASESS:	/* The number of available DAA sessions. This may vary with
					   time and circumstances */
	TPM_DaaSessions_GetSpace(&uint32, tpm_state->tpm_stclear_data.daaSessions);
	printf(" TPM_GetCapability_CapProperty: TPM_CAP_PROP_SESSION_DAA space %u\n", uint32);
	rc = TPM_Sbuffer_Append32(capabilityResponse, uint32);
	break;
      case TPM_CAP_PROP_CONTEXT_DIST:	/* The maximum distance between context count values. This
					   MUST be at least 2^16-1. */
	printf(" TPM_GetCapability_CapProperty: TPM_CAP_PROP_CONTEXT_DIST\n");
	rc = TPM_Sbuffer_Append32(capabilityResponse, 0xffffffff);
	break;
      case TPM_CAP_PROP_DAA_INTERRUPT:	/* BOOL. A value of TRUE indicates that the TPM will accept
					   ANY command while executing a DAA Join or Sign.

					   A value of FALSE indicates that the TPM will invalidate
					   the DAA Join or Sign upon the receipt of any command
					   other than the next join/sign in the session or a
					   TPM_SaveContext */
	printf(" TPM_GetCapability_CapProperty: TPM_CAP_PROP_DAA_INTERRUPT\n");
	rc = TPM_Sbuffer_Append8(capabilityResponse, TRUE);   
	break;
      case TPM_CAP_PROP_SESSIONS: /* UNIT32. The number of available authorization and transport
				     sessions from the pool. This may vary with time and
				     circumstances. */
	TPM_AuthSessions_GetSpace(&uint32, tpm_state->tpm_stclear_data.authSessions);
	TPM_TransportSessions_GetSpace(&uint32a, tpm_state->tpm_stclear_data.transSessions);
	printf(" TPM_GetCapability_CapProperty: TPM_CAP_PROP_SESSIONS %u + %u\n", uint32, uint32a);
	rc = TPM_Sbuffer_Append32(capabilityResponse, uint32 + uint32a);
	break;
      case TPM_CAP_PROP_MAX_SESSIONS: /* uint32_t. The maximum number of sessions the
					 TPM supports. */
	printf(" TPM_GetCapability_CapProperty: TPM_CAP_PROP_MAX_SESSIONS\n");
	rc = TPM_Sbuffer_Append32(capabilityResponse,
				  TPM_MIN_AUTH_SESSIONS + TPM_MIN_TRANS_SESSIONS);
	break;
      case TPM_CAP_PROP_CMK_RESTRICTION: /* uint32_t TPM_Permanent_Data -> restrictDelegate */
	printf(" TPM_GetCapability_CapProperty: TPM_CAP_PROP_CMK_RESTRICTION %08x\n",
	       tpm_state->tpm_permanent_data.restrictDelegate);
	rc = TPM_Sbuffer_Append32(capabilityResponse,
				  tpm_state->tpm_permanent_data.restrictDelegate);
	break;
      case TPM_CAP_PROP_DURATION: 
	printf(" TPM_GetCapability_CapProperty: TPM_CAP_PROP_DURATION\n");
	rc = TPM_GetCapability_CapPropDuration(capabilityResponse);
	break;
      case TPM_CAP_PROP_ACTIVE_COUNTER: /* TPM_COUNT_ID. The id of the current counter. 0xff..ff if
					   no counter is active */
	TPM_Counters_GetActiveCounter(&uint32, tpm_state->tpm_stclear_data.countID);
	/* The illegal value after releasing an active counter must be mapped back to the null
	   value */
	if (uint32 == TPM_COUNT_ID_ILLEGAL) {
	    uint32 = TPM_COUNT_ID_NULL;
	}
	printf(" TPM_GetCapability_CapProperty: TPM_CAP_PROP_ACTIVE_COUNTER %u\n", uint32);
	rc = TPM_Sbuffer_Append32(capabilityResponse, uint32);
	break;
      case TPM_CAP_PROP_MAX_NV_AVAILABLE: /* uint32_t. Deprecated.  The maximum number of NV space
					     that can be allocated, MAY vary with time and
					     circumstances.  This capability was not implemented
					     consistently, and is replaced by
					     TPM_NV_INDEX_TRIAL.  */
	rc = TPM_NVIndexEntries_GetFreeSpace(&uint32, &(tpm_state->tpm_nv_index_entries));
	if (rc == 0) {
	    printf(" TPM_GetCapability_CapProperty: TPM_CAP_PROP_MAX_NV_AVAILABLE %u\n", uint32);
	    rc = TPM_Sbuffer_Append32(capabilityResponse, uint32);
	}
	/* There should always be free space >= 0.  If the call fails here, there is an internal
	   error. */
	else {
	    printf(" TPM_GetCapability_CapProperty: Error (fatal) "
		   "in TPM_CAP_PROP_MAX_NV_AVAILABLE\n");
	    rc = TPM_FAIL;
	}
	break;
      case TPM_CAP_PROP_INPUT_BUFFER: /* uint32_t. The size of the TPM input and output buffers in
					 bytes. */
	printf(" TPM_GetCapability_CapProperty: TPM_CAP_PROP_INPUT_BUFFER %u\n",
	       TPM_BUFFER_MAX);
	rc = TPM_Sbuffer_Append32(capabilityResponse, TPM_BUFFER_MAX);
	break;
     default:
	printf("TPM_GetCapability_CapProperty: Error, illegal capProperty %08x\n", capProperty);
	rc = TPM_BAD_MODE;
	break;
    }
    return rc;
}    

/* TPM_VERSION structure. The Major and Minor must indicate 1.1.

   The manufacturer information MUST indicate the firmware version of the TPM.

   Any software using this structure MUST be aware that when included in a structure the value MUST
   be 1.1.0.0, when reported by this command the manufacturer information MAY include firmware
   versions.  The use of this value is deprecated, new software SHOULD use TPM_CAP_VERSION_VAL to
   obtain version information regarding the TPM.
   
   Return 0.0 for revision for 1.1 backward compatibility, since TPM_PERMANENT_DATA now holds the
   new type TPM_VERSION_BYTE.
*/

static TPM_RESULT TPM_GetCapability_CapVersion(TPM_STORE_BUFFER *capabilityResponse)
{
    TPM_RESULT	rc = 0;
    TPM_STRUCT_VER	tpm_struct_ver;

    TPM_StructVer_Init(&tpm_struct_ver);
    printf(" TPM_GetCapability_CapVersion: %u.%u.%u.%u\n",
	   tpm_struct_ver.major, tpm_struct_ver.minor,
	   tpm_struct_ver.revMajor, tpm_struct_ver.revMinor);
    rc = TPM_StructVer_Store(capabilityResponse, &tpm_struct_ver);
    return rc;
}

/* A Boolean value.

   TRUE indicates that the TPM has enough memory available to load a key of the type specified by
   ALGORITHM.

   FALSE indicates that the TPM does not have enough memory.
*/

static TPM_RESULT TPM_GetCapability_CapCheckLoaded(TPM_STORE_BUFFER *capabilityResponse,
						   const TPM_KEY_HANDLE_ENTRY *tpm_key_handle_entry,
						   TPM_SIZED_BUFFER *subCap)
{
    TPM_RESULT		rc = 0;
    uint32_t		stream_size;
    unsigned char	*stream;
    TPM_KEY_PARMS	keyParms;
    TPM_BOOL		isSpace;
    uint32_t		index;

    TPM_KeyParms_Init(&keyParms);		/* freed @1 */
    if (rc == 0) {
	/* make temporary copies so the subCap is not touched */
	stream = subCap->buffer;
	stream_size = subCap->size;
	rc = TPM_KeyParms_Load(&keyParms, &stream, &stream_size);
    }
    if (rc == 0) {
	if (keyParms.algorithmID == TPM_ALG_RSA) {
	    TPM_KeyHandleEntries_IsSpace(&isSpace, &index, tpm_key_handle_entry);
	}
	else {
	    printf(" TPM_GetCapability_CapCheckLoaded: algorithmID %08x is not TPM_ALG_RSA %08x\n",
		   keyParms.algorithmID, TPM_ALG_RSA);
	    isSpace = FALSE;
	}
    }
    if (rc == 0) {
	printf(" TPM_GetCapability_CapCheckLoaded: Return %02x\n", isSpace);
	rc = TPM_Sbuffer_Append(capabilityResponse, &isSpace, sizeof(TPM_BOOL));
    }
    TPM_KeyParms_Delete(&keyParms);		/* @1 */
    return rc;
}

/* (Deprecated) This indicates the mode of a symmetric encryption. Mode is Electronic CookBook (ECB)
   or some other such mechanism.
*/

static TPM_RESULT TPM_GetCapability_CapSymMode(TPM_STORE_BUFFER *capabilityResponse,
					       TPM_SYM_MODE symMode)
{
    TPM_RESULT	rc = 0;
    
    symMode = symMode;	/* not currently used */
    printf(" TPM_GetCapability_CapSymMode: Return %02x\n", FALSE);
    rc = TPM_Sbuffer_Append8(capabilityResponse, FALSE);
    return rc;
}

/* Boolean value of ownerEvict. The handle MUST point to a valid key handle.
 */

static TPM_RESULT TPM_GetCapability_CapKeyStatus(TPM_STORE_BUFFER *capabilityResponse,
						 TPM_KEY_HANDLE_ENTRY *tpm_key_handle_entries,
						 uint32_t tpm_key_handle)
{
    TPM_RESULT			rc = 0;
    TPM_KEY_HANDLE_ENTRY	*tpm_key_handle_entry;	/* corresponding to handle */
    TPM_BOOL			ownerEvict;
    
    printf(" TPM_GetCapability_CapKeyStatus: key handle %08x\n", tpm_key_handle);
    /* map from the handle to the TPM_KEY structure */
    if (rc == 0) {
	rc = TPM_KeyHandleEntries_GetEntry(&tpm_key_handle_entry,
					   tpm_key_handle_entries,
					   tpm_key_handle);
	if (rc != 0) {
	    printf("TPM_GetCapability_CapKeyStatus: Error, key handle %08x not found\n",
		   tpm_key_handle);
	}
    }
    /* test the ownerEvict bit */
    if (rc == 0) {
	ownerEvict = (tpm_key_handle_entry->keyControl & TPM_KEY_CONTROL_OWNER_EVICT) ?
		     TRUE : FALSE;;
	printf(" TPM_GetCapability_CapKeyStatus: return %02x\n", ownerEvict);
	rc = TPM_Sbuffer_Append(capabilityResponse, &ownerEvict, sizeof(TPM_BOOL));
    }
    return rc;
}

/* Manufacturer specific. The manufacturer may provide any additional information regarding the TPM
   and the TPM state but MUST not expose any sensitive information.
*/

static TPM_RESULT TPM_GetCapability_CapMfr(TPM_STORE_BUFFER *capabilityResponse,
					   tpm_state_t *tpm_state,
					   TPM_SIZED_BUFFER *subCap)
{
    TPM_RESULT	rc = 0;
    uint32_t	subCap32;
    
    /* all of the subCaps are at least a uint32_t.  Some have more data */
    if (rc == 0) {
	if (subCap->size >= sizeof(uint32_t)) {
	    subCap32 = htonl(*(uint32_t *)subCap->buffer);
	    printf(" TPM_GetCapability_CapMfr: subCap %08x\n", subCap32);
	}
	else {
	    printf("TPM_GetCapability_CapMfr: Error, subCap size %u < %lu\n",
		   subCap->size, (unsigned long)sizeof(uint32_t));
	    rc = TPM_BAD_MODE;
	}
    }
    /* switch on the subCap and append the get capability response to the capabilityResponse
       buffer */
    if (rc == 0) {
	switch(subCap32) {
#ifdef TPM_POSIX
	  case TPM_CAP_PROCESS_ID:
	    if (subCap->size == sizeof(uint32_t)) {
		pid_t pid = getpid();
		printf(" TPM_GetCapability_CapMfr: TPM_CAP_PROCESS_ID %u\n", (uint32_t)pid);
		rc = TPM_Sbuffer_Append32(capabilityResponse, (uint32_t)pid);
	    }
	    else {
		printf("TPM_GetCapability_CapMfr: Error, Bad subCap size %u\n", subCap->size);
		rc = TPM_BAD_MODE;
	    }
	    break;
#endif
	  default:
	    capabilityResponse = capabilityResponse;	/* not used */
	    tpm_state = tpm_state;			/* not used */
	    printf("TPM_GetCapability_CapMfr: Error, unsupported subCap %08x\n", subCap32);
	    rc = TPM_BAD_MODE;
	    break;
	}
    }
    return rc;
}

/* Returns a TPM_NV_DATA_PUBLIC structure that indicates the values for the TPM_NV_INDEX
*/

static TPM_RESULT TPM_GetCapability_CapNVIndex(TPM_STORE_BUFFER *capabilityResponse,
					       tpm_state_t *tpm_state,
					       uint32_t nvIndex)
{
    TPM_RESULT		rc = 0;
    TPM_NV_DATA_PUBLIC 	*tpm_nv_data_public;
    
    printf(" TPM_GetCapability_CapNVIndex: nvIndex %08x\n", nvIndex);
    /* map from the nvIndex to the TPM_NV_DATA_PUBLIC structure */
    if (rc == 0) {
	rc = TPM_NVIndexEntries_GetDataPublic(&tpm_nv_data_public,
					      &(tpm_state->tpm_nv_index_entries),
					      nvIndex);
    }
    /* serialize the structure */
    if (rc == 0) {
	rc = TPM_NVDataPublic_Store(capabilityResponse, tpm_nv_data_public,
				    FALSE);	/* do not optimize digestAtRelease */
    }
    return rc;
}

/* Returns a Boolean value.

   TRUE means that the TPM supports the algorithm for TPM_EstablishTransport, TPM_ExecuteTransport
   and TPM_ReleaseTransportSigned.

   FALSE indicates that for these three commands the algorithm is not supported."
*/

static TPM_RESULT TPM_GetCapability_CapTransAlg(TPM_STORE_BUFFER *capabilityResponse,
						TPM_ALGORITHM_ID algorithmID)
{
    TPM_RESULT	rc = 0;
    TPM_BOOL	supported;

    printf(" TPM_GetCapability_CapTransAlg: algorithmID %08x\n", algorithmID);
    TPM_TransportPublic_CheckAlgId(&supported, algorithmID);
    printf("  TPM_GetCapability_CapTransAlg: Result %08x\n", supported);
    rc = TPM_Sbuffer_Append(capabilityResponse, &supported, sizeof(TPM_BOOL));
    return rc;
}

/* Returns a TPM_KEY_HANDLE_LIST structure that enumerates all handles currently loaded in the TPM
   for the given resource type.

   TPM_KEY_HANDLE_LIST is the number of handles followed by a list of the handles.

   When describing keys the handle list only contains the number of handles that an external manager
   can operate with and does not include the EK or SRK.

   Legal resources are TPM_RT_KEY, TPM_RT_AUTH, TPM_RT_TRANS, TPM_RT_COUNTER

   TPM_RT_CONTEXT is valid and returns not a list of handles but a list of the context count values.
*/

static TPM_RESULT TPM_GetCapability_CapHandle(TPM_STORE_BUFFER *capabilityResponse,
					      tpm_state_t *tpm_state,
					      TPM_RESOURCE_TYPE resourceType)
{
    TPM_RESULT	rc = 0;

    printf(" TPM_GetCapability_CapHandle: resourceType %08x\n", resourceType);
    switch (resourceType) {
      case TPM_RT_KEY:
	printf("  TPM_GetCapability_CapHandle: TPM_RT_KEY\n");
	rc = TPM_KeyHandleEntries_StoreHandles(capabilityResponse,
					       tpm_state->tpm_key_handle_entries);
	break;
      case TPM_RT_AUTH:
	printf("  TPM_GetCapability_CapHandle: TPM_RT_AUTH\n");
	rc = TPM_AuthSessions_StoreHandles(capabilityResponse,
					   tpm_state->tpm_stclear_data.authSessions);
	break;
      case TPM_RT_TRANS:
	printf("  TPM_GetCapability_CapHandle: TPM_RT_TRANS\n");
	rc = TPM_TransportSessions_StoreHandles(capabilityResponse,
						tpm_state->tpm_stclear_data.transSessions);
	break;
      case TPM_RT_CONTEXT:
	printf("  TPM_GetCapability_CapHandle: TPM_RT_CONTEXT\n");
	rc = TPM_ContextList_StoreHandles(capabilityResponse,
					  tpm_state->tpm_stclear_data.contextList);
	break;
      case TPM_RT_COUNTER:
	printf("  TPM_GetCapability_CapHandle: TPM_RT_COUNTER\n");
	rc = TPM_Counters_StoreHandles(capabilityResponse,
				       tpm_state->tpm_permanent_data.monotonicCounter);
	break;
      case TPM_RT_DAA_TPM:
	printf("  TPM_GetCapability_CapHandle: TPM_RT_DAA_TPM\n");
	rc = TPM_DaaSessions_StoreHandles(capabilityResponse,
					  tpm_state->tpm_stclear_data.daaSessions);
	break;
      default:
	printf("TPM_GetCapability_CapHandle: Error, illegal resource type %08x\n",
	       resourceType);
	rc = TPM_BAD_PARAMETER;
    }
    return rc;
}

/* Returns Boolean value.

   TRUE means the TPM supports the encryption scheme in a transport session.
*/

static TPM_RESULT TPM_GetCapability_CapTransEs(TPM_STORE_BUFFER *capabilityResponse,
					       TPM_ENC_SCHEME encScheme)
{
    TPM_RESULT	rc = 0;
    TPM_BOOL	supported;

    printf(" TPM_GetCapability_CapTransEs: encScheme %04hx\n", encScheme);
    switch (encScheme) {
	/* supported protocols */
      case TPM_ES_SYM_CTR:
      case TPM_ES_SYM_OFB:
	supported = TRUE;
	break;
	/* unsupported protocols */
      case TPM_ES_RSAESPKCSv15:
      case TPM_ES_RSAESOAEP_SHA1_MGF1:
      default:
	supported = FALSE;
	break;
    }	
    printf("  TPM_GetCapability_CapTransEs: Result %08x\n", supported);
    rc = TPM_Sbuffer_Append(capabilityResponse, &supported, sizeof(TPM_BOOL));
    return rc;
}

/* Boolean value.

   TRUE indicates that the TPM supports the encryption algorithm in OSAP encryption of AuthData
   values
*/

static TPM_RESULT TPM_GetCapability_CapAuthEncrypt(TPM_STORE_BUFFER *capabilityResponse,
						   TPM_ALGORITHM_ID algorithmID)
{
    TPM_RESULT	rc = 0;
    TPM_BOOL	supported;

    printf(" TPM_GetCapability_CapAuthEncrypt: algorithmID %08x\n", algorithmID);
    switch (algorithmID) {
      case TPM_ALG_XOR:
      case TPM_ALG_AES128:
	/* supported protocols */
	supported = TRUE;
	break;
      case TPM_ALG_RSA:
      case TPM_ALG_SHA:
      case TPM_ALG_HMAC:
      case TPM_ALG_MGF1:
      case TPM_ALG_AES192:
      case TPM_ALG_AES256:
      default:
	/* unsupported protocols */
	supported = FALSE;
	break;
    }	
    printf("  TPM_GetCapability_CapAuthEncrypt: Result %08x\n", supported);
    rc = TPM_Sbuffer_Append(capabilityResponse, &supported, sizeof(TPM_BOOL));
    return rc;
}

/* Boolean value.

   TRUE indicates that the TPM supports the size for the given version.

   For instance a request could ask for version 1.1 size 2 and the TPM would indicate TRUE. For 1.1
   size 3 the TPM would indicate FALSE. For 1.2 size 3 the TPM would indicate TRUE.
*/

static TPM_RESULT TPM_GetCapability_CapSelectSize(TPM_STORE_BUFFER *capabilityResponse,
						  TPM_SIZED_BUFFER *subCap)
{
    TPM_RESULT		rc = 0;
    TPM_SELECT_SIZE	tpm_select_size;
    unsigned char	*stream;
    uint32_t		stream_size;
    TPM_BOOL		supported;

    printf(" TPM_GetCapability_CapSelectSize:\n");
    TPM_SelectSize_Init(&tpm_select_size);		/* no free required */
    /* deserialize the subCap to the structure */
    if (rc == 0) {
	stream = subCap->buffer;
	stream_size = subCap->size;
	rc = TPM_SelectSize_Load(&tpm_select_size, &stream , &stream_size);
    }
    if (rc == 0) {
	/* The TPM MUST return an error if sizeOfSelect is 0 */
	printf("  TPM_GetCapability_CapSelectSize: subCap reqSize %u\n",
	       tpm_select_size.reqSize);
	if ((tpm_select_size.reqSize > (TPM_NUM_PCR/CHAR_BIT)) ||
	    (tpm_select_size.reqSize == 0)) {
	    supported = FALSE;
	}
	else {
	    supported = TRUE;
	}
    }
    if (rc == 0) {
	printf("  TPM_GetCapability_CapSelectSize: Result %08x\n", supported);
	rc = TPM_Sbuffer_Append(capabilityResponse, &supported, sizeof(TPM_BOOL));
    }
    return rc;
}

#if  (TPM_REVISION >= 103)	/* added for rev 103 */
/* TPM_GetCapability_CapDaLogic() rev 100

   A TPM_DA_INFO or TPM_DA_INFO_LIMITED structure that returns data according to the selected entity
   type (e.g., TPM_ET_KEYHANDLE, TPM_ET_OWNER, TPM_ET_SRK, TPM_ET_COUNTER, TPM_ET_OPERATOR,
   etc.). If the implemented dictionary attack logic does not support different secret types, the
   entity type can be ignored.
*/

static TPM_RESULT TPM_GetCapability_CapDaLogic(TPM_STORE_BUFFER *capabilityResponse,
					       TPM_SIZED_BUFFER *subCap,
					       tpm_state_t *tpm_state)
{
    TPM_RESULT		rc = 0;
    TPM_DA_INFO_LIMITED tpm_da_info_limited;
    TPM_DA_INFO		tpm_da_info;

    printf(" TPM_GetCapability_CapDaLogic:\n");
    TPM_DaInfoLimited_Init(&tpm_da_info_limited);	/* freed @1 */
    TPM_DaInfo_Init(&tpm_da_info);			/* freed @2 */
    subCap = subCap;			/* dictionary attack mitigation not per entity type in this
					   implementation. */
    /* if disableFullDALogicInfo is TRUE, the full dictionary attack TPM_GetCapability info is
       deactivated.  The returned structure is TPM_DA_INFO_LIMITED. */
    if (tpm_state->tpm_permanent_flags.disableFullDALogicInfo) {
	TPM_DaInfoLimited_Set(&tpm_da_info_limited, tpm_state);
	rc = TPM_DaInfoLimited_Store(capabilityResponse, &tpm_da_info_limited);
	
    }
    /* if disableFullDALogicInfo is FALSE, the full dictionary attack TPM_GetCapability 
       info is activated.  The returned structure is 
       TPM_DA_INFO. */
    else {
	TPM_DaInfo_Set(&tpm_da_info, tpm_state);
	rc = TPM_DaInfo_Store(capabilityResponse, &tpm_da_info);
    }
    TPM_DaInfoLimited_Delete(&tpm_da_info_limited);	/* @1 */
    TPM_DaInfo_Delete(&tpm_da_info);			/* @2 */
    return rc;
}
#endif

/* Returns TPM_CAP_VERSION_INFO structure.

   The TPM fills in the structure and returns the information indicating what the TPM currently
   supports.
*/

static TPM_RESULT TPM_GetCapability_CapVersionVal(TPM_STORE_BUFFER *capabilityResponse,
						  TPM_PERMANENT_DATA *tpm_permanent_data)
{
    TPM_RESULT			rc = 0;
    TPM_CAP_VERSION_INFO	tpm_cap_version_info;

    printf(" TPM_GetCapability_CapVersionVal:\n");
    TPM_CapVersionInfo_Set(&tpm_cap_version_info, tpm_permanent_data);	/* freed @1 */
    printf("  TPM_GetCapability_CapVersionVal: specLevel %04hx\n", tpm_cap_version_info.specLevel);
    printf("  TPM_GetCapability_CapVersionVal: errataRev %02x\n", tpm_cap_version_info.errataRev);
    printf("  TPM_GetCapability_CapVersionVal: revMajor %02x revMinor %02x\n",
	   tpm_cap_version_info.version.revMajor, tpm_cap_version_info.version.revMinor);
    printf("  TPM_GetCapability_CapVersionVal: tpmVendorID %02x %02x %02x %02x\n",
	   tpm_cap_version_info.tpmVendorID[0],
	   tpm_cap_version_info.tpmVendorID[1],
	   tpm_cap_version_info.tpmVendorID[2],
	   tpm_cap_version_info.tpmVendorID[3]);
    rc = TPM_CapVersionInfo_Store(capabilityResponse, &tpm_cap_version_info);
    TPM_CapVersionInfo_Delete(&tpm_cap_version_info);			/* @1 */
    return rc;
}

/* Returns a 4 element array of uint32_t values each denoting the timeout value in microseconds for
   the following in this order:
							 
   TIMEOUT_A, TIMEOUT_B, TIMEOUT_C, TIMEOUT_D

   Where these timeouts are to be used is determined by the platform specific TPM Interface
   Specification.
*/

static TPM_RESULT TPM_GetCapability_CapPropTisTimeout(TPM_STORE_BUFFER *capabilityResponse)
{
    TPM_RESULT			rc = 0;

    printf(" TPM_GetCapability_CapPropTisTimeout:\n");
    if (rc == 0) { 
	rc = TPM_Sbuffer_Append32(capabilityResponse, TPM_TIMEOUT_A);
    }								   
    if (rc == 0) {						   
	rc = TPM_Sbuffer_Append32(capabilityResponse, TPM_TIMEOUT_B);
    }								   
    if (rc == 0) {						   
	rc = TPM_Sbuffer_Append32(capabilityResponse, TPM_TIMEOUT_C);
    }								   
    if (rc == 0) {						   
	rc = TPM_Sbuffer_Append32(capabilityResponse, TPM_TIMEOUT_D);
    }
    return rc;
}

/* Returns a 3 element array of uint32_t values each denoting the duration value in microseconds of
   the duration of the three classes of commands: Small, Medium and Long in the following in this
   order:

   SMALL_DURATION, MEDIUM_DURATION, LONG_DURATION
*/

static TPM_RESULT TPM_GetCapability_CapPropDuration(TPM_STORE_BUFFER *capabilityResponse)
{
    TPM_RESULT			rc = 0;

    printf(" TPM_GetCapability_CapPropDuration:\n");
    if (rc == 0) { 
	rc = TPM_Sbuffer_Append32(capabilityResponse, TPM_SMALL_DURATION);
    }								   
    if (rc == 0) {						   
	rc = TPM_Sbuffer_Append32(capabilityResponse, TPM_MEDIUM_DURATION);
    }								   
    if (rc == 0) {						   
	rc = TPM_Sbuffer_Append32(capabilityResponse, TPM_LONG_DURATION);
    }								   
    return rc;
}

/* 7.3 TPM_GetCapabilityOwner rev 98

   TPM_GetCapabilityOwner enables the TPM Owner to retrieve all the non-volatile flags and the
   volatile flags in a single operation.  This command is deprecated, mandatory.

   The flags summarize many operational aspects of the TPM. The information represented by some
   flags is private to the TPM Owner. So, for simplicity, proof of ownership of the TPM must be
   presented to retrieve the set of flags. When necessary, the flags that are not private to the
   Owner can be deduced by Users via other (more specific) means.
   
   The normal TPM authentication mechanisms are sufficient to prove the integrity of the
   response. No additional integrity check is required.

   For 31>=N>=0

   1. Bit-N of the TPM_PERMANENT_FLAGS structure is the Nth bit after the opening bracket in the
   definition of TPM_PERMANENT_FLAGS in the version of the specification indicated by the parameter
   "version". The bit immediately after the opening bracket is the 0th bit.

   2. Bit-N of the TPM_STCLEAR_FLAGS structure is the Nth bit after the opening bracket in the
   definition of TPM_STCLEAR_FLAGS in the version of the specification indicated by the parameter
   "version". The bit immediately after the opening bracket is the 0th bit.

   3. Bit-N of non_volatile_flags corresponds to the Nth bit in TPM_PERMANENT_FLAGS, and the lsb of
   non_volatile_flags corresponds to bit0 of TPM_PERMANENT_FLAGS

   4. Bit-N of volatile_flags corresponds to the Nth bit in TPM_STCLEAR_FLAGS, and the lsb of
   volatile_flags corresponds to bit0 of TPM_STCLEAR_FLAGS
*/

TPM_RESULT TPM_Process_GetCapabilityOwner(tpm_state_t *tpm_state,
					  TPM_STORE_BUFFER *response,
					  TPM_TAG tag,
					  uint32_t paramSize,
					  TPM_COMMAND_CODE ordinal,
					  unsigned char *command,
					  TPM_TRANSPORT_INTERNAL *transportInternal)
{
    TPM_RESULT	rcf = 0;			/* fatal error precluding response */
    TPM_RESULT	returnCode = TPM_SUCCESS;	/* command return code */

    /* input parameters */
    TPM_AUTHHANDLE	authHandle;	/* The authorization session handle used for Owner
					   authentication. */
    TPM_NONCE		nonceOdd;	/* Nonce generated by system associated with authHandle */
    TPM_BOOL	continueAuthSession = TRUE;	/* The continue use flag for the authorization
						   session */
    TPM_AUTHDATA	ownerAuth;	/* The authorization session digest for inputs and owner
					   authentication. HMAC key: ownerAuth. */

    /* processing parameters */
    unsigned char *	inParamStart;	/* starting point of inParam's */
    unsigned char *	inParamEnd;	/* ending point of inParam's */
    TPM_DIGEST		inParamDigest;
    TPM_BOOL		auditStatus;		/* audit the ordinal */
    TPM_BOOL		transportEncrypt;	/* wrapped in encrypted transport session */
    TPM_BOOL		authHandleValid = FALSE;
    TPM_AUTH_SESSION_DATA *auth_session_data;	/* session data for authHandle */
    TPM_SECRET		*hmacKey;

    /* output parameters */
    uint32_t		outParamStart;		/* starting point of outParam's */
    uint32_t		outParamEnd;		/* ending point of outParam's */
    TPM_DIGEST		outParamDigest;
    TPM_VERSION		version;		/* A properly filled out version structure. */
    uint32_t		non_volatile_flags;	/* The current state of the non-volatile flags. */
    uint32_t		volatile_flags;		/* The current state of the volatile flags. */

    printf("TPM_Process_GetCapabilityOwner: Ordinal Entry\n");
    /*
      get inputs
    */
    /* save the starting point of inParam's for authorization and auditing */
    inParamStart = command;
    /* save the ending point of inParam's for authorization and auditing */
    inParamEnd = command;
    /* digest the input parameters */
    if (returnCode == TPM_SUCCESS) {
	returnCode = TPM_GetInParamDigest(inParamDigest,	/* output */
					  &auditStatus,		/* output */
					  &transportEncrypt,	/* output */
					  tpm_state,
					  tag,
					  ordinal,
					  inParamStart,
					  inParamEnd,
					  transportInternal);
    }
    /* check state */
    if (returnCode == TPM_SUCCESS) {
	returnCode = TPM_CheckState(tpm_state, tag, TPM_CHECK_ALL);
    }
    /* check tag */
    if (returnCode == TPM_SUCCESS) {
	returnCode = TPM_CheckRequestTag1(tag);
    }
    /* get the 'below the line' authorization parameters */
    if (returnCode == TPM_SUCCESS) {
	returnCode = TPM_AuthParams_Get(&authHandle,
					&authHandleValid,
					nonceOdd,
					&continueAuthSession,
					ownerAuth,
					&command, &paramSize);
    }
    if (returnCode == TPM_SUCCESS) {
	if (paramSize != 0) {
	    printf("TPM_Process_GetCapabilityOwner: Error, command has %u extra bytes\n",
		   paramSize);
	    returnCode = TPM_BAD_PARAM_SIZE;
	}
    }
    /* do not terminate sessions if the command did not parse correctly */
    if (returnCode != TPM_SUCCESS) {
	authHandleValid = FALSE;
    }
    /*
      Processing
    */
    /* 1. The TPM validates that the TPM Owner authorizes the command. */
    if (returnCode == TPM_SUCCESS) {
	returnCode = TPM_AuthSessions_GetData(&auth_session_data,
					      &hmacKey,
					      tpm_state,
					      authHandle,
					      TPM_PID_NONE,
					      TPM_ET_OWNER,
					      ordinal,
					      NULL,
					      &(tpm_state->tpm_permanent_data.ownerAuth), /* OIAP */
					      tpm_state->tpm_permanent_data.ownerAuth);	  /* OSAP */
    }
    if (returnCode == TPM_SUCCESS) {
	returnCode = TPM_Authdata_Check(tpm_state,
					*hmacKey,		/* owner HMAC key */
					inParamDigest,
					auth_session_data,	/* authorization session */
					nonceOdd,		/* Nonce generated by system
								   associated with authHandle */
					continueAuthSession,
					ownerAuth);		/* Authorization digest for input */
    }
    /* 2. The TPM creates the parameter non_volatile_flags by setting each bit to the same state as
       the corresponding bit in TPM_PERMANENT_FLAGS. Bits in non_volatile_flags for which there is
       no corresponding bit in TPM_PERMANENT_FLAGS are set to zero. */
    if (returnCode == TPM_SUCCESS) {
	returnCode = TPM_PermanentFlags_StoreBitmap(&non_volatile_flags,
						    &(tpm_state->tpm_permanent_flags));
    }	 
    /* 3. The TPM creates the parameter volatile_flags by setting each bit to the same state as the
       corresponding bit in TPM_STCLEAR_FLAGS. Bits in volatile_flags for which there is no
       corresponding bit in TPM_STCLEAR_FLAGS are set to zero. */
    if (returnCode == TPM_SUCCESS) {
	returnCode = TPM_StclearFlags_StoreBitmap(&volatile_flags,
						  &(tpm_state->tpm_stclear_flags));
    }	 
    /* 4. The TPM generates the parameter "version". */
    if (returnCode == TPM_SUCCESS) {
	TPM_Version_Set(&version, &(tpm_state->tpm_permanent_data));
    }	 
    /* 5. The TPM returns non_volatile_flags, volatile_flags and version to the caller. */
    /*
      response
    */
    /* standard response: tag, (dummy) paramSize, returnCode.  Failure is fatal. */
    if (rcf == 0) {
	printf("TPM_Process_GetCapabilityOwner: Ordinal returnCode %08x %u\n",
	       returnCode, returnCode);
	rcf = TPM_Sbuffer_StoreInitialResponse(response, tag, returnCode);
    }
    /* success response, append the rest of the parameters.  */
    if (rcf == 0) {
	if (returnCode == TPM_SUCCESS) {
	    /* checkpoint the beginning of the outParam's */
	    outParamStart = response->buffer_current - response->buffer;
	    /* return the version */
	    returnCode = TPM_Version_Store(response, &version);
	}
	/* return the non_volatile_flags */
	if (returnCode == TPM_SUCCESS) {
	    returnCode = TPM_Sbuffer_Append32(response, non_volatile_flags);
	}
	/* return the volatile_flags */
	if (returnCode == TPM_SUCCESS) {
	    returnCode = TPM_Sbuffer_Append32(response, volatile_flags);
	    /* checkpoint the end of the outParam's */
	    outParamEnd = response->buffer_current - response->buffer;
	}
	/* digest the above the line output parameters */
	if (returnCode == TPM_SUCCESS) {
	    returnCode = TPM_GetOutParamDigest(outParamDigest,	/* output */
					       auditStatus,	/* input audit status */
					       transportEncrypt,
					       tag,			
					       returnCode,
					       ordinal,		/* command ordinal */
					       response->buffer + outParamStart,	/* start */
					       outParamEnd - outParamStart);	/* length */
	}
	/* calculate and set the below the line parameters */
	if (returnCode == TPM_SUCCESS) {
	    returnCode = TPM_AuthParams_Set(response,
					    *hmacKey,	/* owner HMAC key */
					    auth_session_data,
					    outParamDigest,
					    nonceOdd,
					    continueAuthSession);
	}
	/* audit if required */
	if ((returnCode == TPM_SUCCESS) && auditStatus) {
	    returnCode = TPM_ProcessAudit(tpm_state,
					  transportEncrypt,
					  inParamDigest,
					  outParamDigest,
					  ordinal);
	}
	/* adjust the initial response */
	rcf = TPM_Sbuffer_StoreFinalResponse(response, returnCode, tpm_state);
    }
    /* if there was an error, or continueAuthSession is FALSE, terminate the session */
    if (((rcf != 0) ||
	 ((returnCode != TPM_SUCCESS) && (returnCode != TPM_DEFEND_LOCK_RUNNING)) ||
	 !continueAuthSession) &&
	authHandleValid) {
	TPM_AuthSessions_TerminateHandle(tpm_state->tpm_stclear_data.authSessions, authHandle);
    }
    /*
      cleanup
    */
    return rcf;
}

/* 29.1 TPM_GetCapabilitySigned rev 94

   TPM_GetCapabilitySigned is almost the same as TPM_GetCapability. The differences are that the
   input includes a challenge (a nonce) and the response includes a digital signature to vouch for
   the source of the answer.

   If a caller itself requires proof, it is sufficient to use any signing key for which only the TPM
   and the caller have AuthData.

   If a caller requires proof for a third party, the signing key must be one whose signature is
   trusted by the third party. A TPM-identity key may be suitable.
*/

TPM_RESULT TPM_Process_GetCapabilitySigned(tpm_state_t *tpm_state,
					   TPM_STORE_BUFFER *response,
					   TPM_TAG tag,
					   uint32_t paramSize,
					   TPM_COMMAND_CODE ordinal,
					   unsigned char *command,
					   TPM_TRANSPORT_INTERNAL *transportInternal)
{
    TPM_RESULT	rcf = 0;			/* fatal error precluding response */
    TPM_RESULT	returnCode = TPM_SUCCESS;	/* command return code */

    /* input parameters */
    TPM_KEY_HANDLE	keyHandle;	/* The handle of a loaded key that can perform digital
					   signatures. */
    TPM_NONCE		antiReplay;	/* Nonce provided to allow caller to defend against replay
					   of messages */
    TPM_CAPABILITY_AREA capArea = 0;	/* Partition of capabilities to be interrogated */
    TPM_SIZED_BUFFER	subCap;		/* Further definition of information */
    TPM_AUTHHANDLE	authHandle;	/* The authorization session handle used for keyHandle
					   authorization */
    TPM_NONCE		nonceOdd;	/* Nonce generated by system associated with authHandle */
    TPM_BOOL	continueAuthSession = TRUE;	/* The continue use flag for the authorization
						   session handle */
    TPM_AUTHDATA	privAuth;	/* The authorization session digest that authorizes the use
					   of keyHandle. HMAC key: key.usageAuth */
   
    /* processing parameters */
    unsigned char *	inParamStart;		/* starting point of inParam's */
    unsigned char *	inParamEnd;		/* ending point of inParam's */
    TPM_DIGEST		inParamDigest;
    TPM_BOOL		auditStatus;		/* audit the ordinal */
    TPM_BOOL		transportEncrypt = FALSE;/* wrapped in encrypted transport session */
    TPM_BOOL		authHandleValid = FALSE;
    TPM_AUTH_SESSION_DATA *auth_session_data;	/* session data for authHandle */
    TPM_SECRET		*hmacKey;
    TPM_KEY		*sigKey = NULL;		/* the key specified by keyHandle */
    TPM_SECRET		*keyUsageAuth;
    TPM_BOOL		parentPCRStatus;
    uint16_t		subCap16;		/* the subCap as a uint16_t */
    uint32_t		subCap32;		/* the subCap as a uint32_t */
    TPM_STORE_BUFFER	r1Response;		/* capability response */
    const unsigned char *r1_buffer;		/* r1 serialization */
    uint32_t		r1_length;
    TPM_DIGEST		s1;

    /* output parameters */
    uint32_t		outParamStart;	/* starting point of outParam's */
    uint32_t		outParamEnd;	/* ending point of outParam's */
    TPM_DIGEST		outParamDigest;
    TPM_VERSION		version;	/* A properly filled out version structure. */
    TPM_SIZED_BUFFER	resp;		/* The capability response */
    TPM_SIZED_BUFFER	sig;		/* The resulting digital signature. */

    printf("TPM_Process_GetCapabilitySigned: Ordinal Entry\n");
    TPM_SizedBuffer_Init(&subCap);	/* freed @1 */
    TPM_SizedBuffer_Init(&resp);	/* freed @2 */
    TPM_SizedBuffer_Init(&sig);		/* freed @3 */
    TPM_Sbuffer_Init(&r1Response);	/* freed @4 */
    /*
      get inputs
    */
    /* get keyHandle parameter */
    if (returnCode == TPM_SUCCESS) {
	returnCode = TPM_Load32(&keyHandle, &command, &paramSize);
    }
    /* save the starting point of inParam's for authorization and auditing */
    inParamStart = command;
    /* get antiReplay parameter */
    if (returnCode == TPM_SUCCESS) {
	printf("TPM_Process_GetCapabilitySigned: keyHandle %08x\n", keyHandle);
	returnCode = TPM_Nonce_Load(antiReplay, &command, &paramSize);
    }
    /* get capArea parameter */
    if (returnCode == TPM_SUCCESS) {
	returnCode = TPM_Load32(&capArea, &command, &paramSize);
    }
    /* get get subCap parameter */
    if (returnCode == TPM_SUCCESS) {
	returnCode = TPM_SizedBuffer_Load(&subCap, &command, &paramSize);
    }
    /* save the ending point of inParam's for authorization and auditing */
    inParamEnd = command;
    /* digest the input parameters */
    if (returnCode == TPM_SUCCESS) {
	returnCode = TPM_GetInParamDigest(inParamDigest,	/* output */
					  &auditStatus,		/* output */
					  &transportEncrypt,	/* output */
					  tpm_state,
					  tag,
					  ordinal,
					  inParamStart,
					  inParamEnd,
					  transportInternal);
    }
    /* check state */
    if (returnCode == TPM_SUCCESS) {
	returnCode = TPM_CheckState(tpm_state, tag, TPM_CHECK_ALL);
    }
    /* check tag */
    if (returnCode == TPM_SUCCESS) {
	returnCode = TPM_CheckRequestTag10(tag);
    }
    /* get the optional 'below the line' authorization parameters */
    if ((returnCode == TPM_SUCCESS) && (tag == TPM_TAG_RQU_AUTH1_COMMAND)) {
	returnCode = TPM_AuthParams_Get(&authHandle,
					&authHandleValid,
					nonceOdd,
					&continueAuthSession,
					privAuth,
					&command, &paramSize);
    }
    if (returnCode == TPM_SUCCESS) {
	if (paramSize != 0) {
	    printf("TPM_Process_GetCapabilitySigned: Error, command has %u extra bytes\n",
		   paramSize);
	    returnCode = TPM_BAD_PARAM_SIZE;
	}
    }
    /* do not terminate sessions if the command did not parse correctly */
    if (returnCode != TPM_SUCCESS) {
	authHandleValid = FALSE;
    }
    /*
      Processing
    */
    /* get the key corresponding to the keyHandle parameter */
    if (returnCode == TPM_SUCCESS) {
	returnCode = TPM_KeyHandleEntries_GetKey(&sigKey, &parentPCRStatus, tpm_state, keyHandle,
						 FALSE,		/* not r/o, used to sign */
						 FALSE,		/* do not ignore PCRs */
						 FALSE);	/* cannot use EK */
    }
    /* 1. The TPM validates the authority to use keyHandle */
    if ((returnCode == TPM_SUCCESS) && (tag == TPM_TAG_RQU_COMMAND)) {
	if (sigKey->authDataUsage != TPM_AUTH_NEVER) {
	    printf("TPM_Process_GetCapabilitySigned: Error, authorization required\n");
	    returnCode = TPM_AUTHFAIL;
	}
    }
    /* get keyHandle -> usageAuth */
    if ((returnCode == TPM_SUCCESS) && (tag == TPM_TAG_RQU_AUTH1_COMMAND)) {
	returnCode = TPM_Key_GetUsageAuth(&keyUsageAuth, sigKey);
    }	 
    /* get the session data */
    if ((returnCode == TPM_SUCCESS) && (tag == TPM_TAG_RQU_AUTH1_COMMAND)) {
	returnCode = TPM_AuthSessions_GetData(&auth_session_data,
					      &hmacKey,
					      tpm_state,
					      authHandle,
					      TPM_PID_NONE,
					      TPM_ET_KEYHANDLE,
					      ordinal,
					      sigKey,
					      keyUsageAuth,		/* OIAP */
					      sigKey->tpm_store_asymkey->pubDataDigest); /* OSAP */
    }
    /* 1. The TPM MUST validate the authorization to use the key pointed to by keyHandle. */
    if ((returnCode == TPM_SUCCESS) && (tag == TPM_TAG_RQU_AUTH1_COMMAND)) {
	returnCode = TPM_Authdata_Check(tpm_state,
					*hmacKey,		/* HMAC key */
					inParamDigest,
					auth_session_data,	/* authorization session */
					nonceOdd,		/* Nonce generated by system
								   associated with authHandle */
					continueAuthSession,
					privAuth);		/* Authorization digest for input */
    }
    

    /* subCap is often a uint16_t or uint32_t, create them now */
    if (returnCode == TPM_SUCCESS) {
	TPM_GetSubCapInt(&subCap16, &subCap32, &subCap);
    }
    /* 2. The TPM calls TPM_GetCapability passing the capArea and subCap fields and saving the resp
       field as R1 */
    if (returnCode == TPM_SUCCESS) {
	returnCode = TPM_GetCapabilityCommon(&r1Response, tpm_state,
					     capArea, subCap16, subCap32, &subCap);
    }	 
    if (returnCode == TPM_SUCCESS) {
	/* get the capability r1 serialization */
	TPM_Sbuffer_Get(&r1Response, &r1_buffer, &r1_length);
	printf("TPM_Process_GetCapabilitySigned: resp length %08x\n", r1_length);
	TPM_PrintFour("TPM_Process_GetCapabilitySigned: Hashing resp", r1_buffer);
	TPM_PrintFour("TPM_Process_GetCapabilitySigned: antiReplay", antiReplay);
	/* 3. The TPM creates S1 by taking a SHA1 hash of the concatenation (r1 || antiReplay).	 */
	returnCode = TPM_SHA1(s1,
			      r1_length, r1_buffer,
			      TPM_NONCE_SIZE, antiReplay,
			      0, NULL);
    }	 
    /* 4. The TPM validates the authority to use keyHandle */
    /* The key in keyHandle MUST have a KEYUSAGE value of type TPM_KEY_SIGNING or TPM_KEY_LEGACY or
       TPM_KEY_IDENTITY. */
    if (returnCode == TPM_SUCCESS) {
	if ((sigKey->keyUsage != TPM_KEY_SIGNING) &&
	    ((sigKey->keyUsage) != TPM_KEY_IDENTITY) &&
	    ((sigKey->keyUsage) != TPM_KEY_LEGACY)) {
	    printf("TPM_Process_GetCapabilitySigned: Error, keyUsage %04hx is invalid\n",
		   sigKey->keyUsage);
	    returnCode = TPM_INVALID_KEYUSAGE;
	}
    }
    /* 5. The TPM creates a digital signature of S1 using the key in keyHandle and returns the
       result in sig. */
    if (returnCode == TPM_SUCCESS) {
	if (sigKey->algorithmParms.sigScheme != TPM_SS_RSASSAPKCS1v15_SHA1) {
	    printf("TPM_Process_GetCapabilitySigned: Error, inappropriate signature scheme %04x\n",
		   sigKey->algorithmParms.sigScheme);
	    returnCode = TPM_INAPPROPRIATE_SIG;
	}
    }
    if (returnCode == TPM_SUCCESS) {
	TPM_PrintFour("TPM_Process_GetCapabilitySigned: Signing s1", s1);
	returnCode = TPM_RSASignToSizedBuffer(&sig,		/* signature */
					      s1,		/* message */
					      TPM_DIGEST_SIZE,	/* message size */
					      sigKey);		/* signing key and parameters */
    }
    /*
      response
    */
    /* standard response: tag, (dummy) paramSize, returnCode.  Failure is fatal. */
    if (rcf == 0) {
	printf("TPM_Process_GetCapabilitySigned: Ordinal returnCode %08x %u\n",
	       returnCode, returnCode);
	rcf = TPM_Sbuffer_StoreInitialResponse(response, tag, returnCode);
    }
    /* success response, append the rest of the parameters.  */
    if (rcf == 0) {
	if (returnCode == TPM_SUCCESS) {
	    /* checkpoint the beginning of the outParam's */
	    outParamStart = response->buffer_current - response->buffer;
	    /* return the version */
	    TPM_Version_Set(&version, &(tpm_state->tpm_permanent_data));
	    returnCode = TPM_Version_Store(response, &version);
	}
	/* return the capability response size */
	if (returnCode == TPM_SUCCESS) {
	    returnCode = TPM_Sbuffer_Append32(response, r1_length);
	}
	/* return the capability response */
	if (returnCode == TPM_SUCCESS) {
	    returnCode = TPM_Sbuffer_Append(response, r1_buffer, r1_length);
	}
	/* return the signature */
	if (returnCode == TPM_SUCCESS) {
	    returnCode = TPM_SizedBuffer_Store(response, &sig);
	    /* checkpoint the end of the outParam's */
	    outParamEnd = response->buffer_current - response->buffer;
	}
	/* digest the above the line output parameters */
	if (returnCode == TPM_SUCCESS) {
	    returnCode = TPM_GetOutParamDigest(outParamDigest,	/* output */
					       auditStatus,	/* input audit status */
					       transportEncrypt,
					       tag,			
					       returnCode,
					       ordinal,		/* command ordinal */
					       response->buffer + outParamStart,	/* start */
					       outParamEnd - outParamStart);	/* length */
	}
	/* calculate and set the below the line parameters */
	if ((returnCode == TPM_SUCCESS) && (tag == TPM_TAG_RQU_AUTH1_COMMAND)) {
	    returnCode = TPM_AuthParams_Set(response,
					    *hmacKey,	/* owner HMAC key */
					    auth_session_data,
					    outParamDigest,
					    nonceOdd,
					    continueAuthSession);
	}
	/* audit if required */
	if ((returnCode == TPM_SUCCESS) && auditStatus) {
	    returnCode = TPM_ProcessAudit(tpm_state,
					  transportEncrypt,
					  inParamDigest,
					  outParamDigest,
					  ordinal);
	}
	/* adjust the initial response */
	rcf = TPM_Sbuffer_StoreFinalResponse(response, returnCode, tpm_state);
    }
    /* if there was an error, or continueAuthSession is FALSE, terminate the session */
    if (((rcf != 0) ||
	 ((returnCode != TPM_SUCCESS) && (returnCode != TPM_DEFEND_LOCK_RUNNING)) ||
	 !continueAuthSession) &&
	authHandleValid) {
	TPM_AuthSessions_TerminateHandle(tpm_state->tpm_stclear_data.authSessions, authHandle);
    }
    /*
      cleanup
    */
    TPM_SizedBuffer_Delete(&subCap);	/* @1 */
    TPM_SizedBuffer_Delete(&resp);	/* @2 */
    TPM_SizedBuffer_Delete(&sig);	/* @3 */
    TPM_Sbuffer_Delete(&r1Response);	/* @4 */
    return rcf;
}

/* 7.2 TPM_SetCapability rev 96

   This command sets values in the TPM
*/

TPM_RESULT TPM_Process_SetCapability(tpm_state_t *tpm_state,
				     TPM_STORE_BUFFER *response,
				     TPM_TAG tag,
				     uint32_t paramSize,
				     TPM_COMMAND_CODE ordinal,
				     unsigned char *command,
				     TPM_TRANSPORT_INTERNAL *transportInternal)
{
    TPM_RESULT	rcf = 0;		/* fatal error precluding response */
    TPM_TAG	returnCode = 0;		/* command return code */

    /* input parameters */
    TPM_CAPABILITY_AREA capArea;	/* Partition of capabilities to be set */
    TPM_SIZED_BUFFER	subCap;		/* Further definition of information */
    TPM_SIZED_BUFFER	setValue;	/* The value to set */
    TPM_AUTHHANDLE	authHandle;	/* The authorization session handle used for owner
					   authentication. */
    TPM_NONCE		nonceOdd;	/* Nonce generated by system associated with authHandle */
    TPM_BOOL		continueAuthSession = TRUE;	/* The continue use flag for the
							   authorization session handle */
    TPM_AUTHDATA	ownerAuth;	/* Authorization. HMAC key: owner.usageAuth */

    /* processing parameters */
    unsigned char *	inParamStart;		/* starting point of inParam's */
    unsigned char *	inParamEnd;		/* ending point of inParam's */
    TPM_DIGEST		inParamDigest;
    TPM_BOOL		auditStatus;		/* audit the ordinal */
    TPM_BOOL		transportEncrypt = FALSE;/* wrapped in encrypted transport session */
    TPM_BOOL		authHandleValid = FALSE;
    TPM_AUTH_SESSION_DATA *auth_session_data;	/* session data for authHandle */
    TPM_SECRET		*hmacKey;
    uint16_t		subCap16;		/* the subCap as a uint16_t */
    uint32_t		subCap32;		/* the subCap as a uint32_t */
    TPM_BOOL		ownerAuthorized = FALSE;	/* TRUE if owner authorization validated */
    TPM_BOOL		presenceAuthorized = FALSE;	/* TRUE if physicalPresence validated */

    /* output parameters */
    uint32_t		outParamStart;		/* starting point of outParam's */
    uint32_t		outParamEnd;		/* ending point of outParam's */
    TPM_DIGEST		outParamDigest;

    printf("TPM_Process_SetCapability: Ordinal Entry\n");
    TPM_SizedBuffer_Init(&subCap);	/* freed @1 */
    TPM_SizedBuffer_Init(&setValue);	/* freed @2 */
    /*
      get inputs
    */
    /* save the starting point of inParam's for authorization and auditing */
    inParamStart = command;
    /* get capArea parameter */
    if (returnCode == TPM_SUCCESS) {
	returnCode = TPM_Load32(&capArea, &command, &paramSize);
    }
    /* get subCap parameter */
    if (returnCode == TPM_SUCCESS) {
	printf("TPM_Process_SetCapability: capArea %08x \n", capArea);
	returnCode = TPM_SizedBuffer_Load(&subCap, &command, &paramSize);
    }
    /* get setValue parameter */
    if (returnCode == TPM_SUCCESS) {
	returnCode = TPM_SizedBuffer_Load(&setValue , &command, &paramSize);
    }
    /* save the ending point of inParam's for authorization and auditing */
    inParamEnd = command;
    /* digest the input parameters */
    if (returnCode == TPM_SUCCESS) {
	returnCode = TPM_GetInParamDigest(inParamDigest,	/* output */
					  &auditStatus,		/* output */
					  &transportEncrypt,	/* output */
					  tpm_state,
					  tag,
					  ordinal,
					  inParamStart,
					  inParamEnd,
					  transportInternal);
    }
    /* check state */
    if (returnCode == TPM_SUCCESS) {
	returnCode = TPM_CheckState(tpm_state, tag, (TPM_CHECK_NOT_SHUTDOWN |
						     TPM_CHECK_NO_LOCKOUT));
    }
    /* check tag */
    if (returnCode == TPM_SUCCESS) {
	returnCode = TPM_CheckRequestTag10(tag);
    }
    /* get the optional 'below the line' authorization parameters */
    if ((returnCode == TPM_SUCCESS) && (tag == TPM_TAG_RQU_AUTH1_COMMAND)) {
	ownerAuthorized = TRUE;
	returnCode = TPM_AuthParams_Get(&authHandle,
					&authHandleValid,
					nonceOdd,
					&continueAuthSession,
					ownerAuth,
					&command, &paramSize);
    }
    if (returnCode == TPM_SUCCESS) {
	if (paramSize != 0) {
	    printf("TPM_Process_SetCapability: Error, command has %u extra bytes\n",
		   paramSize);
	    returnCode = TPM_BAD_PARAM_SIZE;
	}
    }
    /* do not terminate sessions if the command did not parse correctly */
    if (returnCode != TPM_SUCCESS) {
	authHandleValid = FALSE;
    }
    /*
      Processing
    */
    /* 1. If tag = TPM_TAG_RQU_AUTH1_COMMAND, validate the command and parameters using ownerAuth,
       return TPM_AUTHFAIL on error */
    if ((returnCode == TPM_SUCCESS) && (tag == TPM_TAG_RQU_AUTH1_COMMAND)) {
	returnCode = TPM_AuthSessions_GetData(&auth_session_data,
					      &hmacKey,
					      tpm_state,
					      authHandle,
					      TPM_PID_NONE,
					      TPM_ET_OWNER,
					      ordinal,
					      NULL,
					      &(tpm_state->tpm_permanent_data.ownerAuth), /* OIAP */
					      tpm_state->tpm_permanent_data.ownerAuth);	  /* OSAP */
    }
    if ((returnCode == TPM_SUCCESS) && (tag == TPM_TAG_RQU_AUTH1_COMMAND)) {
	returnCode = TPM_Authdata_Check(tpm_state,
					*hmacKey,		/* owner HMAC key */
					inParamDigest,	
					auth_session_data,	/* authorization session */
					nonceOdd,		/* Nonce generated by system
								   associated with authHandle */
					continueAuthSession,
					ownerAuth);		/* Authorization digest for input */
    }
    if (returnCode == TPM_SUCCESS) {
	returnCode = TPM_Global_GetPhysicalPresence(&presenceAuthorized, tpm_state);
    }
    /* 2. The TPM validates the capArea and subCap indicators, including the ability to set value
       based on any set restrictions */
    /* 3. If the capArea and subCap indicators conform with one of the entries in the structure
       TPM_CAPABILITY_AREA (Values for TPM_SetCapability) */
    /* a. The TPM sets the relevant flag/data to the value of setValue parameter.  */
    /* 4. Else */
    /* a. Return the error code TPM_BAD_PARAMETER. */
    if (returnCode == TPM_SUCCESS) {
	/* subCap is often a uint16_t or uint32_t, create them now */
	TPM_GetSubCapInt(&subCap16, &subCap32, &subCap);
	returnCode = TPM_SetCapabilityCommon(tpm_state, ownerAuthorized, presenceAuthorized,
					     capArea, subCap16, subCap32, &subCap,
					     &setValue);
    }
    /*
      response
    */
    /* standard response: tag, (dummy) paramSize, returnCode.  Failure is fatal. */
    if (rcf == 0) {
	printf("TPM_Process_SetCapability: Ordinal returnCode %08x %u\n",
	       returnCode, returnCode);
	rcf = TPM_Sbuffer_StoreInitialResponse(response, tag, returnCode);
    }
    /* success response, append the rest of the parameters.  */
    if (rcf == 0) {
	if (returnCode == TPM_SUCCESS) {
	    /* checkpoint the beginning of the outParam's */
	    outParamStart = response->buffer_current - response->buffer;
	    /* checkpoint the end of the outParam's */
	    outParamEnd = response->buffer_current - response->buffer;
	}
	/* digest the above the line output parameters */
	if (returnCode == TPM_SUCCESS) {
	    returnCode = TPM_GetOutParamDigest(outParamDigest,	/* output */
					       auditStatus,	/* input audit status */
					       transportEncrypt,
					       tag,			
					       returnCode,
					       ordinal,		/* command ordinal */
					       response->buffer + outParamStart,	/* start */
					       outParamEnd - outParamStart);	/* length */
	}
	/* calculate and set the below the line parameters */
	if ((returnCode == TPM_SUCCESS) && (tag == TPM_TAG_RQU_AUTH1_COMMAND)) {
	    returnCode = TPM_AuthParams_Set(response,
					    *hmacKey,		/* owner HMAC key */
					    auth_session_data,
					    outParamDigest,
					    nonceOdd,
					    continueAuthSession);
	}
	/* audit if required */
	if ((returnCode == TPM_SUCCESS) && auditStatus) {
	    returnCode = TPM_ProcessAudit(tpm_state,
					  transportEncrypt,
					  inParamDigest,
					  outParamDigest,
					  ordinal);
	}
	/* adjust the initial response */
	rcf = TPM_Sbuffer_StoreFinalResponse(response, returnCode, tpm_state);
    }
    /* if there was an error, terminate the session. */
    if (((rcf != 0) ||
	 ((returnCode != TPM_SUCCESS) && (returnCode != TPM_DEFEND_LOCK_RUNNING)) ||
	 !continueAuthSession) &&
	authHandleValid) {
	TPM_AuthSessions_TerminateHandle(tpm_state->tpm_stclear_data.authSessions, authHandle);
    }
    /*
      cleanup
    */
    TPM_SizedBuffer_Delete(&subCap);	/* @1 */
    TPM_SizedBuffer_Delete(&setValue);	/* @2 */
    return rcf;
}

/* TPM_SetCapabilityCommon() is common code for setting a capability from setValue
   
   NOTE: This function assumes that the caller has validated either owner authorization or physical
   presence!
*/

TPM_RESULT TPM_SetCapabilityCommon(tpm_state_t *tpm_state,
				   TPM_BOOL ownerAuthorized,
				   TPM_BOOL presenceAuthorized,
				   TPM_CAPABILITY_AREA capArea, 
				   uint16_t subCap16, 
				   uint32_t subCap32,
				   TPM_SIZED_BUFFER *subCap,
				   TPM_SIZED_BUFFER *setValue)
{
    TPM_RESULT	rc = 0;
    TPM_BOOL	valueBool;
    uint32_t	valueUint32 = 0;	/* start with illegal value */
    
    printf(" TPM_SetCapabilityCommon:\n");
    subCap16 = subCap16;			/* not used */
    subCap = subCap;				/* not used */
    if (rc == 0) {
	if ((capArea == TPM_SET_PERM_FLAGS) ||
	    (capArea == TPM_SET_STCLEAR_FLAGS) ||
	    (capArea == TPM_SET_STANY_FLAGS)) {
	    rc = TPM_SizedBuffer_GetBool(&valueBool, setValue);
	}
	else if (((capArea == TPM_SET_PERM_DATA) && (subCap32 != TPM_PD_DAAPROOF)) ||
		 (capArea == TPM_SET_STCLEAR_DATA)) {	/* deferredPhysicalPresence */
	    rc = TPM_SizedBuffer_GetUint32(&valueUint32, setValue);
	}
    }
    if (rc == 0) {
	switch (capArea) {
	  case TPM_SET_PERM_FLAGS:
	    rc = TPM_SetCapability_CapPermFlags(tpm_state, ownerAuthorized, presenceAuthorized,
						subCap32, valueBool);
	    break;
	  case TPM_SET_PERM_DATA:
	    rc = TPM_SetCapability_CapPermData(tpm_state, ownerAuthorized, presenceAuthorized,
					       subCap32, valueUint32);
	    break;
	  case TPM_SET_STCLEAR_FLAGS:
	    rc = TPM_SetCapability_CapStclearFlags(tpm_state, ownerAuthorized, presenceAuthorized,
						   subCap32, valueBool);
	    break;
	  case TPM_SET_STCLEAR_DATA:
	    rc = TPM_SetCapability_CapStclearData(tpm_state, ownerAuthorized, presenceAuthorized,
						  subCap32, valueUint32);
	    break;
	  case TPM_SET_STANY_FLAGS:
	    rc = TPM_SetCapability_CapStanyFlags(tpm_state, ownerAuthorized, presenceAuthorized,
						 subCap32, valueBool);
	    break;
	  case TPM_SET_STANY_DATA:
	    rc = TPM_SetCapability_CapStanyData(tpm_state, ownerAuthorized, presenceAuthorized,
						subCap32, setValue);
	    break;
	  case TPM_SET_VENDOR:
	    rc = TPM_SetCapability_CapVendor(tpm_state, ownerAuthorized, presenceAuthorized,
					     subCap32, setValue);
	    break;
	  default:
	    printf("TPM_SetCapabilityCommon: Error, unsupported capArea %08x", capArea);
	    rc = TPM_BAD_MODE;
	    break;
	}
    }
    return rc;
}

/* TPM_SetCapability_Flag() tests if the values are not already equal.	If they are not, 'flag' is
   set to 'value' and 'altered' is set TRUE.  Otherwise 'altered' is returned unchanged.

   The 'altered' flag is used by the caller to determine if an NVRAM write is required.
*/

void TPM_SetCapability_Flag(TPM_BOOL *altered,
			    TPM_BOOL *flag,
			    TPM_BOOL value)
{
    /* If the values are not already equal.  Can't use != since there are many values for TRUE. */
    if ((value && !*flag) ||
	(!value && *flag)) {
	*altered = TRUE;
	*flag = value;
    }
    return;
}

/* TPM_SetCapability_CapPermFlags() rev 100

   Sets TPM_PERMANENT_FLAGS values
*/
     
static TPM_RESULT TPM_SetCapability_CapPermFlags(tpm_state_t *tpm_state,
						 TPM_BOOL ownerAuthorized,
						 TPM_BOOL presenceAuthorized,
						 uint32_t subCap32,
						 TPM_BOOL valueBool)
{
    TPM_RESULT	rc = 0;
    TPM_BOOL	altered = FALSE;	/* TRUE if the structure has been changed */
    
    printf(" TPM_SetCapability_CapPermFlags: valueBool %02x\n", valueBool);
    if (rc == 0) {						   
	switch (subCap32) {
	  case TPM_PF_DISABLE:
	    printf("  TPM_SetCapability_CapPermFlags: TPM_PF_DISABLE\n");
	    /* Owner authorization or physical presence
	       TPM_OwnerSetDisable
	       TPM_PhysicalEnable
	       TPM_PhysicalDisable
	    */
	    if (rc == 0) {
		if (!ownerAuthorized && !presenceAuthorized) {
		    printf("TPM_SetCapability_CapPermFlags: Error, no authorization\n");
		    rc = TPM_AUTHFAIL;
		}
	    }
	    if (rc == 0) {
		TPM_SetCapability_Flag(&altered,
				       &(tpm_state->tpm_permanent_flags.disable),
				       valueBool);
	    }
	    break;
	  case TPM_PF_OWNERSHIP:
	    printf("  TPM_SetCapability_CapPermFlags: TPM_PF_OWNERSHIP\n");
	    /* No authorization. No ownerInstalled. Physical presence asserted
	       Not available when TPM deactivated or disabled
	       TPM_SetOwnerInstall
	    */
	    if (rc == 0) {
		if (tpm_state->tpm_permanent_data.ownerInstalled) {
		    printf("TPM_SetCapability_CapPermFlags: Error, owner installed\n");
		    rc = TPM_OWNER_SET;
		}
	    }
	    if (rc == 0) {
		if (!presenceAuthorized) {
		    printf("TPM_SetCapability_CapPermFlags: Error, no physicalPresence\n");
		    rc = TPM_AUTHFAIL;
		}
	    }
	    if (rc == 0) {
		if (tpm_state->tpm_permanent_flags.disable) {
		    printf("TPM_SetCapability_CapPermFlags: Error, disabled\n");
		    rc = TPM_DISABLED;
		}
	    }
	    if (rc == 0) {
		if (tpm_state->tpm_stclear_flags.deactivated) {
		    printf("TPM_SetCapability_CapPermFlags: Error, deactivated\n");
		    rc = TPM_DEACTIVATED;
		}
	    }
	    if (rc == 0) {
		TPM_SetCapability_Flag(&altered,
				       &(tpm_state->tpm_permanent_flags.ownership),
				       valueBool);
	    }
	    break;
	  case TPM_PF_DEACTIVATED:
	    printf("  TPM_SetCapability_CapPermFlags: TPM_PF_DEACTIVATED\n");
	    /* No authorization, physical presence assertion
	       Not available when TPM disabled
	       TPM_PhysicalSetDeactivated
	    */
	    if (rc == 0) {
		if (!presenceAuthorized) {
		    printf("TPM_SetCapability_CapPermFlags: Error, no physicalPresence\n");
		    rc = TPM_AUTHFAIL;
		}
	    }
	    if (rc == 0) {
		if (tpm_state->tpm_permanent_flags.disable) {
		    printf("TPM_SetCapability_CapPermFlags: Error, disabled\n");
		    rc = TPM_DISABLED;
		}
	    }
	    if (rc == 0) {
		TPM_SetCapability_Flag(&altered,
				       &(tpm_state->tpm_permanent_flags.deactivated),
				       valueBool);
	    }
	    break;
	  case TPM_PF_READPUBEK:
	    printf("  TPM_SetCapability_CapPermFlags: TPM_PF_READPUBEK\n");
	    /* Owner authorization
	       Not available when TPM deactivated or disabled
	    */
	    if (rc == 0) {
		if (!ownerAuthorized) {
		    printf("TPM_SetCapability_CapPermFlags: Error, not owner authorized\n");
		    rc = TPM_AUTHFAIL;
		}
	    }
	    if (rc == 0) {
		if (tpm_state->tpm_permanent_flags.disable) {
		    printf("TPM_SetCapability_CapPermFlags: Error, disabled\n");
		    rc = TPM_DISABLED;
		}
	    }
	    if (rc == 0) {
		if (tpm_state->tpm_stclear_flags.deactivated) {
		    printf("TPM_SetCapability_CapPermFlags: Error, deactivated\n");
		    rc = TPM_DEACTIVATED;
		}
	    }
	    if (rc == 0) {
		TPM_SetCapability_Flag(&altered,
				       &(tpm_state->tpm_permanent_flags.readPubek),
				       valueBool);
	    }
	    if (rc == 0) {
		printf("  TPM_SetCapability_CapPermFlags : readPubek %02x\n",
		       tpm_state->tpm_permanent_flags.readPubek);
	    }
	    break;
	  case TPM_PF_DISABLEOWNERCLEAR:
	    printf("  TPM_SetCapability_CapPermFlags: TPM_PF_DISABLEOWNERCLEAR\n");
	    /* Owner authorization. Can only set to TRUE, FALSE invalid value. 
	       After being set only ForceClear resets back to FALSE.
	       Not available when TPM deactivated or disabled
	       TPM_DisableOwnerClear */
	    if (rc == 0) {
		if (!ownerAuthorized) {
		    printf("TPM_SetCapability_CapPermFlags: Error, not owner authorized\n");
		    rc = TPM_AUTHFAIL;
		}
	    }
	    if (rc == 0) {
		if (!valueBool) {
		    printf("TPM_SetCapability_CapPermFlags: Error, cannot set FALSE\n");
		    rc = TPM_BAD_PARAMETER;
		}
	    }
	    if (rc == 0) {
		if (tpm_state->tpm_permanent_flags.disable) {
		    printf("TPM_SetCapability_CapPermFlags: Error, disabled\n");
		    rc = TPM_DISABLED;
		}
	    }
	    if (rc == 0) {
		if (tpm_state->tpm_stclear_flags.deactivated) {
		    printf("TPM_SetCapability_CapPermFlags: Error, deactivated\n");
		    rc = TPM_DEACTIVATED;
		}
	    }
	    if (rc == 0) {
		TPM_SetCapability_Flag(&altered,
				       &(tpm_state->tpm_permanent_flags.disableOwnerClear),
				       valueBool);
	    }
	    break;
	  case TPM_PF_ALLOWMAINTENANCE:
	    printf("  TPM_SetCapability_CapPermFlags: TPM_PF_ALLOWMAINTENANCE\n");
	    /* Owner authorization. Can only set to FALSE, TRUE invalid value. 
	       After being set only changing TPM owner resets back to TRUE
	       Not available when TPM deactivated or disabled
	       TPM_KillMaintenanceFeature
	    */
	    if (rc == 0) {
		if (!ownerAuthorized) {
		    printf("TPM_SetCapability_CapPermFlags: Error, not owner authorized\n");
		    rc = TPM_AUTHFAIL;
		}
	    }
	    if (rc == 0) {
		if (valueBool) {
		    printf("TPM_SetCapability_CapPermFlags: Error, cannot set TRUE\n");
		    rc = TPM_BAD_PARAMETER;
		}
	    }
	    if (rc == 0) {
		if (tpm_state->tpm_permanent_flags.disable) {
		    printf("TPM_SetCapability_CapPermFlags: Error, disabled\n");
		    rc = TPM_DISABLED;
		}
	    }
	    if (rc == 0) {
		if (tpm_state->tpm_stclear_flags.deactivated) {
		    printf("TPM_SetCapability_CapPermFlags: Error, deactivated\n");
		    rc = TPM_DEACTIVATED;
		}
	    }
	    if (rc == 0) {
		TPM_SetCapability_Flag(&altered,
				       &(tpm_state->tpm_permanent_flags.allowMaintenance),
				       valueBool);
	    }
	    break;
	  case TPM_PF_READSRKPUB:
	    printf("  TPM_SetCapability_CapPermFlags: TPM_PF_READSRKPUB\n");
	    /* Owner Authorization
	       Not available when TPM deactivated or disabled
	       TPM_SetCapability
	    */
	    if (rc == 0) {
		if (!ownerAuthorized) {
		    printf("TPM_SetCapability_CapPermFlags: Error, not owner authorized\n");
		    rc = TPM_AUTHFAIL;
		}
	    }
	    if (rc == 0) {
		if (tpm_state->tpm_permanent_flags.disable) {
		    printf("TPM_SetCapability_CapPermFlags: Error, disable is TRUE\n");
		    rc = TPM_DISABLED;
		}
	    }
	    if (rc == 0) {
		if (tpm_state->tpm_stclear_flags.deactivated) {
		    printf("TPM_SetCapability_CapPermFlags: Error, deactivated is TRUE\n");
		    rc = TPM_DEACTIVATED;
		}
	    }
	    if (rc == 0) {
		TPM_SetCapability_Flag(&altered,
				       &(tpm_state->tpm_permanent_flags.readSRKPub),
				       valueBool);
	    }
	    break;
	  case TPM_PF_TPMESTABLISHED:
	    printf("  TPM_SetCapability_CapPermFlags: TPM_PF_TPMESTABLISHED\n");
	    /* Locality 3 or locality 4
	       Can only set to FALSE
	       TPM_ResetEstablishmentBit
	    */
	    if (rc == 0) {
		rc = TPM_Locality_Check(TPM_LOC_THREE | TPM_LOC_FOUR,  /* BYTE bitmap */
					tpm_state->tpm_stany_flags.localityModifier);
	    }
	    if (rc == 0) {
		if (valueBool) {
		    printf("TPM_SetCapability_CapPermFlags: Error, can only set to FALSE\n");
		    rc = TPM_BAD_PARAMETER;
		}
	    }
	    if (rc == 0) {
		TPM_SetCapability_Flag(&altered,
				       &(tpm_state->tpm_permanent_flags.tpmEstablished),
				       valueBool);
	    }
	    break;
#if  (TPM_REVISION >= 103)	/* added for rev 103 */
	  case TPM_PF_DISABLEFULLDALOGICINFO:
	    /* Owner Authorization
	       TPM_SetCapability
	    */
	    printf("  TPM_SetCapability_CapPermFlags: TPM_PF_DISABLEFULLDALOGICINFO\n");
	    if (rc == 0) {
		if (!ownerAuthorized) {
		    printf("TPM_SetCapability_CapPermFlags: Error, not owner authorized\n");
		    rc = TPM_AUTHFAIL;
		}
	    }
	    if (rc == 0) {
		TPM_SetCapability_Flag(&altered,
				       &(tpm_state->tpm_permanent_flags.disableFullDALogicInfo),
				       valueBool);
	    }
	    break;
#endif
	  case TPM_PF_PHYSICALPRESENCELIFETIMELOCK:
	  case TPM_PF_PHYSICALPRESENCEHWENABLE:
	  case TPM_PF_PHYSICALPRESENCECMDENABLE:
	  case TPM_PF_CEKPUSED:
	  case TPM_PF_TPMPOST:
	  case TPM_PF_TPMPOSTLOCK:
	  case TPM_PF_FIPS:
	  case TPM_PF_OPERATOR:
	  case TPM_PF_ENABLEREVOKEEK:
	  case TPM_PF_NV_LOCKED:
	  case TPM_PF_MAINTENANCEDONE:
	  default:
	    printf("TPM_SetCapability_CapPermFlags: Error, bad subCap32 %u\n",
		   subCap32);
	    rc = TPM_BAD_PARAMETER;
	}
    }
    rc = TPM_PermanentAll_NVStore(tpm_state,
				  altered,
				  rc);
    return rc;
}

/* TPM_SetCapability_CapPermData() rev 105

   Sets TPM_PERMANENT_DATA values
*/
     
static TPM_RESULT TPM_SetCapability_CapPermData(tpm_state_t *tpm_state,
						TPM_BOOL ownerAuthorized,
						TPM_BOOL presenceAuthorized,
						uint32_t subCap32,
						uint32_t valueUint32)
{
    TPM_RESULT	rc = 0;
    TPM_BOOL	writeAllNV = FALSE;	/* TRUE if the structure has been changed */
    
    printf(" TPM_SetCapability_CapPermData:\n");
    presenceAuthorized = presenceAuthorized;			/* not used */
    if (rc == 0) {						   
	switch (subCap32) {
	  case TPM_PD_RESTRICTDELEGATE:
	    printf("  TPM_SetCapability_CapPermData: TPM_PD_RESTRICTDELEGATE\n");
	    /* Owner authorization.  Not available when TPM deactivated or disabled */
	    /* TPM_CMK_SetRestrictions */
	    if (rc == 0) {
		if (!ownerAuthorized) {
		    printf("TPM_SetCapability_CapPermData: Error, not owner authorized\n");
		    rc = TPM_AUTHFAIL;
		}
	    }
	    if (rc == 0) {
		if (tpm_state->tpm_permanent_flags.disable) {
		    printf("TPM_SetCapability_CapPermData: Error, disabled\n");
		    rc = TPM_DISABLED;
		}
	    }
	    if (rc == 0) {
		if (tpm_state->tpm_stclear_flags.deactivated) {
		    printf("TPM_SetCapability_CapPermData: Error, deactivated\n");
		    rc = TPM_DEACTIVATED;
		}
	    }
	    if (rc == 0) {
		if (tpm_state->tpm_permanent_data.restrictDelegate != valueUint32) {
		    tpm_state->tpm_permanent_data.restrictDelegate = valueUint32;
		    writeAllNV = TRUE;
		}
	    }
	    break;
	  case TPM_PD_DAAPROOF:
	    /* TPM_PD_DAAPROOF This capability has no value.  When specified by TPM_SetCapability, a
	       new daaProof, tpmDAASeed, and daaBlobKey are generated. */
		rc = TPM_PermanentData_InitDaa(&(tpm_state->tpm_permanent_data));
		writeAllNV = TRUE;
	    break;
	  case TPM_PD_REVMAJOR:
	  case TPM_PD_REVMINOR:
	  case TPM_PD_TPMPROOF:
	  case TPM_PD_OWNERAUTH:
	  case TPM_PD_OPERATORAUTH:
	  case TPM_PD_MANUMAINTPUB:
	  case TPM_PD_ENDORSEMENTKEY:
	  case TPM_PD_SRK:
	  case TPM_PD_DELEGATEKEY:
	  case TPM_PD_CONTEXTKEY:
	  case TPM_PD_AUDITMONOTONICCOUNTER:
	  case TPM_PD_MONOTONICCOUNTER:
	  case TPM_PD_PCRATTRIB:
	  case TPM_PD_ORDINALAUDITSTATUS:
	  case TPM_PD_AUTHDIR:
	  case TPM_PD_RNGSTATE:
	  case TPM_PD_FAMILYTABLE:
	  case TPM_DELEGATETABLE:
	  case TPM_PD_EKRESET:
	  case TPM_PD_LASTFAMILYID:
	  case TPM_PD_NOOWNERNVWRITE:
	  case TPM_PD_TPMDAASEED:
	  default:
	    printf("TPM_SetCapability_CapPermData: Error, bad subCap32 %u\n",
		   subCap32);
	    rc = TPM_BAD_PARAMETER;
	}
    }
    rc = TPM_PermanentAll_NVStore(tpm_state,
				  writeAllNV,
				  rc);
    return rc;
}

/* TPM_SetCapability_CapStclearFlags() rev 85

   Sets TPM_STCLEAR_FLAGS values
*/
     
static TPM_RESULT TPM_SetCapability_CapStclearFlags(tpm_state_t *tpm_state,
						    TPM_BOOL ownerAuthorized,
						    TPM_BOOL presenceAuthorized,
						    uint32_t subCap32,
						    TPM_BOOL valueBool)
{
    TPM_RESULT			rc = 0;

    printf(" TPM_SetCapability_CapStclearFlags: valueBool %02x\n", valueBool);
    ownerAuthorized = ownerAuthorized;		/* not used */
    presenceAuthorized = presenceAuthorized;	/* not used */
    if (rc == 0) {						   
	switch (subCap32) {
	  case TPM_SF_DISABLEFORCECLEAR:
	    printf("  TPM_SetCapability_CapStclearFlags: TPM_SF_DISABLEFORCECLEAR\n");
	    /* Not available when TPM deactivated or disabled */
	    /* TPM_DisableForceClear */
	    if (rc == 0) {
		if (tpm_state->tpm_permanent_flags.disable) {
		    printf("TPM_SetCapability_CapStclearFlags: Error, disabled\n");
		    rc = TPM_DISABLED;
		}
	    }
	    if (rc == 0) {
		if (tpm_state->tpm_stclear_flags.deactivated) {
		    printf("TPM_SetCapability_CapStclearFlags: Error, deactivated\n");
		    rc = TPM_DEACTIVATED;
		}
	    }
	    /* Can only set to TRUE */
	    if (rc == 0) {
		if (!valueBool) {
		    printf("TPM_SetCapability_CapStclearFlags: Error, cannot set FALSE\n");
		    rc = TPM_BAD_PARAMETER;
		}
	    }
	    if (rc == 0) {
		tpm_state->tpm_stclear_flags.disableForceClear = TRUE;
	    }
	    break;
	  case TPM_SF_DEACTIVATED:
	  case TPM_SF_PHYSICALPRESENCE:
	  case TPM_SF_PHYSICALPRESENCELOCK:
	  case TPM_SF_BGLOBALLOCK:
	  default:
	    printf("TPM_SetCapability_CapStclearFlags: Error, bad subCap32 %u\n",
		   subCap32);
	    rc = TPM_BAD_PARAMETER;
	}
    }
    return rc;
}

/* TPM_SetCapability_CapStclearData() rev 100

   Sets TPM_STCLEAR_DATA values
*/
     
static TPM_RESULT TPM_SetCapability_CapStclearData(tpm_state_t *tpm_state,
						   TPM_BOOL ownerAuthorized,
						   TPM_BOOL presenceAuthorized,
						   uint32_t subCap32,
						   uint32_t valueUint32)
{
    TPM_RESULT			rc = 0;
#if  (TPM_REVISION < 103)	/* added for rev 103 */
    tpm_state = tpm_state;	/* to quiet the compiler */
    presenceAuthorized = presenceAuthorized;
    valueUint32 = valueUint32;
#endif

    printf(" TPM_SetCapability_CapStclearData:\n");
    ownerAuthorized = ownerAuthorized;		/* not used */
    if (rc == 0) {						   
	switch (subCap32) {
#if  (TPM_REVISION >= 103)	/* added for rev 103 */
	  case TPM_SD_DEFERREDPHYSICALPRESENCE:
	    printf("  TPM_SetCapability_CapStclearData: TPM_SD_DEFERREDPHYSICALPRESENCE\n");
	    /* Can only set to TRUE if PhysicalPresence is asserted.  Can set to FALSE at any
	       time. */
	    /* 1. If physical presence is not asserted */
	    /* a. If TPM_SetCapability -> setValue has a bit set that is not already set in
	       TPM_STCLEAR_DATA -> deferredPhysicalPresence, return TPM_BAD_PRESENCE. */
	    if (rc == 0) {
		if (!presenceAuthorized) {
		    if (~(tpm_state->tpm_stclear_data.deferredPhysicalPresence) & valueUint32) {
			printf("TPM_SetCapability_CapStclearData: "
			       "Error, no physicalPresence and deferredPhysicalPresence %08x\n",
			       tpm_state->tpm_stclear_data.deferredPhysicalPresence);
			rc = TPM_BAD_PRESENCE;
		    }
		}
	    }
	    /* 2.Set TPM_STCLEAR_DATA -> deferredPhysicalPresence to TPM_SetCapability -> setValue.
	    */
	    if (rc == 0) {
		printf("   TPM_SetCapability_CapStclearData: deferredPhysicalPresence now %08x\n",
		       valueUint32);
		tpm_state->tpm_stclear_data.deferredPhysicalPresence = valueUint32;
	    }
	    break;
#endif
	  case TPM_SD_CONTEXTNONCEKEY:
	  case TPM_SD_COUNTID:
	  case TPM_SD_OWNERREFERENCE:
	  case TPM_SD_DISABLERESETLOCK:
	  case TPM_SD_PCR:
	  default:
	    printf("TPM_SetCapability_CapStclearData: Error, bad subCap32 %u\n",
		   subCap32);
	    rc = TPM_BAD_PARAMETER;
	}
    }
    return rc;
}

/* TPM_SetCapability_CapStanyFlags() rev 85

   Sets TPM_STANY_FLAGS values
*/
     
static TPM_RESULT TPM_SetCapability_CapStanyFlags(tpm_state_t *tpm_state,
						  TPM_BOOL ownerAuthorized,
						  TPM_BOOL presenceAuthorized,
						  uint32_t subCap32,
						  TPM_BOOL valueBool)
{
    TPM_RESULT			rc = 0;

    printf(" TPM_SetCapability_CapStanyFlags:\n");
    ownerAuthorized = ownerAuthorized;			/* not used */
    presenceAuthorized = presenceAuthorized;		/* not used */
    if (rc == 0) {						   
	switch (subCap32) {
	  case TPM_AF_TOSPRESENT:
	    printf("  TPM_SetCapability_CapStanyFlags: TPM_AF_TOSPRESENT\n");
	    /* locality 3 or 4 */
	    /* Not available when TPM deactivated or disabled */
	    if (rc == 0) {
		rc = TPM_Locality_Check(TPM_LOC_THREE | TPM_LOC_FOUR,
					tpm_state->tpm_stany_flags.localityModifier);
	    }
	    if (rc == 0) {
		if (tpm_state->tpm_permanent_flags.disable) {
		    printf("TPM_SetCapability_CapStanyFlags: Error, disabled\n");
		    rc = TPM_DISABLED;
		}
	    }
	    if (rc == 0) {
		if (tpm_state->tpm_stclear_flags.deactivated) {
		    printf("TPM_SetCapability_CapStanyFlags: Error, deactivated\n");
		    rc = TPM_DEACTIVATED;
		}
	    }
	    /* can only be set to FALSE */
	    if (rc == 0) {
		if (valueBool) {
		    printf("TPM_SetCapability_CapStanyFlags: Error, cannot set TRUE\n");
		    rc = TPM_BAD_PARAMETER;
		}
	    }
	    if (rc == 0) {
		tpm_state->tpm_stany_flags.TOSPresent = FALSE;
	    }
	    break;
	  case TPM_AF_POSTINITIALISE:
	  case TPM_AF_LOCALITYMODIFIER:
	  case TPM_AF_TRANSPORTEXCLUSIVE:
	  default:
	    printf("TPM_SetCapability_CapStanyFlags: Error, bad subCap32 %u\n",
		   subCap32);
	    rc = TPM_BAD_PARAMETER;
	}
    }
    return rc;
}

/* TPM_SetCapability_CapStanyData() rev 85

   Sets TPM_STANY_DATA values
*/

static TPM_RESULT TPM_SetCapability_CapStanyData(tpm_state_t *tpm_state,
						 TPM_BOOL ownerAuthorized,
						 TPM_BOOL presenceAuthorized,
						 uint32_t subCap32,
						 TPM_SIZED_BUFFER *setValue)
{
    TPM_RESULT			rc = 0;

    printf(" TPM_SetCapability_CapStanyData:\n");
    tpm_state = tpm_state;			/* not used */
    ownerAuthorized = ownerAuthorized;		/* not used */
    presenceAuthorized = presenceAuthorized;	/* not used */
    setValue = setValue;			/* not used */
    if (rc == 0) {						   
	switch (subCap32) {
	  case TPM_AD_CONTEXTNONCESESSION:
	  case TPM_AD_AUDITDIGEST:
	  case TPM_AD_CURRENTTICKS:
	  case TPM_AD_CONTEXTCOUNT:
	  case TPM_AD_CONTEXTLIST:
	  case TPM_AD_SESSIONS:
	  default:
	    printf("TPM_SetCapability_CapStanyData: Error, bad subCap32 %u\n",
		   subCap32);
	    rc = TPM_BAD_PARAMETER;
	}				
    }
    return rc;
}

/* These are subCaps to TPM_SetCapability -> TPM_SET_VENDOR capArea, the vendor specific area.
*/

static TPM_RESULT TPM_SetCapability_CapVendor(tpm_state_t *tpm_state,
					      TPM_BOOL ownerAuthorized,
					      TPM_BOOL presenceAuthorized,
					      uint32_t subCap32,
					      TPM_SIZED_BUFFER *setValue)
{
    TPM_RESULT			rc = 0;
    
    printf(" TPM_SetCapability_CapVendor:\n");
    ownerAuthorized = ownerAuthorized;		/* not used */
    presenceAuthorized = presenceAuthorized;	/* not used */
    setValue = setValue;
    /* make temporary copies so the setValue is not touched */
    if (rc == 0) {
	switch(subCap32) {
	  default:
	    printf("TPM_SetCapability_CapVendor: Error, unsupported subCap %08x\n", subCap32);
	    tpm_state = tpm_state;			/* not used */
	    rc = TPM_BAD_PARAMETER;
	    break;

	}
    }
    return rc;
}