How to link to OpenSSL correctly using Nodejs's c++ module

  • 2020-03-30 03:39:45
  • OfStack

The reason for this is that, for some reason, I recently wrote a c++ module for Nodejs and called it on the js side.   Network communications naturally depend on SSL, so you need to link to Openssl's library.

Our expectation was that the user would have an Openssl runtime installed, and then our c++ module would dynamically link to Openssl's so library to run.

At first everything seemed fine until we found out that the openssl function didn't work:

PKCS7_sign ()
PKCS7_sign ()
We found that:

If our c++ module were dynamically linked to the Openssl library, the compilation would be fine.
If our c++ module is statically linked to the Openssl library, the compilation will be fine, but at run time, the place where the function is called will not work, and the return value of this function is 0.
That's true on Linux, but on the Mac. With Mac tried, found no problem Mac. So, thought this might be a bug of Nodejs. Then go to Nodejs quote it to a bug: [https://github.com/joyent/node/issues/8026] [1]

At the same time, Google searched for similar keywords for nodejs linking to openssl.

Find these articles:

(link: https://github.com/TooTallNate/node-gyp/wiki/Linking-to-OpenSSL)

(link: https://github.com/joyent/node/issues/3915)

(link: http://serverfault.com/questions/338092/how-can-i-build-node-js-using-static-libssl-and-crypto-libraries)

(link: https://github.com/robhawkes/node-extension/issues/1)

Through the search, we found that Nodejs himself also used the Openssl library, presumably Nodejs own crypto module is also implemented using Openssl lib.

Write the first article: above (link: https://github.com/TooTallNate/node-gyp/wiki/Linking-to-OpenSSL) Nodejs developers is that cute guy.

Basic conclusion:

Nodejs USES Openssl himself
Prior to Nodejs 0.6, Nodejs was dynamically linked to the Openssl library, and later versions were statically linked.
Then find the Node has been reply my bug over there: (link: https://github.com/joyent/node/issues/8026)

Node explains why:

After Node compiled itself, it cleared all the symbols it didn't use, so we couldn't find them at run time.

Thanks to Node's quick reply, I can't help but admire how active Node is.


Related articles: