ssl_c.cpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <random>
  5. #include "common/common_types.h"
  6. #include "core/hle/ipc.h"
  7. #include "core/hle/service/ssl_c.h"
  8. #include "core/memory.h"
  9. namespace Service {
  10. namespace SSL {
  11. // TODO: Implement a proper CSPRNG in the future when actual security is needed
  12. static std::mt19937 rand_gen;
  13. static void Initialize(Interface* self) {
  14. u32* cmd_buff = Kernel::GetCommandBuffer();
  15. // Seed random number generator when the SSL service is initialized
  16. std::random_device rand_device;
  17. rand_gen.seed(rand_device());
  18. // Stub, return success
  19. cmd_buff[1] = RESULT_SUCCESS.raw;
  20. }
  21. static void GenerateRandomData(Interface* self) {
  22. u32* cmd_buff = Kernel::GetCommandBuffer();
  23. u32 size = cmd_buff[1];
  24. VAddr address = cmd_buff[3];
  25. // Fill the output buffer with random data.
  26. u32 data = 0;
  27. u32 i = 0;
  28. while (i < size) {
  29. if ((i % 4) == 0) {
  30. // The random number generator returns 4 bytes worth of data, so generate new random
  31. // data when i == 0 and when i is divisible by 4
  32. data = rand_gen();
  33. }
  34. if (size > 4) {
  35. // Use up the entire 4 bytes of the random data for as long as possible
  36. Memory::Write32(address + i, data);
  37. i += 4;
  38. } else if (size == 2) {
  39. Memory::Write16(address + i, static_cast<u16>(data & 0xffff));
  40. i += 2;
  41. } else {
  42. Memory::Write8(address + i, static_cast<u8>(data & 0xff));
  43. i++;
  44. }
  45. }
  46. // Stub, return success
  47. cmd_buff[1] = RESULT_SUCCESS.raw;
  48. }
  49. const Interface::FunctionInfo FunctionTable[] = {
  50. {0x00010002, Initialize, "Initialize"},
  51. {0x000200C2, nullptr, "CreateContext"},
  52. {0x00030000, nullptr, "CreateRootCertChain"},
  53. {0x00040040, nullptr, "DestroyRootCertChain"},
  54. {0x00050082, nullptr, "AddTrustedRootCA"},
  55. {0x00060080, nullptr, "RootCertChainAddDefaultCert"},
  56. {0x00070080, nullptr, "RootCertChainRemoveCert"},
  57. {0x000D0084, nullptr, "OpenClientCertContext"},
  58. {0x000E0040, nullptr, "OpenDefaultClientCertContext"},
  59. {0x000F0040, nullptr, "CloseClientCertContext"},
  60. {0x00110042, GenerateRandomData, "GenerateRandomData"},
  61. {0x00120042, nullptr, "InitializeConnectionSession"},
  62. {0x00130040, nullptr, "StartConnection"},
  63. {0x00140040, nullptr, "StartConnectionGetOut"},
  64. {0x00150082, nullptr, "Read"},
  65. {0x00160082, nullptr, "ReadPeek"},
  66. {0x00170082, nullptr, "Write"},
  67. {0x00180080, nullptr, "ContextSetRootCertChain"},
  68. {0x00190080, nullptr, "ContextSetClientCert"},
  69. {0x001B0080, nullptr, "ContextClearOpt"},
  70. {0x001C00C4, nullptr, "ContextGetProtocolCipher"},
  71. {0x001E0040, nullptr, "DestroyContext"},
  72. {0x001F0082, nullptr, "ContextInitSharedmem"},
  73. };
  74. SSL_C::SSL_C() {
  75. Register(FunctionTable);
  76. }
  77. } // namespace SSL_C
  78. } // namespace Service