https.get 方法可否获得certificate
发布于 9 年前 作者 shuson 4337 次浏览 最后一次编辑是 8 年前 来自 问答

想使用https 模块模拟 client 得到server的 certificate,但是发现在callback中 response object没有提供certificate信息。 而java却提供了相关的api

javax.net.ssl.HttpsURLConnection
java.security.cert.Certificate

请问node能否做到

update:

由于nodejs的proto chain和一系列不明朗的文档 最简单的方法居然是:

var https = require('https');
https.get('https://blabla.whatever.com', function(res) {
  console.log("cert info: ", res.socket.getPeerCertificate());
}).on('error', function(e) {
  console.error(e);
});
4 回复

https://nodejs.org/api/tls.html 这个模块是可以得到证书的。 https 也应用了该模块。我的想法是,从 https.get 返回的对象中找到 tls 对应的那个加解密对象,然后从中取出证书。

@alsotang 谢谢思路,刚刚试了下,卡住了。我看到tls中有个tlsSocket.getPeerCertificate()。 从IncomingMessage(aka response)中拿到了net.socket (by message.socket)但是此socket object里找了半天没找到tlsSocket的入口。 是不是node没有实现这个

@alsotang 找到了,,,最后一行忘了看,,,

With HTTPS support, use request.connection.verifyPeer() and request.connection.getPeerCertificate() to obtain the client’s authentication details.

就是req的connection,甚至还有一个req.socket 自豪地采用 CNodeJS ionic

回到顶部