ISslService.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using Ryujinx.Common.Logging;
  2. using Ryujinx.HLE.HOS.Services.Ssl.SslService;
  3. using Ryujinx.HLE.HOS.Services.Ssl.Types;
  4. using Ryujinx.Memory;
  5. using System;
  6. using System.Runtime.CompilerServices;
  7. using System.Runtime.InteropServices;
  8. namespace Ryujinx.HLE.HOS.Services.Ssl
  9. {
  10. [Service("ssl")]
  11. class ISslService : IpcService
  12. {
  13. // NOTE: The SSL service is used by games to connect it to various official online services, which we do not intend to support.
  14. // In this case it is acceptable to stub all calls of the service.
  15. public ISslService(ServiceCtx context) { }
  16. [CommandHipc(0)]
  17. // CreateContext(nn::ssl::sf::SslVersion, u64, pid) -> object<nn::ssl::sf::ISslContext>
  18. public ResultCode CreateContext(ServiceCtx context)
  19. {
  20. SslVersion sslVersion = (SslVersion)context.RequestData.ReadUInt32();
  21. ulong pidPlaceholder = context.RequestData.ReadUInt64();
  22. MakeObject(context, new ISslContext(context.Request.HandleDesc.PId, sslVersion));
  23. Logger.Stub?.PrintStub(LogClass.ServiceSsl, new { sslVersion });
  24. return ResultCode.Success;
  25. }
  26. private uint ComputeCertificateBufferSizeRequired(ReadOnlySpan<BuiltInCertificateManager.CertStoreEntry> entries)
  27. {
  28. uint totalSize = 0;
  29. for (int i = 0; i < entries.Length; i++)
  30. {
  31. totalSize += (uint)Unsafe.SizeOf<BuiltInCertificateInfo>();
  32. totalSize += (uint)entries[i].Data.Length;
  33. }
  34. return totalSize;
  35. }
  36. [CommandHipc(2)]
  37. // GetCertificates(buffer<CaCertificateId, 5> ids) -> (u32 certificates_count, buffer<bytes, 6> certificates)
  38. public ResultCode GetCertificates(ServiceCtx context)
  39. {
  40. ReadOnlySpan<CaCertificateId> ids = MemoryMarshal.Cast<byte, CaCertificateId>(context.Memory.GetSpan(context.Request.SendBuff[0].Position, (int)context.Request.SendBuff[0].Size));
  41. if (!BuiltInCertificateManager.Instance.TryGetCertificates(ids, out BuiltInCertificateManager.CertStoreEntry[] entries))
  42. {
  43. throw new InvalidOperationException();
  44. }
  45. if (ComputeCertificateBufferSizeRequired(entries) > context.Request.ReceiveBuff[0].Size)
  46. {
  47. return ResultCode.InvalidCertBufSize;
  48. }
  49. using (WritableRegion region = context.Memory.GetWritableRegion(context.Request.ReceiveBuff[0].Position, (int)context.Request.ReceiveBuff[0].Size))
  50. {
  51. Span<byte> rawData = region.Memory.Span;
  52. Span<BuiltInCertificateInfo> infos = MemoryMarshal.Cast<byte, BuiltInCertificateInfo>(rawData)[..entries.Length];
  53. Span<byte> certificatesData = rawData[(Unsafe.SizeOf<BuiltInCertificateInfo>() * entries.Length)..];
  54. for (int i = 0; i < infos.Length; i++)
  55. {
  56. entries[i].Data.CopyTo(certificatesData);
  57. infos[i] = new BuiltInCertificateInfo
  58. {
  59. Id = entries[i].Id,
  60. Status = entries[i].Status,
  61. CertificateDataSize = (ulong)entries[i].Data.Length,
  62. CertificateDataOffset = (ulong)(rawData.Length - certificatesData.Length)
  63. };
  64. certificatesData = certificatesData[entries[i].Data.Length..];
  65. }
  66. }
  67. context.ResponseData.Write(entries.Length);
  68. return ResultCode.Success;
  69. }
  70. [CommandHipc(3)]
  71. // GetCertificateBufSize(buffer<CaCertificateId, 5> ids) -> u32 buffer_size;
  72. public ResultCode GetCertificateBufSize(ServiceCtx context)
  73. {
  74. ReadOnlySpan<CaCertificateId> ids = MemoryMarshal.Cast<byte, CaCertificateId>(context.Memory.GetSpan(context.Request.SendBuff[0].Position, (int)context.Request.SendBuff[0].Size));
  75. if (!BuiltInCertificateManager.Instance.TryGetCertificates(ids, out BuiltInCertificateManager.CertStoreEntry[] entries))
  76. {
  77. throw new InvalidOperationException();
  78. }
  79. context.ResponseData.Write(ComputeCertificateBufferSizeRequired(entries));
  80. return ResultCode.Success;
  81. }
  82. [CommandHipc(5)]
  83. // SetInterfaceVersion(u32)
  84. public ResultCode SetInterfaceVersion(ServiceCtx context)
  85. {
  86. // 1 = 3.0.0+, 2 = 5.0.0+, 3 = 6.0.0+
  87. uint interfaceVersion = context.RequestData.ReadUInt32();
  88. Logger.Stub?.PrintStub(LogClass.ServiceSsl, new { interfaceVersion });
  89. return ResultCode.Success;
  90. }
  91. }
  92. }