Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong result after Ref.deref(struct) #22

Open
microshine opened this issue Feb 6, 2016 · 6 comments
Open

Wrong result after Ref.deref(struct) #22

microshine opened this issue Feb 6, 2016 · 6 comments

Comments

@microshine
Copy link

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
Copy link

rmhrisk commented Jun 12, 2016

@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
Copy link
Owner

What is the type of CK_FLAGS?

@microshine
Copy link
Author

CK_FLAGS - type from pkcs11t.ts file

@rmhrisk
Copy link

rmhrisk commented Jul 1, 2016

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

@microshine
Copy link
Author

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
Copy link

sudiehu commented May 28, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants