3
* Licensed Materials - Property of IBM
5
* trousers - An open source TCG Software Stack
7
* (C) Copyright International Business Machines Corp. 2004-2006
18
#include "trousers/tss.h"
19
#include "trousers/trousers.h"
20
#include "spi_internal_types.h"
21
#include "spi_utils.h"
22
#include "capabilities.h"
26
UINT32 nextObjectHandle = 0xC0000000;
28
pthread_mutex_t keylist_lock = PTHREAD_MUTEX_INITIALIZER;
29
pthread_mutex_t handle_lock = PTHREAD_MUTEX_INITIALIZER;
31
struct obj_list tpm_list;
32
struct obj_list context_list;
33
struct obj_list hash_list;
34
struct obj_list pcrs_list;
35
struct obj_list policy_list;
36
struct obj_list rsakey_list;
37
struct obj_list encdata_list;
40
list_init(struct obj_list *list)
43
pthread_mutex_init(&list->lock, NULL);
50
list_init(&context_list);
51
list_init(&hash_list);
52
list_init(&pcrs_list);
53
list_init(&policy_list);
54
list_init(&rsakey_list);
55
list_init(&encdata_list);
61
pthread_mutex_lock(&handle_lock);
63
/* return any object handle except NULL_HOBJECT */
66
} while (nextObjectHandle == NULL_HOBJECT);
68
pthread_mutex_unlock(&handle_lock);
70
return nextObjectHandle;
73
/* search through the provided list for an object with handle matching
74
* @handle. If found, return a pointer to the object with the list
75
* locked, else return NULL. To release the lock, caller should
76
* call obj_list_put() after manipulating the object.
79
obj_list_get_obj(struct obj_list *list, UINT32 handle)
81
struct tsp_object *obj;
83
pthread_mutex_lock(&list->lock);
85
for (obj = list->head; obj; obj = obj->next) {
86
if (obj->handle == handle)
91
pthread_mutex_unlock(&list->lock);
96
/* search through the provided list for an object with TSP context
97
* matching @tspContext. If found, return a pointer to the object
98
* with the list locked, else return NULL. To release the lock,
99
* caller should call obj_list_put() after manipulating the object.
102
obj_list_get_tspcontext(struct obj_list *list, UINT32 tspContext)
104
struct tsp_object *obj;
106
pthread_mutex_lock(&list->lock);
108
for (obj = list->head; obj; obj = obj->next) {
109
if (obj->tspContext == tspContext)
116
/* release a list whose handle was returned by obj_list_get_obj() */
118
obj_list_put(struct obj_list *list)
120
pthread_mutex_unlock(&list->lock);
124
obj_list_add(struct obj_list *list, UINT32 tsp_context, TSS_FLAG flags, void *data,
125
TSS_HOBJECT *phObject)
127
struct tsp_object *new_obj, *tmp;
129
new_obj = calloc(1, sizeof(struct tsp_object));
130
if (new_obj == NULL) {
131
LogError("malloc of %zd bytes failed.", sizeof(struct tsp_object));
132
return TSPERR(TSS_E_OUTOFMEMORY);
135
new_obj->handle = obj_get_next_handle();
136
new_obj->flags = flags;
137
new_obj->data = data;
139
if (list == &context_list)
140
new_obj->tspContext = new_obj->handle;
142
new_obj->tspContext = tsp_context;
144
pthread_mutex_lock(&list->lock);
146
if (list->head == NULL) {
147
list->head = new_obj;
150
list->head = new_obj;
154
pthread_mutex_unlock(&list->lock);
156
*phObject = new_obj->handle;
162
obj_list_remove(struct obj_list *list, TSS_HOBJECT hObject, TSS_HCONTEXT tspContext)
164
struct tsp_object *obj, *prev = NULL;
165
TSS_RESULT result = TSPERR(TSS_E_INVALID_HANDLE);
167
pthread_mutex_lock(&list->lock);
169
for (obj = list->head; obj; prev = obj, obj = obj->next) {
170
if (obj->handle == hObject) {
171
/* validate tspContext */
172
if (obj->tspContext != tspContext)
177
prev->next = obj->next;
179
list->head = obj->next;
181
result = TSS_SUCCESS;
186
pthread_mutex_unlock(&list->lock);
191
/* a generic routine for removing all members of a list who's tsp context
192
* matches @tspContext */
194
obj_list_close(struct obj_list *list, TSS_HCONTEXT tspContext)
196
struct tsp_object *index;
197
struct tsp_object *next = NULL;
198
struct tsp_object *toKill;
199
struct tsp_object *prev = NULL;
201
pthread_mutex_lock(&list->lock);
203
for (index = list->head; index; ) {
205
if (index->tspContext == tspContext) {
208
list->head = toKill->next;
210
prev->next = toKill->next;
223
pthread_mutex_unlock(&list->lock);
227
obj_close_context(TSS_HCONTEXT tspContext)
229
obj_list_close(&tpm_list, tspContext);
230
obj_list_close(&context_list, tspContext);
231
obj_list_close(&pcrs_list, tspContext);
232
obj_list_close(&policy_list, tspContext);
234
/* these three must be custom due to the need to free members of their
235
* private data areas. */
236
obj_list_hash_close(&hash_list, tspContext);
237
obj_list_rsakey_close(&rsakey_list, tspContext);
238
obj_list_encdata_close(&encdata_list, tspContext);
241
/* When a policy object is closed, all references to it must be removed. This function
242
* calls the object specific routines for each working object type to remove all refs to the
245
obj_lists_remove_policy_refs(TSS_HPOLICY hPolicy, TSS_HCONTEXT tspContext)
247
obj_rsakey_remove_policy_refs(hPolicy, tspContext);
248
obj_encdata_remove_policy_refs(hPolicy, tspContext);
249
obj_tpm_remove_policy_refs(hPolicy, tspContext);