⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tcs.c

📁 xen虚拟机源代码安装包
💻 C
📖 第 1 页 / 共 3 页
字号:
// ===================================================================// // Copyright (c) 2005, Intel Corp.// All rights reserved.//// Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions // are met:////   * Redistributions of source code must retain the above copyright //     notice, this list of conditions and the following disclaimer.//   * Redistributions in binary form must reproduce the above //     copyright notice, this list of conditions and the following //     disclaimer in the documentation and/or other materials provided //     with the distribution.//   * Neither the name of Intel Corporation nor the names of its //     contributors may be used to endorse or promote products derived//     from this software without specific prior written permission.//// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE // COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED// OF THE POSSIBILITY OF SUCH DAMAGE.// ===================================================================// // tcs.c// //  This file contains the functions that implement a TCS.// // ==================================================================#include <stdio.h>#include <string.h>#include <malloc.h>#include "tcg.h"#include "bsg.h"#include "tcs.h"#include "contextmgr.h"#include "tpmddl.h"#include "log.h"#include "hashtable.h"#include "hashtable_itr.h"// Static Global Vars for the TCSstatic int TCS_m_nCount = 0;#define TCPA_MAX_BUFFER_LENGTH 0x2000static BYTE InBuf [TCPA_MAX_BUFFER_LENGTH];static BYTE OutBuf[TCPA_MAX_BUFFER_LENGTH];struct hashtable *context_ht;// -------------------------- Hash table functions --------------------static unsigned int hashfunc32(void *ky) {  return (* (UINT32 *) ky);}static int equals32(void *k1, void *k2) {  return (*(UINT32 *) k1 == *(UINT32 *) k2);}CONTEXT_HANDLE *LookupContext( TCS_CONTEXT_HANDLE  hContext) {  return( (CONTEXT_HANDLE *) hashtable_search(context_ht, &hContext) );}// ---------------------------------------------------------------------------------// Initialization/Uninitialization SubComponent API// ---------------------------------------------------------------------------------TPM_RESULT TCS_create() {  TDDL_RESULT hRes = TDDL_E_FAIL;  TPM_RESULT result = TPM_FAIL;    if (TCS_m_nCount == 0) {    vtpmloginfo(VTPM_LOG_TCS, "Constructing new TCS:\n");    hRes = TDDL_Open();    context_ht = create_hashtable(10, hashfunc32, equals32);	      if ((hRes == TDDL_SUCCESS) && (context_ht != NULL)) {      result = TPM_SUCCESS;      TCS_m_nCount++;    } else {      result = TPM_IOERROR;      hashtable_destroy(context_ht, 1);    }  } else    TCS_m_nCount++;      return(result);}void TCS_destroy(){  TCS_m_nCount--;    if (TCS_m_nCount == 0) {    vtpmloginfo(VTPM_LOG_TCS, "Destructing TCS:\n");    TDDL_Close();    struct hashtable_itr *context_itr;    TCS_CONTEXT_HANDLE  *hContext;        // Close all the TCS contexts. TCS should evict keys based on this    if (hashtable_count(context_ht) > 0) {      context_itr = hashtable_iterator(context_ht);      do {        hContext = (TCS_CONTEXT_HANDLE *) hashtable_iterator_key(context_itr);	if (TCS_CloseContext(*hContext) != TPM_SUCCESS) 	    vtpmlogerror(VTPM_LOG_TCS, "Failed to close context %d properly.\n", *hContext);            } while (hashtable_iterator_advance(context_itr));      free(context_itr);    }    hashtable_destroy(context_ht, 1);  }  }TPM_RESULT TCS_Malloc(  TCS_CONTEXT_HANDLE  hContext, // in                        UINT32              MemSize, // in                        BYTE**              ppMemPtr) {// out  TPM_RESULT returnCode = TPM_FAIL;  CONTEXT_HANDLE* pContextHandle = LookupContext(hContext);    if (pContextHandle != NULL && ppMemPtr != NULL) {    *ppMemPtr = (BYTE *)AddMemBlock(pContextHandle, MemSize);    returnCode = TPM_SUCCESS;  }    return returnCode;}TPM_RESULT TCS_FreeMemory(  TCS_CONTEXT_HANDLE  hContext, // in                            BYTE*               pMemory) { // in  TPM_RESULT returnCode = TPM_FAIL;  CONTEXT_HANDLE* pContextHandle = LookupContext(hContext);    if ( (pContextHandle != NULL && pMemory != NULL) &&       (DeleteMemBlock(pContextHandle, pMemory) == TRUE) )    returnCode = TPM_SUCCESS;     return returnCode;}TPM_RESULT TCS_OpenContext(TCS_CONTEXT_HANDLE* hContext) { // out  TPM_RESULT returnCode = TPM_FAIL;  TCS_CONTEXT_HANDLE *newContext;    vtpmloginfo(VTPM_LOG_TCS, "Calling TCS_OpenContext:\n");    if (hContext) {    CONTEXT_HANDLE* pContextHandle = (CONTEXT_HANDLE *) malloc(sizeof(CONTEXT_HANDLE));    if (pContextHandle == NULL)       return TPM_SIZE;        // initialize to 0    pContextHandle->nBlockCount = 0;    pContextHandle->pTopBlock = NULL;    pContextHandle->pHandleList = NULL;        // Create New Block    AddMemBlock(pContextHandle, BLOCK_SIZE);        newContext = (TCS_CONTEXT_HANDLE *) malloc(sizeof(TCS_CONTEXT_HANDLE));    *newContext = (TCS_CONTEXT_HANDLE) (((uintptr_t) pContextHandle >> 2) & 0xffffffff);        if (hashtable_search(context_ht, &newContext) !=NULL)    	*newContext += 1;        pContextHandle->handle = *newContext;    if (!hashtable_insert(context_ht, newContext, pContextHandle)) {        free(newContext);        free(pContextHandle);    	returnCode = TPM_FAIL;    } else {    	*hContext = *newContext;    	returnCode = TPM_SUCCESS;    }  }    return(returnCode);}TPM_RESULT TCS_CloseContext(TCS_CONTEXT_HANDLE hContext) {// in  //FIXME: TCS SHOULD Track failed auths and make sure  //we don't try and re-free them here.  TPM_RESULT returnCode = TPM_FAIL;    CONTEXT_HANDLE* pContextHandle = LookupContext(hContext);    if(pContextHandle != NULL) {    // Print test info    vtpmloginfo(VTPM_LOG_TCS, "Calling TCS_CloseContext.\n");          // free memory for all the blocks    DeleteMemBlock(pContextHandle, NULL );          pContextHandle->pTopBlock = NULL;        FreeHandleList(pContextHandle);    if (pContextHandle->pHandleList != NULL)       vtpmlogerror(VTPM_LOG_TCS, "Not all handles evicted from TPM.\n");        // Release the TPM's resources    if (hashtable_remove(context_ht, &hContext) == NULL)       vtpmlogerror(VTPM_LOG_TCS, "Not all handles evicted from TPM.\n");        free(pContextHandle);    returnCode = TPM_SUCCESS;  }    vtpmloginfo(VTPM_LOG_TCS_DEEP, "Finished closing context\n");  return(returnCode);}// ------------------------------------------------------------------// Internal Functions// ------------------------------------------------------------------int packAuth(BYTE* dst, TCS_AUTH* auth) {  // CHECK: according to the command specs, the outgoing auth params are:  // nonceEven  // nonceOdd  // continueAuthSession  // auth digest for return params  //  // this is a bit different than this code...    return BSG_PackList(dst, 4, 		      BSG_TYPE_UINT32, &(auth->AuthHandle), 		      BSG_TPM_NONCE, &(auth->NonceOdd), 		      BSG_TYPE_BOOL, &(auth->fContinueAuthSession), 		      BSG_TPM_AUTHDATA, &(auth->HMAC));}int unpackAuth(TCS_AUTH* auth, BYTE* src) {  return BSG_UnpackList(src, 3, 			BSG_TPM_NONCE, &(auth->NonceEven), 			BSG_TYPE_BOOL, &(auth->fContinueAuthSession), 			BSG_TPM_AUTHDATA, &(auth->HMAC));}// ------------------------------------------------------------------// Authorization Commands// ------------------------------------------------------------------TPM_RESULT TCSP_OIAP(TCS_CONTEXT_HANDLE hContext, // in		     TCS_AUTHHANDLE*  authHandle, // out 		     TPM_NONCE*   nonce0)  // out{  // setup input/output parameters block  TPM_TAG tag = TPM_TAG_RQU_COMMAND;  TPM_COMMAND_CODE ordinal = TPM_ORD_OIAP;  UINT32 paramSize = 0;  TPM_RESULT returnCode = TPM_SUCCESS;    // setup the TPM driver input and output buffers  TDDL_RESULT hRes = TDDL_E_FAIL;  TDDL_UINT32  InLength = TCPA_MAX_BUFFER_LENGTH;  TDDL_UINT32  OutLength = TCPA_MAX_BUFFER_LENGTH;    // check input params  if (authHandle == NULL || nonce0 == NULL)     return TPM_BAD_PARAMETER;    // Convert Byte Input parameter in the input byte stream InBuf  InLength = BSG_PackList(InBuf, 3, 			  BSG_TPM_TAG, &tag, 			  BSG_TYPE_UINT32, &paramSize, 			  BSG_TPM_COMMAND_CODE, &ordinal);      // fill paramSize again as we now have the correct size  BSG_Pack(BSG_TYPE_UINT32, &InLength, InBuf+2);    vtpmloginfo(VTPM_LOG_TCS_DEEP, "Sending paramSize = %d\n", InLength);    // call the TPM driver  if ((hRes = TDDL_TransmitData(InBuf, InLength, OutBuf, &OutLength))       == TDDL_SUCCESS) {        // unpack to get the tag, paramSize, & returnCode    int i = BSG_UnpackList( OutBuf, 3, 			    BSG_TPM_TAG, &tag, 			    BSG_TYPE_UINT32, &paramSize, 			    BSG_TPM_COMMAND_CODE, &returnCode);        if (returnCode == TPM_SUCCESS && tag == TPM_TAG_RSP_COMMAND) {      // Extract the remaining output parameters      BSG_UnpackList(OutBuf+i, 2, 		     BSG_TYPE_UINT32, authHandle, 		     BSG_TPM_NONCE, nonce0);            if (!AddHandleToList(hContext, TPM_RT_AUTH, *authHandle))         vtpmlogerror(VTPM_LOG_TCS, "New AuthHandle not recorded\n");            vtpmloginfo(VTPM_LOG_TCS_DEEP, "Received paramSize : %d\n", paramSize);    } else       vtpmlogerror(VTPM_LOG_TCS, "Failed with return code %s\n", tpm_get_error_name(returnCode));      }    return(returnCode);}TPM_RESULT TCSP_OSAP(TCS_CONTEXT_HANDLE hContext,  // in		     TPM_ENTITY_TYPE  entityType,  // in		     UINT32    entityValue, // in		     TPM_NONCE   nonceOddOSAP, // in		     TCS_AUTHHANDLE*  authHandle,  // out 		     TPM_NONCE*   nonceEven,  // out		     TPM_NONCE*   nonceEvenOSAP) // out{  // setup input/output parameters block  TPM_TAG tag = TPM_TAG_RQU_COMMAND;  UINT32 paramSize = 0;  TPM_COMMAND_CODE ordinal = TPM_ORD_OSAP;  TPM_RESULT returnCode = TPM_SUCCESS;    // setup the TPM driver input and output buffers  TDDL_RESULT hRes = TDDL_E_FAIL;  TDDL_UINT32  InLength = TCPA_MAX_BUFFER_LENGTH;  TDDL_UINT32  OutLength = TCPA_MAX_BUFFER_LENGTH;    // check input params  if (authHandle == NULL || nonceEven == NULL || nonceEvenOSAP == NULL)    return TPM_BAD_PARAMETER;    // Convert Byte Input parameter in the input byte stream InBuf  InLength = BSG_PackList(InBuf, 6, 			  BSG_TPM_TAG, &tag, 			  BSG_TYPE_UINT32, &paramSize, 			  BSG_TPM_COMMAND_CODE, &ordinal, 			  BSG_TYPE_UINT16, &entityType, 			  BSG_TYPE_UINT32, &entityValue, 			  BSG_TPM_NONCE, &nonceOddOSAP);    // fill paramSize again as we now have the correct size  BSG_Pack(BSG_TYPE_UINT32, &InLength, InBuf+2);    vtpmloginfo(VTPM_LOG_TCS_DEEP, "Sending paramSize = %d\n", InLength);    // call the TPM driver  if ((hRes = TDDL_TransmitData(InBuf, InLength, OutBuf, &OutLength))             == TDDL_SUCCESS) {    // unpack to get the tag, paramSize, & returnCode    int i = BSG_UnpackList(OutBuf, 3, 			   BSG_TPM_TAG, &tag, 			   BSG_TYPE_UINT32, &paramSize, 			   BSG_TPM_COMMAND_CODE, &returnCode);        if (returnCode == TPM_SUCCESS && tag == TPM_TAG_RSP_COMMAND) {      // Extract the remaining output parameters      BSG_UnpackList(OutBuf+i, 3, 		     BSG_TYPE_UINT32, authHandle, 		     BSG_TPM_NONCE, nonceEven, 		     BSG_TPM_NONCE, nonceEvenOSAP);            if (!AddHandleToList(hContext, TPM_RT_AUTH, *authHandle)) {	    vtpmlogerror(VTPM_LOG_TCS, "New AuthHandle not recorded\n");      }            vtpmloginfo(VTPM_LOG_TCS_DEEP, "Received paramSize : %d\n", paramSize);    } else       vtpmlogerror(VTPM_LOG_TCS, "Failed with return code %s\n", tpm_get_error_name(returnCode));      }    return(returnCode);}TPM_RESULT TCSP_TakeOwnership(TCS_CONTEXT_HANDLE hContext,   // in			      UINT16    protocolID,   // in			      UINT32    encOwnerAuthSize, // in 			      BYTE*    encOwnerAuth,  // in			      UINT32    encSrkAuthSize,  // in			      BYTE*    encSrkAuth,   // in			      UINT32*    SrkSize,   // in, out			      BYTE**    Srk,    // in, out			      TCS_AUTH*   ownerAuth)   // in, out{  // setup input/output parameters block  TPM_TAG tag = TPM_TAG_RQU_AUTH1_COMMAND;  UINT32 paramSize = 0;  TPM_COMMAND_CODE ordinal = TPM_ORD_TakeOwnership;  TPM_RESULT returnCode = TPM_SUCCESS;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -