2014-10-29 18:38:33 -07:00
|
|
|
// Copyright 2014 Citra Emulator Project
|
2014-12-16 21:38:14 -08:00
|
|
|
// Licensed under GPLv2 or any later version
|
2014-10-29 18:38:33 -07:00
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2015-11-22 12:57:06 -08:00
|
|
|
#include <random>
|
2016-02-01 22:17:41 -08:00
|
|
|
#include "common/common_types.h"
|
2016-09-20 23:52:38 -07:00
|
|
|
#include "core/hle/service/ssl_c.h"
|
2014-10-29 18:38:33 -07:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Namespace SSL_C
|
|
|
|
|
|
|
|
namespace SSL_C {
|
|
|
|
|
2015-11-22 12:57:06 -08:00
|
|
|
// TODO: Implement a proper CSPRNG in the future when actual security is needed
|
|
|
|
static std::mt19937 rand_gen;
|
|
|
|
|
|
|
|
static void Initialize(Service::Interface* self) {
|
|
|
|
u32* cmd_buff = Kernel::GetCommandBuffer();
|
|
|
|
|
|
|
|
// Seed random number generator when the SSL service is initialized
|
|
|
|
std::random_device rand_device;
|
|
|
|
rand_gen.seed(rand_device());
|
|
|
|
|
|
|
|
// Stub, return success
|
|
|
|
cmd_buff[1] = RESULT_SUCCESS.raw;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void GenerateRandomData(Service::Interface* self) {
|
|
|
|
u32* cmd_buff = Kernel::GetCommandBuffer();
|
|
|
|
|
|
|
|
u32 size = cmd_buff[1];
|
|
|
|
VAddr address = cmd_buff[3];
|
|
|
|
|
|
|
|
// Fill the output buffer with random data.
|
|
|
|
u32 data = 0;
|
|
|
|
u32 i = 0;
|
|
|
|
while (i < size) {
|
|
|
|
if ((i % 4) == 0) {
|
2016-09-17 17:38:01 -07:00
|
|
|
// The random number generator returns 4 bytes worth of data, so generate new random
|
|
|
|
// data when i == 0 and when i is divisible by 4
|
2015-11-22 12:57:06 -08:00
|
|
|
data = rand_gen();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (size > 4) {
|
|
|
|
// Use up the entire 4 bytes of the random data for as long as possible
|
2016-04-16 07:24:39 -07:00
|
|
|
Memory::Write32(address + i, data);
|
2015-11-22 12:57:06 -08:00
|
|
|
i += 4;
|
|
|
|
} else if (size == 2) {
|
2016-04-16 07:24:39 -07:00
|
|
|
Memory::Write16(address + i, static_cast<u16>(data & 0xffff));
|
2015-11-22 12:57:06 -08:00
|
|
|
i += 2;
|
|
|
|
} else {
|
2016-04-16 07:24:39 -07:00
|
|
|
Memory::Write8(address + i, static_cast<u8>(data & 0xff));
|
2015-11-22 12:57:06 -08:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stub, return success
|
|
|
|
cmd_buff[1] = RESULT_SUCCESS.raw;
|
|
|
|
}
|
|
|
|
|
2014-10-29 18:38:33 -07:00
|
|
|
const Interface::FunctionInfo FunctionTable[] = {
|
2016-09-17 17:38:01 -07:00
|
|
|
{0x00010002, Initialize, "Initialize"},
|
|
|
|
{0x000200C2, nullptr, "CreateContext"},
|
|
|
|
{0x00030000, nullptr, "CreateRootCertChain"},
|
|
|
|
{0x00040040, nullptr, "DestroyRootCertChain"},
|
|
|
|
{0x00050082, nullptr, "AddTrustedRootCA"},
|
|
|
|
{0x00060080, nullptr, "RootCertChainAddDefaultCert"},
|
|
|
|
{0x00070080, nullptr, "RootCertChainRemoveCert"},
|
2016-12-07 21:08:31 -08:00
|
|
|
{0x000D0084, nullptr, "OpenClientCertContext"},
|
2016-09-17 17:38:01 -07:00
|
|
|
{0x000E0040, nullptr, "OpenDefaultClientCertContext"},
|
|
|
|
{0x000F0040, nullptr, "CloseClientCertContext"},
|
|
|
|
{0x00110042, GenerateRandomData, "GenerateRandomData"},
|
|
|
|
{0x00120042, nullptr, "InitializeConnectionSession"},
|
|
|
|
{0x00130040, nullptr, "StartConnection"},
|
|
|
|
{0x00140040, nullptr, "StartConnectionGetOut"},
|
|
|
|
{0x00150082, nullptr, "Read"},
|
2016-12-07 21:08:31 -08:00
|
|
|
{0x00160082, nullptr, "ReadPeek"},
|
2016-09-17 17:38:01 -07:00
|
|
|
{0x00170082, nullptr, "Write"},
|
|
|
|
{0x00180080, nullptr, "ContextSetRootCertChain"},
|
|
|
|
{0x00190080, nullptr, "ContextSetClientCert"},
|
|
|
|
{0x001B0080, nullptr, "ContextClearOpt"},
|
2016-12-07 21:08:31 -08:00
|
|
|
{0x001C00C4, nullptr, "ContextGetProtocolCipher"},
|
2016-09-17 17:38:01 -07:00
|
|
|
{0x001E0040, nullptr, "DestroyContext"},
|
2016-09-18 18:01:46 -07:00
|
|
|
{0x001F0082, nullptr, "ContextInitSharedmem"},
|
|
|
|
};
|
2014-10-29 18:38:33 -07:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Interface class
|
|
|
|
|
|
|
|
Interface::Interface() {
|
2015-01-30 10:56:49 -08:00
|
|
|
Register(FunctionTable);
|
2014-10-29 18:38:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|