Home | History | Annotate | Line # | Download | only in tcstp
      1 /*
      2  * The Initial Developer of the Original Code is Intel Corporation.
      3  * Portions created by Intel Corporation are Copyright (C) 2007 Intel Corporation.
      4  * All Rights Reserved.
      5  * trousers - An open source TCG Software Stack
      6  *
      7  * Author: james.xu (at) intel.com Rossey.liu (at) intel.com
      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 
     30 TSS_RESULT
     31 tcs_wrap_NV_DefineOrReleaseSpace(struct tcsd_thread_data *data)
     32 {
     33 	TCS_CONTEXT_HANDLE hContext;
     34 	UINT32 cPubInfoSize;
     35 	BYTE *pubInfo = NULL;
     36 	TSS_RESULT result;
     37 	TPM_ENCAUTH encAuth;
     38 	TPM_AUTH Auth, *pAuth;
     39 
     40 	if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
     41 		return TCSERR(TSS_E_INTERNAL_ERROR);
     42 
     43 	if ((result = ctx_verify_context(hContext)))
     44 		goto done;
     45 
     46 	if (getData(TCSD_PACKET_TYPE_UINT32, 1, &cPubInfoSize, 0, &data->comm))
     47 		return TCSERR(TSS_E_INTERNAL_ERROR);
     48 
     49 	pubInfo = calloc(1, cPubInfoSize);
     50 	if (pubInfo == NULL) {
     51 		LogError("malloc of %u bytes failed.", cPubInfoSize);
     52 		return TCSERR(TSS_E_OUTOFMEMORY);
     53 	}
     54 
     55 	if (getData(TCSD_PACKET_TYPE_PBYTE, 2, pubInfo, cPubInfoSize, &data->comm)) {
     56 		free(pubInfo);
     57 		return TCSERR(TSS_E_INTERNAL_ERROR);
     58 	}
     59 
     60 	if (getData(TCSD_PACKET_TYPE_ENCAUTH, 3, &encAuth, 0, &data->comm)) {
     61 		free(pubInfo);
     62 		return TCSERR(TSS_E_INTERNAL_ERROR);
     63 	}
     64 
     65 	if (getData(TCSD_PACKET_TYPE_AUTH, 4, &Auth, 0, &data->comm))
     66 		pAuth = NULL;
     67 	else
     68 		pAuth = &Auth;
     69 
     70 	MUTEX_LOCK(tcsp_lock);
     71 
     72 	result = TCSP_NV_DefineOrReleaseSpace_Internal(hContext,
     73 						       cPubInfoSize, pubInfo, encAuth, pAuth);
     74 
     75 	MUTEX_UNLOCK(tcsp_lock);
     76 
     77 	free(pubInfo);
     78 
     79 	if (result == TSS_SUCCESS) {
     80 		initData(&data->comm, 1);
     81 		if ( pAuth) {
     82 			if (setData(TCSD_PACKET_TYPE_AUTH, 0, pAuth, 0, &data->comm)) {
     83 				return TCSERR(TSS_E_INTERNAL_ERROR);
     84 			}
     85 		}
     86 	} else
     87 done:		initData(&data->comm, 0);
     88 
     89 	data->comm.hdr.u.result = result;
     90 	return TSS_SUCCESS;
     91 }
     92 
     93 TSS_RESULT
     94 tcs_wrap_NV_WriteValue(struct tcsd_thread_data *data)
     95 {
     96 	TCS_CONTEXT_HANDLE hContext;
     97 	TSS_NV_INDEX hNVStore;
     98 	UINT32 offset,ulDataLength;
     99 	BYTE *rgbDataToWrite = NULL;
    100 	TSS_RESULT result;
    101 	TPM_AUTH Auth, *pAuth;
    102 
    103 	if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
    104 		return TCSERR(TSS_E_INTERNAL_ERROR);
    105 
    106 	if ((result = ctx_verify_context(hContext)))
    107 		goto done;
    108 
    109 	if (getData(TCSD_PACKET_TYPE_UINT32, 1, &hNVStore, 0, &data->comm))
    110 		return TCSERR(TSS_E_INTERNAL_ERROR);
    111 
    112 	if (getData(TCSD_PACKET_TYPE_UINT32, 2, &offset, 0, &data->comm))
    113 		return TCSERR(TSS_E_INTERNAL_ERROR);
    114 
    115 	if (getData(TCSD_PACKET_TYPE_UINT32, 3, &ulDataLength, 0, &data->comm))
    116 		return TCSERR(TSS_E_INTERNAL_ERROR);
    117 
    118 	rgbDataToWrite = calloc(1, ulDataLength);
    119 	if (rgbDataToWrite == NULL) {
    120 		LogError("malloc of %u bytes failed.", ulDataLength);
    121 		return TCSERR(TSS_E_OUTOFMEMORY);
    122 	}
    123 
    124 	if (getData(TCSD_PACKET_TYPE_PBYTE, 4, rgbDataToWrite, ulDataLength, &data->comm)) {
    125 		free(rgbDataToWrite);
    126 		return TCSERR(TSS_E_INTERNAL_ERROR);
    127 	}
    128 
    129 	if (getData(TCSD_PACKET_TYPE_AUTH, 5, &Auth, 0, &data->comm))
    130 		pAuth = NULL;
    131 	else
    132 		pAuth = &Auth;
    133 
    134 	MUTEX_LOCK(tcsp_lock);
    135 
    136 	result = TCSP_NV_WriteValue_Internal(hContext, hNVStore,
    137 					     offset, ulDataLength, rgbDataToWrite, pAuth);
    138 
    139 	MUTEX_UNLOCK(tcsp_lock);
    140 
    141 	free(rgbDataToWrite);
    142 
    143 	if (result == TSS_SUCCESS) {
    144 		initData(&data->comm, 1);
    145 		if (pAuth) {
    146 			if (setData(TCSD_PACKET_TYPE_AUTH, 0, pAuth, 0, &data->comm)) {
    147 				return TCSERR(TSS_E_INTERNAL_ERROR);
    148 			}
    149 		}
    150 	} else
    151 done:		initData(&data->comm, 0);
    152 
    153 	data->comm.hdr.u.result = result;
    154 	return TSS_SUCCESS;
    155 }
    156 
    157 TSS_RESULT
    158 tcs_wrap_NV_WriteValueAuth(struct tcsd_thread_data *data)
    159 {
    160 	TCS_CONTEXT_HANDLE hContext;
    161 	TSS_NV_INDEX hNVStore;
    162 	UINT32 offset,ulDataLength;
    163 	BYTE *rgbDataToWrite = NULL;
    164 	TSS_RESULT result;
    165 	TPM_AUTH Auth, *pAuth;
    166 
    167 	if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
    168 		return TCSERR(TSS_E_INTERNAL_ERROR);
    169 
    170 	if ((result = ctx_verify_context(hContext)))
    171 		goto done;
    172 
    173 	if (getData(TCSD_PACKET_TYPE_UINT32, 1, &hNVStore, 0, &data->comm))
    174 		return TCSERR(TSS_E_INTERNAL_ERROR);
    175 
    176 	if (getData(TCSD_PACKET_TYPE_UINT32, 2, &offset, 0, &data->comm))
    177 		return TCSERR(TSS_E_INTERNAL_ERROR);
    178 
    179 	if (getData(TCSD_PACKET_TYPE_UINT32, 3, &ulDataLength, 0, &data->comm))
    180 		return TCSERR(TSS_E_INTERNAL_ERROR);
    181 
    182 	rgbDataToWrite = calloc(1, ulDataLength);
    183 	if (rgbDataToWrite == NULL) {
    184 		LogError("malloc of %u bytes failed.", ulDataLength);
    185 		return TCSERR(TSS_E_OUTOFMEMORY);
    186 	}
    187 
    188 	if (getData(TCSD_PACKET_TYPE_PBYTE, 4, rgbDataToWrite, ulDataLength, &data->comm)) {
    189 		free(rgbDataToWrite);
    190 		return TCSERR(TSS_E_INTERNAL_ERROR);
    191 	}
    192 	if (getData(TCSD_PACKET_TYPE_AUTH, 5, &Auth, 0, &data->comm)) {
    193 		free(rgbDataToWrite);
    194 		return TCSERR(TSS_E_INTERNAL_ERROR);
    195 	} else
    196 		pAuth = &Auth;
    197 
    198 	MUTEX_LOCK(tcsp_lock);
    199 
    200 	result = TCSP_NV_WriteValueAuth_Internal(hContext, hNVStore,
    201 						 offset, ulDataLength, rgbDataToWrite, pAuth);
    202 
    203 	MUTEX_UNLOCK(tcsp_lock);
    204 
    205 	free(rgbDataToWrite);
    206 
    207 	if (result == TSS_SUCCESS) {
    208 		initData(&data->comm, 1);
    209 		if ( pAuth) {
    210 			if (setData(TCSD_PACKET_TYPE_AUTH, 0, pAuth, 0, &data->comm)) {
    211 				return TCSERR(TSS_E_INTERNAL_ERROR);
    212 			}
    213 		}
    214 	} else
    215 done:		initData(&data->comm, 0);
    216 
    217 	data->comm.hdr.u.result = result;
    218 	return TSS_SUCCESS;
    219 }
    220 
    221 TSS_RESULT
    222 tcs_wrap_NV_ReadValue(struct tcsd_thread_data *data)
    223 {
    224 	TCS_CONTEXT_HANDLE hContext;
    225 	TSS_NV_INDEX hNVStore;
    226 	UINT32 offset,ulDataLength, i;
    227 	BYTE *rgbDataRead = NULL;
    228 	TSS_RESULT result;
    229 	TPM_AUTH Auth, *pAuth;
    230 
    231 	if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
    232 		return TCSERR(TSS_E_INTERNAL_ERROR);
    233 
    234 	if ((result = ctx_verify_context(hContext)))
    235 		goto done;
    236 
    237 	if (getData(TCSD_PACKET_TYPE_UINT32, 1, &hNVStore, 0, &data->comm))
    238 		return TCSERR(TSS_E_INTERNAL_ERROR);
    239 
    240 	if (getData(TCSD_PACKET_TYPE_UINT32, 2, &offset, 0, &data->comm))
    241 		return TCSERR(TSS_E_INTERNAL_ERROR);
    242 
    243 	if (getData(TCSD_PACKET_TYPE_UINT32, 3, &ulDataLength, 0, &data->comm))
    244 		return TCSERR(TSS_E_INTERNAL_ERROR);
    245 
    246 	if (getData(TCSD_PACKET_TYPE_AUTH, 4, &Auth, 0, &data->comm))
    247 		pAuth = NULL;
    248 	else
    249 		pAuth = &Auth;
    250 
    251 	MUTEX_LOCK(tcsp_lock);
    252 
    253 	result = TCSP_NV_ReadValue_Internal(hContext, hNVStore,
    254 					    offset, &ulDataLength, pAuth, &rgbDataRead);
    255 
    256 	MUTEX_UNLOCK(tcsp_lock);
    257 
    258 	if (result == TSS_SUCCESS) {
    259 		i = 0;
    260 		initData(&data->comm, 3);
    261 		if ( pAuth) {
    262 			if (setData(TCSD_PACKET_TYPE_AUTH, i++, pAuth, 0, &data->comm)) {
    263 				free(rgbDataRead);
    264 				return TCSERR(TSS_E_INTERNAL_ERROR);
    265 			}
    266 		}
    267 		if (setData(TCSD_PACKET_TYPE_UINT32, i++, &ulDataLength, 0, &data->comm)) {
    268 			free(rgbDataRead);
    269 			return TCSERR(TSS_E_INTERNAL_ERROR);
    270 		}
    271 		if (setData(TCSD_PACKET_TYPE_PBYTE, i++, rgbDataRead, ulDataLength, &data->comm)) {
    272 			free(rgbDataRead);
    273 			return TCSERR(TSS_E_INTERNAL_ERROR);
    274 		}
    275 		free(rgbDataRead);
    276 	} else
    277 done:		initData(&data->comm, 0);
    278 
    279 	data->comm.hdr.u.result = result;
    280 	return TSS_SUCCESS;
    281 }
    282 
    283 TSS_RESULT
    284 tcs_wrap_NV_ReadValueAuth(struct tcsd_thread_data *data)
    285 {
    286 	TCS_CONTEXT_HANDLE hContext;
    287 	TSS_NV_INDEX hNVStore;
    288 	UINT32 offset,ulDataLength, i;
    289 	BYTE *rgbDataRead = NULL;
    290 	TSS_RESULT result;
    291 	TPM_AUTH NVAuth, *pNVAuth;
    292 
    293 	if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
    294 		return TCSERR(TSS_E_INTERNAL_ERROR);
    295 
    296 	if ((result = ctx_verify_context(hContext)))
    297 		goto done;
    298 
    299 	if (getData(TCSD_PACKET_TYPE_UINT32, 1, &hNVStore, 0, &data->comm))
    300 		return TCSERR(TSS_E_INTERNAL_ERROR);
    301 
    302 	if (getData(TCSD_PACKET_TYPE_UINT32, 2, &offset, 0, &data->comm))
    303 		return TCSERR(TSS_E_INTERNAL_ERROR);
    304 
    305 	if (getData(TCSD_PACKET_TYPE_UINT32, 3, &ulDataLength, 0, &data->comm))
    306 		return TCSERR(TSS_E_INTERNAL_ERROR);
    307 
    308 	if (getData(TCSD_PACKET_TYPE_AUTH, 4, &NVAuth, 0, &data->comm)) {
    309 		pNVAuth = NULL;
    310 	} else {
    311 		pNVAuth = &NVAuth;
    312 	}
    313 
    314 	MUTEX_LOCK(tcsp_lock);
    315 
    316 	result = TCSP_NV_ReadValueAuth_Internal(hContext, hNVStore,
    317 						offset, &ulDataLength, pNVAuth, &rgbDataRead);
    318 
    319 	MUTEX_UNLOCK(tcsp_lock);
    320 
    321 	if (result == TSS_SUCCESS) {
    322 		i = 0;
    323 		initData(&data->comm, 3);
    324 		if ( pNVAuth) {
    325 			if (setData(TCSD_PACKET_TYPE_AUTH, i++, pNVAuth, 0, &data->comm)) {
    326 				free(rgbDataRead);
    327 				return TCSERR(TSS_E_INTERNAL_ERROR);
    328 			}
    329 		}
    330 		if (setData(TCSD_PACKET_TYPE_UINT32, i++, &ulDataLength, 0, &data->comm)) {
    331 			free(rgbDataRead);
    332 			return TCSERR(TSS_E_INTERNAL_ERROR);
    333 		}
    334 		if (setData(TCSD_PACKET_TYPE_PBYTE, i++, rgbDataRead, ulDataLength, &data->comm)) {
    335 			free(rgbDataRead);
    336 			return TCSERR(TSS_E_INTERNAL_ERROR);
    337 		}
    338 		free(rgbDataRead);
    339 	} else
    340 done:		initData(&data->comm, 0);
    341 
    342 	data->comm.hdr.u.result = result;
    343 	return TSS_SUCCESS;
    344 }
    345 
    346