sta / sta/websocket-sharp

Server with SSL enabled throws exceptions while receiving handshake

Open
#37 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C#
Stars
6.1k
Forks
1.7k
PR merge metrics
No merged PRs in 30d

Description

I'm trying to get a websocket server set up that runs on port 7031 and uses SSL. However, whenever I try to open a connection with the server it doesn't work, and the log file has this information:

4/7/2014 8:25:39 PM|Fatal|<>c__DisplayClass3.<acceptRequestAsync>b__2:479|WebSocketSharp.WebSocketException: An exception has occurred while receiving a handshake. ---> System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
                          Parameter name: value:-1 char: 
                             at WebSocketSharp.Ext.EqualsWith(Int32 value, Char c, Action`1 action) in c:\Sources\websocket-sharp\websocket-sharp\Ext.cs:line 444
                             at WebSocketSharp.WsStream.readHandshakeHeaders(Stream stream) in c:\Sources\websocket-sharp\websocket-sharp\WsStream.cs:line 121
                             at WebSocketSharp.WsStream.ReadHandshake[T](Func`2 parser, Int32 millisecondsTimeout) in c:\Sources\websocket-sharp\websocket-sharp\WsStream.cs:line 202
                             --- End of inner exception stack trace ---
                             at WebSocketSharp.WsStream.ReadHandshake[T](Func`2 parser, Int32 millisecondsTimeout) in c:\Sources\websocket-sharp\websocket-sharp\WsStream.cs:line 225
                             at WebSocketSharp.Net.WebSockets.TcpListenerWebSocketContext..ctor(TcpClient client, X509Certificate cert, Boolean secure, Logger logger) in c:\Sources\websocket-sharp\websocket-sharp\Net\WebSockets\TcpListenerWebSocketContext.cs:line 67
                             at WebSocketSharp.Ext.GetWebSocketContext(TcpClient client, X509Certificate cert, Boolean secure, Logger logger) in c:\Sources\websocket-sharp\websocket-sharp\Ext.cs:line 515
                             at WebSocketSharp.Server.WebSocketServer.<>c__DisplayClass3.<acceptRequestAsync>b__2(Object state) in c:\Sources\websocket-sharp\websocket-sharp\Server\WebSocketServer.cs:line 479

I amended the function EqualsWith to show the int and char passed in so that is where the -1 comes from on the second line. I'm starting the server the following way:

_secureListener = new WebSocketServer(7031, true);
                var certLocation = Path.Combine(Path.GetDirectoryName(
                    new Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath), "server.pfx");
                _secureListener.Certificate = new X509Certificate2(certLocation, "password");
                _secureListener.AddWebSocketService<Listener>("/");
                _secureListener.Log.File = Path.Combine(Path.GetDirectoryName(
                    new Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath), "websocketlog.txt");
                _secureListener.Start();

The certificate being used was created the following way:

makecert.exe -r -pe -n "CN=localhost" -sky exchange -sv server.pvk server.cer
pvk2pfx -pvk server.pvk -spc server.cer -pfx server.pfx

When I connect from my application's corresponding browser extensions (Chrome & Firefox) the connection is immediately closed with the status code 1006.

Any ideas what could be going wrong?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with WsStream.cs at readHandshakeHeaders (line 121) and Ext.cs at EqualsWith (line 444), using the provided stack trace and SSL server setup as the reproduction context. Test secure connections from Chrome and Firefox and trace the handshake input that produces -1; done means the server accepts the SSL WebSocket handshake without the exception and the clients no longer close with status 1006.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
networking
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.