Home >Web Front-end >JS Tutorial >How to correctly use Nodejs's c module to link to OpenSSL_node.js
The cause of the matter is this. For some reasons, I recently wrote the c module of Nodejs, and then called it on the js side. Network communication is naturally inseparable from ssl, so it needs to be linked to the Openssl library.
Our original expectation was that users would need to install the Openssl runtime library, and then our c module would be dynamically linked to the Openssl so library to run.
Everything looked good at first, until we discovered that this openssl function doesn't work:
PKCS7_sign()
PKCS7_sign ( )
We found:
If our c module is dynamically linked with the Openssl library, there will be no problem in compilation. But when running, an error will appear: PKCS7_sign symbol cannot be found.
If our c module is statically linked with the Openssl library, there will be no problem in compilation, but when running, the place where this function is called has no effect, and the return value of this function is 0. According to the documentation, an error occurs, but the error is obtained using the Openssl function ERR_get_error The code is also 0. It means there is no error code.
This is true on Linux, but what about Mac? I tried it on Mac and found that there was no problem with Mac. So I thought this might be a bug in Nodejs. Then I went to Nodejs and reported a bug to it: [https:// github.com/joyent/node/issues/8026][1]
At the same time, I searched for keywords similar to nodejs linking to openssl on google.
Found several articles like this:
https://github.com/TooTallNate/node-gyp/wiki/Linking-to-OpenSSL
https://github.com/joyent/node/issues/3915
https://github.com/robhawkes/node-extension/issues/1
Through searching, we found that Nodejs itself also uses the Openssl library. It is speculated that nodejs's own crypto module is also implemented using the Openssl lib. This can be found from the source code of Nodejs, which contains all the latest Openssl Source code.
The handsome guy who wrote the first article above: https://github.com/TooTallNate/node-gyp/wiki/Linking-to-OpenSSL is a Nodejs developer.
Basic conclusion:
Nodejs uses Openssl itself
Before Nodejs 0.6, Nodejs was dynamically linked to the Openssl library. Later versions were statically linked.
At this time, I found that Node had already responded to my bug: https://github.com/joyent/node/issues/8026
The reason explained by Node:
After Node compiled itself, it cleared the symbols it did not use, so we could not find the symbols when running. So they fixed the bug and retained all the symbols. This caused the size of Node to become larger. 400k.
Thanks Node for the quick reply, I have to admire Node’s activeness. Like.