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-2006 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_CertifyKey(struct tcsd_thread_data *data) 32 { 33 TCS_CONTEXT_HANDLE hContext; 34 TCS_KEY_HANDLE certHandle, keyHandle; 35 TPM_AUTH *pCertAuth = NULL, *pKeyAuth = NULL, certAuth, keyAuth, nullAuth; 36 UINT32 CertifyInfoSize, outDataSize; 37 BYTE *CertifyInfo, *outData; 38 TCPA_NONCE antiReplay; 39 TSS_RESULT result; 40 UINT32 i; 41 42 memset(&nullAuth, 0, sizeof(TPM_AUTH)); 43 memset(&certAuth, 0, sizeof(TPM_AUTH)); 44 memset(&keyAuth, 0, sizeof(TPM_AUTH)); 45 46 if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm)) 47 return TCSERR(TSS_E_INTERNAL_ERROR); 48 49 if ((result = ctx_verify_context(hContext))) 50 goto done; 51 52 LogDebugFn("thread %ld context %x", THREAD_ID, hContext); 53 54 if (getData(TCSD_PACKET_TYPE_UINT32, 1, &certHandle, 0, &data->comm)) 55 return TCSERR(TSS_E_INTERNAL_ERROR); 56 if (getData(TCSD_PACKET_TYPE_UINT32, 2, &keyHandle, 0, &data->comm)) 57 return TCSERR(TSS_E_INTERNAL_ERROR); 58 if (getData(TCSD_PACKET_TYPE_NONCE, 3, &antiReplay, 0, &data->comm)) 59 return TCSERR(TSS_E_INTERNAL_ERROR); 60 61 if (getData(TCSD_PACKET_TYPE_AUTH, 4, &certAuth, 0, &data->comm)) 62 return TCSERR(TSS_E_INTERNAL_ERROR); 63 if (getData(TCSD_PACKET_TYPE_AUTH, 5, &keyAuth, 0, &data->comm)) 64 return TCSERR(TSS_E_INTERNAL_ERROR); 65 66 if (memcmp(&nullAuth, &certAuth, sizeof(TPM_AUTH))) 67 pCertAuth = &certAuth; 68 69 if (memcmp(&nullAuth, &keyAuth, sizeof(TPM_AUTH))) 70 pKeyAuth = &keyAuth; 71 72 MUTEX_LOCK(tcsp_lock); 73 74 result = TCSP_CertifyKey_Internal(hContext, certHandle, keyHandle, antiReplay, pCertAuth, 75 pKeyAuth, &CertifyInfoSize, &CertifyInfo, &outDataSize, 76 &outData); 77 78 MUTEX_UNLOCK(tcsp_lock); 79 80 if (result == TSS_SUCCESS) { 81 i = 0; 82 initData(&data->comm, 6); 83 if (pCertAuth) { 84 if (setData(TCSD_PACKET_TYPE_AUTH, i++, pCertAuth, 0, &data->comm)) { 85 free(CertifyInfo); 86 free(outData); 87 return TCSERR(TSS_E_INTERNAL_ERROR); 88 } 89 } 90 if (pKeyAuth) { 91 if (setData(TCSD_PACKET_TYPE_AUTH, i++, pKeyAuth, 0, &data->comm)) { 92 free(CertifyInfo); 93 free(outData); 94 return TCSERR(TSS_E_INTERNAL_ERROR); 95 } 96 } 97 if (setData(TCSD_PACKET_TYPE_UINT32, i++, &CertifyInfoSize, 0, &data->comm)) { 98 free(CertifyInfo); 99 free(outData); 100 return TCSERR(TSS_E_INTERNAL_ERROR); 101 } 102 if (setData(TCSD_PACKET_TYPE_PBYTE, i++, CertifyInfo, CertifyInfoSize, &data->comm)) { 103 free(CertifyInfo); 104 free(outData); 105 return TCSERR(TSS_E_INTERNAL_ERROR); 106 } 107 free(CertifyInfo); 108 if (setData(TCSD_PACKET_TYPE_UINT32, i++, &outDataSize, 0, &data->comm)) { 109 free(outData); 110 return TCSERR(TSS_E_INTERNAL_ERROR); 111 } 112 if (setData(TCSD_PACKET_TYPE_PBYTE, i++, outData, outDataSize, &data->comm)) { 113 free(outData); 114 return TCSERR(TSS_E_INTERNAL_ERROR); 115 } 116 free(outData); 117 } else 118 done: initData(&data->comm, 0); 119 120 data->comm.hdr.u.result = result; 121 122 return TSS_SUCCESS; 123 } 124