How can I implement CryptSignMessage function from Crypt32.dll in node.js?

0

A client asked me to use CryptSignMessage function from Crypt32.dll library in nodejs. I tried some code but I had no luck. I surfed a lot and I couldn't see valuable info to get me on road. I researched in stackoverflow and didn't see this question, so I think maybe an expert in C++ or Crypto api can give me a hand.

I am using 'ffi' library (https://github.com/node-ffi/node-ffi) and I have succed running dummy functions from a compiled C code and too I could execute another functions from that library (like CryptUnprotectData and CryptProtectData), but like I said I couldn't make CryptSignMessage run correctly.

This is the code that I have. I'm going to paste all of it (it's not so much) to make you clear about this problem.

const ref = require("ref");
const ffi = require("ffi");
const Struct = require("ref-struct");
const ArrayType = require('ref-array');

const PKCS_7_ASN_ENCODING = 0x00010000;
const X509_ASN_ENCODING = 0x00000001; 

const szOID_OIWSEC_sha1 = "1.3.14.3.2.26";

// const CRYPTOAPI_BLOB = Struct({
//     cbData: ref.types.int32,
//     pbData: ref.types.int,
// })

const CRYPT_ALGORITHM_IDENTIFIER = Struct({
    pszObjId: ref.types.CString,
    Parameters: ref.types.int //new ref.refType(CRYPTOAPI_BLOB)
})

const CRYPT_SIGN_MESSAGE_PARA  = Struct({ 
    cbSize: ref.types.int32,
    dwMsgEncodingType: ref.types.int32,
    pSigningCert: ref.types.int,
    HashAlgorithm: new ref.refType(CRYPT_ALGORITHM_IDENTIFIER),
    pvHashAuxInfo: ref.types.int,
    cMsgCert: ref.types.int32,
    rgpMsgCert: ref.types.int,
    cMsgCrl: ref.types.int32,
    rgpMsgCrl: ref.types.int,
    cAuthAttr: ref.types.int32,
    rgAuthAttr: ref.types.int,
    cUnauthAttr: ref.types.int32,
    rgUnauthAttr: ref.types.int,
    dwFlags: ref.types.int32,
    dwInnerContentType: ref.types.int32
 })

const SIGN_STRUCTURE = new ref.refType(CRYPT_SIGN_MESSAGE_PARA);

const Crypto = new ffi.Library('Crypt32', {
    "CryptSignMessage" : ['bool', [SIGN_STRUCTURE, 'bool', 'int', ArrayType(ref.types.int), ArrayType(ref.types.int32), ref.types.int, ref.types.long]],
});

const signData = new CRYPT_SIGN_MESSAGE_PARA()
signData.cbSize = Object.keys(signData).length;
signData.dwMsgEncodingType = PKCS_7_ASN_ENCODING;
signData.pSigningCert = 0; // possible struct
signData.HashAlgorithm.pszObjId = szOID_OIWSEC_sha1;
signData.HashAlgorithm.Parameters = 0;
signData.pvHashAuxInfo = 0;
signData.cMsgCert = 1;
signData.rgpMsgCert = ref.NULL;
signData.cMsgCrl = 0;
signData.rgpMsgCrl = ref.NULL_POINTER;
signData.cAuthAttr = 0;
signData.rgAuthAttr = 0;
signData.cUnauthAttr = 0;
signData.rgUnauthAttr = ref.NULL_POINTER;
signData.dwFlags = 0;
signData.dwInnerContentType = 0;

const messageArray = ['a message'];
const messageArraySize = messageArray.length;
const cbSignedMessageBlob = 0;

const result = Crypto.CryptSignMessage(signData.ref(), false, 1, messageArray, messageArraySize, 0, cbSignedMessageBlob)
console.log(result)

I run this with node and I get no error but I know that it is not being implemented well and that I need more information about how to make it right

I want somebody could tell me in detailed which is the right way that I have to take to implement such function in nodejs and, if it is not possible, what recommendations you give me to tell the client relationed to the lenguage to use.

Thanks!

c++
node.js
windows
cryptography
ffi
asked on Stack Overflow Oct 21, 2019 by Federico Peralta • edited Oct 21, 2019 by Federico Peralta

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0