p4n1
p4n1

Reputation: 115

Error: Self-signed certificate, node.JS https client certificate authentication with request

I'd like to ask for your lights on the below: Using nodeJS, I'm trying to establish https connection between my server, which acts as a client in this case and an external server. We want to use client certificate validation, and the external server has provided self-signed private.key and client.crt certificate. My code is the below:

router.get('/mywebhook', function (req, res) {
console.log('/mywebhook');
var request = require("request");
//var options;
var options = { method: 'GET',
    url: 'https://externalserverURL'
    ,cert: fs.readFileSync('certs/client_crt.pem')
    ,key: fs.readFileSync('certs/key.pem')
}


request(options, function (error, response, body) {
    if (error){
        //throw new Error(error);
        console.log(error);
        return res.end(error); 
    }
    console.log('all ok')
    return res.end(body);
});

});

I am getting though, the below error: Error: self signed certificate code: 'DEPTH_ZERO_SELF_SIGNED_CERT'

Setting sst-strict = false is something I want to avoid. Am I missing something? Any help?

Upvotes: 1

Views: 2177

Answers (1)

Kerem atam
Kerem atam

Reputation: 2787

You may either add extra root certificates to your server or you may do Node spesific configuration with environment variable.

  • For Node to recognize your self signed certificate you may use NODE_EXTRA_CA_CERTS environment variable by giving path to file extra-ca-certs.pem file will make node add them :
env NODE_EXTRA_CA_CERTS=./rootCA.crt node client.js

Upvotes: 2

Related Questions