C# ASP.NET核心Linux SSL-SSL读取Bio失败,出现OpenSSL错误

C# ASP.NET核心Linux SSL-SSL读取Bio失败,出现OpenSSL错误,c#,linux,ssl,https,asp.net-core,C#,Linux,Ssl,Https,Asp.net Core,我目前正在开发一个asp.net核心应用程序。应用程序本身在Windows下使用Kestrel运行得非常完美。现在,我将应用程序发布到linux环境(Lubuntu 16.04.1 64位) 向所有内容发出正常http请求时,一切正常 当我通过https调用页面时,一切正常,但通过wss建立的WebSocket连接立即关闭。还发现(多亏了)在没有响应主体的情况下进行状态码响应会产生该错误。但我很确定两者都有不同的起源。Websocket错误似乎发生在升级响应时。问题已报告给WebSockets

我目前正在开发一个asp.net核心应用程序。应用程序本身在Windows下使用Kestrel运行得非常完美。现在,我将应用程序发布到linux环境(Lubuntu 16.04.1 64位)

向所有内容发出正常http请求时,一切正常

当我通过https调用页面时,一切正常,但通过wss建立的WebSocket连接立即关闭。还发现(多亏了)在没有响应主体的情况下进行状态码响应会产生该错误。但我很确定两者都有不同的起源。Websocket错误似乎发生在升级响应时。问题已报告给WebSockets Github Repo()。报告状态代码错误

在program.cs中,我将Kestrel配置为使用ssl

IConfigurationRoot config = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory() + "/Config")
            .AddJsonFile("appsettings.json")
            .Build();

        IWebHost host = new WebHostBuilder()
            .UseConfiguration(config)
            .UseContentRoot(Directory.GetCurrentDirectory())
            .UseKestrel(options =>
            {
                options.NoDelay = true;
                options.UseHttps(new X509Certificate2(Directory.GetCurrentDirectory() + config["SSL:Cert:Path"], config["SSL:Cert:Password"])); //Currently set to 'cert.pfx' with pw 'cert'
            })
            .UseIISIntegration()
            .UseStartup<Startup>()
            .Build();

        host.Run();
如前所述。。。在windows上,一切正常。。。只有linux在制造问题

以下是Stacktrace:

System.IO.IOException: The encryption operation failed, see inner exception. ---> Interop+OpenSsl+SslException: SSL Read BIO failed with OpenSSL error - .
   at Interop.OpenSsl.BioRead(SafeBioHandle bio, Byte[] buffer, Int32 count)
   at Interop.OpenSsl.Encrypt(SafeSslHandle context, Byte[] input, Int32 offset, Int32 count, Byte[]& output, SslErrorCode& errorCode)
   at System.Net.SslStreamPal.EncryptDecryptHelper(SafeDeleteContext securityContext, Byte[] input, Int32 offset, Int32 size, Boolean encrypt, Byte[]& output, Int32& resultSize)
   --- End of inner exception stack trace ---
   at System.Net.Security.SslStreamInternal.StartWriting(Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslStreamInternal.ProcessWrite(Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslStreamInternal.BeginWrite(Byte[] buffer, Int32 offset, Int32 count, AsyncCallback asyncCallback, Object asyncState)
   at System.Net.Security.SslStream.<>c.<WriteAsync>b__86_0(Byte[] bufferArg, Int32 offsetArg, Int32 sizeArg, AsyncCallback callback, Object state)
   at System.Threading.Tasks.TaskFactory`1.FromAsyncImpl[TArg1,TArg2,TArg3](Func`6 beginMethod, Func`2 endFunction, Action`1 endAction, TArg1 arg1, TArg2 arg2, TArg3 arg3, Object state, TaskCreationOptions creationOptions)
   at System.Threading.Tasks.TaskFactory.FromAsync[TArg1,TArg2,TArg3](Func`6 beginMethod, Action`1 endMethod, TArg1 arg1, TArg2 arg2, TArg3 arg3, Object state)
   at System.Net.Security.SslStream.WriteAsync(Byte[] buffer, Int32 offset, Int32 size, CancellationToken cancellationToken)
   at Microsoft.AspNetCore.Server.Kestrel.Filter.Internal.StreamSocketOutput.WriteAsync(ArraySegment`1 buffer, Boolean chunk, CancellationToken cancellationToken)
   at Microsoft.AspNetCore.Server.Kestrel.Internal.Http.Frame.<FlushAsync>d__139.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Server.Kestrel.Internal.Http.Frame.<Microsoft-AspNetCore-Http-Features-IHttpUpgradeFeature-UpgradeAsync>d__249.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.WebSockets.Server.WebSocketMiddleware.UpgradeHandshake.<AcceptAsync>d__6.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at HomeSystem.Methods.WebsocketServerMiddleware.<Invoke>d__5.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Builder.Extensions.MapMiddleware.<Invoke>d__3.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.<Invoke>d__6.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.<Invoke>d__6.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Hosting.Internal.RequestServicesContainerMiddleware.<Invoke>d__3.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Server.Kestrel.Internal.Http.Frame`1.<RequestProcessingAsync>d__2.MoveNext()
System.IO.IOException:加密操作失败,请参阅内部异常。-->Interop+OpenSsl+SslException:SSL读取BIO失败,OpenSsl错误-。
在Interop.OpenSsl.biorad(SafeBioHandle bio,字节[]缓冲区,Int32计数)
在Interop.OpenSsl.Encrypt(SafeSslHandle上下文、字节[]输入、Int32偏移量、Int32计数、字节[]和输出、SslErrorCode和errorCode)
在System.Net.SslStreamPal.EncryptDecryptDecryptHelper(SafeDeleteContext securityContext,字节[]输入,Int32偏移量,Int32大小,布尔加密,字节[]和输出,Int32和resultSize)
---内部异常堆栈跟踪的结束---
位于System.Net.Security.SslStreamInternal.StartWriting(字节[]缓冲区、Int32偏移量、Int32计数、AsyncProtocolRequest asyncRequest)
位于System.Net.Security.SslStreamInternal.ProcessWrite(字节[]缓冲区、Int32偏移量、Int32计数、AsyncProtocolRequest asyncRequest)
位于System.Net.Security.SslStreamInternal.BeginWrite(字节[]缓冲区、Int32偏移量、Int32计数、AsyncCallback AsyncCallback、Object asyncState)
在System.Net.Security.SslStream.c.b_uu86_0(字节[]bufferArg、Int32 offsetArg、Int32 sizeArg、AsyncCallback回调、对象状态)
在System.Threading.Tasks.TaskFactory`1.FromAsyncImpl[TArg1,TArg2,TArg3](Func`6 beginMethod,Func`2 endFunction,Action`1 endAction,TArg1 arg1,TArg2 arg2,TArg3 arg3,对象状态,TaskCreationOptions creationOptions creationOptions)
在System.Threading.Tasks.TaskFactory.FromAsync[TArg1,TArg2,TArg3](Func`6 beginMethod,Action`1 endMethod,TArg1 arg1,TArg2 arg2,TArg3 arg3,对象状态)
位于System.Net.Security.SslStream.WriteAsync(字节[]缓冲区,Int32偏移量,Int32大小,CancellationToken CancellationToken)
位于Microsoft.AspNetCore.Server.Kestrel.Filter.Internal.StreamSocketOutput.WriteAsync(ArraySegment`1缓冲区,布尔块,CancellationToken CancellationToken)
在Microsoft.AspNetCore.Server.Kestrel.Internal.Http.Frame.d_u139.MoveNext()中
---来自引发异常的上一个位置的堆栈结束跟踪---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)
在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中
在Microsoft.AspNetCore.Server.Kestrel.Internal.Http.Frame.d_u249.MoveNext()中
---来自引发异常的上一个位置的堆栈结束跟踪---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)
在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中
在Microsoft.AspNetCore.WebSockets.Server.WebSocketMiddleware.UpgradeHandshake.d_u6.MoveNext()中
---来自引发异常的上一个位置的堆栈结束跟踪---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)
在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中
在HomeSystem.Methods.WebsocketServerMiddleware.d_u5.MoveNext()中
---来自引发异常的上一个位置的堆栈结束跟踪---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)
在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中
在Microsoft.AspNetCore.Builder.Extensions.MapMiddleware.d_u3.MoveNext()中
---来自引发异常的上一个位置的堆栈结束跟踪---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)
在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中
在Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.d_u6.MoveNext()中
---来自引发异常的上一个位置的堆栈结束跟踪---
在Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.d_u6.MoveNext()中
---来自引发异常的上一个位置的堆栈结束跟踪---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)
在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中
在Microsoft.AspNetCore.Hosting.Internal.RequestServicesContainerMiddleware.d_u3.MoveNext()中
---来自引发异常的上一个位置的堆栈结束跟踪---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)
在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中
在Microsoft.AspNetCore.Server.Kestrel.Internal.Http.Frame`1.d_uu2.MoveNext()中
我的openssl版本是:
OpenSSL 1.0.2g-fips 2016年3月1日

这台机器上的OpenSSL版本是什么?OpenSSL版本的结果是:OpenSSL 1.0.2g-fips 2016年3月1日我在一个带有https和kestrelCorefx的非websocket请求上看到了相同的错误。这里提出的问题我在这里描述的问题与websocket有关。当我禁用它们时,一切正常。我已经为WebSocket的回购发行了一期:。看来他们已经知道这个问题了。
        if (context.WebSockets.IsWebSocketRequest)
        {
            WebSocket socket = await context.WebSockets.AcceptWebSocketAsync();

            if (socket != null && socket.State == WebSocketState.Open)
            {
                 ArraySegment<byte> buffer = new ArraySegment<byte>(new byte[4096]);
                 await socket.ReceiveAsync(buffer, CancellationToken.None);

                 ... followed by handling of the message ... gets never called
            }
        }
 var url = "@(Context.Request.IsHttps ? "wss" : "ws")://@Context.Request.Host/socket";
System.IO.IOException: The encryption operation failed, see inner exception. ---> Interop+OpenSsl+SslException: SSL Read BIO failed with OpenSSL error - .
   at Interop.OpenSsl.BioRead(SafeBioHandle bio, Byte[] buffer, Int32 count)
   at Interop.OpenSsl.Encrypt(SafeSslHandle context, Byte[] input, Int32 offset, Int32 count, Byte[]& output, SslErrorCode& errorCode)
   at System.Net.SslStreamPal.EncryptDecryptHelper(SafeDeleteContext securityContext, Byte[] input, Int32 offset, Int32 size, Boolean encrypt, Byte[]& output, Int32& resultSize)
   --- End of inner exception stack trace ---
   at System.Net.Security.SslStreamInternal.StartWriting(Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslStreamInternal.ProcessWrite(Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslStreamInternal.BeginWrite(Byte[] buffer, Int32 offset, Int32 count, AsyncCallback asyncCallback, Object asyncState)
   at System.Net.Security.SslStream.<>c.<WriteAsync>b__86_0(Byte[] bufferArg, Int32 offsetArg, Int32 sizeArg, AsyncCallback callback, Object state)
   at System.Threading.Tasks.TaskFactory`1.FromAsyncImpl[TArg1,TArg2,TArg3](Func`6 beginMethod, Func`2 endFunction, Action`1 endAction, TArg1 arg1, TArg2 arg2, TArg3 arg3, Object state, TaskCreationOptions creationOptions)
   at System.Threading.Tasks.TaskFactory.FromAsync[TArg1,TArg2,TArg3](Func`6 beginMethod, Action`1 endMethod, TArg1 arg1, TArg2 arg2, TArg3 arg3, Object state)
   at System.Net.Security.SslStream.WriteAsync(Byte[] buffer, Int32 offset, Int32 size, CancellationToken cancellationToken)
   at Microsoft.AspNetCore.Server.Kestrel.Filter.Internal.StreamSocketOutput.WriteAsync(ArraySegment`1 buffer, Boolean chunk, CancellationToken cancellationToken)
   at Microsoft.AspNetCore.Server.Kestrel.Internal.Http.Frame.<FlushAsync>d__139.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Server.Kestrel.Internal.Http.Frame.<Microsoft-AspNetCore-Http-Features-IHttpUpgradeFeature-UpgradeAsync>d__249.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.WebSockets.Server.WebSocketMiddleware.UpgradeHandshake.<AcceptAsync>d__6.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at HomeSystem.Methods.WebsocketServerMiddleware.<Invoke>d__5.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Builder.Extensions.MapMiddleware.<Invoke>d__3.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.<Invoke>d__6.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.<Invoke>d__6.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Hosting.Internal.RequestServicesContainerMiddleware.<Invoke>d__3.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Server.Kestrel.Internal.Http.Frame`1.<RequestProcessingAsync>d__2.MoveNext()