From 7a5eda59146306dedaf3e6f07f97a8c6898543dd Mon Sep 17 00:00:00 2001 From: Frederic L Date: Tue, 30 Oct 2018 05:03:25 +0100 Subject: global: Use std::optional instead of boost::optional (#1578) * get rid of boost::optional * Remove optional references * Use std::reference_wrapper for optional references * Fix clang format * Fix clang format part 2 * Adressed feedback * Fix clang format and MacOS build --- src/core/crypto/key_manager.cpp | 44 ++++++++++++++++++++--------------------- src/core/crypto/key_manager.h | 9 +++++---- 2 files changed, 27 insertions(+), 26 deletions(-) (limited to 'src/core/crypto') diff --git a/src/core/crypto/key_manager.cpp b/src/core/crypto/key_manager.cpp index 89ae79eb3..904afa039 100644 --- a/src/core/crypto/key_manager.cpp +++ b/src/core/crypto/key_manager.cpp @@ -141,28 +141,28 @@ Key128 DeriveKeyblobMACKey(const Key128& keyblob_key, const Key128& mac_source) return mac_key; } -boost::optional DeriveSDSeed() { +std::optional DeriveSDSeed() { const FileUtil::IOFile save_43(FileUtil::GetUserPath(FileUtil::UserPath::NANDDir) + "/system/save/8000000000000043", "rb+"); if (!save_43.IsOpen()) - return boost::none; + return {}; const FileUtil::IOFile sd_private( FileUtil::GetUserPath(FileUtil::UserPath::SDMCDir) + "/Nintendo/Contents/private", "rb+"); if (!sd_private.IsOpen()) - return boost::none; + return {}; std::array private_seed{}; if (sd_private.ReadBytes(private_seed.data(), private_seed.size()) != private_seed.size()) { - return boost::none; + return {}; } std::array buffer{}; std::size_t offset = 0; for (; offset + 0x10 < save_43.GetSize(); ++offset) { if (!save_43.Seek(offset, SEEK_SET)) { - return boost::none; + return {}; } save_43.ReadBytes(buffer.data(), buffer.size()); @@ -172,12 +172,12 @@ boost::optional DeriveSDSeed() { } if (!save_43.Seek(offset + 0x10, SEEK_SET)) { - return boost::none; + return {}; } Key128 seed{}; if (save_43.ReadBytes(seed.data(), seed.size()) != seed.size()) { - return boost::none; + return {}; } return seed; } @@ -291,26 +291,26 @@ static std::array MGF1(const std::array& seed) { } template -static boost::optional FindTicketOffset(const std::array& data) { +static std::optional FindTicketOffset(const std::array& data) { u64 offset = 0; for (size_t i = 0x20; i < data.size() - 0x10; ++i) { if (data[i] == 0x1) { offset = i + 1; break; } else if (data[i] != 0x0) { - return boost::none; + return {}; } } return offset; } -boost::optional> ParseTicket(const TicketRaw& ticket, - const RSAKeyPair<2048>& key) { +std::optional> ParseTicket(const TicketRaw& ticket, + const RSAKeyPair<2048>& key) { u32 cert_authority; std::memcpy(&cert_authority, ticket.data() + 0x140, sizeof(cert_authority)); if (cert_authority == 0) - return boost::none; + return {}; if (cert_authority != Common::MakeMagic('R', 'o', 'o', 't')) { LOG_INFO(Crypto, "Attempting to parse ticket with non-standard certificate authority {:08X}.", @@ -321,7 +321,7 @@ boost::optional> ParseTicket(const TicketRaw& ticket, std::memcpy(rights_id.data(), ticket.data() + 0x2A0, sizeof(Key128)); if (rights_id == Key128{}) - return boost::none; + return {}; Key128 key_temp{}; @@ -356,17 +356,17 @@ boost::optional> ParseTicket(const TicketRaw& ticket, std::memcpy(m_2.data(), rsa_step.data() + 0x21, m_2.size()); if (m_0 != 0) - return boost::none; + return {}; m_1 = m_1 ^ MGF1<0x20>(m_2); m_2 = m_2 ^ MGF1<0xDF>(m_1); const auto offset = FindTicketOffset(m_2); - if (offset == boost::none) - return boost::none; - ASSERT(offset.get() > 0); + if (!offset) + return {}; + ASSERT(*offset > 0); - std::memcpy(key_temp.data(), m_2.data() + offset.get(), key_temp.size()); + std::memcpy(key_temp.data(), m_2.data() + *offset, key_temp.size()); return std::make_pair(rights_id, key_temp); } @@ -661,8 +661,8 @@ void KeyManager::DeriveSDSeedLazy() { return; const auto res = DeriveSDSeed(); - if (res != boost::none) - SetKey(S128KeyType::SDSeed, res.get()); + if (res) + SetKey(S128KeyType::SDSeed, *res); } static Key128 CalculateCMAC(const u8* source, size_t size, const Key128& key) { @@ -889,9 +889,9 @@ void KeyManager::DeriveETicket(PartitionDataManager& data) { for (const auto& raw : res) { const auto pair = ParseTicket(raw, rsa_key); - if (pair == boost::none) + if (!pair) continue; - const auto& [rid, key] = pair.value(); + const auto& [rid, key] = *pair; u128 rights_id; std::memcpy(rights_id.data(), rid.data(), rid.size()); SetKey(S128KeyType::Titlekey, key, rights_id[1], rights_id[0]); diff --git a/src/core/crypto/key_manager.h b/src/core/crypto/key_manager.h index cccb3c0ae..22f268c65 100644 --- a/src/core/crypto/key_manager.h +++ b/src/core/crypto/key_manager.h @@ -6,9 +6,10 @@ #include #include +#include #include + #include -#include #include #include "common/common_types.h" #include "core/crypto/partition_data_manager.h" @@ -191,14 +192,14 @@ Key128 DeriveMasterKey(const std::array& keyblob, const Key128& master std::array DecryptKeyblob(const std::array& encrypted_keyblob, const Key128& key); -boost::optional DeriveSDSeed(); +std::optional DeriveSDSeed(); Loader::ResultStatus DeriveSDKeys(std::array& sd_keys, KeyManager& keys); std::vector GetTicketblob(const FileUtil::IOFile& ticket_save); // Returns a pair of {rights_id, titlekey}. Fails if the ticket has no certificate authority (offset // 0x140-0x144 is zero) -boost::optional> ParseTicket( - const TicketRaw& ticket, const RSAKeyPair<2048>& eticket_extended_key); +std::optional> ParseTicket(const TicketRaw& ticket, + const RSAKeyPair<2048>& eticket_extended_key); } // namespace Core::Crypto -- cgit v1.2.3