spl: Start skeletoning spl.

This commit is contained in:
Michael Scire
2019-04-24 01:18:18 -07:00
parent f6645387b0
commit 9858d6fc95
10 changed files with 763 additions and 0 deletions

View File

@@ -0,0 +1,48 @@
/*
* Copyright (c) 2018-2019 Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <switch.h>
#include <stratosphere.hpp>
#include "spl_general_service.hpp"
Result GeneralService::GetConfig(Out<u64> out, u32 which) {
return this->secmon_wrapper->GetConfig(out.GetPointer(), static_cast<SplConfigItem>(which));
}
Result GeneralService::ExpMod(OutPointerWithClientSize<u8> out, InPointer<u8> base, InPointer<u8> exp, InPointer<u8> mod) {
return this->secmon_wrapper->ExpMod(out.pointer, out.num_elements, base.pointer, base.num_elements, exp.pointer, exp.num_elements, mod.pointer, mod.num_elements);
}
Result GeneralService::SetConfig(u32 which, u64 value) {
return this->secmon_wrapper->SetConfig(static_cast<SplConfigItem>(which), value);
}
Result GeneralService::GenerateRandomBytes(OutPointerWithClientSize<u8> out) {
return this->secmon_wrapper->GenerateRandomBytes(out.pointer, out.num_elements);
}
Result GeneralService::IsDevelopment(Out<bool> is_dev) {
return this->secmon_wrapper->IsDevelopment(is_dev.GetPointer());
}
Result GeneralService::SetBootReason(BootReasonValue boot_reason) {
return this->secmon_wrapper->SetBootReason(boot_reason);
}
Result GeneralService::GetBootReason(Out<BootReasonValue> out) {
return this->secmon_wrapper->GetBootReason(out.GetPointer());
}

View File

@@ -0,0 +1,52 @@
/*
* Copyright (c) 2018-2019 Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <switch.h>
#include <stratosphere.hpp>
#include "spl_types.hpp"
#include "spl_secmon_wrapper.hpp"
class GeneralService final : public IServiceObject {
private:
SecureMonitorWrapper *secmon_wrapper;
public:
GeneralService(SecureMonitorWrapper *sw) : secmon_wrapper(sw) {
/* ... */
}
virtual ~GeneralService() { /* ... */ }
private:
/* Actual commands. */
virtual Result GetConfig(Out<u64> out, u32 which);
virtual Result ExpMod(OutPointerWithClientSize<u8> out, InPointer<u8> base, InPointer<u8> exp, InPointer<u8> mod);
virtual Result SetConfig(u32 which, u64 value);
virtual Result GenerateRandomBytes(OutPointerWithClientSize<u8> out);
virtual Result IsDevelopment(Out<bool> is_dev);
virtual Result SetBootReason(BootReasonValue boot_reason);
virtual Result GetBootReason(Out<BootReasonValue> out);
public:
DEFINE_SERVICE_DISPATCH_TABLE {
MakeServiceCommandMeta<Spl_Cmd_GetConfig, &GeneralService::GetConfig>(),
MakeServiceCommandMeta<Spl_Cmd_ExpMod, &GeneralService::ExpMod>(),
MakeServiceCommandMeta<Spl_Cmd_SetConfig, &GeneralService::SetConfig>(),
MakeServiceCommandMeta<Spl_Cmd_GenerateRandomBytes, &GeneralService::GenerateRandomBytes>(),
MakeServiceCommandMeta<Spl_Cmd_IsDevelopment, &GeneralService::IsDevelopment>(),
MakeServiceCommandMeta<Spl_Cmd_SetBootReason, &GeneralService::SetBootReason, FirmwareVersion_300>(),
MakeServiceCommandMeta<Spl_Cmd_GetBootReason, &GeneralService::GetBootReason, FirmwareVersion_300>(),
};
};

View File

@@ -0,0 +1,110 @@
/*
* Copyright (c) 2018-2019 Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <cstdlib>
#include <cstdint>
#include <cstring>
#include <malloc.h>
#include <switch.h>
#include <stratosphere.hpp>
#include "spl_random_service.hpp"
#include "spl_general_service.hpp"
extern "C" {
extern u32 __start__;
u32 __nx_applet_type = AppletType_None;
#define INNER_HEAP_SIZE 0x28000
size_t nx_inner_heap_size = INNER_HEAP_SIZE;
char nx_inner_heap[INNER_HEAP_SIZE];
void __libnx_initheap(void);
void __appInit(void);
void __appExit(void);
/* Exception handling. */
alignas(16) u8 __nx_exception_stack[0x1000];
u64 __nx_exception_stack_size = sizeof(__nx_exception_stack);
void __libnx_exception_handler(ThreadExceptionDump *ctx);
u64 __stratosphere_title_id = TitleId_Spl;
void __libstratosphere_exception_handler(AtmosphereFatalErrorContext *ctx);
}
void __libnx_exception_handler(ThreadExceptionDump *ctx) {
StratosphereCrashHandler(ctx);
}
void __libnx_initheap(void) {
void* addr = nx_inner_heap;
size_t size = nx_inner_heap_size;
/* Newlib */
extern char* fake_heap_start;
extern char* fake_heap_end;
fake_heap_start = (char*)addr;
fake_heap_end = (char*)addr + size;
}
void __appInit(void) {
SetFirmwareVersionForLibnx();
/* SPL doesn't really access any services... */
}
void __appExit(void) {
/* SPL doesn't really access any services... */
}
struct SplServerOptions {
static constexpr size_t PointerBufferSize = 0x800;
static constexpr size_t MaxDomains = 0;
static constexpr size_t MaxDomainObjects = 0;
};
/* Single secure monitor wrapper singleton. */
static SecureMonitorWrapper s_secmon_wrapper;
/* Helpers for creating services. */
static const auto MakeRandomService = []() { return std::make_shared<RandomService>(&s_secmon_wrapper); };
static const auto MakeGeneralService = []() { return std::make_shared<GeneralService>(&s_secmon_wrapper); };
int main(int argc, char **argv)
{
consoleDebugInit(debugDevice_SVC);
/* Create server manager. */
static auto s_server_manager = WaitableManager<SplServerOptions>(1);
/* Create services. */
s_server_manager.AddWaitable(new ServiceServer<RandomService, +MakeRandomService>("csrng", 9));
if (GetRuntimeFirmwareVersion() >= FirmwareVersion_400) {
s_server_manager.AddWaitable(new ServiceServer<GeneralService, +MakeGeneralService>("spl:", 9));
/* TODO: Other services. */
} else {
/* TODO, DeprecatedGeneralService */
}
/* Loop forever, servicing our services. */
s_server_manager.Process();
return 0;
}

View File

@@ -0,0 +1,24 @@
/*
* Copyright (c) 2018-2019 Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <switch.h>
#include <stratosphere.hpp>
#include "spl_random_service.hpp"
Result RandomService::GenerateRandomBytes(OutBuffer<u8> out) {
return this->secmon_wrapper->GenerateRandomBytes(out.buffer, out.num_elements);
}

View File

@@ -0,0 +1,40 @@
/*
* Copyright (c) 2018-2019 Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <switch.h>
#include <stratosphere.hpp>
#include "spl_types.hpp"
#include "spl_secmon_wrapper.hpp"
class RandomService final : public IServiceObject {
private:
SecureMonitorWrapper *secmon_wrapper;
public:
RandomService(SecureMonitorWrapper *sw) : secmon_wrapper(sw) {
/* ... */
}
virtual ~RandomService() { /* ... */ }
private:
/* Actual commands. */
virtual Result GenerateRandomBytes(OutBuffer<u8> out);
public:
DEFINE_SERVICE_DISPATCH_TABLE {
MakeServiceCommandMeta<Spl_Cmd_GenerateRandomBytes, &RandomService::GenerateRandomBytes>(),
};
};

View File

@@ -0,0 +1,80 @@
/*
* Copyright (c) 2018-2019 Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <switch.h>
#include <stratosphere.hpp>
#include "spl_secmon_wrapper.hpp"
Result SecureMonitorWrapper::ConvertToSplResult(SmcResult result) {
if (result == SmcResult_Success) {
return ResultSuccess;
}
if (result < SmcResult_Max) {
return MAKERESULT(Module_Spl, static_cast<u32>(result));
}
return ResultSplUnknownSmcResult;
}
Result SecureMonitorWrapper::GetConfig(u64 *out, SplConfigItem which) {
/* TODO */
return ResultKernelConnectionClosed;
}
Result SecureMonitorWrapper::ExpMod(void *out, size_t out_size, const void *base, size_t base_size, const void *exp, size_t exp_size, const void *mod, size_t mod_size) {
/* TODO */
return ResultKernelConnectionClosed;
}
Result SecureMonitorWrapper::SetConfig(SplConfigItem which, u64 value) {
/* TODO */
return ResultKernelConnectionClosed;
}
Result SecureMonitorWrapper::GenerateRandomBytes(void *out, size_t size) {
/* TODO */
return ResultKernelConnectionClosed;
}
Result SecureMonitorWrapper::IsDevelopment(bool *out) {
u64 is_retail;
Result rc = this->GetConfig(&is_retail, SplConfigItem_IsRetail);
if (R_FAILED(rc)) {
return rc;
}
*out = (is_retail == 0);
return ResultSuccess;
}
Result SecureMonitorWrapper::SetBootReason(BootReasonValue boot_reason) {
if (this->IsBootReasonSet()) {
return ResultSplBootReasonAlreadySet;
}
this->boot_reason = boot_reason;
this->boot_reason_set = true;
return ResultSuccess;
}
Result SecureMonitorWrapper::GetBootReason(BootReasonValue *out) {
if (!this->IsBootReasonSet()) {
return ResultSplBootReasonNotSet;
}
*out = GetBootReason();
return ResultSuccess;
}

View File

@@ -0,0 +1,53 @@
/*
* Copyright (c) 2018-2019 Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <switch.h>
#include <stratosphere.hpp>
#include "spl_types.hpp"
class SecureMonitorWrapper {
public:
static constexpr size_t MaxAesKeyslots = 6;
static constexpr size_t MaxAesKeyslotsDeprecated = 4;
private:
uintptr_t keyslot_owners[MaxAesKeyslots] = {};
BootReasonValue boot_reason = {};
bool boot_reason_set = false;
private:
static size_t GetMaxKeyslots() {
return (GetRuntimeFirmwareVersion() >= FirmwareVersion_600) ? MaxAesKeyslots : MaxAesKeyslotsDeprecated;
}
private:
BootReasonValue GetBootReason() const {
return this->boot_reason;
}
bool IsBootReasonSet() const {
return this->boot_reason_set;
}
static Result ConvertToSplResult(SmcResult result);
public:
void Initialize();
public:
Result GetConfig(u64 *out, SplConfigItem which);
Result ExpMod(void *out, size_t out_size, const void *base, size_t base_size, const void *exp, size_t exp_size, const void *mod, size_t mod_size);
Result SetConfig(SplConfigItem which, u64 value);
Result GenerateRandomBytes(void *out, size_t size);
Result IsDevelopment(bool *out);
Result SetBootReason(BootReasonValue boot_reason);
Result GetBootReason(BootReasonValue *out);
};

View File

@@ -0,0 +1,122 @@
/*
* Copyright (c) 2018-2019 Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <cstdlib>
#include <cstdint>
#include <cstring>
#include <switch.h>
#include <stratosphere.hpp>
enum SmcResult : u32 {
SmcResult_Success = 0,
SmcResult_NotImplemented = 1,
SmcResult_InvalidArgument = 2,
SmcResult_InProgress = 3,
SmcResult_NoAsyncOperation = 4,
SmcResult_InvalidAsyncOperation = 5,
SmcResult_Blacklisted = 6,
SmcResult_Max = 99,
};
enum SmcCipherMode : u32 {
SmcCipherMode_CbcEncrypt = 0,
SmcCipherMode_CbcDecrypt = 1,
SmcCipherMode_Ctr = 2,
SmcCipherMode_Cmac = 3,
};
enum EsKeyType : u32 {
EsKeyType_TitleKey = 0,
EsKeyType_ElicenseKey = 1,
};
struct AsyncOperationKey {
u64 value;
};
struct BootReasonValue {
u8 power_intr;
u8 rtc_intr;
u8 _0x3;
u8 boot_reason;
};
static_assert(sizeof(BootReasonValue) == sizeof(u32), "BootReasonValue definition!");
struct AesKey {
u8 data[AES_128_KEY_SIZE];
};
struct IvCtr {
u8 data[AES_128_KEY_SIZE];
};
struct Cmac {
u8 data[AES_128_KEY_SIZE];
};
struct AccessKey {
u8 data[AES_128_KEY_SIZE];
};
struct KeySource {
u8 data[AES_128_KEY_SIZE];
};
enum SplServiceCmd {
/* 1.0.0+ */
Spl_Cmd_GetConfig = 0,
Spl_Cmd_ExpMod = 1,
Spl_Cmd_GenerateAesKek = 2,
Spl_Cmd_LoadAesKey = 3,
Spl_Cmd_GenerateAesKey = 4,
Spl_Cmd_SetConfig = 5,
Spl_Cmd_GenerateRandomBytes = 7,
Spl_Cmd_ImportLotusKey = 9,
Spl_Cmd_DecryptLotusMessage = 10,
Spl_Cmd_IsDevelopment = 11,
Spl_Cmd_GenerateSpecificAesKey = 12,
Spl_Cmd_DecryptRsaPrivateKey = 13,
Spl_Cmd_DecryptAesKey = 14,
Spl_Cmd_CryptAesCtr = 15,
Spl_Cmd_ComputeCmac = 16,
Spl_Cmd_ImportEsKey = 17,
Spl_Cmd_UnwrapTitleKey = 18,
Spl_Cmd_LoadTitleKey = 19,
/* 2.0.0+ */
Spl_Cmd_UnwrapCommonTitleKey = 20,
Spl_Cmd_AllocateAesKeyslot = 21,
Spl_Cmd_FreeAesKeyslot = 22,
Spl_Cmd_GetAesKeyslotEvent = 23,
/* 3.0.0+ */
Spl_Cmd_SetBootReason = 24,
Spl_Cmd_GetBootReason = 25,
/* 5.0.0+ */
Spl_Cmd_ImportSslKey = 26,
Spl_Cmd_SslExpMod = 27,
Spl_Cmd_ImportDrmKey = 28,
Spl_Cmd_DrmExpMod = 29,
Spl_Cmd_ReEncryptRsaPrivateKey = 30,
Spl_Cmd_GetPackage2Hash = 31,
/* 6.0.0+ */
Spl_Cmd_UnwrapElicenseKey = 31, /* re-used command id :( */
Spl_Cmd_LoadElicenseKey = 32,
};