关于这个问题,我试图在 node.js 中启动一个 TLS 服务器,以反映我在 OpenSSL 中创建的那个。我已经从命令行使用 OpenSSL 测试了客户端和服务器,它们成功建立了连接。当我尝试将服务器移植到 node.js(并且仍然使用 OpenSSL 客户端连接到它)时,我收到“无共享密码”错误。我想知道在使用该passphrase
选项时是否需要做一些特别的事情tls.createServer()
下面是我分别针对服务器和客户端的成功 OpenSSL 命令,请注意 passphrase.txt 文件包含一行密码:
$ openssl s_server -accept 8888 -cert server.cert -key server.key -pass file:passphrase.txt -CAfile ca.cert
$ openssl s_client -connect 127.0.0.1:8888 -cert client.cert -key client.key -pass file:passphrase.txt -CAfile ca.cert
如果我使用附加参数为客户端和/或服务器指定密码,我也可以建立成功的连接-cipher 'ECDHE-ECDSA-AES128-GCM-SHA256'
。我正在使用由 CA 生成openssl ecparam
并签名的椭圆曲线密钥,该 CA 是使用openssl ca
我在上一个问题中讨论的。
用 node.js 编写的服务器代码如下所示:
var tls = require('tls');
var fs = require('fs');
var msg = '***********\n\nHello there secure client!\n\n***********';
var port = 8888;
var host = 'localhost';
var options = {
cert : fs.readFileSync('server.cert'),
key : fs.readFileSync('server.key'),
passphrase : (fs.readFileSync('passphrase.txt')).toString(),
ca : fs.readFileSync('ca.cert'),
// ciphers: 'ECDHE-ECDSA-AES128-GCM-SHA256',
// requestCert : true,
// rejectUnauthorized : true
};
tls.createServer(options, function(cleartextStream) {
if (cleartextStream.authorized) {
console.log('Server-side connection authorized by a Certificate Authority.');
} else {
// TODO this code does not appear to get executed even on failed connections
console.log('Server-side connection not authorized: ' + cleartextStream.authorizationError);
}
// send the server message to the client
cleartextStream.write(msg);
cleartextStream.setEncoding('utf8');
cleartextStream.pipe(cleartextStream);
}).listen(port, function() {
console.log('Server started on port: ' + port);
}).on('clientError', function(err){
console.log('A failed client connection attempt occurred.');
console.error(err);
console.log();
});
在命令行上调用上述代码node tlsServer.js
并尝试连接 OpenSSL 客户端后,我收到以下消息。
服务器:
$ node tlsServer.js
Server started on port: 8888
<< client started here >>
A failed client connection attempt occurred.
[Error: 6396:error:1408A0C1:SSL routines:SSL3_GET_CLIENT_HELLO:no shared cipher:openssl\ssl\s3_srvr.c:1132:
]
客户:
$ openssl s_client -connect 127.0.0.1:8888 -cert client.cert -key client.key -pass file:passphrase.txt -CAfile ca.cert
CONNECTED(00000003)
2674688:error:140790E5:SSL routines:SSL23_WRITE:ssl handshake failure:s23_lib.c:177:
---
no peer certificate available
---
No client certificate CA names sent
---
SSL handshake has read 0 bytes and written 320 bytes
---
New, (NONE), Cipher is (NONE)
Secure Renegotiation IS NOT supported
Compression: NONE
Expansion: NONE
---
我正在使用节点 v0.6.15。当我在发送到的选项列表中取消注释ciphers
、requestCert
和时,错误不会改变。我也有客户端的 node.js cersion,当我尝试连接到节点服务器时,我得到一个套接字挂起代码 ECONNRESET,在尝试连接到 OpenSSL 服务器时出现以下错误:rejectUnauthorized
tls.createServer()
Connection to localhost:8888 could not be made.
[Error: 6968:error:14077410:SSL routines:SSL23_GET_SERVER_HELLO:sslv3 alert handshake failure:openssl\ssl\s23_clnt.c:602:
]
提前感谢您的帮助和想法!