SslManagedSocketConnection.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. using Ryujinx.HLE.HOS.Services.Sockets.Bsd;
  2. using Ryujinx.HLE.HOS.Services.Ssl.Types;
  3. using System;
  4. using System.IO;
  5. using System.Net.Security;
  6. using System.Net.Sockets;
  7. using System.Security.Authentication;
  8. namespace Ryujinx.HLE.HOS.Services.Ssl.SslService
  9. {
  10. class SslManagedSocketConnection : ISslConnectionBase
  11. {
  12. public int SocketFd { get; }
  13. public ISocket Socket { get; }
  14. private BsdContext _bsdContext;
  15. private SslVersion _sslVersion;
  16. private SslStream _stream;
  17. private bool _isBlockingSocket;
  18. private int _previousReadTimeout;
  19. public SslManagedSocketConnection(BsdContext bsdContext, SslVersion sslVersion, int socketFd, ISocket socket)
  20. {
  21. _bsdContext = bsdContext;
  22. _sslVersion = sslVersion;
  23. SocketFd = socketFd;
  24. Socket = socket;
  25. }
  26. private void StartSslOperation()
  27. {
  28. // Save blocking state
  29. _isBlockingSocket = Socket.Blocking;
  30. // Force blocking for SslStream
  31. Socket.Blocking = true;
  32. }
  33. private void EndSslOperation()
  34. {
  35. // Restore blocking state
  36. Socket.Blocking = _isBlockingSocket;
  37. }
  38. private void StartSslReadOperation()
  39. {
  40. StartSslOperation();
  41. if (!_isBlockingSocket)
  42. {
  43. _previousReadTimeout = _stream.ReadTimeout;
  44. _stream.ReadTimeout = 1;
  45. }
  46. }
  47. private void EndSslReadOperation()
  48. {
  49. if (!_isBlockingSocket)
  50. {
  51. _stream.ReadTimeout = _previousReadTimeout;
  52. }
  53. EndSslOperation();
  54. }
  55. // NOTE: We silence warnings about TLS 1.0 and 1.1 as games will likely use it.
  56. #pragma warning disable SYSLIB0039
  57. private static SslProtocols TranslateSslVersion(SslVersion version)
  58. {
  59. switch (version & SslVersion.VersionMask)
  60. {
  61. case SslVersion.Auto:
  62. return SslProtocols.Tls | SslProtocols.Tls11 | SslProtocols.Tls12 | SslProtocols.Tls13;
  63. case SslVersion.TlsV10:
  64. return SslProtocols.Tls;
  65. case SslVersion.TlsV11:
  66. return SslProtocols.Tls11;
  67. case SslVersion.TlsV12:
  68. return SslProtocols.Tls12;
  69. case SslVersion.TlsV13:
  70. return SslProtocols.Tls13;
  71. default:
  72. throw new NotImplementedException(version.ToString());
  73. }
  74. }
  75. #pragma warning restore SYSLIB0039
  76. public ResultCode Handshake(string hostName)
  77. {
  78. StartSslOperation();
  79. _stream = new SslStream(new NetworkStream(((ManagedSocket)Socket).Socket, false), false, null, null);
  80. _stream.AuthenticateAsClient(hostName, null, TranslateSslVersion(_sslVersion), false);
  81. EndSslOperation();
  82. return ResultCode.Success;
  83. }
  84. public ResultCode Peek(out int peekCount, Memory<byte> buffer)
  85. {
  86. // NOTE: We cannot support that on .NET SSL API.
  87. // As Nintendo's curl implementation detail check if a connection is alive via Peek, we just return that it would block to let it know that it's alive.
  88. peekCount = -1;
  89. return ResultCode.WouldBlock;
  90. }
  91. public int Pending()
  92. {
  93. // Unsupported
  94. return 0;
  95. }
  96. private static bool TryTranslateWinSockError(bool isBlocking, WsaError error, out ResultCode resultCode)
  97. {
  98. switch (error)
  99. {
  100. case WsaError.WSAETIMEDOUT:
  101. resultCode = isBlocking ? ResultCode.Timeout : ResultCode.WouldBlock;
  102. return true;
  103. case WsaError.WSAECONNABORTED:
  104. resultCode = ResultCode.ConnectionAbort;
  105. return true;
  106. case WsaError.WSAECONNRESET:
  107. resultCode = ResultCode.ConnectionReset;
  108. return true;
  109. default:
  110. resultCode = ResultCode.Success;
  111. return false;
  112. }
  113. }
  114. public ResultCode Read(out int readCount, Memory<byte> buffer)
  115. {
  116. if (!Socket.Poll(0, SelectMode.SelectRead))
  117. {
  118. readCount = -1;
  119. return ResultCode.WouldBlock;
  120. }
  121. StartSslReadOperation();
  122. try
  123. {
  124. readCount = _stream.Read(buffer.Span);
  125. }
  126. catch (IOException exception)
  127. {
  128. readCount = -1;
  129. if (exception.InnerException is SocketException socketException)
  130. {
  131. WsaError socketErrorCode = (WsaError)socketException.SocketErrorCode;
  132. if (TryTranslateWinSockError(_isBlockingSocket, socketErrorCode, out ResultCode result))
  133. {
  134. return result;
  135. }
  136. else
  137. {
  138. throw socketException;
  139. }
  140. }
  141. else
  142. {
  143. throw exception;
  144. }
  145. }
  146. finally
  147. {
  148. EndSslReadOperation();
  149. }
  150. return ResultCode.Success;
  151. }
  152. public ResultCode Write(out int writtenCount, ReadOnlyMemory<byte> buffer)
  153. {
  154. if (!Socket.Poll(0, SelectMode.SelectWrite))
  155. {
  156. writtenCount = 0;
  157. return ResultCode.WouldBlock;
  158. }
  159. StartSslOperation();
  160. try
  161. {
  162. _stream.Write(buffer.Span);
  163. }
  164. catch (IOException exception)
  165. {
  166. writtenCount = -1;
  167. if (exception.InnerException is SocketException socketException)
  168. {
  169. WsaError socketErrorCode = (WsaError)socketException.SocketErrorCode;
  170. if (TryTranslateWinSockError(_isBlockingSocket, socketErrorCode, out ResultCode result))
  171. {
  172. return result;
  173. }
  174. else
  175. {
  176. throw socketException;
  177. }
  178. }
  179. else
  180. {
  181. throw exception;
  182. }
  183. }
  184. finally
  185. {
  186. EndSslOperation();
  187. }
  188. // .NET API doesn't provide the size written, assume all written.
  189. writtenCount = buffer.Length;
  190. return ResultCode.Success;
  191. }
  192. public ResultCode GetServerCertificate(string hostname, Span<byte> certificates, out uint storageSize, out uint certificateCount)
  193. {
  194. byte[] rawCertData = _stream.RemoteCertificate.GetRawCertData();
  195. storageSize = (uint)rawCertData.Length;
  196. certificateCount = 1;
  197. if (rawCertData.Length > certificates.Length)
  198. {
  199. return ResultCode.CertBufferTooSmall;
  200. }
  201. rawCertData.CopyTo(certificates);
  202. return ResultCode.Success;
  203. }
  204. public void Dispose()
  205. {
  206. _bsdContext.CloseFileDescriptor(SocketFd);
  207. }
  208. }
  209. }