1 2 /* 3 * Licensed Materials - Property of IBM 4 * 5 * trousers - An open source TCG Software Stack 6 * 7 * (C) Copyright International Business Machines Corp. 2006, 2007 8 * 9 */ 10 11 12 #include <stdlib.h> 13 #include <stdio.h> 14 #include <errno.h> 15 #include <string.h> 16 17 #include "trousers/tss.h" 18 #include "trousers/trousers.h" 19 #include "trousers_types.h" 20 #include "spi_utils.h" 21 #include "capabilities.h" 22 #include "tsplog.h" 23 #include "obj.h" 24 25 /* 26 * adds a new daa object to the daa list with TSP context tspContext 27 */ 28 TSS_RESULT 29 obj_daa_add(TSS_HCONTEXT tspContext, TSS_HOBJECT *phObject) 30 { 31 TSS_RESULT result; 32 struct tr_daa_obj *daa = calloc(1, sizeof(struct tr_daa_obj)); 33 34 if (daa == NULL) { 35 LogError("malloc of %d bytes failed.", sizeof(struct tr_daa_obj)); 36 return TSPERR(TSS_E_OUTOFMEMORY); 37 } 38 39 if ((result = obj_list_add(&daa_list, tspContext, 0, daa, phObject))) { 40 free(daa); 41 return result; 42 } 43 44 return TSS_SUCCESS; 45 } 46 47 void 48 daa_free(void *data) 49 { 50 struct tr_daa_obj *daa = (struct tr_daa_obj *)data; 51 52 /* free all pointers in the tr_daa_obj object here */ 53 free(daa); 54 } 55 56 /* 57 * remove DAA object hObject from the DAA list 58 */ 59 TSS_RESULT 60 obj_daa_remove(TSS_HOBJECT hObject, TSS_HCONTEXT tspContext) 61 { 62 TSS_RESULT result; 63 64 if ((result = obj_list_remove(&daa_list, &daa_free, hObject, tspContext))) 65 return result; 66 67 return TSS_SUCCESS; 68 } 69 70 TSS_BOOL 71 obj_is_daa(TSS_HOBJECT hObject) 72 { 73 TSS_BOOL answer = FALSE; 74 75 if ((obj_list_get_obj(&daa_list, hObject))) { 76 answer = TRUE; 77 obj_list_put(&daa_list); 78 } 79 80 return answer; 81 } 82 83 TSS_RESULT 84 obj_daa_get_tsp_context(TSS_HDAA hDaa, TSS_HCONTEXT *tspContext) 85 { 86 struct tsp_object *obj; 87 88 if ((obj = obj_list_get_obj(&daa_list, hDaa)) == NULL) 89 return TSPERR(TSS_E_INVALID_HANDLE); 90 91 *tspContext = obj->tspContext; 92 93 obj_list_put(&daa_list); 94 95 return TSS_SUCCESS; 96 } 97 98 static TSS_RESULT 99 obj_daa_get_and_lock_data(TSS_HDAA hDaa, struct tr_daa_obj **daa) 100 { 101 struct tsp_object *obj; 102 103 if ((obj = obj_list_get_obj(&daa_list, hDaa)) == NULL) 104 return TSPERR(TSS_E_INVALID_HANDLE); 105 *daa = obj->data; 106 return TSS_SUCCESS; 107 } 108 109 TSS_RESULT 110 obj_daa_get_handle_tpm(TSS_HDAA hDAA, TPM_HANDLE *hTPM) { 111 struct tr_daa_obj *daa_struct; 112 TSS_RESULT result; 113 114 if( (result = obj_daa_get_and_lock_data( hDAA, &daa_struct)) != TSS_SUCCESS) return result; 115 *hTPM = daa_struct->tpm_handle; 116 obj_list_put(&daa_list); 117 return TSS_SUCCESS; 118 } 119 120 TSS_RESULT 121 obj_daa_set_handle_tpm(TSS_HDAA hDAA, TPM_HANDLE hTPM) { 122 struct tr_daa_obj *daa_struct; 123 TSS_RESULT result; 124 125 if( (result = obj_daa_get_and_lock_data( hDAA, &daa_struct)) != TSS_SUCCESS) return result; 126 daa_struct->tpm_handle = hTPM; 127 obj_list_put(&daa_list); 128 return TSS_SUCCESS; 129 } 130 131 TSS_RESULT 132 obj_daa_get_session_handle(TSS_HDAA hDAA, UINT32 *session_handle) { 133 struct tr_daa_obj *daa_struct; 134 TSS_RESULT result; 135 136 if( (result = obj_daa_get_and_lock_data( hDAA, &daa_struct)) != TSS_SUCCESS) return result; 137 *session_handle = daa_struct->session_handle; 138 obj_list_put(&daa_list); 139 return TSS_SUCCESS; 140 } 141 142 TSS_RESULT 143 obj_daa_set_session_handle(TSS_HDAA hDAA, UINT32 session_handle) { 144 struct tr_daa_obj *daa_struct; 145 TSS_RESULT result; 146 147 if( (result = obj_daa_get_and_lock_data( hDAA, &daa_struct)) != TSS_SUCCESS) return result; 148 daa_struct->session_handle = session_handle; 149 obj_list_put(&daa_list); 150 return TSS_SUCCESS; 151 } 152