-
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <tss/platform.h> #include <tss/tss_error.h> #include <tss/tss_defines.h> #include <tss/tss_structs.h> #include <tss/tss_typedef.h> #include <tss/tss_error_basics.h> #include <tss/tspi.h> #include <trousers/tss.h> #include <tss/tpm.h> const char *get_error(TSS_RESULT res) { switch(ERROR_CODE(res)) { case 0x0001L: return "Authentication failed"; case TSS_SUCCESS: return "success"; case TSS_E_INVALID_HANDLE: return "hContext or phObject is an invalid handle"; case TSS_E_BAD_PARAMETER: return "persistentstoragetype is not valid/One or more parameters is incorrect"; case TSS_E_INTERNAL_ERROR: return "an error occurred internal to the TSS"; case TSS_E_PS_KEY_NOTFOUND: return "NOT FOUND SRK"; case TSS_E_INVALID_ATTRIB_FLAG: return "attribflag is incorrect"; case TSS_E_INVALID_ATTRIB_SUBFLAG: return "subflag is incorrect"; case TSS_E_INVALID_ATTRIB_DATA: return "ulAttrib is incorrect"; case TSS_E_KEY_ALREADY_REGISTERED: return "UUID used"; case TSS_E_KEY_NOT_LOADED: return "the addressed key is currently not loaded"; default: return "unknown error"; } } int main(int argc,char *argv) { TSS_RESULT result; TSS_HTPM hTPM; TSS_HCONTEXT hContext; TSS_HPOLICY hownerpolicy,hSRKPolicy; TSS_HKEY hSRK; TSS_HKEY hkey,hkey2; TSS_UUID SRK_UUID=TSS_UUID_SRK; TSS_FLAG initFlags; //密钥标记 TSS_UUID bindkeyUUID=TSS_UUID_USK1; //用户的绑定密钥 printf("创建上下文对象......\n"); result=Tspi_Context_Create(&hContext); if(result!=TSS_SUCCESS) { printf("Context_Create ERROR:%s(%04x)\n",get_error(result),result); } result=Tspi_Context_Connect(hContext,NULL); if(result!=TSS_SUCCESS) { printf("Context_Connect ERROR:%s(%04x)\n",get_error(result),result); } printf("创建TPM对象......\n"); result=Tspi_Context_GetTpmObject(hContext,&hTPM); if(result!=TSS_SUCCESS) { printf("Tspi_Context_GetTpmObject ERROR:%s(%04x)\n",get_error(result),result); } result=Tspi_GetPolicyObject(hTPM,TSS_POLICY_USAGE,&hownerpolicy); if(result!=TSS_SUCCESS) { printf("Tspi_GetPolicyObject ERROR:%s(%04x)\n",get_error(result),result); } result=Tspi_Policy_SetSecret(hownerpolicy,TSS_SECRET_MODE_POPUP,0,NULL); if(result!=TSS_SUCCESS) { printf("Tspi_Policy_SetSecret ERROR:%s(%04x)\n",get_error(result),result); } printf("载入SRK密钥......\n"); result=Tspi_Context_LoadKeyByUUID(hContext,TSS_PS_TYPE_SYSTEM,SRK_UUID,&hSRK); if(result!=TSS_SUCCESS) { printf("Tspi_Context_LoadKeyByUUID ERROR:%s(%04x)\n",get_error(result),result); } printf("获取SRK的策略对象......"); result=Tspi_GetPolicyObject(hSRK,TSS_POLICY_USAGE,&hSRKPolicy); if(result!=TSS_SUCCESS) { printf("Tspi_GetPolicyObject ERROR:%s(%04x)\n",get_error(result),result); } printf("设置SRK的策略授权......"); result=Tspi_Policy_SetSecret(hSRKPolicy,TSS_SECRET_MODE_POPUP,0,NULL); if(result!=TSS_SUCCESS) { printf("Tspi_Policy_SetSecret ERROR:%s(%04x)\n",get_error(result),result); } printf("创建绑定密钥......\n"); initFlags=TSS_KEY_TYPE_BIND|TSS_KEY_SIZE_512|TSS_KEY_NO_AUTHORIZATION; //设置密钥标记 result=Tspi_Context_CreateObject(hContext,TSS_OBJECT_TYPE_RSAKEY,initFlags,&hkey); //创建绑定密钥 if(result!=TSS_SUCCESS) { printf("Tspi_Context_CreateObject ERROR:%s(%04x)\n",get_error(result),result); } printf("在TPM产生密钥前,设置填充类型......\n"); result=Tspi_SetAttribUint32(hkey,TSS_TSPATTRIB_KEY_INFO,TSS_TSPATTRIB_KEYINFO_ENCSCHEME,TSS_ES_RSAESPKCSV15); if(result!=TSS_SUCCESS) { printf("Tspi_SetAttribUint32 ERROR:%s(%04x)\n",get_error(result),result); } printf("产生密钥,该密钥不合PCR绑定\n"); result=Tspi_Key_CreateKey(hkey,hSRK,0); if(result!=TSS_SUCCESS) { printf("Tspi_Key_CreateKey ERROR:%s(%04x)\n",get_error(result),result); } printf("装载绑定密钥到UUID......\n"); result=Tspi_Context_RegisterKey(hContext,hkey,TSS_PS_TYPE_USER,bindkeyUUID,TSS_PS_TYPE_SYSTEM,SRK_UUID); if(result!=TSS_SUCCESS) { if(ERROR_CODE(result)==TSS_E_KEY_ALREADY_REGISTERED) { printf("UUID已被使用,注销此密钥......\n"); result=Tspi_Context_UnregisterKey(hContext,TSS_PS_TYPE_USER,bindkeyUUID,&hkey); if(result!=TSS_SUCCESS) printf("UUID注销失败 Tspi_Context_UnregisterKey ERROR :%s(%04x)\n",get_error(result),result); result=Tspi_Context_RegisterKey(hContext,hkey,TSS_PS_TYPE_USER,bindkeyUUID,TSS_PS_TYPE_SYSTEM,SRK_UUID); if(result!=TSS_SUCCESS) printf("Tspi_Context_RegisterKey ERROR:%s(%04x)\n",get_error(result),result); } } printf("SUCCESS!\n"); Tspi_Context_Close(hContext); return 0; }
tpm协议栈测试程序
最新推荐文章于 2023-06-22 02:10:18 发布