ASP.NET Core Linux SSL – SSL Read Bio失败,出现OpenSSL错误

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

当一切正常的http请求一切正常。

当我通过https调用页面时,一切正常,但通过wss所做的WebSocket-Connection会立即closures。 也想出了(感谢乔恩 ),使一个StatusCode-Response没有响应身体产生的错误。 但我很确定他们有不同的出身。 Websocket错误似乎发生在升级响应。 问题已经报告给WebSockets Github-Repo( https://github.com/aspnet/WebSockets/issues/112 )。 这里报告了StatusCode的错误。

在program.cs中,我将Kestrelconfiguration为使用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(); 

好的,我发现这个问题可能与WebSocket-Connection有关。 所以在这里多一点WebSocket。

WebSocket在启动时使用以下代码进行configuration。

  app.Map("/socket", socketApp => { socketApp.UseWebSockets(new WebSocketOptions() { KeepAliveInterval = new TimeSpan(1, 0, 0, 0) }); socketApp.UseMiddleware<WebsocketServerMiddleware>(); }); 

WebsocketServerMiddleware的重要部分:

  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 } } 

WebSocketUrl在视图中由此创build:

  var url = "@(Context.Request.IsHttps ? "wss" : "ws")://@Context.Request.Host/socket"; 

正如前面提到的…在Windows everthing工作正常…只有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() 

我的openssl版本是:OpenSSL 1.0.2g-fips 2016年3月1日