简体   繁体   English

从PKCS11读取C ++结构有什么问题?

[英]What is wrong in C++ struct reading from PKCS11?

I'm trying to use PKCS11 lib. 我正在尝试使用PKCS11 lib。 I've got a wrong structure data on Windows10 x64 from C_GetInfo function. 我在C10GetInfo函数的Windows10 x64上得到了错误的结构数据。

My code 我的代码

CK_C_GetInfo f_C_GetInfo = (CK_C_GetInfo)dlsym(__pkcs11->dlHandle, "C_GetInfo");
CK_C_Finalize f_C_Finalize = (CK_C_Finalize)dlsym(__pkcs11->dlHandle, "C_Finalize");

CK_INFO _info;
rv = f_C_GetInfo(&_info);

C++ declarations C ++声明

/* an unsigned value, at least 32 bits long */
// typedef unsigned long int CK_ULONG;
typedef unsigned long int CK_ULONG;

/* at least 32 bits; each bit is a Boolean flag */
typedef CK_ULONG          CK_FLAGS;

typedef struct CK_VERSION {
    CK_BYTE       major;  /* integer portion of version number */
    CK_BYTE       minor;  /* 1/100ths portion of version number */
} CK_VERSION;

typedef CK_VERSION CK_PTR CK_VERSION_PTR;

typedef struct CK_INFO {
    CK_VERSION    cryptokiVersion;     /* Cryptoki interface ver */
    CK_UTF8CHAR   manufacturerID[32];  /* blank padded */
    CK_FLAGS      flags;               /* must be zero */

    CK_UTF8CHAR   libraryDescription[32];  /* blank padded */
    CK_VERSION    libraryVersion;          /* version of library */
} CK_INFO;

typedef CK_INFO CK_PTR    CK_INFO_PTR;

// Function C_GetInfo
CK_RV C_GetInfo ( CK_INFO_PTR pInfo );

Information from debugger 来自调试器的信息

在此输入图像描述 在此输入图像描述

Wrong structure data starts from parameter flags , it MUST be 0 (Hex: 00 00 00 00 ). 错误的结构数据从参数flags开始,必须为0(十六进制: 00 00 00 00 )。 But it has hex value 00 00 52 75 . 但它的十六进制值为00 00 52 75

I can fix this error if I change type of parameter flags to char[4] 如果我将参数flags类型更改为char[4]我可以修复此错误

What is wrong? 怎么了? Why is PKCS11 interface wrong for this structure? 为什么这个结构的PKCS11接口错误?

The first two elements cryptokiVersion and manufacturerID take up 34 bytes. 前两个元素cryptokiVersionmanufacturerID占用34个字节。

manufacturerID mentions that its blank padded, if you look at the bytes, there is a series of 0x20 which is UTF-8 for manufacturerID提到它的空白填充,如果你查看字节,有一系列的0x20UTF-8 for .

Followed by 4 0x00 其次是4 0x00

Followed by 52 75 74 .... 其次是52 75 74 ....

There is no padding in the data between manufacturerID and flags , but your structure definition contains 2 bytes of padding to align the unsigned long with the next boundary, which would be at 36 bytes. manufacturerIDflags之间的数据中没有填充,但是您的结构定义包含2个字节的填充,以使unsigned long与下一个边界对齐,该边界将为36个字节。

Try removing the padding from your structure. 尝试从结构中删除填充。

#pragma pack(push, p1, 1)

typedef struct CK_INFO {
    CK_VERSION    cryptokiVersion;     /* Cryptoki interface ver */
    CK_UTF8CHAR   manufacturerID[32];  /* blank padded */
    CK_FLAGS      flags;               /* must be zero */

    CK_UTF8CHAR   libraryDescription[32];  /* blank padded */
    CK_VERSION    libraryVersion;          /* version of library */
} CK_INFO;

#pragma pack(pop, p1)

It is extremely important to remember, that according to PKCS#11 all structures are packed to the minimal size, supported by the platform, and not aligned to 4 or 8 bytes. 值得记住的是,根据PKCS#11,所有结构都被打包到最小尺寸,由平台支持,而不是对齐到4或8字节。 Not doing this makes PKCS#11 drivers incompatible with applications. 不这样做会使PKCS#11驱动程序与应用程序不兼容。 PKCS#11 standard is strict on this. PKCS#11标准对此非常严格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM