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. 2004-2007 8 * 9 */ 10 11#include <stdlib.h> 12#include <stdio.h> 13#include <syslog.h> 14#include <string.h> 15#include <netdb.h> 16 17#include "trousers/tss.h" 18#include "trousers_types.h" 19#include "tcs_tsp.h" 20#include "tcs_utils.h" 21#include "tcs_int_literals.h" 22#include "capabilities.h" 23#include "tcslog.h" 24#include "tcsd_wrap.h" 25#include "tcsd.h" 26#include "tcs_utils.h" 27#include "rpc_tcstp_tcs.h" 28 29 30TSS_RESULT 31tcs_wrap_ReadCounter(struct tcsd_thread_data *data) 32{ 33 TCS_CONTEXT_HANDLE hContext; 34 TSS_COUNTER_ID idCounter; 35 TPM_COUNTER_VALUE counterValue; 36 TSS_RESULT result; 37 38 if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm)) 39 return TCSERR(TSS_E_INTERNAL_ERROR); 40 41 if ((result = ctx_verify_context(hContext))) 42 goto done; 43 44 LogDebugFn("thread %ld context %x", THREAD_ID, hContext); 45 46 if (getData(TCSD_PACKET_TYPE_UINT32, 1, &idCounter, 0, &data->comm)) 47 return TCSERR(TSS_E_INTERNAL_ERROR); 48 49 MUTEX_LOCK(tcsp_lock); 50 51 result = TCSP_ReadCounter_Internal(hContext, idCounter, &counterValue); 52 53 MUTEX_UNLOCK(tcsp_lock); 54 55 if (result == TSS_SUCCESS) { 56 initData(&data->comm, 1); 57 if (setData(TCSD_PACKET_TYPE_COUNTER_VALUE, 0, &counterValue, 0, &data->comm)) 58 return TCSERR(TSS_E_INTERNAL_ERROR); 59 } else 60done: initData(&data->comm, 0); 61 62 data->comm.hdr.u.result = result; 63 return TSS_SUCCESS; 64} 65 66TSS_RESULT 67tcs_wrap_CreateCounter(struct tcsd_thread_data *data) 68{ 69 TCS_CONTEXT_HANDLE hContext; 70 TSS_COUNTER_ID idCounter; 71 TPM_COUNTER_VALUE counterValue; 72 TPM_AUTH auth; 73 TPM_ENCAUTH encauth; 74 UINT32 LabelSize; 75 BYTE *pLabel = NULL; 76 TSS_RESULT result; 77 78 if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm)) 79 return TCSERR(TSS_E_INTERNAL_ERROR); 80 81 if ((result = ctx_verify_context(hContext))) 82 goto done; 83 84 LogDebugFn("thread %ld context %x", THREAD_ID, hContext); 85 86 if (getData(TCSD_PACKET_TYPE_UINT32, 1, &LabelSize, 0, &data->comm)) 87 return TCSERR(TSS_E_INTERNAL_ERROR); 88 89 if ((pLabel = calloc(1, LabelSize)) == NULL) { 90 LogError("malloc of %u bytes failed.", LabelSize); 91 return TCSERR(TSS_E_OUTOFMEMORY); 92 } 93 94 if (getData(TCSD_PACKET_TYPE_PBYTE, 2, &pLabel, LabelSize, &data->comm)) { 95 free(pLabel); 96 return TCSERR(TSS_E_INTERNAL_ERROR); 97 } 98 if (getData(TCSD_PACKET_TYPE_ENCAUTH, 3, &encauth, 0, &data->comm)) { 99 free(pLabel); 100 return TCSERR(TSS_E_INTERNAL_ERROR); 101 } 102 if (getData(TCSD_PACKET_TYPE_AUTH, 4, &auth, 0, &data->comm)) { 103 free(pLabel); 104 return TCSERR(TSS_E_INTERNAL_ERROR); 105 } 106 107 MUTEX_LOCK(tcsp_lock); 108 109 result = TCSP_CreateCounter_Internal(hContext, LabelSize, pLabel, encauth, &auth, 110 &idCounter, &counterValue); 111 112 MUTEX_UNLOCK(tcsp_lock); 113 114 free(pLabel); 115 116 if (result == TSS_SUCCESS) { 117 initData(&data->comm, 3); 118 if (setData(TCSD_PACKET_TYPE_AUTH, 0, &auth, 0, &data->comm)) 119 return TCSERR(TSS_E_INTERNAL_ERROR); 120 if (setData(TCSD_PACKET_TYPE_UINT32, 1, &idCounter, 0, &data->comm)) 121 return TCSERR(TSS_E_INTERNAL_ERROR); 122 if (setData(TCSD_PACKET_TYPE_COUNTER_VALUE, 2, &counterValue, 0, &data->comm)) 123 return TCSERR(TSS_E_INTERNAL_ERROR); 124 } else 125done: initData(&data->comm, 0); 126 127 data->comm.hdr.u.result = result; 128 return TSS_SUCCESS; 129} 130 131TSS_RESULT 132tcs_wrap_IncrementCounter(struct tcsd_thread_data *data) 133{ 134 TCS_CONTEXT_HANDLE hContext; 135 TSS_COUNTER_ID idCounter; 136 TPM_COUNTER_VALUE counterValue; 137 TPM_AUTH auth; 138 TSS_RESULT result; 139 140 if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm)) 141 return TCSERR(TSS_E_INTERNAL_ERROR); 142 143 if ((result = ctx_verify_context(hContext))) 144 goto done; 145 146 LogDebugFn("thread %ld context %x", THREAD_ID, hContext); 147 148 if (getData(TCSD_PACKET_TYPE_UINT32, 1, &idCounter, 0, &data->comm)) 149 return TCSERR(TSS_E_INTERNAL_ERROR); 150 if (getData(TCSD_PACKET_TYPE_AUTH, 2, &auth, 0, &data->comm)) 151 return TCSERR(TSS_E_INTERNAL_ERROR); 152 153 MUTEX_LOCK(tcsp_lock); 154 155 result = TCSP_IncrementCounter_Internal(hContext, idCounter, &auth, &counterValue); 156 157 MUTEX_UNLOCK(tcsp_lock); 158 159 if (result == TSS_SUCCESS) { 160 initData(&data->comm, 2); 161 if (setData(TCSD_PACKET_TYPE_AUTH, 0, &auth, 0, &data->comm)) 162 return TCSERR(TSS_E_INTERNAL_ERROR); 163 if (setData(TCSD_PACKET_TYPE_COUNTER_VALUE, 1, &counterValue, 0, &data->comm)) 164 return TCSERR(TSS_E_INTERNAL_ERROR); 165 } else 166done: initData(&data->comm, 0); 167 168 data->comm.hdr.u.result = result; 169 return TSS_SUCCESS; 170} 171 172TSS_RESULT 173tcs_wrap_ReleaseCounter(struct tcsd_thread_data *data) 174{ 175 TCS_CONTEXT_HANDLE hContext; 176 TSS_COUNTER_ID idCounter; 177 TPM_AUTH auth; 178 TSS_RESULT result; 179 180 if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm)) 181 return TCSERR(TSS_E_INTERNAL_ERROR); 182 183 if ((result = ctx_verify_context(hContext))) 184 goto done; 185 186 LogDebugFn("thread %ld context %x", THREAD_ID, hContext); 187 188 if (getData(TCSD_PACKET_TYPE_UINT32, 1, &idCounter, 0, &data->comm)) 189 return TCSERR(TSS_E_INTERNAL_ERROR); 190 if (getData(TCSD_PACKET_TYPE_AUTH, 2, &auth, 0, &data->comm)) 191 return TCSERR(TSS_E_INTERNAL_ERROR); 192 193 MUTEX_LOCK(tcsp_lock); 194 195 result = TCSP_ReleaseCounter_Internal(hContext, idCounter, &auth); 196 197 MUTEX_UNLOCK(tcsp_lock); 198 199 if (result == TSS_SUCCESS) { 200 initData(&data->comm, 1); 201 if (setData(TCSD_PACKET_TYPE_AUTH, 0, &auth, 0, &data->comm)) 202 return TCSERR(TSS_E_INTERNAL_ERROR); 203 } else 204done: initData(&data->comm, 0); 205 206 data->comm.hdr.u.result = result; 207 return TSS_SUCCESS; 208} 209 210TSS_RESULT 211tcs_wrap_ReleaseCounterOwner(struct tcsd_thread_data *data) 212{ 213 TCS_CONTEXT_HANDLE hContext; 214 TSS_COUNTER_ID idCounter; 215 TPM_AUTH auth; 216 TSS_RESULT result; 217 218 if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm)) 219 return TCSERR(TSS_E_INTERNAL_ERROR); 220 221 if ((result = ctx_verify_context(hContext))) 222 goto done; 223 224 LogDebugFn("thread %ld context %x", THREAD_ID, hContext); 225 226 if (getData(TCSD_PACKET_TYPE_UINT32, 1, &idCounter, 0, &data->comm)) 227 return TCSERR(TSS_E_INTERNAL_ERROR); 228 if (getData(TCSD_PACKET_TYPE_AUTH, 2, &auth, 0, &data->comm)) 229 return TCSERR(TSS_E_INTERNAL_ERROR); 230 231 MUTEX_LOCK(tcsp_lock); 232 233 result = TCSP_ReleaseCounterOwner_Internal(hContext, idCounter, &auth); 234 235 MUTEX_UNLOCK(tcsp_lock); 236 237 if (result == TSS_SUCCESS) { 238 initData(&data->comm, 1); 239 if (setData(TCSD_PACKET_TYPE_AUTH, 0, &auth, 0, &data->comm)) 240 return TCSERR(TSS_E_INTERNAL_ERROR); 241 } else 242done: initData(&data->comm, 0); 243 244 data->comm.hdr.u.result = result; 245 return TSS_SUCCESS; 246} 247