ISslConnection.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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. context.ResponseData.Write((uint)_sessionCacheMode);
  275. Logger.Stub?.PrintStub(LogClass.ServiceSsl, new { _sessionCacheMode });
  276. return ResultCode.Success;
  277. }
  278. [CommandHipc(19)]
  279. // FlushSessionCache()
  280. public ResultCode FlushSessionCache(ServiceCtx context)
  281. {
  282. throw new ServiceNotImplementedException(this, context);
  283. }
  284. [CommandHipc(20)]
  285. // SetRenegotiationMode(nn::ssl::sf::RenegotiationMode)
  286. public ResultCode SetRenegotiationMode(ServiceCtx context)
  287. {
  288. throw new ServiceNotImplementedException(this, context);
  289. }
  290. [CommandHipc(21)]
  291. // GetRenegotiationMode() -> nn::ssl::sf::RenegotiationMode
  292. public ResultCode GetRenegotiationMode(ServiceCtx context)
  293. {
  294. throw new ServiceNotImplementedException(this, context);
  295. }
  296. [CommandHipc(22)]
  297. // SetOption(b8 value, nn::ssl::sf::OptionType option)
  298. public ResultCode SetOption(ServiceCtx context)
  299. {
  300. bool value = context.RequestData.ReadUInt32() != 0;
  301. OptionType option = (OptionType)context.RequestData.ReadUInt32();
  302. Logger.Stub?.PrintStub(LogClass.ServiceSsl, new { option, value });
  303. return SetOption(option, value);
  304. }
  305. [CommandHipc(23)]
  306. // GetOption(nn::ssl::sf::OptionType) -> b8
  307. public ResultCode GetOption(ServiceCtx context)
  308. {
  309. OptionType option = (OptionType)context.RequestData.ReadUInt32();
  310. Logger.Stub?.PrintStub(LogClass.ServiceSsl, new { option });
  311. ResultCode result = GetOption(option, out bool value);
  312. if (result == ResultCode.Success)
  313. {
  314. context.ResponseData.Write(value);
  315. }
  316. return result;
  317. }
  318. [CommandHipc(24)]
  319. // GetVerifyCertErrors() -> (u32, u32, buffer<bytes, 6>)
  320. public ResultCode GetVerifyCertErrors(ServiceCtx context)
  321. {
  322. throw new ServiceNotImplementedException(this, context);
  323. }
  324. [CommandHipc(25)] // 4.0.0+
  325. // GetCipherInfo(u32) -> buffer<bytes, 6>
  326. public ResultCode GetCipherInfo(ServiceCtx context)
  327. {
  328. throw new ServiceNotImplementedException(this, context);
  329. }
  330. [CommandHipc(26)]
  331. // SetNextAlpnProto(buffer<bytes, 5>) -> u32
  332. public ResultCode SetNextAlpnProto(ServiceCtx context)
  333. {
  334. ulong inputDataPosition = context.Request.SendBuff[0].Position;
  335. ulong inputDataSize = context.Request.SendBuff[0].Size;
  336. _nextAplnProto = new byte[inputDataSize];
  337. context.Memory.Read(inputDataPosition, _nextAplnProto);
  338. Logger.Stub?.PrintStub(LogClass.ServiceSsl, new { inputDataSize });
  339. return ResultCode.Success;
  340. }
  341. [CommandHipc(27)]
  342. // GetNextAlpnProto(buffer<bytes, 6>) -> u32
  343. public ResultCode GetNextAlpnProto(ServiceCtx context)
  344. {
  345. ulong outputDataPosition = context.Request.ReceiveBuff[0].Position;
  346. ulong outputDataSize = context.Request.ReceiveBuff[0].Size;
  347. context.Memory.Write(outputDataPosition, _nextAplnProto);
  348. context.ResponseData.Write(_nextAplnProto.Length);
  349. Logger.Stub?.PrintStub(LogClass.ServiceSsl, new { outputDataSize });
  350. return ResultCode.Success;
  351. }
  352. private ResultCode SetOption(OptionType option, bool value)
  353. {
  354. switch (option)
  355. {
  356. case OptionType.DoNotCloseSocket:
  357. _doNotClockSocket = value;
  358. break;
  359. case OptionType.GetServerCertChain:
  360. _getServerCertChain = value;
  361. break;
  362. case OptionType.SkipDefaultVerify:
  363. _skipDefaultVerify = value;
  364. break;
  365. case OptionType.EnableAlpn:
  366. _enableAlpn = value;
  367. break;
  368. default:
  369. Logger.Warning?.Print(LogClass.ServiceSsl, $"Unsupported option {option}");
  370. return ResultCode.InvalidOption;
  371. }
  372. return ResultCode.Success;
  373. }
  374. private ResultCode GetOption(OptionType option, out bool value)
  375. {
  376. switch (option)
  377. {
  378. case OptionType.DoNotCloseSocket:
  379. value = _doNotClockSocket;
  380. break;
  381. case OptionType.GetServerCertChain:
  382. value = _getServerCertChain;
  383. break;
  384. case OptionType.SkipDefaultVerify:
  385. value = _skipDefaultVerify;
  386. break;
  387. case OptionType.EnableAlpn:
  388. value = _enableAlpn;
  389. break;
  390. default:
  391. Logger.Warning?.Print(LogClass.ServiceSsl, $"Unsupported option {option}");
  392. value = false;
  393. return ResultCode.InvalidOption;
  394. }
  395. return ResultCode.Success;
  396. }
  397. public void Dispose()
  398. {
  399. _connection?.Dispose();
  400. }
  401. }
  402. }