自定义 iojs 插件无法加载 RSA 私钥

Custom iojs addon failed to load RSA private key

我尝试在 iojs 插件中加载 RSA 私钥的代码片段。

void IojsAddon::New(const FunctionCallbackInfo<Value>& args) {
  Isolate* isolate = Isolate::GetCurrent();
  HandleScope scope(isolate);

  if (args.IsConstructCall()) {
    EVP_PKEY* ca_key;
    BIO* keyb = BIO_new(BIO_s_file());
    BIO_read_filename(keyb, "/path/to/key.pem");
    ca_key = PEM_read_bio_PrivateKey(keyb, nullptr, nullptr, nullptr);

    std::cout << "type: " << ca_key->type << std::endl; // type: 6
    std::cout << "check: " << RSA_check_key(ca_key->pkey.rsa) << std::endl; // check: -1
    RSA_print_fp(stdout, ca_key->pkey.rsa, 0);  // Segmentation fault: 11

    IojsAddon* addon = new IojsAddon();
    addon->Wrap(args.This());
    args.GetReturnValue().Set(args.This());
  } else {
    const int argc = 1;
    Local<Value> argv[argc] = { args[0] };
    Local<Function> cons = Local<Function>::New(isolate, constructor);
    args.GetReturnValue().Set(cons->NewInstance(argc, argv));
  }
}

在独立应用程序中成功加载 RSA 私钥的代码片段,与之前的完全相同。

int main(int argc, char **argv) {
  EVP_PKEY* ca_key;
  BIO* keyb = BIO_new(BIO_s_file());
  BIO_read_filename(keyb, "/path/to/key.pem");
  ca_key = PEM_read_bio_PrivateKey(keyb, nullptr, nullptr, nullptr);

  std::cout << "type: " << ca_key->type << std::endl;  // type: 6
  std::cout << "check: " << RSA_check_key(ca_key->pkey.rsa) << std::endl; // check: -1 
  RSA_print_fp(stdout, ca_key->pkey.rsa, 0);  // print out the key info successfully.
  return (0);
}

P.S.

我在这里错过了什么?任何帮助将不胜感激!

我会在这里回答我自己的问题。通过在 binding.gyp 中包含 openssl 头文件来解决它。我已经成功读取了 X509 证书,但没有包含任何头文件,我只是假设构建系统会为我做这件事。但事实并非如此。你必须自己做。

查看 Linking-to-OpenSSl 了解有关使用 openssl 运行 iojs 插件的更多信息。