异步Socket与TLS/SSL

3

我在MSDN上看到了以下异步套接字的示例程序。我尝试过该程序并运行正常。是否可能修改异步套接字以支持TLS / SSL?该如何实现?

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;

// State object for reading client data asynchronously
public class StateObject {
    // Client  socket.
    public Socket workSocket = null;

    // Size of receive buffer.
    public const int BufferSize = 1024;

    // Receive buffer.
    public byte[] buffer = new byte[BufferSize];

    // Received data string.
    public StringBuilder sb = new StringBuilder();  
}

public class AsynchronousSocketListener {
    // Thread signal.
    public static ManualResetEvent allDone = new ManualResetEvent(false);

    public static void StartListening() {
        // Data buffer for incoming data.
        byte[] bytes = new Byte[1024];

        // Establish the local endpoint for the socket.
        // The DNS name of the computer
        // running the listener is "host.contoso.com".
        IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
        IPAddress ipAddress = ipHostInfo.AddressList[0];
        IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11000);

        // Create a TCP/IP socket.
        Socket listener = new Socket(AddressFamily.InterNetwork,
        SocketType.Stream, ProtocolType.Tcp );

        // Bind the socket to the local endpoint and listen for incoming connections.
        try {
            listener.Bind(localEndPoint);
            listener.Listen(100);

            while (true) {
                // Set the event to nonsignaled state.
                allDone.Reset();

                // Start an asynchronous socket to listen for connections.
                Console.WriteLine("Waiting for a connection...");

                listener.BeginAccept(new AsyncCallback(AcceptCallback), listener );

                // Wait until a connection is made before continuing.
                allDone.WaitOne();
            }
        } 
        catch (Exception e) {
            Console.WriteLine(e.ToString());
        }

        Console.WriteLine("\nPress ENTER to continue...");
        Console.Read();    
    }

    public static void AcceptCallback(IAsyncResult ar) {
        // Signal the main thread to continue.
        allDone.Set();

        // Get the socket that handles the client request.
        Socket listener = (Socket) ar.AsyncState;
        Socket handler = listener.EndAccept(ar);

        // Create the state object.
        StateObject state = new StateObject();
        state.workSocket = handler;
        handler.BeginReceive( state.buffer, 0, StateObject.BufferSize, 0,
        new AsyncCallback(ReadCallback), state);
    }

    public static void ReadCallback(IAsyncResult ar) {
        String content = String.Empty;

        // Retrieve the state object and the handler socket
        // from the asynchronous state object.
        StateObject state = (StateObject) ar.AsyncState;
        Socket handler = state.workSocket;

        // Read data from the client socket. 
        int bytesRead = handler.EndReceive(ar);

        if (bytesRead > 0) {
            // There  might be more data, so store the data received so far.
            state.sb.Append(Encoding.ASCII.GetString(
            state.buffer,0,bytesRead));

            // Check for end-of-file tag. If it is not there, read 
            // more data.
            content = state.sb.ToString();
            if (content.IndexOf("<EOF>") > -1) {
                // All the data has been read from the 
                // client. Display it on the console.
                Console.WriteLine("Read {0} bytes from socket. \n Data : {1}",
                content.Length, content );
                // Echo the data back to the client.
                Send(handler, content);
            } 
            else {
                // Not all data received. Get more.
                handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
                new AsyncCallback(ReadCallback), state);
            }
        }
    }

    private static void Send(Socket handler, String data) {
        // Convert the string data to byte data using ASCII encoding.
        byte[] byteData = Encoding.ASCII.GetBytes(data);

        // Begin sending the data to the remote device.
        handler.BeginSend(byteData, 0, byteData.Length, 0,
        new AsyncCallback(SendCallback), handler);
    }

    private static void SendCallback(IAsyncResult ar) {
        try {
            // Retrieve the socket from the state object.
            Socket handler = (Socket) ar.AsyncState;

            // Complete sending the data to the remote device.
            int bytesSent = handler.EndSend(ar);
            Console.WriteLine("Sent {0} bytes to client.", bytesSent);

            handler.Shutdown(SocketShutdown.Both);
            handler.Close();

        } 
        catch (Exception e) {
            Console.WriteLine(e.ToString());
        }
    }


    public static int Main(String[] args) {
        StartListening();
        return 0;
    }
}
4个回答

2

只是提供一个更新的链接,以获取更近期的文档,因为那个链接已经过时,可能在未来某个时间停止工作。https://learn.microsoft.com/zh-cn/windows/uwp/networking/sockets - undefined

2
“不确定如何在C#中实现,但是可以使用Java中的SSLEngine来完成。该API通常被认为是相当难编程的。因此,是的,可以使用异步套接字进行SSL/TLS,但我不确定Java的SSLEngine的等效物是什么。也许在C#中有另一个(更好的)API可用于此。”
“沿着这条路,你几乎肯定会遇到一些问题(基于Java的经验,但在C#中同样适用):”
“虽然SSLSocket倾向于表现得与普通Socket相似,但由于SSL/TLS的性质,存在一些轻微的边缘情况。这些差异的影响在异步I/O中更加重要。其中一些问题在“正确关闭SSLSocket”(在Java中)的这个(相当长的)答案中描述。
此外,一些 SSL/TLS 行为已经在应用层上定义不清,而且异步行为会变得有点混乱。我想到的是客户端证书重新协商(或者重新协商一般情况)。使用 SSL/TLS,任何一方原则上都可以发起重新协商握手。例如,在 Apache Httpd 中只保护一个目录使用客户端证书,或者在 Java 容器中只有 Web 应用程序的一部分需要 CLIENT-CERT。即使是 IIS 在使用客户端证书身份验证时也默认使用重新协商。这包括在 SSL/TLS 连接期间执行第二次握手(有效地从客户端获取更多信息:其客户端证书)。
当它工作时(通常使用阻塞 I/O),流量看起来像这样(这里同时涉及 SSL 和 HTTP 层):
C->S SSL Client Hello
S->C SSL Server Hello, Certificate, Server Hello Done
C->S SSL Client Key Exchange, Change Cipher Spec, Finished
S->C SSL Change Cipher Spec
(then encrypted)
C->S SSL Finished
C->S HTTP GET /.../
S->C SSL Hello Request
C->S SSL Client Hello
S->C SSL Server Hello, Certificate, Certificate Request, Server Hello Done
C->S SSL Certificate, Client Key Exchange, Certificate Verify, Change Cipher
Spec, Finished
S->C SSL Change Cipher Spec
C->S SSL Finished
S->C HTTP 200 OK

异步模式下重新协商相当棘手,因为重新协商应该同时适用于流量的双方。因此,在应用层使用 SSL/TLS 会话期间,SSL/TLS 会话的基本属性可能会发生变化(通常不希望应用程序层处理此类情况)。一方仍然可以发送数据,假定某些 SSL/TLS 设置,而重新协商正在进行,从而影响双方。
所有这些的实现可能很困难,例如在Grizzly issue中所示。

谢谢Bruno,C#不支持SSLEngine。我尝试通过组合异步使用SSLStream,但仍然存在一些错误。 - Javanese Girl
@JavaneseGirl,确实,如果我没有表达清楚,我很抱歉。我只是说通常情况下是可能的,但你需要类似于SSLEngine的东西(我不确定需要什么,你可能需要第三方库)。虽然SSLEngine(在Java中)为您完成了大部分实际的SSL/TLS工作,但知道何时进行wrap/unwrap(请参阅其文档)仍需要相当多的手动工作,特别是在异步模式下。老实说,我不知道SSLStream是否支持类似的功能,但我无法想象编程会很容易(特别是由于我提到的理论问题)。 - Bruno
@JavaneseGirl 感谢您接受我的答案。看起来您是一个比较新的用户,确实SO鼓励发布者接受答案,但不要感到过于着急。我的回答可能是不完整的(缺少C#解决方案),如果您保持开放更长时间,您可能会得到更好的答案。 - Bruno
在 .net 中,你可以告诉 SslStream 不允许重新协商。事实上,从 .net 7 开始,默认情况下是禁用的,而且 TLS 1.3 本身也不支持重新协商,因为它是一个安全漏洞。所以,除非你明确需要支持某个使用较旧的 TLS 版本并执行无法禁用的重新协商的旧软件,否则这实际上不是一个问题。 - undefined

1
在.NET中,使用SslStream类。它拥有构建.NET中SSL客户端/服务器所需的一切。非常容易集成。适用于所有版本的.NET。

这样做。以您喜欢的方式打开套接字,创建一个基于它的NetworkStream,再在其上创建一个SslStream,调用适当的AuthenticateAsX方法,然后像与任何其他流一样进行交互,唯一的例外是您无法寻找它或检查位置。在其上添加一个BinaryReader/Writer或StreamReader/Writer,与与控制台交互完全相同。 - undefined

-1

直接套接字通信工作在会话OSI模型层。TLS和SSL工作在表示层(比会话层更高)。因此,据我所知,您不能直接使用套接字并具有SSL或TLS级别的安全性。

查看OSI模型


4
SSL/TLS的设计目标之一是在现有的“会话层”之上模拟一个层(使顶部层更安全)。话虽如此,讨论TCP套接字时不应直接跳入OSI模型。TCP/IP模型没有那么严格(或分层),您所链接的维基百科页面上有相关部分介绍。 - Bruno
谢谢Bruno,我会去看看的。 - Adam Spicer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接