TooTallNate / ref-struct

Create ABI-compliant "struct" instances on top of Buffers
118 stars 78 forks source link

Wrong result after Ref.deref(struct) #22

Open microshine opened 8 years ago

microshine commented 8 years ago

TypeScript PKCS11 def

export const CK_ULONG = "ulong";
export const CK_BYTE = "byte";
export const CK_UTF8CHAR = CK_BYTE;

export const CK_VERSION = RefStruct({
    major: CK_BYTE, // integer portion of version number
    minor: CK_BYTE  // 1/100ths portion of version number
});

export const CK_INFO = RefStruct({
    /* manufacturerID and libraryDecription have been changed from
     * CK_CHAR to CK_UTF8CHAR for v2.10 */
    cryptokiVersion: CK_VERSION,                /* Cryptoki interface ver */
    manufacturerID: RefArray(CK_UTF8CHAR, 32),  /* blank padded */
    flags: CK_FLAGS,                            /* must be zero */

    /* libraryDescription and libraryVersion are new for v2.0 */
    libraryDescription: RefArray(CK_UTF8CHAR, 32),  /* blank padded */
    libraryVersion: CK_VERSION                      /* version of library */
});

Using:

var $info = Ref.alloc(CKI.CK_INFO);
if ((rs = pkcs11.C_GetInfo($info))) {
    throw new Error("Error: " + rs);
}
var info = Ref.deref($info);
console.log(info.cryptokiVersion);
console.log(new Buffer(info.manufacturerID).toString());
console.log(info.flags);
console.log(new Buffer(info.libraryDescription).toString());
console.log(info.libraryVersion);

Function tested on Windows 10 x64 Result is wrong:

version: {2, 30}                                    // correct
mId: "SoftHSM                         "             // correct
flags: 1833500672                                   // not correct, must be 0
desc: "plementation of PKCS11          "            // not correct, must be "Implementation of PKCS11"
libVersion: {1, 0}                                  // not correct, must be {2, 0}

Here is result of $info in hex

021e536f667448534d2020202020202020202020202020202020202020202020202000000000496d706c656d656e746174696f6e206f6620504b435331312020202020202020020001000000
version
hex: 021e
{2, 30}

mId
hex: 536f667448534d20202020202020202020202020202020202020202020202020
"SoftHSM                         "

flags
hex: 00000000            <- error starts here, it seems has wrong offset during parsing
0

desc
hex: 496d706c656d656e746174696f6e206f6620504b435331312020202020202020
"Implementation of PKCS11        "

libVersion
hex: 0200
{2,0}

?unknow data
1000000
rmhrisk commented 8 years ago

@TooTallNate sorry to bother you on this one but we are stuck on this one as well. We need this for our PKCS# 11 interop library - https://github.com/PeculiarVentures/graphene

TooTallNate commented 8 years ago

What is the type of CK_FLAGS?

microshine commented 8 years ago

CK_FLAGS - type from pkcs11t.ts file

rmhrisk commented 8 years ago

@TooTallNate any chance you can look at this, we would appreciate it.

microshine commented 8 years ago

I found answer to my question, but I've got next question

/*
 * If you're using Microsoft Developer Studio 5.0 to produce
 * Win32 stuff, this might be done by using the following
 * preprocessor directive before including pkcs11.h or pkcs11t.h:
 *
 * #pragma pack(push, cryptoki, 1)
 *
 * and using the following preprocessor directive after including
 * pkcs11.h or pkcs11t.h:
 *
 * #pragma pack(pop, cryptoki)
 */

How can I apply #pragma pack(push, cryptoki, 1) from JavaScript?

sudiehu commented 3 years ago

I found answer to my question, but I've got next question

/*
 * If you're using Microsoft Developer Studio 5.0 to produce
 * Win32 stuff, this might be done by using the following
 * preprocessor directive before including pkcs11.h or pkcs11t.h:
 *
 * #pragma pack(push, cryptoki, 1)
 *
 * and using the following preprocessor directive after including
 * pkcs11.h or pkcs11t.h:
 *
 * #pragma pack(pop, cryptoki)
 */

How can I apply #pragma pack(push, cryptoki, 1) from JavaScript?

you can try set the property of struct isPacked=true