key_manager.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. // Copyright 2018 yuzu emulator team
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <fstream>
  5. #include <locale>
  6. #include <sstream>
  7. #include <mbedtls/sha256.h>
  8. #include "common/assert.h"
  9. #include "common/common_paths.h"
  10. #include "common/file_util.h"
  11. #include "common/logging/log.h"
  12. #include "core/crypto/key_manager.h"
  13. #include "core/settings.h"
  14. #include "key_manager.h"
  15. namespace Core::Crypto {
  16. static u8 ToHexNibble(char c1) {
  17. if (c1 >= 65 && c1 <= 70)
  18. return c1 - 55;
  19. if (c1 >= 97 && c1 <= 102)
  20. return c1 - 87;
  21. if (c1 >= 48 && c1 <= 57)
  22. return c1 - 48;
  23. throw std::logic_error("Invalid hex digit");
  24. }
  25. template <size_t Size>
  26. static std::array<u8, Size> HexStringToArray(std::string_view str) {
  27. std::array<u8, Size> out{};
  28. for (size_t i = 0; i < 2 * Size; i += 2) {
  29. auto d1 = str[i];
  30. auto d2 = str[i + 1];
  31. out[i / 2] = (ToHexNibble(d1) << 4) | ToHexNibble(d2);
  32. }
  33. return out;
  34. }
  35. std::array<u8, 16> operator""_array16(const char* str, size_t len) {
  36. if (len != 32)
  37. throw std::logic_error("Not of correct size.");
  38. return HexStringToArray<16>(str);
  39. }
  40. std::array<u8, 32> operator""_array32(const char* str, size_t len) {
  41. if (len != 64)
  42. throw std::logic_error("Not of correct size.");
  43. return HexStringToArray<32>(str);
  44. }
  45. KeyManager::KeyManager() {
  46. // Initialize keys
  47. std::string hactool_keys_dir = FileUtil::GetHactoolConfigurationPath();
  48. std::string yuzu_keys_dir = FileUtil::GetUserPath(FileUtil::UserPath::KeysDir);
  49. if (Settings::values.use_dev_keys) {
  50. dev_mode = true;
  51. AttemptLoadKeyFile(yuzu_keys_dir, hactool_keys_dir, "dev.keys", false);
  52. } else {
  53. dev_mode = false;
  54. AttemptLoadKeyFile(yuzu_keys_dir, hactool_keys_dir, "prod.keys", false);
  55. }
  56. AttemptLoadKeyFile(yuzu_keys_dir, hactool_keys_dir, "title.keys", true);
  57. }
  58. void KeyManager::LoadFromFile(std::string_view filename_, bool is_title_keys) {
  59. const auto filename = std::string(filename_);
  60. std::ifstream file(filename);
  61. if (!file.is_open())
  62. return;
  63. std::string line;
  64. while (std::getline(file, line)) {
  65. std::vector<std::string> out;
  66. std::stringstream stream(line);
  67. std::string item;
  68. while (std::getline(stream, item, '='))
  69. out.push_back(std::move(item));
  70. if (out.size() != 2)
  71. continue;
  72. out[0].erase(std::remove(out[0].begin(), out[0].end(), ' '), out[0].end());
  73. out[1].erase(std::remove(out[1].begin(), out[1].end(), ' '), out[1].end());
  74. if (is_title_keys) {
  75. auto rights_id_raw = HexStringToArray<16>(out[0]);
  76. u128 rights_id = *reinterpret_cast<std::array<u64, 2>*>(&rights_id_raw);
  77. Key128 key = HexStringToArray<16>(out[1]);
  78. SetKey(S128KeyType::Titlekey, key, rights_id[1], rights_id[0]);
  79. } else {
  80. std::transform(out[0].begin(), out[0].end(), out[0].begin(), ::tolower);
  81. if (s128_file_id.find(out[0]) != s128_file_id.end()) {
  82. const auto index = s128_file_id[out[0]];
  83. Key128 key = HexStringToArray<16>(out[1]);
  84. SetKey(index.type, key, index.field1, index.field2);
  85. } else if (s256_file_id.find(out[0]) != s256_file_id.end()) {
  86. const auto index = s256_file_id[out[0]];
  87. Key256 key = HexStringToArray<32>(out[1]);
  88. SetKey(index.type, key, index.field1, index.field2);
  89. }
  90. }
  91. }
  92. }
  93. void KeyManager::AttemptLoadKeyFile(std::string_view dir1_, std::string_view dir2_,
  94. std::string_view filename_, bool title) {
  95. std::string dir1(dir1_);
  96. std::string dir2(dir2_);
  97. std::string filename(filename_);
  98. if (FileUtil::Exists(dir1 + DIR_SEP + filename))
  99. LoadFromFile(dir1 + DIR_SEP + filename, title);
  100. else if (FileUtil::Exists(dir2 + DIR_SEP + filename))
  101. LoadFromFile(dir2 + DIR_SEP + filename, title);
  102. }
  103. bool KeyManager::HasKey(S128KeyType id, u64 field1, u64 field2) const {
  104. return s128_keys.find({id, field1, field2}) != s128_keys.end();
  105. }
  106. bool KeyManager::HasKey(S256KeyType id, u64 field1, u64 field2) const {
  107. return s256_keys.find({id, field1, field2}) != s256_keys.end();
  108. }
  109. Key128 KeyManager::GetKey(S128KeyType id, u64 field1, u64 field2) const {
  110. if (!HasKey(id, field1, field2))
  111. return {};
  112. return s128_keys.at({id, field1, field2});
  113. }
  114. Key256 KeyManager::GetKey(S256KeyType id, u64 field1, u64 field2) const {
  115. if (!HasKey(id, field1, field2))
  116. return {};
  117. return s256_keys.at({id, field1, field2});
  118. }
  119. void KeyManager::SetKey(S128KeyType id, Key128 key, u64 field1, u64 field2) {
  120. s128_keys[{id, field1, field2}] = key;
  121. }
  122. void KeyManager::SetKey(S256KeyType id, Key256 key, u64 field1, u64 field2) {
  123. s256_keys[{id, field1, field2}] = key;
  124. }
  125. std::unordered_map<std::string, KeyIndex<S128KeyType>> KeyManager::s128_file_id = {
  126. {"master_key_00", {S128KeyType::Master, 0, 0}},
  127. {"master_key_01", {S128KeyType::Master, 1, 0}},
  128. {"master_key_02", {S128KeyType::Master, 2, 0}},
  129. {"master_key_03", {S128KeyType::Master, 3, 0}},
  130. {"master_key_04", {S128KeyType::Master, 4, 0}},
  131. {"package1_key_00", {S128KeyType::Package1, 0, 0}},
  132. {"package1_key_01", {S128KeyType::Package1, 1, 0}},
  133. {"package1_key_02", {S128KeyType::Package1, 2, 0}},
  134. {"package1_key_03", {S128KeyType::Package1, 3, 0}},
  135. {"package1_key_04", {S128KeyType::Package1, 4, 0}},
  136. {"package2_key_00", {S128KeyType::Package2, 0, 0}},
  137. {"package2_key_01", {S128KeyType::Package2, 1, 0}},
  138. {"package2_key_02", {S128KeyType::Package2, 2, 0}},
  139. {"package2_key_03", {S128KeyType::Package2, 3, 0}},
  140. {"package2_key_04", {S128KeyType::Package2, 4, 0}},
  141. {"titlekek_00", {S128KeyType::Titlekek, 0, 0}},
  142. {"titlekek_01", {S128KeyType::Titlekek, 1, 0}},
  143. {"titlekek_02", {S128KeyType::Titlekek, 2, 0}},
  144. {"titlekek_03", {S128KeyType::Titlekek, 3, 0}},
  145. {"titlekek_04", {S128KeyType::Titlekek, 4, 0}},
  146. {"eticket_rsa_kek", {S128KeyType::ETicketRSAKek, 0, 0}},
  147. {"key_area_key_application_00",
  148. {S128KeyType::KeyArea, 0, static_cast<u64>(KeyAreaKeyType::Application)}},
  149. {"key_area_key_application_01",
  150. {S128KeyType::KeyArea, 1, static_cast<u64>(KeyAreaKeyType::Application)}},
  151. {"key_area_key_application_02",
  152. {S128KeyType::KeyArea, 2, static_cast<u64>(KeyAreaKeyType::Application)}},
  153. {"key_area_key_application_03",
  154. {S128KeyType::KeyArea, 3, static_cast<u64>(KeyAreaKeyType::Application)}},
  155. {"key_area_key_application_04",
  156. {S128KeyType::KeyArea, 4, static_cast<u64>(KeyAreaKeyType::Application)}},
  157. {"key_area_key_ocean_00", {S128KeyType::KeyArea, 0, static_cast<u64>(KeyAreaKeyType::Ocean)}},
  158. {"key_area_key_ocean_01", {S128KeyType::KeyArea, 1, static_cast<u64>(KeyAreaKeyType::Ocean)}},
  159. {"key_area_key_ocean_02", {S128KeyType::KeyArea, 2, static_cast<u64>(KeyAreaKeyType::Ocean)}},
  160. {"key_area_key_ocean_03", {S128KeyType::KeyArea, 3, static_cast<u64>(KeyAreaKeyType::Ocean)}},
  161. {"key_area_key_ocean_04", {S128KeyType::KeyArea, 4, static_cast<u64>(KeyAreaKeyType::Ocean)}},
  162. {"key_area_key_system_00", {S128KeyType::KeyArea, 0, static_cast<u64>(KeyAreaKeyType::System)}},
  163. {"key_area_key_system_01", {S128KeyType::KeyArea, 1, static_cast<u64>(KeyAreaKeyType::System)}},
  164. {"key_area_key_system_02", {S128KeyType::KeyArea, 2, static_cast<u64>(KeyAreaKeyType::System)}},
  165. {"key_area_key_system_03", {S128KeyType::KeyArea, 3, static_cast<u64>(KeyAreaKeyType::System)}},
  166. {"key_area_key_system_04", {S128KeyType::KeyArea, 4, static_cast<u64>(KeyAreaKeyType::System)}},
  167. };
  168. std::unordered_map<std::string, KeyIndex<S256KeyType>> KeyManager::s256_file_id = {
  169. {"header_key", {S256KeyType::Header, 0, 0}},
  170. {"sd_card_save_key", {S256KeyType::SDSave, 0, 0}},
  171. {"sd_card_nca_key", {S256KeyType::SDNCA, 0, 0}},
  172. };
  173. } // namespace Core::Crypto