ISslConnection.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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 bufferAddress = context.Request.ReceiveBuff[0].Position;
  111. ulong bufferLen = context.Request.ReceiveBuff[0].Size;
  112. using (var region = context.Memory.GetWritableRegion(bufferAddress, (int)bufferLen, true))
  113. {
  114. Encoding.ASCII.GetBytes(_hostName, region.Memory.Span);
  115. }
  116. context.ResponseData.Write((uint)_hostName.Length);
  117. Logger.Info?.Print(LogClass.ServiceSsl, _hostName);
  118. return ResultCode.Success;
  119. }
  120. [CommandHipc(6)]
  121. // GetVerifyOption() -> nn::ssl::sf::VerifyOption
  122. public ResultCode GetVerifyOption(ServiceCtx context)
  123. {
  124. context.ResponseData.Write((uint)_verifyOption);
  125. Logger.Stub?.PrintStub(LogClass.ServiceSsl, new { _verifyOption });
  126. return ResultCode.Success;
  127. }
  128. [CommandHipc(7)]
  129. // GetIoMode() -> nn::ssl::sf::IoMode
  130. public ResultCode GetIoMode(ServiceCtx context)
  131. {
  132. context.ResponseData.Write((uint)_ioMode);
  133. Logger.Stub?.PrintStub(LogClass.ServiceSsl, new { _ioMode });
  134. return ResultCode.Success;
  135. }
  136. [CommandHipc(8)]
  137. // DoHandshake()
  138. public ResultCode DoHandshake(ServiceCtx context)
  139. {
  140. if (_connection == null)
  141. {
  142. return ResultCode.NoSocket;
  143. }
  144. return _connection.Handshake(_hostName);
  145. }
  146. [CommandHipc(9)]
  147. // DoHandshakeGetServerCert() -> (u32, u32, buffer<bytes, 6>)
  148. public ResultCode DoHandshakeGetServerCert(ServiceCtx context)
  149. {
  150. if (_connection == null)
  151. {
  152. return ResultCode.NoSocket;
  153. }
  154. ResultCode result = _connection.Handshake(_hostName);
  155. if (result == ResultCode.Success)
  156. {
  157. if (_getServerCertChain)
  158. {
  159. using (WritableRegion region = context.Memory.GetWritableRegion(context.Request.ReceiveBuff[0].Position, (int)context.Request.ReceiveBuff[0].Size))
  160. {
  161. result = _connection.GetServerCertificate(_hostName, region.Memory.Span, out uint bufferSize, out uint certificateCount);
  162. context.ResponseData.Write(bufferSize);
  163. context.ResponseData.Write(certificateCount);
  164. }
  165. }
  166. else
  167. {
  168. context.ResponseData.Write(0);
  169. context.ResponseData.Write(0);
  170. }
  171. }
  172. return result;
  173. }
  174. [CommandHipc(10)]
  175. // Read() -> (u32, buffer<bytes, 6>)
  176. public ResultCode Read(ServiceCtx context)
  177. {
  178. if (_connection == null)
  179. {
  180. return ResultCode.NoSocket;
  181. }
  182. ResultCode result;
  183. using (WritableRegion region = context.Memory.GetWritableRegion(context.Request.ReceiveBuff[0].Position, (int)context.Request.ReceiveBuff[0].Size))
  184. {
  185. // TODO: Better error management.
  186. result = _connection.Read(out int readCount, region.Memory);
  187. if (result == ResultCode.Success)
  188. {
  189. context.ResponseData.Write(readCount);
  190. }
  191. }
  192. return result;
  193. }
  194. [CommandHipc(11)]
  195. // Write(buffer<bytes, 5>) -> s32
  196. public ResultCode Write(ServiceCtx context)
  197. {
  198. if (_connection == null)
  199. {
  200. return ResultCode.NoSocket;
  201. }
  202. // We don't dispose as this isn't supposed to be modified
  203. WritableRegion region = context.Memory.GetWritableRegion(context.Request.SendBuff[0].Position, (int)context.Request.SendBuff[0].Size);
  204. // TODO: Better error management.
  205. ResultCode result = _connection.Write(out int writtenCount, region.Memory);
  206. if (result == ResultCode.Success)
  207. {
  208. context.ResponseData.Write(writtenCount);
  209. }
  210. return result;
  211. }
  212. [CommandHipc(12)]
  213. // Pending() -> s32
  214. public ResultCode Pending(ServiceCtx context)
  215. {
  216. if (_connection == null)
  217. {
  218. return ResultCode.NoSocket;
  219. }
  220. context.ResponseData.Write(_connection.Pending());
  221. return ResultCode.Success;
  222. }
  223. [CommandHipc(13)]
  224. // Peek() -> (s32, buffer<bytes, 6>)
  225. public ResultCode Peek(ServiceCtx context)
  226. {
  227. if (_connection == null)
  228. {
  229. return ResultCode.NoSocket;
  230. }
  231. ResultCode result;
  232. using (WritableRegion region = context.Memory.GetWritableRegion(context.Request.ReceiveBuff[0].Position, (int)context.Request.ReceiveBuff[0].Size))
  233. {
  234. // TODO: Better error management.
  235. result = _connection.Peek(out int peekCount, region.Memory);
  236. if (result == ResultCode.Success)
  237. {
  238. context.ResponseData.Write(peekCount);
  239. }
  240. }
  241. return result;
  242. }
  243. [CommandHipc(14)]
  244. // Poll(nn::ssl::sf::PollEvent poll_event, u32 timeout) -> nn::ssl::sf::PollEvent
  245. public ResultCode Poll(ServiceCtx context)
  246. {
  247. throw new ServiceNotImplementedException(this, context);
  248. }
  249. [CommandHipc(15)]
  250. // GetVerifyCertError()
  251. public ResultCode GetVerifyCertError(ServiceCtx context)
  252. {
  253. throw new ServiceNotImplementedException(this, context);
  254. }
  255. [CommandHipc(16)]
  256. // GetNeededServerCertBufferSize() -> u32
  257. public ResultCode GetNeededServerCertBufferSize(ServiceCtx context)
  258. {
  259. throw new ServiceNotImplementedException(this, context);
  260. }
  261. [CommandHipc(17)]
  262. // SetSessionCacheMode(nn::ssl::sf::SessionCacheMode)
  263. public ResultCode SetSessionCacheMode(ServiceCtx context)
  264. {
  265. SessionCacheMode sessionCacheMode = (SessionCacheMode)context.RequestData.ReadUInt32();
  266. Logger.Stub?.PrintStub(LogClass.ServiceSsl, new { sessionCacheMode });
  267. _sessionCacheMode = sessionCacheMode;
  268. return ResultCode.Success;
  269. }
  270. [CommandHipc(18)]
  271. // GetSessionCacheMode() -> nn::ssl::sf::SessionCacheMode
  272. public ResultCode GetSessionCacheMode(ServiceCtx context)
  273. {
  274. throw new ServiceNotImplementedException(this, context);
  275. }
  276. [CommandHipc(19)]
  277. // FlushSessionCache()
  278. public ResultCode FlushSessionCache(ServiceCtx context)
  279. {
  280. throw new ServiceNotImplementedException(this, context);
  281. }
  282. [CommandHipc(20)]
  283. // SetRenegotiationMode(nn::ssl::sf::RenegotiationMode)
  284. public ResultCode SetRenegotiationMode(ServiceCtx context)
  285. {
  286. throw new ServiceNotImplementedException(this, context);
  287. }
  288. [CommandHipc(21)]
  289. // GetRenegotiationMode() -> nn::ssl::sf::RenegotiationMode
  290. public ResultCode GetRenegotiationMode(ServiceCtx context)
  291. {
  292. throw new ServiceNotImplementedException(this, context);
  293. }
  294. [CommandHipc(22)]
  295. // SetOption(b8 value, nn::ssl::sf::OptionType option)
  296. public ResultCode SetOption(ServiceCtx context)
  297. {
  298. bool value = context.RequestData.ReadUInt32() != 0;
  299. OptionType option = (OptionType)context.RequestData.ReadUInt32();
  300. Logger.Stub?.PrintStub(LogClass.ServiceSsl, new { option, value });
  301. return SetOption(option, value);
  302. }
  303. [CommandHipc(23)]
  304. // GetOption(nn::ssl::sf::OptionType) -> b8
  305. public ResultCode GetOption(ServiceCtx context)
  306. {
  307. OptionType option = (OptionType)context.RequestData.ReadUInt32();
  308. Logger.Stub?.PrintStub(LogClass.ServiceSsl, new { option });
  309. ResultCode result = GetOption(option, out bool value);
  310. if (result == ResultCode.Success)
  311. {
  312. context.ResponseData.Write(value);
  313. }
  314. return result;
  315. }
  316. [CommandHipc(24)]
  317. // GetVerifyCertErrors() -> (u32, u32, buffer<bytes, 6>)
  318. public ResultCode GetVerifyCertErrors(ServiceCtx context)
  319. {
  320. throw new ServiceNotImplementedException(this, context);
  321. }
  322. [CommandHipc(25)] // 4.0.0+
  323. // GetCipherInfo(u32) -> buffer<bytes, 6>
  324. public ResultCode GetCipherInfo(ServiceCtx context)
  325. {
  326. throw new ServiceNotImplementedException(this, context);
  327. }
  328. [CommandHipc(26)]
  329. // SetNextAlpnProto(buffer<bytes, 5>) -> u32
  330. public ResultCode SetNextAlpnProto(ServiceCtx context)
  331. {
  332. ulong inputDataPosition = context.Request.SendBuff[0].Position;
  333. ulong inputDataSize = context.Request.SendBuff[0].Size;
  334. _nextAplnProto = new byte[inputDataSize];
  335. context.Memory.Read(inputDataPosition, _nextAplnProto);
  336. Logger.Stub?.PrintStub(LogClass.ServiceSsl, new { inputDataSize });
  337. return ResultCode.Success;
  338. }
  339. [CommandHipc(27)]
  340. // GetNextAlpnProto(buffer<bytes, 6>) -> u32
  341. public ResultCode GetNextAlpnProto(ServiceCtx context)
  342. {
  343. ulong outputDataPosition = context.Request.ReceiveBuff[0].Position;
  344. ulong outputDataSize = context.Request.ReceiveBuff[0].Size;
  345. context.Memory.Write(outputDataPosition, _nextAplnProto);
  346. context.ResponseData.Write(_nextAplnProto.Length);
  347. Logger.Stub?.PrintStub(LogClass.ServiceSsl, new { outputDataSize });
  348. return ResultCode.Success;
  349. }
  350. private ResultCode SetOption(OptionType option, bool value)
  351. {
  352. switch (option)
  353. {
  354. case OptionType.DoNotCloseSocket:
  355. _doNotClockSocket = value;
  356. break;
  357. case OptionType.GetServerCertChain:
  358. _getServerCertChain = value;
  359. break;
  360. case OptionType.SkipDefaultVerify:
  361. _skipDefaultVerify = value;
  362. break;
  363. case OptionType.EnableAlpn:
  364. _enableAlpn = value;
  365. break;
  366. default:
  367. Logger.Warning?.Print(LogClass.ServiceSsl, $"Unsupported option {option}");
  368. return ResultCode.InvalidOption;
  369. }
  370. return ResultCode.Success;
  371. }
  372. private ResultCode GetOption(OptionType option, out bool value)
  373. {
  374. switch (option)
  375. {
  376. case OptionType.DoNotCloseSocket:
  377. value = _doNotClockSocket;
  378. break;
  379. case OptionType.GetServerCertChain:
  380. value = _getServerCertChain;
  381. break;
  382. case OptionType.SkipDefaultVerify:
  383. value = _skipDefaultVerify;
  384. break;
  385. case OptionType.EnableAlpn:
  386. value = _enableAlpn;
  387. break;
  388. default:
  389. Logger.Warning?.Print(LogClass.ServiceSsl, $"Unsupported option {option}");
  390. value = false;
  391. return ResultCode.InvalidOption;
  392. }
  393. return ResultCode.Success;
  394. }
  395. public void Dispose()
  396. {
  397. _connection?.Dispose();
  398. }
  399. }
  400. }