我正在尝试设置具有相互身份验证的 https 服务器。
我为服务器创建了密钥和证书(自动签名)。
现在我使用 firefox 连接到服务器而不提供任何客户端证书。
这应该导致req.socket.authorized
存在false
(如此处所述),但由于某种原因,在一些刷新(并且没有改变任何东西)之后,消息从权利
Unauthorized: Client certificate required (UNABLE_TO_GET_ISSUER_CERT)
变为
Client certificate was authenticated but certificate information could not be retrieved.
对我来说这是出乎意料的,因为这意味着req.socket.authorized == true
即使没有客户端证书。有人可以解释一下为什么会这样吗?
这是我的代码:
const express = require('express')
const app = express()
const fs = require('fs')
const https = require('https')
// ...
const opts = { key: fs.readFileSync('./cryptoMaterial/private_key.pem'),
cert: fs.readFileSync('./cryptoMaterial/certificate.pem'),
requestCert: true,
rejectUnauthorized: false,
ca: [ fs.readFileSync('./cryptoMaterial/certificate.pem') ]
}
const clientAuthMiddleware = () => (req, res, next) => {
if (!req.secure && req.header('x-forwarded-proto') != 'https') {
return res.redirect('https://' + req.header('host') + req.url);
}
// Ensure that the certificate was validated at the protocol level
if (!req.socket.authorized) { // <-- THIS SHOULD BE ALWAYS FALSE
res.status(401).send(
'Unauthorized: Client certificate required ' +
'(' + req.socket.authorizationError + ')'
);
return
}
// Obtain certificate details
var cert = req.socket.getPeerCertificate();
if (!cert || !Object.keys(cert).length) {
// Handle the bizarre and probably not-real case that a certificate was
// validated but we can't actually inspect it
res.status(500).send(
'Client certificate was authenticated but certificate ' +
'information could not be retrieved.'
);
return
}
return next();
};
app.use(clientAuthMiddleware());
// ...
https.createServer(opts, app).listen(PORT)