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
|
/*
* Licensed Materials - Property of IBM
*
* trousers - An open source TCG Software Stack
*
* (C) Copyright International Business Machines Corp. 2004-2006
*
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include "trousers/tss.h"
#include "trousers_types.h"
#include "trousers_types.h"
#include "tcs_tsp.h"
#include "tcs_utils.h"
#include "tcs_int_literals.h"
#include "capabilities.h"
#include "tcslog.h"
#include "tcsps.h"
#include "req_mgr.h"
#include "tcs_key_ps.h"
/*
* mem_cache_lock will be responsible for protecting the key_mem_cache_head list. This is a
* TCSD global linked list of all keys which have been loaded into the TPM at some time.
*/
MUTEX_DECLARE_INIT(mem_cache_lock);
/*
* tcs_keyhandle_lock is only used to make TCS keyhandle generation atomic for all TCSD
* threads.
*/
static MUTEX_DECLARE_INIT(tcs_keyhandle_lock);
/*
* timestamp_lock is only used to make TCS key timestamp generation atomic for all TCSD
* threads.
*/
static MUTEX_DECLARE_INIT(timestamp_lock);
TCS_KEY_HANDLE
getNextTcsKeyHandle()
{
static TCS_KEY_HANDLE NextTcsKeyHandle = 0x22330000;
TCS_KEY_HANDLE ret;
MUTEX_LOCK(tcs_keyhandle_lock);
do {
ret = NextTcsKeyHandle++;
} while (NextTcsKeyHandle == SRK_TPM_HANDLE || NextTcsKeyHandle == NULL_TCS_HANDLE);
MUTEX_UNLOCK(tcs_keyhandle_lock);
return ret;
}
UINT32
getNextTimeStamp()
{
static UINT32 time_stamp = 1;
UINT32 ret;
MUTEX_LOCK(timestamp_lock);
ret = time_stamp++;
MUTEX_UNLOCK(timestamp_lock);
return ret;
}
/* only called from load key paths, so no locking */
TCPA_STORE_PUBKEY *
mc_get_pub_by_slot(TCPA_KEY_HANDLE tpm_handle)
{
struct key_mem_cache *tmp;
TCPA_STORE_PUBKEY *ret;
if (tpm_handle == NULL_TPM_HANDLE)
return NULL;
for (tmp = key_mem_cache_head; tmp; tmp = tmp->next) {
LogDebugFn("TCSD mem_cached handle: 0x%x",
tmp->tcs_handle);
if (tmp->tpm_handle == tpm_handle) {
ret = tmp->blob ? &tmp->blob->pubKey : NULL;
return ret;
}
}
LogDebugFn("returning NULL TCPA_STORE_PUBKEY");
return NULL;
}
/* only called from load key paths, so no locking */
TCPA_STORE_PUBKEY *
mc_get_pub_by_handle(TCS_KEY_HANDLE tcs_handle)
{
struct key_mem_cache *tmp;
TCPA_STORE_PUBKEY *ret;
LogDebugFn("looking for 0x%x", tcs_handle);
for (tmp = key_mem_cache_head; tmp; tmp = tmp->next) {
LogDebugFn("TCSD mem_cached handle: 0x%x",
tmp->tcs_handle);
if (tmp->tcs_handle == tcs_handle) {
ret = tmp->blob ? &tmp->blob->pubKey : NULL;
return ret;
}
}
LogDebugFn("returning NULL TCPA_STORE_PUBKEY");
return NULL;
}
/* only called from load key paths, so no locking */
TSS_RESULT
mc_set_parent_by_handle(TCS_KEY_HANDLE tcs_handle, TCS_KEY_HANDLE p_tcs_handle)
{
struct key_mem_cache *tmp, *parent;
/* find parent */
for (tmp = key_mem_cache_head; tmp; tmp = tmp->next) {
LogDebug("TCSD mem_cached handle: 0x%x", tmp->tcs_handle);
if (tmp->tcs_handle == p_tcs_handle) {
parent = tmp;
break;
}
}
/* didn't find parent */
if (tmp == NULL)
goto done;
/* set parent blob in child */
for (tmp = key_mem_cache_head; tmp; tmp = tmp->next) {
if (tmp->tcs_handle == tcs_handle) {
tmp->parent = parent;
return TSS_SUCCESS;
}
}
done:
return TCSERR(TSS_E_FAIL);
}
TCPA_RESULT
ensureKeyIsLoaded(TCS_CONTEXT_HANDLE hContext, TCS_KEY_HANDLE keyHandle, TCPA_KEY_HANDLE * keySlot)
{
TCPA_RESULT result = TSS_SUCCESS;
TCPA_STORE_PUBKEY *myPub;
LogDebugFn("0x%x", keyHandle);
if (!ctx_has_key_loaded(hContext, keyHandle))
return TCSERR(TCS_E_INVALID_KEY);
MUTEX_LOCK(mem_cache_lock);
*keySlot = mc_get_slot_by_handle(keyHandle);
LogDebug("keySlot is %08X", *keySlot);
if (*keySlot == NULL_TPM_HANDLE || isKeyLoaded(*keySlot) == FALSE) {
LogDebug("calling mc_get_pub_by_handle");
if ((myPub = mc_get_pub_by_handle(keyHandle)) == NULL) {
LogDebug("Failed to find pub by handle");
result = TCSERR(TCS_E_KM_LOADFAILED);
goto done;
}
LogDebugFn("calling LoadKeyShim");
if ((result = LoadKeyShim(hContext, myPub, NULL, keySlot))) {
LogDebug("Failed shim");
goto done;
}
if (*keySlot == NULL_TPM_HANDLE) {
LogDebug("Key slot is still invalid after ensureKeyIsLoaded");
result = TCSERR(TCS_E_KM_LOADFAILED);
goto done;
}
}
mc_update_time_stamp(*keySlot);
done:
MUTEX_UNLOCK(mem_cache_lock);
LogDebugFn("Exit");
return result;
}
/* only called from load key paths, so no locking */
TSS_UUID *
mc_get_uuid_by_pub(TCPA_STORE_PUBKEY *pub)
{
TSS_UUID *ret;
struct key_mem_cache *tmp;
for (tmp = key_mem_cache_head; tmp; tmp = tmp->next) {
LogDebugFn("TCSD mem_cached handle: 0x%x", tmp->tcs_handle);
if (tmp->blob &&
tmp->blob->pubKey.keyLength == pub->keyLength &&
!memcmp(tmp->blob->pubKey.key, pub->key, pub->keyLength)) {
ret = &tmp->uuid;
return ret;
}
}
return NULL;
}
TSS_RESULT
mc_get_handles_by_uuid(TSS_UUID *uuid, TCS_KEY_HANDLE *tcsHandle, TCPA_KEY_HANDLE *slot)
{
struct key_mem_cache *tmp;
for (tmp = key_mem_cache_head; tmp; tmp = tmp->next) {
if (!memcmp(&tmp->uuid, uuid, sizeof(TSS_UUID))) {
*tcsHandle = tmp->tcs_handle;
*slot = tmp->tpm_handle;
return TSS_SUCCESS;
}
}
return TCSERR(TSS_E_FAIL);
}
TCS_KEY_HANDLE
mc_get_handle_by_encdata(BYTE *encData)
{
struct key_mem_cache *tmp;
TCS_KEY_HANDLE ret;
MUTEX_LOCK(mem_cache_lock);
for (tmp = key_mem_cache_head; tmp; tmp = tmp->next) {
LogDebugFn("TCSD mem_cached handle: 0x%x", tmp->tcs_handle);
if (!tmp->blob || tmp->blob->encSize == 0)
continue;
if (!memcmp(tmp->blob->encData, encData, tmp->blob->encSize)) {
ret = tmp->tcs_handle;
MUTEX_UNLOCK(mem_cache_lock);
return ret;
}
}
MUTEX_UNLOCK(mem_cache_lock);
return 0;
}
TSS_RESULT
mc_update_encdata(BYTE *encData, BYTE *newEncData)
{
struct key_mem_cache *tmp;
BYTE *tmp_enc_data;
MUTEX_LOCK(mem_cache_lock);
for (tmp = key_mem_cache_head; tmp; tmp = tmp->next) {
LogDebugFn("TCSD mem_cached handle: 0x%x", tmp->tcs_handle);
if (!tmp->blob || tmp->blob->encSize == 0)
continue;
if (!memcmp(tmp->blob->encData, encData, tmp->blob->encSize)) {
tmp_enc_data = (BYTE *)malloc(tmp->blob->encSize);
if (tmp_enc_data == NULL) {
LogError("malloc of %u bytes failed.", tmp->blob->encSize);
MUTEX_UNLOCK(mem_cache_lock);
return TCSERR(TSS_E_OUTOFMEMORY);
}
memcpy(tmp_enc_data, newEncData, tmp->blob->encSize);
free(tmp->blob->encData);
tmp->blob->encData = tmp_enc_data;
MUTEX_UNLOCK(mem_cache_lock);
return TSS_SUCCESS;
}
}
MUTEX_UNLOCK(mem_cache_lock);
LogError("Couldn't find requested encdata in mem cache");
return TCSERR(TSS_E_INTERNAL_ERROR);
}
/*
* only called from load key paths and the init (single thread time) path,
* so no locking
*/
TSS_RESULT
mc_add_entry(TCS_KEY_HANDLE tcs_handle,
TCPA_KEY_HANDLE tpm_handle,
TSS_KEY *key_blob)
{
struct key_mem_cache *entry, *tmp;
/* Make sure the cache doesn't already have an entry for this key */
for (tmp = key_mem_cache_head; tmp; tmp = tmp->next) {
LogDebugFn("TCSD mem_cached handle: 0x%x", tmp->tcs_handle);
if (tcs_handle == tmp->tcs_handle) {
return TSS_SUCCESS;
}
}
/* Not found - we need to create a new entry */
entry = (struct key_mem_cache *)calloc(1, sizeof(struct key_mem_cache));
if (entry == NULL) {
LogError("malloc of %zd bytes failed.", sizeof(struct key_mem_cache));
return TCSERR(TSS_E_OUTOFMEMORY);
}
entry->tcs_handle = tcs_handle;
if (tpm_handle != NULL_TPM_HANDLE)
entry->time_stamp = getNextTimeStamp();
entry->tpm_handle = tpm_handle;
if (!key_blob)
goto add;
/* allocate space for the blob */
entry->blob = calloc(1, sizeof(TSS_KEY));
if (entry->blob == NULL) {
LogError("malloc of %zd bytes failed.", sizeof(TSS_KEY));
free(entry);
return TCSERR(TSS_E_OUTOFMEMORY);
}
memcpy(entry->blob, key_blob, sizeof(TSS_KEY));
/* allocate space for the key parameters if necessary */
if (key_blob->algorithmParms.parmSize) {
BYTE *tmp_parms = (BYTE *)malloc(key_blob->algorithmParms.parmSize);
if (tmp_parms == NULL) {
LogError("malloc of %u bytes failed.", key_blob->algorithmParms.parmSize);
free(entry->blob);
free(entry);
return TCSERR(TSS_E_OUTOFMEMORY);
}
memcpy(tmp_parms, key_blob->algorithmParms.parms, key_blob->algorithmParms.parmSize);
entry->blob->algorithmParms.parms = tmp_parms;
}
entry->blob->algorithmParms.parmSize = key_blob->algorithmParms.parmSize;
/* allocate space for the public key */
if (key_blob->pubKey.keyLength > 0) {
entry->blob->pubKey.key = (BYTE *)malloc(key_blob->pubKey.keyLength);
if (entry->blob->pubKey.key == NULL) {
LogError("malloc of %u bytes failed.", key_blob->pubKey.keyLength);
free(entry->blob->algorithmParms.parms);
free(entry->blob);
free(entry);
return TCSERR(TSS_E_OUTOFMEMORY);
}
memcpy(entry->blob->pubKey.key, key_blob->pubKey.key, key_blob->pubKey.keyLength);
}
entry->blob->pubKey.keyLength = key_blob->pubKey.keyLength;
/* allocate space for the PCR info */
if (key_blob->PCRInfoSize > 0) {
entry->blob->PCRInfo = (BYTE *)malloc(key_blob->PCRInfoSize);
if (entry->blob->PCRInfo == NULL) {
LogError("malloc of %u bytes failed.", key_blob->PCRInfoSize);
free(entry->blob->pubKey.key);
free(entry->blob->algorithmParms.parms);
free(entry->blob);
free(entry);
return TCSERR(TSS_E_OUTOFMEMORY);
}
memcpy(entry->blob->PCRInfo, key_blob->PCRInfo, key_blob->PCRInfoSize);
}
entry->blob->PCRInfoSize = key_blob->PCRInfoSize;
/* allocate space for the encData if necessary */
if (key_blob->encSize > 0) {
entry->blob->encData = (BYTE *)malloc(key_blob->encSize);
if (entry->blob->encData == NULL) {
LogError("malloc of %u bytes failed.", key_blob->encSize);
free(entry->blob->PCRInfo);
free(entry->blob->pubKey.key);
free(entry->blob->algorithmParms.parms);
free(entry->blob);
free(entry);
return TCSERR(TSS_E_OUTOFMEMORY);
}
memcpy(entry->blob->encData, key_blob->encData, key_blob->encSize);
}
entry->blob->encSize = key_blob->encSize;
add:
/* add to the front of the list */
entry->next = key_mem_cache_head;
if (key_mem_cache_head) {
/* set the reference count to 0 initially for all keys not being the SRK. Up
* the call chain, a reference to this mem cache entry will be set in the
* context object of the calling context and this reference count will be
* incremented there. */
entry->ref_cnt = 0;
key_mem_cache_head->prev = entry;
} else {
/* if we are the SRK, initially set the reference count to 1, so that it is
* always seen as loaded in the TPM. */
entry->ref_cnt = 1;
}
key_mem_cache_head = entry;
return TSS_SUCCESS;
}
/* caller must lock the mem cache before calling! */
TSS_RESULT
mc_remove_entry(TCS_KEY_HANDLE tcs_handle)
{
struct key_mem_cache *cur;
for (cur = key_mem_cache_head; cur; cur = cur->next) {
if (cur->tcs_handle == tcs_handle) {
if (cur->blob) {
destroy_key_refs(cur->blob);
free(cur->blob);
}
if (cur->prev != NULL)
cur->prev->next = cur->next;
if (cur->next != NULL)
cur->next->prev = cur->prev;
if (cur == key_mem_cache_head)
key_mem_cache_head = cur->next;
free(cur);
return TSS_SUCCESS;
}
}
return TCSERR(TSS_E_FAIL);
}
TSS_RESULT
mc_add_entry_init(TCS_KEY_HANDLE tcs_handle,
TCPA_KEY_HANDLE tpm_handle,
TSS_KEY *key_blob,
TSS_UUID *uuid)
{
struct key_mem_cache *entry, *tmp;
/* Make sure the cache doesn't already have an entry for this key */
MUTEX_LOCK(mem_cache_lock);
for (tmp = key_mem_cache_head; tmp; tmp = tmp->next) {
if (tcs_handle == tmp->tcs_handle) {
mc_remove_entry(tcs_handle);
}
}
MUTEX_UNLOCK(mem_cache_lock);
/* Not found - we need to create a new entry */
entry = (struct key_mem_cache *)calloc(1, sizeof(struct key_mem_cache));
if (entry == NULL) {
LogError("malloc of %zd bytes failed.", sizeof(struct key_mem_cache));
return TCSERR(TSS_E_OUTOFMEMORY);
}
entry->tcs_handle = tcs_handle;
if (tpm_handle != NULL_TPM_HANDLE)
entry->time_stamp = getNextTimeStamp();
entry->tpm_handle = tpm_handle;
if (key_blob) {
/* allocate space for the blob */
entry->blob = malloc(sizeof(TSS_KEY));
if (entry->blob == NULL) {
LogError("malloc of %zd bytes failed.", sizeof(TSS_KEY));
free(entry);
return TCSERR(TSS_E_OUTOFMEMORY);
}
memcpy(entry->blob, key_blob, sizeof(TSS_KEY));
/* allocate space for the key parameters if necessary */
if (key_blob->algorithmParms.parmSize) {
BYTE *tmp_parms = (BYTE *)malloc(key_blob->algorithmParms.parmSize);
if (tmp_parms == NULL) {
LogError("malloc of %u bytes failed.",
key_blob->algorithmParms.parmSize);
free(entry->blob);
free(entry);
return TCSERR(TSS_E_OUTOFMEMORY);
}
memcpy(tmp_parms, key_blob->algorithmParms.parms,
key_blob->algorithmParms.parmSize);
entry->blob->algorithmParms.parms = tmp_parms;
}
/* allocate space for the public key */
entry->blob->pubKey.key = (BYTE *)malloc(key_blob->pubKey.keyLength);
if (entry->blob->pubKey.key == NULL) {
LogError("malloc of %u bytes failed.", key_blob->pubKey.keyLength);
free(entry->blob);
free(entry);
return TCSERR(TSS_E_OUTOFMEMORY);
}
memcpy(entry->blob->pubKey.key, key_blob->pubKey.key, key_blob->pubKey.keyLength);
/* allocate space for the encData if necessary */
if (key_blob->encSize != 0) {
entry->blob->encData = (BYTE *)malloc(key_blob->encSize);
if (entry->blob->encData == NULL) {
LogError("malloc of %u bytes failed.", key_blob->encSize);
free(entry->blob->pubKey.key);
free(entry->blob);
free(entry);
return TCSERR(TSS_E_OUTOFMEMORY);
}
memcpy(entry->blob->encData, key_blob->encData, key_blob->encSize);
}
entry->blob->encSize = key_blob->encSize;
}
memcpy(&entry->uuid, uuid, sizeof(TSS_UUID));
MUTEX_LOCK(mem_cache_lock);
entry->next = key_mem_cache_head;
if (key_mem_cache_head)
key_mem_cache_head->prev = entry;
entry->ref_cnt = 1;
key_mem_cache_head = entry;
MUTEX_UNLOCK(mem_cache_lock);
return TSS_SUCCESS;
}
/* only called from evict key paths, so no locking */
TSS_RESULT
mc_set_slot_by_slot(TCPA_KEY_HANDLE old_handle, TCPA_KEY_HANDLE new_handle)
{
struct key_mem_cache *tmp;
for (tmp = key_mem_cache_head; tmp; tmp = tmp->next) {
if (tmp->tpm_handle == old_handle) {
LogDebugFn("Set TCS key 0x%x, old TPM handle: 0x%x "
"new TPM handle: 0x%x", tmp->tcs_handle,
old_handle, new_handle);
if (new_handle == NULL_TPM_HANDLE)
tmp->time_stamp = 0;
else
tmp->time_stamp = getNextTimeStamp();
tmp->tpm_handle = new_handle;
return TSS_SUCCESS;
}
}
return TCSERR(TSS_E_FAIL);
}
/* only called from load key paths, so no locking */
TSS_RESULT
mc_set_slot_by_handle(TCS_KEY_HANDLE tcs_handle, TCPA_KEY_HANDLE tpm_handle)
{
struct key_mem_cache *tmp;
for (tmp = key_mem_cache_head; tmp; tmp = tmp->next) {
LogDebug("TCSD mem_cached handle: 0x%x", tmp->tcs_handle);
if (tmp->tcs_handle == tcs_handle) {
if (tpm_handle == NULL_TPM_HANDLE)
tmp->time_stamp = 0;
else
tmp->time_stamp = getNextTimeStamp();
tmp->tpm_handle = tpm_handle;
return TSS_SUCCESS;
}
}
return TCSERR(TSS_E_FAIL);
}
/* the beginnings of a key manager start here ;-) */
TSS_RESULT
key_mgr_evict(TCS_CONTEXT_HANDLE hContext, TCS_KEY_HANDLE hKey)
{
TSS_RESULT result = TCS_SUCCESS;
if ((result = ctx_remove_key_loaded(hContext, hKey)))
return result;
if ((result = key_mgr_dec_ref_count(hKey)))
return result;
key_mgr_ref_count();
return result;
}
TSS_RESULT
key_mgr_load_by_blob(TCS_CONTEXT_HANDLE hContext, TCS_KEY_HANDLE hUnwrappingKey,
UINT32 cWrappedKeyBlob, BYTE *rgbWrappedKeyBlob,
TPM_AUTH *pAuth, TCS_KEY_HANDLE *phKeyTCSI, TCS_KEY_HANDLE *phKeyHMAC)
{
TSS_RESULT result;
/* Check that auth for the parent key is loaded outside the mem_cache_lock. We have to do
* this here because if the TPM can't process this request right now, the thread could be
* put to sleep while holding the mem_cache_lock, which would result in a deadlock */
if (pAuth) {
if ((result = auth_mgr_check(hContext, &pAuth->AuthHandle)))
return result;
}
MUTEX_LOCK(mem_cache_lock);
if (TPM_VERSION_IS(1,2)) {
result = TCSP_LoadKey2ByBlob_Internal(hContext, hUnwrappingKey, cWrappedKeyBlob,
rgbWrappedKeyBlob, pAuth, phKeyTCSI);
} else {
result = TCSP_LoadKeyByBlob_Internal(hContext, hUnwrappingKey, cWrappedKeyBlob,
rgbWrappedKeyBlob, pAuth, phKeyTCSI,
phKeyHMAC);
}
MUTEX_UNLOCK(mem_cache_lock);
return result;
}
/* create a reference to one key. This is called from the key_mgr_load_*
* functions only, so no locking is done.
*/
TSS_RESULT
key_mgr_inc_ref_count(TCS_KEY_HANDLE key_handle)
{
struct key_mem_cache *cur;
for (cur = key_mem_cache_head; cur; cur = cur->next) {
LogDebugFn("TCSD mem_cached handle: 0x%x", cur->tcs_handle);
if (cur->tcs_handle == key_handle) {
cur->ref_cnt++;
return TSS_SUCCESS;
}
}
return TCSERR(TSS_E_FAIL);
}
/* de-reference one key. This is called by the context routines, so
* locking is necessary.
*/
TSS_RESULT
key_mgr_dec_ref_count(TCS_KEY_HANDLE key_handle)
{
struct key_mem_cache *cur;
MUTEX_LOCK(mem_cache_lock);
for (cur = key_mem_cache_head; cur; cur = cur->next) {
if (cur->tcs_handle == key_handle) {
cur->ref_cnt--;
LogDebugFn("decrementing ref cnt for key 0x%x",
key_handle);
MUTEX_UNLOCK(mem_cache_lock);
return TSS_SUCCESS;
}
}
MUTEX_UNLOCK(mem_cache_lock);
return TCSERR(TSS_E_FAIL);
}
/* run through the global list and free any keys with reference counts of 0 */
void
key_mgr_ref_count()
{
struct key_mem_cache *tmp, *cur;
MUTEX_LOCK(mem_cache_lock);
for (cur = key_mem_cache_head; cur;) {
if (cur->ref_cnt == 0) {
if (cur->tpm_handle != NULL_TPM_HANDLE) {
LogDebugFn("Key 0x%x being freed from TPM", cur->tpm_handle);
internal_EvictByKeySlot(cur->tpm_handle);
}
LogDebugFn("Key 0x%x being freed", cur->tcs_handle);
if (cur->blob) {
destroy_key_refs(cur->blob);
free(cur->blob);
}
if (cur->prev != NULL)
cur->prev->next = cur->next;
if (cur->next != NULL)
cur->next->prev = cur->prev;
tmp = cur;
if (cur == key_mem_cache_head)
key_mem_cache_head = cur->next;
cur = cur->next;
free(tmp);
} else {
cur = cur->next;
}
}
MUTEX_UNLOCK(mem_cache_lock);
}
/* only called from load key paths, so no locking */
TCPA_KEY_HANDLE
mc_get_slot_by_handle(TCS_KEY_HANDLE tcs_handle)
{
struct key_mem_cache *tmp;
TCS_KEY_HANDLE ret;
for (tmp = key_mem_cache_head; tmp; tmp = tmp->next) {
LogDebugFn("TCSD mem_cached handle: 0x%x", tmp->tcs_handle);
if (tmp->tcs_handle == tcs_handle) {
ret = tmp->tpm_handle;
return ret;
}
}
LogDebugFn("returning NULL_TPM_HANDLE");
return NULL_TPM_HANDLE;
}
/* called from functions outside the load key path */
TCPA_KEY_HANDLE
mc_get_slot_by_handle_lock(TCS_KEY_HANDLE tcs_handle)
{
struct key_mem_cache *tmp;
TCS_KEY_HANDLE ret;
MUTEX_LOCK(mem_cache_lock);
for (tmp = key_mem_cache_head; tmp; tmp = tmp->next) {
LogDebugFn("TCSD mem_cached handle: 0x%x", tmp->tcs_handle);
if (tmp->tcs_handle == tcs_handle) {
ret = tmp->tpm_handle;
MUTEX_UNLOCK(mem_cache_lock);
return ret;
}
}
MUTEX_UNLOCK(mem_cache_lock);
LogDebugFn("returning NULL_TPM_HANDLE");
return NULL_TPM_HANDLE;
}
/* only called from load key paths, so no locking */
TCPA_KEY_HANDLE
mc_get_slot_by_pub(TCPA_STORE_PUBKEY *pub)
{
struct key_mem_cache *tmp;
TCPA_KEY_HANDLE ret;
for (tmp = key_mem_cache_head; tmp; tmp = tmp->next) {
LogDebugFn("TCSD mem_cached handle: 0x%x", tmp->tcs_handle);
if (tmp->blob &&
!memcmp(tmp->blob->pubKey.key, pub->key, pub->keyLength)) {
ret = tmp->tpm_handle;
return ret;
}
}
LogDebugFn("returning NULL_TPM_HANDLE");
return NULL_TPM_HANDLE;
}
/* Check the mem cache for a key with public key pub. If a parent TCS key handle
* is passed in, make sure the parent of the key find matches it, else return
* key not found */
/* only called from load key paths, so no locking */
TCS_KEY_HANDLE
mc_get_handle_by_pub(TCPA_STORE_PUBKEY *pub, TCS_KEY_HANDLE parent)
{
struct key_mem_cache *tmp;
for (tmp = key_mem_cache_head; tmp; tmp = tmp->next) {
LogDebugFn("TCSD mem_cached handle: 0x%x", tmp->tcs_handle);
if (tmp->blob &&
pub->keyLength == tmp->blob->pubKey.keyLength &&
!memcmp(tmp->blob->pubKey.key, pub->key, pub->keyLength)) {
if (parent) {
if (!tmp->parent)
continue;
if (parent == tmp->parent->tcs_handle)
return tmp->tcs_handle;
} else
return tmp->tcs_handle;
}
}
LogDebugFn("returning NULL_TCS_HANDLE");
return NULL_TCS_HANDLE;
}
/* only called from load key paths, so no locking */
TCPA_STORE_PUBKEY *
mc_get_parent_pub_by_pub(TCPA_STORE_PUBKEY *pub)
{
struct key_mem_cache *tmp;
TCPA_STORE_PUBKEY *ret = NULL;
for (tmp = key_mem_cache_head; tmp; tmp = tmp->next) {
LogDebugFn("TCSD mem_cached handle: 0x%x", tmp->tcs_handle);
if (tmp->tcs_handle == TPM_KEYHND_SRK) {
LogDebugFn("skipping the SRK");
continue;
}
if (tmp->blob &&
!memcmp(tmp->blob->pubKey.key, pub->key, pub->keyLength)) {
if (tmp->parent && tmp->parent->blob) {
ret = &tmp->parent->blob->pubKey;
LogDebugFn("Success");
} else {
LogError("parent pointer not set in key mem cache object w/ TCS "
"handle: 0x%x", tmp->tcs_handle);
}
return ret;
}
}
LogDebugFn("returning NULL TCPA_STORE_PUBKEY");
return NULL;
}
/* only called from load key paths, so no locking */
TSS_RESULT
mc_get_blob_by_pub(TCPA_STORE_PUBKEY *pub, TSS_KEY **ret_key)
{
struct key_mem_cache *tmp;
for (tmp = key_mem_cache_head; tmp; tmp = tmp->next) {
LogDebugFn("TCSD mem_cached handle: 0x%x", tmp->tcs_handle);
if (tmp->blob &&
!memcmp(tmp->blob->pubKey.key, pub->key, pub->keyLength)) {
*ret_key = tmp->blob;
return TSS_SUCCESS;
}
}
LogDebugFn("returning TSS_E_FAIL");
return TCSERR(TSS_E_FAIL);
}
/* only called from load key paths, so no locking */
TCS_KEY_HANDLE
mc_get_handle_by_slot(TCPA_KEY_HANDLE tpm_handle)
{
struct key_mem_cache *tmp;
TCS_KEY_HANDLE ret;
for (tmp = key_mem_cache_head; tmp; tmp = tmp->next) {
LogDebugFn("TCSD mem_cached handle: 0x%x", tmp->tcs_handle);
if (tmp->tpm_handle == tpm_handle) {
ret = tmp->tcs_handle;
return ret;
}
}
return NULL_TCS_HANDLE;
}
/* only called from load key paths, so no locking */
TSS_RESULT
mc_update_time_stamp(TCPA_KEY_HANDLE tpm_handle)
{
struct key_mem_cache *tmp;
for (tmp = key_mem_cache_head; tmp; tmp = tmp->next) {
LogDebugFn("TCSD mem_cached handle: 0x%x", tmp->tcs_handle);
if (tmp->tpm_handle == tpm_handle) {
tmp->time_stamp = getNextTimeStamp();
return TSS_SUCCESS;
}
}
return TCSERR(TSS_E_FAIL);
}
/* Right now this evicts the LRU key assuming it's not the parent */
TSS_RESULT
evictFirstKey(TCS_KEY_HANDLE parent_tcs_handle)
{
struct key_mem_cache *tmp;
TCS_KEY_HANDLE tpm_handle_to_evict = NULL_TPM_HANDLE;
UINT32 smallestTimeStamp = ~(0U); /* largest */
TSS_RESULT result;
UINT32 count;
/* First, see if there are any known keys worth evicting */
if ((result = clearUnknownKeys(InternalContext, &count)))
return result;
if (count > 0) {
LogDebugFn("Evicted %u unknown keys", count);
return TSS_SUCCESS;
}
for (tmp = key_mem_cache_head; tmp; tmp = tmp->next) {
if (tmp->tpm_handle != NULL_TPM_HANDLE && /* not already evicted */
tmp->tpm_handle != SRK_TPM_HANDLE && /* not the srk */
tmp->tcs_handle != parent_tcs_handle && /* not my parent */
tmp->time_stamp < smallestTimeStamp) { /* is the smallest time
stamp so far */
tpm_handle_to_evict = tmp->tpm_handle;
smallestTimeStamp = tmp->time_stamp;
}
}
if (tpm_handle_to_evict != NULL_TCS_HANDLE) {
if ((result = internal_EvictByKeySlot(tpm_handle_to_evict)))
return result;
LogDebugFn("Evicted key w/ TPM handle 0x%x", tpm_handle_to_evict);
result = mc_set_slot_by_slot(tpm_handle_to_evict, NULL_TPM_HANDLE);
} else
return TSS_SUCCESS;
return result;
}
TSS_BOOL
isKeyLoaded(TCPA_KEY_HANDLE keySlot)
{
UINT64 offset;
UINT32 i;
TCPA_KEY_HANDLE_LIST keyList;
UINT32 respSize;
BYTE *resp;
TSS_RESULT result;
if (keySlot == SRK_TPM_HANDLE) {
return TRUE;
}
if ((result = TCSP_GetCapability_Internal(InternalContext, TCPA_CAP_KEY_HANDLE, 0, NULL,
&respSize, &resp)))
goto not_loaded;
offset = 0;
UnloadBlob_KEY_HANDLE_LIST(&offset, resp, &keyList);
free(resp);
for (i = 0; i < keyList.loaded; i++) {
LogDebugFn("loaded TPM key handle: 0x%x", keyList.handle[i]);
if (keyList.handle[i] == keySlot) {
free(keyList.handle);
return TRUE;
}
}
free(keyList.handle);
not_loaded:
LogDebugFn("Key is not loaded, changing slot");
mc_set_slot_by_slot(keySlot, NULL_TPM_HANDLE);
return FALSE;
}
/* all calls to LoadKeyShim are inside locks */
TSS_RESULT
LoadKeyShim(TCS_CONTEXT_HANDLE hContext, TCPA_STORE_PUBKEY *pubKey,
TSS_UUID *parentUuid, TCPA_KEY_HANDLE *slotOut)
{
TCPA_STORE_PUBKEY *parentPub;
UINT32 result;
TCPA_KEY_HANDLE keySlot;
TCPA_KEY_HANDLE parentSlot;
TCS_KEY_HANDLE tcsKeyHandle;
TSS_KEY *myKey;
UINT64 offset;
TCS_KEY_HANDLE parentHandle;
BYTE keyBlob[1024];
LogDebugFn("calling mc_get_slot_by_pub");
/* If I'm loaded, then no point being here. Get the slot and return */
keySlot = mc_get_slot_by_pub(pubKey);
if (keySlot != NULL_TPM_HANDLE && isKeyLoaded(keySlot)) {
*slotOut = keySlot;
return TSS_SUCCESS;
}
/*
* Before proceeding, the parent must be loaded.
* If the parent is registered, then it can be loaded by UUID.
* If not, then the shim will be called to load it's parent and then try
* to load it based on the persistent store.
*/
LogDebugFn("calling mc_get_parent_pub_by_pub");
/* Check if the Key is in the memory cache */
if ((parentPub = mc_get_parent_pub_by_pub(pubKey)) == NULL) {
#if 0
LogDebugFn("parentPub is NULL");
/* If parentUUID is not handed in, then this key was never loaded and isn't reg'd */
if (parentUuid == NULL)
return TCSERR(TCS_E_KM_LOADFAILED);
LogDebugFn("calling TCSP_LoadKeyByUUID_Internal");
/* This will try to load my parent by UUID */
if ((result = TCSP_LoadKeyByUUID_Internal(hContext, parentUuid, NULL, &parentSlot)))
return result;
#else
return TCSERR(TCS_E_KM_LOADFAILED);
#endif
} else {
LogDebugFn("calling LoadKeyShim");
if ((result = LoadKeyShim(hContext, parentPub, NULL, &parentSlot)))
return result;
}
/*
* Now that the parent is loaded, I can load myself.
* If I'm registered, that's by UUID. If I'm not,
* that's by blob. If there is no persistent storage data, then I cannot be
* loaded by blob. The user must have some point loaded this key manually.
*/
/* check the mem cache */
if (mc_get_blob_by_pub(pubKey, &myKey) == TSS_SUCCESS) {
parentHandle = mc_get_handle_by_slot(parentSlot);
if (parentHandle == 0)
return TCSERR(TCS_E_KM_LOADFAILED);
offset = 0;
LoadBlob_TSS_KEY(&offset, keyBlob, myKey);
if (TPM_VERSION_IS(1,2))
result = TCSP_LoadKey2ByBlob_Internal(hContext,
parentHandle, offset,
keyBlob, NULL,
&tcsKeyHandle);
else
result = TCSP_LoadKeyByBlob_Internal(hContext,
parentHandle, offset,
keyBlob, NULL,
&tcsKeyHandle, slotOut);
if (result)
return result;
return ctx_mark_key_loaded(hContext, tcsKeyHandle);
#if TSS_BUILD_PS
} else {
TSS_UUID *uuid;
/* check registered */
if (ps_is_pub_registered(pubKey) == FALSE)
return TCSERR(TCS_E_KM_LOADFAILED);
//uuid = mc_get_uuid_by_pub(pubKey); // XXX pub is not in MC
if ((result = ps_get_uuid_by_pub(pubKey, &uuid)))
return result;
if ((result = TCSP_LoadKeyByUUID_Internal(hContext, uuid, NULL, &tcsKeyHandle))) {
free(uuid);
return result;
}
free(uuid);
*slotOut = mc_get_slot_by_handle(tcsKeyHandle);
return ctx_mark_key_loaded(hContext, tcsKeyHandle);
#endif
}
return TCSERR(TCS_E_KM_LOADFAILED);
}
TSS_RESULT
owner_evict_init()
{
TSS_RESULT result = TSS_SUCCESS;
TCPA_KEY_HANDLE_LIST keyList = { 0, NULL };
BYTE *respData = NULL, ownerEvictCtr = 0;
UINT32 respDataSize = 0, i;
UINT64 offset = 0;
/* If we're a 1.1 TPM, we can exit immediately since only 1.2+ supports owner evict */
if (TPM_VERSION_IS(1,1))
return TSS_SUCCESS;
if ((result = TCSP_GetCapability_Internal(InternalContext, TPM_CAP_KEY_HANDLE, 0, NULL,
&respDataSize, &respData)))
return result;
if ((result = UnloadBlob_KEY_HANDLE_LIST(&offset, respData, &keyList))) {
free(respData);
return result;
}
free(respData);
for (i = 0; i < keyList.loaded; i++) {
UINT64 offset = 0;
UINT32 keyHandle;
LoadBlob_UINT32(&offset, keyList.handle[i], (BYTE *)&keyHandle);
/* get the ownerEvict flag for this key handle */
result = TCSP_GetCapability_Internal(InternalContext, TPM_CAP_KEY_STATUS,
sizeof(UINT32), (BYTE *)&keyHandle,
&respDataSize, &respData);
/* special case, invalid keys are automatically evicted later */
if (result == TPM_E_INVALID_KEYHANDLE)
continue;
if (result != TSS_SUCCESS) {
free(keyList.handle);
return result;
}
if (*(TPM_BOOL *)respData == TRUE) {
TSS_UUID uuid = TSS_UUID_OWNEREVICT(ownerEvictCtr);
LogDebugFn("Found an owner evict key, assigned uuid %hhu", ownerEvictCtr);
if ((result = mc_add_entry_init(getNextTcsKeyHandle(), keyList.handle[i],
NULL, &uuid))) {
free(keyList.handle);
return result;
}
ownerEvictCtr++;
}
}
return result;
}
/* find next lowest OWNEREVICT uuid */
TSS_RESULT
mc_find_next_ownerevict_uuid(TSS_UUID *uuid)
{
TCS_KEY_HANDLE tmpKey;
TCPA_KEY_HANDLE tmpSlot;
UINT16 seed = 0;
TSS_RESULT result = TCSERR(TSS_E_FAIL);
MUTEX_LOCK(mem_cache_lock);
for (seed = 0; seed <= 255; seed++) {
TSS_UUID tmpUuid = TSS_UUID_OWNEREVICT(seed);
/* if UUID is found, continue on, trying the next UUID */
if (!mc_get_handles_by_uuid(&tmpUuid, &tmpKey, &tmpSlot))
continue;
/* UUID is not found, so its the first one available */
memcpy(uuid, &tmpUuid, sizeof(TSS_UUID));
result = TSS_SUCCESS;
break;
}
MUTEX_UNLOCK(mem_cache_lock);
return result;
}
TSS_RESULT
mc_set_uuid(TCS_KEY_HANDLE tcs_handle, TSS_UUID *uuid)
{
struct key_mem_cache *tmp;
TSS_RESULT result = TCSERR(TSS_E_FAIL);
MUTEX_LOCK(mem_cache_lock);
LogDebugFn("looking for 0x%x", tcs_handle);
for (tmp = key_mem_cache_head; tmp; tmp = tmp->next) {
LogDebugFn("TCSD mem_cached handle: 0x%x", tmp->tcs_handle);
if (tmp->tcs_handle == tcs_handle) {
LogDebugFn("Handle found, re-setting UUID");
memcpy(&tmp->uuid, uuid, sizeof(TSS_UUID));
result = TSS_SUCCESS;
break;
}
}
MUTEX_UNLOCK(mem_cache_lock);
return result;
}
|