summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorZach Hilman <zachhilman@gmail.com>2018-08-16 23:12:31 +0200
committerZach Hilman <zachhilman@gmail.com>2018-08-23 17:53:30 +0200
commit61a5b56abde4a87e1e66c76b506bdd4dada58389 (patch)
tree186d2efbf0577104b32379a18ea60f4c5fdf6679
parentkey_manager: Add support for KEK and SD seed derivation (diff)
downloadyuzu-61a5b56abde4a87e1e66c76b506bdd4dada58389.tar
yuzu-61a5b56abde4a87e1e66c76b506bdd4dada58389.tar.gz
yuzu-61a5b56abde4a87e1e66c76b506bdd4dada58389.tar.bz2
yuzu-61a5b56abde4a87e1e66c76b506bdd4dada58389.tar.lz
yuzu-61a5b56abde4a87e1e66c76b506bdd4dada58389.tar.xz
yuzu-61a5b56abde4a87e1e66c76b506bdd4dada58389.tar.zst
yuzu-61a5b56abde4a87e1e66c76b506bdd4dada58389.zip
-rw-r--r--src/core/crypto/key_manager.cpp46
-rw-r--r--src/core/crypto/key_manager.h2
2 files changed, 45 insertions, 3 deletions
diff --git a/src/core/crypto/key_manager.cpp b/src/core/crypto/key_manager.cpp
index e4b33f750..994ac4eec 100644
--- a/src/core/crypto/key_manager.cpp
+++ b/src/core/crypto/key_manager.cpp
@@ -153,17 +153,17 @@ void KeyManager::LoadFromFile(const std::string& filename, bool is_title_keys) {
u128 rights_id{};
std::memcpy(rights_id.data(), rights_id_raw.data(), rights_id_raw.size());
Key128 key = Common::HexStringToArray<16>(out[1]);
- SetKey(S128KeyType::Titlekey, key, rights_id[1], rights_id[0]);
+ s128_keys[{S128KeyType::Titlekey, rights_id[1], rights_id[0]}] = key;
} else {
std::transform(out[0].begin(), out[0].end(), out[0].begin(), ::tolower);
if (s128_file_id.find(out[0]) != s128_file_id.end()) {
const auto index = s128_file_id.at(out[0]);
Key128 key = Common::HexStringToArray<16>(out[1]);
- SetKey(index.type, key, index.field1, index.field2);
+ s128_keys[{index.type, index.field1, index.field2}] = key;
} else if (s256_file_id.find(out[0]) != s256_file_id.end()) {
const auto index = s256_file_id.at(out[0]);
Key256 key = Common::HexStringToArray<32>(out[1]);
- SetKey(index.type, key, index.field1, index.field2);
+ s256_keys[{index.type, index.field1, index.field2}] = key;
}
}
}
@@ -197,11 +197,51 @@ Key256 KeyManager::GetKey(S256KeyType id, u64 field1, u64 field2) const {
return s256_keys.at({id, field1, field2});
}
+template <size_t Size>
+void KeyManager::WriteKeyToFile(bool title_key, std::string_view keyname,
+ std::array<u8, Size> key) {
+ const std::string yuzu_keys_dir = FileUtil::GetUserPath(FileUtil::UserPath::KeysDir);
+ std::string filename = "title.keys_autogenerated";
+ if (!title_key)
+ filename = dev_mode ? "dev.keys_autogenerated" : "prod.keys_autogenerated";
+ const auto add_info_text = !FileUtil::Exists(yuzu_keys_dir + DIR_SEP + filename);
+ std::ofstream file(yuzu_keys_dir + DIR_SEP + filename, std::ios::app);
+ if (!file.is_open())
+ return;
+ if (add_info_text) {
+ file << "# This file is autogenerated by Yuzu" << std::endl
+ << "# It serves to store keys that were automatically generated from the normal keys"
+ << std::endl
+ << "# If you are experiencing issues involving keys, it may help to delete this file"
+ << std::endl;
+ }
+
+ file << std::endl
+ << fmt::format("{} = {}", keyname, Common::HexArrayToString(key)) << std::endl;
+ AttemptLoadKeyFile(yuzu_keys_dir, yuzu_keys_dir, filename, title_key);
+}
+
void KeyManager::SetKey(S128KeyType id, Key128 key, u64 field1, u64 field2) {
+ const auto iter = std::find_if(
+ s128_file_id.begin(), s128_file_id.end(),
+ [&id, &field1, &field2](const std::pair<std::string, KeyIndex<S128KeyType>> elem) {
+ return std::tie(elem.second.type, elem.second.field1, elem.second.field2) ==
+ std::tie(id, field1, field2);
+ });
+ if (iter != s128_file_id.end())
+ WriteKeyToFile(id == S128KeyType::Titlekey, iter->first, key);
s128_keys[{id, field1, field2}] = key;
}
void KeyManager::SetKey(S256KeyType id, Key256 key, u64 field1, u64 field2) {
+ const auto iter = std::find_if(
+ s256_file_id.begin(), s256_file_id.end(),
+ [&id, &field1, &field2](const std::pair<std::string, KeyIndex<S256KeyType>> elem) {
+ return std::tie(elem.second.type, elem.second.field1, elem.second.field2) ==
+ std::tie(id, field1, field2);
+ });
+ if (iter != s256_file_id.end())
+ WriteKeyToFile(false, iter->first, key);
s256_keys[{id, field1, field2}] = key;
}
diff --git a/src/core/crypto/key_manager.h b/src/core/crypto/key_manager.h
index 31040dc55..78d0b64e0 100644
--- a/src/core/crypto/key_manager.h
+++ b/src/core/crypto/key_manager.h
@@ -106,6 +106,8 @@ private:
void LoadFromFile(const std::string& filename, bool is_title_keys);
void AttemptLoadKeyFile(const std::string& dir1, const std::string& dir2,
const std::string& filename, bool title);
+ template <size_t Size>
+ void WriteKeyToFile(bool title_key, std::string_view keyname, std::array<u8, Size> key);
static const boost::container::flat_map<std::string, KeyIndex<S128KeyType>> s128_file_id;
static const boost::container::flat_map<std::string, KeyIndex<S256KeyType>> s256_file_id;