Home | History | Annotate | Line # | Download | only in tcstp
      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 
     30 TSS_RESULT
     31 tcs_wrap_ReadCurrentTicks(struct tcsd_thread_data *data)
     32 {
     33 	TCS_CONTEXT_HANDLE hContext;
     34 	UINT32 pulCurrentTime;
     35 	BYTE *prgbCurrentTime;
     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 	LogDebugFn("thread %ld context %x", THREAD_ID, hContext);
     42 
     43 	MUTEX_LOCK(tcsp_lock);
     44 
     45 	result = TCSP_ReadCurrentTicks_Internal(hContext, &pulCurrentTime, &prgbCurrentTime);
     46 
     47 	MUTEX_UNLOCK(tcsp_lock);
     48 
     49 	if (result == TSS_SUCCESS) {
     50 		initData(&data->comm, 2);
     51 		if (setData(TCSD_PACKET_TYPE_UINT32, 0, &pulCurrentTime, 0, &data->comm)) {
     52 			free(prgbCurrentTime);
     53 			return TCSERR(TSS_E_INTERNAL_ERROR);
     54 		}
     55 		if (setData(TCSD_PACKET_TYPE_PBYTE, 1, prgbCurrentTime, pulCurrentTime,
     56 			    &data->comm)) {
     57 			free(prgbCurrentTime);
     58 			return TCSERR(TSS_E_INTERNAL_ERROR);
     59 		}
     60 		free(prgbCurrentTime);
     61 	} else
     62 		initData(&data->comm, 0);
     63 
     64 	data->comm.hdr.u.result = result;
     65 	return TSS_SUCCESS;
     66 }
     67 
     68 TSS_RESULT
     69 tcs_wrap_TickStampBlob(struct tcsd_thread_data *data)
     70 {
     71 	TCS_CONTEXT_HANDLE hContext;
     72 	TCS_KEY_HANDLE hKey;
     73 	TPM_AUTH auth, *pAuth;
     74 	TPM_NONCE nonce;
     75 	TPM_DIGEST digest;
     76 	UINT32 sigSize, tcSize, i;
     77 	BYTE *sig, *tc;
     78 	TSS_RESULT result;
     79 
     80 	if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
     81 		return TCSERR(TSS_E_INTERNAL_ERROR);
     82 
     83 	if ((result = ctx_verify_context(hContext)))
     84 		goto done;
     85 
     86 	LogDebugFn("thread %ld context %x", THREAD_ID, hContext);
     87 
     88 	if (getData(TCSD_PACKET_TYPE_UINT32, 1, &hKey, 0, &data->comm))
     89 		return TCSERR(TSS_E_INTERNAL_ERROR);
     90 	if (getData(TCSD_PACKET_TYPE_NONCE, 2, &nonce, 0, &data->comm))
     91 		return TCSERR(TSS_E_INTERNAL_ERROR);
     92 	if (getData(TCSD_PACKET_TYPE_DIGEST, 3, &digest, 0, &data->comm))
     93 		return TCSERR(TSS_E_INTERNAL_ERROR);
     94 	if (getData(TCSD_PACKET_TYPE_AUTH, 4, &auth, 0, &data->comm))
     95 		pAuth = NULL;
     96 	else
     97 		pAuth = &auth;
     98 
     99 	MUTEX_LOCK(tcsp_lock);
    100 
    101 	result = TCSP_TickStampBlob_Internal(hContext, hKey, &nonce, &digest, pAuth, &sigSize, &sig,
    102 					     &tcSize, &tc);
    103 
    104 	MUTEX_UNLOCK(tcsp_lock);
    105 
    106 	if (result == TSS_SUCCESS) {
    107 		initData(&data->comm, 5);
    108 		i = 0;
    109 		if (pAuth) {
    110 			if (setData(TCSD_PACKET_TYPE_AUTH, i++, pAuth, 0, &data->comm)) {
    111 				free(sig);
    112 				free(tc);
    113 				return TCSERR(TSS_E_INTERNAL_ERROR);
    114 			}
    115 		}
    116 		if (setData(TCSD_PACKET_TYPE_UINT32, i++, &sigSize, 0, &data->comm)) {
    117 			free(sig);
    118 			free(tc);
    119 			return TCSERR(TSS_E_INTERNAL_ERROR);
    120 		}
    121 		if (setData(TCSD_PACKET_TYPE_PBYTE, i++, sig, sigSize, &data->comm)) {
    122 			free(sig);
    123 			free(tc);
    124 			return TCSERR(TSS_E_INTERNAL_ERROR);
    125 		}
    126 		if (setData(TCSD_PACKET_TYPE_UINT32, i++, &tcSize, 0, &data->comm)) {
    127 			free(sig);
    128 			free(tc);
    129 			return TCSERR(TSS_E_INTERNAL_ERROR);
    130 		}
    131 		if (setData(TCSD_PACKET_TYPE_PBYTE, i++, tc, tcSize, &data->comm)) {
    132 			free(sig);
    133 			free(tc);
    134 			return TCSERR(TSS_E_INTERNAL_ERROR);
    135 		}
    136 	} else
    137 done:		initData(&data->comm, 0);
    138 
    139 	data->comm.hdr.u.result = result;
    140 	return TSS_SUCCESS;
    141 }
    142