简体   繁体   English

在 C++ 节点插件中使用 Node::Buffers

[英]Working with Node::Buffers in C++ Node Addons

I'm working on a Node addon that encrypts data using Windows DPAPI.我正在开发一个使用 Windows DPAPI 加密数据的 Node 插件。 I'm passing two Javascript Uint8Array to the C++ code using NAN.我使用 NAN 将两个 Javascript Uint8Array 传递给 C++ 代码。

This is what the typescript interface looks like:这是 typescript 接口的样子:

export interface DpapiBindings{
    protectData(dataToEncrypt: Uint8Array, optionalEntropy: Uint8Array, scope: string): Uint8Array
}

I'd like to then create a Node::Buffer in the C++ code:然后我想在 C++ 代码中创建一个 Node::Buffer :

void ProtectData( Nan::NAN_METHOD_ARGS_TYPE info)
{
    v8::Isolate* isolate = info.GetIsolate();

   // 
    auto buffer = node::Buffer::Data(info[0]);
    auto len = node::Buffer::Length(info[0]);

    DATA_BLOB entropyBlob;
    entropyBlob.pbData = nullptr;
    if (!info[1]->IsNull())
    {
        entropyBlob.pbData = reinterpret_cast<BYTE*>(node::Buffer::Data(info[1]));
        entropyBlob.cbData = node::Buffer::Length(info[1]);
    }

    DATA_BLOB dataIn;
    DATA_BLOB dataOut;

    // initialize input data
    dataIn.pbData = reinterpret_cast<BYTE*>(buffer);
    dataIn.cbData = len;

    success = CryptProtectData(
            &dataIn,
            nullptr,
            entropyBlob.pbData ? &entropyBlob : nullptr,
            nullptr, 
            nullptr,
            flags,
            &dataOut);

    auto returnBuffer = Nan::CopyBuffer(reinterpret_cast<const char*>(dataOut.pbData), dataOut.cbData).ToLocalChecked();
    LocalFree(dataOut.pbData);

    info.GetReturnValue().Set(returnBuffer);

}

I'm new to C++, my question is: When working with node::Buffer::Data and node::Buffer::Length in C++ code, and calling into CryptProtectData, do I need to worry about buffer overflows?我是 C++ 的新手,我的问题是:在 C++ 代码中使用 node::Buffer::Data 和 node::Buffer::Length 并调用 CryptProtectData 时,我需要担心缓冲区溢出吗? If so, how do I protect against it?如果是这样,我该如何防范它? Should I be appending a null char to buffer and len?我应该将 null 字符附加到缓冲区和 len 吗?

No, you don't need to worry about overflow.不,您无需担心溢出。 The dataIn and dataOut structures are pointers with a length. dataIndataOut结构是有长度的指针。

BufferCopy is not what you want to use though. BufferCopy 不是您想要使用的。 You want to use: Nan::MaybeLocal<v8::Object> Nan::NewBuffer(char* data, uint32_t size) .您想使用: Nan::MaybeLocal<v8::Object> Nan::NewBuffer(char* data, uint32_t size)

https://github.com/nodejs/nan/blob/master/doc/buffers.md#api_nan_new_buffer https://github.com/nodejs/nan/blob/master/doc/buffers.md#api_nan_new_buffer

and make sure you free the dataOut.pbData memory when you're done (i see you are with the LocalFree call.) the reason it can't overflow is that CryptProtectData allocates that buffer based on the size it needs.并确保在完成后释放dataOut.pbData memory(我看到你正在使用LocalFree调用。)它不能溢出的原因是CryptProtectData根据它需要的大小分配该缓冲区。

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

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