[Crypto] Slightly more robust error handling

This commit is contained in:
wheremyfoodat 2023-06-27 01:12:17 +03:00
parent 0494ca0064
commit 3cf8427670
5 changed files with 41 additions and 34 deletions

View file

@ -9,13 +9,12 @@ namespace Crypto {
std::ifstream file(path, std::ios::in);
if (file.fail()) {
Helpers::warn("keys: Couldn't read key file: %s", path.c_str());
Helpers::warn("Keys: Couldn't read key file: %s", path.c_str());
return;
}
while (!file.eof()) {
std::string line;
std::getline(file, line);
// Skip obvious invalid lines
@ -24,9 +23,8 @@ namespace Crypto {
}
const auto parts = Helpers::split(line, '=');
if (parts.size() != 2) {
Helpers::warn("keys: Failed to parse %s", line.c_str());
Helpers::warn("Keys: Failed to parse %s", line.c_str());
continue;
}
@ -37,16 +35,15 @@ namespace Crypto {
char keyType;
bool is_generator = name == "generator";
if (!is_generator && std::sscanf(name.c_str(), "slot0x%zXKey%c", &slotId, &keyType) != 2) {
Helpers::warn("keys: Ignoring unknown key %s", name.c_str());
Helpers::warn("Keys: Ignoring unknown key %s", name.c_str());
continue;
}
auto key = createKeyFromHex(rawKeyHex);
if (!key.has_value()) {
Helpers::warn("keys: Failed to parse raw key %s", rawKeyHex.c_str());
Helpers::warn("Keys: Failed to parse raw key %s", rawKeyHex.c_str());
continue;
}
@ -56,7 +53,7 @@ namespace Crypto {
}
if (slotId >= AesKeySlotCount) {
Helpers::warn("keys: Invalid key slot id %u", slotId);
Helpers::warn("Keys: Invalid key slot id %u", slotId);
continue;
}
@ -71,7 +68,7 @@ namespace Crypto {
setNormalKey(slotId, key.value());
break;
default:
Helpers::warn("keys: Invalid key type %c", keyType);
Helpers::warn("Keys: Invalid key type %c", keyType);
break;
}
}
@ -80,5 +77,7 @@ namespace Crypto {
for (std::size_t i = 0; i < AesKeySlotCount; i++) {
updateNormalKey(i);
}
keysLoaded = true;
}
};

View file

@ -54,6 +54,14 @@ bool NCCH::loadFromHeader(Crypto::AESEngine &aesEngine, IOFile& file, const FSIn
romFS.hashRegionSize = u64(*(u32*)&header[0x1B8]) * mediaUnit;
if (encrypted) {
if (!aesEngine.haveKeys()) {
Helpers::panic(
"Loaded an encrypted ROM but AES keys don't seem to have been provided correctly! Navigate to the emulator's\n"
"app data folder and make sure you have a sysdata directory with a file called aes_keys.txt which contains your keys!"
);
return false;
}
Crypto::AESKey primaryKeyY;
Crypto::AESKey secondaryKeyY;
std::memcpy(primaryKeyY.data(), header, primaryKeyY.size());

View file

@ -137,15 +137,17 @@ bool Emulator::loadROM(const std::filesystem::path& path) {
char* appData = SDL_GetPrefPath(nullptr, "Alber");
const std::filesystem::path appDataPath = std::filesystem::path(appData);
const std::filesystem::path dataPath = appDataPath / path.filename().stem();
const std::filesystem::path aesKeysPath = appDataPath / "sysdata" / "aes_keys.txt";
const std::filesystem::path aesKeysPath = appDataPath / "sysdata" / "aes_keys.txt";
IOFile::setAppDataDir(dataPath);
SDL_free(appData);
if (std::filesystem::exists(aesKeysPath)) {
// Open the text file containing our AES keys if it exists. We use the std::filesystem::exists overload that takes an error code param to
// avoid the call throwing exceptions
std::error_code ec;
if (std::filesystem::exists(aesKeysPath, ec) && !ec) {
aesEngine.loadKeys(aesKeysPath);
}
SDL_free(appData);
kernel.initializeFS();
auto extension = path.extension();

View file

@ -5,7 +5,7 @@ int main (int argc, char *argv[]) {
emu.initGraphicsContext();
auto romPath = std::filesystem::current_path() / (argc > 1 ? argv[1] : "Metroid Prime - Federation Force (Europe) (En,Fr,De,Es,It).3ds");
auto romPath = std::filesystem::current_path() / (argc > 1 ? argv[1] : "OoT Demo Encrypted.3ds");
if (!emu.loadROM(romPath)) {
// For some reason just .c_str() doesn't show the proper path
Helpers::panic("Failed to load ROM file: %s", romPath.string().c_str());