How To Implement Two-way SSL in node.js

Recently I have had to work with SSL certificates a lot and have just finished adding a SSL certificate into Node.js. One of the services I am using requires two-way SSL I forgot how much I liked coding in Node.js, I haven’t got the opportunity to do it recently. I decided the learning would make a nice blog post…

Assuming you have Node.js installed with NPM, if not check out http://nodejs.org/download/.

Step 1
Create a new directory to hold your Node.js application.

Step 2
You need some modules to implement two-way SSL in Node.js. You of course want a package.json file to handle dependencies and make it easy for future contributors of your application to get the necessary dependencies. We are going to create a package.json file and download and add the necessary dependencies to the newly created package.json file.

Step 3
Assuming you have PKCS 12 file (.p12 file) called certificate.p12 you need to extract a public certificate and private key. You will want to copy the generated privateKey.pem and publicCert.pem to a new directory resources/keys inside the NodeApp directory.

Step 4
With this done we are ready to finish up the Node.js app. Create a file server.js in the NodeApp folder and add the following code.

Lines 15, 16: load the private key and public cert.
Line 17: you are required to add a passphrase when you generate the private key so whatever password you used enter the appropriate password here.

A pretty trivial example but you should now have a working implementation of two-way SSL in Node.js.

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *