As I ask in stackoverflow I try to mane a multi-certificate https server.
const fs = require('fs');
const { exit } = require('process');
let https;
try {
https = require('node:https');
} catch (err) {
console.log('https support is disabled!');
}
const cert_dir = '/etc/myapp/certs';
const secureContext = {}
fs.stat(cert_dir,(err,cb) => {
if(err){
console.log(err);
exit(-1);
}
fs.readdir(cert_dir,(err, files) => {
if (err){
console.log(err);
} else {
files.filter(item => {
return path.extname(item)=='.cert'
}).forEach(file => {
console.log(file);
const cert = path.join(cert_dir,file);
const key = path.join(cert_dir,(path.basename(file,".cert")+".key"))
fs.stat(key, (err, stat) => {
if(err == null){
const x509 = new X509Certificate(fs.readFileSync(cert));
console.log("cert Info",x509);
// get domain here
}
});
});
}
});
});
const options = {
SNICallback: function (domain, cb) {
if (secureContext[domain]) {
if (cb) {
cb(null, secureContext[domain]);
} else {
// compatibility for older versions of node
return secureContext[domain];
}
} else {
throw new Error('No keys/certificates for domain requested');
}
},
// must list a default key and cert because required by tls.createServer()
key: fs.readFileSync('../path_to_key.pem'),
cert: fs.readFileSync('../path_to_cert.crt'),
};
const server = https.createServer(options,handle);
server.listen(449);
But I cannot find a way to match a certificate by its domain. DO you know how I can find the domain out of random certificate?
I mean in Java I am able to do so:
https://stackoverflow.com/a/14020952/4706711
But why not in nodejs (despite being different and complete opposite)