Implement support for parsing/interacting with NCAs. (#942)
* fs: implement support for interacting with ncas. * spl: extend to use virtual keyslots
This commit is contained in:
@@ -17,17 +17,204 @@
|
||||
|
||||
namespace ams::spl {
|
||||
|
||||
HardwareType GetHardwareType() {
|
||||
u64 out_val = 0;
|
||||
R_ABORT_UNLESS(splGetConfig(SplConfigItem_HardwareType, &out_val));
|
||||
return static_cast<HardwareType>(out_val);
|
||||
namespace {
|
||||
|
||||
enum class InitializeMode {
|
||||
None,
|
||||
General,
|
||||
Crypto,
|
||||
Ssl,
|
||||
Es,
|
||||
Fs,
|
||||
Manu
|
||||
};
|
||||
|
||||
os::Mutex g_mutex(false);
|
||||
s32 g_initialize_count = 0;
|
||||
InitializeMode g_initialize_mode = InitializeMode::None;
|
||||
|
||||
Result AllocateAesKeySlotImpl(s32 *out) {
|
||||
return serviceDispatchOut(splCryptoGetServiceSession(), 21, *out);
|
||||
}
|
||||
|
||||
Result DeallocateAesKeySlotImpl(s32 slot) {
|
||||
return serviceDispatchIn(splCryptoGetServiceSession(), 22, slot);
|
||||
}
|
||||
|
||||
Result GetAesKeySlotAvailableEventImpl(Handle *out) {
|
||||
return serviceDispatch(splCryptoGetServiceSession(), 23,
|
||||
.out_handle_attrs = { SfOutHandleAttr_HipcCopy },
|
||||
.out_handles = out,
|
||||
);
|
||||
}
|
||||
|
||||
void GetAesKeySlotAvailableEvent(os::SystemEvent *out) {
|
||||
/* Get libnx event. */
|
||||
Handle handle = svc::InvalidHandle;
|
||||
R_ABORT_UNLESS(GetAesKeySlotAvailableEventImpl(std::addressof(handle)));
|
||||
|
||||
/* Attach to event. */
|
||||
out->AttachReadableHandle(handle, true, os::EventClearMode_ManualClear);
|
||||
}
|
||||
|
||||
template<typename F>
|
||||
Result WaitAvailableKeySlotAndExecute(F f) {
|
||||
os::SystemEvent event;
|
||||
auto is_event_initialized = false;
|
||||
while (true) {
|
||||
R_TRY_CATCH(static_cast<::ams::Result>(f())) {
|
||||
R_CATCH(spl::ResultOutOfKeyslots) {
|
||||
if (!is_event_initialized) {
|
||||
GetAesKeySlotAvailableEvent(std::addressof(event));
|
||||
is_event_initialized = true;
|
||||
}
|
||||
event.Wait();
|
||||
continue;
|
||||
}
|
||||
} R_END_TRY_CATCH;
|
||||
|
||||
return ResultSuccess();
|
||||
}
|
||||
}
|
||||
|
||||
template<typename F>
|
||||
void Initialize(InitializeMode mode, F f) {
|
||||
std::scoped_lock lk(g_mutex);
|
||||
|
||||
AMS_ASSERT(g_initialize_count >= 0);
|
||||
AMS_ABORT_UNLESS(mode != InitializeMode::None);
|
||||
|
||||
if (g_initialize_count == 0) {
|
||||
AMS_ABORT_UNLESS(g_initialize_mode == InitializeMode::None);
|
||||
f();
|
||||
g_initialize_mode = mode;
|
||||
} else {
|
||||
AMS_ABORT_UNLESS(g_initialize_mode == mode);
|
||||
}
|
||||
|
||||
++g_initialize_count;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void Initialize() {
|
||||
return Initialize(InitializeMode::General, [&]() {
|
||||
R_ABORT_UNLESS(splInitialize());
|
||||
});
|
||||
}
|
||||
|
||||
void InitializeForCrypto() {
|
||||
return Initialize(InitializeMode::Crypto, [&]() {
|
||||
R_ABORT_UNLESS(splCryptoInitialize());
|
||||
});
|
||||
}
|
||||
|
||||
void InitializeForSsl() {
|
||||
return Initialize(InitializeMode::Ssl, [&]() {
|
||||
R_ABORT_UNLESS(splSslInitialize());
|
||||
});
|
||||
}
|
||||
|
||||
void InitializeForEs() {
|
||||
return Initialize(InitializeMode::Es, [&]() {
|
||||
R_ABORT_UNLESS(splEsInitialize());
|
||||
});
|
||||
}
|
||||
|
||||
void InitializeForFs() {
|
||||
return Initialize(InitializeMode::Fs, [&]() {
|
||||
R_ABORT_UNLESS(splFsInitialize());
|
||||
});
|
||||
}
|
||||
|
||||
void InitializeForManu() {
|
||||
return Initialize(InitializeMode::Manu, [&]() {
|
||||
R_ABORT_UNLESS(splManuInitialize());
|
||||
});
|
||||
}
|
||||
|
||||
void Finalize() {
|
||||
std::scoped_lock lk(g_mutex);
|
||||
AMS_ASSERT(g_initialize_count > 0);
|
||||
AMS_ABORT_UNLESS(g_initialize_mode != InitializeMode::None);
|
||||
|
||||
if ((--g_initialize_count) == 0) {
|
||||
switch (g_initialize_mode) {
|
||||
case InitializeMode::General: splExit(); break;
|
||||
case InitializeMode::Crypto: splCryptoExit(); break;
|
||||
case InitializeMode::Ssl: splSslExit(); break;
|
||||
case InitializeMode::Es: splEsExit(); break;
|
||||
case InitializeMode::Fs: splFsExit(); break;
|
||||
case InitializeMode::Manu: splManuExit(); break;
|
||||
AMS_UNREACHABLE_DEFAULT_CASE();
|
||||
}
|
||||
g_initialize_mode = InitializeMode::None;
|
||||
}
|
||||
}
|
||||
|
||||
Result AllocateAesKeySlot(s32 *out_slot) {
|
||||
return WaitAvailableKeySlotAndExecute([&]() -> Result {
|
||||
return AllocateAesKeySlotImpl(out_slot);
|
||||
});
|
||||
}
|
||||
|
||||
Result DeallocateAesKeySlot(s32 slot) {
|
||||
return DeallocateAesKeySlotImpl(slot);
|
||||
}
|
||||
|
||||
Result GenerateAesKek(AccessKey *access_key, const void *key_source, size_t key_source_size, s32 generation, u32 option) {
|
||||
AMS_ASSERT(key_source_size == sizeof(KeySource));
|
||||
return splCryptoGenerateAesKek(key_source, generation, option, static_cast<void *>(access_key));
|
||||
}
|
||||
|
||||
Result LoadAesKey(s32 slot, const AccessKey &access_key, const void *key_source, size_t key_source_size) {
|
||||
AMS_ASSERT(key_source_size == sizeof(KeySource));
|
||||
return splCryptoLoadAesKey(std::addressof(access_key), key_source, static_cast<u32>(slot));
|
||||
}
|
||||
|
||||
Result GenerateAesKey(void *dst, size_t dst_size, const AccessKey &access_key, const void *key_source, size_t key_source_size) {
|
||||
AMS_ASSERT(dst_size >= crypto::AesEncryptor128::KeySize);
|
||||
AMS_ASSERT(key_source_size == sizeof(KeySource));
|
||||
return WaitAvailableKeySlotAndExecute([&]() -> Result {
|
||||
return splCryptoGenerateAesKey(std::addressof(access_key), key_source, dst);
|
||||
});
|
||||
}
|
||||
|
||||
Result GenerateSpecificAesKey(void *dst, size_t dst_size, const void *key_source, size_t key_source_size, s32 generation, u32 option) {
|
||||
AMS_ASSERT(dst_size >= crypto::AesEncryptor128::KeySize);
|
||||
AMS_ASSERT(key_source_size == sizeof(KeySource));
|
||||
return splFsGenerateSpecificAesKey(key_source, static_cast<u32>(generation), option, dst);
|
||||
}
|
||||
|
||||
Result ComputeCtr(void *dst, size_t dst_size, s32 slot, const void *src, size_t src_size, const void *iv, size_t iv_size) {
|
||||
AMS_ASSERT(iv_size >= 0x10);
|
||||
AMS_ASSERT(dst_size >= src_size);
|
||||
|
||||
return splCryptoCryptAesCtr(src, dst, src_size, static_cast<s32>(slot), iv);
|
||||
}
|
||||
|
||||
Result DecryptAesKey(void *dst, size_t dst_size, const void *key_source, size_t key_source_size, s32 generation, u32 option) {
|
||||
AMS_ASSERT(dst_size >= crypto::AesEncryptor128::KeySize);
|
||||
AMS_ASSERT(key_source_size == sizeof(KeySource));
|
||||
return WaitAvailableKeySlotAndExecute([&]() -> Result {
|
||||
return splCryptoDecryptAesKey(key_source, static_cast<u32>(generation), option, dst);
|
||||
});
|
||||
}
|
||||
|
||||
Result GetConfig(u64 *out, ConfigItem item) {
|
||||
return splGetConfig(static_cast<::SplConfigItem>(item), out);
|
||||
}
|
||||
|
||||
bool IsDevelopment() {
|
||||
bool is_dev;
|
||||
R_ABORT_UNLESS(splIsDevelopment(std::addressof(is_dev)));
|
||||
return is_dev;
|
||||
}
|
||||
|
||||
MemoryArrangement GetMemoryArrangement() {
|
||||
u64 arrange = 0;
|
||||
R_ABORT_UNLESS(splGetConfig(SplConfigItem_MemoryArrange, &arrange));
|
||||
arrange &= 0x3F;
|
||||
switch (arrange) {
|
||||
u64 mode = 0;
|
||||
R_ABORT_UNLESS(spl::GetConfig(std::addressof(mode), spl::ConfigItem::MemoryMode));
|
||||
switch (mode & 0x3F) {
|
||||
case 2:
|
||||
return MemoryArrangement_StandardForAppletDev;
|
||||
case 3:
|
||||
@@ -41,52 +228,55 @@ namespace ams::spl {
|
||||
}
|
||||
}
|
||||
|
||||
bool IsDisabledProgramVerification() {
|
||||
u64 val = 0;
|
||||
R_ABORT_UNLESS(splGetConfig(SplConfigItem_DisableProgramVerification, &val));
|
||||
return val != 0;
|
||||
Result SetBootReason(BootReasonValue boot_reason) {
|
||||
static_assert(sizeof(boot_reason) == sizeof(u32));
|
||||
|
||||
u32 v;
|
||||
std::memcpy(std::addressof(v), std::addressof(boot_reason), sizeof(v));
|
||||
|
||||
return splSetBootReason(v);
|
||||
}
|
||||
|
||||
bool IsDevelopmentHardware() {
|
||||
bool is_dev_hardware;
|
||||
R_ABORT_UNLESS(splIsDevelopment(&is_dev_hardware));
|
||||
return is_dev_hardware;
|
||||
Result GetBootReason(BootReasonValue *out) {
|
||||
static_assert(sizeof(*out) == sizeof(u32));
|
||||
|
||||
u32 v;
|
||||
R_TRY(splGetBootReason(std::addressof(v)));
|
||||
|
||||
std::memcpy(out, std::addressof(v), sizeof(*out));
|
||||
return ResultSuccess();
|
||||
}
|
||||
|
||||
bool IsDevelopmentFunctionEnabled() {
|
||||
u64 val = 0;
|
||||
R_ABORT_UNLESS(splGetConfig(SplConfigItem_IsDebugMode, &val));
|
||||
return val != 0;
|
||||
}
|
||||
|
||||
bool IsRecoveryBoot() {
|
||||
u64 val = 0;
|
||||
R_ABORT_UNLESS(splGetConfig(SplConfigItem_IsRecoveryBoot, &val));
|
||||
return val != 0;
|
||||
}
|
||||
|
||||
bool IsMariko() {
|
||||
const auto hw_type = GetHardwareType();
|
||||
switch (hw_type) {
|
||||
SocType GetSocType() {
|
||||
switch (GetHardwareType()) {
|
||||
case HardwareType::Icosa:
|
||||
case HardwareType::Copper:
|
||||
return false;
|
||||
return SocType_Erista;
|
||||
case HardwareType::Hoag:
|
||||
case HardwareType::Iowa:
|
||||
return true;
|
||||
case HardwareType::_Five_:
|
||||
return SocType_Mariko;
|
||||
AMS_UNREACHABLE_DEFAULT_CASE();
|
||||
}
|
||||
}
|
||||
|
||||
Result GenerateAesKek(AccessKey *access_key, const void *key_source, size_t key_source_size, u32 generation, u32 option) {
|
||||
AMS_ASSERT(key_source_size == sizeof(KeySource));
|
||||
return splCryptoGenerateAesKek(key_source, generation, option, static_cast<void *>(access_key));
|
||||
Result GetPackage2Hash(void *dst, size_t dst_size) {
|
||||
AMS_ASSERT(dst_size >= crypto::Sha256Generator::HashSize);
|
||||
return splFsGetPackage2Hash(dst);
|
||||
}
|
||||
|
||||
Result GenerateAesKey(void *dst, size_t dst_size, const AccessKey &access_key, const void *key_source, size_t key_source_size) {
|
||||
AMS_ASSERT(dst_size == crypto::AesEncryptor128::KeySize);
|
||||
AMS_ASSERT(key_source_size == sizeof(KeySource));
|
||||
return splCryptoGenerateAesKey(std::addressof(access_key), key_source, dst);
|
||||
Result GenerateRandomBytes(void *out, size_t buffer_size) {
|
||||
return splGetRandomBytes(out, buffer_size);
|
||||
}
|
||||
|
||||
Result LoadPreparedAesKey(s32 slot, const AccessKey &access_key) {
|
||||
if (g_initialize_mode == InitializeMode::Fs) {
|
||||
return splFsLoadTitlekey(std::addressof(access_key), static_cast<u32>(slot));
|
||||
} else {
|
||||
/* TODO: libnx binding not available. */
|
||||
/* return splEsLoadTitlekey(std::addressof(access_key), static_cast<u32>(slot)); */
|
||||
AMS_ABORT_UNLESS(false);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user