Chinaunix首页 | 论坛 | 博客
  • 博客访问: 539611
  • 博文数量: 51
  • 博客积分: 345
  • 博客等级: 民兵
  • 技术积分: 534
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-21 12:02
个人简介

文章分类

全部博文(51)

文章存档

2023年(2)

2022年(1)

2021年(7)

2020年(10)

2019年(2)

2016年(20)

2015年(5)

2014年(1)

2011年(3)

我的朋友

分类: LINUX

2011-03-30 08:18:51

下面使用一段程序来对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/运行代码
  1. //创建一个绑定密钥

  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <tss/platform.h>
  6. #include <tss/tss_error.h>
  7. #include <tss/tss_defines.h>
  8. #include <tss/tss_structs.h>
  9. #include <tss/tss_typedef.h>
  10. #include <tss/tss_error_basics.h>
  11. #include <tss/tspi.h>
  12. #include <trousers/tss.h>
  13. #include <tss/tpm.h>

  14. const char *get_error(TSS_RESULT res)
  15. {
  16.     switch(ERROR_CODE(res))
  17.     {        
  18.         case 0x0001L:
  19.             return "Authentication failed";
  20.         case TSS_SUCCESS:
  21.             return "success";
  22.         case TSS_E_INVALID_HANDLE:
  23.             return "hContext or phObject is an invalid handle";
  24.         case TSS_E_BAD_PARAMETER:
  25.             return "persistentstoragetype is not valid/One or more parameters is incorrect";
  26.         case TSS_E_INTERNAL_ERROR:
  27.             return "an error occurred internal to the TSS";
  28.         case TSS_E_PS_KEY_NOTFOUND:
  29.             return "NOT FOUND SRK";
  30.         case TSS_E_INVALID_ATTRIB_FLAG:
  31.             return "attribflag is incorrect";
  32.         case TSS_E_INVALID_ATTRIB_SUBFLAG:
  33.             return "subflag is incorrect";
  34.         case TSS_E_INVALID_ATTRIB_DATA:
  35.             return "ulAttrib is incorrect";
  36.         case TSS_E_KEY_ALREADY_REGISTERED:
  37.             return "UUID used";
  38.         case TSS_E_KEY_NOT_LOADED:
  39.             return "the addressed key is currently not loaded";
  40.         default:
  41.             return "unknown error";
  42.     }
  43. }
  44. int main(int argc,char *argv)
  45. {
  46.     TSS_RESULT result;
  47.     TSS_HTPM hTPM;
  48.     TSS_HCONTEXT hContext;
  49.     TSS_HPOLICY hownerpolicy,hSRKPolicy;
  50.     TSS_HKEY hSRK;
  51.     TSS_HKEY hkey,hkey2;
  52.     TSS_UUID SRK_UUID=TSS_UUID_SRK;
  53.     TSS_FLAG initFlags;            //密钥标记
  54.     TSS_UUID bindkeyUUID=TSS_UUID_USK1;    //用户的绑定密钥

  55.     printf("创建上下文对象......\n");
  56.     result=Tspi_Context_Create(&hContext);
  57.     if(result!=TSS_SUCCESS)
  58.     {
  59.         printf("Context_Create ERROR:%s(%04x)\n",get_error(result),result);
  60.     }
  61.     result=Tspi_Context_Connect(hContext,NULL);
  62.     if(result!=TSS_SUCCESS)
  63.     {
  64.         printf("Context_Connect ERROR:%s(%04x)\n",get_error(result),result);
  65.     }

  66.     printf("创建TPM对象......\n");
  67.     result=Tspi_Context_GetTpmObject(hContext,&hTPM);
  68.     if(result!=TSS_SUCCESS)
  69.     {
  70.         printf("Tspi_Context_GetTpmObject ERROR:%s(%04x)\n",get_error(result),result);
  71.     }
  72.     result=Tspi_GetPolicyObject(hTPM,TSS_POLICY_USAGE,&hownerpolicy);
  73.     if(result!=TSS_SUCCESS)
  74.     {
  75.         printf("Tspi_GetPolicyObject ERROR:%s(%04x)\n",get_error(result),result);
  76.     }
  77.     result=Tspi_Policy_SetSecret(hownerpolicy,TSS_SECRET_MODE_POPUP,0,NULL);
  78.     if(result!=TSS_SUCCESS)
  79.     {
  80.         printf("Tspi_Policy_SetSecret ERROR:%s(%04x)\n",get_error(result),result);
  81.     }
  82.     printf("载入SRK密钥......\n");
  83.     result=Tspi_Context_LoadKeyByUUID(hContext,TSS_PS_TYPE_SYSTEM,SRK_UUID,&hSRK);
  84.     if(result!=TSS_SUCCESS)
  85.     {
  86.         printf("Tspi_Context_LoadKeyByUUID ERROR:%s(%04x)\n",get_error(result),result);
  87.     }
  88.     printf("获取SRK的策略对象......");
  89.     result=Tspi_GetPolicyObject(hSRK,TSS_POLICY_USAGE,&hSRKPolicy);
  90.     if(result!=TSS_SUCCESS)
  91.     {
  92.         printf("Tspi_GetPolicyObject ERROR:%s(%04x)\n",get_error(result),result);
  93.     }
  94.     printf("设置SRK的策略授权......");
  95.     result=Tspi_Policy_SetSecret(hSRKPolicy,TSS_SECRET_MODE_POPUP,0,NULL);
  96.     if(result!=TSS_SUCCESS)
  97.     {
  98.         printf("Tspi_Policy_SetSecret ERROR:%s(%04x)\n",get_error(result),result);
  99.     }
  100.     printf("创建绑定密钥......\n");
  101.     initFlags=TSS_KEY_TYPE_BIND|TSS_KEY_SIZE_512|TSS_KEY_NO_AUTHORIZATION;                //设置密钥标记
  102.     result=Tspi_Context_CreateObject(hContext,TSS_OBJECT_TYPE_RSAKEY,initFlags,&hkey);        //创建绑定密钥
  103.     if(result!=TSS_SUCCESS)
  104.     {
  105.         printf("Tspi_Context_CreateObject ERROR:%s(%04x)\n",get_error(result),result);
  106.     }

  107.     printf("在TPM产生密钥前,设置填充类型......\n");
  108.     result=Tspi_SetAttribUint32(hkey,TSS_TSPATTRIB_KEY_INFO,TSS_TSPATTRIB_KEYINFO_ENCSCHEME,TSS_ES_RSAESPKCSV15);
  109.     if(result!=TSS_SUCCESS)
  110.     {
  111.         printf("Tspi_SetAttribUint32 ERROR:%s(%04x)\n",get_error(result),result);
  112.     }
  113.     printf("产生密钥,该密钥不合PCR绑定\n");
  114.     result=Tspi_Key_CreateKey(hkey,hSRK,0);
  115.     if(result!=TSS_SUCCESS)
  116.     {
  117.         printf("Tspi_Key_CreateKey ERROR:%s(%04x)\n",get_error(result),result);
  118.     }                

  119.     printf("装载绑定密钥到UUID......\n");
  120.     result=Tspi_Context_RegisterKey(hContext,hkey,TSS_PS_TYPE_USER,bindkeyUUID,TSS_PS_TYPE_SYSTEM,SRK_UUID);
  121.     if(result!=TSS_SUCCESS)
  122.     {
  123.         if(ERROR_CODE(result)==TSS_E_KEY_ALREADY_REGISTERED)
  124.         {
  125.             printf("UUID已被使用,注销此密钥......\n");
  126.             result=Tspi_Context_UnregisterKey(hContext,TSS_PS_TYPE_USER,bindkeyUUID,&hkey);
  127.             if(result!=TSS_SUCCESS)
  128.                 printf("UUID注销失败 Tspi_Context_UnregisterKey ERROR :%s(%04x)\n",get_error(result),result);

  129.             result=Tspi_Context_RegisterKey(hContext,hkey,TSS_PS_TYPE_USER,bindkeyUUID,TSS_PS_TYPE_SYSTEM,SRK_UUID);
  130.             if(result!=TSS_SUCCESS)
  131.                 printf("Tspi_Context_RegisterKey ERROR:%s(%04x)\n",get_error(result),result);
  132.         }
  133.     }
  134.     printf("SUCCESS!\n");
  135.     Tspi_Context_Close(hContext);
  136.     return 0;
  137. }
  138. lin@lin-laptop:~/TPM$ gcc -o generate_key generate_key.c -ltspi
  139. lin@lin-laptop:~/TPM$ ./generate_key 
  140. 创建上下文对象......
  141. 创建TPM对象......
  142. 载入SRK密钥......
  143. 获取SRK的密钥对象......设置SRK的策略授权......创建绑定密钥......
  144. 在TPM产生密钥前,设置填充类型......
  145. 产生密钥,该密钥不合PCR绑定
  146. TSS Authentication Dialog
  147. Enter PIN:
  148. 装载绑定密钥到UUID......
  149. UUID已被使用,注销此密钥......
  150. SUCCESS!

  151. 5/调试
  152. /usr/local/include/tss 下有tss_error.h 这里有所有程序中可能出现的错误代码信息
  153. 下面是TSS_RESULT的布局:
  154.   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
  155.   20 //  +---+-+-+-----------------------+-------+-----------------------+
  156.   21 //  |Lev|C|R|     Facility          | Layer |         Code          | 
  157.   22 //  +---+-+-+-----------------------+-------+-----------------------+
  158.   23 //  | Platform specific coding      | TSS error coding system       |
  159.   24 //  +---+-+-+-----------------------+-------+-----------------------+  

  160. ERROR_CODE(error)和ERROR_LAYER(error)是用来移去多余信息为的掩码
阅读(7083) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~