Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2185
  • 博文数量: 1
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 0
  • 用 户 组: 普通用户
  • 注册时间: 2016-09-11 17:43
个人简介

没有总结的人生,不厚重

文章分类
文章存档

2016年(1)

我的朋友
最近访客

分类: 网络与安全

2016-04-13 23:35:31

pcks#11 是一个公钥加密的规范,关于此规范的说明可以google到很多文档,后面有空在写文章总结
nss pcks#11接口调用说明参考链接 pcks#11 Impl for nss,本文主要介绍nss中的动态链接库softokn3.so关于pcks#11的实现部分。
编译nss 代码时,提供了一个测试命令shlibsign来测试pcks#11的实现。main的代码片段如下:

点击(此处)折叠或打开

  1. /* Get the platform-dependent library name of the
  2.      * NSS cryptographic module.
  3.      */
  4.     libname = PR_GetLibraryName(NULL, "softokn3");   //这里通过nspr框架(这个框架见备注说明),加载softokn3的动态链接库
  5.     assert(libname != NULL);
  6.     lib = PR_LoadLibrary(libname);
  7.     assert(lib != NULL);
  8.     PR_FreeLibraryName(libname);


  9.     if (FIPSMODE) {
  10.         /* FIPSMODE == FC_GetFunctionList */
  11.         /* library path must be set to an already signed softokn3/freebl */
  12.         pC_GetFunctionList = (CK_C_GetFunctionList)
  13.                              PR_FindFunctionSymbol(lib, "FC_GetFunctionList");
  14.     } else {
  15.         /* NON FIPS mode == C_GetFunctionList */
  16.         pC_GetFunctionList = (CK_C_GetFunctionList)
  17.                              PR_FindFunctionSymbol(lib, "C_GetFunctionList");  //查找 C_GetFunctionList
  18.      }
  19.     assert(pC_GetFunctionList != NULL);

  20.     crv = (*pC_GetFunctionList)(&pFunctionList);
  21.     assert(crv == CKR_OK);

  22.     if (configDir) {
  23.     if (!dbPrefix) {
  24.             dbPrefix = PL_strdup("");
  25.         }
  26.         crv = softokn_Init(pFunctionList, configDir, dbPrefix);
  27.         if (crv != CKR_OK) {
  28.             logIt("Failed to use provided database directory "
  29.                   "will just initialize the volatile certdb.\n");
  30.             crv = softokn_Init(pFunctionList, NULL, NULL); /* NoDB Init */
  31.         }
  32.     } else {
  33.         crv = softokn_Init(pFunctionList, NULL, NULL); /* NoDB Init */
  34.     }

C_GetFunctionList的代码片段如下:

点击(此处)折叠或打开

  1. /* return the function list */
  2. CK_RV NSC_GetFunctionList(CK_FUNCTION_LIST_PTR *pFunctionList)
  3. {
  4.     CHECK_FORK();

  5.     *pFunctionList = (CK_FUNCTION_LIST_PTR) &sftk_funcList;//这里返回的是sftk_funclist,这里是个全局变量
  6.     return CKR_OK;
  7. }

  8. /* return the function list */
  9. CK_RV C_GetFunctionList(CK_FUNCTION_LIST_PTR *pFunctionList)
  10. {
  11.     CHECK_FORK();

  12.     return NSC_GetFunctionList(pFunctionList);
  13. }
sftk_funcList的初始化代码如下:

点击(此处)折叠或打开

  1. /* build the crypto module table */
  2. static const CK_FUNCTION_LIST sftk_funcList = {
  3.     { 1, 10 },
  4.  
  5. #undef CK_PKCS11_FUNCTION_INFO
  6. #undef CK_NEED_ARG_LIST
  7.  
  8. #define CK_PKCS11_FUNCTION_INFO(func) \
  9.                 __PASTE(NS,func),
  10. #include "pkcs11f.h"  //这里的关键是引用了pkcs11f.h,并且重新定义了CK_PKCS11_FUNCTION_INFO宏,此宏相当于所有func前面增加NS
  11.  
  12. };
pcks11f.h的代码片段如下:
点击(此处)折叠或打开
  1. /* C_Initialize initializes the PKCS #11 library. */
  2. CK_PKCS11_FUNCTION_INFO(C_Initialize)  //这里展开之后相当于变成了NSC_Initialize,说明所有的实现函数最终都是包含NSC前缀的
  3. #ifdef CK_NEED_ARG_LIST
  4. (
  5.   CK_VOID_PTR pInitArgs /* if this is not NULL_PTR, it gets
  6.                             * cast to CK_C_INITIALIZE_ARGS_PTR
  7.                             * and dereferenced */
  8. );
  9. #endif
NSC_Initialize的实现代码如下:

点击(此处)折叠或打开

  1. CK_RV NSC_Initialize(CK_VOID_PTR pReserved)
  2. {
  3.     CK_RV crv;
  4.     
  5.     sftk_ForkReset(pReserved, &crv);

  6.     if (nsc_init) {
  7.     return CKR_CRYPTOKI_ALREADY_INITIALIZED;
  8.     }
  9.     crv = nsc_CommonInitialize(pReserved,PR_FALSE);
  10.     nsc_init = (PRBool) (crv == CKR_OK);
  11.     return crv;
  12. }
找到了这种定义的逻辑,再分析这种代码应该就不难了,这里的代码定义的比较紧凑和巧妙,很多开源的框架,包括glibc都会使用到这种技巧,这是读起来或者用工具跟踪代码的时候比较麻烦。



阅读(608) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:没有了

给主人留下些什么吧!~~