I'm trying to create a client in NodeJs that will validate the server certificate it connects to. I haven't found examples/documentation on the subject, and the example at https://github.com/vanjakom/JavaScriptPlayground/blob/master/client_server_ssl_nodejs/client.js doesn't fail the server, although the server certificate doesn't comply with the ca.crt used by the client.
can anyone suggest a way to verify the server certificate by the client?
slightly modified code from the site origin:
var https = require('https');
var fs = require("fs");
var options = {
host: 'localhost',
port: 8000,
path: '/test',
method: 'GET',
//key: fs.readFileSync("keys/userB.key"),
//cert: fs.readFileSync("certs/userB.crt"),
ca: fs.readFileSync("ca.crt")
,rejectUnauthorized:true,
requestCert:true,
agent:false
};
var req = https.request(options, function(res) {
console.log("statusCode: ", res.statusCode);
console.log("headers: ", res.headers);
res.on('data', function(d) {
console.log(""+d);
});
});
req.end();
req.on('error', function(e) {
console.error(e);
});