summaryrefslogtreecommitdiff
path: root/src/core/crypto/key_manager.cpp
diff options
context:
space:
mode:
authorGravatar bunnei2018-08-04 14:33:11 -0400
committerGravatar GitHub2018-08-04 14:33:11 -0400
commit2b06301dbfbfe79687219bf7783a6d1b47695401 (patch)
tree222cc27ecbc7f7e86d2edef8d36436600dee7d7a /src/core/crypto/key_manager.cpp
parentMerge pull request #919 from lioncash/sign (diff)
parentAdd missing parameter to files.push_back() (diff)
downloadyuzu-2b06301dbfbfe79687219bf7783a6d1b47695401.tar.gz
yuzu-2b06301dbfbfe79687219bf7783a6d1b47695401.tar.xz
yuzu-2b06301dbfbfe79687219bf7783a6d1b47695401.zip
Merge pull request #849 from DarkLordZach/xci
XCI and Encrypted NCA Support
Diffstat (limited to 'src/core/crypto/key_manager.cpp')
-rw-r--r--src/core/crypto/key_manager.cpp215
1 files changed, 215 insertions, 0 deletions
diff --git a/src/core/crypto/key_manager.cpp b/src/core/crypto/key_manager.cpp
new file mode 100644
index 000000000..678ac5752
--- /dev/null
+++ b/src/core/crypto/key_manager.cpp
@@ -0,0 +1,215 @@
1// Copyright 2018 yuzu emulator team
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#include <array>
6#include <fstream>
7#include <locale>
8#include <sstream>
9#include <string_view>
10#include <mbedtls/sha256.h>
11#include "common/assert.h"
12#include "common/common_paths.h"
13#include "common/file_util.h"
14#include "common/logging/log.h"
15#include "core/crypto/key_manager.h"
16#include "core/settings.h"
17#include "key_manager.h"
18
19namespace Core::Crypto {
20
21static u8 ToHexNibble(char c1) {
22 if (c1 >= 65 && c1 <= 70)
23 return c1 - 55;
24 if (c1 >= 97 && c1 <= 102)
25 return c1 - 87;
26 if (c1 >= 48 && c1 <= 57)
27 return c1 - 48;
28 throw std::logic_error("Invalid hex digit");
29}
30
31template <size_t Size>
32static std::array<u8, Size> HexStringToArray(std::string_view str) {
33 std::array<u8, Size> out{};
34 for (size_t i = 0; i < 2 * Size; i += 2) {
35 auto d1 = str[i];
36 auto d2 = str[i + 1];
37 out[i / 2] = (ToHexNibble(d1) << 4) | ToHexNibble(d2);
38 }
39 return out;
40}
41
42std::array<u8, 16> operator""_array16(const char* str, size_t len) {
43 if (len != 32)
44 throw std::logic_error("Not of correct size.");
45 return HexStringToArray<16>(str);
46}
47
48std::array<u8, 32> operator""_array32(const char* str, size_t len) {
49 if (len != 64)
50 throw std::logic_error("Not of correct size.");
51 return HexStringToArray<32>(str);
52}
53
54KeyManager::KeyManager() {
55 // Initialize keys
56 const std::string hactool_keys_dir = FileUtil::GetHactoolConfigurationPath();
57 const std::string yuzu_keys_dir = FileUtil::GetUserPath(FileUtil::UserPath::KeysDir);
58 if (Settings::values.use_dev_keys) {
59 dev_mode = true;
60 AttemptLoadKeyFile(yuzu_keys_dir, hactool_keys_dir, "dev.keys", false);
61 } else {
62 dev_mode = false;
63 AttemptLoadKeyFile(yuzu_keys_dir, hactool_keys_dir, "prod.keys", false);
64 }
65
66 AttemptLoadKeyFile(yuzu_keys_dir, hactool_keys_dir, "title.keys", true);
67}
68
69void KeyManager::LoadFromFile(std::string_view filename_, bool is_title_keys) {
70 const auto filename = std::string(filename_);
71 std::ifstream file(filename);
72 if (!file.is_open())
73 return;
74
75 std::string line;
76 while (std::getline(file, line)) {
77 std::vector<std::string> out;
78 std::stringstream stream(line);
79 std::string item;
80 while (std::getline(stream, item, '='))
81 out.push_back(std::move(item));
82
83 if (out.size() != 2)
84 continue;
85
86 out[0].erase(std::remove(out[0].begin(), out[0].end(), ' '), out[0].end());
87 out[1].erase(std::remove(out[1].begin(), out[1].end(), ' '), out[1].end());
88
89 if (is_title_keys) {
90 auto rights_id_raw = HexStringToArray<16>(out[0]);
91 u128 rights_id{};
92 std::memcpy(rights_id.data(), rights_id_raw.data(), rights_id_raw.size());
93 Key128 key = HexStringToArray<16>(out[1]);
94 SetKey(S128KeyType::Titlekey, key, rights_id[1], rights_id[0]);
95 } else {
96 std::transform(out[0].begin(), out[0].end(), out[0].begin(), ::tolower);
97 if (s128_file_id.find(out[0]) != s128_file_id.end()) {
98 const auto index = s128_file_id.at(out[0]);
99 Key128 key = HexStringToArray<16>(out[1]);
100 SetKey(index.type, key, index.field1, index.field2);
101 } else if (s256_file_id.find(out[0]) != s256_file_id.end()) {
102 const auto index = s256_file_id.at(out[0]);
103 Key256 key = HexStringToArray<32>(out[1]);
104 SetKey(index.type, key, index.field1, index.field2);
105 }
106 }
107 }
108}
109
110void KeyManager::AttemptLoadKeyFile(std::string_view dir1_, std::string_view dir2_,
111 std::string_view filename_, bool title) {
112 const std::string dir1(dir1_);
113 const std::string dir2(dir2_);
114 const std::string filename(filename_);
115 if (FileUtil::Exists(dir1 + DIR_SEP + filename))
116 LoadFromFile(dir1 + DIR_SEP + filename, title);
117 else if (FileUtil::Exists(dir2 + DIR_SEP + filename))
118 LoadFromFile(dir2 + DIR_SEP + filename, title);
119}
120
121bool KeyManager::HasKey(S128KeyType id, u64 field1, u64 field2) const {
122 return s128_keys.find({id, field1, field2}) != s128_keys.end();
123}
124
125bool KeyManager::HasKey(S256KeyType id, u64 field1, u64 field2) const {
126 return s256_keys.find({id, field1, field2}) != s256_keys.end();
127}
128
129Key128 KeyManager::GetKey(S128KeyType id, u64 field1, u64 field2) const {
130 if (!HasKey(id, field1, field2))
131 return {};
132 return s128_keys.at({id, field1, field2});
133}
134
135Key256 KeyManager::GetKey(S256KeyType id, u64 field1, u64 field2) const {
136 if (!HasKey(id, field1, field2))
137 return {};
138 return s256_keys.at({id, field1, field2});
139}
140
141void KeyManager::SetKey(S128KeyType id, Key128 key, u64 field1, u64 field2) {
142 s128_keys[{id, field1, field2}] = key;
143}
144
145void KeyManager::SetKey(S256KeyType id, Key256 key, u64 field1, u64 field2) {
146 s256_keys[{id, field1, field2}] = key;
147}
148
149bool KeyManager::KeyFileExists(bool title) {
150 const std::string hactool_keys_dir = FileUtil::GetHactoolConfigurationPath();
151 const std::string yuzu_keys_dir = FileUtil::GetUserPath(FileUtil::UserPath::KeysDir);
152 if (title) {
153 return FileUtil::Exists(hactool_keys_dir + DIR_SEP + "title.keys") ||
154 FileUtil::Exists(yuzu_keys_dir + DIR_SEP + "title.keys");
155 }
156
157 if (Settings::values.use_dev_keys) {
158 return FileUtil::Exists(hactool_keys_dir + DIR_SEP + "dev.keys") ||
159 FileUtil::Exists(yuzu_keys_dir + DIR_SEP + "dev.keys");
160 }
161
162 return FileUtil::Exists(hactool_keys_dir + DIR_SEP + "prod.keys") ||
163 FileUtil::Exists(yuzu_keys_dir + DIR_SEP + "prod.keys");
164}
165
166const std::unordered_map<std::string, KeyIndex<S128KeyType>> KeyManager::s128_file_id = {
167 {"master_key_00", {S128KeyType::Master, 0, 0}},
168 {"master_key_01", {S128KeyType::Master, 1, 0}},
169 {"master_key_02", {S128KeyType::Master, 2, 0}},
170 {"master_key_03", {S128KeyType::Master, 3, 0}},
171 {"master_key_04", {S128KeyType::Master, 4, 0}},
172 {"package1_key_00", {S128KeyType::Package1, 0, 0}},
173 {"package1_key_01", {S128KeyType::Package1, 1, 0}},
174 {"package1_key_02", {S128KeyType::Package1, 2, 0}},
175 {"package1_key_03", {S128KeyType::Package1, 3, 0}},
176 {"package1_key_04", {S128KeyType::Package1, 4, 0}},
177 {"package2_key_00", {S128KeyType::Package2, 0, 0}},
178 {"package2_key_01", {S128KeyType::Package2, 1, 0}},
179 {"package2_key_02", {S128KeyType::Package2, 2, 0}},
180 {"package2_key_03", {S128KeyType::Package2, 3, 0}},
181 {"package2_key_04", {S128KeyType::Package2, 4, 0}},
182 {"titlekek_00", {S128KeyType::Titlekek, 0, 0}},
183 {"titlekek_01", {S128KeyType::Titlekek, 1, 0}},
184 {"titlekek_02", {S128KeyType::Titlekek, 2, 0}},
185 {"titlekek_03", {S128KeyType::Titlekek, 3, 0}},
186 {"titlekek_04", {S128KeyType::Titlekek, 4, 0}},
187 {"eticket_rsa_kek", {S128KeyType::ETicketRSAKek, 0, 0}},
188 {"key_area_key_application_00",
189 {S128KeyType::KeyArea, 0, static_cast<u64>(KeyAreaKeyType::Application)}},
190 {"key_area_key_application_01",
191 {S128KeyType::KeyArea, 1, static_cast<u64>(KeyAreaKeyType::Application)}},
192 {"key_area_key_application_02",
193 {S128KeyType::KeyArea, 2, static_cast<u64>(KeyAreaKeyType::Application)}},
194 {"key_area_key_application_03",
195 {S128KeyType::KeyArea, 3, static_cast<u64>(KeyAreaKeyType::Application)}},
196 {"key_area_key_application_04",
197 {S128KeyType::KeyArea, 4, static_cast<u64>(KeyAreaKeyType::Application)}},
198 {"key_area_key_ocean_00", {S128KeyType::KeyArea, 0, static_cast<u64>(KeyAreaKeyType::Ocean)}},
199 {"key_area_key_ocean_01", {S128KeyType::KeyArea, 1, static_cast<u64>(KeyAreaKeyType::Ocean)}},
200 {"key_area_key_ocean_02", {S128KeyType::KeyArea, 2, static_cast<u64>(KeyAreaKeyType::Ocean)}},
201 {"key_area_key_ocean_03", {S128KeyType::KeyArea, 3, static_cast<u64>(KeyAreaKeyType::Ocean)}},
202 {"key_area_key_ocean_04", {S128KeyType::KeyArea, 4, static_cast<u64>(KeyAreaKeyType::Ocean)}},
203 {"key_area_key_system_00", {S128KeyType::KeyArea, 0, static_cast<u64>(KeyAreaKeyType::System)}},
204 {"key_area_key_system_01", {S128KeyType::KeyArea, 1, static_cast<u64>(KeyAreaKeyType::System)}},
205 {"key_area_key_system_02", {S128KeyType::KeyArea, 2, static_cast<u64>(KeyAreaKeyType::System)}},
206 {"key_area_key_system_03", {S128KeyType::KeyArea, 3, static_cast<u64>(KeyAreaKeyType::System)}},
207 {"key_area_key_system_04", {S128KeyType::KeyArea, 4, static_cast<u64>(KeyAreaKeyType::System)}},
208};
209
210const std::unordered_map<std::string, KeyIndex<S256KeyType>> KeyManager::s256_file_id = {
211 {"header_key", {S256KeyType::Header, 0, 0}},
212 {"sd_card_save_key", {S256KeyType::SDSave, 0, 0}},
213 {"sd_card_nca_key", {S256KeyType::SDNCA, 0, 0}},
214};
215} // namespace Core::Crypto