0

我正在尝试在 C# 中构建一个类似于stunnel 客户端模式的 SSL 隧道;即将未加密的连接转发到加密的连接。

为什么这不起作用?

using System;
using System.Net;
using System.Net.Sockets;
using System.Net.Security;
using System.Collections;
using System.Security;
using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates;
using System.Threading;
using System.Text;
using System.IO;

namespace Teststunnel
{
    public class SslWrapper
    {
        public SslWrapper(IPEndPoint remote, int port)
        {
            listener = new TcpListener (port);
            Console.Error.WriteLine ("Established listener");
            listenport = port;
            server = remote;
            listener.Start ();
            new Thread (mainEventLoop).Start();
        }

        private TcpListener listener;

        private int listenport;

        private IPEndPoint server;

        private void mainEventLoop()
        {
        restart:
            Console.Error.WriteLine ("Waiting for connections...");
            TcpClient a = listener.AcceptTcpClient ();
            Console.Error.WriteLine ("Connection received!");
            tunnel (a);
            goto restart;
        }

        private void tunnel(TcpClient nconn)
        {
            SslStream sstrm = new SslStream (
                nconn.GetStream (),
                false);
            Console.Error.WriteLine (server.ToString());
            try
            {
                sstrm.AuthenticateAsClient (server.ToString());
            }
            catch (AuthenticationException e)
            {
                Console.WriteLine("Exception: {0}", e.Message);
                if (e.InnerException != null)
                {
                    Console.WriteLine("Inner exception: {0}", e.InnerException.Message);
                }
                Console.WriteLine ("Authentication failed - closing the connection.");
                nconn.Close();
                return;
            }
        }
    }
}

在我的主要功能中,我使用new SslWrapper (new IPEndPoint(IPAddress.Parse("198.23.240.183"), 443), 9999);. 但是,当我尝试连接时,应用程序崩溃:

未处理的异常:System.IO.IOException:身份验证或解密失败。---> Mono.Security.Protocol.Tls.TlsException:认证或解密失败。在 Mono.Security.Protocol.Tls.RecordProtocol.ReadRecordBuffer(Int32 contentType,System.IO.Stream 记录)[0x00000] 在:0 在 Mono.Security.Protocol.Tls.RecordProtocol.InternalReceiveRecordCallback (IAsyncResult asyncResult) [0x00000] 在: 0

是的,我正在使用单声道。但是,我确信我已经使用mozroots. 我究竟做错了什么?这也可以在 Microsoft .NET 上重现。

4

0 回答 0