简体   繁体   中英

Cross platform (including Windows) way to use OS trusted certificates with nodejs

Is there a node module available or code that can read trusted certificates from the OS in a cross platform way? Others answers cover how to use ubuntus which I could adapt for MacOS X, but I have not been able to find any code that can easily read Windows trusted certificates. Other questions have pointed to where they exist in the registry and that I should be using the CryptoAPI to load them, but I have not been able to find a good example of doing this from node yet.

It sounds like the problem you are describing is not really about reading the certificates, but simply knowing where they live.

Trying to build something that can automatically discover where trusted certificates live on any running machine is almost certainly impossible, and probably not a good idea anyway.

Instead, this sounds like it should be solved with a simple environment variable (the problem after all is about variability of certificate paths on different environments ).

You might set 3 environment variable, let's call them CERT_PATH , KEY_PATH and CA_PATH . You can configure these on any OS to point to the correct location. You can then reference them in your application:

var fs = require('fs');  
var https = require('https');

var options = {  
    key: fs.readFileSync(process.env.KEY_PATH),
    cert: fs.readFileSync(process.env.CERT_PATH),
    ca: fs.readFileSync(process.env.CA_PATH),
};

https.createServer(options, function (req, res) {  
    console.log(new Date()+' '+
        req.connection.remoteAddress+' '+
        req.method+' '+req.url);
    res.writeHead(200);
    res.end("hello world\n");
  }).listen(4433);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM