我需要使用 ws Node JS 中的客户端连接到单独的 WebSocket 服务器。
我可以在我的 Chrome 中使用示例程序进行连接,因为我在我的机器上的受信任的根证书颁发机构存储中安装了我的自签名根 CA。
我知道 Node JS 使用硬编码的根 CA 列表(愚蠢),但我希望有某种方法可以导入我自己的。
我试过了:
export NODE_EXTRA_CA_CERTS=C:\\Users\\IT1\\Documents\\security\\rootCA.pem
// Using just ca
var test = new WebSocket(uri, {
ca: fs.readFileSync("C:\\Users\\IT1\\Documents\\security\\rootCA.pem")
});
// Using cert and key
var test = new WebSocket(uri, {
cert: fs.readFileSync("C:\\Users\\IT1\\Documents\\security\\rootCA.crt"),
key: fs.readFileSync("C:\\Users\\IT1\\Documents\\security\\rootCA.key")
});
// Using ca, cert and key
var test = new WebSocket(uri, {
ca: fs.readFileSync("C:\\Users\\IT1\\Documents\\security\\rootCA.pem"),
cert: fs.readFileSync("C:\\Users\\IT1\\Documents\\security\\rootCA.crt"),
key: fs.readFileSync("C:\\Users\\IT1\\Documents\\security\\rootCA.key")
});
无论如何,我总是收到以下错误消息:
events.js:200
throw er; // Unhandled 'error' event
^
Error: unable to verify the first certificate
at TLSSocket.onConnectSecure (_tls_wrap.js:1321:34)
at TLSSocket.emit (events.js:223:5)
at TLSSocket._finishInit (_tls_wrap.js:794:8)
at TLSWrap.ssl.onhandshakedone (_tls_wrap.js:608:12)
Emitted 'error' event on WebSocket instance at:
at ClientRequest.<anonymous> (C:\Users\IT1\source\repos\WebSocketTest\WebSocketTest\node_modules\ws\lib\websocket.js:554:15)
at ClientRequest.emit (events.js:223:5)
at TLSSocket.socketErrorListener (_http_client.js:406:9)
at TLSSocket.emit (events.js:223:5)
at emitErrorNT (internal/streams/destroy.js:92:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
at processTicksAndRejections (internal/process/task_queues.js:81:21) {
code: 'UNABLE_TO_VERIFY_LEAF_SIGNATURE'
}
另外,我不能使用
rejectUnauthorized: true
.我需要它被授权,所以请不要建议作为解决方案。请帮忙,我真的在这个问题上摸不着头脑。
最佳答案
在摸索了将近一两天之后,我决定在 OpenSSL 中验证证书。事实证明,证书使用了不同的加密算法(SHA 256 与 DES3 或类似的算法)。它在浏览器中运行良好的原因是因为我之前已经为该域安装了不同的证书。
故事的道德启示:
关于node.js - 在 Node JS 中为 WebSocket (ws) 使用自签名 CA 证书,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59741716/