使用自签名证书保护websockets

pab*_*blo 29 ssl ssl-certificate websocket

我想使用安全的Websockets来提高成功率.我不需要加密.

我想在使用带有自签名证书的安全Websockets(wss://example.com)时会看到警告吗?我尝试使用Chrome 10,我没有看到警告,也没有要求我接受证书.它只是有效.

这是chrome中的错误还是预期的行为?我将来能否使用自签名证书?

谢谢

kan*_*aka 20

是的,这是Chrome的当前行为,但我不希望它继续成为未来的政策.在firefox 4中(如果在about:config中启用WebSockets),您将收到有关证书的警告.要批准证书,您可能还必须在浏览器中输入WebSockets URL(用https代替wss)并在那里批准它(因为WebSockets连接中有关自签名证书的警告可能不会给您批准它的机会).

我希望所有浏览器都能收集正确的行为,即抛出一个允许自签名证书被批准的警告对话框.

  • 我指的是http://www.ietf.org/mail-archive/web/hybi/current/msg01605.html.它表示80端口的成功率仅为63%,而端口443的成功率为95%.我也被迫使用端口443或8080,因为我使用nginx作为代理,它不支持HTTP 1.1.端口443需要一个证书,这使得东西变得复杂并且可能有点开销.我不确定有多少用户阻止端口8080. (2认同)

Rob*_*b W 15

自第19版(http://crbug.com/53836)以来,Chrome拒绝自签名证书.如果您尝试连接到使用自签名证书的wss URL,则会以静默方式中止请求.
要允许使用自签名证书,请使用--ignore-certificate-errors标记启动Chrome ,例如:

chromium --user-data-dir=/tmp/whatever --ignore-certificate-errors
Run Code Online (Sandbox Code Playgroud)

据我所知,没有办法让Firefox接受你的wss自签名证书.所以,只需ws://在Firefox中进行测试.如果您通过https测试Web应用程序,则必须切换首选项以允许连接到(不安全)ws://URL:

  1. 访问 about:config
  2. 设置network.websocket.allowInsecureFromHTTPStrue

  • 您可以使用的一个技巧是使用自签名证书访问与websocket端点相同的地址/端口的HTTPS URL,当它抛出一个框时,您可以添加安全例外.此异常将继续用于WebSocket连接.许多WebSocket实现也可以提供HTTPS页面.否则,通过apache或`openssl s_server`站起来快速HTTPS服务器只是为了添加安全异常也可以.这适用于所有主流浏览器,包括FireFox. (9认同)

cho*_*ovy 13

我通过以下方式得到了它:

https://github.com/einaros/ws/blob/master/test/WebSocketServer.test.js#L514

首先生成自签名证书:

openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem -days 100 -nodes
Run Code Online (Sandbox Code Playgroud)

然后使用节点的内置https服务器从Express应用程序创建您的httpsServer:

var privateKey  = fs.readFileSync('sslcert/key.pem', 'utf8');
var certificate = fs.readFileSync('sslcert/cert.pem', 'utf8');

var credentials = {key: privateKey, cert: certificate};
var express = require('express');
var app = express();

//... bunch of other express stuff here ...

//pass in your express app and credentials to create an https server
var httpsServer = https.createServer(credentials, app);
httpsServer.listen(8443);
Run Code Online (Sandbox Code Playgroud)

然后设置你的websocket服务器(具有讽刺意味的是,它将使用与http服务器相同的端口,我不知道这个,但我想协议可以共享端口? - 这让我有一段时间了).

var WebSocketServer = require('ws').Server;
var wss = new WebSocketServer({
    server: httpsServer
  });

wss.on('connection', function connection(ws) {
  ws.on('message', function incoming(message) {
    console.log('received: %s', message);
  });

  ws.send('something');
});
Run Code Online (Sandbox Code Playgroud)

现在浏览到https://0.0.0.0:8443服务器并接受Chrome中的自签名证书.然后,websockets现在应该工作在浏览器旁边.

打开chrome devtools控制台并输入:

var ws = new WebSocket('wss://0.0.0.0:8443');
ws.send('foo');
Run Code Online (Sandbox Code Playgroud)

....或者你用于httpsServer的任何主机:端口,这里的关键是你正在使用wss://协议

在节点快速Web服务器中,您应该看到一条消息记录到控制台.使用启动服务器node ./server.js

http://www.chovy.com/web-development/self-signed-certs-with-secure-websockets-in-node-js/