ssl.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "core/hle/ipc_helpers.h"
  4. #include "core/hle/service/service.h"
  5. #include "core/hle/service/sm/sm.h"
  6. #include "core/hle/service/ssl/ssl.h"
  7. namespace Service::SSL {
  8. enum class CertificateFormat : u32 {
  9. Pem = 1,
  10. Der = 2,
  11. };
  12. class ISslConnection final : public ServiceFramework<ISslConnection> {
  13. public:
  14. explicit ISslConnection(Core::System& system_) : ServiceFramework{system_, "ISslConnection"} {
  15. // clang-format off
  16. static const FunctionInfo functions[] = {
  17. {0, nullptr, "SetSocketDescriptor"},
  18. {1, nullptr, "SetHostName"},
  19. {2, nullptr, "SetVerifyOption"},
  20. {3, nullptr, "SetIoMode"},
  21. {4, nullptr, "GetSocketDescriptor"},
  22. {5, nullptr, "GetHostName"},
  23. {6, nullptr, "GetVerifyOption"},
  24. {7, nullptr, "GetIoMode"},
  25. {8, nullptr, "DoHandshake"},
  26. {9, nullptr, "DoHandshakeGetServerCert"},
  27. {10, nullptr, "Read"},
  28. {11, nullptr, "Write"},
  29. {12, nullptr, "Pending"},
  30. {13, nullptr, "Peek"},
  31. {14, nullptr, "Poll"},
  32. {15, nullptr, "GetVerifyCertError"},
  33. {16, nullptr, "GetNeededServerCertBufferSize"},
  34. {17, nullptr, "SetSessionCacheMode"},
  35. {18, nullptr, "GetSessionCacheMode"},
  36. {19, nullptr, "FlushSessionCache"},
  37. {20, nullptr, "SetRenegotiationMode"},
  38. {21, nullptr, "GetRenegotiationMode"},
  39. {22, nullptr, "SetOption"},
  40. {23, nullptr, "GetOption"},
  41. {24, nullptr, "GetVerifyCertErrors"},
  42. {25, nullptr, "GetCipherInfo"},
  43. {26, nullptr, "SetNextAlpnProto"},
  44. {27, nullptr, "GetNextAlpnProto"},
  45. {28, nullptr, "SetDtlsSocketDescriptor"},
  46. {29, nullptr, "GetDtlsHandshakeTimeout"},
  47. {30, nullptr, "SetPrivateOption"},
  48. {31, nullptr, "SetSrtpCiphers"},
  49. {32, nullptr, "GetSrtpCipher"},
  50. {33, nullptr, "ExportKeyingMaterial"},
  51. {34, nullptr, "SetIoTimeout"},
  52. {35, nullptr, "GetIoTimeout"},
  53. };
  54. // clang-format on
  55. RegisterHandlers(functions);
  56. }
  57. };
  58. class ISslContext final : public ServiceFramework<ISslContext> {
  59. public:
  60. explicit ISslContext(Core::System& system_) : ServiceFramework{system_, "ISslContext"} {
  61. static const FunctionInfo functions[] = {
  62. {0, &ISslContext::SetOption, "SetOption"},
  63. {1, nullptr, "GetOption"},
  64. {2, &ISslContext::CreateConnection, "CreateConnection"},
  65. {3, nullptr, "GetConnectionCount"},
  66. {4, &ISslContext::ImportServerPki, "ImportServerPki"},
  67. {5, &ISslContext::ImportClientPki, "ImportClientPki"},
  68. {6, nullptr, "RemoveServerPki"},
  69. {7, nullptr, "RemoveClientPki"},
  70. {8, nullptr, "RegisterInternalPki"},
  71. {9, nullptr, "AddPolicyOid"},
  72. {10, nullptr, "ImportCrl"},
  73. {11, nullptr, "RemoveCrl"},
  74. {12, nullptr, "ImportClientCertKeyPki"},
  75. {13, nullptr, "GeneratePrivateKeyAndCert"},
  76. };
  77. RegisterHandlers(functions);
  78. }
  79. private:
  80. void SetOption(Kernel::HLERequestContext& ctx) {
  81. struct Parameters {
  82. u8 enable;
  83. u32 option;
  84. };
  85. IPC::RequestParser rp{ctx};
  86. const auto parameters = rp.PopRaw<Parameters>();
  87. LOG_WARNING(Service_SSL, "(STUBBED) called. enable={}, option={}", parameters.enable,
  88. parameters.option);
  89. IPC::ResponseBuilder rb{ctx, 2};
  90. rb.Push(ResultSuccess);
  91. }
  92. void CreateConnection(Kernel::HLERequestContext& ctx) {
  93. LOG_WARNING(Service_SSL, "(STUBBED) called");
  94. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  95. rb.Push(ResultSuccess);
  96. rb.PushIpcInterface<ISslConnection>(system);
  97. }
  98. void ImportServerPki(Kernel::HLERequestContext& ctx) {
  99. IPC::RequestParser rp{ctx};
  100. const auto certificate_format = rp.PopEnum<CertificateFormat>();
  101. [[maybe_unused]] const auto pkcs_12_certificates = ctx.ReadBuffer(0);
  102. constexpr u64 server_id = 0;
  103. LOG_WARNING(Service_SSL, "(STUBBED) called, certificate_format={}", certificate_format);
  104. IPC::ResponseBuilder rb{ctx, 4};
  105. rb.Push(ResultSuccess);
  106. rb.Push(server_id);
  107. }
  108. void ImportClientPki(Kernel::HLERequestContext& ctx) {
  109. [[maybe_unused]] const auto pkcs_12_certificate = ctx.ReadBuffer(0);
  110. [[maybe_unused]] const auto ascii_password = [&ctx] {
  111. if (ctx.CanReadBuffer(1)) {
  112. return ctx.ReadBuffer(1);
  113. }
  114. return std::span<const u8>{};
  115. }();
  116. constexpr u64 client_id = 0;
  117. LOG_WARNING(Service_SSL, "(STUBBED) called");
  118. IPC::ResponseBuilder rb{ctx, 4};
  119. rb.Push(ResultSuccess);
  120. rb.Push(client_id);
  121. }
  122. };
  123. class SSL final : public ServiceFramework<SSL> {
  124. public:
  125. explicit SSL(Core::System& system_) : ServiceFramework{system_, "ssl"} {
  126. // clang-format off
  127. static const FunctionInfo functions[] = {
  128. {0, &SSL::CreateContext, "CreateContext"},
  129. {1, nullptr, "GetContextCount"},
  130. {2, nullptr, "GetCertificates"},
  131. {3, nullptr, "GetCertificateBufSize"},
  132. {4, nullptr, "DebugIoctl"},
  133. {5, &SSL::SetInterfaceVersion, "SetInterfaceVersion"},
  134. {6, nullptr, "FlushSessionCache"},
  135. {7, nullptr, "SetDebugOption"},
  136. {8, nullptr, "GetDebugOption"},
  137. };
  138. // clang-format on
  139. RegisterHandlers(functions);
  140. }
  141. private:
  142. u32 ssl_version{};
  143. void CreateContext(Kernel::HLERequestContext& ctx) {
  144. LOG_WARNING(Service_SSL, "(STUBBED) called");
  145. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  146. rb.Push(ResultSuccess);
  147. rb.PushIpcInterface<ISslContext>(system);
  148. }
  149. void SetInterfaceVersion(Kernel::HLERequestContext& ctx) {
  150. LOG_DEBUG(Service_SSL, "called");
  151. IPC::RequestParser rp{ctx};
  152. ssl_version = rp.Pop<u32>();
  153. IPC::ResponseBuilder rb{ctx, 2};
  154. rb.Push(ResultSuccess);
  155. }
  156. };
  157. void InstallInterfaces(SM::ServiceManager& service_manager, Core::System& system) {
  158. std::make_shared<SSL>(system)->InstallAsService(service_manager);
  159. }
  160. } // namespace Service::SSL