下面使用一段程序来对TSS协议栈进行测试,该程序实现以下操作
创建上下文对象......
创建TPM对象......
载入SRK密钥......
获取SRK的策略对象......
设置SRK的策略授权......
创建绑定密钥......
装载绑定密钥到UUID......
1/启动TPM
$ modprobe tpmd_dev
$ tpmd -d -f // -d 进入debug 模式 -f 使应用在前台运行
2/启动tcsd
$ tcsd -e -f
root@lin-laptop:/home/lin# tcsd -fe
TCSD TDDL ioctl: (22) Invalid argument
TCSD TDDL Falling back to Read/Write device support.
TCSD trousers 0.3.6: TCSD up and running.
虽然报出错误,但是不影响运行,这里我也不清楚怎么回事。
3/设置owner和SRK密码
$ tpm_takeownership
我在这里出现了一个错误,被困扰了很长时间。
lin@lin-laptop:~/TPM$ tpm_takeownership
Enter owner password:
Confirm password:
Enter SRK password:
Confirm password:
这样输入密钥 在运行程序时肯定会有认证失败的消息Tspi_Key_CreateKey ERROR:Authentication failed(0001)
这是由于编码方式引起的,使用unicode编码方式 就不会出现认证失败的错误。
即: lin@lin-laptop:~/TPM$ tpm_takeownership -u
4/运行代码
- //创建一个绑定密钥
-
-
#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;
-
}
- lin@lin-laptop:~/TPM$ gcc -o generate_key generate_key.c -ltspi
- lin@lin-laptop:~/TPM$ ./generate_key
- 创建上下文对象......
- 创建TPM对象......
- 载入SRK密钥......
- 获取SRK的密钥对象......设置SRK的策略授权......创建绑定密钥......
- 在TPM产生密钥前,设置填充类型......
- 产生密钥,该密钥不合PCR绑定
- TSS Authentication Dialog
- Enter PIN:
- 装载绑定密钥到UUID......
- UUID已被使用,注销此密钥......
- SUCCESS!
- 5/调试
- /usr/local/include/tss 下有tss_error.h 这里有所有程序中可能出现的错误代码信息
- 下面是TSS_RESULT的布局:
- 19 // 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
- 20 // +---+-+-+-----------------------+-------+-----------------------+
- 21 // |Lev|C|R| Facility | Layer | Code |
- 22 // +---+-+-+-----------------------+-------+-----------------------+
- 23 // | Platform specific coding | TSS error coding system |
- 24 // +---+-+-+-----------------------+-------+-----------------------+
- ERROR_CODE(error)和ERROR_LAYER(error)是用来移去多余信息为的掩码
阅读(7083) | 评论(0) | 转发(0) |