CNs are important!!! -days 3650
openssl req -x509 -newkey rsa:4096 -keyout server_key.pem -out server_cert.pem -nodes -days 3650 -subj "/CN=localhost"
openssl req -newkey rsa:4096 -keyout admin_key.pem -out admin_csr.pem -nodes -days 3650 -subj "/CN=admin"
openssl x509 -req -in admin_csr.pem -CA server_cert.pem -CAkey server_key.pem -out admin_cert.pem -set_serial 01 -days 3650
openssl pkcs12 -export -clcerts -in admin_cert.pem -inkey admin_key.pem -out admin.p12
security import admin.p12
TLS options:
{
key: fs.readFileSync('server_key.pem'),
cert: fs.readFileSync('server_cert.pem'),
ca: [ fs.readFileSync('server_cert.pem') ],
requestCert: true,
rejectUnauthorized: false, // true to reject, false to handle (i.g. show error message)
}
Request handling:
(req, res) => {
const cert = req.connection.getPeerCertificate();
if (req.client.authorized) {
res.send(` ${cert.subject.CN} issued by ${cert.issuer.CN} is valid`);
} else if (cert.subject) {
res.status(403).send(`${cert.subject.CN} issued by ${cert.issuer.CN} is not valid`);
} else {
res.status(401).send('Certificate is not provided')
}
}
curl --insecure --cert admin.p12:password --cert-type p12 https://localhost/auth