为什么C#中的SSL隧道不起作用?

时间:2013-06-19 15:41:04

标签: c# .net sockets ssl mono

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

为什么这不起作用?

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 record)[0x00000] in:0       在Mono.Security.Protocol.Tls.RecordProtocol.InternalReceiveRecordCallback(IAsyncResult asyncResult)[0x00000] in:0

  

是的,我正在使用Mono。但是,我确信我已使用mozroots导入了所有正确的SSL根证书。我究竟做错了什么?这在Microsoft .NET上也是可重现的。

0 个答案:

没有答案