【发布时间】:2014-02-24 13:53:45
【问题描述】:
我编写了一个 WebSocket 服务器,它目前仅适用于 ws:// 连接。
使用 http://www.websocket.org/echo.html(在 Chrome 上)和 http://websocket4net.codeplex.com/ 测试。
现在我还想支持wss:// 连接 (TLS)。
我已经让它与 WebSocket4Net 一起使用,但是在使用回声测试时,
在 C# 代码中执行SslStream.AuthenticateAsServer(); 后,javascript 立即抛出异常。
它进行 TLS 协商,但 Chrome 拒绝连接。
SslStream 读写(ServerSide 由 AuthenticateAsServer 完成):
Read(5, 0, 5) == 5
Read(517, 5, 512) == 512
Write(887, 0, 887)
Read(887, 0, 5) == 5
Read(887, 5, 262) == 262
Read(5, 0, 5) == 5
Read(6, 5, 1) == 1
Read(5, 0, 5) == 5
Read(53, 5, 48) == 48
Write(59, 0, 59)
我使用以下步骤添加证书:
makecert -sv CA.pvk -r -n "CN=Dev" -a sha256 -len 2048 -sky signature -cy authority CA.cer
makecert -ic CA.cer -iv CA.pvk -n "CN=localhost, CN=127.0.0.1" -a sha256 -len 2048 -sky exchange -sv CA_localhost.pvk -pe CA_localhost.cer
cert2spc CA_localhost.cer CA_localhost.spc
pvkimprt -pfx CA_localhost.spc CA_localhost.pvk //Select export
//Import CA.cer into your Computer store's Trusted Root Certification Authorities (certmgr.msc)
//Import the private key that the server is going to use into the server machine's Personal store.
//This is achieved by importing the .pfx file that you generated earlier.
由new SslStream(new NetworkStream(clientSocket), true)创建的SslStream
对于 AuthenticateAsServer,我使用的是“CA_localhost.cer”证书。
确保证书显示在 certmgr.msc 中后,我打开了
在 Chrome 中使用 http://www.websocket.org/echo.html 并使用 wss://localhost:12345 作为地址(我的服务器正在侦听)并选中复选框。
点击Connect后我只得到:
ERROR: undefined
DISCONNECTED
javascript 控制台显示:
WebSocket 连接到“wss://127.0.0.1:12345/?encoding=text”失败:在收到握手响应之前连接已关闭
在AuthenticateAsServer 之后的服务器端,属性IsAuthenticated 和IsEncrypted 设置为true,但之后我立即收到零长度的数据,关闭连接。
我假设我在证书创建和/或安装过程中出错了,但我不知道我做错了什么。
有什么想法吗?
【问题讨论】: