ssl.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. };
  46. // clang-format on
  47. RegisterHandlers(functions);
  48. }
  49. };
  50. class ISslContext final : public ServiceFramework<ISslContext> {
  51. public:
  52. explicit ISslContext(Core::System& system_) : ServiceFramework{system_, "ISslContext"} {
  53. static const FunctionInfo functions[] = {
  54. {0, &ISslContext::SetOption, "SetOption"},
  55. {1, nullptr, "GetOption"},
  56. {2, &ISslContext::CreateConnection, "CreateConnection"},
  57. {3, nullptr, "GetConnectionCount"},
  58. {4, &ISslContext::ImportServerPki, "ImportServerPki"},
  59. {5, &ISslContext::ImportClientPki, "ImportClientPki"},
  60. {6, nullptr, "RemoveServerPki"},
  61. {7, nullptr, "RemoveClientPki"},
  62. {8, nullptr, "RegisterInternalPki"},
  63. {9, nullptr, "AddPolicyOid"},
  64. {10, nullptr, "ImportCrl"},
  65. {11, nullptr, "RemoveCrl"},
  66. };
  67. RegisterHandlers(functions);
  68. }
  69. private:
  70. void SetOption(Kernel::HLERequestContext& ctx) {
  71. struct Parameters {
  72. u8 enable;
  73. u32 option;
  74. };
  75. IPC::RequestParser rp{ctx};
  76. const auto parameters = rp.PopRaw<Parameters>();
  77. LOG_WARNING(Service_SSL, "(STUBBED) called. enable={}, option={}", parameters.enable,
  78. parameters.option);
  79. IPC::ResponseBuilder rb{ctx, 2};
  80. rb.Push(ResultSuccess);
  81. }
  82. void CreateConnection(Kernel::HLERequestContext& ctx) {
  83. LOG_WARNING(Service_SSL, "(STUBBED) called");
  84. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  85. rb.Push(ResultSuccess);
  86. rb.PushIpcInterface<ISslConnection>(system);
  87. }
  88. void ImportServerPki(Kernel::HLERequestContext& ctx) {
  89. IPC::RequestParser rp{ctx};
  90. const auto certificate_format = rp.PopEnum<CertificateFormat>();
  91. const auto pkcs_12_certificates = ctx.ReadBuffer(0);
  92. constexpr u64 server_id = 0;
  93. LOG_WARNING(Service_SSL, "(STUBBED) called, certificate_format={}", certificate_format);
  94. IPC::ResponseBuilder rb{ctx, 4};
  95. rb.Push(ResultSuccess);
  96. rb.Push(server_id);
  97. }
  98. void ImportClientPki(Kernel::HLERequestContext& ctx) {
  99. const auto pkcs_12_certificate = ctx.ReadBuffer(0);
  100. const auto ascii_password = [&ctx] {
  101. if (ctx.CanReadBuffer(1)) {
  102. return ctx.ReadBuffer(1);
  103. }
  104. return std::vector<u8>{};
  105. }();
  106. constexpr u64 client_id = 0;
  107. LOG_WARNING(Service_SSL, "(STUBBED) called");
  108. IPC::ResponseBuilder rb{ctx, 4};
  109. rb.Push(ResultSuccess);
  110. rb.Push(client_id);
  111. }
  112. };
  113. class SSL final : public ServiceFramework<SSL> {
  114. public:
  115. explicit SSL(Core::System& system_) : ServiceFramework{system_, "ssl"} {
  116. // clang-format off
  117. static const FunctionInfo functions[] = {
  118. {0, &SSL::CreateContext, "CreateContext"},
  119. {1, nullptr, "GetContextCount"},
  120. {2, nullptr, "GetCertificates"},
  121. {3, nullptr, "GetCertificateBufSize"},
  122. {4, nullptr, "DebugIoctl"},
  123. {5, &SSL::SetInterfaceVersion, "SetInterfaceVersion"},
  124. {6, nullptr, "FlushSessionCache"},
  125. {7, nullptr, "SetDebugOption"},
  126. {8, nullptr, "GetDebugOption"},
  127. };
  128. // clang-format on
  129. RegisterHandlers(functions);
  130. }
  131. private:
  132. u32 ssl_version{};
  133. void CreateContext(Kernel::HLERequestContext& ctx) {
  134. LOG_WARNING(Service_SSL, "(STUBBED) called");
  135. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  136. rb.Push(ResultSuccess);
  137. rb.PushIpcInterface<ISslContext>(system);
  138. }
  139. void SetInterfaceVersion(Kernel::HLERequestContext& ctx) {
  140. LOG_DEBUG(Service_SSL, "called");
  141. IPC::RequestParser rp{ctx};
  142. ssl_version = rp.Pop<u32>();
  143. IPC::ResponseBuilder rb{ctx, 2};
  144. rb.Push(ResultSuccess);
  145. }
  146. };
  147. void InstallInterfaces(SM::ServiceManager& service_manager, Core::System& system) {
  148. std::make_shared<SSL>(system)->InstallAsService(service_manager);
  149. }
  150. } // namespace Service::SSL