ssl.cpp 5.9 KB

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