简体   繁体   English

WebSocket TLS 证书设置乐趣

[英]WebSocket TLS certificate setup fun

I'm trying to setup a WebSocket over TLS.我正在尝试通过 TLS 设置 WebSocket。

For that end I'm going by the directions on page 81 in the following book.为此,我将按照下本书第 81 页的说明进行操作。

https://www.amazon.com/WebSocket-Client-Server-Communications-Andrew-Lombardi/dp/1449369278/ref=sr_1_1?keywords=websocket&qid=1581782142&sr=8-1 https://www.amazon.com/WebSocket-Client-Server-Communications-Andrew-Lombardi/dp/1449369278/ref=sr_1_1?keywords=websocket&qid=1581782142&sr=8-1

I setup the server code as they say in the book.我按照他们在书中所说的那样设置了服务器代码。 All good there, but there is some problem with the certificates.那里一切都很好,但证书有一些问题。

According to the book I am supposed to take four steps in setting up the certificates根据这本书,我应该采取四个步骤来设置证书

1. Generate a 2048 bit key.
openssl genrsa -des -passout pass:x -out server.pass.key 2048

2. Generate a passphrase free key.
openssl rsa -passin pass:x -in server.pass.key -out server.key

3. Generate csr from the private key.
openssl req -new -key server.key -out server.csr

4. Generate the certificate
openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt

When I run step 1 I get error当我运行第 1 步时出现错误

"UI_set_result:result too small .." 

For which the fix is here .修复方法在这里

He basically says to run the following snipped instead of step 1.他基本上说要运行以下剪辑而不是步骤 1。

openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:2048 -out server.key

It looks like he is skipping the .pass part.看起来他正在跳过 .pass 部分。 For which step 2 is taken.为此采取了第 2 步。 So I am unsure if I should take that step or not, after the fix.所以我不确定在修复之后我是否应该采取这一步。

I run those four steps, then in server.js i have:我运行这四个步骤,然后在 server.js 中我有:

var connection={
    ssl:true,
    port:port_number_here,
    ssl_key:'server.key',
    ssl_cert:'server.crt'
    }

//..
var processRequest=function(req,res){
    res.writeHead(200);
    res.end("Hi!\n");
    //console.log('connecting');
    };

var app=null;
app = httpsServ.createServer({
    key: fs.readFileSync(connection.ssl_key),
    cert:fs.readFileSync(connection.ssl_cert)
    },processRequest).listen(connection.port);


var wss = new WebSocketServer({server:app});

var clients=[];
var client_number=0;
wss.on('connection', function(cclient_socket){
    console.log('Estabished Connection with client.');
    }

Then I start the server with:然后我启动服务器:

node server.js

But when I run the client code via Firefox I get error:但是当我通过 Firefox 运行客户端代码时,出现错误:

Firefox can’t establish a connection to the server at wss://somedomain:someportnumber/.

And in Chromium I get error:在 Chromium 中,我收到错误消息:

(index):9 WebSocket connection to 'wss://thedomain.org:theportnumber/'    
failed: Error in connection establishment: net::ERR_CERT_AUTHORITY_INVALID

I am unsure how to proceed from here.我不确定如何从这里开始。 I would guess I somehow didn't make the certificates correctly but the error doesn't give me much to work with.我想我不知何故没有正确制作证书,但该错误并没有给我太多的工作。

Any help would be appreciated.任何帮助,将不胜感激。

ps I tried implementing the following directions, to no avail. ps我尝试执行以下说明,但无济于事。

https://stackoverflow.com/a/41366949/322537 https://stackoverflow.com/a/41366949/322537

Also, I have a suspicion the Chromium error "ERR_CERT_AUTHORITY_INVALID" is a key thing here.另外,我怀疑 Chromium 错误“ERR_CERT_AUTHORITY_INVALID”是这里的关键。 I googled it and found https://www.guildcafe.com/fix-net-err_cert_authority_invalid-error.html it has to do with the certificate authority.我用谷歌搜索,发现https://www.guildcafe.com/fix-net-err_cert_authority_invalid-error.html它与证书颁发机构有关。 which is just me.这只是我。 I'm still stranded though for I don't know how to fix it.我仍然搁浅,因为我不知道如何解决它。

It looks to me like you use a self-signed server certificate.在我看来,您使用的是自签名服务器证书。 You have to tell your client -- your Chromium instance--to trust that key before it will use a websocket to connect to the server you are developing.你必须告诉你的客户端——你的 Chromium 实例——在它使用 websocket 连接到你正在开发的服务器之前信任该密钥。 You can sweet-talk your browser into accepting an incorrectly signed https webpage connection, but not a websocket connection.您可以用甜言蜜语让浏览器接受签名不正确的 https 网页连接,但不能接受 websocket 连接。

In Chromium's case you actually need to tell your machine's OS (not the server machine, but rather the machine where you run Chromium).在 Chromium 的情况下,您实际上需要告诉您机器的操作系统(不是服务器机器,而是您运行 Chromium 的机器)。 Please look up those instructions for your OS.请查看适用于您的操作系统的说明。 You're looking up "trust self-signed certificate in Chrome on Ubuntu" or ".. on MacOS" or "...on Windows" or whatever.您正在查找“在 Ubuntu 上的 Chrome 中信任自签名证书”或“.. 在 MacOS 上”或“...在 Windows 上”或其他任何内容。

Firefox has its own UI for that purpose.为此,Firefox 有自己的用户界面。 You can read about that too.您也可以阅读相关内容。 It might be a quicker route to success for you.对你来说,这可能是一条更快的成功之路。

Then there's Let's Encrypt, with which you can make a trusted certificate for free.然后是 Let's Encrypt,您可以使用它免费制作受信任的证书。 For a tutorial, check this out.有关教程,请查看此内容。 https://itnext.io/node-express-letsencrypt-generate-a-free-ssl-certificate-and-run-an-https-server-in-5-minutes-a730fbe528ca https://itnext.io/node-express-letsencrypt-generate-a-free-ssl-certificate-and-run-an-https-server-in-5-minutes-a730fbe528ca

Are we having fun yet?我们刚才玩的高兴吗? Are we?我们是吗? Are we?我们是吗?

解决方案不是像书中描述的那样创建证书,而是简单地引用已经为给定网站创建的 ssl 证书。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM