I have a server that is listening for clients with a TcpListener.BeginAcceptTcpClient()
method. When a client connects, the EndAcceptTcpClient
method gives me the TcpClient
object and then I call that client stream's NetworkStream.BeginRead()
to listen for data transmitted from the client.
Everything works well until a client (who is expecting the server to support SSL) attempts to connect to the (insecure) server. The data that is retrieved from the NetworkStream.BeginRead()
callback when the client attempts to do the SSL handshaking, is gibberish of course, but what is the proper way to detect this handshake after reading the data vs a "normal" message from a client?
Does an SSL handshake have a known header/signature identifier that I could check for by looking at the first few bytes of received messages to distiguish the difference between a normal message and the SSL handshake? From inspection the bytes received, it looks like it may be ASCII 0x16 0x03 0x01 0x00
(a.k.a <SYN><ETX><SOH><NUL>
).
If there is a known header, is there a response I can send to the client to tell it the authentication failed (via the NetworkStream)?