How to generate self-signed certificate for usage in Express4 or Node.js HTTP
I needed to generate a self-signed certificate for usage with node.js and express, since I don’t want to buy a certificate for just trying out and playing with it.
Let’s figure out how to do it.
You can also take a look at the following YouTube Video
Make sure you have install openssl, if you haven’t install it
RHEL/CentOS systems
yum install opensslDebian
apt-get install opensslTo be able to use SSL you need to generate
- Certificate Authority
- Server Certificate
Before we generate anything we need to generate a secure pass phrase
# pwgen 50 1 -s > passphrase# cat passphraseTNOojiJgDeqP1WwUYflXzBpbfZyl1vkAiuoXoikXRPQ9d1VBkCSo that is our secure pass phrase, and whenever it asks you for a pass phrase you can just copy paste the one we generated above.
Let’s generate Certificate Authority first
Private Key
# openssl genrsa -des3 -out ca.key 1024Generating RSA private key, 1024 bit long modulus...++++++...++++++e is 65537 (0x10001)Enter pass phrase for ca.key:Verifying - Enter pass phrase for ca.key:Certificate Signing Request
openssl req -new -key ca.key -out ca.csrEnter pass phrase for ca.key:You are about to be asked to enter information that will be incorporatedinto your certificate request.What you are about to enter is what is called a Distinguished Name or a DN.There are quite a few fields but you can leave some blankFor some fields there will be a default value,If you enter '.', the field will be left blank.-----Country Name (2 letter code) [AU]:State or Province Name (full name) [Some-State]:Locality Name (eg, city) []:Organization Name (eg, company) [Internet Widgits Pty Ltd]:Organizational Unit Name (eg, section) []:Common Name (e.g. server FQDN or YOUR name) []:Email Address []:
Please enter the following 'extra' attributesto be sent with your certificate requestA challenge password []:An optional company name []:Signing the certificate
# openssl x509 -req -days 365 -in ca.csr -out ca.crt -signkey ca.keySignature oksubject=/C=AU/ST=Some-State/O=Internet Widgits Pty LtdGetting Private keyEnter pass phrase for ca.key:Now let’s generate the Server Certificate
Private Key with pass phrase
# openssl genrsa -des3 -out server.key 1024Generating RSA private key, 1024 bit long modulus................................++++++.....++++++e is 65537 (0x10001)Enter pass phrase for server.key:Verifying - Enter pass phrase for server.key:Certificate Signing Request
# openssl req -new -key server.key -out server.csrEnter pass phrase for server.key:You are about to be asked to enter information that will be incorporatedinto your certificate request.What you are about to enter is what is called a Distinguished Name or a DN.There are quite a few fields but you can leave some blankFor some fields there will be a default value,If you enter '.', the field will be left blank.-----Country Name (2 letter code) [AU]:State or Province Name (full name) [Some-State]:Locality Name (eg, city) []:Organization Name (eg, company) [Internet Widgits Pty Ltd]:Organizational Unit Name (eg, section) []:Common Name (e.g. server FQDN or YOUR name) []:Email Address []:
Please enter the following 'extra' attributesto be sent with your certificate requestA challenge password []:An optional company name []:Private Key without pass phrase This will remove the pass phrase from the key, this step is crucial without this it will not work
# cp server.key server.key.passphrase# openssl rsa -in server.key.passphrase -out server.keyopenssl rsa -in server.key.passphrase -out server.keyEnter pass phrase for server.key.passphrase:writing RSA keySigning the certificate
openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crtSignature oksubject=/C=AU/ST=Some-State/O=Internet Widgits Pty LtdGetting Private keyThese are the files we have now
ls -latotal 36drwxr-xr-x 2 user user 4096 Sep 5 16:19 .drwxr-xr-x 12 user user 4096 Sep 5 16:09 ..-rw-r--r-- 1 user user 757 Sep 5 16:12 ca.crt-rw-r--r-- 1 user user 603 Sep 5 16:10 ca.csr-rw-r--r-- 1 user user 963 Sep 5 16:09 ca.key-rw-r--r-- 1 user user 757 Sep 5 16:19 server.crt-rw-r--r-- 1 user user 603 Sep 5 16:16 server.csr-rw-r--r-- 1 user user 887 Sep 5 16:18 server.key-rw-r--r-- 1 user user 951 Sep 5 16:17 server.key.passphraseThere you go now we have everything needed, lets see how we can create a HTTPS server with node.js
var https = require('https'), fs = require('fs'), express = require('express'), app = express();
var secureServer = https.createServer({ key: fs.readFileSync('./ssl/server.key'), cert: fs.readFileSync('./ssl/server.crt'), ca: fs.readFileSync('./ssl/ca.crt'), requestCert: true, rejectUnauthorized: false}, app).listen('8443', function() { console.log("Secure Express server listening on port 8443");});