Node.js/Express.js Chain Certificate Not working Node.js/Express.js Chain Certificate Not working express express

Node.js/Express.js Chain Certificate Not working


Does your intermediate certificate file contains multiple certificate blocks?

If that's the case you should split them into different files and read them one by one. You can pass them as an array to the ca parameter.

I've got it working with the code below:

var https = require('https'),    read = require('fs').readFileSync,    httpsOptions = {        key: read('ssl/mycertificate.key', 'utf8'),        cert: read('ssl/mycertificate.crt', 'utf8'),        ca: [            read('ssl/rapidssl_1.pem', 'utf8'),            read('ssl/rapidssl_2.pem', 'utf8')        ]    };https.createServer(httpsOptions, function (req, res) {    // ...});


Handy little snippet if you actually can't modify any SSL-related files on the server - you can split the "ssl chain" file yourself. Spent a little while when tried to get Node and socket.io to work with SSL (was getting net::ERR_INSECURE_RESPONSE error on the client) so thought will share it:

var read = require('fs').readFileSync;var privateKey = read(MY_KEY_LOCATION, 'utf8');var certificate = read(MY_CERT_LOCATION, 'utf8');var chainLines = read(MY_CHAIN_LOCATION, 'utf8').split("\n");var cert = [];var ca = [];chainLines.forEach(function(line) {  cert.push(line);  if (line.match(/-END CERTIFICATE-/)) {    ca.push(cert.join("\n"));    cert = [];  }});var credentials = {  "key": privateKey,  "cert": certificate,  "ca": ca};var httpsServer = https.createServer(credentials, app);var io = require('socket.io').listen(httpsServer);