ISslConnection.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. using Ryujinx.Common.Logging;
  2. using Ryujinx.HLE.Exceptions;
  3. using Ryujinx.HLE.HOS.Services.Sockets.Bsd;
  4. using Ryujinx.HLE.HOS.Services.Ssl.Types;
  5. using Ryujinx.Memory;
  6. using System;
  7. using System.Text;
  8. namespace Ryujinx.HLE.HOS.Services.Ssl.SslService
  9. {
  10. class ISslConnection : IpcService, IDisposable
  11. {
  12. private bool _doNotClockSocket;
  13. private bool _getServerCertChain;
  14. private bool _skipDefaultVerify;
  15. private bool _enableAlpn;
  16. private SslVersion _sslVersion;
  17. private IoMode _ioMode;
  18. private VerifyOption _verifyOption;
  19. private SessionCacheMode _sessionCacheMode;
  20. private string _hostName;
  21. private ISslConnectionBase _connection;
  22. private BsdContext _bsdContext;
  23. private readonly ulong _processId;
  24. private byte[] _nextAplnProto;
  25. public ISslConnection(ulong processId, SslVersion sslVersion)
  26. {
  27. _processId = processId;
  28. _sslVersion = sslVersion;
  29. _ioMode = IoMode.Blocking;
  30. _sessionCacheMode = SessionCacheMode.None;
  31. _verifyOption = VerifyOption.PeerCa | VerifyOption.HostName;
  32. }
  33. [CommandHipc(0)]
  34. // SetSocketDescriptor(u32) -> u32
  35. public ResultCode SetSocketDescriptor(ServiceCtx context)
  36. {
  37. if (_connection != null)
  38. {
  39. return ResultCode.AlreadyInUse;
  40. }
  41. _bsdContext = BsdContext.GetContext(_processId);
  42. if (_bsdContext == null)
  43. {
  44. return ResultCode.InvalidSocket;
  45. }
  46. int inputFd = context.RequestData.ReadInt32();
  47. int internalFd = _bsdContext.DuplicateFileDescriptor(inputFd);
  48. if (internalFd == -1)
  49. {
  50. return ResultCode.InvalidSocket;
  51. }
  52. InitializeConnection(internalFd);
  53. int outputFd = inputFd;
  54. if (_doNotClockSocket)
  55. {
  56. outputFd = -1;
  57. }
  58. context.ResponseData.Write(outputFd);
  59. return ResultCode.Success;
  60. }
  61. private void InitializeConnection(int socketFd)
  62. {
  63. ISocket bsdSocket = _bsdContext.RetrieveSocket(socketFd);
  64. _connection = new SslManagedSocketConnection(_bsdContext, _sslVersion, socketFd, bsdSocket);
  65. }
  66. [CommandHipc(1)]
  67. // SetHostName(buffer<bytes, 5>)
  68. public ResultCode SetHostName(ServiceCtx context)
  69. {
  70. ulong hostNameDataPosition = context.Request.SendBuff[0].Position;
  71. ulong hostNameDataSize = context.Request.SendBuff[0].Size;
  72. byte[] hostNameData = new byte[hostNameDataSize];
  73. context.Memory.Read(hostNameDataPosition, hostNameData);
  74. _hostName = Encoding.ASCII.GetString(hostNameData).Trim('\0');
  75. Logger.Info?.Print(LogClass.ServiceSsl, _hostName);
  76. return ResultCode.Success;
  77. }
  78. [CommandHipc(2)]
  79. // SetVerifyOption(nn::ssl::sf::VerifyOption)
  80. public ResultCode SetVerifyOption(ServiceCtx context)
  81. {
  82. _verifyOption = (VerifyOption)context.RequestData.ReadUInt32();
  83. Logger.Stub?.PrintStub(LogClass.ServiceSsl, new { _verifyOption });
  84. return ResultCode.Success;
  85. }
  86. [CommandHipc(3)]
  87. // SetIoMode(nn::ssl::sf::IoMode)
  88. public ResultCode SetIoMode(ServiceCtx context)
  89. {
  90. if (_connection == null)
  91. {
  92. return ResultCode.NoSocket;
  93. }
  94. _ioMode = (IoMode)context.RequestData.ReadUInt32();
  95. _connection.Socket.Blocking = _ioMode == IoMode.Blocking;
  96. Logger.Stub?.PrintStub(LogClass.ServiceSsl, new { _ioMode });
  97. return ResultCode.Success;
  98. }
  99. [CommandHipc(4)]
  100. // GetSocketDescriptor() -> u32
  101. public ResultCode GetSocketDescriptor(ServiceCtx context)
  102. {
  103. context.ResponseData.Write(_connection.SocketFd);
  104. return ResultCode.Success;
  105. }
  106. [CommandHipc(5)]
  107. // GetHostName(buffer<bytes, 6>) -> u32
  108. public ResultCode GetHostName(ServiceCtx context)
  109. {
  110. ulong hostNameDataPosition = context.Request.ReceiveBuff[0].Position;
  111. ulong hostNameDataSize = context.Request.ReceiveBuff[0].Size;
  112. byte[] hostNameData = new byte[hostNameDataSize];
  113. Encoding.ASCII.GetBytes(_hostName, hostNameData);
  114. context.Memory.Write(hostNameDataPosition, hostNameData);
  115. context.ResponseData.Write((uint)_hostName.Length);
  116. Logger.Info?.Print(LogClass.ServiceSsl, _hostName);
  117. return ResultCode.Success;
  118. }
  119. [CommandHipc(6)]
  120. // GetVerifyOption() -> nn::ssl::sf::VerifyOption
  121. public ResultCode GetVerifyOption(ServiceCtx context)
  122. {
  123. context.ResponseData.Write((uint)_verifyOption);
  124. Logger.Stub?.PrintStub(LogClass.ServiceSsl, new { _verifyOption });
  125. return ResultCode.Success;
  126. }
  127. [CommandHipc(7)]
  128. // GetIoMode() -> nn::ssl::sf::IoMode
  129. public ResultCode GetIoMode(ServiceCtx context)
  130. {
  131. context.ResponseData.Write((uint)_ioMode);
  132. Logger.Stub?.PrintStub(LogClass.ServiceSsl, new { _ioMode });
  133. return ResultCode.Success;
  134. }
  135. [CommandHipc(8)]
  136. // DoHandshake()
  137. public ResultCode DoHandshake(ServiceCtx context)
  138. {
  139. if (_connection == null)
  140. {
  141. return ResultCode.NoSocket;
  142. }
  143. return _connection.Handshake(_hostName);
  144. }
  145. [CommandHipc(9)]
  146. // DoHandshakeGetServerCert() -> (u32, u32, buffer<bytes, 6>)
  147. public ResultCode DoHandshakeGetServerCert(ServiceCtx context)
  148. {
  149. if (_connection == null)
  150. {
  151. return ResultCode.NoSocket;
  152. }
  153. ResultCode result = _connection.Handshake(_hostName);
  154. if (result == ResultCode.Success)
  155. {
  156. if (_getServerCertChain)
  157. {
  158. using (WritableRegion region = context.Memory.GetWritableRegion(context.Request.ReceiveBuff[0].Position, (int)context.Request.ReceiveBuff[0].Size))
  159. {
  160. result = _connection.GetServerCertificate(_hostName, region.Memory.Span, out uint bufferSize, out uint certificateCount);
  161. context.ResponseData.Write(bufferSize);
  162. context.ResponseData.Write(certificateCount);
  163. }
  164. }
  165. else
  166. {
  167. context.ResponseData.Write(0);
  168. context.ResponseData.Write(0);
  169. }
  170. }
  171. return result;
  172. }
  173. [CommandHipc(10)]
  174. // Read() -> (u32, buffer<bytes, 6>)
  175. public ResultCode Read(ServiceCtx context)
  176. {
  177. if (_connection == null)
  178. {
  179. return ResultCode.NoSocket;
  180. }
  181. ResultCode result;
  182. using (WritableRegion region = context.Memory.GetWritableRegion(context.Request.ReceiveBuff[0].Position, (int)context.Request.ReceiveBuff[0].Size))
  183. {
  184. // TODO: Better error management.
  185. result = _connection.Read(out int readCount, region.Memory);
  186. if (result == ResultCode.Success)
  187. {
  188. context.ResponseData.Write(readCount);
  189. }
  190. }
  191. return result;
  192. }
  193. [CommandHipc(11)]
  194. // Write(buffer<bytes, 5>) -> s32
  195. public ResultCode Write(ServiceCtx context)
  196. {
  197. if (_connection == null)
  198. {
  199. return ResultCode.NoSocket;
  200. }
  201. // We don't dispose as this isn't supposed to be modified
  202. WritableRegion region = context.Memory.GetWritableRegion(context.Request.SendBuff[0].Position, (int)context.Request.SendBuff[0].Size);
  203. // TODO: Better error management.
  204. ResultCode result = _connection.Write(out int writtenCount, region.Memory);
  205. if (result == ResultCode.Success)
  206. {
  207. context.ResponseData.Write(writtenCount);
  208. }
  209. return result;
  210. }
  211. [CommandHipc(12)]
  212. // Pending() -> s32
  213. public ResultCode Pending(ServiceCtx context)
  214. {
  215. if (_connection == null)
  216. {
  217. return ResultCode.NoSocket;
  218. }
  219. context.ResponseData.Write(_connection.Pending());
  220. return ResultCode.Success;
  221. }
  222. [CommandHipc(13)]
  223. // Peek() -> (s32, buffer<bytes, 6>)
  224. public ResultCode Peek(ServiceCtx context)
  225. {
  226. if (_connection == null)
  227. {
  228. return ResultCode.NoSocket;
  229. }
  230. ResultCode result;
  231. using (WritableRegion region = context.Memory.GetWritableRegion(context.Request.ReceiveBuff[0].Position, (int)context.Request.ReceiveBuff[0].Size))
  232. {
  233. // TODO: Better error management.
  234. result = _connection.Peek(out int peekCount, region.Memory);
  235. if (result == ResultCode.Success)
  236. {
  237. context.ResponseData.Write(peekCount);
  238. }
  239. }
  240. return result;
  241. }
  242. [CommandHipc(14)]
  243. // Poll(nn::ssl::sf::PollEvent poll_event, u32 timeout) -> nn::ssl::sf::PollEvent
  244. public ResultCode Poll(ServiceCtx context)
  245. {
  246. throw new ServiceNotImplementedException(this, context);
  247. }
  248. [CommandHipc(15)]
  249. // GetVerifyCertError()
  250. public ResultCode GetVerifyCertError(ServiceCtx context)
  251. {
  252. throw new ServiceNotImplementedException(this, context);
  253. }
  254. [CommandHipc(16)]
  255. // GetNeededServerCertBufferSize() -> u32
  256. public ResultCode GetNeededServerCertBufferSize(ServiceCtx context)
  257. {
  258. throw new ServiceNotImplementedException(this, context);
  259. }
  260. [CommandHipc(17)]
  261. // SetSessionCacheMode(nn::ssl::sf::SessionCacheMode)
  262. public ResultCode SetSessionCacheMode(ServiceCtx context)
  263. {
  264. SessionCacheMode sessionCacheMode = (SessionCacheMode)context.RequestData.ReadUInt32();
  265. Logger.Stub?.PrintStub(LogClass.ServiceSsl, new { sessionCacheMode });
  266. _sessionCacheMode = sessionCacheMode;
  267. return ResultCode.Success;
  268. }
  269. [CommandHipc(18)]
  270. // GetSessionCacheMode() -> nn::ssl::sf::SessionCacheMode
  271. public ResultCode GetSessionCacheMode(ServiceCtx context)
  272. {
  273. throw new ServiceNotImplementedException(this, context);
  274. }
  275. [CommandHipc(19)]
  276. // FlushSessionCache()
  277. public ResultCode FlushSessionCache(ServiceCtx context)
  278. {
  279. throw new ServiceNotImplementedException(this, context);
  280. }
  281. [CommandHipc(20)]
  282. // SetRenegotiationMode(nn::ssl::sf::RenegotiationMode)
  283. public ResultCode SetRenegotiationMode(ServiceCtx context)
  284. {
  285. throw new ServiceNotImplementedException(this, context);
  286. }
  287. [CommandHipc(21)]
  288. // GetRenegotiationMode() -> nn::ssl::sf::RenegotiationMode
  289. public ResultCode GetRenegotiationMode(ServiceCtx context)
  290. {
  291. throw new ServiceNotImplementedException(this, context);
  292. }
  293. [CommandHipc(22)]
  294. // SetOption(b8 value, nn::ssl::sf::OptionType option)
  295. public ResultCode SetOption(ServiceCtx context)
  296. {
  297. bool value = context.RequestData.ReadUInt32() != 0;
  298. OptionType option = (OptionType)context.RequestData.ReadUInt32();
  299. Logger.Stub?.PrintStub(LogClass.ServiceSsl, new { option, value });
  300. return SetOption(option, value);
  301. }
  302. [CommandHipc(23)]
  303. // GetOption(nn::ssl::sf::OptionType) -> b8
  304. public ResultCode GetOption(ServiceCtx context)
  305. {
  306. OptionType option = (OptionType)context.RequestData.ReadUInt32();
  307. Logger.Stub?.PrintStub(LogClass.ServiceSsl, new { option });
  308. ResultCode result = GetOption(option, out bool value);
  309. if (result == ResultCode.Success)
  310. {
  311. context.ResponseData.Write(value);
  312. }
  313. return result;
  314. }
  315. [CommandHipc(24)]
  316. // GetVerifyCertErrors() -> (u32, u32, buffer<bytes, 6>)
  317. public ResultCode GetVerifyCertErrors(ServiceCtx context)
  318. {
  319. throw new ServiceNotImplementedException(this, context);
  320. }
  321. [CommandHipc(25)] // 4.0.0+
  322. // GetCipherInfo(u32) -> buffer<bytes, 6>
  323. public ResultCode GetCipherInfo(ServiceCtx context)
  324. {
  325. throw new ServiceNotImplementedException(this, context);
  326. }
  327. [CommandHipc(26)]
  328. // SetNextAlpnProto(buffer<bytes, 5>) -> u32
  329. public ResultCode SetNextAlpnProto(ServiceCtx context)
  330. {
  331. ulong inputDataPosition = context.Request.SendBuff[0].Position;
  332. ulong inputDataSize = context.Request.SendBuff[0].Size;
  333. _nextAplnProto = new byte[inputDataSize];
  334. context.Memory.Read(inputDataPosition, _nextAplnProto);
  335. Logger.Stub?.PrintStub(LogClass.ServiceSsl, new { inputDataSize });
  336. return ResultCode.Success;
  337. }
  338. [CommandHipc(27)]
  339. // GetNextAlpnProto(buffer<bytes, 6>) -> u32
  340. public ResultCode GetNextAlpnProto(ServiceCtx context)
  341. {
  342. ulong outputDataPosition = context.Request.ReceiveBuff[0].Position;
  343. ulong outputDataSize = context.Request.ReceiveBuff[0].Size;
  344. context.Memory.Write(outputDataPosition, _nextAplnProto);
  345. context.ResponseData.Write(_nextAplnProto.Length);
  346. Logger.Stub?.PrintStub(LogClass.ServiceSsl, new { outputDataSize });
  347. return ResultCode.Success;
  348. }
  349. private ResultCode SetOption(OptionType option, bool value)
  350. {
  351. switch (option)
  352. {
  353. case OptionType.DoNotCloseSocket:
  354. _doNotClockSocket = value;
  355. break;
  356. case OptionType.GetServerCertChain:
  357. _getServerCertChain = value;
  358. break;
  359. case OptionType.SkipDefaultVerify:
  360. _skipDefaultVerify = value;
  361. break;
  362. case OptionType.EnableAlpn:
  363. _enableAlpn = value;
  364. break;
  365. default:
  366. Logger.Warning?.Print(LogClass.ServiceSsl, $"Unsupported option {option}");
  367. return ResultCode.InvalidOption;
  368. }
  369. return ResultCode.Success;
  370. }
  371. private ResultCode GetOption(OptionType option, out bool value)
  372. {
  373. switch (option)
  374. {
  375. case OptionType.DoNotCloseSocket:
  376. value = _doNotClockSocket;
  377. break;
  378. case OptionType.GetServerCertChain:
  379. value = _getServerCertChain;
  380. break;
  381. case OptionType.SkipDefaultVerify:
  382. value = _skipDefaultVerify;
  383. break;
  384. case OptionType.EnableAlpn:
  385. value = _enableAlpn;
  386. break;
  387. default:
  388. Logger.Warning?.Print(LogClass.ServiceSsl, $"Unsupported option {option}");
  389. value = false;
  390. return ResultCode.InvalidOption;
  391. }
  392. return ResultCode.Success;
  393. }
  394. public void Dispose()
  395. {
  396. _connection?.Dispose();
  397. }
  398. }
  399. }