【问题标题】:How can I get information around the full certificate chain with Nodejs?如何使用 Nodejs 获取有关完整证书链的信息?
【发布时间】:2023-03-19 05:31:01
【问题描述】:

我正在节点中创建一个服务来验证我们公司所依赖的所有域的所有证书的状态。最初我们只关心到期日期,但以后可能需要更多信息。我可以通过

检索最低级别证书的适当详细信息
var https = require('https');

var options = {
    host: 'google.com',
    port: 443,
    method: 'GET'
};

const request = https.request(options, function(res) {
    console.log(res.connection.getPeerCertificate());
});

request.end();

但我希望获取证书链中每个证书的详细信息。这在nodejs中怎么可能?

即对于 google.com,我想获得每个人的完整详细信息

Google Trust Services - GlobalSign CA-R2   ->   GTS CA 101  ->    www.google.com 

我想我可以递归调用每个证书的颁发者,但不太确定如何或是否可能。

【问题讨论】:

    标签: node.js ssl certificate


    【解决方案1】:

    根据the doc,如果你像这样通过true

    res.connection.getPeerCertificate(true)
    

    然后,您将获得有关整个链条的详细信息。当请求完整的证书链时,每个证书都将包含一个 issuerCertificate 属性,其中包含一个表示其颁发者证书的对象,您可以使用它来跟踪链。这是一个例子:

    var https = require('https');
    
    var options = {
        host: 'google.com',
        port: 443,
        method: 'GET'
    };
    
    const request = https.request(options, function(res) {
        let cert = res.connection.getPeerCertificate(true);
        let list = new Set(); 
        do {
            list.add(cert);
            console.log("subject", cert.subject);
            console.log("issuer", cert.issuer);
            console.log("valid_from", cert.valid_from);
            console.log("valid_to", cert.valid_to);
            cert = cert.issuerCertificate;
        } while (cert && typeof cert === "object" && !list.has(cert));
    
        res.on('data', data => {
            //console.log(data.toString('utf8'));
        });
    });
    
    request.end();
    

    文档没有解释你如何知道你何时处于链的末端(我原以为它会由 null 发行者表示,但 console.log() 报告了一个循环引用,所以我添加了一个 @ 987654328@ 来跟踪我们迄今为止看到的证书,以便检测链何时变为圆形以了解何时停止跟踪该链。

    【讨论】:

    • 有时我想知道我怎么会这么瞎。通过true 正是我所需要的。感谢您提供详细的示例!
    猜你喜欢
    • 1970-01-01
    • 2014-07-07
    • 1970-01-01
    • 2015-08-22
    • 1970-01-01
    • 1970-01-01
    • 2016-04-05
    • 1970-01-01
    • 2023-03-18
    相关资源
    最近更新 更多