客户端身份验证未正常工作(使用证书)。

huangapple go评论68阅读模式
英文:

CLient authentication does not work properly (with certificate)

问题

I want to check (dynamically via config file) a client certificate, but this does not work properly.

If I don't have a certificate I can still access the site sometimes.

this is my code

Server:

const { createServer } = require("https")

// config.ssl.client_crt = true or false

createServer({
      cert: readFileSync(config.ssl.crt_file),
      key: readFileSync(config.ssl.key_file),
      requestCert: config.ssl.client_crt,
      rejectUnauthorized: false,
      ca: readFileSync(config.ssl.ca_file),
},app).listen(config.port)

app.get("/", (req, res) => {
    authenticator.req_check(req, config.ssl.client_crt, function(ok) {
        if (! ok) {
            return res.send("nope")
        }

        return res.send("ok")
    })
});

authenticator:

module.exports = {
    req_check(req, client_crt, cb) {
        if (client_crt && req.client.authorized == false) { // <-- Pay attention!
            return cb(false)

        } else {
            return cb(true)
        }
    }
}

This works, but when I write it this way the value of req.client.authorized is sometimes ignored

if (client_crt && ! req.client.authorized) {
    return cb(false)
}

in my opinion it is the same, or am i wrong?

of course i don't want anyone to access the site without a certificate, so i need your help to make the check absolutely reliable

英文:

I want to check (dynamically via config file) a client certificate, but this does not work properly.

If I don't have a certificate I can still access the site sometimes.

this is my code

Server:

const { createServer } = require(&quot;https&quot;)

// config.ssl.client_crt = true or false

createServer({
      cert: readFileSync(config.ssl.crt_file),
      key: readFileSync(config.ssl.key_file),
      requestCert: config.ssl.client_crt,
      rejectUnauthorized: false,
      ca: readFileSync(config.ssl.ca_file),
},app).listen(config.port)

app.get(&quot;/&quot;, (req, res) =&gt; {
    authenticator.req_check(req, config.ssl.client_crt, function(ok) {
        if (! ok) {
            return res.send(&quot;nope&quot;)
        }

        return res.send(&quot;ok&quot;)
    })
});

authenticator:

module.exports = {
    req_check(req, client_crt, cb) {
        if (client_crt &amp;&amp; req.client.authorized == false) { // &lt;-- Pay attention!
            return cb(false)

        } else {
            return cb(true)
        }
    }
}

This works, but when I write it this way the value of req.client.authorized is sometimes ignored

if (client_crt &amp;&amp; ! req.client.authorized) {
    return cb(false)
}

in my opinion it is the same, or am i wrong?

of course i don't want anyone to access the site without a certificate, so i need your help to make the check absolutely reliable

答案1

得分: 0

如果要强制执行客户端证书授权,必须设置rejectUnauthorized: true

此外,在建立HTTPS连接之前,甚至在第一个HTTPS请求发送到该连接之前,应该已经检查了证书:

var server = https.createServer({
  cert: readFileSync(config.ssl.crt_file),
  key: readFileSync(config.ssl.key_file),
  requestCert: config.ssl.client_crt,
  rejectUnauthorized: true,
  ca: readFileSync(config.ssl.ca_file),
}, app)
.listen(config.port, function() {
  server.on("secureConnection", function(socket) {
    var now = new Date().getTime();
    var cert = socket.getPeerCertificate();
    if (!socket.authorized ||
        now < new Date(cert.valid_from).getTime() ||
        now > new Date(cert.valid_to).getTime())
      socket.end();
 });
});
英文:

If you want authorization by client certificate to be enforced, you must set rejectUnauthorized: true.

Also, certificates should be checked already when an HTTPS connection is set up, even before the first HTTPS request goes over that connection:

var server = https.createServer({
  cert: readFileSync(config.ssl.crt_file),
  key: readFileSync(config.ssl.key_file),
  requestCert: config.ssl.client_crt,
  rejectUnauthorized: true,
  ca: readFileSync(config.ssl.ca_file),
}, app)
.listen(config.port, function() {
  server.on(&quot;secureConnection&quot;, function(socket) {
    var now = new Date().getTime();
    var cert = socket.getPeerCertificate();
    if (!socket.authorized ||
        now &lt; new Date(cert.valid_from).getTime() ||
        now &gt; new Date(cert.valid_to).getTime())
      socket.end();
 });
});

huangapple
  • 本文由 发表于 2023年1月7日 16:38:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75039043.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定