SSLStream:“调用SSPI失败”异常

时间:2012-10-29 12:55:33

标签: c# mono .net-2.0 sslstream sspi


我有一个奇怪的问题:我在c#中编写了一个基于.net2的服务器和客户端,它们通过.net 2 SslStream聊天。在C1和S之间有1个连接用于发送命令,在cl和s之间有一个用于发送文件的连接 在我的Windows机器上本地一切正常。但是,如果我在我的Linux服务器上运行服务器(使用单声道),共享文件在某些​​情况下不起作用。我有不同的文件类别,其中有2个工作,第三个,服务器挂起SSLStream.AuthenticateAsServer(....);,客户端抛出一个异常,消息“调用SSPI失败,请参阅内部异常”。 innerexception是一个System.ComponentModel.Win32Exception,消息“收到的消息是意外的或格式错误。”

我认为我在linux上运行它的方式可能是错误的。实际上我使用VS2012进行本地开发,当我想把它放在服务器上时,我使用mono server.exe上传VS和Im的编译exe来启动它。但是我也尝试在Windows上使用monodevelop来编译它(monodevelop中编译的东西也在本地工作)并在linux服务器上使用它,但结果却相同。

非常感谢任何帮助。



这是我缩短的代码:

客户端:

//gets auth guid before, then it does
Thread Thre = new Thread(sendfile);
Thre.Start(authguid);


private void sendfile(string guid){
TcpClient cl = new TcpClient();
cl.Connect(hostname, 8789);
SslStream Stream = new SslStream(cl.GetStream(), true, new RemoteCertificateValidationCallback(ServerConnector.ValidateServerCertificate));
Stream.AuthenticateAsClient(hostname);//throws the exception
//sends auth guid and file

}

服务器:

 public class FServer
    {

        protected static bool halt = false;
        protected List<FConnection> connections;
        private TcpListener tcpServer;
        private IPEndPoint ipEnde = new IPEndPoint(IPAddress.Any, 8789);
        private delegate void dlgAccept();
        private dlgAccept accepting;
        private IAsyncResult resAccept;

        public FServer()
        {
            tcpServer = new TcpListener(ipEnde);
            connections = new List<FConnection>();
            tcpServer.Start();
            accepting = new dlgAccept(accept);
            resAccept = accepting.BeginInvoke(null, null);


        }

        public void accept()
        {
            do
            {
                if (tcpServer.Pending())
                    connections.Add(new FConnection(tcpServer.AcceptTcpClient(), this));

                else
                    Thread.Sleep(750);
            } while (!halt && tcpServer != null);
        }

        public class FConnection 
        {
           public SslStream stream;
            //.....some other properties

           public static bool ValidateClientCertificate (object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
            {
                return true;

            }

            public FConnection(TcpClient cl, FServer inst)
            {
                instance = inst;
                client = cl;

                    stream = new SslStream(client.GetStream(), false, new RemoteCertificateValidationCallback(ValidateClientCertificate), null);
                    stream.AuthenticateAsServer(instance.mainserver.serverCert, false, SslProtocols.Tls, false);//hangs sometimes on this
                    if (stream.IsAuthenticated && stream.IsEncrypted)
                    {
                    }
                    else
                    {
                        this.Dispose();
                    }

                    //auth and receiving file

            }

        }
    }

2 个答案:

答案 0 :(得分:5)

好的,我更深入地研究了这个问题,并在此处找到了解决方案:

http://social.msdn.microsoft.com/forums/en-US/netfxnetcom/thread/18b4a0ce-b348-403d-8655-fe9d558f8d6b

信用额度来自MSDN paksys

问题出在客户端,我改变了

Stream.AuthenticateAsClient(hostname);

X509Certificates.X509Certificate2Collection xc = new X509Certificates.X509Certificate2Collection();
Stream.AuthenticateAsClient(hostname, xc, Security.Authentication.SslProtocols.Tls, false);

答案 1 :(得分:2)

我遇到了同样的问题,接受的答案对我不起作用。问题是服务器证书使用SHA-2。这是我用来修复它的代码:

X509Certificates.X509Certificate2Collection xc = new X509Certificates.X509Certificate2Collection();
Stream.AuthenticateAsClient(hostname, xc, Security.Authentication.SslProtocols.Tls12, false);
  • 请注意,与接受答案的唯一区别是sslProtocol

希望它可以帮助某人